[CMAKE]
[reactos.git] / subsystems / win32 / win32k / eng / surface.c
index db5d380..fb59ff5 100644 (file)
@@ -37,28 +37,19 @@ gajBitsPerFormat[11] =
 };
 
 
-ULONG FASTCALL BitmapFormat(WORD Bits, DWORD Compression)
+ULONG FASTCALL BitmapFormat(ULONG cBits, ULONG iCompression)
 {
-    switch (Compression)
+    switch (iCompression)
     {
         case BI_RGB:
             /* Fall through */
         case BI_BITFIELDS:
-            switch (Bits)
-            {
-                case 1:
-                    return BMF_1BPP;
-                case 4:
-                    return BMF_4BPP;
-                case 8:
-                    return BMF_8BPP;
-                case 16:
-                    return BMF_16BPP;
-                case 24:
-                    return BMF_24BPP;
-                case 32:
-                    return BMF_32BPP;
-            }
+            if (cBits <= 1) return BMF_1BPP;
+            if (cBits <= 4) return BMF_4BPP;
+            if (cBits <= 8) return BMF_8BPP;
+            if (cBits <= 16) return BMF_16BPP;
+            if (cBits <= 24) return BMF_24BPP;
+            if (cBits <= 32) return BMF_32BPP;
             return 0;
 
         case BI_RLE4:
@@ -140,45 +131,6 @@ SURFACE_Cleanup(PVOID ObjectBody)
     return TRUE;
 }
 
-static
-void
-SURFACE_vSetDefaultPalette(
-    PSURFACE psurfBmp)
-{
-    ULONG cBitsPixel = BitsPerFormat(psurfBmp->SurfObj.iBitmapFormat);
-
-    /* Find a suitable palette for this bitmap
-     * Increment internal objects share count
-     * so we can call PALETTE_ShareUnlockPalette
-     * or GDIOBJ_IncrementShareCount safely */
-    switch(cBitsPixel)
-    {
-        case 1:
-            psurfBmp->ppal = &gpalMono;
-            GDIOBJ_IncrementShareCount(&gpalMono.BaseObject);
-            break;
-        case 4:
-        case 8:
-            psurfBmp->ppal = PALETTE_ShareLockPalette(StockObjects[DEFAULT_PALETTE]);
-            break;
-        case 15:
-            psurfBmp->ppal = &gpalRGB555;
-            GDIOBJ_IncrementShareCount(&gpalRGB555.BaseObject);
-            break;
-        case 16:
-            psurfBmp->ppal = &gpalRGB565;
-            GDIOBJ_IncrementShareCount(&gpalRGB565.BaseObject);
-            break;
-        case 24:
-        case 32:
-            psurfBmp->ppal = &gpalRGB;
-            GDIOBJ_IncrementShareCount((POBJ)&gpalRGB);
-            break;
-        default:
-            DPRINT1("Could not determine palette for bit depth %u.\n", cBitsPixel);
-            break;
-    }
-}
 
 PSURFACE
 NTAPI
@@ -190,9 +142,16 @@ SURFACE_AllocSurface(
 {
     PSURFACE psurf;
     SURFOBJ *pso;
-    
+
+    /* Verify format */
+    if (iFormat < BMF_1BPP || iFormat > BMF_PNG)
+    {
+        DPRINT1("Invalid bitmap format: %ld\n", iFormat);
+        return NULL;
+    }
+
     /* Allocate a SURFACE object */
-    psurf = (PSURFACE)GDIOBJ_AllocObjWithHandle(GDI_OBJECT_TYPE_BITMAP);
+    psurf = (PSURFACE)GDIOBJ_AllocObjWithHandle(GDI_OBJECT_TYPE_BITMAP, sizeof(SURFACE));
 
     if (psurf)
     {
@@ -205,7 +164,9 @@ SURFACE_AllocSurface(
         pso->iType = iType;
         pso->iUniq = InterlockedIncrement((PLONG)&giUniqueSurface);
 
-        SURFACE_vSetDefaultPalette(psurf);
+        /* Assign a default palette and increment its reference count */
+        psurf->ppal = appalSurfaceDefault[iFormat];
+        GDIOBJ_vReferenceObjectByPointer(&psurf->ppal->BaseObject);
     }
 
     return psurf;
@@ -221,14 +182,25 @@ SURFACE_bSetBitmapBits(
 {
     SURFOBJ *pso = &psurf->SurfObj;
     PVOID pvSection;
+    UCHAR cBitsPixel;
 
     /* Only bitmaps can have bits */
     ASSERT(psurf->SurfObj.iType == STYPE_BITMAP);
 
-    /* If no width is given, calculate it */
-    if (!ulWidth)
-        ulWidth = DIB_GetDIBWidthBytes(pso->sizlBitmap.cx, 
-                                       BitsPerFormat(pso->iBitmapFormat));
+    /* Get bits per pixel from the format */
+    cBitsPixel = gajBitsPerFormat[pso->iBitmapFormat];
+
+    /* Is a width in bytes given? */
+    if (ulWidth)
+    {
+        /* Align the width (Windows compatibility, drivers expect that) */
+        ulWidth = WIDTH_BYTES_ALIGN32((ulWidth << 3) / cBitsPixel, cBitsPixel);
+    }
+       else
+       {
+        /* Calculate width from the bitmap width in pixels */
+        ulWidth = WIDTH_BYTES_ALIGN32(pso->sizlBitmap.cx, cBitsPixel);
+       }
 
     /* Calculate the bitmap size in bytes */
     pso->cjBits = ulWidth * pso->sizlBitmap.cy;
@@ -255,7 +227,7 @@ SURFACE_bSetBitmapBits(
                                         (fjBitmap & BMF_NOZEROINIT) ?
                                                 0 : FL_ZERO_MEMORY,
                                         pso->cjBits, TAG_DIB);
-            
+
             /* Free the section already, but keep the mapping */
             if (pvBits) EngFreeSectionMem(pvSection, NULL);
         }
@@ -313,12 +285,12 @@ EngCreateBitmap(
     {
         /* Bail out if that failed */
         DPRINT1("SURFACE_bSetBitmapBits failed.\n");
-        SURFACE_FreeSurfaceByHandle(hbmp);
+        GDIOBJ_vDeleteObject(&psurf->BaseObject);
         return NULL;
     }
 
     /* Set public ownership */
-    GDIOBJ_SetOwnership(hbmp, NULL);
+    GDIOBJ_vSetObjectOwner(&psurf->BaseObject, GDI_OBJ_HMGR_PUBLIC);
 
     /* Unlock the surface and return */
     SURFACE_UnlockSurface(psurf);
@@ -352,7 +324,7 @@ EngCreateDeviceBitmap(
     hbmp = (HBITMAP)psurf->SurfObj.hsurf;
 
     /* Set public ownership */
-    GDIOBJ_SetOwnership(hbmp, NULL);
+    GDIOBJ_vSetObjectOwner(&psurf->BaseObject, GDI_OBJ_HMGR_PUBLIC);
 
     /* Unlock the surface and return */
     SURFACE_UnlockSurface(psurf);
@@ -383,7 +355,7 @@ EngCreateDeviceSurface(
     hsurf = psurf->SurfObj.hsurf;
 
     /* Set public ownership */
-    GDIOBJ_SetOwnership(hsurf, NULL);
+    GDIOBJ_vSetObjectOwner(&psurf->BaseObject, GDI_OBJ_HMGR_PUBLIC);
 
     /* Unlock the surface and return */
     SURFACE_UnlockSurface(psurf);
@@ -404,7 +376,7 @@ EngAssociateSurface(
     ppdev = (PDEVOBJ*)hdev;
 
     /* Lock the surface */
-    psurf = SURFACE_LockSurface(hsurf);
+    psurf = SURFACE_ShareLockSurface(hsurf);
     if (!psurf)
     {
         return FALSE;
@@ -422,7 +394,7 @@ EngAssociateSurface(
     /* Get palette */
     psurf->ppal = PALETTE_ShareLockPalette(ppdev->devinfo.hpalDefault);
 
-    SURFACE_UnlockSurface(psurf);
+    SURFACE_ShareUnlockSurface(psurf);
 
     return TRUE;
 }
@@ -443,7 +415,7 @@ EngModifySurface(
     PSURFACE psurf;
     PDEVOBJ* ppdev;
 
-    psurf = SURFACE_LockSurface(hsurf);
+    psurf = SURFACE_ShareLockSurface(hsurf);
     if (psurf == NULL)
     {
         return FALSE;
@@ -466,7 +438,7 @@ EngModifySurface(
     /* Get palette */
     psurf->ppal = PALETTE_ShareLockPalette(ppdev->devinfo.hpalDefault);
 
-    SURFACE_UnlockSurface(psurf);
+    SURFACE_ShareUnlockSurface(psurf);
 
     return TRUE;
 }
@@ -476,8 +448,16 @@ BOOL
 APIENTRY
 EngDeleteSurface(IN HSURF hsurf)
 {
-    GDIOBJ_SetOwnership(hsurf, PsGetCurrentProcess());
-    SURFACE_FreeSurfaceByHandle(hsurf);
+    PSURFACE psurf;
+
+    psurf = SURFACE_ShareLockSurface(hsurf);
+    if (!psurf)
+    {
+        DPRINT1("Could not reference surface to delete\n");
+        return FALSE;
+    }
+
+    GDIOBJ_vDeleteObject(&psurf->BaseObject);
     return TRUE;
 }
 
@@ -507,19 +487,17 @@ SURFOBJ *
 APIENTRY
 EngLockSurface(IN HSURF hsurf)
 {
-    SURFACE *psurf = GDIOBJ_ShareLockObj(hsurf, GDI_OBJECT_TYPE_BITMAP);
+    SURFACE *psurf = SURFACE_ShareLockSurface(hsurf);
 
-    if (psurf != NULL)
-        return &psurf->SurfObj;
-
-    return NULL;
+    return psurf ? &psurf->SurfObj : NULL;
 }
 
 VOID
 APIENTRY
 NtGdiEngUnlockSurface(IN SURFOBJ *pso)
 {
-    EngUnlockSurface(pso);
+    UNIMPLEMENTED;
+    ASSERT(FALSE);
 }
 
 VOID
@@ -529,78 +507,8 @@ EngUnlockSurface(IN SURFOBJ *pso)
     if (pso != NULL)
     {
         SURFACE *psurf = CONTAINING_RECORD(pso, SURFACE, SurfObj);
-        GDIOBJ_ShareUnlockObjByPtr((POBJ)psurf);
-    }
-}
-
-HBITMAP
-FASTCALL
-IntCreateBitmap(
-    IN SIZEL Size,
-    IN LONG Width,
-    IN ULONG Format,
-    IN ULONG Flags,
-    IN PVOID Bits)
-{
-    HBITMAP hbmp;
-    SURFOBJ *pso;
-    PSURFACE psurf;
-    PVOID UncompressedBits;
-    ULONG UncompressedFormat;
-    
-    if (Format == 0)
-        return 0;
-
-    /* Allocate a surface */
-    psurf = SURFACE_AllocSurface(STYPE_BITMAP, Size.cx, Size.cy, Format);
-    if (!psurf)
-    {
-        DPRINT1("SURFACE_AllocSurface failed.\n");
-        return NULL;
-    }
-
-    /* Get the handle for the bitmap and the surfobj */
-    hbmp = (HBITMAP)psurf->SurfObj.hsurf;
-    pso = &psurf->SurfObj;
-
-    if (Format == BMF_4RLE)
-    {
-        pso->lDelta = DIB_GetDIBWidthBytes(Size.cx, BitsPerFormat(BMF_4BPP));
-        pso->cjBits = pso->lDelta * Size.cy;
-        UncompressedFormat = BMF_4BPP;
-        UncompressedBits = EngAllocMem(FL_ZERO_MEMORY, pso->cjBits, TAG_DIB);
-        Decompress4bpp(Size, (BYTE *)Bits, (BYTE *)UncompressedBits, pso->lDelta);
-        Flags |= BMF_RLE_HACK;
+        SURFACE_ShareUnlockSurface(psurf);
     }
-    else if (Format == BMF_8RLE)
-    {
-        pso->lDelta = DIB_GetDIBWidthBytes(Size.cx, BitsPerFormat(BMF_8BPP));
-        pso->cjBits = pso->lDelta * Size.cy;
-        UncompressedFormat = BMF_8BPP;
-        UncompressedBits = EngAllocMem(FL_ZERO_MEMORY, pso->cjBits, TAG_DIB);
-        Decompress8bpp(Size, (BYTE *)Bits, (BYTE *)UncompressedBits, pso->lDelta);
-        Flags |= BMF_RLE_HACK;
-    }
-    else
-    {
-        pso->lDelta = abs(Width);
-        pso->cjBits = pso->lDelta * Size.cy;
-        UncompressedBits = Bits;
-        UncompressedFormat = Format;
-    }
-
-    /* Set the bitmap bits */
-    if (!SURFACE_bSetBitmapBits(psurf, Flags, Width, UncompressedBits))
-    {
-        /* Bail out if that failed */
-        DPRINT1("SURFACE_bSetBitmapBits failed.\n");
-        SURFACE_FreeSurfaceByHandle(hbmp);
-        return NULL;
-    }
-
-    /* Unlock the surface and return */
-    SURFACE_UnlockSurface(psurf);
-    return hbmp;
 }
 
 /* EOF */