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