Pickle's port for this emu
[pcsx_rearmed.git] / plugins / gpu-gles / gpuDraw.c
index 3d09c33..597f286 100644 (file)
@@ -39,7 +39,7 @@
 #include "texture.h"\r
 #else\r
 #include "gpuExternals.h"\r
-#include "GPUPlugin.h"\r
+#include "gpuPlugin.h"\r
 #include "gpuDraw.h"\r
 #include "gpuPrim.h"\r
 #include "gpuTexture.h"\r
@@ -309,6 +309,48 @@ HGLRC GLCONTEXT=NULL;
 #endif\r
 \r
 #ifdef MAEMO_CHANGES\r
+#define MODE_RAW 0\r
+#define MODE_X11 1\r
+#define MODE_SDL 2\r
+int pandora_driver_mode = MODE_RAW;\r
+int use_fsaa = 0;\r
+\r
+EGLDisplay display;\r
+EGLConfig  config;\r
+EGLContext context;\r
+EGLSurface surface;\r
+\r
+#if defined(USE_X11)\r
+#include "X11/Xlib.h"\r
+#include "X11/Xutil.h"\r
+#include "X11/Xatom.h"\r
+\r
+Window                 x11Window       = 0;\r
+Display*               x11Display      = 0;\r
+long                   x11Screen       = 0;\r
+XVisualInfo            x11Visual;\r
+XVisualInfo*   px11Visual      = 0;\r
+Colormap        x11Colormap    = 0;\r
+#endif\r
+\r
+EGLint attrib_list_fsaa[] =\r
+{\r
+       EGL_SURFACE_TYPE, EGL_WINDOW_BIT,\r
+       EGL_BUFFER_SIZE,    0,\r
+       EGL_DEPTH_SIZE,     16,\r
+       EGL_SAMPLE_BUFFERS, 1,\r
+       EGL_SAMPLES,        4,\r
+       EGL_NONE\r
+};\r
+\r
+EGLint attrib_list[] =\r
+{\r
+       EGL_SURFACE_TYPE, EGL_WINDOW_BIT,\r
+       EGL_BUFFER_SIZE,  0,\r
+       EGL_DEPTH_SIZE,   16,\r
+       EGL_NONE\r
+};\r
+\r
 bool TestEGLError(const char* pszLocation)\r
 {\r
        /*\r
@@ -320,79 +362,154 @@ bool TestEGLError(const char* pszLocation)
        if (iErr != EGL_SUCCESS)\r
        {\r
                printf("%s failed (%d).\n", pszLocation, iErr);\r
-               return false;\r
+               return FALSE;\r
        }\r
 \r
-       return true;\r
+       return TRUE;\r
 }\r
 \r
 void maemoGLinit(){\r
-       iResX= 800;\r
-    iResY =480;\r
+       printf ("GL init\n");\r
 \r
+       EGLint numConfigs;\r
+       EGLint majorVersion;\r
+       EGLint minorVersion;\r
+#if defined(USE_X11)\r
+       enum\r
+       {\r
+       _NET_WM_STATE_REMOVE =0,\r
+       _NET_WM_STATE_ADD = 1,\r
+       _NET_WM_STATE_TOGGLE =2\r
+       };\r
        \r
-       printf ("maemo GL init\n");\r
-       long int winxid=GDK_WINDOW_XID(GTK_WIDGET(windowG)->window);\r
-       printf ("%d\n",winxid);\r
+       Window                          sRootWindow;\r
+       XSetWindowAttributes    sWA;\r
+       unsigned int                ui32Mask;\r
+       int                                 i32Depth;\r
+#endif\r
        \r
-       EGLContext                      context = 0;\r
-       EGLConfig                       eglConfig       = 0;\r
-       EGLContext                      eglContext      = 0;\r
-       display = eglGetDisplay( EGL_DEFAULT_DISPLAY );\r
+       EGLint *attribList = NULL;\r
+       if (use_fsaa)\r
+       {\r
+               printf( "GLES: Using Full Scene Antialiasing\n" );\r
+               attribList = attrib_list_fsaa;\r
+       }\r
+       else\r
+       {\r
+               attribList = attrib_list;\r
+       }\r
+\r
+#if defined(USE_X11)\r
+       pandora_driver_mode = MODE_X11; // TODO make configurable\r
+#else\r
+       pandora_driver_mode = MODE_RAW; // TODO make configurable\r
+#endif\r
        \r
-       if( eglInitialize( display, NULL, NULL ) == EGL_FALSE )\r
+    switch(pandora_driver_mode)\r
             {            \r
-                printf( "EGL Initialize failed!\n" );\r
+#if defined(USE_X11)\r
+        case MODE_X11:\r
+            // Initializes the display and screen\r
+            x11Display = XOpenDisplay( ":0" );\r
+            if (!x11Display)\r
+            {\r
+                printf("GLES Error: Unable to open X display\n");\r
+            }\r
+            x11Screen = XDefaultScreen( x11Display );\r
                                \r
+            // Gets the display parameters so we can pass the same parameters to the window to be created.\r
+            sRootWindow        = RootWindow(x11Display, x11Screen);\r
+            i32Depth   = DefaultDepth(x11Display, x11Screen);\r
+            px11Visual = &x11Visual;\r
+            XMatchVisualInfo( x11Display, x11Screen, i32Depth, TrueColor, px11Visual);\r
+            if (!px11Visual)\r
+            {\r
+                printf("GLES Error: Unable to acquire visual\n");\r
                        }\r
+            // Colormap of the specified visual type for the display.\r
+            x11Colormap = XCreateColormap( x11Display, sRootWindow, px11Visual->visual, AllocNone );\r
+            sWA.colormap = x11Colormap;\r
+\r
+            // List of events to be handled by the application. Add to these for handling other events.\r
+            sWA.event_mask = StructureNotifyMask | ExposureMask | ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask;\r
 \r
-const EGLint attributeList[] = { \r
-                                     EGL_SURFACE_TYPE, EGL_WINDOW_BIT, \r
-                                     /*EGL_BUFFER_SIZE, 32, */\r
-                                     EGL_NONE \r
-                                   };\r
+            // Display capabilities list.\r
+            ui32Mask = CWBackPixel | CWBorderPixel | CWEventMask | CWColormap;\r
                                                           \r
-       EGLint pi32ConfigAttribs[5];\r
-       pi32ConfigAttribs[0] = EGL_SURFACE_TYPE;\r
-       pi32ConfigAttribs[1] = EGL_WINDOW_BIT;\r
-       pi32ConfigAttribs[2] = EGL_RENDERABLE_TYPE;\r
-       pi32ConfigAttribs[3] = EGL_OPENGL_ES2_BIT;      \r
-       pi32ConfigAttribs[4] = EGL_NONE;\r
-\r
-       EGLint pi32ContextAttribs[3];\r
-       pi32ContextAttribs[0] = EGL_CONTEXT_CLIENT_VERSION;\r
-       pi32ContextAttribs[1] = 2;\r
-       pi32ContextAttribs[2] = EGL_NONE;\r
-\r
-       int iConfigs;\r
-       if (!eglChooseConfig(display, attributeList, &eglConfig, 1, &iConfigs) || (iConfigs != 1))\r
+            // Creates the X11 window\r
+            x11Window = XCreateWindow( x11Display, RootWindow(x11Display, x11Screen), 0, 0, iResX, iResY,\r
+                                        0, CopyFromParent, InputOutput, CopyFromParent, ui32Mask, &sWA);\r
+\r
+            // Make the window viewable and flush the output buffer.\r
+            XMapWindow(x11Display, x11Window);\r
+            XFlush(x11Display);\r
+\r
+            // Make the window fullscreen\r
+            unsigned char fullScreen = 1;\r
+            Atom wmState = XInternAtom(x11Display, "_NET_WM_STATE", False);\r
+            Atom wmFullScreen = XInternAtom(x11Display,"_NET_WM_STATE_FULLSCREEN", False);\r
+\r
+            XEvent xev;\r
+            xev.xclient.type               = ClientMessage;\r
+            xev.xclient.serial             = 0;\r
+            xev.xclient.send_event      = True;\r
+            xev.xclient.window             = x11Window;\r
+            xev.xclient.message_type    = wmState;\r
+            xev.xclient.format             = 32;\r
+            xev.xclient.data.l[0]              = (fullScreen ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE);\r
+            xev.xclient.data.l[1]              = wmFullScreen;\r
+            xev.xclient.data.l[2]              = 0;\r
+\r
+            XSendEvent(x11Display, DefaultRootWindow(x11Display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);\r
+\r
+            display = eglGetDisplay( (EGLNativeDisplayType)x11Display );\r
+            break;\r
+#endif\r
+        case MODE_RAW:\r
+        default:\r
+            display = eglGetDisplay( (EGLNativeDisplayType)0 );\r
+            break;\r
+    }\r
+\r
+       if( display == EGL_NO_DISPLAY )\r
        {\r
-               printf("Error: eglChooseConfig() failed.\n");\r
+               printf( "GLES EGL Error: GL No Display\n" );\r
        }\r
-       printf ("%d\n",iConfigs);\r
-       surface = eglCreateWindowSurface(display, eglConfig, (void*)winxid, NULL);\r
-       printf ("%d\n",surface);\r
-    if (!TestEGLError("eglCreateWindowSurface"))\r
+\r
+       if( !eglInitialize( display, &majorVersion, &minorVersion ) )\r
        {\r
-               printf ("eglCreateWindowSurface fail");\r
+               printf( "GLES EGL Error: eglInitialize failed\n" );\r
        }\r
 \r
-       //context = eglCreateContext(display, eglConfig, NULL, pi32ContextAttribs);\r
-       context =eglCreateContext( display, eglConfig,\r
-                                    EGL_NO_CONTEXT, NULL \r
-                                  );\r
-printf ("%d\n",context);\r
-       if (!TestEGLError("eglCreateContext"))\r
+       if( !eglChooseConfig( display, attribList, &config, 1, &numConfigs ) )\r
        {\r
-               printf("error eglCreateContext");\r
+               printf( "GLES EGL Error: eglChooseConfig failed\n" );\r
        }\r
 \r
-       eglMakeCurrent(display, surface, surface, context);\r
+       context = eglCreateContext( display, config, NULL, NULL );\r
+       if( context==0 )\r
+       {\r
+               printf( "GLES EGL Error: eglCreateContext failed\n" );\r
+       }\r
 \r
-       if (!TestEGLError("eglMakeCurrent"))\r
+    switch(pandora_driver_mode)\r
        {\r
-               printf("error eglMakeCurrent");\r
+#if defined(USE_X11)\r
+        case MODE_X11:\r
+            surface = eglCreateWindowSurface( display, config, (EGLNativeDisplayType)x11Window, NULL );\r
+            break;\r
+#endif\r
+        case MODE_RAW:\r
+        default:\r
+            surface = eglCreateWindowSurface( display, config, (EGLNativeDisplayType)0, NULL );\r
+            break;\r
        }\r
+\r
+    eglMakeCurrent( display, surface, surface, context );\r
+    if (!TestEGLError("eglMakeCurrent"))\r
+        printf("error eglMakeCurrent");\r
+    else\r
+        printf("GLES Window Opened\n");\r
 }\r
 #endif\r
 \r
@@ -505,6 +622,20 @@ void GLcleanup()
  if(!bWindowMode && dcGlobal) \r
   ReleaseDC(hWWindow,dcGlobal);\r
 #endif\r
+\r
+       eglMakeCurrent( display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );\r
+       eglDestroySurface( display, surface );\r
+       eglDestroyContext( display, context );\r
+       eglTerminate( display );\r
+\r
+#if defined(USE_X11)\r
+       if (pandora_driver_mode == MODE_X11)\r
+       {\r
+               if (x11Window) XDestroyWindow(x11Display, x11Window);\r
+               if (x11Colormap) XFreeColormap( x11Display, x11Colormap );\r
+               if (x11Display) XCloseDisplay(x11Display);\r
+       }\r
+#endif\r
 }\r
 \r
 ////////////////////////////////////////////////////////////////////////\r