gdi32: Avoid copying the brush pattern if the format matches the DC.
[wine/multimedia.git] / dlls / gdi32 / dibdrv / objects.c
blobddd356370de68c2fc80f37001642992a8afb6962
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 void solid_pen_line_callback(dibdrv_physdev *pdev, INT x, INT y)
313 RECT rect;
315 rect.left = x;
316 rect.right = x + 1;
317 rect.top = y;
318 rect.bottom = y + 1;
319 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
320 return;
323 #define OUT_LEFT 1
324 #define OUT_RIGHT 2
325 #define OUT_TOP 4
326 #define OUT_BOTTOM 8
328 static inline DWORD calc_outcode(const POINT *pt, const RECT *clip)
330 DWORD out = 0;
331 if(pt->x < clip->left) out |= OUT_LEFT;
332 else if(pt->x >= clip->right) out |= OUT_RIGHT;
333 if(pt->y < clip->top) out |= OUT_TOP;
334 else if(pt->y >= clip->bottom) out |= OUT_BOTTOM;
336 return out;
339 /******************************************************************************
340 * clip_line
342 * Clips the start and end points to a rectangle.
344 * Note, this treats the end point like the start point. If the
345 * caller doesn't want it displayed, it should exclude it. If the end
346 * point is clipped out, then the likelihood is that the new end point
347 * should be displayed.
349 * Returns 0 if totally excluded, 1 if partially clipped and 2 if unclipped.
351 * This derivation is based on the comments in X.org's xserver/mi/mizerclip.c,
352 * however the Bresenham error term is defined differently so the equations
353 * will also differ.
355 * For x major lines we have 2dy >= err + bias > 2dy - 2dx
356 * 0 >= err + bias - 2dy > -2dx
358 * Note dx, dy, m and n are all +ve.
360 * Moving the start pt from x1 to x1 + m, we need to figure out y1 + n.
361 * err = 2dy - dx + 2mdy - 2ndx
362 * 0 >= 2dy - dx + 2mdy - 2ndx + bias - 2dy > -2dx
363 * 0 >= 2mdy - 2ndx + bias - dx > -2dx
364 * which of course will give exactly one solution for n,
365 * so looking at the >= inequality
366 * n >= (2mdy + bias - dx) / 2dx
367 * n = ceiling((2mdy + bias - dx) / 2dx)
368 * = (2mdy + bias + dx - 1) / 2dx (assuming division truncation)
370 * Moving start pt from y1 to y1 + n we need to figure out x1 + m - there may be several
371 * solutions we pick the one that minimizes m (ie that first unlipped pt). As above:
372 * 0 >= 2mdy - 2ndx + bias - dx > -2dx
373 * 2mdy > 2ndx - bias - dx
374 * m > (2ndx - bias - dx) / 2dy
375 * m = floor((2ndx - bias - dx) / 2dy) + 1
376 * m = (2ndx - bias - dx) / 2dy + 1
378 * Moving end pt from x2 to x2 - m, we need to figure out y2 - n
379 * err = 2dy - dx + 2(dx - m)dy - 2(dy - n)dx
380 * = 2dy - dx - 2mdy + 2ndx
381 * 0 >= 2dy - dx - 2mdy + 2ndx + bias - 2dy > -2dx
382 * 0 >= 2ndx - 2mdy + bias - dx > -2dx
383 * again exactly one solution.
384 * 2ndx <= 2mdy - bias + dx
385 * n = floor((2mdy - bias + dx) / 2dx)
386 * = (2mdy - bias + dx) / 2dx
388 * Moving end pt from y2 to y2 - n when need x2 - m this time maximizing x2 - m so
389 * mininizing m to include all of the points at y = y2 - n. As above:
390 * 0 >= 2ndx - 2mdy + bias - dx > -2dx
391 * 2mdy >= 2ndx + bias - dx
392 * m = ceiling((2ndx + bias - dx) / 2dy)
393 * = (2ndx + bias - dx - 1) / 2dy + 1
395 * For y major lines, symmetry (dx <-> dy and swap the cases over) gives:
397 * Moving start point from y1 to y1 + n find x1 + m
398 * m = (2ndx + bias + dy - 1) / 2dy
400 * Moving start point from x1 to x1 + m find y1 + n
401 * n = (2mdy - bias - dy) / 2ndx + 1
403 * Moving end point from y2 to y2 - n find x1 - m
404 * m = (2ndx - bias + dy) / 2dy
406 * Moving end point from x2 to x2 - m find y2 - n
407 * n = (2mdy + bias - dy - 1) / 2dx + 1
409 int clip_line(const POINT *start, const POINT *end, const RECT *clip,
410 const bres_params *params, POINT *pt1, POINT *pt2)
412 int m, n;
413 BOOL clipped = FALSE;
414 DWORD start_oc, end_oc;
415 const int bias = params->bias;
416 const unsigned int dx = params->dx;
417 const unsigned int dy = params->dy;
418 const unsigned int two_dx = params->dx * 2;
419 const unsigned int two_dy = params->dy * 2;
420 const BOOL xmajor = is_xmajor(params->octant);
421 const BOOL neg_slope = !is_pos_slope(params->octant);
423 *pt1 = *start;
424 *pt2 = *end;
426 start_oc = calc_outcode(start, clip);
427 end_oc = calc_outcode(end, clip);
429 while(1)
431 if(start_oc == 0 && end_oc == 0) return clipped ? 1 : 2; /* trivial accept */
432 if(start_oc & end_oc) return 0; /* trivial reject */
434 clipped = TRUE;
435 if(start_oc & OUT_LEFT)
437 m = clip->left - start->x;
438 if(xmajor)
439 n = (m * two_dy + bias + dx - 1) / two_dx;
440 else
441 n = (m * two_dy - bias - dy) / two_dx + 1;
443 pt1->x = clip->left;
444 if(neg_slope) n = -n;
445 pt1->y = start->y + n;
446 start_oc = calc_outcode(pt1, clip);
448 else if(start_oc & OUT_RIGHT)
450 m = start->x - clip->right + 1;
451 if(xmajor)
452 n = (m * two_dy + bias + dx - 1) / two_dx;
453 else
454 n = (m * two_dy - bias - dy) / two_dx + 1;
456 pt1->x = clip->right - 1;
457 if(neg_slope) n = -n;
458 pt1->y = start->y - n;
459 start_oc = calc_outcode(pt1, clip);
461 else if(start_oc & OUT_TOP)
463 n = clip->top - start->y;
464 if(xmajor)
465 m = (n * two_dx - bias - dx) / two_dy + 1;
466 else
467 m = (n * two_dx + bias + dy - 1) / two_dy;
469 pt1->y = clip->top;
470 if(neg_slope) m = -m;
471 pt1->x = start->x + m;
472 start_oc = calc_outcode(pt1, clip);
474 else if(start_oc & OUT_BOTTOM)
476 n = start->y - clip->bottom + 1;
477 if(xmajor)
478 m = (n * two_dx - bias - dx) / two_dy + 1;
479 else
480 m = (n * two_dx + bias + dy - 1) / two_dy;
482 pt1->y = clip->bottom - 1;
483 if(neg_slope) m = -m;
484 pt1->x = start->x - m;
485 start_oc = calc_outcode(pt1, clip);
487 else if(end_oc & OUT_LEFT)
489 m = clip->left - end->x;
490 if(xmajor)
491 n = (m * two_dy - bias + dx) / two_dx;
492 else
493 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
495 pt2->x = clip->left;
496 if(neg_slope) n = -n;
497 pt2->y = end->y + n;
498 end_oc = calc_outcode(pt2, clip);
500 else if(end_oc & OUT_RIGHT)
502 m = end->x - clip->right + 1;
503 if(xmajor)
504 n = (m * two_dy - bias + dx) / two_dx;
505 else
506 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
508 pt2->x = clip->right - 1;
509 if(neg_slope) n = -n;
510 pt2->y = end->y - n;
511 end_oc = calc_outcode(pt2, clip);
513 else if(end_oc & OUT_TOP)
515 n = clip->top - end->y;
516 if(xmajor)
517 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
518 else
519 m = (n * two_dx - bias + dy) / two_dy;
521 pt2->y = clip->top;
522 if(neg_slope) m = -m;
523 pt2->x = end->x + m;
524 end_oc = calc_outcode(pt2, clip);
526 else if(end_oc & OUT_BOTTOM)
528 n = end->y - clip->bottom + 1;
529 if(xmajor)
530 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
531 else
532 m = (n * two_dx - bias + dy) / two_dy;
534 pt2->y = clip->bottom - 1;
535 if(neg_slope) m = -m;
536 pt2->x = end->x - m;
537 end_oc = calc_outcode(pt2, clip);
542 static void bres_line_with_bias(INT x1, INT y1, INT x2, INT y2, const bres_params *params, INT err,
543 BOOL last_pt, void (* callback)(dibdrv_physdev*,INT,INT), dibdrv_physdev *pdev)
545 const int xadd = is_x_increasing(params->octant) ? 1 : -1;
546 const int yadd = is_y_increasing(params->octant) ? 1 : -1;
547 INT erradd;
549 if (is_xmajor(params->octant)) /* line is "more horizontal" */
551 erradd = 2*params->dy - 2*params->dx;
552 while(x1 != x2)
554 callback(pdev, x1, y1);
555 if (err + params->bias > 0)
557 y1 += yadd;
558 err += erradd;
560 else err += 2*params->dy;
561 x1 += xadd;
563 if(last_pt) callback(pdev, x1, y1);
565 else /* line is "more vertical" */
567 erradd = 2*params->dx - 2*params->dy;
568 while(y1 != y2)
570 callback(pdev, x1, y1);
571 if (err + params->bias > 0)
573 x1 += xadd;
574 err += erradd;
576 else err += 2*params->dx;
577 y1 += yadd;
579 if(last_pt) callback(pdev, x1, y1);
583 static BOOL solid_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
585 const WINEREGION *clip = get_wine_region(pdev->clip);
587 if(start->y == end->y)
589 RECT rect;
590 int i;
592 rect.left = start->x;
593 rect.top = start->y;
594 rect.right = end->x;
595 rect.bottom = end->y + 1;
596 order_end_points(&rect.left, &rect.right);
597 for(i = 0; i < clip->numRects; i++)
599 if(clip->rects[i].top >= rect.bottom) break;
600 if(clip->rects[i].bottom <= rect.top) continue;
601 /* Optimize the unclipped case */
602 if(clip->rects[i].left <= rect.left && clip->rects[i].right >= rect.right)
604 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
605 break;
607 if(clip->rects[i].right > rect.left && clip->rects[i].left < rect.right)
609 RECT tmp = rect;
610 tmp.left = max(rect.left, clip->rects[i].left);
611 tmp.right = min(rect.right, clip->rects[i].right);
612 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &tmp, pdev->pen_and, pdev->pen_xor);
616 else if(start->x == end->x)
618 RECT rect;
619 int i;
621 rect.left = start->x;
622 rect.top = start->y;
623 rect.right = end->x + 1;
624 rect.bottom = end->y;
625 order_end_points(&rect.top, &rect.bottom);
626 for(i = 0; i < clip->numRects; i++)
628 /* Optimize unclipped case */
629 if(clip->rects[i].top <= rect.top && clip->rects[i].bottom >= rect.bottom &&
630 clip->rects[i].left <= rect.left && clip->rects[i].right >= rect.right)
632 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
633 break;
635 if(clip->rects[i].top >= rect.bottom) break;
636 if(clip->rects[i].bottom <= rect.top) continue;
637 if(clip->rects[i].right > rect.left && clip->rects[i].left < rect.right)
639 RECT tmp = rect;
640 tmp.top = max(rect.top, clip->rects[i].top);
641 tmp.bottom = min(rect.bottom, clip->rects[i].bottom);
642 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &tmp, pdev->pen_and, pdev->pen_xor);
646 else
648 bres_params params;
649 INT dx = end->x - start->x;
650 INT dy = end->y - start->y;
651 INT i;
653 params.dx = abs(dx);
654 params.dy = abs(dy);
655 params.octant = get_octant_mask(dx, dy);
656 /* Octants 3, 5, 6 and 8 take a bias */
657 params.bias = (params.octant & 0xb4) ? 1 : 0;
659 for(i = 0; i < clip->numRects; i++)
661 POINT clipped_start, clipped_end;
662 int clip_status;
663 clip_status = clip_line(start, end, clip->rects + i, &params, &clipped_start, &clipped_end);
665 if(clip_status)
667 int m = abs(clipped_start.x - start->x);
668 int n = abs(clipped_start.y - start->y);
669 int err;
670 BOOL last_pt = FALSE;
672 if(is_xmajor(params.octant))
673 err = 2 * params.dy - params.dx + m * 2 * params.dy - n * 2 * params.dx;
674 else
675 err = 2 * params.dx - params.dy + n * 2 * params.dx - m * 2 * params.dy;
677 if(clip_status == 1 && (end->x != clipped_end.x || end->y != clipped_end.y)) last_pt = TRUE;
679 bres_line_with_bias(clipped_start.x, clipped_start.y, clipped_end.x, clipped_end.y, &params,
680 err, last_pt, solid_pen_line_callback, pdev);
682 if(clip_status == 2) break; /* completely unclipped, so we can finish */
687 release_wine_region(pdev->clip);
688 return TRUE;
691 static BOOL solid_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts)
693 int i;
695 assert( num >= 2 );
696 for (i = 0; i < num - 1; i++)
697 if (!solid_pen_line( pdev, pts + i, pts + i + 1 ))
698 return FALSE;
700 return TRUE;
703 void reset_dash_origin(dibdrv_physdev *pdev)
705 pdev->dash_pos.cur_dash = 0;
706 pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[0];
707 pdev->dash_pos.mark = TRUE;
710 static inline void skip_dash(dibdrv_physdev *pdev, unsigned int skip)
712 skip %= pdev->pen_pattern.total_len;
713 while(skip)
715 if(pdev->dash_pos.left_in_dash > skip)
717 pdev->dash_pos.left_in_dash -= skip;
718 return;
720 skip -= pdev->dash_pos.left_in_dash;
721 pdev->dash_pos.cur_dash++;
722 if(pdev->dash_pos.cur_dash == pdev->pen_pattern.count) pdev->dash_pos.cur_dash = 0;
723 pdev->dash_pos.left_in_dash = pdev->pen_pattern.dashes[pdev->dash_pos.cur_dash];
724 pdev->dash_pos.mark = !pdev->dash_pos.mark;
728 static inline void get_dash_colors(const dibdrv_physdev *pdev, DWORD *and, DWORD *xor)
730 if(pdev->dash_pos.mark)
732 *and = pdev->pen_and;
733 *xor = pdev->pen_xor;
735 else /* space */
737 get_pen_bkgnd_masks( pdev, and, xor );
741 static void dashed_pen_line_callback(dibdrv_physdev *pdev, INT x, INT y)
743 RECT rect;
744 DWORD and, xor;
746 get_dash_colors(pdev, &and, &xor);
747 skip_dash(pdev, 1);
748 rect.left = x;
749 rect.right = x + 1;
750 rect.top = y;
751 rect.bottom = y + 1;
752 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
753 return;
756 static BOOL dashed_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
758 const WINEREGION *clip = get_wine_region(pdev->clip);
759 DWORD and, xor;
760 int i, dash_len;
761 RECT rect;
762 const dash_pos start_pos = pdev->dash_pos;
764 if(start->y == end->y) /* hline */
766 BOOL l_to_r;
767 INT left, right, cur_x;
769 rect.top = start->y;
770 rect.bottom = start->y + 1;
772 if(start->x <= end->x)
774 left = start->x;
775 right = end->x - 1;
776 l_to_r = TRUE;
778 else
780 left = end->x + 1;
781 right = start->x;
782 l_to_r = FALSE;
785 for(i = 0; i < clip->numRects; i++)
787 if(clip->rects[i].top > start->y) break;
788 if(clip->rects[i].bottom <= start->y) continue;
790 if(clip->rects[i].right > left && clip->rects[i].left <= right)
792 int clipped_left = max(clip->rects[i].left, left);
793 int clipped_right = min(clip->rects[i].right - 1, right);
795 pdev->dash_pos = start_pos;
797 if(l_to_r)
799 cur_x = clipped_left;
800 if(cur_x != left)
801 skip_dash(pdev, clipped_left - left);
803 while(cur_x <= clipped_right)
805 get_dash_colors(pdev, &and, &xor);
806 dash_len = pdev->dash_pos.left_in_dash;
807 if(cur_x + dash_len > clipped_right + 1)
808 dash_len = clipped_right - cur_x + 1;
809 rect.left = cur_x;
810 rect.right = cur_x + dash_len;
812 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
813 cur_x += dash_len;
814 skip_dash(pdev, dash_len);
817 else
819 cur_x = clipped_right;
820 if(cur_x != right)
821 skip_dash(pdev, right - clipped_right);
823 while(cur_x >= clipped_left)
825 get_dash_colors(pdev, &and, &xor);
826 dash_len = pdev->dash_pos.left_in_dash;
827 if(cur_x - dash_len < clipped_left - 1)
828 dash_len = cur_x - clipped_left + 1;
829 rect.left = cur_x - dash_len + 1;
830 rect.right = cur_x + 1;
832 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
833 cur_x -= dash_len;
834 skip_dash(pdev, dash_len);
839 pdev->dash_pos = start_pos;
840 skip_dash(pdev, right - left + 1);
842 else if(start->x == end->x) /* vline */
844 BOOL t_to_b;
845 INT top, bottom, cur_y;
847 rect.left = start->x;
848 rect.right = start->x + 1;
850 if(start->y <= end->y)
852 top = start->y;
853 bottom = end->y - 1;
854 t_to_b = TRUE;
856 else
858 top = end->y + 1;
859 bottom = start->y;
860 t_to_b = FALSE;
863 for(i = 0; i < clip->numRects; i++)
865 if(clip->rects[i].top > bottom) break;
866 if(clip->rects[i].bottom <= top) continue;
867 if(clip->rects[i].right > start->x && clip->rects[i].left <= start->x)
869 int clipped_top = max(clip->rects[i].top, top);
870 int clipped_bottom = min(clip->rects[i].bottom - 1, bottom);
872 pdev->dash_pos = start_pos;
874 if(t_to_b)
876 cur_y = clipped_top;
877 if(cur_y != top)
878 skip_dash(pdev, clipped_top - top);
880 while(cur_y <= clipped_bottom)
882 get_dash_colors(pdev, &and, &xor);
883 dash_len = pdev->dash_pos.left_in_dash;
884 if(cur_y + dash_len > clipped_bottom + 1)
885 dash_len = clipped_bottom - cur_y + 1;
886 rect.top = cur_y;
887 rect.bottom = cur_y + dash_len;
889 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
890 cur_y += dash_len;
891 skip_dash(pdev, dash_len);
894 else
896 cur_y = clipped_bottom;
897 if(cur_y != bottom)
898 skip_dash(pdev, bottom - clipped_bottom);
900 while(cur_y >= clipped_top)
902 get_dash_colors(pdev, &and, &xor);
903 dash_len = pdev->dash_pos.left_in_dash;
904 if(cur_y - dash_len < clipped_top - 1)
905 dash_len = cur_y - clipped_top + 1;
906 rect.top = cur_y - dash_len + 1;
907 rect.bottom = cur_y + 1;
909 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, and, xor);
910 cur_y -= dash_len;
911 skip_dash(pdev, dash_len);
916 pdev->dash_pos = start_pos;
917 skip_dash(pdev, bottom - top + 1);
919 else
921 bres_params params;
922 INT dx = end->x - start->x;
923 INT dy = end->y - start->y;
924 INT i;
926 params.dx = abs(dx);
927 params.dy = abs(dy);
928 params.octant = get_octant_mask(dx, dy);
929 /* Octants 3, 5, 6 and 8 take a bias */
930 params.bias = (params.octant & 0xb4) ? 1 : 0;
932 for(i = 0; i < clip->numRects; i++)
934 POINT clipped_start, clipped_end;
935 int clip_status;
936 clip_status = clip_line(start, end, clip->rects + i, &params, &clipped_start, &clipped_end);
938 if(clip_status)
940 int m = abs(clipped_start.x - start->x);
941 int n = abs(clipped_start.y - start->y);
942 int err;
943 BOOL last_pt = FALSE;
945 pdev->dash_pos = start_pos;
947 if(is_xmajor(params.octant))
949 err = 2 * params.dy - params.dx + m * 2 * params.dy - n * 2 * params.dx;
950 skip_dash(pdev, m);
952 else
954 err = 2 * params.dx - params.dy + n * 2 * params.dx - m * 2 * params.dy;
955 skip_dash(pdev, n);
957 if(clip_status == 1 && (end->x != clipped_end.x || end->y != clipped_end.y)) last_pt = TRUE;
959 bres_line_with_bias(clipped_start.x, clipped_start.y, clipped_end.x, clipped_end.y, &params,
960 err, last_pt, dashed_pen_line_callback, pdev);
962 if(clip_status == 2) break; /* completely unclipped, so we can finish */
965 pdev->dash_pos = start_pos;
966 if(is_xmajor(params.octant))
967 skip_dash(pdev, params.dx);
968 else
969 skip_dash(pdev, params.dy);
972 release_wine_region(pdev->clip);
973 return TRUE;
976 static BOOL dashed_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts)
978 int i;
980 assert( num >= 2 );
981 for (i = 0; i < num - 1; i++)
982 if (!dashed_pen_line( pdev, pts + i, pts + i + 1 ))
983 return FALSE;
985 return TRUE;
988 static BOOL null_pen_lines(dibdrv_physdev *pdev, int num, POINT *pts)
990 return TRUE;
993 static const dash_pattern dash_patterns[5] =
995 {0, {0}, 0}, /* PS_SOLID - a pseudo-pattern used to initialise unpatterned pens. */
996 {2, {18, 6}, 24}, /* PS_DASH */
997 {2, {3, 3}, 6}, /* PS_DOT */
998 {4, {9, 6, 3, 6}, 24}, /* PS_DASHDOT */
999 {6, {9, 3, 3, 3, 3, 3}, 24} /* PS_DASHDOTDOT */
1002 /***********************************************************************
1003 * dibdrv_SelectPen
1005 HPEN dibdrv_SelectPen( PHYSDEV dev, HPEN hpen )
1007 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectPen );
1008 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1009 LOGPEN logpen;
1010 DWORD style;
1012 TRACE("(%p, %p)\n", dev, hpen);
1014 if (!GetObjectW( hpen, sizeof(logpen), &logpen ))
1016 /* must be an extended pen */
1017 EXTLOGPEN *elp;
1018 INT size = GetObjectW( hpen, 0, NULL );
1020 if (!size) return 0;
1022 elp = HeapAlloc( GetProcessHeap(), 0, size );
1024 GetObjectW( hpen, size, elp );
1025 /* FIXME: add support for user style pens */
1026 logpen.lopnStyle = elp->elpPenStyle;
1027 logpen.lopnWidth.x = elp->elpWidth;
1028 logpen.lopnWidth.y = 0;
1029 logpen.lopnColor = elp->elpColor;
1031 HeapFree( GetProcessHeap(), 0, elp );
1034 if (hpen == GetStockObject( DC_PEN ))
1035 logpen.lopnColor = GetDCPenColor( dev->hdc );
1037 pdev->pen_colorref = logpen.lopnColor;
1038 pdev->pen_color = get_pixel_color( pdev, pdev->pen_colorref, TRUE );
1039 calc_and_xor_masks(GetROP2(dev->hdc), pdev->pen_color, &pdev->pen_and, &pdev->pen_xor);
1041 pdev->pen_pattern = dash_patterns[PS_SOLID];
1043 pdev->defer |= DEFER_PEN;
1045 style = logpen.lopnStyle & PS_STYLE_MASK;
1047 switch(style)
1049 case PS_SOLID:
1050 if(logpen.lopnStyle & PS_GEOMETRIC) break;
1051 if(logpen.lopnWidth.x > 1) break;
1052 pdev->pen_lines = solid_pen_lines;
1053 pdev->defer &= ~DEFER_PEN;
1054 break;
1056 case PS_DASH:
1057 case PS_DOT:
1058 case PS_DASHDOT:
1059 case PS_DASHDOTDOT:
1060 if(logpen.lopnStyle & PS_GEOMETRIC) break;
1061 if(logpen.lopnWidth.x > 1) break;
1062 pdev->pen_lines = dashed_pen_lines;
1063 pdev->pen_pattern = dash_patterns[style];
1064 pdev->defer &= ~DEFER_PEN;
1065 break;
1067 case PS_NULL:
1068 pdev->pen_lines = null_pen_lines;
1069 pdev->defer &= ~DEFER_PEN;
1070 break;
1072 default:
1073 break;
1076 return next->funcs->pSelectPen( next, hpen );
1079 /***********************************************************************
1080 * dibdrv_SetDCPenColor
1082 COLORREF dibdrv_SetDCPenColor( PHYSDEV dev, COLORREF color )
1084 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCPenColor );
1085 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1087 if (GetCurrentObject(dev->hdc, OBJ_PEN) == GetStockObject( DC_PEN ))
1089 pdev->pen_colorref = color;
1090 pdev->pen_color = get_pixel_color( pdev, pdev->pen_colorref, TRUE );
1091 calc_and_xor_masks(GetROP2(dev->hdc), pdev->pen_color, &pdev->pen_and, &pdev->pen_xor);
1094 return next->funcs->pSetDCPenColor( next, color );
1097 /**********************************************************************
1098 * solid_brush
1100 * Fill a number of rectangles with the solid brush
1101 * FIXME: Should we insist l < r && t < b? Currently we assume this.
1103 static BOOL solid_brush(dibdrv_physdev *pdev, dib_info *dib, int num, const RECT *rects, HRGN region)
1105 int i, j;
1106 const WINEREGION *clip = get_wine_region( region );
1108 if (!clip)
1110 dib->funcs->solid_rects( dib, num, rects, pdev->brush_and, pdev->brush_xor );
1111 return TRUE;
1114 for(i = 0; i < num; i++)
1116 for(j = 0; j < clip->numRects; j++)
1118 RECT rect = rects[i];
1120 /* Optimize unclipped case */
1121 if(clip->rects[j].top <= rect.top && clip->rects[j].bottom >= rect.bottom &&
1122 clip->rects[j].left <= rect.left && clip->rects[j].right >= rect.right)
1124 dib->funcs->solid_rects( dib, 1, &rect, pdev->brush_and, pdev->brush_xor );
1125 break;
1128 if(clip->rects[j].top >= rect.bottom) break;
1129 if(clip->rects[j].bottom <= rect.top) continue;
1131 if(clip->rects[j].right > rect.left && clip->rects[j].left < rect.right)
1133 rect.left = max(rect.left, clip->rects[j].left);
1134 rect.top = max(rect.top, clip->rects[j].top);
1135 rect.right = min(rect.right, clip->rects[j].right);
1136 rect.bottom = min(rect.bottom, clip->rects[j].bottom);
1138 dib->funcs->solid_rects( dib, 1, &rect, pdev->brush_and, pdev->brush_xor );
1142 release_wine_region( region );
1143 return TRUE;
1146 static void free_pattern_brush_bits( dibdrv_physdev *pdev )
1148 HeapFree(GetProcessHeap(), 0, pdev->brush_and_bits);
1149 HeapFree(GetProcessHeap(), 0, pdev->brush_xor_bits);
1150 pdev->brush_and_bits = NULL;
1151 pdev->brush_xor_bits = NULL;
1154 void free_pattern_brush( dibdrv_physdev *pdev )
1156 free_pattern_brush_bits( pdev );
1157 free_dib_info( &pdev->brush_dib );
1160 static BOOL create_pattern_brush_bits(dibdrv_physdev *pdev)
1162 DWORD size = pdev->brush_dib.height * abs(pdev->brush_dib.stride);
1163 DWORD *brush_bits = pdev->brush_dib.bits.ptr;
1164 DWORD *and_bits, *xor_bits;
1166 assert(pdev->brush_and_bits == NULL);
1167 assert(pdev->brush_xor_bits == NULL);
1169 assert(pdev->brush_dib.stride > 0);
1171 and_bits = pdev->brush_and_bits = HeapAlloc(GetProcessHeap(), 0, size);
1172 xor_bits = pdev->brush_xor_bits = HeapAlloc(GetProcessHeap(), 0, size);
1174 if(!and_bits || !xor_bits)
1176 ERR("Failed to create pattern brush bits\n");
1177 free_pattern_brush_bits( pdev );
1178 return FALSE;
1181 while(size)
1183 calc_and_xor_masks(pdev->brush_rop, *brush_bits++, and_bits++, xor_bits++);
1184 size -= 4;
1187 return TRUE;
1190 static const DWORD hatches[6][8] =
1192 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
1193 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
1194 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_FDIAGONAL */
1195 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_BDIAGONAL */
1196 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
1197 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 } /* HS_DIAGCROSS */
1200 static BOOL create_hatch_brush_bits(dibdrv_physdev *pdev)
1202 dib_info hatch;
1203 rop_mask fg_mask, bg_mask;
1204 rop_mask_bits mask_bits;
1205 DWORD size;
1206 BOOL ret;
1208 assert(pdev->brush_and_bits == NULL);
1209 assert(pdev->brush_xor_bits == NULL);
1211 /* Just initialise brush_dib with the color / sizing info. We don't
1212 need the bits as we'll calculate the rop masks straight from
1213 the hatch patterns. */
1215 copy_dib_color_info(&pdev->brush_dib, &pdev->dib);
1216 pdev->brush_dib.width = 8;
1217 pdev->brush_dib.height = 8;
1218 pdev->brush_dib.stride = get_dib_stride( pdev->brush_dib.width, pdev->brush_dib.bit_count );
1220 size = pdev->brush_dib.height * pdev->brush_dib.stride;
1222 mask_bits.and = pdev->brush_and_bits = HeapAlloc(GetProcessHeap(), 0, size);
1223 mask_bits.xor = pdev->brush_xor_bits = HeapAlloc(GetProcessHeap(), 0, size);
1225 if(!mask_bits.and || !mask_bits.xor)
1227 ERR("Failed to create pattern brush bits\n");
1228 free_pattern_brush_bits( pdev );
1229 return FALSE;
1232 hatch.bit_count = 1;
1233 hatch.height = hatch.width = 8;
1234 hatch.stride = 4;
1235 hatch.bits.ptr = (void *) hatches[pdev->brush_hatch];
1236 hatch.bits.free = hatch.bits.param = NULL;
1237 hatch.bits.is_copy = FALSE;
1239 fg_mask.and = pdev->brush_and;
1240 fg_mask.xor = pdev->brush_xor;
1241 get_brush_bkgnd_masks( pdev, &bg_mask.and, &bg_mask.xor );
1243 ret = pdev->brush_dib.funcs->create_rop_masks( &pdev->brush_dib, &hatch, &fg_mask, &bg_mask, &mask_bits );
1244 if(!ret) free_pattern_brush_bits( pdev );
1246 return ret;
1249 static BOOL matching_pattern_format( dib_info *dib, dib_info *pattern )
1251 if (dib->bit_count != pattern->bit_count) return FALSE;
1252 if (dib->stride != pattern->stride) return FALSE;
1254 switch (dib->bit_count)
1256 case 1:
1257 case 4:
1258 case 8:
1259 if (dib->color_table_size != pattern->color_table_size) return FALSE;
1260 return !memcmp( dib->color_table, pattern->color_table, dib->color_table_size * sizeof(RGBQUAD) );
1261 case 16:
1262 case 32:
1263 return (dib->red_mask == pattern->red_mask &&
1264 dib->green_mask == pattern->green_mask &&
1265 dib->blue_mask == pattern->blue_mask);
1267 return TRUE;
1270 static void select_pattern_brush( dibdrv_physdev *pdev, dib_info *pattern )
1272 RECT rect;
1274 free_pattern_brush( pdev );
1275 copy_dib_color_info(&pdev->brush_dib, &pdev->dib);
1277 pdev->brush_dib.height = pattern->height;
1278 pdev->brush_dib.width = pattern->width;
1279 pdev->brush_dib.stride = get_dib_stride( pdev->brush_dib.width, pdev->brush_dib.bit_count );
1281 if (matching_pattern_format( &pdev->brush_dib, pattern ))
1283 pdev->brush_dib.bits.ptr = pattern->bits.ptr;
1284 pdev->brush_dib.bits.is_copy = FALSE;
1285 pdev->brush_dib.bits.free = NULL;
1286 return;
1289 pdev->brush_dib.bits.ptr = HeapAlloc( GetProcessHeap(), 0,
1290 pdev->brush_dib.height * pdev->brush_dib.stride );
1291 pdev->brush_dib.bits.is_copy = TRUE;
1292 pdev->brush_dib.bits.free = free_heap_bits;
1294 rect.left = rect.top = 0;
1295 rect.right = pattern->width;
1296 rect.bottom = pattern->height;
1298 pdev->brush_dib.funcs->convert_to(&pdev->brush_dib, pattern, &rect);
1301 /**********************************************************************
1302 * pattern_brush
1304 * Fill a number of rectangles with the pattern brush
1305 * FIXME: Should we insist l < r && t < b? Currently we assume this.
1307 static BOOL pattern_brush(dibdrv_physdev *pdev, dib_info *dib, int num, const RECT *rects, HRGN region)
1309 int i, j;
1310 const WINEREGION *clip;
1311 POINT origin;
1313 if(pdev->brush_and_bits == NULL)
1315 switch(pdev->brush_style)
1317 case BS_DIBPATTERN:
1318 if (pdev->brush_pattern_usage == DIB_PAL_COLORS)
1320 dib_info pattern;
1321 HPALETTE pal = GetCurrentObject( pdev->dev.hdc, OBJ_PAL );
1322 if (!init_dib_info_from_brush( &pattern, pdev->brush_pattern_info,
1323 pdev->brush_pattern_bits, DIB_PAL_COLORS, pal ))
1324 return FALSE;
1325 select_pattern_brush( pdev, &pattern );
1326 free_dib_info( &pattern );
1328 if(!create_pattern_brush_bits(pdev))
1329 return FALSE;
1330 break;
1332 case BS_HATCHED:
1333 if(!create_hatch_brush_bits(pdev))
1334 return FALSE;
1335 break;
1337 default:
1338 ERR("Unexpected brush style %d\n", pdev->brush_style);
1339 return FALSE;
1343 GetBrushOrgEx(pdev->dev.hdc, &origin);
1345 clip = get_wine_region( region );
1347 if (!clip)
1349 dib->funcs->pattern_rects( dib, num, rects, &origin, &pdev->brush_dib, pdev->brush_and_bits, pdev->brush_xor_bits );
1350 return TRUE;
1353 for(i = 0; i < num; i++)
1355 for(j = 0; j < clip->numRects; j++)
1357 RECT rect = rects[i];
1359 /* Optimize unclipped case */
1360 if(clip->rects[j].top <= rect.top && clip->rects[j].bottom >= rect.bottom &&
1361 clip->rects[j].left <= rect.left && clip->rects[j].right >= rect.right)
1363 dib->funcs->pattern_rects( dib, 1, &rect, &origin, &pdev->brush_dib, pdev->brush_and_bits, pdev->brush_xor_bits );
1364 break;
1367 if(clip->rects[j].top >= rect.bottom) break;
1368 if(clip->rects[j].bottom <= rect.top) continue;
1370 if(clip->rects[j].right > rect.left && clip->rects[j].left < rect.right)
1372 rect.left = max(rect.left, clip->rects[j].left);
1373 rect.top = max(rect.top, clip->rects[j].top);
1374 rect.right = min(rect.right, clip->rects[j].right);
1375 rect.bottom = min(rect.bottom, clip->rects[j].bottom);
1377 dib->funcs->pattern_rects( dib, 1, &rect, &origin, &pdev->brush_dib, pdev->brush_and_bits, pdev->brush_xor_bits );
1381 release_wine_region( region );
1383 /* we need to recompute the bits each time for DIB_PAL_COLORS */
1384 if (pdev->brush_style == BS_DIBPATTERN && pdev->brush_pattern_usage == DIB_PAL_COLORS)
1385 free_pattern_brush_bits( pdev );
1387 return TRUE;
1390 static BOOL null_brush(dibdrv_physdev *pdev, dib_info *dib, int num, const RECT *rects, HRGN region)
1392 return TRUE;
1395 void update_brush_rop( dibdrv_physdev *pdev, INT rop )
1397 pdev->brush_rop = rop;
1398 if(pdev->brush_style == BS_SOLID || pdev->brush_style == BS_HATCHED)
1399 calc_and_xor_masks(rop, pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
1400 free_pattern_brush_bits( pdev );
1403 /***********************************************************************
1404 * dibdrv_SelectBrush
1406 HBRUSH dibdrv_SelectBrush( PHYSDEV dev, HBRUSH hbrush, HBITMAP bitmap,
1407 const BITMAPINFO *info, void *bits, UINT usage )
1409 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBrush );
1410 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1411 LOGBRUSH logbrush;
1413 TRACE("(%p, %p)\n", dev, hbrush);
1415 if (bitmap || info) /* pattern brush */
1417 dib_info pattern;
1418 BOOL ret;
1420 if (!info)
1422 BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
1424 if (!bmp) return 0;
1425 ret = init_dib_info_from_bitmapobj( &pattern, bmp, 0 );
1426 GDI_ReleaseObj( bitmap );
1427 if (!ret) return 0;
1428 select_pattern_brush( pdev, &pattern );
1429 free_dib_info( &pattern );
1431 else if (usage != DIB_PAL_COLORS)
1433 if (!init_dib_info_from_brush( &pattern, info, bits, DIB_RGB_COLORS, 0 )) return 0;
1434 select_pattern_brush( pdev, &pattern );
1435 free_dib_info( &pattern );
1437 else
1439 /* brush is actually selected only when it's used */
1440 free_pattern_brush( pdev );
1443 pdev->brush_rects = pattern_brush;
1444 pdev->brush_style = BS_DIBPATTERN;
1445 pdev->brush_pattern_info = info;
1446 pdev->brush_pattern_bits = bits;
1447 pdev->brush_pattern_usage = usage;
1448 pdev->defer &= ~DEFER_BRUSH;
1450 return next->funcs->pSelectBrush( next, hbrush, bitmap, info, bits, usage );
1453 if (!GetObjectW( hbrush, sizeof(logbrush), &logbrush )) return 0;
1455 if (hbrush == GetStockObject( DC_BRUSH ))
1456 logbrush.lbColor = GetDCBrushColor( dev->hdc );
1458 pdev->brush_style = logbrush.lbStyle;
1460 pdev->defer |= DEFER_BRUSH;
1462 free_pattern_brush( pdev );
1464 switch(logbrush.lbStyle)
1466 case BS_SOLID:
1467 pdev->brush_colorref = logbrush.lbColor;
1468 pdev->brush_color = get_pixel_color( pdev, pdev->brush_colorref, TRUE );
1469 calc_and_xor_masks(GetROP2(dev->hdc), pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
1470 pdev->brush_rects = solid_brush;
1471 pdev->defer &= ~DEFER_BRUSH;
1472 break;
1474 case BS_NULL:
1475 pdev->brush_rects = null_brush;
1476 pdev->defer &= ~DEFER_BRUSH;
1477 break;
1479 case BS_HATCHED:
1480 if(logbrush.lbHatch > HS_DIAGCROSS) return 0;
1481 pdev->brush_hatch = logbrush.lbHatch;
1482 pdev->brush_colorref = logbrush.lbColor;
1483 pdev->brush_color = get_pixel_color( pdev, pdev->brush_colorref, TRUE );
1484 calc_and_xor_masks(GetROP2(dev->hdc), pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
1485 pdev->brush_rects = pattern_brush;
1486 pdev->defer &= ~DEFER_BRUSH;
1487 break;
1489 default:
1490 return 0;
1493 return next->funcs->pSelectBrush( next, hbrush, bitmap, info, bits, usage );
1496 /***********************************************************************
1497 * dibdrv_SetDCBrushColor
1499 COLORREF dibdrv_SetDCBrushColor( PHYSDEV dev, COLORREF color )
1501 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCBrushColor );
1502 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
1504 if (GetCurrentObject(dev->hdc, OBJ_BRUSH) == GetStockObject( DC_BRUSH ))
1506 pdev->brush_colorref = color;
1507 pdev->brush_color = get_pixel_color( pdev, pdev->brush_colorref, TRUE );
1508 calc_and_xor_masks(GetROP2(dev->hdc), pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
1511 return next->funcs->pSetDCBrushColor( next, color );
1514 BOOL brush_rects(dibdrv_physdev *pdev, int num, const RECT *rects)
1516 return pdev->brush_rects( pdev, &pdev->dib, num, rects, pdev->clip );