9 # include <X11/extensions/Xsp.h>
12 # include <X11/Xatom.h>
23 #define DIE(format, ...) do { \
24 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
25 fprintf(stderr, format "\n", ## __VA_ARGS__); \
29 /* Helper functions */
31 static void centerRectangle(SDL_Rect& result, int areaW, int areaH, int w, int h)
33 result.x = areaW / 2 - w / 2;
35 result.y = areaH / 2 - h / 2;
39 /* Base scaler for stupid scalers */
40 /** Does nothing but center the image */
41 class DummyScaler : public Scaler
43 SDL_Surface * m_screen;
47 DummyScaler(SDL_Surface* screen, int w, int h)
50 centerRectangle(m_area, GUI.Width, GUI.Height, w, h);
59 class Factory : public ScalerFactory
61 const char * getName() const
66 bool canEnable(int bpp, int w, int h) const
71 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
73 return new DummyScaler(screen, w, h);
77 static const Factory factory;
79 virtual const char * getName() const
84 virtual uint8* getDrawBuffer() const
86 const int Bpp = screen->format->BitsPerPixel / 8;
87 const int pitch = screen->pitch;
88 return ((uint8*) screen->pixels)
93 virtual unsigned int getDrawBufferPitch() const
98 virtual void getRenderedGUIArea(unsigned short & x, unsigned short & y,
99 unsigned short & w, unsigned short & h) const
101 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
104 virtual void getRatio(float & x, float & y) const
109 virtual void prepare() { };
111 virtual void finish()
113 SDL_UpdateRects(m_screen, 1, &m_area);
116 virtual void pause() { };
117 virtual void resume() { };
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 const SWScaler::Factory SWScaler::factory;
220 /* Platform specific scalers */
223 class ARMScaler : public Scaler
225 SDL_Surface * m_screen;
228 const int m_w, m_h, m_Bpp;
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)
235 centerRectangle(m_area, GUI.Width, GUI.Height, w * 2, h * 2);
236 m_surface = reinterpret_cast<uint8*>(malloc(w * h * m_Bpp));
244 class Factory : public ScalerFactory
246 const char * getName() const
251 bool canEnable(int bpp, int w, int h) const
253 return bpp == 16 && w * 2 < GUI.Width && h * 2 < GUI.Height &&
254 w % 16 == 0 /* asm assumes w div by 16 */;
257 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
259 return new ARMScaler(screen, w, h);
263 static const Factory factory;
265 virtual const char * getName() const
267 return "software ARM 2x scaling";
270 uint8* getDrawBuffer() const
275 unsigned int getDrawBufferPitch() const
280 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
281 unsigned short & w, unsigned short & h) const
283 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
286 void getRatio(float & x, float & y) const
295 uint16 * src = reinterpret_cast<uint16*>(m_surface);
296 uint16 * dst = reinterpret_cast<uint16*>(
297 ((uint8*) m_screen->pixels)
299 + (m_area.y * m_screen->pitch));
300 const int src_pitch = m_w;
301 const int dst_pitch = m_screen->pitch / m_Bpp;
304 for (y = 0; y < m_h*2; y++) {
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;"
316 "orr r11,r11,r11,lsl #16;"
317 "orr r5,r5,r5,lsr #16;"
320 "orr r4,r4,r4,lsl #16;"
321 "orr r3,r3,r3,lsr #16;"
322 "stmia r0!,{r3,r4,r5,r11,r12,r14};"
325 "orr r3,r3,r3,lsr #16;"
326 "orr r4,r4,r4,lsl #16;"
329 "orr r5,r5,r5,lsr #16;"
330 "orr r6,r6,r6,lsl #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;"
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};"
346 "ldmia r13!,{r4,r5,r6,r7,r8,r9,r10,r11,r12,r14};"
348 : "r" (dst), "r" (src), "r" (m_w)
349 : "r0", "r1", "r2", "r3"
352 if (y&1) src += src_pitch;
355 SDL_UpdateRects(m_screen, 1, &m_area);
361 const ARMScaler::Factory ARMScaler::factory;
366 class HDScalerBase : public Scaler
368 SDL_Surface * m_screen;
370 const int m_w, m_h, m_Bpp;
371 const float ratio_x, ratio_y;
373 // SDL/X11 stuff we save for faster access.
377 // Shared memory segment info.
383 /** Sends a message to hildon-desktop.
384 * This function comes mostly straight from libhildon.
386 void sendMessage(Atom message_type,
387 uint32 l0, uint32 l1, uint32 l2, uint32 l3, uint32 l4)
389 XEvent event = { 0 };
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;
401 XSendEvent (display, window, True,
406 /** Sends all configuration parameters for the remote texture. */
411 if (Config.fullscreen) {
412 parent = WMinfo.info.x11.fswindow;
414 parent = WMinfo.info.x11.wmwindow;
415 yoffset = 60; // Hardcode the title bar size for now.
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),
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)
436 centerRectangle(m_area, GUI.Width, GUI.Height, w * r_x, h * r_y);
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.
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));
447 display = WMinfo.info.x11.display;
449 // The parent window needs to be mapped, so we sync it.
450 XSync(display, True);
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);
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.
469 XNextEvent(display, &e);
472 if (e.xproperty.atom ==
473 HDATOM(_HILDON_TEXTURE_CLIENT_READY)) {
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);
486 DIE("Failed to create shared memory");
488 shmaddr = shmat(shmid, 0, 0);
489 if (shmaddr == (void*)-1) {
490 DIE("Failed to attach shared memory");
493 // Send all configuration events to hildon-desktop
498 virtual ~HDScalerBase()
500 // Hide, unparent and deattach the remote texture
501 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_SHOW),
503 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_PARENT),
505 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_SHM),
508 // Destroy our managed window and shared memory segment
509 XDestroyWindow(display, window);
510 XSync(display, True);
512 shmctl(shmid, IPC_RMID, 0);
515 virtual uint8* getDrawBuffer() const
517 return reinterpret_cast<uint8*>(shmaddr);
520 virtual unsigned int getDrawBufferPitch() const
525 virtual void getRenderedGUIArea(unsigned short & x, unsigned short & y,
526 unsigned short & w, unsigned short & h) const
528 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
531 virtual void getRatio(float & x, float & y) const
533 x = ratio_x; y = ratio_y;
536 virtual void prepare()
541 virtual void finish()
543 // Send a damage event to hildon-desktop.
544 sendMessage(HDATOM(_HILDON_TEXTURE_CLIENT_MESSAGE_DAMAGE),
546 XSync(display, False);
549 virtual void pause() { };
550 virtual void resume() { };
553 class HDFillScaler : public HDScalerBase
555 HDFillScaler(SDL_Surface* screen, int w, int h)
556 : HDScalerBase(screen, w, h,
557 GUI.Width / (float)w, GUI.Height / (float)h)
562 class Factory : public ScalerFactory
564 const char * getName() const
569 bool canEnable(int bpp, int w, int h) const
574 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
576 return new HDFillScaler(screen, w, h-20);
580 static const Factory factory;
582 virtual const char * getName() const
584 return "hildon-desktop fill screen scaling";
587 const HDFillScaler::Factory HDFillScaler::factory;
589 class HDSquareScaler : public HDScalerBase
591 HDSquareScaler(SDL_Surface* screen, int w, int h, float ratio)
592 : HDScalerBase(screen, w, h, ratio, ratio)
597 class Factory : public ScalerFactory
599 const char * getName() const
604 bool canEnable(int bpp, int w, int h) const
609 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
611 return new HDSquareScaler(screen, w, h,
612 fminf(GUI.Width / (float)w, GUI.Height / (float)h));
616 static const Factory factory;
618 virtual const char * getName() const
620 return "hildon-desktop square screen scaling";
623 const HDSquareScaler::Factory HDSquareScaler::factory;
625 class HDDummy : public DummyScaler
627 HDDummy(SDL_Surface* screen, int w, int h)
628 : DummyScaler(screen, w, h)
630 hd_set_non_compositing(true);
636 hd_set_non_compositing(false);
639 class Factory : public ScalerFactory
641 const char * getName() const
646 bool canEnable(int bpp, int w, int h) const
648 return Config.fullscreen; // This makes sense only in fullscreen
651 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
653 return new HDDummy(screen, w, h);
657 static const Factory factory;
659 const char * getName() const
661 return "compositor disabled and no scaling";
664 const HDDummy::Factory HDDummy::factory;
666 class HDSW : public SWScaler
668 HDSW(SDL_Surface* screen, int w, int h)
669 : SWScaler(screen, w, h)
671 hd_set_non_compositing(true);
677 hd_set_non_compositing(false);
680 class Factory : public ScalerFactory
682 const char * getName() const
687 bool canEnable(int bpp, int w, int h) const
689 return Config.fullscreen; // This makes sense only in fullscreen
692 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
694 return new HDSW(screen, w, h);
698 static const Factory factory;
700 const char * getName() const
702 return "compositor disabled and software 2x scaling";
705 const HDSW::Factory HDSW::factory;
708 class HDARM : public ARMScaler
710 HDARM(SDL_Surface* screen, int w, int h)
711 : ARMScaler(screen, w, h)
713 hildon_set_non_compositing(true);
719 hildon_set_non_compositing(false);
722 class Factory : public ScalerFactory
724 const char * getName() const
729 bool canEnable(int bpp, int w, int h) const
731 return Config.fullscreen; // This makes sense only in fullscreen
734 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
736 return new HDARM(screen, w, h);
740 static const Factory factory;
742 const char * getName() const
744 return "compositor disabled and software ARM 2x scaling";
747 const HDARM::Factory HDARM::factory;
752 class XSPScaler : public Scaler
754 SDL_Surface* m_screen;
756 SDL_Rect m_real_area;
757 bool m_should_enable, m_enabled; // Try to avoid flicker.
759 static void setDoubling(bool enable)
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);
770 XSPScaler(SDL_Surface* screen, int w, int h)
771 : m_screen(screen), m_should_enable(true), m_enabled(false)
773 centerRectangle(m_area, GUI.Width, GUI.Height,
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;
784 if (m_enabled) setDoubling(false);
787 class Factory : public ScalerFactory
789 const char * getName() const
794 bool canEnable(int bpp, int w, int h) const
796 return w * 2 < GUI.Width && h * 2 < GUI.Height;
799 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
801 return new XSPScaler(screen, w, h);
805 static const Factory factory;
807 const char * getName() const
809 return "XSP pixel doubling";
812 uint8* getDrawBuffer() const
814 const int Bpp = screen->format->BitsPerPixel / 8;
815 const int pitch = screen->pitch;
816 return ((uint8*) screen->pixels)
818 + (m_area.y * pitch);
821 unsigned int getDrawBufferPitch() const
823 return screen->pitch;
826 void getRenderedGUIArea(unsigned short & x, unsigned short & y,
827 unsigned short & w, unsigned short & h) const
829 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
832 void getRatio(float & x, float & y) const
839 if (m_should_enable && !m_enabled) {
847 SDL_UpdateRects(m_screen, 1, &m_real_area);
852 m_should_enable = false;
861 m_should_enable = true; // Will enable later
864 const XSPScaler::Factory XSPScaler::factory;
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 */
873 &HDSquareScaler::factory, /* h-d assisted square scaling */
874 &HDSW::factory, /* non-composited soft 2x scaling */
877 &XSPScaler::factory, /* n8x0 pixel doubling */
880 &ARMScaler::factory, /* arm 2x scaling */
882 &SWScaler::factory, /* soft 2x scaling */
884 &HDDummy::factory, /* non composited */
886 &DummyScaler::factory, /* failsafe */
887 /* The following scalers will not be automatically enabled, no matter what */
889 &HDFillScaler::factory,
893 /* Entry point functions */
895 const ScalerFactory* searchForScaler(int bpp, int w, int h)
897 const int n = sizeof(scalers) / sizeof(ScalerFactory*);
900 if (Config.scaler && strcasecmp(Config.scaler, "help") == 0 ) {
902 printf("Scalers list:\n");
903 for (i = 0; i < n; i++) {
904 printf(" %s\n", scalers[i]->getName());
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.
916 "Selected scaler '%s' cannot be enabled in this mode\n",
918 break; // Fallback to another scaler.
923 fprintf(stderr, "Selected scaler '%s' does not exist\n",
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)) {
935 DIE("Can't use any scaler; this shouldn't happen.");