gdi32: Simplify the Bresenham line drawing.
[wine/multimedia.git] / dlls / gdi32 / dibdrv / objects.c
blob9fbb3c9ebff77cc293fadb816ed42ad476694acd
1 /*
2 * DIB driver GDI objects.
4 * Copyright 2011 Huw Davies
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.
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.
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
21 #include <assert.h>
22 #include <stdlib.h>
24 #include "gdi_private.h"
25 #include "dibdrv.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(dib);
33 * Decompose the 16 ROP2s into an expression of the form
35 * D = (D & A) ^ X
37 * Where A and X depend only on P (and so can be precomputed).
39 * A X
41 * R2_BLACK 0 0 0
42 * R2_NOTMERGEPEN ~(D | P) ~P ~P
43 * R2_MASKNOTPEN ~P & D ~P 0
44 * R2_NOTCOPYPEN ~P 0 ~P
45 * R2_MASKPENNOT P & ~D P P
46 * R2_NOT ~D 1 1
47 * R2_XORPEN P ^ D 1 P
48 * R2_NOTMASKPEN ~(P & D) P 1
49 * R2_MASKPEN P & D P 0
50 * R2_NOTXORPEN ~(P ^ D) 1 ~P
51 * R2_NOP D 1 0
52 * R2_MERGENOTPEN ~P | D P ~P
53 * R2_COPYPEN P 0 P
54 * R2_MERGEPENNOT P | ~D ~P 1
55 * R2_MERGEPEN P | D ~P P
56 * R2_WHITE 1 0 1
60 /* A = (P & A1) ^ A2 */
61 #define ZERO { 0u, 0u}
62 #define ONE { 0u, ~0u}
63 #define P {~0u, 0u}
64 #define NOT_P {~0u, ~0u}
66 static const DWORD rop2_and_array[16][2] =
68 ZERO, NOT_P, NOT_P, ZERO,
69 P, ONE, ONE, P,
70 P, ONE, ONE, P,
71 ZERO, NOT_P, NOT_P, ZERO
74 /* X = (P & X1) ^ X2 */
75 static const DWORD rop2_xor_array[16][2] =
77 ZERO, NOT_P, ZERO, NOT_P,
78 P, ONE, P, ONE,
79 ZERO, NOT_P, ZERO, NOT_P,
80 P, ONE, P, ONE
83 #undef NOT_P
84 #undef P
85 #undef ONE
86 #undef ZERO
88 void get_rop_codes(INT rop, struct rop_codes *codes)
90 /* NB The ROP2 codes start at one and the arrays are zero-based */
91 codes->a1 = rop2_and_array[rop-1][0];
92 codes->a2 = rop2_and_array[rop-1][1];
93 codes->x1 = rop2_xor_array[rop-1][0];
94 codes->x2 = rop2_xor_array[rop-1][1];
97 void calc_and_xor_masks(INT rop, DWORD color, DWORD *and, DWORD *xor)
99 struct rop_codes codes;
100 get_rop_codes( rop, &codes );
102 *and = (color & codes.a1) ^ codes.a2;
103 *xor = (color & codes.x1) ^ codes.x2;
106 static inline RGBQUAD rgbquad_from_colorref(COLORREF c)
108 RGBQUAD ret;
110 ret.rgbRed = GetRValue(c);
111 ret.rgbGreen = GetGValue(c);
112 ret.rgbBlue = GetBValue(c);
113 ret.rgbReserved = 0;
114 return ret;
117 static inline BOOL rgbquad_equal(const RGBQUAD *a, const RGBQUAD *b)
119 if(a->rgbRed == b->rgbRed &&
120 a->rgbGreen == b->rgbGreen &&
121 a->rgbBlue == b->rgbBlue)
122 return TRUE;
123 return FALSE;
126 static COLORREF make_rgb_colorref( dibdrv_physdev *pdev, COLORREF color,
127 BOOL *got_pixel, DWORD *pixel )
129 BYTE type = color >> 24;
130 WORD index = LOWORD( color );
131 HPALETTE pal = GetCurrentObject( pdev->dev.hdc, OBJ_PAL );
132 PALETTEENTRY pal_ent;
134 *pixel = 0;
135 *got_pixel = FALSE;
137 switch( type )
139 case 0: break;
141 case 0x10: /* DIBINDEX */
142 *got_pixel = TRUE;
143 *pixel = 0;
144 color = RGB(0, 0, 0);
146 if (pdev->dib.bit_count <= 8 && index < (1 << pdev->dib.bit_count))
148 *pixel = index;
149 if (index < pdev->dib.color_table_size)
150 color = RGB( pdev->dib.color_table[index].rgbRed,
151 pdev->dib.color_table[index].rgbGreen,
152 pdev->dib.color_table[index].rgbBlue );
154 break;
156 case 2: /* PALETTERGB */
157 color &= 0xffffff;
158 break;
160 case 1: /* PALETTEINDEX */
161 if (!GetPaletteEntries( pal, index, 1, &pal_ent ))
162 GetPaletteEntries( pal, 0, 1, &pal_ent );
163 color = RGB( pal_ent.peRed, pal_ent.peGreen, pal_ent.peBlue );
164 break;
166 default:
167 FIXME("Unhandled color type %08x\n", color);
168 color &= 0xffffff;
171 return color;
174 /******************************************************************
175 * get_pixel_color
177 * 1 bit bitmaps map the fg/bg colors as follows:
178 * If the fg colorref exactly matches one of the color table entries then
179 * that entry is the fg color and the other is the bg.
180 * Otherwise the bg color is mapped to the closest entry in the table and
181 * the fg takes the other one.
183 DWORD get_pixel_color( dibdrv_physdev *pdev, COLORREF color, BOOL mono_fixup )
185 RGBQUAD fg_quad;
186 BOOL got_pixel;
187 DWORD pixel;
188 COLORREF rgb_ref;
190 rgb_ref = make_rgb_colorref( pdev, color, &got_pixel, &pixel );
191 if (got_pixel) return pixel;
193 if (pdev->dib.bit_count != 1 || !mono_fixup)
194 return pdev->dib.funcs->colorref_to_pixel( &pdev->dib, rgb_ref );
196 fg_quad = rgbquad_from_colorref( rgb_ref );
197 if(rgbquad_equal(&fg_quad, pdev->dib.color_table))
198 return 0;
199 if(rgbquad_equal(&fg_quad, pdev->dib.color_table + 1))
200 return 1;
202 if(color == GetBkColor(pdev->dev.hdc)) return pdev->bkgnd_color;
203 else return pdev->bkgnd_color ? 0 : 1;
206 /***************************************************************************
207 * get_pen_bkgnd_masks
209 * Returns the pre-calculated bkgnd color masks unless the dib is 1 bpp.
210 * In this case since there are several fg sources (pen, brush, text)
211 * this makes pdev->bkgnd_color unusable. So here we take the inverse
212 * of the relevant fg color (which is always set up correctly).
214 static inline void get_pen_bkgnd_masks(const dibdrv_physdev *pdev, DWORD *and, DWORD *xor)
216 if(pdev->dib.bit_count != 1 || GetBkMode(pdev->dev.hdc) == TRANSPARENT)
218 *and = pdev->bkgnd_and;
219 *xor = pdev->bkgnd_xor;
221 else
223 DWORD color = ~pdev->pen_color;
224 if(pdev->pen_colorref == GetBkColor(pdev->dev.hdc)) color = pdev->pen_color;
225 calc_and_xor_masks( GetROP2(pdev->dev.hdc), color, and, xor );
229 static inline void get_brush_bkgnd_masks(const dibdrv_physdev *pdev, DWORD *and, DWORD *xor)
231 if(GetBkMode(pdev->dev.hdc) == TRANSPARENT)
233 *and = pdev->bkgnd_and;
234 *xor = pdev->bkgnd_xor;
236 else
238 DWORD color = pdev->bkgnd_color;
240 if(pdev->dib.bit_count == 1)
242 if(pdev->brush_colorref == GetBkColor(pdev->dev.hdc))
243 color = pdev->brush_color;
244 else
245 color = ~pdev->brush_color;
247 calc_and_xor_masks( pdev->brush_rop, color, and, xor );
251 static inline void order_end_points(int *s, int *e)
253 if(*s > *e)
255 int tmp;
256 tmp = *s + 1;
257 *s = *e + 1;
258 *e = tmp;
262 #define Y_INCREASING_MASK 0x0f
263 #define X_INCREASING_MASK 0xc3
264 #define X_MAJOR_MASK 0x99
265 #define POS_SLOPE_MASK 0x33
267 static inline BOOL is_xmajor(DWORD octant)
269 return octant & X_MAJOR_MASK;
272 static inline BOOL is_pos_slope(DWORD octant)
274 return octant & POS_SLOPE_MASK;
277 static inline BOOL is_x_increasing(DWORD octant)
279 return octant & X_INCREASING_MASK;
282 static inline BOOL is_y_increasing(DWORD octant)
284 return octant & Y_INCREASING_MASK;
287 /**********************************************************************
288 * get_octant_number
290 * Return the octant number starting clockwise from the +ve x-axis.
292 static inline int get_octant_number(int dx, int dy)
294 if(dy > 0)
295 if(dx > 0)
296 return ( dx > dy) ? 1 : 2;
297 else
298 return (-dx > dy) ? 4 : 3;
299 else
300 if(dx < 0)
301 return (-dx > -dy) ? 5 : 6;
302 else
303 return ( dx > -dy) ? 8 : 7;
306 static inline DWORD get_octant_mask(int dx, int dy)
308 return 1 << (get_octant_number(dx, dy) - 1);
311 static inline int get_bias( DWORD mask )
313 /* Octants 3, 5, 6 and 8 take a bias */
314 return (mask & 0xb4) ? 1 : 0;
317 static void solid_pen_line_callback(dibdrv_physdev *pdev, INT x, INT y)
319 RECT rect;
321 rect.left = x;
322 rect.right = x + 1;
323 rect.top = y;
324 rect.bottom = y + 1;
325 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
326 return;
329 #define OUT_LEFT 1
330 #define OUT_RIGHT 2
331 #define OUT_TOP 4
332 #define OUT_BOTTOM 8
334 static inline DWORD calc_outcode(const POINT *pt, const RECT *clip)
336 DWORD out = 0;
337 if(pt->x < clip->left) out |= OUT_LEFT;
338 else if(pt->x >= clip->right) out |= OUT_RIGHT;
339 if(pt->y < clip->top) out |= OUT_TOP;
340 else if(pt->y >= clip->bottom) out |= OUT_BOTTOM;
342 return out;
345 /******************************************************************************
346 * clip_line
348 * Clips the start and end points to a rectangle.
350 * Note, this treats the end point like the start point. If the
351 * caller doesn't want it displayed, it should exclude it. If the end
352 * point is clipped out, then the likelihood is that the new end point
353 * should be displayed.
355 * Returns 0 if totally excluded, 1 if partially clipped and 2 if unclipped.
357 * This derivation is based on the comments in X.org's xserver/mi/mizerclip.c,
358 * however the Bresenham error term is defined differently so the equations
359 * will also differ.
361 * For x major lines we have 2dy >= err + bias > 2dy - 2dx
362 * 0 >= err + bias - 2dy > -2dx
364 * Note dx, dy, m and n are all +ve.
366 * Moving the start pt from x1 to x1 + m, we need to figure out y1 + n.
367 * err = 2dy - dx + 2mdy - 2ndx
368 * 0 >= 2dy - dx + 2mdy - 2ndx + bias - 2dy > -2dx
369 * 0 >= 2mdy - 2ndx + bias - dx > -2dx
370 * which of course will give exactly one solution for n,
371 * so looking at the >= inequality
372 * n >= (2mdy + bias - dx) / 2dx
373 * n = ceiling((2mdy + bias - dx) / 2dx)
374 * = (2mdy + bias + dx - 1) / 2dx (assuming division truncation)
376 * Moving start pt from y1 to y1 + n we need to figure out x1 + m - there may be several
377 * solutions we pick the one that minimizes m (ie that first unlipped pt). As above:
378 * 0 >= 2mdy - 2ndx + bias - dx > -2dx
379 * 2mdy > 2ndx - bias - dx
380 * m > (2ndx - bias - dx) / 2dy
381 * m = floor((2ndx - bias - dx) / 2dy) + 1
382 * m = (2ndx - bias - dx) / 2dy + 1
384 * Moving end pt from x2 to x2 - m, we need to figure out y2 - n
385 * err = 2dy - dx + 2(dx - m)dy - 2(dy - n)dx
386 * = 2dy - dx - 2mdy + 2ndx
387 * 0 >= 2dy - dx - 2mdy + 2ndx + bias - 2dy > -2dx
388 * 0 >= 2ndx - 2mdy + bias - dx > -2dx
389 * again exactly one solution.
390 * 2ndx <= 2mdy - bias + dx
391 * n = floor((2mdy - bias + dx) / 2dx)
392 * = (2mdy - bias + dx) / 2dx
394 * Moving end pt from y2 to y2 - n when need x2 - m this time maximizing x2 - m so
395 * mininizing m to include all of the points at y = y2 - n. As above:
396 * 0 >= 2ndx - 2mdy + bias - dx > -2dx
397 * 2mdy >= 2ndx + bias - dx
398 * m = ceiling((2ndx + bias - dx) / 2dy)
399 * = (2ndx + bias - dx - 1) / 2dy + 1
401 * For y major lines, symmetry (dx <-> dy and swap the cases over) gives:
403 * Moving start point from y1 to y1 + n find x1 + m
404 * m = (2ndx + bias + dy - 1) / 2dy
406 * Moving start point from x1 to x1 + m find y1 + n
407 * n = (2mdy - bias - dy) / 2ndx + 1
409 * Moving end point from y2 to y2 - n find x1 - m
410 * m = (2ndx - bias + dy) / 2dy
412 * Moving end point from x2 to x2 - m find y2 - n
413 * n = (2mdy + bias - dy - 1) / 2dx + 1
415 int clip_line(const POINT *start, const POINT *end, const RECT *clip,
416 const bres_params *params, POINT *pt1, POINT *pt2)
418 int m, n;
419 BOOL clipped = FALSE;
420 DWORD start_oc, end_oc;
421 const int bias = params->bias;
422 const unsigned int dx = params->dx;
423 const unsigned int dy = params->dy;
424 const unsigned int two_dx = params->dx * 2;
425 const unsigned int two_dy = params->dy * 2;
426 const BOOL xmajor = is_xmajor(params->octant);
427 const BOOL neg_slope = !is_pos_slope(params->octant);
429 *pt1 = *start;
430 *pt2 = *end;
432 start_oc = calc_outcode(start, clip);
433 end_oc = calc_outcode(end, clip);
435 while(1)
437 if(start_oc == 0 && end_oc == 0) return clipped ? 1 : 2; /* trivial accept */
438 if(start_oc & end_oc) return 0; /* trivial reject */
440 clipped = TRUE;
441 if(start_oc & OUT_LEFT)
443 m = clip->left - start->x;
444 if(xmajor)
445 n = (m * two_dy + bias + dx - 1) / two_dx;
446 else
447 n = (m * two_dy - bias - dy) / two_dx + 1;
449 pt1->x = clip->left;
450 if(neg_slope) n = -n;
451 pt1->y = start->y + n;
452 start_oc = calc_outcode(pt1, clip);
454 else if(start_oc & OUT_RIGHT)
456 m = start->x - clip->right + 1;
457 if(xmajor)
458 n = (m * two_dy + bias + dx - 1) / two_dx;
459 else
460 n = (m * two_dy - bias - dy) / two_dx + 1;
462 pt1->x = clip->right - 1;
463 if(neg_slope) n = -n;
464 pt1->y = start->y - n;
465 start_oc = calc_outcode(pt1, clip);
467 else if(start_oc & OUT_TOP)
469 n = clip->top - start->y;
470 if(xmajor)
471 m = (n * two_dx - bias - dx) / two_dy + 1;
472 else
473 m = (n * two_dx + bias + dy - 1) / two_dy;
475 pt1->y = clip->top;
476 if(neg_slope) m = -m;
477 pt1->x = start->x + m;
478 start_oc = calc_outcode(pt1, clip);
480 else if(start_oc & OUT_BOTTOM)
482 n = start->y - clip->bottom + 1;
483 if(xmajor)
484 m = (n * two_dx - bias - dx) / two_dy + 1;
485 else
486 m = (n * two_dx + bias + dy - 1) / two_dy;
488 pt1->y = clip->bottom - 1;
489 if(neg_slope) m = -m;
490 pt1->x = start->x - m;
491 start_oc = calc_outcode(pt1, clip);
493 else if(end_oc & OUT_LEFT)
495 m = clip->left - end->x;
496 if(xmajor)
497 n = (m * two_dy - bias + dx) / two_dx;
498 else
499 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
501 pt2->x = clip->left;
502 if(neg_slope) n = -n;
503 pt2->y = end->y + n;
504 end_oc = calc_outcode(pt2, clip);
506 else if(end_oc & OUT_RIGHT)
508 m = end->x - clip->right + 1;
509 if(xmajor)
510 n = (m * two_dy - bias + dx) / two_dx;
511 else
512 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
514 pt2->x = clip->right - 1;
515 if(neg_slope) n = -n;
516 pt2->y = end->y - n;
517 end_oc = calc_outcode(pt2, clip);
519 else if(end_oc & OUT_TOP)
521 n = clip->top - end->y;
522 if(xmajor)
523 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
524 else
525 m = (n * two_dx - bias + dy) / two_dy;
527 pt2->y = clip->top;
528 if(neg_slope) m = -m;
529 pt2->x = end->x + m;
530 end_oc = calc_outcode(pt2, clip);
532 else if(end_oc & OUT_BOTTOM)
534 n = end->y - clip->bottom + 1;
535 if(xmajor)
536 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
537 else
538 m = (n * two_dx - bias + dy) / two_dy;
540 pt2->y = clip->bottom - 1;
541 if(neg_slope) m = -m;
542 pt2->x = end->x - m;
543 end_oc = calc_outcode(pt2, clip);
548 static void bres_line_with_bias(const POINT *start, const struct line_params *params,
549 void (* callback)(dibdrv_physdev*,INT,INT), dibdrv_physdev *pdev)
551 POINT pt = *start;
552 int len = params->length, err = params->err_start;
554 if (params->x_major)
556 while(len--)
558 callback(pdev, pt.x, pt.y);
559 if (err + params->bias > 0)
561 pt.y += params->y_inc;
562 err += params->err_add_1;
564 else err += params->err_add_2;
565 pt.x += params->x_inc;
568 else
570 while(len--)
572 callback(pdev, pt.x, pt.y);
573 if (err + params->bias > 0)
575 pt.x += params->x_inc;
576 err += params->err_add_1;
578 else err += params->err_add_2;
579 pt.y += params->y_inc;
584 static BOOL solid_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
586 const WINEREGION *clip = get_wine_region(pdev->clip);
588 if(start->y == end->y)
590 RECT rect;
591 int i;
593 rect.left = start->x;
594 rect.top = start->y;
595 rect.right = end->x;
596 rect.bottom = end->y + 1;
597 order_end_points(&rect.left, &rect.right);
598 for(i = 0; i < clip->numRects; i++)
600 if(clip->rects[i].top >= rect.bottom) break;
601 if(clip->rects[i].bottom <= rect.top) continue;
602 /* Optimize the unclipped case */
603 if(clip->rects[i].left <= rect.left && clip->rects[i].right >= rect.right)
605 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
606 break;
608 if(clip->rects[i].right > rect.left && clip->rects[i].left < rect.right)
610 RECT tmp = rect;
611 tmp.left = max(rect.left, clip->rects[i].left);
612 tmp.right = min(rect.right, clip->rects[i].right);
613 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &tmp, pdev->pen_and, pdev->pen_xor);
617 else if(start->x == end->x)
619 RECT rect;
620 int i;
622 rect.left = start->x;
623 rect.top = start->y;
624 rect.right = end->x + 1;
625 rect.bottom = end->y;
626 order_end_points(&rect.top, &rect.bottom);
627 for(i = 0; i < clip->numRects; i++)
629 /* Optimize unclipped case */
630 if(clip->rects[i].top <= rect.top && clip->rects[i].bottom >= rect.bottom &&
631 clip->rects[i].left <= rect.left && clip->rects[i].right >= rect.right)
633 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
634 break;
636 if(clip->rects[i].top >= rect.bottom) break;
637 if(clip->rects[i].bottom <= rect.top) continue;
638 if(clip->rects[i].right > rect.left && clip->rects[i].left < rect.right)
640 RECT tmp = rect;
641 tmp.top = max(rect.top, clip->rects[i].top);
642 tmp.bottom = min(rect.bottom, clip->rects[i].bottom);
643 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &tmp, pdev->pen_and, pdev->pen_xor);
647 else
649 bres_params clip_params;
650 struct line_params line_params;
651 INT dx = end->x - start->x, dy = end->y - start->y;
652 INT abs_dx = abs(dx), abs_dy = abs(dy);
653 INT i;
655 clip_params.dx = abs_dx;
656 clip_params.dy = abs_dy;
657 clip_params.octant = get_octant_mask(dx, dy);
658 clip_params.bias = get_bias( clip_params.octant );
660 line_params.bias = clip_params.bias;
661 line_params.x_major = is_xmajor( clip_params.octant );
662 line_params.x_inc = is_x_increasing( clip_params.octant ) ? 1 : -1;
663 line_params.y_inc = is_y_increasing( clip_params.octant ) ? 1 : -1;
665 if (line_params.x_major)
667 line_params.err_add_1 = 2 * abs_dy - 2 * abs_dx;
668 line_params.err_add_2 = 2 * abs_dy;
670 else
672 line_params.err_add_1 = 2 * abs_dx - 2 * abs_dy;
673 line_params.err_add_2 = 2 * abs_dx;
676 for(i = 0; i < clip->numRects; i++)
678 POINT clipped_start, clipped_end;
679 int clip_status;
680 clip_status = clip_line(start, end, clip->rects + i, &clip_params, &clipped_start, &clipped_end);
682 if(clip_status)
684 int m = abs(clipped_start.x - start->x);
685 int n = abs(clipped_start.y - start->y);
687 if (line_params.x_major)
689 line_params.err_start = 2 * abs_dy - abs_dx + m * 2 * abs_dy - n * 2 * abs_dx;
690 line_params.length = abs( clipped_end.x - clipped_start.x ) + 1;
692 else
694 line_params.err_start = 2 * abs_dx - abs_dy + n * 2 * abs_dx - m * 2 * abs_dy;
695 line_params.length = abs( clipped_end.y - clipped_start.y ) + 1;
698 if (clipped_end.x == end->x && clipped_end.y == end->y) line_params.length--;
700 bres_line_with_bias( &clipped_start, &line_params, solid_pen_line_callback, pdev );
702 if(clip_status == 2) break; /* completely unclipped, so we can finish */
707 release_wine_region(pdev->clip);
708 return TRUE;
711 static BOOL solid_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts)
713 int i;
715 assert( num >= 2 );
716 for (i = 0; i < num - 1; i++)
717 if (!solid_pen_line( pdev, pts + i, pts + i + 1 ))
718 return FALSE;
720 return TRUE;
723 void reset_dash_origin(dibdrv_physdev *pdev)
725 pdev->dash_pos.cur_dash = 0;
726 pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[0];
727 pdev->dash_pos.mark = TRUE;
730 static inline void skip_dash(dibdrv_physdev *pdev, unsigned int skip)
732 skip %= pdev->pen_pattern.total_len;
733 while(skip)
735 if(pdev->dash_pos.left_in_dash > skip)
737 pdev->dash_pos.left_in_dash -= skip;
738 return;
740 skip -= pdev->dash_pos.left_in_dash;
741 pdev->dash_pos.cur_dash++;
742 if(pdev->dash_pos.cur_dash == pdev->pen_pattern.count) pdev->dash_pos.cur_dash = 0;
743 pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[pdev->dash_pos.cur_dash];
744 pdev->dash_pos.mark = !pdev->dash_pos.mark;
748 static inline void get_dash_colors(const dibdrv_physdev *pdev, DWORD *and, DWORD *xor)
750 if(pdev->dash_pos.mark)
752 *and = pdev->pen_and;
753 *xor = pdev->pen_xor;
755 else /* space */
757 get_pen_bkgnd_masks( pdev, and, xor );
761 static void dashed_pen_line_callback(dibdrv_physdev *pdev, INT x, INT y)
763 RECT rect;
764 DWORD and, xor;
766 get_dash_colors(pdev, &and, &xor);
767 skip_dash(pdev, 1);
768 rect.left = x;
769 rect.right = x + 1;
770 rect.top = y;
771 rect.bottom = y + 1;
772 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
773 return;
776 static BOOL dashed_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
778 const WINEREGION *clip = get_wine_region(pdev->clip);
779 DWORD and, xor;
780 int i, dash_len;
781 RECT rect;
782 const dash_pos start_pos = pdev->dash_pos;
784 if(start->y == end->y) /* hline */
786 BOOL l_to_r;
787 INT left, right, cur_x;
789 rect.top = start->y;
790 rect.bottom = start->y + 1;
792 if(start->x <= end->x)
794 left = start->x;
795 right = end->x - 1;
796 l_to_r = TRUE;
798 else
800 left = end->x + 1;
801 right = start->x;
802 l_to_r = FALSE;
805 for(i = 0; i < clip->numRects; i++)
807 if(clip->rects[i].top > start->y) break;
808 if(clip->rects[i].bottom <= start->y) continue;
810 if(clip->rects[i].right > left && clip->rects[i].left <= right)
812 int clipped_left = max(clip->rects[i].left, left);
813 int clipped_right = min(clip->rects[i].right - 1, right);
815 pdev->dash_pos = start_pos;
817 if(l_to_r)
819 cur_x = clipped_left;
820 if(cur_x != left)
821 skip_dash(pdev, clipped_left - left);
823 while(cur_x <= clipped_right)
825 get_dash_colors(pdev, &and, &xor);
826 dash_len = pdev->dash_pos.left_in_dash;
827 if(cur_x + dash_len > clipped_right + 1)
828 dash_len = clipped_right - cur_x + 1;
829 rect.left = cur_x;
830 rect.right = cur_x + dash_len;
832 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
833 cur_x += dash_len;
834 skip_dash(pdev, dash_len);
837 else
839 cur_x = clipped_right;
840 if(cur_x != right)
841 skip_dash(pdev, right - clipped_right);
843 while(cur_x >= clipped_left)
845 get_dash_colors(pdev, &and, &xor);
846 dash_len = pdev->dash_pos.left_in_dash;
847 if(cur_x - dash_len < clipped_left - 1)
848 dash_len = cur_x - clipped_left + 1;
849 rect.left = cur_x - dash_len + 1;
850 rect.right = cur_x + 1;
852 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
853 cur_x -= dash_len;
854 skip_dash(pdev, dash_len);
859 pdev->dash_pos = start_pos;
860 skip_dash(pdev, right - left + 1);
862 else if(start->x == end->x) /* vline */
864 BOOL t_to_b;
865 INT top, bottom, cur_y;
867 rect.left = start->x;
868 rect.right = start->x + 1;
870 if(start->y <= end->y)
872 top = start->y;
873 bottom = end->y - 1;
874 t_to_b = TRUE;
876 else
878 top = end->y + 1;
879 bottom = start->y;
880 t_to_b = FALSE;
883 for(i = 0; i < clip->numRects; i++)
885 if(clip->rects[i].top > bottom) break;
886 if(clip->rects[i].bottom <= top) continue;
887 if(clip->rects[i].right > start->x && clip->rects[i].left <= start->x)
889 int clipped_top = max(clip->rects[i].top, top);
890 int clipped_bottom = min(clip->rects[i].bottom - 1, bottom);
892 pdev->dash_pos = start_pos;
894 if(t_to_b)
896 cur_y = clipped_top;
897 if(cur_y != top)
898 skip_dash(pdev, clipped_top - top);
900 while(cur_y <= clipped_bottom)
902 get_dash_colors(pdev, &and, &xor);
903 dash_len = pdev->dash_pos.left_in_dash;
904 if(cur_y + dash_len > clipped_bottom + 1)
905 dash_len = clipped_bottom - cur_y + 1;
906 rect.top = cur_y;
907 rect.bottom = cur_y + dash_len;
909 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
910 cur_y += dash_len;
911 skip_dash(pdev, dash_len);
914 else
916 cur_y = clipped_bottom;
917 if(cur_y != bottom)
918 skip_dash(pdev, bottom - clipped_bottom);
920 while(cur_y >= clipped_top)
922 get_dash_colors(pdev, &and, &xor);
923 dash_len = pdev->dash_pos.left_in_dash;
924 if(cur_y - dash_len < clipped_top - 1)
925 dash_len = cur_y - clipped_top + 1;
926 rect.top = cur_y - dash_len + 1;
927 rect.bottom = cur_y + 1;
929 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
930 cur_y -= dash_len;
931 skip_dash(pdev, dash_len);
936 pdev->dash_pos = start_pos;
937 skip_dash(pdev, bottom - top + 1);
939 else
941 bres_params clip_params;
942 struct line_params line_params;
943 INT dx = end->x - start->x, dy = end->y - start->y;
944 INT abs_dx = abs(dx), abs_dy = abs(dy);
945 INT i;
947 clip_params.dx = abs_dx;
948 clip_params.dy = abs_dy;
949 clip_params.octant = get_octant_mask(dx, dy);
950 clip_params.bias = get_bias( clip_params.octant );
952 line_params.bias = clip_params.bias;
953 line_params.x_major = is_xmajor( clip_params.octant );
954 line_params.x_inc = is_x_increasing( clip_params.octant ) ? 1 : -1;
955 line_params.y_inc = is_y_increasing( clip_params.octant ) ? 1 : -1;
957 if (line_params.x_major)
959 line_params.err_add_1 = 2 * abs_dy - 2 * abs_dx;
960 line_params.err_add_2 = 2 * abs_dy;
962 else
964 line_params.err_add_1 = 2 * abs_dx - 2 * abs_dy;
965 line_params.err_add_2 = 2 * abs_dx;
968 for(i = 0; i < clip->numRects; i++)
970 POINT clipped_start, clipped_end;
971 int clip_status;
972 clip_status = clip_line(start, end, clip->rects + i, &clip_params, &clipped_start, &clipped_end);
974 if(clip_status)
976 int m = abs(clipped_start.x - start->x);
977 int n = abs(clipped_start.y - start->y);
979 pdev->dash_pos = start_pos;
981 if (line_params.x_major)
983 line_params.err_start = 2 * abs_dy - abs_dx + m * 2 * abs_dy - n * 2 * abs_dx;
984 line_params.length = abs( clipped_end.x - clipped_start.x ) + 1;
985 skip_dash(pdev, m);
987 else
989 line_params.err_start = 2 * abs_dx - abs_dy + n * 2 * abs_dx - m * 2 * abs_dy;
990 line_params.length = abs( clipped_end.y - clipped_start.y ) + 1;
991 skip_dash(pdev, n);
993 if (clipped_end.x == end->x && clipped_end.y == end->y) line_params.length--;
995 bres_line_with_bias( &clipped_start, &line_params, dashed_pen_line_callback, pdev );
997 if(clip_status == 2) break; /* completely unclipped, so we can finish */
1000 pdev->dash_pos = start_pos;
1001 if(line_params.x_major)
1002 skip_dash(pdev, abs_dx);
1003 else
1004 skip_dash(pdev, abs_dy);
1007 release_wine_region(pdev->clip);
1008 return TRUE;
1011 static BOOL dashed_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts)
1013 int i;
1015 assert( num >= 2 );
1016 for (i = 0; i < num - 1; i++)
1017 if (!dashed_pen_line( pdev, pts + i, pts + i + 1 ))
1018 return FALSE;
1020 return TRUE;
1023 static BOOL null_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts)
1025 return TRUE;
1028 static const dash_pattern dash_patterns[5] =
1030 {0, {0}, 0}, /* PS_SOLID - a pseudo-pattern used to initialise unpatterned pens. */
1031 {2, {18, 6}, 24}, /* PS_DASH */
1032 {2, {3, 3}, 6}, /* PS_DOT */
1033 {4, {9, 6, 3, 6}, 24}, /* PS_DASHDOT */
1034 {6, {9, 3, 3, 3, 3, 3}, 24} /* PS_DASHDOTDOT */
1037 /***********************************************************************
1038 * dibdrv_SelectPen
1040 HPEN dibdrv_SelectPen( PHYSDEV dev, HPEN hpen )
1042 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectPen );
1043 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1044 LOGPEN logpen;
1045 DWORD style;
1047 TRACE("(%p, %p)\n", dev, hpen);
1049 if (!GetObjectW( hpen, sizeof(logpen), &logpen ))
1051 /* must be an extended pen */
1052 EXTLOGPEN *elp;
1053 INT size = GetObjectW( hpen, 0, NULL );
1055 if (!size) return 0;
1057 elp = HeapAlloc( GetProcessHeap(), 0, size );
1059 GetObjectW( hpen, size, elp );
1060 /* FIXME: add support for user style pens */
1061 logpen.lopnStyle = elp->elpPenStyle;
1062 logpen.lopnWidth.x = elp->elpWidth;
1063 logpen.lopnWidth.y = 0;
1064 logpen.lopnColor = elp->elpColor;
1066 HeapFree( GetProcessHeap(), 0, elp );
1069 if (hpen == GetStockObject( DC_PEN ))
1070 logpen.lopnColor = GetDCPenColor( dev->hdc );
1072 pdev->pen_colorref = logpen.lopnColor;
1073 pdev->pen_color = get_pixel_color( pdev, pdev->pen_colorref, TRUE );
1074 calc_and_xor_masks(GetROP2(dev->hdc), pdev->pen_color, &pdev->pen_and, &pdev->pen_xor);
1076 pdev->pen_pattern = dash_patterns[PS_SOLID];
1078 pdev->defer |= DEFER_PEN;
1080 style = logpen.lopnStyle & PS_STYLE_MASK;
1082 switch(style)
1084 case PS_SOLID:
1085 if(logpen.lopnStyle & PS_GEOMETRIC) break;
1086 if(logpen.lopnWidth.x > 1) break;
1087 pdev->pen_lines = solid_pen_lines;
1088 pdev->defer &= ~DEFER_PEN;
1089 break;
1091 case PS_DASH:
1092 case PS_DOT:
1093 case PS_DASHDOT:
1094 case PS_DASHDOTDOT:
1095 if(logpen.lopnStyle & PS_GEOMETRIC) break;
1096 if(logpen.lopnWidth.x > 1) break;
1097 pdev->pen_lines = dashed_pen_lines;
1098 pdev->pen_pattern = dash_patterns[style];
1099 pdev->defer &= ~DEFER_PEN;
1100 break;
1102 case PS_NULL:
1103 pdev->pen_lines = null_pen_lines;
1104 pdev->defer &= ~DEFER_PEN;
1105 break;
1107 default:
1108 break;
1111 return next->funcs->pSelectPen( next, hpen );
1114 /***********************************************************************
1115 * dibdrv_SetDCPenColor
1117 COLORREF dibdrv_SetDCPenColor( PHYSDEV dev, COLORREF color )
1119 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCPenColor );
1120 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1122 if (GetCurrentObject(dev->hdc, OBJ_PEN) == GetStockObject( DC_PEN ))
1124 pdev->pen_colorref = color;
1125 pdev->pen_color = get_pixel_color( pdev, pdev->pen_colorref, TRUE );
1126 calc_and_xor_masks(GetROP2(dev->hdc), pdev->pen_color, &pdev->pen_and, &pdev->pen_xor);
1129 return next->funcs->pSetDCPenColor( next, color );
1132 void solid_rects( dib_info *dib, int num, const RECT *rects, const rop_mask *color, HRGN region )
1134 int i, j;
1135 const WINEREGION *clip;
1137 if (!region)
1139 dib->funcs->solid_rects( dib, num, rects, color->and, color->xor );
1140 return;
1143 clip = get_wine_region( region );
1145 for(i = 0; i < num; i++)
1147 for(j = 0; j < clip->numRects; j++)
1149 RECT clipped_rect;
1151 if (intersect_rect( &clipped_rect, rects + i, clip->rects + j ))
1152 dib->funcs->solid_rects( dib, 1, &clipped_rect, color->and, color->xor );
1155 release_wine_region( region );
1158 /**********************************************************************
1159 * solid_brush
1161 * Fill a number of rectangles with the solid brush
1163 static BOOL solid_brush(dibdrv_physdev *pdev, dib_info *dib, int num, const RECT *rects, HRGN region)
1165 rop_mask brush_color;
1167 brush_color.and = pdev->brush_and;
1168 brush_color.xor = pdev->brush_xor;
1170 solid_rects( dib, num, rects, &brush_color, region );
1171 return TRUE;
1174 static void free_pattern_brush_bits( dibdrv_physdev *pdev )
1176 HeapFree(GetProcessHeap(), 0, pdev->brush_and_bits);
1177 HeapFree(GetProcessHeap(), 0, pdev->brush_xor_bits);
1178 pdev->brush_and_bits = NULL;
1179 pdev->brush_xor_bits = NULL;
1182 void free_pattern_brush( dibdrv_physdev *pdev )
1184 free_pattern_brush_bits( pdev );
1185 free_dib_info( &pdev->brush_dib );
1188 static BOOL create_pattern_brush_bits(dibdrv_physdev *pdev)
1190 DWORD size = pdev->brush_dib.height * abs(pdev->brush_dib.stride);
1191 DWORD *brush_bits = pdev->brush_dib.bits.ptr;
1192 DWORD *and_bits, *xor_bits;
1194 assert(pdev->brush_and_bits == NULL);
1195 assert(pdev->brush_xor_bits == NULL);
1197 assert(pdev->brush_dib.stride > 0);
1199 and_bits = pdev->brush_and_bits = HeapAlloc(GetProcessHeap(), 0, size);
1200 xor_bits = pdev->brush_xor_bits = HeapAlloc(GetProcessHeap(), 0, size);
1202 if(!and_bits || !xor_bits)
1204 ERR("Failed to create pattern brush bits\n");
1205 free_pattern_brush_bits( pdev );
1206 return FALSE;
1209 while(size)
1211 calc_and_xor_masks(pdev->brush_rop, *brush_bits++, and_bits++, xor_bits++);
1212 size -= 4;
1215 return TRUE;
1218 static const DWORD hatches[6][8] =
1220 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
1221 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
1222 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_FDIAGONAL */
1223 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_BDIAGONAL */
1224 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
1225 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 } /* HS_DIAGCROSS */
1228 static BOOL create_hatch_brush_bits(dibdrv_physdev *pdev)
1230 dib_info hatch;
1231 rop_mask fg_mask, bg_mask;
1232 rop_mask_bits mask_bits;
1233 DWORD size;
1234 BOOL ret;
1236 assert(pdev->brush_and_bits == NULL);
1237 assert(pdev->brush_xor_bits == NULL);
1239 /* Just initialise brush_dib with the color / sizing info. We don't
1240 need the bits as we'll calculate the rop masks straight from
1241 the hatch patterns. */
1243 copy_dib_color_info(&pdev->brush_dib, &pdev->dib);
1244 pdev->brush_dib.width = 8;
1245 pdev->brush_dib.height = 8;
1246 pdev->brush_dib.stride = get_dib_stride( pdev->brush_dib.width, pdev->brush_dib.bit_count );
1248 size = pdev->brush_dib.height * pdev->brush_dib.stride;
1250 mask_bits.and = pdev->brush_and_bits = HeapAlloc(GetProcessHeap(), 0, size);
1251 mask_bits.xor = pdev->brush_xor_bits = HeapAlloc(GetProcessHeap(), 0, size);
1253 if(!mask_bits.and || !mask_bits.xor)
1255 ERR("Failed to create pattern brush bits\n");
1256 free_pattern_brush_bits( pdev );
1257 return FALSE;
1260 hatch.bit_count = 1;
1261 hatch.height = hatch.width = 8;
1262 hatch.stride = 4;
1263 hatch.bits.ptr = (void *) hatches[pdev->brush_hatch];
1264 hatch.bits.free = hatch.bits.param = NULL;
1265 hatch.bits.is_copy = FALSE;
1267 fg_mask.and = pdev->brush_and;
1268 fg_mask.xor = pdev->brush_xor;
1269 get_brush_bkgnd_masks( pdev, &bg_mask.and, &bg_mask.xor );
1271 ret = pdev->brush_dib.funcs->create_rop_masks( &pdev->brush_dib, &hatch, &fg_mask, &bg_mask, &mask_bits );
1272 if(!ret) free_pattern_brush_bits( pdev );
1274 return ret;
1277 static BOOL matching_pattern_format( dib_info *dib, dib_info *pattern )
1279 if (dib->bit_count != pattern->bit_count) return FALSE;
1280 if (dib->stride != pattern->stride) return FALSE;
1282 switch (dib->bit_count)
1284 case 1:
1285 case 4:
1286 case 8:
1287 if (dib->color_table_size != pattern->color_table_size) return FALSE;
1288 return !memcmp( dib->color_table, pattern->color_table, dib->color_table_size * sizeof(RGBQUAD) );
1289 case 16:
1290 case 32:
1291 return (dib->red_mask == pattern->red_mask &&
1292 dib->green_mask == pattern->green_mask &&
1293 dib->blue_mask == pattern->blue_mask);
1295 return TRUE;
1298 static void select_pattern_brush( dibdrv_physdev *pdev, dib_info *pattern )
1300 RECT rect;
1302 free_pattern_brush( pdev );
1303 copy_dib_color_info(&pdev->brush_dib, &pdev->dib);
1305 pdev->brush_dib.height = pattern->height;
1306 pdev->brush_dib.width = pattern->width;
1307 pdev->brush_dib.stride = get_dib_stride( pdev->brush_dib.width, pdev->brush_dib.bit_count );
1309 if (matching_pattern_format( &pdev->brush_dib, pattern ))
1311 pdev->brush_dib.bits.ptr = pattern->bits.ptr;
1312 pdev->brush_dib.bits.is_copy = FALSE;
1313 pdev->brush_dib.bits.free = NULL;
1314 return;
1317 pdev->brush_dib.bits.ptr = HeapAlloc( GetProcessHeap(), 0,
1318 pdev->brush_dib.height * pdev->brush_dib.stride );
1319 pdev->brush_dib.bits.is_copy = TRUE;
1320 pdev->brush_dib.bits.free = free_heap_bits;
1322 rect.left = rect.top = 0;
1323 rect.right = pattern->width;
1324 rect.bottom = pattern->height;
1326 pdev->brush_dib.funcs->convert_to(&pdev->brush_dib, pattern, &rect);
1329 /**********************************************************************
1330 * pattern_brush
1332 * Fill a number of rectangles with the pattern brush
1333 * FIXME: Should we insist l < r && t < b? Currently we assume this.
1335 static BOOL pattern_brush(dibdrv_physdev *pdev, dib_info *dib, int num, const RECT *rects, HRGN region)
1337 int i, j;
1338 const WINEREGION *clip;
1339 POINT origin;
1341 if(pdev->brush_and_bits == NULL)
1343 switch(pdev->brush_style)
1345 case BS_DIBPATTERN:
1346 if (pdev->brush_pattern_usage == DIB_PAL_COLORS)
1348 dib_info pattern;
1349 HPALETTE pal = GetCurrentObject( pdev->dev.hdc, OBJ_PAL );
1350 if (!init_dib_info_from_brush( &pattern, pdev->brush_pattern_info,
1351 pdev->brush_pattern_bits, DIB_PAL_COLORS, pal ))
1352 return FALSE;
1353 select_pattern_brush( pdev, &pattern );
1354 free_dib_info( &pattern );
1356 if(!create_pattern_brush_bits(pdev))
1357 return FALSE;
1358 break;
1360 case BS_HATCHED:
1361 if(!create_hatch_brush_bits(pdev))
1362 return FALSE;
1363 break;
1365 default:
1366 ERR("Unexpected brush style %d\n", pdev->brush_style);
1367 return FALSE;
1371 GetBrushOrgEx(pdev->dev.hdc, &origin);
1373 clip = get_wine_region( region );
1375 if (!clip)
1377 dib->funcs->pattern_rects( dib, num, rects, &origin, &pdev->brush_dib, pdev->brush_and_bits, pdev->brush_xor_bits );
1378 return TRUE;
1381 for(i = 0; i < num; i++)
1383 for(j = 0; j < clip->numRects; j++)
1385 RECT rect = rects[i];
1387 /* Optimize unclipped case */
1388 if(clip->rects[j].top <= rect.top && clip->rects[j].bottom >= rect.bottom &&
1389 clip->rects[j].left <= rect.left && clip->rects[j].right >= rect.right)
1391 dib->funcs->pattern_rects( dib, 1, &rect, &origin, &pdev->brush_dib, pdev->brush_and_bits, pdev->brush_xor_bits );
1392 break;
1395 if(clip->rects[j].top >= rect.bottom) break;
1396 if(clip->rects[j].bottom <= rect.top) continue;
1398 if(clip->rects[j].right > rect.left && clip->rects[j].left < rect.right)
1400 rect.left = max(rect.left, clip->rects[j].left);
1401 rect.top = max(rect.top, clip->rects[j].top);
1402 rect.right = min(rect.right, clip->rects[j].right);
1403 rect.bottom = min(rect.bottom, clip->rects[j].bottom);
1405 dib->funcs->pattern_rects( dib, 1, &rect, &origin, &pdev->brush_dib, pdev->brush_and_bits, pdev->brush_xor_bits );
1409 release_wine_region( region );
1411 /* we need to recompute the bits each time for DIB_PAL_COLORS */
1412 if (pdev->brush_style == BS_DIBPATTERN && pdev->brush_pattern_usage == DIB_PAL_COLORS)
1413 free_pattern_brush_bits( pdev );
1415 return TRUE;
1418 static BOOL null_brush(dibdrv_physdev *pdev, dib_info *dib, int num, const RECT *rects, HRGN region)
1420 return TRUE;
1423 void update_brush_rop( dibdrv_physdev *pdev, INT rop )
1425 pdev->brush_rop = rop;
1426 if(pdev->brush_style == BS_SOLID || pdev->brush_style == BS_HATCHED)
1427 calc_and_xor_masks(rop, pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
1428 free_pattern_brush_bits( pdev );
1431 /***********************************************************************
1432 * dibdrv_SelectBrush
1434 HBRUSH dibdrv_SelectBrush( PHYSDEV dev, HBRUSH hbrush, HBITMAP bitmap,
1435 const BITMAPINFO *info, void *bits, UINT usage )
1437 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBrush );
1438 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1439 LOGBRUSH logbrush;
1441 TRACE("(%p, %p)\n", dev, hbrush);
1443 if (bitmap || info) /* pattern brush */
1445 dib_info pattern;
1446 BOOL ret;
1448 if (!info)
1450 BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
1452 if (!bmp) return 0;
1453 ret = init_dib_info_from_bitmapobj( &pattern, bmp, 0 );
1454 GDI_ReleaseObj( bitmap );
1455 if (!ret) return 0;
1456 select_pattern_brush( pdev, &pattern );
1457 free_dib_info( &pattern );
1459 else if (usage != DIB_PAL_COLORS)
1461 if (!init_dib_info_from_brush( &pattern, info, bits, DIB_RGB_COLORS, 0 )) return 0;
1462 select_pattern_brush( pdev, &pattern );
1463 free_dib_info( &pattern );
1465 else
1467 /* brush is actually selected only when it's used */
1468 free_pattern_brush( pdev );
1471 pdev->brush_rects = pattern_brush;
1472 pdev->brush_style = BS_DIBPATTERN;
1473 pdev->brush_pattern_info = info;
1474 pdev->brush_pattern_bits = bits;
1475 pdev->brush_pattern_usage = usage;
1476 pdev->defer &= ~DEFER_BRUSH;
1478 return next->funcs->pSelectBrush( next, hbrush, bitmap, info, bits, usage );
1481 if (!GetObjectW( hbrush, sizeof(logbrush), &logbrush )) return 0;
1483 if (hbrush == GetStockObject( DC_BRUSH ))
1484 logbrush.lbColor = GetDCBrushColor( dev->hdc );
1486 pdev->brush_style = logbrush.lbStyle;
1488 pdev->defer |= DEFER_BRUSH;
1490 free_pattern_brush( pdev );
1492 switch(logbrush.lbStyle)
1494 case BS_SOLID:
1495 pdev->brush_colorref = logbrush.lbColor;
1496 pdev->brush_color = get_pixel_color( pdev, pdev->brush_colorref, TRUE );
1497 calc_and_xor_masks(GetROP2(dev->hdc), pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
1498 pdev->brush_rects = solid_brush;
1499 pdev->defer &= ~DEFER_BRUSH;
1500 break;
1502 case BS_NULL:
1503 pdev->brush_rects = null_brush;
1504 pdev->defer &= ~DEFER_BRUSH;
1505 break;
1507 case BS_HATCHED:
1508 if(logbrush.lbHatch > HS_DIAGCROSS) return 0;
1509 pdev->brush_hatch = logbrush.lbHatch;
1510 pdev->brush_colorref = logbrush.lbColor;
1511 pdev->brush_color = get_pixel_color( pdev, pdev->brush_colorref, TRUE );
1512 calc_and_xor_masks(GetROP2(dev->hdc), pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
1513 pdev->brush_rects = pattern_brush;
1514 pdev->defer &= ~DEFER_BRUSH;
1515 break;
1517 default:
1518 return 0;
1521 return next->funcs->pSelectBrush( next, hbrush, bitmap, info, bits, usage );
1524 /***********************************************************************
1525 * dibdrv_SetDCBrushColor
1527 COLORREF dibdrv_SetDCBrushColor( PHYSDEV dev, COLORREF color )
1529 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCBrushColor );
1530 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1532 if (GetCurrentObject(dev->hdc, OBJ_BRUSH) == GetStockObject( DC_BRUSH ))
1534 pdev->brush_colorref = color;
1535 pdev->brush_color = get_pixel_color( pdev, pdev->brush_colorref, TRUE );
1536 calc_and_xor_masks(GetROP2(dev->hdc), pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
1539 return next->funcs->pSetDCBrushColor( next, color );
1542 BOOL brush_rects(dibdrv_physdev *pdev, int num, const RECT *rects)
1544 return pdev->brush_rects( pdev, &pdev->dib, num, rects, pdev->clip );