fd187bd8acd83b96695cd672c8bb79b3859c8929
[framework/graphics/cairo.git] / src / cairo-directfb-surface.c
1 /* cairo - a vector graphics library with display and print output
2  *
3  * Copyright © 2012 Intel Corporation
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it either under the terms of the GNU Lesser General Public
7  * License version 2.1 as published by the Free Software Foundation
8  * (the "LGPL") or, at your option, under the terms of the Mozilla
9  * Public License Version 1.1 (the "MPL"). If you do not alter this
10  * notice, a recipient may use your version of this file under either
11  * the MPL or the LGPL.
12  *
13  * You should have received a copy of the LGPL along with this library
14  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
16  * You should have received a copy of the MPL along with this library
17  * in the file COPYING-MPL-1.1
18  *
19  * The contents of this file are subject to the Mozilla Public License
20  * Version 1.1 (the "License"); you may not use this file except in
21  * compliance with the License. You may obtain a copy of the License at
22  * http://www.mozilla.org/MPL/
23  *
24  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26  * the specific language governing rights and limitations.
27  *
28  * The Original Code is the cairo graphics library.
29  *
30  * The Initial Developer of the Original Code is Chris Wilson
31  *
32  * Contributor(s):
33  *    Chris Wilson <chris@chris-wilson.co.uk>
34  */
35
36 #include "cairoint.h"
37 #include "cairo-directfb.h"
38
39 #include "cairo-clip-private.h"
40 #include "cairo-compositor-private.h"
41 #include "cairo-default-context-private.h"
42 #include "cairo-error-private.h"
43 #include "cairo-image-surface-private.h"
44 #include "cairo-pattern-private.h"
45 #include "cairo-surface-backend-private.h"
46 #include "cairo-surface-fallback-private.h"
47
48 #include <pixman.h>
49
50 #include <directfb.h>
51 #include <direct/types.h>
52 #include <direct/debug.h>
53 #include <direct/memcpy.h>
54 #include <direct/util.h>
55
56 slim_hidden_proto(cairo_directfb_surface_create);
57
58 typedef struct _cairo_dfb_surface {
59     cairo_image_surface_t image;
60
61     IDirectFB                   *dfb;
62     IDirectFBSurface            *dfb_surface;
63
64     unsigned             blit_premultiplied : 1;
65 } cairo_dfb_surface_t;
66
67 static cairo_content_t
68 _directfb_format_to_content (DFBSurfacePixelFormat format)
69 {
70     cairo_content_t content = 0;
71
72     if (DFB_PIXELFORMAT_HAS_ALPHA (format))
73         content |= CAIRO_CONTENT_ALPHA;
74     if (DFB_COLOR_BITS_PER_PIXEL (format))
75         content |= CAIRO_CONTENT_COLOR_ALPHA;
76
77     assert(content);
78     return content;
79 }
80
81 static inline pixman_format_code_t
82 _directfb_to_pixman_format (DFBSurfacePixelFormat format)
83 {
84     switch (format) {
85     case DSPF_UNKNOWN: return 0;
86     case DSPF_ARGB1555: return PIXMAN_a1r5g5b5;
87     case DSPF_RGB16: return PIXMAN_r5g6b5;
88     case DSPF_RGB24: return PIXMAN_r8g8b8;
89     case DSPF_RGB32: return PIXMAN_x8r8g8b8;
90     case DSPF_ARGB: return PIXMAN_a8r8g8b8;
91     case DSPF_A8: return PIXMAN_a8;
92     case DSPF_YUY2: return PIXMAN_yuy2;
93     case DSPF_RGB332: return PIXMAN_r3g3b2;
94     case DSPF_UYVY: return 0;
95     case DSPF_I420: return 0;
96     case DSPF_YV12: return PIXMAN_yv12;
97     case DSPF_LUT8: return 0;
98     case DSPF_ALUT44: return 0;
99     case DSPF_AiRGB: return 0;
100     case DSPF_A1: return 0; /* bit reversed, oops */
101     case DSPF_NV12: return 0;
102     case DSPF_NV16: return 0;
103     case DSPF_ARGB2554: return 0;
104     case DSPF_ARGB4444: return PIXMAN_a4r4g4b4;
105     case DSPF_NV21: return 0;
106     case DSPF_AYUV: return 0;
107     case DSPF_A4: return PIXMAN_a4;
108     case DSPF_ARGB1666: return 0;
109     case DSPF_ARGB6666: return 0;
110     case DSPF_RGB18: return 0;
111     case DSPF_LUT2: return 0;
112     case DSPF_RGB444: return PIXMAN_x4r4g4b4;
113     case DSPF_RGB555: return PIXMAN_x1r5g5b5;
114 #if DFB_NUM_PIXELFORMATS >= 29
115     case DSPF_BGR555: return PIXMAN_x1b5g5r5;
116 #endif
117     }
118     return 0;
119 }
120
121 static cairo_surface_t *
122 _cairo_dfb_surface_create_similar (void            *abstract_src,
123                                    cairo_content_t  content,
124                                    int              width,
125                                    int              height)
126 {
127     cairo_dfb_surface_t *other  = abstract_src;
128     DFBSurfacePixelFormat     format;
129     IDirectFBSurface      *buffer;
130     DFBSurfaceDescription  dsc;
131     cairo_surface_t *surface;
132
133     if (width <= 0 || height <= 0)
134         return _cairo_image_surface_create_with_content (content, width, height);
135
136     switch (content) {
137     default:
138         ASSERT_NOT_REACHED;
139     case CAIRO_CONTENT_COLOR_ALPHA:
140         format = DSPF_ARGB;
141         break;
142     case CAIRO_CONTENT_COLOR:
143         format = DSPF_RGB32;
144         break;
145     case CAIRO_CONTENT_ALPHA:
146         format = DSPF_A8;
147         break;
148     }
149
150     dsc.flags       = DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT;
151     dsc.caps        = DSCAPS_PREMULTIPLIED;
152     dsc.width       = width;
153     dsc.height      = height;
154     dsc.pixelformat = format;
155
156     if (other->dfb->CreateSurface (other->dfb, &dsc, &buffer))
157         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_DEVICE_ERROR));
158
159     surface = cairo_directfb_surface_create (other->dfb, buffer);
160     buffer->Release (buffer);
161
162     return surface;
163 }
164
165 static cairo_status_t
166 _cairo_dfb_surface_finish (void *abstract_surface)
167 {
168     cairo_dfb_surface_t *surface = abstract_surface;
169
170     surface->dfb_surface->Release (surface->dfb_surface);
171     return _cairo_image_surface_finish (abstract_surface);
172 }
173
174 static cairo_surface_t *
175 _cairo_dfb_surface_map_to_image (void *abstract_surface,
176                                  const cairo_rectangle_int_t *extents)
177 {
178     cairo_dfb_surface_t *surface = abstract_surface;
179
180     if (surface->image.pixman_image == NULL) {
181         IDirectFBSurface *buffer = surface->dfb_surface;
182         pixman_image_t *image;
183         void *data;
184         int pitch;
185
186         if (buffer->Lock (buffer, DSLF_READ | DSLF_WRITE, &data, &pitch))
187             return _cairo_surface_create_in_error(_cairo_error (CAIRO_STATUS_NO_MEMORY));
188
189         image = pixman_image_create_bits (surface->image.pixman_format,
190                                           surface->image.width,
191                                           surface->image.height,
192                                           data, pitch);
193         if (image == NULL) {
194             buffer->Unlock (buffer);
195             return _cairo_surface_create_in_error(_cairo_error (CAIRO_STATUS_NO_MEMORY));
196         }
197         _cairo_image_surface_init (&surface->image, image, surface->image.pixman_format);
198     }
199
200     return _cairo_image_surface_map_to_image (&surface->image, extents);
201 }
202
203 static cairo_int_status_t
204 _cairo_dfb_surface_unmap_image (void *abstract_surface,
205                                 cairo_image_surface_t *image)
206 {
207     return CAIRO_INT_STATUS_SUCCESS;
208 }
209
210 static cairo_status_t
211 _cairo_dfb_surface_flush (void *abstract_surface)
212 {
213     cairo_dfb_surface_t *surface = abstract_surface;
214
215     if (surface->image.pixman_image) {
216         surface->dfb_surface->Unlock (surface->dfb_surface);
217
218         pixman_image_unref (surface->image.pixman_image);
219         surface->image.pixman_image = NULL;
220         surface->image.data = NULL;
221     }
222
223     return CAIRO_STATUS_SUCCESS;
224 }
225
226 #if 0
227 static inline DFBSurfacePixelFormat
228 _directfb_from_pixman_format (pixman_format_code_t format)
229 {
230     switch ((int) format) {
231     case PIXMAN_a1r5g5b5: return DSPF_ARGB1555;
232     case PIXMAN_r5g6b5: return DSPF_RGB16;
233     case PIXMAN_r8g8b8: return DSPF_RGB24;
234     case PIXMAN_x8r8g8b8: return DSPF_RGB32;
235     case PIXMAN_a8r8g8b8: return DSPF_ARGB;
236     case PIXMAN_a8: return DSPF_A8;
237     case PIXMAN_yuy2: return DSPF_YUY2;
238     case PIXMAN_r3g3b2: return DSPF_RGB332;
239     case PIXMAN_yv12: return DSPF_YV12;
240     case PIXMAN_a1: return DSPF_A1; /* bit reversed, oops */
241     case PIXMAN_a4r4g4b4: return DSPF_ARGB4444;
242     case PIXMAN_a4: return DSPF_A4;
243     case PIXMAN_x4r4g4b4: return DSPF_RGB444;
244     case PIXMAN_x1r5g5b5: return DSPF_RGB555;
245 #if DFB_NUM_PIXELFORMATS >= 29
246     case PIXMAN_x1b5g5r5: return DSPF_BGR555;
247 #endif
248     default: return 0;
249     }
250 }
251
252 static cairo_bool_t
253 _directfb_get_operator (cairo_operator_t         operator,
254                         DFBSurfaceBlendFunction *ret_srcblend,
255                         DFBSurfaceBlendFunction *ret_dstblend)
256 {
257     DFBSurfaceBlendFunction srcblend = DSBF_ONE;
258     DFBSurfaceBlendFunction dstblend = DSBF_ZERO;
259
260     switch (operator) {
261     case CAIRO_OPERATOR_CLEAR:
262         srcblend = DSBF_ZERO;
263         dstblend = DSBF_ZERO;
264         break;
265     case CAIRO_OPERATOR_SOURCE:
266         srcblend = DSBF_ONE;
267         dstblend = DSBF_ZERO;
268         break;
269     case CAIRO_OPERATOR_OVER:
270         srcblend = DSBF_ONE;
271         dstblend = DSBF_INVSRCALPHA;
272         break;
273     case CAIRO_OPERATOR_IN:
274         srcblend = DSBF_DESTALPHA;
275         dstblend = DSBF_ZERO;
276         break;
277     case CAIRO_OPERATOR_OUT:
278         srcblend = DSBF_INVDESTALPHA;
279         dstblend = DSBF_ZERO;
280         break;
281     case CAIRO_OPERATOR_ATOP:
282         srcblend = DSBF_DESTALPHA;
283         dstblend = DSBF_INVSRCALPHA;
284         break;
285     case CAIRO_OPERATOR_DEST:
286         srcblend = DSBF_ZERO;
287         dstblend = DSBF_ONE;
288         break;
289     case CAIRO_OPERATOR_DEST_OVER:
290         srcblend = DSBF_INVDESTALPHA;
291         dstblend = DSBF_ONE;
292         break;
293     case CAIRO_OPERATOR_DEST_IN:
294         srcblend = DSBF_ZERO;
295         dstblend = DSBF_SRCALPHA;
296         break;
297     case CAIRO_OPERATOR_DEST_OUT:
298         srcblend = DSBF_ZERO;
299         dstblend = DSBF_INVSRCALPHA;
300         break;
301     case CAIRO_OPERATOR_DEST_ATOP:
302         srcblend = DSBF_INVDESTALPHA;
303         dstblend = DSBF_SRCALPHA;
304         break;
305     case CAIRO_OPERATOR_XOR:
306         srcblend = DSBF_INVDESTALPHA;
307         dstblend = DSBF_INVSRCALPHA;
308         break;
309     case CAIRO_OPERATOR_ADD:
310         srcblend = DSBF_ONE;
311         dstblend = DSBF_ONE;
312         break;
313     case CAIRO_OPERATOR_SATURATE:
314         /* XXX This does not work. */
315 #if 0
316         srcblend = DSBF_SRCALPHASAT;
317         dstblend = DSBF_ONE;
318         break;
319 #endif
320     case CAIRO_OPERATOR_MULTIPLY:
321     case CAIRO_OPERATOR_SCREEN:
322     case CAIRO_OPERATOR_OVERLAY:
323     case CAIRO_OPERATOR_DARKEN:
324     case CAIRO_OPERATOR_LIGHTEN:
325     case CAIRO_OPERATOR_COLOR_DODGE:
326     case CAIRO_OPERATOR_COLOR_BURN:
327     case CAIRO_OPERATOR_HARD_LIGHT:
328     case CAIRO_OPERATOR_SOFT_LIGHT:
329     case CAIRO_OPERATOR_DIFFERENCE:
330     case CAIRO_OPERATOR_EXCLUSION:
331     case CAIRO_OPERATOR_HSL_HUE:
332     case CAIRO_OPERATOR_HSL_SATURATION:
333     case CAIRO_OPERATOR_HSL_COLOR:
334     case CAIRO_OPERATOR_HSL_LUMINOSITY:
335     default:
336         return FALSE;
337     }
338
339     *ret_srcblend = srcblend;
340     *ret_dstblend = dstblend;
341
342     return TRUE;
343 }
344 #define RUN_CLIPPED(surface, clip_region, clip, func) {\
345     if ((clip_region) != NULL) {\
346         int n_clips = cairo_region_num_rectangles (clip_region), n; \
347         for (n = 0; n < n_clips; n++) {\
348             if (clip) {\
349                 DFBRegion  reg, *cli = (clip); \
350                 cairo_rectangle_int_t rect; \
351                 cairo_region_get_rectangle (clip_region, n, &rect); \
352                 reg.x1 = rect.x; \
353                 reg.y1 = rect.y; \
354                 reg.x2 = rect.x + rect.width - 1; \
355                 reg.y2 = rect.y + rect.height - 1; \
356                 if (reg.x2 < cli->x1 || reg.y2 < cli->y1 ||\
357                     reg.x1 > cli->x2 || reg.y1 > cli->y2)\
358                     continue;\
359                 if (reg.x1 < cli->x1)\
360                     reg.x1 = cli->x1;\
361                 if (reg.y1 < cli->y1)\
362                     reg.y1 = cli->y1;\
363                 if (reg.x2 > cli->x2)\
364                     reg.x2 = cli->x2;\
365                 if (reg.y2 > cli->y2)\
366                     reg.y2 = cli->y2;\
367                 (surface)->dfbsurface->SetClip ((surface)->dfbsurface, &reg);\
368             } else {\
369                 DFBRegion reg; \
370                 cairo_rectangle_int_t rect; \
371                 cairo_region_get_rectangle (clip_region, n, &rect); \
372                 reg.x1 = rect.x; \
373                 reg.y1 = rect.y; \
374                 reg.x2 = rect.x + rect.width - 1; \
375                 reg.y2 = rect.y + rect.height - 1; \
376                 (surface)->dfbsurface->SetClip ((surface)->dfbsurface, &reg); \
377             }\
378             func;\
379         }\
380     } else {\
381         (surface)->dfbsurface->SetClip ((surface)->dfbsurface, clip);\
382         func;\
383     }\
384 }
385
386 static cairo_int_status_t
387 _cairo_dfb_surface_fill_rectangles (void                  *abstract_surface,
388                                          cairo_operator_t       op,
389                                          const cairo_color_t   *color,
390                                          cairo_rectangle_int_t *rects,
391                                          int                    n_rects)
392 {
393     cairo_dfb_surface_t *dst   = abstract_surface;
394     DFBSurfaceDrawingFlags    flags;
395     DFBSurfaceBlendFunction   sblend;
396     DFBSurfaceBlendFunction   dblend;
397     DFBRectangle              r[n_rects];
398     int                       i;
399
400     D_DEBUG_AT (CairoDFB_Render,
401                 "%s( dst=%p, op=%d, color=%p, rects=%p, n_rects=%d ).\n",
402                 __FUNCTION__, dst, op, color, rects, n_rects);
403
404     if (! dst->supported_destination)
405         return CAIRO_INT_STATUS_UNSUPPORTED;
406
407     if (! _directfb_get_operator (op, &sblend, &dblend))
408         return CAIRO_INT_STATUS_UNSUPPORTED;
409
410     if (CAIRO_COLOR_IS_OPAQUE (color)) {
411         if (sblend == DSBF_SRCALPHA)
412             sblend = DSBF_ONE;
413         else if (sblend == DSBF_INVSRCALPHA)
414             sblend = DSBF_ZERO;
415
416         if (dblend == DSBF_SRCALPHA)
417             dblend = DSBF_ONE;
418         else if (dblend == DSBF_INVSRCALPHA)
419             dblend = DSBF_ZERO;
420     }
421     if ((dst->base.content & CAIRO_CONTENT_ALPHA) == 0) {
422         if (sblend == DSBF_DESTALPHA)
423             sblend = DSBF_ONE;
424         else if (sblend == DSBF_INVDESTALPHA)
425             sblend = DSBF_ZERO;
426
427         if (dblend == DSBF_DESTALPHA)
428             dblend = DSBF_ONE;
429         else if (dblend == DSBF_INVDESTALPHA)
430             dblend = DSBF_ZERO;
431     }
432
433     flags = (sblend == DSBF_ONE && dblend == DSBF_ZERO) ? DSDRAW_NOFX : DSDRAW_BLEND;
434     dst->dfbsurface->SetDrawingFlags (dst->dfbsurface, flags);
435     if (flags & DSDRAW_BLEND) {
436         dst->dfbsurface->SetSrcBlendFunction (dst->dfbsurface, sblend);
437         dst->dfbsurface->SetDstBlendFunction (dst->dfbsurface, dblend);
438     }
439
440     dst->dfbsurface->SetColor (dst->dfbsurface,
441                                color->red_short >> 8,
442                                color->green_short >> 8,
443                                color->blue_short >> 8,
444                                color->alpha_short >> 8);
445
446     for (i = 0; i < n_rects; i++) {
447         r[i].x = rects[i].x;
448         r[i].y = rects[i].y;
449         r[i].w = rects[i].width;
450         r[i].h = rects[i].height;
451     }
452
453     RUN_CLIPPED (dst, NULL, NULL,
454                  dst->dfbsurface->FillRectangles (dst->dfbsurface, r, n_rects));
455
456     return CAIRO_STATUS_SUCCESS;
457 }
458 #endif
459
460 static cairo_surface_backend_t
461 _cairo_dfb_surface_backend = {
462     CAIRO_SURFACE_TYPE_DIRECTFB, /*type*/
463     _cairo_dfb_surface_finish, /*finish*/
464     _cairo_default_context_create,
465
466     _cairo_dfb_surface_create_similar,/*create_similar*/
467     NULL, /* create similar image */
468     _cairo_dfb_surface_map_to_image,
469     _cairo_dfb_surface_unmap_image,
470
471     _cairo_surface_default_source,
472     _cairo_surface_default_acquire_source_image,
473     _cairo_surface_default_release_source_image,
474     NULL,
475
476     NULL, /* copy_page */
477     NULL, /* show_page */
478
479     _cairo_image_surface_get_extents,
480     _cairo_image_surface_get_font_options,
481
482     _cairo_dfb_surface_flush,
483     NULL, /* mark_dirty_rectangle */
484
485     _cairo_surface_fallback_paint,
486     _cairo_surface_fallback_mask,
487     _cairo_surface_fallback_stroke,
488     _cairo_surface_fallback_fill,
489     NULL, /* fill-stroke */
490     _cairo_surface_fallback_glyphs,
491 };
492
493 cairo_surface_t *
494 cairo_directfb_surface_create (IDirectFB *dfb, IDirectFBSurface *dfbsurface)
495 {
496     cairo_dfb_surface_t *surface;
497     DFBSurfacePixelFormat     format;
498     DFBSurfaceCapabilities caps;
499     pixman_format_code_t pixman_format;
500     int width, height;
501
502     D_ASSERT (dfb != NULL);
503     D_ASSERT (dfbsurface != NULL);
504
505     dfbsurface->GetPixelFormat (dfbsurface, &format);
506     dfbsurface->GetSize (dfbsurface, &width, &height);
507
508     pixman_format = _directfb_to_pixman_format (format);
509     if (! pixman_format_supported_destination (pixman_format))
510         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
511
512     surface = calloc (1, sizeof (cairo_dfb_surface_t));
513     if (surface == NULL)
514         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
515
516     /* XXX dfb -> device */
517     _cairo_surface_init (&surface->image.base,
518                          &_cairo_dfb_surface_backend,
519                          NULL, /* device */
520                          _directfb_format_to_content (format));
521
522     surface->image.pixman_format = pixman_format;
523     surface->image.format = _cairo_format_from_pixman_format (pixman_format);
524
525     surface->image.width = width;
526     surface->image.height = height;
527     surface->image.depth = PIXMAN_FORMAT_DEPTH(pixman_format);
528
529     surface->dfb = dfb;
530     surface->dfb_surface = dfbsurface;
531     dfbsurface->AddRef (dfbsurface);
532
533     dfbsurface->GetCapabilities (dfbsurface, &caps);
534     if (caps & DSCAPS_PREMULTIPLIED)
535         surface->blit_premultiplied = TRUE;
536
537     return &surface->image.base;
538 }
539 slim_hidden_def(cairo_directfb_surface_create);