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 /************************************************************************/
33 static float _lineLength(const Point& pt1, const Point& pt2)
35 /* approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm.
36 With alpha = 1, beta = 3/8, giving results with the largest error less
37 than 7% compared to the exact value. */
38 Point diff = {pt2.x - pt1.x, pt2.y - pt1.y};
39 if (diff.x < 0) diff.x = -diff.x;
40 if (diff.y < 0) diff.y = -diff.y;
41 return (diff.x > diff.y) ? (diff.x + diff.y * 0.375f) : (diff.y + diff.x * 0.375f);
45 static void _lineSplitAt(const Line& cur, float at, Line& left, Line& right)
47 auto len = _lineLength(cur.pt1, cur.pt2);
48 auto dx = ((cur.pt2.x - cur.pt1.x) / len) * at;
49 auto dy = ((cur.pt2.y - cur.pt1.y) / len) * at;
51 left.pt2.x = left.pt1.x + dx;
52 left.pt2.y = left.pt1.y + dy;
58 static void _growOutlineContour(SwOutline& outline, uint32_t n)
60 if (outline.reservedCntrsCnt >= outline.cntrsCnt + n) return;
61 outline.reservedCntrsCnt = outline.cntrsCnt + n;
62 outline.cntrs = static_cast<uint32_t*>(realloc(outline.cntrs, outline.reservedCntrsCnt * sizeof(uint32_t)));
63 assert(outline.cntrs);
67 static void _growOutlinePoint(SwOutline& outline, uint32_t n)
69 if (outline.reservedPtsCnt >= outline.ptsCnt + n) return;
70 outline.reservedPtsCnt = outline.ptsCnt + n;
71 outline.pts = static_cast<SwPoint*>(realloc(outline.pts, outline.reservedPtsCnt * sizeof(SwPoint)));
73 outline.types = static_cast<uint8_t*>(realloc(outline.types, outline.reservedPtsCnt * sizeof(uint8_t)));
74 assert(outline.types);
78 static void _delOutline(SwOutline* outline)
82 if (outline->cntrs) free(outline->cntrs);
83 if (outline->pts) free(outline->pts);
84 if (outline->types) free(outline->types);
89 static void _outlineEnd(SwOutline& outline)
91 _growOutlineContour(outline, 1);
92 if (outline.ptsCnt > 0) {
93 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
99 static void _outlineMoveTo(SwOutline& outline, const Point* to)
103 _growOutlinePoint(outline, 1);
105 outline.pts[outline.ptsCnt] = TO_SWPOINT(to);
106 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
108 if (outline.ptsCnt > 0) {
109 _growOutlineContour(outline, 1);
110 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
118 static void _outlineLineTo(SwOutline& outline, const Point* to)
122 _growOutlinePoint(outline, 1);
124 outline.pts[outline.ptsCnt] = TO_SWPOINT(to);
125 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
130 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to)
132 assert(ctrl1 && ctrl2 && to);
134 _growOutlinePoint(outline, 3);
136 outline.pts[outline.ptsCnt] = TO_SWPOINT(ctrl1);
137 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
140 outline.pts[outline.ptsCnt] = TO_SWPOINT(ctrl2);
141 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
144 outline.pts[outline.ptsCnt] = TO_SWPOINT(to);
145 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
150 static void _outlineClose(SwOutline& outline)
154 if (outline.cntrsCnt > 0) {
155 i = outline.cntrs[outline.cntrsCnt - 1] + 1;
160 //Make sure there is at least one point in the current path
161 if (outline.ptsCnt == i) {
162 outline.opened = true;
167 _growOutlinePoint(outline, 1);
169 outline.pts[outline.ptsCnt] = outline.pts[i];
170 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
173 outline.opened = false;
177 static void _initBBox(SwBBox& bbox)
179 bbox.min.x = bbox.min.y = 0;
180 bbox.max.x = bbox.max.y = 0;
184 static bool _updateBBox(SwOutline* outline, SwBBox& bbox)
186 if (!outline) return false;
188 auto pt = outline->pts;
191 if (outline->ptsCnt <= 0) {
203 for(uint32_t i = 1; i < outline->ptsCnt; ++i, ++pt) {
204 if (xMin > pt->x) xMin = pt->x;
205 if (xMax < pt->x) xMax = pt->x;
206 if (yMin > pt->y) yMin = pt->y;
207 if (yMax < pt->y) yMax = pt->y;
209 bbox.min.x = xMin >> 6;
210 bbox.max.x = (xMax + 63) >> 6;
211 bbox.min.y = yMin >> 6;
212 bbox.max.y = (yMax + 63) >> 6;
214 if (xMax - xMin < 1 && yMax - yMin < 1) return false;
220 static bool _checkValid(const SwOutline* outline, const SwBBox& bbox, const SwSize& clip)
224 if (outline->ptsCnt == 0 || outline->cntrsCnt <= 0) return false;
227 if (bbox.min.x >= clip.w || bbox.min.y >= clip.h || bbox.max.x <= 0 || bbox.max.y <= 0) return false;
233 static void _transformOutline(SwOutline* outline, const Matrix* transform)
235 if (!transform) return;
239 for(uint32_t i = 0; i < outline->ptsCnt; ++i) {
240 auto dx = static_cast<float>(outline->pts[i].x >> 6);
241 auto dy = static_cast<float>(outline->pts[i].y >> 6);
242 auto tx = dx * transform->e11 + dy * transform->e12 + transform->e31;
243 auto ty = dx * transform->e21 + dy * transform->e22 + transform->e32;
244 auto pt = Point{tx, ty};
245 outline->pts[i] = TO_SWPOINT(&pt);
250 static void _dashLineTo(SwDashStroke& dash, const Point* to)
252 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
253 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
255 Line cur = {dash.ptCur, *to};
256 auto len = _lineLength(cur.pt1, cur.pt2);
258 if (len < dash.curLen) {
260 if (!dash.curOpGap) {
261 _outlineMoveTo(*dash.outline, &dash.ptCur);
262 _outlineLineTo(*dash.outline, to);
265 while (len > dash.curLen) {
268 _lineSplitAt(cur, dash.curLen, left, right);;
269 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
270 if (!dash.curOpGap) {
271 _outlineMoveTo(*dash.outline, &left.pt1);
272 _outlineLineTo(*dash.outline, &left.pt2);
274 dash.curLen = dash.pattern[dash.curIdx];
275 dash.curOpGap = !dash.curOpGap;
277 dash.ptCur = cur.pt1;
281 if (!dash.curOpGap) {
282 _outlineMoveTo(*dash.outline, &cur.pt1);
283 _outlineLineTo(*dash.outline, &cur.pt2);
285 if (dash.curLen < 1) {
287 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
288 dash.curLen = dash.pattern[dash.curIdx];
289 dash.curOpGap = !dash.curOpGap;
296 static void _dashCubicTo(SwDashStroke& dash, const Point* ctrl1, const Point* ctrl2, const Point* to)
298 _growOutlinePoint(*dash.outline, dash.outline->ptsCnt >> 1);
299 _growOutlineContour(*dash.outline, dash.outline->cntrsCnt >> 1);
301 Bezier cur = { dash.ptCur, *ctrl1, *ctrl2, *to};
302 auto len = bezLength(cur);
304 if (len < dash.curLen) {
306 if (!dash.curOpGap) {
307 _outlineMoveTo(*dash.outline, &dash.ptCur);
308 _outlineCubicTo(*dash.outline, ctrl1, ctrl2, to);
311 while (len > dash.curLen) {
314 bezSplitAt(cur, dash.curLen, left, right);
315 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
316 if (!dash.curOpGap) {
317 _outlineMoveTo(*dash.outline, &left.start);
318 _outlineCubicTo(*dash.outline, &left.ctrl1, &left.ctrl2, &left.end);
320 dash.curLen = dash.pattern[dash.curIdx];
321 dash.curOpGap = !dash.curOpGap;
323 dash.ptCur = right.start;
327 if (!dash.curOpGap) {
328 _outlineMoveTo(*dash.outline, &cur.start);
329 _outlineCubicTo(*dash.outline, &cur.ctrl1, &cur.ctrl2, &cur.end);
331 if (dash.curLen < 1) {
333 dash.curIdx = (dash.curIdx + 1) % dash.cnt;
334 dash.curLen = dash.pattern[dash.curIdx];
335 dash.curOpGap = !dash.curOpGap;
342 SwOutline* _genDashOutline(const Shape* sdata)
346 const PathCommand* cmds = nullptr;
347 auto cmdCnt = sdata->pathCommands(&cmds);
349 const Point* pts = nullptr;
350 auto ptsCnt = sdata->pathCoords(&pts);
352 //No actual shape data
353 if (cmdCnt == 0 || ptsCnt == 0) return nullptr;
358 dash.ptStart = {0, 0};
360 dash.curOpGap = false;
362 const float* pattern;
363 dash.cnt = sdata->strokeDash(&pattern);
364 assert(dash.cnt > 0 && pattern);
366 //Is it safe to mutual exclusive?
367 dash.pattern = const_cast<float*>(pattern);
368 dash.outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
369 assert(dash.outline);
370 dash.outline->opened = true;
373 auto outlinePtsCnt = 0;
374 auto outlineCntrsCnt = 0;
376 for (uint32_t i = 0; i < cmdCnt; ++i) {
377 switch(*(cmds + i)) {
378 case PathCommand::Close: {
382 case PathCommand::MoveTo: {
387 case PathCommand::LineTo: {
391 case PathCommand::CubicTo: {
398 ++outlinePtsCnt; //for close
399 ++outlineCntrsCnt; //for end
401 //Reserve Approximitely 20x...
402 _growOutlinePoint(*dash.outline, outlinePtsCnt * 20);
403 _growOutlineContour(*dash.outline, outlineCntrsCnt * 20);
405 while (cmdCnt-- > 0) {
407 case PathCommand::Close: {
408 _dashLineTo(dash, &dash.ptStart);
411 case PathCommand::MoveTo: {
414 dash.curLen = *dash.pattern;
415 dash.curOpGap = false;
416 dash.ptStart = dash.ptCur = *pts;
420 case PathCommand::LineTo: {
421 _dashLineTo(dash, pts);
425 case PathCommand::CubicTo: {
426 _dashCubicTo(dash, pts, pts + 1, pts + 2);
434 _outlineEnd(*dash.outline);
440 /************************************************************************/
441 /* External Class Implementation */
442 /************************************************************************/
444 bool shapeGenRle(SwShape& shape, const Shape* sdata, const SwSize& clip, const Matrix* transform)
446 if (!shapeGenOutline(shape, sdata)) return false;
448 _transformOutline(shape.outline, transform);
450 if (!_updateBBox(shape.outline, shape.bbox)) goto end;
452 if (!_checkValid(shape.outline, shape.bbox, clip)) goto end;
455 if (shape.outline->opened) return true;
457 shape.rle = rleRender(shape.outline, shape.bbox, clip);
459 if (shape.rle) return true;
464 void shapeDelOutline(SwShape& shape)
466 auto outline = shape.outline;
467 _delOutline(outline);
468 shape.outline = nullptr;
472 void shapeReset(SwShape& shape)
474 shapeDelOutline(shape);
477 _initBBox(shape.bbox);
481 bool shapeGenOutline(SwShape& shape, const Shape* sdata)
485 const PathCommand* cmds = nullptr;
486 auto cmdCnt = sdata->pathCommands(&cmds);
488 const Point* pts = nullptr;
489 auto ptsCnt = sdata->pathCoords(&pts);
491 //No actual shape data
492 if (cmdCnt == 0 || ptsCnt == 0) return false;
495 auto outlinePtsCnt = 0;
496 auto outlineCntrsCnt = 0;
498 for (uint32_t i = 0; i < cmdCnt; ++i) {
499 switch(*(cmds + i)) {
500 case PathCommand::Close: {
504 case PathCommand::MoveTo: {
509 case PathCommand::LineTo: {
513 case PathCommand::CubicTo: {
520 ++outlinePtsCnt; //for close
521 ++outlineCntrsCnt; //for end
523 auto outline = shape.outline;
524 if (!outline) outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
526 outline->opened = true;
528 _growOutlinePoint(*outline, outlinePtsCnt);
529 _growOutlineContour(*outline, outlineCntrsCnt);
532 while (cmdCnt-- > 0) {
534 case PathCommand::Close: {
535 _outlineClose(*outline);
538 case PathCommand::MoveTo: {
539 _outlineMoveTo(*outline, pts);
543 case PathCommand::LineTo: {
544 _outlineLineTo(*outline, pts);
548 case PathCommand::CubicTo: {
549 _outlineCubicTo(*outline, pts, pts + 1, pts + 2);
557 _outlineEnd(*outline);
560 //outline->flags = SwOutline::FillRule::Winding;
562 shape.outline = outline;
568 void shapeFree(SwShape& shape)
570 shapeDelOutline(shape);
575 rleFree(shape.strokeRle);
576 strokeFree(shape.stroke);
581 void shapeDelStroke(SwShape& shape)
583 if (!shape.stroke) return;
584 rleFree(shape.strokeRle);
585 shape.strokeRle = nullptr;
586 strokeFree(shape.stroke);
587 shape.stroke = nullptr;
591 void shapeResetStroke(SwShape& shape, const Shape* sdata)
593 if (!shape.stroke) shape.stroke = static_cast<SwStroke*>(calloc(1, sizeof(SwStroke)));
594 auto stroke = shape.stroke;
597 strokeReset(*stroke, sdata);
599 rleFree(shape.strokeRle);
600 shape.strokeRle = nullptr;
604 bool shapeGenStrokeRle(SwShape& shape, const Shape* sdata, const SwSize& clip)
608 SwOutline* shapeOutline = nullptr;
611 if (sdata->strokeDash(nullptr) > 0) {
612 shapeOutline = _genDashOutline(sdata);
613 if (!shapeOutline) return false;
615 //Normal Style stroke
617 if (!shape.outline) {
618 if (!shapeGenOutline(shape, sdata)) return false;
620 shapeOutline = shape.outline;
623 if (!strokeParseOutline(*shape.stroke, *shapeOutline)) return false;
625 auto strokeOutline = strokeExportOutline(*shape.stroke);
626 if (!strokeOutline) return false;
629 _updateBBox(strokeOutline, bbox);
631 if (!_checkValid(strokeOutline, bbox, clip)) return false;
633 shape.strokeRle = rleRender(strokeOutline, bbox, clip);
635 _delOutline(strokeOutline);
641 bool shapeGenFillColors(SwShape& shape, const Fill* fill, const Matrix* transform, bool ctable)
643 return fillGenColorTable(shape.fill, fill, transform, ctable);
647 void shapeResetFill(SwShape& shape)
649 if (!shape.fill) shape.fill = static_cast<SwFill*>(calloc(1, sizeof(SwFill)));
652 fillReset(shape.fill);
656 void shapeDelFill(SwShape& shape)
658 if (!shape.fill) return;
659 fillFree(shape.fill);
660 shape.fill = nullptr;
664 #endif /* _TVG_SW_SHAPE_H_ */