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
);
146 EngCreateDeviceBitmap(IN DHSURF dhsurf
,
153 NewBitmap
= EngCreateBitmap(Size
, DIB_GetDIBWidthBytes(Size
.cx
, BitsPerFormat(Format
)), Format
, 0, NULL
);
156 DPRINT1("EngCreateBitmap failed\n");
160 pso
= EngLockSurface((HSURF
)NewBitmap
);
163 DPRINT1("EngLockSurface failed on newly created bitmap!\n");
164 GreDeleteObject(NewBitmap
);
168 pso
->dhsurf
= dhsurf
;
169 EngUnlockSurface(pso
);
174 VOID
Decompress4bpp(SIZEL Size
, BYTE
*CompressedBits
, BYTE
*UncompressedBits
, LONG Delta
)
180 int width
= ((Size
.cx
+1)/2);
181 int height
= Size
.cy
- 1;
182 BYTE
*begin
= CompressedBits
;
183 BYTE
*bits
= CompressedBits
;
187 length
= *bits
++ / 2;
193 if (x
>= width
) break;
194 temp
= UncompressedBits
+ (((height
- y
) * Delta
) + x
);
221 temp
= UncompressedBits
+ (((height
- y
) * Delta
) + x
);
226 if ((bits
- begin
) & 1)
235 VOID
Decompress8bpp(SIZEL Size
, BYTE
*CompressedBits
, BYTE
*UncompressedBits
, LONG Delta
)
242 int height
= Size
.cy
- 1;
243 BYTE
*begin
= CompressedBits
;
244 BYTE
*bits
= CompressedBits
;
254 if (x
>= width
) break;
255 temp
= UncompressedBits
+ (((height
- y
) * Delta
) + x
);
281 temp
= UncompressedBits
+ (((height
- y
) * Delta
) + x
);
286 if ((bits
- begin
) & 1)
296 IntCreateBitmap(IN SIZEL Size
,
305 PVOID UncompressedBits
;
306 ULONG UncompressedFormat
;
311 psurf
= SURFACE_AllocSurfaceWithHandle();
316 hbmp
= psurf
->BaseObject
.hHmgr
;
318 pso
= &psurf
->SurfObj
;
320 if (Format
== BMF_4RLE
)
322 pso
->lDelta
= DIB_GetDIBWidthBytes(Size
.cx
, BitsPerFormat(BMF_4BPP
));
323 pso
->cjBits
= pso
->lDelta
* Size
.cy
;
324 UncompressedFormat
= BMF_4BPP
;
325 UncompressedBits
= EngAllocMem(FL_ZERO_MEMORY
, pso
->cjBits
, TAG_DIB
);
326 Decompress4bpp(Size
, (BYTE
*)Bits
, (BYTE
*)UncompressedBits
, pso
->lDelta
);
328 else if (Format
== BMF_8RLE
)
330 pso
->lDelta
= DIB_GetDIBWidthBytes(Size
.cx
, BitsPerFormat(BMF_8BPP
));
331 pso
->cjBits
= pso
->lDelta
* Size
.cy
;
332 UncompressedFormat
= BMF_8BPP
;
333 UncompressedBits
= EngAllocMem(FL_ZERO_MEMORY
, pso
->cjBits
, TAG_DIB
);
334 Decompress8bpp(Size
, (BYTE
*)Bits
, (BYTE
*)UncompressedBits
, pso
->lDelta
);
338 pso
->lDelta
= abs(Width
);
339 pso
->cjBits
= pso
->lDelta
* Size
.cy
;
340 UncompressedBits
= Bits
;
341 UncompressedFormat
= Format
;
344 if (UncompressedBits
!= NULL
)
346 pso
->pvBits
= UncompressedBits
;
350 if (pso
->cjBits
== 0)
356 if (0 != (Flags
& BMF_USERMEM
))
358 pso
->pvBits
= EngAllocUserMem(pso
->cjBits
, 0);
362 pso
->pvBits
= EngAllocMem(0 != (Flags
& BMF_NOZEROINIT
) ?
364 pso
->cjBits
, TAG_DIB
);
366 if (pso
->pvBits
== NULL
)
368 SURFACE_UnlockSurface(psurf
);
369 SURFACE_FreeSurfaceByHandle(hbmp
);
370 SetLastWin32Error(ERROR_NOT_ENOUGH_MEMORY
);
376 if (0 == (Flags
& BMF_TOPDOWN
))
378 pso
->pvScan0
= (PVOID
)((ULONG_PTR
)pso
->pvBits
+ pso
->cjBits
- pso
->lDelta
);
379 pso
->lDelta
= - pso
->lDelta
;
383 pso
->pvScan0
= pso
->pvBits
;
386 pso
->dhsurf
= 0; /* device managed surface */
387 pso
->hsurf
= (HSURF
)hbmp
;
390 pso
->sizlBitmap
= Size
;
391 pso
->iBitmapFormat
= UncompressedFormat
;
392 pso
->iType
= STYPE_BITMAP
;
393 pso
->fjBitmap
= Flags
& (BMF_TOPDOWN
| BMF_NOZEROINIT
);
398 psurf
->dimension
.cx
= 0;
399 psurf
->dimension
.cy
= 0;
401 psurf
->hSecure
= NULL
;
402 psurf
->hDIBSection
= NULL
;
404 SURFACE_UnlockSurface(psurf
);
409 /* Name gleaned from C++ symbol information for SURFMEM::bInitDIB */
410 typedef struct _DEVBITMAPINFO
417 } DEVBITMAPINFO
, *PDEVBITMAPINFO
;
421 SURFMEM_bCreateDib(IN PDEVBITMAPINFO BitmapInfo
,
424 BOOLEAN Compressed
= FALSE
;
425 ULONG ScanLine
= 0; // Compiler is dumb
430 BOOLEAN AllocatedLocally
= FALSE
;
433 * First, check the format so we can get the aligned scanline width.
434 * RLE and the newer fancy-smanshy JPG/PNG support do NOT have scanlines
435 * since they are compressed surfaces!
437 switch (BitmapInfo
->Format
)
440 ScanLine
= ((BitmapInfo
->Width
+ 31) & ~31) >> 3;
444 ScanLine
= ((BitmapInfo
->Width
+ 7) & ~7) >> 1;
448 ScanLine
= (BitmapInfo
->Width
+ 3) & ~3;
452 ScanLine
= ((BitmapInfo
->Width
+ 1) & ~1) << 1;
456 ScanLine
= ((BitmapInfo
->Width
* 3) + 3) & ~3;
460 ScanLine
= BitmapInfo
->Width
<< 2;
471 DPRINT1("Invalid bitmap format\n");
475 /* Does the device manage its own surface? */
478 /* We need to allocate bits for the caller, figure out the size */
481 /* Note: we should not be seeing this scenario from ENGDDI */
483 Size
= BitmapInfo
->Size
;
487 /* The height times the bytes for each scanline */
488 Size
= BitmapInfo
->Height
* ScanLine
;
493 /* Check for allocation flag */
494 if (BitmapInfo
->Flags
& BMF_USERMEM
)
496 /* Get the bits from user-mode memory */
497 Bits
= EngAllocUserMem(Size
, 'mbuG');
501 /* Get kernel bits (zeroed out if requested) */
502 Bits
= EngAllocMem((BitmapInfo
->Flags
& BMF_NOZEROINIT
) ? 0 : FL_ZERO_MEMORY
,
506 AllocatedLocally
= TRUE
;
507 /* Bail out if that failed */
508 if (!Bits
) return NULL
;
513 /* Should not have asked for user memory */
514 ASSERT((BitmapInfo
->Flags
& BMF_USERMEM
) == 0);
517 /* Allocate the actual surface object structure */
518 psurf
= SURFACE_AllocSurfaceWithHandle();
521 if(Bits
&& AllocatedLocally
)
523 if(BitmapInfo
->Flags
& BMF_USERMEM
)
524 EngFreeUserMem(Bits
);
531 /* We should now have our surface object */
532 pso
= &psurf
->SurfObj
;
534 /* Save format and flags */
535 pso
->iBitmapFormat
= BitmapInfo
->Format
;
536 pso
->fjBitmap
= BitmapInfo
->Flags
& (BMF_TOPDOWN
| BMF_UMPDMEM
| BMF_USERMEM
);
538 /* Save size and type */
539 LocalSize
.cy
= BitmapInfo
->Height
;
540 LocalSize
.cx
= BitmapInfo
->Width
;
541 pso
->sizlBitmap
= LocalSize
;
542 pso
->iType
= STYPE_BITMAP
;
544 /* Device-managed surface, no flags or dimension */
549 psurf
->dimension
.cx
= 0;
550 psurf
->dimension
.cy
= 0;
551 psurf
->hSecure
= NULL
;
552 psurf
->hDIBSection
= NULL
;
558 /* Check for bitmap type */
561 /* Number of bits is based on the height times the scanline */
562 pso
->cjBits
= BitmapInfo
->Height
* ScanLine
;
563 if (BitmapInfo
->Flags
& BMF_TOPDOWN
)
565 /* For topdown, the base address starts with the bits */
566 pso
->pvScan0
= pso
->pvBits
;
567 pso
->lDelta
= ScanLine
;
571 /* Otherwise we start with the end and go up */
572 pso
->pvScan0
= (PVOID
)((ULONG_PTR
)pso
->pvBits
+ pso
->cjBits
- ScanLine
);
573 pso
->lDelta
= -ScanLine
;
578 /* Compressed surfaces don't have scanlines! */
580 pso
->cjBits
= BitmapInfo
->Size
;
582 /* Check for JPG or PNG */
583 if ((BitmapInfo
->Format
!= BMF_JPEG
) && (BitmapInfo
->Format
!= BMF_PNG
))
585 /* Wherever the bit data is */
586 pso
->pvScan0
= pso
->pvBits
;
590 /* Fancy formats don't use a base address */
592 ASSERT(FALSE
); // ENGDDI shouldn't be creating PNGs for drivers ;-)
596 /* Finally set the handle and uniq */
597 pso
->hsurf
= (HSURF
)psurf
->BaseObject
.hHmgr
;
600 /* Unlock and return the surface */
601 SURFACE_UnlockSurface(psurf
);
610 EngCreateBitmap(IN SIZEL Size
,
617 DEVBITMAPINFO BitmapInfo
;
619 /* Capture the parameters */
620 BitmapInfo
.Format
= Format
;
621 BitmapInfo
.Width
= Size
.cx
;
622 BitmapInfo
.Height
= Size
.cy
;
623 BitmapInfo
.Flags
= Flags
;
626 * If the display driver supports framebuffer access, use the scanline width
627 * to determine the actual width of the bitmap, and convert it to pels instead
630 if ((Bits
) && (Width
))
632 switch (BitmapInfo
.Format
)
634 /* Do the conversion for each bit depth we support */
636 BitmapInfo
.Width
= Width
* 8;
639 BitmapInfo
.Width
= Width
* 2;
642 BitmapInfo
.Width
= Width
;
645 BitmapInfo
.Width
= Width
/ 2;
648 BitmapInfo
.Width
= Width
/ 3;
651 BitmapInfo
.Width
= Width
/ 4;
656 /* Now create the surface */
657 Surface
= SURFMEM_bCreateDib(&BitmapInfo
, Bits
);
658 if (!Surface
) return 0;
660 /* Set public ownership and reutrn the handle */
661 GDIOBJ_SetOwnership(Surface
->hsurf
, NULL
);
662 return Surface
->hsurf
;
669 EngCreateDeviceSurface(IN DHSURF dhsurf
,
677 psurf
= SURFACE_AllocSurfaceWithHandle();
683 hsurf
= psurf
->BaseObject
.hHmgr
;
684 GDIOBJ_SetOwnership(hsurf
, NULL
);
686 pso
= &psurf
->SurfObj
;
688 pso
->dhsurf
= dhsurf
;
690 pso
->sizlBitmap
= Size
;
691 pso
->iBitmapFormat
= Format
;
692 pso
->lDelta
= DIB_GetDIBWidthBytes(Size
.cx
, BitsPerFormat(Format
));
693 pso
->iType
= STYPE_DEVICE
;
698 SURFACE_UnlockSurface(psurf
);
717 ppdev
= (PDEVOBJ
*)hdev
;
719 /* Lock the surface */
720 psurf
= SURFACE_LockSurface(hsurf
);
725 pso
= &psurf
->SurfObj
;
727 /* Associate the hdev */
729 pso
->dhpdev
= ppdev
->dhpdev
;
731 /* Hook up specified functions */
732 psurf
->flHooks
= flHooks
;
734 SURFACE_UnlockSurface(psurf
);
757 psurf
= SURFACE_LockSurface(hsurf
);
763 ppdev
= (PDEVOBJ
*)hdev
;
764 pso
= &psurf
->SurfObj
;
765 pso
->dhsurf
= dhsurf
;
766 pso
->lDelta
= lDelta
;
767 pso
->pvScan0
= pvScan0
;
769 /* Associate the hdev */
771 pso
->dhpdev
= ppdev
->dhpdev
;
773 /* Hook up specified functions */
774 psurf
->flHooks
= flHooks
;
776 SURFACE_UnlockSurface(psurf
);
785 EngDeleteSurface(IN HSURF hsurf
)
787 GDIOBJ_SetOwnership(hsurf
, PsGetCurrentProcess());
788 SURFACE_FreeSurfaceByHandle(hsurf
);
796 EngEraseSurface(SURFOBJ
*pso
,
802 return FillSolid(pso
, Rect
, iColor
);
809 NtGdiEngLockSurface(IN HSURF hsurf
)
811 return EngLockSurface(hsurf
);
819 EngLockSurface(IN HSURF hsurf
)
821 SURFACE
*psurf
= GDIOBJ_ShareLockObj(hsurf
, GDI_OBJECT_TYPE_BITMAP
);
824 return &psurf
->SurfObj
;
834 NtGdiEngUnlockSurface(IN SURFOBJ
*pso
)
836 EngUnlockSurface(pso
);
843 EngUnlockSurface(IN SURFOBJ
*pso
)
847 SURFACE
*psurf
= CONTAINING_RECORD(pso
, SURFACE
, SurfObj
);
848 GDIOBJ_ShareUnlockObjByPtr((POBJ
)psurf
);