[CMAKE]
[reactos.git] / subsystems / win32 / win32k / objects / bitblt.c
index f685b11..2744493 100644 (file)
 #define NDEBUG
 #include <debug.h>
 
+#define ROP_USES_SOURCE(Rop)  (((((Rop) & 0xCC0000) >> 2) != ((Rop) & 0x330000)) || ((((Rop) & 0xCC000000) >> 2) != ((Rop) & 0x33000000)))
+#define ROP_USES_MASK(Rop)    (((Rop) & 0xFF000000) != (((Rop) & 0xff0000) << 8))
 
+#define FIXUP_ROP(Rop) if(((Rop) & 0xFF000000) == 0) Rop = MAKEROP4((Rop), (Rop))
+#define ROP_TO_ROP4(Rop) ((Rop) >> 16)
 
 BOOL APIENTRY
 NtGdiAlphaBlend(
@@ -42,6 +46,8 @@ NtGdiAlphaBlend(
 {
     PDC DCDest;
     PDC DCSrc;
+    HDC ahDC[2];
+    PGDIOBJ apObj[2];
     SURFACE *BitmapDest, *BitmapSrc;
     RECTL DestRect, SourceRect;
     BOOL bResult;
@@ -51,49 +57,30 @@ NtGdiAlphaBlend(
 
     if (WidthDest < 0 || HeightDest < 0 || WidthSrc < 0 || HeightSrc < 0)
     {
-        SetLastWin32Error(ERROR_INVALID_PARAMETER);
+        EngSetLastError(ERROR_INVALID_PARAMETER);
         return FALSE;
     }
 
-    DCDest = DC_LockDc(hDCDest);
-    if (NULL == DCDest)
+    DPRINT("Locking DCs\n");
+    ahDC[0] = hDCDest;
+    ahDC[1] = hDCSrc ;
+    if (!GDIOBJ_bLockMultipleObjects(2, ahDC, apObj, GDIObjType_DC_TYPE))
     {
-        DPRINT1("Invalid destination dc handle (0x%08x) passed to NtGdiAlphaBlend\n", hDCDest);
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
+        DPRINT1("Invalid dc handle (dest=0x%08x, src=0x%08x) passed to NtGdiAlphaBlend\n", hDCDest, hDCSrc);
+        EngSetLastError(ERROR_INVALID_HANDLE);
         return FALSE;
     }
+    DCDest = apObj[0];
+    DCSrc = apObj[1];
 
-    if (DCDest->dctype == DC_TYPE_INFO)
+    if (DCDest->dctype == DC_TYPE_INFO || DCDest->dctype == DCTYPE_INFO)
     {
-        DC_UnlockDc(DCDest);
+        GDIOBJ_vUnlockObject(&DCSrc->BaseObject);
+        GDIOBJ_vUnlockObject(&DCDest->BaseObject);
         /* Yes, Windows really returns TRUE in this case */
         return TRUE;
     }
 
-    if (hDCSrc != hDCDest)
-    {
-        DCSrc = DC_LockDc(hDCSrc);
-        if (NULL == DCSrc)
-        {
-            DC_UnlockDc(DCDest);
-            DPRINT1("Invalid source dc handle (0x%08x) passed to NtGdiAlphaBlend\n", hDCSrc);
-            SetLastWin32Error(ERROR_INVALID_HANDLE);
-            return FALSE;
-        }
-
-        if (DCSrc->dctype == DC_TYPE_INFO)
-        {
-            DC_UnlockDc(DCSrc);
-            DC_UnlockDc(DCDest);
-            /* Yes, Windows really returns TRUE in this case */
-            return TRUE;
-        }
-    }
-    else
-    {
-        DCSrc = DCDest;
-    }
-
     DestRect.left   = XOriginDest;
     DestRect.top    = YOriginDest;
     DestRect.right  = XOriginDest + WidthDest;
@@ -121,35 +108,35 @@ NtGdiAlphaBlend(
         !SourceRect.right ||
         !SourceRect.bottom)
     {
-        if (hDCSrc != hDCDest)
-            DC_UnlockDc(DCSrc);
-        DC_UnlockDc(DCDest);
+        GDIOBJ_vUnlockObject(&DCSrc->BaseObject);
+        GDIOBJ_vUnlockObject(&DCDest->BaseObject);
         return TRUE;
     }
 
+    /* Prepare DCs for blit */
+    DPRINT("Preparing DCs for blit\n");
+    DC_vPrepareDCsForBlit(DCDest, DestRect, DCSrc, SourceRect);
+
     /* Determine surfaces to be used in the bitblt */
     BitmapDest = DCDest->dclevel.pSurface;
     if (!BitmapDest)
     {
-        if (hDCSrc != hDCDest)
-            DC_UnlockDc(DCSrc);
-        DC_UnlockDc(DCDest);
-        return FALSE;
+        bResult = FALSE ;
+        goto leave ;
     }
 
     BitmapSrc = DCSrc->dclevel.pSurface;
     if (!BitmapSrc)
     {
-        if (hDCSrc != hDCDest)
-            DC_UnlockDc(DCSrc);
-        DC_UnlockDc(DCDest);
-        return FALSE;
+        bResult = FALSE;
+        goto leave;
     }
 
     /* Create the XLATEOBJ. */
     EXLATEOBJ_vInitXlateFromDCs(&exlo, DCSrc, DCDest);
 
     /* Perform the alpha blend operation */
+    DPRINT("Performing the alpha Blend\n");
     bResult = IntEngAlphaBlend(&BitmapDest->SurfObj,
                                &BitmapSrc->SurfObj,
                                DCDest->rosdc.CombinedClip,
@@ -159,9 +146,11 @@ NtGdiAlphaBlend(
                                &BlendObj);
 
     EXLATEOBJ_vCleanup(&exlo);
-    DC_UnlockDc(DCDest);
-    if (hDCSrc != hDCDest)
-        DC_UnlockDc(DCSrc);
+leave :
+    DPRINT("Finishing blit\n");
+    DC_vFinishBlit(DCDest, DCSrc);
+    GDIOBJ_vUnlockObject(&DCSrc->BaseObject);
+    GDIOBJ_vUnlockObject(&DCDest->BaseObject);
 
     return bResult;
 }
@@ -180,127 +169,21 @@ NtGdiBitBlt(
     IN DWORD crBackColor,
     IN FLONG fl)
 {
-    PDC DCDest;
-    PDC DCSrc = NULL;
-    PDC_ATTR pdcattr = NULL;
-    SURFACE *BitmapDest, *BitmapSrc = NULL;
-    RECTL DestRect;
-    POINTL SourcePoint;
-    BOOL Status = FALSE;
-    EXLATEOBJ exlo;
-    XLATEOBJ *XlateObj = NULL;
-    BOOL UsesSource = ROP3_USES_SOURCE(ROP);
-
-    DCDest = DC_LockDc(hDCDest);
-    if (NULL == DCDest)
-    {
-        DPRINT("Invalid destination dc handle (0x%08x) passed to NtGdiBitBlt\n", hDCDest);
-        return FALSE;
-    }
-
-    if (DCDest->dctype == DC_TYPE_INFO)
-    {
-        DC_UnlockDc(DCDest);
-        /* Yes, Windows really returns TRUE in this case */
-        return TRUE;
-    }
-
-    if (UsesSource)
-    {
-        if (hDCSrc != hDCDest)
-        {
-            DCSrc = DC_LockDc(hDCSrc);
-            if (NULL == DCSrc)
-            {
-                DC_UnlockDc(DCDest);
-                DPRINT("Invalid source dc handle (0x%08x) passed to NtGdiBitBlt\n", hDCSrc);
-                return FALSE;
-            }
-            if (DCSrc->dctype == DC_TYPE_INFO)
-            {
-                DC_UnlockDc(DCSrc);
-                DC_UnlockDc(DCDest);
-                /* Yes, Windows really returns TRUE in this case */
-                return TRUE;
-            }
-        }
-        else
-        {
-            DCSrc = DCDest;
-        }
-    }
-
-    pdcattr = DCDest->pdcattr;
-
-    if (pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
-        DC_vUpdateFillBrush(DCDest);
-
-    DestRect.left   = XDest;
-    DestRect.top    = YDest;
-    DestRect.right  = XDest+Width;
-    DestRect.bottom = YDest+Height;
-    IntLPtoDP(DCDest, (LPPOINT)&DestRect, 2);
-
-    DestRect.left   += DCDest->ptlDCOrig.x;
-    DestRect.top    += DCDest->ptlDCOrig.y;
-    DestRect.right  += DCDest->ptlDCOrig.x;
-    DestRect.bottom += DCDest->ptlDCOrig.y;
-
-    SourcePoint.x = XSrc;
-    SourcePoint.y = YSrc;
-
-    if (UsesSource)
-    {
-        IntLPtoDP(DCSrc, (LPPOINT)&SourcePoint, 1);
-
-        SourcePoint.x += DCSrc->ptlDCOrig.x;
-        SourcePoint.y += DCSrc->ptlDCOrig.y;
-    }
-
-    /* Determine surfaces to be used in the bitblt */
-    BitmapDest = DCDest->dclevel.pSurface;
-    if (!BitmapDest)
-        goto cleanup;
-
-    if (UsesSource)
-    {
-        {
-            BitmapSrc = DCSrc->dclevel.pSurface;
-            if (!BitmapSrc)
-                goto cleanup;
-        }
-    }
-
-    /* Create the XLATEOBJ. */
-    if (UsesSource)
-    {
-        EXLATEOBJ_vInitXlateFromDCs(&exlo, DCSrc, DCDest);
-        XlateObj = &exlo.xlo;
-    }
-
-    /* Perform the bitblt operation */
-    Status = IntEngBitBlt(&BitmapDest->SurfObj,
-                          BitmapSrc ? &BitmapSrc->SurfObj : NULL,
-                          NULL,
-                          DCDest->rosdc.CombinedClip,
-                          XlateObj,
-                          &DestRect,
-                          &SourcePoint,
-                          NULL,
-                          &DCDest->eboFill.BrushObject,
-                          &DCDest->dclevel.pbrFill->ptOrigin,
-                          ROP3_TO_ROP4(ROP));
-
-       if (UsesSource)
-        EXLATEOBJ_vCleanup(&exlo);
-cleanup:
-    if (UsesSource && hDCSrc != hDCDest)
-    {
-        DC_UnlockDc(DCSrc);
-    }
-    DC_UnlockDc(DCDest);
-
-    return Status;
+    /* Forward to NtGdiMaskBlt */
+    // TODO : what's fl for?
+    return NtGdiMaskBlt(hDCDest,
+                        XDest,
+                        YDest,
+                        Width,
+                        Height,
+                        hDCSrc,
+                        XSrc,
+                        YSrc,
+                        NULL,
+                        0,
+                        0,
+                        ROP,
+                        crBackColor);
 }
 
 BOOL APIENTRY
@@ -318,97 +201,34 @@ NtGdiTransparentBlt(
     COLORREF TransColor)
 {
     PDC DCDest, DCSrc;
+    HDC ahDC[2];
+    PGDIOBJ apObj[2];
     RECTL rcDest, rcSrc;
     SURFACE *BitmapDest, *BitmapSrc = NULL;
-    HPALETTE SourcePalette = 0, DestPalette = 0;
-    PPALETTE PalDestGDI, PalSourceGDI;
     ULONG TransparentColor = 0;
     BOOL Ret = FALSE;
     EXLATEOBJ exlo;
 
-    if(!(DCDest = DC_LockDc(hdcDst)))
-    {
-        DPRINT1("Invalid destination dc handle (0x%08x) passed to NtGdiTransparentBlt\n", hdcDst);
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
-        return FALSE;
-    }
-    if (DCDest->dctype == DC_TYPE_INFO)
-    {
-        DC_UnlockDc(DCDest);
-        /* Yes, Windows really returns TRUE in this case */
-        return TRUE;
-    }
-
-    if((hdcDst != hdcSrc) && !(DCSrc = DC_LockDc(hdcSrc)))
+    DPRINT("Locking DCs\n");
+    ahDC[0] = hdcDst;
+    ahDC[1] = hdcSrc ;
+    if (!GDIOBJ_bLockMultipleObjects(2, ahDC, apObj, GDIObjType_DC_TYPE))
     {
-        DC_UnlockDc(DCDest);
-        DPRINT1("Invalid source dc handle (0x%08x) passed to NtGdiTransparentBlt\n", hdcSrc);
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
+        DPRINT1("Invalid dc handle (dest=0x%08x, src=0x%08x) passed to NtGdiAlphaBlend\n", hdcDst, hdcSrc);
+        EngSetLastError(ERROR_INVALID_HANDLE);
         return FALSE;
     }
+    DCDest = apObj[0];
+    DCSrc = apObj[1];
 
-    if(hdcDst == hdcSrc)
-    {
-        DCSrc = DCDest;
-    }
-
-    if (DCSrc->dctype == DC_TYPE_INFO)
+    if (DCDest->dctype == DC_TYPE_INFO || DCDest->dctype == DCTYPE_INFO)
     {
-        DC_UnlockDc(DCSrc);
-        if(hdcDst != hdcSrc)
-        {
-            DC_UnlockDc(DCDest);
-        }
+        GDIOBJ_vUnlockObject(&DCSrc->BaseObject);
+        GDIOBJ_vUnlockObject(&DCDest->BaseObject);
         /* Yes, Windows really returns TRUE in this case */
         return TRUE;
     }
 
-    BitmapDest = DCDest->dclevel.pSurface;
-    if (!BitmapDest)
-    {
-        goto done;
-    }
-
-    BitmapSrc = DCSrc->dclevel.pSurface;
-    if (!BitmapSrc)
-    {
-        goto done;
-    }
-
-    DestPalette = BitmapDest->hDIBPalette;
-    if (!DestPalette) DestPalette = pPrimarySurface->devinfo.hpalDefault;
-
-    SourcePalette = BitmapSrc->hDIBPalette;
-    if (!SourcePalette) SourcePalette = pPrimarySurface->devinfo.hpalDefault;
-
-    if(!(PalSourceGDI = PALETTE_LockPalette(SourcePalette)))
-    {
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
-        goto done;
-    }
-    PALETTE_UnlockPalette(PalSourceGDI);
-
-    if(DestPalette != SourcePalette)
-    {
-        if (!(PalDestGDI = PALETTE_LockPalette(DestPalette)))
-        {
-            SetLastWin32Error(ERROR_INVALID_HANDLE);
-            goto done;
-        }
-        PALETTE_UnlockPalette(PalDestGDI);
-    }
-    else
-    {
-        PalDestGDI = PalSourceGDI;
-    }
-
-    /* Translate Transparent (RGB) Color to the source palette */
-    EXLATEOBJ_vInitialize(&exlo, &gpalRGB, PalSourceGDI, 0, 0, 0);
-    TransparentColor = XLATEOBJ_iXlate(&exlo.xlo, (ULONG)TransColor);
-    EXLATEOBJ_vCleanup(&exlo);
-
-    EXLATEOBJ_vInitialize(&exlo, PalSourceGDI, PalDestGDI, 0, 0, 0);
-
     rcDest.left   = xDst;
     rcDest.top    = yDst;
     rcDest.right  = rcDest.left + cxDst;
@@ -431,100 +251,42 @@ NtGdiTransparentBlt(
     rcSrc.right  += DCSrc->ptlDCOrig.x;
     rcSrc.bottom += DCSrc->ptlDCOrig.y;
 
+    /* Prepare for blit */
+    DC_vPrepareDCsForBlit(DCDest, rcDest, DCSrc, rcSrc);
+
+    BitmapDest = DCDest->dclevel.pSurface;
+    if (!BitmapDest)
+    {
+        goto done;
+    }
+
+    BitmapSrc = DCSrc->dclevel.pSurface;
+    if (!BitmapSrc)
+    {
+        goto done;
+    }
+
+    /* Translate Transparent (RGB) Color to the source palette */
+    EXLATEOBJ_vInitialize(&exlo, &gpalRGB, BitmapSrc->ppal, 0, 0, 0);
+    TransparentColor = XLATEOBJ_iXlate(&exlo.xlo, (ULONG)TransColor);
+    EXLATEOBJ_vCleanup(&exlo);
+
+    EXLATEOBJ_vInitXlateFromDCs(&exlo, DCSrc, DCDest);
+
     Ret = IntEngTransparentBlt(&BitmapDest->SurfObj, &BitmapSrc->SurfObj,
         DCDest->rosdc.CombinedClip, &exlo.xlo, &rcDest, &rcSrc,
         TransparentColor, 0);
 
-done:
-    DC_UnlockDc(DCSrc);
-    if(hdcDst != hdcSrc)
-    {
-        DC_UnlockDc(DCDest);
-    }
     EXLATEOBJ_vCleanup(&exlo);
-    return Ret;
-}
 
-/***********************************************************************
-* MaskBlt
-* Ported from WINE by sedwards 11-4-03
-*
-* Someone thought it would be faster to do it here and then switch back
-* to GDI32. I dunno. Write a test and let me know.
-* A. It should be in here!
-*/
+done:
+    DC_vFinishBlit(DCDest, DCSrc);
+    GDIOBJ_vUnlockObject(&DCDest->BaseObject);
+    GDIOBJ_vUnlockObject(&DCSrc->BaseObject);
 
-static const DWORD ROP3Table[256] =
-{
-  0x000042, 0x010289, 0x020C89, 0x0300AA, 0x040C88, 0x0500A9, 0x060865, 0x0702C5,
-  0x080F08, 0x090245, 0x0A0329, 0x0B0B2A, 0x0C0324, 0x0D0B25, 0x0E08A5, 0x0F0001,
-  0x100C85, 0x1100A6, 0x120868, 0x1302C8, 0x140869, 0x1502C9, 0x165CCA, 0x171D54,
-  0x180D59, 0x191CC8, 0x1A06C5, 0x1B0768, 0x1C06CA, 0x1D0766, 0x1E01A5, 0x1F0385,
-  0x200F09, 0x210248, 0x220326, 0x230B24, 0x240D55, 0x251CC5, 0x2606C8, 0x271868,
-  0x280369, 0x2916CA, 0x2A0CC9, 0x2B1D58, 0x2C0784, 0x2D060A, 0x2E064A, 0x2F0E2A,
-  0x30032A, 0x310B28, 0x320688, 0x330008, 0x3406C4, 0x351864, 0x3601A8, 0x370388,
-  0x38078A, 0x390604, 0x3A0644, 0x3B0E24, 0x3C004A, 0x3D18A4, 0x3E1B24, 0x3F00EA,
-  0x400F0A, 0x410249, 0x420D5D, 0x431CC4, 0x440328, 0x450B29, 0x4606C6, 0x47076A,
-  0x480368, 0x4916C5, 0x4A0789, 0x4B0605, 0x4C0CC8, 0x4D1954, 0x4E0645, 0x4F0E25,
-  0x500325, 0x510B26, 0x5206C9, 0x530764, 0x5408A9, 0x550009, 0x5601A9, 0x570389,
-  0x580785, 0x590609, 0x5A0049, 0x5B18A9, 0x5C0649, 0x5D0E29, 0x5E1B29, 0x5F00E9,
-  0x600365, 0x6116C6, 0x620786, 0x630608, 0x640788, 0x650606, 0x660046, 0x6718A8,
-  0x6858A6, 0x690145, 0x6A01E9, 0x6B178A, 0x6C01E8, 0x6D1785, 0x6E1E28, 0x6F0C65,
-  0x700CC5, 0x711D5C, 0x720648, 0x730E28, 0x740646, 0x750E26, 0x761B28, 0x7700E6,
-  0x7801E5, 0x791786, 0x7A1E29, 0x7B0C68, 0x7C1E24, 0x7D0C69, 0x7E0955, 0x7F03C9,
-  0x8003E9, 0x810975, 0x820C49, 0x831E04, 0x840C48, 0x851E05, 0x8617A6, 0x8701C5,
-  0x8800C6, 0x891B08, 0x8A0E06, 0x8B0666, 0x8C0E08, 0x8D0668, 0x8E1D7C, 0x8F0CE5,
-  0x900C45, 0x911E08, 0x9217A9, 0x9301C4, 0x9417AA, 0x9501C9, 0x960169, 0x97588A,
-  0x981888, 0x990066, 0x9A0709, 0x9B07A8, 0x9C0704, 0x9D07A6, 0x9E16E6, 0x9F0345,
-  0xA000C9, 0xA11B05, 0xA20E09, 0xA30669, 0xA41885, 0xA50065, 0xA60706, 0xA707A5,
-  0xA803A9, 0xA90189, 0xAA0029, 0xAB0889, 0xAC0744, 0xAD06E9, 0xAE0B06, 0xAF0229,
-  0xB00E05, 0xB10665, 0xB21974, 0xB30CE8, 0xB4070A, 0xB507A9, 0xB616E9, 0xB70348,
-  0xB8074A, 0xB906E6, 0xBA0B09, 0xBB0226, 0xBC1CE4, 0xBD0D7D, 0xBE0269, 0xBF08C9,
-  0xC000CA, 0xC11B04, 0xC21884, 0xC3006A, 0xC40E04, 0xC50664, 0xC60708, 0xC707AA,
-  0xC803A8, 0xC90184, 0xCA0749, 0xCB06E4, 0xCC0020, 0xCD0888, 0xCE0B08, 0xCF0224,
-  0xD00E0A, 0xD1066A, 0xD20705, 0xD307A4, 0xD41D78, 0xD50CE9, 0xD616EA, 0xD70349,
-  0xD80745, 0xD906E8, 0xDA1CE9, 0xDB0D75, 0xDC0B04, 0xDD0228, 0xDE0268, 0xDF08C8,
-  0xE003A5, 0xE10185, 0xE20746, 0xE306EA, 0xE40748, 0xE506E5, 0xE61CE8, 0xE70D79,
-  0xE81D74, 0xE95CE6, 0xEA02E9, 0xEB0849, 0xEC02E8, 0xED0848, 0xEE0086, 0xEF0A08,
-  0xF00021, 0xF10885, 0xF20B05, 0xF3022A, 0xF40B0A, 0xF50225, 0xF60265, 0xF708C5,
-  0xF802E5, 0xF90845, 0xFA0089, 0xFB0A09, 0xFC008A, 0xFD0A0A, 0xFE02A9, 0xFF0062,
-};
-
-static __inline BYTE
-SwapROP3_SrcDst(BYTE bRop3)
-{
-    return (bRop3 & 0x99) | ((bRop3 & 0x22) << 1) | ((bRop3 & 0x44) >> 1);
+    return Ret;
 }
 
-#define FRGND_ROP3(ROP4)    ((ROP4) & 0x00FFFFFF)
-#define BKGND_ROP3(ROP4)    (ROP3Table[(SwapROP3_SrcDst((ROP4)>>24)) & 0xFF])
-#define DSTCOPY    0x00AA0029
-#define DSTERASE    0x00220326 /* dest = dest & (~src) : DSna */
-
-/* NOTE: An alternative algorithm could use a pattern brush, created from
- * the mask bitmap and then use raster operation 0xCA to combine the fore
- * and background bitmaps. In this case erasing the bits beforehand would be
- * unneccessary. On the other hand the Operation does not provide an optimized
- * version in the DIB code, while SRCAND and SRCPAINT do.
- * A fully correct implementation would call Eng/DrvBitBlt, but our
- * EngBitBlt completely ignores the mask surface.
- *
- * Msk Fg Bk => x
- *  P  S  D        DPSDxax
- * ------------------------------------------
- *  0  0  0     0  0000xax = 000ax = 00x = 0
- *  0  0  1     1  1001xax = 101ax = 10x = 1
- *  0  1  0     0  0010xax = 001ax = 00x = 0
- *  0  1  1     1  1011xax = 100ax = 10x = 1
- *  1  0  0     0  0100xax = 010ax = 00x = 0
- *  1  0  1     0  1101xax = 111ax = 11x = 0
- *  1  1  0     1  0110xax = 011ax = 01x = 1
- *  1  1  1     1  1111xax = 110ax = 10x = 1
- * 
- * Operation index = 11001010 = 0xCA = PSaDPnao = DPSDxax 
- *                                     ^ no, this is not random letters, its reverse Polish notation
- */
-
 BOOL APIENTRY
 NtGdiMaskBlt(
     HDC hdcDest,
@@ -541,97 +303,188 @@ NtGdiMaskBlt(
     DWORD dwRop,
     IN DWORD crBackColor)
 {
-    HBITMAP hbmFore, hbmBack;
-    HDC hdcMask, hdcFore, hdcBack;
-    PDC pdc;
-    HBRUSH hbr;
-    COLORREF crFore, crBack;
-
-    if (!hbmMask)
-        return NtGdiBitBlt(hdcDest,
-                           nXDest,
-                           nYDest,
-                           nWidth,
-                           nHeight,
-                           hdcSrc,
-                           nXSrc,
-                           nYSrc,
-                           FRGND_ROP3(dwRop),
-                           crBackColor,
-                           0);
-
-    /* Lock the dest DC */
-    pdc = DC_LockDc(hdcDest);
-    if (!pdc) return FALSE;
-
-    /* Get brush and colors from dest dc */
-    hbr = pdc->pdcattr->hbrush;
-    crFore = pdc->pdcattr->crForegroundClr;
-    crBack = pdc->pdcattr->crBackgroundClr;
-
-    /* Unlock the DC */
-    DC_UnlockDc(pdc);
+    PDC DCDest;
+    PDC DCSrc = NULL;
+    HDC ahDC[2];
+    PGDIOBJ apObj[2];
+    PDC_ATTR pdcattr = NULL;
+    SURFACE *BitmapDest, *BitmapSrc = NULL, *psurfMask = NULL;
+    RECTL DestRect, SourceRect;
+    POINTL SourcePoint, MaskPoint;
+    BOOL Status = FALSE;
+    EXLATEOBJ exlo;
+    XLATEOBJ *XlateObj = NULL;
+    BOOL UsesSource = ROP_USES_SOURCE(dwRop);
+    BOOL UsesMask;
 
-    /* 1. Create mask bitmap's dc */
-    hdcMask = NtGdiCreateCompatibleDC(hdcDest);
-    NtGdiSelectBitmap(hdcMask, hbmMask);
+    FIXUP_ROP(dwRop);
 
-    /* 2. Create masked Background bitmap */
+    UsesMask = ROP_USES_MASK(dwRop);
 
-    /* 2.1 Create bitmap */
-    hdcBack = NtGdiCreateCompatibleDC(hdcDest);
-    hbmBack = NtGdiCreateCompatibleBitmap(hdcDest, nWidth, nHeight);
-    NtGdiSelectBitmap(hdcBack, hbmBack);
+    //DPRINT1("dwRop : 0x%08x\n", dwRop);
+    if (!hdcDest || (UsesSource && !hdcSrc))
+    {
+        EngSetLastError(ERROR_INVALID_PARAMETER);
+        return FALSE;
+    }
 
-    /* 2.2 Copy source bitmap */
-    NtGdiSelectBrush(hdcBack, hbr);
-    IntGdiSetBkColor(hdcBack, crBack);
-    IntGdiSetTextColor(hdcBack, crFore);
-    NtGdiBitBlt(hdcBack, 0, 0, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, SRCCOPY, 0, 0);
+    /* Take care of mask bitmap */
+    if(hbmMask)
+    {
+        psurfMask = SURFACE_ShareLockSurface(hbmMask);
+        if(!psurfMask)
+        {
+            EngSetLastError(ERROR_INVALID_HANDLE);
+            return FALSE;
+        }
+    }
 
-    /* 2.3 Do the background rop */
-    NtGdiBitBlt(hdcBack, 0, 0, nWidth, nHeight, hdcDest, nXDest, nYDest, BKGND_ROP3(dwRop), 0, 0);
+    if(UsesMask)
+    {
+        if(!psurfMask)
+        {
+            EngSetLastError(ERROR_INVALID_PARAMETER);
+            return FALSE;
+        }
+        if(gajBitsPerFormat[psurfMask->SurfObj.iBitmapFormat] != 1)
+        {
+            EngSetLastError(ERROR_INVALID_PARAMETER);
+            SURFACE_ShareUnlockSurface(psurfMask);
+            return FALSE;
+        }
+    }
+    else if(psurfMask)
+    {
+        DPRINT1("Getting Mask bitmap without needing it?\n");
+        SURFACE_ShareUnlockSurface(psurfMask);
+        psurfMask = NULL;
+    }
+    MaskPoint.x = xMask;
+    MaskPoint.y = yMask;
 
-    /* 2.4 Erase the foreground pixels */
-    IntGdiSetBkColor(hdcBack, 0xffffffff);
-    IntGdiSetTextColor(hdcBack, 0);
-    NtGdiBitBlt(hdcBack, 0, 0, nWidth, nHeight, hdcMask, xMask, yMask, SRCAND, 0, 0);
+    /* Take care of source and destination bitmap */
+    DPRINT("Locking DCs\n");
+    ahDC[0] = hdcDest;
+    ahDC[1] = UsesSource ? hdcSrc : NULL;
+    if (!GDIOBJ_bLockMultipleObjects(2, ahDC, apObj, GDIObjType_DC_TYPE))
+    {
+        DPRINT1("Invalid dc handle (dest=0x%08x, src=0x%08x) passed to NtGdiAlphaBlend\n", hdcDest, hdcSrc);
+        EngSetLastError(ERROR_INVALID_HANDLE);
+        return FALSE;
+    }
+    DCDest = apObj[0];
+    DCSrc = apObj[1];
 
-    /* 3. Create masked Foreground bitmap */
+    ASSERT(DCDest);
+    if (NULL == DCDest)
+    {
+        if(DCSrc) DC_UnlockDc(DCSrc);
+        DPRINT("Invalid destination dc handle (0x%08x) passed to NtGdiBitBlt\n", hdcDest);
+        return FALSE;
+    }
 
-    /* 3.1 Create bitmap */
-    hdcFore = NtGdiCreateCompatibleDC(hdcDest);
-    hbmFore = NtGdiCreateCompatibleBitmap(hdcDest, nWidth, nHeight);
-    NtGdiSelectBitmap(hdcFore, hbmFore);
+    if (DCDest->dctype == DC_TYPE_INFO)
+    {
+        if(DCSrc) DC_UnlockDc(DCSrc);
+        DC_UnlockDc(DCDest);
+        /* Yes, Windows really returns TRUE in this case */
+        return TRUE;
+    }
 
-    /* 3.2 Copy the dest bitmap */
-    NtGdiSelectBrush(hdcFore, hbr);
-    IntGdiSetBkColor(hdcFore, crBack);
-    IntGdiSetTextColor(hdcFore, crFore);
-    NtGdiBitBlt(hdcFore, 0, 0, nWidth, nHeight, hdcDest, nXDest, nYDest, SRCCOPY, 0, 0);
+    if (UsesSource)
+    {
+        ASSERT(DCSrc);
+        if (DCSrc->dctype == DC_TYPE_INFO)
+        {
+            DC_UnlockDc(DCDest);
+            DC_UnlockDc(DCSrc);
+            /* Yes, Windows really returns TRUE in this case */
+            return TRUE;
+        }
+    }
 
-    /* 2.3 Do the foreground rop */
-    NtGdiBitBlt(hdcFore, 0, 0, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, FRGND_ROP3(dwRop), 0,0);
+    pdcattr = DCDest->pdcattr;
 
-    /* 2.4 Erase the background pixels */
-    IntGdiSetBkColor(hdcFore, 0);
-    IntGdiSetTextColor(hdcFore, 0xffffffff);
-    NtGdiBitBlt(hdcFore, 0, 0, nWidth, nHeight, hdcMask, xMask, yMask, SRCAND, 0, 0);
+    DestRect.left   = nXDest;
+    DestRect.top    = nYDest;
+    DestRect.right  = nXDest + nWidth;
+    DestRect.bottom = nYDest + nHeight;
+    IntLPtoDP(DCDest, (LPPOINT)&DestRect, 2);
 
-    /* 3. Combine the fore and background into the background bitmap */
-    NtGdiBitBlt(hdcBack, 0, 0, nWidth, nHeight, hdcFore, 0, 0, SRCPAINT, 0, 0);
+    DestRect.left   += DCDest->ptlDCOrig.x;
+    DestRect.top    += DCDest->ptlDCOrig.y;
+    DestRect.right  += DCDest->ptlDCOrig.x;
+    DestRect.bottom += DCDest->ptlDCOrig.y;
 
-    /* 4. Copy the result to hdcDest */
-    NtGdiBitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, hdcBack, 0, 0, SRCCOPY, 0, 0);
+    SourcePoint.x = nXSrc;
+    SourcePoint.y = nYSrc;
 
-    /* 5. delete all temp objects */
-    NtGdiDeleteObjectApp(hdcBack);
-    NtGdiDeleteObjectApp(hdcFore);
-    NtGdiDeleteObjectApp(hdcMask);
-    GreDeleteObject(hbmFore);
-    GreDeleteObject(hbmBack);
+    if (UsesSource)
+    {
+        IntLPtoDP(DCSrc, (LPPOINT)&SourcePoint, 1);
 
-    return TRUE;
+        SourcePoint.x += DCSrc->ptlDCOrig.x;
+        SourcePoint.y += DCSrc->ptlDCOrig.y;
+        /* Calculate Source Rect */
+        SourceRect.left = SourcePoint.x;
+        SourceRect.top = SourcePoint.y;
+        SourceRect.right = SourcePoint.x + DestRect.right - DestRect.left;
+        SourceRect.bottom = SourcePoint.y + DestRect.bottom - DestRect.top ;
+    }
+
+    /* Prepare blit */
+    DC_vPrepareDCsForBlit(DCDest, DestRect, DCSrc, SourceRect);
+
+    if (pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
+        DC_vUpdateFillBrush(DCDest);
+
+    /* Determine surfaces to be used in the bitblt */
+    BitmapDest = DCDest->dclevel.pSurface;
+    if (!BitmapDest)
+        goto cleanup;
+
+    if (UsesSource)
+    {
+        {
+            BitmapSrc = DCSrc->dclevel.pSurface;
+            if (!BitmapSrc)
+                goto cleanup;
+        }
+    }
+
+    /* Create the XLATEOBJ. */
+    if (UsesSource)
+    {
+        EXLATEOBJ_vInitXlateFromDCs(&exlo, DCSrc, DCDest);
+        XlateObj = &exlo.xlo;
+    }
+
+
+    /* Perform the bitblt operation */
+    Status = IntEngBitBlt(&BitmapDest->SurfObj,
+                          BitmapSrc ? &BitmapSrc->SurfObj : NULL,
+                          psurfMask ? &psurfMask->SurfObj : NULL,
+                          DCDest->rosdc.CombinedClip,
+                          XlateObj,
+                          &DestRect,
+                          &SourcePoint,
+                          &MaskPoint,
+                          &DCDest->eboFill.BrushObject,
+                          &DCDest->dclevel.pbrFill->ptOrigin,
+                          ROP_TO_ROP4(dwRop));
+
+    if (UsesSource)
+        EXLATEOBJ_vCleanup(&exlo);
+cleanup:
+    DC_vFinishBlit(DCDest, DCSrc);
+    if (UsesSource)
+    {
+        DC_UnlockDc(DCSrc);
+    }
+    DC_UnlockDc(DCDest);
+    if(psurfMask) SURFACE_ShareUnlockSurface(psurfMask);
+
+    return Status;
 }
 
 BOOL
@@ -674,6 +527,8 @@ GreStretchBltMask(
     PDC DCDest;
     PDC DCSrc  = NULL;
     PDC DCMask = NULL;
+    HDC ahDC[3];
+    PGDIOBJ apObj[3];
     PDC_ATTR pdcattr;
     SURFACE *BitmapDest, *BitmapSrc = NULL;
     SURFACE *BitmapMask = NULL;
@@ -684,60 +539,61 @@ GreStretchBltMask(
     EXLATEOBJ exlo;
     XLATEOBJ *XlateObj = NULL;
     POINTL BrushOrigin;
-    BOOL UsesSource = ROP3_USES_SOURCE(ROP);
+    BOOL UsesSource;
+    BOOL UsesMask;
+
+    FIXUP_ROP(ROP);
+    UsesSource = ROP_USES_SOURCE(ROP);
+    UsesMask = ROP_USES_MASK(ROP);
 
     if (0 == WidthDest || 0 == HeightDest || 0 == WidthSrc || 0 == HeightSrc)
     {
-        SetLastWin32Error(ERROR_INVALID_PARAMETER);
+        EngSetLastError(ERROR_INVALID_PARAMETER);
         return FALSE;
     }
 
-    DCDest = DC_LockDc(hDCDest);
-    if (NULL == DCDest)
+    if (!hDCDest || (UsesSource && !hDCSrc) || (UsesMask && !hDCMask))
     {
-        DPRINT1("Invalid destination dc handle (0x%08x) passed to NtGdiStretchBlt\n", hDCDest);
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
+        EngSetLastError(ERROR_INVALID_PARAMETER);
         return FALSE;
     }
 
+    ahDC[0] = hDCDest;
+    ahDC[1] = UsesSource ? hDCSrc : NULL;
+    ahDC[2] = UsesMask ? hDCMask : NULL;
+    if (!GDIOBJ_bLockMultipleObjects(3, ahDC, apObj, GDIObjType_DC_TYPE))
+    {
+        DPRINT1("Invalid dc handle (dest=0x%08x, src=0x%08x) passed to NtGdiAlphaBlend\n", hDCDest, hDCSrc);
+        EngSetLastError(ERROR_INVALID_HANDLE);
+        return FALSE;
+    }
+    DCDest = apObj[0];
+    DCSrc = apObj[1];
+    DCMask = apObj[2];
+
     if (DCDest->dctype == DC_TYPE_INFO)
     {
-        DC_UnlockDc(DCDest);
+        if(DCSrc) GDIOBJ_vUnlockObject(&DCSrc->BaseObject);
+        if(DCMask) GDIOBJ_vUnlockObject(&DCMask->BaseObject);
+        GDIOBJ_vUnlockObject(&DCDest->BaseObject);
         /* Yes, Windows really returns TRUE in this case */
         return TRUE;
     }
 
     if (UsesSource)
     {
-        if (hDCSrc != hDCDest)
-        {
-            DCSrc = DC_LockDc(hDCSrc);
-            if (NULL == DCSrc)
-            {
-                DC_UnlockDc(DCDest);
-                DPRINT1("Invalid source dc handle (0x%08x) passed to NtGdiStretchBlt\n", hDCSrc);
-                SetLastWin32Error(ERROR_INVALID_HANDLE);
-                return FALSE;
-            }
-            if (DCSrc->dctype == DC_TYPE_INFO)
-            {
-                DC_UnlockDc(DCSrc);
-                DC_UnlockDc(DCDest);
-                /* Yes, Windows really returns TRUE in this case */
-                return TRUE;
-            }
-        }
-        else
+        if (DCSrc->dctype == DC_TYPE_INFO)
         {
-            DCSrc = DCDest;
+            GDIOBJ_vUnlockObject(&DCDest->BaseObject);
+            GDIOBJ_vUnlockObject(&DCSrc->BaseObject);
+            if(DCMask) GDIOBJ_vUnlockObject(&DCMask->BaseObject);
+            /* Yes, Windows really returns TRUE in this case */
+            return TRUE;
         }
     }
 
     pdcattr = DCDest->pdcattr;
 
-    if (pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
-        DC_vUpdateFillBrush(DCDest);
-
     DestRect.left   = XOriginDest;
     DestRect.top    = YOriginDest;
     DestRect.right  = XOriginDest+WidthDest;
@@ -767,6 +623,12 @@ GreStretchBltMask(
     BrushOrigin.x = 0;
     BrushOrigin.y = 0;
 
+    /* Only prepare Source and Dest, hdcMask represents a DIB */
+    DC_vPrepareDCsForBlit(DCDest, DestRect, DCSrc, SourceRect);
+
+    if (pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
+        DC_vUpdateFillBrush(DCDest);
+
     /* Determine surfaces to be used in the bitblt */
     BitmapDest = DCDest->dclevel.pSurface;
     if (BitmapDest == NULL)
@@ -787,28 +649,25 @@ GreStretchBltMask(
     BrushOrigin.y += DCDest->ptlDCOrig.y;
 
     /* Make mask surface for source surface */
-    if (BitmapSrc && hDCMask)
+    if (BitmapSrc && DCMask)
     {
-        DCMask = DC_LockDc(hDCMask);
-        if (DCMask)
+        BitmapMask = DCMask->dclevel.pSurface;
+        if (BitmapMask &&
+            (BitmapMask->SurfObj.sizlBitmap.cx < WidthSrc ||
+             BitmapMask->SurfObj.sizlBitmap.cy < HeightSrc))
         {
-            BitmapMask = DCMask->dclevel.pSurface;
-            if (BitmapMask &&
-                (BitmapMask->SurfObj.sizlBitmap.cx < WidthSrc ||
-                 BitmapMask->SurfObj.sizlBitmap.cy < HeightSrc))
-            {
-                DPRINT1("%dx%d mask is smaller than %dx%d bitmap\n", 
-                        BitmapMask->SurfObj.sizlBitmap.cx, BitmapMask->SurfObj.sizlBitmap.cy, 
-                        WidthSrc, HeightSrc);
-                goto failed;
-            }
-            /* Create mask offset point */
-            MaskPoint.x = XOriginMask;
-            MaskPoint.y = YOriginMask;
-            IntLPtoDP(DCMask, &MaskPoint, 1);
-            MaskPoint.x += DCMask->ptlDCOrig.x;
-            MaskPoint.y += DCMask->ptlDCOrig.x;
+            DPRINT1("%dx%d mask is smaller than %dx%d bitmap\n",
+                    BitmapMask->SurfObj.sizlBitmap.cx, BitmapMask->SurfObj.sizlBitmap.cy,
+                    WidthSrc, HeightSrc);
+            EXLATEOBJ_vCleanup(&exlo);
+            goto failed;
         }
+        /* Create mask offset point */
+        MaskPoint.x = XOriginMask;
+        MaskPoint.y = YOriginMask;
+        IntLPtoDP(DCMask, &MaskPoint, 1);
+        MaskPoint.x += DCMask->ptlDCOrig.x;
+        MaskPoint.y += DCMask->ptlDCOrig.x;
     }
 
     /* Perform the bitblt operation */
@@ -822,14 +681,15 @@ GreStretchBltMask(
                               BitmapMask ? &MaskPoint : NULL,
                               &DCDest->eboFill.BrushObject,
                               &BrushOrigin,
-                              ROP3_TO_ROP4(ROP));
-
-failed:
+                              ROP_TO_ROP4(ROP));
     if (UsesSource)
     {
         EXLATEOBJ_vCleanup(&exlo);
     }
-    if (UsesSource && hDCSrc != hDCDest)
+
+failed:
+    DC_vFinishBlit(DCDest, DCSrc);
+    if (UsesSource)
     {
         DC_UnlockDc(DCSrc);
     }
@@ -889,18 +749,13 @@ IntPatBlt(
 {
     RECTL DestRect;
     SURFACE *psurf;
-    EBRUSHOBJ eboFill;
+    EBRUSHOBJ eboFill ;
     POINTL BrushOrigin;
     BOOL ret;
 
     ASSERT(pbrush);
 
-    psurf = pdc->dclevel.pSurface;
-    if (psurf == NULL)
-    {
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
-        return FALSE;
-    }
+    FIXUP_ROP(dwRop);
 
     if (pbrush->flAttrs & GDIBRUSH_IS_NULL)
     {
@@ -939,6 +794,13 @@ IntPatBlt(
     BrushOrigin.x = pbrush->ptOrigin.x + pdc->ptlDCOrig.x;
     BrushOrigin.y = pbrush->ptOrigin.y + pdc->ptlDCOrig.y;
 
+    DC_vPrepareDCsForBlit(pdc, DestRect, NULL, DestRect);
+
+    psurf = pdc->dclevel.pSurface;
+
+    if (pdc->pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
+        DC_vUpdateFillBrush(pdc);
+
     EBRUSHOBJ_vInit(&eboFill, pbrush, pdc);
 
     ret = IntEngBitBlt(
@@ -950,9 +812,11 @@ IntPatBlt(
         &DestRect,
         NULL,
         NULL,
-        &eboFill.BrushObject, // use pDC->eboFill
+        &eboFill.BrushObject,
         &BrushOrigin,
-        ROP3_TO_ROP4(dwRop));
+        ROP_TO_ROP4(dwRop));
+
+    DC_vFinishBlit(pdc, NULL);
 
     EBRUSHOBJ_vCleanup(&eboFill);
 
@@ -974,7 +838,7 @@ IntGdiPolyPatBlt(
     pdc = DC_LockDc(hDC);
     if (!pdc)
     {
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
+        EngSetLastError(ERROR_INVALID_HANDLE);
         return FALSE;
     }
 
@@ -987,7 +851,7 @@ IntGdiPolyPatBlt(
 
     for (i = 0; i < cRects; i++)
     {
-        pbrush = BRUSH_LockBrush(pRects->hBrush);
+        pbrush = BRUSH_ShareLockBrush(pRects->hBrush);
         if(pbrush != NULL)
         {
             IntPatBlt(
@@ -998,7 +862,7 @@ IntGdiPolyPatBlt(
                 pRects->r.bottom,
                 dwRop,
                 pbrush);
-            BRUSH_UnlockBrush(pbrush);
+            BRUSH_ShareUnlockBrush(pbrush);
         }
         pRects++;
     }
@@ -1023,7 +887,7 @@ NtGdiPatBlt(
     PDC_ATTR pdcattr;
     BOOL ret;
 
-    BOOL UsesSource = ROP3_USES_SOURCE(ROP);
+    BOOL UsesSource = ROP_USES_SOURCE(ROP);
     if (UsesSource)
     {
         /* in this case we call on GdiMaskBlt */
@@ -1033,7 +897,7 @@ NtGdiPatBlt(
     dc = DC_LockDc(hDC);
     if (dc == NULL)
     {
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
+        EngSetLastError(ERROR_INVALID_HANDLE);
         return FALSE;
     }
     if (dc->dctype == DC_TYPE_INFO)
@@ -1048,17 +912,17 @@ NtGdiPatBlt(
     if (pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
         DC_vUpdateFillBrush(dc);
 
-    pbrush = BRUSH_LockBrush(pdcattr->hbrush);
+    pbrush = BRUSH_ShareLockBrush(pdcattr->hbrush);
     if (pbrush == NULL)
     {
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
+        EngSetLastError(ERROR_INVALID_HANDLE);
         DC_UnlockDc(dc);
         return FALSE;
     }
 
     ret = IntPatBlt(dc, XLeft, YLeft, Width, Height, ROP, pbrush);
 
-    BRUSH_UnlockBrush(pbrush);
+    BRUSH_ShareUnlockBrush(pbrush);
     DC_UnlockDc(dc);
 
     return ret;
@@ -1078,10 +942,10 @@ NtGdiPolyPatBlt(
 
     if (cRects > 0)
     {
-        rb = ExAllocatePoolWithTag(PagedPool, sizeof(PATRECT) * cRects, TAG_PATBLT);
+        rb = ExAllocatePoolWithTag(PagedPool, sizeof(PATRECT) * cRects, GDITAG_PLGBLT_DATA);
         if (!rb)
         {
-            SetLastWin32Error(ERROR_NOT_ENOUGH_MEMORY);
+            EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
             return FALSE;
         }
         _SEH2_TRY
@@ -1101,7 +965,7 @@ NtGdiPolyPatBlt(
 
         if (!NT_SUCCESS(Status))
         {
-            ExFreePoolWithTag(rb, TAG_PATBLT);
+            ExFreePoolWithTag(rb, GDITAG_PLGBLT_DATA);
             SetLastNtError(Status);
             return FALSE;
         }
@@ -1110,7 +974,7 @@ NtGdiPolyPatBlt(
     Ret = IntGdiPolyPatBlt(hDC, dwRop, rb, cRects, Mode);
 
     if (cRects > 0)
-        ExFreePoolWithTag(rb, TAG_PATBLT);
+        ExFreePoolWithTag(rb, GDITAG_PLGBLT_DATA);
 
     return Ret;
 }