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