[NtGDI] Fix ExtSelectClipRgn Tests
[reactos.git] / win32ss / gdi / ntgdi / cliprgn.c
index a6dd20e..566ce3f 100644 (file)
@@ -2,7 +2,7 @@
  * COPYRIGHT:        GNU GPL, See COPYING in the top level directory
  * PROJECT:          ReactOS Win32k subsystem
  * PURPOSE:          Clip region functions
- * FILE:             subsystems/win32/win32k/objects/cliprgn.c
+ * FILE:             win32ss/gdi/ntgdi/cliprgn.c
  * PROGRAMER:        Unknown
  */
 
 #define NDEBUG
 #include <debug.h>
 
-int FASTCALL
-CLIPPING_UpdateGCRegion(DC* Dc)
+VOID
+FASTCALL
+IntGdiReleaseRaoRgn(PDC pDC)
+{
+    INT Index = GDI_HANDLE_GET_INDEX(pDC->BaseObject.hHmgr);
+    PGDI_TABLE_ENTRY Entry = &GdiHandleTable->Entries[Index];
+    pDC->fs |= DC_FLAG_DIRTY_RAO;
+    Entry->Flags |= GDI_ENTRY_VALIDATE_VIS; // Need to validate Vis.
+}
+
+VOID
+FASTCALL
+IntGdiReleaseVisRgn(PDC pDC)
 {
-   PROSRGNDATA CombinedRegion;
-   //HRGN hRgnVis;
-   PREGION prgnClip, prgnGCClip;
+    IntGdiReleaseRaoRgn(pDC);
+    REGION_Delete(pDC->prgnVis);
+    pDC->prgnVis = prgnDefault; // Vis can not be NULL!!!
+}
 
-    /* Would prefer this, but the rest of the code sucks... */
-    //ASSERT(Dc->rosdc.hGCClipRgn);
-    //ASSERT(Dc->rosdc.hClipRgn);
-   ASSERT(Dc->prgnVis);
-   //hRgnVis = Dc->prgnVis->BaseObject.hHmgr;
+//
+// Updating Vis Region Attribute the for DC Attributes.
+// BTW: This system region has an user attribute for it.
+//
+VOID
+FASTCALL
+UpdateVisRgn(
+    PDC pdc)
+{
+    INT Index = GDI_HANDLE_GET_INDEX(pdc->BaseObject.hHmgr);
+    PGDI_TABLE_ENTRY pEntry = &GdiHandleTable->Entries[Index];
+
+    /* Setup Vis Region Attribute information to User side */
+    pEntry->Flags |= GDI_ENTRY_VALIDATE_VIS;
+    pdc->pdcattr->VisRectRegion.iComplexity = REGION_GetRgnBox(pdc->prgnVis, &pdc->pdcattr->VisRectRegion.Rect);
+    pdc->pdcattr->VisRectRegion.AttrFlags = ATTR_RGN_VALID;
+    pEntry->Flags &= ~GDI_ENTRY_VALIDATE_VIS;
+}
 
-   if (Dc->rosdc.hGCClipRgn == NULL)
-      Dc->rosdc.hGCClipRgn = IntSysCreateRectRgn(0, 0, 0, 0);
+//
+//  Selecting Vis Region.
+//
+VOID
+FASTCALL
+GdiSelectVisRgn(
+    HDC hdc,
+    PREGION prgn)
+{
+    DC *dc;
 
-   prgnGCClip = REGION_LockRgn(Dc->rosdc.hGCClipRgn);
-   ASSERT(prgnGCClip);
+    if (!(dc = DC_LockDc(hdc)))
+    {
+        EngSetLastError(ERROR_INVALID_HANDLE);
+        return;
+    }
 
-   if (Dc->rosdc.hClipRgn == NULL)
-      IntGdiCombineRgn(prgnGCClip, Dc->prgnVis, NULL, RGN_COPY);
-   else
-   {
-      prgnClip = REGION_LockRgn(Dc->rosdc.hClipRgn); // FIXME: Locking order, ugh!
-      IntGdiCombineRgn(prgnGCClip, Dc->prgnVis, prgnClip, RGN_AND);
-      REGION_UnlockRgn(prgnClip);
-   }
-   REGION_UnlockRgn(prgnGCClip);
+    if (!prgn)
+    {
+       DPRINT1("SVR: Setting NULL Region\n");
+       IntGdiReleaseVisRgn(dc);
+       IntSetDefaultRegion(dc);
+       DC_UnlockDc(dc);
+       return;
+    }
 
-   NtGdiOffsetRgn(Dc->rosdc.hGCClipRgn, Dc->ptlDCOrig.x, Dc->ptlDCOrig.y);
+    dc->fs |= DC_FLAG_DIRTY_RAO;
 
-   if((CombinedRegion = RGNOBJAPI_Lock(Dc->rosdc.hGCClipRgn, NULL)))
-   {
-     CLIPOBJ *CombinedClip;
+    ASSERT(dc->prgnVis != NULL);
+    ASSERT(prgn != NULL);
 
-     CombinedClip = IntEngCreateClipRegion(CombinedRegion->rdh.nCount,
-        CombinedRegion->Buffer,
-        &CombinedRegion->rdh.rcBound);
+    REGION_bCopy(dc->prgnVis, prgn);
+    REGION_bOffsetRgn(dc->prgnVis, -dc->ptlDCOrig.x, -dc->ptlDCOrig.y);
 
-     RGNOBJAPI_Unlock(CombinedRegion);
+    DC_UnlockDc(dc);
+}
 
-     if ( !CombinedClip )
-     {
-       DPRINT1("IntEngCreateClipRegion() failed\n");
-       return ERROR;
-     }
+_Success_(return!=ERROR)
+int
+FASTCALL
+IntSelectClipRgn(
+    _In_ PDC dc,
+    _In_ PREGION prgn,
+    _In_ int fnMode)
+{
+    int Ret = ERROR;
+    PREGION prgnNClip, prgnOrigClip = dc->dclevel.prgnClip;
+
+    //
+    // No Coping Regions and no intersecting Regions or an User calling w NULL Region or have the Original Clip Region.
+    //
+    if (fnMode != RGN_COPY && (fnMode != RGN_AND || !prgn || prgnOrigClip))
+    {
+        prgnNClip = IntSysCreateRectpRgn(0, 0, 0, 0);
+
+        // Have Original Clip Region.
+        if (prgnOrigClip)
+        {
+           // This will fail on NULL prgn.
+           Ret = IntGdiCombineRgn(prgnNClip, prgnOrigClip, prgn, fnMode);
+
+           if (Ret)
+           {
+              REGION_Delete(prgnOrigClip);
+              dc->dclevel.prgnClip = prgnNClip;
+              IntGdiReleaseRaoRgn(dc);
+           }
+           else
+              REGION_Delete(prgnNClip);
+        }
+        else // NULL Original Clip Region, setup a new one and process mode.
+        {
+            PREGION prgnClip;
+            RECTL rcl;
+            PSURFACE pSurface;
+
+            // See IntSetDefaultRegion.
+
+            rcl.left   = 0;
+            rcl.top    = 0;
+            rcl.right  = dc->dclevel.sizl.cx;
+            rcl.bottom = dc->dclevel.sizl.cy;
+
+            //EngAcquireSemaphoreShared(pdc->ppdev->hsemDevLock);
+            if (dc->ppdev->flFlags & PDEV_META_DEVICE)
+            {
+                pSurface = dc->dclevel.pSurface;
+                if (pSurface && pSurface->flags & PDEV_SURFACE)
+                {
+                   rcl.left   += dc->ppdev->ptlOrigion.x;
+                   rcl.top    += dc->ppdev->ptlOrigion.y;
+                   rcl.right  += dc->ppdev->ptlOrigion.x;
+                   rcl.bottom += dc->ppdev->ptlOrigion.y;
+                }
+            }
+            //EngReleaseSemaphore(pdc->ppdev->hsemDevLock);
+
+            rcl.left   += dc->ptlDCOrig.x;
+            rcl.top    += dc->ptlDCOrig.y;
+            rcl.right  += dc->ptlDCOrig.x;
+            rcl.bottom += dc->ptlDCOrig.y;
+
+            prgnClip = IntSysCreateRectpRgnIndirect(&rcl);
+
+            Ret = IntGdiCombineRgn(prgnNClip, prgnClip, prgn, fnMode);
+
+            if (Ret)
+            {
+                dc->dclevel.prgnClip = prgnNClip;
+                IntGdiReleaseRaoRgn(dc);
+            }
+            else
+                REGION_Delete(prgnNClip);
+
+            REGION_Delete(prgnClip);
+        }
+        return Ret;
+    }
 
-     if(Dc->rosdc.CombinedClip != NULL)
-       IntEngDeleteClipRegion(Dc->rosdc.CombinedClip);
+    // Fall through to normal RectOS mode.
 
-      Dc->rosdc.CombinedClip = CombinedClip ;
-   }
+    //
+    // Handle NULL Region and Original Clip Region.
+    //
+    if (!prgn)
+    {
+        if (prgnOrigClip)
+        {
+            REGION_Delete(dc->dclevel.prgnClip);
+            dc->dclevel.prgnClip = NULL;
+            IntGdiReleaseRaoRgn(dc);
+        }
+        return SIMPLEREGION;
+    }
 
-   return NtGdiOffsetRgn(Dc->rosdc.hGCClipRgn, -Dc->ptlDCOrig.x, -Dc->ptlDCOrig.y);
+    //
+    // Combine the new Clip region with original Clip and caller Region.
+    //
+    if ( prgnOrigClip &&
+        (Ret = IntGdiCombineRgn(prgnOrigClip, prgn, NULL, RGN_COPY)) ) // Clip could fail.
+    {
+        IntGdiReleaseRaoRgn(dc);
+    }
+    else // NULL original Clip, just copy caller region to new.
+    {
+       prgnNClip = IntSysCreateRectpRgn(0, 0, 0, 0);
+       REGION_bCopy(prgnNClip, prgn);
+       Ret = REGION_Complexity(prgnNClip);
+       dc->dclevel.prgnClip = prgnNClip;
+       IntGdiReleaseRaoRgn(dc);
+    }
+    return Ret;
 }
 
-INT FASTCALL
-GdiSelectVisRgn(HDC hdc, HRGN hrgn)
+//
+// Call from Gdi Batch Subsystem.
+//
+// Was setup to just handle RGN_COPY only and return VOID, since this was called from Gdi32.
+// Tested in place of the other, complexity aside.
+//
+
+_Success_(return!=ERROR)
+int
+FASTCALL
+IntGdiExtSelectClipRect(
+    _In_ PDC dc,
+    _In_ PRECTL prcl,
+    _In_ int fnMode)
 {
-  int retval;
-  DC *dc;
-  PREGION prgn;
+    int Ret = ERROR;
+    PREGION prgn;
+    RECTL rect;
+    BOOL NoRegion = fnMode & GDIBS_NORECT;
 
-  if (!hrgn)
-  {
-       EngSetLastError(ERROR_INVALID_PARAMETER);
-       return ERROR;
-  }
-  if (!(dc = DC_LockDc(hdc)))
-  {
-       EngSetLastError(ERROR_INVALID_HANDLE);
-       return ERROR;
-  }
+    fnMode &= ~GDIBS_NORECT;
 
-  dc->fs &= ~DC_FLAG_DIRTY_RAO;
+    if (NoRegion) // NULL Region.
+    {
+        if (fnMode == RGN_COPY)
+        {
+           Ret = IntSelectClipRgn( dc, NULL, RGN_COPY);
 
-  ASSERT (dc->prgnVis != NULL);
+           if (dc->fs & DC_FLAG_DIRTY_RAO)
+               CLIPPING_UpdateGCRegion(dc);
 
-  prgn = RGNOBJAPI_Lock(hrgn, NULL);
-  retval = prgn ? IntGdiCombineRgn(dc->prgnVis, prgn, NULL, RGN_COPY) : ERROR;
-  RGNOBJAPI_Unlock(prgn);
-  if ( retval != ERROR )
-  {
-    IntGdiOffsetRgn(dc->prgnVis, -dc->ptlDCOrig.x, -dc->ptlDCOrig.y);
-    CLIPPING_UpdateGCRegion(dc);
-  }
-  DC_UnlockDc(dc);
+           if (Ret) // Copy? Return Vis complexity.
+               Ret = REGION_Complexity(dc->prgnVis);
+        }
+    }
+    else // Have a box to build a region with.
+    {
+        if (dc->dclevel.prgnClip)
+        {
+            REGION_GetRgnBox(dc->dclevel.prgnClip, &rect);
+
+            if (prcl->left   == rect.left  &&
+                prcl->top    == rect.top   &&
+                prcl->right  == rect.right &&
+                prcl->bottom == rect.bottom)
+            {
+                return REGION_Complexity( dc->prgnRao ? dc->prgnRao : dc->prgnVis );
+            }
+        }
 
-  return retval;
-}
+        prgn = IntSysCreateRectpRgnIndirect(prcl);
+
+        Ret = IntSelectClipRgn( dc, prgn, fnMode);
 
+        if (dc->fs & DC_FLAG_DIRTY_RAO)
+            CLIPPING_UpdateGCRegion(dc);
 
-int FASTCALL GdiExtSelectClipRgn(PDC dc,
-                                 HRGN hrgn,
-                                 int fnMode)
+        if (Ret) // In this case NtGdiExtSelectClipRgn tests pass.
+            Ret = REGION_Complexity( dc->prgnRao ? dc->prgnRao : dc->prgnVis );
+
+        REGION_Delete(prgn);
+    }
+    return Ret;
+}
+
+_Success_(return!=ERROR)
+int
+FASTCALL
+IntGdiExtSelectClipRgn(
+    _In_ PDC dc,
+    _In_ PREGION prgn,
+    _In_ int fnMode)
 {
-  // dc->fs &= ~DC_FLAG_DIRTY_RAO;
+    int Ret = ERROR;
 
-  if (!hrgn)
-  {
-    if (fnMode == RGN_COPY)
+    if (!prgn)
     {
-      if (dc->rosdc.hClipRgn != NULL)
-      {
-        GreDeleteObject(dc->rosdc.hClipRgn);
-        dc->rosdc.hClipRgn = NULL;
-      }
+        if (fnMode == RGN_COPY)
+        {
+           if ((Ret = IntSelectClipRgn( dc, NULL, RGN_COPY)))
+               Ret = REGION_Complexity(dc->prgnVis);
+        }
     }
     else
     {
-      EngSetLastError(ERROR_INVALID_PARAMETER);
-      return ERROR;
+        if ((Ret = IntSelectClipRgn( dc, prgn, fnMode)))
+        {
+            DPRINT("IntGdiExtSelectClipRgn A %d\n",Ret);
+            // Update the Rao, it must be this way for now.
+            if (dc->fs & DC_FLAG_DIRTY_RAO)
+                CLIPPING_UpdateGCRegion(dc);
+
+            Ret = REGION_Complexity( dc->prgnRao ? dc->prgnRao : dc->prgnVis );
+            DPRINT("IntGdiExtSelectClipRgn B %d\n",Ret);
+        }
     }
-  }
-  else
-  {
-    if (!dc->rosdc.hClipRgn)
+    return Ret;
+}
+
+int
+APIENTRY
+NtGdiExtSelectClipRgn(
+    HDC  hDC,
+    HRGN  hrgn,
+    int  fnMode)
+{
+    int retval;
+    DC *dc;
+    PREGION prgn;
+
+    if ( fnMode < RGN_AND || fnMode > RGN_COPY )
     {
-      RECTL rect;
-      if(dc->prgnVis)
-      {
-        REGION_GetRgnBox(dc->prgnVis, &rect);
-        dc->rosdc.hClipRgn = IntSysCreateRectRgnIndirect(&rect);
-      }
-      else
-      {
-        dc->rosdc.hClipRgn = IntSysCreateRectRgn(0, 0, 0, 0);
-      }
+        EngSetLastError(ERROR_INVALID_PARAMETER);
+        return ERROR;
     }
-    if(fnMode == RGN_COPY)
+
+    if (!(dc = DC_LockDc(hDC)))
     {
-      NtGdiCombineRgn(dc->rosdc.hClipRgn, hrgn, 0, fnMode);
+        EngSetLastError(ERROR_INVALID_HANDLE);
+        return ERROR;
+    }
+
+    prgn = REGION_LockRgn(hrgn);
+
+    if ((prgn == NULL) && (fnMode != RGN_COPY))
+    {
+        //EngSetLastError(ERROR_INVALID_HANDLE); doesn't set this.
+        retval = ERROR;
     }
     else
-      NtGdiCombineRgn(dc->rosdc.hClipRgn, dc->rosdc.hClipRgn, hrgn, fnMode);
-  }
+    {
+#if 0   // Testing GDI Batch.
+        {
+            RECTL rcl;
+            if (prgn)
+                REGION_GetRgnBox(prgn, &rcl);
+            else
+                fnMode |= GDIBS_NORECT;
+            retval = IntGdiExtSelectClipRect(dc, &rcl, fnMode);
+        }
+#else
+        retval = IntGdiExtSelectClipRgn(dc, prgn, fnMode);
+#endif
+    }
 
-  return CLIPPING_UpdateGCRegion(dc);
-}
+    if (prgn)
+        REGION_UnlockRgn(prgn);
 
+    DC_UnlockDc(dc);
+    return retval;
+}
 
-int APIENTRY NtGdiExtSelectClipRgn(HDC  hDC,
-                                HRGN  hrgn,
-                               int  fnMode)
+_Success_(return!=ERROR)
+INT
+FASTCALL
+GdiGetClipBox(
+    _In_ HDC hdc,
+    _Out_ LPRECT prc)
 {
-  int retval;
-  DC *dc;
+    PDC pdc;
+    INT iComplexity;
 
-  if (!(dc = DC_LockDc(hDC)))
-  {
-       EngSetLastError(ERROR_INVALID_HANDLE);
-       return ERROR;
-  }
+    /* Lock the DC */
+    pdc = DC_LockDc(hdc);
+    if (!pdc)
+    {
+        return ERROR;
+    }
 
-  retval = GdiExtSelectClipRgn ( dc, hrgn, fnMode );
+    /* Update RAO region if necessary */
+    if (pdc->fs & DC_FLAG_DIRTY_RAO)
+        CLIPPING_UpdateGCRegion(pdc);
 
-  DC_UnlockDc(dc);
-  return retval;
+    /* Check if we have a RAO region (intersection of API and VIS region) */
+    if (pdc->prgnRao)
+    {
+        /* We have a RAO region, use it */
+        iComplexity = REGION_GetRgnBox(pdc->prgnRao, prc);
+    }
+    else
+    {
+        /* No RAO region means no API region, so use the VIS region */
+        ASSERT(pdc->prgnVis);
+        iComplexity = REGION_GetRgnBox(pdc->prgnVis, prc);
+    }
+
+    /* Unlock the DC */
+    DC_UnlockDc(pdc);
+
+    /* Convert the rect to logical coordinates */
+    IntDPtoLP(pdc, (LPPOINT)prc, 2);
+
+    /* Return the complexity */
+    return iComplexity;
 }
 
-INT FASTCALL
-GdiGetClipBox(HDC hDC, PRECTL rc)
+_Success_(return!=ERROR)
+INT
+APIENTRY
+NtGdiGetAppClipBox(
+    _In_ HDC hdc,
+    _Out_ LPRECT prc)
 {
-   INT retval;
-   PDC dc;
-   PROSRGNDATA pRgnNew, pRgn = NULL;
-   BOOL Unlock = FALSE; // Small HACK
-
-   if (!(dc = DC_LockDc(hDC)))
-   {
-      return ERROR;
-   }
-
-   /* FIXME: Rao and Vis only! */
-   if (dc->prgnAPI) // APIRGN
-   {
-      pRgn = dc->prgnAPI;
-   }
-   else if (dc->dclevel.prgnMeta) // METARGN
-   {
-      pRgn = dc->dclevel.prgnMeta;
-   }
-   else if (dc->rosdc.hClipRgn)
-   {
-          Unlock = TRUE ;
-       pRgn = REGION_LockRgn(dc->rosdc.hClipRgn); // CLIPRGN
-   }
-
-   if (pRgn)
-   {
-      pRgnNew = IntSysCreateRectpRgn( 0, 0, 0, 0 );
-
-         if (!pRgnNew)
-      {
-         DC_UnlockDc(dc);
-                if(Unlock) REGION_UnlockRgn(pRgn);
-         return ERROR;
-      }
-
-      IntGdiCombineRgn(pRgnNew, dc->prgnVis, pRgn, RGN_AND);
-
-      retval = REGION_GetRgnBox(pRgnNew, rc);
-
-         REGION_Delete(pRgnNew);
-
-      DC_UnlockDc(dc);
-         if(Unlock) REGION_UnlockRgn(pRgn);
-      return retval;
-   }
-
-   retval = REGION_GetRgnBox(dc->prgnVis, rc);
-   IntDPtoLP(dc, (LPPOINT)rc, 2);
-   DC_UnlockDc(dc);
-
-   return retval;
+    RECT rect;
+    INT iComplexity;
+
+    /* Call the internal function */
+    iComplexity = GdiGetClipBox(hdc, &rect);
+
+    if (iComplexity != ERROR)
+    {
+        _SEH2_TRY
+        {
+            ProbeForWrite(prc, sizeof(RECT), 1);
+            *prc = rect;
+        }
+        _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+        {
+            iComplexity = ERROR;
+        }
+        _SEH2_END
+    }
+
+    /* Return the complexity */
+    return iComplexity;
 }
 
-INT APIENTRY
-NtGdiGetAppClipBox(HDC hDC, PRECTL rc)
+INT
+APIENTRY
+NtGdiExcludeClipRect(
+    _In_ HDC hdc,
+    _In_ INT xLeft,
+    _In_ INT yTop,
+    _In_ INT xRight,
+    _In_ INT yBottom)
 {
-  INT Ret;
-  NTSTATUS Status = STATUS_SUCCESS;
-  RECTL Saferect;
+    INT iComplexity;
+    RECTL rect;
+    PDC pdc;
 
-  Ret = GdiGetClipBox(hDC, &Saferect);
+    /* Lock the DC */
+    pdc = DC_LockDc(hdc);
+    if (pdc == NULL)
+    {
+        EngSetLastError(ERROR_INVALID_HANDLE);
+        return ERROR;
+    }
 
-  _SEH2_TRY
-  {
-    ProbeForWrite(rc,
-                  sizeof(RECT),
-                  1);
-    *rc = Saferect;
-  }
-  _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
-  {
-    Status = _SEH2_GetExceptionCode();
-  }
-  _SEH2_END;
+    /* Convert coordinates to device space */
+    rect.left = xLeft;
+    rect.top = yTop;
+    rect.right = xRight;
+    rect.bottom = yBottom;
+    RECTL_vMakeWellOrdered(&rect);
+    IntLPtoDP(pdc, (LPPOINT)&rect, 2);
 
-  if(!NT_SUCCESS(Status))
-  {
-    SetLastNtError(Status);
-    return ERROR;
-  }
+    /* Check if we already have a clip region */
+    if (pdc->dclevel.prgnClip != NULL)
+    {
+        /* We have a region, subtract the rect */
+        iComplexity = REGION_SubtractRectFromRgn(pdc->dclevel.prgnClip,
+                                                 pdc->dclevel.prgnClip,
+                                                 &rect);
+    }
+    else
+    {
+        /* We don't have a clip region yet, create an empty region */
+        pdc->dclevel.prgnClip = IntSysCreateRectpRgn(0, 0, 0, 0);
+        if (pdc->dclevel.prgnClip == NULL)
+        {
+            iComplexity = ERROR;
+        }
+        else
+        {
+            /* Subtract the rect from the VIS region */
+            iComplexity = REGION_SubtractRectFromRgn(pdc->dclevel.prgnClip,
+                                                     pdc->prgnVis,
+                                                     &rect);
+        }
+    }
 
-  return Ret;
-}
+    /* Emulate Windows behavior */
+    if (iComplexity == SIMPLEREGION)
+        iComplexity = COMPLEXREGION;
 
-int APIENTRY NtGdiExcludeClipRect(HDC  hDC,
-                         int  LeftRect,
-                         int  TopRect,
-                         int  RightRect,
-                         int  BottomRect)
-{
-   INT Result;
-   RECTL Rect;
-   PREGION prgnNew, prgnClip;
-   PDC dc = DC_LockDc(hDC);
-
-   if (!dc)
-   {
-      EngSetLastError(ERROR_INVALID_HANDLE);
-      return ERROR;
-   }
-
-   Rect.left = LeftRect;
-   Rect.top = TopRect;
-   Rect.right = RightRect;
-   Rect.bottom = BottomRect;
-
-   IntLPtoDP(dc, (LPPOINT)&Rect, 2);
-
-   prgnNew = IntSysCreateRectpRgnIndirect(&Rect);
-   if (!prgnNew)
-   {
-      Result = ERROR;
-   }
-   else
-   {
-      if (!dc->rosdc.hClipRgn)
-      {
-         dc->rosdc.hClipRgn = IntSysCreateRectRgn(0, 0, 0, 0);
-         prgnClip = REGION_LockRgn(dc->rosdc.hClipRgn);
-         IntGdiCombineRgn(prgnClip, dc->prgnVis, prgnNew, RGN_DIFF);
-         REGION_UnlockRgn(prgnClip);
-         Result = SIMPLEREGION;
-      }
-      else
-      {
-         prgnClip = REGION_LockRgn(dc->rosdc.hClipRgn);
-         Result = IntGdiCombineRgn(prgnClip, prgnClip, prgnNew, RGN_DIFF);
-         REGION_UnlockRgn(prgnClip);
-      }
-      REGION_Delete(prgnNew);
-   }
-   if (Result != ERROR)
-      CLIPPING_UpdateGCRegion(dc);
-
-   DC_UnlockDc(dc);
-
-   return Result;
+    /* If we succeeded, mark the RAO region as dirty */
+    if (iComplexity != ERROR)
+        pdc->fs |= DC_FLAG_DIRTY_RAO;
+
+    /* Unlock the DC */
+    DC_UnlockDc(pdc);
+
+    return iComplexity;
 }
 
-int APIENTRY NtGdiIntersectClipRect(HDC  hDC,
-                           int  LeftRect,
-                           int  TopRect,
-                           int  RightRect,
-                           int  BottomRect)
+INT
+APIENTRY
+NtGdiIntersectClipRect(
+    _In_ HDC hdc,
+    _In_ INT xLeft,
+    _In_ INT yTop,
+    _In_ INT xRight,
+    _In_ INT yBottom)
 {
-   INT Result;
-   RECTL Rect;
-   HRGN NewRgn;
-   PDC dc = DC_LockDc(hDC);
-
-   DPRINT("NtGdiIntersectClipRect(%p, %d,%d-%d,%d)\n",
-      hDC, LeftRect, TopRect, RightRect, BottomRect);
-
-   if (!dc)
-   {
-      EngSetLastError(ERROR_INVALID_HANDLE);
-      return ERROR;
-   }
-
-   Rect.left = LeftRect;
-   Rect.top = TopRect;
-   Rect.right = RightRect;
-   Rect.bottom = BottomRect;
-
-   IntLPtoDP(dc, (LPPOINT)&Rect, 2);
-
-   NewRgn = IntSysCreateRectRgnIndirect(&Rect);
-   if (!NewRgn)
-   {
-      Result = ERROR;
-   }
-   else if (!dc->rosdc.hClipRgn)
-   {
-      dc->rosdc.hClipRgn = NewRgn;
-      Result = SIMPLEREGION;
-   }
-   else
-   {
-      Result = NtGdiCombineRgn(dc->rosdc.hClipRgn, dc->rosdc.hClipRgn, NewRgn, RGN_AND);
-      GreDeleteObject(NewRgn);
-   }
-   if (Result != ERROR)
-      CLIPPING_UpdateGCRegion(dc);
-
-   DC_UnlockDc(dc);
-
-   return Result;
+    INT iComplexity;
+    RECTL rect;
+    PREGION prgnNew;
+    PDC pdc;
+
+    DPRINT("NtGdiIntersectClipRect(%p, %d,%d-%d,%d)\n",
+            hdc, xLeft, yTop, xRight, yBottom);
+
+    /* Lock the DC */
+    pdc = DC_LockDc(hdc);
+    if (!pdc)
+    {
+        EngSetLastError(ERROR_INVALID_HANDLE);
+        return ERROR;
+    }
+
+    /* Convert coordinates to device space */
+    rect.left = xLeft;
+    rect.top = yTop;
+    rect.right = xRight;
+    rect.bottom = yBottom;
+    IntLPtoDP(pdc, (LPPOINT)&rect, 2);
+
+    /* Check if we already have a clip region */
+    if (pdc->dclevel.prgnClip != NULL)
+    {
+        /* We have a region, crop it */
+        iComplexity = REGION_CropRegion(pdc->dclevel.prgnClip,
+                                        pdc->dclevel.prgnClip,
+                                        &rect);
+    }
+    else
+    {
+        /* We don't have a region yet, allocate a new one */
+        prgnNew = IntSysCreateRectpRgnIndirect(&rect);
+        if (prgnNew == NULL)
+        {
+            iComplexity = ERROR;
+        }
+        else
+        {
+            /* Set the new region */
+            pdc->dclevel.prgnClip = prgnNew;
+            iComplexity = SIMPLEREGION;
+        }
+    }
+
+    /* If we succeeded, mark the RAO region as dirty */
+    if (iComplexity != ERROR)
+        pdc->fs |= DC_FLAG_DIRTY_RAO;
+
+    /* Unlock the DC */
+    DC_UnlockDc(pdc);
+
+    return iComplexity;
 }
 
-int APIENTRY NtGdiOffsetClipRgn(HDC  hDC,
-                       int  XOffset,
-                       int  YOffset)
+INT
+APIENTRY
+NtGdiOffsetClipRgn(
+    _In_ HDC hdc,
+    _In_ INT xOffset,
+    _In_ INT yOffset)
 {
-  INT Result;
-  DC *dc;
+    INT iComplexity;
+    PDC pdc;
+    POINTL apt[2];
 
-  if(!(dc = DC_LockDc(hDC)))
-  {
-    EngSetLastError(ERROR_INVALID_HANDLE);
-    return ERROR;
-  }
+    /* Lock the DC */
+    pdc = DC_LockDc(hdc);
+    if (pdc == NULL)
+    {
+        if (!hdc) EngSetLastError(ERROR_INVALID_HANDLE);
+        return ERROR;
+    }
 
-  if(dc->rosdc.hClipRgn != NULL)
-  {
-    Result = NtGdiOffsetRgn(dc->rosdc.hClipRgn,
-                            XOffset,
-                            YOffset);
-    CLIPPING_UpdateGCRegion(dc);
-  }
-  else
-  {
-    Result = NULLREGION;
-  }
+    /* Check if we have a clip region */
+    if (pdc->dclevel.prgnClip != NULL)
+    {
+        /* Convert coordinates into device space. Note that we need to convert
+           2 coordinates to account for rotation / shear / offset */
+        apt[0].x = 0;
+        apt[0].y = 0;
+        apt[1].x = xOffset;
+        apt[1].y = yOffset;
+        IntLPtoDP(pdc, &apt, 2);
+
+        /* Offset the clip region */
+        if (!REGION_bOffsetRgn(pdc->dclevel.prgnClip,
+                               apt[1].x - apt[0].x,
+                               apt[1].y - apt[0].y))
+        {
+            iComplexity = ERROR;
+        }
+        else
+        {
+            IntGdiReleaseRaoRgn(pdc);
+            UpdateVisRgn(pdc);
+            iComplexity = REGION_Complexity(pdc->dclevel.prgnClip);
+        }
+
+        /* Mark the RAO region as dirty */
+        pdc->fs |= DC_FLAG_DIRTY_RAO;
+    }
+    else
+    {
+        /* NULL means no clipping, i.e. the "whole" region */
+        iComplexity = SIMPLEREGION;
+    }
 
-  DC_UnlockDc(dc);
-  return Result;
+    /* Unlock the DC and return the complexity */
+    DC_UnlockDc(pdc);
+    return iComplexity;
 }
 
 BOOL APIENTRY NtGdiPtVisible(HDC  hDC,
                     int  X,
                     int  Y)
 {
-  HRGN rgn;
-  DC *dc;
+    BOOL ret = FALSE;
+    PDC dc;
+    PREGION prgn;
 
-  if(!(dc = DC_LockDc(hDC)))
-  {
-    EngSetLastError(ERROR_INVALID_HANDLE);
-    return FALSE;
-  }
+    if(!(dc = DC_LockDc(hDC)))
+    {
+        EngSetLastError(ERROR_INVALID_HANDLE);
+        return FALSE;
+    }
+
+    prgn = dc->prgnRao ? dc->prgnRao : dc->prgnVis;
 
-  rgn = dc->rosdc.hGCClipRgn;
-  DC_UnlockDc(dc);
+    if (prgn)
+    {
+        POINT pt = {X, Y};
+        IntLPtoDP(dc, &pt, 1);
+        ret = REGION_PtInRegion(prgn, pt.x, pt.y);
+    }
 
-  return (rgn ? NtGdiPtInRegion(rgn, X, Y) : FALSE);
+    DC_UnlockDc(dc);
+
+    return ret;
 }
 
-BOOL APIENTRY NtGdiRectVisible(HDC  hDC,
-                      LPRECT UnsafeRect)
+BOOL
+APIENTRY
+NtGdiRectVisible(
+    HDC hDC,
+    LPRECT UnsafeRect)
 {
-   NTSTATUS Status = STATUS_SUCCESS;
-   PROSRGNDATA Rgn;
-   PDC dc = DC_LockDc(hDC);
-   BOOL Result = FALSE;
-   RECTL Rect;
-
-   if (!dc)
-   {
-      EngSetLastError(ERROR_INVALID_HANDLE);
-      return FALSE;
-   }
-
-   _SEH2_TRY
-   {
-      ProbeForRead(UnsafeRect,
+    NTSTATUS Status = STATUS_SUCCESS;
+    PDC dc = DC_LockDc(hDC);
+    BOOL Result = FALSE;
+    RECTL Rect;
+    PREGION prgn;
+
+    if (!dc)
+    {
+        EngSetLastError(ERROR_INVALID_HANDLE);
+        return FALSE;
+    }
+
+    _SEH2_TRY
+    {
+        ProbeForRead(UnsafeRect,
                    sizeof(RECT),
                    1);
-      Rect = *UnsafeRect;
-   }
-   _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
-   {
-      Status = _SEH2_GetExceptionCode();
-   }
-   _SEH2_END;
-
-   if(!NT_SUCCESS(Status))
-   {
-      DC_UnlockDc(dc);
-      SetLastNtError(Status);
-      return FALSE;
-   }
-
-   if (dc->rosdc.hGCClipRgn)
-   {
-      if((Rgn = (PROSRGNDATA)RGNOBJAPI_Lock(dc->rosdc.hGCClipRgn, NULL)))
-      {
+        Rect = *UnsafeRect;
+    }
+    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+    {
+        Status = _SEH2_GetExceptionCode();
+    }
+    _SEH2_END;
+
+    if(!NT_SUCCESS(Status))
+    {
+        DC_UnlockDc(dc);
+        SetLastNtError(Status);
+        return FALSE;
+    }
+
+    if (dc->fs & DC_FLAG_DIRTY_RAO)
+        CLIPPING_UpdateGCRegion(dc);
+
+    prgn = dc->prgnRao ? dc->prgnRao : dc->prgnVis;
+    if (prgn)
+    {
          IntLPtoDP(dc, (LPPOINT)&Rect, 2);
-         Result = REGION_RectInRegion(Rgn, &Rect);
-         RGNOBJAPI_Unlock(Rgn);
-      }
-   }
-   DC_UnlockDc(dc);
+         Result = REGION_RectInRegion(prgn, &Rect);
+    }
+    DC_UnlockDc(dc);
 
-   return Result;
+    return Result;
 }
 
 int
 FASTCALL
 IntGdiSetMetaRgn(PDC pDC)
 {
-  INT Ret = ERROR;
-  PROSRGNDATA TempRgn;
+    INT Ret = ERROR;
 
-  if ( pDC->dclevel.prgnMeta )
-  {
-     if ( pDC->dclevel.prgnClip )
-     {
-        TempRgn = IntSysCreateRectpRgn(0,0,0,0);
-        if (TempRgn)
+    if ( pDC->dclevel.prgnMeta )
+    {
+        if ( pDC->dclevel.prgnClip )
         {
-           Ret = IntGdiCombineRgn( TempRgn,
-                     pDC->dclevel.prgnMeta,
-                     pDC->dclevel.prgnClip,
-                                   RGN_AND);
-           if ( Ret )
-           {
-              GDIOBJ_vDereferenceObject(&pDC->dclevel.prgnMeta->BaseObject);
-              if (!((PROSRGNDATA)pDC->dclevel.prgnMeta)->BaseObject.ulShareCount)
-                 REGION_Delete(pDC->dclevel.prgnMeta);
-
-              pDC->dclevel.prgnMeta = TempRgn;
-
-              GDIOBJ_vDereferenceObject(&pDC->dclevel.prgnClip->BaseObject);
-              if (!((PROSRGNDATA)pDC->dclevel.prgnClip)->BaseObject.ulShareCount)
-                 REGION_Delete(pDC->dclevel.prgnClip);
-
-              pDC->dclevel.prgnClip = NULL;
-
-              IntGdiReleaseRaoRgn(pDC);
-           }
-           else
-              REGION_Delete(TempRgn);
+            PREGION prgn = IntSysCreateRectpRgn(0,0,0,0);
+            if ( prgn )
+            {
+                if (REGION_bIntersectRegion(prgn, pDC->dclevel.prgnMeta, pDC->dclevel.prgnClip))
+                {
+                    // See Restore/SaveDC
+                    REGION_Delete(pDC->dclevel.prgnMeta);
+                    pDC->dclevel.prgnMeta = prgn;
+
+                    REGION_Delete(pDC->dclevel.prgnClip);
+                    pDC->dclevel.prgnClip = NULL;
+                    IntGdiReleaseRaoRgn(pDC);
+
+                    Ret = REGION_Complexity(pDC->dclevel.prgnMeta);
+                }
+                else
+                    REGION_Delete(prgn);
+            }
         }
-     }
-     else
-        Ret = REGION_Complexity(pDC->dclevel.prgnMeta);
-  }
-  else
-  {
-     if ( pDC->dclevel.prgnClip )
-     {
-        Ret = REGION_Complexity(pDC->dclevel.prgnClip);
-        pDC->dclevel.prgnMeta = pDC->dclevel.prgnClip;
-        pDC->dclevel.prgnClip = NULL;
-     }
-     else
-       Ret = SIMPLEREGION;
-  }
-  return Ret;
+        else
+            Ret = REGION_Complexity(pDC->dclevel.prgnMeta);
+    }
+    else
+    {
+        if ( pDC->dclevel.prgnClip )
+        {
+            Ret = REGION_Complexity(pDC->dclevel.prgnClip);
+            pDC->dclevel.prgnMeta = pDC->dclevel.prgnClip;
+            pDC->dclevel.prgnClip = NULL;
+        }
+        else
+            Ret = SIMPLEREGION;
+    }
+
+    return Ret;
 }
 
 
@@ -533,88 +766,109 @@ int APIENTRY NtGdiSetMetaRgn(HDC  hDC)
   return Ret;
 }
 
-INT FASTCALL
-NEW_CLIPPING_UpdateGCRegion(PDC pDC)
+VOID
+FASTCALL
+CLIPPING_UpdateGCRegion(PDC pDC)
 {
-  CLIPOBJ * co;
+    // Moved from Release Rao. Though it still gets over written.
+    RECTL_vSetEmptyRect(&pDC->erclClip);
+
+    /* Must have VisRgn set to a valid state! */
+    ASSERT (pDC->prgnVis);
+#if 0 // (w2k3) This works with limitations. (w7u) ReactOS relies on Rao.
+    if ( !pDC->dclevel.prgnClip &&
+         !pDC->dclevel.prgnMeta &&
+         !pDC->prgnAPI)
+    {
+        if (pDC->prgnRao)
+            REGION_Delete(pDC->prgnRao);
+        pDC->prgnRao = NULL;
 
-  /* Must have VisRgn set to a valid state! */
-  ASSERT (pDC->prgnVis);
+        REGION_bOffsetRgn(pDC->prgnVis, pDC->ptlDCOrig.x, pDC->ptlDCOrig.y);
 
-// FIXME: this seems to be broken!
+        RtlCopyMemory(&pDC->erclClip,
+                      &pDC->prgnVis->rdh.rcBound,
+                       sizeof(RECTL));
 
-  if (pDC->prgnAPI)
-  {
-     REGION_Delete(pDC->prgnAPI);
-     pDC->prgnAPI = IntSysCreateRectpRgn(0,0,0,0);
-  }
+        IntEngUpdateClipRegion(&pDC->co,
+                                pDC->prgnVis->rdh.nCount,
+                                pDC->prgnVis->Buffer,
+                               &pDC->erclClip);
 
-  if (pDC->prgnRao)
-  {
-     REGION_Delete(pDC->prgnRao);
-     pDC->prgnRao = IntSysCreateRectpRgn(0,0,0,0);
-  }
+        REGION_bOffsetRgn(pDC->prgnVis, -pDC->ptlDCOrig.x, -pDC->ptlDCOrig.y);
 
-  if (!pDC->prgnRao)
-  {
-     return ERROR;
-  }
+        pDC->fs &= ~DC_FLAG_DIRTY_RAO;
+        UpdateVisRgn(pDC);
+        return;
+    }
+#endif
+    if (pDC->prgnAPI)
+    {
+        REGION_Delete(pDC->prgnAPI);
+        pDC->prgnAPI = NULL;
+    }
 
-  if (pDC->dclevel.prgnMeta && pDC->dclevel.prgnClip)
-  {
-     IntGdiCombineRgn( pDC->prgnAPI,
-                       pDC->dclevel.prgnClip,
-                       pDC->dclevel.prgnMeta,
-                       RGN_AND);
-  }
-  else
-  {
-     if (pDC->dclevel.prgnClip)
-     {
-        IntGdiCombineRgn( pDC->prgnAPI,
-                          pDC->dclevel.prgnClip,
-                          NULL,
-                          RGN_COPY);
-     }
-     else if (pDC->dclevel.prgnMeta)
-     {
-        IntGdiCombineRgn( pDC->prgnAPI,
-                          pDC->dclevel.prgnMeta,
-                          NULL,
-                          RGN_COPY);
-     }
-  }
+    if (pDC->prgnRao)
+        REGION_Delete(pDC->prgnRao);
+
+    pDC->prgnRao = IntSysCreateRectpRgn(0,0,0,0);
+
+    ASSERT(pDC->prgnRao);
+
+    if (pDC->dclevel.prgnMeta || pDC->dclevel.prgnClip)
+    {
+        pDC->prgnAPI = IntSysCreateRectpRgn(0,0,0,0);
+        if (!pDC->dclevel.prgnMeta)
+        {
+            REGION_bCopy(pDC->prgnAPI,
+                         pDC->dclevel.prgnClip);
+        }
+        else if (!pDC->dclevel.prgnClip)
+        {
+            REGION_bCopy(pDC->prgnAPI,
+                         pDC->dclevel.prgnMeta);
+        }
+        else
+        {
+            REGION_bIntersectRegion(pDC->prgnAPI,
+                                    pDC->dclevel.prgnClip,
+                                    pDC->dclevel.prgnMeta);
+        }
+    }
 
-  IntGdiCombineRgn( pDC->prgnRao,
-                    pDC->prgnVis,
-                    pDC->prgnAPI,
-                    RGN_AND);
+    if (pDC->prgnAPI)
+    {
+        REGION_bIntersectRegion(pDC->prgnRao,
+                                pDC->prgnVis,
+                                pDC->prgnAPI);
+    }
+    else
+    {
+        REGION_bCopy(pDC->prgnRao,
+                     pDC->prgnVis);
+    }
 
-  RtlCopyMemory(&pDC->erclClip,
-                &pDC->prgnRao->rdh.rcBound,
-                sizeof(RECTL));
 
-  pDC->fs &= ~DC_FLAG_DIRTY_RAO;
+    REGION_bOffsetRgn(pDC->prgnRao, pDC->ptlDCOrig.x, pDC->ptlDCOrig.y);
 
-  IntGdiOffsetRgn(pDC->prgnRao, pDC->ptlDCOrig.x, pDC->ptlDCOrig.y);
+    RtlCopyMemory(&pDC->erclClip,
+                  &pDC->prgnRao->rdh.rcBound,
+                  sizeof(RECTL));
 
-  // pDC->co should be used. Example, CLIPOBJ_cEnumStart uses XCLIPOBJ to build
-  // the rects from region objects rects in pClipRgn->Buffer.
-  // With pDC->co.pClipRgn->Buffer,
-  // pDC->co.pClipRgn = pDC->prgnRao ? pDC->prgnRao : pDC->prgnVis;
+    pDC->fs &= ~DC_FLAG_DIRTY_RAO;
+    UpdateVisRgn(pDC);
 
-  co = IntEngCreateClipRegion(pDC->prgnRao->rdh.nCount,
-                              pDC->prgnRao->Buffer,
-                                 &pDC->erclClip);
-  if (co)
-  {
-    if (pDC->rosdc.CombinedClip != NULL)
-      IntEngDeleteClipRegion(pDC->rosdc.CombinedClip);
+    // pDC->co should be used. Example, CLIPOBJ_cEnumStart uses XCLIPOBJ to build
+    // the rects from region objects rects in pClipRgn->Buffer.
+    // With pDC->co.pClipRgn->Buffer,
+    // pDC->co.pClipRgn = pDC->prgnRao ? pDC->prgnRao : pDC->prgnVis;
 
-    pDC->rosdc.CombinedClip = co;
-  }
+    IntEngUpdateClipRegion(&pDC->co,
+                           pDC->prgnRao->rdh.nCount,
+                           pDC->prgnRao->Buffer,
+                           &pDC->erclClip);
 
-  return IntGdiOffsetRgn(pDC->prgnRao, -pDC->ptlDCOrig.x, -pDC->ptlDCOrig.y);
+    REGION_bOffsetRgn(pDC->prgnRao, -pDC->ptlDCOrig.x, -pDC->ptlDCOrig.y);
 }
 
 /* EOF */