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
,
107 ASSERT(iSaveLevel
> 0);
108 DPRINT("DC_vRestoreDC(%p, %ld)\n", pdc
->BaseObject
.hHmgr
, iSaveLevel
);
110 /* Loop the save levels */
111 while (pdc
->dclevel
.lSaveDepth
> iSaveLevel
)
113 hdcSave
= pdc
->dclevel
.hdcSave
;
114 DPRINT("RestoreDC = %p\n", hdcSave
);
116 /* Set us as the owner */
117 if (!GreSetObjectOwner(hdcSave
, GDI_OBJ_HMGR_POWNED
))
119 /* Could not get ownership. That's bad! */
120 DPRINT1("Could not get ownership of saved DC (%p) for hdc %p!\n",
121 hdcSave
, pdc
->BaseObject
.hHmgr
);
125 /* Lock the saved dc */
126 pdcSave
= DC_LockDc(hdcSave
);
129 /* WTF? Internal error! */
130 DPRINT1("Could not lock the saved DC (%p) for dc %p!\n",
131 hdcSave
, pdc
->BaseObject
.hHmgr
);
135 /* Remove the saved dc from the queue */
136 pdc
->dclevel
.hdcSave
= pdcSave
->dclevel
.hdcSave
;
138 /* Decrement save level */
139 pdc
->dclevel
.lSaveDepth
--;
141 /* Is this the state we want? */
142 if (pdc
->dclevel
.lSaveDepth
== iSaveLevel
)
144 /* Copy the state back */
145 DC_vCopyState(pdcSave
, pdc
, FALSE
);
147 /* Only memory DC's change their surface */
148 if (pdc
->dctype
== DCTYPE_MEMORY
)
149 DC_vSelectSurface(pdc
, pdcSave
->dclevel
.pSurface
);
151 // Restore Path by removing it, if the Save flag is set.
152 // BeginPath will takecare of the rest.
153 if (pdc
->dclevel
.hPath
&& pdc
->dclevel
.flPath
& DCPATH_SAVE
)
155 PATH_Delete(pdc
->dclevel
.hPath
);
156 pdc
->dclevel
.hPath
= 0;
157 pdc
->dclevel
.flPath
&= ~DCPATH_SAVE
;
161 /* Prevent save dc from being restored */
162 pdcSave
->dclevel
.lSaveDepth
= 1;
165 DC_UnlockDc(pdcSave
);
166 /* Delete the saved dc */
167 GreDeleteObject(hdcSave
);
170 DPRINT("Leave DC_vRestoreDC()\n");
183 DPRINT("NtGdiRestoreDC(%p, %d)\n", hdc
, iSaveLevel
);
185 /* Lock the original DC */
186 pdc
= DC_LockDc(hdc
);
189 EngSetLastError(ERROR_INVALID_HANDLE
);
193 ASSERT(pdc
->dclevel
.lSaveDepth
> 0);
195 /* Negative values are relative to the stack top */
197 iSaveLevel
= pdc
->dclevel
.lSaveDepth
+ iSaveLevel
;
199 /* Check if we have a valid instance */
200 if (iSaveLevel
<= 0 || iSaveLevel
>= pdc
->dclevel
.lSaveDepth
)
202 DPRINT("Illegal save level, requested: %ld, current: %ld\n",
203 iSaveLevel
, pdc
->dclevel
.lSaveDepth
);
205 EngSetLastError(ERROR_INVALID_PARAMETER
);
209 /* Call the internal function */
210 DC_vRestoreDC(pdc
, iSaveLevel
);
214 DPRINT("Leave NtGdiRestoreDC\n");
228 DPRINT("NtGdiSaveDC(%p)\n", hDC
);
230 /* Lock the original dc */
231 pdc
= DC_LockDc(hDC
);
234 DPRINT("Could not lock DC\n");
235 EngSetLastError(ERROR_INVALID_HANDLE
);
239 /* Allocate a new dc */
240 pdcSave
= DC_AllocDcWithHandle();
243 DPRINT("Could not allocate a new DC\n");
247 hdcSave
= pdcSave
->BaseObject
.hHmgr
;
249 InterlockedIncrement(&pdc
->ppdev
->cPdevRefs
);
250 DC_vInitDc(pdcSave
, DCTYPE_MEMORY
, pdc
->ppdev
);
252 /* Handle references here correctly */
253 // pdcSrc->dclevel.pSurface = NULL;
254 // pdcSrc->dclevel.pbrFill = NULL;
255 // pdcSrc->dclevel.pbrLine = NULL;
256 // pdcSrc->dclevel.ppal = NULL;
258 /* Make it a kernel handle
259 (FIXME: windows handles this different, see wiki)*/
260 GreSetObjectOwner(hdcSave
, GDI_OBJ_HMGR_PUBLIC
);
262 /* Copy the current state */
263 DC_vCopyState(pdc
, pdcSave
, TRUE
);
265 /* Only memory DC's change their surface */
266 if (pdc
->dctype
== DCTYPE_MEMORY
)
267 DC_vSelectSurface(pdcSave
, pdc
->dclevel
.pSurface
);
269 /* Copy path. FIXME: why this way? */
270 pdcSave
->dclevel
.hPath
= pdc
->dclevel
.hPath
;
271 pdcSave
->dclevel
.flPath
= pdc
->dclevel
.flPath
| DCPATH_SAVESTATE
;
272 if (pdcSave
->dclevel
.hPath
) pdcSave
->dclevel
.flPath
|= DCPATH_SAVE
;
274 /* Set new dc as save dc */
275 pdc
->dclevel
.hdcSave
= hdcSave
;
277 /* Increase save depth, return old value */
278 lSaveDepth
= pdc
->dclevel
.lSaveDepth
++;
280 /* Cleanup and return */
281 DC_UnlockDc(pdcSave
);
284 DPRINT("Leave NtGdiSaveDC: %ld, hdcSave = %p\n", lSaveDepth
, hdcSave
);