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