Upload Tizen2.0 source
[framework/graphics/cairo.git] / boilerplate / cairo-boilerplate-directfb.c
1 /*
2 Test were run with the following script
3 target can be directfb_bitmap or directfb
4
5 export CAIRO_TEST_TARGET=directfb_bitmap
6 export DFBARGS=quiet,no-banner,no-debug,log-file=dfblog,system=x11
7 cd cairo/test
8 make check
9
10 */
11
12 #include "cairo-boilerplate-private.h"
13
14 #include <cairo-directfb.h>
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #include <direct/debug.h>
20
21 D_DEBUG_DOMAIN (CairoDFB_Boiler, "CairoDFB/Boiler", "Cairo DirectFB Boilerplate");
22
23 /* macro for a safe call to DirectFB functions */
24 #define DFBCHECK(x...)  do{                                     \
25     err = x;                                                    \
26     if (err != DFB_OK) {                                        \
27         fprintf (stderr, "%s <%d>:\n\t", __FILE__, __LINE__); \
28         goto ERROR; \
29     }                                                           \
30 } while (0)
31
32 typedef struct _DFBInfo {
33     IDirectFB              *dfb;
34     IDirectFBDisplayLayer  *layer;
35     IDirectFBWindow        *window;
36     IDirectFBSurface       *surface;
37 } DFBInfo;
38
39 static void
40 _cairo_boilerplate_directfb_cleanup (void *closure)
41 {
42     DFBInfo *info = (DFBInfo *) closure;
43
44     if (info->surface)
45         info->surface->Release (info->surface);
46
47     if (info->window)
48         info->window->Release (info->window);
49
50     if (info->layer)
51         info->layer->Release (info->layer);
52
53     if (info->dfb)
54         info->dfb->Release (info->dfb);
55
56     free (info);
57 }
58
59 static DFBInfo *
60 init (void)
61 {
62     DFBDisplayLayerConfig        layer_config;
63     DFBGraphicsDeviceDescription desc;
64     int err;
65     DFBInfo *info;
66
67     info = xcalloc (1, sizeof (DFBInfo));
68     if (info == NULL)
69         return NULL;
70
71     DFBCHECK (DirectFBInit (NULL, NULL));
72     DFBCHECK (DirectFBCreate (&info->dfb));
73     info->dfb->GetDeviceDescription (info->dfb, &desc);
74
75     DFBCHECK (info->dfb->GetDisplayLayer (info->dfb,
76                                           DLID_PRIMARY, &info->layer));
77     info->layer->SetCooperativeLevel (info->layer, DLSCL_ADMINISTRATIVE);
78
79     if ((desc.blitting_flags & (DSBLIT_BLEND_ALPHACHANNEL |
80                                 DSBLIT_BLEND_COLORALPHA)) !=
81         (DSBLIT_BLEND_ALPHACHANNEL | DSBLIT_BLEND_COLORALPHA))
82     {
83         layer_config.flags = DLCONF_BUFFERMODE;
84         layer_config.buffermode = DLBM_BACKSYSTEM;
85         info->layer->SetConfiguration (info->layer, &layer_config);
86     }
87
88     return info;
89
90 ERROR:
91     if (info != NULL)
92         _cairo_boilerplate_directfb_cleanup (info);
93     return NULL;
94 }
95
96 static cairo_surface_t *
97 _cairo_boilerplate_directfb_window_create_surface (DFBInfo         *info,
98                                                    cairo_content_t  content,
99                                                    int              width,
100                                                    int              height)
101 {
102     DFBWindowDescription desc;
103     int err;
104
105     D_DEBUG_AT (CairoDFB_Boiler, "%s (%p, %s, %dx%d)\n", __FUNCTION__, info,
106                 content == CAIRO_CONTENT_ALPHA       ? "ALPHA" :
107                 content == CAIRO_CONTENT_COLOR       ? "RGB"   :
108                 content == CAIRO_CONTENT_COLOR_ALPHA ? "ARGB"  : "unknown content!",
109                 width, height);
110
111     desc.flags  = DWDESC_POSX | DWDESC_POSY |
112                   DWDESC_WIDTH | DWDESC_HEIGHT;
113     desc.caps   = DSCAPS_NONE;
114     desc.posx   = 0;
115     desc.posy   = 0;
116     desc.width  = width;
117     desc.height = height;
118     if (content == CAIRO_CONTENT_COLOR_ALPHA) {
119         desc.flags |= DWDESC_CAPS | DWDESC_PIXELFORMAT;
120         desc.caps  |= DWCAPS_DOUBLEBUFFER | DWCAPS_ALPHACHANNEL;
121         desc.pixelformat = DSPF_ARGB;
122     }
123
124     DFBCHECK (info->layer->CreateWindow (info->layer, &desc, &info->window));
125     info->window->SetOpacity (info->window, 0xFF);
126     info->window->GetSurface (info->window, &info->surface);
127     info->surface->SetColor (info->surface, 0xFF, 0xFF, 0xFF, 0xFF);
128     info->surface->FillRectangle (info->surface,0, 0, desc.width, desc.height);
129     info->surface->Flip (info->surface, NULL, 0);
130
131     return cairo_directfb_surface_create (info->dfb, info->surface);
132
133 ERROR:
134     _cairo_boilerplate_directfb_cleanup (info);
135     return NULL;
136 }
137
138 static cairo_surface_t *
139 _cairo_boilerplate_directfb_bitmap_create_surface (DFBInfo         *info,
140                                                    cairo_content_t  content,
141                                                    int              width,
142                                                    int              height)
143 {
144     int  err;
145     DFBSurfaceDescription  desc;
146
147     D_DEBUG_AT (CairoDFB_Boiler, "%s (%p, %s, %dx%d)\n", __FUNCTION__, info,
148                 content == CAIRO_CONTENT_ALPHA       ? "ALPHA" :
149                 content == CAIRO_CONTENT_COLOR       ? "RGB"   :
150                 content == CAIRO_CONTENT_COLOR_ALPHA ? "ARGB"  : "unknown content!",
151                 width, height);
152
153     desc.flags = DSDESC_WIDTH | DSDESC_HEIGHT;
154     desc.caps = DSCAPS_NONE;
155     desc.width  = width;
156     desc.height = height;
157     if (content == CAIRO_CONTENT_COLOR_ALPHA) {
158         desc.flags |= DSDESC_PIXELFORMAT;
159         desc.pixelformat = DSPF_ARGB;
160     }
161     DFBCHECK (info->dfb->CreateSurface (info->dfb, &desc, &info->surface));
162
163     return cairo_directfb_surface_create (info->dfb, info->surface);
164
165 ERROR:
166     _cairo_boilerplate_directfb_cleanup (info);
167     return NULL;
168 }
169
170 static cairo_surface_t *
171 _cairo_boilerplate_directfb_create_surface (const char                *name,
172                                             cairo_content_t            content,
173                                             double                     width,
174                                             double                     height,
175                                             double                     max_width,
176                                             double                     max_height,
177                                             cairo_boilerplate_mode_t   mode,
178                                             void                     **closure)
179 {
180
181     DFBInfo *info;
182
183     info = init ();
184     if (info == NULL)
185         return NULL;
186
187     *closure = info;
188
189     D_DEBUG_AT (CairoDFB_Boiler, "%s ('%s', %s, %dx%d, %s)\n",
190                 __FUNCTION__, name,
191                 content == CAIRO_CONTENT_ALPHA       ? "ALPHA" :
192                 content == CAIRO_CONTENT_COLOR       ? "RGB"   :
193                 content == CAIRO_CONTENT_COLOR_ALPHA ? "ARGB"  : "unknown content!",
194                 width, height,
195                 mode == CAIRO_BOILERPLATE_MODE_TEST ? "TEST" :
196                 mode == CAIRO_BOILERPLATE_MODE_PERF ? "PERF" : "unknown mode!");
197
198     if (width == 0)
199         width = 1;
200     if (height == 0)
201         height = 1;
202
203     if (mode == CAIRO_BOILERPLATE_MODE_TEST)
204         return _cairo_boilerplate_directfb_bitmap_create_surface (info, content, width, height);
205     else /* mode == CAIRO_BOILERPLATE_MODE_PERF */
206         return _cairo_boilerplate_directfb_window_create_surface (info, content, width, height);
207 }
208
209 static const cairo_boilerplate_target_t targets[] = {
210     {
211         "directfb", "directfb", NULL, NULL,
212         CAIRO_SURFACE_TYPE_DIRECTFB, CAIRO_CONTENT_COLOR, 0,
213         "cairo_directfb_surface_create",
214         _cairo_boilerplate_directfb_create_surface,
215         cairo_surface_create_similar,
216         NULL, NULL,
217         _cairo_boilerplate_get_image_surface,
218         cairo_surface_write_to_png,
219         _cairo_boilerplate_directfb_cleanup,
220         NULL, NULL, TRUE, FALSE, FALSE
221     },
222     {
223         "directfb-bitmap", "directfb", NULL, NULL,
224         CAIRO_SURFACE_TYPE_DIRECTFB, CAIRO_CONTENT_COLOR_ALPHA, 0,
225         "cairo_directfb_surface_create",
226         _cairo_boilerplate_directfb_create_surface,
227         cairo_surface_create_similar,
228         NULL, NULL,
229         _cairo_boilerplate_get_image_surface,
230         cairo_surface_write_to_png,
231         _cairo_boilerplate_directfb_cleanup,
232         NULL, NULL, FALSE, FALSE, FALSE
233     },
234 };
235 CAIRO_BOILERPLATE (directfb, targets);