1 /****************************************************************************
2 *
3 * The MIT License (MIT)
4 *
5 * Copyright 2012 - 2020 Vivante Corporation, Santa Clara, California.
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * 'Software'), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject
14 * to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial
18 * portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 * IN NO EVENT SHALL VIVANTE AND/OR ITS SUPPLIERS BE LIABLE FOR ANY
24 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 *****************************************************************************/
29
30 #include <stddef.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include "vg_lite.h"
35
vg_lite_upload_path(vg_lite_path_t * path)36 vg_lite_error_t vg_lite_upload_path(vg_lite_path_t * path)
37 {
38 uint32_t bytes;
39 vg_lite_buffer_t Buf, *buffer;
40 buffer = &Buf;
41
42 /* Compute the number of bytes required for path + command buffer prefix/postfix. */
43 bytes = (8 + path->path_length + 7 + 8) & ~7;
44
45 /* Allocate GPU memory. */
46 buffer->width = bytes;
47 buffer->height = 1;
48 buffer->stride = 0;
49 buffer->format = VG_LITE_A8;
50 if (vg_lite_allocate(buffer) != VG_LITE_SUCCESS) {
51 return VG_LITE_OUT_OF_MEMORY;
52 }
53
54 /* Initialize command buffer prefix. */
55 ((uint32_t *) buffer->memory)[0] = 0x40000000 | ((path->path_length + 7) / 8);
56 ((uint32_t *) buffer->memory)[1] = 0;
57
58 /* Copy the path data. */
59 memcpy((uint32_t *) buffer->memory + 2, path->path, path->path_length);
60
61 /* Initialize command buffer postfix. */
62 ((uint32_t *) buffer->memory)[bytes / 4 - 2] = 0x70000000;
63 ((uint32_t *) buffer->memory)[bytes / 4 - 1] = 0;
64
65 /* Mark path as uploaded. */
66 path->uploaded.handle = buffer->handle;
67 path->uploaded.address = buffer->address;
68 path->uploaded.memory = buffer->memory;
69 path->uploaded.bytes = bytes;
70 path->path_changed = 0;
71 VLM_PATH_ENABLE_UPLOAD(*path); /* Implicitly enable path uploading. */
72
73 /* Return pointer to vg_lite_buffer structure. */
74 return VG_LITE_SUCCESS;
75 }
76
77 /* Path data operations. */
78 #define CDALIGN(value, by) (((value) + (by) - 1) & ~((by) - 1))
79 #define CDMIN(x, y) ((x) > (y) ? (y) : (x))
80 #define CDMAX(x, y) ((x) > (y) ? (x) : (y))
81
get_data_count(uint8_t cmd)82 static int32_t get_data_count(uint8_t cmd)
83 {
84 static int32_t count[] = {
85 0,
86 0,
87 2,
88 2,
89 2,
90 2,
91 4,
92 4,
93 6,
94 6,
95 5,
96 5,
97 5,
98 5,
99 5,
100 5,
101 5,
102 5
103 };
104
105 if (cmd > VLC_OP_LCWARC_REL) {
106 return -1;
107 }
108 else {
109 return count[cmd];
110 }
111 }
112
compute_pathbounds(float * xmin,float * ymin,float * xmax,float * ymax,float x,float y)113 static void compute_pathbounds(float *xmin, float *ymin, float *xmax, float *ymax, float x, float y)
114 {
115 if (xmin != NULL)
116 {
117 *xmin = *xmin < x ? *xmin : x;
118 }
119
120 if (xmax != NULL)
121 {
122 *xmax = *xmax > x ? *xmax : x;
123 }
124
125 if (ymin != NULL)
126 {
127 *ymin = *ymin < y ? *ymin : y;
128 }
129
130 if (ymax != NULL)
131 {
132 *ymax = *ymax > y ? *ymax : y;
133 }
134 }
135
get_data_size(vg_lite_format_t format)136 static int32_t get_data_size(vg_lite_format_t format)
137 {
138 int32_t data_size = 0;
139
140 switch (format) {
141 case VG_LITE_S8:
142 data_size = sizeof(int8_t);
143 break;
144
145 case VG_LITE_S16:
146 data_size = sizeof(int16_t);
147 break;
148
149 case VG_LITE_S32:
150 data_size = sizeof(int32_t);
151 break;
152
153 default:
154 data_size = sizeof(vg_lite_float_t);
155 break;
156 }
157
158 return data_size;
159 }
160
vg_lite_path_calc_length(uint8_t * cmd,uint32_t count,vg_lite_format_t format)161 int32_t vg_lite_path_calc_length(uint8_t *cmd, uint32_t count, vg_lite_format_t format)
162 {
163 int32_t size = 0;
164 int32_t dCount = 0;
165 uint32_t i = 0;
166 int32_t data_size = 0;
167
168 data_size = get_data_size(format);
169
170 for (i = 0; i < count; i++) {
171 size++; /* OP CODE. */
172
173 dCount = get_data_count(cmd[i]);
174 if (dCount > 0) {
175 size = CDALIGN(size, data_size);
176 size += dCount * data_size;
177 }
178 }
179
180 return size;
181 }
182
vg_lite_path_append(vg_lite_path_t * path,uint8_t * cmd,void * data,uint32_t seg_count)183 vg_lite_error_t vg_lite_path_append(vg_lite_path_t *path,
184 uint8_t *cmd,
185 void *data,
186 uint32_t seg_count)
187 {
188 vg_lite_error_t error = VG_LITE_SUCCESS;
189 uint32_t i;
190 int32_t j;
191 int32_t offset = 0;
192 int32_t dataCount = 0;
193 float *dataf = (float*) data;
194 float *pathf = NULL;
195 int32_t *data_s32 = (int32_t*) data;
196 int32_t *path_s32 = NULL;
197 int16_t *data_s16 = (int16_t*) data;
198 int16_t *path_s16 = NULL;
199 int8_t *data_s8 = (int8_t*) data;
200 int8_t *path_s8 = NULL;
201 uint8_t *pathc = NULL;
202 int32_t data_size;
203 uint8_t arc_path = 0;
204 float px = 0.0f, py = 0.0f, cx = 0.0f, cy = 0.0f;
205 int rel = 0;
206
207 if(cmd == NULL || data == NULL || path == NULL || path->path == NULL)
208 return VG_LITE_INVALID_ARGUMENT;
209
210 for(i = 0; i < seg_count; i++) {
211 if(cmd[i] > VLC_OP_LCWARC_REL)
212 return VG_LITE_INVALID_ARGUMENT;
213 }
214
215 data_size = get_data_size(path->format);
216 path->path_changed= 1;
217 pathf = (float *)path->path;
218 path_s32 = (int32_t *)path->path;
219 path_s16 = (int16_t *)path->path;
220 path_s8 = (int8_t *)path->path;
221 pathc = (uint8_t *)path->path;
222
223 /* Loop to fill path data. */
224 for (i = 0; i < seg_count; i++) {
225 *(pathc + offset) = cmd[i];
226 offset++;
227
228 dataCount = get_data_count(cmd[i]);
229 if (dataCount > 0) {
230 offset = CDALIGN(offset, data_size);
231 if ((cmd[i] > VLC_OP_CLOSE) &&
232 ((cmd[i] & 0x01) == 1)){
233 rel = 1;
234 }
235 else {
236 rel = 0;
237 }
238 if(cmd[i] < VLC_OP_SCCWARC) {
239 for (j = 0; j < dataCount / 2; j++) {
240 switch (path->format) {
241 case VG_LITE_S8:
242 path_s8 = (int8_t *)(pathc + offset);
243 path_s8[j * 2] = *data_s8++;
244 path_s8[j * 2 + 1] = *data_s8++;
245
246 if (rel) {
247 cx = px + path_s8[j * 2];
248 cy = py + path_s8[j * 2 + 1];
249 }
250 else {
251 cx = path_s8[j * 2];
252 cy = path_s8[j * 2 + 1];
253 }
254 break;
255 case VG_LITE_S16:
256 path_s16 = (int16_t *)(pathc + offset);
257 path_s16[j * 2] = *data_s16++;
258 path_s16[j * 2 + 1] = *data_s16++;
259
260 if (rel) {
261 cx = px + path_s16[j * 2];
262 cy = py + path_s16[j * 2 + 1];
263 }
264 else {
265 cx = path_s16[j * 2];
266 cy = path_s16[j * 2 + 1];
267 }
268 break;
269 case VG_LITE_S32:
270 path_s32 = (int32_t *)(pathc + offset);
271 path_s32[j * 2] = *data_s32++;
272 path_s32[j * 2 + 1] = *data_s32++;
273
274 if (rel) {
275 cx = px + path_s32[j * 2];
276 cy = py + path_s32[j * 2 + 1];
277 }
278 else {
279 cx = path_s32[j * 2];
280 cy = path_s32[j * 2 + 1];
281 }
282 break;
283 case VG_LITE_FP32:
284 pathf = (float *)(pathc + offset);
285 pathf[j * 2] = *dataf++;
286 pathf[j * 2 + 1] = *dataf++;
287
288 if (rel) {
289 cx = px + pathf[j * 2];
290 cy = py + pathf[j * 2 + 1];
291 }
292 else {
293 cx = pathf[j * 2];
294 cy = pathf[j * 2 + 1];
295 }
296 break;
297
298 default:
299 return VG_LITE_INVALID_ARGUMENT;
300 }
301
302 /* Update path bounds. */
303 path->bounding_box[0] = CDMIN(path->bounding_box[0], cx);
304 path->bounding_box[2] = CDMAX(path->bounding_box[2], cx);
305 path->bounding_box[1] = CDMIN(path->bounding_box[1], cy);
306 path->bounding_box[3] = CDMAX(path->bounding_box[3], cy);
307 }
308 }
309 else {
310 arc_path = 1;
311 pathf = (float *)(pathc + offset);
312 pathf[0] = *dataf++;
313 pathf[1] = *dataf++;
314 pathf[2] = *dataf++;
315 pathf[3] = *dataf++;
316 pathf[4] = *dataf++;
317
318 if (rel) {
319 cx = px + pathf[3];
320 cy = py + pathf[4];
321 }
322 else {
323 cx = pathf[3];
324 cy = pathf[4];
325 }
326
327 /* Update path bounds. */
328 compute_pathbounds(&path->bounding_box[0], &path->bounding_box[1], &path->bounding_box[2], &path->bounding_box[3],cx + 2 * pathf[0],cy + 2 * pathf[1]);
329 compute_pathbounds(&path->bounding_box[0], &path->bounding_box[1], &path->bounding_box[2], &path->bounding_box[3],px + 2 * pathf[1],py + 2 * pathf[1]);
330 compute_pathbounds(&path->bounding_box[0], &path->bounding_box[1], &path->bounding_box[2], &path->bounding_box[3],cx - 2 * pathf[0],cy - 2 * pathf[1]);
331 compute_pathbounds(&path->bounding_box[0], &path->bounding_box[1], &path->bounding_box[2], &path->bounding_box[3],px - 2 * pathf[1],py - 2 * pathf[1]);
332 }
333 px = cx;
334 py = cy;
335
336 offset += dataCount * data_size;
337 }
338 }
339
340 path->path_length = offset;
341
342 if(arc_path) {
343 error = vg_lite_init_arc_path(path,
344 VG_LITE_FP32,
345 path->quality,
346 path->path_length,
347 path->path,
348 path->bounding_box[0], path->bounding_box[1],
349 path->bounding_box[2], path->bounding_box[3]);
350 }
351
352 return error;
353 }
354