9 # include <SDL_syswm.h>
10 # 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;
375 static bool initialized;
378 HAAScalerBase(SDL_Surface* screen, int w, int h, float r_x, float r_y)
379 : m_screen(screen), m_w(w), m_h(h),
380 m_Bpp(m_screen->format->BitsPerPixel / 8),
381 ratio_x(r_x), ratio_y(r_y)
383 const bool fullscreen = m_screen->flags & SDL_FULLSCREEN;
384 centerRectangle(m_area, GUI.Width, GUI.Height, w * r_x, h * r_y);
386 // Clear the SDL screen with black, just in case it gets drawn.
387 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
393 HAA_SetVideoMode(); // Tell HAA we might have changed video mode
396 actor = HAA_CreateActor(0, m_w, m_h, m_screen->format->BitsPerPixel);
397 HAA_SetPosition(actor, m_area.x, m_area.y + (fullscreen ? 0 : 60));
398 // In windowed mode, take care of the title bar (xoffset = 60)
399 HAA_SetScale(actor, r_x, r_y);
404 virtual ~HAAScalerBase()
406 HAA_FreeActor(actor);
409 uint8* getDrawBuffer() const
411 return reinterpret_cast<uint8*>(actor->surface->pixels);
414 unsigned int getDrawBufferPitch() const
416 return actor->surface->pitch;
419 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
420 unsigned short & w, unsigned short & h) const
422 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
425 void getRatio(float & x, float & y) const
427 x = ratio_x; y = ratio_y;
443 bool filter(const SDL_Event& event)
445 return HAA_FilterEvent(&event) == 0;
448 bool HAAScalerBase::initialized = false;
450 class HAAFillScaler : public HAAScalerBase
452 HAAFillScaler(SDL_Surface* screen, int w, int h)
453 : HAAScalerBase(screen, w, h,
454 GUI.Width / (float)w, GUI.Height / (float)h)
459 class Factory : public ScalerFactory
461 const char * getName() const
466 bool canEnable(int bpp, int w, int h) const
471 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
473 return new HAAFillScaler(screen, w, h-20);
477 static const Factory factory;
479 const char * getName() const
481 return "HAA fill screen scaling";
484 const HAAFillScaler::Factory HAAFillScaler::factory;
486 class HAASquareScaler : public HAAScalerBase
488 HAASquareScaler(SDL_Surface* screen, int w, int h, float ratio)
489 : HAAScalerBase(screen, w, h, ratio, ratio)
494 class Factory : public ScalerFactory
496 const char * getName() const
501 bool canEnable(int bpp, int w, int h) const
506 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
508 return new HAASquareScaler(screen, w, h,
509 fminf(GUI.Width / (float)w, GUI.Height / (float)h));
513 static const Factory factory;
515 const char * getName() const
517 return "HAA square screen scaling";
520 const HAASquareScaler::Factory HAASquareScaler::factory;
525 class XSPScaler : public Scaler
527 SDL_Surface* m_screen;
529 SDL_Rect m_real_area;
530 bool m_should_enable, m_enabled; // Try to avoid flicker.
532 static void setDoubling(bool enable)
534 SDL_SysWMinfo wminfo;
535 SDL_VERSION(&wminfo.version);
536 if ( SDL_GetWMInfo(&wminfo) ) {
537 Display *dpy = wminfo.info.x11.display;
538 XSPSetPixelDoubling(dpy, 0, enable ? 1 : 0);
543 XSPScaler(SDL_Surface* screen, int w, int h)
544 : m_screen(screen), m_should_enable(true), m_enabled(false)
546 centerRectangle(m_area, GUI.Width, GUI.Height,
549 m_real_area.x = m_area.x;
550 m_real_area.y = m_area.y;
551 m_real_area.w = m_area.w / 2;
552 m_real_area.h = m_area.h / 2;
557 if (m_enabled) setDoubling(false);
560 class Factory : public ScalerFactory
562 const char * getName() const
567 bool canEnable(int bpp, int w, int h) const
569 return w * 2 < GUI.Width && h * 2 < GUI.Height;
572 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
574 return new XSPScaler(screen, w, h);
578 static const Factory factory;
580 const char * getName() const
582 return "XSP pixel doubling";
585 uint8* getDrawBuffer() const
587 const int Bpp = screen->format->BitsPerPixel / 8;
588 const int pitch = screen->pitch;
589 return ((uint8*) screen->pixels)
591 + (m_area.y * pitch);
594 unsigned int getDrawBufferPitch() const
596 return screen->pitch;
599 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
600 unsigned short & w, unsigned short & h) const
602 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
605 void getRatio(float & x, float & y) const
612 if (m_should_enable && !m_enabled) {
620 SDL_UpdateRects(m_screen, 1, &m_real_area);
625 m_should_enable = false;
634 m_should_enable = true; // Will enable later
637 bool filter(const SDL_Event& event)
639 if (event.type == SDL_ACTIVEEVENT &&
640 (event.active.state & SDL_APPINPUTFOCUS)) {
641 if (event.active.gain) {
653 const XSPScaler::Factory XSPScaler::factory;
656 static const ScalerFactory* scalers[] = {
657 /* More useful scalers come first */
659 &XSPScaler::factory, /* n8x0 pixel doubling */
662 &ARMScaler::factory, /* arm 2x scaling */
665 &HAASquareScaler::factory, /* n900 animation actor scaling */
667 &SWScaler::factory, /* soft 2x scaling */
668 &DummyScaler::factory, /* failsafe */
669 /* The following scalers will not be automatically enabled, no matter what */
671 &HAAFillScaler::factory,
675 /* Entry point functions */
677 const ScalerFactory* searchForScaler(int bpp, int w, int h)
679 const int n = sizeof(scalers) / sizeof(ScalerFactory*);
682 if (Config.scaler && strcasecmp(Config.scaler, "help") == 0 ) {
684 printf("Scalers list:\n");
685 for (i = 0; i < n; i++) {
686 printf(" %s\n", scalers[i]->getName());
688 DIE("End of scalers list");
689 } else if (Config.scaler && strcasecmp(Config.scaler, "auto") != 0 ) {
690 // We prefer a specific scaler
691 for (i = 0; i < n; i++) {
692 if (strcasecmp(scalers[i]->getName(), Config.scaler) == 0) {
693 if (scalers[i]->canEnable(bpp, w, h)) {
694 // Found the scaler selected by the user, and we can use it.
698 "Selected scaler '%s' cannot be enabled in this mode\n",
700 break; // Fallback to another scaler.
705 fprintf(stderr, "Selected scaler '%s' does not exist\n",
710 // Just try them all now, in a buildtime set priority.
711 for (i = 0; i < n; i++) {
712 if (scalers[i]->canEnable(bpp, w, h)) {
717 DIE("Can't use any scaler; this shouldn't happen.");