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
23 #include "gdi_private.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(dib);
32 * Decompose the 16 ROP2s into an expression of the form
36 * Where A and X depend only on P (and so can be precomputed).
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
47 * R2_NOTMASKPEN ~(P & D) P 1
48 * R2_MASKPEN P & D P 0
49 * R2_NOTXORPEN ~(P ^ D) 1 ~P
51 * R2_MERGENOTPEN ~P | D P ~P
53 * R2_MERGEPENNOT P | ~D ~P 1
54 * R2_MERGEPEN P | D ~P P
59 /* A = (P & A1) | (~P & A2) */
61 #define ONE {0xffffffff, 0xffffffff}
62 #define P {0xffffffff, 0}
63 #define NOT_P {0, 0xffffffff}
65 static const DWORD rop2_and_array[16][2] =
67 ZERO, NOT_P, NOT_P, ZERO,
70 ZERO, NOT_P, NOT_P, ZERO
73 /* X = (P & X1) | (~P & X2) */
74 static const DWORD rop2_xor_array[16][2] =
76 ZERO, NOT_P, ZERO, NOT_P,
78 ZERO, NOT_P, ZERO, NOT_P,
87 void calc_and_xor_masks(INT rop, DWORD color, DWORD *and, DWORD *xor)
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]);
94 static inline void order_end_points(int *s, int *e)
105 static inline BOOL pt_in_rect( const RECT *rect, const POINT *pt )
107 return ((pt->x >= rect->left) && (pt->x < rect->right) &&
108 (pt->y >= rect->top) && (pt->y < rect->bottom));
111 #define Y_INCREASING_MASK 0x0f
112 #define X_INCREASING_MASK 0xc3
113 #define X_MAJOR_MASK 0x99
114 #define POS_SLOPE_MASK 0x33
116 static inline BOOL is_xmajor(DWORD octant)
118 return octant & X_MAJOR_MASK;
121 static inline BOOL is_pos_slope(DWORD octant)
123 return octant & POS_SLOPE_MASK;
126 static inline BOOL is_x_increasing(DWORD octant)
128 return octant & X_INCREASING_MASK;
131 static inline BOOL is_y_increasing(DWORD octant)
133 return octant & Y_INCREASING_MASK;
136 /**********************************************************************
139 * Return the octant number starting clockwise from the +ve x-axis.
141 static inline int get_octant_number(int dx, int dy)
145 return ( dx > dy) ? 1 : 2;
147 return (-dx > dy) ? 4 : 3;
150 return (-dx > -dy) ? 5 : 6;
152 return ( dx > -dy) ? 8 : 7;
155 static inline DWORD get_octant_mask(int dx, int dy)
157 return 1 << (get_octant_number(dx, dy) - 1);
160 static void solid_pen_line_callback(dibdrv_physdev *pdev, INT x, INT y)
168 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
177 static inline DWORD calc_outcode(const POINT *pt, const RECT *clip)
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;
195 /******************************************************************************
198 * Clips the start and end points to a rectangle.
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.
205 * Returns 0 if totally excluded, 1 if partially clipped and 2 if unclipped.
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
211 * For x major lines we have 2dy >= err + bias > 2dy - 2dx
212 * 0 >= err + bias - 2dy > -2dx
214 * Note dx, dy, m and n are all +ve.
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)
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
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
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
251 * For y major lines, symmetry (dx <-> dy and swap the cases over) gives:
253 * Moving start point from y1 to y1 + n find x1 + m
254 * m = (2ndx + bias + dy - 1) / 2dy
256 * Moving start point from x1 to x1 + m find y1 + n
257 * n = (2mdy - bias - dy) / 2ndx + 1
259 * Moving end point from y2 to y2 - n find x1 - m
260 * m = (2ndx - bias + dy) / 2dy
262 * Moving end point from x2 to x2 - m find y2 - n
263 * n = (2mdy + bias - dy - 1) / 2dx + 1
265 static int clip_line(const POINT *start, const POINT *end, const RECT *clip,
266 const bres_params *params, POINT *pt1, POINT *pt2)
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);
282 start_oc = calc_outcode(start, clip);
283 end_oc = calc_outcode(end, clip);
287 if(start_oc == 0 && end_oc == 0) return clipped ? 1 : 2; /* trivial accept */
288 if(start_oc & end_oc) return 0; /* trivial reject */
291 if(start_oc & OUT_LEFT)
293 m = clip->left - start->x;
295 n = (m * two_dy + bias + dx - 1) / two_dx;
297 n = (m * two_dy - bias - dy) / two_dx + 1;
300 if(neg_slope) n = -n;
301 pt1->y = start->y + n;
302 start_oc = calc_outcode(pt1, clip);
304 else if(start_oc & OUT_RIGHT)
306 m = start->x - clip->right + 1;
308 n = (m * two_dy + bias + dx - 1) / two_dx;
310 n = (m * two_dy - bias - dy) / two_dx + 1;
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);
317 else if(start_oc & OUT_TOP)
319 n = clip->top - start->y;
321 m = (n * two_dx - bias - dx) / two_dy + 1;
323 m = (n * two_dx + bias + dy - 1) / two_dy;
326 if(neg_slope) m = -m;
327 pt1->x = start->x + m;
328 start_oc = calc_outcode(pt1, clip);
330 else if(start_oc & OUT_BOTTOM)
332 n = start->y - clip->bottom + 1;
334 m = (n * two_dx - bias - dx) / two_dy + 1;
336 m = (n * two_dx + bias + dy - 1) / two_dy;
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);
343 else if(end_oc & OUT_LEFT)
345 m = clip->left - end->x;
347 n = (m * two_dy - bias + dx) / two_dx;
349 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
352 if(neg_slope) n = -n;
354 end_oc = calc_outcode(pt2, clip);
356 else if(end_oc & OUT_RIGHT)
358 m = end->x - clip->right + 1;
360 n = (m * two_dy - bias + dx) / two_dx;
362 n = (m * two_dy + bias - dy - 1) / two_dx + 1;
364 pt2->x = clip->right - 1;
365 if(neg_slope) n = -n;
367 end_oc = calc_outcode(pt2, clip);
369 else if(end_oc & OUT_TOP)
371 n = clip->top - end->y;
373 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
375 m = (n * two_dx - bias + dy) / two_dy;
378 if(neg_slope) m = -m;
380 end_oc = calc_outcode(pt2, clip);
382 else if(end_oc & OUT_BOTTOM)
384 n = end->y - clip->bottom + 1;
386 m = (n * two_dx + bias - dx - 1) / two_dy + 1;
388 m = (n * two_dx - bias + dy) / two_dy;
390 pt2->y = clip->bottom - 1;
391 if(neg_slope) m = -m;
393 end_oc = calc_outcode(pt2, clip);
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)
401 const int xadd = is_x_increasing(params->octant) ? 1 : -1;
402 const int yadd = is_y_increasing(params->octant) ? 1 : -1;
405 if (is_xmajor(params->octant)) /* line is "more horizontal" */
407 erradd = 2*params->dy - 2*params->dx;
410 callback(pdev, x1, y1);
411 if (err + params->bias > 0)
416 else err += 2*params->dy;
419 if(last_pt) callback(pdev, x1, y1);
421 else /* line is "more vertical" */
423 erradd = 2*params->dx - 2*params->dy;
426 callback(pdev, x1, y1);
427 if (err + params->bias > 0)
432 else err += 2*params->dx;
435 if(last_pt) callback(pdev, x1, y1);
439 static BOOL solid_pen_line(dibdrv_physdev *pdev, POINT *start, POINT *end)
441 const WINEREGION *clip = get_wine_region(pdev->clip);
443 if(start->y == end->y)
448 rect.left = start->x;
451 rect.bottom = end->y + 1;
452 order_end_points(&rect.left, &rect.right);
453 for(i = 0; i < clip->numRects; i++)
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)
460 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
463 if(clip->rects[i].right > rect.left && clip->rects[i].left < rect.right)
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);
472 else if(start->x == end->x)
477 rect.left = start->x;
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++)
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)
488 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->pen_and, pdev->pen_xor);
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)
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);
505 INT dx = end->x - start->x;
506 INT dy = end->y - start->y;
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;
515 for(i = 0; i < clip->numRects; i++)
517 POINT clipped_start, clipped_end;
519 clip_status = clip_line(start, end, clip->rects + i, ¶ms, &clipped_start, &clipped_end);
523 int m = abs(clipped_start.x - start->x);
524 int n = abs(clipped_start.y - start->y);
526 BOOL last_pt = FALSE;
528 if(is_xmajor(params.octant))
529 err = 2 * params.dy - params.dx + m * 2 * params.dy - n * 2 * params.dx;
531 err = 2 * params.dx - params.dy + n * 2 * params.dx - m * 2 * params.dy;
533 if(clip_status == 1 && (end->x != clipped_end.x || end->y != clipped_end.y)) last_pt = TRUE;
535 bres_line_with_bias(clipped_start.x, clipped_start.y, clipped_end.x, clipped_end.y, ¶ms,
536 err, last_pt, solid_pen_line_callback, pdev);
538 if(clip_status == 2) break; /* completely unclipped, so we can finish */
543 release_wine_region(pdev->clip);
547 /***********************************************************************
550 HPEN CDECL dibdrv_SelectPen( PHYSDEV dev, HPEN hpen )
552 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectPen );
553 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
556 TRACE("(%p, %p)\n", dev, hpen);
558 if (!GetObjectW( hpen, sizeof(logpen), &logpen ))
560 /* must be an extended pen */
562 INT size = GetObjectW( hpen, 0, NULL );
566 elp = HeapAlloc( GetProcessHeap(), 0, size );
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;
575 HeapFree( GetProcessHeap(), 0, elp );
578 if (hpen == GetStockObject( DC_PEN ))
579 logpen.lopnColor = GetDCPenColor( dev->hdc );
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);
584 pdev->defer |= DEFER_PEN;
586 switch(logpen.lopnStyle & PS_STYLE_MASK)
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;
598 return next->funcs->pSelectPen( next, hpen );
601 /***********************************************************************
602 * dibdrv_SetDCPenColor
604 COLORREF CDECL dibdrv_SetDCPenColor( PHYSDEV dev, COLORREF color )
606 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCPenColor );
607 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
609 if (GetCurrentObject(dev->hdc, OBJ_PEN) == GetStockObject( DC_PEN ))
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);
615 return next->funcs->pSetDCPenColor( next, color );
618 /**********************************************************************
621 * Fill a number of rectangles with the solid brush
622 * FIXME: Should we insist l < r && t < b? Currently we assume this.
624 static BOOL solid_brush(dibdrv_physdev *pdev, int num, RECT *rects)
627 const WINEREGION *clip = get_wine_region(pdev->clip);
629 for(i = 0; i < num; i++)
631 for(j = 0; j < clip->numRects; j++)
633 RECT rect = rects[i];
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)
639 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->brush_and, pdev->brush_xor);
643 if(clip->rects[j].top >= rect.bottom) break;
644 if(clip->rects[j].bottom <= rect.top) continue;
646 if(clip->rects[j].right > rect.left && clip->rects[j].left < rect.right)
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);
653 pdev->dib.funcs->solid_rects(&pdev->dib, 1, &rect, pdev->brush_and, pdev->brush_xor);
657 release_wine_region(pdev->clip);
661 void update_brush_rop( dibdrv_physdev *pdev, INT rop )
663 if(pdev->brush_style == BS_SOLID)
664 calc_and_xor_masks(rop, pdev->brush_color, &pdev->brush_and, &pdev->brush_xor);
667 /***********************************************************************
670 HBRUSH CDECL dibdrv_SelectBrush( PHYSDEV dev, HBRUSH hbrush )
672 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSelectBrush );
673 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
676 TRACE("(%p, %p)\n", dev, hbrush);
678 if (!GetObjectW( hbrush, sizeof(logbrush), &logbrush )) return 0;
680 if (hbrush == GetStockObject( DC_BRUSH ))
681 logbrush.lbColor = GetDCBrushColor( dev->hdc );
683 pdev->brush_style = logbrush.lbStyle;
685 pdev->defer |= DEFER_BRUSH;
687 switch(logbrush.lbStyle)
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;
699 return next->funcs->pSelectBrush( next, hbrush );
702 /***********************************************************************
703 * dibdrv_SetDCBrushColor
705 COLORREF CDECL dibdrv_SetDCBrushColor( PHYSDEV dev, COLORREF color )
707 PHYSDEV next = GET_NEXT_PHYSDEV( dev, pSetDCBrushColor );
708 dibdrv_physdev *pdev = get_dibdrv_pdev(dev);
710 if (GetCurrentObject(dev->hdc, OBJ_BRUSH) == GetStockObject( DC_BRUSH ))
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);
716 return next->funcs->pSetDCBrushColor( next, color );