fail gracefully when selected scaler can't be used
[drnoksnes] / platform / sdlv.cpp
1 #include <stdio.h>
2
3 #include <X11/Xlib.h>
4 #include <X11/Xutil.h>
5 #include <SDL.h>
6 #include <SDL_syswm.h>
7
8 #if CONF_XSP
9 #       include <X11/extensions/Xsp.h>
10 #endif
11
12 #include "snes9x.h"
13 #include "platform.h"
14 #include "display.h"
15 #include "gfx.h"
16 #include "ppu.h"
17
18 #define DIE(format, ...) do { \
19                 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
20                 fprintf(stderr, format "\n", ## __VA_ARGS__); \
21                 abort(); \
22         } while (0);
23
24 struct gui GUI;
25
26 static SDL_Surface* screen;
27
28 static SDL_Rect windowSize, screenSize;
29 static bool gotWindowSize, gotScreenSize;
30
31 class Scaler;
32 /** The current scaler object */
33 static Scaler* scaler;
34
35 static void centerRectangle(SDL_Rect& result, int areaW, int areaH, int w, int h)
36 {
37         result.x = areaW / 2 - w / 2;
38         result.w = w;
39         result.y = areaH / 2 - h / 2;
40         result.h = h;
41 }
42
43 class Scaler
44 {
45 public:
46         Scaler() { };
47         virtual ~Scaler() { };
48
49         virtual const char * getName() const = 0;
50
51         virtual uint8* getDrawBuffer() const = 0;
52         virtual unsigned int getDrawBufferPitch() const = 0;
53         virtual void getRenderedGUIArea(unsigned short & x, unsigned short & y,
54                                                                         unsigned short & w, unsigned short & h)
55                                                                         const = 0;
56         virtual int getRatio() const = 0;
57         virtual void prepare() = 0;
58         virtual void finish() = 0;
59         virtual void pause() = 0;
60         virtual void resume() = 0;
61 };
62
63 class ScalerFactory
64 {
65 public:
66         ScalerFactory() { };
67         virtual ~ScalerFactory() { };
68         virtual const char * getName() const = 0;
69         virtual bool canEnable(int bpp, int w, int h) const = 0;
70         virtual Scaler* instantiate(SDL_Surface* screen, int w, int h) const = 0;
71 };
72
73 class DummyScaler : public Scaler
74 {
75         SDL_Surface * m_screen;
76         SDL_Rect m_area;
77
78         DummyScaler(SDL_Surface* screen, int w, int h)
79         : m_screen(screen)
80         {
81                 centerRectangle(m_area, GUI.Width, GUI.Height, w, h);
82         }
83 public:
84
85         ~DummyScaler()
86         {
87         };
88
89         class Factory : public ScalerFactory
90         {
91                 const char * getName() const
92                 {
93                         return "none";
94                 }
95
96                 bool canEnable(int bpp, int w, int h) const
97                 {
98                         return true;
99                 }
100
101                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
102                 {
103                         return new DummyScaler(screen, w, h);
104                 }
105         };
106
107         static const Factory factory;
108
109         const char * getName() const
110         {
111                 return "no scaling";
112         }
113
114         uint8* getDrawBuffer() const
115         {
116                 const int Bpp = screen->format->BitsPerPixel / 8;
117                 const int pitch = screen->pitch;
118                 return ((uint8*) screen->pixels)
119                         + (m_area.x * Bpp)
120                         + (m_area.y * pitch);
121         };
122
123         unsigned int getDrawBufferPitch() const
124         {
125                 return screen->pitch;
126         };
127
128         void getRenderedGUIArea(unsigned short & x, unsigned short & y,
129                                                         unsigned short & w, unsigned short & h) const
130         {
131                 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
132         };
133
134         int getRatio() const
135         {
136                 return 1;
137         };
138
139         void prepare() { };
140
141         void finish()
142         {
143                 SDL_UpdateRects(m_screen, 1, &m_area);
144         };
145
146         void pause() { };
147         void resume() { };
148 };
149 const DummyScaler::Factory DummyScaler::factory;
150
151 #ifdef __arm__
152 class ARMScaler : public Scaler
153 {
154         SDL_Surface * m_screen;
155         SDL_Rect m_area;
156         uint8 * m_surface;
157         const int m_w, m_h, m_Bpp;
158
159         ARMScaler(SDL_Surface* screen, int w, int h)
160         : m_screen(screen), m_w(w), m_h(h),
161          m_Bpp(m_screen->format->BitsPerPixel / 8)
162         {
163                 centerRectangle(m_area, GUI.Width, GUI.Height, w * 2, h * 2);
164                 m_surface = reinterpret_cast<uint8*>(malloc(w * h * m_Bpp));
165         }
166 public:
167         ~ARMScaler()
168         {
169                 free(m_surface);
170         };
171
172         class Factory : public ScalerFactory
173         {
174                 const char * getName() const
175                 {
176                         return "2x";
177                 }
178
179                 bool canEnable(int bpp, int w, int h) const
180                 {
181                         return bpp == 16 && w * 2 < GUI.Width && h * 2 < GUI.Height &&
182                                 w % 16 == 0 /* asm assumes w div by 16 */;
183                 }
184
185                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
186                 {
187                         return new ARMScaler(screen, w, h);
188                 }
189         };
190
191         static const Factory factory;
192
193         const char * getName() const
194         {
195                 return "software ARM 2x scaling";
196         }
197
198         uint8* getDrawBuffer() const
199         {
200                 return m_surface;
201         };
202
203         unsigned int getDrawBufferPitch() const
204         {
205                 return m_w * m_Bpp;
206         };
207
208         void getRenderedGUIArea(unsigned short & x, unsigned short & y,
209                                                         unsigned short & w, unsigned short & h) const
210         {
211                 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
212         };
213
214         int getRatio() const
215         {
216                 return 2;
217         };
218
219         void prepare() { };
220
221         void finish()
222         {
223                 uint16 * src = reinterpret_cast<uint16*>(m_surface);
224                 uint16 * dst = reinterpret_cast<uint16*>(
225                         ((uint8*) m_screen->pixels)
226                         + (m_area.x * m_Bpp)
227                         + (m_area.y * m_screen->pitch));
228                 const int src_pitch = m_w;
229                 const int dst_pitch = m_screen->pitch / m_Bpp;
230                 int y;
231
232                 for (y = 0; y < m_h*2; y++) {
233                         asm volatile
234                         (
235                                 "mov r0, %0; mov r1, %1; mov r2, %2;"
236                                 "stmdb r13!,{r4,r5,r6,r7,r8,r9,r10,r11,r12,r14};"
237                                 "1:     ldmia r1!,{r3,r4,r5,r6,r7,r8,r9,r10};"
238                                 "mov r14,r5,lsr #16;"
239                                 "mov r12,r5,lsl #16;"
240                                 "orr r14,r14,r14,lsl #16;"
241                                 "orr r12,r12,r12,lsr #16;"
242                                 "mov r11,r4,lsr #16;"
243                                 "mov r5,r4,lsl #16;"
244                                 "orr r11,r11,r11,lsl #16;"
245                                 "orr r5,r5,r5,lsr #16;"
246                                 "mov r4,r3,lsr #16;"
247                                 "mov r3,r3,lsl #16;"
248                                 "orr r4,r4,r4,lsl #16;"
249                                 "orr r3,r3,r3,lsr #16;"
250                                 "stmia r0!,{r3,r4,r5,r11,r12,r14};"
251                                 "mov r3,r6,lsl #16;"
252                                 "mov r4,r6,lsr #16;"
253                                 "orr r3,r3,r3,lsr #16;"
254                                 "orr r4,r4,r4,lsl #16;"
255                                 "mov r5,r7,lsl #16;"
256                                 "mov r6,r7,lsr #16;"
257                                 "orr r5,r5,r5,lsr #16;"
258                                 "orr r6,r6,r6,lsl #16;"
259                                 "mov r7,r8,lsl #16;"
260                                 "mov r8,r8,lsr #16;"
261                                 "orr r7,r7,r7,lsr #16;"
262                                 "orr r8,r8,r8,lsl #16;"
263                                 "mov r12,r10,lsr #16;"
264                                 "mov r11,r10,lsl #16;"
265                                 "orr r12,r12,r12,lsl #16;"
266                                 "orr r11,r11,r11,lsr #16;"
267                                 "mov r10,r9,lsr #16;"
268                                 "mov r9,r9,lsl #16;"
269                                 "orr r10,r10,r10,lsl #16;"
270                                 "orr r9,r9,r9,lsr #16;"
271                                 "stmia r0!,{r3,r4,r5,r6,r7,r8,r9,r10,r11,r12};"
272                                 "subs r2,r2,#16;"
273                                 "bhi 1b;"
274                                 "ldmia r13!,{r4,r5,r6,r7,r8,r9,r10,r11,r12,r14};"
275                         :
276                         : "r" (dst), "r" (src), "r" (m_w)
277                         : "r0", "r1", "r2", "r3"
278                         );
279                         dst += dst_pitch;
280                         if (y&1) src += src_pitch;
281                 }
282
283                 SDL_UpdateRects(m_screen, 1, &m_area);
284         };
285
286         void pause() { };
287         void resume() { };
288 };
289 const ARMScaler::Factory ARMScaler::factory;
290 #endif
291
292 class SWScaler : public Scaler
293 {
294         SDL_Surface * m_screen;
295         SDL_Rect m_area;
296         uint8 * m_surface;
297         const int m_w, m_h, m_Bpp;
298
299         SWScaler(SDL_Surface* screen, int w, int h)
300         : m_screen(screen), m_w(w), m_h(h),
301          m_Bpp(m_screen->format->BitsPerPixel / 8)
302         {
303                 centerRectangle(m_area, GUI.Width, GUI.Height, w * 2, h * 2);
304                 m_surface = reinterpret_cast<uint8*>(malloc(w * h * m_Bpp));
305         }
306 public:
307         ~SWScaler()
308         {
309                 free(m_surface);
310         };
311
312         class Factory : public ScalerFactory
313         {
314                 const char * getName() const
315                 {
316                         return "soft2x";
317                 }
318
319                 bool canEnable(int bpp, int w, int h) const
320                 {
321                         return w * 2 < GUI.Width && h * 2 < GUI.Height;
322                 }
323
324                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
325                 {
326                         return new SWScaler(screen, w, h);
327                 }
328         };
329
330         static const Factory factory;
331
332         const char * getName() const
333         {
334                 return "software 2x scaling";
335         }
336
337         uint8* getDrawBuffer() const
338         {
339                 return m_surface;
340         };
341
342         unsigned int getDrawBufferPitch() const
343         {
344                 return m_w * m_Bpp;
345         };
346
347         void getRenderedGUIArea(unsigned short & x, unsigned short & y,
348                                                         unsigned short & w, unsigned short & h) const
349         {
350                 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
351         };
352
353         int getRatio() const
354         {
355                 return 2;
356         };
357
358         void prepare() { };
359
360         void finish()
361         {
362                 uint16 * src = reinterpret_cast<uint16*>(m_surface);
363                 uint16 * dst = reinterpret_cast<uint16*>(
364                         ((uint8*) m_screen->pixels)
365                         + (m_area.x * m_Bpp)
366                         + (m_area.y * m_screen->pitch));
367                 const int src_pitch = m_w;
368                 const int dst_pitch = m_screen->pitch / m_Bpp;
369                 int x, y;
370
371                 for (y = 0; y < m_h*2; y++) {
372                         for (x = 0; x < m_w*2; x+=2) {
373                                 dst[x] = src[x/2];
374                                 dst[x + 1] = src[x/2];
375                         }
376                         dst += dst_pitch;
377                         if (y&1) src += src_pitch;
378                 }
379
380                 SDL_UpdateRects(m_screen, 1, &m_area);
381         };
382
383         void pause() { };
384         void resume() { };
385 };
386 const SWScaler::Factory SWScaler::factory;
387
388 #if CONF_XSP
389 class XSPScaler : public Scaler
390 {
391         SDL_Surface* m_screen;
392         SDL_Rect m_area;
393         SDL_Rect m_real_area;
394         bool m_should_enable, m_enabled; // Try to avoid flicker.
395
396         static void setDoubling(bool enable)
397         {
398                 SDL_SysWMinfo wminfo;
399                 SDL_VERSION(&wminfo.version);
400                 if ( SDL_GetWMInfo(&wminfo) ) {
401                         Display *dpy = wminfo.info.x11.display;
402                         XSPSetPixelDoubling(dpy, 0, enable ? 1 : 0);
403                         XFlush(dpy);
404                 }
405         }
406
407         XSPScaler(SDL_Surface* screen, int w, int h)
408         : m_screen(screen), m_enabled(false), m_should_enable(true)
409         {
410                 centerRectangle(m_area, GUI.Width, GUI.Height,
411                         w * 2, h * 2);
412
413                 m_real_area.x = m_area.x;
414                 m_real_area.y = m_area.y;
415                 m_real_area.w = m_area.w / 2;
416                 m_real_area.h = m_area.h / 2;
417         };
418 public:
419         ~XSPScaler()
420         {
421                 if (m_enabled) setDoubling(false);
422         };
423
424         class Factory : public ScalerFactory
425         {
426                 const char * getName() const
427                 {
428                         return "xsp";
429                 }
430
431                 bool canEnable(int bpp, int w, int h) const
432                 {
433                         return w * 2 < GUI.Width && h * 2 < GUI.Height;
434                 };
435
436                 Scaler* instantiate(SDL_Surface* screen, int w, int h) const
437                 {
438                         return new XSPScaler(screen, w, h);
439                 }
440         };
441
442         static const Factory factory;
443
444         const char * getName() const
445         {
446                 return "XSP pixel doubling";
447         }
448
449         uint8* getDrawBuffer() const
450         {
451                 const int Bpp = screen->format->BitsPerPixel / 8;
452                 const int pitch = screen->pitch;
453                 return ((uint8*) screen->pixels)
454                         + (m_area.x * Bpp)
455                         + (m_area.y * pitch);
456         };
457
458         unsigned int getDrawBufferPitch() const
459         {
460                 return screen->pitch;
461         };
462
463         void getRenderedGUIArea(unsigned short & x, unsigned short & y,
464                                                         unsigned short & w, unsigned short & h) const
465         {
466                 x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
467         };
468
469         int getRatio() const
470         {
471                 return 2;
472         };
473
474         void prepare() 
475         {
476                 if (m_should_enable && !m_enabled) {
477                         setDoubling(true);
478                         m_enabled = true;
479                 }
480         };
481
482         void finish()
483         {
484                 SDL_UpdateRects(m_screen, 1, &m_real_area);
485         };
486
487         void pause()
488         {
489                 m_should_enable = false;
490                 if (m_enabled) {
491                         setDoubling(false);
492                         m_enabled = false;
493                 }
494         };
495
496         void resume()
497         {
498                 m_should_enable = true; // Will enable later
499         };
500 };
501 const XSPScaler::Factory XSPScaler::factory;
502 #endif
503
504 static const ScalerFactory* scalers[] = {
505 #if CONF_XSP
506         &XSPScaler::factory,
507 #endif
508 #ifdef __arm__
509         &ARMScaler::factory,
510 #endif
511         &SWScaler::factory,
512         &DummyScaler::factory
513 };
514
515 static const ScalerFactory* searchForScaler(int bpp, int w, int h)
516 {
517         const int n = sizeof(scalers) / sizeof(ScalerFactory*);
518         int i;
519
520         if (Config.scaler && strcasecmp(Config.scaler, "help") == 0 ) {
521                 // List scalers
522                 printf("Scalers list:\n");
523                 for (i = 0; i < n; i++) {
524                         printf(" %s\n", scalers[i]->getName());
525                 }
526                 DIE("End of scalers list");
527         } else if (Config.scaler && strcasecmp(Config.scaler, "auto") != 0 ) {
528                 // We prefer a specific scaler
529                 for (i = 0; i < n; i++) {
530                         if (strcasecmp(scalers[i]->getName(), Config.scaler) == 0) {
531                                 if (scalers[i]->canEnable(bpp, w, h)) {
532                                         // Found the scaler selected by the user, and we can use it.
533                                         return scalers[i];
534                                 } else {
535                                         fprintf(stderr, "Selected scaler '%s' cannot be enabled\n",
536                                                 Config.scaler);
537                                 }
538                         }
539                 }
540                 fprintf(stderr, "Selected scaler '%s' does not exist\n", Config.scaler);
541         }
542
543         // Just try them all now, in a set priority.
544         for (i = 0; i < n; i++) {
545                 if (scalers[i]->canEnable(bpp, w, h)) {
546                         return scalers[i];
547                 }
548         }
549
550         DIE("Can't use any scaler; this shouldn't happen.");
551 }
552
553 static void calculateScreenSize()
554 {
555         SDL_SysWMinfo wminfo;
556         SDL_VERSION(&wminfo.version);
557
558         if ( SDL_GetWMInfo(&wminfo) ) {
559                 Display *dpy = wminfo.info.x11.display;
560                 Window w;
561                 SDL_Rect* size;
562                 XWindowAttributes xwa;
563
564                 if (Config.fullscreen) {
565                         w =  wminfo.info.x11.fswindow;
566                         size = &screenSize;
567                         gotScreenSize = true;
568                 } else {
569                         w =  wminfo.info.x11.wmwindow;
570                         size = &windowSize;
571                         gotWindowSize = true;
572                 }
573
574                 XGetWindowAttributes(dpy, w, &xwa);
575                 size->x = xwa.x;
576                 size->y = xwa.y;
577                 size->w = xwa.width;
578                 size->h = xwa.height;
579         }
580 }
581
582 void S9xSetTitle(const char *title)
583 {
584         SDL_SysWMinfo info;
585         SDL_VERSION(&info.version);
586         if ( SDL_GetWMInfo(&info) ) {
587                 Display *dpy = info.info.x11.display;
588                 Window win;
589                 if (dpy) {
590                         win = info.info.x11.fswindow;
591                         if (win) XStoreName(dpy, win, title);
592                         win = info.info.x11.wmwindow;
593                         if (win) XStoreName(dpy, win, title);
594                 }
595         }
596 }
597
598 static void freeVideoSurface()
599 {
600         screen = 0; // There's no need to free the screen surface.
601         GFX.Screen = 0;
602
603         free(GFX.SubScreen); GFX.SubScreen = 0;
604         free(GFX.ZBuffer); GFX.ZBuffer = 0;
605         free(GFX.SubZBuffer); GFX.SubZBuffer = 0;
606
607         delete scaler; scaler = 0;
608 }
609
610 static void setupVideoSurface()
611 {
612         // Real surface area.
613         const unsigned gameWidth = IMAGE_WIDTH;
614         const unsigned gameHeight = IMAGE_HEIGHT;
615
616 #ifdef MAEMO
617         if ((Config.fullscreen && !gotScreenSize) ||
618                 (!Config.fullscreen && !gotWindowSize)) {
619                 // Do a first try, in order to get window/screen size
620                 screen = SDL_SetVideoMode(gameWidth, gameHeight, 16,
621                         SDL_SWSURFACE | SDL_RESIZABLE |
622                         (Config.fullscreen ? SDL_FULLSCREEN : 0));
623                 if (!screen) DIE("SDL_SetVideoMode: %s", SDL_GetError());
624                 calculateScreenSize();
625         }
626         if (Config.fullscreen) {
627                 GUI.Width = screenSize.w;
628                 GUI.Height = screenSize.h;
629         } else {
630                 GUI.Width = windowSize.w;
631                 GUI.Height = windowSize.h;
632         }
633 #else
634         GUI.Width = gameWidth;
635         GUI.Height = gameHeight;
636 #endif
637
638         // Safeguard
639         if (gameHeight > GUI.Height || gameWidth > GUI.Width)
640                 DIE("Video is larger than window size!");
641
642         const ScalerFactory* sFactory =
643                 searchForScaler(Settings.SixteenBit ? 16 : 8, gameWidth, gameHeight);
644
645         screen = SDL_SetVideoMode(GUI.Width, GUI.Height,
646                                                                 Settings.SixteenBit ? 16 : 8,
647                                                                 SDL_SWSURFACE |
648                                                                 (Config.fullscreen ? SDL_FULLSCREEN : 0));
649         if (!screen)
650                 DIE("SDL_SetVideoMode: %s", SDL_GetError());
651         
652         SDL_ShowCursor(SDL_DISABLE);
653
654         scaler = sFactory->instantiate(screen, gameWidth, gameHeight);
655
656         // We get pitch surface values from SDL
657         GFX.RealPitch = GFX.Pitch = scaler->getDrawBufferPitch();
658         GFX.ZPitch = GFX.Pitch / 2; // gfx & tile.cpp depend on this, unfortunately.
659         GFX.PixSize = screen->format->BitsPerPixel / 8;
660         
661         GFX.Screen = scaler->getDrawBuffer();
662         GFX.SubScreen = (uint8 *) malloc(GFX.Pitch * IMAGE_HEIGHT);
663         GFX.ZBuffer =  (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
664         GFX.SubZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
665
666         GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
667         GFX.PPL = GFX.Pitch >> 1;
668         GFX.PPLx2 = GFX.Pitch;
669
670         scaler->getRenderedGUIArea(GUI.RenderX, GUI.RenderY, GUI.RenderW, GUI.RenderH);
671         GUI.Scale = scaler->getRatio();
672
673         printf("Video: %dx%d (%dx%d output), %hu bits per pixel, %s, %s\n",
674                 gameWidth, gameHeight,
675                 screen->w, screen->h, screen->format->BitsPerPixel,
676                 Config.fullscreen ? "fullscreen" : "windowed",
677                 scaler->getName());
678 }
679
680 static void drawOnscreenControls()
681 {
682         if (Config.touchscreenInput) {
683                 S9xInputScreenChanged();
684                 if (Config.touchscreenShow) {
685                         scaler->pause();
686                         S9xInputScreenDraw(Settings.SixteenBit ? 2 : 1,
687                                                                 screen->pixels, screen->pitch);
688                         SDL_Flip(screen);
689                         scaler->resume();
690                 }
691         }
692 }
693
694 void S9xInitDisplay(int argc, const char ** argv)
695 {       
696         if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) 
697                 DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
698
699         setupVideoSurface();
700         drawOnscreenControls();
701 }
702
703 void S9xDeinitDisplay()
704 {
705         freeVideoSurface();     
706         SDL_QuitSubSystem(SDL_INIT_VIDEO);
707 }
708
709 void S9xVideoToggleFullscreen()
710 {
711         Config.fullscreen = !Config.fullscreen;
712         freeVideoSurface();
713         setupVideoSurface();
714         drawOnscreenControls();
715 }
716
717 void S9xVideoOutputFocus(bool hasFocus)
718 {
719 #if 0 // TODO
720         if (Config.xsp) {
721                 setDoubling(hasFocus);
722         } 
723 #endif
724 }
725
726 // This is here for completeness, but palette mode is useless on N8x0
727 void S9xSetPalette ()
728 {
729         if (Settings.SixteenBit) return;
730         
731         SDL_Color colors[256];
732         int brightness = IPPU.MaxBrightness *138;
733         for (int i = 0; i < 256; i++)
734         {
735                 colors[i].r = ((PPU.CGDATA[i] >> 0) & 0x1F) * brightness;
736                 colors[i].g = ((PPU.CGDATA[i] >> 5) & 0x1F) * brightness;
737                 colors[i].b = ((PPU.CGDATA[i] >> 10) & 0x1F) * brightness;
738         }
739         
740         SDL_SetColors(screen, colors, 0, 256);
741 }
742
743 bool8_32 S9xInitUpdate ()
744 {
745         scaler->prepare();
746
747         return TRUE;
748 }
749
750 bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)
751 {
752         scaler->finish();
753
754         return TRUE;
755 }
756