support for nonsquare scalers
[drnoksnes] / platform / sdlvscalers.cpp
1 #include <stdio.h> 
2 #include <math.h>
3
4 #include <X11/Xlib.h>
5 #include <X11/Xutil.h>
6 #include <SDL.h>
7
8 #if CONF_XSP
9 #       include <X11/extensions/Xsp.h>
10 #endif
11 #if CONF_HD
12 #       include <X11/Xatom.h>
13 #       include <sys/ipc.h>
14 #       include <sys/shm.h>
15 #endif
16
17 #include "snes9x.h"
18 #include "display.h"
19 #include "platform.h"
20 #include "scaler.h"
21 #include "sdlv.h"
22
23 #define DIE(format, ...) do { \
24                 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
25                 fprintf(stderr, format "\n", ## __VA_ARGS__); \
26                 abort(); \
27         } while (0);
28
29 /* Helper functions */
30
31 static void centerRectangle(SDL_Rect& result, int areaW, int areaH, int w, int h)
32 {
33         result.x = areaW / 2 - w / 2;
34         result.w = w;
35         result.y = areaH / 2 - h / 2;
36         result.h = h;
37 }
38
39 /* Base scaler for stupid scalers */
40 /** Does nothing but center the image */
41 class DummyScaler : public Scaler
42 {
43         SDL_Surface * m_screen;
44         SDL_Rect m_area;
45
46 protected:
47         DummyScaler(SDL_Surface* screen, int w, int h)
48         : m_screen(screen)
49         {
50                 centerRectangle(m_area, GUI.Width, GUI.Height, w, h);
51         }
52
53 public:
54
55         ~DummyScaler()
56         {
57         };
58
59         class Factory : public ScalerFactory
60         {
61                 const char * getName() const
62                 {
63                         return "none";
64                 }
65
66                 bool canEnable(int bpp, int w, int h) const
67                 {
68                         return true;
69                 }
70
71                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
72                 {
73                         return new DummyScaler(screen, w, h);
74                 }
75         };
76
77         static const Factory factory;
78
79         virtual const char * getName() const
80         {
81                 return "no scaling";
82         }
83
84         virtual uint8* getDrawBuffer() const
85         {
86                 const int Bpp = screen->format->BitsPerPixel / 8;
87                 const int pitch = screen->pitch;
88                 return ((uint8*) screen->pixels)
89                         + (m_area.x * Bpp)
90                         + (m_area.y * pitch);
91         };
92
93         virtual unsigned int getDrawBufferPitch() const
94         {
95                 return screen->pitch;
96         };
97
98         virtual void getRenderedGUIArea(unsigned short & x, unsigned short & y,
99                                                         unsigned short & w, unsigned short & h) const
100         {
101                 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
102         };
103
104         virtual void getRatio(float & x, float & y) const
105         {
106                 x = 1.0f; y = 1.0f;
107         };
108
109         virtual void prepare() { };
110
111         virtual void finish()
112         {
113                 SDL_UpdateRects(m_screen, 1, &m_area);
114         };
115
116         virtual void pause() { };
117         virtual void resume() { };
118 };
119 const DummyScaler::Factory DummyScaler::factory;
120
121 /* Basic and slow software scaler */
122
123 class SWScaler : public Scaler
124 {
125         SDL_Surface * m_screen;
126         SDL_Rect m_area;
127         uint8 * m_surface;
128         const int m_w, m_h, m_Bpp;
129
130 protected:
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)
134         {
135                 centerRectangle(m_area, GUI.Width, GUI.Height, w * 2, h * 2);
136                 m_surface = reinterpret_cast<uint8*>(malloc(w * h * m_Bpp));
137         }
138 public:
139         virtual ~SWScaler()
140         {
141                 free(m_surface);
142         };
143
144         class Factory : public ScalerFactory
145         {
146                 const char * getName() const
147                 {
148                         return "soft2x";
149                 }
150
151                 bool canEnable(int bpp, int w, int h) const
152                 {
153                         return w * 2 < GUI.Width && h * 2 < GUI.Height;
154                 }
155
156                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
157                 {
158                         return new SWScaler(screen, w, h);
159                 }
160         };
161
162         static const Factory factory;
163
164         virtual const char * getName() const
165         {
166                 return "software 2x scaling";
167         }
168
169         uint8* getDrawBuffer() const
170         {
171                 return m_surface;
172         };
173
174         unsigned int getDrawBufferPitch() const
175         {
176                 return m_w * m_Bpp;
177         };
178
179         void getRenderedGUIArea(unsigned short & x, unsigned short & y,
180                                                         unsigned short & w, unsigned short & h) const
181         {
182                 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
183         };
184
185         void getRatio(float & x, float & y) const
186         {
187                 x = 2.0f; y = 2.0f;
188         };
189
190         void prepare() { };
191
192         void finish()
193         {
194                 uint16 * src = reinterpret_cast<uint16*>(m_surface);
195                 uint16 * dst = reinterpret_cast<uint16*>(
196                         ((uint8*) m_screen->pixels)
197                         + (m_area.x * m_Bpp)
198                         + (m_area.y * m_screen->pitch));
199                 const int src_pitch = m_w;
200                 const int dst_pitch = m_screen->pitch / m_Bpp;
201                 int x, y;
202
203                 for (y = 0; y < m_h*2; y++) {
204                         for (x = 0; x < m_w*2; x+=2) {
205                                 dst[x] = src[x/2];
206                                 dst[x + 1] = src[x/2];
207                         }
208                         dst += dst_pitch;
209                         if (y&1) src += src_pitch;
210                 }
211
212                 SDL_UpdateRects(m_screen, 1, &m_area);
213         };
214
215         void pause() { };
216         void resume() { };
217 };
218 const SWScaler::Factory SWScaler::factory;
219
220 /* Platform specific scalers */
221
222 #ifdef __arm__
223 class ARMScaler : public Scaler
224 {
225         SDL_Surface * m_screen;
226         SDL_Rect m_area;
227         uint8 * m_surface;
228         const int m_w, m_h, m_Bpp;
229
230 protected:
231         ARMScaler(SDL_Surface* screen, int w, int h)
232         : m_screen(screen), m_w(w), m_h(h),
233          m_Bpp(m_screen->format->BitsPerPixel / 8)
234         {
235                 centerRectangle(m_area, GUI.Width, GUI.Height, w * 2, h * 2);
236                 m_surface = reinterpret_cast<uint8*>(malloc(w * h * m_Bpp));
237         }
238 public:
239         virtual ~ARMScaler()
240         {
241                 free(m_surface);
242         };
243
244         class Factory : public ScalerFactory
245         {
246                 const char * getName() const
247                 {
248                         return "arm2x";
249                 }
250
251                 bool canEnable(int bpp, int w, int h) const
252                 {
253                         return bpp == 16 && w * 2 < GUI.Width && h * 2 < GUI.Height &&
254                                 w % 16 == 0 /* asm assumes w div by 16 */;
255                 }
256
257                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
258                 {
259                         return new ARMScaler(screen, w, h);
260                 }
261         };
262
263         static const Factory factory;
264
265         virtual const char * getName() const
266         {
267                 return "software ARM 2x scaling";
268         }
269
270         uint8* getDrawBuffer() const
271         {
272                 return m_surface;
273         };
274
275         unsigned int getDrawBufferPitch() const
276         {
277                 return m_w * m_Bpp;
278         };
279
280         void getRenderedGUIArea(unsigned short & x, unsigned short & y,
281                                                         unsigned short & w, unsigned short & h) const
282         {
283                 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
284         };
285
286         void getRatio(float & x, float & y) const
287         {
288                 x = 2.0f; y = 2.0f;
289         };
290
291         void prepare() { };
292
293         void finish()
294         {
295                 uint16 * src = reinterpret_cast<uint16*>(m_surface);
296                 uint16 * dst = reinterpret_cast<uint16*>(
297                         ((uint8*) m_screen->pixels)
298                         + (m_area.x * m_Bpp)
299                         + (m_area.y * m_screen->pitch));
300                 const int src_pitch = m_w;
301                 const int dst_pitch = m_screen->pitch / m_Bpp;
302                 int y;
303
304                 for (y = 0; y < m_h*2; y++) {
305                         asm volatile
306                         (
307                                 "mov r0, %0; mov r1, %1; mov r2, %2;"
308                                 "stmdb r13!,{r4,r5,r6,r7,r8,r9,r10,r11,r12,r14};"
309                                 "1:     ldmia r1!,{r3,r4,r5,r6,r7,r8,r9,r10};"
310                                 "mov r14,r5,lsr #16;"
311                                 "mov r12,r5,lsl #16;"
312                                 "orr r14,r14,r14,lsl #16;"
313                                 "orr r12,r12,r12,lsr #16;"
314                                 "mov r11,r4,lsr #16;"
315                                 "mov r5,r4,lsl #16;"
316                                 "orr r11,r11,r11,lsl #16;"
317                                 "orr r5,r5,r5,lsr #16;"
318                                 "mov r4,r3,lsr #16;"
319                                 "mov r3,r3,lsl #16;"
320                                 "orr r4,r4,r4,lsl #16;"
321                                 "orr r3,r3,r3,lsr #16;"
322                                 "stmia r0!,{r3,r4,r5,r11,r12,r14};"
323                                 "mov r3,r6,lsl #16;"
324                                 "mov r4,r6,lsr #16;"
325                                 "orr r3,r3,r3,lsr #16;"
326                                 "orr r4,r4,r4,lsl #16;"
327                                 "mov r5,r7,lsl #16;"
328                                 "mov r6,r7,lsr #16;"
329                                 "orr r5,r5,r5,lsr #16;"
330                                 "orr r6,r6,r6,lsl #16;"
331                                 "mov r7,r8,lsl #16;"
332                                 "mov r8,r8,lsr #16;"
333                                 "orr r7,r7,r7,lsr #16;"
334                                 "orr r8,r8,r8,lsl #16;"
335                                 "mov r12,r10,lsr #16;"
336                                 "mov r11,r10,lsl #16;"
337                                 "orr r12,r12,r12,lsl #16;"
338                                 "orr r11,r11,r11,lsr #16;"
339                                 "mov r10,r9,lsr #16;"
340                                 "mov r9,r9,lsl #16;"
341                                 "orr r10,r10,r10,lsl #16;"
342                                 "orr r9,r9,r9,lsr #16;"
343                                 "stmia r0!,{r3,r4,r5,r6,r7,r8,r9,r10,r11,r12};"
344                                 "subs r2,r2,#16;"
345                                 "bhi 1b;"
346                                 "ldmia r13!,{r4,r5,r6,r7,r8,r9,r10,r11,r12,r14};"
347                         :
348                         : "r" (dst), "r" (src), "r" (m_w)
349                         : "r0", "r1", "r2", "r3"
350                         );
351                         dst += dst_pitch;
352                         if (y&1) src += src_pitch;
353                 }
354
355                 SDL_UpdateRects(m_screen, 1, &m_area);
356         };
357
358         void pause() { };
359         void resume() { };
360 };
361 const ARMScaler::Factory ARMScaler::factory;
362 #endif
363
364 #if CONF_HD
365
366 class HDScalerBase : public Scaler
367 {
368         SDL_Surface * m_screen;
369         SDL_Rect m_area;
370         const int m_w, m_h, m_Bpp;
371         const float ratio_x, ratio_y;
372
373         // SDL/X11 stuff we save for faster access.
374         Display* display;
375         Window window;
376
377         // Shared memory segment info.
378         key_t shmkey;
379         int shmid;
380         void *shmaddr;
381
382 private:
383         /** Sends a message to hildon-desktop.
384           * This function comes mostly straight from libhildon.
385           */
386         void sendMessage(Atom message_type,
387                 uint32 l0, uint32 l1, uint32 l2, uint32 l3, uint32 l4)
388         {
389                 XEvent event = { 0 };
390
391                 event.xclient.type = ClientMessage;
392                 event.xclient.window = window;
393                 event.xclient.message_type = message_type;
394                 event.xclient.format = 32;
395                 event.xclient.data.l[0] = l0;
396                 event.xclient.data.l[1] = l1;
397                 event.xclient.data.l[2] = l2;
398                 event.xclient.data.l[3] = l3;
399                 event.xclient.data.l[4] = l4;
400
401                 XSendEvent (display, window, True,
402                             StructureNotifyMask,
403                             (XEvent *)&event);
404         }
405
406         /** Sends all configuration parameters for the remote texture. */
407         void reconfigure()
408         {
409                 Window parent;
410                 int yoffset = 0;
411                 if (Config.fullscreen) {
412                         parent = WMinfo.info.x11.fswindow;
413                 } else {
414                         parent = WMinfo.info.x11.wmwindow;
415                         yoffset = 60; // Hardcode the title bar size for now.
416                 }
417
418                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_SHM),
419                         (uint32) shmkey, m_w, m_h, m_Bpp, 0);
420                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_PARENT),
421                         (uint32) parent, 0, 0, 0, 0);
422                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_POSITION),
423                         m_area.x, yoffset + m_area.y, m_area.w, m_area.h, 0);
424                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_SCALE),
425                         ratio_x * (1 << 16), ratio_y * (1 << 16), 0, 0, 0);
426                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_SHOW),
427                         1, 255, 0, 0, 0);
428         }
429
430 protected:
431         HDScalerBase(SDL_Surface* screen, int w, int h, float r_x, float r_y)
432         : m_screen(screen), m_w(w), m_h(h),
433          m_Bpp(m_screen->format->BitsPerPixel / 8),
434          ratio_x(r_x), ratio_y(r_y)
435         {
436                 centerRectangle(m_area, GUI.Width, GUI.Height, w * r_x, h * r_y);
437
438                 // What we're going to do:
439                 //  - Create a new window that we're going to manage
440                 //  - Set up that window as a Hildon Remote Texture
441                 //  - Render to that new window, instead of the SDL window ("screen").
442                 // Yet another load of uglyness, but hey.
443
444                 // Clear the SDL screen with black, just in case it gets drawn.
445                 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
446
447                 display = WMinfo.info.x11.display;
448
449                 // The parent window needs to be mapped, so we sync it.
450                 XSync(display, True);
451
452                 // Create our alternative window.
453                 const int blackColor = BlackPixel(display, DefaultScreen(display));
454                 window = XCreateSimpleWindow(display, DefaultRootWindow(display),
455                         0, 0, m_w, m_h, 0, blackColor, blackColor);
456                 XStoreName(display, window, "DrNokSnes Video output window");
457                 Atom atom = HDATOM(_HILDON_WM_WINDOW_TYPE_REMOTE_TEXTURE);
458                 XChangeProperty(display, window, HDATOM(_NET_WM_WINDOW_TYPE),
459                         XA_ATOM, 32, PropModeReplace,
460                         (unsigned char *) &atom, 1);
461                 XSelectInput(display, window, PropertyChangeMask | StructureNotifyMask);
462                 XMapWindow(display, window);
463
464                 // Wait for "ready" property, set up by hildon-desktop after a while
465                 // For now, loop here. In the future, merge with main event loop.
466                 bool ready = false;
467                 while (!ready) {
468                         XEvent e;
469                         XNextEvent(display, &e);
470                         switch(e.type) {
471                                 case PropertyNotify:
472                                         if (e.xproperty.atom ==
473                                           HDATOM(_HILDON_TEXTURE_CLIENT_READY)) {
474                                                 ready = true;
475                                         }
476                                         break;
477                                 default:
478                                         break;
479                         }
480                 }
481
482                 // Create a shared memory segment with hildon-desktop
483                 shmkey = ftok(S9xGetFilename(FILE_ROM), 'v');
484                 shmid = shmget(shmkey, m_w * m_h * m_Bpp, IPC_CREAT | 0777);
485                 if (shmid < 0) {
486                         DIE("Failed to create shared memory");
487                 }
488                 shmaddr = shmat(shmid, 0, 0);
489                 if (shmaddr == (void*)-1) {
490                         DIE("Failed to attach shared memory");
491                 }
492
493                 // Send all configuration events to hildon-desktop
494                 reconfigure();
495         }
496
497 public:
498         virtual ~HDScalerBase()
499         {
500                 // Hide, unparent and deattach the remote texture
501                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_SHOW),
502                         0, 255, 0, 0, 0);
503                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_PARENT),
504                         0, 0, 0, 0, 0);
505                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_SHM),
506                         0, 0, 0, 0, 0);
507                 XFlush(display);
508                 // Destroy our managed window and shared memory segment
509                 XDestroyWindow(display, window);
510                 XSync(display, True);
511                 shmdt(shmaddr);
512                 shmctl(shmid, IPC_RMID, 0);
513         };
514
515         virtual uint8* getDrawBuffer() const
516         {
517                 return reinterpret_cast<uint8*>(shmaddr);
518         };
519
520         virtual unsigned int getDrawBufferPitch() const
521         {
522                 return m_w * m_Bpp;
523         };
524
525         virtual void getRenderedGUIArea(unsigned short & x, unsigned short & y,
526                                                         unsigned short & w, unsigned short & h) const
527         {
528                 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
529         };
530
531         virtual void getRatio(float & x, float & y) const
532         {
533                 x = ratio_x; y = ratio_y;
534         };
535
536         virtual void prepare()
537         {
538
539         };
540
541         virtual void finish()
542         {
543                 // Send a damage event to hildon-desktop.
544                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_DAMAGE),
545                         0, 0, m_w, m_h, 0);
546                 XSync(display, False);
547         };
548
549         virtual void pause() { };
550         virtual void resume() { };
551 };
552
553 class HDFillScaler : public HDScalerBase
554 {
555         HDFillScaler(SDL_Surface* screen, int w, int h)
556         : HDScalerBase(screen, w, h,
557                 GUI.Width / (float)w, GUI.Height / (float)h)
558         {
559         }
560
561 public:
562         class Factory : public ScalerFactory
563         {
564                 const char * getName() const
565                 {
566                         return "hdfill";
567                 }
568
569                 bool canEnable(int bpp, int w, int h) const
570                 {
571                         return true;
572                 }
573
574                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
575                 {
576                         return new HDFillScaler(screen, w, h-20);
577                 }
578         };
579
580         static const Factory factory;
581
582         virtual const char * getName() const
583         {
584                 return "hildon-desktop fill screen scaling";
585         }
586 };
587 const HDFillScaler::Factory HDFillScaler::factory;
588
589 class HDSquareScaler : public HDScalerBase
590 {
591         HDSquareScaler(SDL_Surface* screen, int w, int h, float ratio)
592         : HDScalerBase(screen, w, h, ratio, ratio)
593         {
594         }
595
596 public:
597         class Factory : public ScalerFactory
598         {
599                 const char * getName() const
600                 {
601                         return "hdsq";
602                 }
603
604                 bool canEnable(int bpp, int w, int h) const
605                 {
606                         return true;
607                 }
608
609                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
610                 {
611                         return new HDSquareScaler(screen, w, h,
612                                 fminf(GUI.Width / (float)w, GUI.Height / (float)h));
613                 }
614         };
615
616         static const Factory factory;
617
618         virtual const char * getName() const
619         {
620                 return "hildon-desktop square screen scaling";
621         }
622 };
623 const HDSquareScaler::Factory HDSquareScaler::factory;
624
625 class HDDummy : public DummyScaler
626 {
627         HDDummy(SDL_Surface* screen, int w, int h)
628         : DummyScaler(screen, w, h)
629         {
630                 hd_set_non_compositing(true);
631         }
632         
633 public:
634         ~HDDummy()
635         {
636                 hd_set_non_compositing(false);
637         };
638
639         class Factory : public ScalerFactory
640         {
641                 const char * getName() const
642                 {
643                         return "hddummy";
644                 }
645
646                 bool canEnable(int bpp, int w, int h) const
647                 {
648                         return Config.fullscreen; // This makes sense only in fullscreen
649                 }
650
651                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
652                 {
653                         return new HDDummy(screen, w, h);
654                 }
655         };
656
657         static const Factory factory;
658
659         const char * getName() const
660         {
661                 return "compositor disabled and no scaling";
662         }
663 };
664 const HDDummy::Factory HDDummy::factory;
665
666 class HDSW : public SWScaler
667 {
668         HDSW(SDL_Surface* screen, int w, int h)
669         : SWScaler(screen, w, h)
670         {
671                 hd_set_non_compositing(true);
672         }
673         
674 public:
675         ~HDSW()
676         {
677                 hd_set_non_compositing(false);
678         };
679
680         class Factory : public ScalerFactory
681         {
682                 const char * getName() const
683                 {
684                         return "hdsoft2x";
685                 }
686
687                 bool canEnable(int bpp, int w, int h) const
688                 {
689                         return Config.fullscreen; // This makes sense only in fullscreen
690                 }
691
692                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
693                 {
694                         return new HDSW(screen, w, h);
695                 }
696         };
697
698         static const Factory factory;
699
700         const char * getName() const
701         {
702                 return "compositor disabled and software 2x scaling";
703         }
704 };
705 const HDSW::Factory HDSW::factory;
706
707 #ifdef __arm__
708 class HDARM : public ARMScaler
709 {
710         HDARM(SDL_Surface* screen, int w, int h)
711         : ARMScaler(screen, w, h)
712         {
713                 hildon_set_non_compositing(true);
714         }
715         
716 public:
717         ~HDARM()
718         {
719                 hildon_set_non_compositing(false);
720         };
721
722         class Factory : public ScalerFactory
723         {
724                 const char * getName() const
725                 {
726                         return "hdarm2x";
727                 }
728
729                 bool canEnable(int bpp, int w, int h) const
730                 {
731                         return Config.fullscreen; // This makes sense only in fullscreen
732                 }
733
734                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
735                 {
736                         return new HDARM(screen, w, h);
737                 }
738         };
739
740         static const Factory factory;
741
742         const char * getName() const
743         {
744                 return "compositor disabled and software ARM 2x scaling";
745         }
746 };
747 const HDARM::Factory HDARM::factory;
748 #endif /* __arm__ */
749 #endif /* CONF_HD */
750
751 #if CONF_XSP
752 class XSPScaler : public Scaler
753 {
754         SDL_Surface* m_screen;
755         SDL_Rect m_area;
756         SDL_Rect m_real_area;
757         bool m_should_enable, m_enabled; // Try to avoid flicker.
758
759         static void setDoubling(bool enable)
760         {
761                 SDL_SysWMinfo wminfo;
762                 SDL_VERSION(&wminfo.version);
763                 if ( SDL_GetWMInfo(&wminfo) ) {
764                         Display *dpy = wminfo.info.x11.display;
765                         XSPSetPixelDoubling(dpy, 0, enable ? 1 : 0);
766                         XFlush(dpy);
767                 }
768         }
769
770         XSPScaler(SDL_Surface* screen, int w, int h)
771         : m_screen(screen), m_should_enable(true), m_enabled(false)
772         {
773                 centerRectangle(m_area, GUI.Width, GUI.Height,
774                         w * 2, h * 2);
775
776                 m_real_area.x = m_area.x;
777                 m_real_area.y = m_area.y;
778                 m_real_area.w = m_area.w / 2;
779                 m_real_area.h = m_area.h / 2;
780         };
781 public:
782         ~XSPScaler()
783         {
784                 if (m_enabled) setDoubling(false);
785         };
786
787         class Factory : public ScalerFactory
788         {
789                 const char * getName() const
790                 {
791                         return "xsp";
792                 }
793
794                 bool canEnable(int bpp, int w, int h) const
795                 {
796                         return w * 2 < GUI.Width && h * 2 < GUI.Height;
797                 };
798
799                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
800                 {
801                         return new XSPScaler(screen, w, h);
802                 }
803         };
804
805         static const Factory factory;
806
807         const char * getName() const
808         {
809                 return "XSP pixel doubling";
810         }
811
812         uint8* getDrawBuffer() const
813         {
814                 const int Bpp = screen->format->BitsPerPixel / 8;
815                 const int pitch = screen->pitch;
816                 return ((uint8*) screen->pixels)
817                         + (m_area.x * Bpp)
818                         + (m_area.y * pitch);
819         };
820
821         unsigned int getDrawBufferPitch() const
822         {
823                 return screen->pitch;
824         };
825
826         void getRenderedGUIArea(unsigned short & x, unsigned short & y,
827                                                         unsigned short & w, unsigned short & h) const
828         {
829                 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
830         };
831
832         void getRatio(float & x, float & y) const
833         {
834                 x = 2.0f; y = 2.0f;
835         };
836
837         void prepare() 
838         {
839                 if (m_should_enable && !m_enabled) {
840                         setDoubling(true);
841                         m_enabled = true;
842                 }
843         };
844
845         void finish()
846         {
847                 SDL_UpdateRects(m_screen, 1, &m_real_area);
848         };
849
850         void pause()
851         {
852                 m_should_enable = false;
853                 if (m_enabled) {
854                         setDoubling(false);
855                         m_enabled = false;
856                 }
857         };
858
859         void resume()
860         {
861                 m_should_enable = true; // Will enable later
862         };
863 };
864 const XSPScaler::Factory XSPScaler::factory;
865 #endif
866
867 static const ScalerFactory* scalers[] = {
868 /* More useful scalers come first */
869 #if CONF_HD && defined(__arm__)
870         &HDARM::factory,                                /* non-composited arm 2x scaling */
871 #endif
872 #if CONF_HD
873         &HDSquareScaler::factory,               /* h-d assisted square scaling */
874         &HDSW::factory,                                 /* non-composited soft 2x scaling */
875 #endif
876 #if CONF_XSP
877         &XSPScaler::factory,                    /* n8x0 pixel doubling */
878 #endif
879 #ifdef __arm__
880         &ARMScaler::factory,                    /* arm 2x scaling */
881 #endif
882         &SWScaler::factory,                             /* soft 2x scaling */
883 #if CONF_HD
884         &HDDummy::factory,                              /* non composited */
885 #endif
886         &DummyScaler::factory,                  /* failsafe */
887 /* The following scalers will not be automatically enabled, no matter what */
888 #if CONF_HD
889         &HDFillScaler::factory,
890 #endif
891 };
892
893 /* Entry point functions */
894
895 const ScalerFactory* searchForScaler(int bpp, int w, int h)
896 {
897         const int n = sizeof(scalers) / sizeof(ScalerFactory*);
898         int i;
899
900         if (Config.scaler && strcasecmp(Config.scaler, "help") == 0 ) {
901                 // List scalers
902                 printf("Scalers list:\n");
903                 for (i = 0; i < n; i++) {
904                         printf(" %s\n", scalers[i]->getName());
905                 }
906                 DIE("End of scalers list");
907         } else if (Config.scaler && strcasecmp(Config.scaler, "auto") != 0 ) {
908                 // We prefer a specific scaler
909                 for (i = 0; i < n; i++) {
910                         if (strcasecmp(scalers[i]->getName(), Config.scaler) == 0) {
911                                 if (scalers[i]->canEnable(bpp, w, h)) {
912                                         // Found the scaler selected by the user, and we can use it.
913                                         return scalers[i];
914                                 } else {
915                                         fprintf(stderr,
916                                                 "Selected scaler '%s' cannot be enabled in this mode\n",
917                                                 Config.scaler);
918                                         break; // Fallback to another scaler.
919                                 }
920                         }
921                 }
922                 if (i == n) {
923                         fprintf(stderr, "Selected scaler '%s' does not exist\n",
924                                 Config.scaler);
925                 }
926         }
927
928         // Just try them all now, in a buildtime set priority.
929         for (i = 0; i < n; i++) {
930                 if (scalers[i]->canEnable(bpp, w, h)) {
931                         return scalers[i];
932                 }
933         }
934
935         DIE("Can't use any scaler; this shouldn't happen.");
936 }
937