Sync with trunk (r48545)
[reactos.git] / subsystems / win32 / win32k / ntuser / cursoricon.c
index 917b142..4b0fa99 100644 (file)
@@ -38,7 +38,7 @@
 
 #include <win32k.h>
 
-#define NDEBUG
+//#define NDEBUG
 #include <debug.h>
 
 static PAGED_LOOKASIDE_LIST gProcessLookasideList;
@@ -1217,71 +1217,92 @@ UserDrawIconEx(
     HBRUSH hbrFlickerFreeDraw,
     UINT diFlags)
 {
-    BOOL Ret = FALSE;
-    HBITMAP hbmMask, hbmColor;
-    BITMAP bmpColor, bm;
+    PSURFACE psurfColor = NULL, psurfMask, psurfDst = NULL;
+    HGDIOBJ hObjs[3];
+    PGDIOBJ pObjs[3];
     BOOL DoFlickerFree;
-    INT iOldBkColor = 0, iOldTxtColor = 0;
-
-    HDC hMemDC, hDestDC = hDc;
-    HGDIOBJ hOldOffBrush = 0;
-    HGDIOBJ hOldOffBmp = 0;
-    HBITMAP hTmpBmp = 0, hOffBmp = 0;
-    BOOL bAlpha = FALSE;
-    INT x=xLeft, y=yTop;
-
-    hbmMask = pIcon->IconInfo.hbmMask;
-    hbmColor = pIcon->IconInfo.hbmColor;
+    PDC pdc;
+    HSURF hsurfDst = NULL;
+    RECTL rcSrc, rcDst;
+    CLIPOBJ clo, *pclo;
+    EXLATEOBJ exlo;
+    BOOL bAlpha = FALSE, Ret = FALSE, bStretch;
 
     if (istepIfAniCur)
         DPRINT1("NtUserDrawIconEx: istepIfAniCur is not supported!\n");
 
-    if (!hbmMask || !IntGdiGetObject(hbmMask, sizeof(BITMAP), (PVOID)&bm))
+    DPRINT("Flags : 0x%08x\n", diFlags);
+
+    hObjs[0] = pIcon->IconInfo.hbmMask;
+    hObjs[1] = pIcon->IconInfo.hbmColor;
+    hObjs[2] = hDc;
+    GDIOBJ_LockMultipleObjs(3, hObjs, pObjs);
+    psurfMask = pObjs[0];
+    psurfColor = pObjs[1];
+    pdc = pObjs[2];
+
+    if (!pIcon->IconInfo.hbmMask
+         || !psurfMask)
     {
+        DPRINT1("No hbmMask?!\n");
+        if(pdc) DC_UnlockDc(pdc);
+        if(psurfColor) SURFACE_UnlockSurface(psurfColor);
         return FALSE;
     }
 
-    if (hbmColor && !IntGdiGetObject(hbmColor, sizeof(BITMAP), (PVOID)&bmpColor))
+    if (pIcon->IconInfo.hbmColor
+         && !psurfColor)
     {
+        DPRINT1("Unable to lock the color Bitmap?!\n");
+        SURFACE_UnlockSurface(psurfMask);
+        if(pdc) DC_UnlockDc(pdc);
         return FALSE;
     }
 
-    if(!(hMemDC = NtGdiCreateCompatibleDC(hDc)))
+    if(!psurfColor)
+    {
+        DPRINT("Monochrome Icon\n");
+        psurfColor = psurfMask;
+        RECTL_vSetRect(&rcSrc, 0, pIcon->Size.cy, pIcon->Size.cx, 2*pIcon->Size.cy);
+    }
+    else
     {
-        DPRINT1("NtGdiCreateCompatibleDC failed!\n");
+        DPRINT("Color Icon\n");
+        RECTL_vSetRect(&rcSrc, 0, 0, pIcon->Size.cx, pIcon->Size.cy);
+    }
+
+    if(!pdc)
+    {
+        SetLastWin32Error(ERROR_INVALID_PARAMETER);
+        SURFACE_UnlockSurface(psurfMask);
+        if(psurfColor != psurfMask) SURFACE_UnlockSurface(psurfColor);
+        DPRINT1("Invalid DC!\n");
         return FALSE;
     }
 
     /* Check for alpha */
-    if (hbmColor
-            && (bmpColor.bmBitsPixel == 32)
+    if ((BitsPerFormat(psurfColor->SurfObj.iBitmapFormat) == 32)
             && (diFlags & DI_IMAGE))
     {
-        SURFACE *psurfOff = NULL;
         PFN_DIB_GetPixel fnSource_GetPixel = NULL;
         INT i, j;
 
         /* In order to correctly display 32 bit icons Windows first scans the image,
            because information about transparency is not stored in any image's headers */
-        psurfOff = SURFACE_LockSurface(hbmColor);
-        if (psurfOff)
+        fnSource_GetPixel = DibFunctionsForBitmapFormat[psurfColor->SurfObj.iBitmapFormat].DIB_GetPixel;
+        if (fnSource_GetPixel)
         {
-            fnSource_GetPixel = DibFunctionsForBitmapFormat[psurfOff->SurfObj.iBitmapFormat].DIB_GetPixel;
-            if (fnSource_GetPixel)
+            for (i = 0; i < psurfColor->SurfObj.sizlBitmap.cx; i++)
             {
-                for (i = 0; i < psurfOff->SurfObj.sizlBitmap.cx; i++)
+                for (j = 0; j < psurfColor->SurfObj.sizlBitmap.cy; j++)
                 {
-                    for (j = 0; j < psurfOff->SurfObj.sizlBitmap.cy; j++)
-                    {
-                        bAlpha = ((BYTE)(fnSource_GetPixel(&psurfOff->SurfObj, i, j) >> 24) & 0xff);
-                        if (bAlpha)
-                            break;
-                    }
+                    bAlpha = ((BYTE)(fnSource_GetPixel(&psurfColor->SurfObj, i, j) >> 24) & 0xff);
                     if (bAlpha)
                         break;
                 }
+                if (bAlpha)
+                    break;
             }
-            SURFACE_UnlockSurface(psurfOff);
         }
     }
 
@@ -1293,45 +1314,90 @@ UserDrawIconEx(
         cyHeight = ((diFlags & DI_DEFAULTSIZE) ?
                     UserGetSystemMetrics(SM_CYICON) : pIcon->Size.cy);
 
+    /* Check stretching */
+    bStretch = (pIcon->Size.cx != cxWidth) || (pIcon->Size.cy != cyHeight);
+
     DoFlickerFree = (hbrFlickerFreeDraw &&
                      (GDI_HANDLE_GET_TYPE(hbrFlickerFreeDraw) == GDI_OBJECT_TYPE_BRUSH));
 
     if (DoFlickerFree)
     {
-        hDestDC = NtGdiCreateCompatibleDC(hDc);
-        if(!hDestDC)
+        EBRUSHOBJ ebo;
+        PBRUSH pBrush ;
+        POINTL ptBrushOrig;
+
+        pBrush = BRUSH_LockBrush(hbrFlickerFreeDraw);
+        if(!pBrush)
         {
-            DPRINT1("NtGdiCreateCompatibleDC failed!\n");
-            Ret = FALSE;
-            goto Cleanup ;
+            SetLastWin32Error(ERROR_INVALID_PARAMETER);
+            DPRINT1("Invalid brush!\n");
+            goto cleanup;
         }
-        hOffBmp = NtGdiCreateCompatibleBitmap(hDc, cxWidth, cyHeight);
-        if(!hOffBmp)
+
+        hsurfDst = IntCreateCompatibleBitmap(pdc, cxWidth, cyHeight);
+        if(!hsurfDst)
         {
-            DPRINT1("NtGdiCreateCompatibleBitmap failed!\n");
-            goto Cleanup ;
+            DPRINT1("Error : Failed to allocate the offscreen surface\n");
+            goto cleanup;
         }
-        hOldOffBmp = NtGdiSelectBitmap(hDestDC, hOffBmp);
-        hOldOffBrush = NtGdiSelectBrush(hDestDC, hbrFlickerFreeDraw);
-        NtGdiPatBlt(hDestDC, 0, 0, cxWidth, cyHeight, PATCOPY);
-        NtGdiSelectBrush(hDestDC, hOldOffBrush);
-        x=y=0;
+        psurfDst = SURFACE_LockSurface(hsurfDst);
+        if(!psurfDst)
+        {
+            DPRINT1("Error : Failed to lock the offScreen bitmap\n");
+            goto cleanup;
+        }
+        RECTL_vSetRect(&rcDst, 0, 0, cxWidth, cyHeight);
+
+        ptBrushOrig.x = pBrush->ptOrigin.x;
+        ptBrushOrig.y = pBrush->ptOrigin.y;
+
+        EBRUSHOBJ_vInit(&ebo, pBrush, pdc);
+
+        clo.iDComplexity =  DC_TRIVIAL;
+        pclo = &clo;
+
+        IntEngBitBlt(&psurfDst->SurfObj, NULL, NULL, pclo, NULL, &rcDst, NULL,
+                     NULL, &ebo.BrushObject, &ptBrushOrig, ROP3_TO_ROP4(PATCOPY));
+
+        EBRUSHOBJ_vCleanup(&ebo);
+        BRUSH_UnlockBrush(pBrush);
     }
+    else
+    {
+        RECT rcBmp;
+        RECTL_vSetRect(&rcDst, xLeft, yTop, xLeft + cxWidth, yTop + cyHeight);
+        IntLPtoDP(pdc, (LPPOINT)&rcDst, 2);
+        RECTL_vOffsetRect(&rcDst, pdc->ptlDCOrig.x, pdc->ptlDCOrig.y);
+
+        DC_vPrepareDCsForBlit(pdc, rcDst, NULL, rcDst );
 
-    /* Set Background/foreground colors */
-    iOldTxtColor = IntGdiSetTextColor(hDc, 0); //black
-    iOldBkColor = IntGdiSetBkColor(hDc, 0x00FFFFFF); //white
+        if (pdc->pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
+            DC_vUpdateFillBrush(pdc);
+
+        psurfDst = pdc->dclevel.pSurface;
+        pclo = pdc->rosdc.CombinedClip;
+        RECTL_vSetRect(&rcBmp, 0, 0, psurfDst->SurfObj.sizlBitmap.cx, psurfDst->SurfObj.sizlBitmap.cy);
+        if(!RECTL_bIntersectRect(&rcDst, &rcDst, &rcBmp))
+        {
+            Ret = FALSE;
+            goto done;
+        }
+    }
+
+    /* Optimization : use directly the palette of the DC,
+     * so we XLATE only once, and then we directly copy bits */
+    EXLATEOBJ_vInitialize(&exlo, psurfColor->ppal, pdc->dclevel.pSurface->ppal, 0, 0, 0xFFFFFFFF);
 
        if(bAlpha && (diFlags & DI_IMAGE))
        {
                BLENDFUNCTION pixelblend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
         BYTE Alpha;
         INT i, j;
-        PSURFACE psurf;
+        PSURFACE psurf = NULL;
         PBYTE ptr ;
         HBITMAP hMemBmp = NULL;
 
-        hMemBmp = BITMAP_CopyBitmap(hbmColor);
+        hMemBmp = BITMAP_CopyBitmap(pIcon->IconInfo.hbmColor);
         if(!hMemBmp)
         {
             DPRINT1("BITMAP_CopyBitmap failed!");
@@ -1360,109 +1426,116 @@ UserDrawIconEx(
             }
         }
 
-        SURFACE_UnlockSurface(psurf);
-
-        hTmpBmp = NtGdiSelectBitmap(hMemDC, hMemBmp);
-
-        Ret = NtGdiAlphaBlend(hDestDC,
-                                                 x,
-                                                     y,
-                              cxWidth,
-                              cyHeight,
-                              hMemDC,
-                              0,
-                              0,
-                              pIcon->Size.cx,
-                              pIcon->Size.cy,
-                              pixelblend,
-                              NULL);
-        NtGdiSelectBitmap(hMemDC, hTmpBmp);
+        DPRINT("Performing alpha blending\n");
+        Ret = IntEngAlphaBlend(&psurfDst->SurfObj,
+                               &psurf->SurfObj,
+                               pclo,
+                               &exlo.xlo,
+                               &rcDst,
+                               &rcSrc,
+                               (BLENDOBJ*)&pixelblend);
+
     CleanupAlpha:
+        if(psurf) SURFACE_UnlockSurface(psurf);
         if(hMemBmp) NtGdiDeleteObjectApp(hMemBmp);
                if(Ret) goto done;
+        else DPRINT1("IntEngAlphaBlend failed!\n");
     }
 
-    if (diFlags & DI_MASK)
+    if (diFlags & DI_IMAGE)
     {
-        hTmpBmp = NtGdiSelectBitmap(hMemDC, hbmMask);
-        NtGdiStretchBlt(hDestDC,
-                        x,
-                        y,
-                        cxWidth,
-                        cyHeight,
-                        hMemDC,
-                        0,
-                        0,
-                        pIcon->Size.cx,
-                        pIcon->Size.cy,
-                        SRCAND,
-                        0);
-        NtGdiSelectBitmap(hMemDC, hTmpBmp);
-    }
-
-    if(diFlags & DI_IMAGE)
-    {
-               if (hbmColor)
+        POINTL ptMaskOrig = {0,0};
+        if(bStretch)
         {
-            DWORD rop = (diFlags & DI_MASK) ? SRCINVERT : SRCCOPY ;
-            hTmpBmp = NtGdiSelectBitmap(hMemDC, hbmColor);
-            NtGdiStretchBlt(hDestDC,
-                            x,
-                            y,
-                            cxWidth,
-                            cyHeight,
-                            hMemDC,
-                            0,
-                            0,
-                            pIcon->Size.cx,
-                            pIcon->Size.cy,
-                            rop,
-                            0);
-            NtGdiSelectBitmap(hMemDC, hTmpBmp);
+            DPRINT("Stretching\n");
+            Ret = IntEngStretchBlt(&psurfDst->SurfObj,
+                                   &psurfColor->SurfObj,
+                                   (diFlags & DI_MASK) ? &psurfMask->SurfObj : NULL,
+                                   pclo,
+                                   &exlo.xlo,
+                                   &rcDst,
+                                   &rcSrc,
+                                   (diFlags & DI_MASK) ? &ptMaskOrig : NULL,
+                                   NULL,
+                                   NULL,
+                                   (diFlags & DI_MASK) ? R4_MASK : ROP3_TO_ROP4(SRCCOPY));
+            if(!Ret) DPRINT1("IntEngStretchBlt Failed\n");
         }
         else
         {
-            /* Mask bitmap holds the information in its second half */
-            DWORD rop = (diFlags & DI_MASK) ? SRCINVERT : SRCCOPY ;
-            hTmpBmp = NtGdiSelectBitmap(hMemDC, hbmMask);
-            NtGdiStretchBlt(hDestDC,
-                            x,
-                            y,
-                            cxWidth,
-                            cyHeight,
-                            hMemDC,
-                            0,
-                            pIcon->Size.cy,
-                            pIcon->Size.cx,
-                            pIcon->Size.cy,
-                            rop,
-                            0);
-            NtGdiSelectBitmap(hMemDC, hTmpBmp);
+            DPRINT("Blting\n");
+            Ret = IntEngBitBlt(&psurfDst->SurfObj,
+                               &psurfColor->SurfObj,
+                               (diFlags & DI_MASK) ? &psurfMask->SurfObj : NULL,
+                               pclo,
+                               &exlo.xlo,
+                               &rcDst,
+                               (PPOINTL)&rcSrc,
+                               (diFlags & DI_MASK) ? &ptMaskOrig : NULL,
+                               NULL,
+                               NULL,
+                               (diFlags & DI_MASK) ? R4_MASK : ROP3_TO_ROP4(SRCCOPY));
+            if(!Ret) DPRINT1("IntEngBitBlt Failed\n");
         }
     }
+    else
+    {
+        DPRINT1("Uh? Calling DrawIcon without anything to draw? diFlags %d\n", diFlags);
+    }
 
 done:
-    if(hDestDC != hDc)
+    if(DoFlickerFree && Ret)
     {
-        NtGdiBitBlt(hDc, xLeft, yTop, cxWidth, cyHeight, hDestDC, 0, 0, SRCCOPY, 0, 0);
-    }
+        POINTL ptSrc = {0,0};
+        RECTL rcBmp;
+
+        RECTL_vSetRect(&rcDst, xLeft, yTop, xLeft + cxWidth, yTop + cyHeight);
+
+        IntLPtoDP(pdc, (LPPOINT)&rcDst, 2);
+
+        RECTL_vOffsetRect(&rcDst, pdc->ptlDCOrig.x, pdc->ptlDCOrig.y);
+        DC_vPrepareDCsForBlit(pdc, rcDst, NULL, rcDst );
+
+        if (pdc->pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
+            DC_vUpdateFillBrush(pdc);
+
+        RECTL_vSetRect(&rcBmp, 0, 0,
+                       pdc->dclevel.pSurface->SurfObj.sizlBitmap.cx,
+                       pdc->dclevel.pSurface->SurfObj.sizlBitmap.cy);
 
-    /* Restore foreground and background colors */
-    IntGdiSetBkColor(hDc, iOldBkColor);
-    IntGdiSetTextColor(hDc, iOldTxtColor);
+        if(RECTL_bIntersectRect(&rcDst, &rcDst, &rcBmp))
+        {
+            /* Copy everything */
+            DPRINT("Copying bits from offscreen buffer\n");
+            Ret = IntEngCopyBits(&pdc->dclevel.pSurface->SurfObj,
+                                 &psurfDst->SurfObj,
+                                 pdc->rosdc.CombinedClip,
+                                 gpxloTrivial,
+                                 &rcDst,
+                                 &ptSrc);
+            if(!Ret) DPRINT1("IntEngCopyBits Failed\n");
+        }
+
+        DC_vFinishBlit(pdc, NULL);
+    }
 
-    Ret = TRUE ;
+    if(!DoFlickerFree) DC_vFinishBlit(pdc, NULL);
+    EXLATEOBJ_vCleanup(&exlo);
 
-Cleanup:
-    NtGdiDeleteObjectApp(hMemDC);
-    if(hDestDC != hDc)
+cleanup:
+    if(psurfColor != psurfMask)
+        SURFACE_UnlockSurface(psurfColor);
+    SURFACE_UnlockSurface(psurfMask);
+    if(hsurfDst)
     {
-        if(hOldOffBmp) NtGdiSelectBitmap(hDestDC, hOldOffBmp);
-        NtGdiDeleteObjectApp(hDestDC);
-        if(hOffBmp) NtGdiDeleteObjectApp(hOffBmp);
+        if(psurfDst) SURFACE_UnlockSurface(psurfDst);
+        GreDeleteObject(hsurfDst);
     }
+    DC_UnlockDc(pdc);
 
-    return Ret;
+    DPRINT("return %s\n", Ret ? "TRUE" : "FALSE") ;
+
+    return Ret ;
 }
 
 /*