summaryrefslogtreecommitdiff
path: root/boilerplate/cairo-boilerplate-directfb.c
blob: 7a6462f6f06a1b24bf80b51ff4b103257368b6c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
Test were run with the following script
target can be directfb_bitmap or directfb

export CAIRO_TEST_TARGET=directfb_bitmap
export DFBARGS=quiet,no-banner,no-debug,log-file=dfblog,system=x11
cd cairo/test
make check

*/

#include "cairo-boilerplate.h"
#include "cairo-boilerplate-directfb-private.h"

#include <cairo-directfb.h>

#include <stdio.h>
#include <stdlib.h>

#include <direct/debug.h>

D_DEBUG_DOMAIN (CairoDFB_Boiler, "CairoDFB/Boiler", "Cairo DirectFB Boilerplate");

/* macro for a safe call to DirectFB functions */
#define DFBCHECK(x...)  do{                                     \
    err = x;                                                    \
    if (err != DFB_OK) {                                        \
	fprintf (stderr, "%s <%d>:\n\t", __FILE__, __LINE__); \
	goto ERROR; \
    }                                                           \
} while (0)

typedef struct _DFBInfo {
    IDirectFB              *dfb;
    IDirectFBDisplayLayer  *layer;
    IDirectFBWindow        *window;
    IDirectFBSurface       *surface;
} DFBInfo;

void
_cairo_boilerplate_directfb_cleanup (void *closure)
{
    DFBInfo *info = (DFBInfo *) closure;

    if (info->surface)
	info->surface->Release (info->surface);

    if (info->window)
	info->window->Release (info->window);

    if (info->layer)
	info->layer->Release (info->layer);

    if (info->dfb)
	info->dfb->Release (info->dfb);

    free (info);
}

static DFBInfo *
init (void)
{
    DFBDisplayLayerConfig        layer_config;
    DFBGraphicsDeviceDescription desc;
    int err;
    DFBInfo *info;

    info = xcalloc (1, sizeof (DFBInfo));
    if (info == NULL)
	return NULL;

    DFBCHECK (DirectFBInit (NULL, NULL));
    DFBCHECK (DirectFBCreate (&info->dfb));
    info->dfb->GetDeviceDescription (info->dfb, &desc);

    DFBCHECK (info->dfb->GetDisplayLayer (info->dfb,
					  DLID_PRIMARY, &info->layer));
    info->layer->SetCooperativeLevel (info->layer, DLSCL_ADMINISTRATIVE);

    if ((desc.blitting_flags & (DSBLIT_BLEND_ALPHACHANNEL |
				DSBLIT_BLEND_COLORALPHA)) !=
	(DSBLIT_BLEND_ALPHACHANNEL | DSBLIT_BLEND_COLORALPHA))
    {
	layer_config.flags = DLCONF_BUFFERMODE;
	layer_config.buffermode = DLBM_BACKSYSTEM;
	info->layer->SetConfiguration (info->layer, &layer_config);
    }

    return info;

ERROR:
    if (info != NULL)
	_cairo_boilerplate_directfb_cleanup (info);
    return NULL;
}

static cairo_surface_t *
_cairo_boilerplate_directfb_window_create_surface (DFBInfo		*info,
						   cairo_content_t	 content,
						   int			 width,
						   int			 height)
{
    DFBWindowDescription desc;
    int err;

    D_DEBUG_AT (CairoDFB_Boiler, "%s (%p, %s, %dx%d)\n", __FUNCTION__, info,
		content == CAIRO_CONTENT_ALPHA       ? "ALPHA" :
		content == CAIRO_CONTENT_COLOR       ? "RGB"   :
		content == CAIRO_CONTENT_COLOR_ALPHA ? "ARGB"  : "unknown content!",
		width, height);

    desc.flags  = DWDESC_POSX | DWDESC_POSY |
	          DWDESC_WIDTH | DWDESC_HEIGHT;
    desc.caps   = DSCAPS_NONE;
    desc.posx   = 0;
    desc.posy   = 0;
    desc.width  = width;
    desc.height = height;
    if (content == CAIRO_CONTENT_COLOR_ALPHA) {
	desc.flags |= DWDESC_CAPS | DSDESC_PIXELFORMAT;
	desc.caps  |= DWCAPS_DOUBLEBUFFER | DWCAPS_ALPHACHANNEL;
	desc.pixelformat = DSPF_ARGB;
    }

    DFBCHECK (info->layer->CreateWindow (info->layer, &desc, &info->window));
    info->window->SetOpacity (info->window, 0xFF);
    info->window->GetSurface (info->window, &info->surface);
    info->surface->SetColor (info->surface, 0xFF, 0xFF, 0xFF, 0xFF);
    info->surface->FillRectangle (info->surface,0, 0, desc.width, desc.height);
    info->surface->Flip (info->surface, NULL, 0);

    return cairo_directfb_surface_create (info->dfb, info->surface);

ERROR:
    _cairo_boilerplate_directfb_cleanup (info);
    return NULL;
}

static cairo_surface_t *
_cairo_boilerplate_directfb_bitmap_create_surface (DFBInfo		*info,
						   cairo_content_t	 content,
						   int			 width,
						   int			 height)
{
    int  err;
    DFBSurfaceDescription  desc;

    D_DEBUG_AT (CairoDFB_Boiler, "%s (%p, %s, %dx%d)\n", __FUNCTION__, info,
		content == CAIRO_CONTENT_ALPHA       ? "ALPHA" :
		content == CAIRO_CONTENT_COLOR       ? "RGB"   :
		content == CAIRO_CONTENT_COLOR_ALPHA ? "ARGB"  : "unknown content!",
		width, height);

    desc.flags = DSDESC_WIDTH | DSDESC_HEIGHT;
    desc.caps = DSCAPS_NONE;
    desc.width  = width;
    desc.height = height;
    if (content == CAIRO_CONTENT_COLOR_ALPHA) {
	desc.flags |= DSDESC_PIXELFORMAT;
	desc.pixelformat = DSPF_ARGB;
    }
    DFBCHECK (info->dfb->CreateSurface (info->dfb, &desc, &info->surface));

    return cairo_directfb_surface_create (info->dfb, info->surface);

ERROR:
    _cairo_boilerplate_directfb_cleanup (info);
    return NULL;
}

cairo_surface_t *
_cairo_boilerplate_directfb_create_surface (const char			 *name,
					    cairo_content_t		  content,
					    int				  width,
					    int				  height,
					    int				  max_width,
					    int				  max_height,
					    cairo_boilerplate_mode_t	  mode,
					    int                           id,
					    void			**closure)
{

    DFBInfo *info;

    info = init ();
    if (info == NULL)
        return NULL;

    *closure = info;

    D_DEBUG_AT (CairoDFB_Boiler, "%s ('%s', %s, %dx%d, %s)\n",
		__FUNCTION__, name,
                content == CAIRO_CONTENT_ALPHA       ? "ALPHA" :
                content == CAIRO_CONTENT_COLOR       ? "RGB"   :
                content == CAIRO_CONTENT_COLOR_ALPHA ? "ARGB"  : "unknown content!",
                width, height,
                mode == CAIRO_BOILERPLATE_MODE_TEST ? "TEST" :
                mode == CAIRO_BOILERPLATE_MODE_PERF ? "PERF" : "unknown mode!");

    if (width == 0)
	width = 1;
    if (height == 0)
	height = 1;

    if (mode == CAIRO_BOILERPLATE_MODE_TEST)
	return _cairo_boilerplate_directfb_bitmap_create_surface (info, content, width, height);
    else /* mode == CAIRO_BOILERPLATE_MODE_PERF */
	return _cairo_boilerplate_directfb_window_create_surface (info, content, width, height);
}