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