[WIN32K]
[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
457 int FASTCALL
458 CLIPPING_UpdateGCRegion(DC* Dc);
459
460 static
461 void
462 DC_vUpdateDC(PDC pdc)
463 {
464 HRGN hVisRgn ;
465 PPDEVOBJ ppdev = pdc->ppdev ;
466
467 pdc->dhpdev = ppdev->dhpdev;
468
469 SURFACE_ShareUnlockSurface(pdc->dclevel.pSurface);
470 pdc->dclevel.pSurface = PDEVOBJ_pSurface(ppdev);
471
472 PDEVOBJ_sizl(pdc->ppdev, &pdc->dclevel.sizl);
473 hVisRgn = NtGdiCreateRectRgn(0, 0, pdc->dclevel.sizl.cx, pdc->dclevel.sizl.cy);
474 ASSERT(hVisRgn);
475 GdiSelectVisRgn(pdc->BaseObject.hHmgr, hVisRgn);
476 GreDeleteObject(hVisRgn);
477
478 pdc->flGraphicsCaps = ppdev->devinfo.flGraphicsCaps;
479 pdc->flGraphicsCaps2 = ppdev->devinfo.flGraphicsCaps2;
480
481 /* Mark EBRUSHOBJs as dirty */
482 pdc->pdcattr->ulDirty_ |= DIRTY_DEFAULT ;
483 }
484
485 /* Prepare a blit for up to 2 DCs */
486 /* rc1 and rc2 are the rectangles where we want to draw or
487 * from where we take pixels. */
488 VOID
489 FASTCALL
490 DC_vPrepareDCsForBlit(PDC pdc1,
491 RECT rc1,
492 PDC pdc2,
493 RECT rc2)
494 {
495 PDC pdcFirst, pdcSecond;
496 PRECT prcFirst, prcSecond;
497 /* Lock them in good order */
498 if(pdc2)
499 {
500 if((ULONG_PTR)pdc1->BaseObject.hHmgr >= (ULONG_PTR)pdc2->BaseObject.hHmgr)
501 {
502 pdcFirst = pdc1;
503 prcFirst = &rc1;
504 pdcSecond = pdc2;
505 prcSecond = &rc2;
506 }
507 else
508 {
509 pdcFirst = pdc2;
510 prcFirst = &rc2;
511 pdcSecond = pdc1;
512 prcSecond = &rc1;
513 }
514 }
515 else
516 {
517 pdcFirst = pdc1 ;
518 prcFirst = &rc1;
519 pdcSecond = NULL ;
520 }
521
522 if(pdcFirst && pdcFirst->dctype == DCTYPE_DIRECT)
523 {
524 EngAcquireSemaphore(pdcFirst->ppdev->hsemDevLock);
525 MouseSafetyOnDrawStart(pdcFirst->ppdev,
526 prcFirst->left,
527 prcFirst->top,
528 prcFirst->right,
529 prcFirst->bottom) ;
530 /* Update surface if needed */
531 if(pdcFirst->ppdev->pSurface != pdcFirst->dclevel.pSurface)
532 {
533 DC_vUpdateDC(pdcFirst);
534 }
535 }
536 if(pdcSecond && pdcSecond->dctype == DCTYPE_DIRECT)
537 {
538 EngAcquireSemaphore(pdcSecond->ppdev->hsemDevLock);
539 MouseSafetyOnDrawStart(pdcSecond->ppdev,
540 prcSecond->left,
541 prcSecond->top,
542 prcSecond->right,
543 prcSecond->bottom) ;
544 /* Update surface if needed */
545 if(pdcSecond->ppdev->pSurface != pdcSecond->dclevel.pSurface)
546 {
547 DC_vUpdateDC(pdcSecond);
548 }
549 }
550 }
551
552 /* Finishes a blit for one or two DCs */
553 VOID
554 FASTCALL
555 DC_vFinishBlit(PDC pdc1, PDC pdc2)
556 {
557 if(pdc1->dctype == DCTYPE_DIRECT)
558 {
559 MouseSafetyOnDrawEnd(pdc1->ppdev);
560 EngReleaseSemaphore(pdc1->ppdev->hsemDevLock);
561 }
562
563 if(pdc2)
564 {
565 if(pdc2->dctype == DCTYPE_DIRECT)
566 {
567 MouseSafetyOnDrawEnd(pdc2->ppdev);
568 EngReleaseSemaphore(pdc2->ppdev->hsemDevLock);
569 }
570 }
571 }
572
573 HDC
574 NTAPI
575 GreOpenDCW(
576 PUNICODE_STRING pustrDevice,
577 DEVMODEW *pdmInit,
578 PUNICODE_STRING pustrLogAddr,
579 ULONG iType,
580 BOOL bDisplay,
581 HANDLE hspool,
582 VOID *pDriverInfo2,
583 VOID *pUMdhpdev)
584 {
585 PPDEVOBJ ppdev;
586 PDC pdc;
587 HDC hdc;
588
589 DPRINT("GreOpenDCW(%S, iType=%ld)\n",
590 pustrDevice ? pustrDevice->Buffer : NULL, iType);
591
592 /* Get a PDEVOBJ for the device */
593 ppdev = EngpGetPDEV(pustrDevice);
594 if (!ppdev)
595 {
596 DPRINT1("Didn't find a suitable PDEV\n");
597 return NULL;
598 }
599
600 DPRINT("GreOpenDCW - ppdev = %p\n", ppdev);
601
602 pdc = DC_AllocDcWithHandle();
603 if (!pdc)
604 {
605 DPRINT1("Could not Allocate a DC\n");
606 PDEVOBJ_vRelease(ppdev);
607 return NULL;
608 }
609 hdc = pdc->BaseObject.hHmgr;
610
611 /* Lock ppdev and initialize the new DC */
612 DC_vInitDc(pdc, iType, ppdev);
613 /* FIXME: HACK! */
614 DC_InitHack(pdc);
615
616 DC_AllocDcAttr(pdc);
617
618 DC_UnlockDc(pdc);
619
620 DPRINT("returning hdc = %p\n", hdc);
621
622 return hdc;
623 }
624
625 HDC
626 APIENTRY
627 NtGdiOpenDCW(
628 PUNICODE_STRING pustrDevice,
629 DEVMODEW *pdmInit,
630 PUNICODE_STRING pustrLogAddr,
631 ULONG iType,
632 BOOL bDisplay,
633 HANDLE hspool,
634 VOID *pDriverInfo2,
635 VOID *pUMdhpdev)
636 {
637 UNICODE_STRING ustrDevice;
638 WCHAR awcDevice[CCHDEVICENAME];
639 DEVMODEW dmInit;
640 PVOID dhpdev;
641 HDC hdc;
642
643 /* Only if a devicename is given, we need any data */
644 if (pustrDevice)
645 {
646 /* Initialize destination string */
647 RtlInitEmptyUnicodeString(&ustrDevice, awcDevice, sizeof(awcDevice));
648
649 _SEH2_TRY
650 {
651 /* Probe the UNICODE_STRING and the buffer */
652 ProbeForRead(pustrDevice, sizeof(UNICODE_STRING), 1);
653 ProbeForRead(pustrDevice->Buffer, pustrDevice->Length, 1);
654
655 /* Copy the string */
656 RtlCopyUnicodeString(&ustrDevice, pustrDevice);
657
658 if (pdmInit)
659 {
660 /* FIXME: could be larger */
661 ProbeForRead(pdmInit, sizeof(DEVMODEW), 1);
662 RtlCopyMemory(&dmInit, pdmInit, sizeof(DEVMODEW));
663 }
664
665 if (pUMdhpdev)
666 {
667 ProbeForWrite(pUMdhpdev, sizeof(HANDLE), 1);
668 }
669 }
670 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
671 {
672 SetLastNtError(_SEH2_GetExceptionCode());
673 _SEH2_YIELD(return NULL);
674 }
675 _SEH2_END
676 }
677 else
678 {
679 pdmInit = NULL;
680 pUMdhpdev = NULL;
681 }
682
683 /* FIXME: HACK! */
684 if (pustrDevice)
685 {
686 UNICODE_STRING ustrDISPLAY = RTL_CONSTANT_STRING(L"DISPLAY");
687 if (RtlEqualUnicodeString(&ustrDevice, &ustrDISPLAY, TRUE))
688 {
689 pustrDevice = NULL;
690 }
691 }
692
693 /* Call the internal function */
694 hdc = GreOpenDCW(pustrDevice ? &ustrDevice : NULL,
695 pdmInit ? &dmInit : NULL,
696 NULL, // fixme pwszLogAddress
697 iType,
698 bDisplay,
699 hspool,
700 NULL, //FIXME: pDriverInfo2
701 pUMdhpdev ? &dhpdev : NULL);
702
703 /* If we got a HDC and a UM dhpdev is requested,... */
704 if (hdc && pUMdhpdev)
705 {
706 /* Copy dhpdev to caller (FIXME: use dhpdev?? */
707 _SEH2_TRY
708 {
709 /* Pointer was already probed */
710 *(HANDLE*)pUMdhpdev = dhpdev;
711 }
712 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
713 {
714 /* Ignore error */
715 }
716 _SEH2_END
717 }
718
719 return hdc;
720 }
721
722
723 HDC
724 APIENTRY
725 NtGdiCreateCompatibleDC(HDC hdc)
726 {
727 HDC hdcNew;
728 PPDEVOBJ ppdev;
729 PDC pdc, pdcNew;
730
731 DPRINT("NtGdiCreateCompatibleDC(0x%p)\n", hdc);
732
733 /* Did the caller provide a DC? */
734 if (hdc)
735 {
736 /* Yes, try to lock it */
737 pdc = DC_LockDc(hdc);
738 if (!pdc)
739 {
740 DPRINT1("Could not lock source DC %p\n", hdc);
741 return NULL;
742 }
743
744 /* Get the pdev from the DC */
745 ppdev = pdc->ppdev;
746 InterlockedIncrement(&ppdev->cPdevRefs);
747
748 /* Unlock the source DC */
749 DC_UnlockDc(pdc);
750 }
751 else
752 {
753 /* No DC given, get default device */
754 ppdev = EngpGetPDEV(NULL);
755 }
756
757 if (!ppdev)
758 {
759 DPRINT1("Didn't find a suitable PDEV\n");
760 return NULL;
761 }
762
763 /* Allocate a new DC */
764 pdcNew = DC_AllocDcWithHandle();
765 if (!pdcNew)
766 {
767 DPRINT1("Could not allocate a new DC\n");
768 PDEVOBJ_vRelease(ppdev);
769 return NULL;
770 }
771 hdcNew = pdcNew->BaseObject.hHmgr;
772
773 /* Lock ppdev and initialize the new DC */
774 DC_vInitDc(pdcNew, DCTYPE_MEMORY, ppdev);
775 /* FIXME: HACK! */
776 DC_InitHack(pdcNew);
777
778 /* Allocate a dc attribute */
779 DC_AllocDcAttr(pdcNew);
780
781 // HACK!
782 DC_vSelectSurface(pdcNew, psurfDefaultBitmap);
783
784 DC_UnlockDc(pdcNew);
785
786 DPRINT("Leave NtGdiCreateCompatibleDC hdcNew = %p\n", hdcNew);
787
788 return hdcNew;
789 }
790
791 BOOL
792 FASTCALL
793 IntGdiDeleteDC(HDC hDC, BOOL Force)
794 {
795 PDC DCToDelete = DC_LockDc(hDC);
796
797 if (DCToDelete == NULL)
798 {
799 SetLastWin32Error(ERROR_INVALID_HANDLE);
800 return FALSE;
801 }
802
803 if (!Force)
804 {
805 if (DCToDelete->fs & DC_FLAG_PERMANENT)
806 {
807 DPRINT1("No! You Naughty Application!\n");
808 DC_UnlockDc(DCToDelete);
809 return UserReleaseDC(NULL, hDC, FALSE);
810 }
811 }
812
813 DC_UnlockDc(DCToDelete);
814
815 if (!IsObjectDead(hDC))
816 {
817 if (!GDIOBJ_FreeObjByHandle(hDC, GDI_OBJECT_TYPE_DC))
818 {
819 DPRINT1("DC_FreeDC failed\n");
820 }
821 }
822 else
823 {
824 DPRINT1("Attempted to Delete 0x%x currently being destroyed!!!\n", hDC);
825 }
826
827 return TRUE;
828 }
829
830 BOOL
831 APIENTRY
832 NtGdiDeleteObjectApp(HANDLE DCHandle)
833 {
834 /* Complete all pending operations */
835 NtGdiFlushUserBatch();
836
837 if (GDI_HANDLE_IS_STOCKOBJ(DCHandle)) return TRUE;
838
839 if (GDI_HANDLE_GET_TYPE(DCHandle) != GDI_OBJECT_TYPE_DC)
840 return GreDeleteObject((HGDIOBJ) DCHandle);
841
842 if (IsObjectDead((HGDIOBJ)DCHandle)) return TRUE;
843
844 if (!GDIOBJ_OwnedByCurrentProcess(DCHandle))
845 {
846 SetLastWin32Error(ERROR_INVALID_HANDLE);
847 return FALSE;
848 }
849
850 return IntGdiDeleteDC(DCHandle, FALSE);
851 }
852
853 BOOL
854 FASTCALL
855 MakeInfoDC(PDC pdc, BOOL bSet)
856 {
857 PSURFACE pSurface;
858 SIZEL sizl;
859
860 /* Can not be a display DC. */
861 if (pdc->fs & DC_FLAG_DISPLAY) return FALSE;
862 if (bSet)
863 {
864 if (pdc->fs & DC_FLAG_TEMPINFODC || pdc->dctype == DC_TYPE_DIRECT)
865 return FALSE;
866
867 pSurface = pdc->dclevel.pSurface;
868 pdc->fs |= DC_FLAG_TEMPINFODC;
869 pdc->pSurfInfo = pSurface;
870 pdc->dctype = DC_TYPE_INFO;
871 pdc->dclevel.pSurface = NULL;
872
873 PDEVOBJ_sizl(pdc->ppdev, &sizl);
874
875 if ( sizl.cx == pdc->dclevel.sizl.cx &&
876 sizl.cy == pdc->dclevel.sizl.cy )
877 return TRUE;
878
879 pdc->dclevel.sizl.cx = sizl.cx;
880 pdc->dclevel.sizl.cy = sizl.cy;
881 }
882 else
883 {
884 if (!(pdc->fs & DC_FLAG_TEMPINFODC) || pdc->dctype != DC_TYPE_INFO)
885 return FALSE;
886
887 pSurface = pdc->pSurfInfo;
888 pdc->fs &= ~DC_FLAG_TEMPINFODC;
889 pdc->dclevel.pSurface = pSurface;
890 pdc->dctype = DC_TYPE_DIRECT;
891 pdc->pSurfInfo = NULL;
892
893 if ( !pSurface ||
894 (pSurface->SurfObj.sizlBitmap.cx == pdc->dclevel.sizl.cx &&
895 pSurface->SurfObj.sizlBitmap.cy == pdc->dclevel.sizl.cy) )
896 return TRUE;
897
898 pdc->dclevel.sizl.cx = pSurface->SurfObj.sizlBitmap.cx;
899 pdc->dclevel.sizl.cy = pSurface->SurfObj.sizlBitmap.cy;
900 }
901 return IntSetDefaultRegion(pdc);
902 }
903
904 /*
905 * @implemented
906 */
907 BOOL
908 APIENTRY
909 NtGdiMakeInfoDC(
910 IN HDC hdc,
911 IN BOOL bSet)
912 {
913 BOOL Ret;
914 PDC pdc = DC_LockDc(hdc);
915 if (pdc)
916 {
917 Ret = MakeInfoDC(pdc, bSet);
918 DC_UnlockDc(pdc);
919 return Ret;
920 }
921 return FALSE;
922 }
923
924
925 HDC FASTCALL
926 IntGdiCreateDC(
927 PUNICODE_STRING Driver,
928 PUNICODE_STRING pustrDevice,
929 PVOID pUMdhpdev,
930 CONST PDEVMODEW pdmInit,
931 BOOL CreateAsIC)
932 {
933 HDC hdc;
934
935 hdc = GreOpenDCW(pustrDevice,
936 pdmInit,
937 NULL,
938 CreateAsIC ? DCTYPE_INFO :
939 (Driver ? DC_TYPE_DIRECT : DC_TYPE_DIRECT),
940 TRUE,
941 NULL,
942 NULL,
943 pUMdhpdev);
944
945 return hdc;
946 }
947
948 HDC FASTCALL
949 IntGdiCreateDisplayDC(HDEV hDev, ULONG DcType, BOOL EmptyDC)
950 {
951 HDC hDC;
952 UNIMPLEMENTED;
953 ASSERT(FALSE);
954
955 if (DcType == DC_TYPE_MEMORY)
956 hDC = NtGdiCreateCompatibleDC(NULL); // OH~ Yuck! I think I taste vomit in my mouth!
957 else
958 hDC = IntGdiCreateDC(NULL, NULL, NULL, NULL, (DcType == DC_TYPE_INFO));
959
960 return hDC;
961 }
962