2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Win32k subsystem
4 * PURPOSE: Functions for saving and restoring dc states
5 * FILE: subsystems/win32/win32k/objects/dcstate.c
6 * PROGRAMER: Timo Kreuzer (timo.kreuzer@rectos.org)
16 DC_vCopyState(PDC pdcSrc
, PDC pdcDst
, BOOL To
)
18 DPRINT("DC_vCopyState(%p, %p)\n", pdcSrc
->BaseObject
.hHmgr
, pdcDst
->BaseObject
.hHmgr
);
20 /* Copy full DC attribute */
21 *pdcDst
->pdcattr
= *pdcSrc
->pdcattr
;
23 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
24 /* The VisRectRegion field needs to be set to a valid state */
26 /* Mark some fields as dirty */
27 pdcDst
->pdcattr
->ulDirty_
|= (DIRTY_FILL
|DIRTY_LINE
|DIRTY_TEXT
|DIRTY_BACKGROUND
|DIRTY_CHARSET
|DC_ICM_NOT_CALIBRATED
|DC_ICM_NOT_SET
); // Note: Use if, To is FALSE....
30 pdcDst
->dclevel
.pColorSpace
= pdcSrc
->dclevel
.pColorSpace
;
31 pdcDst
->dclevel
.lSaveDepth
= pdcSrc
->dclevel
.lSaveDepth
;
32 pdcDst
->dclevel
.hdcSave
= pdcSrc
->dclevel
.hdcSave
;
33 pdcDst
->dclevel
.laPath
= pdcSrc
->dclevel
.laPath
;
34 pdcDst
->dclevel
.ca
= pdcSrc
->dclevel
.ca
;
35 pdcDst
->dclevel
.mxWorldToDevice
= pdcSrc
->dclevel
.mxWorldToDevice
;
36 pdcDst
->dclevel
.mxDeviceToWorld
= pdcSrc
->dclevel
.mxDeviceToWorld
;
37 pdcDst
->dclevel
.mxWorldToPage
= pdcSrc
->dclevel
.mxWorldToPage
;
38 pdcDst
->dclevel
.efM11PtoD
= pdcSrc
->dclevel
.efM11PtoD
;
39 pdcDst
->dclevel
.efM22PtoD
= pdcSrc
->dclevel
.efM22PtoD
;
40 pdcDst
->dclevel
.sizl
= pdcSrc
->dclevel
.sizl
;
41 pdcDst
->dclevel
.hpal
= pdcSrc
->dclevel
.hpal
;
43 /* Handle references here correctly */
44 DC_vSelectFillBrush(pdcDst
, pdcSrc
->dclevel
.pbrFill
);
45 DC_vSelectLineBrush(pdcDst
, pdcSrc
->dclevel
.pbrLine
);
46 DC_vSelectPalette(pdcDst
, pdcSrc
->dclevel
.ppal
);
48 /* Dereference the old font, reference the new one */
49 if (pdcDst
->dclevel
.plfnt
) LFONT_ShareUnlockFont(pdcDst
->dclevel
.plfnt
); /// @todo should aways be != NULL
50 GDIOBJ_vReferenceObjectByPointer(&pdcSrc
->dclevel
.plfnt
->BaseObject
);
51 pdcDst
->dclevel
.plfnt
= pdcSrc
->dclevel
.plfnt
;
53 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
56 IntGdiExtSelectClipRgn(pdcDst
, pdcSrc
->dclevel
.prgnClip
, RGN_COPY
);
57 if (pdcDst
->dclevel
.prgnMeta
)
59 REGION_Delete(pdcDst
->dclevel
.prgnMeta
);
60 pdcDst
->dclevel
.prgnMeta
= NULL
;
62 if (pdcSrc
->dclevel
.prgnMeta
)
64 pdcDst
->dclevel
.prgnMeta
= IntSysCreateRectpRgn(0, 0, 0, 0);
65 IntGdiCombineRgn(pdcDst
->dclevel
.prgnMeta
, pdcSrc
->dclevel
.prgnMeta
, NULL
, RGN_COPY
);
67 pdcDst
->fs
|= DC_FLAG_DIRTY_RAO
;
74 IntGdiCleanDC(HDC hDC
)
77 if (!hDC
) return FALSE
;
79 if (!dc
) return FALSE
;
83 DC_vCopyState(defaultDCstate
, dc
, FALSE
);
84 /* Update the brushes now, because they reference some objects (the DC palette)
85 * Which belong to the current process, and this DC might be used for another process
86 * after being cleaned up (for GetDC(0) for instance) */
87 DC_vUpdateFillBrush(dc
);
88 DC_vUpdateBackgroundBrush(dc
);
89 DC_vUpdateLineBrush(dc
);
90 DC_vUpdateTextBrush(dc
);
93 /* DC_vCopyState frees the Clip rgn and the Meta rgn. Take care of the other ones
94 * There is no need to clear prgnVis, as UserGetDC updates it immediately. */
96 REGION_Delete(dc
->prgnRao
);
98 REGION_Delete(dc
->prgnAPI
);
99 dc
->prgnRao
= dc
->prgnAPI
= NULL
;
101 dc
->fs
|= DC_FLAG_DIRTY_RAO
;
115 IN OPTIONAL VOID
*pDriverInfo2
,
116 OUT VOID
*ppUMdhpdev
)
132 ASSERT(iSaveLevel
> 0);
133 DPRINT("DC_vRestoreDC(%p, %ld)\n", pdc
->BaseObject
.hHmgr
, iSaveLevel
);
135 /* Loop the save levels */
136 while (pdc
->dclevel
.lSaveDepth
> iSaveLevel
)
138 hdcSave
= pdc
->dclevel
.hdcSave
;
139 DPRINT("RestoreDC = %p\n", hdcSave
);
141 /* Set us as the owner */
142 if (!GreSetObjectOwner(hdcSave
, GDI_OBJ_HMGR_POWNED
))
144 /* Could not get ownership. That's bad! */
145 DPRINT1("Could not get ownership of saved DC (%p) for hdc %p!\n",
146 hdcSave
, pdc
->BaseObject
.hHmgr
);
150 /* Lock the saved dc */
151 pdcSave
= DC_LockDc(hdcSave
);
154 /* WTF? Internal error! */
155 DPRINT1("Could not lock the saved DC (%p) for dc %p!\n",
156 hdcSave
, pdc
->BaseObject
.hHmgr
);
160 /* Remove the saved dc from the queue */
161 pdc
->dclevel
.hdcSave
= pdcSave
->dclevel
.hdcSave
;
163 /* Decrement save level */
164 pdc
->dclevel
.lSaveDepth
--;
166 /* Is this the state we want? */
167 if (pdc
->dclevel
.lSaveDepth
== iSaveLevel
)
169 /* Copy the state back */
170 DC_vCopyState(pdcSave
, pdc
, FALSE
);
172 /* Only memory DC's change their surface */
173 if (pdc
->dctype
== DCTYPE_MEMORY
)
174 DC_vSelectSurface(pdc
, pdcSave
->dclevel
.pSurface
);
176 // Restore Path by removing it, if the Save flag is set.
177 // BeginPath will takecare of the rest.
178 if (pdc
->dclevel
.hPath
&& pdc
->dclevel
.flPath
& DCPATH_SAVE
)
180 PATH_Delete(pdc
->dclevel
.hPath
);
181 pdc
->dclevel
.hPath
= 0;
182 pdc
->dclevel
.flPath
&= ~DCPATH_SAVE
;
186 /* Prevent save dc from being restored */
187 pdcSave
->dclevel
.lSaveDepth
= 1;
190 DC_UnlockDc(pdcSave
);
191 /* Delete the saved dc */
192 GreDeleteObject(hdcSave
);
195 DPRINT("Leave DC_vRestoreDC()\n");
208 DPRINT("NtGdiRestoreDC(%p, %d)\n", hdc
, iSaveLevel
);
210 /* Lock the original DC */
211 pdc
= DC_LockDc(hdc
);
214 EngSetLastError(ERROR_INVALID_HANDLE
);
218 ASSERT(pdc
->dclevel
.lSaveDepth
> 0);
220 /* Negative values are relative to the stack top */
222 iSaveLevel
= pdc
->dclevel
.lSaveDepth
+ iSaveLevel
;
224 /* Check if we have a valid instance */
225 if (iSaveLevel
<= 0 || iSaveLevel
>= pdc
->dclevel
.lSaveDepth
)
227 DPRINT("Illegal save level, requested: %ld, current: %ld\n",
228 iSaveLevel
, pdc
->dclevel
.lSaveDepth
);
230 EngSetLastError(ERROR_INVALID_PARAMETER
);
234 /* Call the internal function */
235 DC_vRestoreDC(pdc
, iSaveLevel
);
239 DPRINT("Leave NtGdiRestoreDC\n");
253 DPRINT("NtGdiSaveDC(%p)\n", hDC
);
255 /* Lock the original dc */
256 pdc
= DC_LockDc(hDC
);
259 DPRINT("Could not lock DC\n");
260 EngSetLastError(ERROR_INVALID_HANDLE
);
264 /* Allocate a new dc */
265 pdcSave
= DC_AllocDcWithHandle();
268 DPRINT("Could not allocate a new DC\n");
272 hdcSave
= pdcSave
->BaseObject
.hHmgr
;
274 InterlockedIncrement(&pdc
->ppdev
->cPdevRefs
);
275 DC_vInitDc(pdcSave
, DCTYPE_MEMORY
, pdc
->ppdev
);
277 /* Handle references here correctly */
278 //pdcSrc->dclevel.pSurface = NULL;
279 //pdcSrc->dclevel.pbrFill = NULL;
280 //pdcSrc->dclevel.pbrLine = NULL;
281 //pdcSrc->dclevel.ppal = NULL;
283 /* Make it a kernel handle
284 (FIXME: Windows handles this differently, see Wiki) */
285 GDIOBJ_vSetObjectOwner(&pdcSave
->BaseObject
, GDI_OBJ_HMGR_PUBLIC
);
287 /* Copy the current state */
288 DC_vCopyState(pdc
, pdcSave
, FALSE
);
290 /* Only memory DC's change their surface */
291 if (pdc
->dctype
== DCTYPE_MEMORY
)
292 DC_vSelectSurface(pdcSave
, pdc
->dclevel
.pSurface
);
295 /* FIXME: Why this way? */
296 pdcSave
->dclevel
.hPath
= pdc
->dclevel
.hPath
;
297 pdcSave
->dclevel
.flPath
= pdc
->dclevel
.flPath
| DCPATH_SAVESTATE
;
298 if (pdcSave
->dclevel
.hPath
) pdcSave
->dclevel
.flPath
|= DCPATH_SAVE
;
300 /* Set new dc as save dc */
301 pdc
->dclevel
.hdcSave
= hdcSave
;
303 /* Increase save depth, return old value */
304 lSaveDepth
= pdc
->dclevel
.lSaveDepth
++;
306 /* Cleanup and return */
307 DC_UnlockDc(pdcSave
);
310 DPRINT("Leave NtGdiSaveDC: %ld, hdcSave = %p\n", lSaveDepth
, hdcSave
);