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