2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: Functions for saving and restoring dc states
5 * FILE: subsystem/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_
|= 0x0012001f; // 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
);
49 pdcDst
->dclevel
.plfnt
= pdcSrc
->dclevel
.plfnt
;
51 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
52 if (To
) // Copy "To" SaveDC state.
54 if (pdcSrc
->rosdc
.hClipRgn
)
56 pdcDst
->rosdc
.hClipRgn
= IntSysCreateRectRgn(0, 0, 0, 0);
57 NtGdiCombineRgn(pdcDst
->rosdc
.hClipRgn
, pdcSrc
->rosdc
.hClipRgn
, 0, RGN_COPY
);
59 // FIXME! Handle prgnMeta!
61 else // Copy "!To" RestoreDC state.
62 { /* The VisRectRegion field needs to be set to a valid state */
63 GdiExtSelectClipRgn(pdcDst
, pdcSrc
->rosdc
.hClipRgn
, RGN_COPY
);
69 IntGdiCleanDC(HDC hDC
)
72 if (!hDC
) return FALSE
;
74 if (!dc
) return FALSE
;
76 if (defaultDCstate
) DC_vCopyState(defaultDCstate
, dc
, FALSE
);
90 IN OPTIONAL VOID
*pDriverInfo2
,
104 PEPROCESS pepCurrentProcess
;
108 ASSERT(iSaveLevel
> 0);
109 DPRINT("DC_vRestoreDC(%p, %ld)\n", pdc
->BaseObject
.hHmgr
, iSaveLevel
);
111 /* Get current process */
112 pepCurrentProcess
= PsGetCurrentProcess();
114 /* Loop the save levels */
115 while (pdc
->dclevel
.lSaveDepth
> iSaveLevel
)
117 hdcSave
= pdc
->dclevel
.hdcSave
;
118 DPRINT("RestoreDC = %p\n", hdcSave
);
120 /* Set us as the owner */
121 if (!GDIOBJ_SetOwnership(hdcSave
, pepCurrentProcess
))
123 /* Could not get ownership. That's bad! */
124 DPRINT1("Could not get ownership of saved DC (%p) for hdc %p!\n",
125 hdcSave
, pdc
->BaseObject
.hHmgr
);
129 /* Lock the saved dc */
130 pdcSave
= DC_LockDc(hdcSave
);
133 /* WTF? Internal error! */
134 DPRINT1("Could not lock the saved DC (%p) for dc %p!\n",
135 hdcSave
, pdc
->BaseObject
.hHmgr
);
139 /* Remove the saved dc from the queue */
140 pdc
->dclevel
.hdcSave
= pdcSave
->dclevel
.hdcSave
;
142 /* Decrement save level */
143 pdc
->dclevel
.lSaveDepth
--;
145 /* Is this the state we want? */
146 if (pdc
->dclevel
.lSaveDepth
== iSaveLevel
)
148 /* Copy the state back */
149 DC_vCopyState(pdcSave
, pdc
, FALSE
);
151 /* Only memory DC's change their surface */
152 if (pdcSave
->dctype
== DCTYPE_MEMORY
)
153 DC_vSelectSurface(pdc
, pdcSave
->dclevel
.pSurface
);
155 // Restore Path by removing it, if the Save flag is set.
156 // BeginPath will takecare of the rest.
157 if (pdc
->dclevel
.hPath
&& pdc
->dclevel
.flPath
& DCPATH_SAVE
)
159 PATH_Delete(pdc
->dclevel
.hPath
);
160 pdc
->dclevel
.hPath
= 0;
161 pdc
->dclevel
.flPath
&= ~DCPATH_SAVE
;
165 /* Prevent save dc from being restored */
166 pdcSave
->dclevel
.lSaveDepth
= 1;
169 DC_UnlockDc(pdcSave
);
170 /* Delete the saved dc */
171 GreDeleteObject(hdcSave
);
174 DPRINT("Leave DC_vRestoreDC()\n");
187 DPRINT("NtGdiRestoreDC(%p, %d)\n", hdc
, iSaveLevel
);
189 /* Lock the original DC */
190 pdc
= DC_LockDc(hdc
);
193 SetLastWin32Error(ERROR_INVALID_HANDLE
);
197 ASSERT(pdc
->dclevel
.lSaveDepth
> 0);
199 /* Negative values are relative to the stack top */
201 iSaveLevel
= pdc
->dclevel
.lSaveDepth
+ iSaveLevel
;
203 /* Check if we have a valid instance */
204 if (iSaveLevel
<= 0 || iSaveLevel
>= pdc
->dclevel
.lSaveDepth
)
206 DPRINT("Illegal save level, requested: %ld, current: %ld\n",
207 iSaveLevel
, pdc
->dclevel
.lSaveDepth
);
209 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
213 /* Call the internal function */
214 DC_vRestoreDC(pdc
, iSaveLevel
);
218 DPRINT("Leave NtGdiRestoreDC\n");
232 DPRINT("NtGdiSaveDC(%p)\n", hDC
);
234 /* Lock the original dc */
235 pdc
= DC_LockDc(hDC
);
238 DPRINT("Could not lock DC\n");
239 SetLastWin32Error(ERROR_INVALID_HANDLE
);
243 /* Allocate a new dc */
244 pdcSave
= DC_AllocDcWithHandle();
247 DPRINT("Could not allocate a new DC\n");
251 hdcSave
= pdcSave
->BaseObject
.hHmgr
;
253 InterlockedIncrement(&pdc
->ppdev
->cPdevRefs
);
254 DC_vInitDc(pdcSave
, DCTYPE_MEMORY
, pdc
->ppdev
);
256 /* Handle references here correctly */
257 // pdcSrc->dclevel.pSurface = NULL;
258 // pdcSrc->dclevel.pbrFill = NULL;
259 // pdcSrc->dclevel.pbrLine = NULL;
260 // pdcSrc->dclevel.ppal = NULL;
262 /* Make it a kernel handle
263 (FIXME: windows handles this different, see wiki)*/
264 GDIOBJ_SetOwnership(hdcSave
, NULL
);
266 /* Copy the current state */
267 DC_vCopyState(pdc
, pdcSave
, TRUE
);
269 /* Only memory DC's change their surface */
270 if (pdc
->dctype
== DCTYPE_MEMORY
)
271 DC_vSelectSurface(pdcSave
, pdc
->dclevel
.pSurface
);
273 /* Copy path. FIXME: why this way? */
274 pdcSave
->dclevel
.hPath
= pdc
->dclevel
.hPath
;
275 pdcSave
->dclevel
.flPath
= pdc
->dclevel
.flPath
| DCPATH_SAVESTATE
;
276 if (pdcSave
->dclevel
.hPath
) pdcSave
->dclevel
.flPath
|= DCPATH_SAVE
;
278 /* Set new dc as save dc */
279 pdc
->dclevel
.hdcSave
= hdcSave
;
281 /* Increase save depth, return old value */
282 lSaveDepth
= pdc
->dclevel
.lSaveDepth
++;
284 /* Cleanup and return */
285 DC_UnlockDc(pdcSave
);
288 DPRINT("Leave NtGdiSaveDC: %ld, hdcSave = %p\n", lSaveDepth
, hdcSave
);