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