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 /* Copy full DC attribute */
19 *pdcDst
->pdcattr
= *pdcSrc
->pdcattr
;
21 /* Mark some fields as dirty */
22 pdcDst
->pdcattr
->ulDirty_
|= 0x0012001f; // Note: Use if, To is FALSE....
25 pdcDst
->dclevel
.pColorSpace
= pdcSrc
->dclevel
.pColorSpace
;
26 pdcDst
->dclevel
.lSaveDepth
= pdcSrc
->dclevel
.lSaveDepth
;
27 pdcDst
->dclevel
.hdcSave
= pdcSrc
->dclevel
.hdcSave
;
28 pdcDst
->dclevel
.laPath
= pdcSrc
->dclevel
.laPath
;
29 pdcDst
->dclevel
.ca
= pdcSrc
->dclevel
.ca
;
30 pdcDst
->dclevel
.mxWorldToDevice
= pdcSrc
->dclevel
.mxWorldToDevice
;
31 pdcDst
->dclevel
.mxDeviceToWorld
= pdcSrc
->dclevel
.mxDeviceToWorld
;
32 pdcDst
->dclevel
.mxWorldToPage
= pdcSrc
->dclevel
.mxWorldToPage
;
33 pdcDst
->dclevel
.efM11PtoD
= pdcSrc
->dclevel
.efM11PtoD
;
34 pdcDst
->dclevel
.efM22PtoD
= pdcSrc
->dclevel
.efM22PtoD
;
35 pdcDst
->dclevel
.sizl
= pdcSrc
->dclevel
.sizl
;
36 pdcDst
->dclevel
.hpal
= pdcSrc
->dclevel
.hpal
;
38 /* Handle references here correctly */
39 DC_vSelectSurface(pdcDst
, pdcSrc
->dclevel
.pSurface
);
40 DC_vSelectFillBrush(pdcDst
, pdcSrc
->dclevel
.pbrFill
);
41 DC_vSelectLineBrush(pdcDst
, pdcSrc
->dclevel
.pbrLine
);
42 DC_vSelectPalette(pdcDst
, pdcSrc
->dclevel
.ppal
);
45 pdcDst
->dclevel
.plfnt
= pdcSrc
->dclevel
.plfnt
;
48 if (pdcDst
->dctype
!= DC_TYPE_MEMORY
)
50 pdcDst
->rosdc
.bitsPerPixel
= pdcSrc
->rosdc
.bitsPerPixel
;
53 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
54 if (To
) // Copy "To" SaveDC state.
56 if (pdcSrc
->rosdc
.hClipRgn
)
58 pdcDst
->rosdc
.hClipRgn
= IntSysCreateRectRgn(0, 0, 0, 0);
59 NtGdiCombineRgn(pdcDst
->rosdc
.hClipRgn
, pdcSrc
->rosdc
.hClipRgn
, 0, RGN_COPY
);
61 // FIXME! Handle prgnMeta!
63 else // Copy "!To" RestoreDC state.
64 { /* The VisRectRegion field needs to be set to a valid state */
65 GdiExtSelectClipRgn(pdcDst
, pdcSrc
->rosdc
.hClipRgn
, RGN_COPY
);
71 IntGdiCleanDC(HDC hDC
)
74 if (!hDC
) return FALSE
;
76 if (!dc
) return FALSE
;
78 if (defaultDCstate
) DC_vCopyState(defaultDCstate
, dc
, FALSE
);
80 if (dc
->dctype
!= DC_TYPE_MEMORY
)
82 dc
->rosdc
.bitsPerPixel
= defaultDCstate
->rosdc
.bitsPerPixel
;
97 IN OPTIONAL VOID
*pDriverInfo2
,
114 DPRINT("NtGdiRestoreDC(%lx, %d)\n", hdc
, iSaveLevel
);
116 /* Lock the original DC */
117 pdc
= DC_LockDc(hdc
);
120 SetLastWin32Error(ERROR_INVALID_HANDLE
);
124 ASSERT(pdc
->dclevel
.lSaveDepth
> 0);
126 /* Negative values are relative to the stack top */
128 iSaveLevel
= pdc
->dclevel
.lSaveDepth
+ iSaveLevel
;
130 /* Check if we have a valid instance */
131 if (iSaveLevel
<= 0 || iSaveLevel
>= pdc
->dclevel
.lSaveDepth
)
133 DPRINT("Illegal save level, requested: %ld, current: %ld\n",
134 iSaveLevel
, pdc
->dclevel
.lSaveDepth
);
136 SetLastWin32Error(ERROR_INVALID_PARAMETER
);
140 /* Loop the save levels */
141 while (pdc
->dclevel
.lSaveDepth
> iSaveLevel
)
143 hdcSave
= pdc
->dclevel
.hdcSave
;
145 /* Set us as the owner */
146 if (!IntGdiSetDCOwnerEx(hdcSave
, GDI_OBJ_HMGR_POWNED
, FALSE
))
148 /* Could not get ownership. That's bad! */
149 DPRINT1("Could not get ownership of saved DC (%p) for dc %p!\n",
154 /* Lock the saved dc */
155 pdcSave
= DC_LockDc(hdcSave
);
158 /* WTF? Internal error! */
159 DPRINT1("Could not lock the saved DC (%p) for dc %p!\n",
165 /* Remove the saved dc from the queue */
166 pdc
->dclevel
.hdcSave
= pdcSave
->dclevel
.hdcSave
;
168 /* Decrement save level */
169 pdc
->dclevel
.lSaveDepth
--;
171 /* Is this the state we want? */
172 if (pdc
->dclevel
.lSaveDepth
== iSaveLevel
)
174 /* Copy the state back */
175 DC_vCopyState(pdcSave
, pdc
, FALSE
);
177 // Restore Path by removing it, if the Save flag is set.
178 // BeginPath will takecare of the rest.
179 if (pdc
->dclevel
.hPath
&& pdc
->dclevel
.flPath
& DCPATH_SAVE
)
181 PATH_Delete(pdc
->dclevel
.hPath
);
182 pdc
->dclevel
.hPath
= 0;
183 pdc
->dclevel
.flPath
&= ~DCPATH_SAVE
;
185 // Attempt to plug the leak!
186 if (pdcSave
->rosdc
.hClipRgn
)
188 DPRINT("Have hClipRgn!\n");
189 REGION_FreeRgnByHandle(pdcSave
->rosdc
.hClipRgn
);
191 // FIXME! Handle prgnMeta!
194 /* Delete the saved dc */
195 GreDeleteObject(hdcSave
);
200 DPRINT("Leaving NtGdiRestoreDC\n");
214 DPRINT("NtGdiSaveDC(%lx)\n", hDC
);
216 /* Lock the original dc */
217 pdc
= DC_LockDc(hDC
);
220 DPRINT("Could not lock DC\n");
221 SetLastWin32Error(ERROR_INVALID_HANDLE
);
225 /* Allocate a new dc */
226 pdcSave
= DC_AllocDC(NULL
);
229 DPRINT("Could not allocate a new DC\n");
233 hdcSave
= pdcSave
->BaseObject
.hHmgr
;
235 /* Copy the current state */
236 DC_vCopyState(pdc
, pdcSave
, TRUE
);
238 /* Make it a kernel handle
239 (FIXME: windows handles this different, see wiki)*/
240 IntGdiSetDCOwnerEx(hdcSave
, GDI_OBJ_HMGR_NONE
, FALSE
);
242 /* Copy path. FIXME: why this way? */
243 pdcSave
->dclevel
.hPath
= pdc
->dclevel
.hPath
;
244 pdcSave
->dclevel
.flPath
= pdc
->dclevel
.flPath
| DCPATH_SAVESTATE
;
245 if (pdcSave
->dclevel
.hPath
) pdcSave
->dclevel
.flPath
|= DCPATH_SAVE
;
247 /* Set new dc as save dc */
248 pdc
->dclevel
.hdcSave
= hdcSave
;
250 /* Increase save depth, return old value */
251 lSaveDepth
= pdc
->dclevel
.lSaveDepth
++;
253 /* Cleanup and return */
254 DC_UnlockDc(pdcSave
);
257 DPRINT("Leave NtGdiSaveDC: %ld\n", lSaveDepth
);