diff options
author | Hermet Park <hermetpark@gmail.com> | 2019-09-11 11:59:21 +0900 |
---|---|---|
committer | Hermet Park <hermetpark@gmail.com> | 2019-09-11 12:01:19 +0900 |
commit | 20abcba3b0d4222790ef5e2f41d56c7914e0ff26 (patch) | |
tree | 2b918aaff3c57622511a1dc75d54885d1029b875 | |
parent | d7006f6a09e60d4496488a24732bf4ad047e70d2 (diff) |
ector: code refactoring.
more neat and clean code, no logical changes.
-rw-r--r-- | src/lib/ector/software/ector_software_buffer.c | 99 |
1 files changed, 47 insertions, 52 deletions
diff --git a/src/lib/ector/software/ector_software_buffer.c b/src/lib/ector/software/ector_software_buffer.c index e7d288e1e1..b894f4c615 100644 --- a/src/lib/ector/software/ector_software_buffer.c +++ b/src/lib/ector/software/ector_software_buffer.c | |||
@@ -10,8 +10,6 @@ | |||
10 | 10 | ||
11 | #define MY_CLASS ECTOR_SOFTWARE_BUFFER_CLASS | 11 | #define MY_CLASS ECTOR_SOFTWARE_BUFFER_CLASS |
12 | 12 | ||
13 | #define fail(fmt, ...) do { ERR(fmt, ##__VA_ARGS__); goto on_fail; } while (0) | ||
14 | |||
15 | typedef struct _Ector_Software_Buffer_Map | 13 | typedef struct _Ector_Software_Buffer_Map |
16 | { | 14 | { |
17 | EINA_INLIST; | 15 | EINA_INLIST; |
@@ -49,24 +47,20 @@ _pixels_gry8_to_argb_convert(uint32_t *dst, const uint8_t *src, int len) | |||
49 | EOLIAN static void | 47 | EOLIAN static void |
50 | _ector_software_buffer_base_pixels_clear(Eo *obj, Ector_Software_Buffer_Base_Data *pd) | 48 | _ector_software_buffer_base_pixels_clear(Eo *obj, Ector_Software_Buffer_Base_Data *pd) |
51 | { | 49 | { |
52 | if (!pd->pixels.u8) | 50 | if (!pd->pixels.u8) return; |
53 | return; | ||
54 | 51 | ||
55 | if (pd->internal.maps) | 52 | if (pd->internal.maps) |
56 | fail("Can not call pixels_clear when the buffer is mapped."); | ||
57 | |||
58 | efl_event_callback_call(obj, ECTOR_BUFFER_EVENT_DETACHED, pd->pixels.u8); | ||
59 | if (!pd->nofree) | ||
60 | { | 53 | { |
61 | free(pd->pixels.u8); | 54 | ERR("Can not call pixels_clear when the buffer is mapped."); |
55 | return; | ||
62 | } | 56 | } |
63 | pd->pixels.u8 = NULL; | ||
64 | pd->nofree = EINA_FALSE; | ||
65 | 57 | ||
66 | return; | 58 | //TODO: Neccessary? |
59 | // efl_event_callback_call(obj, ECTOR_BUFFER_EVENT_DETACHED, pd->pixels.u8); | ||
67 | 60 | ||
68 | on_fail: | 61 | if (!pd->nofree) free(pd->pixels.u8); |
69 | return; | 62 | pd->pixels.u8 = NULL; |
63 | pd->nofree = EINA_FALSE; | ||
70 | } | 64 | } |
71 | 65 | ||
72 | EOLIAN static Eina_Bool | 66 | EOLIAN static Eina_Bool |
@@ -74,28 +68,24 @@ _ector_software_buffer_base_ector_buffer_pixels_set(Eo *obj, Ector_Software_Buff | |||
74 | void *pixels, int width, int height, int stride, | 68 | void *pixels, int width, int height, int stride, |
75 | Efl_Gfx_Colorspace cspace, Eina_Bool writable) | 69 | Efl_Gfx_Colorspace cspace, Eina_Bool writable) |
76 | { | 70 | { |
77 | unsigned pxs; | 71 | unsigned int pxs; |
78 | 72 | ||
79 | //if (pd->generic->immutable) | 73 | #if 0 |
80 | // fail("This buffer is immutable."); | 74 | if (pd->generic->immutable) |
81 | 75 | { | |
82 | if (pd->internal.maps) | 76 | ERR("This buffer is immutable."); |
83 | fail("Can not call pixels_set when the buffer is mapped."); | 77 | return EINA_FALSE; |
78 | } | ||
79 | #endif | ||
84 | 80 | ||
85 | if (cspace == EFL_GFX_COLORSPACE_ARGB8888) | 81 | if (pd->internal.maps) return EINA_FALSE; |
86 | pxs = 4; | ||
87 | else if (cspace == EFL_GFX_COLORSPACE_GRY8) | ||
88 | pxs = 1; | ||
89 | else | ||
90 | fail("Unsupported colorspace: %u", cspace); | ||
91 | 82 | ||
92 | if (((unsigned long long)(uintptr_t)pixels) & (pxs - 1)) | 83 | if (cspace == EFL_GFX_COLORSPACE_ARGB8888) pxs = 4; |
93 | fail ("Pixel data is not aligned to %u bytes!", pxs); | 84 | else if (cspace == EFL_GFX_COLORSPACE_GRY8) pxs = 1; |
85 | else return EINA_FALSE; | ||
94 | 86 | ||
95 | if (stride == 0) | 87 | if (stride == 0) stride = width * pxs; |
96 | stride = width * pxs; | 88 | else if (stride < (int)(width * pxs)) return EINA_FALSE; |
97 | else if (stride < (int)(width * pxs)) | ||
98 | fail ("Stride is less than minimum stride: provided %u bytes, minimum %u bytes!", stride, (width * pxs)); | ||
99 | 89 | ||
100 | if (pd->pixels.u8 && (pd->pixels.u8 != pixels)) | 90 | if (pd->pixels.u8 && (pd->pixels.u8 != pixels)) |
101 | _ector_software_buffer_base_pixels_clear(obj, pd); | 91 | _ector_software_buffer_base_pixels_clear(obj, pd); |
@@ -117,10 +107,8 @@ _ector_software_buffer_base_ector_buffer_pixels_set(Eo *obj, Ector_Software_Buff | |||
117 | pd->generic->cspace = cspace; | 107 | pd->generic->cspace = cspace; |
118 | pd->stride = stride; | 108 | pd->stride = stride; |
119 | pd->pixel_size = pxs; | 109 | pd->pixel_size = pxs; |
120 | return EINA_TRUE; | ||
121 | 110 | ||
122 | on_fail: | 111 | return EINA_TRUE; |
123 | return EINA_FALSE; | ||
124 | } | 112 | } |
125 | 113 | ||
126 | EOLIAN static void * | 114 | EOLIAN static void * |
@@ -136,21 +124,28 @@ _ector_software_buffer_base_ector_buffer_map(Eo *obj EINA_UNUSED, Ector_Software | |||
136 | if (!w) w = pd->generic->w; | 124 | if (!w) w = pd->generic->w; |
137 | if (!h) h = pd->generic->h; | 125 | if (!h) h = pd->generic->h; |
138 | 126 | ||
139 | if (!pd->pixels.u8 || !pd->stride) | 127 | if (!pd->pixels.u8 || !pd->stride) goto on_fail; |
140 | fail("Buffer has no pixel data yet"); | 128 | |
141 | if (((x + w) > pd->generic->w) || (y + h > pd->generic->h)) | 129 | if (((x + w) > pd->generic->w) || (y + h > pd->generic->h)) |
142 | fail("Invalid region requested: wanted %u,%u %ux%u but image is %ux%u", | 130 | { |
143 | x, y, w, h, pd->generic->w, pd->generic->h); | 131 | ERR("Invalid region requested: wanted %u,%u %ux%u but image is %ux%u", |
132 | x, y, w, h, pd->generic->w, pd->generic->h); | ||
133 | goto on_fail; | ||
134 | } | ||
144 | if ((mode & ECTOR_BUFFER_ACCESS_FLAG_WRITE) && !pd->writable) | 135 | if ((mode & ECTOR_BUFFER_ACCESS_FLAG_WRITE) && !pd->writable) |
145 | fail("Can not map a read-only buffer for writing"); | 136 | { |
137 | ERR("Can not map a read-only buffer for writing"); | ||
138 | goto on_fail; | ||
139 | } | ||
146 | 140 | ||
147 | pxs = (pd->generic->cspace == EFL_GFX_COLORSPACE_ARGB8888) ? 4 : 1; | 141 | pxs = (pd->generic->cspace == EFL_GFX_COLORSPACE_ARGB8888) ? 4 : 1; |
148 | if (cspace == EFL_GFX_COLORSPACE_ARGB8888) | 142 | if (cspace == EFL_GFX_COLORSPACE_ARGB8888) pxs_dest = 4; |
149 | pxs_dest = 4; | 143 | else if (cspace == EFL_GFX_COLORSPACE_GRY8) pxs_dest = 1; |
150 | else if (cspace == EFL_GFX_COLORSPACE_GRY8) | ||
151 | pxs_dest = 1; | ||
152 | else | 144 | else |
153 | fail("Unsupported colorspace: %u", cspace); | 145 | { |
146 | ERR("Unsupported colorspace: %u", cspace); | ||
147 | goto on_fail; | ||
148 | } | ||
154 | 149 | ||
155 | if ((mode & ECTOR_BUFFER_ACCESS_FLAG_WRITE) && | 150 | if ((mode & ECTOR_BUFFER_ACCESS_FLAG_WRITE) && |
156 | (mode & ECTOR_BUFFER_ACCESS_FLAG_COW)) | 151 | (mode & ECTOR_BUFFER_ACCESS_FLAG_COW)) |
@@ -163,8 +158,8 @@ _ector_software_buffer_base_ector_buffer_map(Eo *obj EINA_UNUSED, Ector_Software | |||
163 | } | 158 | } |
164 | } | 159 | } |
165 | 160 | ||
166 | map = calloc(1, sizeof(*map)); | 161 | map = calloc(1, sizeof(Ector_Software_Buffer_Map)); |
167 | if (!map) fail("Out of memory"); | 162 | if (!map) goto on_fail; |
168 | 163 | ||
169 | off = (pxs * x) + (pd->stride * y); | 164 | off = (pxs * x) + (pd->stride * y); |
170 | dst_stride = w * pxs_dest; | 165 | dst_stride = w * pxs_dest; |
@@ -183,7 +178,7 @@ _ector_software_buffer_base_ector_buffer_map(Eo *obj EINA_UNUSED, Ector_Software | |||
183 | map->size = w * h * pxs_dest; | 178 | map->size = w * h * pxs_dest; |
184 | map->allocated = EINA_TRUE; | 179 | map->allocated = EINA_TRUE; |
185 | map->ptr = malloc(map->size); | 180 | map->ptr = malloc(map->size); |
186 | if (!map->ptr) fail("Out of memory"); | 181 | if (!map->ptr) goto on_fail; |
187 | 182 | ||
188 | if (cspace == EFL_GFX_COLORSPACE_ARGB8888) | 183 | if (cspace == EFL_GFX_COLORSPACE_ARGB8888) |
189 | { | 184 | { |
@@ -202,7 +197,9 @@ _ector_software_buffer_base_ector_buffer_map(Eo *obj EINA_UNUSED, Ector_Software | |||
202 | map->size = w * h * pxs_dest; | 197 | map->size = w * h * pxs_dest; |
203 | map->allocated = EINA_TRUE; | 198 | map->allocated = EINA_TRUE; |
204 | map->ptr = malloc(map->size); | 199 | map->ptr = malloc(map->size); |
205 | if (!map->ptr) fail("Out of memory"); | 200 | |
201 | if (!map->ptr) goto on_fail; | ||
202 | |||
206 | for (k = 0; k < h; k++) | 203 | for (k = 0; k < h; k++) |
207 | memcpy(map->ptr + k * dst_stride, pd->pixels.u8 + x + (k + y) * pd->stride, dst_stride); | 204 | memcpy(map->ptr + k * dst_stride, pd->pixels.u8 + x + (k + y) * pd->stride, dst_stride); |
208 | } | 205 | } |
@@ -311,9 +308,7 @@ _ector_software_buffer_efl_object_destructor(Eo *obj, void *data EINA_UNUSED) | |||
311 | efl_data_unref(obj, pd->generic); | 308 | efl_data_unref(obj, pd->generic); |
312 | efl_destructor(efl_super(obj, MY_CLASS)); | 309 | efl_destructor(efl_super(obj, MY_CLASS)); |
313 | if (pd->internal.maps) | 310 | if (pd->internal.maps) |
314 | { | 311 | ERR("Pixel data is still mapped during destroy!"); |
315 | ERR("Pixel data is still mapped during destroy! Check your code!"); | ||
316 | } | ||
317 | } | 312 | } |
318 | 313 | ||
319 | #include "ector_software_buffer.eo.c" | 314 | #include "ector_software_buffer.eo.c" |