summaryrefslogtreecommitdiff
path: root/src/modules/evas/engines/gl_generic/evas_ector_gl_buffer.c
blob: 26c2c47c9b86cc2411261ba7dd7d1425db6ac5f1 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#define ECTOR_GL_BUFFER_BASE_PROTECTED

#include "evas_common_private.h"
#include "evas_gl_private.h"

#include <software/Ector_Software.h>
#include <gl/Ector_GL.h>
#include "Evas_Engine_GL_Generic.h"

#include "evas_ector_buffer.eo.h"
#include "evas_ector_gl_buffer.eo.h"
#include "evas_gl_private.h"

#define MY_CLASS EVAS_ECTOR_GL_BUFFER_CLASS

typedef struct _Ector_GL_Buffer_Map Ector_GL_Buffer_Map;
typedef struct _Evas_Ector_GL_Buffer_Data Evas_Ector_GL_Buffer_Data;

static int _map_id = 0;

struct _Ector_GL_Buffer_Map
{
   EINA_INLIST;
   void *ptr;
   unsigned int base_size; // in bytes
   unsigned int x, y, w, h;
   void *image_data, *base_data;
   int map_id;
   size_t length;
   Efl_Gfx_Colorspace cspace;
   Evas_GL_Image *im;
   Eina_Bool allocated;
   Ector_Buffer_Access_Flag mode;
};

struct _Evas_Ector_GL_Buffer_Data
{
   Evas_Public_Data *evas;
   Evas_GL_Image *glim;
   Eina_Bool alpha_only, was_render;
   Ector_GL_Buffer_Map *maps;
};

#undef ENFN
#undef ENDT
#undef ENC

#define ENFN pd->evas->engine.func
#define ENC _evas_engine_context(pd->evas)

// testing out some macros to maybe add to eina
#define EINA_INLIST_REMOVE(l,i) do { l = (__typeof__(l)) eina_inlist_remove(EINA_INLIST_GET(l), EINA_INLIST_GET(i)); } while (0)
#define EINA_INLIST_APPEND(l,i) do { l = (__typeof__(l)) eina_inlist_append(EINA_INLIST_GET(l), EINA_INLIST_GET(i)); } while (0)
#define EINA_INLIST_PREPEND(l,i) do { l = (__typeof__(l)) eina_inlist_prepend(EINA_INLIST_GET(l), EINA_INLIST_GET(i)); } while (0)

#define fail(fmt, ...) do { ERR(fmt, ##__VA_ARGS__); goto on_fail; } while (0)

#if 0
static inline void
_mapped_image_dump(Eo *buf, Evas_GL_Image *im, const char *fmt, int id)
{
   if (!im || !im->im) return;
   evas_common_save_image_to_file(im->im, eina_slstr_printf("/tmp/dump/%s_%02d_buf_%p_im_%p.png", fmt, id, buf, im),
                                  NULL, 100, 9, NULL);
}

#define MAP_DUMP(_im, _fmt) _mapped_image_dump(obj, _im, _fmt, map->map_id)
#else
#define MAP_DUMP(...)
#endif

/* FIXME: Conversion routines don't belong here */
static inline void
_pixels_argb_to_gry8_convert(uint8_t *dst, const uint32_t *src, int len)
{
   int k;
   for (k = 0; k < len; k++)
     {
        const uint32_t *s = src++;
        *dst++ = A_VAL(s);
     }
}

static inline void
_pixels_gry8_to_argb_convert(uint32_t *dst, const uint8_t *src, int len)
{
   int k;
   for (k = 0; k < len; k++)
     {
        const uint8_t s = *src++;
        *dst++ = ARGB_JOIN(s, s, s, s);
     }
}

static inline Eina_Bool
_evas_gl_image_is_fbo(Evas_GL_Image *glim)
{
   return glim && glim->tex && glim->tex->pt && glim->tex->pt->fb;
}

EOLIAN static void
_evas_ector_gl_buffer_gl_buffer_prepare(Eo *obj, Evas_Ector_GL_Buffer_Data *pd,
                                        Evas_Canvas *eo_evas,
                                        int w, int h, Efl_Gfx_Colorspace cspace,
                                        Ector_Buffer_Flag flags EINA_UNUSED)
{
   Render_Engine_GL_Generic *re;
   Evas_Engine_GL_Context *gc;
   Evas_GL_Image *im;

   // this is meant to be called only once
   EINA_SAFETY_ON_FALSE_GOTO(!pd->evas, on_fail);
   EINA_SAFETY_ON_FALSE_GOTO(!efl_finalized_get(obj), on_fail);

   if (cspace == EFL_GFX_COLORSPACE_ARGB8888)
     pd->alpha_only = EINA_FALSE;
   else if (cspace == EFL_GFX_COLORSPACE_GRY8)
     pd->alpha_only = EINA_TRUE;
   else
     fail("Unsupported colorspace: %u", cspace);

   // FIXME: we should not rely on evas canvas in the module (just evas engine)
   pd->evas = efl_data_xref(eo_evas, EVAS_CANVAS_CLASS, obj);
   re = ENC;
   gc = re->window_gl_context_get(re->software.ob);

   im = evas_gl_common_image_surface_new(gc, w, h, EINA_TRUE, EINA_FALSE);
   if (!im) fail("Failed to create GL surface!");

   pd->glim = im;
   return;

on_fail:
   if (pd->glim) evas_gl_common_image_free(pd->glim);
   pd->glim = NULL;
}

static inline void *
_image_get(Evas_Ector_GL_Buffer_Data *pd, Eina_Bool render)
{
   if (pd->maps != NULL)
     fail("Image is currently mapped!");

   if (!pd->glim || !pd->glim->tex || !pd->glim->tex->pt)
     fail("Image has no texture!");

   evas_gl_common_image_ref(pd->glim);
   if (render)
     {
        if (!pd->glim->tex->pt->fb)
          fail("Image has no FBO!");
        pd->was_render = EINA_TRUE;
     }
   return pd->glim;

on_fail:
   return NULL;
}

EOLIAN static void *
_evas_ector_gl_buffer_evas_ector_buffer_drawable_image_get(Eo *obj EINA_UNUSED,
                                                           Evas_Ector_GL_Buffer_Data *pd)
{
   return _image_get(pd, EINA_FALSE);
}

EOLIAN static void *
_evas_ector_gl_buffer_evas_ector_buffer_render_image_get(Eo *obj EINA_UNUSED,
                                                         Evas_Ector_GL_Buffer_Data *pd)
{
   return _image_get(pd, EINA_TRUE);
}

EOLIAN static Eina_Bool
_evas_ector_gl_buffer_evas_ector_buffer_engine_image_release(Eo *obj EINA_UNUSED,
                                                             Evas_Ector_GL_Buffer_Data *pd,
                                                             void *image)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(image, EINA_FALSE);
   EINA_SAFETY_ON_FALSE_RETURN_VAL(pd->glim == image, EINA_FALSE);

   if (pd->was_render)
     pd->glim = evas_gl_common_image_surface_detach(pd->glim);

   evas_gl_common_image_free(pd->glim);

   return EINA_TRUE;
}

EOLIAN static void
_evas_ector_gl_buffer_ector_buffer_size_get(Eo *obj EINA_UNUSED,
                                            Evas_Ector_GL_Buffer_Data *pd,
                                            int *w, int *h)
{
   if (w) *w = pd->glim->w;
   if (h) *h = pd->glim->h;
}

EOLIAN static Efl_Gfx_Colorspace
_evas_ector_gl_buffer_ector_buffer_cspace_get(Eo *obj EINA_UNUSED,
                                              Evas_Ector_GL_Buffer_Data *pd)
{
   if (pd->alpha_only)
     return EFL_GFX_COLORSPACE_GRY8;
   else
     return EFL_GFX_COLORSPACE_ARGB8888;
}

EOLIAN static Ector_Buffer_Flag
_evas_ector_gl_buffer_ector_buffer_flags_get(Eo *obj EINA_UNUSED,
                                             Evas_Ector_GL_Buffer_Data *pd EINA_UNUSED)
{
   return ECTOR_BUFFER_FLAG_CPU_READABLE | ECTOR_BUFFER_FLAG_DRAWABLE |
         ECTOR_BUFFER_FLAG_CPU_WRITABLE | ECTOR_BUFFER_FLAG_RENDERABLE;
}

EOLIAN static void *
_evas_ector_gl_buffer_ector_buffer_map(Eo *obj EINA_UNUSED, Evas_Ector_GL_Buffer_Data *pd,
                                       unsigned int *length,
                                       Ector_Buffer_Access_Flag mode,
                                       unsigned int x, unsigned int y, unsigned int w, unsigned int h,
                                       Efl_Gfx_Colorspace cspace, unsigned int *stride)
{
   Eina_Bool write = !!(mode & ECTOR_BUFFER_ACCESS_FLAG_WRITE);
   Ector_GL_Buffer_Map *map = NULL;
   Eina_Bool tofree = EINA_FALSE;
   Evas_GL_Image *im = NULL;
   unsigned int W, H;
   int len, err;
   uint32_t *data;
   int pxs;

   W = pd->glim->w;
   H = pd->glim->h;
   if (!w) w = W - x;
   if (!h) h = H - y;
   if ((x + w > W) || (y + h > H)) return NULL;

   if (write && _evas_gl_image_is_fbo(pd->glim))
     {
        // Can not open FBO data to write!
        im = ENFN->image_data_get(ENC, pd->glim, EINA_FALSE, &data, &err, &tofree);
        if (!im) return NULL;
     }
   else
     {
        im = ENFN->image_data_get(ENC, pd->glim, write, &data, &err, &tofree);
        if (!im) return NULL;
     }

   map = calloc(1, sizeof(*map));
   map->mode = mode;
   map->cspace = cspace;
   map->x = x;
   map->y = y;
   map->w = w;
   map->h = h;
   map->image_data = data;
   map->im = tofree ? im : NULL;

   len = W * H;
   if (cspace == EFL_GFX_COLORSPACE_GRY8)
     {
        uint8_t *data8 = malloc(len);

        if (!data8) goto on_fail;
        _pixels_argb_to_gry8_convert(data8, data, len);
        map->allocated = EINA_TRUE;
        map->base_data = data8;
        map->ptr = data8 + x + (y * W);
        pxs = 1;
     }
   else
     {
        map->allocated = EINA_FALSE;
        map->base_data = data;
        map->ptr = data + x + (y * W);
        pxs = 4;
     }

   map->map_id = ++_map_id;
   map->base_size = len * pxs;
   map->length = (W * h + w - W) * pxs;
   if (stride) *stride = W * pxs;
   if (length) *length = map->length;

   MAP_DUMP(im, "in");

   EINA_INLIST_PREPEND(pd->maps, map);
   return map->ptr;

on_fail:
   free(map);
   return NULL;
}

EOLIAN static void
_evas_ector_gl_buffer_ector_buffer_unmap(Eo *obj EINA_UNUSED, Evas_Ector_GL_Buffer_Data *pd,
                                         void *data, unsigned int length)
{
   Ector_GL_Buffer_Map *map;
   if (!data) return;

   EINA_INLIST_FOREACH(pd->maps, map)
     {
        if ((map->ptr == data) && (map->length == length))
          {
             EINA_INLIST_REMOVE(pd->maps, map);
             if (map->mode & ECTOR_BUFFER_ACCESS_FLAG_WRITE)
               {
                  Evas_GL_Image *old_glim = pd->glim;
                  int W, H;

                  W = pd->glim->w;
                  H = pd->glim->h;

                  if (map->cspace == EFL_GFX_COLORSPACE_GRY8)
                    _pixels_gry8_to_argb_convert(map->image_data, map->base_data, W * H);

                  if (map->im)
                    {
                       MAP_DUMP(map->im, "out_w_free");
                       pd->glim = evas_gl_common_image_surface_update(map->im);
                       evas_gl_common_image_free(old_glim);
                    }
                  else
                    {
                       MAP_DUMP(old_glim, "out_w_nofree");
                       pd->glim = evas_gl_common_image_surface_update(old_glim);
                    }
               }
             else
               {
                  if (map->im)
                    {
                       MAP_DUMP(map->im, "out_ro_free");
                       ENFN->image_free(ENC, map->im);
                    }
                  else
                    {
                       MAP_DUMP(pd->glim, "out_ro_nofree");
                       ENFN->image_data_put(ENC, pd->glim, map->image_data);
                    }
               }
             if (map->allocated)
               free(map->base_data);
             free(map);
             return;
          }
     }

   ERR("Tried to unmap a non-mapped region!");
}

EOLIAN static Efl_Object *
_evas_ector_gl_buffer_efl_object_finalize(Eo *obj, Evas_Ector_GL_Buffer_Data *pd)
{
   if (!pd->glim)
     {
        ERR("Buffer was not initialized properly!");
        return NULL;
     }
   return efl_finalize(efl_super(obj, MY_CLASS));
}

EOLIAN static void
_evas_ector_gl_buffer_efl_object_destructor(Eo *obj, Evas_Ector_GL_Buffer_Data *pd)
{
   evas_gl_common_image_free(pd->glim);
   efl_data_xunref(pd->evas->evas, pd->evas, obj);
   efl_destructor(efl_super(obj, MY_CLASS));
}

#include "evas_ector_gl_buffer.eo.c"