[YAROTOWS] Reintegrate the branch. For a brighter future.
[reactos.git] / reactos / 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 PEPROCESS pepCurrentProcess;
105 HDC hdcSave;
106 PDC pdcSave;
107
108 ASSERT(iSaveLevel > 0);
109 DPRINT("DC_vRestoreDC(%p, %ld)\n", pdc->BaseObject.hHmgr, iSaveLevel);
110
111 /* Get current process */
112 pepCurrentProcess = PsGetCurrentProcess();
113
114 /* Loop the save levels */
115 while (pdc->dclevel.lSaveDepth > iSaveLevel)
116 {
117 hdcSave = pdc->dclevel.hdcSave;
118 DPRINT("RestoreDC = %p\n", hdcSave);
119
120 /* Set us as the owner */
121 if (!GDIOBJ_SetOwnership(hdcSave, pepCurrentProcess))
122 {
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);
126 return;// FALSE;
127 }
128
129 /* Lock the saved dc */
130 pdcSave = DC_LockDc(hdcSave);
131 if (!pdcSave)
132 {
133 /* WTF? Internal error! */
134 DPRINT1("Could not lock the saved DC (%p) for dc %p!\n",
135 hdcSave, pdc->BaseObject.hHmgr);
136 return;// FALSE;
137 }
138
139 /* Remove the saved dc from the queue */
140 pdc->dclevel.hdcSave = pdcSave->dclevel.hdcSave;
141
142 /* Decrement save level */
143 pdc->dclevel.lSaveDepth--;
144
145 /* Is this the state we want? */
146 if (pdc->dclevel.lSaveDepth == iSaveLevel)
147 {
148 /* Copy the state back */
149 DC_vCopyState(pdcSave, pdc, FALSE);
150
151 /* Only memory DC's change their surface */
152 if (pdc->dctype == DCTYPE_MEMORY)
153 DC_vSelectSurface(pdc, pdcSave->dclevel.pSurface);
154
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)
158 {
159 PATH_Delete(pdc->dclevel.hPath);
160 pdc->dclevel.hPath = 0;
161 pdc->dclevel.flPath &= ~DCPATH_SAVE;
162 }
163 }
164
165 /* Prevent save dc from being restored */
166 pdcSave->dclevel.lSaveDepth = 1;
167
168 /* Unlock it */
169 DC_UnlockDc(pdcSave);
170 /* Delete the saved dc */
171 GreDeleteObject(hdcSave);
172 }
173
174 DPRINT("Leave DC_vRestoreDC()\n");
175 }
176
177
178
179 BOOL
180 APIENTRY
181 NtGdiRestoreDC(
182 HDC hdc,
183 INT iSaveLevel)
184 {
185 PDC pdc;
186
187 DPRINT("NtGdiRestoreDC(%p, %d)\n", hdc, iSaveLevel);
188
189 /* Lock the original DC */
190 pdc = DC_LockDc(hdc);
191 if (!pdc)
192 {
193 SetLastWin32Error(ERROR_INVALID_HANDLE);
194 return FALSE;
195 }
196
197 ASSERT(pdc->dclevel.lSaveDepth > 0);
198
199 /* Negative values are relative to the stack top */
200 if (iSaveLevel < 0)
201 iSaveLevel = pdc->dclevel.lSaveDepth + iSaveLevel;
202
203 /* Check if we have a valid instance */
204 if (iSaveLevel <= 0 || iSaveLevel >= pdc->dclevel.lSaveDepth)
205 {
206 DPRINT("Illegal save level, requested: %ld, current: %ld\n",
207 iSaveLevel, pdc->dclevel.lSaveDepth);
208 DC_UnlockDc(pdc);
209 SetLastWin32Error(ERROR_INVALID_PARAMETER);
210 return FALSE;
211 }
212
213 /* Call the internal function */
214 DC_vRestoreDC(pdc, iSaveLevel);
215
216 DC_UnlockDc(pdc);
217
218 DPRINT("Leave NtGdiRestoreDC\n");
219 return TRUE;
220 }
221
222
223 INT
224 APIENTRY
225 NtGdiSaveDC(
226 HDC hDC)
227 {
228 HDC hdcSave;
229 PDC pdc, pdcSave;
230 INT lSaveDepth;
231
232 DPRINT("NtGdiSaveDC(%p)\n", hDC);
233
234 /* Lock the original dc */
235 pdc = DC_LockDc(hDC);
236 if (pdc == NULL)
237 {
238 DPRINT("Could not lock DC\n");
239 SetLastWin32Error(ERROR_INVALID_HANDLE);
240 return 0;
241 }
242
243 /* Allocate a new dc */
244 pdcSave = DC_AllocDcWithHandle();
245 if (pdcSave == NULL)
246 {
247 DPRINT("Could not allocate a new DC\n");
248 DC_UnlockDc(pdc);
249 return 0;
250 }
251 hdcSave = pdcSave->BaseObject.hHmgr;
252
253 InterlockedIncrement(&pdc->ppdev->cPdevRefs);
254 DC_vInitDc(pdcSave, DCTYPE_MEMORY, pdc->ppdev);
255
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;
261
262 /* Make it a kernel handle
263 (FIXME: windows handles this different, see wiki)*/
264 GDIOBJ_SetOwnership(hdcSave, NULL);
265
266 /* Copy the current state */
267 DC_vCopyState(pdc, pdcSave, TRUE);
268
269 /* Only memory DC's change their surface */
270 if (pdc->dctype == DCTYPE_MEMORY)
271 DC_vSelectSurface(pdcSave, pdc->dclevel.pSurface);
272
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;
277
278 /* Set new dc as save dc */
279 pdc->dclevel.hdcSave = hdcSave;
280
281 /* Increase save depth, return old value */
282 lSaveDepth = pdc->dclevel.lSaveDepth++;
283
284 /* Cleanup and return */
285 DC_UnlockDc(pdcSave);
286 DC_UnlockDc(pdc);
287
288 DPRINT("Leave NtGdiSaveDC: %ld, hdcSave = %p\n", lSaveDepth, hdcSave);
289 return lSaveDepth;
290 }
291