Update PDEVOBJ and GRAPHICS_DEVICE fields
[reactos.git] / reactos / subsystems / win32 / win32k / eng / surface.c
1 /*
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
7 * REVISION HISTORY:
8 * 3/7/1999: Created
9 * 9/11/2000: Updated to handle real pixel packed bitmaps (UPDATE TO DATE COMPLETED)
10 * TESTING TO BE DONE:
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
13 */
14
15 #include <w32k.h>
16
17 #define NDEBUG
18 #include <debug.h>
19
20 enum Rle_EscapeCodes
21 {
22 RLE_EOL = 0, /* End of line */
23 RLE_END = 1, /* End of bitmap */
24 RLE_DELTA = 2 /* Delta */
25 };
26
27 INT FASTCALL BitsPerFormat(ULONG Format)
28 {
29 switch (Format)
30 {
31 case BMF_1BPP:
32 return 1;
33
34 case BMF_4BPP:
35 /* Fall through */
36 case BMF_4RLE:
37 return 4;
38
39 case BMF_8BPP:
40 /* Fall through */
41 case BMF_8RLE:
42 return 8;
43
44 case BMF_16BPP:
45 return 16;
46
47 case BMF_24BPP:
48 return 24;
49
50 case BMF_32BPP:
51 return 32;
52
53 default:
54 return 0;
55 }
56 }
57
58 ULONG FASTCALL BitmapFormat(WORD Bits, DWORD Compression)
59 {
60 switch (Compression)
61 {
62 case BI_RGB:
63 /* Fall through */
64 case BI_BITFIELDS:
65 switch (Bits)
66 {
67 case 1:
68 return BMF_1BPP;
69 case 4:
70 return BMF_4BPP;
71 case 8:
72 return BMF_8BPP;
73 case 16:
74 return BMF_16BPP;
75 case 24:
76 return BMF_24BPP;
77 case 32:
78 return BMF_32BPP;
79 }
80 return 0;
81
82 case BI_RLE4:
83 return BMF_4RLE;
84
85 case BI_RLE8:
86 return BMF_8RLE;
87
88 default:
89 return 0;
90 }
91 }
92
93 BOOL INTERNAL_CALL
94 SURFACE_Cleanup(PVOID ObjectBody)
95 {
96 PSURFACE psurf = (PSURFACE)ObjectBody;
97 PVOID pvBits = psurf->SurfObj.pvBits;
98
99 /* If this is an API bitmap, free the bits */
100 if (pvBits != NULL &&
101 (psurf->flFlags & BITMAPOBJ_IS_APIBITMAP))
102 {
103 /* Check if we have a DIB section */
104 if (psurf->hSecure)
105 {
106 // FIXME: IMPLEMENT ME!
107 // MmUnsecureVirtualMemory(psurf->hSecure);
108 if (psurf->hDIBSection)
109 {
110 /* DIB was created from a section */
111 NTSTATUS Status;
112
113 pvBits = (PVOID)((ULONG_PTR)pvBits - psurf->dwOffset);
114 Status = ZwUnmapViewOfSection(NtCurrentProcess(), pvBits);
115 if (!NT_SUCCESS(Status))
116 {
117 DPRINT1("Could not unmap section view!\n");
118 // Should we BugCheck here?
119 }
120 }
121 else
122 {
123 /* DIB was allocated */
124 EngFreeUserMem(pvBits);
125 }
126 }
127 else
128 {
129 // FIXME: use TAG
130 ExFreePool(psurf->SurfObj.pvBits);
131 }
132
133 if (psurf->hDIBPalette != NULL)
134 {
135 GreDeleteObject(psurf->hDIBPalette);
136 }
137 }
138
139 if (NULL != psurf->BitsLock)
140 {
141 ExFreePoolWithTag(psurf->BitsLock, TAG_SURFACE);
142 psurf->BitsLock = NULL;
143 }
144
145 return TRUE;
146 }
147
148 BOOL INTERNAL_CALL
149 SURFACE_InitBitsLock(PSURFACE psurf)
150 {
151 psurf->BitsLock = ExAllocatePoolWithTag(NonPagedPool,
152 sizeof(FAST_MUTEX),
153 TAG_SURFACE);
154 if (NULL == psurf->BitsLock)
155 {
156 return FALSE;
157 }
158
159 ExInitializeFastMutex(psurf->BitsLock);
160
161 return TRUE;
162 }
163
164 void INTERNAL_CALL
165 SURFACE_CleanupBitsLock(PSURFACE psurf)
166 {
167 if (NULL != psurf->BitsLock)
168 {
169 ExFreePoolWithTag(psurf->BitsLock, TAG_SURFACE);
170 psurf->BitsLock = NULL;
171 }
172 }
173
174
175 /*
176 * @implemented
177 */
178 HBITMAP APIENTRY
179 EngCreateDeviceBitmap(IN DHSURF dhsurf,
180 IN SIZEL Size,
181 IN ULONG Format)
182 {
183 HBITMAP NewBitmap;
184 SURFOBJ *pso;
185
186 NewBitmap = EngCreateBitmap(Size, DIB_GetDIBWidthBytes(Size.cx, BitsPerFormat(Format)), Format, 0, NULL);
187 if (!NewBitmap)
188 {
189 DPRINT1("EngCreateBitmap failed\n");
190 return 0;
191 }
192
193 pso = EngLockSurface((HSURF)NewBitmap);
194 if (!pso)
195 {
196 DPRINT1("EngLockSurface failed on newly created bitmap!\n");
197 GreDeleteObject(NewBitmap);
198 return NULL;
199 }
200
201 pso->dhsurf = dhsurf;
202 EngUnlockSurface(pso);
203
204 return NewBitmap;
205 }
206
207 VOID Decompress4bpp(SIZEL Size, BYTE *CompressedBits, BYTE *UncompressedBits, LONG Delta)
208 {
209 int x = 0;
210 int y = Size.cy - 1;
211 int c;
212 int length;
213 int width = ((Size.cx+1)/2);
214 int height = Size.cy - 1;
215 BYTE *begin = CompressedBits;
216 BYTE *bits = CompressedBits;
217 BYTE *temp;
218 while (y >= 0)
219 {
220 length = *bits++ / 2;
221 if (length)
222 {
223 c = *bits++;
224 while (length--)
225 {
226 if (x >= width) break;
227 temp = UncompressedBits + (((height - y) * Delta) + x);
228 x++;
229 *temp = c;
230 }
231 }
232 else
233 {
234 length = *bits++;
235 switch (length)
236 {
237 case RLE_EOL:
238 x = 0;
239 y--;
240 break;
241 case RLE_END:
242 return;
243 case RLE_DELTA:
244 x += (*bits++)/2;
245 y -= (*bits++)/2;
246 break;
247 default:
248 length /= 2;
249 while (length--)
250 {
251 c = *bits++;
252 if (x < width)
253 {
254 temp = UncompressedBits + (((height - y) * Delta) + x);
255 x++;
256 *temp = c;
257 }
258 }
259 if ((bits - begin) & 1)
260 {
261 bits++;
262 }
263 }
264 }
265 }
266 }
267
268 VOID Decompress8bpp(SIZEL Size, BYTE *CompressedBits, BYTE *UncompressedBits, LONG Delta)
269 {
270 int x = 0;
271 int y = Size.cy - 1;
272 int c;
273 int length;
274 int width = Size.cx;
275 int height = Size.cy - 1;
276 BYTE *begin = CompressedBits;
277 BYTE *bits = CompressedBits;
278 BYTE *temp;
279 while (y >= 0)
280 {
281 length = *bits++;
282 if (length)
283 {
284 c = *bits++;
285 while (length--)
286 {
287 if (x >= width) break;
288 temp = UncompressedBits + (((height - y) * Delta) + x);
289 x++;
290 *temp = c;
291 }
292 }
293 else
294 {
295 length = *bits++;
296 switch (length)
297 {
298 case RLE_EOL:
299 x = 0;
300 y--;
301 break;
302 case RLE_END:
303 return;
304 case RLE_DELTA:
305 x += *bits++;
306 y -= *bits++;
307 break;
308 default:
309 while (length--)
310 {
311 c = *bits++;
312 if (x < width)
313 {
314 temp = UncompressedBits + (((height - y) * Delta) + x);
315 x++;
316 *temp = c;
317 }
318 }
319 if ((bits - begin) & 1)
320 {
321 bits++;
322 }
323 }
324 }
325 }
326 }
327
328 HBITMAP FASTCALL
329 IntCreateBitmap(IN SIZEL Size,
330 IN LONG Width,
331 IN ULONG Format,
332 IN ULONG Flags,
333 IN PVOID Bits)
334 {
335 HBITMAP hbmp;
336 SURFOBJ *pso;
337 PSURFACE psurf;
338 PVOID UncompressedBits;
339 ULONG UncompressedFormat;
340
341 if (Format == 0)
342 return 0;
343
344 psurf = SURFACE_AllocSurfaceWithHandle();
345 if (psurf == NULL)
346 {
347 return 0;
348 }
349 hbmp = psurf->BaseObject.hHmgr;
350
351 if (! SURFACE_InitBitsLock(psurf))
352 {
353 SURFACE_UnlockSurface(psurf);
354 SURFACE_FreeSurfaceByHandle(hbmp);
355 return 0;
356 }
357 pso = &psurf->SurfObj;
358
359 if (Format == BMF_4RLE)
360 {
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);
366 }
367 else if (Format == BMF_8RLE)
368 {
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);
374 }
375 else
376 {
377 pso->lDelta = abs(Width);
378 pso->cjBits = pso->lDelta * Size.cy;
379 UncompressedBits = Bits;
380 UncompressedFormat = Format;
381 }
382
383 if (UncompressedBits != NULL)
384 {
385 pso->pvBits = UncompressedBits;
386 }
387 else
388 {
389 if (pso->cjBits == 0)
390 {
391 pso->pvBits = NULL;
392 }
393 else
394 {
395 if (0 != (Flags & BMF_USERMEM))
396 {
397 pso->pvBits = EngAllocUserMem(pso->cjBits, 0);
398 }
399 else
400 {
401 pso->pvBits = EngAllocMem(0 != (Flags & BMF_NOZEROINIT) ?
402 0 : FL_ZERO_MEMORY,
403 pso->cjBits, TAG_DIB);
404 }
405 if (pso->pvBits == NULL)
406 {
407 SURFACE_UnlockSurface(psurf);
408 SURFACE_FreeSurfaceByHandle(hbmp);
409 SetLastWin32Error(ERROR_NOT_ENOUGH_MEMORY);
410 return 0;
411 }
412 }
413 }
414
415 if (0 == (Flags & BMF_TOPDOWN))
416 {
417 pso->pvScan0 = (PVOID)((ULONG_PTR)pso->pvBits + pso->cjBits - pso->lDelta);
418 pso->lDelta = - pso->lDelta;
419 }
420 else
421 {
422 pso->pvScan0 = pso->pvBits;
423 }
424
425 pso->dhsurf = 0; /* device managed surface */
426 pso->hsurf = (HSURF)hbmp;
427 pso->dhpdev = NULL;
428 pso->hdev = NULL;
429 pso->sizlBitmap = Size;
430 pso->iBitmapFormat = UncompressedFormat;
431 pso->iType = STYPE_BITMAP;
432 pso->fjBitmap = Flags & (BMF_TOPDOWN | BMF_NOZEROINIT);
433 pso->iUniq = 0;
434
435 psurf->flHooks = 0;
436 psurf->flFlags = 0;
437 psurf->dimension.cx = 0;
438 psurf->dimension.cy = 0;
439
440 psurf->hSecure = NULL;
441 psurf->hDIBSection = NULL;
442
443 SURFACE_UnlockSurface(psurf);
444
445 return hbmp;
446 }
447
448 /*
449 * @implemented
450 */
451 HBITMAP APIENTRY
452 EngCreateBitmap(IN SIZEL Size,
453 IN LONG Width,
454 IN ULONG Format,
455 IN ULONG Flags,
456 IN PVOID Bits)
457 {
458 HBITMAP hNewBitmap;
459
460 hNewBitmap = IntCreateBitmap(Size, Width, Format, Flags, Bits);
461 if ( !hNewBitmap )
462 return 0;
463
464 GDIOBJ_SetOwnership(hNewBitmap, NULL);
465
466 return hNewBitmap;
467 }
468
469 /*
470 * @unimplemented
471 */
472 HSURF APIENTRY
473 EngCreateDeviceSurface(IN DHSURF dhsurf,
474 IN SIZEL Size,
475 IN ULONG Format)
476 {
477 HSURF hsurf;
478 SURFOBJ *pso;
479 PSURFACE psurf;
480
481 psurf = SURFACE_AllocSurfaceWithHandle();
482 if (!psurf)
483 {
484 return 0;
485 }
486
487 hsurf = psurf->BaseObject.hHmgr;
488 GDIOBJ_SetOwnership(hsurf, NULL);
489
490 if (!SURFACE_InitBitsLock(psurf))
491 {
492 SURFACE_UnlockSurface(psurf);
493 SURFACE_FreeSurfaceByHandle(hsurf);
494 return 0;
495 }
496 pso = &psurf->SurfObj;
497
498 pso->dhsurf = dhsurf;
499 pso->hsurf = hsurf;
500 pso->sizlBitmap = Size;
501 pso->iBitmapFormat = Format;
502 pso->lDelta = DIB_GetDIBWidthBytes(Size.cx, BitsPerFormat(Format));
503 pso->iType = STYPE_DEVICE;
504 pso->iUniq = 0;
505
506 psurf->flHooks = 0;
507
508 SURFACE_UnlockSurface(psurf);
509
510 return hsurf;
511 }
512
513 /*
514 * @implemented
515 */
516 BOOL
517 APIENTRY
518 EngAssociateSurface(
519 IN HSURF hsurf,
520 IN HDEV hdev,
521 IN FLONG flHooks)
522 {
523 SURFOBJ *pso;
524 PSURFACE psurf;
525 PDEVOBJ* ppdev;
526
527 ppdev = (PDEVOBJ*)hdev;
528
529 /* Lock the surface */
530 psurf = SURFACE_LockSurface(hsurf);
531 if (!psurf)
532 {
533 return FALSE;
534 }
535 pso = &psurf->SurfObj;
536
537 /* Associate the hdev */
538 pso->hdev = hdev;
539 pso->dhpdev = ppdev->dhpdev;
540
541 /* Hook up specified functions */
542 psurf->flHooks = flHooks;
543
544 SURFACE_UnlockSurface(psurf);
545
546 return TRUE;
547 }
548
549 /*
550 * @implemented
551 */
552 BOOL APIENTRY
553 EngModifySurface(
554 IN HSURF hsurf,
555 IN HDEV hdev,
556 IN FLONG flHooks,
557 IN FLONG flSurface,
558 IN DHSURF dhsurf,
559 OUT VOID *pvScan0,
560 IN LONG lDelta,
561 IN VOID *pvReserved)
562 {
563 SURFOBJ *pso;
564 PSURFACE psurf;
565 PDEVOBJ* ppdev;
566
567 psurf = SURFACE_LockSurface(hsurf);
568 if (psurf == NULL)
569 {
570 return FALSE;
571 }
572
573 ppdev = (PDEVOBJ*)hdev;
574 pso = &psurf->SurfObj;
575 pso->dhsurf = dhsurf;
576 pso->lDelta = lDelta;
577 pso->pvScan0 = pvScan0;
578
579 /* Associate the hdev */
580 pso->hdev = hdev;
581 pso->dhpdev = ppdev->dhpdev;
582
583 /* Hook up specified functions */
584 psurf->flHooks = flHooks;
585
586 SURFACE_UnlockSurface(psurf);
587
588 return TRUE;
589 }
590
591 /*
592 * @implemented
593 */
594 BOOL APIENTRY
595 EngDeleteSurface(IN HSURF hsurf)
596 {
597 GDIOBJ_SetOwnership(hsurf, PsGetCurrentProcess());
598 SURFACE_FreeSurfaceByHandle(hsurf);
599 return TRUE;
600 }
601
602 /*
603 * @implemented
604 */
605 BOOL APIENTRY
606 EngEraseSurface(SURFOBJ *pso,
607 RECTL *Rect,
608 ULONG iColor)
609 {
610 ASSERT(pso);
611 ASSERT(Rect);
612 return FillSolid(pso, Rect, iColor);
613 }
614
615 /*
616 * @implemented
617 */
618 SURFOBJ * APIENTRY
619 NtGdiEngLockSurface(IN HSURF hsurf)
620 {
621 return EngLockSurface(hsurf);
622 }
623
624
625 /*
626 * @implemented
627 */
628 SURFOBJ * APIENTRY
629 EngLockSurface(IN HSURF hsurf)
630 {
631 SURFACE *psurf = GDIOBJ_ShareLockObj(hsurf, GDI_OBJECT_TYPE_BITMAP);
632
633 if (psurf != NULL)
634 return &psurf->SurfObj;
635
636 return NULL;
637 }
638
639
640 /*
641 * @implemented
642 */
643 VOID APIENTRY
644 NtGdiEngUnlockSurface(IN SURFOBJ *pso)
645 {
646 EngUnlockSurface(pso);
647 }
648
649 /*
650 * @implemented
651 */
652 VOID APIENTRY
653 EngUnlockSurface(IN SURFOBJ *pso)
654 {
655 if (pso != NULL)
656 {
657 SURFACE *psurf = CONTAINING_RECORD(pso, SURFACE, SurfObj);
658 GDIOBJ_ShareUnlockObjByPtr((POBJ)psurf);
659 }
660 }
661
662
663 /* EOF */