2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef _TVG_SW_SHAPE_H_
18 #define _TVG_SW_SHAPE_H_
20 #include "tvgSwCommon.h"
22 /************************************************************************/
23 /* Internal Class Implementation */
24 /************************************************************************/
26 static void _growOutlineContour(SwOutline& outline, uint32_t n)
30 outline.cntrs = nullptr;
32 outline.reservedCntrsCnt = 0;
35 if (outline.reservedCntrsCnt >= outline.cntrsCnt + n) return;
37 //cout << "Grow Cntrs: " << outline.reservedCntrsCnt << " -> " << outline.cntrsCnt + n << endl;;
38 outline.reservedCntrsCnt = n;
39 outline.cntrs = static_cast<uint32_t*>(realloc(outline.cntrs, n * sizeof(uint32_t)));
40 assert(outline.cntrs);
44 static void _growOutlinePoint(SwOutline& outline, uint32_t n)
48 outline.pts = nullptr;
50 outline.types = nullptr;
51 outline.reservedPtsCnt = 0;
56 if (outline.reservedPtsCnt >= outline.ptsCnt + n) return;
58 //cout << "Grow Pts: " << outline.reservedPtsCnt << " -> " << outline.ptsCnt + n << endl;
59 outline.reservedPtsCnt = n;
60 outline.pts = static_cast<SwPoint*>(realloc(outline.pts, n * sizeof(SwPoint)));
62 outline.types = static_cast<uint8_t*>(realloc(outline.types, n * sizeof(uint8_t)));
63 assert(outline.types);
67 static void _outlineEnd(SwOutline& outline)
69 _growOutlineContour(outline, 1);
70 if (outline.ptsCnt > 0) {
71 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
77 static void _outlineMoveTo(SwOutline& outline, const Point* to)
81 _growOutlinePoint(outline, 1);
83 outline.pts[outline.ptsCnt] = TO_SWPOINT(to);
84 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
86 if (outline.ptsCnt > 0) {
87 _growOutlineContour(outline, 1);
88 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
96 static void _outlineLineTo(SwOutline& outline, const Point* to)
100 _growOutlinePoint(outline, 1);
102 outline.pts[outline.ptsCnt] = TO_SWPOINT(to);
103 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
109 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to)
111 assert(ctrl1 && ctrl2 && to);
113 _growOutlinePoint(outline, 3);
115 outline.pts[outline.ptsCnt] = TO_SWPOINT(ctrl1);
116 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
119 outline.pts[outline.ptsCnt] = TO_SWPOINT(ctrl2);
120 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
123 outline.pts[outline.ptsCnt] = TO_SWPOINT(to);
124 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
129 static void _outlineClose(SwOutline& outline)
133 if (outline.cntrsCnt > 0) {
134 i = outline.cntrs[outline.cntrsCnt - 1] + 1;
139 //Make sure there is at least one point in the current path
140 if (outline.ptsCnt == i) {
141 outline.opened = true;
146 _growOutlinePoint(outline, 1);
148 outline.pts[outline.ptsCnt] = outline.pts[i];
149 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
152 outline.opened = false;
156 static void _initBBox(SwShape& sdata)
158 sdata.bbox.min.x = sdata.bbox.min.y = 0;
159 sdata.bbox.max.x = sdata.bbox.max.y = 0;
163 static bool _updateBBox(SwShape& sdata)
165 auto outline = sdata.outline;
168 auto pt = outline->pts;
171 if (outline->ptsCnt <= 0) {
183 for(uint32_t i = 1; i < outline->ptsCnt; ++i, ++pt) {
185 if (xMin > pt->x) xMin = pt->x;
186 if (xMax < pt->x) xMax = pt->x;
187 if (yMin > pt->y) yMin = pt->y;
188 if (yMax < pt->y) yMax = pt->y;
190 sdata.bbox.min.x = xMin >> 6;
191 sdata.bbox.max.x = (xMax + 63) >> 6;
192 sdata.bbox.min.y = yMin >> 6;
193 sdata.bbox.max.y = (yMax + 63) >> 6;
195 if (xMax - xMin < 1 || yMax - yMin < 1) return false;
201 static bool _checkValid(SwShape& sdata, const SwSize& clip)
203 assert(sdata.outline);
205 if (sdata.outline->ptsCnt == 0 || sdata.outline->cntrsCnt <= 0) return false;
208 if ((sdata.bbox.min.x > clip.w || sdata.bbox.min.y > clip.h) ||
209 (sdata.bbox.min.x + sdata.bbox.max.x < 0) ||
210 (sdata.bbox.min.y + sdata.bbox.max.y < 0)) return false;
216 static void _deleteRle(SwShape& sdata)
218 if (!sdata.rle) return;
219 if (sdata.rle->spans) free(sdata.rle->spans);
226 /************************************************************************/
227 /* External Class Implementation */
228 /************************************************************************/
230 void shapeTransformOutline(const Shape& shape, SwShape& sdata, const RenderTransform& transform)
232 auto outline = sdata.outline;
235 for(uint32_t i = 0; i < outline->ptsCnt; ++i) {
236 auto dx = static_cast<float>(outline->pts[i].x >> 6);
237 auto dy = static_cast<float>(outline->pts[i].y >> 6);
238 auto tx = dx * transform.e11 + dy * transform.e12 + transform.e13;
239 auto ty = dx * transform.e21 + dy * transform.e22 + transform.e23;
240 auto pt = Point{tx + transform.e31, ty + transform.e32};
241 outline->pts[i] = TO_SWPOINT(&pt);
246 bool shapeGenRle(const Shape& shape, SwShape& sdata, const SwSize& clip)
248 if (!_updateBBox(sdata)) goto end;
249 if (!_checkValid(sdata, clip)) goto end;
251 sdata.rle = rleRender(sdata, clip);
254 if (sdata.rle) return true;
259 void shapeDelOutline(SwShape& sdata)
261 auto outline = sdata.outline;
262 if (!outline) return;
264 if (outline->cntrs) free(outline->cntrs);
265 if (outline->pts) free(outline->pts);
266 if (outline->types) free(outline->types);
268 sdata.outline = nullptr;
272 void shapeReset(SwShape& sdata)
274 shapeDelOutline(sdata);
280 bool shapeGenOutline(const Shape& shape, SwShape& sdata)
282 const PathCommand* cmds = nullptr;
283 auto cmdCnt = shape.pathCommands(&cmds);
285 const Point* pts = nullptr;
286 auto ptsCnt = shape.pathCoords(&pts);
288 //No actual shape data
289 if (cmdCnt == 0 || ptsCnt == 0) return false;
292 auto outlinePtsCnt = 0;
293 auto outlineCntrsCnt = 0;
295 for (uint32_t i = 0; i < cmdCnt; ++i) {
296 switch(*(cmds + i)) {
297 case PathCommand::Close: {
301 case PathCommand::MoveTo: {
306 case PathCommand::LineTo: {
310 case PathCommand::CubicTo: {
317 ++outlinePtsCnt; //for close
318 ++outlineCntrsCnt; //for end
320 auto outline = sdata.outline;
321 if (!outline) outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
324 _growOutlinePoint(*outline, outlinePtsCnt);
325 _growOutlineContour(*outline, outlineCntrsCnt);
328 while (cmdCnt-- > 0) {
330 case PathCommand::Close: {
331 _outlineClose(*outline);
334 case PathCommand::MoveTo: {
335 _outlineMoveTo(*outline, pts);
339 case PathCommand::LineTo: {
340 _outlineLineTo(*outline, pts);
344 case PathCommand::CubicTo: {
345 _outlineCubicTo(*outline, pts, pts + 1, pts + 2);
353 _outlineEnd(*outline);
356 //outline->flags = SwOutline::FillRule::Winding;
358 sdata.outline = outline;
364 void shapeFree(SwShape* sdata)
368 shapeDelOutline(*sdata);
370 strokeFree(sdata->stroke);
375 void shapeResetStroke(const Shape& shape, SwShape& sdata)
377 if (!sdata.stroke) sdata.stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
378 auto stroke = sdata.stroke;
381 strokeReset(*stroke, shape.strokeWidth(), shape.strokeCap(), shape.strokeJoin());
385 bool shapeGenStrokeRle(const Shape& shape, SwShape& sdata, const SwSize& clip)
387 if (!sdata.outline) {
388 if (!shapeGenOutline(shape, sdata)) return false;
391 if (!_checkValid(sdata, clip)) return false;
393 if (!strokeParseOutline(*sdata.stroke, *sdata.outline)) return false;
398 #endif /* _TVG_SW_SHAPE_H_ */