[YAROTOWS] Reintegrate the branch. For a brighter future.
[reactos.git] / reactos / subsystems / win32 / win32k / eng / surface.c
index bda6561..622b99d 100644 (file)
@@ -1,27 +1,10 @@
-/*
- *  ReactOS W32 Subsystem
- *  Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
 /*
  * COPYRIGHT:         See COPYING in the top level directory
  * PROJECT:           ReactOS kernel
  * PURPOSE:           GDI Driver Surace Functions
  * FILE:              subsys/win32k/eng/surface.c
- * PROGRAMER:         Jason Filby
+ * PROGRAMERS:        Jason Filby
+ *                    Timo Kreuzer
  * REVISION HISTORY:
  *                 3/7/1999: Created
  *                 9/11/2000: Updated to handle real pixel packed bitmaps (UPDATE TO DATE COMPLETED)
  *   refer to \test\microwin\src\engine\devdraw.c for info on correct pixel plotting for various formats
  */
 
-#include <w32k.h>
+#include <win32k.h>
 
 #define NDEBUG
 #include <debug.h>
 
-enum Rle_EscapeCodes
-{
-    RLE_EOL   = 0, /* End of line */
-    RLE_END   = 1, /* End of bitmap */
-    RLE_DELTA = 2  /* Delta */
-};
+ULONG giUniqueSurface = 0;
 
-INT FASTCALL BitsPerFormat(ULONG Format)
+UCHAR
+gajBitsPerFormat[11] =
 {
-    switch (Format)
-    {
-        case BMF_1BPP:
-            return 1;
-
-        case BMF_4BPP:
-            /* Fall through */
-        case BMF_4RLE:
-            return 4;
-
-        case BMF_8BPP:
-            /* Fall through */
-        case BMF_8RLE:
-            return 8;
-
-        case BMF_16BPP:
-            return 16;
-
-        case BMF_24BPP:
-            return 24;
-
-        case BMF_32BPP:
-            return 32;
+    0, /*  0: unused */
+    1, /*  1: BMF_1BPP */
+    4, /*  2: BMF_4BPP */
+    8, /*  3: BMF_8BPP */
+   16, /*  4: BMF_16BPP */
+   24, /*  5: BMF_24BPP */
+   32, /*  6: BMF_32BPP */
+    4, /*  7: BMF_4RLE */
+    8, /*  8: BMF_8RLE */
+    0, /*  9: BMF_JPEG */
+    0, /* 10: BMF_PNG */
+};
 
-        default:
-            return 0;
-    }
-}
 
 ULONG FASTCALL BitmapFormat(WORD Bits, DWORD Compression)
 {
@@ -108,409 +72,344 @@ ULONG FASTCALL BitmapFormat(WORD Bits, DWORD Compression)
     }
 }
 
-BOOL INTERNAL_CALL
-BITMAPOBJ_InitBitsLock(BITMAPOBJ *BitmapObj)
-{
-    BitmapObj->BitsLock = ExAllocatePoolWithTag(NonPagedPool,
-                          sizeof(FAST_MUTEX),
-                          TAG_BITMAPOBJ);
-    if (NULL == BitmapObj->BitsLock)
-    {
-        return FALSE;
-    }
-
-    ExInitializeFastMutex(BitmapObj->BitsLock);
-
-    return TRUE;
-}
-
-void INTERNAL_CALL
-BITMAPOBJ_CleanupBitsLock(BITMAPOBJ *BitmapObj)
-{
-    if (NULL != BitmapObj->BitsLock)
-    {
-        ExFreePoolWithTag(BitmapObj->BitsLock, TAG_BITMAPOBJ);
-        BitmapObj->BitsLock = NULL;
-    }
-}
-
-
-/*
- * @implemented
- */
-HBITMAP STDCALL
-EngCreateDeviceBitmap(IN DHSURF dhsurf,
-                      IN SIZEL Size,
-                      IN ULONG Format)
+BOOL
+INTERNAL_CALL
+SURFACE_Cleanup(PVOID ObjectBody)
 {
-    HBITMAP NewBitmap;
-    SURFOBJ *SurfObj;
+    PSURFACE psurf = (PSURFACE)ObjectBody;
+    PVOID pvBits = psurf->SurfObj.pvBits;
+    NTSTATUS Status;
 
-    NewBitmap = EngCreateBitmap(Size, DIB_GetDIBWidthBytes(Size.cx, BitsPerFormat(Format)), Format, 0, NULL);
-    if (!NewBitmap)
+    /* Check if the surface has bits */
+    if (pvBits)
     {
-        DPRINT1("EngCreateBitmap failed\n");
-        return 0;
-    }
+        /* Only bitmaps can have bits */
+        ASSERT(psurf->SurfObj.iType == STYPE_BITMAP);
 
-    SurfObj = EngLockSurface((HSURF)NewBitmap);
-    SurfObj->dhsurf = dhsurf;
-    EngUnlockSurface(SurfObj);
+        /* Check if it is a DIB section */
+        if (psurf->hDIBSection)
+        {
+            /* Unsecure the memory */
+            EngUnsecureMem(psurf->hSecure);
 
-    return NewBitmap;
-}
+            /* Calculate the real start of the section */
+            pvBits = (PVOID)((ULONG_PTR)pvBits - psurf->dwOffset);
 
-VOID Decompress4bpp(SIZEL Size, BYTE *CompressedBits, BYTE *UncompressedBits, LONG Delta)
-{
-    int x = 0;
-    int y = Size.cy - 1;
-    int c;
-    int length;
-    int width = ((Size.cx+1)/2);
-    int height = Size.cy - 1;
-    BYTE *begin = CompressedBits;
-    BYTE *bits = CompressedBits;
-    BYTE *temp;
-    while (y >= 0)
-    {
-        length = *bits++ / 2;
-        if (length)
-        {
-            c = *bits++;
-            while (length--)
+            /* Unmap the section */
+            Status = MmUnmapViewOfSection(PsGetCurrentProcess(), pvBits);
+            if (!NT_SUCCESS(Status))
             {
-                if (x >= width) break;
-                temp = UncompressedBits + (((height - y) * Delta) + x);
-                x++;
-                *temp = c;
+                DPRINT1("Could not unmap section view!\n");
+                // Should we BugCheck here?
+                ASSERT(FALSE);
             }
         }
-        else
+        else if (psurf->SurfObj.fjBitmap & BMF_USERMEM)
         {
-            length = *bits++;
-            switch (length)
-            {
-                case RLE_EOL:
-                    x = 0;
-                    y--;
-                    break;
-                case RLE_END:
-                    return;
-                case RLE_DELTA:
-                    x += (*bits++)/2;
-                    y -= (*bits++)/2;
-                    break;
-                default:
-                    length /= 2;
-                    while (length--)
-                    {
-                        c = *bits++;
-                        if (x < width)
-                        {
-                            temp = UncompressedBits + (((height - y) * Delta) + x);
-                            x++;
-                            *temp = c;
-                        }
-                    }
-                    if ((bits - begin) & 1)
-                    {
-                        bits++;
-                    }
-            }
+            /* Bitmap was allocated from usermode memory */
+            EngFreeUserMem(pvBits);
         }
-    }
-}
-
-VOID Decompress8bpp(SIZEL Size, BYTE *CompressedBits, BYTE *UncompressedBits, LONG Delta)
-{
-    int x = 0;
-    int y = Size.cy - 1;
-    int c;
-    int length;
-    int width = Size.cx;
-    int height = Size.cy - 1;
-    BYTE *begin = CompressedBits;
-    BYTE *bits = CompressedBits;
-    BYTE *temp;
-    while (y >= 0)
-    {
-        length = *bits++;
-        if (length)
+        else if (psurf->SurfObj.fjBitmap & BMF_KMSECTION)
         {
-            c = *bits++;
-            while (length--)
+            /* Bitmap was allocated from a kernel section */
+            if (!EngFreeSectionMem(NULL, pvBits))
             {
-                if (x >= width) break;
-                temp = UncompressedBits + (((height - y) * Delta) + x);
-                x++;
-                *temp = c;
+                DPRINT1("EngFreeSectionMem failed for %p!\n", pvBits);
+                // Should we BugCheck here?
+                ASSERT(FALSE);
             }
         }
+        else if (psurf->SurfObj.fjBitmap & BMF_RLE_HACK)
+        {
+            /* HACK: Free RLE decompressed bits */
+            EngFreeMem(pvBits);
+        }
         else
         {
-            length = *bits++;
-            switch (length)
-            {
-                case RLE_EOL:
-                    x = 0;
-                    y--;
-                    break;
-                case RLE_END:
-                    return;
-                case RLE_DELTA:
-                    x += *bits++;
-                    y -= *bits++;
-                    break;
-                default:
-                    while (length--)
-                    {
-                        c = *bits++;
-                        if (x < width)
-                        {
-                            temp = UncompressedBits + (((height - y) * Delta) + x);
-                            x++;
-                            *temp = c;
-                        }
-                    }
-                    if ((bits - begin) & 1)
-                    {
-                        bits++;
-                    }
-            }
+            /* There should be nothing to free */
+            ASSERT(psurf->SurfObj.fjBitmap & BMF_DONT_FREE);
         }
     }
+
+    /* Free palette */
+    if(psurf->ppal)
+    {
+        PALETTE_ShareUnlockPalette(psurf->ppal);
+    }
+
+    return TRUE;
 }
 
-HBITMAP FASTCALL
-IntCreateBitmap(IN SIZEL Size,
-                IN LONG Width,
-                IN ULONG Format,
-                IN ULONG Flags,
-                IN PVOID Bits)
-{
-    HBITMAP NewBitmap;
-    SURFOBJ *SurfObj;
-    BITMAPOBJ *BitmapObj;
-    PVOID UncompressedBits;
-    ULONG UncompressedFormat;
 
-    if (Format == 0)
-        return 0;
+PSURFACE
+NTAPI
+SURFACE_AllocSurface(
+    IN ULONG iType,
+    IN ULONG cx,
+    IN ULONG cy,
+    IN ULONG iFormat)
+{
+    PSURFACE psurf;
+    SURFOBJ *pso;
 
-    BitmapObj = BITMAPOBJ_AllocBitmapWithHandle();
-    if (BitmapObj == NULL)
+    /* Verify format */
+    if (iFormat < BMF_1BPP || iFormat > BMF_PNG)
     {
-        return 0;
+        DPRINT1("Invalid bitmap format: %ld\n", iFormat);
+        return NULL;
     }
-    NewBitmap = BitmapObj->BaseObject.hHmgr;
 
-    if (! BITMAPOBJ_InitBitsLock(BitmapObj))
-    {
-        BITMAPOBJ_UnlockBitmap(BitmapObj);
-        BITMAPOBJ_FreeBitmapByHandle(NewBitmap);
-        return 0;
-    }
-    SurfObj = &BitmapObj->SurfObj;
+    /* Allocate a SURFACE object */
+    psurf = (PSURFACE)GDIOBJ_AllocObjWithHandle(GDI_OBJECT_TYPE_BITMAP);
 
-    if (Format == BMF_4RLE)
+    if (psurf)
     {
-        SurfObj->lDelta = DIB_GetDIBWidthBytes(Size.cx, BitsPerFormat(BMF_4BPP));
-        SurfObj->cjBits = SurfObj->lDelta * Size.cy;
-        UncompressedFormat = BMF_4BPP;
-        UncompressedBits = EngAllocMem(FL_ZERO_MEMORY, SurfObj->cjBits, 0);
-        Decompress4bpp(Size, (BYTE *)Bits, (BYTE *)UncompressedBits, SurfObj->lDelta);
+        /* Initialize the basic fields */
+        pso = &psurf->SurfObj;
+        pso->hsurf = psurf->BaseObject.hHmgr;
+        pso->sizlBitmap.cx = cx;
+        pso->sizlBitmap.cy = cy;
+        pso->iBitmapFormat = iFormat;
+        pso->iType = iType;
+        pso->iUniq = InterlockedIncrement((PLONG)&giUniqueSurface);
+
+        /* Assign a default palette and increment its reference count */
+        psurf->ppal = appalSurfaceDefault[iFormat];
+        GDIOBJ_IncrementShareCount(&psurf->ppal->BaseObject);
     }
-    else if (Format == BMF_8RLE)
-    {
-        SurfObj->lDelta = DIB_GetDIBWidthBytes(Size.cx, BitsPerFormat(BMF_8BPP));
-        SurfObj->cjBits = SurfObj->lDelta * Size.cy;
-        UncompressedFormat = BMF_8BPP;
-        UncompressedBits = EngAllocMem(FL_ZERO_MEMORY, SurfObj->cjBits, 0);
-        Decompress8bpp(Size, (BYTE *)Bits, (BYTE *)UncompressedBits, SurfObj->lDelta);
-    }
-    else
+
+    return psurf;
+}
+
+BOOL
+NTAPI
+SURFACE_bSetBitmapBits(
+    IN PSURFACE psurf,
+    IN USHORT fjBitmap,
+    IN ULONG ulWidth,
+    IN PVOID pvBits OPTIONAL)
+{
+    SURFOBJ *pso = &psurf->SurfObj;
+    PVOID pvSection;
+    UCHAR cBitsPixel;
+
+    /* Only bitmaps can have bits */
+    ASSERT(psurf->SurfObj.iType == STYPE_BITMAP);
+
+    /* Get bits per pixel from the format */
+    cBitsPixel = gajBitsPerFormat[pso->iBitmapFormat];
+
+    /* Is a width in bytes given? */
+    if (ulWidth)
     {
-        SurfObj->lDelta = abs(Width);
-        SurfObj->cjBits = SurfObj->lDelta * Size.cy;
-        UncompressedBits = Bits;
-        UncompressedFormat = Format;
+        /* 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;
 
-    if (UncompressedBits != NULL)
+    /* Did the caller provide bits? */
+    if (pvBits)
     {
-        SurfObj->pvBits = UncompressedBits;
+        /* Yes, so let him free it */
+        fjBitmap |= BMF_DONT_FREE;
     }
-    else
+    else if (pso->cjBits)
     {
-        if (SurfObj->cjBits == 0)
+        /* We must allocate memory, check what kind */
+        if (fjBitmap & BMF_USERMEM)
         {
-            SurfObj->pvBits = NULL;
+            /* User mode memory was requested */
+            pvBits = EngAllocUserMem(pso->cjBits, 0);
         }
         else
         {
-            if (0 != (Flags & BMF_USERMEM))
-            {
-                SurfObj->pvBits = EngAllocUserMem(SurfObj->cjBits, 0);
-            }
-            else
-            {
-                SurfObj->pvBits = EngAllocMem(0 != (Flags & BMF_NOZEROINIT) ?
-                                                  0 : FL_ZERO_MEMORY,
-                                              SurfObj->cjBits, 0);
-            }
-            if (SurfObj->pvBits == NULL)
-            {
-                BITMAPOBJ_UnlockBitmap(BitmapObj);
-                BITMAPOBJ_FreeBitmapByHandle(NewBitmap);
-                return 0;
-            }
+            /* Use a kernel mode section */
+            fjBitmap |= BMF_KMSECTION;
+            pvBits = EngAllocSectionMem(&pvSection,
+                                        (fjBitmap & BMF_NOZEROINIT) ?
+                                                0 : FL_ZERO_MEMORY,
+                                        pso->cjBits, TAG_DIB);
+
+            /* Free the section already, but keep the mapping */
+            if (pvBits) EngFreeSectionMem(pvSection, NULL);
         }
+
+        /* Check for failure */
+        if (!pvBits) return FALSE;
     }
 
-    if (0 == (Flags & BMF_TOPDOWN))
+    /* Set pvBits, pvScan0 and lDelta */
+    pso->pvBits = pvBits;
+    if (fjBitmap & BMF_TOPDOWN)
     {
-        SurfObj->pvScan0 = (PVOID) ((ULONG_PTR) SurfObj->pvBits + SurfObj->cjBits - SurfObj->lDelta);
-        SurfObj->lDelta = - SurfObj->lDelta;
+        /* Topdown is the normal way */
+        pso->pvScan0 = pso->pvBits;
+        pso->lDelta = ulWidth;
     }
     else
     {
-        SurfObj->pvScan0 = SurfObj->pvBits;
+        /* Inversed bitmap (bottom up) */
+        pso->pvScan0 = (PVOID)((ULONG_PTR)pso->pvBits + pso->cjBits - ulWidth);
+        pso->lDelta = -ulWidth;
     }
 
-    SurfObj->dhsurf = 0; /* device managed surface */
-    SurfObj->hsurf = (HSURF)NewBitmap;
-    SurfObj->dhpdev = NULL;
-    SurfObj->hdev = NULL;
-    SurfObj->sizlBitmap = Size;
-    SurfObj->iBitmapFormat = UncompressedFormat;
-    SurfObj->iType = STYPE_BITMAP;
-    SurfObj->fjBitmap = Flags & (BMF_TOPDOWN | BMF_NOZEROINIT);
-    SurfObj->iUniq = 0;
-
-    BitmapObj->flHooks = 0;
-    BitmapObj->flFlags = 0;
-    BitmapObj->dimension.cx = 0;
-    BitmapObj->dimension.cy = 0;
-    BitmapObj->dib = NULL;
-
-    BITMAPOBJ_UnlockBitmap(BitmapObj);
-
-    return NewBitmap;
+    pso->fjBitmap = fjBitmap;
+
+    /* Success */
+    return TRUE;
 }
 
-/*
- * @implemented
- */
-HBITMAP STDCALL
-EngCreateBitmap(IN SIZEL Size,
-                IN LONG Width,
-                IN ULONG Format,
-                IN ULONG Flags,
-                IN PVOID Bits)
+HBITMAP
+APIENTRY
+EngCreateBitmap(
+    IN SIZEL sizl,
+    IN LONG lWidth,
+    IN ULONG iFormat,
+    IN ULONG fl,
+    IN PVOID pvBits)
 {
-    HBITMAP NewBitmap;
+    PSURFACE psurf;
+    HBITMAP hbmp;
 
-    NewBitmap = IntCreateBitmap(Size, Width, Format, Flags, Bits);
-    if ( !NewBitmap )
-        return 0;
+    /* Allocate a surface */
+    psurf = SURFACE_AllocSurface(STYPE_BITMAP, sizl.cx, sizl.cy, iFormat);
+    if (!psurf)
+    {
+        DPRINT1("SURFACE_AllocSurface failed.\n");
+        return NULL;
+    }
+
+    /* Get the handle for the bitmap */
+    hbmp = (HBITMAP)psurf->SurfObj.hsurf;
 
-    GDIOBJ_SetOwnership(NewBitmap, NULL);
+    /* Set the bitmap bits */
+    if (!SURFACE_bSetBitmapBits(psurf, fl, lWidth, pvBits))
+    {
+        /* Bail out if that failed */
+        DPRINT1("SURFACE_bSetBitmapBits failed.\n");
+        SURFACE_FreeSurfaceByHandle(hbmp);
+        return NULL;
+    }
+
+    /* Set public ownership */
+    GDIOBJ_SetOwnership(hbmp, NULL);
 
-    return NewBitmap;
+    /* Unlock the surface and return */
+    SURFACE_UnlockSurface(psurf);
+    return hbmp;
 }
 
 /*
- * @unimplemented
+ * @implemented
  */
-HSURF STDCALL
-EngCreateDeviceSurface(IN DHSURF dhsurf,
-                       IN SIZEL Size,
-                       IN ULONG Format)
+HBITMAP
+APIENTRY
+EngCreateDeviceBitmap(
+    IN DHSURF dhsurf,
+    IN SIZEL sizl,
+    IN ULONG iFormat)
 {
-    HSURF NewSurface;
-    SURFOBJ *SurfObj;
-    BITMAPOBJ *BitmapObj;
+    PSURFACE psurf;
+    HBITMAP hbmp;
 
-    BitmapObj = BITMAPOBJ_AllocBitmapWithHandle();
-    if (!BitmapObj)
+    /* Allocate a surface */
+    psurf = SURFACE_AllocSurface(STYPE_DEVBITMAP, sizl.cx, sizl.cy, iFormat);
+    if (!psurf)
     {
         return 0;
     }
 
-    NewSurface = BitmapObj->BaseObject.hHmgr;
-    GDIOBJ_SetOwnership(NewSurface, NULL);
+    /* Set the device handle */
+    psurf->SurfObj.dhsurf = dhsurf;
 
-    if (!BITMAPOBJ_InitBitsLock(BitmapObj))
-    {
-        BITMAPOBJ_UnlockBitmap(BitmapObj);
-        BITMAPOBJ_FreeBitmapByHandle(NewSurface);
-        return 0;
-    }
-    SurfObj = &BitmapObj->SurfObj;
-
-    SurfObj->dhsurf = dhsurf;
-    SurfObj->hsurf = NewSurface;
-    SurfObj->sizlBitmap = Size;
-    SurfObj->iBitmapFormat = Format;
-    SurfObj->lDelta = DIB_GetDIBWidthBytes(Size.cx, BitsPerFormat(Format));
-    SurfObj->iType = STYPE_DEVICE;
-    SurfObj->iUniq = 0;
-
-    BitmapObj->flHooks = 0;
+    /* Get the handle for the bitmap */
+    hbmp = (HBITMAP)psurf->SurfObj.hsurf;
 
-    BITMAPOBJ_UnlockBitmap(BitmapObj);
+    /* Set public ownership */
+    GDIOBJ_SetOwnership(hbmp, NULL);
 
-    return NewSurface;
+    /* Unlock the surface and return */
+    SURFACE_UnlockSurface(psurf);
+    return hbmp;
 }
 
-PFN FASTCALL DriverFunction(DRVENABLEDATA *DED, ULONG DriverFunc)
+HSURF
+APIENTRY
+EngCreateDeviceSurface(
+    IN DHSURF dhsurf,
+    IN SIZEL sizl,
+    IN ULONG iFormat)
 {
-    ULONG i;
+    PSURFACE psurf;
+    HSURF hsurf;
 
-    for (i=0; i<DED->c; i++)
+    /* Allocate a surface */
+    psurf = SURFACE_AllocSurface(STYPE_DEVICE, sizl.cx, sizl.cy, iFormat);
+    if (!psurf)
     {
-        if (DED->pdrvfn[i].iFunc == DriverFunc)
-        {
-            return DED->pdrvfn[i].pfn;
-        }
+        return 0;
     }
-    return NULL;
+
+    /* Set the device handle */
+    psurf->SurfObj.dhsurf = dhsurf;
+
+    /* Get the handle for the surface */
+    hsurf = psurf->SurfObj.hsurf;
+
+    /* Set public ownership */
+    GDIOBJ_SetOwnership(hsurf, NULL);
+
+    /* Unlock the surface and return */
+    SURFACE_UnlockSurface(psurf);
+    return hsurf;
 }
 
-/*
- * @implemented
- */
-BOOL STDCALL
-EngAssociateSurface(IN HSURF Surface,
-                    IN HDEV Dev,
-                    IN ULONG Hooks)
+BOOL
+APIENTRY
+EngAssociateSurface(
+    IN HSURF hsurf,
+    IN HDEV hdev,
+    IN FLONG flHooks)
 {
-    SURFOBJ *SurfObj;
-    BITMAPOBJ *BitmapObj;
-    GDIDEVICE* Device;
+    SURFOBJ *pso;
+    PSURFACE psurf;
+    PDEVOBJ* ppdev;
 
-    Device = (GDIDEVICE*)Dev;
+    ppdev = (PDEVOBJ*)hdev;
 
-    BitmapObj = BITMAPOBJ_LockBitmap(Surface);
-    ASSERT(BitmapObj);
-    SurfObj = &BitmapObj->SurfObj;
+    /* Lock the surface */
+    psurf = SURFACE_LockSurface(hsurf);
+    if (!psurf)
+    {
+        return FALSE;
+    }
+    pso = &psurf->SurfObj;
 
     /* Associate the hdev */
-    SurfObj->hdev = Dev;
-    SurfObj->dhpdev = Device->hPDev;
+    pso->hdev = hdev;
+    pso->dhpdev = ppdev->dhpdev;
 
     /* Hook up specified functions */
-    BitmapObj->flHooks = Hooks;
+    psurf->flags &= ~HOOK_FLAGS;
+    psurf->flags |= (flHooks & HOOK_FLAGS);
 
-    BITMAPOBJ_UnlockBitmap(BitmapObj);
+    /* Get palette */
+    psurf->ppal = PALETTE_ShareLockPalette(ppdev->devinfo.hpalDefault);
+
+    SURFACE_UnlockSurface(psurf);
 
     return TRUE;
 }
 
-/*
- * @implemented
- */
-BOOL STDCALL
+BOOL
+APIENTRY
 EngModifySurface(
     IN HSURF hsurf,
     IN HDEV hdev,
@@ -522,103 +421,97 @@ EngModifySurface(
     IN VOID *pvReserved)
 {
     SURFOBJ *pso;
+    PSURFACE psurf;
+    PDEVOBJ* ppdev;
 
-    pso = EngLockSurface(hsurf);
-    if (pso == NULL)
-    {
-        return FALSE;
-    }
-
-    if (!EngAssociateSurface(hsurf, hdev, flHooks))
+    psurf = SURFACE_LockSurface(hsurf);
+    if (psurf == NULL)
     {
-        EngUnlockSurface(pso);
-
         return FALSE;
     }
 
+    ppdev = (PDEVOBJ*)hdev;
+    pso = &psurf->SurfObj;
     pso->dhsurf = dhsurf;
     pso->lDelta = lDelta;
     pso->pvScan0 = pvScan0;
 
-    EngUnlockSurface(pso);
+    /* Associate the hdev */
+    pso->hdev = hdev;
+    pso->dhpdev = ppdev->dhpdev;
+
+    /* Hook up specified functions */
+    psurf->flags &= ~HOOK_FLAGS;
+    psurf->flags |= (flHooks & HOOK_FLAGS);
+
+    /* Get palette */
+    psurf->ppal = PALETTE_ShareLockPalette(ppdev->devinfo.hpalDefault);
+
+    SURFACE_UnlockSurface(psurf);
 
     return TRUE;
 }
 
-/*
- * @implemented
- */
-BOOL STDCALL
-EngDeleteSurface(IN HSURF Surface)
+
+BOOL
+APIENTRY
+EngDeleteSurface(IN HSURF hsurf)
 {
-    GDIOBJ_SetOwnership(Surface, PsGetCurrentProcess());
-    BITMAPOBJ_FreeBitmapByHandle(Surface);
+    GDIOBJ_SetOwnership(hsurf, PsGetCurrentProcess());
+    SURFACE_FreeSurfaceByHandle(hsurf);
     return TRUE;
 }
 
-/*
- * @implemented
- */
-BOOL STDCALL
-EngEraseSurface(SURFOBJ *Surface,
-                RECTL *Rect,
-                ULONG iColor)
+BOOL
+APIENTRY
+EngEraseSurface(
+    SURFOBJ *pso,
+    RECTL *prcl,
+    ULONG iColor)
 {
-    ASSERT(Surface);
-    ASSERT(Rect);
-    return FillSolid(Surface, Rect, iColor);
+    ASSERT(pso);
+    ASSERT(prcl);
+    return FillSolid(pso, prcl, iColor);
 }
 
-#define GDIBdyToHdr(body)                                                      \
-  ((PGDIOBJHDR)(body) - 1)
-
-
 /*
  * @implemented
  */
-SURFOBJ * STDCALL
-NtGdiEngLockSurface(IN HSURF Surface)
+SURFOBJ * APIENTRY
+NtGdiEngLockSurface(IN HSURF hsurf)
 {
-    return EngLockSurface(Surface);
+    return EngLockSurface(hsurf);
 }
 
 
-/*
- * @implemented
- */
-SURFOBJ * STDCALL
-EngLockSurface(IN HSURF Surface)
+SURFOBJ *
+APIENTRY
+EngLockSurface(IN HSURF hsurf)
 {
-    BITMAPOBJ *bmp = GDIOBJ_ShareLockObj(Surface, GDI_OBJECT_TYPE_BITMAP);
+    SURFACE *psurf = GDIOBJ_ShareLockObj(hsurf, GDI_OBJECT_TYPE_BITMAP);
 
-    if (bmp != NULL)
-        return &bmp->SurfObj;
+    if (psurf != NULL)
+        return &psurf->SurfObj;
 
     return NULL;
 }
 
-
-/*
- * @implemented
- */
-VOID STDCALL
-NtGdiEngUnlockSurface(IN SURFOBJ *Surface)
+VOID
+APIENTRY
+NtGdiEngUnlockSurface(IN SURFOBJ *pso)
 {
-    EngUnlockSurface(Surface);
+    EngUnlockSurface(pso);
 }
 
-/*
- * @implemented
- */
-VOID STDCALL
-EngUnlockSurface(IN SURFOBJ *Surface)
+VOID
+APIENTRY
+EngUnlockSurface(IN SURFOBJ *pso)
 {
-    if (Surface != NULL)
+    if (pso != NULL)
     {
-        BITMAPOBJ *bmp = CONTAINING_RECORD(Surface, BITMAPOBJ, SurfObj);
-        GDIOBJ_ShareUnlockObjByPtr((POBJ)bmp);
+        SURFACE *psurf = CONTAINING_RECORD(pso, SURFACE, SurfObj);
+        GDIOBJ_ShareUnlockObjByPtr((POBJ)psurf);
     }
 }
 
-
 /* EOF */