9 # include <SDL_syswm.h>
10 # include <X11/extensions/Xsp.h>
21 #define DIE(format, ...) do { \
22 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
23 fprintf(stderr, format "\n", ## __VA_ARGS__); \
27 /* Helper functions */
29 static void centerRectangle(SDL_Rect& result, int areaW, int areaH, int w, int h)
31 result.x = areaW / 2 - w / 2;
33 result.y = areaH / 2 - h / 2;
37 /* Base scaler for stupid scalers */
38 /** Does nothing but center the image */
39 class DummyScaler : public Scaler
41 SDL_Surface * m_screen;
45 DummyScaler(SDL_Surface* screen, int w, int h)
48 centerRectangle(m_area, GUI.Width, GUI.Height, w, h);
57 class Factory : public ScalerFactory
59 const char * getName() const
64 bool canEnable(int bpp, int w, int h) const
69 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
71 return new DummyScaler(screen, w, h);
75 static const Factory factory;
77 virtual const char * getName() const
82 virtual uint8* getDrawBuffer() const
84 const int Bpp = screen->format->BitsPerPixel / 8;
85 const int pitch = screen->pitch;
86 return ((uint8*) screen->pixels)
91 virtual unsigned int getDrawBufferPitch() const
96 virtual void getRenderedGUIArea(unsigned short & x, unsigned short & y,
97 unsigned short & w, unsigned short & h) const
99 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
102 virtual void getRatio(float & x, float & y) const
107 virtual void prepare() { };
109 virtual void finish()
111 SDL_UpdateRects(m_screen, 1, &m_area);
114 virtual void pause() { };
115 virtual void resume() { };
117 virtual bool filter(const SDL_Event& event) { return false; };
119 const DummyScaler::Factory DummyScaler::factory;
121 /* Basic and slow software scaler */
123 class SWScaler : public Scaler
125 SDL_Surface * m_screen;
128 const int m_w, m_h, m_Bpp;
131 SWScaler(SDL_Surface* screen, int w, int h)
132 : m_screen(screen), m_w(w), m_h(h),
133 m_Bpp(m_screen->format->BitsPerPixel / 8)
135 centerRectangle(m_area, GUI.Width, GUI.Height, w * 2, h * 2);
136 m_surface = reinterpret_cast<uint8*>(malloc(w * h * m_Bpp));
144 class Factory : public ScalerFactory
146 const char * getName() const
151 bool canEnable(int bpp, int w, int h) const
153 return w * 2 < GUI.Width && h * 2 < GUI.Height;
156 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
158 return new SWScaler(screen, w, h);
162 static const Factory factory;
164 virtual const char * getName() const
166 return "software 2x scaling";
169 uint8* getDrawBuffer() const
174 unsigned int getDrawBufferPitch() const
179 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
180 unsigned short & w, unsigned short & h) const
182 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
185 void getRatio(float & x, float & y) const
194 uint16 * src = reinterpret_cast<uint16*>(m_surface);
195 uint16 * dst = reinterpret_cast<uint16*>(
196 ((uint8*) m_screen->pixels)
198 + (m_area.y * m_screen->pitch));
199 const int src_pitch = m_w;
200 const int dst_pitch = m_screen->pitch / m_Bpp;
203 for (y = 0; y < m_h*2; y++) {
204 for (x = 0; x < m_w*2; x+=2) {
206 dst[x + 1] = src[x/2];
209 if (y&1) src += src_pitch;
212 SDL_UpdateRects(m_screen, 1, &m_area);
218 bool filter(const SDL_Event& event) { return false; };
220 const SWScaler::Factory SWScaler::factory;
222 /* Platform specific scalers */
225 class ARMScaler : public Scaler
227 SDL_Surface * m_screen;
230 const int m_w, m_h, m_Bpp;
233 ARMScaler(SDL_Surface* screen, int w, int h)
234 : m_screen(screen), m_w(w), m_h(h),
235 m_Bpp(m_screen->format->BitsPerPixel / 8)
237 centerRectangle(m_area, GUI.Width, GUI.Height, w * 2, h * 2);
238 m_surface = reinterpret_cast<uint8*>(malloc(w * h * m_Bpp));
246 class Factory : public ScalerFactory
248 const char * getName() const
253 bool canEnable(int bpp, int w, int h) const
255 return bpp == 16 && w * 2 < GUI.Width && h * 2 < GUI.Height &&
256 w % 16 == 0 /* asm assumes w div by 16 */;
259 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
261 return new ARMScaler(screen, w, h);
265 static const Factory factory;
267 virtual const char * getName() const
269 return "software ARM 2x scaling";
272 uint8* getDrawBuffer() const
277 unsigned int getDrawBufferPitch() const
282 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
283 unsigned short & w, unsigned short & h) const
285 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
288 void getRatio(float & x, float & y) const
297 uint16 * src = reinterpret_cast<uint16*>(m_surface);
298 uint16 * dst = reinterpret_cast<uint16*>(
299 ((uint8*) m_screen->pixels)
301 + (m_area.y * m_screen->pitch));
302 const int src_pitch = m_w;
303 const int dst_pitch = m_screen->pitch / m_Bpp;
306 for (y = 0; y < m_h*2; y++) {
309 "mov r0, %0; mov r1, %1; mov r2, %2;"
310 "stmdb r13!,{r4,r5,r6,r7,r8,r9,r10,r11,r12,r14};"
311 "1: ldmia r1!,{r3,r4,r5,r6,r7,r8,r9,r10};"
312 "mov r14,r5,lsr #16;"
313 "mov r12,r5,lsl #16;"
314 "orr r14,r14,r14,lsl #16;"
315 "orr r12,r12,r12,lsr #16;"
316 "mov r11,r4,lsr #16;"
318 "orr r11,r11,r11,lsl #16;"
319 "orr r5,r5,r5,lsr #16;"
322 "orr r4,r4,r4,lsl #16;"
323 "orr r3,r3,r3,lsr #16;"
324 "stmia r0!,{r3,r4,r5,r11,r12,r14};"
327 "orr r3,r3,r3,lsr #16;"
328 "orr r4,r4,r4,lsl #16;"
331 "orr r5,r5,r5,lsr #16;"
332 "orr r6,r6,r6,lsl #16;"
335 "orr r7,r7,r7,lsr #16;"
336 "orr r8,r8,r8,lsl #16;"
337 "mov r12,r10,lsr #16;"
338 "mov r11,r10,lsl #16;"
339 "orr r12,r12,r12,lsl #16;"
340 "orr r11,r11,r11,lsr #16;"
341 "mov r10,r9,lsr #16;"
343 "orr r10,r10,r10,lsl #16;"
344 "orr r9,r9,r9,lsr #16;"
345 "stmia r0!,{r3,r4,r5,r6,r7,r8,r9,r10,r11,r12};"
348 "ldmia r13!,{r4,r5,r6,r7,r8,r9,r10,r11,r12,r14};"
350 : "r" (dst), "r" (src), "r" (m_w)
351 : "r0", "r1", "r2", "r3"
354 if (y&1) src += src_pitch;
357 SDL_UpdateRects(m_screen, 1, &m_area);
362 bool filter(const SDL_Event& event) { return false; };
364 const ARMScaler::Factory ARMScaler::factory;
368 class HAAScalerBase : public Scaler
370 SDL_Surface *m_screen;
373 const int m_w, m_h, m_Bpp;
374 const float ratio_x, ratio_y;
376 static bool initialized;
379 HAAScalerBase(SDL_Surface* screen, int w, int h, float r_x, float r_y)
380 : m_screen(screen), m_w(w), m_h(h),
381 m_Bpp(m_screen->format->BitsPerPixel / 8),
382 ratio_x(r_x), ratio_y(r_y)
384 const bool fullscreen = m_screen->flags & SDL_FULLSCREEN;
385 centerRectangle(m_area, GUI.Width, GUI.Height, w * r_x, h * r_y);
387 // Clear the SDL screen with black, just in case it gets drawn.
388 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
394 HAA_SetVideoMode(); // Tell HAA we might have changed video mode
397 actor = HAA_CreateActor(0, m_w, m_h, m_screen->format->BitsPerPixel);
398 HAA_SetPosition(actor, m_area.x, m_area.y + (fullscreen ? 0 : 60));
399 // In windowed mode, take care of the title bar (xoffset = 60)
400 HAA_SetScale(actor, r_x, r_y);
405 virtual ~HAAScalerBase()
407 HAA_FreeActor(actor);
410 uint8* getDrawBuffer() const
412 return reinterpret_cast<uint8*>(actor->surface->pixels);
415 unsigned int getDrawBufferPitch() const
417 return actor->surface->pitch;
420 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
421 unsigned short & w, unsigned short & h) const
423 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
426 void getRatio(float & x, float & y) const
428 x = ratio_x; y = ratio_y;
444 bool filter(const SDL_Event& event)
446 return HAA_FilterEvent(&event) == 0;
449 bool HAAScalerBase::initialized = false;
451 class HAAFillScaler : public HAAScalerBase
453 HAAFillScaler(SDL_Surface* screen, int w, int h)
454 : HAAScalerBase(screen, w, h,
455 GUI.Width / (float)w, GUI.Height / (float)h)
460 class Factory : public ScalerFactory
462 const char * getName() const
467 bool canEnable(int bpp, int w, int h) const
472 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
474 return new HAAFillScaler(screen, w, h-20);
478 static const Factory factory;
480 const char * getName() const
482 return "HAA fill screen scaling";
485 const HAAFillScaler::Factory HAAFillScaler::factory;
487 class HAASquareScaler : public HAAScalerBase
489 HAASquareScaler(SDL_Surface* screen, int w, int h, float ratio)
490 : HAAScalerBase(screen, w, h, ratio, ratio)
495 class Factory : public ScalerFactory
497 const char * getName() const
502 bool canEnable(int bpp, int w, int h) const
507 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
509 return new HAASquareScaler(screen, w, h,
510 fminf(GUI.Width / (float)w, GUI.Height / (float)h));
514 static const Factory factory;
516 const char * getName() const
518 return "HAA square screen scaling";
521 const HAASquareScaler::Factory HAASquareScaler::factory;
526 class XSPScaler : public Scaler
528 SDL_Surface* m_screen;
530 SDL_Rect m_real_area;
531 bool m_should_enable, m_enabled; // Try to avoid flicker.
533 static void setDoubling(bool enable)
535 SDL_SysWMinfo wminfo;
536 SDL_VERSION(&wminfo.version);
537 if ( SDL_GetWMInfo(&wminfo) ) {
538 Display *dpy = wminfo.info.x11.display;
539 XSPSetPixelDoubling(dpy, 0, enable ? 1 : 0);
544 XSPScaler(SDL_Surface* screen, int w, int h)
545 : m_screen(screen), m_should_enable(true), m_enabled(false)
547 centerRectangle(m_area, GUI.Width, GUI.Height,
550 m_real_area.x = m_area.x;
551 m_real_area.y = m_area.y;
552 m_real_area.w = m_area.w / 2;
553 m_real_area.h = m_area.h / 2;
558 if (m_enabled) setDoubling(false);
561 class Factory : public ScalerFactory
563 const char * getName() const
568 bool canEnable(int bpp, int w, int h) const
570 return w * 2 < GUI.Width && h * 2 < GUI.Height;
573 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
575 return new XSPScaler(screen, w, h);
579 static const Factory factory;
581 const char * getName() const
583 return "XSP pixel doubling";
586 uint8* getDrawBuffer() const
588 const int Bpp = screen->format->BitsPerPixel / 8;
589 const int pitch = screen->pitch;
590 return ((uint8*) screen->pixels)
592 + (m_area.y * pitch);
595 unsigned int getDrawBufferPitch() const
597 return screen->pitch;
600 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
601 unsigned short & w, unsigned short & h) const
603 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
606 void getRatio(float & x, float & y) const
613 if (m_should_enable && !m_enabled) {
621 SDL_UpdateRects(m_screen, 1, &m_real_area);
626 m_should_enable = false;
635 m_should_enable = true; // Will enable later
638 bool filter(const SDL_Event& event)
640 if (event.type == SDL_ACTIVEEVENT &&
641 (event.active.state & SDL_APPINPUTFOCUS)) {
642 if (event.active.gain) {
654 const XSPScaler::Factory XSPScaler::factory;
657 static const ScalerFactory* scalers[] = {
658 /* More useful scalers come first */
660 &XSPScaler::factory, /* n8x0 pixel doubling */
663 &ARMScaler::factory, /* arm 2x scaling */
666 &HAASquareScaler::factory, /* n900 animation actor scaling */
668 &SWScaler::factory, /* soft 2x scaling */
669 &DummyScaler::factory, /* failsafe */
670 /* The following scalers will not be automatically enabled, no matter what */
672 &HAAFillScaler::factory,
676 /* Entry point functions */
678 const ScalerFactory* searchForScaler(int bpp, int w, int h)
680 const int n = sizeof(scalers) / sizeof(ScalerFactory*);
683 if (Config.scaler && strcasecmp(Config.scaler, "help") == 0 ) {
685 printf("Scalers list:\n");
686 for (i = 0; i < n; i++) {
687 printf(" %s\n", scalers[i]->getName());
689 DIE("End of scalers list");
690 } else if (Config.scaler && strcasecmp(Config.scaler, "auto") != 0 ) {
691 // We prefer a specific scaler
692 for (i = 0; i < n; i++) {
693 if (strcasecmp(scalers[i]->getName(), Config.scaler) == 0) {
694 if (scalers[i]->canEnable(bpp, w, h)) {
695 // Found the scaler selected by the user, and we can use it.
699 "Selected scaler '%s' cannot be enabled in this mode\n",
701 break; // Fallback to another scaler.
706 fprintf(stderr, "Selected scaler '%s' does not exist\n",
711 // Just try them all now, in a buildtime set priority.
712 for (i = 0; i < n; i++) {
713 if (scalers[i]->canEnable(bpp, w, h)) {
718 DIE("Can't use any scaler; this shouldn't happen.");