can now build outside maemo
[drnoksnes] / platform / sdlv.cpp
index e48c6fa..e45a804 100644 (file)
@@ -56,6 +56,8 @@ public:
        virtual int getRatio() const = 0;
        virtual void prepare() = 0;
        virtual void finish() = 0;
+       virtual void pause() = 0;
+       virtual void resume() = 0;
 };
 
 class ScalerFactory
@@ -64,7 +66,7 @@ public:
        ScalerFactory() { };
        virtual ~ScalerFactory() { };
        virtual const char * getName() const = 0;
-       virtual bool canEnable(int w, int h) const = 0;
+       virtual bool canEnable(int bpp, int w, int h) const = 0;
        virtual Scaler* instantiate(SDL_Surface* screen, int w, int h) const = 0;
 };
 
@@ -91,7 +93,7 @@ public:
                        return "none";
                }
 
-               bool canEnable(int w, int h) const
+               bool canEnable(int bpp, int w, int h) const
                {
                        return true;
                }
@@ -140,9 +142,153 @@ public:
        {
                SDL_UpdateRects(m_screen, 1, &m_area);
        };
+
+       void pause() { };
+       void resume() { };
 };
 const DummyScaler::Factory DummyScaler::factory;
 
+#ifdef __arm__
+class ARMScaler : public Scaler
+{
+       SDL_Surface * m_screen;
+       SDL_Rect m_area;
+       uint8 * m_surface;
+       const int m_w, m_h, m_Bpp;
+
+       ARMScaler(SDL_Surface* screen, int w, int h)
+       : m_screen(screen), m_w(w), m_h(h),
+        m_Bpp(m_screen->format->BitsPerPixel / 8)
+       {
+               centerRectangle(m_area, GUI.Width, GUI.Height, w * 2, h * 2);
+               m_surface = reinterpret_cast<uint8*>(malloc(w * h * m_Bpp));
+       }
+public:
+       ~ARMScaler()
+       {
+               free(m_surface);
+       };
+
+       class Factory : public ScalerFactory
+       {
+               const char * getName() const
+               {
+                       return "2x";
+               }
+
+               bool canEnable(int bpp, int w, int h) const
+               {
+                       return bpp == 16 && w * 2 < GUI.Width && h * 2 < GUI.Height &&
+                               w % 16 == 0 /* asm assumes w div by 16 */;
+               }
+
+               Scaler* instantiate(SDL_Surface* screen, int w, int h) const
+               {
+                       return new ARMScaler(screen, w, h);
+               }
+       };
+
+       static const Factory factory;
+
+       const char * getName() const
+       {
+               return "software ARM 2x scaling";
+       }
+
+       uint8* getDrawBuffer() const
+       {
+               return m_surface;
+       };
+
+       unsigned int getDrawBufferPitch() const
+       {
+               return m_w * m_Bpp;
+       };
+
+       void getRenderedGUIArea(unsigned short & x, unsigned short & y,
+                                                       unsigned short & w, unsigned short & h) const
+       {
+               x = m_area.x; y = m_area.y; w = m_area.w; h = m_area.h;
+       };
+
+       int getRatio() const
+       {
+               return 2;
+       };
+
+       void prepare() { };
+
+       void finish()
+       {
+               uint16 * src = reinterpret_cast<uint16*>(m_surface);
+               uint16 * dst = reinterpret_cast<uint16*>(
+                       ((uint8*) m_screen->pixels)
+                       + (m_area.x * m_Bpp)
+                       + (m_area.y * m_screen->pitch));
+               const int src_pitch = m_w;
+               const int dst_pitch = m_screen->pitch / m_Bpp;
+               int y;
+
+               for (y = 0; y < m_h*2; y++) {
+                       asm volatile
+                       (
+                               "mov r0, %0; mov r1, %1; mov r2, %2;"
+                               "stmdb r13!,{r4,r5,r6,r7,r8,r9,r10,r11,r12,r14};"
+                               "1:     ldmia r1!,{r3,r4,r5,r6,r7,r8,r9,r10};"
+                               "mov r14,r5,lsr #16;"
+                               "mov r12,r5,lsl #16;"
+                               "orr r14,r14,r14,lsl #16;"
+                               "orr r12,r12,r12,lsr #16;"
+                               "mov r11,r4,lsr #16;"
+                               "mov r5,r4,lsl #16;"
+                               "orr r11,r11,r11,lsl #16;"
+                               "orr r5,r5,r5,lsr #16;"
+                               "mov r4,r3,lsr #16;"
+                               "mov r3,r3,lsl #16;"
+                               "orr r4,r4,r4,lsl #16;"
+                               "orr r3,r3,r3,lsr #16;"
+                               "stmia r0!,{r3,r4,r5,r11,r12,r14};"
+                               "mov r3,r6,lsl #16;"
+                               "mov r4,r6,lsr #16;"
+                               "orr r3,r3,r3,lsr #16;"
+                               "orr r4,r4,r4,lsl #16;"
+                               "mov r5,r7,lsl #16;"
+                               "mov r6,r7,lsr #16;"
+                               "orr r5,r5,r5,lsr #16;"
+                               "orr r6,r6,r6,lsl #16;"
+                               "mov r7,r8,lsl #16;"
+                               "mov r8,r8,lsr #16;"
+                               "orr r7,r7,r7,lsr #16;"
+                               "orr r8,r8,r8,lsl #16;"
+                               "mov r12,r10,lsr #16;"
+                               "mov r11,r10,lsl #16;"
+                               "orr r12,r12,r12,lsl #16;"
+                               "orr r11,r11,r11,lsr #16;"
+                               "mov r10,r9,lsr #16;"
+                               "mov r9,r9,lsl #16;"
+                               "orr r10,r10,r10,lsl #16;"
+                               "orr r9,r9,r9,lsr #16;"
+                               "stmia r0!,{r3,r4,r5,r6,r7,r8,r9,r10,r11,r12};"
+                               "subs r2,r2,#16;"
+                               "bhi 1b;"
+                               "ldmia r13!,{r4,r5,r6,r7,r8,r9,r10,r11,r12,r14};"
+                       :
+                       : "r" (dst), "r" (src), "r" (m_w)
+                       : "r0", "r1", "r2", "r3"
+                       );
+                       dst += dst_pitch;
+                       if (y&1) src += src_pitch;
+               }
+
+               SDL_UpdateRects(m_screen, 1, &m_area);
+       };
+
+       void pause() { };
+       void resume() { };
+};
+const ARMScaler::Factory ARMScaler::factory;
+#endif
+
 class SWScaler : public Scaler
 {
        SDL_Surface * m_screen;
@@ -167,10 +313,10 @@ public:
        {
                const char * getName() const
                {
-                       return "2x";
+                       return "soft2x";
                }
 
-               bool canEnable(int w, int h) const
+               bool canEnable(int bpp, int w, int h) const
                {
                        return w * 2 < GUI.Width && h * 2 < GUI.Height;
                }
@@ -233,6 +379,9 @@ public:
 
                SDL_UpdateRects(m_screen, 1, &m_area);
        };
+
+       void pause() { };
+       void resume() { };
 };
 const SWScaler::Factory SWScaler::factory;
 
@@ -242,6 +391,7 @@ class XSPScaler : public Scaler
        SDL_Surface* m_screen;
        SDL_Rect m_area;
        SDL_Rect m_real_area;
+       bool m_should_enable, m_enabled; // Try to avoid flicker.
 
        static void setDoubling(bool enable)
        {
@@ -255,11 +405,10 @@ class XSPScaler : public Scaler
        }
 
        XSPScaler(SDL_Surface* screen, int w, int h)
-       : m_screen(screen)
+       : m_screen(screen), m_should_enable(true), m_enabled(false)
        {
                centerRectangle(m_area, GUI.Width, GUI.Height,
                        w * 2, h * 2);
-               setDoubling(true);
 
                m_real_area.x = m_area.x;
                m_real_area.y = m_area.y;
@@ -269,7 +418,7 @@ class XSPScaler : public Scaler
 public:
        ~XSPScaler()
        {
-               setDoubling(false);
+               if (m_enabled) setDoubling(false);
        };
 
        class Factory : public ScalerFactory
@@ -279,7 +428,7 @@ public:
                        return "xsp";
                }
 
-               bool canEnable(int w, int h) const
+               bool canEnable(int bpp, int w, int h) const
                {
                        return w * 2 < GUI.Width && h * 2 < GUI.Height;
                };
@@ -322,12 +471,32 @@ public:
                return 2;
        };
 
-       void prepare() { };
+       void prepare() 
+       {
+               if (m_should_enable && !m_enabled) {
+                       setDoubling(true);
+                       m_enabled = true;
+               }
+       };
 
        void finish()
        {
                SDL_UpdateRects(m_screen, 1, &m_real_area);
        };
+
+       void pause()
+       {
+               m_should_enable = false;
+               if (m_enabled) {
+                       setDoubling(false);
+                       m_enabled = false;
+               }
+       };
+
+       void resume()
+       {
+               m_should_enable = true; // Will enable later
+       };
 };
 const XSPScaler::Factory XSPScaler::factory;
 #endif
@@ -336,11 +505,14 @@ static const ScalerFactory* scalers[] = {
 #if CONF_XSP
        &XSPScaler::factory,
 #endif
+#ifdef __arm__
+       &ARMScaler::factory,
+#endif
        &SWScaler::factory,
        &DummyScaler::factory
 };
 
-static const ScalerFactory* searchForScaler(int w, int h)
+static const ScalerFactory* searchForScaler(int bpp, int w, int h)
 {
        const int n = sizeof(scalers) / sizeof(ScalerFactory*);
        int i;
@@ -356,22 +528,31 @@ static const ScalerFactory* searchForScaler(int w, int h)
                // We prefer a specific scaler
                for (i = 0; i < n; i++) {
                        if (strcasecmp(scalers[i]->getName(), Config.scaler) == 0) {
-                               if (!scalers[i]->canEnable(w, h)) {
-                                       DIE("Cannot use selected scaler");
+                               if (scalers[i]->canEnable(bpp, w, h)) {
+                                       // Found the scaler selected by the user, and we can use it.
+                                       return scalers[i];
+                               } else {
+                                       fprintf(stderr,
+                                               "Selected scaler '%s' cannot be enabled in this mode\n",
+                                               Config.scaler);
+                                       break; // Fallback to another scaler.
                                }
-                               return scalers[i];
                        }
                }
-               DIE("Select scaler does not exist");
-       } else {
-               // Just try them all
-               for (i = 0; i < n; i++) {
-                       if (scalers[i]->canEnable(w, h)) {
-                               return scalers[i];
-                       }
+               if (i == n) {
+                       fprintf(stderr, "Selected scaler '%s' does not exist\n",
+                               Config.scaler);
                }
-               DIE("Can't use any scaler");
        }
+
+       // Just try them all now, in a buildtime set priority.
+       for (i = 0; i < n; i++) {
+               if (scalers[i]->canEnable(bpp, w, h)) {
+                       return scalers[i];
+               }
+       }
+
+       DIE("Can't use any scaler; this shouldn't happen.");
 }
 
 static void calculateScreenSize()
@@ -428,7 +609,7 @@ static void freeVideoSurface()
        free(GFX.ZBuffer); GFX.ZBuffer = 0;
        free(GFX.SubZBuffer); GFX.SubZBuffer = 0;
 
-       free(scaler); scaler = 0;
+       delete scaler; scaler = 0;
 }
 
 static void setupVideoSurface()
@@ -437,11 +618,6 @@ static void setupVideoSurface()
        const unsigned gameWidth = IMAGE_WIDTH;
        const unsigned gameHeight = IMAGE_HEIGHT;
 
-       if (scaler) {
-               delete scaler;
-               scaler = 0;
-       }
-
 #ifdef MAEMO
        if ((Config.fullscreen && !gotScreenSize) ||
                (!Config.fullscreen && !gotWindowSize)) {
@@ -468,7 +644,8 @@ static void setupVideoSurface()
        if (gameHeight > GUI.Height || gameWidth > GUI.Width)
                DIE("Video is larger than window size!");
 
-       const ScalerFactory* sFactory = searchForScaler(gameWidth, gameHeight);
+       const ScalerFactory* sFactory =
+               searchForScaler(Settings.SixteenBit ? 16 : 8, gameWidth, gameHeight);
 
        screen = SDL_SetVideoMode(GUI.Width, GUI.Height,
                                                                Settings.SixteenBit ? 16 : 8,
@@ -510,9 +687,12 @@ static void drawOnscreenControls()
        if (Config.touchscreenInput) {
                S9xInputScreenChanged();
                if (Config.touchscreenShow) {
+                       scaler->pause();
+                       SDL_FillRect(screen, NULL, 0);
                        S9xInputScreenDraw(Settings.SixteenBit ? 2 : 1,
                                                                screen->pixels, screen->pitch);
                        SDL_Flip(screen);
+                       scaler->resume();
                }
        }
 }