[YAROTOWS] Reintegrate the branch. For a brighter future.
[reactos.git] / reactos / subsystems / win32 / win32k / ntuser / windc.c
index fdf8f48..e553417 100644 (file)
@@ -9,7 +9,7 @@
 
 /* INCLUDES ******************************************************************/
 
-#include <w32k.h>
+#include <win32k.h>
 
 #define NDEBUG
 #include <debug.h>
@@ -19,9 +19,10 @@ int FASTCALL CLIPPING_UpdateGCRegion(DC* Dc);
 /* GLOBALS *******************************************************************/
 
 /* NOTE - I think we should store this per window station (including gdi objects) */
+/* Answer: No, use the DCE pMonitor to compare with! */
 
-static PDCE FirstDce = NULL;
-//static INT DCECount = 0; // Count of DCE in system.
+static LIST_ENTRY LEDce;
+static INT DCECount = 0; // Count of DCE in system.
 
 #define DCX_CACHECOMPAREMASK (DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | \
                               DCX_NORESETATTRS | DCX_LOCKWINDOWUPDATE | \
@@ -40,6 +41,9 @@ DceCreateDisplayDC(VOID)
   UNICODE_STRING DriverName;
   RtlInitUnicodeString(&DriverName, L"DISPLAY");
   hDC = IntGdiCreateDC(&DriverName, NULL, NULL, NULL, FALSE);
+
+  co_IntGraphicsCheck(TRUE);
+
 //
 // If NULL, first time through! Build the default window dc!
 //
@@ -51,13 +55,14 @@ DceCreateDisplayDC(VOID)
       defaultDCstate->pdcattr = &defaultDCstate->dcattr;
       DC_vCopyState(dc, defaultDCstate, TRUE);
       DC_UnlockDc( dc );
+      InitializeListHead(&LEDce);
   }
   return hDC;
 }
 
 static
 HRGN FASTCALL
-DceGetVisRgn(PWINDOW_OBJECT Window, ULONG Flags, HWND hWndChild, ULONG CFlags)
+DceGetVisRgn(PWND Window, ULONG Flags, HWND hWndChild, ULONG CFlags)
 {
   HRGN VisRgn;
 
@@ -73,12 +78,9 @@ DceGetVisRgn(PWINDOW_OBJECT Window, ULONG Flags, HWND hWndChild, ULONG CFlags)
 }
 
 PDCE FASTCALL
-DceAllocDCE(PWINDOW_OBJECT Window OPTIONAL, DCE_TYPE Type)
+DceAllocDCE(PWND Window OPTIONAL, DCE_TYPE Type)
 {
   PDCE pDce;
-  PWND Wnd = NULL;
-
-  if (Window) Wnd = Window->Wnd;
 
   pDce = ExAllocatePoolWithTag(PagedPool, sizeof(DCE), TAG_PDCE);
   if(!pDce)
@@ -90,24 +92,17 @@ DceAllocDCE(PWINDOW_OBJECT Window OPTIONAL, DCE_TYPE Type)
       ExFreePoolWithTag(pDce, TAG_PDCE);
       return NULL;
   }
-
-  pDce->hwndCurrent = (Window ? Window->hSelf : NULL);
-  pDce->pwndOrg  = Wnd;
-  pDce->pwndClip = Wnd;
+  DCECount++;
+  DPRINT("Alloc DCE's! %d\n",DCECount);
+  pDce->hwndCurrent = (Window ? Window->head.h : NULL);
+  pDce->pwndOrg  = Window;
+  pDce->pwndClip = Window;
   pDce->hrgnClip = NULL;
   pDce->hrgnClipPublic = NULL;
   pDce->hrgnSavedVis = NULL;
   pDce->ppiOwner = NULL;
 
-  KeEnterCriticalRegion();
-  if (FirstDce == NULL)
-  {
-     FirstDce = pDce;
-     InitializeListHead(&FirstDce->List);
-  }
-  else
-     InsertTailList(&FirstDce->List, &pDce->List);
-  KeLeaveCriticalRegion();
+  InsertTailList(&LEDce, &pDce->List);
 
   DCU_SetDcUndeletable(pDce->hDC);
 
@@ -129,12 +124,12 @@ DceAllocDCE(PWINDOW_OBJECT Window OPTIONAL, DCE_TYPE Type)
   else
   {
      pDce->DCXFlags = DCX_DCEBUSY;
-     if (Wnd)
+     if (Window)
      {
         if (Type == DCE_WINDOW_DC)
         {
-          if (Wnd->style & WS_CLIPCHILDREN) pDce->DCXFlags |= DCX_CLIPCHILDREN;
-          if (Wnd->style & WS_CLIPSIBLINGS) pDce->DCXFlags |= DCX_CLIPSIBLINGS;
+          if (Window->style & WS_CLIPCHILDREN) pDce->DCXFlags |= DCX_CLIPCHILDREN;
+          if (Window->style & WS_CLIPSIBLINGS) pDce->DCXFlags |= DCX_CLIPSIBLINGS;
         }
      }
   }
@@ -142,12 +137,11 @@ DceAllocDCE(PWINDOW_OBJECT Window OPTIONAL, DCE_TYPE Type)
 }
 
 static VOID APIENTRY
-DceSetDrawable( PWINDOW_OBJECT Window OPTIONAL,
+DceSetDrawable( PWND Window OPTIONAL,
                 HDC hDC,
                 ULONG Flags,
                 BOOL SetClipOrigin)
 {
-  PWND Wnd;
   DC *dc = DC_LockDc(hDC);
   if(!dc)
       return;
@@ -159,16 +153,15 @@ DceSetDrawable( PWINDOW_OBJECT Window OPTIONAL,
   }
   else
   {
-      Wnd = Window->Wnd;
       if (Flags & DCX_WINDOW)
       {
-         dc->ptlDCOrig.x = Wnd->rcWindow.left;
-         dc->ptlDCOrig.y = Wnd->rcWindow.top;
+         dc->ptlDCOrig.x = Window->rcWindow.left;
+         dc->ptlDCOrig.y = Window->rcWindow.top;
       }
       else
       {
-         dc->ptlDCOrig.x = Wnd->rcClient.left;
-         dc->ptlDCOrig.y = Wnd->rcClient.top;
+         dc->ptlDCOrig.x = Window->rcClient.left;
+         dc->ptlDCOrig.y = Window->rcClient.top;
       }
   }
   DC_UnlockDc(dc);
@@ -186,7 +179,7 @@ DceDeleteClipRgn(DCE* Dce)
    }
    else if (Dce->hrgnClip != NULL)
    {
-      REGION_FreeRgnByHandle(Dce->hrgnClip);
+      GDIOBJ_FreeObjByHandle(Dce->hrgnClip, GDI_OBJECT_TYPE_REGION|GDI_OBJECT_TYPE_SILENT);
    }
 
    Dce->hrgnClip = NULL;
@@ -198,13 +191,12 @@ DceDeleteClipRgn(DCE* Dce)
 static INT FASTCALL
 DceReleaseDC(DCE* dce, BOOL EndPaint)
 {
-   if (DCX_DCEBUSY != (dce->DCXFlags & (DCX_DCEEMPTY | DCX_DCEBUSY)))
+   if (DCX_DCEBUSY != (dce->DCXFlags & (DCX_INDESTROY | DCX_DCEEMPTY | DCX_DCEBUSY)))
    {
       return 0;
    }
 
    /* restore previous visible region */
-
    if ((dce->DCXFlags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) &&
          ((dce->DCXFlags & DCX_CACHE) || EndPaint))
    {
@@ -213,45 +205,62 @@ DceReleaseDC(DCE* dce, BOOL EndPaint)
 
    if (dce->DCXFlags & DCX_CACHE)
    {
-     if (!(dce->DCXFlags & DCX_NORESETATTRS))
-     {
-       /* make the DC clean so that SetDCState doesn't try to update the vis rgn */
-       IntGdiSetHookFlags(dce->hDC, DCHF_VALIDATEVISRGN);
+      if (!(dce->DCXFlags & DCX_NORESETATTRS))
+      {
+         /* make the DC clean so that SetDCState doesn't try to update the vis rgn */
+         IntGdiSetHookFlags(dce->hDC, DCHF_VALIDATEVISRGN);
 
-       // Clean the DC
-       if (!IntGdiCleanDC(dce->hDC)) return 0;
+         // Clean the DC
+         if (!IntGdiCleanDC(dce->hDC)) return 0;
 
-       if (dce->DCXFlags & DCX_DCEDIRTY)
-       {
-         /* don't keep around invalidated entries
-          * because SetDCState() disables hVisRgn updates
-          * by removing dirty bit. */
-         dce->hwndCurrent = 0;
-         dce->DCXFlags &= DCX_CACHE;
-         dce->DCXFlags |= DCX_DCEEMPTY;
-       }
-     }
-     dce->DCXFlags &= ~DCX_DCEBUSY;
-     DPRINT("Exit!!!!! DCX_CACHE!!!!!!   hDC-> %x \n", dce->hDC);
-     if (!IntGdiSetDCOwnerEx( dce->hDC, GDI_OBJ_HMGR_NONE, FALSE))
-        return 0;
-     dce->ptiOwner = NULL; // Reset ownership.
-     dce->ppiOwner = NULL;
+         if (dce->DCXFlags & DCX_DCEDIRTY)
+         {
+           /* don't keep around invalidated entries
+            * because SetDCState() disables hVisRgn updates
+            * by removing dirty bit. */
+           dce->hwndCurrent = 0;
+           dce->DCXFlags &= DCX_CACHE;
+           dce->DCXFlags |= DCX_DCEEMPTY;
+         }
+      }
+      dce->DCXFlags &= ~DCX_DCEBUSY;
+      DPRINT("Exit!!!!! DCX_CACHE!!!!!!   hDC-> %x \n", dce->hDC);
+      if (!IntGdiSetDCOwnerEx( dce->hDC, GDI_OBJ_HMGR_NONE, FALSE))
+         return 0;
+      dce->ptiOwner = NULL; // Reset ownership.
+      dce->ppiOwner = NULL;
+
+#if 0 // Need to research and fix before this is a "growing" issue.
+      if (++DCECache > 32)
+      {
+         pLE = LEDce.Flink;
+         pDCE = CONTAINING_RECORD(pLE, DCE, List);
+         do
+         {
+            if (!(pDCE->DCXFlags & DCX_DCEBUSY))
+            {  /* Free the unused cache DCEs. */
+               pDCE = DceFreeDCE(pDCE, TRUE);
+               if (!pDCE) break;
+               continue;
+            }
+         }
+         while (pLE != &LEDce );
+      }
+#endif      
    }
    return 1; // Released!
 }
 
 static VOID FASTCALL
-DceUpdateVisRgn(DCE *Dce, PWINDOW_OBJECT Window, ULONG Flags)
+DceUpdateVisRgn(DCE *Dce, PWND Window, ULONG Flags)
 {
    HANDLE hRgnVisible = NULL;
    ULONG DcxFlags;
-   PWINDOW_OBJECT DesktopWindow;
+   PWND DesktopWindow;
 
    if (Flags & DCX_PARENTCLIP)
    {
-      PWINDOW_OBJECT Parent;
-      PWND ParentWnd;
+      PWND Parent;
 
       Parent = Window->spwndParent;
       if(!Parent)
@@ -260,9 +269,7 @@ DceUpdateVisRgn(DCE *Dce, PWINDOW_OBJECT Window, ULONG Flags)
          goto noparent;
       }
 
-      ParentWnd = Parent->Wnd;
-
-      if (ParentWnd->style & WS_CLIPSIBLINGS)
+      if (Parent->style & WS_CLIPSIBLINGS)
       {
          DcxFlags = DCX_CLIPSIBLINGS |
                     (Flags & ~(DCX_CLIPCHILDREN | DCX_WINDOW));
@@ -271,14 +278,14 @@ DceUpdateVisRgn(DCE *Dce, PWINDOW_OBJECT Window, ULONG Flags)
       {
          DcxFlags = Flags & ~(DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_WINDOW);
       }
-      hRgnVisible = DceGetVisRgn(Parent, DcxFlags, Window->hSelf, Flags);
+      hRgnVisible = DceGetVisRgn(Parent, DcxFlags, Window->head.h, Flags);
    }
    else if (Window == NULL)
    {
       DesktopWindow = UserGetWindowObject(IntGetDesktopWindow());
       if (NULL != DesktopWindow)
       {
-         hRgnVisible = IntSysCreateRectRgnIndirect(&DesktopWindow->Wnd->rcWindow);
+         hRgnVisible = IntSysCreateRectRgnIndirect(&DesktopWindow->rcWindow);
       }
       else
       {
@@ -326,23 +333,21 @@ noparent:
 }
 
 HDC FASTCALL
-UserGetDCEx(PWINDOW_OBJECT Window OPTIONAL, HANDLE ClipRegion, ULONG Flags)
+UserGetDCEx(PWND Wnd OPTIONAL, HANDLE ClipRegion, ULONG Flags)
 {
-   PWINDOW_OBJECT Parent;
+   PWND Parent;
    ULONG DcxFlags;
    DCE* Dce = NULL;
    BOOL UpdateClipOrigin = FALSE;
-   PWND Wnd = NULL;
    HDC hDC = NULL;
    PPROCESSINFO ppi;
+   PLIST_ENTRY pLE;
 
-   if (NULL == Window)
+   if (NULL == Wnd)
    {
       Flags &= ~DCX_USESTYLE;
       Flags |= DCX_CACHE;
    }
-   else
-       Wnd = Window->Wnd;
 
    if (Flags & (DCX_WINDOW | DCX_PARENTCLIP)) Flags |= DCX_CACHE;
 
@@ -407,9 +412,9 @@ UserGetDCEx(PWINDOW_OBJECT Window OPTIONAL, HANDLE ClipRegion, ULONG Flags)
       Flags &= ~(DCX_PARENTCLIP | DCX_CLIPCHILDREN);
    }
 
-   Parent = (Window ? Window->spwndParent : NULL);
+   Parent = (Wnd ? Wnd->spwndParent : NULL);
 
-   if (NULL == Window || !(Wnd->style & WS_CHILD) || NULL == Parent)
+   if (NULL == Wnd || !(Wnd->style & WS_CHILD) || NULL == Parent)
    {
       Flags &= ~DCX_PARENTCLIP;
       Flags |= DCX_CLIPSIBLINGS;
@@ -421,10 +426,10 @@ UserGetDCEx(PWINDOW_OBJECT Window OPTIONAL, HANDLE ClipRegion, ULONG Flags)
    if (Flags & DCX_PARENTCLIP)
    {
       if ((Wnd->style & WS_VISIBLE) &&
-          (Parent->Wnd->style & WS_VISIBLE))
+          (Parent->style & WS_VISIBLE))
       {
          Flags &= ~DCX_CLIPCHILDREN;
-         if (Parent->Wnd->style & WS_CLIPSIBLINGS)
+         if (Parent->style & WS_CLIPSIBLINGS)
          {
             Flags |= DCX_CLIPSIBLINGS;
          }
@@ -445,7 +450,8 @@ UserGetDCEx(PWINDOW_OBJECT Window OPTIONAL, HANDLE ClipRegion, ULONG Flags)
       DCE* DceEmpty = NULL;
       DCE* DceUnused = NULL;
       KeEnterCriticalRegion();
-      Dce = FirstDce;
+      pLE = LEDce.Flink;
+      Dce = CONTAINING_RECORD(pLE, DCE, List);
       do
       {
 // The reason for this you may ask?
@@ -464,15 +470,17 @@ UserGetDCEx(PWINDOW_OBJECT Window OPTIONAL, HANDLE ClipRegion, ULONG Flags)
             {
                DceEmpty = Dce;
             }
-            else if (Dce->hwndCurrent == (Window ? Window->hSelf : NULL) &&
+            else if (Dce->hwndCurrent == (Wnd ? Wnd->head.h : NULL) &&
                      ((Dce->DCXFlags & DCX_CACHECOMPAREMASK) == DcxFlags))
             {
                UpdateClipOrigin = TRUE;
                break;
             }
          }
-        Dce = (PDCE)Dce->List.Flink;
-      } while (Dce != FirstDce);
+         pLE = Dce->List.Flink;
+         Dce = CONTAINING_RECORD(pLE, DCE, List);
+      }
+      while (pLE != &LEDce);
       KeLeaveCriticalRegion();
 
       Dce = (DceEmpty == NULL) ? DceUnused : DceEmpty;
@@ -483,19 +491,22 @@ UserGetDCEx(PWINDOW_OBJECT Window OPTIONAL, HANDLE ClipRegion, ULONG Flags)
       }
       if (!Dce) return NULL;
 
-      Dce->hwndCurrent = (Window ? Window->hSelf : NULL);
+      Dce->hwndCurrent = (Wnd ? Wnd->head.h : NULL);
    }
    else // If we are here, we are POWNED or having CLASS.
    {
       KeEnterCriticalRegion();
-      Dce = FirstDce;
+      pLE = LEDce.Flink;
+      Dce = CONTAINING_RECORD(pLE, DCE, List);
       do
       {   // Check for Window handle than HDC match for CLASS.
-          if ((Dce->hwndCurrent == Window->hSelf) ||
+          if ((Dce->hwndCurrent == Wnd->head.h) ||
               (Dce->hDC == hDC))
              break;
-          Dce = (PDCE)Dce->List.Flink;
-      } while (Dce != FirstDce);
+          pLE = Dce->List.Flink;
+          Dce = CONTAINING_RECORD(pLE, DCE, List);
+      }
+      while (pLE != &LEDce);
       KeLeaveCriticalRegion();
 
       if ( (Flags & (DCX_INTERSECTRGN|DCX_EXCLUDERGN)) &&
@@ -526,11 +537,10 @@ UserGetDCEx(PWINDOW_OBJECT Window OPTIONAL, HANDLE ClipRegion, ULONG Flags)
         http://www.reactos.org/archives/public/ros-dev/2008-July/010498.html
         http://www.reactos.org/archives/public/ros-dev/2008-July/010499.html
     */
-   if ((Dce != FirstDce))
+   if (pLE != &LEDce)
    {
       RemoveEntryList(&Dce->List);
-      InsertHeadList(&FirstDce->List, &Dce->List);
-      FirstDce = Dce;
+      InsertHeadList(&LEDce, &Dce->List);
    }
 
    /* Introduced in rev 6691 and modified later. */
@@ -538,29 +548,35 @@ UserGetDCEx(PWINDOW_OBJECT Window OPTIONAL, HANDLE ClipRegion, ULONG Flags)
    {
       Flags |= DCX_INTERSECTRGN | DCX_KEEPCLIPRGN;
       Dce->DCXFlags |= DCX_INTERSECTRGN | DCX_KEEPCLIPRGN;
-      ClipRegion = Window->hrgnUpdate;
+      ClipRegion = Wnd->hrgnUpdate;
    }
 
    if (ClipRegion == (HRGN) 1)
    {
       if (!(Flags & DCX_WINDOW))
       {
-         Dce->hrgnClip = IntSysCreateRectRgnIndirect(&Window->Wnd->rcClient);
+         Dce->hrgnClip = IntSysCreateRectRgnIndirect(&Wnd->rcClient);
       }
       else
       {
-         Dce->hrgnClip = IntSysCreateRectRgnIndirect(&Window->Wnd->rcWindow);
+         Dce->hrgnClip = IntSysCreateRectRgnIndirect(&Wnd->rcWindow);
       }
       Dce->DCXFlags &= ~DCX_KEEPCLIPRGN;
    }
    else if (ClipRegion != NULL)
    {
+      if (Dce->hrgnClip != NULL)
+      {
+         DPRINT1("Should not be called!!\n");
+         GDIOBJ_FreeObjByHandle(Dce->hrgnClip, GDI_OBJECT_TYPE_REGION|GDI_OBJECT_TYPE_SILENT);
+         Dce->hrgnClip = NULL;
+      }
       Dce->hrgnClip = ClipRegion;
    }
 
-   DceSetDrawable(Window, Dce->hDC, Flags, UpdateClipOrigin);
+   DceSetDrawable(Wnd, Dce->hDC, Flags, UpdateClipOrigin);
 
-   DceUpdateVisRgn(Dce, Window, Flags);
+   DceUpdateVisRgn(Dce, Wnd, Flags);
 
    if (Dce->DCXFlags & DCX_CACHE)
    {
@@ -595,15 +611,15 @@ PDCE FASTCALL
 DceFreeDCE(PDCE pdce, BOOLEAN Force)
 {
   DCE *ret;
+  PLIST_ENTRY pLE;
   BOOL Hit = FALSE;
 
   if (NULL == pdce) return NULL;
 
-  ret = (PDCE) pdce->List.Flink;
+  pLE = pdce->List.Flink;
+  ret = CONTAINING_RECORD(pLE, DCE, List);
 
-#if 0 /* FIXME */
-  SetDCHook(pdce->hDC, NULL, 0L);
-#endif
+  pdce->DCXFlags |= DCX_INDESTROY;
 
   if (Force && !GDIOBJ_OwnedByCurrentProcess(pdce->hDC))
   {
@@ -619,12 +635,18 @@ DceFreeDCE(PDCE pdce, BOOLEAN Force)
          Hit = TRUE;
      }
   }
+  else
+  {
+     if (!GreGetObjectOwner(pdce->hDC, GDIObjType_DC_TYPE))
+        DC_SetOwnership( pdce->hDC, PsGetCurrentProcess());
+  }
 
   if (!Hit) IntGdiDeleteDC(pdce->hDC, TRUE);
 
-  if (pdce->hrgnClip && ! (pdce->DCXFlags & DCX_KEEPCLIPRGN))
+  if (pdce->hrgnClip && !(pdce->DCXFlags & DCX_KEEPCLIPRGN))
   {
-      REGION_FreeRgnByHandle(pdce->hrgnClip);
+      GDIOBJ_FreeObjByHandle(pdce->hrgnClip, GDI_OBJECT_TYPE_REGION|GDI_OBJECT_TYPE_SILENT);
+      pdce->hrgnClip = NULL;
   }
 
   RemoveEntryList(&pdce->List);
@@ -632,70 +654,197 @@ DceFreeDCE(PDCE pdce, BOOLEAN Force)
   if (IsListEmpty(&pdce->List))
   {
       DPRINT1("List is Empty! DCE! -> %x\n" , pdce);
-      FirstDce = NULL;
-      ret = NULL;
+      return NULL;
   }
 
   ExFreePoolWithTag(pdce, TAG_PDCE);
 
+  DCECount--;
+  DPRINT("Freed DCE's! %d \n", DCECount);
+
   return ret;
 }
 
+/***********************************************************************
+ *           DceFreeWindowDCE
+ *
+ * Remove owned DCE and reset unreleased cache DCEs.
+ */
+void FASTCALL
+DceFreeWindowDCE(PWND Window)
+{
+  PDCE pDCE;
+  PLIST_ENTRY pLE;
+
+  if (DCECount <= 0)
+  {
+     DPRINT1("FreeWindowDCE No Entry! %d\n",DCECount);
+     return;
+  }
+
+  pLE = LEDce.Flink;
+  pDCE = CONTAINING_RECORD(pLE, DCE, List);
+  do
+  {
+     if (!pDCE)
+     {
+        DPRINT1("FreeWindowDCE No DCE Pointer!\n");
+        break;
+     }
+     if (IsListEmpty(&pDCE->List))
+     {
+        DPRINT1("FreeWindowDCE List is Empty!!!!\n");
+        break;
+     }
+     if ( pDCE->hwndCurrent == Window->head.h &&
+          !(pDCE->DCXFlags & DCX_DCEEMPTY) )
+     {
+        if (!(pDCE->DCXFlags & DCX_CACHE)) /* owned or Class DCE*/
+        {
+           if (Window->pcls->style & CS_CLASSDC) /* Test Class first */
+           {
+              if (pDCE->DCXFlags & (DCX_INTERSECTRGN | DCX_EXCLUDERGN)) /* Class DCE*/
+                 DceDeleteClipRgn(pDCE);
+              // Update and reset Vis Rgn and clear the dirty bit.
+              // Should release VisRgn than reset it to default.
+              DceUpdateVisRgn(pDCE, Window, pDCE->DCXFlags);
+              pDCE->DCXFlags = DCX_DCEEMPTY|DCX_CACHE;
+              pDCE->hwndCurrent = 0;
+
+              DPRINT("POWNED DCE going Cheap!! DCX_CACHE!! hDC-> %x \n", pDCE->hDC);
+              if (!IntGdiSetDCOwnerEx( pDCE->hDC, GDI_OBJ_HMGR_NONE, FALSE))
+              {
+                  DPRINT1("Fail Owner Switch hDC-> %x \n", pDCE->hDC);
+                  break;
+              }
+              /* Do not change owner so thread can clean up! */
+           }
+           else if (Window->pcls->style & CS_OWNDC) /* owned DCE*/
+           {
+              pDCE = DceFreeDCE(pDCE, FALSE);
+              if (!pDCE) break;
+              continue;
+           }
+           else
+           {
+              DPRINT1("Not POWNED or CLASSDC hwndCurrent -> %x \n", pDCE->hwndCurrent);
+              //ASSERT(FALSE);
+           }
+        }
+        else
+        {
+           if (pDCE->DCXFlags & DCX_DCEBUSY) /* shared cache DCE */
+           {
+              /* FIXME: AFAICS we are doing the right thing here so
+               * this should be a DPRINT. But this is best left as an ERR
+               * because the 'application error' is likely to come from
+               * another part of Wine (i.e. it's our fault after all).
+               * We should change this to DPRINT when ReactOS is more stable
+               * (for 1.0?).
+               */
+              DPRINT1("[%p] GetDC() without ReleaseDC()!\n", Window->head.h);
+              DceReleaseDC(pDCE, FALSE);
+           }
+           pDCE->DCXFlags |= DCX_DCEEMPTY;
+           pDCE->hwndCurrent = 0;
+        }
+     }
+     pLE = pDCE->List.Flink;
+     pDCE = CONTAINING_RECORD(pLE, DCE, List);
+  }
+  while (pLE != &LEDce);
+}
+
 void FASTCALL
 DceFreeClassDCE(HDC hDC)
 {
-   PDCE pDCE = FirstDce;
-   KeEnterCriticalRegion();
+   PDCE pDCE;
+   PLIST_ENTRY pLE;
+   pLE = LEDce.Flink;
+   pDCE = CONTAINING_RECORD(pLE, DCE, List);
+
    do
    {
        if(!pDCE) break;
        if (pDCE->hDC == hDC)
        {
-          pDCE = DceFreeDCE(pDCE, FALSE);
-          if(!pDCE) break;
+          pDCE = DceFreeDCE(pDCE, TRUE); // Might have gone cheap!
+          if (!pDCE) break;
           continue;
        }
-       pDCE = (PDCE)pDCE->List.Flink;
-   } while (pDCE != FirstDce);
-   KeLeaveCriticalRegion();
+       pLE = pDCE->List.Flink;
+       pDCE = CONTAINING_RECORD(pLE, DCE, List);
+   }
+   while (pLE != &LEDce);
+}
+
+void FASTCALL
+DceFreeThreadDCE(PTHREADINFO pti)
+{
+   PDCE pDCE;
+   PLIST_ENTRY pLE;
+   pLE = LEDce.Flink;
+   pDCE = CONTAINING_RECORD(pLE, DCE, List);
+
+   do
+   {
+       if(!pDCE) break;
+       if (pDCE->ptiOwner == pti)
+       {
+          if (pDCE->DCXFlags & DCX_CACHE)
+          {
+             pDCE = DceFreeDCE(pDCE, TRUE);
+             if (!pDCE) break;
+             continue;
+          }
+       }
+       pLE = pDCE->List.Flink;
+       pDCE = CONTAINING_RECORD(pLE, DCE, List);
+   }
+   while (pLE != &LEDce);
 }
 
 VOID FASTCALL
 DceEmptyCache(VOID)
 {
-   PDCE pDCE = FirstDce;
-   KeEnterCriticalRegion();
+   PDCE pDCE;
+   PLIST_ENTRY pLE;
+   pLE = LEDce.Flink;
+   pDCE = CONTAINING_RECORD(pLE, DCE, List);
+
    do
    {
       if(!pDCE) break;
       pDCE = DceFreeDCE(pDCE, TRUE);
       if(!pDCE) break;
-   } while (pDCE != FirstDce);
-   KeLeaveCriticalRegion();
-   FirstDce = NULL;
+   }
+   while (pLE != &LEDce);
 }
 
 VOID FASTCALL
-DceResetActiveDCEs(PWINDOW_OBJECT Window)
+DceResetActiveDCEs(PWND Window)
 {
    DCE *pDCE;
    PDC dc;
-   PWINDOW_OBJECT CurrentWindow;
+   PWND CurrentWindow;
    INT DeltaX;
    INT DeltaY;
+   PLIST_ENTRY pLE;
 
    if (NULL == Window)
    {
       return;
    }
-   pDCE = FirstDce;
+   pLE = LEDce.Flink;
+   pDCE = CONTAINING_RECORD(pLE, DCE, List);
    if(!pDCE) return; // Another null test!
    do
    {
       if(!pDCE) break;
-      if (0 == (pDCE->DCXFlags & DCX_DCEEMPTY))
+      if(pLE == &LEDce) break;
+      if (0 == (pDCE->DCXFlags & (DCX_DCEEMPTY|DCX_INDESTROY)))
       {
-         if (Window->hSelf == pDCE->hwndCurrent)
+         if (Window->head.h == pDCE->hwndCurrent)
          {
             CurrentWindow = Window;
          }
@@ -704,7 +853,8 @@ DceResetActiveDCEs(PWINDOW_OBJECT Window)
             CurrentWindow = UserGetWindowObject(pDCE->hwndCurrent);
             if (NULL == CurrentWindow)
             {
-               pDCE = (PDCE) pDCE->List.Flink;
+               pLE = pDCE->List.Flink;
+               pDCE = CONTAINING_RECORD(pLE, DCE, List);
                continue;
             }
          }
@@ -712,24 +862,25 @@ DceResetActiveDCEs(PWINDOW_OBJECT Window)
          if (!GDIOBJ_ValidateHandle(pDCE->hDC, GDI_OBJECT_TYPE_DC) ||
              (dc = DC_LockDc(pDCE->hDC)) == NULL)
          {
-            pDCE = (PDCE) pDCE->List.Flink;
+            pLE = pDCE->List.Flink;
+            pDCE = CONTAINING_RECORD(pLE, DCE, List);
             continue;
          }
          if (Window == CurrentWindow || IntIsChildWindow(Window, CurrentWindow))
          {
             if (pDCE->DCXFlags & DCX_WINDOW)
             {
-               DeltaX = CurrentWindow->Wnd->rcWindow.left - dc->ptlDCOrig.x;
-               DeltaY = CurrentWindow->Wnd->rcWindow.top - dc->ptlDCOrig.y;
-               dc->ptlDCOrig.x = CurrentWindow->Wnd->rcWindow.left;
-               dc->ptlDCOrig.y = CurrentWindow->Wnd->rcWindow.top;
+               DeltaX = CurrentWindow->rcWindow.left - dc->ptlDCOrig.x;
+               DeltaY = CurrentWindow->rcWindow.top - dc->ptlDCOrig.y;
+               dc->ptlDCOrig.x = CurrentWindow->rcWindow.left;
+               dc->ptlDCOrig.y = CurrentWindow->rcWindow.top;
             }
             else
             {
-               DeltaX = CurrentWindow->Wnd->rcClient.left - dc->ptlDCOrig.x;
-               DeltaY = CurrentWindow->Wnd->rcClient.top - dc->ptlDCOrig.y;
-               dc->ptlDCOrig.x = CurrentWindow->Wnd->rcClient.left;
-               dc->ptlDCOrig.y = CurrentWindow->Wnd->rcClient.top;
+               DeltaX = CurrentWindow->rcClient.left - dc->ptlDCOrig.x;
+               DeltaY = CurrentWindow->rcClient.top - dc->ptlDCOrig.y;
+               dc->ptlDCOrig.x = CurrentWindow->rcClient.left;
+               dc->ptlDCOrig.y = CurrentWindow->rcClient.top;
             }
             if (NULL != dc->rosdc.hClipRgn)
             {
@@ -745,46 +896,55 @@ DceResetActiveDCEs(PWINDOW_OBJECT Window)
 
          DceUpdateVisRgn(pDCE, CurrentWindow, pDCE->DCXFlags);
 
-         if (Window->hSelf != pDCE->hwndCurrent)
+         if (Window->head.h != pDCE->hwndCurrent)
          {
 //            IntEngWindowChanged(CurrentWindow, WOC_RGN_CLIENT);
 //            UserDerefObject(CurrentWindow);
          }
       }
-      pDCE =(PDCE) pDCE->List.Flink;
-   } while (pDCE != FirstDce);
+      pLE = pDCE->List.Flink;
+      pDCE = CONTAINING_RECORD(pLE, DCE, List);
+   }
+   while (pLE != &LEDce);
 }
 
 HWND FASTCALL
 IntWindowFromDC(HDC hDc)
 {
   DCE *Dce;
+  PLIST_ENTRY pLE;
   HWND Ret = NULL;
-  KeEnterCriticalRegion();
-  Dce = FirstDce;
+
+  pLE = LEDce.Flink;
+  Dce = CONTAINING_RECORD(pLE, DCE, List);
   do
   {
-      if(Dce->hDC == hDc)
+      if (Dce->hDC == hDc)
       {
-         Ret = Dce->hwndCurrent;
+         if (Dce->DCXFlags & DCX_INDESTROY)
+            Ret = NULL;
+         else
+            Ret = Dce->hwndCurrent;
          break;
       }
-      Dce = (PDCE)Dce->List.Flink;
-  } while (Dce != FirstDce);
-  KeLeaveCriticalRegion();
+      pLE = Dce->List.Flink;
+      Dce = CONTAINING_RECORD(pLE, DCE, List);
+  }
+  while (pLE != &LEDce);
   return Ret;
 }
 
 INT FASTCALL
-UserReleaseDC(PWINDOW_OBJECT Window, HDC hDc, BOOL EndPaint)
+UserReleaseDC(PWND Window, HDC hDc, BOOL EndPaint)
 {
   PDCE dce;
+  PLIST_ENTRY pLE;
   INT nRet = 0;
   BOOL Hit = FALSE;
 
   DPRINT("%p %p\n", Window, hDc);
-  dce = FirstDce;
-  KeEnterCriticalRegion();
+  pLE = LEDce.Flink;
+  dce = CONTAINING_RECORD(pLE, DCE, List);
   do
   {
      if(!dce) break;
@@ -793,11 +953,10 @@ UserReleaseDC(PWINDOW_OBJECT Window, HDC hDc, BOOL EndPaint)
         Hit = TRUE;
         break;
      }
-
-     dce = (PDCE) dce->List.Flink;
+     pLE = dce->List.Flink;
+     dce = CONTAINING_RECORD(pLE, DCE, List);
   }
-  while (dce != FirstDce );
-  KeLeaveCriticalRegion();
+  while (pLE != &LEDce );
 
   if ( Hit && (dce->DCXFlags & DCX_DCEBUSY))
   {
@@ -808,7 +967,7 @@ UserReleaseDC(PWINDOW_OBJECT Window, HDC hDc, BOOL EndPaint)
 }
 
 HDC FASTCALL
-UserGetWindowDC(PWINDOW_OBJECT Wnd)
+UserGetWindowDC(PWND Wnd)
 {
   return UserGetDCEx(Wnd, 0, DCX_USESTYLE | DCX_WINDOW);
 }
@@ -817,7 +976,7 @@ HWND FASTCALL
 UserGethWnd( HDC hdc, PWNDOBJ *pwndo)
 {
   PWNDGDI pWndgdi;
-  PWINDOW_OBJECT Wnd;
+  PWND Wnd;
   HWND hWnd;
 
   hWnd = IntWindowFromDC(hdc);
@@ -837,7 +996,7 @@ UserGethWnd( HDC hdc, PWNDOBJ *pwndo)
 HDC APIENTRY
 NtUserGetDCEx(HWND hWnd OPTIONAL, HANDLE ClipRegion, ULONG Flags)
 {
-  PWINDOW_OBJECT Wnd=NULL;
+  PWND Wnd=NULL;
   DECLARE_RETURN(HDC);
 
   DPRINT("Enter NtUserGetDCEx\n");