Mercurial > MadButterfly
comparison include/mb_graph_engine_openvg.h @ 582:dd546c4da180 openvg
deal with EGL surface, context for OpenVG GE
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Wed, 30 Jun 2010 01:32:47 +0800 |
parents | 7bfeccdc91cb |
children | ca57132020e4 |
comparison
equal
deleted
inserted
replaced
581:953acea89f76 | 582:dd546c4da180 |
---|---|
1 #ifndef __MB_GE_OPENVG_H_ | 1 #ifndef __MB_GE_OPENVG_H_ |
2 #define __MB_GE_OPENVG_H_ | 2 #define __MB_GE_OPENVG_H_ |
3 | 3 |
4 #include <stdio.h> | 4 #include <stdio.h> |
5 #include <GL/glut.h> | 5 #include <stdlib.h> |
6 #include <EGL/egl.h> | |
7 #include <VG/openvg.h> | |
6 #include "mb_basic_types.h" | 8 #include "mb_basic_types.h" |
7 #include "mb_img_ldr.h" | 9 #include "mb_img_ldr.h" |
8 | 10 |
9 /*! \defgroup mb_ge_cairo MadButterfly Graphic Engine with Cairo | 11 /*! \defgroup mb_ge_cairo MadButterfly Graphic Engine with Cairo |
10 * @{ | 12 * @{ |
22 #define mbe_image_surface_get_format(surface) ((mb_img_fmt_t)0) | 24 #define mbe_image_surface_get_format(surface) ((mb_img_fmt_t)0) |
23 #define mbe_image_surface_get_height(surface) (1) | 25 #define mbe_image_surface_get_height(surface) (1) |
24 #define mbe_image_surface_get_width(surface) (1) | 26 #define mbe_image_surface_get_width(surface) (1) |
25 #define mbe_image_surface_get_data(surface) ((unsigned char *)NULL) | 27 #define mbe_image_surface_get_data(surface) ((unsigned char *)NULL) |
26 #define mbe_scaled_font_reference(scaled) ((mbe_scaled_font_t *)NULL) | 28 #define mbe_scaled_font_reference(scaled) ((mbe_scaled_font_t *)NULL) |
27 /* For OpenVG backend, never invoke xlib surface. | |
28 * #define mbe_xlib_surface_create cairo_xlib_surface_create | |
29 */ | |
30 #define mbe_pattern_create_radial(cx0, cy0, radius0, \ | 29 #define mbe_pattern_create_radial(cx0, cy0, radius0, \ |
31 cx1, cy1, radius1, stops, stop_cnt) \ | 30 cx1, cy1, radius1, stops, stop_cnt) \ |
32 ((mbe_pattern_t *)NULL) | 31 ((mbe_pattern_t *)NULL) |
33 #define mbe_pattern_create_linear(x0, y0, x1, y1, stops, stop_cnt) \ | 32 #define mbe_pattern_create_linear(x0, y0, x1, y1, stops, stop_cnt) \ |
34 ((mbe_pattern_t *)NULL) | 33 ((mbe_pattern_t *)NULL) |
35 #define mbe_image_surface_create(fmt, w, h) ((mbe_surface_t *)NULL) | |
36 #define mbe_scaled_font_destroy(scaled) | 34 #define mbe_scaled_font_destroy(scaled) |
37 #define mbe_font_face_reference(face) ((mbe_font_face_t *)NULL) | 35 #define mbe_font_face_reference(face) ((mbe_font_face_t *)NULL) |
38 #define mbe_scaled_font_create(face, fnt_mtx, ctm) ((mbe_scaled_font_t *)NULL) | 36 #define mbe_scaled_font_create(face, fnt_mtx, ctm) ((mbe_scaled_font_t *)NULL) |
39 #define mbe_pattern_set_matrix(ptn, mtx) | 37 #define mbe_pattern_set_matrix(ptn, mtx) |
40 #define mbe_font_face_destroy(face) | 38 #define mbe_font_face_destroy(face) |
41 #define mbe_paint_with_alpha(canvas, alpha) | 39 #define mbe_paint_with_alpha(canvas, alpha) |
42 #define mbe_surface_destroy(surface) | |
43 #define mbe_set_source_rgba(canvas, r, g, b, a) | 40 #define mbe_set_source_rgba(canvas, r, g, b, a) |
44 #define mbe_set_scaled_font(canvas, scaled) | 41 #define mbe_set_scaled_font(canvas, scaled) |
45 #define mbe_pattern_destroy(pattern) | 42 #define mbe_pattern_destroy(pattern) |
46 #define mbe_get_scaled_font(canvas) ((mbe_scaled_font_t *)NULL) | 43 #define mbe_get_scaled_font(canvas) ((mbe_scaled_font_t *)NULL) |
47 #define mbe_query_font_face(family, slant, weight) ((mbe_font_face_t *)NULL) | 44 #define mbe_query_font_face(family, slant, weight) ((mbe_font_face_t *)NULL) |
49 #define mbe_set_line_width(canvas, w) | 46 #define mbe_set_line_width(canvas, w) |
50 #define mbe_set_source_rgb(canvas, r, g, b) | 47 #define mbe_set_source_rgb(canvas, r, g, b) |
51 #define mbe_get_font_face(canvas) ((mbe_font_face_t *)NULL) | 48 #define mbe_get_font_face(canvas) ((mbe_font_face_t *)NULL) |
52 #define mbe_fill_preserve(canvas) | 49 #define mbe_fill_preserve(canvas) |
53 #define mbe_copy_source(src_canvas, dst_canvas) | 50 #define mbe_copy_source(src_canvas, dst_canvas) |
54 #define mbe_set_source(canvas, pattern) | 51 #define mbe_set_source(canvas, pattern) \ |
52 do { (canvas)->src = (pattern); } while(0) | |
55 #define mbe_reset_clip(canvas) | 53 #define mbe_reset_clip(canvas) |
56 #define mbe_get_target(canvas) ((mbe_surface_t *)NULL) | 54 #define mbe_get_target(canvas) ((mbe_surface_t *)(canvas)->tgt) |
57 #define mbe_close_path(canvas) | 55 #define mbe_close_path(canvas) |
58 #define mbe_text_path(canvas, utf8) | 56 #define mbe_text_path(canvas, utf8) |
59 #define mbe_transform(canvas, mtx) | 57 #define mbe_transform(canvas, mtx) |
60 #define mbe_rectangle(canvas, x, y, w, h) | 58 #define mbe_rectangle(canvas, x, y, w, h) |
61 #define mbe_in_stroke(canvas, x, y) (0) | 59 #define mbe_in_stroke(canvas, x, y) (0) |
65 #define mbe_move_to(canvas, x, y) | 63 #define mbe_move_to(canvas, x, y) |
66 #define mbe_line_to(canvas, x, y) | 64 #define mbe_line_to(canvas, x, y) |
67 #define mbe_in_fill(canvas, x, y) (0) | 65 #define mbe_in_fill(canvas, x, y) (0) |
68 #define mbe_destroy(canvas) | 66 #define mbe_destroy(canvas) |
69 #define mbe_stroke(canvas) | 67 #define mbe_stroke(canvas) |
70 #define mbe_create(surface) ((mbe_t *)NULL) | |
71 #define mbe_clear(canvas) | 68 #define mbe_clear(canvas) |
72 #define mbe_paint(canvas) | 69 #define mbe_paint(canvas) |
73 #define mbe_save(canvas) | 70 #define mbe_save(canvas) |
74 #define mbe_fill(canvas) | 71 #define mbe_fill(canvas) |
75 #define mbe_clip(canvas) | 72 #define mbe_clip(canvas) |
92 }; | 89 }; |
93 | 90 |
94 struct _ge_openvg_mbe { | 91 struct _ge_openvg_mbe { |
95 mbe_pattern_t *src; | 92 mbe_pattern_t *src; |
96 mbe_surface_t *tgt; | 93 mbe_surface_t *tgt; |
97 void *ctx; | 94 EGLContext ctx; |
98 }; | 95 }; |
99 | 96 |
100 struct _ge_openvg_surface { | 97 struct _ge_openvg_surface { |
101 void *surface; | 98 void *surface; |
102 mbe_t *asso_mbe; | 99 mbe_t *asso_mbe; |
117 (vgmtx)[6] = (mtx)[2]; \ | 114 (vgmtx)[6] = (mtx)[2]; \ |
118 (vgmtx)[7] = (mtx)[5]; \ | 115 (vgmtx)[7] = (mtx)[5]; \ |
119 (vgmtx)[8] = 1; \ | 116 (vgmtx)[8] = 1; \ |
120 } while(0) | 117 } while(0) |
121 | 118 |
119 extern EGLNativeDisplayType _ge_openvg_disp_id; | |
120 | |
121 #define _VG_DISPLAY() eglGetDisplay(_ge_openvg_disp_id) | |
122 | |
123 static int | |
124 _openvg_find_confg(mb_img_fmt_t fmt, int w, int h, | |
125 EGLConfig *config) { | |
126 EGLDisplay display; | |
127 EGLint attrib_list[16]; | |
128 EGLConfig configs[1]; | |
129 EGLint nconfigs; | |
130 int i = 0; | |
131 EGLBoolean r; | |
132 | |
133 switch(fmt) { | |
134 case MB_IFMT_ARGB32: | |
135 attrib_list[i++] = EGL_RED_SIZE; | |
136 attrib_list[i++] = 8; | |
137 attrib_list[i++] = EGL_GREEN_SIZE; | |
138 attrib_list[i++] = 8; | |
139 attrib_list[i++] = EGL_BLUE_SIZE; | |
140 attrib_list[i++] = 8; | |
141 attrib_list[i++] = EGL_ALPHA_SIZE; | |
142 attrib_list[i++] = 8; | |
143 break; | |
144 | |
145 case MB_IFMT_RGB24: | |
146 attrib_list[i++] = EGL_RED_SIZE; | |
147 attrib_list[i++] = 8; | |
148 attrib_list[i++] = EGL_GREEN_SIZE; | |
149 attrib_list[i++] = 8; | |
150 attrib_list[i++] = EGL_BLUE_SIZE; | |
151 attrib_list[i++] = 8; | |
152 break; | |
153 | |
154 case MB_IFMT_A8: | |
155 attrib_list[i++] = EGL_ALPHA_SIZE; | |
156 attrib_list[i++] = 8; | |
157 break; | |
158 | |
159 case MB_IFMT_A1: | |
160 attrib_list[i++] = EGL_ALPHA_SIZE; | |
161 attrib_list[i++] = 1; | |
162 break; | |
163 | |
164 case MB_IFMT_RGB16_565: | |
165 attrib_list[i++] = EGL_RED_SIZE; | |
166 attrib_list[i++] = 5; | |
167 attrib_list[i++] = EGL_GREEN_SIZE; | |
168 attrib_list[i++] = 6; | |
169 attrib_list[i++] = EGL_BLUE_SIZE; | |
170 attrib_list[i++] = 5; | |
171 break; | |
172 | |
173 default: | |
174 return -1; | |
175 } | |
176 | |
177 attrib_list[i++] = EGL_SURFACE_TYPE; | |
178 attrib_list[i++] = EGL_PBUFFER_BIT; | |
179 #if 0 | |
180 attrib_list[i++] = EGL_MAX_PBUFFER_WIDTH; | |
181 attrib_list[i++] = w; | |
182 attrib_list[i++] = EGL_MAX_PBUFFER_HEIGHT; | |
183 attrib_list[i++] = h; | |
184 #endif | |
185 | |
186 attrib_list[i++] = EGL_NONE; | |
187 | |
188 display = _VG_DISPLAY(); | |
189 r = eglChooseConfig(display, attrib_list, configs, 1, &nconfigs); | |
190 if(!r) | |
191 return -1; | |
192 | |
193 *config = configs[0]; | |
194 | |
195 return 0; | |
196 } | |
197 | |
198 #define EGL_GLX 1 | |
199 #ifdef EGL_GLX | |
200 #include <X11/Xlib.h> | |
201 #include <X11/Xutil.h> | |
202 | |
203 static int | |
204 _get_img_fmt_from_xvisual(Display *display, Visual *visual) { | |
205 VisualID visual_id; | |
206 XVisualInfo temp; | |
207 XVisualInfo *infos; | |
208 int n; | |
209 int fmt = -1; | |
210 | |
211 visual_id = XVisualIDFromVisual(visual); | |
212 temp.visualid = visual_id; | |
213 infos = XGetVisualInfo(display, VisualIDMask, &temp, &n); | |
214 if(n != 1) | |
215 return -1; | |
216 | |
217 switch(infos->depth) { | |
218 case 32: | |
219 fmt = MB_IFMT_ARGB32; | |
220 break; | |
221 | |
222 case 24: | |
223 fmt = MB_IFMT_RGB24; | |
224 break; | |
225 | |
226 case 16: | |
227 fmt = MB_IFMT_RGB16_565; | |
228 break; | |
229 | |
230 case 8: | |
231 fmt = MB_IFMT_A8; | |
232 break; | |
233 | |
234 case 1: | |
235 fmt = MB_IFMT_A1; | |
236 break; | |
237 } | |
238 | |
239 return fmt; | |
240 } | |
241 | |
242 /*! \brief Create an EGL window surface for X11. | |
243 * | |
244 * This function is compiled only for GLX enabled. | |
245 */ | |
246 static mbe_surface_t * | |
247 mbe_xlib_surface_create(Display *display, Drawable drawable, | |
248 Visual *visual, int width, int height) { | |
249 EGLDisplay egl_disp; | |
250 EGLSurface egl_surface; | |
251 mbe_surface_t *surface; | |
252 EGLConfig config; | |
253 EGLint attrib_list[2] = {EGL_NONE}; | |
254 int fmt; | |
255 int r; | |
256 | |
257 fmt = _get_img_fmt_from_xvisual(display, visual); | |
258 if(fmt == -1) | |
259 return NULL; | |
260 | |
261 r = _openvg_find_confg(fmt, width, height, &config); | |
262 if(r != 0) | |
263 return NULL; | |
264 | |
265 egl_disp = eglGetDisplay(display); | |
266 if(egl_disp == EGL_NO_DISPLAY) | |
267 return NULL; | |
268 | |
269 egl_surface = eglCreateWindowSurface(egl_disp, config, drawable, | |
270 attrib_list); | |
271 | |
272 surface = (mbe_surface_t *)malloc(sizeof(mbe_surface_t)); | |
273 if(surface == NULL) { | |
274 eglDestroySurface(egl_disp, egl_surface); | |
275 return NULL; | |
276 } | |
277 | |
278 surface->surface = egl_surface; | |
279 | |
280 return surface; | |
281 } | |
282 | |
283 #endif | |
284 | |
285 static mbe_surface_t * | |
286 mbe_image_surface_create(mb_img_fmt_t fmt, int w, int h) { | |
287 EGLSurface surface; | |
288 EGLDisplay display; | |
289 EGLConfig config; | |
290 EGLint attrib_list[2] = {EGL_NONE}; | |
291 mbe_surface_t *mbe_surface; | |
292 int r; | |
293 | |
294 | |
295 r = _openvg_find_confg(fmt, w, h, &config); | |
296 if(r != 0) | |
297 return NULL; | |
298 | |
299 display = _VG_DISPLAY(); | |
300 /* Some implementation does not support pbuffer. | |
301 * We need use some other surface to replace this one. | |
302 */ | |
303 surface = eglCreatePbufferSurface(display, config, attrib_list); | |
304 if(surface == EGL_NO_SURFACE) | |
305 return NULL; | |
306 | |
307 mbe_surface = (mbe_surface_t *)malloc(sizeof(mbe_surface_t)); | |
308 if(mbe_surface == NULL) | |
309 return NULL; | |
310 mbe_surface->surface = surface; | |
311 mbe_surface->asso_mbe = NULL; | |
312 | |
313 return mbe_surface; | |
314 } | |
315 | |
316 static void | |
317 mbe_surface_destroy(mbe_surface_t *surface) { | |
318 EGLDisplay display; | |
319 | |
320 display = _VG_DISPLAY(); | |
321 eglDestroySurface(display, surface->surface); | |
322 | |
323 if(surface->asso_mbe) | |
324 surface->asso_mbe->tgt = NULL; | |
325 | |
326 free(surface); | |
327 } | |
328 | |
329 static mbe_t * | |
330 mbe_create(mbe_surface_t *surface) { | |
331 EGLDisplay display; | |
332 EGLConfig config; | |
333 EGLContext ctx, shared; | |
334 EGLint attrib_list[2] = {EGL_NONE}; | |
335 mbe_t *canvas; | |
336 | |
337 display = _VG_DISPLAY(); | |
338 ctx = eglCreateContext(display, config, shared, attrib_list); | |
339 if(ctx == EGL_NO_CONTEXT) | |
340 return NULL; | |
341 | |
342 canvas = (mbe_t *)malloc(sizeof(mbe_t)); | |
343 if(canvas == NULL) | |
344 return NULL; | |
345 | |
346 canvas->src = NULL; | |
347 canvas->tgt = surface; | |
348 canvas->ctx = ctx; | |
349 | |
350 return canvas; | |
351 } | |
122 | 352 |
123 /* @} */ | 353 /* @} */ |
124 | 354 |
125 #endif /* __MB_GE_OPENVG_H_ */ | 355 #endif /* __MB_GE_OPENVG_H_ */ |