d292e2e83173fd7c96d0b26fc3befd3dac83ba2c
[reactos.git] / win32ss / gdi / ntgdi / gdibatch.c
1
2 #include <win32k.h>
3
4 #define NDEBUG
5 #include <debug.h>
6
7 BOOL FASTCALL IntPatBlt( PDC,INT,INT,INT,INT,DWORD,PEBRUSHOBJ);
8 BOOL APIENTRY IntExtTextOutW(IN PDC,IN INT,IN INT,IN UINT,IN OPTIONAL PRECTL,IN LPCWSTR,IN INT,IN OPTIONAL LPINT,IN DWORD);
9
10
11 //
12 // Gdi Batch Flush support functions.
13 //
14
15 //
16 // DoDeviceSync
17 //
18 // based on IntEngEnter from eng/engmisc.c
19 //
20 VOID
21 FASTCALL
22 DoDeviceSync( SURFOBJ *Surface, PRECTL Rect, FLONG fl)
23 {
24 PPDEVOBJ Device = (PDEVOBJ*)Surface->hdev;
25 // No punting and "Handle to a surface, provided that the surface is device-managed.
26 // Otherwise, dhsurf is zero".
27 if (!(Device->flFlags & PDEV_DRIVER_PUNTED_CALL) && (Surface->dhsurf))
28 {
29 if (Device->DriverFunctions.SynchronizeSurface)
30 {
31 Device->DriverFunctions.SynchronizeSurface(Surface, Rect, fl);
32 }
33 else
34 {
35 if (Device->DriverFunctions.Synchronize)
36 {
37 Device->DriverFunctions.Synchronize(Surface->dhpdev, Rect);
38 }
39 }
40 }
41 }
42
43 VOID
44 FASTCALL
45 SynchronizeDriver(FLONG Flags)
46 {
47 SURFOBJ *SurfObj;
48 //PPDEVOBJ Device;
49
50 if (Flags & GCAPS2_SYNCFLUSH)
51 Flags = DSS_FLUSH_EVENT;
52 if (Flags & GCAPS2_SYNCTIMER)
53 Flags = DSS_TIMER_EVENT;
54
55 //Device = IntEnumHDev();
56 // UNIMPLEMENTED;
57 //ASSERT(FALSE);
58 SurfObj = 0;// EngLockSurface( Device->pSurface );
59 if(!SurfObj) return;
60 DoDeviceSync( SurfObj, NULL, Flags);
61 EngUnlockSurface(SurfObj);
62 return;
63 }
64
65 //
66 // Process the batch.
67 //
68 ULONG
69 FASTCALL
70 GdiFlushUserBatch(PDC dc, PGDIBATCHHDR pHdr)
71 {
72 ULONG Cmd = 0, Size = 0;
73 PDC_ATTR pdcattr = NULL;
74
75 if (dc)
76 {
77 pdcattr = dc->pdcattr;
78 }
79
80 _SEH2_TRY
81 {
82 Cmd = pHdr->Cmd;
83 Size = pHdr->Size; // Return the full size of the structure.
84 }
85 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
86 {
87 DPRINT1("WARNING! GdiBatch Fault!\n");
88 _SEH2_YIELD(return 0;)
89 }
90 _SEH2_END;
91
92 switch(Cmd)
93 {
94 case GdiBCPatBlt:
95 {
96 PGDIBSPATBLT pgDPB;
97 DWORD dwRop, flags;
98 HBRUSH hOrgBrush;
99 COLORREF crColor, crBkColor, crBrushClr;
100 ULONG ulForegroundClr, ulBackgroundClr, ulBrushClr;
101 if (!dc) break;
102 pgDPB = (PGDIBSPATBLT) pHdr;
103 /* Convert the ROP3 to a ROP4 */
104 dwRop = pgDPB->dwRop;
105 dwRop = MAKEROP4(dwRop & 0xFF0000, dwRop);
106 /* Check if the rop uses a source */
107 if (WIN32_ROP4_USES_SOURCE(dwRop))
108 {
109 /* This is not possible */
110 break;
111 }
112 /* Check if the DC has no surface (empty mem or info DC) */
113 if (dc->dclevel.pSurface == NULL)
114 {
115 /* Nothing to do */
116 break;
117 }
118 // Save current attributes and flags
119 crColor = dc->pdcattr->crForegroundClr;
120 crBkColor = dc->pdcattr->ulBackgroundClr;
121 crBrushClr = dc->pdcattr->crBrushClr;
122 ulForegroundClr = dc->pdcattr->ulForegroundClr;
123 ulBackgroundClr = dc->pdcattr->ulBackgroundClr;
124 ulBrushClr = dc->pdcattr->ulBrushClr;
125 hOrgBrush = dc->pdcattr->hbrush;
126 flags = dc->pdcattr->ulDirty_ & (DIRTY_BACKGROUND | DIRTY_TEXT | DIRTY_FILL | DC_BRUSH_DIRTY);
127 // Set the attribute snapshot
128 dc->pdcattr->hbrush = pgDPB->hbrush;
129 dc->pdcattr->crForegroundClr = pgDPB->crForegroundClr;
130 dc->pdcattr->crBackgroundClr = pgDPB->crBackgroundClr;
131 dc->pdcattr->crBrushClr = pgDPB->crBrushClr;
132 dc->pdcattr->ulForegroundClr = pgDPB->ulForegroundClr;
133 dc->pdcattr->ulBackgroundClr = pgDPB->ulBackgroundClr;
134 dc->pdcattr->ulBrushClr = pgDPB->ulBrushClr;
135 // Process dirty attributes if any.
136 if (dc->pdcattr->ulDirty_ & (DIRTY_FILL | DC_BRUSH_DIRTY))
137 DC_vUpdateFillBrush(dc);
138 if (dc->pdcattr->ulDirty_ & DIRTY_TEXT)
139 DC_vUpdateTextBrush(dc);
140 if (pdcattr->ulDirty_ & DIRTY_BACKGROUND)
141 DC_vUpdateBackgroundBrush(dc);
142 /* Call the internal function */
143 IntPatBlt(dc, pgDPB->nXLeft, pgDPB->nYLeft, pgDPB->nWidth, pgDPB->nHeight, dwRop, &dc->eboFill);
144 // Restore attributes and flags
145 dc->pdcattr->hbrush = hOrgBrush;
146 dc->pdcattr->crForegroundClr = crColor;
147 dc->pdcattr->crBackgroundClr = crBkColor;
148 dc->pdcattr->crBrushClr = crBrushClr;
149 dc->pdcattr->ulForegroundClr = ulForegroundClr;
150 dc->pdcattr->ulBackgroundClr = ulBackgroundClr;
151 dc->pdcattr->ulBrushClr = ulBrushClr;
152 dc->pdcattr->ulDirty_ |= flags;
153 break;
154 }
155
156 case GdiBCPolyPatBlt:
157 {
158 PGDIBSPPATBLT pgDPB;
159 EBRUSHOBJ eboFill;
160 PBRUSH pbrush;
161 PPATRECT pRects;
162 INT i;
163 DWORD dwRop, flags;
164 COLORREF crColor, crBkColor, crBrushClr;
165 ULONG ulForegroundClr, ulBackgroundClr, ulBrushClr;
166 if (!dc) break;
167 pgDPB = (PGDIBSPPATBLT) pHdr;
168 /* Convert the ROP3 to a ROP4 */
169 dwRop = pgDPB->rop4;
170 dwRop = MAKEROP4(dwRop & 0xFF0000, dwRop);
171 /* Check if the rop uses a source */
172 if (WIN32_ROP4_USES_SOURCE(dwRop))
173 {
174 /* This is not possible */
175 break;
176 }
177 /* Check if the DC has no surface (empty mem or info DC) */
178 if (dc->dclevel.pSurface == NULL)
179 {
180 /* Nothing to do */
181 break;
182 }
183 // Save current attributes and flags
184 crColor = dc->pdcattr->crForegroundClr;
185 crBkColor = dc->pdcattr->ulBackgroundClr;
186 crBrushClr = dc->pdcattr->crBrushClr;
187 ulForegroundClr = dc->pdcattr->ulForegroundClr;
188 ulBackgroundClr = dc->pdcattr->ulBackgroundClr;
189 ulBrushClr = dc->pdcattr->ulBrushClr;
190 flags = dc->pdcattr->ulDirty_ & (DIRTY_BACKGROUND | DIRTY_TEXT | DIRTY_FILL | DC_BRUSH_DIRTY);
191 // Set the attribute snapshot
192 dc->pdcattr->crForegroundClr = pgDPB->crForegroundClr;
193 dc->pdcattr->crBackgroundClr = pgDPB->crBackgroundClr;
194 dc->pdcattr->crBrushClr = pgDPB->crBrushClr;
195 dc->pdcattr->ulForegroundClr = pgDPB->ulForegroundClr;
196 dc->pdcattr->ulBackgroundClr = pgDPB->ulBackgroundClr;
197 dc->pdcattr->ulBrushClr = pgDPB->ulBrushClr;
198 // Process dirty attributes if any
199 if (dc->pdcattr->ulDirty_ & DIRTY_TEXT)
200 DC_vUpdateTextBrush(dc);
201 if (pdcattr->ulDirty_ & DIRTY_BACKGROUND)
202 DC_vUpdateBackgroundBrush(dc);
203
204 DPRINT1("GdiBCPolyPatBlt Testing\n");
205 pRects = &pgDPB->pRect[0];
206
207 for (i = 0; i < pgDPB->Count; i++)
208 {
209 pbrush = BRUSH_ShareLockBrush(pRects->hBrush);
210
211 /* Check if we could lock the brush */
212 if (pbrush != NULL)
213 {
214 /* Initialize a brush object */
215 EBRUSHOBJ_vInitFromDC(&eboFill, pbrush, dc);
216
217 IntPatBlt(
218 dc,
219 pRects->r.left,
220 pRects->r.top,
221 pRects->r.right,
222 pRects->r.bottom,
223 dwRop,
224 &eboFill);
225
226 /* Cleanup the brush object and unlock the brush */
227 EBRUSHOBJ_vCleanup(&eboFill);
228 BRUSH_ShareUnlockBrush(pbrush);
229 }
230 pRects++;
231 }
232
233 // Restore attributes and flags
234 dc->pdcattr->crForegroundClr = crColor;
235 dc->pdcattr->crBackgroundClr = crBkColor;
236 dc->pdcattr->crBrushClr = crBrushClr;
237 dc->pdcattr->ulForegroundClr = ulForegroundClr;
238 dc->pdcattr->ulBackgroundClr = ulBackgroundClr;
239 dc->pdcattr->ulBrushClr = ulBrushClr;
240 dc->pdcattr->ulDirty_ |= flags;
241 break;
242 }
243
244 case GdiBCTextOut:
245 {
246 PGDIBSTEXTOUT pgO;
247 COLORREF crColor = -1, crBkColor;
248 ULONG ulForegroundClr, ulBackgroundClr;
249 DWORD flags = 0, flXform = 0, saveflags, saveflXform = 0;
250 FLONG flTextAlign = -1;
251 HANDLE hlfntNew;
252 PRECTL lprc;
253 USHORT jBkMode;
254 LONG lBkMode;
255 POINTL ptlViewportOrg;
256 if (!dc) break;
257 pgO = (PGDIBSTEXTOUT) pHdr;
258
259 // Save current attributes, flags and Set the attribute snapshots
260 saveflags = dc->pdcattr->ulDirty_ & (DIRTY_BACKGROUND|DIRTY_LINE|DIRTY_TEXT|DIRTY_FILL|DC_BRUSH_DIRTY|DIRTY_CHARSET);
261
262 // In this instance check for differences and set the appropriate dirty flags.
263 if ( dc->pdcattr->crForegroundClr != pgO->crForegroundClr)
264 {
265 crColor = dc->pdcattr->crForegroundClr;
266 dc->pdcattr->crForegroundClr = pgO->crForegroundClr;
267 ulForegroundClr = dc->pdcattr->ulForegroundClr;
268 dc->pdcattr->ulForegroundClr = pgO->ulForegroundClr;
269 flags |= (DIRTY_FILL|DIRTY_LINE|DIRTY_TEXT);
270 }
271 if (dc->pdcattr->crBackgroundClr != pgO->crBackgroundClr)
272 {
273 crBkColor = dc->pdcattr->ulBackgroundClr;
274 dc->pdcattr->crBackgroundClr = pgO->crBackgroundClr;
275 ulBackgroundClr = dc->pdcattr->ulBackgroundClr;
276 dc->pdcattr->ulBackgroundClr = pgO->ulBackgroundClr;
277 flags |= (DIRTY_FILL|DIRTY_LINE|DIRTY_TEXT|DIRTY_BACKGROUND);
278 }
279 if (dc->pdcattr->flTextAlign != pgO->flTextAlign)
280 {
281 flTextAlign = dc->pdcattr->flTextAlign;
282 dc->pdcattr->flTextAlign = pgO->flTextAlign;
283 }
284 if (dc->pdcattr->hlfntNew != pgO->hlfntNew)
285 {
286 hlfntNew = dc->pdcattr->hlfntNew;
287 dc->pdcattr->hlfntNew = pgO->hlfntNew;
288 dc->pdcattr->ulDirty_ &= ~SLOW_WIDTHS;
289 flags |= DIRTY_CHARSET;
290 }
291
292 if ( dc->pdcattr->ptlViewportOrg.x != pgO->ptlViewportOrg.x ||
293 dc->pdcattr->ptlViewportOrg.y != pgO->ptlViewportOrg.y )
294 {
295 saveflXform = dc->pdcattr->flXform & (PAGE_XLATE_CHANGED|WORLD_XFORM_CHANGED|DEVICE_TO_WORLD_INVALID);
296 ptlViewportOrg = dc->pdcattr->ptlViewportOrg;
297 dc->pdcattr->ptlViewportOrg = pgO->ptlViewportOrg;
298 flXform = (PAGE_XLATE_CHANGED|WORLD_XFORM_CHANGED|DEVICE_TO_WORLD_INVALID);
299 }
300
301 dc->pdcattr->ulDirty_ |= flags;
302
303 jBkMode = dc->pdcattr->jBkMode;
304 dc->pdcattr->jBkMode = pgO->lBkMode;
305 lBkMode = dc->pdcattr->lBkMode;
306 dc->pdcattr->lBkMode = pgO->lBkMode;
307
308 lprc = (pgO->Options & GDIBS_NORECT) ? NULL : &pgO->Rect;
309 pgO->Options &= ~GDIBS_NORECT;
310
311 IntExtTextOutW( dc,
312 pgO->x,
313 pgO->y,
314 pgO->Options,
315 lprc,
316 (LPCWSTR)&pgO->String[pgO->Size/sizeof(WCHAR)],
317 pgO->cbCount,
318 pgO->Size ? (LPINT)&pgO->Buffer : NULL,
319 pgO->iCS_CP );
320
321 // Restore attributes and flags
322 dc->pdcattr->jBkMode = jBkMode;
323 dc->pdcattr->lBkMode = lBkMode;
324
325 if (saveflXform)
326 {
327 dc->pdcattr->ptlViewportOrg = ptlViewportOrg;
328 dc->pdcattr->flXform |= saveflXform|flXform;
329 }
330
331 if (flags & DIRTY_TEXT && crColor != -1)
332 {
333 dc->pdcattr->crForegroundClr = crColor;
334 dc->pdcattr->ulForegroundClr = ulForegroundClr;
335 }
336 if (flags & DIRTY_BACKGROUND)
337 {
338 dc->pdcattr->crBackgroundClr = crBkColor;
339 dc->pdcattr->ulBackgroundClr = ulBackgroundClr;
340 }
341 if (flTextAlign != -1)
342 {
343 dc->pdcattr->flTextAlign = flTextAlign;
344 }
345
346 if (flags & DIRTY_CHARSET)
347 {
348 dc->pdcattr->hlfntNew = hlfntNew;
349 dc->pdcattr->ulDirty_ &= ~SLOW_WIDTHS;
350 }
351 dc->pdcattr->ulDirty_ |= saveflags | flags;
352 break;
353 }
354
355 case GdiBCExtTextOut:
356 {
357 PGDIBSEXTTEXTOUT pgO;
358 COLORREF crBkColor;
359 ULONG ulBackgroundClr;
360 POINTL ptlViewportOrg;
361 DWORD flags = 0, flXform = 0, saveflags, saveflXform = 0;
362 if (!dc) break;
363 pgO = (PGDIBSEXTTEXTOUT) pHdr;
364
365 saveflags = dc->pdcattr->ulDirty_ & (DIRTY_BACKGROUND|DIRTY_TEXT|DIRTY_FILL|DC_BRUSH_DIRTY|DIRTY_CHARSET);
366
367 if (dc->pdcattr->crBackgroundClr != pgO->ulBackgroundClr)
368 {
369 crBkColor = dc->pdcattr->crBackgroundClr;
370 ulBackgroundClr = dc->pdcattr->ulBackgroundClr;
371 dc->pdcattr->crBackgroundClr = pgO->ulBackgroundClr;
372 dc->pdcattr->ulBackgroundClr = pgO->ulBackgroundClr;
373 flags |= (DIRTY_BACKGROUND|DIRTY_LINE|DIRTY_FILL);
374 }
375
376 if ( dc->pdcattr->ptlViewportOrg.x != pgO->ptlViewportOrg.x ||
377 dc->pdcattr->ptlViewportOrg.y != pgO->ptlViewportOrg.y )
378 {
379 saveflXform = dc->pdcattr->flXform & (PAGE_XLATE_CHANGED|WORLD_XFORM_CHANGED|DEVICE_TO_WORLD_INVALID);
380 ptlViewportOrg = dc->pdcattr->ptlViewportOrg;
381 dc->pdcattr->ptlViewportOrg = pgO->ptlViewportOrg;
382 flXform = (PAGE_XLATE_CHANGED|WORLD_XFORM_CHANGED|DEVICE_TO_WORLD_INVALID);
383 }
384
385 dc->pdcattr->ulDirty_ |= flags;
386
387 IntExtTextOutW( dc,
388 0,
389 0,
390 pgO->Options,
391 &pgO->Rect,
392 NULL,
393 pgO->Count,
394 NULL,
395 0 );
396
397 if (saveflXform)
398 {
399 dc->pdcattr->ptlViewportOrg = ptlViewportOrg;
400 dc->pdcattr->flXform |= saveflXform|flXform;
401 }
402
403 if (flags & DIRTY_BACKGROUND)
404 {
405 dc->pdcattr->crBackgroundClr = crBkColor;
406 dc->pdcattr->ulBackgroundClr = ulBackgroundClr;
407 }
408 dc->pdcattr->ulDirty_ |= saveflags | flags;
409 break;
410 }
411
412 case GdiBCSetBrushOrg:
413 {
414 PGDIBSSETBRHORG pgSBO;
415 if (!dc) break;
416 pgSBO = (PGDIBSSETBRHORG) pHdr;
417 pdcattr->ptlBrushOrigin = pgSBO->ptlBrushOrigin;
418 DC_vSetBrushOrigin(dc, pgSBO->ptlBrushOrigin.x, pgSBO->ptlBrushOrigin.y);
419 break;
420 }
421
422 case GdiBCExtSelClipRgn:
423 break;
424
425 case GdiBCSelObj:
426 {
427 PGDIBSOBJECT pgO;
428
429 if (!dc) break;
430 pgO = (PGDIBSOBJECT) pHdr;
431
432 DC_hSelectFont(dc, (HFONT)pgO->hgdiobj);
433 break;
434 }
435
436 case GdiBCDelRgn:
437 DPRINT("Delete Region Object!\n");
438 /* Fall through */
439 case GdiBCDelObj:
440 {
441 PGDIBSOBJECT pgO = (PGDIBSOBJECT) pHdr;
442 GreDeleteObject( pgO->hgdiobj );
443 break;
444 }
445
446 default:
447 break;
448 }
449
450 return Size;
451 }
452
453 /*
454 * NtGdiFlush
455 *
456 * Flushes the calling thread's current batch.
457 */
458 __kernel_entry
459 NTSTATUS
460 APIENTRY
461 NtGdiFlush(
462 VOID)
463 {
464 SynchronizeDriver(GCAPS2_SYNCFLUSH);
465 return STATUS_SUCCESS;
466 }
467
468 /*
469 * NtGdiFlushUserBatch
470 *
471 * Callback for thread batch flush routine.
472 *
473 * Think small & fast!
474 */
475 NTSTATUS
476 APIENTRY
477 NtGdiFlushUserBatch(VOID)
478 {
479 PTEB pTeb = NtCurrentTeb();
480 ULONG GdiBatchCount = pTeb->GdiBatchCount;
481
482 if( (GdiBatchCount > 0) && (GdiBatchCount <= (GDIBATCHBUFSIZE/4)))
483 {
484 HDC hDC = (HDC) pTeb->GdiTebBatch.HDC;
485
486 /* If hDC is zero and the buffer fills up with delete objects we need
487 to run anyway.
488 */
489 if (hDC || GdiBatchCount)
490 {
491 PCHAR pHdr = (PCHAR)&pTeb->GdiTebBatch.Buffer[0];
492 PDC pDC = NULL;
493
494 if (GDI_HANDLE_GET_TYPE(hDC) == GDILoObjType_LO_DC_TYPE && GreIsHandleValid(hDC))
495 {
496 pDC = DC_LockDc(hDC);
497 }
498
499 // No need to init anything, just go!
500 for (; GdiBatchCount > 0; GdiBatchCount--)
501 {
502 ULONG Size;
503 // Process Gdi Batch!
504 Size = GdiFlushUserBatch(pDC, (PGDIBATCHHDR) pHdr);
505 if (!Size) break;
506 pHdr += Size;
507 }
508
509 if (pDC)
510 {
511 DC_UnlockDc(pDC);
512 }
513
514 // Exit and clear out for the next round.
515 pTeb->GdiTebBatch.Offset = 0;
516 pTeb->GdiBatchCount = 0;
517 pTeb->GdiTebBatch.HDC = 0;
518 }
519 }
520
521 // FIXME: On Windows XP the function returns &pTeb->RealClientId, maybe VOID?
522 return STATUS_SUCCESS;
523 }
524
525