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;
377 HAAScalerBase(SDL_Surface* screen, int w, int h, float r_x, float r_y)
378 : m_screen(screen), m_w(w), m_h(h),
379 m_Bpp(m_screen->format->BitsPerPixel / 8),
380 ratio_x(r_x), ratio_y(r_y)
382 const bool fullscreen = m_screen->flags & SDL_FULLSCREEN;
383 centerRectangle(m_area, GUI.Width, GUI.Height, w * r_x, h * r_y);
385 // Clear the SDL screen with black, just in case it gets drawn.
386 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
388 HAA_Init(fullscreen ? SDL_FULLSCREEN : 0);
389 actor = HAA_CreateActor(0, m_w, m_h, m_screen->format->BitsPerPixel);
390 HAA_SetPosition(actor, m_area.x, m_area.y + (fullscreen ? 0 : 60));
391 HAA_SetScale(actor, r_x, r_y);
396 virtual ~HAAScalerBase()
398 HAA_FreeActor(actor);
402 uint8* getDrawBuffer() const
404 return reinterpret_cast<uint8*>(actor->surface->pixels);
407 unsigned int getDrawBufferPitch() const
409 return actor->surface->pitch;
412 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
413 unsigned short & w, unsigned short & h) const
415 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
418 void getRatio(float & x, float & y) const
420 x = ratio_x; y = ratio_y;
436 bool filter(const SDL_Event& event)
438 return HAA_FilterEvent(&event) == 0;
442 class HAAFillScaler : public HAAScalerBase
444 HAAFillScaler(SDL_Surface* screen, int w, int h)
445 : HAAScalerBase(screen, w, h,
446 GUI.Width / (float)w, GUI.Height / (float)h)
451 class Factory : public ScalerFactory
453 const char * getName() const
458 bool canEnable(int bpp, int w, int h) const
463 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
465 return new HAAFillScaler(screen, w, h-20);
469 static const Factory factory;
471 const char * getName() const
473 return "HAA fill screen scaling";
476 const HAAFillScaler::Factory HAAFillScaler::factory;
478 class HAASquareScaler : public HAAScalerBase
480 HAASquareScaler(SDL_Surface* screen, int w, int h, float ratio)
481 : HAAScalerBase(screen, w, h, ratio, ratio)
486 class Factory : public ScalerFactory
488 const char * getName() const
493 bool canEnable(int bpp, int w, int h) const
498 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
500 return new HAASquareScaler(screen, w, h,
501 fminf(GUI.Width / (float)w, GUI.Height / (float)h));
505 static const Factory factory;
507 const char * getName() const
509 return "HAA square screen scaling";
512 const HAASquareScaler::Factory HAASquareScaler::factory;
517 class XSPScaler : public Scaler
519 SDL_Surface* m_screen;
521 SDL_Rect m_real_area;
522 bool m_should_enable, m_enabled; // Try to avoid flicker.
524 static void setDoubling(bool enable)
526 SDL_SysWMinfo wminfo;
527 SDL_VERSION(&wminfo.version);
528 if ( SDL_GetWMInfo(&wminfo) ) {
529 Display *dpy = wminfo.info.x11.display;
530 XSPSetPixelDoubling(dpy, 0, enable ? 1 : 0);
535 XSPScaler(SDL_Surface* screen, int w, int h)
536 : m_screen(screen), m_should_enable(true), m_enabled(false)
538 centerRectangle(m_area, GUI.Width, GUI.Height,
541 m_real_area.x = m_area.x;
542 m_real_area.y = m_area.y;
543 m_real_area.w = m_area.w / 2;
544 m_real_area.h = m_area.h / 2;
549 if (m_enabled) setDoubling(false);
552 class Factory : public ScalerFactory
554 const char * getName() const
559 bool canEnable(int bpp, int w, int h) const
561 return w * 2 < GUI.Width && h * 2 < GUI.Height;
564 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
566 return new XSPScaler(screen, w, h);
570 static const Factory factory;
572 const char * getName() const
574 return "XSP pixel doubling";
577 uint8* getDrawBuffer() const
579 const int Bpp = screen->format->BitsPerPixel / 8;
580 const int pitch = screen->pitch;
581 return ((uint8*) screen->pixels)
583 + (m_area.y * pitch);
586 unsigned int getDrawBufferPitch() const
588 return screen->pitch;
591 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
592 unsigned short & w, unsigned short & h) const
594 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
597 void getRatio(float & x, float & y) const
604 if (m_should_enable && !m_enabled) {
612 SDL_UpdateRects(m_screen, 1, &m_real_area);
617 m_should_enable = false;
626 m_should_enable = true; // Will enable later
629 bool filter(const SDL_Event& event)
631 if (event.type == SDL_ACTIVEEVENT &&
632 (event.active.state & SDL_APPINPUTFOCUS)) {
633 if (event.active.gain) {
645 const XSPScaler::Factory XSPScaler::factory;
648 static const ScalerFactory* scalers[] = {
649 /* More useful scalers come first */
651 &XSPScaler::factory, /* n8x0 pixel doubling */
654 &ARMScaler::factory, /* arm 2x scaling */
657 &HAASquareScaler::factory, /* n900 animation actor scaling */
659 &SWScaler::factory, /* soft 2x scaling */
660 &DummyScaler::factory, /* failsafe */
661 /* The following scalers will not be automatically enabled, no matter what */
663 &HAAFillScaler::factory,
667 /* Entry point functions */
669 const ScalerFactory* searchForScaler(int bpp, int w, int h)
671 const int n = sizeof(scalers) / sizeof(ScalerFactory*);
674 if (Config.scaler && strcasecmp(Config.scaler, "help") == 0 ) {
676 printf("Scalers list:\n");
677 for (i = 0; i < n; i++) {
678 printf(" %s\n", scalers[i]->getName());
680 DIE("End of scalers list");
681 } else if (Config.scaler && strcasecmp(Config.scaler, "auto") != 0 ) {
682 // We prefer a specific scaler
683 for (i = 0; i < n; i++) {
684 if (strcasecmp(scalers[i]->getName(), Config.scaler) == 0) {
685 if (scalers[i]->canEnable(bpp, w, h)) {
686 // Found the scaler selected by the user, and we can use it.
690 "Selected scaler '%s' cannot be enabled in this mode\n",
692 break; // Fallback to another scaler.
697 fprintf(stderr, "Selected scaler '%s' does not exist\n",
702 // Just try them all now, in a buildtime set priority.
703 for (i = 0; i < n; i++) {
704 if (scalers[i]->canEnable(bpp, w, h)) {
709 DIE("Can't use any scaler; this shouldn't happen.");