1 /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2 /* cairo - a vector graphics library with display and print output
4 * Copyright © 2012 Intel Corporation
6 * This library is free software; you can redistribute it and/or
7 * modify it either under the terms of the GNU Lesser General Public
8 * License version 2.1 as published by the Free Software Foundation
9 * (the "LGPL") or, at your option, under the terms of the Mozilla
10 * Public License Version 1.1 (the "MPL"). If you do not alter this
11 * notice, a recipient may use your version of this file under either
12 * the MPL or the LGPL.
14 * You should have received a copy of the LGPL along with this library
15 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17 * You should have received a copy of the MPL along with this library
18 * in the file COPYING-MPL-1.1
20 * The contents of this file are subject to the Mozilla Public License
21 * Version 1.1 (the "License"); you may not use this file except in
22 * compliance with the License. You may obtain a copy of the License at
23 * http://www.mozilla.org/MPL/
25 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27 * the specific language governing rights and limitations.
29 * The Original Code is the cairo graphics library.
31 * The Initial Developer of the Original Code is University of Southern
35 * Carl D. Worth <cworth@cworth.org>
36 * Behdad Esfahbod <behdad@behdad.org>
37 * Chris Wilson <chris@chris-wilson.co.uk>
38 * Karl Tomlinson <karlt+@karlt.net>, Mozilla Corporation
41 /* The original X drawing API was very restrictive in what it could handle,
42 * pixel-aligned fill/blits are all that map into Cairo's drawing model.
47 #include "cairo-win32-private.h"
49 #include "cairo-boxes-private.h"
50 #include "cairo-clip-inline.h"
51 #include "cairo-compositor-private.h"
52 #include "cairo-image-surface-private.h"
53 #include "cairo-pattern-private.h"
54 #include "cairo-region-private.h"
55 #include "cairo-surface-inline.h"
56 #include "cairo-surface-offset-private.h"
58 #if !defined(AC_SRC_OVER)
59 #define AC_SRC_OVER 0x00
64 BYTE SourceConstantAlpha;
70 /* for compatibility with VC++ 6 */
72 #define AC_SRC_ALPHA 0x01
75 #define PELS_72DPI ((LONG)(72. / 0.0254))
77 /* the low-level interface */
84 static cairo_bool_t fill_box (cairo_box_t *box, void *closure)
86 struct fill_box *fb = closure;
89 rect.left = _cairo_fixed_integer_part (box->p1.x);
90 rect.top = _cairo_fixed_integer_part (box->p1.y);
91 rect.right = _cairo_fixed_integer_part (box->p2.x);
92 rect.bottom = _cairo_fixed_integer_part (box->p2.y);
94 TRACE ((stderr, "%s\n", __FUNCTION__));
95 return FillRect (fb->dc, &rect, fb->brush);
99 cairo_rectangle_int_t limit;
104 cairo_rectangle_int_t limit;
108 cairo_win32_alpha_blend_func_t alpha_blend;
111 static cairo_bool_t copy_box (cairo_box_t *box, void *closure)
113 const struct copy_box *cb = closure;
114 int x = _cairo_fixed_integer_part (box->p1.x);
115 int y = _cairo_fixed_integer_part (box->p1.y);
116 int width = _cairo_fixed_integer_part (box->p2.x - box->p1.x);
117 int height = _cairo_fixed_integer_part (box->p2.y - box->p1.y);
119 TRACE ((stderr, "%s\n", __FUNCTION__));
120 return BitBlt (cb->dst, x, y, width, height,
121 cb->src, x + cb->tx, y + cb->ty,
125 static cairo_bool_t alpha_box (cairo_box_t *box, void *closure)
127 const struct copy_box *cb = closure;
128 int x = _cairo_fixed_integer_part (box->p1.x);
129 int y = _cairo_fixed_integer_part (box->p1.y);
130 int width = _cairo_fixed_integer_part (box->p2.x - box->p1.x);
131 int height = _cairo_fixed_integer_part (box->p2.y - box->p1.y);
133 TRACE ((stderr, "%s\n", __FUNCTION__));
134 return cb->alpha_blend (cb->dst, x, y, width, height,
135 cb->src, x + cb->tx, y + cb->ty, width, height,
140 cairo_rectangle_int_t limit;
147 static cairo_bool_t upload_box (cairo_box_t *box, void *closure)
149 const struct upload_box *cb = closure;
150 int x = _cairo_fixed_integer_part (box->p1.x);
151 int y = _cairo_fixed_integer_part (box->p1.y);
152 int width = _cairo_fixed_integer_part (box->p2.x - box->p1.x);
153 int height = _cairo_fixed_integer_part (box->p2.y - box->p1.y);
155 TRACE ((stderr, "%s\n", __FUNCTION__));
156 return StretchDIBits (cb->dst, x, y + height - 1, width, -height,
157 x + cb->tx, height - (y + cb->ty - 1),
160 DIB_RGB_COLORS, SRCCOPY);
163 /* the mid-level: converts boxes into drawing operations */
165 static COLORREF color_to_rgb(const cairo_color_t *c)
167 return RGB (c->red_short >> 8, c->green_short >> 8, c->blue_short >> 8);
170 static cairo_int_status_t
171 fill_boxes (cairo_win32_display_surface_t *dst,
172 const cairo_pattern_t *src,
173 cairo_boxes_t *boxes)
175 const cairo_color_t *color = &((cairo_solid_pattern_t *) src)->color;
176 cairo_status_t status = CAIRO_STATUS_SUCCESS;
179 TRACE ((stderr, "%s\n", __FUNCTION__));
181 fb.dc = dst->win32.dc;
182 fb.brush = CreateSolidBrush (color_to_rgb(color));
184 return _cairo_win32_print_gdi_error (__FUNCTION__);
186 if (! _cairo_boxes_for_each_box (boxes, fill_box, &fb))
187 status = CAIRO_INT_STATUS_UNSUPPORTED;
189 DeleteObject (fb.brush);
194 static cairo_bool_t source_contains_box (cairo_box_t *box, void *closure)
196 struct check_box *data = closure;
198 /* The box is pixel-aligned so the truncation is safe. */
200 _cairo_fixed_integer_part (box->p1.x) + data->tx >= data->limit.x &&
201 _cairo_fixed_integer_part (box->p1.y) + data->ty >= data->limit.y &&
202 _cairo_fixed_integer_part (box->p2.x) + data->tx <= data->limit.x + data->limit.width &&
203 _cairo_fixed_integer_part (box->p2.y) + data->ty <= data->limit.y + data->limit.height;
206 static cairo_status_t
207 copy_boxes (cairo_win32_display_surface_t *dst,
208 const cairo_pattern_t *source,
209 cairo_boxes_t *boxes)
211 const cairo_surface_pattern_t *pattern;
213 cairo_surface_t *surface;
214 cairo_status_t status;
216 TRACE ((stderr, "%s\n", __FUNCTION__));
218 pattern = (const cairo_surface_pattern_t *) source;
219 surface = _cairo_surface_get_source (pattern->surface, &cb.limit);
220 if (surface->type == CAIRO_SURFACE_TYPE_IMAGE) {
221 surface = to_image_surface(surface)->parent;
223 return CAIRO_INT_STATUS_UNSUPPORTED;
225 if (surface->type != CAIRO_SURFACE_TYPE_WIN32)
226 return CAIRO_INT_STATUS_UNSUPPORTED;
228 if (! _cairo_matrix_is_integer_translation (&source->matrix,
230 return CAIRO_INT_STATUS_UNSUPPORTED;
232 cb.dst = dst->win32.dc;
233 cb.src = to_win32_surface(surface)->dc;
235 /* First check that the data is entirely within the image */
236 if (! _cairo_boxes_for_each_box (boxes, source_contains_box, &cb))
237 return CAIRO_INT_STATUS_UNSUPPORTED;
239 status = __cairo_surface_flush (surface, 0);
245 status = CAIRO_STATUS_SUCCESS;
246 if (! _cairo_boxes_for_each_box (boxes, copy_box, &cb))
247 status = CAIRO_INT_STATUS_UNSUPPORTED;
249 _cairo_win32_display_surface_discard_fallback (dst);
253 static cairo_status_t
254 upload_boxes (cairo_win32_display_surface_t *dst,
255 const cairo_pattern_t *source,
256 cairo_boxes_t *boxes)
258 const cairo_surface_pattern_t *pattern;
259 struct upload_box cb;
260 cairo_surface_t *surface;
261 cairo_image_surface_t *image;
263 cairo_status_t status;
265 TRACE ((stderr, "%s\n", __FUNCTION__));
267 if ((dst->win32.flags & CAIRO_WIN32_SURFACE_CAN_STRETCHDIB) == 0)
268 return CAIRO_INT_STATUS_UNSUPPORTED;
270 if (! _cairo_matrix_is_integer_translation (&source->matrix,
272 return CAIRO_INT_STATUS_UNSUPPORTED;
274 pattern = (const cairo_surface_pattern_t *) source;
275 surface = _cairo_surface_get_source (pattern->surface, &cb.limit);
277 /* First check that the data is entirely within the image */
278 if (! _cairo_boxes_for_each_box (boxes, source_contains_box, &cb))
279 return CAIRO_INT_STATUS_UNSUPPORTED;
281 if (surface->type != CAIRO_SURFACE_TYPE_IMAGE) {
282 status = _cairo_surface_acquire_source_image (surface,
283 &image, &image_extra);
287 image = to_image_surface(surface);
289 status = CAIRO_INT_STATUS_UNSUPPORTED;
290 if (!(image->format == CAIRO_FORMAT_ARGB32 ||
291 image->format == CAIRO_FORMAT_RGB24))
293 if (image->stride != 4*image->width)
296 cb.dst = dst->win32.dc;
297 cb.data = image->data;
299 cb.bi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
300 cb.bi.bmiHeader.biWidth = image->width;
301 cb.bi.bmiHeader.biHeight = -image->height;
302 cb.bi.bmiHeader.biSizeImage = 0;
303 cb.bi.bmiHeader.biXPelsPerMeter = PELS_72DPI;
304 cb.bi.bmiHeader.biYPelsPerMeter = PELS_72DPI;
305 cb.bi.bmiHeader.biPlanes = 1;
306 cb.bi.bmiHeader.biBitCount = 32;
307 cb.bi.bmiHeader.biCompression = BI_RGB;
308 cb.bi.bmiHeader.biClrUsed = 0;
309 cb.bi.bmiHeader.biClrImportant = 0;
313 status = CAIRO_STATUS_SUCCESS;
314 if (! _cairo_boxes_for_each_box (boxes, upload_box, &cb))
315 status = CAIRO_INT_STATUS_UNSUPPORTED;
317 _cairo_win32_display_surface_discard_fallback (dst);
319 if (&image->base != surface)
320 _cairo_surface_release_source_image (surface, image, image_extra);
325 static cairo_status_t
326 alpha_blend_boxes (cairo_win32_display_surface_t *dst,
327 const cairo_pattern_t *source,
328 cairo_boxes_t *boxes,
331 const cairo_surface_pattern_t *pattern;
333 cairo_surface_t *surface;
334 cairo_win32_display_surface_t *src;
335 cairo_status_t status;
337 TRACE ((stderr, "%s\n", __FUNCTION__));
338 if (source->type != CAIRO_PATTERN_TYPE_SURFACE)
339 return CAIRO_INT_STATUS_UNSUPPORTED;
341 pattern = (const cairo_surface_pattern_t *) source;
342 surface = _cairo_surface_get_source (pattern->surface, &cb.limit);
343 if (surface->type == CAIRO_SURFACE_TYPE_IMAGE) {
344 surface = to_image_surface(surface)->parent;
346 return CAIRO_INT_STATUS_UNSUPPORTED;
348 if (surface->type != CAIRO_SURFACE_TYPE_WIN32)
349 return CAIRO_INT_STATUS_UNSUPPORTED;
351 if (! _cairo_matrix_is_integer_translation (&source->matrix,
353 return CAIRO_INT_STATUS_UNSUPPORTED;
355 src = to_win32_display_surface (surface);
356 cb.dst = dst->win32.dc;
357 cb.src = src->win32.dc;
359 /* First check that the data is entirely within the image */
360 if (! _cairo_boxes_for_each_box (boxes, source_contains_box, &cb))
361 return CAIRO_INT_STATUS_UNSUPPORTED;
363 status = __cairo_surface_flush (&src->win32.base, 0);
367 cb.bf.BlendOp = AC_SRC_OVER;
368 cb.bf.BlendFlags = 0;
369 cb.bf.SourceConstantAlpha = alpha;
370 cb.bf.AlphaFormat = (src->win32.format == CAIRO_FORMAT_ARGB32) ? AC_SRC_ALPHA : 0;
371 cb.alpha_blend = to_win32_device(dst->win32.base.device)->alpha_blend;
375 status = CAIRO_STATUS_SUCCESS;
376 if (! _cairo_boxes_for_each_box (boxes, alpha_box, &cb))
377 status = CAIRO_INT_STATUS_UNSUPPORTED;
379 _cairo_win32_display_surface_discard_fallback (dst);
384 can_alpha_blend (cairo_win32_display_surface_t *dst)
386 if ((dst->win32.flags & CAIRO_WIN32_SURFACE_CAN_ALPHABLEND) == 0)
389 return to_win32_device(dst->win32.base.device)->alpha_blend != NULL;
392 static cairo_status_t
393 draw_boxes (cairo_composite_rectangles_t *composite,
394 cairo_boxes_t *boxes)
396 cairo_win32_display_surface_t *dst = to_win32_display_surface(composite->surface);
397 cairo_operator_t op = composite->op;
398 const cairo_pattern_t *src = &composite->source_pattern.base;
399 cairo_int_status_t status;
401 TRACE ((stderr, "%s\n", __FUNCTION__));
402 if (boxes->num_boxes == 0 && composite->is_bounded)
403 return CAIRO_STATUS_SUCCESS;
405 if (!boxes->is_pixel_aligned)
406 return CAIRO_INT_STATUS_UNSUPPORTED;
408 if (op == CAIRO_OPERATOR_CLEAR)
409 op = CAIRO_OPERATOR_SOURCE;
411 if (op == CAIRO_OPERATOR_OVER &&
412 _cairo_pattern_is_opaque (src, &composite->bounded))
413 op = CAIRO_OPERATOR_SOURCE;
415 if (dst->win32.base.is_clear &&
416 (op == CAIRO_OPERATOR_OVER || op == CAIRO_OPERATOR_ADD))
417 op = CAIRO_OPERATOR_SOURCE;
419 if (op == CAIRO_OPERATOR_SOURCE) {
420 status = CAIRO_INT_STATUS_UNSUPPORTED;
421 if (src->type == CAIRO_PATTERN_TYPE_SURFACE) {
422 status = copy_boxes (dst, src, boxes);
423 if (status == CAIRO_INT_STATUS_UNSUPPORTED)
424 status = upload_boxes (dst, src, boxes);
425 } else if (src->type == CAIRO_PATTERN_TYPE_SOLID) {
426 status = fill_boxes (dst, src, boxes);
431 if (op == CAIRO_OPERATOR_OVER && can_alpha_blend (dst))
432 return alpha_blend_boxes (dst, src, boxes, 255);
434 return CAIRO_INT_STATUS_UNSUPPORTED;
437 static cairo_status_t
438 opacity_boxes (cairo_composite_rectangles_t *composite,
439 cairo_boxes_t *boxes)
441 cairo_win32_display_surface_t *dst = to_win32_display_surface(composite->surface);
442 cairo_operator_t op = composite->op;
443 const cairo_pattern_t *src = &composite->source_pattern.base;
445 TRACE ((stderr, "%s\n", __FUNCTION__));
446 if (composite->mask_pattern.base.type != CAIRO_PATTERN_TYPE_SOLID)
447 return CAIRO_INT_STATUS_UNSUPPORTED;
449 if (boxes->num_boxes == 0 && composite->is_bounded)
450 return CAIRO_STATUS_SUCCESS;
452 if (!boxes->is_pixel_aligned)
453 return CAIRO_INT_STATUS_UNSUPPORTED;
455 if (op != CAIRO_OPERATOR_OVER)
456 return CAIRO_INT_STATUS_UNSUPPORTED;
458 if (!can_alpha_blend (dst))
459 return CAIRO_INT_STATUS_UNSUPPORTED;
461 return alpha_blend_boxes (dst, src, boxes,
462 composite->mask_pattern.solid.color.alpha_short >> 8);
465 /* high-level compositor interface */
467 static cairo_bool_t check_blit (cairo_composite_rectangles_t *composite)
469 cairo_win32_display_surface_t *dst;
471 if (composite->clip->path)
474 dst = to_win32_display_surface (composite->surface);
478 if (dst->win32.format != CAIRO_FORMAT_RGB24)
481 if (dst->win32.flags & CAIRO_WIN32_SURFACE_CAN_BITBLT)
484 return dst->image == NULL;
487 static cairo_int_status_t
488 _cairo_win32_gdi_compositor_paint (const cairo_compositor_t *compositor,
489 cairo_composite_rectangles_t *composite)
491 cairo_int_status_t status = CAIRO_INT_STATUS_UNSUPPORTED;
493 if (check_blit (composite)) {
496 TRACE ((stderr, "%s\n", __FUNCTION__));
497 _cairo_clip_steal_boxes (composite->clip, &boxes);
498 status = draw_boxes (composite, &boxes);
499 _cairo_clip_unsteal_boxes (composite->clip, &boxes);
505 static cairo_int_status_t
506 _cairo_win32_gdi_compositor_mask (const cairo_compositor_t *compositor,
507 cairo_composite_rectangles_t *composite)
509 cairo_int_status_t status = CAIRO_INT_STATUS_UNSUPPORTED;
511 if (check_blit (composite)) {
514 TRACE ((stderr, "%s\n", __FUNCTION__));
515 _cairo_clip_steal_boxes (composite->clip, &boxes);
516 status = opacity_boxes (composite, &boxes);
517 _cairo_clip_unsteal_boxes (composite->clip, &boxes);
523 static cairo_int_status_t
524 _cairo_win32_gdi_compositor_stroke (const cairo_compositor_t *compositor,
525 cairo_composite_rectangles_t *composite,
526 const cairo_path_fixed_t *path,
527 const cairo_stroke_style_t *style,
528 const cairo_matrix_t *ctm,
529 const cairo_matrix_t *ctm_inverse,
531 cairo_antialias_t antialias)
533 cairo_int_status_t status;
535 status = CAIRO_INT_STATUS_UNSUPPORTED;
536 if (check_blit (composite) &&
537 _cairo_path_fixed_stroke_is_rectilinear (path)) {
540 TRACE ((stderr, "%s\n", __FUNCTION__));
541 _cairo_boxes_init_with_clip (&boxes, composite->clip);
542 status = _cairo_path_fixed_stroke_rectilinear_to_boxes (path,
547 if (likely (status == CAIRO_INT_STATUS_SUCCESS))
548 status = draw_boxes (composite, &boxes);
549 _cairo_boxes_fini (&boxes);
555 static cairo_int_status_t
556 _cairo_win32_gdi_compositor_fill (const cairo_compositor_t *compositor,
557 cairo_composite_rectangles_t *composite,
558 const cairo_path_fixed_t *path,
559 cairo_fill_rule_t fill_rule,
561 cairo_antialias_t antialias)
563 cairo_int_status_t status;
565 status = CAIRO_INT_STATUS_UNSUPPORTED;
566 if (check_blit (composite) &&
567 _cairo_path_fixed_fill_is_rectilinear (path)) {
570 TRACE ((stderr, "%s\n", __FUNCTION__));
571 _cairo_boxes_init_with_clip (&boxes, composite->clip);
572 status = _cairo_path_fixed_fill_rectilinear_to_boxes (path,
576 if (likely (status == CAIRO_INT_STATUS_SUCCESS))
577 status = draw_boxes (composite, &boxes);
578 _cairo_boxes_fini (&boxes);
584 static cairo_bool_t check_glyphs (cairo_composite_rectangles_t *composite,
585 cairo_scaled_font_t *scaled_font)
587 if (! _cairo_clip_is_region (composite->clip))
590 if (cairo_scaled_font_get_type (scaled_font) != CAIRO_FONT_TYPE_WIN32)
593 if (! _cairo_pattern_is_opaque_solid (&composite->source_pattern.base))
596 return (composite->op == CAIRO_OPERATOR_CLEAR ||
597 composite->op == CAIRO_OPERATOR_SOURCE ||
598 composite->op == CAIRO_OPERATOR_OVER);
601 static cairo_int_status_t
602 _cairo_win32_gdi_compositor_glyphs (const cairo_compositor_t *compositor,
603 cairo_composite_rectangles_t*composite,
604 cairo_scaled_font_t *scaled_font,
605 cairo_glyph_t *glyphs,
607 cairo_bool_t overlap)
609 cairo_int_status_t status;
611 status = CAIRO_INT_STATUS_UNSUPPORTED;
612 if (check_blit (composite) && check_glyphs (composite, scaled_font)) {
613 cairo_win32_display_surface_t *dst = to_win32_display_surface (composite->surface);
615 TRACE ((stderr, "%s\n", __FUNCTION__));
616 status = _cairo_win32_display_surface_set_clip(dst, composite->clip);
620 status = _cairo_win32_surface_emit_glyphs (&dst->win32,
621 &composite->source_pattern.base,
627 _cairo_win32_display_surface_unset_clip (dst);
633 const cairo_compositor_t *
634 _cairo_win32_gdi_compositor_get (void)
636 static cairo_compositor_t compositor;
638 if (compositor.delegate == NULL) {
639 compositor.delegate = &_cairo_fallback_compositor;
641 compositor.paint = _cairo_win32_gdi_compositor_paint;
642 compositor.mask = _cairo_win32_gdi_compositor_mask;
643 compositor.fill = _cairo_win32_gdi_compositor_fill;
644 compositor.stroke = _cairo_win32_gdi_compositor_stroke;
645 compositor.glyphs = _cairo_win32_gdi_compositor_glyphs;