5af205260755a3c4ba0f45d32dd273c03bf1cad1
[reactos.git] / subsystems / win32 / win32k / objects / dcstate.c
1 /*
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)
7 */
8
9 #include <win32k.h>
10
11 #define NDEBUG
12 #include <debug.h>
13
14 VOID
15 FASTCALL
16 DC_vCopyState(PDC pdcSrc, PDC pdcDst, BOOL To)
17 {
18 DPRINT("DC_vCopyState(%p, %p)\n", pdcSrc->BaseObject.hHmgr, pdcDst->BaseObject.hHmgr);
19
20 /* Copy full DC attribute */
21 *pdcDst->pdcattr = *pdcSrc->pdcattr;
22
23 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
24 /* The VisRectRegion field needs to be set to a valid state */
25
26 /* Mark some fields as dirty */
27 pdcDst->pdcattr->ulDirty_ |= 0x0012001f; // Note: Use if, To is FALSE....
28
29 /* Copy DC level */
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;
42
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);
47
48 // FIXME: handle refs
49 pdcDst->dclevel.plfnt = pdcSrc->dclevel.plfnt;
50
51 /* Get/SetDCState() don't change hVisRgn field ("Undoc. Windows" p.559). */
52 if (To) // Copy "To" SaveDC state.
53 {
54 if (pdcSrc->rosdc.hClipRgn)
55 {
56 pdcDst->rosdc.hClipRgn = IntSysCreateRectRgn(0, 0, 0, 0);
57 NtGdiCombineRgn(pdcDst->rosdc.hClipRgn, pdcSrc->rosdc.hClipRgn, 0, RGN_COPY);
58 }
59 // FIXME! Handle prgnMeta!
60 }
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);
64 }
65 }
66
67
68 BOOL FASTCALL
69 IntGdiCleanDC(HDC hDC)
70 {
71 PDC dc;
72 if (!hDC) return FALSE;
73 dc = DC_LockDc(hDC);
74 if (!dc) return FALSE;
75 // Clean the DC
76 if (defaultDCstate) DC_vCopyState(defaultDCstate, dc, FALSE);
77
78 DC_UnlockDc(dc);
79
80 return TRUE;
81 }
82
83
84 BOOL
85 APIENTRY
86 NtGdiResetDC(
87 IN HDC hdc,
88 IN LPDEVMODEW pdm,
89 OUT PBOOL pbBanding,
90 IN OPTIONAL VOID *pDriverInfo2,
91 OUT VOID *ppUMdhpdev)
92 {
93 UNIMPLEMENTED;
94 return 0;
95 }
96
97
98 VOID
99 NTAPI
100 DC_vRestoreDC(
101 IN PDC pdc,
102 INT iSaveLevel)
103 {
104 HDC hdcSave;
105 PDC pdcSave;
106
107 ASSERT(iSaveLevel > 0);
108 DPRINT("DC_vRestoreDC(%p, %ld)\n", pdc->BaseObject.hHmgr, iSaveLevel);
109
110 /* Loop the save levels */
111 while (pdc->dclevel.lSaveDepth > iSaveLevel)
112 {
113 hdcSave = pdc->dclevel.hdcSave;
114 DPRINT("RestoreDC = %p\n", hdcSave);
115
116 /* Set us as the owner */
117 if (!GreSetObjectOwner(hdcSave, GDI_OBJ_HMGR_POWNED))
118 {
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);
122 return;// FALSE;
123 }
124
125 /* Lock the saved dc */
126 pdcSave = DC_LockDc(hdcSave);
127 if (!pdcSave)
128 {
129 /* WTF? Internal error! */
130 DPRINT1("Could not lock the saved DC (%p) for dc %p!\n",
131 hdcSave, pdc->BaseObject.hHmgr);
132 return;// FALSE;
133 }
134
135 /* Remove the saved dc from the queue */
136 pdc->dclevel.hdcSave = pdcSave->dclevel.hdcSave;
137
138 /* Decrement save level */
139 pdc->dclevel.lSaveDepth--;
140
141 /* Is this the state we want? */
142 if (pdc->dclevel.lSaveDepth == iSaveLevel)
143 {
144 /* Copy the state back */
145 DC_vCopyState(pdcSave, pdc, FALSE);
146
147 /* Only memory DC's change their surface */
148 if (pdc->dctype == DCTYPE_MEMORY)
149 DC_vSelectSurface(pdc, pdcSave->dclevel.pSurface);
150
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)
154 {
155 PATH_Delete(pdc->dclevel.hPath);
156 pdc->dclevel.hPath = 0;
157 pdc->dclevel.flPath &= ~DCPATH_SAVE;
158 }
159 }
160
161 /* Prevent save dc from being restored */
162 pdcSave->dclevel.lSaveDepth = 1;
163
164 /* Unlock it */
165 DC_UnlockDc(pdcSave);
166 /* Delete the saved dc */
167 GreDeleteObject(hdcSave);
168 }
169
170 DPRINT("Leave DC_vRestoreDC()\n");
171 }
172
173
174
175 BOOL
176 APIENTRY
177 NtGdiRestoreDC(
178 HDC hdc,
179 INT iSaveLevel)
180 {
181 PDC pdc;
182
183 DPRINT("NtGdiRestoreDC(%p, %d)\n", hdc, iSaveLevel);
184
185 /* Lock the original DC */
186 pdc = DC_LockDc(hdc);
187 if (!pdc)
188 {
189 EngSetLastError(ERROR_INVALID_HANDLE);
190 return FALSE;
191 }
192
193 ASSERT(pdc->dclevel.lSaveDepth > 0);
194
195 /* Negative values are relative to the stack top */
196 if (iSaveLevel < 0)
197 iSaveLevel = pdc->dclevel.lSaveDepth + iSaveLevel;
198
199 /* Check if we have a valid instance */
200 if (iSaveLevel <= 0 || iSaveLevel >= pdc->dclevel.lSaveDepth)
201 {
202 DPRINT("Illegal save level, requested: %ld, current: %ld\n",
203 iSaveLevel, pdc->dclevel.lSaveDepth);
204 DC_UnlockDc(pdc);
205 EngSetLastError(ERROR_INVALID_PARAMETER);
206 return FALSE;
207 }
208
209 /* Call the internal function */
210 DC_vRestoreDC(pdc, iSaveLevel);
211
212 DC_UnlockDc(pdc);
213
214 DPRINT("Leave NtGdiRestoreDC\n");
215 return TRUE;
216 }
217
218
219 INT
220 APIENTRY
221 NtGdiSaveDC(
222 HDC hDC)
223 {
224 HDC hdcSave;
225 PDC pdc, pdcSave;
226 INT lSaveDepth;
227
228 DPRINT("NtGdiSaveDC(%p)\n", hDC);
229
230 /* Lock the original dc */
231 pdc = DC_LockDc(hDC);
232 if (pdc == NULL)
233 {
234 DPRINT("Could not lock DC\n");
235 EngSetLastError(ERROR_INVALID_HANDLE);
236 return 0;
237 }
238
239 /* Allocate a new dc */
240 pdcSave = DC_AllocDcWithHandle();
241 if (pdcSave == NULL)
242 {
243 DPRINT("Could not allocate a new DC\n");
244 DC_UnlockDc(pdc);
245 return 0;
246 }
247 hdcSave = pdcSave->BaseObject.hHmgr;
248
249 InterlockedIncrement(&pdc->ppdev->cPdevRefs);
250 DC_vInitDc(pdcSave, DCTYPE_MEMORY, pdc->ppdev);
251
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;
257
258 /* Make it a kernel handle
259 (FIXME: windows handles this different, see wiki)*/
260 GreSetObjectOwner(hdcSave, GDI_OBJ_HMGR_PUBLIC);
261
262 /* Copy the current state */
263 DC_vCopyState(pdc, pdcSave, TRUE);
264
265 /* Only memory DC's change their surface */
266 if (pdc->dctype == DCTYPE_MEMORY)
267 DC_vSelectSurface(pdcSave, pdc->dclevel.pSurface);
268
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;
273
274 /* Set new dc as save dc */
275 pdc->dclevel.hdcSave = hdcSave;
276
277 /* Increase save depth, return old value */
278 lSaveDepth = pdc->dclevel.lSaveDepth++;
279
280 /* Cleanup and return */
281 DC_UnlockDc(pdcSave);
282 DC_UnlockDc(pdc);
283
284 DPRINT("Leave NtGdiSaveDC: %ld, hdcSave = %p\n", lSaveDepth, hdcSave);
285 return lSaveDepth;
286 }
287