1426770ab315ca71f7d19d42e11ab1c340cca51f
[reactos.git] / subsystems / win32 / win32k / objects / 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 <w32k.h>
10 #include <bugcodes.h>
11
12 #define NDEBUG
13 #include <debug.h>
14
15 //FIXME: windows uses 0x0012009f
16 #define DIRTY_DEFAULT DIRTY_CHARSET|DIRTY_BACKGROUND|DIRTY_TEXT|DIRTY_LINE|DIRTY_FILL
17
18 PSURFACE psurfDefaultBitmap = NULL;
19 PBRUSH pbrDefaultBrush = NULL;
20
21 // FIXME: these should go to floatobj.h or something
22 #define FLOATOBJ_0 {0x00000000, 0x00000000}
23 #define FLOATOBJ_1 {0x40000000, 0x00000002}
24 #define FLOATOBJ_16 {0x40000000, 0x00000006}
25 #define FLOATOBJ_1_16 {0x40000000, 0xfffffffe}
26
27 static const FLOATOBJ gef0 = FLOATOBJ_0;
28 static const FLOATOBJ gef1 = FLOATOBJ_1;
29 static const FLOATOBJ gef16 = FLOATOBJ_16;
30
31 static const MATRIX gmxWorldToDeviceDefault =
32 {
33 FLOATOBJ_16, FLOATOBJ_0,
34 FLOATOBJ_0, FLOATOBJ_16,
35 FLOATOBJ_0, FLOATOBJ_0,
36 0, 0, 0x4b
37 };
38
39 static const MATRIX gmxDeviceToWorldDefault =
40 {
41 FLOATOBJ_1_16, FLOATOBJ_0,
42 FLOATOBJ_0, FLOATOBJ_1_16,
43 FLOATOBJ_0, FLOATOBJ_0,
44 0, 0, 0x53
45 };
46
47 static const MATRIX gmxWorldToPageDefault =
48 {
49 FLOATOBJ_1, FLOATOBJ_0,
50 FLOATOBJ_0, FLOATOBJ_1,
51 FLOATOBJ_0, FLOATOBJ_0,
52 0, 0, 0x63
53 };
54
55 // HACK!! Fix XFORMOBJ then use 1:16 / 16:1
56 #define gmxWorldToDeviceDefault gmxWorldToPageDefault
57 #define gmxDeviceToWorldDefault gmxWorldToPageDefault
58
59 /** Internal functions ********************************************************/
60
61 NTSTATUS
62 InitDcImpl()
63 {
64 psurfDefaultBitmap = SURFACE_ShareLockSurface(StockObjects[DEFAULT_BITMAP]);
65 if (!psurfDefaultBitmap)
66 return STATUS_UNSUCCESSFUL;
67
68 pbrDefaultBrush = BRUSH_ShareLockBrush(StockObjects[BLACK_BRUSH]);
69 if (!pbrDefaultBrush)
70 return STATUS_UNSUCCESSFUL;
71
72 return STATUS_SUCCESS;
73 }
74
75
76 PDC
77 NTAPI
78 DC_AllocDcWithHandle()
79 {
80 PDC pdc;
81 pdc = (PDC)GDIOBJ_AllocObjWithHandle(GDILoObjType_LO_DC_TYPE);
82
83 pdc->pdcattr = &pdc->dcattr;
84
85 return pdc;
86 }
87
88
89 void
90 DC_InitHack(PDC pdc)
91 {
92 HRGN hVisRgn;
93
94 TextIntRealizeFont(pdc->pdcattr->hlfntNew,NULL);
95 pdc->pdcattr->iCS_CP = ftGdiGetTextCharsetInfo(pdc,NULL,0);
96
97 /* This should never fail */
98 ASSERT(pdc->dclevel.ppal);
99
100 /* Select regions */
101 // FIXME: too complicated, broken error handling
102 pdc->rosdc.hClipRgn = NULL;
103 pdc->rosdc.hGCClipRgn = NULL;
104 hVisRgn = NtGdiCreateRectRgn(0, 0, pdc->dclevel.sizl.cx, pdc->dclevel.sizl.cy);
105 ASSERT(hVisRgn);
106 GdiSelectVisRgn(pdc->BaseObject.hHmgr, hVisRgn);
107 GreDeleteObject(hVisRgn);
108 ASSERT(pdc->prgnVis);
109 }
110
111 VOID
112 NTAPI
113 DC_vInitDc(
114 PDC pdc,
115 DCTYPE dctype,
116 PPDEVOBJ ppdev)
117 {
118 if (dctype == DCTYPE_DIRECT)
119 {
120 /* Lock ppdev */
121 EngAcquireSemaphoreShared(ppdev->hsemDevLock);
122 }
123
124 /* Setup some basic fields */
125 pdc->dctype = dctype;
126 pdc->ppdev = ppdev;
127 pdc->dhpdev = ppdev->dhpdev;
128 pdc->hsem = ppdev->hsemDevLock;
129 pdc->flGraphicsCaps = ppdev->devinfo.flGraphicsCaps;
130 pdc->flGraphicsCaps2 = ppdev->devinfo.flGraphicsCaps2;
131 pdc->fs = DC_DIRTY_RAO;
132
133 /* Setup dc attribute */
134 pdc->pdcattr = &pdc->dcattr;
135 pdc->dcattr.pvLDC = NULL;
136 pdc->dcattr.ulDirty_ = DIRTY_DEFAULT;
137 if (ppdev == gppdevPrimary)
138 pdc->dcattr.ulDirty_ |= DC_PRIMARY_DISPLAY;
139
140 /* Setup the DC size */
141 if (dctype == DCTYPE_MEMORY)
142 {
143 /* Memory DCs have a 1 x 1 bitmap by default */
144 pdc->dclevel.sizl.cx = 1;
145 pdc->dclevel.sizl.cy = 1;
146 }
147 else
148 {
149 /* Other DC's are as big as the related PDEV */
150 pdc->dclevel.sizl.cx = ppdev->gdiinfo.ulHorzRes;
151 pdc->dclevel.sizl.cy = ppdev->gdiinfo.ulVertRes;
152 }
153
154 /* Setup Window rect based on DC size */
155 pdc->erclWindow.left = 0;
156 pdc->erclWindow.top = 0;
157 pdc->erclWindow.right = pdc->dclevel.sizl.cx;
158 pdc->erclWindow.bottom = pdc->dclevel.sizl.cy;
159
160 if (dctype == DCTYPE_DIRECT)
161 {
162 /* Direct DCs get the surface from the PDEV */
163 pdc->dclevel.pSurface = PDEVOBJ_pSurface(ppdev);
164
165 pdc->erclBounds.left = 0x7fffffff;
166 pdc->erclBounds.top = 0x7fffffff;
167 pdc->erclBounds.right = 0x80000000;
168 pdc->erclBounds.bottom = 0x80000000;
169 pdc->erclBoundsApp.left = 0xffffffff;
170 pdc->erclBoundsApp.top = 0xfffffffc;
171 pdc->erclBoundsApp.right = 0x00007ffc; // FIXME
172 pdc->erclBoundsApp.bottom = 0x00000333; // FIXME
173 pdc->erclClip = pdc->erclBounds;
174 // pdc->co
175
176 pdc->fs |= DC_SYNCHRONIZEACCESS | DC_ACCUM_APP | DC_PERMANANT | DC_DISPLAY;
177 }
178 else
179 {
180 /* Non-direct DCs don't have a surface by default */
181 pdc->dclevel.pSurface = NULL;
182
183 // FIXME: HACK, because our code expects a surface
184 pdc->dclevel.pSurface = SURFACE_ShareLockSurface(StockObjects[DEFAULT_BITMAP]);
185
186 pdc->erclBounds.left = 0;
187 pdc->erclBounds.top = 0;
188 pdc->erclBounds.right = 0;
189 pdc->erclBounds.bottom = 0;
190 pdc->erclBoundsApp = pdc->erclBounds;
191 pdc->erclClip = pdc->erclWindow;
192 // pdc->co = NULL
193 }
194
195 // pdc->dcattr.VisRectRegion:
196
197 /* Setup coordinate transformation data */
198 pdc->dclevel.mxWorldToDevice = gmxWorldToDeviceDefault;
199 pdc->dclevel.mxDeviceToWorld = gmxDeviceToWorldDefault;
200 pdc->dclevel.mxWorldToPage = gmxWorldToPageDefault;
201 pdc->dclevel.efM11PtoD = gef16;
202 pdc->dclevel.efM22PtoD = gef16;
203 pdc->dclevel.efDxPtoD = gef0;
204 pdc->dclevel.efDyPtoD = gef0;
205 pdc->dclevel.efM11_TWIPS = gef0;
206 pdc->dclevel.efM22_TWIPS = gef0;
207 pdc->dclevel.efPr11 = gef0;
208 pdc->dclevel.efPr22 = gef0;
209 pdc->dcattr.mxWorldToDevice = pdc->dclevel.mxWorldToDevice;
210 pdc->dcattr.mxDeviceToWorld = pdc->dclevel.mxDeviceToWorld;
211 pdc->dcattr.mxWorldToPage = pdc->dclevel.mxWorldToPage;
212 pdc->dcattr.efM11PtoD = pdc->dclevel.efM11PtoD;
213 pdc->dcattr.efM22PtoD = pdc->dclevel.efM22PtoD;
214 pdc->dcattr.efDxPtoD = pdc->dclevel.efDxPtoD;
215 pdc->dcattr.efDyPtoD = pdc->dclevel.efDyPtoD;
216 pdc->dcattr.iMapMode = MM_TEXT;
217 pdc->dcattr.dwLayout = 0;
218 pdc->dcattr.flXform = PAGE_TO_DEVICE_SCALE_IDENTITY |
219 PAGE_TO_DEVICE_IDENTITY |
220 WORLD_TO_PAGE_IDENTITY;
221
222 /* Setup more coordinates */
223 pdc->ptlDCOrig.x = 0;
224 pdc->ptlDCOrig.y = 0;
225 pdc->dcattr.lWindowOrgx = 0;
226 pdc->dcattr.ptlWindowOrg.x = 0;
227 pdc->dcattr.ptlWindowOrg.y = 0;
228 pdc->dcattr.szlWindowExt.cx = 1;
229 pdc->dcattr.szlWindowExt.cy = 1;
230 pdc->dcattr.ptlViewportOrg.x = 0;
231 pdc->dcattr.ptlViewportOrg.y = 0;
232 pdc->dcattr.szlViewportExt.cx = 1;
233 pdc->dcattr.szlViewportExt.cy = 1;
234 pdc->dcattr.szlVirtualDevicePixel.cx = 0;
235 pdc->dcattr.szlVirtualDevicePixel.cy = 0;
236 pdc->dcattr.szlVirtualDeviceMm.cx = 0;
237 pdc->dcattr.szlVirtualDeviceMm.cy = 0;
238 pdc->dcattr.szlVirtualDeviceSize.cx = 0;
239 pdc->dcattr.szlVirtualDeviceSize.cy = 0;
240
241 /* Setup regions */
242 pdc->prgnAPI = NULL;
243 pdc->prgnVis = NULL; // FIXME
244 pdc->prgnRao = NULL;
245
246 /* Setup palette */
247 pdc->dclevel.hpal = StockObjects[DEFAULT_PALETTE];
248 pdc->dclevel.ppal = PALETTE_ShareLockPalette(pdc->dclevel.hpal);
249
250 /* Setup path */
251 pdc->dclevel.hPath = NULL;
252 pdc->dclevel.flPath = 0;
253 // pdc->dclevel.lapath:
254
255 /* Setup colors */
256 pdc->dcattr.crBackgroundClr = RGB(0xff, 0xff, 0xff);
257 pdc->dcattr.ulBackgroundClr = RGB(0xff, 0xff, 0xff);
258 pdc->dcattr.crForegroundClr = RGB(0, 0, 0);
259 pdc->dcattr.ulForegroundClr = RGB(0, 0, 0);
260 pdc->dcattr.crBrushClr = RGB(0xff, 0xff, 0xff);
261 pdc->dcattr.ulBrushClr = RGB(0xff, 0xff, 0xff);
262 pdc->dcattr.crPenClr = RGB(0, 0, 0);
263 pdc->dcattr.ulPenClr = RGB(0, 0, 0);
264
265 /* Select the default fill and line brush */
266 pdc->dcattr.hbrush = StockObjects[WHITE_BRUSH];
267 pdc->dcattr.hpen = StockObjects[BLACK_PEN];
268 pdc->dclevel.pbrFill = BRUSH_ShareLockBrush(pdc->pdcattr->hbrush);
269 pdc->dclevel.pbrLine = PEN_ShareLockPen(pdc->pdcattr->hpen);
270 pdc->dclevel.ptlBrushOrigin.x = 0;
271 pdc->dclevel.ptlBrushOrigin.y = 0;
272 pdc->dcattr.ptlBrushOrigin = pdc->dclevel.ptlBrushOrigin;
273
274 /* Initialize EBRUSHOBJs */
275 EBRUSHOBJ_vInit(&pdc->eboFill, pdc->dclevel.pbrFill, pdc);
276 EBRUSHOBJ_vInit(&pdc->eboLine, pdc->dclevel.pbrLine, pdc);
277 EBRUSHOBJ_vInit(&pdc->eboText, pbrDefaultBrush, pdc);
278 EBRUSHOBJ_vInit(&pdc->eboBackground, pbrDefaultBrush, pdc);
279
280 /* Setup fill data */
281 pdc->dcattr.jROP2 = R2_COPYPEN;
282 pdc->dcattr.jBkMode = 2;
283 pdc->dcattr.lBkMode = 2;
284 pdc->dcattr.jFillMode = ALTERNATE;
285 pdc->dcattr.lFillMode = 1;
286 pdc->dcattr.jStretchBltMode = 1;
287 pdc->dcattr.lStretchBltMode = 1;
288 pdc->ptlFillOrigin.x = 0;
289 pdc->ptlFillOrigin.y = 0;
290
291 /* Setup drawing position */
292 pdc->dcattr.ptlCurrent.x = 0;
293 pdc->dcattr.ptlCurrent.y = 0;
294 pdc->dcattr.ptfxCurrent.x = 0;
295 pdc->dcattr.ptfxCurrent.y = 0;
296
297 /* Setup ICM data */
298 pdc->dclevel.lIcmMode = 0;
299 pdc->dcattr.lIcmMode = 0;
300 pdc->dcattr.hcmXform = NULL;
301 pdc->dcattr.flIcmFlags = 0;
302 pdc->dcattr.IcmBrushColor = CLR_INVALID;
303 pdc->dcattr.IcmPenColor = CLR_INVALID;
304 pdc->dcattr.pvLIcm = NULL;
305 pdc->dcattr.hColorSpace = NULL; // FIXME: 0189001f
306 pdc->dclevel.pColorSpace = NULL; // FIXME
307 pdc->pClrxFormLnk = NULL;
308 // pdc->dclevel.ca =
309
310 /* Setup font data */
311 pdc->hlfntCur = NULL; // FIXME: 2f0a0cf8
312 pdc->pPFFList = NULL;
313 pdc->flSimulationFlags = 0;
314 pdc->lEscapement = 0;
315 pdc->prfnt = NULL;
316 pdc->dcattr.flFontMapper = 0;
317 pdc->dcattr.flTextAlign = 0;
318 pdc->dcattr.lTextAlign = 0;
319 pdc->dcattr.lTextExtra = 0;
320 pdc->dcattr.lRelAbs = 1;
321 pdc->dcattr.lBreakExtra = 0;
322 pdc->dcattr.cBreak = 0;
323 pdc->dcattr.hlfntNew = StockObjects[SYSTEM_FONT];
324 // pdc->dclevel.pFont = LFONT_ShareLockFont(pdc->dcattr.hlfntNew);
325
326 /* Other stuff */
327 pdc->hdcNext = NULL;
328 pdc->hdcPrev = NULL;
329 pdc->ipfdDevMax = 0x0000ffff;
330 pdc->ulCopyCount = -1;
331 pdc->ptlDoBanding.x = 0;
332 pdc->ptlDoBanding.y = 0;
333 pdc->dclevel.lSaveDepth = 1;
334 pdc->dclevel.hdcSave = NULL;
335 pdc->dcattr.iGraphicsMode = GM_COMPATIBLE;
336 pdc->dcattr.iCS_CP = 0;
337 pdc->pSurfInfo = NULL;
338
339 }
340
341 BOOL
342 INTERNAL_CALL
343 DC_Cleanup(PVOID ObjectBody)
344 {
345 PDC pdc = (PDC)ObjectBody;
346
347 /* Free DC_ATTR */
348 DC_vFreeDcAttr(pdc);
349
350 /* Delete saved DCs */
351 DC_vRestoreDC(pdc, 1);
352
353 /* Deselect dc objects */
354 DC_vSelectSurface(pdc, NULL);
355 DC_vSelectFillBrush(pdc, NULL);
356 DC_vSelectLineBrush(pdc, NULL);
357 DC_vSelectPalette(pdc, NULL);
358
359 /* Cleanup the dc brushes */
360 EBRUSHOBJ_vCleanup(&pdc->eboFill);
361 EBRUSHOBJ_vCleanup(&pdc->eboLine);
362 EBRUSHOBJ_vCleanup(&pdc->eboText);
363 EBRUSHOBJ_vCleanup(&pdc->eboBackground);
364
365 /* Free regions */
366 if (pdc->rosdc.hClipRgn)
367 GreDeleteObject(pdc->rosdc.hClipRgn);
368 if (pdc->prgnVis)
369 REGION_FreeRgnByHandle(pdc->prgnVis->BaseObject.hHmgr);
370 ASSERT(pdc->rosdc.hGCClipRgn);
371 if (pdc->rosdc.hGCClipRgn)
372 GreDeleteObject(pdc->rosdc.hGCClipRgn);
373 if (NULL != pdc->rosdc.CombinedClip)
374 IntEngDeleteClipRegion(pdc->rosdc.CombinedClip);
375
376 PATH_Delete(pdc->dclevel.hPath);
377
378 if(pdc->dclevel.pSurface)
379 SURFACE_ShareUnlockSurface(pdc->dclevel.pSurface);
380
381 PDEVOBJ_vRelease(pdc->ppdev) ;
382
383 return TRUE;
384 }
385
386 BOOL
387 FASTCALL
388 DC_SetOwnership(HDC hDC, PEPROCESS Owner)
389 {
390 INT Index;
391 PGDI_TABLE_ENTRY Entry;
392 PDC pDC;
393 BOOL ret = FALSE;
394
395 /* FIXME: This function has broken error handling */
396
397 if (!GDIOBJ_SetOwnership(hDC, Owner))
398 {
399 DPRINT1("GDIOBJ_SetOwnership failed\n");
400 return FALSE;
401 }
402
403 pDC = DC_LockDc(hDC);
404 if (!pDC)
405 {
406 DPRINT1("Could not lock DC\n");
407 return FALSE;
408 }
409
410 /*
411 System Regions:
412 These regions do not use attribute sections and when allocated, use
413 gdiobj level functions.
414 */
415 if (pDC->rosdc.hClipRgn)
416 { // FIXME! HAX!!!
417 Index = GDI_HANDLE_GET_INDEX(pDC->rosdc.hClipRgn);
418 Entry = &GdiHandleTable->Entries[Index];
419 if (Entry->UserData) FreeObjectAttr(Entry->UserData);
420 Entry->UserData = NULL;
421 //
422 if (!GDIOBJ_SetOwnership(pDC->rosdc.hClipRgn, Owner)) goto leave;
423 }
424 if (pDC->prgnVis)
425 { // FIXME! HAX!!!
426 Index = GDI_HANDLE_GET_INDEX(pDC->prgnVis->BaseObject.hHmgr);
427 Entry = &GdiHandleTable->Entries[Index];
428 if (Entry->UserData) FreeObjectAttr(Entry->UserData);
429 Entry->UserData = NULL;
430 //
431 if (!GDIOBJ_SetOwnership(pDC->prgnVis->BaseObject.hHmgr, Owner)) goto leave;
432 }
433 if (pDC->rosdc.hGCClipRgn)
434 { // FIXME! HAX!!!
435 Index = GDI_HANDLE_GET_INDEX(pDC->rosdc.hGCClipRgn);
436 Entry = &GdiHandleTable->Entries[Index];
437 if (Entry->UserData) FreeObjectAttr(Entry->UserData);
438 Entry->UserData = NULL;
439 //
440 if (!GDIOBJ_SetOwnership(pDC->rosdc.hGCClipRgn, Owner)) goto leave;
441 }
442 if (pDC->dclevel.hPath)
443 {
444 if (!GDIOBJ_SetOwnership(pDC->dclevel.hPath, Owner)) goto leave;
445 }
446 ret = TRUE;
447
448 leave:
449 DC_UnlockDc(pDC);
450
451 return ret;
452 }
453
454 HDC
455 NTAPI
456 GreOpenDCW(
457 PUNICODE_STRING pustrDevice,
458 DEVMODEW *pdmInit,
459 PUNICODE_STRING pustrLogAddr,
460 ULONG iType,
461 BOOL bDisplay,
462 HANDLE hspool,
463 VOID *pDriverInfo2,
464 VOID *pUMdhpdev)
465 {
466 PPDEVOBJ ppdev;
467 PDC pdc;
468 HDC hdc;
469
470 DPRINT("GreOpenDCW(%S, iType=%ld)\n",
471 pustrDevice ? pustrDevice->Buffer : NULL, iType);
472
473 /* Get a PDEVOBJ for the device */
474 ppdev = EngpGetPDEV(pustrDevice);
475 if (!ppdev)
476 {
477 DPRINT1("Didn't find a suitable PDEV\n");
478 return NULL;
479 }
480
481 DPRINT("GreOpenDCW - ppdev = %p\n", ppdev);
482
483 pdc = DC_AllocDcWithHandle();
484 if (!pdc)
485 {
486 DPRINT1("Could not Allocate a DC\n");
487 PDEVOBJ_vRelease(ppdev);
488 return NULL;
489 }
490 hdc = pdc->BaseObject.hHmgr;
491
492 /* Lock ppdev and initialize the new DC */
493 DC_vInitDc(pdc, iType, ppdev);
494 /* FIXME: HACK! */
495 DC_InitHack(pdc);
496
497 DC_AllocDcAttr(pdc);
498
499 DC_UnlockDc(pdc);
500
501 DPRINT("returning hdc = %p\n", hdc);
502
503 return hdc;
504 }
505
506 HDC
507 APIENTRY
508 NtGdiOpenDCW(
509 PUNICODE_STRING pustrDevice,
510 DEVMODEW *pdmInit,
511 PUNICODE_STRING pustrLogAddr,
512 ULONG iType,
513 BOOL bDisplay,
514 HANDLE hspool,
515 VOID *pDriverInfo2,
516 VOID *pUMdhpdev)
517 {
518 UNICODE_STRING ustrDevice;
519 WCHAR awcDevice[CCHDEVICENAME];
520 DEVMODEW dmInit;
521 PVOID dhpdev;
522 HDC hdc;
523
524 /* Only if a devicename is given, we need any data */
525 if (pustrDevice)
526 {
527 /* Initialize destination string */
528 RtlInitEmptyUnicodeString(&ustrDevice, awcDevice, sizeof(awcDevice));
529
530 _SEH2_TRY
531 {
532 /* Probe the UNICODE_STRING and the buffer */
533 ProbeForRead(pustrDevice, sizeof(UNICODE_STRING), 1);
534 ProbeForRead(pustrDevice->Buffer, pustrDevice->Length, 1);
535
536 /* Copy the string */
537 RtlCopyUnicodeString(&ustrDevice, pustrDevice);
538
539 if (pdmInit)
540 {
541 /* FIXME: could be larger */
542 ProbeForRead(pdmInit, sizeof(DEVMODEW), 1);
543 RtlCopyMemory(&dmInit, pdmInit, sizeof(DEVMODEW));
544 }
545
546 if (pUMdhpdev)
547 {
548 ProbeForWrite(pUMdhpdev, sizeof(HANDLE), 1);
549 }
550 }
551 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
552 {
553 SetLastNtError(_SEH2_GetExceptionCode());
554 _SEH2_YIELD(return NULL);
555 }
556 _SEH2_END
557 }
558 else
559 {
560 pdmInit = NULL;
561 pUMdhpdev = NULL;
562 }
563
564 /* FIXME: HACK! */
565 if (pustrDevice)
566 {
567 UNICODE_STRING ustrDISPLAY = RTL_CONSTANT_STRING(L"DISPLAY");
568 if (RtlEqualUnicodeString(&ustrDevice, &ustrDISPLAY, TRUE))
569 {
570 pustrDevice = NULL;
571 }
572 }
573
574 /* Call the internal function */
575 hdc = GreOpenDCW(pustrDevice ? &ustrDevice : NULL,
576 pdmInit ? &dmInit : NULL,
577 NULL, // fixme pwszLogAddress
578 iType,
579 bDisplay,
580 hspool,
581 NULL, //FIXME: pDriverInfo2
582 pUMdhpdev ? &dhpdev : NULL);
583
584 /* If we got a HDC and a UM dhpdev is requested,... */
585 if (hdc && pUMdhpdev)
586 {
587 /* Copy dhpdev to caller (FIXME: use dhpdev?? */
588 _SEH2_TRY
589 {
590 /* Pointer was already probed */
591 *(HANDLE*)pUMdhpdev = dhpdev;
592 }
593 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
594 {
595 /* Ignore error */
596 }
597 _SEH2_END
598 }
599
600 return hdc;
601 }
602
603
604 HDC
605 APIENTRY
606 NtGdiCreateCompatibleDC(HDC hdc)
607 {
608 HDC hdcNew;
609 PPDEVOBJ ppdev;
610 PDC pdc, pdcNew;
611
612 DPRINT("NtGdiCreateCompatibleDC(0x%p)\n", hdc);
613
614 /* Did the caller provide a DC? */
615 if (hdc)
616 {
617 /* Yes, try to lock it */
618 pdc = DC_LockDc(hdc);
619 if (!pdc)
620 {
621 DPRINT1("Could not lock source DC %p\n", hdc);
622 return NULL;
623 }
624
625 /* Get the pdev from the DC */
626 ppdev = pdc->ppdev;
627 InterlockedIncrement(&ppdev->cPdevRefs);
628
629 /* Unlock the source DC */
630 DC_UnlockDc(pdc);
631 }
632 else
633 {
634 /* No DC given, get default device */
635 ppdev = EngpGetPDEV(NULL);
636 }
637
638 if (!ppdev)
639 {
640 DPRINT1("Didn't find a suitable PDEV\n");
641 return NULL;
642 }
643
644 /* Allocate a new DC */
645 pdcNew = DC_AllocDcWithHandle();
646 if (!pdcNew)
647 {
648 DPRINT1("Could not allocate a new DC\n");
649 PDEVOBJ_vRelease(ppdev);
650 return NULL;
651 }
652 hdcNew = pdcNew->BaseObject.hHmgr;
653
654 /* Lock ppdev and initialize the new DC */
655 DC_vInitDc(pdcNew, DCTYPE_MEMORY, ppdev);
656 /* FIXME: HACK! */
657 DC_InitHack(pdcNew);
658
659 /* Allocate a dc attribute */
660 DC_AllocDcAttr(pdcNew);
661
662 // HACK!
663 DC_vSelectSurface(pdcNew, psurfDefaultBitmap);
664
665 DC_UnlockDc(pdcNew);
666
667 DPRINT("Leave NtGdiCreateCompatibleDC hdcNew = %p\n", hdcNew);
668
669 return hdcNew;
670 }
671
672 BOOL
673 FASTCALL
674 IntGdiDeleteDC(HDC hDC, BOOL Force)
675 {
676 PDC DCToDelete = DC_LockDc(hDC);
677
678 if (DCToDelete == NULL)
679 {
680 SetLastWin32Error(ERROR_INVALID_HANDLE);
681 return FALSE;
682 }
683
684 if (!Force)
685 {
686 if (DCToDelete->fs & DC_FLAG_PERMANENT)
687 {
688 DPRINT1("No! You Naughty Application!\n");
689 DC_UnlockDc(DCToDelete);
690 return UserReleaseDC(NULL, hDC, FALSE);
691 }
692 }
693
694 DC_UnlockDc(DCToDelete);
695
696 if (!IsObjectDead(hDC))
697 {
698 if (!GDIOBJ_FreeObjByHandle(hDC, GDI_OBJECT_TYPE_DC))
699 {
700 DPRINT1("DC_FreeDC failed\n");
701 }
702 }
703 else
704 {
705 DPRINT1("Attempted to Delete 0x%x currently being destroyed!!!\n", hDC);
706 }
707
708 return TRUE;
709 }
710
711 BOOL
712 APIENTRY
713 NtGdiDeleteObjectApp(HANDLE DCHandle)
714 {
715 /* Complete all pending operations */
716 NtGdiFlushUserBatch();
717
718 if (GDI_HANDLE_IS_STOCKOBJ(DCHandle)) return TRUE;
719
720 if (GDI_HANDLE_GET_TYPE(DCHandle) != GDI_OBJECT_TYPE_DC)
721 return GreDeleteObject((HGDIOBJ) DCHandle);
722
723 if (IsObjectDead((HGDIOBJ)DCHandle)) return TRUE;
724
725 if (!GDIOBJ_OwnedByCurrentProcess(DCHandle))
726 {
727 SetLastWin32Error(ERROR_INVALID_HANDLE);
728 return FALSE;
729 }
730
731 return IntGdiDeleteDC(DCHandle, FALSE);
732 }
733
734 BOOL
735 FASTCALL
736 MakeInfoDC(PDC pdc, BOOL bSet)
737 {
738 PSURFACE pSurface;
739 SIZEL sizl;
740
741 /* Can not be a display DC. */
742 if (pdc->fs & DC_FLAG_DISPLAY) return FALSE;
743 if (bSet)
744 {
745 if (pdc->fs & DC_FLAG_TEMPINFODC || pdc->dctype == DC_TYPE_DIRECT)
746 return FALSE;
747
748 pSurface = pdc->dclevel.pSurface;
749 pdc->fs |= DC_FLAG_TEMPINFODC;
750 pdc->pSurfInfo = pSurface;
751 pdc->dctype = DC_TYPE_INFO;
752 pdc->dclevel.pSurface = NULL;
753
754 PDEVOBJ_sizl(pdc->ppdev, &sizl);
755
756 if ( sizl.cx == pdc->dclevel.sizl.cx &&
757 sizl.cy == pdc->dclevel.sizl.cy )
758 return TRUE;
759
760 pdc->dclevel.sizl.cx = sizl.cx;
761 pdc->dclevel.sizl.cy = sizl.cy;
762 }
763 else
764 {
765 if (!(pdc->fs & DC_FLAG_TEMPINFODC) || pdc->dctype != DC_TYPE_INFO)
766 return FALSE;
767
768 pSurface = pdc->pSurfInfo;
769 pdc->fs &= ~DC_FLAG_TEMPINFODC;
770 pdc->dclevel.pSurface = pSurface;
771 pdc->dctype = DC_TYPE_DIRECT;
772 pdc->pSurfInfo = NULL;
773
774 if ( !pSurface ||
775 (pSurface->SurfObj.sizlBitmap.cx == pdc->dclevel.sizl.cx &&
776 pSurface->SurfObj.sizlBitmap.cy == pdc->dclevel.sizl.cy) )
777 return TRUE;
778
779 pdc->dclevel.sizl.cx = pSurface->SurfObj.sizlBitmap.cx;
780 pdc->dclevel.sizl.cy = pSurface->SurfObj.sizlBitmap.cy;
781 }
782 return IntSetDefaultRegion(pdc);
783 }
784
785 /*
786 * @implemented
787 */
788 BOOL
789 APIENTRY
790 NtGdiMakeInfoDC(
791 IN HDC hdc,
792 IN BOOL bSet)
793 {
794 BOOL Ret;
795 PDC pdc = DC_LockDc(hdc);
796 if (pdc)
797 {
798 Ret = MakeInfoDC(pdc, bSet);
799 DC_UnlockDc(pdc);
800 return Ret;
801 }
802 return FALSE;
803 }
804
805
806 HDC FASTCALL
807 IntGdiCreateDC(
808 PUNICODE_STRING Driver,
809 PUNICODE_STRING pustrDevice,
810 PVOID pUMdhpdev,
811 CONST PDEVMODEW pdmInit,
812 BOOL CreateAsIC)
813 {
814 HDC hdc;
815
816 hdc = GreOpenDCW(pustrDevice,
817 pdmInit,
818 NULL,
819 CreateAsIC ? DCTYPE_INFO :
820 (Driver ? DC_TYPE_DIRECT : DC_TYPE_DIRECT),
821 TRUE,
822 NULL,
823 NULL,
824 pUMdhpdev);
825
826 return hdc;
827 }
828
829 HDC FASTCALL
830 IntGdiCreateDisplayDC(HDEV hDev, ULONG DcType, BOOL EmptyDC)
831 {
832 HDC hDC;
833 UNIMPLEMENTED;
834 ASSERT(FALSE);
835
836 if (DcType == DC_TYPE_MEMORY)
837 hDC = NtGdiCreateCompatibleDC(NULL); // OH~ Yuck! I think I taste vomit in my mouth!
838 else
839 hDC = IntGdiCreateDC(NULL, NULL, NULL, NULL, (DcType == DC_TYPE_INFO));
840
841 return hDC;
842 }
843