[OPENGL32]
[reactos.git] / dll / opengl / opengl32 / wgl.c
index 32503e7..a8547c1 100644 (file)
@@ -14,6 +14,8 @@ WINE_DEFAULT_DEBUG_CHANNEL(wgl);
 static CRITICAL_SECTION dc_data_cs = {NULL, -1, 0, 0, 0, 0};
 static struct wgl_dc_data* dc_data_list = NULL;
 
+LIST_ENTRY ContextListHead;
+
 /* FIXME: suboptimal */
 static
 struct wgl_dc_data*
@@ -77,7 +79,7 @@ get_dc_data(HDC hdc)
         data->nb_icd_formats = data->icd_data->DrvDescribePixelFormat(hdc, 0, 0, NULL);
     else
         data->nb_icd_formats = 0;
-    TRACE("ICD %S has %u formats for HDC %x.\n", data->icd_data->DriverName, data->nb_icd_formats, hdc);
+    TRACE("ICD %S has %u formats for HDC %x.\n", data->icd_data ? data->icd_data->DriverName : NULL, data->nb_icd_formats, hdc);
     data->nb_sw_formats = sw_DescribePixelFormat(hdc, 0, 0, NULL);
     data->next = dc_data_list;
     dc_data_list = data;
@@ -139,7 +141,11 @@ INT WINAPI wglDescribePixelFormat(HDC hdc, INT format, UINT size, PIXELFORMATDES
     /* Query ICD if needed */
     if(format <= dc_data->nb_icd_formats)
     {
-        if(!dc_data->icd_data->DrvDescribePixelFormat(hdc, format, size, descr))
+        struct ICD_Data* icd_data = dc_data->icd_data;
+        /* SetPixelFormat may have NULLified this */
+        if (!icd_data)
+            icd_data = IntGetIcdData(hdc);
+        if(!icd_data->DrvDescribePixelFormat(hdc, format, size, descr))
         {
             ret = 0;
         }
@@ -174,7 +180,7 @@ INT WINAPI wglChoosePixelFormat(HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd)
     if (!count) return 0;
 
     best_format = 0;
-    best.dwFlags = 0;
+    best.dwFlags = PFD_GENERIC_FORMAT;
     best.cAlphaBits = -1;
     best.cColorBits = -1;
     best.cDepthBits = -1;
@@ -329,6 +335,9 @@ INT WINAPI wglChoosePixelFormat(HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd)
         continue;
 
     found:
+        /* Prefer HW accelerated formats */
+        if ((format.dwFlags & PFD_GENERIC_FORMAT) && !(best.dwFlags & PFD_GENERIC_FORMAT))
+            continue;
         best_format = i;
         best = format;
         bestDBuffer = format.dwFlags & PFD_DOUBLEBUFFER;
@@ -370,7 +379,7 @@ HGLRC WINAPI wglCreateContext(HDC hdc)
     struct wgl_context* context;
     DHGLRC dhglrc;
     
-    TRACE("Creating context for %p, format %i\n", hdc);
+    TRACE("Creating context for %p.\n", hdc);
     
     if(!dc_data)
     {
@@ -422,6 +431,9 @@ HGLRC WINAPI wglCreateContext(HDC hdc)
     context->pixelformat = dc_data->pixelformat;
     context->thread_id = 0;
     
+    /* Insert into the list */
+    InsertTailList(&ContextListHead, &context->ListEntry);
+
     context->magic = 'GLRC';
     TRACE("Success!\n");
     return (HGLRC)context;
@@ -525,6 +537,7 @@ BOOL WINAPI wglDeleteContext(HGLRC hglrc)
         sw_DeleteContext(context->dhglrc);
     
     context->magic = 0;
+    RemoveEntryList(&context->ListEntry);
     HeapFree(GetProcessHeap(), 0, context);
     
     return TRUE;
@@ -683,7 +696,7 @@ BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
                 ERR("DrvSetContext failed!\n");
                 /* revert */
                 InterlockedExchange(&ctx->thread_id, 0);
-                IntSetCurrentDispatchTable(&StubTable.glDispatchTable);
+                IntSetCurrentDispatchTable(NULL);
                 SetLastError(ERROR_INVALID_PARAMETER);
                 return FALSE;
             }
@@ -715,8 +728,7 @@ BOOL WINAPI wglMakeCurrent(HDC hdc, HGLRC hglrc)
         InterlockedExchange(&old_ctx->thread_id, 0);
         /* Unset it */
         IntMakeCurrent(NULL, NULL, NULL);
-        /* Reset the no-op table */
-        set_api_table(&StubTable);
+        IntSetCurrentDispatchTable(NULL);
         /* Test conformance (extreme cases) */
         return hglrc == NULL;
     }
@@ -871,7 +883,7 @@ BOOL WINAPI wglShareLists(HGLRC hglrcSrc, HGLRC hglrcDst)
     return sw_ShareLists(ctx_src->dhglrc, ctx_dst->dhglrc);
 }
 
-BOOL WINAPI wglSwapBuffers(HDC hdc)
+BOOL WINAPI DECLSPEC_HOTPATCH wglSwapBuffers(HDC hdc)
 {
     struct wgl_dc_data* dc_data = get_dc_data(hdc);
     
@@ -902,3 +914,18 @@ DWORD WINAPI wglSwapMultipleBuffers(UINT count, CONST WGLSWAP * toSwap)
 {
     return 0;
 }
+
+/* Clean up on DLL unload */
+void
+IntDeleteAllContexts(void)
+{
+    struct wgl_context* context;
+    LIST_ENTRY* Entry = ContextListHead.Flink;
+
+    while (Entry != &ContextListHead)
+    {
+        context = CONTAINING_RECORD(Entry, struct wgl_context, ListEntry);
+        wglDeleteContext((HGLRC)context);
+        Entry = Entry->Flink;
+    }
+}