gdi32: Now that we have our own Bresenham routine, we don't need the LPARAM cast.
[wine] / dlls / gdi32 / dibdrv / objects.c
1 /*
2  * DIB driver GDI objects.
3  *
4  * Copyright 2011 Huw Davies
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdlib.h>
22
23 #include "gdi_private.h"
24 #include "dibdrv.h"
25
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(dib);
29
30 /*
31  *
32  * Decompose the 16 ROP2s into an expression of the form
33  *
34  * D = (D & A) ^ X
35  *
36  * Where A and X depend only on P (and so can be precomputed).
37  *
38  *                                       A    X
39  *
40  * R2_BLACK         0                    0    0
41  * R2_NOTMERGEPEN   ~(D | P)            ~P   ~P
42  * R2_MASKNOTPEN    ~P & D              ~P    0
43  * R2_NOTCOPYPEN    ~P                   0   ~P
44  * R2_MASKPENNOT    P & ~D               P    P
45  * R2_NOT           ~D                   1    1
46  * R2_XORPEN        P ^ D                1    P
47  * R2_NOTMASKPEN    ~(P & D)             P    1
48  * R2_MASKPEN       P & D                P    0
49  * R2_NOTXORPEN     ~(P ^ D)             1   ~P
50  * R2_NOP           D                    1    0
51  * R2_MERGENOTPEN   ~P | D               P   ~P
52  * R2_COPYPEN       P                    0    P
53  * R2_MERGEPENNOT   P | ~D              ~P    1
54  * R2_MERGEPEN      P | D               ~P    P
55  * R2_WHITE         1                    0    1
56  *
57  */
58
59 /* A = (P & A1) | (~P & A2) */
60 #define ZERO {0, 0}
61 #define ONE {0xffffffff, 0xffffffff}
62 #define P {0xffffffff, 0}
63 #define NOT_P {0, 0xffffffff}
64
65 static const DWORD rop2_and_array[16][2] =
66 {
67     ZERO, NOT_P, NOT_P, ZERO,
68     P,    ONE,   ONE,   P,
69     P,    ONE,   ONE,   P,
70     ZERO, NOT_P, NOT_P, ZERO
71 };
72
73 /* X = (P & X1) | (~P & X2) */
74 static const DWORD rop2_xor_array[16][2] =
75 {
76     ZERO, NOT_P, ZERO, NOT_P,
77     P,    ONE,   P,    ONE,
78     ZERO, NOT_P, ZERO, NOT_P,
79     P,    ONE,   P,    ONE
80 };
81
82 #undef NOT_P
83 #undef P
84 #undef ONE
85 #undef ZERO
86
87 void calc_and_xor_masks(INT rop, DWORD color, DWORD *and, DWORD *xor)
88 {
89     /* NB The ROP2 codes start at one and the arrays are zero-based */
90     *and = (color & rop2_and_array[rop-1][0]) | ((~color) & rop2_and_array[rop-1][1]);
91     *xor = (color & rop2_xor_array[rop-1][0]) | ((~color) & rop2_xor_array[rop-1][1]);
92 }
93
94 static inline void order_end_points(int *s, int *e)
95 {
96     if(*s > *e)
97     {
98         int tmp;
99         tmp = *s + 1;
100         *s = *e + 1;
101         *e = tmp;
102     }
103 }
104
105 static inline BOOL pt_in_rect( const RECT *rect, const POINT *pt )
106 {
107     return ((pt->x >= rect->left) && (pt->x < rect->right) &&
108             (pt->y >= rect->top) && (pt->y < rect->bottom));
109 }
110
111 #define Y_INCREASING_MASK 0x0f
112 #define X_INCREASING_MASK 0xc3
113 #define X_MAJOR_MASK      0x99
114 #define POS_SLOPE_MASK    0x33
115
116 static inline BOOL is_xmajor(DWORD octant)
117 {
118     return octant & X_MAJOR_MASK;
119 }
120
121 static inline BOOL is_pos_slope(DWORD octant)
122 {
123     return octant & POS_SLOPE_MASK;
124 }
125
126 static inline BOOL is_x_increasing(DWORD octant)
127 {
128     return octant & X_INCREASING_MASK;
129 }
130
131 static inline BOOL is_y_increasing(DWORD octant)
132 {
133     return octant & Y_INCREASING_MASK;
134 }
135
136 /**********************************************************************
137  *                  get_octant_number
138  *
139  * Return the octant number starting clockwise from the +ve x-axis.
140  */
141 static inline int get_octant_number(int dx, int dy)
142 {
143     if(dy > 0)
144         if(dx > 0)
145             return ( dx >  dy) ? 1 : 2;
146         else
147             return (-dx >  dy) ? 4 : 3;
148     else
149         if(dx < 0)
150             return (-dx > -dy) ? 5 : 6;
151         else
152             return ( dx > -dy) ? 8 : 7;
153 }
154
155 static inline DWORD get_octant_mask(int dx, int dy)
156 {
157     return 1 << (get_octant_number(dx, dy) - 1);
158 }
159
160 static void solid_pen_line_callback(dibdrv_physdev *pdev, INT x, INT y)
161 {
162     RECT rect;
163
164     rect.left   = x;
165     rect.right  = x + 1;
166     rect.top    = y;
167     rect.bottom = y + 1;
168     pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
169     return;
170 }
171
172 #define OUT_LEFT    1
173 #define OUT_RIGHT   2
174 #define OUT_TOP     4
175 #define OUT_BOTTOM  8
176
177 static inline DWORD calc_outcode(const POINT *pt, const RECT *clip)
178 {
179     DWORD out = 0;
180     if(pt->x < clip->left)         out |= OUT_LEFT;
181     else if(pt->x >= clip->right)  out |= OUT_RIGHT;
182     if(pt->y < clip->top)          out |= OUT_TOP;
183     else if(pt->y >= clip->bottom) out |= OUT_BOTTOM;
184
185     return out;
186 }
187
188 typedef struct
189 {
190     unsigned int dx, dy;
191     int bias;
192     DWORD octant;
193 } bres_params;
194
195 /******************************************************************************
196  *                clip_line
197  *
198  * Clips the start and end points to a rectangle.
199  *
200  * Note, this treats the end point like the start point.  If the
201  * caller doesn't want it displayed, it should exclude it.  If the end
202  * point is clipped out, then the likelihood is that the new end point
203  * should be displayed.
204  *
205  * Returns 0 if totally excluded, 1 if partially clipped and 2 if unclipped.
206  *
207  * This derivation is based on the comments in X.org's xserver/mi/mizerclip.c,
208  * however the Bresenham error term is defined differently so the equations
209  * will also differ.
210  *
211  * For x major lines we have 2dy >= err + bias > 2dy - 2dx
212  *                           0   >= err + bias - 2dy > -2dx
213  *
214  * Note dx, dy, m and n are all +ve.
215  *
216  * Moving the start pt from x1 to x1 + m, we need to figure out y1 + n.
217  *                     err = 2dy - dx + 2mdy - 2ndx
218  *                      0 >= 2dy - dx + 2mdy - 2ndx + bias - 2dy > -2dx
219  *                      0 >= 2mdy - 2ndx + bias - dx > -2dx
220  *                      which of course will give exactly one solution for n,
221  *                      so looking at the >= inequality
222  *                      n >= (2mdy + bias - dx) / 2dx
223  *                      n = ceiling((2mdy + bias - dx) / 2dx)
224  *                        = (2mdy + bias + dx - 1) / 2dx (assuming division truncation)
225  *
226  * Moving start pt from y1 to y1 + n we need to figure out x1 + m - there may be several
227  * solutions we pick the one that minimizes m (ie that first unlipped pt). As above:
228  *                     0 >= 2mdy - 2ndx + bias - dx > -2dx
229  *                  2mdy > 2ndx - bias - dx
230  *                     m > (2ndx - bias - dx) / 2dy
231  *                     m = floor((2ndx - bias - dx) / 2dy) + 1
232  *                     m = (2ndx - bias - dx) / 2dy + 1
233  *
234  * Moving end pt from x2 to x2 - m, we need to figure out y2 - n
235  *                  err = 2dy - dx + 2(dx - m)dy - 2(dy - n)dx
236  *                      = 2dy - dx - 2mdy + 2ndx
237  *                   0 >= 2dy - dx - 2mdy + 2ndx + bias - 2dy > -2dx
238  *                   0 >= 2ndx - 2mdy + bias - dx > -2dx
239  *                   again exactly one solution.
240  *                   2ndx <= 2mdy - bias + dx
241  *                   n = floor((2mdy - bias + dx) / 2dx)
242  *                     = (2mdy - bias + dx) / 2dx
243  *
244  * Moving end pt from y2 to y2 - n when need x2 - m this time maximizing x2 - m so
245  * mininizing m to include all of the points at y = y2 - n.  As above:
246  *                  0 >= 2ndx - 2mdy + bias - dx > -2dx
247  *               2mdy >= 2ndx + bias - dx
248  *                   m = ceiling((2ndx + bias - dx) / 2dy)
249  *                     = (2ndx + bias - dx - 1) / 2dy + 1
250  *
251  * For y major lines, symmetry (dx <-> dy and swap the cases over) gives:
252  *
253  * Moving start point from y1 to y1 + n find x1 + m
254  *                     m = (2ndx + bias + dy - 1) / 2dy
255  *
256  * Moving start point from x1 to x1 + m find y1 + n
257  *                     n = (2mdy - bias - dy) / 2ndx + 1
258  *
259  * Moving end point from y2 to y2 - n find x1 - m
260  *                     m = (2ndx - bias + dy) / 2dy
261  *
262  * Moving end point from x2 to x2 - m find y2 - n
263  *                     n = (2mdy + bias - dy - 1) / 2dx + 1
264  */
265 static int clip_line(const POINT *start, const POINT *end, const RECT *clip,
266                      const bres_params *params, POINT *pt1, POINT *pt2)
267 {
268     unsigned int n, m;
269     BOOL clipped = FALSE;
270     DWORD start_oc, end_oc;
271     const int bias = params->bias;
272     const unsigned int dx = params->dx;
273     const unsigned int dy = params->dy;
274     const unsigned int two_dx = params->dx * 2;
275     const unsigned int two_dy = params->dy * 2;
276     const BOOL xmajor = is_xmajor(params->octant);
277     const BOOL neg_slope = !is_pos_slope(params->octant);
278
279     *pt1 = *start;
280     *pt2 = *end;
281
282     start_oc = calc_outcode(start, clip);
283     end_oc = calc_outcode(end, clip);
284
285     while(1)
286     {
287         if(start_oc == 0 && end_oc == 0) return clipped ? 1 : 2; /* trivial accept */
288         if(start_oc & end_oc)            return 0; /* trivial reject */
289
290         clipped = TRUE;
291         if(start_oc & OUT_LEFT)
292         {
293             m = clip->left - start->x;
294             if(xmajor)
295                 n = (m * two_dy + bias + dx - 1) / two_dx;
296             else
297                 n = (m * two_dy - bias - dy) / two_dx + 1;
298
299             pt1->x = clip->left;
300             if(neg_slope) n = -n;
301             pt1->y = start->y + n;
302             start_oc = calc_outcode(pt1, clip);
303         }
304         else if(start_oc & OUT_RIGHT)
305         {
306             m = start->x - clip->right + 1;
307             if(xmajor)
308                 n = (m * two_dy + bias + dx - 1) / two_dx;
309             else
310                 n = (m * two_dy - bias - dy) / two_dx + 1;
311
312             pt1->x = clip->right - 1;
313             if(neg_slope) n = -n;
314             pt1->y = start->y - n;
315             start_oc = calc_outcode(pt1, clip);
316         }
317         else if(start_oc & OUT_TOP)
318         {
319             n = clip->top - start->y;
320             if(xmajor)
321                 m = (n * two_dx - bias - dx) / two_dy + 1;
322             else
323                 m = (n * two_dx + bias + dy - 1) / two_dy;
324
325             pt1->y = clip->top;
326             if(neg_slope) m = -m;
327             pt1->x = start->x + m;
328             start_oc = calc_outcode(pt1, clip);
329         }
330         else if(start_oc & OUT_BOTTOM)
331         {
332             n = start->y - clip->bottom + 1;
333             if(xmajor)
334                 m = (n * two_dx - bias - dx) / two_dy + 1;
335             else
336                 m = (n * two_dx + bias + dy - 1) / two_dy;
337
338             pt1->y = clip->bottom - 1;
339             if(neg_slope) m = -m;
340             pt1->x = start->x - m;
341             start_oc = calc_outcode(pt1, clip);
342         }
343         else if(end_oc & OUT_LEFT)
344         {
345             m = clip->left - end->x;
346             if(xmajor)
347                 n = (m * two_dy - bias + dx) / two_dx;
348             else
349                 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
350
351             pt2->x = clip->left;
352             if(neg_slope) n = -n;
353             pt2->y = end->y + n;
354             end_oc = calc_outcode(pt2, clip);
355         }
356         else if(end_oc & OUT_RIGHT)
357         {
358             m = end->x - clip->right + 1;
359             if(xmajor)
360                 n = (m * two_dy - bias + dx) / two_dx;
361             else
362                 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
363
364             pt2->x = clip->right - 1;
365             if(neg_slope) n = -n;
366             pt2->y = end->y - n;
367             end_oc = calc_outcode(pt2, clip);
368         }
369         else if(end_oc & OUT_TOP)
370         {
371             n = clip->top - end->y;
372             if(xmajor)
373                 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
374             else
375                 m = (n * two_dx - bias + dy) / two_dy;
376
377             pt2->y = clip->top;
378             if(neg_slope) m = -m;
379             pt2->x = end->x + m;
380             end_oc = calc_outcode(pt2, clip);
381         }
382         else if(end_oc & OUT_BOTTOM)
383         {
384             n = end->y - clip->bottom + 1;
385             if(xmajor)
386                 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
387             else
388                 m = (n * two_dx - bias + dy) / two_dy;
389
390             pt2->y = clip->bottom - 1;
391             if(neg_slope) m = -m;
392             pt2->x = end->x - m;
393             end_oc = calc_outcode(pt2, clip);
394         }
395     }
396 }
397
398 static void bres_line_with_bias(INT x1, INT y1, INT x2, INT y2, const bres_params *params, INT err,
399                                 BOOL last_pt, void (* callback)(dibdrv_physdev*,INT,INT), dibdrv_physdev *pdev)
400 {
401     const int xadd = is_x_increasing(params->octant) ? 1 : -1;
402     const int yadd = is_y_increasing(params->octant) ? 1 : -1;
403     INT erradd;
404
405     if (is_xmajor(params->octant))  /* line is "more horizontal" */
406     {
407         erradd = 2*params->dy - 2*params->dx;
408         while(x1 != x2)
409         {
410             callback(pdev, x1, y1);
411             if (err + params->bias > 0)
412             {
413                 y1 += yadd;
414                 err += erradd;
415             }
416             else err += 2*params->dy;
417             x1 += xadd;
418         }
419         if(last_pt) callback(pdev, x1, y1);
420     }
421     else   /* line is "more vertical" */
422     {
423         erradd = 2*params->dx - 2*params->dy;
424         while(y1 != y2)
425         {
426             callback(pdev, x1, y1);
427             if (err + params->bias > 0)
428             {
429                 x1 += xadd;
430                 err += erradd;
431             }
432             else err += 2*params->dx;
433             y1 += yadd;
434         }
435         if(last_pt) callback(pdev, x1, y1);
436     }
437 }
438
439 static BOOL solid_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
440 {
441     const WINEREGION *clip = get_wine_region(pdev->clip);
442
443     if(start->y == end->y)
444     {
445         RECT rect;
446         int i;
447
448         rect.left   = start->x;
449         rect.top    = start->y;
450         rect.right  = end->x;
451         rect.bottom = end->y + 1;
452         order_end_points(&rect.left, &rect.right);
453         for(i = 0; i < clip->numRects; i++)
454         {
455             if(clip->rects[i].top >= rect.bottom) break;
456             if(clip->rects[i].bottom <= rect.top) continue;
457             /* Optimize the unclipped case */
458             if(clip->rects[i].left <= rect.left && clip->rects[i].right >= rect.right)
459             {
460                 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
461                 break;
462             }
463             if(clip->rects[i].right > rect.left && clip->rects[i].left < rect.right)
464             {
465                 RECT tmp = rect;
466                 tmp.left = max(rect.left, clip->rects[i].left);
467                 tmp.right = min(rect.right, clip->rects[i].right);
468                 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &tmp, pdev->pen_and, pdev->pen_xor);
469             }
470         }
471     }
472     else if(start->x == end->x)
473     {
474         RECT rect;
475         int i;
476
477         rect.left   = start->x;
478         rect.top    = start->y;
479         rect.right  = end->x + 1;
480         rect.bottom = end->y;
481         order_end_points(&rect.top, &rect.bottom);
482         for(i = 0; i < clip->numRects; i++)
483         {
484             /* Optimize unclipped case */
485             if(clip->rects[i].top <= rect.top && clip->rects[i].bottom >= rect.bottom &&
486                clip->rects[i].left <= rect.left && clip->rects[i].right >= rect.right)
487             {
488                 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
489                 break;
490             }
491             if(clip->rects[i].top >= rect.bottom) break;
492             if(clip->rects[i].bottom <= rect.top) continue;
493             if(clip->rects[i].right > rect.left && clip->rects[i].left < rect.right)
494             {
495                 RECT tmp = rect;
496                 tmp.top = max(rect.top, clip->rects[i].top);
497                 tmp.bottom = min(rect.bottom, clip->rects[i].bottom);
498                 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &tmp, pdev->pen_and, pdev->pen_xor);
499             }
500         }
501     }
502     else
503     {
504         bres_params params;
505         INT dx = end->x - start->x;
506         INT dy = end->y - start->y;
507         INT i;
508
509         params.dx = abs(dx);
510         params.dy = abs(dy);
511         params.octant = get_octant_mask(dx, dy);
512         /* Octants 3, 5, 6 and 8 take a bias */
513         params.bias = (params.octant & 0xb4) ? 1 : 0;
514
515         for(i = 0; i < clip->numRects; i++)
516         {
517             POINT clipped_start, clipped_end;
518             int clip_status;
519             clip_status = clip_line(start, end, clip->rects + i, &params, &clipped_start, &clipped_end);
520
521             if(clip_status)
522             {
523                 int m = abs(clipped_start.x - start->x);
524                 int n = abs(clipped_start.y - start->y);
525                 int err;
526                 BOOL last_pt = FALSE;
527
528                 if(is_xmajor(params.octant))
529                     err = 2 * params.dy - params.dx + m * 2 * params.dy - n * 2 * params.dx;
530                 else
531                     err = 2 * params.dx - params.dy + n * 2 * params.dx - m * 2 * params.dy;
532
533                 if(clip_status == 1 && (end->x != clipped_end.x || end->y != clipped_end.y)) last_pt = TRUE;
534
535                 bres_line_with_bias(clipped_start.x, clipped_start.y, clipped_end.x, clipped_end.y, &params,
536                                     err, last_pt, solid_pen_line_callback, pdev);
537
538                 if(clip_status == 2) break; /* completely unclipped, so we can finish */
539             }
540         }
541
542     }
543     release_wine_region(pdev->clip);
544     return TRUE;
545 }
546
547 /***********************************************************************
548  *           dibdrv_SelectPen
549  */
550 HPEN CDECL dibdrv_SelectPen( PHYSDEV dev, HPEN hpen )
551 {
552     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectPen );
553     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
554     LOGPEN logpen;
555
556     TRACE("(%p, %p)\n", dev, hpen);
557
558     if (!GetObjectW( hpen, sizeof(logpen), &logpen ))
559     {
560         /* must be an extended pen */
561         EXTLOGPEN *elp;
562         INT size = GetObjectW( hpen, 0, NULL );
563
564         if (!size) return 0;
565
566         elp = HeapAlloc( GetProcessHeap(), 0, size );
567
568         GetObjectW( hpen, size, elp );
569         /* FIXME: add support for user style pens */
570         logpen.lopnStyle = elp->elpPenStyle;
571         logpen.lopnWidth.x = elp->elpWidth;
572         logpen.lopnWidth.y = 0;
573         logpen.lopnColor = elp->elpColor;
574
575         HeapFree( GetProcessHeap(), 0, elp );
576     }
577
578     if (hpen == GetStockObject( DC_PEN ))
579         logpen.lopnColor = GetDCPenColor( dev->hdc );
580
581     pdev->pen_color = pdev->dib.funcs->colorref_to_pixel(&pdev->dib, logpen.lopnColor);
582     calc_and_xor_masks(GetROP2(dev->hdc), pdev->pen_color, &pdev->pen_and, &pdev->pen_xor);
583
584     pdev->defer |= DEFER_PEN;
585
586     switch(logpen.lopnStyle & PS_STYLE_MASK)
587     {
588     case PS_SOLID:
589         if(logpen.lopnStyle & PS_GEOMETRIC) break;
590         if(logpen.lopnWidth.x > 1) break;
591         pdev->pen_line = solid_pen_line;
592         pdev->defer &= ~DEFER_PEN;
593         break;
594     default:
595         break;
596     }
597
598     return next->funcs->pSelectPen( next, hpen );
599 }
600
601 /***********************************************************************
602  *           dibdrv_SetDCPenColor
603  */
604 COLORREF CDECL dibdrv_SetDCPenColor( PHYSDEV dev, COLORREF color )
605 {
606     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCPenColor );
607     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
608
609     if (GetCurrentObject(dev->hdc, OBJ_PEN) == GetStockObject( DC_PEN ))
610     {
611         pdev->pen_color = pdev->dib.funcs->colorref_to_pixel(&pdev->dib, color);
612         calc_and_xor_masks(GetROP2(dev->hdc), pdev->pen_color, &pdev->pen_and, &pdev->pen_xor);
613     }
614
615     return next->funcs->pSetDCPenColor( next, color );
616 }
617
618 /**********************************************************************
619  *             solid_brush
620  *
621  * Fill a number of rectangles with the solid brush
622  * FIXME: Should we insist l < r && t < b?  Currently we assume this.
623  */
624 static BOOL solid_brush(dibdrv_physdev *pdev, int num, RECT *rects)
625 {
626     int i, j;
627     const WINEREGION *clip = get_wine_region(pdev->clip);
628
629     for(i = 0; i < num; i++)
630     {
631         for(j = 0; j < clip->numRects; j++)
632         {
633             RECT rect = rects[i];
634
635             /* Optimize unclipped case */
636             if(clip->rects[j].top <= rect.top && clip->rects[j].bottom >= rect.bottom &&
637                clip->rects[j].left <= rect.left && clip->rects[j].right >= rect.right)
638             {
639                 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->brush_and, pdev->brush_xor);
640                 break;
641             }
642
643             if(clip->rects[j].top >= rect.bottom) break;
644             if(clip->rects[j].bottom <= rect.top) continue;
645
646             if(clip->rects[j].right > rect.left && clip->rects[j].left < rect.right)
647             {
648                 rect.left   = max(rect.left,   clip->rects[j].left);
649                 rect.top    = max(rect.top,    clip->rects[j].top);
650                 rect.right  = min(rect.right,  clip->rects[j].right);
651                 rect.bottom = min(rect.bottom, clip->rects[j].bottom);
652
653                 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->brush_and, pdev->brush_xor);
654             }
655         }
656     }
657     release_wine_region(pdev->clip);
658     return TRUE;
659 }
660
661 void update_brush_rop( dibdrv_physdev *pdev, INT rop )
662 {
663     if(pdev->brush_style == BS_SOLID)
664         calc_and_xor_masks(rop, pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
665 }
666
667 /***********************************************************************
668  *           dibdrv_SelectBrush
669  */
670 HBRUSH CDECL dibdrv_SelectBrush( PHYSDEV dev, HBRUSH hbrush )
671 {
672     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBrush );
673     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
674     LOGBRUSH logbrush;
675
676     TRACE("(%p, %p)\n", dev, hbrush);
677
678     if (!GetObjectW( hbrush, sizeof(logbrush), &logbrush )) return 0;
679
680     if (hbrush == GetStockObject( DC_BRUSH ))
681         logbrush.lbColor = GetDCBrushColor( dev->hdc );
682
683     pdev->brush_style = logbrush.lbStyle;
684
685     pdev->defer |= DEFER_BRUSH;
686
687     switch(logbrush.lbStyle)
688     {
689     case BS_SOLID:
690         pdev->brush_color = pdev->dib.funcs->colorref_to_pixel(&pdev->dib, logbrush.lbColor);
691         calc_and_xor_masks(GetROP2(dev->hdc), pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
692         pdev->brush_rects = solid_brush;
693         pdev->defer &= ~DEFER_BRUSH;
694         break;
695     default:
696         break;
697     }
698
699     return next->funcs->pSelectBrush( next, hbrush );
700 }
701
702 /***********************************************************************
703  *           dibdrv_SetDCBrushColor
704  */
705 COLORREF CDECL dibdrv_SetDCBrushColor( PHYSDEV dev, COLORREF color )
706 {
707     PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCBrushColor );
708     dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
709
710     if (GetCurrentObject(dev->hdc, OBJ_BRUSH) == GetStockObject( DC_BRUSH ))
711     {
712         pdev->brush_color = pdev->dib.funcs->colorref_to_pixel(&pdev->dib, color);
713         calc_and_xor_masks(GetROP2(dev->hdc), pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
714     }
715
716     return next->funcs->pSetDCBrushColor( next, color );
717 }