9 # include <X11/extensions/Xsp.h>
20 #define DIE(format, ...) do { \
21 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
22 fprintf(stderr, format "\n", ## __VA_ARGS__); \
26 /* Helper functions */
28 static void centerRectangle(SDL_Rect& result, int areaW, int areaH, int w, int h)
30 result.x = areaW / 2 - w / 2;
32 result.y = areaH / 2 - h / 2;
36 /* Base scaler for stupid scalers */
37 /** Does nothing but center the image */
38 class DummyScaler : public Scaler
40 SDL_Surface * m_screen;
44 DummyScaler(SDL_Surface* screen, int w, int h)
47 centerRectangle(m_area, GUI.Width, GUI.Height, w, h);
56 class Factory : public ScalerFactory
58 const char * getName() const
63 bool canEnable(int bpp, int w, int h) const
68 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
70 return new DummyScaler(screen, w, h);
74 static const Factory factory;
76 virtual const char * getName() const
81 virtual uint8* getDrawBuffer() const
83 const int Bpp = screen->format->BitsPerPixel / 8;
84 const int pitch = screen->pitch;
85 return ((uint8*) screen->pixels)
90 virtual unsigned int getDrawBufferPitch() const
95 virtual void getRenderedGUIArea(unsigned short & x, unsigned short & y,
96 unsigned short & w, unsigned short & h) const
98 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
101 virtual void getRatio(float & x, float & y) const
106 virtual void prepare() { };
108 virtual void finish()
110 SDL_UpdateRects(m_screen, 1, &m_area);
113 virtual void pause() { };
114 virtual void resume() { };
116 virtual bool filter(const SDL_Event& event) { return false; };
118 const DummyScaler::Factory DummyScaler::factory;
120 /* Basic and slow software scaler */
122 class SWScaler : public Scaler
124 SDL_Surface * m_screen;
127 const int m_w, m_h, m_Bpp;
130 SWScaler(SDL_Surface* screen, int w, int h)
131 : m_screen(screen), m_w(w), m_h(h),
132 m_Bpp(m_screen->format->BitsPerPixel / 8)
134 centerRectangle(m_area, GUI.Width, GUI.Height, w * 2, h * 2);
135 m_surface = reinterpret_cast<uint8*>(malloc(w * h * m_Bpp));
143 class Factory : public ScalerFactory
145 const char * getName() const
150 bool canEnable(int bpp, int w, int h) const
152 return w * 2 < GUI.Width && h * 2 < GUI.Height;
155 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
157 return new SWScaler(screen, w, h);
161 static const Factory factory;
163 virtual const char * getName() const
165 return "software 2x scaling";
168 uint8* getDrawBuffer() const
173 unsigned int getDrawBufferPitch() const
178 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
179 unsigned short & w, unsigned short & h) const
181 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
184 void getRatio(float & x, float & y) const
193 uint16 * src = reinterpret_cast<uint16*>(m_surface);
194 uint16 * dst = reinterpret_cast<uint16*>(
195 ((uint8*) m_screen->pixels)
197 + (m_area.y * m_screen->pitch));
198 const int src_pitch = m_w;
199 const int dst_pitch = m_screen->pitch / m_Bpp;
202 for (y = 0; y < m_h*2; y++) {
203 for (x = 0; x < m_w*2; x+=2) {
205 dst[x + 1] = src[x/2];
208 if (y&1) src += src_pitch;
211 SDL_UpdateRects(m_screen, 1, &m_area);
217 bool filter(const SDL_Event& event) { return false; };
219 const SWScaler::Factory SWScaler::factory;
221 /* Platform specific scalers */
224 class ARMScaler : public Scaler
226 SDL_Surface * m_screen;
229 const int m_w, m_h, m_Bpp;
232 ARMScaler(SDL_Surface* screen, int w, int h)
233 : m_screen(screen), m_w(w), m_h(h),
234 m_Bpp(m_screen->format->BitsPerPixel / 8)
236 centerRectangle(m_area, GUI.Width, GUI.Height, w * 2, h * 2);
237 m_surface = reinterpret_cast<uint8*>(malloc(w * h * m_Bpp));
245 class Factory : public ScalerFactory
247 const char * getName() const
252 bool canEnable(int bpp, int w, int h) const
254 return bpp == 16 && w * 2 < GUI.Width && h * 2 < GUI.Height &&
255 w % 16 == 0 /* asm assumes w div by 16 */;
258 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
260 return new ARMScaler(screen, w, h);
264 static const Factory factory;
266 virtual const char * getName() const
268 return "software ARM 2x scaling";
271 uint8* getDrawBuffer() const
276 unsigned int getDrawBufferPitch() const
281 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
282 unsigned short & w, unsigned short & h) const
284 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
287 void getRatio(float & x, float & y) const
296 uint16 * src = reinterpret_cast<uint16*>(m_surface);
297 uint16 * dst = reinterpret_cast<uint16*>(
298 ((uint8*) m_screen->pixels)
300 + (m_area.y * m_screen->pitch));
301 const int src_pitch = m_w;
302 const int dst_pitch = m_screen->pitch / m_Bpp;
305 for (y = 0; y < m_h*2; y++) {
308 "mov r0, %0; mov r1, %1; mov r2, %2;"
309 "stmdb r13!,{r4,r5,r6,r7,r8,r9,r10,r11,r12,r14};"
310 "1: ldmia r1!,{r3,r4,r5,r6,r7,r8,r9,r10};"
311 "mov r14,r5,lsr #16;"
312 "mov r12,r5,lsl #16;"
313 "orr r14,r14,r14,lsl #16;"
314 "orr r12,r12,r12,lsr #16;"
315 "mov r11,r4,lsr #16;"
317 "orr r11,r11,r11,lsl #16;"
318 "orr r5,r5,r5,lsr #16;"
321 "orr r4,r4,r4,lsl #16;"
322 "orr r3,r3,r3,lsr #16;"
323 "stmia r0!,{r3,r4,r5,r11,r12,r14};"
326 "orr r3,r3,r3,lsr #16;"
327 "orr r4,r4,r4,lsl #16;"
330 "orr r5,r5,r5,lsr #16;"
331 "orr r6,r6,r6,lsl #16;"
334 "orr r7,r7,r7,lsr #16;"
335 "orr r8,r8,r8,lsl #16;"
336 "mov r12,r10,lsr #16;"
337 "mov r11,r10,lsl #16;"
338 "orr r12,r12,r12,lsl #16;"
339 "orr r11,r11,r11,lsr #16;"
340 "mov r10,r9,lsr #16;"
342 "orr r10,r10,r10,lsl #16;"
343 "orr r9,r9,r9,lsr #16;"
344 "stmia r0!,{r3,r4,r5,r6,r7,r8,r9,r10,r11,r12};"
347 "ldmia r13!,{r4,r5,r6,r7,r8,r9,r10,r11,r12,r14};"
349 : "r" (dst), "r" (src), "r" (m_w)
350 : "r0", "r1", "r2", "r3"
353 if (y&1) src += src_pitch;
356 SDL_UpdateRects(m_screen, 1, &m_area);
361 bool filter(const SDL_Event& event) { return false; };
363 const ARMScaler::Factory ARMScaler::factory;
367 class HAAScalerBase : public Scaler
369 SDL_Surface *m_screen;
372 const int m_w, m_h, m_Bpp;
373 const float ratio_x, ratio_y;
376 HAAScalerBase(SDL_Surface* screen, int w, int h, float r_x, float r_y)
377 : m_screen(screen), m_w(w), m_h(h),
378 m_Bpp(m_screen->format->BitsPerPixel / 8),
379 ratio_x(r_x), ratio_y(r_y)
381 centerRectangle(m_area, GUI.Width, GUI.Height, w * r_x, h * r_y);
383 // Clear the SDL screen with black, just in case it gets drawn.
384 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
386 HAA_Init(m_screen->flags & SDL_FULLSCREEN);
387 actor = HAA_CreateActor(0, m_w, m_h, m_screen->format->BitsPerPixel);
388 HAA_SetPosition(actor, m_area.x, m_area.y + 60);
389 HAA_SetScale(actor, r_x, r_y);
394 virtual ~HAAScalerBase()
396 HAA_FreeActor(actor);
400 uint8* getDrawBuffer() const
402 return reinterpret_cast<uint8*>(actor->surface->pixels);
405 unsigned int getDrawBufferPitch() const
407 return actor->surface->pitch;
410 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
411 unsigned short & w, unsigned short & h) const
413 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
416 void getRatio(float & x, float & y) const
418 x = ratio_x; y = ratio_y;
434 bool filter(const SDL_Event& event)
436 return HAA_FilterEvent(&event) == 0;
440 class HAAFillScaler : public HAAScalerBase
442 HAAFillScaler(SDL_Surface* screen, int w, int h)
443 : HAAScalerBase(screen, w, h,
444 GUI.Width / (float)w, GUI.Height / (float)h)
449 class Factory : public ScalerFactory
451 const char * getName() const
456 bool canEnable(int bpp, int w, int h) const
461 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
463 return new HAAFillScaler(screen, w, h-20);
467 static const Factory factory;
469 const char * getName() const
471 return "HAA fill screen scaling";
474 const HAAFillScaler::Factory HAAFillScaler::factory;
476 class HAASquareScaler : public HAAScalerBase
478 HAASquareScaler(SDL_Surface* screen, int w, int h, float ratio)
479 : HAAScalerBase(screen, w, h, ratio, ratio)
484 class Factory : public ScalerFactory
486 const char * getName() const
491 bool canEnable(int bpp, int w, int h) const
496 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
498 return new HAASquareScaler(screen, w, h,
499 fminf(GUI.Width / (float)w, GUI.Height / (float)h));
503 static const Factory factory;
505 const char * getName() const
507 return "HAA square screen scaling";
510 const HAASquareScaler::Factory HAASquareScaler::factory;
515 class XSPScaler : public Scaler
517 SDL_Surface* m_screen;
519 SDL_Rect m_real_area;
520 bool m_should_enable, m_enabled; // Try to avoid flicker.
522 static void setDoubling(bool enable)
524 SDL_SysWMinfo wminfo;
525 SDL_VERSION(&wminfo.version);
526 if ( SDL_GetWMInfo(&wminfo) ) {
527 Display *dpy = wminfo.info.x11.display;
528 XSPSetPixelDoubling(dpy, 0, enable ? 1 : 0);
533 XSPScaler(SDL_Surface* screen, int w, int h)
534 : m_screen(screen), m_should_enable(true), m_enabled(false)
536 centerRectangle(m_area, GUI.Width, GUI.Height,
539 m_real_area.x = m_area.x;
540 m_real_area.y = m_area.y;
541 m_real_area.w = m_area.w / 2;
542 m_real_area.h = m_area.h / 2;
547 if (m_enabled) setDoubling(false);
550 class Factory : public ScalerFactory
552 const char * getName() const
557 bool canEnable(int bpp, int w, int h) const
559 return w * 2 < GUI.Width && h * 2 < GUI.Height;
562 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
564 return new XSPScaler(screen, w, h);
568 static const Factory factory;
570 const char * getName() const
572 return "XSP pixel doubling";
575 uint8* getDrawBuffer() const
577 const int Bpp = screen->format->BitsPerPixel / 8;
578 const int pitch = screen->pitch;
579 return ((uint8*) screen->pixels)
581 + (m_area.y * pitch);
584 unsigned int getDrawBufferPitch() const
586 return screen->pitch;
589 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
590 unsigned short & w, unsigned short & h) const
592 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
595 void getRatio(float & x, float & y) const
602 if (m_should_enable && !m_enabled) {
610 SDL_UpdateRects(m_screen, 1, &m_real_area);
615 m_should_enable = false;
624 m_should_enable = true; // Will enable later
627 bool filter(const SDL_Event& event)
629 if (event.type == SDL_ACTIVEEVENT) {
630 if (scaler && (event.active.state & SDL_APPINPUTFOCUS)) {
631 if (event.active.gain)
643 const XSPScaler::Factory XSPScaler::factory;
646 static const ScalerFactory* scalers[] = {
647 /* More useful scalers come first */
649 &XSPScaler::factory, /* n8x0 pixel doubling */
652 &ARMScaler::factory, /* arm 2x scaling */
655 &HAASquareScaler::factory, /* n900 animation actor scaling */
657 &SWScaler::factory, /* soft 2x scaling */
658 &DummyScaler::factory, /* failsafe */
659 /* The following scalers will not be automatically enabled, no matter what */
661 &HAAFillScaler::factory,
665 /* Entry point functions */
667 const ScalerFactory* searchForScaler(int bpp, int w, int h)
669 const int n = sizeof(scalers) / sizeof(ScalerFactory*);
672 if (Config.scaler && strcasecmp(Config.scaler, "help") == 0 ) {
674 printf("Scalers list:\n");
675 for (i = 0; i < n; i++) {
676 printf(" %s\n", scalers[i]->getName());
678 DIE("End of scalers list");
679 } else if (Config.scaler && strcasecmp(Config.scaler, "auto") != 0 ) {
680 // We prefer a specific scaler
681 for (i = 0; i < n; i++) {
682 if (strcasecmp(scalers[i]->getName(), Config.scaler) == 0) {
683 if (scalers[i]->canEnable(bpp, w, h)) {
684 // Found the scaler selected by the user, and we can use it.
688 "Selected scaler '%s' cannot be enabled in this mode\n",
690 break; // Fallback to another scaler.
695 fprintf(stderr, "Selected scaler '%s' does not exist\n",
700 // Just try them all now, in a buildtime set priority.
701 for (i = 0; i < n; i++) {
702 if (scalers[i]->canEnable(bpp, w, h)) {
707 DIE("Can't use any scaler; this shouldn't happen.");