2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: GDI Driver Surace Functions
5 * FILE: subsys/win32k/eng/surface.c
6 * PROGRAMER: Jason Filby
9 * 9/11/2000: Updated to handle real pixel packed bitmaps (UPDATE TO DATE COMPLETED)
11 * - Create a GDI bitmap with all formats, perform all drawing operations on them, render to VGA surface
12 * refer to \test\microwin\src\engine\devdraw.c for info on correct pixel plotting for various formats
22 RLE_EOL
= 0, /* End of line */
23 RLE_END
= 1, /* End of bitmap */
24 RLE_DELTA
= 2 /* Delta */
27 INT FASTCALL
BitsPerFormat(ULONG Format
)
58 ULONG FASTCALL
BitmapFormat(WORD Bits
, DWORD Compression
)
94 SURFACE_Cleanup(PVOID ObjectBody
)
96 PSURFACE psurf
= (PSURFACE
)ObjectBody
;
97 PVOID pvBits
= psurf
->SurfObj
.pvBits
;
99 /* If this is an API bitmap, free the bits */
100 if (pvBits
!= NULL
&&
101 (psurf
->flFlags
& BITMAPOBJ_IS_APIBITMAP
))
103 /* Check if we have a DIB section */
106 // FIXME: IMPLEMENT ME!
107 // MmUnsecureVirtualMemory(psurf->hSecure);
108 if (psurf
->hDIBSection
)
110 /* DIB was created from a section */
113 pvBits
= (PVOID
)((ULONG_PTR
)pvBits
- psurf
->dwOffset
);
114 Status
= ZwUnmapViewOfSection(NtCurrentProcess(), pvBits
);
115 if (!NT_SUCCESS(Status
))
117 DPRINT1("Could not unmap section view!\n");
118 // Should we BugCheck here?
123 /* DIB was allocated */
124 EngFreeUserMem(pvBits
);
130 ExFreePool(psurf
->SurfObj
.pvBits
);
133 if (psurf
->hDIBPalette
!= NULL
)
135 GreDeleteObject(psurf
->hDIBPalette
);
139 if (NULL
!= psurf
->BitsLock
)
141 ExFreePoolWithTag(psurf
->BitsLock
, TAG_SURFACE
);
142 psurf
->BitsLock
= NULL
;
149 SURFACE_InitBitsLock(PSURFACE psurf
)
151 psurf
->BitsLock
= ExAllocatePoolWithTag(NonPagedPool
,
154 if (NULL
== psurf
->BitsLock
)
159 ExInitializeFastMutex(psurf
->BitsLock
);
165 SURFACE_CleanupBitsLock(PSURFACE psurf
)
167 if (NULL
!= psurf
->BitsLock
)
169 ExFreePoolWithTag(psurf
->BitsLock
, TAG_SURFACE
);
170 psurf
->BitsLock
= NULL
;
179 EngCreateDeviceBitmap(IN DHSURF dhsurf
,
186 NewBitmap
= EngCreateBitmap(Size
, DIB_GetDIBWidthBytes(Size
.cx
, BitsPerFormat(Format
)), Format
, 0, NULL
);
189 DPRINT1("EngCreateBitmap failed\n");
193 pso
= EngLockSurface((HSURF
)NewBitmap
);
196 DPRINT1("EngLockSurface failed on newly created bitmap!\n");
197 GreDeleteObject(NewBitmap
);
201 pso
->dhsurf
= dhsurf
;
202 EngUnlockSurface(pso
);
207 VOID
Decompress4bpp(SIZEL Size
, BYTE
*CompressedBits
, BYTE
*UncompressedBits
, LONG Delta
)
213 int width
= ((Size
.cx
+1)/2);
214 int height
= Size
.cy
- 1;
215 BYTE
*begin
= CompressedBits
;
216 BYTE
*bits
= CompressedBits
;
220 length
= *bits
++ / 2;
226 if (x
>= width
) break;
227 temp
= UncompressedBits
+ (((height
- y
) * Delta
) + x
);
254 temp
= UncompressedBits
+ (((height
- y
) * Delta
) + x
);
259 if ((bits
- begin
) & 1)
268 VOID
Decompress8bpp(SIZEL Size
, BYTE
*CompressedBits
, BYTE
*UncompressedBits
, LONG Delta
)
275 int height
= Size
.cy
- 1;
276 BYTE
*begin
= CompressedBits
;
277 BYTE
*bits
= CompressedBits
;
287 if (x
>= width
) break;
288 temp
= UncompressedBits
+ (((height
- y
) * Delta
) + x
);
314 temp
= UncompressedBits
+ (((height
- y
) * Delta
) + x
);
319 if ((bits
- begin
) & 1)
329 IntCreateBitmap(IN SIZEL Size
,
338 PVOID UncompressedBits
;
339 ULONG UncompressedFormat
;
344 psurf
= SURFACE_AllocSurfaceWithHandle();
349 hbmp
= psurf
->BaseObject
.hHmgr
;
351 if (! SURFACE_InitBitsLock(psurf
))
353 SURFACE_UnlockSurface(psurf
);
354 SURFACE_FreeSurfaceByHandle(hbmp
);
357 pso
= &psurf
->SurfObj
;
359 if (Format
== BMF_4RLE
)
361 pso
->lDelta
= DIB_GetDIBWidthBytes(Size
.cx
, BitsPerFormat(BMF_4BPP
));
362 pso
->cjBits
= pso
->lDelta
* Size
.cy
;
363 UncompressedFormat
= BMF_4BPP
;
364 UncompressedBits
= EngAllocMem(FL_ZERO_MEMORY
, pso
->cjBits
, TAG_DIB
);
365 Decompress4bpp(Size
, (BYTE
*)Bits
, (BYTE
*)UncompressedBits
, pso
->lDelta
);
367 else if (Format
== BMF_8RLE
)
369 pso
->lDelta
= DIB_GetDIBWidthBytes(Size
.cx
, BitsPerFormat(BMF_8BPP
));
370 pso
->cjBits
= pso
->lDelta
* Size
.cy
;
371 UncompressedFormat
= BMF_8BPP
;
372 UncompressedBits
= EngAllocMem(FL_ZERO_MEMORY
, pso
->cjBits
, TAG_DIB
);
373 Decompress8bpp(Size
, (BYTE
*)Bits
, (BYTE
*)UncompressedBits
, pso
->lDelta
);
377 pso
->lDelta
= abs(Width
);
378 pso
->cjBits
= pso
->lDelta
* Size
.cy
;
379 UncompressedBits
= Bits
;
380 UncompressedFormat
= Format
;
383 if (UncompressedBits
!= NULL
)
385 pso
->pvBits
= UncompressedBits
;
389 if (pso
->cjBits
== 0)
395 if (0 != (Flags
& BMF_USERMEM
))
397 pso
->pvBits
= EngAllocUserMem(pso
->cjBits
, 0);
401 pso
->pvBits
= EngAllocMem(0 != (Flags
& BMF_NOZEROINIT
) ?
403 pso
->cjBits
, TAG_DIB
);
405 if (pso
->pvBits
== NULL
)
407 SURFACE_UnlockSurface(psurf
);
408 SURFACE_FreeSurfaceByHandle(hbmp
);
409 SetLastWin32Error(ERROR_NOT_ENOUGH_MEMORY
);
415 if (0 == (Flags
& BMF_TOPDOWN
))
417 pso
->pvScan0
= (PVOID
)((ULONG_PTR
)pso
->pvBits
+ pso
->cjBits
- pso
->lDelta
);
418 pso
->lDelta
= - pso
->lDelta
;
422 pso
->pvScan0
= pso
->pvBits
;
425 pso
->dhsurf
= 0; /* device managed surface */
426 pso
->hsurf
= (HSURF
)hbmp
;
429 pso
->sizlBitmap
= Size
;
430 pso
->iBitmapFormat
= UncompressedFormat
;
431 pso
->iType
= STYPE_BITMAP
;
432 pso
->fjBitmap
= Flags
& (BMF_TOPDOWN
| BMF_NOZEROINIT
);
437 psurf
->dimension
.cx
= 0;
438 psurf
->dimension
.cy
= 0;
440 psurf
->hSecure
= NULL
;
441 psurf
->hDIBSection
= NULL
;
443 SURFACE_UnlockSurface(psurf
);
448 /* Name gleaned from C++ symbol information for SURFMEM::bInitDIB */
449 typedef struct _DEVBITMAPINFO
456 } DEVBITMAPINFO
, *PDEVBITMAPINFO
;
460 SURFMEM_bCreateDib(IN PDEVBITMAPINFO BitmapInfo
,
463 BOOLEAN Compressed
= FALSE
;
464 ULONG ScanLine
= 0; // Compiler is dumb
471 * First, check the format so we can get the aligned scanline width.
472 * RLE and the newer fancy-smanshy JPG/PNG support do NOT have scanlines
473 * since they are compressed surfaces!
475 switch (BitmapInfo
->Format
)
478 //ScanLine = ((BitmapInfo->Width + 31) & ~31) / 8;
482 //ScanLine = ((BitmapInfo->Width + 7) & ~7) / 2;
486 //ScanLine = ((BitmapInfo->Width + 3) & ~3);
490 //ScanLine = ((BitmapInfo->Width + 1) & ~1) * 2;
494 //ScanLine = ((BitmapInfo->Width * 3) + 3) & ~3;
498 // ScanLine = BitmapInfo->Width * 4;
509 DPRINT1("Invalid bitmap format\n");
513 ScanLine
= BitmapInfo
->Width
;
515 /* Does the device manage its own surface? */
518 /* We need to allocate bits for the caller, figure out the size */
521 /* Note: we should not be seeing this scenario from ENGDDI */
523 Size
= BitmapInfo
->Size
;
527 /* The height times the bytes for each scanline */
528 Size
= BitmapInfo
->Height
* ScanLine
;
533 /* Check for allocation flag */
534 if (BitmapInfo
->Flags
& BMF_USERMEM
)
536 /* Get the bits from user-mode memory */
537 Bits
= EngAllocUserMem(Size
, 'mbuG');
541 /* Get kernel bits (zeroed out if requested) */
542 Bits
= EngAllocMem((BitmapInfo
->Flags
& BMF_NOZEROINIT
) ? 0 : FL_ZERO_MEMORY
,
547 /* Bail out if that failed */
548 if (!Bits
) return NULL
;
553 /* Should not have asked for user memory */
554 ASSERT((BitmapInfo
->Flags
& BMF_USERMEM
) == 0);
557 /* Allocate the actual surface object structure */
558 psurf
= SURFACE_AllocSurfaceWithHandle();
559 if (!psurf
) return NULL
;
561 /* Lock down the surface */
562 if (!SURFACE_InitBitsLock(psurf
))
564 /* Bail out if that failed */
565 SURFACE_UnlockSurface(psurf
);
566 SURFACE_FreeSurface(psurf
);
570 /* We should now have our surface object */
571 pso
= &psurf
->SurfObj
;
573 /* Save format and flags */
574 pso
->iBitmapFormat
= BitmapInfo
->Format
;
575 pso
->fjBitmap
= BitmapInfo
->Flags
& (BMF_TOPDOWN
| BMF_UMPDMEM
| BMF_USERMEM
);
577 /* Save size and type */
578 LocalSize
.cy
= BitmapInfo
->Height
;
579 LocalSize
.cx
= BitmapInfo
->Width
;
580 pso
->sizlBitmap
= LocalSize
;
581 pso
->iType
= STYPE_BITMAP
;
583 /* Device-managed surface, no flags or dimension */
588 psurf
->dimension
.cx
= 0;
589 psurf
->dimension
.cy
= 0;
590 psurf
->hSecure
= NULL
;
591 psurf
->hDIBSection
= NULL
;
597 /* Check for bitmap type */
600 /* Number of bits is based on the height times the scanline */
601 pso
->cjBits
= BitmapInfo
->Height
* ScanLine
;
602 if (BitmapInfo
->Flags
& BMF_TOPDOWN
)
604 /* For topdown, the base address starts with the bits */
605 pso
->pvScan0
= pso
->pvBits
;
606 pso
->lDelta
= ScanLine
;
610 /* Otherwise we start with the end and go up */
611 pso
->pvScan0
= (PVOID
)((ULONG_PTR
)pso
->pvBits
+ pso
->cjBits
- ScanLine
);
612 pso
->lDelta
= -ScanLine
;
617 /* Compressed surfaces don't have scanlines! */
618 ASSERT(FALSE
); // Should not get here on ENGDDI
620 pso
->cjBits
= BitmapInfo
->Size
;
622 /* Check for JPG or PNG */
623 if ((BitmapInfo
->Format
!= BMF_JPEG
) && (BitmapInfo
->Format
!= BMF_PNG
))
625 /* Wherever the bit data is */
626 pso
->pvScan0
= pso
->pvBits
;
630 /* Fancy formats don't use a base address */
632 ASSERT(FALSE
); // ENGDDI shouldn't be creating PNGs for drivers ;-)
636 /* Finally set the handle and uniq */
637 pso
->hsurf
= (HSURF
)psurf
->BaseObject
.hHmgr
;
640 /* Unlock and return the surface */
641 SURFACE_UnlockSurface(psurf
);
650 EngCreateBitmap(IN SIZEL Size
,
657 DEVBITMAPINFO BitmapInfo
;
659 /* Capture the parameters */
660 BitmapInfo
.Format
= Format
;
661 BitmapInfo
.Width
= Size
.cx
;
662 BitmapInfo
.Height
= Size
.cy
;
663 BitmapInfo
.Flags
= Flags
;
666 * If the display driver supports framebuffer access, use the scanline width
667 * to determine the actual width of the bitmap, and convert it to pels instead
670 if ((Bits
) && (Width
))
673 switch (BitmapInfo
.Format
)
675 /* Do the conversion for each bit depth we support */
677 BitmapInfo
.Width
= Width
* 8;
680 BitmapInfo
.Width
= Width
* 2;
683 BitmapInfo
.Width
= Width
;
686 BitmapInfo
.Width
= Width
/ 2;
689 BitmapInfo
.Width
= Width
/ 3;
692 BitmapInfo
.Width
= Width
/ 4;
696 BitmapInfo
.Width
= Width
;
700 /* Now create the surface */
701 Surface
= SURFMEM_bCreateDib(&BitmapInfo
, Bits
);
702 if (!Surface
) return 0;
704 /* Set public ownership and reutrn the handle */
705 GDIOBJ_SetOwnership(Surface
->hsurf
, NULL
);
706 return Surface
->hsurf
;
713 EngCreateDeviceSurface(IN DHSURF dhsurf
,
721 psurf
= SURFACE_AllocSurfaceWithHandle();
727 hsurf
= psurf
->BaseObject
.hHmgr
;
728 GDIOBJ_SetOwnership(hsurf
, NULL
);
730 if (!SURFACE_InitBitsLock(psurf
))
732 SURFACE_UnlockSurface(psurf
);
733 SURFACE_FreeSurfaceByHandle(hsurf
);
736 pso
= &psurf
->SurfObj
;
738 pso
->dhsurf
= dhsurf
;
740 pso
->sizlBitmap
= Size
;
741 pso
->iBitmapFormat
= Format
;
742 pso
->lDelta
= DIB_GetDIBWidthBytes(Size
.cx
, BitsPerFormat(Format
));
743 pso
->iType
= STYPE_DEVICE
;
748 SURFACE_UnlockSurface(psurf
);
767 ppdev
= (PDEVOBJ
*)hdev
;
769 /* Lock the surface */
770 psurf
= SURFACE_LockSurface(hsurf
);
775 pso
= &psurf
->SurfObj
;
777 /* Associate the hdev */
779 pso
->dhpdev
= ppdev
->dhpdev
;
781 /* Hook up specified functions */
782 psurf
->flHooks
= flHooks
;
784 SURFACE_UnlockSurface(psurf
);
807 psurf
= SURFACE_LockSurface(hsurf
);
813 ppdev
= (PDEVOBJ
*)hdev
;
814 pso
= &psurf
->SurfObj
;
815 pso
->dhsurf
= dhsurf
;
816 pso
->lDelta
= lDelta
;
817 pso
->pvScan0
= pvScan0
;
819 /* Associate the hdev */
821 pso
->dhpdev
= ppdev
->dhpdev
;
823 /* Hook up specified functions */
824 psurf
->flHooks
= flHooks
;
826 SURFACE_UnlockSurface(psurf
);
835 EngDeleteSurface(IN HSURF hsurf
)
837 GDIOBJ_SetOwnership(hsurf
, PsGetCurrentProcess());
838 SURFACE_FreeSurfaceByHandle(hsurf
);
846 EngEraseSurface(SURFOBJ
*pso
,
852 return FillSolid(pso
, Rect
, iColor
);
859 NtGdiEngLockSurface(IN HSURF hsurf
)
861 return EngLockSurface(hsurf
);
869 EngLockSurface(IN HSURF hsurf
)
871 SURFACE
*psurf
= GDIOBJ_ShareLockObj(hsurf
, GDI_OBJECT_TYPE_BITMAP
);
874 return &psurf
->SurfObj
;
884 NtGdiEngUnlockSurface(IN SURFOBJ
*pso
)
886 EngUnlockSurface(pso
);
893 EngUnlockSurface(IN SURFOBJ
*pso
)
897 SURFACE
*psurf
= CONTAINING_RECORD(pso
, SURFACE
, SurfObj
);
898 GDIOBJ_ShareUnlockObjByPtr((POBJ
)psurf
);