03efa6f035f186db3cbfac634acd05748dd62116
[reactos.git] / reactos / win32ss / gdi / ntgdi / dclife.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: Functions for creation and destruction of DCs
5 * FILE: subsystem/win32/win32k/objects/dclife.c
6 * PROGRAMER: Timo Kreuzer (timo.kreuzer@rectos.org)
7 */
8
9 #include <win32k.h>
10
11 #define NDEBUG
12 #include <debug.h>
13
14 // FIXME: Windows uses 0x0012009f
15 #define DIRTY_DEFAULT DIRTY_CHARSET|DIRTY_BACKGROUND|DIRTY_TEXT|DIRTY_LINE|DIRTY_FILL
16
17 PSURFACE psurfDefaultBitmap = NULL;
18 PBRUSH pbrDefaultBrush = NULL;
19
20 static const MATRIX gmxWorldToDeviceDefault =
21 {
22 FLOATOBJ_16, FLOATOBJ_0,
23 FLOATOBJ_0, FLOATOBJ_16,
24 FLOATOBJ_0, FLOATOBJ_0,
25 0, 0, 0x4b
26 };
27
28 static const MATRIX gmxDeviceToWorldDefault =
29 {
30 FLOATOBJ_1_16, FLOATOBJ_0,
31 FLOATOBJ_0, FLOATOBJ_1_16,
32 FLOATOBJ_0, FLOATOBJ_0,
33 0, 0, 0x53
34 };
35
36 static const MATRIX gmxWorldToPageDefault =
37 {
38 FLOATOBJ_1, FLOATOBJ_0,
39 FLOATOBJ_0, FLOATOBJ_1,
40 FLOATOBJ_0, FLOATOBJ_0,
41 0, 0, 0x63
42 };
43
44 // HACK!! Fix XFORMOBJ then use 1:16 / 16:1
45 #define gmxWorldToDeviceDefault gmxWorldToPageDefault
46 #define gmxDeviceToWorldDefault gmxWorldToPageDefault
47
48 /** Internal functions ********************************************************/
49
50 INIT_FUNCTION
51 NTSTATUS
52 NTAPI
53 InitDcImpl()
54 {
55 psurfDefaultBitmap = SURFACE_ShareLockSurface(StockObjects[DEFAULT_BITMAP]);
56 if (!psurfDefaultBitmap)
57 return STATUS_UNSUCCESSFUL;
58
59 pbrDefaultBrush = BRUSH_ShareLockBrush(StockObjects[BLACK_BRUSH]);
60 if (!pbrDefaultBrush)
61 return STATUS_UNSUCCESSFUL;
62
63 return STATUS_SUCCESS;
64 }
65
66
67 PDC
68 NTAPI
69 DC_AllocDcWithHandle()
70 {
71 PDC pdc;
72
73 pdc = (PDC)GDIOBJ_AllocateObject(GDIObjType_DC_TYPE,
74 sizeof(DC),
75 BASEFLAG_LOOKASIDE);
76 if (!pdc)
77 {
78 DPRINT1("Could not allocate a DC.\n");
79 return NULL;
80 }
81
82 if (!GDIOBJ_hInsertObject(&pdc->BaseObject, GDI_OBJ_HMGR_POWNED))
83 {
84 DPRINT1("Could not insert DC into handle table.\n");
85 GDIOBJ_vFreeObject(&pdc->BaseObject);
86 return NULL;
87 }
88
89 pdc->pdcattr = &pdc->dcattr;
90
91 return pdc;
92 }
93
94
95 void
96 DC_InitHack(PDC pdc)
97 {
98 HRGN hVisRgn;
99
100 TextIntRealizeFont(pdc->pdcattr->hlfntNew,NULL);
101 pdc->pdcattr->iCS_CP = ftGdiGetTextCharsetInfo(pdc,NULL,0);
102
103 /* This should never fail */
104 ASSERT(pdc->dclevel.ppal);
105
106 /* Select regions */
107 pdc->rosdc.hClipRgn = NULL;
108 pdc->rosdc.hGCClipRgn = NULL;
109
110 hVisRgn = IntSysCreateRectRgn(0, 0, 1, 1);
111 ASSERT(hVisRgn);
112 GdiSelectVisRgn(pdc->BaseObject.hHmgr, hVisRgn);
113 GreDeleteObject(hVisRgn);
114 }
115
116 VOID
117 NTAPI
118 DC_vInitDc(
119 PDC pdc,
120 DCTYPE dctype,
121 PPDEVOBJ ppdev)
122 {
123 /* Setup some basic fields */
124 pdc->dctype = dctype;
125 pdc->ppdev = ppdev;
126 pdc->dhpdev = ppdev->dhpdev;
127 pdc->hsem = ppdev->hsemDevLock;
128 pdc->flGraphicsCaps = ppdev->devinfo.flGraphicsCaps;
129 pdc->flGraphicsCaps2 = ppdev->devinfo.flGraphicsCaps2;
130 pdc->fs = DC_DIRTY_RAO;
131
132 /* Setup dc attribute */
133 pdc->pdcattr = &pdc->dcattr;
134 pdc->dcattr.pvLDC = NULL;
135 pdc->dcattr.ulDirty_ = DIRTY_DEFAULT;
136 if (ppdev == gppdevPrimary)
137 pdc->dcattr.ulDirty_ |= DC_PRIMARY_DISPLAY;
138
139 /* Setup the DC size */
140 if (dctype == DCTYPE_MEMORY)
141 {
142 /* Memory DCs have a 1 x 1 bitmap by default */
143 pdc->dclevel.sizl.cx = 1;
144 pdc->dclevel.sizl.cy = 1;
145 }
146 else
147 {
148 /* Other DC's are as big as the related PDEV */
149 pdc->dclevel.sizl.cx = ppdev->gdiinfo.ulHorzRes;
150 pdc->dclevel.sizl.cy = ppdev->gdiinfo.ulVertRes;
151 }
152
153 /* Setup Window rect based on DC size */
154 pdc->erclWindow.left = 0;
155 pdc->erclWindow.top = 0;
156 pdc->erclWindow.right = pdc->dclevel.sizl.cx;
157 pdc->erclWindow.bottom = pdc->dclevel.sizl.cy;
158
159 if (dctype == DCTYPE_DIRECT)
160 {
161 /* Direct DCs get the surface from the PDEV */
162 pdc->dclevel.pSurface = PDEVOBJ_pSurface(ppdev);
163
164 pdc->erclBounds.left = 0x7fffffff;
165 pdc->erclBounds.top = 0x7fffffff;
166 pdc->erclBounds.right = 0x80000000;
167 pdc->erclBounds.bottom = 0x80000000;
168 pdc->erclBoundsApp.left = 0xffffffff;
169 pdc->erclBoundsApp.top = 0xfffffffc;
170 pdc->erclBoundsApp.right = 0x00007ffc; // FIXME
171 pdc->erclBoundsApp.bottom = 0x00000333; // FIXME
172 pdc->erclClip = pdc->erclBounds;
173 // pdc->co
174
175 pdc->fs |= DC_SYNCHRONIZEACCESS | DC_ACCUM_APP | DC_PERMANANT | DC_DISPLAY;
176 }
177 else
178 {
179 /* Non-direct DCs don't have a surface by default */
180 pdc->dclevel.pSurface = NULL;
181
182 // FIXME: HACK, because our code expects a surface
183 pdc->dclevel.pSurface = SURFACE_ShareLockSurface(StockObjects[DEFAULT_BITMAP]);
184
185 pdc->erclBounds.left = 0;
186 pdc->erclBounds.top = 0;
187 pdc->erclBounds.right = 0;
188 pdc->erclBounds.bottom = 0;
189 pdc->erclBoundsApp = pdc->erclBounds;
190 pdc->erclClip = pdc->erclWindow;
191 //pdc->co = NULL
192 }
193
194 //pdc->dcattr.VisRectRegion:
195
196 /* Setup coordinate transformation data */
197 pdc->dclevel.mxWorldToDevice = gmxWorldToDeviceDefault;
198 pdc->dclevel.mxDeviceToWorld = gmxDeviceToWorldDefault;
199 pdc->dclevel.mxWorldToPage = gmxWorldToPageDefault;
200 pdc->dclevel.efM11PtoD = gef16;
201 pdc->dclevel.efM22PtoD = gef16;
202 pdc->dclevel.efDxPtoD = gef0;
203 pdc->dclevel.efDyPtoD = gef0;
204 pdc->dclevel.efM11_TWIPS = gef0;
205 pdc->dclevel.efM22_TWIPS = gef0;
206 pdc->dclevel.efPr11 = gef0;
207 pdc->dclevel.efPr22 = gef0;
208 pdc->dcattr.mxWorldToDevice = pdc->dclevel.mxWorldToDevice;
209 pdc->dcattr.mxDeviceToWorld = pdc->dclevel.mxDeviceToWorld;
210 pdc->dcattr.mxWorldToPage = pdc->dclevel.mxWorldToPage;
211 pdc->dcattr.efM11PtoD = pdc->dclevel.efM11PtoD;
212 pdc->dcattr.efM22PtoD = pdc->dclevel.efM22PtoD;
213 pdc->dcattr.efDxPtoD = pdc->dclevel.efDxPtoD;
214 pdc->dcattr.efDyPtoD = pdc->dclevel.efDyPtoD;
215 pdc->dcattr.iMapMode = MM_TEXT;
216 pdc->dcattr.dwLayout = 0;
217 pdc->dcattr.flXform = PAGE_TO_DEVICE_SCALE_IDENTITY |
218 PAGE_TO_DEVICE_IDENTITY |
219 WORLD_TO_PAGE_IDENTITY;
220
221 /* Setup more coordinates */
222 pdc->ptlDCOrig.x = 0;
223 pdc->ptlDCOrig.y = 0;
224 pdc->dcattr.lWindowOrgx = 0;
225 pdc->dcattr.ptlWindowOrg.x = 0;
226 pdc->dcattr.ptlWindowOrg.y = 0;
227 pdc->dcattr.szlWindowExt.cx = 1;
228 pdc->dcattr.szlWindowExt.cy = 1;
229 pdc->dcattr.ptlViewportOrg.x = 0;
230 pdc->dcattr.ptlViewportOrg.y = 0;
231 pdc->dcattr.szlViewportExt.cx = 1;
232 pdc->dcattr.szlViewportExt.cy = 1;
233 pdc->dcattr.szlVirtualDevicePixel.cx = ppdev->gdiinfo.ulHorzRes;
234 pdc->dcattr.szlVirtualDevicePixel.cy = ppdev->gdiinfo.ulVertRes;
235 pdc->dcattr.szlVirtualDeviceMm.cx = ppdev->gdiinfo.ulHorzSize;
236 pdc->dcattr.szlVirtualDeviceMm.cy = ppdev->gdiinfo.ulVertSize;
237 pdc->dcattr.szlVirtualDeviceSize.cx = 0;
238 pdc->dcattr.szlVirtualDeviceSize.cy = 0;
239
240 /* Setup regions */
241 pdc->prgnAPI = NULL;
242 pdc->prgnRao = NULL;
243 /* Allocate a Vis region */
244 pdc->prgnVis = IntSysCreateRectpRgn(0, 0, pdc->dclevel.sizl.cx, pdc->dclevel.sizl.cy);
245 ASSERT(pdc->prgnVis);
246
247 /* Setup palette */
248 pdc->dclevel.hpal = StockObjects[DEFAULT_PALETTE];
249 pdc->dclevel.ppal = PALETTE_ShareLockPalette(pdc->dclevel.hpal);
250
251 /* Setup path */
252 pdc->dclevel.hPath = NULL;
253 pdc->dclevel.flPath = 0;
254 // pdc->dclevel.lapath:
255
256 /* Setup colors */
257 pdc->dcattr.crBackgroundClr = RGB(0xff, 0xff, 0xff);
258 pdc->dcattr.ulBackgroundClr = RGB(0xff, 0xff, 0xff);
259 pdc->dcattr.crForegroundClr = RGB(0, 0, 0);
260 pdc->dcattr.ulForegroundClr = RGB(0, 0, 0);
261 pdc->dcattr.crBrushClr = RGB(0xff, 0xff, 0xff);
262 pdc->dcattr.ulBrushClr = RGB(0xff, 0xff, 0xff);
263 pdc->dcattr.crPenClr = RGB(0, 0, 0);
264 pdc->dcattr.ulPenClr = RGB(0, 0, 0);
265
266 /* Select the default fill and line brush */
267 pdc->dcattr.hbrush = StockObjects[WHITE_BRUSH];
268 pdc->dcattr.hpen = StockObjects[BLACK_PEN];
269 pdc->dclevel.pbrFill = BRUSH_ShareLockBrush(pdc->pdcattr->hbrush);
270 pdc->dclevel.pbrLine = PEN_ShareLockPen(pdc->pdcattr->hpen);
271 pdc->dclevel.ptlBrushOrigin.x = 0;
272 pdc->dclevel.ptlBrushOrigin.y = 0;
273 pdc->dcattr.ptlBrushOrigin = pdc->dclevel.ptlBrushOrigin;
274
275 /* Initialize EBRUSHOBJs */
276 EBRUSHOBJ_vInit(&pdc->eboFill, pdc->dclevel.pbrFill, pdc);
277 EBRUSHOBJ_vInit(&pdc->eboLine, pdc->dclevel.pbrLine, pdc);
278 EBRUSHOBJ_vInit(&pdc->eboText, pbrDefaultBrush, pdc);
279 EBRUSHOBJ_vInit(&pdc->eboBackground, pbrDefaultBrush, pdc);
280
281 /* Setup fill data */
282 pdc->dcattr.jROP2 = R2_COPYPEN;
283 pdc->dcattr.jBkMode = 2;
284 pdc->dcattr.lBkMode = 2;
285 pdc->dcattr.jFillMode = ALTERNATE;
286 pdc->dcattr.lFillMode = 1;
287 pdc->dcattr.jStretchBltMode = 1;
288 pdc->dcattr.lStretchBltMode = 1;
289 pdc->ptlFillOrigin.x = 0;
290 pdc->ptlFillOrigin.y = 0;
291
292 /* Setup drawing position */
293 pdc->dcattr.ptlCurrent.x = 0;
294 pdc->dcattr.ptlCurrent.y = 0;
295 pdc->dcattr.ptfxCurrent.x = 0;
296 pdc->dcattr.ptfxCurrent.y = 0;
297
298 /* Setup ICM data */
299 pdc->dclevel.lIcmMode = 0;
300 pdc->dcattr.lIcmMode = 0;
301 pdc->dcattr.hcmXform = NULL;
302 pdc->dcattr.flIcmFlags = 0;
303 pdc->dcattr.IcmBrushColor = CLR_INVALID;
304 pdc->dcattr.IcmPenColor = CLR_INVALID;
305 pdc->dcattr.pvLIcm = NULL;
306 pdc->dcattr.hColorSpace = NULL; // FIXME: 0189001f
307 pdc->dclevel.pColorSpace = NULL; // FIXME
308 pdc->pClrxFormLnk = NULL;
309 // pdc->dclevel.ca =
310
311 /* Setup font data */
312 pdc->hlfntCur = NULL; // FIXME: 2f0a0cf8
313 pdc->pPFFList = NULL;
314 pdc->flSimulationFlags = 0;
315 pdc->lEscapement = 0;
316 pdc->prfnt = NULL;
317 pdc->dcattr.flFontMapper = 0;
318 pdc->dcattr.flTextAlign = 0;
319 pdc->dcattr.lTextAlign = 0;
320 pdc->dcattr.lTextExtra = 0;
321 pdc->dcattr.lRelAbs = 1;
322 pdc->dcattr.lBreakExtra = 0;
323 pdc->dcattr.cBreak = 0;
324 pdc->dcattr.hlfntNew = StockObjects[SYSTEM_FONT];
325 // pdc->dclevel.pFont = LFONT_ShareLockFont(pdc->dcattr.hlfntNew);
326
327 /* Other stuff */
328 pdc->hdcNext = NULL;
329 pdc->hdcPrev = NULL;
330 pdc->ipfdDevMax = 0x0000ffff;
331 pdc->ulCopyCount = -1;
332 pdc->ptlDoBanding.x = 0;
333 pdc->ptlDoBanding.y = 0;
334 pdc->dclevel.lSaveDepth = 1;
335 pdc->dclevel.hdcSave = NULL;
336 pdc->dcattr.iGraphicsMode = GM_COMPATIBLE;
337 pdc->dcattr.iCS_CP = 0;
338 pdc->pSurfInfo = NULL;
339
340 if (defaultDCstate == NULL)
341 {
342 defaultDCstate = ExAllocatePoolWithTag(PagedPool, sizeof(DC), TAG_DC);
343 RtlZeroMemory(defaultDCstate, sizeof(DC));
344 defaultDCstate->pdcattr = &defaultDCstate->dcattr;
345 DC_vCopyState(pdc, defaultDCstate, TRUE);
346 }
347 }
348
349 BOOL
350 NTAPI
351 DC_Cleanup(PVOID ObjectBody)
352 {
353 PDC pdc = (PDC)ObjectBody;
354
355 /* Free DC_ATTR */
356 DC_vFreeDcAttr(pdc);
357
358 /* Delete saved DCs */
359 DC_vRestoreDC(pdc, 1);
360
361 /* Deselect dc objects */
362 DC_vSelectSurface(pdc, NULL);
363 DC_vSelectFillBrush(pdc, NULL);
364 DC_vSelectLineBrush(pdc, NULL);
365 DC_vSelectPalette(pdc, NULL);
366
367 /* Cleanup the dc brushes */
368 EBRUSHOBJ_vCleanup(&pdc->eboFill);
369 EBRUSHOBJ_vCleanup(&pdc->eboLine);
370 EBRUSHOBJ_vCleanup(&pdc->eboText);
371 EBRUSHOBJ_vCleanup(&pdc->eboBackground);
372
373 /* Free regions */
374 if (pdc->rosdc.hClipRgn && GreIsHandleValid(pdc->rosdc.hClipRgn))
375 GreDeleteObject(pdc->rosdc.hClipRgn);
376 if (pdc->prgnVis)
377 {
378 REGION_Delete(pdc->prgnVis);
379 }
380 if (pdc->rosdc.hGCClipRgn && GreIsHandleValid(pdc->rosdc.hGCClipRgn))
381 {
382 GreDeleteObject(pdc->rosdc.hGCClipRgn);
383 }
384 if (NULL != pdc->rosdc.CombinedClip)
385 IntEngDeleteClipRegion(pdc->rosdc.CombinedClip);
386
387 PATH_Delete(pdc->dclevel.hPath);
388
389 if(pdc->dclevel.pSurface)
390 SURFACE_ShareUnlockSurface(pdc->dclevel.pSurface);
391
392 PDEVOBJ_vRelease(pdc->ppdev) ;
393
394 return TRUE;
395 }
396
397 VOID
398 NTAPI
399 DC_vSetOwner(PDC pdc, ULONG ulOwner)
400 {
401
402 if (pdc->rosdc.hClipRgn)
403 {
404 IntGdiSetRegionOwner(pdc->rosdc.hClipRgn, ulOwner);
405 }
406
407 if (pdc->rosdc.hGCClipRgn)
408 {
409 IntGdiSetRegionOwner(pdc->rosdc.hGCClipRgn, ulOwner);
410 }
411
412 if (pdc->dclevel.hPath)
413 {
414 GreSetObjectOwner(pdc->dclevel.hPath, ulOwner);
415 }
416
417 IntGdiSetBrushOwner(pdc->dclevel.pbrFill, ulOwner);
418 IntGdiSetBrushOwner(pdc->dclevel.pbrLine, ulOwner);
419
420 /* Allocate or free DC attribute */
421 if (ulOwner == GDI_OBJ_HMGR_PUBLIC || ulOwner == GDI_OBJ_HMGR_NONE)
422 {
423 if (pdc->pdcattr != &pdc->dcattr)
424 DC_vFreeDcAttr(pdc);
425 }
426 else if (ulOwner == GDI_OBJ_HMGR_POWNED)
427 {
428 if (pdc->pdcattr == &pdc->dcattr)
429 DC_bAllocDcAttr(pdc);
430 }
431
432 /* Set the DC's ownership */
433 GDIOBJ_vSetObjectOwner(&pdc->BaseObject, ulOwner);
434 }
435
436 BOOL
437 NTAPI
438 GreSetDCOwner(HDC hdc, ULONG ulOwner)
439 {
440 PDC pdc;
441
442 pdc = DC_LockDc(hdc);
443 if (!pdc)
444 {
445 DPRINT1("GreSetDCOwner: Could not lock DC\n");
446 return FALSE;
447 }
448
449 /* Call the internal DC function */
450 DC_vSetOwner(pdc, ulOwner);
451
452 DC_UnlockDc(pdc);
453 return TRUE;
454 }
455
456 static
457 void
458 DC_vUpdateDC(PDC pdc)
459 {
460 HRGN hVisRgn ;
461 PPDEVOBJ ppdev = pdc->ppdev ;
462
463 pdc->dhpdev = ppdev->dhpdev;
464
465 SURFACE_ShareUnlockSurface(pdc->dclevel.pSurface);
466 pdc->dclevel.pSurface = PDEVOBJ_pSurface(ppdev);
467
468 PDEVOBJ_sizl(pdc->ppdev, &pdc->dclevel.sizl);
469 hVisRgn = NtGdiCreateRectRgn(0, 0, pdc->dclevel.sizl.cx, pdc->dclevel.sizl.cy);
470 ASSERT(hVisRgn);
471 GdiSelectVisRgn(pdc->BaseObject.hHmgr, hVisRgn);
472 GreDeleteObject(hVisRgn);
473
474 pdc->flGraphicsCaps = ppdev->devinfo.flGraphicsCaps;
475 pdc->flGraphicsCaps2 = ppdev->devinfo.flGraphicsCaps2;
476
477 /* Mark EBRUSHOBJs as dirty */
478 pdc->pdcattr->ulDirty_ |= DIRTY_DEFAULT ;
479 }
480
481 /* Prepare a blit for up to 2 DCs */
482 /* rc1 and rc2 are the rectangles where we want to draw or
483 * from where we take pixels. */
484 VOID
485 FASTCALL
486 DC_vPrepareDCsForBlit(PDC pdc1,
487 RECT rc1,
488 PDC pdc2,
489 RECT rc2)
490 {
491 PDC pdcFirst, pdcSecond;
492 PRECT prcFirst, prcSecond;
493
494 /* Update brushes */
495 if (pdc1->pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
496 DC_vUpdateFillBrush(pdc1);
497 if (pdc1->pdcattr->ulDirty_ & (DIRTY_LINE | DC_PEN_DIRTY))
498 DC_vUpdateLineBrush(pdc1);
499 if(pdc1->pdcattr->ulDirty_ & DIRTY_TEXT)
500 DC_vUpdateTextBrush(pdc1);
501
502 /* Lock them in good order */
503 if(pdc2)
504 {
505 if((ULONG_PTR)pdc1->ppdev->hsemDevLock >= (ULONG_PTR)pdc2->ppdev->hsemDevLock)
506 {
507 pdcFirst = pdc1;
508 prcFirst = &rc1;
509 pdcSecond = pdc2;
510 prcSecond = &rc2;
511 }
512 else
513 {
514 pdcFirst = pdc2;
515 prcFirst = &rc2;
516 pdcSecond = pdc1;
517 prcSecond = &rc1;
518 }
519 }
520 else
521 {
522 pdcFirst = pdc1 ;
523 prcFirst = &rc1;
524 pdcSecond = NULL;
525 prcSecond = NULL;
526 }
527
528 if(pdcFirst && pdcFirst->dctype == DCTYPE_DIRECT)
529 {
530 EngAcquireSemaphore(pdcFirst->ppdev->hsemDevLock);
531 MouseSafetyOnDrawStart(pdcFirst->ppdev,
532 prcFirst->left,
533 prcFirst->top,
534 prcFirst->right,
535 prcFirst->bottom) ;
536 /* Update surface if needed */
537 if(pdcFirst->ppdev->pSurface != pdcFirst->dclevel.pSurface)
538 {
539 DC_vUpdateDC(pdcFirst);
540 }
541 }
542 if(pdcSecond && pdcSecond->dctype == DCTYPE_DIRECT)
543 {
544 EngAcquireSemaphore(pdcSecond->ppdev->hsemDevLock);
545 MouseSafetyOnDrawStart(pdcSecond->ppdev,
546 prcSecond->left,
547 prcSecond->top,
548 prcSecond->right,
549 prcSecond->bottom) ;
550 /* Update surface if needed */
551 if(pdcSecond->ppdev->pSurface != pdcSecond->dclevel.pSurface)
552 {
553 DC_vUpdateDC(pdcSecond);
554 }
555 }
556 }
557
558 /* Finishes a blit for one or two DCs */
559 VOID
560 FASTCALL
561 DC_vFinishBlit(PDC pdc1, PDC pdc2)
562 {
563 if(pdc1->dctype == DCTYPE_DIRECT)
564 {
565 MouseSafetyOnDrawEnd(pdc1->ppdev);
566 EngReleaseSemaphore(pdc1->ppdev->hsemDevLock);
567 }
568
569 if(pdc2)
570 {
571 if(pdc2->dctype == DCTYPE_DIRECT)
572 {
573 MouseSafetyOnDrawEnd(pdc2->ppdev);
574 EngReleaseSemaphore(pdc2->ppdev->hsemDevLock);
575 }
576 }
577 }
578
579 HDC
580 NTAPI
581 GreOpenDCW(
582 PUNICODE_STRING pustrDevice,
583 DEVMODEW *pdmInit,
584 PUNICODE_STRING pustrLogAddr,
585 ULONG iType,
586 BOOL bDisplay,
587 HANDLE hspool,
588 VOID *pDriverInfo2,
589 VOID *pUMdhpdev)
590 {
591 PPDEVOBJ ppdev;
592 PDC pdc;
593 HDC hdc;
594
595 DPRINT("GreOpenDCW(%S, iType=%ld)\n",
596 pustrDevice ? pustrDevice->Buffer : NULL, iType);
597
598 /* Get a PDEVOBJ for the device */
599 ppdev = EngpGetPDEV(pustrDevice);
600 if (!ppdev)
601 {
602 DPRINT1("Didn't find a suitable PDEV\n");
603 return NULL;
604 }
605
606 DPRINT("GreOpenDCW - ppdev = %p\n", ppdev);
607
608 pdc = DC_AllocDcWithHandle();
609 if (!pdc)
610 {
611 DPRINT1("Could not Allocate a DC\n");
612 PDEVOBJ_vRelease(ppdev);
613 return NULL;
614 }
615 hdc = pdc->BaseObject.hHmgr;
616
617 /* Lock ppdev and initialize the new DC */
618 DC_vInitDc(pdc, iType, ppdev);
619 /* FIXME: HACK! */
620 DC_InitHack(pdc);
621
622 DC_bAllocDcAttr(pdc);
623
624 DC_UnlockDc(pdc);
625
626 DPRINT("Returning hdc = %p\n", hdc);
627
628 return hdc;
629 }
630
631 HDC
632 APIENTRY
633 NtGdiOpenDCW(
634 PUNICODE_STRING pustrDevice,
635 DEVMODEW *pdmInit,
636 PUNICODE_STRING pustrLogAddr,
637 ULONG iType,
638 BOOL bDisplay,
639 HANDLE hspool,
640 VOID *pDriverInfo2,
641 VOID *pUMdhpdev)
642 {
643 UNICODE_STRING ustrDevice;
644 WCHAR awcDevice[CCHDEVICENAME];
645 DEVMODEW dmInit;
646 PVOID dhpdev;
647 HDC hdc;
648
649 /* Only if a devicename is given, we need any data */
650 if (pustrDevice)
651 {
652 /* Initialize destination string */
653 RtlInitEmptyUnicodeString(&ustrDevice, awcDevice, sizeof(awcDevice));
654
655 _SEH2_TRY
656 {
657 /* Probe the UNICODE_STRING and the buffer */
658 ProbeForRead(pustrDevice, sizeof(UNICODE_STRING), 1);
659 ProbeForRead(pustrDevice->Buffer, pustrDevice->Length, 1);
660
661 /* Copy the string */
662 RtlCopyUnicodeString(&ustrDevice, pustrDevice);
663
664 if (pdmInit)
665 {
666 /* FIXME: could be larger */
667 ProbeForRead(pdmInit, sizeof(DEVMODEW), 1);
668 RtlCopyMemory(&dmInit, pdmInit, sizeof(DEVMODEW));
669 }
670
671 if (pUMdhpdev)
672 {
673 ProbeForWrite(pUMdhpdev, sizeof(HANDLE), 1);
674 }
675 }
676 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
677 {
678 SetLastNtError(_SEH2_GetExceptionCode());
679 _SEH2_YIELD(return NULL);
680 }
681 _SEH2_END
682 }
683 else
684 {
685 pdmInit = NULL;
686 pUMdhpdev = NULL;
687 }
688
689 /* FIXME: HACK! */
690 if (pustrDevice)
691 {
692 UNICODE_STRING ustrDISPLAY = RTL_CONSTANT_STRING(L"DISPLAY");
693 if (RtlEqualUnicodeString(&ustrDevice, &ustrDISPLAY, TRUE))
694 {
695 pustrDevice = NULL;
696 }
697 }
698
699 /* Call the internal function */
700 hdc = GreOpenDCW(pustrDevice ? &ustrDevice : NULL,
701 pdmInit ? &dmInit : NULL,
702 NULL, // FIXME: pwszLogAddress
703 iType,
704 bDisplay,
705 hspool,
706 NULL, // FIXME: pDriverInfo2
707 pUMdhpdev ? &dhpdev : NULL);
708
709 /* If we got a HDC and a UM dhpdev is requested,... */
710 if (hdc && pUMdhpdev)
711 {
712 /* Copy dhpdev to caller (FIXME: use dhpdev?) */
713 _SEH2_TRY
714 {
715 /* Pointer was already probed */
716 *(HANDLE*)pUMdhpdev = dhpdev;
717 }
718 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
719 {
720 /* Ignore error */
721 }
722 _SEH2_END
723 }
724
725 return hdc;
726 }
727
728
729 HDC
730 APIENTRY
731 NtGdiCreateCompatibleDC(HDC hdc)
732 {
733 HDC hdcNew;
734 PPDEVOBJ ppdev;
735 PDC pdc, pdcNew;
736
737 DPRINT("NtGdiCreateCompatibleDC(0x%p)\n", hdc);
738
739 /* Did the caller provide a DC? */
740 if (hdc)
741 {
742 /* Yes, try to lock it */
743 pdc = DC_LockDc(hdc);
744 if (!pdc)
745 {
746 DPRINT1("Could not lock source DC %p\n", hdc);
747 return NULL;
748 }
749
750 /* Get the pdev from the DC */
751 ppdev = pdc->ppdev;
752 InterlockedIncrement(&ppdev->cPdevRefs);
753
754 /* Unlock the source DC */
755 DC_UnlockDc(pdc);
756 }
757 else
758 {
759 /* No DC given, get default device */
760 ppdev = EngpGetPDEV(NULL);
761 }
762
763 if (!ppdev)
764 {
765 DPRINT1("Didn't find a suitable PDEV\n");
766 return NULL;
767 }
768
769 /* Allocate a new DC */
770 pdcNew = DC_AllocDcWithHandle();
771 if (!pdcNew)
772 {
773 DPRINT1("Could not allocate a new DC\n");
774 PDEVOBJ_vRelease(ppdev);
775 return NULL;
776 }
777 hdcNew = pdcNew->BaseObject.hHmgr;
778
779 /* Lock ppdev and initialize the new DC */
780 DC_vInitDc(pdcNew, DCTYPE_MEMORY, ppdev);
781 /* FIXME: HACK! */
782 DC_InitHack(pdcNew);
783
784 /* Allocate a dc attribute */
785 DC_bAllocDcAttr(pdcNew);
786
787 // HACK!
788 DC_vSelectSurface(pdcNew, psurfDefaultBitmap);
789
790 DC_UnlockDc(pdcNew);
791
792 DPRINT("Leave NtGdiCreateCompatibleDC hdcNew = %p\n", hdcNew);
793
794 return hdcNew;
795 }
796
797 BOOL
798 FASTCALL
799 IntGdiDeleteDC(HDC hDC, BOOL Force)
800 {
801 PDC DCToDelete = DC_LockDc(hDC);
802
803 if (DCToDelete == NULL)
804 {
805 EngSetLastError(ERROR_INVALID_HANDLE);
806 return FALSE;
807 }
808
809 if (!Force)
810 {
811 /* Windows permits NtGdiDeleteObjectApp to delete a permanent DC
812 * For some reason, it's still a valid handle, pointing to some kernel data.
813 * Not sure if this is a bug, a feature, some cache stuff... Who knows?
814 * See NtGdiDeleteObjectApp test for details */
815 if (DCToDelete->fs & DC_FLAG_PERMANENT)
816 {
817 DC_UnlockDc(DCToDelete);
818 if(UserReleaseDC(NULL, hDC, FALSE))
819 {
820 /* ReactOS feature: Call UserReleaseDC
821 * I don't think Windows does it.
822 * Still, complain, no one should ever call DeleteDC
823 * on a window DC */
824 DPRINT1("No, you naughty application!\n");
825 return TRUE;
826 }
827 else
828 {
829 /* This is not a window owned DC.
830 * Force its deletion */
831 return IntGdiDeleteDC(hDC, TRUE);
832 }
833 }
834 }
835
836 DC_UnlockDc(DCToDelete);
837
838 if (GreIsHandleValid(hDC))
839 {
840 if (!GreDeleteObject(hDC))
841 {
842 DPRINT1("DC_FreeDC failed\n");
843 }
844 }
845 else
846 {
847 DPRINT1("Attempted to Delete 0x%x currently being destroyed!!!\n", hDC);
848 }
849
850 return TRUE;
851 }
852
853 BOOL
854 APIENTRY
855 NtGdiDeleteObjectApp(HANDLE hobj)
856 {
857 /* Complete all pending operations */
858 NtGdiFlushUserBatch(); // FIXME: We shouldn't need this
859
860 if (GDI_HANDLE_IS_STOCKOBJ(hobj)) return TRUE;
861
862 if (GreGetObjectOwner(hobj) != GDI_OBJ_HMGR_POWNED)
863 {
864 EngSetLastError(ERROR_INVALID_HANDLE);
865 return FALSE;
866 }
867
868 if (GDI_HANDLE_GET_TYPE(hobj) != GDI_OBJECT_TYPE_DC)
869 return GreDeleteObject(hobj);
870
871 // FIXME: Everything should be callback based
872 return IntGdiDeleteDC(hobj, FALSE);
873 }
874
875 BOOL
876 FASTCALL
877 MakeInfoDC(PDC pdc, BOOL bSet)
878 {
879 PSURFACE pSurface;
880 SIZEL sizl;
881
882 /* Can not be a display DC. */
883 if (pdc->fs & DC_FLAG_DISPLAY) return FALSE;
884 if (bSet)
885 {
886 if (pdc->fs & DC_FLAG_TEMPINFODC || pdc->dctype == DC_TYPE_DIRECT)
887 return FALSE;
888
889 pSurface = pdc->dclevel.pSurface;
890 pdc->fs |= DC_FLAG_TEMPINFODC;
891 pdc->pSurfInfo = pSurface;
892 pdc->dctype = DC_TYPE_INFO;
893 pdc->dclevel.pSurface = NULL;
894
895 PDEVOBJ_sizl(pdc->ppdev, &sizl);
896
897 if ( sizl.cx == pdc->dclevel.sizl.cx &&
898 sizl.cy == pdc->dclevel.sizl.cy )
899 return TRUE;
900
901 pdc->dclevel.sizl.cx = sizl.cx;
902 pdc->dclevel.sizl.cy = sizl.cy;
903 }
904 else
905 {
906 if (!(pdc->fs & DC_FLAG_TEMPINFODC) || pdc->dctype != DC_TYPE_INFO)
907 return FALSE;
908
909 pSurface = pdc->pSurfInfo;
910 pdc->fs &= ~DC_FLAG_TEMPINFODC;
911 pdc->dclevel.pSurface = pSurface;
912 pdc->dctype = DC_TYPE_DIRECT;
913 pdc->pSurfInfo = NULL;
914
915 if ( !pSurface ||
916 (pSurface->SurfObj.sizlBitmap.cx == pdc->dclevel.sizl.cx &&
917 pSurface->SurfObj.sizlBitmap.cy == pdc->dclevel.sizl.cy) )
918 return TRUE;
919
920 pdc->dclevel.sizl.cx = pSurface->SurfObj.sizlBitmap.cx;
921 pdc->dclevel.sizl.cy = pSurface->SurfObj.sizlBitmap.cy;
922 }
923 return IntSetDefaultRegion(pdc);
924 }
925
926 /*
927 * @implemented
928 */
929 BOOL
930 APIENTRY
931 NtGdiMakeInfoDC(
932 IN HDC hdc,
933 IN BOOL bSet)
934 {
935 BOOL Ret;
936 PDC pdc = DC_LockDc(hdc);
937 if (pdc)
938 {
939 Ret = MakeInfoDC(pdc, bSet);
940 DC_UnlockDc(pdc);
941 return Ret;
942 }
943 return FALSE;
944 }
945
946
947 HDC FASTCALL
948 IntGdiCreateDC(
949 PUNICODE_STRING Driver,
950 PUNICODE_STRING pustrDevice,
951 PVOID pUMdhpdev,
952 CONST PDEVMODEW pdmInit,
953 BOOL CreateAsIC)
954 {
955 HDC hdc;
956
957 hdc = GreOpenDCW(pustrDevice,
958 pdmInit,
959 NULL,
960 CreateAsIC ? DCTYPE_INFO :
961 (Driver ? DC_TYPE_DIRECT : DC_TYPE_DIRECT),
962 TRUE,
963 NULL,
964 NULL,
965 pUMdhpdev);
966
967 return hdc;
968 }
969
970 HDC FASTCALL
971 IntGdiCreateDisplayDC(HDEV hDev, ULONG DcType, BOOL EmptyDC)
972 {
973 HDC hDC;
974 UNIMPLEMENTED;
975 ASSERT(FALSE);
976
977 if (DcType == DC_TYPE_MEMORY)
978 hDC = NtGdiCreateCompatibleDC(NULL); // OH~ Yuck! I think I taste vomit in my mouth!
979 else
980 hDC = IntGdiCreateDC(NULL, NULL, NULL, NULL, (DcType == DC_TYPE_INFO));
981
982 return hDC;
983 }
984