summaryrefslogtreecommitdiff
path: root/src/modules/evas/engines/gl_generic/evas_ector_gl_image_buffer.c
blob: 5f35cc8ce88b37015734a4d9170869fd0b8453e7 (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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

/* this one is read-only buffer wrapping an existing evas_gl_image */

#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_image_buffer.eo.h"
#include "evas_gl_private.h"

#define MY_CLASS EVAS_ECTOR_GL_IMAGE_BUFFER_CLASS

typedef struct _Ector_GL_Buffer_Map Ector_GL_Buffer_Map;
typedef struct _Evas_Ector_GL_Image_Buffer_Data Evas_Ector_GL_Image_Buffer_Data;

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;
   size_t length;
   Efl_Gfx_Colorspace cspace;
   Evas_GL_Image *im;
   Eina_Bool allocated, free_image;
   Ector_Buffer_Access_Flag mode;
};

struct _Evas_Ector_GL_Image_Buffer_Data
{
   Evas_Public_Data *evas;
   Evas_GL_Image *glim;
   Ector_GL_Buffer_Map *maps;
};

#define ENFN pd->evas->engine.func
#define ENDT pd->evas->engine.data.output

// 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 fail(fmt, ...) do { ERR(fmt, ##__VA_ARGS__); goto on_fail; } while (0)

/* 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);
     }
}

EOLIAN static void
_evas_ector_gl_image_buffer_evas_ector_buffer_engine_image_set(Eo *obj, Evas_Ector_GL_Image_Buffer_Data *pd,
                                                               Evas *eo_evas, void *image)
{
   Evas_GL_Image *im = image;
   Evas_Public_Data *evas;

   EINA_SAFETY_ON_FALSE_RETURN(!pd->glim);
   EINA_SAFETY_ON_NULL_RETURN(im);

   evas = efl_data_xref(eo_evas, EVAS_CANVAS_CLASS, obj);
   if (!im->tex)
     {
        Render_Engine_GL_Generic *re = pd->evas->engine.data.output;
        Evas_Engine_GL_Context *gc;

        gc = re->window_gl_context_get(re->software.ob);
        evas_gl_common_image_update(gc, im);

        if (!im->tex)
          fail("Image has no texture!");
     }

   pd->evas = evas;
   evas_gl_common_image_ref(im);
   pd->glim = im;

on_fail:
   efl_data_xunref(eo_evas, evas, obj);
   return;
}

EOLIAN static void *
_evas_ector_gl_image_buffer_evas_ector_buffer_drawable_image_get(Eo *obj EINA_UNUSED,
                                                                 Evas_Ector_GL_Image_Buffer_Data *pd)
{
   if (!pd->glim->tex)
     fail("Image has no texture!");

   evas_gl_common_image_ref(pd->glim);
   return pd->glim;

on_fail:
   return NULL;
}

EOLIAN static Eina_Bool
_evas_ector_gl_image_buffer_evas_ector_buffer_engine_image_release(Eo *obj EINA_UNUSED,
                                                                   Evas_Ector_GL_Image_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);

   evas_gl_common_image_free(pd->glim);
   return EINA_TRUE;
}

EOLIAN static Ector_Buffer_Flag
_evas_ector_gl_image_buffer_ector_buffer_flags_get(Eo *obj EINA_UNUSED,
                                                   Evas_Ector_GL_Image_Buffer_Data *pd EINA_UNUSED)
{
   return ECTOR_BUFFER_FLAG_CPU_READABLE | ECTOR_BUFFER_FLAG_DRAWABLE;
}

EOLIAN static void
_evas_ector_gl_image_buffer_ector_buffer_size_get(Eo *obj EINA_UNUSED,
                                                  Evas_Ector_GL_Image_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_image_buffer_ector_buffer_cspace_get(Eo *obj EINA_UNUSED,
                                                    Evas_Ector_GL_Image_Buffer_Data *pd EINA_UNUSED)
{
   return EFL_GFX_COLORSPACE_ARGB8888;
}

EOLIAN static void *
_evas_ector_gl_image_buffer_ector_buffer_map(Eo *obj EINA_UNUSED, Evas_Ector_GL_Image_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)
{
   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;

   if ((cspace != EFL_GFX_COLORSPACE_GRY8) && (cspace != EFL_GFX_COLORSPACE_ARGB8888))
     {
        ERR("Unsupported colorspace for map: %d", (int) cspace);
        return NULL;
     }

   if (!mode)
     {
        ERR("Invalid access mode for map (none)");
        return NULL;
     }

   if (mode & EFL_GFX_BUFFER_ACCESS_MODE_WRITE)
     {
        ERR("%s does not support write access for map", efl_class_name_get(MY_CLASS));
        return NULL;
     }

   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;

   im = ENFN->image_data_get(ENDT, pd->glim, EINA_FALSE, &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 = im;
   map->free_image = tofree;

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

        if (!data8) goto 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->base_size = len * pxs;
   map->length = (W * h + w - W) * pxs;
   if (stride) *stride = W * pxs;
   if (length) *length = map->length;

   if (!tofree)
     pd->glim = im;

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

fail:
   free(map);
   return NULL;
}

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

   EINA_INLIST_FOREACH(pd->maps, map)
     {
        if ((map->base_data == data) && (map->length == length))
          {
             EINA_INLIST_REMOVE(pd->maps, map);
             if (map->free_image)
               ENFN->image_free(ENDT, map->im);
             else
               ENFN->image_data_put(ENDT, map->im, map->image_data);
             if (map->allocated)
               free(map->base_data);
             free(map);
             return;
          }
     }

   ERR("Tried to unmap a non-mapped region: %p +%u", data, length);
}

EOLIAN static Efl_Object *
_evas_ector_gl_image_buffer_efl_object_finalize(Eo *obj, Evas_Ector_GL_Image_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_image_buffer_efl_object_destructor(Eo *obj, Evas_Ector_GL_Image_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_image_buffer.eo.c"