[CMAKE]
[reactos.git] / subsystems / win32 / win32k / objects / fillshap.c
index bf5e7aa..c1ff201 100644 (file)
@@ -49,7 +49,7 @@ IntGdiPolygon(PDC    dc,
 
     if (!Points || Count < 2 )
     {
-        SetLastWin32Error(ERROR_INVALID_PARAMETER);
+        EngSetLastError(ERROR_INVALID_PARAMETER);
         return FALSE;
     }
 
@@ -214,7 +214,7 @@ NtGdiEllipse(
     dc = DC_LockDc(hDC);
     if (dc == NULL)
     {
-       SetLastWin32Error(ERROR_INVALID_HANDLE);
+       EngSetLastError(ERROR_INVALID_HANDLE);
        return FALSE;
     }
     if (dc->dctype == DC_TYPE_INFO)
@@ -248,12 +248,12 @@ NtGdiEllipse(
     if (pdcattr->ulDirty_ & (DIRTY_LINE | DC_PEN_DIRTY))
         DC_vUpdateLineBrush(dc);
 
-    pbrush = PEN_LockPen(pdcattr->hpen);
+    pbrush = PEN_ShareLockPen(pdcattr->hpen);
     if (!pbrush)
     {
         DPRINT1("Ellipse Fail 1\n");
         DC_UnlockDc(dc);
-        SetLastWin32Error(ERROR_INTERNAL_ERROR);
+        EngSetLastError(ERROR_INTERNAL_ERROR);
         return FALSE;
     }
 
@@ -297,11 +297,11 @@ NtGdiEllipse(
     DPRINT("Ellipse 2: XLeft: %d, YLeft: %d, Width: %d, Height: %d\n",
                CenterX - RadiusX, CenterY + RadiusY, RadiusX*2, RadiusY*2);
 
-    pFillBrushObj = BRUSH_LockBrush(pdcattr->hbrush);
+    pFillBrushObj = BRUSH_ShareLockBrush(pdcattr->hbrush);
     if (NULL == pFillBrushObj)
     {
         DPRINT1("FillEllipse Fail\n");
-        SetLastWin32Error(ERROR_INTERNAL_ERROR);
+        EngSetLastError(ERROR_INTERNAL_ERROR);
         ret = FALSE;
     }
     else
@@ -317,7 +317,7 @@ NtGdiEllipse(
                               RadiusX*2, // Width
                               RadiusY*2, // Height
                               &tmpFillBrushObj);
-        BRUSH_UnlockBrush(pFillBrushObj);
+        BRUSH_ShareUnlockBrush(pFillBrushObj);
     }
 
     if (ret)
@@ -329,7 +329,7 @@ NtGdiEllipse(
                              pbrush);
 
     pbrush->ptPenWidth.x = PenOrigWidth;
-    PEN_UnlockPen(pbrush);
+    PEN_ShareUnlockPen(pbrush);
     DC_UnlockDc(dc);
     DPRINT("Ellipse Exit.\n");
     return ret;
@@ -418,7 +418,7 @@ NtGdiPolyPolyDraw( IN HDC hDC,
     if (nInvalid != 0)
     {
         /* If at least one poly count is 0 or 1, fail */
-        SetLastWin32Error(ERROR_INVALID_PARAMETER);
+        EngSetLastError(ERROR_INVALID_PARAMETER);
         return FALSE;
     }
 
@@ -428,7 +428,7 @@ NtGdiPolyPolyDraw( IN HDC hDC,
                                   TAG_SHAPE);
     if (!pTemp)
     {
-        SetLastWin32Error(ERROR_NOT_ENOUGH_MEMORY);
+        EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
         return FALSE;
     }
 
@@ -465,7 +465,7 @@ NtGdiPolyPolyDraw( IN HDC hDC,
     dc = DC_LockDc(hDC);
     if (!dc)
     {
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
+        EngSetLastError(ERROR_INVALID_HANDLE);
         ExFreePool(pTemp);
         return FALSE;
     }
@@ -506,7 +506,7 @@ NtGdiPolyPolyDraw( IN HDC hDC,
             Ret = IntGdiPolyBezierTo(dc, SafePoints, *SafeCounts);
             break;
         default:
-            SetLastWin32Error(ERROR_INVALID_PARAMETER);
+            EngSetLastError(ERROR_INVALID_PARAMETER);
             Ret = FALSE;
     }
 
@@ -545,10 +545,11 @@ IntRectangle(PDC dc,
         return PATH_Rectangle ( dc, LeftRect, TopRect, RightRect, BottomRect );
     }
 
-    DestRect.left = LeftRect;
-    DestRect.right = RightRect;
-    DestRect.top = TopRect;
-    DestRect.bottom = BottomRect;
+       /* Make sure rectangle is not inverted */
+    DestRect.left   = min(LeftRect, RightRect);
+    DestRect.right  = max(LeftRect, RightRect);
+    DestRect.top    = min(TopRect,  BottomRect);
+    DestRect.bottom = max(TopRect,  BottomRect);
 
     IntLPtoDP(dc, (LPPOINT)&DestRect, 2);
 
@@ -604,7 +605,7 @@ IntRectangle(PDC dc,
                                NULL,
                                &dc->eboFill.BrushObject,
                                &BrushOrigin,
-                               ROP3_TO_ROP4(PATCOPY));
+                               ROP4_FROM_INDEX(R3_OPINDEX_PATCOPY));
         }
     }
 
@@ -667,7 +668,7 @@ NtGdiRectangle(HDC  hDC,
     dc = DC_LockDc(hDC);
     if (!dc)
     {
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
+        EngSetLastError(ERROR_INVALID_HANDLE);
         return FALSE;
     }
     if (dc->dctype == DC_TYPE_INFO)
@@ -713,7 +714,7 @@ IntRoundRect(
     int  yCurveDiameter)
 {
     PDC_ATTR pdcattr;
-    PBRUSH   pbrushLine, pbrushFill;
+    PBRUSH   pbrLine, pbrFill;
     RECTL RectBounds;
     LONG PenWidth, PenOrigWidth;
     BOOL ret = TRUE; // default to success
@@ -747,18 +748,18 @@ IntRoundRect(
     if (pdcattr->ulDirty_ & (DIRTY_LINE | DC_PEN_DIRTY))
         DC_vUpdateLineBrush(dc);
 
-    pbrushLine = PEN_LockPen(pdcattr->hpen);
-    if (!pbrushLine)
+    pbrLine = PEN_ShareLockPen(pdcattr->hpen);
+    if (!pbrLine)
     {
         /* Nothing to do, as we don't have a bitmap */
-        SetLastWin32Error(ERROR_INTERNAL_ERROR);
+        EngSetLastError(ERROR_INTERNAL_ERROR);
         return FALSE;
     }
 
-    PenOrigWidth = PenWidth = pbrushLine->ptPenWidth.x;
-    if (pbrushLine->ulPenStyle == PS_NULL) PenWidth = 0;
+    PenOrigWidth = PenWidth = pbrLine->ptPenWidth.x;
+    if (pbrLine->ulPenStyle == PS_NULL) PenWidth = 0;
 
-    if (pbrushLine->ulPenStyle == PS_INSIDEFRAME)
+    if (pbrLine->ulPenStyle == PS_INSIDEFRAME)
     {
        if (2*PenWidth > (Right - Left)) PenWidth = (Right -Left + 1)/2;
        if (2*PenWidth > (Bottom - Top)) PenWidth = (Bottom -Top + 1)/2;
@@ -769,7 +770,7 @@ IntRoundRect(
     }
 
     if (!PenWidth) PenWidth = 1;
-    pbrushLine->ptPenWidth.x = PenWidth;
+    pbrLine->ptPenWidth.x = PenWidth;
 
     RectBounds.left = Left;
     RectBounds.top = Top;
@@ -783,16 +784,16 @@ IntRoundRect(
     RectBounds.right  += dc->ptlDCOrig.x;
     RectBounds.bottom += dc->ptlDCOrig.y;
 
-    pbrushFill = BRUSH_LockBrush(pdcattr->hbrush);
-    if (NULL == pbrushFill)
+    pbrFill = BRUSH_ShareLockBrush(pdcattr->hbrush);
+    if (!pbrFill)
     {
         DPRINT1("FillRound Fail\n");
-        SetLastWin32Error(ERROR_INTERNAL_ERROR);
+        EngSetLastError(ERROR_INTERNAL_ERROR);
         ret = FALSE;
     }
     else
     {
-        RtlCopyMemory(&brushTemp, pbrushFill, sizeof(brushTemp));
+        RtlCopyMemory(&brushTemp, pbrFill, sizeof(brushTemp));
         brushTemp.ptOrigin.x += RectBounds.left - Left;
         brushTemp.ptOrigin.y += RectBounds.top - Top;
         ret = IntFillRoundRect( dc,
@@ -803,7 +804,7 @@ IntRoundRect(
                                 xCurveDiameter,
                                 yCurveDiameter,
                                 &brushTemp);
-        BRUSH_UnlockBrush(pbrushFill);
+        BRUSH_ShareUnlockBrush(pbrFill);
     }
 
     if (ret)
@@ -814,10 +815,10 @@ IntRoundRect(
                 RectBounds.bottom,
                    xCurveDiameter,
                    yCurveDiameter,
-                   pbrushLine);
+                   pbrLine);
 
-    pbrushLine->ptPenWidth.x = PenOrigWidth;
-    PEN_UnlockPen(pbrushLine);
+    pbrLine->ptPenWidth.x = PenOrigWidth;
+    PEN_ShareUnlockPen(pbrLine);
     return ret;
 }
 
@@ -839,7 +840,7 @@ NtGdiRoundRect(
     if ( !dc )
     {
         DPRINT1("NtGdiRoundRect() - hDC is invalid\n");
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
+        EngSetLastError(ERROR_INVALID_HANDLE);
     }
     else if (dc->dctype == DC_TYPE_INFO)
     {
@@ -856,109 +857,121 @@ NtGdiRoundRect(
     return ret;
 }
 
-BOOL FASTCALL
-IntGdiGradientFill(
-    DC *dc,
+BOOL
+NTAPI
+GreGradientFill(
+    HDC hdc,
     PTRIVERTEX pVertex,
-    ULONG uVertex,
+    ULONG nVertex,
     PVOID pMesh,
-    ULONG uMesh,
+    ULONG nMesh,
     ULONG ulMode)
 {
+    PDC pdc;
     SURFACE *psurf;
-    PPALETTE PalDestGDI;
     EXLATEOBJ exlo;
-    RECTL Extent;
-    POINTL DitherOrg;
+    RECTL rclExtent;
+    POINTL ptlDitherOrg;
     ULONG i;
-    BOOL Ret;
-    HPALETTE hDestPalette;
-
-    ASSERT(dc);
-    ASSERT(pVertex);
-    ASSERT(uVertex);
-    ASSERT(pMesh);
-    ASSERT(uMesh);
+    BOOL bRet;
 
     /* check parameters */
     if (ulMode & GRADIENT_FILL_TRIANGLE)
     {
-        PGRADIENT_TRIANGLE tr = (PGRADIENT_TRIANGLE)pMesh;
+        PGRADIENT_TRIANGLE pTriangle = (PGRADIENT_TRIANGLE)pMesh;
 
-        for (i = 0; i < uMesh; i++, tr++)
+        for (i = 0; i < nMesh; i++, pTriangle++)
         {
-            if (tr->Vertex1 >= uVertex ||
-                    tr->Vertex2 >= uVertex ||
-                    tr->Vertex3 >= uVertex)
+            if (pTriangle->Vertex1 >= nVertex ||
+                pTriangle->Vertex2 >= nVertex ||
+                pTriangle->Vertex3 >= nVertex)
             {
-                SetLastWin32Error(ERROR_INVALID_PARAMETER);
+                EngSetLastError(ERROR_INVALID_PARAMETER);
                 return FALSE;
             }
         }
     }
     else
     {
-        PGRADIENT_RECT rc = (PGRADIENT_RECT)pMesh;
-        for (i = 0; i < uMesh; i++, rc++)
+        PGRADIENT_RECT pRect = (PGRADIENT_RECT)pMesh;
+        for (i = 0; i < nMesh; i++, pRect++)
         {
-            if (rc->UpperLeft >= uVertex || rc->LowerRight >= uVertex)
+            if (pRect->UpperLeft >= nVertex || pRect->LowerRight >= nVertex)
             {
-                SetLastWin32Error(ERROR_INVALID_PARAMETER);
+                EngSetLastError(ERROR_INVALID_PARAMETER);
                 return FALSE;
             }
         }
     }
 
+    /* Lock the output DC */
+    pdc = DC_LockDc(hdc);
+    if(!pdc)
+    {
+        EngSetLastError(ERROR_INVALID_HANDLE);
+        return FALSE;
+    }
+
+    if(pdc->dctype == DC_TYPE_INFO)
+    {
+        DC_UnlockDc(pdc);
+        /* Yes, Windows really returns TRUE in this case */
+        return TRUE;
+    }
+
+    psurf = pdc->dclevel.pSurface;
+    if(!psurf)
+    {
+        /* Memory DC with no surface selected */
+        DC_UnlockDc(pdc);
+        return TRUE; //CHECKME
+    }
+
     /* calculate extent */
-    Extent.left = Extent.right = pVertex->x;
-    Extent.top = Extent.bottom = pVertex->y;
-    for (i = 0; i < uVertex; i++)
+    rclExtent.left = rclExtent.right = pVertex->x;
+    rclExtent.top = rclExtent.bottom = pVertex->y;
+    for (i = 0; i < nVertex; i++)
     {
-        Extent.left = min(Extent.left, (pVertex + i)->x);
-        Extent.right = max(Extent.right, (pVertex + i)->x);
-        Extent.top = min(Extent.top, (pVertex + i)->y);
-        Extent.bottom = max(Extent.bottom, (pVertex + i)->y);
+        rclExtent.left = min(rclExtent.left, (pVertex + i)->x);
+        rclExtent.right = max(rclExtent.right, (pVertex + i)->x);
+        rclExtent.top = min(rclExtent.top, (pVertex + i)->y);
+        rclExtent.bottom = max(rclExtent.bottom, (pVertex + i)->y);
     }
-    IntLPtoDP(dc, (LPPOINT)&Extent, 2);
+    IntLPtoDP(pdc, (LPPOINT)&rclExtent, 2);
 
-    Extent.left   += dc->ptlDCOrig.x;
-    Extent.right  += dc->ptlDCOrig.x;
-    Extent.top    += dc->ptlDCOrig.y;
-    Extent.bottom += dc->ptlDCOrig.y;
+    rclExtent.left   += pdc->ptlDCOrig.x;
+    rclExtent.right  += pdc->ptlDCOrig.x;
+    rclExtent.top    += pdc->ptlDCOrig.y;
+    rclExtent.bottom += pdc->ptlDCOrig.y;
 
-    DitherOrg.x = DitherOrg.y = 0;
-    IntLPtoDP(dc, (LPPOINT)&DitherOrg, 1);
+    ptlDitherOrg.x = ptlDitherOrg.y = 0;
+    IntLPtoDP(pdc, (LPPOINT)&ptlDitherOrg, 1);
 
-    DitherOrg.x += dc->ptlDCOrig.x;
-    DitherOrg.y += dc->ptlDCOrig.y;
+    ptlDitherOrg.x += pdc->ptlDCOrig.x;
+    ptlDitherOrg.y += pdc->ptlDCOrig.y;
 
-    psurf = dc->dclevel.pSurface;
-    /* FIXME - psurf can be NULL!!! Don't assert but handle this case gracefully! */
-    ASSERT(psurf);
+    EXLATEOBJ_vInitialize(&exlo, &gpalRGB, psurf->ppal, 0, 0, 0);
 
-    hDestPalette = psurf->hDIBPalette;
-    if (!hDestPalette) hDestPalette = pPrimarySurface->devinfo.hpalDefault;
+    ASSERT(pdc->rosdc.CombinedClip);
 
-    PalDestGDI = PALETTE_LockPalette(hDestPalette);
-    EXLATEOBJ_vInitialize(&exlo, &gpalRGB, PalDestGDI, 0, 0, 0);
+    DC_vPrepareDCsForBlit(pdc, rclExtent, NULL, rclExtent);
 
-    Ret = IntEngGradientFill(&psurf->SurfObj,
-                             dc->rosdc.CombinedClip,
+    bRet = IntEngGradientFill(&psurf->SurfObj,
+                             pdc->rosdc.CombinedClip,
                              &exlo.xlo,
                              pVertex,
-                             uVertex,
+                             nVertex,
                              pMesh,
-                             uMesh,
-                             &Extent,
-                             &DitherOrg,
+                             nMesh,
+                             &rclExtent,
+                             &ptlDitherOrg,
                              ulMode);
 
     EXLATEOBJ_vCleanup(&exlo);
+    DC_vFinishBlit(pdc, NULL);
+    DC_UnlockDc(pdc);
 
-    if (PalDestGDI)
-        PALETTE_UnlockPalette(PalDestGDI);
-
-    return Ret;
+    return bRet;
 }
 
 BOOL
@@ -966,34 +979,20 @@ APIENTRY
 NtGdiGradientFill(
     HDC hdc,
     PTRIVERTEX pVertex,
-    ULONG uVertex,
+    ULONG nVertex,
     PVOID pMesh,
-    ULONG uMesh,
+    ULONG nMesh,
     ULONG ulMode)
 {
-    DC *dc;
-    BOOL Ret;
+    BOOL bRet;
     PTRIVERTEX SafeVertex;
     PVOID SafeMesh;
-    ULONG SizeMesh;
-    NTSTATUS Status = STATUS_SUCCESS;
+    ULONG cbVertex, cbMesh;
 
-    dc = DC_LockDc(hdc);
-    if (!dc)
-    {
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
-        return FALSE;
-    }
-    if (dc->dctype == DC_TYPE_INFO)
-    {
-        DC_UnlockDc(dc);
-        /* Yes, Windows really returns TRUE in this case */
-        return TRUE;
-    }
-    if (!pVertex || !uVertex || !pMesh || !uMesh)
+    /* Validate parameters */
+    if (!pVertex || !nVertex || !pMesh || !nMesh)
     {
-        DC_UnlockDc(dc);
-        SetLastWin32Error(ERROR_INVALID_PARAMETER);
+        EngSetLastError(ERROR_INVALID_PARAMETER);
         return FALSE;
     }
 
@@ -1001,81 +1000,55 @@ NtGdiGradientFill(
     {
         case GRADIENT_FILL_RECT_H:
         case GRADIENT_FILL_RECT_V:
-            SizeMesh = uMesh * sizeof(GRADIENT_RECT);
+            cbMesh = nMesh * sizeof(GRADIENT_RECT);
             break;
         case GRADIENT_FILL_TRIANGLE:
-            SizeMesh = uMesh * sizeof(TRIVERTEX);
+            cbMesh = nMesh * sizeof(GRADIENT_TRIANGLE);
             break;
         default:
-            DC_UnlockDc(dc);
-            SetLastWin32Error(ERROR_INVALID_PARAMETER);
+            EngSetLastError(ERROR_INVALID_PARAMETER);
             return FALSE;
     }
 
-    _SEH2_TRY
+    cbVertex = nVertex * sizeof(TRIVERTEX) ;
+    if(cbVertex + cbMesh <= cbVertex)
     {
-        ProbeForRead(pVertex,
-                     uVertex * sizeof(TRIVERTEX),
-                     1);
-        ProbeForRead(pMesh,
-                     SizeMesh,
-                     1);
-    }
-    _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
-    {
-        Status = _SEH2_GetExceptionCode();
-    }
-    _SEH2_END;
-
-    if (!NT_SUCCESS(Status))
-    {
-        DC_UnlockDc(dc);
-        SetLastWin32Error(Status);
-        return FALSE;
+        /* Overflow */
+        return FALSE ;
     }
 
-    if (!(SafeVertex = ExAllocatePoolWithTag(PagedPool, (uVertex * sizeof(TRIVERTEX)) + SizeMesh, TAG_SHAPE)))
+    /* Allocate a kernel mode buffer */
+    SafeVertex = ExAllocatePoolWithTag(PagedPool, cbVertex + cbMesh, TAG_SHAPE);
+    if(!SafeVertex)
     {
-        DC_UnlockDc(dc);
-        SetLastWin32Error(ERROR_NOT_ENOUGH_MEMORY);
+        EngSetLastError(ERROR_NOT_ENOUGH_MEMORY);
         return FALSE;
     }
 
-    SafeMesh = (PTRIVERTEX)(SafeVertex + uVertex);
+    SafeMesh = (PVOID)((ULONG_PTR)SafeVertex + cbVertex);
 
+    /* Copy the parameters to kernel mode */
     _SEH2_TRY
     {
-        /* pointers were already probed! */
-        RtlCopyMemory(SafeVertex,
-                      pVertex,
-                      uVertex * sizeof(TRIVERTEX));
-        RtlCopyMemory(SafeMesh,
-                      pMesh,
-                      SizeMesh);
+        ProbeForRead(pVertex, cbVertex, 1);
+        ProbeForRead(pMesh, cbMesh, 1);
+        RtlCopyMemory(SafeVertex, pVertex, cbVertex);
+        RtlCopyMemory(SafeMesh, pMesh, cbMesh);
     }
     _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
     {
-        Status = _SEH2_GetExceptionCode();
-    }
-    _SEH2_END;
-
-    if (!NT_SUCCESS(Status))
-    {
-        DC_UnlockDc(dc);
         ExFreePoolWithTag(SafeVertex, TAG_SHAPE);
-        SetLastNtError(Status);
-        return FALSE;
+        SetLastNtError(_SEH2_GetExceptionCode());
+        _SEH2_YIELD(return FALSE;)
     }
+    _SEH2_END;
 
-    DC_vPrepareDCsForBlit(dc, dc->rosdc.CombinedClip->rclBounds,
-                              NULL, dc->rosdc.CombinedClip->rclBounds);
-
-    Ret = IntGdiGradientFill(dc, SafeVertex, uVertex, SafeMesh, uMesh, ulMode);
+    /* Call the internal function */
+    bRet = GreGradientFill(hdc, SafeVertex, nVertex, SafeMesh, nMesh, ulMode);
 
-    DC_vFinishBlit(dc, NULL) ;
-    DC_UnlockDc(dc);
-    ExFreePool(SafeVertex);
-    return Ret;
+    /* Cleanup and return result */
+    ExFreePoolWithTag(SafeVertex, TAG_SHAPE);
+    return bRet;
 }
 
 BOOL APIENTRY
@@ -1089,8 +1062,6 @@ NtGdiExtFloodFill(
     PDC dc;
     PDC_ATTR   pdcattr;
     SURFACE    *psurf = NULL;
-    HPALETTE   hpal;
-    PPALETTE   ppal;
     EXLATEOBJ  exlo;
     BOOL       Ret = FALSE;
     RECTL      DestRect;
@@ -1100,7 +1071,7 @@ NtGdiExtFloodFill(
     dc = DC_LockDc(hDC);
     if (!dc)
     {
-        SetLastWin32Error(ERROR_INVALID_HANDLE);
+        EngSetLastError(ERROR_INVALID_HANDLE);
         return FALSE;
     }
     if (dc->dctype == DC_TYPE_INFO)
@@ -1135,11 +1106,7 @@ NtGdiExtFloodFill(
         goto cleanup;
     }
 
-    hpal = dc->dclevel.pSurface->hDIBPalette;
-    if (!hpal) hpal = pPrimarySurface->devinfo.hpalDefault;
-    ppal = PALETTE_ShareLockPalette(hpal);
-
-    EXLATEOBJ_vInitialize(&exlo, &gpalRGB, ppal, 0, 0xffffff, 0);
+    EXLATEOBJ_vInitialize(&exlo, &gpalRGB, psurf->ppal, 0, 0xffffff, 0);
 
     /* Only solid fills supported for now
      * How to support pattern brushes and non standard surfaces (not offering dib functions):
@@ -1149,7 +1116,6 @@ NtGdiExtFloodFill(
     Ret = DIB_XXBPP_FloodFillSolid(&psurf->SurfObj, &dc->eboFill.BrushObject, &DestRect, &Pt, ConvColor, FillType);
 
     EXLATEOBJ_vCleanup(&exlo);
-    PALETTE_ShareUnlockPalette(ppal);
 
 cleanup:
     DC_UnlockDc(dc);