[WIN32K]
[reactos.git] / subsystems / win32 / win32k / objects / bitblt.c
index 9249b66..5b59a59 100644 (file)
@@ -18,7 +18,7 @@
 */
 /* $Id: bitmaps.c 28300 2007-08-12 15:20:09Z tkreuzer $ */
 
-#include <w32k.h>
+#include <win32k.h>
 
 #define NDEBUG
 #include <debug.h>
@@ -222,16 +222,13 @@ NtGdiBitBlt(
     }
     else if(DCSrc)
     {
-        DPRINT1("Getting a valid Source handle without using source!!!");
+        DPRINT1("Getting a valid Source handle without using source!!!\n");
         GDIOBJ_UnlockObjByPtr(&DCSrc->BaseObject);
         DCSrc = NULL ;
     }
 
     pdcattr = DCDest->pdcattr;
 
-    if (pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
-        DC_vUpdateFillBrush(DCDest);
-
     DestRect.left   = XDest;
     DestRect.top    = YDest;
     DestRect.right  = XDest+Width;
@@ -262,6 +259,9 @@ NtGdiBitBlt(
     /* 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)
@@ -328,9 +328,7 @@ NtGdiTransparentBlt(
     PGDIOBJ apObj[2];
     RECTL rcDest, rcSrc;
     SURFACE *BitmapDest, *BitmapSrc = NULL;
-    HPALETTE SourcePalette = 0, DestPalette = 0;
-    PPALETTE PalDestGDI, PalSourceGDI;
-    USHORT PalDestMode, PalSrcMode;
+    PPALETTE PalSourceGDI;
     ULONG TransparentColor = 0;
     BOOL Ret = FALSE;
     EXLATEOBJ exlo;
@@ -396,42 +394,27 @@ NtGdiTransparentBlt(
         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)))
+    if (BitmapSrc->ppal)
     {
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
-        goto done;
-    }
-    PalSrcMode = PalSourceGDI->Mode;
-    PALETTE_UnlockPalette(PalSourceGDI);
-
-    if(DestPalette != SourcePalette)
-    {
-        if (!(PalDestGDI = PALETTE_LockPalette(DestPalette)))
-        {
-            SetLastWin32Error(ERROR_INVALID_HANDLE);
-            goto done;
-        }
-        PalDestMode = PalDestGDI->Mode;
-        PALETTE_UnlockPalette(PalDestGDI);
+        GDIOBJ_IncrementShareCount(&BitmapSrc->ppal->BaseObject);
+        PalSourceGDI = BitmapSrc->ppal ;
     }
     else
+        PalSourceGDI = PALETTE_ShareLockPalette(pPrimarySurface->devinfo.hpalDefault) ;
+
+    if(!PalSourceGDI)
     {
-        PalDestMode = PalSrcMode;
-        PalDestGDI = PalSourceGDI;
+        SetLastWin32Error(ERROR_INVALID_HANDLE);
+        goto done;
     }
 
     /* 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);
+    PALETTE_ShareUnlockPalette(PalSourceGDI);
 
-    EXLATEOBJ_vInitialize(&exlo, PalSourceGDI, PalDestGDI, 0, 0, 0);
+    EXLATEOBJ_vInitXlateFromDCs(&exlo, DCSrc, DCDest);
 
     Ret = IntEngTransparentBlt(&BitmapDest->SurfObj, &BitmapSrc->SurfObj,
         DCDest->rosdc.CombinedClip, &exlo.xlo, &rcDest, &rcSrc,
@@ -708,6 +691,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;
@@ -726,52 +711,59 @@ GreStretchBltMask(
         return FALSE;
     }
 
-    DCDest = DC_LockDc(hDCDest);
+    DPRINT("Locking DCs\n");
+    ahDC[0] = hDCDest;
+    ahDC[1] = hDCSrc ;
+    ahDC[2] = hDCMask ;
+    GDIOBJ_LockMultipleObjs(3, ahDC, apObj);
+    DCDest = apObj[0];
+    DCSrc = apObj[1];
+    DCMask = apObj[2];
+
     if (NULL == DCDest)
     {
-        DPRINT1("Invalid destination dc handle (0x%08x) passed to NtGdiStretchBlt\n", hDCDest);
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
+        if(DCSrc) GDIOBJ_UnlockObjByPtr(&DCSrc->BaseObject);
+        if(DCMask) GDIOBJ_UnlockObjByPtr(&DCMask->BaseObject);
+        DPRINT("Invalid destination dc handle (0x%08x) passed to NtGdiBitBlt\n", hDCDest);
         return FALSE;
     }
 
     if (DCDest->dctype == DC_TYPE_INFO)
     {
-        DC_UnlockDc(DCDest);
+        if(DCSrc) GDIOBJ_UnlockObjByPtr(&DCSrc->BaseObject);
+        if(DCMask) GDIOBJ_UnlockObjByPtr(&DCMask->BaseObject);
+        GDIOBJ_UnlockObjByPtr(&DCDest->BaseObject);
         /* Yes, Windows really returns TRUE in this case */
         return TRUE;
     }
 
     if (UsesSource)
     {
-        if (hDCSrc != hDCDest)
+        if (NULL == DCSrc)
         {
-            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;
-            }
+            GDIOBJ_UnlockObjByPtr(&DCDest->BaseObject);
+            if(DCMask) GDIOBJ_UnlockObjByPtr(&DCMask->BaseObject);
+            DPRINT("Invalid source dc handle (0x%08x) passed to NtGdiBitBlt\n", hDCSrc);
+            return FALSE;
         }
-        else
+        if (DCSrc->dctype == DC_TYPE_INFO)
         {
-            DCSrc = DCDest;
+            GDIOBJ_UnlockObjByPtr(&DCDest->BaseObject);
+            GDIOBJ_UnlockObjByPtr(&DCSrc->BaseObject);
+            if(DCMask) GDIOBJ_UnlockObjByPtr(&DCMask->BaseObject);
+            /* Yes, Windows really returns TRUE in this case */
+            return TRUE;
         }
     }
+    else if(DCSrc)
+    {
+        DPRINT1("Getting a valid Source handle without using source!!!\n");
+        GDIOBJ_UnlockObjByPtr(&DCSrc->BaseObject);
+        DCSrc = NULL ;
+    }
 
     pdcattr = DCDest->pdcattr;
 
-    if (pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
-        DC_vUpdateFillBrush(DCDest);
-
     DestRect.left   = XOriginDest;
     DestRect.top    = YOriginDest;
     DestRect.right  = XOriginDest+WidthDest;
@@ -801,6 +793,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)
@@ -821,28 +819,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 */
@@ -857,13 +852,14 @@ GreStretchBltMask(
                               &DCDest->eboFill.BrushObject,
                               &BrushOrigin,
                               ROP3_TO_ROP4(ROP));
-
-failed:
     if (UsesSource)
     {
         EXLATEOBJ_vCleanup(&exlo);
     }
-    if (UsesSource && hDCSrc != hDCDest)
+
+failed:
+    DC_vFinishBlit(DCDest, DCSrc);
+    if (UsesSource)
     {
         DC_UnlockDc(DCSrc);
     }
@@ -923,19 +919,12 @@ 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;
-    }
-
     if (pbrush->flAttrs & GDIBRUSH_IS_NULL)
     {
         return TRUE;
@@ -973,10 +962,15 @@ IntPatBlt(
     BrushOrigin.x = pbrush->ptOrigin.x + pdc->ptlDCOrig.x;
     BrushOrigin.y = pbrush->ptOrigin.y + pdc->ptlDCOrig.y;
 
-    EBRUSHOBJ_vInit(&eboFill, pbrush, pdc);
-
     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(
         &psurf->SurfObj,
         NULL,
@@ -986,14 +980,14 @@ IntPatBlt(
         &DestRect,
         NULL,
         NULL,
-        &eboFill.BrushObject, // use pDC->eboFill
+        &eboFill.BrushObject,
         &BrushOrigin,
         ROP3_TO_ROP4(dwRop));
 
-    EBRUSHOBJ_vCleanup(&eboFill);
-
     DC_vFinishBlit(pdc, NULL);
 
+    EBRUSHOBJ_vCleanup(&eboFill);
+
     return ret;
 }