Tizen 2.0 Release
[framework/graphics/cairo.git] / src / cairo-xlib-core-compositor.c
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
3  *
4  * Copyright © 2002 University of Southern California
5  * Copyright © 2005 Red Hat, Inc.
6  * Copyright © 2011 Intel Corporation
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it either under the terms of the GNU Lesser General Public
10  * License version 2.1 as published by the Free Software Foundation
11  * (the "LGPL") or, at your option, under the terms of the Mozilla
12  * Public License Version 1.1 (the "MPL"). If you do not alter this
13  * notice, a recipient may use your version of this file under either
14  * the MPL or the LGPL.
15  *
16  * You should have received a copy of the LGPL along with this library
17  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
19  * You should have received a copy of the MPL along with this library
20  * in the file COPYING-MPL-1.1
21  *
22  * The contents of this file are subject to the Mozilla Public License
23  * Version 1.1 (the "License"); you may not use this file except in
24  * compliance with the License. You may obtain a copy of the License at
25  * http://www.mozilla.org/MPL/
26  *
27  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
28  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
29  * the specific language governing rights and limitations.
30  *
31  * The Original Code is the cairo graphics library.
32  *
33  * The Initial Developer of the Original Code is University of Southern
34  * California.
35  *
36  * Contributor(s):
37  *      Carl D. Worth <cworth@cworth.org>
38  *      Behdad Esfahbod <behdad@behdad.org>
39  *      Chris Wilson <chris@chris-wilson.co.uk>
40  *      Karl Tomlinson <karlt+@karlt.net>, Mozilla Corporation
41  */
42
43 /* The original X drawing API was very restrictive in what it could handle,
44  * pixel-aligned fill/blits are all that map into Cairo's drawing model.
45  */
46
47 #include "cairoint.h"
48
49 #if !CAIRO_HAS_XLIB_XCB_FUNCTIONS
50
51 #include "cairo-xlib-private.h"
52 #include "cairo-xlib-surface-private.h"
53
54 #include "cairo-boxes-private.h"
55 #include "cairo-clip-inline.h"
56 #include "cairo-compositor-private.h"
57 #include "cairo-image-surface-private.h"
58 #include "cairo-pattern-private.h"
59 #include "cairo-region-private.h"
60 #include "cairo-surface-offset-private.h"
61
62 /* the low-level interface */
63
64 static cairo_int_status_t
65 acquire (void *abstract_dst)
66 {
67     cairo_xlib_surface_t *dst = abstract_dst;
68     return _cairo_xlib_display_acquire (dst->base.device, &dst->display);
69 }
70
71 static cairo_int_status_t
72 release (void *abstract_dst)
73 {
74     cairo_xlib_surface_t *dst = abstract_dst;
75
76     cairo_device_release (&dst->display->base);
77     dst->display = NULL;
78
79     return CAIRO_STATUS_SUCCESS;
80 }
81
82 struct _fill_box {
83     Display *dpy;
84     Drawable drawable;
85     GC gc;
86 };
87
88 static cairo_bool_t fill_box (cairo_box_t *box, void *closure)
89 {
90     struct _fill_box *data = closure;
91     int x = _cairo_fixed_integer_part (box->p1.x);
92     int y = _cairo_fixed_integer_part (box->p1.y);
93     int width  = _cairo_fixed_integer_part (box->p2.x - box->p1.x);
94     int height = _cairo_fixed_integer_part (box->p2.y - box->p1.y);
95
96     XFillRectangle (data->dpy, data->drawable, data->gc, x, y, width, height);
97     return TRUE;
98 }
99
100 static void
101 _characterize_field (uint32_t mask, int *width, int *shift)
102 {
103     *width = _cairo_popcount (mask);
104     /* The final '& 31' is to force a 0 mask to result in 0 shift. */
105     *shift = _cairo_popcount ((mask - 1) & ~mask) & 31;
106 }
107
108 static uint32_t
109 color_to_pixel (cairo_xlib_surface_t    *dst,
110                 const cairo_color_t     *color)
111 {
112     uint32_t rgba = 0;
113     int width, shift;
114
115     _characterize_field (dst->a_mask, &width, &shift);
116     rgba |= color->alpha_short >> (16 - width) << shift;
117
118     _characterize_field (dst->r_mask, &width, &shift);
119     rgba |= color->red_short >> (16 - width) << shift;
120
121     _characterize_field (dst->g_mask, &width, &shift);
122     rgba |= color->green_short >> (16 - width) << shift;
123
124     _characterize_field (dst->b_mask, &width, &shift);
125     rgba |= color->blue_short >> (16 - width) << shift;
126
127     return rgba;
128 }
129
130 static cairo_int_status_t
131 fill_boxes (cairo_xlib_surface_t    *dst,
132             const cairo_color_t     *color,
133             cairo_boxes_t           *boxes)
134 {
135     cairo_surface_t *dither = NULL;
136     cairo_status_t status;
137     struct _fill_box fb;
138
139     status = _cairo_xlib_surface_get_gc (dst->display, dst, &fb.gc);
140     if (unlikely (status))
141         return status;
142
143     fb.dpy = dst->display->display;
144     fb.drawable = dst->drawable;
145
146     if (dst->visual && dst->visual->class != TrueColor && 0) {
147         cairo_solid_pattern_t solid;
148         cairo_surface_attributes_t attrs;
149
150         _cairo_pattern_init_solid (&solid, color);
151 #if 0
152         status = _cairo_pattern_acquire_surface (&solid.base, &dst->base,
153                                                  0, 0,
154                                                  ARRAY_LENGTH (dither_pattern[0]),
155                                                  ARRAY_LENGTH (dither_pattern),
156                                                  CAIRO_PATTERN_ACQUIRE_NONE,
157                                                  &dither,
158                                                  &attrs);
159         if (unlikely (status)) {
160             _cairo_xlib_surface_put_gc (dst->display, dst, fb.gc);
161             return status;
162         }
163 #endif
164
165         XSetTSOrigin (fb.dpy, fb.gc,
166                       - (dst->base.device_transform.x0 + attrs.x_offset),
167                       - (dst->base.device_transform.y0 + attrs.y_offset));
168         XSetTile (fb.dpy, fb.gc, ((cairo_xlib_surface_t *) dither)->drawable);
169     } else {
170         XGCValues gcv;
171
172         gcv.foreground = color_to_pixel (dst, color);
173         gcv.fill_style = FillSolid;
174
175         XChangeGC (fb.dpy, fb.gc, GCFillStyle | GCForeground, &gcv);
176     }
177
178     _cairo_boxes_for_each_box (boxes, fill_box, &fb);
179
180     _cairo_xlib_surface_put_gc (dst->display, dst, fb.gc);
181
182     cairo_surface_destroy (dither);
183
184     return CAIRO_STATUS_SUCCESS;
185 }
186
187 struct _fallback_box {
188     cairo_xlib_surface_t        *dst;
189     cairo_format_t               format;
190     const cairo_pattern_t       *pattern;
191 };
192
193 static cairo_bool_t fallback_box (cairo_box_t *box, void *closure)
194 {
195     struct _fallback_box *data = closure;
196     int x = _cairo_fixed_integer_part (box->p1.x);
197     int y = _cairo_fixed_integer_part (box->p1.y);
198     int width  = _cairo_fixed_integer_part (box->p2.x - box->p1.x);
199     int height = _cairo_fixed_integer_part (box->p2.y - box->p1.y);
200     cairo_surface_t *image;
201     cairo_status_t status;
202
203     /* XXX for EXTEND_NONE and if the box is wholly outside we can just fill */
204
205     image = cairo_surface_create_similar_image (&data->dst->base, data->format,
206                                                 width, height);
207     status = _cairo_surface_offset_paint (image, x, y,
208                                           CAIRO_OPERATOR_SOURCE,
209                                           data->pattern, NULL);
210     if (status == CAIRO_STATUS_SUCCESS) {
211         status = _cairo_xlib_surface_draw_image (data->dst,
212                                                  (cairo_image_surface_t *)image,
213                                                  0, 0,
214                                                  width, height,
215                                                  x, y);
216     }
217     cairo_surface_destroy (image);
218
219     return status == CAIRO_STATUS_SUCCESS;
220 }
221
222 static cairo_int_status_t
223 fallback_boxes (cairo_xlib_surface_t    *dst,
224                 const cairo_pattern_t   *pattern,
225                 cairo_boxes_t           *boxes)
226 {
227     struct _fallback_box fb;
228
229     /* XXX create_similar_image using pixman_format? */
230     switch (dst->depth) {
231     case 8: fb.format = CAIRO_FORMAT_A8; break;
232     case 16: fb.format = CAIRO_FORMAT_RGB16_565; break;
233     case 24: fb.format = CAIRO_FORMAT_RGB24; break;
234     case 30: fb.format = CAIRO_FORMAT_RGB30; break;
235     case 32: fb.format = CAIRO_FORMAT_ARGB32; break;
236     default: return CAIRO_INT_STATUS_UNSUPPORTED;
237     }
238
239     fb.dst = dst;
240     fb.pattern = pattern;
241
242     if (! _cairo_boxes_for_each_box (boxes, fallback_box, &fb))
243         return CAIRO_INT_STATUS_UNSUPPORTED;
244
245     return CAIRO_STATUS_SUCCESS;
246 }
247
248 static cairo_int_status_t
249 render_boxes (cairo_xlib_surface_t      *dst,
250               const cairo_pattern_t     *pattern,
251               cairo_boxes_t             *boxes)
252 {
253     double pad;
254
255     if (_cairo_pattern_analyze_filter (pattern, &pad) != CAIRO_FILTER_NEAREST)
256         return fallback_boxes (dst, pattern, boxes);
257
258     switch (pattern->extend) {
259     default:
260     case CAIRO_EXTEND_NONE:
261     case CAIRO_EXTEND_REFLECT:
262     case CAIRO_EXTEND_PAD:
263         return fallback_boxes (dst, pattern, boxes);
264
265     case CAIRO_EXTEND_REPEAT: /* XXX Use tiling */
266         return fallback_boxes (dst, pattern, boxes);
267     }
268 }
269
270 /* the mid-level: converts boxes into drawing operations */
271
272 struct _box_data {
273     Display *dpy;
274     cairo_xlib_surface_t *dst;
275     cairo_surface_t *src;
276     GC gc;
277     int tx, ty;
278     int width, height;
279 };
280
281 static cairo_bool_t source_contains_box (cairo_box_t *box, void *closure)
282 {
283     struct _box_data *data = closure;
284
285     /* The box is pixel-aligned so the truncation is safe. */
286     return
287         _cairo_fixed_integer_part (box->p1.x) + data->tx >= 0 &&
288         _cairo_fixed_integer_part (box->p1.y) + data->ty >= 0 &&
289         _cairo_fixed_integer_part (box->p2.x) + data->tx <= data->width &&
290         _cairo_fixed_integer_part (box->p2.y) + data->ty <= data->height;
291 }
292
293 static cairo_bool_t image_upload_box (cairo_box_t *box, void *closure)
294 {
295     const struct _box_data *iub = closure;
296     int x = _cairo_fixed_integer_part (box->p1.x);
297     int y = _cairo_fixed_integer_part (box->p1.y);
298     int width  = _cairo_fixed_integer_part (box->p2.x - box->p1.x);
299     int height = _cairo_fixed_integer_part (box->p2.y - box->p1.y);
300
301     return _cairo_xlib_surface_draw_image (iub->dst,
302                                            (cairo_image_surface_t *)iub->src,
303                                            x + iub->tx, y + iub->ty,
304                                            width, height,
305                                            x, y) == CAIRO_STATUS_SUCCESS;
306 }
307
308 static cairo_bool_t
309 surface_matches_image_format (cairo_xlib_surface_t *surface,
310                               cairo_image_surface_t *image)
311 {
312     cairo_format_masks_t format;
313
314     return (_pixman_format_to_masks (image->pixman_format, &format) &&
315             (format.alpha_mask == surface->a_mask || surface->a_mask == 0) &&
316             (format.red_mask   == surface->r_mask || surface->r_mask == 0) &&
317             (format.green_mask == surface->g_mask || surface->g_mask == 0) &&
318             (format.blue_mask  == surface->b_mask || surface->b_mask == 0));
319 }
320
321 static cairo_status_t
322 upload_image_inplace (cairo_xlib_surface_t *dst,
323                       const cairo_pattern_t *source,
324                       cairo_boxes_t *boxes)
325 {
326     const cairo_surface_pattern_t *pattern;
327     struct _box_data iub;
328     cairo_image_surface_t *image;
329
330     if (source->type != CAIRO_PATTERN_TYPE_SURFACE)
331         return CAIRO_INT_STATUS_UNSUPPORTED;
332
333     pattern = (const cairo_surface_pattern_t *) source;
334     if (pattern->surface->type != CAIRO_SURFACE_TYPE_IMAGE)
335         return CAIRO_INT_STATUS_UNSUPPORTED;
336
337     image = (cairo_image_surface_t *) pattern->surface;
338     if (image->format == CAIRO_FORMAT_INVALID)
339         return CAIRO_INT_STATUS_UNSUPPORTED;
340
341     if (image->depth != dst->depth)
342         return CAIRO_INT_STATUS_UNSUPPORTED;
343
344     if (! surface_matches_image_format (dst, image))
345         return CAIRO_INT_STATUS_UNSUPPORTED;
346
347     /* XXX subsurface */
348
349     if (! _cairo_matrix_is_integer_translation (&source->matrix,
350                                                 &iub.tx, &iub.ty))
351         return CAIRO_INT_STATUS_UNSUPPORTED;
352
353     iub.dst = dst;
354     iub.src = &image->base;
355     iub.width  = image->width;
356     iub.height = image->height;
357
358     /* First check that the data is entirely within the image */
359     if (! _cairo_boxes_for_each_box (boxes, source_contains_box, &iub))
360         return CAIRO_INT_STATUS_UNSUPPORTED;
361
362     if (! _cairo_boxes_for_each_box (boxes, image_upload_box, &iub))
363         return CAIRO_INT_STATUS_UNSUPPORTED;
364
365     return CAIRO_STATUS_SUCCESS;
366 }
367
368 static cairo_bool_t copy_box (cairo_box_t *box, void *closure)
369 {
370     const struct _box_data *cb = closure;
371     int x = _cairo_fixed_integer_part (box->p1.x);
372     int y = _cairo_fixed_integer_part (box->p1.y);
373     int width  = _cairo_fixed_integer_part (box->p2.x - box->p1.x);
374     int height = _cairo_fixed_integer_part (box->p2.y - box->p1.y);
375
376     XCopyArea (cb->dpy,
377                ((cairo_xlib_surface_t *)cb->src)->drawable,
378                cb->dst->drawable,
379                cb->gc,
380                x + cb->tx, y + cb->ty,
381                width, height,
382                x, y);
383     return TRUE;
384 }
385
386 static cairo_status_t
387 copy_boxes (cairo_xlib_surface_t *dst,
388             const cairo_pattern_t *source,
389             cairo_boxes_t *boxes)
390 {
391     const cairo_surface_pattern_t *pattern;
392     struct _box_data cb;
393     cairo_xlib_surface_t *src;
394     cairo_status_t status;
395
396     if (source->type != CAIRO_PATTERN_TYPE_SURFACE)
397         return CAIRO_INT_STATUS_UNSUPPORTED;
398
399     /* XXX subsurface */
400
401     pattern = (const cairo_surface_pattern_t *) source;
402     if (pattern->surface->backend->type != CAIRO_SURFACE_TYPE_XLIB)
403         return CAIRO_INT_STATUS_UNSUPPORTED;
404
405     src = (cairo_xlib_surface_t *) pattern->surface;
406     if (src->depth != dst->depth)
407         return CAIRO_INT_STATUS_UNSUPPORTED;
408
409     /* We can only have a single control for subwindow_mode on the
410      * GC. If we have a Window destination, we need to set ClipByChildren,
411      * but if we have a Window source, we need IncludeInferiors. If we have
412      * both a Window destination and source, we must fallback. There is
413      * no convenient way to detect if a drawable is a Pixmap or Window,
414      * therefore we can only rely on those surfaces that we created
415      * ourselves to be Pixmaps, and treat everything else as a potential
416      * Window.
417      */
418     if (! src->owns_pixmap && ! dst->owns_pixmap)
419         return CAIRO_INT_STATUS_UNSUPPORTED;
420
421     if (! _cairo_xlib_surface_same_screen (dst, src))
422         return CAIRO_INT_STATUS_UNSUPPORTED;
423
424     if (! _cairo_matrix_is_integer_translation (&source->matrix,
425                                                 &cb.tx, &cb.ty))
426         return CAIRO_INT_STATUS_UNSUPPORTED;
427
428     cb.dpy = dst->display->display;
429     cb.dst = dst;
430     cb.src = &src->base;
431     cb.width  = src->width;
432     cb.height = src->height;
433
434     /* First check that the data is entirely within the image */
435     if (! _cairo_boxes_for_each_box (boxes, source_contains_box, &cb))
436         return CAIRO_INT_STATUS_UNSUPPORTED;
437
438     status = _cairo_xlib_surface_get_gc (dst->display, dst, &cb.gc);
439     if (unlikely (status))
440         return status;
441
442     if (! src->owns_pixmap) {
443         XGCValues gcv;
444
445         gcv.subwindow_mode = IncludeInferiors;
446         XChangeGC (dst->display->display, cb.gc, GCSubwindowMode, &gcv);
447     }
448
449     status = CAIRO_STATUS_SUCCESS;
450     if (! _cairo_boxes_for_each_box (boxes, copy_box, &cb))
451         status = CAIRO_INT_STATUS_UNSUPPORTED;
452
453     if (! src->owns_pixmap) {
454         XGCValues gcv;
455
456         gcv.subwindow_mode = ClipByChildren;
457         XChangeGC (dst->display->display, cb.gc, GCSubwindowMode, &gcv);
458     }
459
460     _cairo_xlib_surface_put_gc (dst->display, dst, cb.gc);
461
462     return status;
463 }
464
465 static cairo_status_t
466 draw_boxes (cairo_composite_rectangles_t *extents,
467             cairo_boxes_t *boxes)
468 {
469     cairo_xlib_surface_t *dst = (cairo_xlib_surface_t *)extents->surface;
470     cairo_operator_t op = extents->op;
471     const cairo_pattern_t *src = &extents->source_pattern.base;
472     cairo_int_status_t status;
473
474     if (boxes->num_boxes == 0 && extents->is_bounded)
475         return CAIRO_STATUS_SUCCESS;
476
477     if (! boxes->is_pixel_aligned)
478         return CAIRO_INT_STATUS_UNSUPPORTED;
479
480     if (op == CAIRO_OPERATOR_CLEAR)
481         op = CAIRO_OPERATOR_SOURCE;
482
483     if (op == CAIRO_OPERATOR_OVER &&
484         _cairo_pattern_is_opaque (src, &extents->bounded))
485         op = CAIRO_OPERATOR_SOURCE;
486
487     if (dst->base.is_clear && op == CAIRO_OPERATOR_OVER)
488         op = CAIRO_OPERATOR_SOURCE;
489
490     if (op != CAIRO_OPERATOR_SOURCE)
491         return CAIRO_INT_STATUS_UNSUPPORTED;
492
493     status = acquire (dst);
494     if (unlikely (status))
495         return status;
496
497     if (src->type == CAIRO_PATTERN_TYPE_SOLID) {
498         status = fill_boxes (dst,
499                              &((cairo_solid_pattern_t *) src)->color,
500                              boxes);
501     } else {
502         status = upload_image_inplace (dst, src, boxes);
503         if (status == CAIRO_INT_STATUS_UNSUPPORTED)
504             status = copy_boxes (dst, src, boxes);
505         if (status == CAIRO_INT_STATUS_UNSUPPORTED)
506             status = render_boxes (dst, src, boxes);
507     }
508
509     release (dst);
510
511     return status;
512 }
513
514 /* high-level compositor interface */
515
516 static cairo_int_status_t
517 _cairo_xlib_core_compositor_paint (const cairo_compositor_t     *compositor,
518                                    cairo_composite_rectangles_t *extents)
519 {
520     cairo_int_status_t status;
521
522     status = CAIRO_INT_STATUS_UNSUPPORTED;
523     if (_cairo_clip_is_region (extents->clip)) {
524         cairo_boxes_t boxes;
525
526          _cairo_clip_steal_boxes (extents->clip, &boxes);
527          status = draw_boxes (extents, &boxes);
528          _cairo_clip_unsteal_boxes (extents->clip, &boxes);
529     }
530
531     return status;
532 }
533
534 static cairo_int_status_t
535 _cairo_xlib_core_compositor_stroke (const cairo_compositor_t    *compositor,
536                                     cairo_composite_rectangles_t *extents,
537                                     const cairo_path_fixed_t    *path,
538                                     const cairo_stroke_style_t  *style,
539                                     const cairo_matrix_t        *ctm,
540                                     const cairo_matrix_t        *ctm_inverse,
541                                     double                       tolerance,
542                                     cairo_antialias_t            antialias)
543 {
544     cairo_int_status_t status;
545
546     status = CAIRO_INT_STATUS_UNSUPPORTED;
547     if (extents->clip->path == NULL &&
548         _cairo_path_fixed_stroke_is_rectilinear (path)) {
549         cairo_boxes_t boxes;
550
551         _cairo_boxes_init_with_clip (&boxes, extents->clip);
552         status = _cairo_path_fixed_stroke_rectilinear_to_boxes (path,
553                                                                 style,
554                                                                 ctm,
555                                                                 antialias,
556                                                                 &boxes);
557         if (likely (status == CAIRO_INT_STATUS_SUCCESS))
558             status = draw_boxes (extents, &boxes);
559         _cairo_boxes_fini (&boxes);
560     }
561
562     return status;
563 }
564
565 static cairo_int_status_t
566 _cairo_xlib_core_compositor_fill (const cairo_compositor_t      *compositor,
567                                   cairo_composite_rectangles_t  *extents,
568                                   const cairo_path_fixed_t      *path,
569                                   cairo_fill_rule_t              fill_rule,
570                                   double                         tolerance,
571                                   cairo_antialias_t              antialias)
572 {
573     cairo_int_status_t status;
574
575     status = CAIRO_INT_STATUS_UNSUPPORTED;
576     if (extents->clip->path == NULL &&
577         _cairo_path_fixed_fill_is_rectilinear (path)) {
578         cairo_boxes_t boxes;
579
580         _cairo_boxes_init_with_clip (&boxes, extents->clip);
581         status = _cairo_path_fixed_fill_rectilinear_to_boxes (path,
582                                                               fill_rule,
583                                                               antialias,
584                                                               &boxes);
585         if (likely (status == CAIRO_INT_STATUS_SUCCESS))
586             status = draw_boxes (extents, &boxes);
587         _cairo_boxes_fini (&boxes);
588     }
589
590     return status;
591 }
592
593 const cairo_compositor_t *
594 _cairo_xlib_core_compositor_get (void)
595 {
596     static cairo_compositor_t compositor;
597
598     if (compositor.delegate == NULL) {
599         compositor.delegate = _cairo_xlib_fallback_compositor_get ();
600
601         compositor.paint = _cairo_xlib_core_compositor_paint;
602         compositor.mask  = NULL;
603         compositor.fill  = _cairo_xlib_core_compositor_fill;
604         compositor.stroke = _cairo_xlib_core_compositor_stroke;
605         compositor.glyphs = NULL; /* XXX PolyGlyph? */
606     }
607
608     return &compositor;
609 }
610
611 #endif /* !CAIRO_HAS_XLIB_XCB_FUNCTIONS */