microvg  6.0.1
microvg
LLVG_PATH_impl_single.c
Go to the documentation of this file.
1 /*
2  * C
3  *
4  * Copyright 2022-2024 MicroEJ Corp. All rights reserved.
5  * Use of this source code is governed by a BSD-style license that can be found with this software.
6  */
7 
21 #include "vg_configuration.h"
22 
23 #if defined(VG_FEATURE_PATH) && defined(VG_FEATURE_PATH_SINGLE_ARRAY) && \
24  (VG_FEATURE_PATH == VG_FEATURE_PATH_SINGLE_ARRAY)
25 
26 // -----------------------------------------------------------------------------
27 // Includes
28 // -----------------------------------------------------------------------------
29 
30 #include <math.h>
31 #include <string.h>
32 
33 #include <LLVG_PATH_impl.h>
34 
35 #include "vg_path.h"
36 #include "vg_helper.h"
37 #include "bsp_util.h"
38 
39 // -----------------------------------------------------------------------------
40 // Private functions
41 // -----------------------------------------------------------------------------
42 
43 /*
44  * @brief Extends the path to be able to store the command and its parameters.
45  *
46  * @return the offset in path buffer where the command will be stored. If the path
47  * buffer is not large enough to contain the requested command, returns a negative
48  * number corresponding to size the buffer must be enlarged for this command.
49  */
50 static int32_t _extend_path(VG_PATH_HEADER_t *path, jint length, jint cmd, uint32_t nb_fields) {
51  uint32_t index = VG_PATH_get_path_header_size() + path->data_size;
52  uint32_t extra_size = VG_PATH_get_path_command_size(cmd, nb_fields);
53  int32_t ret;
54 
55  if (length >= (index + extra_size)) {
56  path->data_size += extra_size;
57 
58  // return next free space (return a positive value)
59  ret = index;
60  } else {
61  // too small buffer, ret is the required extra size
62  // (return a negative value)
63  ret = -extra_size;
64  }
65 
66  return ret;
67 }
68 
69 static int32_t _close_path(VG_PATH_HEADER_t *path, jint length, jfloat x1, jfloat y1, jfloat x2, jfloat y2) {
70  int32_t index = _extend_path(path, length, LLVG_PATH_CMD_CLOSE, 0);
71  int32_t ret = LLVG_SUCCESS;
72  if (index > 0) {
73  // finalizes the path by storing the path's bounds
74  path->bounds_xmin = x1;
75  path->bounds_xmax = x2;
76  path->bounds_ymin = y1;
77  path->bounds_ymax = y2;
78  (void)VG_PATH_append_path_command0((jbyte *)path, (uint32_t)index, LLVG_PATH_CMD_CLOSE);
79  } else {
80  // too small buffer, ret is the required extra size * -1
81  ret = -index;
82  }
83  return ret;
84 }
85 
86 // -----------------------------------------------------------------------------
87 // Specific path format functions [optional]: weak functions
88 // -----------------------------------------------------------------------------
89 
90 // See the header file for the function documentation
91 BSP_DECLARE_WEAK_FCNT void VG_PATH_initialize(void) {
92  // nothing to do
93 }
94 
95 // See the header file for the function documentation
96 BSP_DECLARE_WEAK_FCNT uint32_t VG_PATH_get_path_header_size(void) {
97  return sizeof(VG_PATH_HEADER_t);
98 }
99 
100 // See the header file for the function documentation
101 BSP_DECLARE_WEAK_FCNT uint32_t VG_PATH_get_path_command_size(jint command, uint32_t nbParams) {
102  (void)command;
103  return (nbParams + (uint32_t)1 /* command */) * sizeof(uint32_t);
104 }
105 
106 // See the header file for the function documentation
107 BSP_DECLARE_WEAK_FCNT uint32_t VG_PATH_append_path_command0(jbyte *path, jint offset, jint cmd) {
108  uint32_t *data = (uint32_t *)(path + offset);
109  *data = VG_PATH_convert_path_command(cmd);
110  return sizeof(uint32_t);
111 }
112 
113 // See the header file for the function documentation
114 BSP_DECLARE_WEAK_FCNT uint32_t VG_PATH_append_path_command1(jbyte *path, jint offset, jint cmd, jfloat x, jfloat y) {
115  uint32_t *data = (uint32_t *)(path + offset);
116  *data = VG_PATH_convert_path_command(cmd);
117  ++data;
118  *data = JFLOAT_TO_UINT32_t(x);
119  ++data;
120  *data = JFLOAT_TO_UINT32_t(y);
121  return (uint32_t)3 * sizeof(uint32_t);
122 }
123 
124 // See the header file for the function documentation
125 BSP_DECLARE_WEAK_FCNT uint32_t VG_PATH_append_path_command2(jbyte *path, jint offset, jint cmd, jfloat x1, jfloat y1,
126  jfloat x2, jfloat y2) {
127  uint32_t *data = (uint32_t *)(path + offset);
128  *data = VG_PATH_convert_path_command(cmd);
129  ++data;
130  *data = JFLOAT_TO_UINT32_t(x1);
131  ++data;
132  *data = JFLOAT_TO_UINT32_t(y1);
133  ++data;
134  *data = JFLOAT_TO_UINT32_t(x2);
135  ++data;
136  *data = JFLOAT_TO_UINT32_t(y2);
137  return (uint32_t)5 * sizeof(uint32_t);
138 }
139 
140 // See the header file for the function documentation
141 BSP_DECLARE_WEAK_FCNT uint32_t VG_PATH_append_path_command3(jbyte *path, jint offset, jint cmd, jfloat x1, jfloat y1,
142  jfloat x2, jfloat y2,
143  jfloat x3, jfloat y3) {
144  uint32_t *data = (uint32_t *)(path + offset);
145  *data = VG_PATH_convert_path_command(cmd);
146  ++data;
147  *data = JFLOAT_TO_UINT32_t(x1);
148  ++data;
149  *data = JFLOAT_TO_UINT32_t(y1);
150  ++data;
151  *data = JFLOAT_TO_UINT32_t(x2);
152  ++data;
153  *data = JFLOAT_TO_UINT32_t(y2);
154  ++data;
155  *data = JFLOAT_TO_UINT32_t(x3);
156  ++data;
157  *data = JFLOAT_TO_UINT32_t(y3);
158  return (uint32_t)7 * sizeof(uint32_t);
159 }
160 
161 // -----------------------------------------------------------------------------
162 // LLVG_PATH_impl.h functions
163 // -----------------------------------------------------------------------------
164 
165 // See the header file for the function documentation
166 jint LLVG_PATH_IMPL_initializePath(jbyte *jpath, jint length) {
167  VG_PATH_HEADER_t *path = (VG_PATH_HEADER_t *)jpath;
168  uint32_t header_size = VG_PATH_get_path_header_size();
169  jint ret = LLVG_SUCCESS;
170 
171  if (length >= header_size) {
172  path->data_size = 0;
173  path->format = VG_PATH_get_path_encoder_format();
174  } else {
175  // the given byte array is too small
176  ret = header_size;
177  }
178 
179  return ret;
180 }
181 
182 // See the header file for the function documentation
183 jint LLVG_PATH_IMPL_appendPathCommand1(jbyte *jpath, jint length, jint cmd, jfloat x, jfloat y) {
184  VG_PATH_HEADER_t *path = (VG_PATH_HEADER_t *)jpath;
185  jint ret = LLVG_SUCCESS;
186 
187  int32_t index = _extend_path(path, length, cmd, 2);
188  if (index > 0) {
189  (void)VG_PATH_append_path_command1((jbyte *)path, (uint32_t)index, cmd, x, y);
190  } else {
191  // too small buffer, ret is the required extra size * -1
192  ret = -index;
193  }
194 
195  return ret;
196 }
197 
198 // See the header file for the function documentation
199 jint LLVG_PATH_IMPL_appendPathCommand2(jbyte *jpath, jint length, jint cmd, jfloat x1, jfloat y1, jfloat x2,
200  jfloat y2) {
201  VG_PATH_HEADER_t *path = (VG_PATH_HEADER_t *)jpath;
202  jint ret = LLVG_SUCCESS;
203 
204  if (LLVG_PATH_CMD_CLOSE == cmd) {
205  // parameters are path's bounds
206  ret = _close_path(path, length, x1, y1, x2, y2);
207  } else {
208  int32_t index = _extend_path(path, length, cmd, 4);
209  if (index > 0) {
210  (void)VG_PATH_append_path_command2((jbyte *)path, (uint32_t)index, cmd, x1, y1, x2, y2);
211  } else {
212  // too small buffer, ret is the required extra size * -1
213  ret = -index;
214  }
215  }
216 
217  return ret;
218 }
219 
220 // See the header file for the function documentation
221 jint LLVG_PATH_IMPL_appendPathCommand3(jbyte *jpath, jint length, jint cmd, jfloat x1, jfloat y1, jfloat x2,
222  jfloat y2, jfloat x3, jfloat y3) {
223  VG_PATH_HEADER_t *path = (VG_PATH_HEADER_t *)jpath;
224  jint ret = LLVG_SUCCESS;
225 
226  int32_t index = _extend_path(path, length, cmd, 6);
227  if (index > 0) {
228  (void)VG_PATH_append_path_command3((jbyte *)path, (uint32_t)index, cmd, x1, y1, x2, y2, x3, y3);
229  } else {
230  // too small buffer, ret is the required extra size * -1
231  ret = -index;
232  }
233 
234  return ret;
235 }
236 
237 // See the header file for the function documentation
238 void LLVG_PATH_IMPL_reopenPath(jbyte *jpath) {
239  VG_PATH_HEADER_t *path = (VG_PATH_HEADER_t *)jpath;
240  path->data_size -= VG_PATH_get_path_command_size(LLVG_PATH_CMD_CLOSE, 0);
241 }
242 
243 // -----------------------------------------------------------------------------
244 // EOF
245 // -----------------------------------------------------------------------------
246 
247 #endif // VG_FEATURE_PATH
MicroEJ MicroVG library low level API: enable some features according to the hardware capacities.
MicroEJ MicroVG library low level API: helper to implement library natives methods.
MicroEJ MicroVG library low level API: implementation of Path.