Mercurial > MadButterfly
comparison src/graph_engine_openvg.c @ 618:35a67a837a53 openvg
Use reference count to trace life of _ge_openvg_img_t objects
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Thu, 08 Jul 2010 13:51:47 +0800 |
parents | 7d70a811829b |
children | 7020ed3c3e37 |
comparison
equal
deleted
inserted
replaced
617:89fee368e367 | 618:35a67a837a53 |
---|---|
24 (mtx)[6] = 0; \ | 24 (mtx)[6] = 0; \ |
25 (mtx)[7] = 0; \ | 25 (mtx)[7] = 0; \ |
26 (mtx)[8] = 1; \ | 26 (mtx)[8] = 1; \ |
27 } while(0) | 27 } while(0) |
28 | 28 |
29 /*! \brief Convert mb_img_fmt_t to VGImageFormat */ | |
30 static VGImageFormat | |
31 _mb_ifmt_2_vgifmt(mb_img_fmt_t fmt) { | |
32 VGImageFormat vgifmt; | |
33 | |
34 switch(fmt) { | |
35 case MB_IFMT_ARGB32: | |
36 vgifmt = VG_sARGB_8888; | |
37 break; | |
38 | |
39 case MB_IFMT_RGB24: | |
40 vgifmt = -1; | |
41 break; | |
42 | |
43 case MB_IFMT_A8: | |
44 vgifmt = VG_A_8; | |
45 break; | |
46 | |
47 case MB_IFMT_A1: | |
48 vgifmt = -1; | |
49 break; | |
50 | |
51 case MB_IFMT_RGB16_565: | |
52 vgifmt = VG_sRGB_565; | |
53 break; | |
54 | |
55 default: | |
56 return -1; | |
57 } | |
58 | |
59 return vgifmt; | |
60 } | |
61 | |
62 /*! \brief create image object for OpenVG */ | |
63 static _ge_openvg_img_t * | |
64 _alloc_vgimage(mb_img_fmt_t fmt, int w, int h) { | |
65 VGImage vg_img; | |
66 VGImageFormat vgifmt; | |
67 _ge_openvg_img_t *ge_img; | |
68 | |
69 vgifmt = _mb_ifmt_2_vgifmt(fmt); | |
70 if(vgifmt == -1) | |
71 return NULL; | |
72 vg_img = vgCreateImage(vgifmt, w, h, | |
73 VG_IMAGE_QUALITY_NONANTIALIASED); | |
74 if(vg_img == VG_INVALID_HANDLE) | |
75 return NULL; | |
76 ge_img = O_ALLOC(_ge_openvg_img_t); | |
77 if(ge_img == NULL) { | |
78 vgDestroyImage(vg_img); | |
79 return NULL; | |
80 } | |
81 ge_img->ref = 1; | |
82 ge_img->img = vg_img; | |
83 ge_img->asso_pattern = NULL; | |
84 ge_img->asso_surface = NULL; | |
85 | |
86 return ge_img; | |
87 } | |
88 | |
89 /*! \brief Free image object for OpenVG */ | |
90 static void | |
91 _free_vgimage(_ge_openvg_img_t *ge_img) { | |
92 if(--ge_img->ref > 0) | |
93 return; | |
94 vgDestroyImage(ge_img->img); | |
95 free(ge_img); | |
96 } | |
97 | |
29 /* | 98 /* |
30 * This implementation supports only from image surface. | 99 * This implementation supports only from image surface. |
31 */ | 100 */ |
32 mbe_pattern_t * | 101 mbe_pattern_t * |
33 mbe_pattern_create_for_surface(mbe_surface_t *surface) { | 102 mbe_pattern_create_for_surface(mbe_surface_t *surface) { |
45 return NULL; | 114 return NULL; |
46 | 115 |
47 ge_img = surface->asso_img; | 116 ge_img = surface->asso_img; |
48 pattern = O_ALLOC(mbe_pattern_t); | 117 pattern = O_ALLOC(mbe_pattern_t); |
49 pattern->asso_img = ge_img; | 118 pattern->asso_img = ge_img; |
119 ge_img->ref++; /* increase reference count */ | |
50 pattern->paint = paint; | 120 pattern->paint = paint; |
51 | 121 |
52 mtx = pattern->mtx; | 122 mtx = pattern->mtx; |
53 MK_ID(mtx); | 123 MK_ID(mtx); |
54 | 124 |
164 * image. | 234 * image. |
165 */ | 235 */ |
166 if(img->fmt != MB_IFMT_ARGB32) | 236 if(img->fmt != MB_IFMT_ARGB32) |
167 return NULL; | 237 return NULL; |
168 | 238 |
169 /* Create and copy pixels into VGImage */ | |
170 vg_img = vgCreateImage(fmt, img->w, img->h, | |
171 VG_IMAGE_QUALITY_NONANTIALIASED); | |
172 if(vg_img == VG_INVALID_HANDLE) | |
173 return NULL; | |
174 vgImageSubData(vg_img, img->content, img->stride, fmt, | |
175 0, 0, img->w, img->h); | |
176 | |
177 /* Allocate objects */ | 239 /* Allocate objects */ |
178 ge_img = O_ALLOC(_ge_openvg_img_t); | 240 ge_img = _alloc_vgimage(MB_IFMT_ARGB32, img->w, img->h); |
179 pattern = O_ALLOC(mbe_pattern_t); | 241 pattern = O_ALLOC(mbe_pattern_t); |
180 paint = vgCreatePaint(); | 242 paint = vgCreatePaint(); |
181 if(ge_img == NULL || pattern == NULL || paint == VG_INVALID_HANDLE) | 243 if(ge_img == NULL || pattern == NULL || paint == VG_INVALID_HANDLE) |
182 goto err; | 244 goto err; |
183 | 245 |
184 | 246 /* Create and copy pixels into VGImage */ |
185 /* Initialize objects */ | 247 vg_img = ge_img->img; |
186 ge_img->img = vg_img; | 248 vgImageSubData(vg_img, img->content, img->stride, fmt, |
187 ge_img->asso_pattern = NULL; | 249 0, 0, img->w, img->h); |
188 ge_img->asso_surface = NULL; | 250 |
189 | |
190 pattern->paint = paint; | 251 pattern->paint = paint; |
191 pattern->asso_img = ge_img; | 252 pattern->asso_img = ge_img; |
192 | 253 |
193 return pattern; | 254 return pattern; |
194 | 255 |
195 err: | 256 err: |
196 if(ge_img) free(ge_img); | 257 if(ge_img) _free_vgimage(ge_img); |
197 if(pattern) free(pattern); | 258 if(pattern) free(pattern); |
198 if(paint != VG_INVALID_HANDLE) vgDestroyPaint(paint); | 259 if(paint != VG_INVALID_HANDLE) vgDestroyPaint(paint); |
199 vgDestroyImage(vg_img); | 260 vgDestroyImage(vg_img); |
200 return NULL; | 261 return NULL; |
262 } | |
263 | |
264 void | |
265 mbe_pattern_destroy(mbe_pattern_t *ptn) { | |
266 if(ptn->asso_img) { | |
267 } | |
201 } | 268 } |
202 | 269 |
203 void | 270 void |
204 mbe_pattern_set_matrix(mbe_pattern_t *ptn, co_aix *mtx) { | 271 mbe_pattern_set_matrix(mbe_pattern_t *ptn, co_aix *mtx) { |
205 co_aix rev[6]; | 272 co_aix rev[6]; |
441 mbe_image_surface_create(mb_img_fmt_t fmt, int w, int h) { | 508 mbe_image_surface_create(mb_img_fmt_t fmt, int w, int h) { |
442 EGLSurface surface; | 509 EGLSurface surface; |
443 EGLDisplay display; | 510 EGLDisplay display; |
444 EGLConfig config; | 511 EGLConfig config; |
445 EGLint attrib_list[5]; | 512 EGLint attrib_list[5]; |
513 _ge_openvg_img_t *ge_img; | |
446 mbe_surface_t *mbe_surface; | 514 mbe_surface_t *mbe_surface; |
447 int r; | 515 int r; |
448 | 516 |
449 | 517 |
450 r = _openvg_find_config(fmt, w, h, &config); | 518 r = _openvg_find_config(fmt, w, h, &config); |
451 if(r != 0) | 519 if(r != 0) |
452 return NULL; | 520 return NULL; |
453 | 521 |
522 ge_img = _alloc_vgimage(fmt, w, h); | |
523 if(ge_img == NULL) | |
524 return NULL; | |
525 | |
454 display = _VG_DISPLAY(); | 526 display = _VG_DISPLAY(); |
455 attrib_list[0] = EGL_WIDTH; | 527 attrib_list[0] = EGL_WIDTH; |
456 attrib_list[1] = w; | 528 attrib_list[1] = w; |
457 attrib_list[2] = EGL_HEIGHT; | 529 attrib_list[2] = EGL_HEIGHT; |
458 attrib_list[3] = h; | 530 attrib_list[3] = h; |
459 attrib_list[4] = EGL_NONE; | 531 attrib_list[4] = EGL_NONE; |
460 /* Some implementation does not support pbuffer. | 532 /* Some implementation does not support pbuffer. |
461 * We need use some other surface to replace this one. | 533 * We need use some other surface to replace this one. |
462 */ | 534 */ |
463 surface = eglCreatePbufferSurface(display, config, attrib_list); | 535 surface = eglCreatePbufferFromClientBuffer(display, EGL_OPENVG_IMAGE, |
464 if(surface == EGL_NO_SURFACE) | 536 (EGLClientBuffer)ge_img->img, |
465 return NULL; | 537 config, attrib_list); |
538 if(surface == EGL_NO_SURFACE) { | |
539 _free_vgimage(ge_img); | |
540 return NULL; | |
541 } | |
466 | 542 |
467 mbe_surface = O_ALLOC(mbe_surface_t); | 543 mbe_surface = O_ALLOC(mbe_surface_t); |
468 if(mbe_surface == NULL) { | 544 if(mbe_surface == NULL) { |
545 _free_vgimage(ge_img); | |
469 eglDestroySurface(display, surface); | 546 eglDestroySurface(display, surface); |
470 return NULL; | 547 return NULL; |
471 } | 548 } |
472 mbe_surface->surface = surface; | 549 mbe_surface->surface = surface; |
473 mbe_surface->asso_mbe = NULL; | 550 mbe_surface->asso_mbe = NULL; |
551 mbe_surface->asso_img = ge_img; | |
474 mbe_surface->w = w; | 552 mbe_surface->w = w; |
475 mbe_surface->h = h; | 553 mbe_surface->h = h; |
476 | 554 |
477 return mbe_surface; | 555 return mbe_surface; |
556 } | |
557 | |
558 void | |
559 mbe_surface_destroy(mbe_surface_t *surface) { | |
560 EGLDisplay display; | |
561 | |
562 display = _VG_DISPLAY(); | |
563 eglDestroySurface(display, surface->surface); | |
564 | |
565 if(surface->asso_mbe) | |
566 surface->asso_mbe->tgt = NULL; | |
567 | |
568 if(surface->asso_img) { | |
569 surface->asso_img->asso_surface = NULL; | |
570 _free_vgimage(surface->asso_img); | |
571 } | |
572 | |
573 free(surface); | |
478 } | 574 } |
479 | 575 |
480 mbe_t * | 576 mbe_t * |
481 mbe_create(mbe_surface_t *surface) { | 577 mbe_create(mbe_surface_t *surface) { |
482 EGLDisplay display; | 578 EGLDisplay display; |