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