2 * ReactOS W32 Subsystem
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS kernel
23 * PURPOSE: GDI Driver Surace Functions
24 * FILE: subsys/win32k/eng/surface.c
25 * PROGRAMER: Jason Filby
28 * 9/11/2000: Updated to handle real pixel packed bitmaps (UPDATE TO DATE COMPLETED)
30 * - Create a GDI bitmap with all formats, perform all drawing operations on them, render to VGA surface
31 * refer to \test\microwin\src\engine\devdraw.c for info on correct pixel plotting for various formats
37 RLE_EOL
= 0, /* End of line */
38 RLE_END
= 1, /* End of bitmap */
39 RLE_DELTA
= 2 /* Delta */
42 INT FASTCALL
BitsPerFormat(ULONG Format
)
46 case BMF_1BPP
: return 1;
48 case BMF_4RLE
: return 4;
50 case BMF_8RLE
: return 8;
51 case BMF_16BPP
: return 16;
52 case BMF_24BPP
: return 24;
53 case BMF_32BPP
: return 32;
58 ULONG FASTCALL
BitmapFormat(WORD Bits
, DWORD Compression
)
66 case 1: return BMF_1BPP
;
67 case 4: return BMF_4BPP
;
68 case 8: return BMF_8BPP
;
69 case 16: return BMF_16BPP
;
70 case 24: return BMF_24BPP
;
71 case 32: return BMF_32BPP
;
75 case BI_RLE4
: return BMF_4RLE
;
76 case BI_RLE8
: return BMF_8RLE
;
83 BITMAPOBJ_InitBitsLock(BITMAPOBJ
*BitmapObj
)
85 BitmapObj
->BitsLock
= ExAllocatePoolWithTag(NonPagedPool
,
88 if (NULL
== BitmapObj
->BitsLock
)
93 ExInitializeFastMutex(BitmapObj
->BitsLock
);
99 BITMAPOBJ_CleanupBitsLock(BITMAPOBJ
*BitmapObj
)
101 if (NULL
!= BitmapObj
->BitsLock
)
103 ExFreePoolWithTag(BitmapObj
->BitsLock
, TAG_BITMAPOBJ
);
104 BitmapObj
->BitsLock
= NULL
;
113 EngCreateDeviceBitmap(IN DHSURF dhsurf
,
120 NewBitmap
= EngCreateBitmap(Size
, DIB_GetDIBWidthBytes(Size
.cx
, BitsPerFormat(Format
)), Format
, 0, NULL
);
121 SurfObj
= EngLockSurface((HSURF
)NewBitmap
);
122 SurfObj
->dhsurf
= dhsurf
;
123 EngUnlockSurface(SurfObj
);
128 VOID
Decompress4bpp(SIZEL Size
, BYTE
*CompressedBits
, BYTE
*UncompressedBits
, LONG Delta
)
134 int width
= ((Size
.cx
+1)/2);
135 int height
= Size
.cy
- 1;
136 BYTE
*begin
= CompressedBits
;
137 BYTE
*bits
= CompressedBits
;
141 length
= *bits
++ / 2;
147 if (x
>= width
) break;
148 temp
= UncompressedBits
+ (((height
- y
) * Delta
) + x
);
173 temp
= UncompressedBits
+ (((height
- y
) * Delta
) + x
);
178 if ((bits
- begin
) & 1)
185 VOID
Decompress8bpp(SIZEL Size
, BYTE
*CompressedBits
, BYTE
*UncompressedBits
, LONG Delta
)
192 int height
= Size
.cy
- 1;
193 BYTE
*begin
= CompressedBits
;
194 BYTE
*bits
= CompressedBits
;
204 if (x
>= width
) break;
205 temp
= UncompressedBits
+ (((height
- y
) * Delta
) + x
);
229 temp
= UncompressedBits
+ (((height
- y
) * Delta
) + x
);
234 if ((bits
- begin
) & 1)
242 IntCreateBitmap(IN SIZEL Size
,
250 BITMAPOBJ
*BitmapObj
;
251 PVOID UncompressedBits
;
252 ULONG UncompressedFormat
;
257 NewBitmap
= BITMAPOBJ_AllocBitmap();
258 if (NewBitmap
== NULL
)
261 BitmapObj
= BITMAPOBJ_LockBitmap(NewBitmap
);
262 if (! BITMAPOBJ_InitBitsLock(BitmapObj
))
264 BITMAPOBJ_UnlockBitmap(BitmapObj
);
265 BITMAPOBJ_FreeBitmap(NewBitmap
);
268 SurfObj
= &BitmapObj
->SurfObj
;
270 if (Format
== BMF_4RLE
)
272 SurfObj
->lDelta
= DIB_GetDIBWidthBytes(Size
.cx
, BitsPerFormat(BMF_4BPP
));
273 SurfObj
->cjBits
= SurfObj
->lDelta
* Size
.cy
;
274 UncompressedFormat
= BMF_4BPP
;
275 UncompressedBits
= EngAllocMem(FL_ZERO_MEMORY
, SurfObj
->cjBits
, 0);
276 Decompress4bpp(Size
, (BYTE
*)Bits
, (BYTE
*)UncompressedBits
, SurfObj
->lDelta
);
278 else if (Format
== BMF_8RLE
)
280 SurfObj
->lDelta
= DIB_GetDIBWidthBytes(Size
.cx
, BitsPerFormat(BMF_8BPP
));
281 SurfObj
->cjBits
= SurfObj
->lDelta
* Size
.cy
;
282 UncompressedFormat
= BMF_8BPP
;
283 UncompressedBits
= EngAllocMem(FL_ZERO_MEMORY
, SurfObj
->cjBits
, 0);
284 Decompress8bpp(Size
, (BYTE
*)Bits
, (BYTE
*)UncompressedBits
, SurfObj
->lDelta
);
288 SurfObj
->lDelta
= abs(Width
);
289 SurfObj
->cjBits
= SurfObj
->lDelta
* Size
.cy
;
290 UncompressedBits
= Bits
;
291 UncompressedFormat
= Format
;
294 if (UncompressedBits
!= NULL
)
296 SurfObj
->pvBits
= UncompressedBits
;
300 if (SurfObj
->cjBits
== 0)
302 SurfObj
->pvBits
= NULL
;
306 if (0 != (Flags
& BMF_USERMEM
))
308 SurfObj
->pvBits
= EngAllocUserMem(SurfObj
->cjBits
, 0);
312 SurfObj
->pvBits
= EngAllocMem(0 != (Flags
& BMF_NOZEROINIT
) ? 0 : FL_ZERO_MEMORY
,
315 if (SurfObj
->pvBits
== NULL
)
317 BITMAPOBJ_UnlockBitmap(BitmapObj
);
318 BITMAPOBJ_FreeBitmap(NewBitmap
);
325 if (0 == (Flags
& BMF_TOPDOWN
))
327 SurfObj
->pvScan0
= (PVOID
) ((ULONG_PTR
) SurfObj
->pvBits
+ SurfObj
->cjBits
- SurfObj
->lDelta
);
328 SurfObj
->lDelta
= - SurfObj
->lDelta
;
332 SurfObj
->pvScan0
= SurfObj
->pvBits
;
335 SurfObj
->dhsurf
= 0; /* device managed surface */
336 SurfObj
->hsurf
= (HSURF
)NewBitmap
;
337 SurfObj
->dhpdev
= NULL
;
338 SurfObj
->hdev
= NULL
;
339 SurfObj
->sizlBitmap
= Size
;
340 SurfObj
->iBitmapFormat
= UncompressedFormat
;
341 SurfObj
->iType
= STYPE_BITMAP
;
342 SurfObj
->fjBitmap
= Flags
& (BMF_TOPDOWN
| BMF_NOZEROINIT
);
345 BitmapObj
->flHooks
= 0;
346 BitmapObj
->flFlags
= 0;
347 BitmapObj
->dimension
.cx
= 0;
348 BitmapObj
->dimension
.cy
= 0;
349 BitmapObj
->dib
= NULL
;
351 BITMAPOBJ_UnlockBitmap(BitmapObj
);
360 EngCreateBitmap(IN SIZEL Size
,
368 NewBitmap
= IntCreateBitmap(Size
, Width
, Format
, Flags
, Bits
);
372 GDIOBJ_SetOwnership(NewBitmap
, NULL
);
381 EngCreateDeviceSurface(IN DHSURF dhsurf
,
387 BITMAPOBJ
*BitmapObj
;
389 NewSurface
= (HSURF
)BITMAPOBJ_AllocBitmap();
390 if (NewSurface
== NULL
)
393 BitmapObj
= BITMAPOBJ_LockBitmap(NewSurface
);
394 if (! BITMAPOBJ_InitBitsLock(BitmapObj
))
396 BITMAPOBJ_UnlockBitmap(BitmapObj
);
397 BITMAPOBJ_FreeBitmap(NewSurface
);
400 SurfObj
= &BitmapObj
->SurfObj
;
402 GDIOBJ_SetOwnership(NewSurface
, NULL
);
404 SurfObj
->dhsurf
= dhsurf
;
405 SurfObj
->hsurf
= NewSurface
;
406 SurfObj
->sizlBitmap
= Size
;
407 SurfObj
->iBitmapFormat
= Format
;
408 SurfObj
->lDelta
= DIB_GetDIBWidthBytes(Size
.cx
, BitsPerFormat(Format
));
409 SurfObj
->iType
= STYPE_DEVICE
;
412 BitmapObj
->flHooks
= 0;
414 BITMAPOBJ_UnlockBitmap(BitmapObj
);
419 PFN FASTCALL
DriverFunction(DRVENABLEDATA
*DED
, ULONG DriverFunc
)
423 for(i
=0; i
<DED
->c
; i
++)
425 if(DED
->pdrvfn
[i
].iFunc
== DriverFunc
)
426 return DED
->pdrvfn
[i
].pfn
;
435 EngAssociateSurface(IN HSURF Surface
,
440 BITMAPOBJ
*BitmapObj
;
443 Device
= (GDIDEVICE
*)Dev
;
445 BitmapObj
= BITMAPOBJ_LockBitmap(Surface
);
447 SurfObj
= &BitmapObj
->SurfObj
;
449 /* Associate the hdev */
451 SurfObj
->dhpdev
= Device
->PDev
;
453 /* Hook up specified functions */
454 BitmapObj
->flHooks
= Hooks
;
456 BITMAPOBJ_UnlockBitmap(BitmapObj
);
477 pso
= EngLockSurface(hsurf
);
483 if (!EngAssociateSurface(hsurf
, hdev
, flHooks
))
485 EngUnlockSurface(pso
);
490 pso
->dhsurf
= dhsurf
;
491 pso
->lDelta
= lDelta
;
492 pso
->pvScan0
= pvScan0
;
494 EngUnlockSurface(pso
);
503 EngDeleteSurface(IN HSURF Surface
)
505 GDIOBJ_SetOwnership(Surface
, PsGetCurrentProcess());
506 BITMAPOBJ_FreeBitmap(Surface
);
514 EngEraseSurface(SURFOBJ
*Surface
,
520 return FillSolid(Surface
, Rect
, iColor
);
523 #define GDIBdyToHdr(body) \
524 ((PGDIOBJHDR)(body) - 1)
530 EngLockSurface(IN HSURF Surface
)
532 BITMAPOBJ
*bmp
= GDIOBJ_ShareLockObj(Surface
, GDI_OBJECT_TYPE_BITMAP
);
535 return &bmp
->SurfObj
;
544 EngUnlockSurface(IN SURFOBJ
*Surface
)
547 GDIOBJ_UnlockObjByPtr(Surface
);