microvg  7.0.0
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, jfloat x3, jfloat y3) {
143  uint32_t *data = (uint32_t *)(path + offset);
144  *data = VG_PATH_convert_path_command(cmd);
145  ++data;
146  *data = JFLOAT_TO_UINT32_t(x1);
147  ++data;
148  *data = JFLOAT_TO_UINT32_t(y1);
149  ++data;
150  *data = JFLOAT_TO_UINT32_t(x2);
151  ++data;
152  *data = JFLOAT_TO_UINT32_t(y2);
153  ++data;
154  *data = JFLOAT_TO_UINT32_t(x3);
155  ++data;
156  *data = JFLOAT_TO_UINT32_t(y3);
157  return (uint32_t)7 * sizeof(uint32_t);
158 }
159 
160 // -----------------------------------------------------------------------------
161 // LLVG_PATH_impl.h functions
162 // -----------------------------------------------------------------------------
163 
164 // See the header file for the function documentation
165 jint LLVG_PATH_IMPL_initializePath(jbyte *jpath, jint length) {
166  VG_PATH_HEADER_t *path = (VG_PATH_HEADER_t *)jpath;
167  uint32_t header_size = VG_PATH_get_path_header_size();
168  jint ret = LLVG_SUCCESS;
169 
170  if (length >= header_size) {
171  path->data_size = 0;
172  path->format = VG_PATH_get_path_encoder_format();
173  } else {
174  // the given byte array is too small
175  ret = header_size;
176  }
177 
178  return ret;
179 }
180 
181 // See the header file for the function documentation
182 jint LLVG_PATH_IMPL_appendPathCommand1(jbyte *jpath, jint length, jint cmd, jfloat x, jfloat y) {
183  VG_PATH_HEADER_t *path = (VG_PATH_HEADER_t *)jpath;
184  jint ret = LLVG_SUCCESS;
185 
186  int32_t index = _extend_path(path, length, cmd, 2);
187  if (index > 0) {
188  (void)VG_PATH_append_path_command1((jbyte *)path, (uint32_t)index, cmd, x, y);
189  } else {
190  // too small buffer, ret is the required extra size * -1
191  ret = -index;
192  }
193 
194  return ret;
195 }
196 
197 // See the header file for the function documentation
198 jint LLVG_PATH_IMPL_appendPathCommand2(jbyte *jpath, jint length, jint cmd, jfloat x1, jfloat y1, jfloat x2,
199  jfloat y2) {
200  VG_PATH_HEADER_t *path = (VG_PATH_HEADER_t *)jpath;
201  jint ret = LLVG_SUCCESS;
202 
203  if (LLVG_PATH_CMD_CLOSE == cmd) {
204  // parameters are path's bounds
205  ret = _close_path(path, length, x1, y1, x2, y2);
206  } else {
207  int32_t index = _extend_path(path, length, cmd, 4);
208  if (index > 0) {
209  (void)VG_PATH_append_path_command2((jbyte *)path, (uint32_t)index, cmd, x1, y1, x2, y2);
210  } else {
211  // too small buffer, ret is the required extra size * -1
212  ret = -index;
213  }
214  }
215 
216  return ret;
217 }
218 
219 // See the header file for the function documentation
220 jint LLVG_PATH_IMPL_appendPathCommand3(jbyte *jpath, jint length, jint cmd, jfloat x1, jfloat y1, jfloat x2, jfloat y2,
221  jfloat x3, jfloat y3) {
222  VG_PATH_HEADER_t *path = (VG_PATH_HEADER_t *)jpath;
223  jint ret = LLVG_SUCCESS;
224 
225  int32_t index = _extend_path(path, length, cmd, 6);
226  if (index > 0) {
227  (void)VG_PATH_append_path_command3((jbyte *)path, (uint32_t)index, cmd, x1, y1, x2, y2, x3, y3);
228  } else {
229  // too small buffer, ret is the required extra size * -1
230  ret = -index;
231  }
232 
233  return ret;
234 }
235 
236 // See the header file for the function documentation
237 void LLVG_PATH_IMPL_reopenPath(jbyte *jpath) {
238  VG_PATH_HEADER_t *path = (VG_PATH_HEADER_t *)jpath;
239  path->data_size -= VG_PATH_get_path_command_size(LLVG_PATH_CMD_CLOSE, 0);
240 }
241 
242 // -----------------------------------------------------------------------------
243 // EOF
244 // -----------------------------------------------------------------------------
245 
246 #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.