initial scaler selector in gui
[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 #include <SDL_syswm.h>
8
9 #if CONF_XSP
10 #       include <X11/extensions/Xsp.h>
11 #endif
12 #if CONF_HD
13 #       include <X11/Xatom.h>
14 #       include <sys/ipc.h>
15 #       include <sys/shm.h>
16 #endif
17
18 #include "snes9x.h"
19 #include "display.h"
20 #include "platform.h"
21 #include "scaler.h"
22 #include "sdlv.h"
23
24 #define DIE(format, ...) do { \
25                 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
26                 fprintf(stderr, format "\n", ## __VA_ARGS__); \
27                 abort(); \
28         } while (0);
29
30 /* Helper functions */
31
32 static void centerRectangle(SDL_Rect& result, int areaW, int areaH, int w, int h)
33 {
34         result.x = areaW / 2 - w / 2;
35         result.w = w;
36         result.y = areaH / 2 - h / 2;
37         result.h = h;
38 }
39
40 /* Base scaler for stupid scalers */
41 /** Does nothing but center the image */
42 class DummyScaler : public Scaler
43 {
44         SDL_Surface * m_screen;
45         SDL_Rect m_area;
46
47 protected:
48         DummyScaler(SDL_Surface* screen, int w, int h)
49         : m_screen(screen)
50         {
51                 centerRectangle(m_area, GUI.Width, GUI.Height, w, h);
52         }
53
54 public:
55
56         ~DummyScaler()
57         {
58         };
59
60         class Factory : public ScalerFactory
61         {
62                 const char * getName() const
63                 {
64                         return "none";
65                 }
66
67                 bool canEnable(int bpp, int w, int h) const
68                 {
69                         return true;
70                 }
71
72                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
73                 {
74                         return new DummyScaler(screen, w, h);
75                 }
76         };
77
78         static const Factory factory;
79
80         virtual const char * getName() const
81         {
82                 return "no scaling";
83         }
84
85         virtual uint8* getDrawBuffer() const
86         {
87                 const int Bpp = screen->format->BitsPerPixel / 8;
88                 const int pitch = screen->pitch;
89                 return ((uint8*) screen->pixels)
90                         + (m_area.x * Bpp)
91                         + (m_area.y * pitch);
92         };
93
94         virtual unsigned int getDrawBufferPitch() const
95         {
96                 return screen->pitch;
97         };
98
99         virtual void getRenderedGUIArea(unsigned short & x, unsigned short & y,
100                                                         unsigned short & w, unsigned short & h) const
101         {
102                 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
103         };
104
105         virtual int getRatio() const
106         {
107                 return 1;
108         };
109
110         virtual void prepare() { };
111
112         virtual void finish()
113         {
114                 SDL_UpdateRects(m_screen, 1, &m_area);
115         };
116
117         virtual void pause() { };
118         virtual void resume() { };
119 };
120 const DummyScaler::Factory DummyScaler::factory;
121
122 /* Basic and slow software scaler */
123
124 class SWScaler : public Scaler
125 {
126         SDL_Surface * m_screen;
127         SDL_Rect m_area;
128         uint8 * m_surface;
129         const int m_w, m_h, m_Bpp;
130
131 protected:
132         SWScaler(SDL_Surface* screen, int w, int h)
133         : m_screen(screen), m_w(w), m_h(h),
134          m_Bpp(m_screen->format->BitsPerPixel / 8)
135         {
136                 centerRectangle(m_area, GUI.Width, GUI.Height, w * 2, h * 2);
137                 m_surface = reinterpret_cast<uint8*>(malloc(w * h * m_Bpp));
138         }
139 public:
140         virtual ~SWScaler()
141         {
142                 free(m_surface);
143         };
144
145         class Factory : public ScalerFactory
146         {
147                 const char * getName() const
148                 {
149                         return "soft2x";
150                 }
151
152                 bool canEnable(int bpp, int w, int h) const
153                 {
154                         return w * 2 < GUI.Width && h * 2 < GUI.Height;
155                 }
156
157                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
158                 {
159                         return new SWScaler(screen, w, h);
160                 }
161         };
162
163         static const Factory factory;
164
165         virtual const char * getName() const
166         {
167                 return "software 2x scaling";
168         }
169
170         uint8* getDrawBuffer() const
171         {
172                 return m_surface;
173         };
174
175         unsigned int getDrawBufferPitch() const
176         {
177                 return m_w * m_Bpp;
178         };
179
180         void getRenderedGUIArea(unsigned short & x, unsigned short & y,
181                                                         unsigned short & w, unsigned short & h) const
182         {
183                 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
184         };
185
186         int getRatio() const
187         {
188                 return 2;
189         };
190
191         void prepare() { };
192
193         void finish()
194         {
195                 uint16 * src = reinterpret_cast<uint16*>(m_surface);
196                 uint16 * dst = reinterpret_cast<uint16*>(
197                         ((uint8*) m_screen->pixels)
198                         + (m_area.x * m_Bpp)
199                         + (m_area.y * m_screen->pitch));
200                 const int src_pitch = m_w;
201                 const int dst_pitch = m_screen->pitch / m_Bpp;
202                 int x, y;
203
204                 for (y = 0; y < m_h*2; y++) {
205                         for (x = 0; x < m_w*2; x+=2) {
206                                 dst[x] = src[x/2];
207                                 dst[x + 1] = src[x/2];
208                         }
209                         dst += dst_pitch;
210                         if (y&1) src += src_pitch;
211                 }
212
213                 SDL_UpdateRects(m_screen, 1, &m_area);
214         };
215
216         void pause() { };
217         void resume() { };
218 };
219 const SWScaler::Factory SWScaler::factory;
220
221 /* Platform specific scalers */
222
223 #ifdef __arm__
224 class ARMScaler : public Scaler
225 {
226         SDL_Surface * m_screen;
227         SDL_Rect m_area;
228         uint8 * m_surface;
229         const int m_w, m_h, m_Bpp;
230
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         int getRatio() const
287         {
288                 return 2;
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 enum hdAtoms {
367         ATOM_HILDON_NON_COMPOSITED_WINDOW = 0,
368         ATOM_NET_WM_STATE,
369         ATOM_NET_WM_STATE_FULLSCREEN,
370         ATOM_NET_WM_WINDOW_TYPE,
371         ATOM_NET_WM_WINDOW_TYPE_NORMAL,
372         ATOM_NET_WM_WINDOW_TYPE_DIALOG,
373         ATOM_HILDON_WM_WINDOW_TYPE_ANIMATION_ACTOR,
374         ATOM_HILDON_ANIMATION_CLIENT_READY,
375         ATOM_HILDON_ANIMATION_CLIENT_MESSAGE_SHOW,
376         ATOM_HILDON_ANIMATION_CLIENT_MESSAGE_POSITION,
377         ATOM_HILDON_ANIMATION_CLIENT_MESSAGE_ROTATION,
378         ATOM_HILDON_ANIMATION_CLIENT_MESSAGE_SCALE,
379         ATOM_HILDON_ANIMATION_CLIENT_MESSAGE_ANCHOR,
380         ATOM_HILDON_ANIMATION_CLIENT_MESSAGE_PARENT,
381         ATOM_HILDON_WM_WINDOW_TYPE_REMOTE_TEXTURE,
382         ATOM_HILDON_TEXTURE_CLIENT_MESSAGE_SHM,
383         ATOM_HILDON_TEXTURE_CLIENT_MESSAGE_DAMAGE,
384         ATOM_HILDON_TEXTURE_CLIENT_MESSAGE_SHOW,
385         ATOM_HILDON_TEXTURE_CLIENT_MESSAGE_POSITION,
386         ATOM_HILDON_TEXTURE_CLIENT_MESSAGE_OFFSET,
387         ATOM_HILDON_TEXTURE_CLIENT_MESSAGE_SCALE,
388         ATOM_HILDON_TEXTURE_CLIENT_MESSAGE_PARENT,
389         ATOM_HILDON_TEXTURE_CLIENT_READY,
390         ATOM_COUNT
391 };
392
393 static const char * hdAtomNames[] = {
394         "_HILDON_NON_COMPOSITED_WINDOW",
395         "_NET_WM_STATE",
396         "_NET_WM_STATE_FULLSCREEN",
397         "_NET_WM_WINDOW_TYPE",
398         "_NET_WM_WINDOW_TYPE_NORMAL",
399         "_NET_WM_WINDOW_TYPE_DIALOG",
400         "_HILDON_WM_WINDOW_TYPE_ANIMATION_ACTOR",
401         "_HILDON_ANIMATION_CLIENT_READY",
402         "_HILDON_ANIMATION_CLIENT_MESSAGE_SHOW",        
403         "_HILDON_ANIMATION_CLIENT_MESSAGE_POSITION",
404         "_HILDON_ANIMATION_CLIENT_MESSAGE_ROTATION",
405         "_HILDON_ANIMATION_CLIENT_MESSAGE_SCALE",
406         "_HILDON_ANIMATION_CLIENT_MESSAGE_ANCHOR",
407         "_HILDON_ANIMATION_CLIENT_MESSAGE_PARENT",
408         "_HILDON_WM_WINDOW_TYPE_REMOTE_TEXTURE",
409         "_HILDON_TEXTURE_CLIENT_MESSAGE_SHM",
410         "_HILDON_TEXTURE_CLIENT_MESSAGE_DAMAGE",
411         "_HILDON_TEXTURE_CLIENT_MESSAGE_SHOW",
412         "_HILDON_TEXTURE_CLIENT_MESSAGE_POSITION",
413         "_HILDON_TEXTURE_CLIENT_MESSAGE_OFFSET",
414         "_HILDON_TEXTURE_CLIENT_MESSAGE_SCALE",
415         "_HILDON_TEXTURE_CLIENT_MESSAGE_PARENT",
416         "_HILDON_TEXTURE_CLIENT_READY",
417         ""
418 };
419
420 static Atom hdAtomsValues[ATOM_COUNT];
421 static bool hdAtomsLoaded = false;
422
423 #define HDATOM(X) hdAtomsValues[ ATOM ## X ]
424
425 static void hildon_load_atoms(Display* display)
426 {
427         if (hdAtomsLoaded) return;
428         
429         XInternAtoms(display, (char**)hdAtomNames, ATOM_COUNT, True, hdAtomsValues);
430         hdAtomsLoaded = true;
431         
432         if (HDATOM(_HILDON_NON_COMPOSITED_WINDOW) == None) {
433                 DIE("Hildon Desktop seems not be loaded, since %s is not defined",
434                         "_HILDON_NON_COMPOSITED_WINDOW");
435                 return;
436         }
437 }
438
439 /** Enables or disables the Hildon NonCompositedWindow property */
440 static void hildon_set_non_compositing(bool enable)
441 {
442         SDL_SysWMinfo wminfo;
443         Display *display;
444         Window xwindow;
445         XSetWindowAttributes xattr;
446         Atom atom;
447         int one = 1;
448         
449         SDL_VERSION(&wminfo.version);
450         if (!SDL_GetWMInfo(&wminfo)) return;
451         
452         wminfo.info.x11.lock_func();
453         display = wminfo.info.x11.display;
454         xwindow = wminfo.info.x11.fswindow;
455         hildon_load_atoms(display);
456
457         if (enable) {
458                 /* 
459                  * The basic idea behind this is to disable the override_redirect
460                  * window attribute, which SDL sets, and instead use _NET_WM_STATE
461                  * to tell hildon-desktop to fullscreen the app.
462                  * I am not really happy with this, which should ideally be fixed
463                  * at the libsdl level, but seems to work.
464                  * As soon as the window is managed by Hildon-Desktop again, set for it
465                  * not to be composited.
466                  */
467                 XUnmapWindow(display, xwindow);
468                 xattr.override_redirect = False;
469                 XChangeWindowAttributes(display, xwindow, CWOverrideRedirect, &xattr);
470
471                 atom = HDATOM(_NET_WM_STATE_FULLSCREEN);
472                 XChangeProperty(display, xwindow, HDATOM(_NET_WM_STATE),
473                         XA_ATOM, 32, PropModeReplace,
474                         (unsigned char *) &atom, 1);
475
476                 XChangeProperty(display, xwindow, HDATOM(_HILDON_NON_COMPOSITED_WINDOW),
477                         XA_INTEGER, 32, PropModeReplace,
478                         (unsigned char *) &one, 1);
479                 XMapWindow(display, xwindow);
480         } else {
481                 xattr.override_redirect = True;
482                 XDeleteProperty(display, xwindow,
483                         HDATOM(_HILDON_NON_COMPOSITED_WINDOW));
484                 XChangeWindowAttributes(display, xwindow, CWOverrideRedirect, &xattr);
485         }
486
487         wminfo.info.x11.unlock_func();
488 }
489
490 class HDScalerBase : public Scaler
491 {
492         SDL_Surface * m_screen;
493         SDL_Rect m_area;
494         const int m_w, m_h, m_Bpp;
495         const float ratio_x, ratio_y;
496
497         // SDL/X11 stuff we save for faster access.
498         SDL_SysWMinfo wminfo;
499         Display* display;
500         Window window;
501
502         // Shared memory segment info.
503         key_t shmkey;
504         int shmid;
505         void *shmaddr;
506
507 private:
508         /** Sends a message to hildon-desktop.
509           * This function comes mostly straight from libhildon.
510           */
511         void sendMessage(Atom message_type,
512                 uint32 l0, uint32 l1, uint32 l2, uint32 l3, uint32 l4)
513         {
514                 XEvent event = { 0 };
515
516                 event.xclient.type = ClientMessage;
517                 event.xclient.window = window;
518                 event.xclient.message_type = message_type;
519                 event.xclient.format = 32;
520                 event.xclient.data.l[0] = l0;
521                 event.xclient.data.l[1] = l1;
522                 event.xclient.data.l[2] = l2;
523                 event.xclient.data.l[3] = l3;
524                 event.xclient.data.l[4] = l4;
525
526                 XSendEvent (display, window, True,
527                             StructureNotifyMask,
528                             (XEvent *)&event);
529         }
530
531         /** Sends all configuration parameters for the remote texture. */
532         void reconfigure()
533         {
534                 SDL_VERSION(&wminfo.version);
535                 if (!SDL_GetWMInfo(&wminfo)) {
536                         DIE("Bad SDL version!");
537                 }
538
539                 Window parent;
540                 int yoffset = 0;
541                 if (Config.fullscreen) {
542                         parent = wminfo.info.x11.fswindow;
543                 } else {
544                         parent = wminfo.info.x11.wmwindow;
545                         yoffset = 60; // Hardcode the title bar size for now.
546                 }
547
548                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_SHM),
549                         (uint32) shmkey, m_w, m_h, m_Bpp, 0);
550                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_PARENT),
551                         (uint32) parent, 0, 0, 0, 0);
552                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_POSITION),
553                         m_area.x, yoffset + m_area.y, m_area.w, m_area.h, 0);
554                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_SCALE),
555                         ratio_x * (1 << 16), ratio_y * (1 << 16), 0, 0, 0);
556                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_SHOW),
557                         1, 255, 0, 0, 0);
558         }
559
560 protected:
561         HDScalerBase(SDL_Surface* screen, int w, int h, float r_x, float r_y)
562         : m_screen(screen), m_w(w), m_h(h),
563          m_Bpp(m_screen->format->BitsPerPixel / 8),
564          ratio_x(r_x), ratio_y(r_y)
565         {
566                 centerRectangle(m_area, GUI.Width, GUI.Height, w * r_x, h * r_y);
567
568                 // What we're going to do:
569                 //  - Create a new window that we're going to manage
570                 //  - Set up that window as a Hildon Remote Texture
571                 //  - Render to that new window, instead of the SDL window ("screen").
572                 // Yet another load of uglyness, but hey.
573
574                 // Barf if this is not a known SDL version.
575                 SDL_VERSION(&wminfo.version);
576                 if (!SDL_GetWMInfo(&wminfo)) {
577                         DIE("Bad SDL version!");
578                 }
579
580                 // Clear the SDL screen with black, just in case it gets drawn.
581                 SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
582
583                 // Get the SDL gfxdisplay (this is where events end up).
584                 display = wminfo.info.x11.display;
585
586                 // The parent window needs to be mapped, so we sync it.
587                 XSync(display, True);
588
589                 // Ensure hildon atoms are synced, and that hildon-desktop is up.
590                 hildon_load_atoms(display);
591
592                 // Create our alternative window.
593                 const int blackColor = BlackPixel(display, DefaultScreen(display));
594                 window = XCreateSimpleWindow(display, DefaultRootWindow(display),
595                         0, 0, m_w, m_h, 0, blackColor, blackColor);
596                 XStoreName(display, window, "DrNokSnes Video output window");
597                 Atom atom = HDATOM(_HILDON_WM_WINDOW_TYPE_REMOTE_TEXTURE);
598                 XChangeProperty(display, window, HDATOM(_NET_WM_WINDOW_TYPE),
599                         XA_ATOM, 32, PropModeReplace,
600                         (unsigned char *) &atom, 1);
601                 XSelectInput(display, window, PropertyChangeMask | StructureNotifyMask);
602                 XMapWindow(display, window);
603
604                 // Wait for "ready" property, set up by hildon-desktop after a while
605                 // For now, loop here. In the future, merge with main event loop.
606                 bool ready = false;
607                 while (!ready) {
608                         XEvent e;
609                         XNextEvent(display, &e);
610                         switch(e.type) {
611                                 case PropertyNotify:
612                                         if (e.xproperty.atom ==
613                                           HDATOM(_HILDON_TEXTURE_CLIENT_READY)) {
614                                                 ready = true;
615                                         }
616                                         break;
617                                 default:
618                                         break;
619                         }
620                 }
621
622                 // Create a shared memory segment with hildon-desktop
623                 shmkey = ftok("/usr/bin/drnoksnes", 'd'); // TODO Put rom file here
624                 shmid = shmget(shmkey, m_w * m_h * m_Bpp, IPC_CREAT | 0777);
625                 if (shmid < 0) {
626                         DIE("Failed to create shared memory");
627                 }
628                 shmaddr = shmat(shmid, 0, 0);
629                 if (shmaddr == (void*)-1) {
630                         DIE("Failed to attach shared memory");
631                 }
632
633                 // Send all configuration events to hildon-desktop
634                 reconfigure();
635         }
636
637 public:
638         virtual ~HDScalerBase()
639         {
640                 // Hide, unparent and deattach the remote texture
641                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_SHOW),
642                         0, 255, 0, 0, 0);
643                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_PARENT),
644                         0, 0, 0, 0, 0);
645                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_SHM),
646                         0, 0, 0, 0, 0);
647                 XFlush(display);
648                 // Destroy our managed window and shared memory segment
649                 XDestroyWindow(display, window);
650                 XSync(display, True);
651                 shmdt(shmaddr);
652                 shmctl(shmid, IPC_RMID, 0);
653         };
654
655         virtual uint8* getDrawBuffer() const
656         {
657                 return reinterpret_cast<uint8*>(shmaddr);
658         };
659
660         virtual unsigned int getDrawBufferPitch() const
661         {
662                 return m_w * m_Bpp;
663         };
664
665         virtual void getRenderedGUIArea(unsigned short & x, unsigned short & y,
666                                                         unsigned short & w, unsigned short & h) const
667         {
668                 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
669         };
670
671         virtual int getRatio() const
672         {
673                 return ratio_y; // TODO
674         };
675
676         virtual void prepare()
677         {
678
679         };
680
681         virtual void finish()
682         {
683                 // Send a damage event to hildon-desktop.
684                 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_DAMAGE),
685                         0, 0, m_w, m_h, 0);
686                 XSync(display, False);
687         };
688
689         virtual void pause() { };
690         virtual void resume() { };
691 };
692
693 class HDFillScaler : public HDScalerBase
694 {
695         HDFillScaler(SDL_Surface* screen, int w, int h)
696         : HDScalerBase(screen, w, h,
697                 GUI.Width / (float)w, GUI.Height / (float)h)
698         {
699         }
700
701 public:
702         class Factory : public ScalerFactory
703         {
704                 const char * getName() const
705                 {
706                         return "hdfill";
707                 }
708
709                 bool canEnable(int bpp, int w, int h) const
710                 {
711                         return true;
712                 }
713
714                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
715                 {
716                         return new HDFillScaler(screen, w, h-20);
717                 }
718         };
719
720         static const Factory factory;
721
722         virtual const char * getName() const
723         {
724                 return "hildon-desktop fill screen scaling";
725         }
726 };
727 const HDFillScaler::Factory HDFillScaler::factory;
728
729 class HDSquareScaler : public HDScalerBase
730 {
731         HDSquareScaler(SDL_Surface* screen, int w, int h, float ratio)
732         : HDScalerBase(screen, w, h, ratio, ratio)
733         {
734         }
735
736 public:
737         class Factory : public ScalerFactory
738         {
739                 const char * getName() const
740                 {
741                         return "hdsq";
742                 }
743
744                 bool canEnable(int bpp, int w, int h) const
745                 {
746                         return true;
747                 }
748
749                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
750                 {
751                         return new HDSquareScaler(screen, w, h,
752                                 fminf(GUI.Width / (float)w, GUI.Height / (float)h));
753                 }
754         };
755
756         static const Factory factory;
757
758         virtual const char * getName() const
759         {
760                 return "hildon-desktop square screen scaling";
761         }
762 };
763 const HDSquareScaler::Factory HDSquareScaler::factory;
764
765 class HDDummy : public DummyScaler
766 {
767         HDDummy(SDL_Surface* screen, int w, int h)
768         : DummyScaler(screen, w, h)
769         {
770                 hildon_set_non_compositing(true);
771         }
772         
773 public:
774         ~HDDummy()
775         {
776                 hildon_set_non_compositing(false);
777         };
778
779         class Factory : public ScalerFactory
780         {
781                 const char * getName() const
782                 {
783                         return "hddummy";
784                 }
785
786                 bool canEnable(int bpp, int w, int h) const
787                 {
788                         return Config.fullscreen; // This makes sense only in fullscreen
789                 }
790
791                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
792                 {
793                         return new HDDummy(screen, w, h);
794                 }
795         };
796
797         static const Factory factory;
798
799         const char * getName() const
800         {
801                 return "compositor disabled and no scaling";
802         }
803 };
804 const HDDummy::Factory HDDummy::factory;
805
806 class HDSW : public SWScaler
807 {
808         HDSW(SDL_Surface* screen, int w, int h)
809         : SWScaler(screen, w, h)
810         {
811                 hildon_set_non_compositing(true);
812         }
813         
814 public:
815         ~HDSW()
816         {
817                 hildon_set_non_compositing(false);
818         };
819
820         class Factory : public ScalerFactory
821         {
822                 const char * getName() const
823                 {
824                         return "hdsoft2x";
825                 }
826
827                 bool canEnable(int bpp, int w, int h) const
828                 {
829                         return Config.fullscreen; // This makes sense only in fullscreen
830                 }
831
832                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
833                 {
834                         return new HDSW(screen, w, h);
835                 }
836         };
837
838         static const Factory factory;
839
840         const char * getName() const
841         {
842                 return "compositor disabled and software 2x scaling";
843         }
844 };
845 const HDSW::Factory HDSW::factory;
846
847 #ifdef __arm__
848 class HDARM : public ARMScaler
849 {
850         HDARM(SDL_Surface* screen, int w, int h)
851         : SWScaler(screen, w, h)
852         {
853                 hildon_set_non_compositing(true);
854         }
855         
856 public:
857         ~HDARM()
858         {
859                 hildon_set_non_compositing(false);
860         };
861
862         class Factory : public ScalerFactory
863         {
864                 const char * getName() const
865                 {
866                         return "hdarm2x";
867                 }
868
869                 bool canEnable(int bpp, int w, int h) const
870                 {
871                         return Config.fullscreen; // This makes sense only in fullscreen
872                 }
873
874                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
875                 {
876                         return new HDARM(screen, w, h);
877                 }
878         };
879
880         static const Factory factory;
881
882         const char * getName() const
883         {
884                 return "compositor disabled and software ARM 2x scaling";
885         }
886 };
887 const HDSW::Factory HDSW::factory;
888 #endif /* __arm__ */
889 #endif /* CONF_HD */
890
891 #if CONF_XSP
892 class XSPScaler : public Scaler
893 {
894         SDL_Surface* m_screen;
895         SDL_Rect m_area;
896         SDL_Rect m_real_area;
897         bool m_should_enable, m_enabled; // Try to avoid flicker.
898
899         static void setDoubling(bool enable)
900         {
901                 SDL_SysWMinfo wminfo;
902                 SDL_VERSION(&wminfo.version);
903                 if ( SDL_GetWMInfo(&wminfo) ) {
904                         Display *dpy = wminfo.info.x11.display;
905                         XSPSetPixelDoubling(dpy, 0, enable ? 1 : 0);
906                         XFlush(dpy);
907                 }
908         }
909
910         XSPScaler(SDL_Surface* screen, int w, int h)
911         : m_screen(screen), m_should_enable(true), m_enabled(false)
912         {
913                 centerRectangle(m_area, GUI.Width, GUI.Height,
914                         w * 2, h * 2);
915
916                 m_real_area.x = m_area.x;
917                 m_real_area.y = m_area.y;
918                 m_real_area.w = m_area.w / 2;
919                 m_real_area.h = m_area.h / 2;
920         };
921 public:
922         ~XSPScaler()
923         {
924                 if (m_enabled) setDoubling(false);
925         };
926
927         class Factory : public ScalerFactory
928         {
929                 const char * getName() const
930                 {
931                         return "xsp";
932                 }
933
934                 bool canEnable(int bpp, int w, int h) const
935                 {
936                         return w * 2 < GUI.Width && h * 2 < GUI.Height;
937                 };
938
939                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
940                 {
941                         return new XSPScaler(screen, w, h);
942                 }
943         };
944
945         static const Factory factory;
946
947         const char * getName() const
948         {
949                 return "XSP pixel doubling";
950         }
951
952         uint8* getDrawBuffer() const
953         {
954                 const int Bpp = screen->format->BitsPerPixel / 8;
955                 const int pitch = screen->pitch;
956                 return ((uint8*) screen->pixels)
957                         + (m_area.x * Bpp)
958                         + (m_area.y * pitch);
959         };
960
961         unsigned int getDrawBufferPitch() const
962         {
963                 return screen->pitch;
964         };
965
966         void getRenderedGUIArea(unsigned short & x, unsigned short & y,
967                                                         unsigned short & w, unsigned short & h) const
968         {
969                 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
970         };
971
972         int getRatio() const
973         {
974                 return 2;
975         };
976
977         void prepare() 
978         {
979                 if (m_should_enable && !m_enabled) {
980                         setDoubling(true);
981                         m_enabled = true;
982                 }
983         };
984
985         void finish()
986         {
987                 SDL_UpdateRects(m_screen, 1, &m_real_area);
988         };
989
990         void pause()
991         {
992                 m_should_enable = false;
993                 if (m_enabled) {
994                         setDoubling(false);
995                         m_enabled = false;
996                 }
997         };
998
999         void resume()
1000         {
1001                 m_should_enable = true; // Will enable later
1002         };
1003 };
1004 const XSPScaler::Factory XSPScaler::factory;
1005 #endif
1006
1007 static const ScalerFactory* scalers[] = {
1008 /* More useful scalers come first */
1009 #if CONF_HD && defined(__arm__)
1010         &HDARM::factory,                                /* non-composited arm 2x scaling */
1011 #endif
1012 #if CONF_HD
1013         &HDSquareScaler::factory,               /* h-d assisted square scaling */
1014         &HDSW::factory,                                 /* non-composited soft 2x scaling */
1015 #endif
1016 #if CONF_XSP
1017         &XSPScaler::factory,                    /* n8x0 pixel doubling */
1018 #endif
1019 #ifdef __arm__
1020         &ARMScaler::factory,                    /* arm 2x scaling */
1021 #endif
1022         &SWScaler::factory,                             /* soft 2x scaling */
1023 #if CONF_HD
1024         &HDDummy::factory,                              /* non composited */
1025 #endif
1026         &DummyScaler::factory,                  /* failsafe */
1027 /* The following scalers will not be automatically enabled, no matter what */
1028 #if CONF_HD
1029         &HDFillScaler::factory,
1030 #endif
1031 };
1032
1033 /* Entry point functions */
1034
1035 const ScalerFactory* searchForScaler(int bpp, int w, int h)
1036 {
1037         const int n = sizeof(scalers) / sizeof(ScalerFactory*);
1038         int i;
1039
1040         if (Config.scaler && strcasecmp(Config.scaler, "help") == 0 ) {
1041                 // List scalers
1042                 printf("Scalers list:\n");
1043                 for (i = 0; i < n; i++) {
1044                         printf(" %s\n", scalers[i]->getName());
1045                 }
1046                 DIE("End of scalers list");
1047         } else if (Config.scaler && strcasecmp(Config.scaler, "auto") != 0 ) {
1048                 // We prefer a specific scaler
1049                 for (i = 0; i < n; i++) {
1050                         if (strcasecmp(scalers[i]->getName(), Config.scaler) == 0) {
1051                                 if (scalers[i]->canEnable(bpp, w, h)) {
1052                                         // Found the scaler selected by the user, and we can use it.
1053                                         return scalers[i];
1054                                 } else {
1055                                         fprintf(stderr,
1056                                                 "Selected scaler '%s' cannot be enabled in this mode\n",
1057                                                 Config.scaler);
1058                                         break; // Fallback to another scaler.
1059                                 }
1060                         }
1061                 }
1062                 if (i == n) {
1063                         fprintf(stderr, "Selected scaler '%s' does not exist\n",
1064                                 Config.scaler);
1065                 }
1066         }
1067
1068         // Just try them all now, in a buildtime set priority.
1069         for (i = 0; i < n; i++) {
1070                 if (scalers[i]->canEnable(bpp, w, h)) {
1071                         return scalers[i];
1072                 }
1073         }
1074
1075         DIE("Can't use any scaler; this shouldn't happen.");
1076 }
1077