gdiplus: Store the gdi32 clip region at Graphics creation time.

Signed-off-by: Vincent Povirk <vincent@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
diff --git a/dlls/gdiplus/gdiplus_private.h b/dlls/gdiplus/gdiplus_private.h
index 33f6ceb..566092e 100644
--- a/dlls/gdiplus/gdiplus_private.h
+++ b/dlls/gdiplus/gdiplus_private.h
@@ -262,6 +262,7 @@
     INT origin_x, origin_y;
     INT gdi_transform_acquire_count, gdi_transform_save;
     GpMatrix gdi_transform;
+    HRGN gdi_clip;
     /* For giving the caller an HDC when we technically can't: */
     HBITMAP temp_hbitmap;
     int temp_hbitmap_width;
diff --git a/dlls/gdiplus/graphics.c b/dlls/gdiplus/graphics.c
index 66a3f76..0207385 100644
--- a/dlls/gdiplus/graphics.c
+++ b/dlls/gdiplus/graphics.c
@@ -348,6 +348,17 @@
         GdipDeleteRegion(rgn);
     }
 
+    if (stat == Ok && graphics->gdi_clip)
+    {
+        if (*hrgn)
+            CombineRgn(*hrgn, *hrgn, graphics->gdi_clip, RGN_AND);
+        else
+        {
+            *hrgn = CreateRectRgn(0,0,0,0);
+            CombineRgn(*hrgn, graphics->gdi_clip, graphics->gdi_clip, RGN_COPY);
+        }
+    }
+
     return stat;
 }
 
@@ -498,8 +509,7 @@
 
         save = SaveDC(graphics->hdc);
 
-        if (hrgn)
-            ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
+        ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
 
         if (hregion)
             ExtSelectClipRgn(graphics->hdc, hregion, RGN_AND);
@@ -2315,6 +2325,13 @@
     (*graphics)->contid = 0;
     get_gdi_transform(*graphics, &(*graphics)->gdi_transform);
 
+    (*graphics)->gdi_clip = CreateRectRgn(0,0,0,0);
+    if (!GetClipRgn(hdc, (*graphics)->gdi_clip))
+    {
+        DeleteObject((*graphics)->gdi_clip);
+        (*graphics)->gdi_clip = NULL;
+    }
+
     TRACE("<-- %p\n", *graphics);
 
     return Ok;
@@ -2439,6 +2456,8 @@
 
     GdipDeleteRegion(graphics->clip);
 
+    DeleteObject(graphics->gdi_clip);
+
     /* Native returns ObjectBusy on the second free, instead of crashing as we'd
      * do otherwise, but we can't have that in the test suite because it means
      * accessing freed memory. */
@@ -3223,9 +3242,9 @@
 
             stat = get_clip_hrgn(graphics, &hrgn);
 
-            if (stat == Ok && hrgn)
+            if (stat == Ok)
             {
-                ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
+                ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
                 DeleteObject(hrgn);
             }
 
@@ -3466,8 +3485,7 @@
     if (retval != Ok)
         goto end;
 
-    if (hrgn)
-        ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
+    ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
 
     gdi_transform_acquire(graphics);
 
@@ -4155,8 +4173,7 @@
     if (retval != Ok)
         goto end;
 
-    if (hrgn)
-        ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
+    ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
 
     gdi_transform_acquire(graphics);
 
@@ -4443,32 +4460,30 @@
     if(!graphics->hdc || !brush_can_fill_path(brush, TRUE))
         return NotImplemented;
 
-    status = GdipGetRegionHRgn(region, graphics, &hrgn);
-    if(status != Ok)
-        return status;
-
     save_state = SaveDC(graphics->hdc);
     EndPath(graphics->hdc);
 
-    ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
-
-    DeleteObject(hrgn);
-
     hrgn = NULL;
     status = get_clip_hrgn(graphics, &hrgn);
-
     if (status != Ok)
     {
         RestoreDC(graphics->hdc, save_state);
         return status;
     }
 
-    if (hrgn)
+    ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
+    DeleteObject(hrgn);
+
+    status = GdipGetRegionHRgn(region, graphics, &hrgn);
+    if (status != Ok)
     {
-        ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
-        DeleteObject(hrgn);
+        RestoreDC(graphics->hdc, save_state);
+        return status;
     }
 
+    ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
+    DeleteObject(hrgn);
+
     if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
     {
         BeginPath(graphics->hdc);
@@ -7001,9 +7016,9 @@
 
     status = get_clip_hrgn(graphics, &hrgn);
 
-    if (status == Ok && hrgn)
+    if (status == Ok)
     {
-        ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
+        ExtSelectClipRgn(graphics->hdc, hrgn, RGN_COPY);
         DeleteObject(hrgn);
     }