removing uneeded check
[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                                         DIE("Cannot use selected scaler");
533                                 }
534                                 return scalers[i];
535                         }
536                 }
537                 DIE("Selected scaler '%s' does not exist", Config.scaler);
538         } else {
539                 // Just try them all
540                 for (i = 0; i < n; i++) {
541                         if (scalers[i]->canEnable(bpp, w, h)) {
542                                 return scalers[i];
543                         }
544                 }
545                 DIE("Can't use any scaler");
546         }
547 }
548
549 static void calculateScreenSize()
550 {
551         SDL_SysWMinfo wminfo;
552         SDL_VERSION(&wminfo.version);
553
554         if ( SDL_GetWMInfo(&wminfo) ) {
555                 Display *dpy = wminfo.info.x11.display;
556                 Window w;
557                 SDL_Rect* size;
558                 XWindowAttributes xwa;
559
560                 if (Config.fullscreen) {
561                         w =  wminfo.info.x11.fswindow;
562                         size = &screenSize;
563                         gotScreenSize = true;
564                 } else {
565                         w =  wminfo.info.x11.wmwindow;
566                         size = &windowSize;
567                         gotWindowSize = true;
568                 }
569
570                 XGetWindowAttributes(dpy, w, &xwa);
571                 size->x = xwa.x;
572                 size->y = xwa.y;
573                 size->w = xwa.width;
574                 size->h = xwa.height;
575         }
576 }
577
578 void S9xSetTitle(const char *title)
579 {
580         SDL_SysWMinfo info;
581         SDL_VERSION(&info.version);
582         if ( SDL_GetWMInfo(&info) ) {
583                 Display *dpy = info.info.x11.display;
584                 Window win;
585                 if (dpy) {
586                         win = info.info.x11.fswindow;
587                         if (win) XStoreName(dpy, win, title);
588                         win = info.info.x11.wmwindow;
589                         if (win) XStoreName(dpy, win, title);
590                 }
591         }
592 }
593
594 static void freeVideoSurface()
595 {
596         screen = 0; // There's no need to free the screen surface.
597         GFX.Screen = 0;
598
599         free(GFX.SubScreen); GFX.SubScreen = 0;
600         free(GFX.ZBuffer); GFX.ZBuffer = 0;
601         free(GFX.SubZBuffer); GFX.SubZBuffer = 0;
602
603         delete scaler; scaler = 0;
604 }
605
606 static void setupVideoSurface()
607 {
608         // Real surface area.
609         const unsigned gameWidth = IMAGE_WIDTH;
610         const unsigned gameHeight = IMAGE_HEIGHT;
611
612 #ifdef MAEMO
613         if ((Config.fullscreen && !gotScreenSize) ||
614                 (!Config.fullscreen && !gotWindowSize)) {
615                 // Do a first try, in order to get window/screen size
616                 screen = SDL_SetVideoMode(gameWidth, gameHeight, 16,
617                         SDL_SWSURFACE | SDL_RESIZABLE |
618                         (Config.fullscreen ? SDL_FULLSCREEN : 0));
619                 if (!screen) DIE("SDL_SetVideoMode: %s", SDL_GetError());
620                 calculateScreenSize();
621         }
622         if (Config.fullscreen) {
623                 GUI.Width = screenSize.w;
624                 GUI.Height = screenSize.h;
625         } else {
626                 GUI.Width = windowSize.w;
627                 GUI.Height = windowSize.h;
628         }
629 #else
630         GUI.Width = gameWidth;
631         GUI.Height = gameHeight;
632 #endif
633
634         // Safeguard
635         if (gameHeight > GUI.Height || gameWidth > GUI.Width)
636                 DIE("Video is larger than window size!");
637
638         const ScalerFactory* sFactory =
639                 searchForScaler(Settings.SixteenBit ? 16 : 8, gameWidth, gameHeight);
640
641         screen = SDL_SetVideoMode(GUI.Width, GUI.Height,
642                                                                 Settings.SixteenBit ? 16 : 8,
643                                                                 SDL_SWSURFACE |
644                                                                 (Config.fullscreen ? SDL_FULLSCREEN : 0));
645         if (!screen)
646                 DIE("SDL_SetVideoMode: %s", SDL_GetError());
647         
648         SDL_ShowCursor(SDL_DISABLE);
649
650         scaler = sFactory->instantiate(screen, gameWidth, gameHeight);
651
652         // We get pitch surface values from SDL
653         GFX.RealPitch = GFX.Pitch = scaler->getDrawBufferPitch();
654         GFX.ZPitch = GFX.Pitch / 2; // gfx & tile.cpp depend on this, unfortunately.
655         GFX.PixSize = screen->format->BitsPerPixel / 8;
656         
657         GFX.Screen = scaler->getDrawBuffer();
658         GFX.SubScreen = (uint8 *) malloc(GFX.Pitch * IMAGE_HEIGHT);
659         GFX.ZBuffer =  (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
660         GFX.SubZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
661
662         GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
663         GFX.PPL = GFX.Pitch >> 1;
664         GFX.PPLx2 = GFX.Pitch;
665
666         scaler->getRenderedGUIArea(GUI.RenderX, GUI.RenderY, GUI.RenderW, GUI.RenderH);
667         GUI.Scale = scaler->getRatio();
668
669         printf("Video: %dx%d (%dx%d output), %hu bits per pixel, %s, %s\n",
670                 gameWidth, gameHeight,
671                 screen->w, screen->h, screen->format->BitsPerPixel,
672                 Config.fullscreen ? "fullscreen" : "windowed",
673                 scaler->getName());
674 }
675
676 static void drawOnscreenControls()
677 {
678         if (Config.touchscreenInput) {
679                 S9xInputScreenChanged();
680                 if (Config.touchscreenShow) {
681                         scaler->pause();
682                         S9xInputScreenDraw(Settings.SixteenBit ? 2 : 1,
683                                                                 screen->pixels, screen->pitch);
684                         SDL_Flip(screen);
685                         scaler->resume();
686                 }
687         }
688 }
689
690 void S9xInitDisplay(int argc, const char ** argv)
691 {       
692         if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) 
693                 DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
694
695         setupVideoSurface();
696         drawOnscreenControls();
697 }
698
699 void S9xDeinitDisplay()
700 {
701         freeVideoSurface();     
702         SDL_QuitSubSystem(SDL_INIT_VIDEO);
703 }
704
705 void S9xVideoToggleFullscreen()
706 {
707         Config.fullscreen = !Config.fullscreen;
708         freeVideoSurface();
709         setupVideoSurface();
710         drawOnscreenControls();
711 }
712
713 void S9xVideoOutputFocus(bool hasFocus)
714 {
715 #if 0 // TODO
716         if (Config.xsp) {
717                 setDoubling(hasFocus);
718         } 
719 #endif
720 }
721
722 // This is here for completeness, but palette mode is useless on N8x0
723 void S9xSetPalette ()
724 {
725         if (Settings.SixteenBit) return;
726         
727         SDL_Color colors[256];
728         int brightness = IPPU.MaxBrightness *138;
729         for (int i = 0; i < 256; i++)
730         {
731                 colors[i].r = ((PPU.CGDATA[i] >> 0) & 0x1F) * brightness;
732                 colors[i].g = ((PPU.CGDATA[i] >> 5) & 0x1F) * brightness;
733                 colors[i].b = ((PPU.CGDATA[i] >> 10) & 0x1F) * brightness;
734         }
735         
736         SDL_SetColors(screen, colors, 0, 256);
737 }
738
739 bool8_32 S9xInitUpdate ()
740 {
741         scaler->prepare();
742
743         return TRUE;
744 }
745
746 bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)
747 {
748         scaler->finish();
749
750         return TRUE;
751 }
752