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 inline SwPoint TO_SWPOINT(const Point* pt)
28 return {SwCoord(pt->x * 64), SwCoord(pt->y * 64)};
32 static void _growOutlineContour(SwOutline& outline, size_t n)
36 outline.cntrs = nullptr;
38 outline.reservedCntrsCnt = 0;
41 if (outline.reservedCntrsCnt >= outline.cntrsCnt + n) return;
43 //cout << "Grow Cntrs: " << outline.reservedCntrsCnt << " -> " << outline.cntrsCnt + n << endl;;
44 outline.reservedCntrsCnt = n;
45 outline.cntrs = static_cast<size_t*>(realloc(outline.cntrs, n * sizeof(size_t)));
46 assert(outline.cntrs);
50 static void _growOutlinePoint(SwOutline& outline, size_t n)
54 outline.pts = nullptr;
56 outline.types = nullptr;
57 outline.reservedPtsCnt = 0;
62 if (outline.reservedPtsCnt >= outline.ptsCnt + n) return;
64 //cout << "Grow Pts: " << outline.reservedPtsCnt << " -> " << outline.ptsCnt + n << endl;
65 outline.reservedPtsCnt = n;
66 outline.pts = static_cast<SwPoint*>(realloc(outline.pts, n * sizeof(SwPoint)));
68 outline.types = static_cast<uint8_t*>(realloc(outline.types, n * sizeof(uint8_t)));
69 assert(outline.types);
73 static void _outlineEnd(SwOutline& outline)
75 _growOutlineContour(outline, 1);
76 if (outline.ptsCnt > 0) {
77 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
83 static void _outlineMoveTo(SwOutline& outline, const Point* to)
87 _growOutlinePoint(outline, 1);
89 outline.pts[outline.ptsCnt] = TO_SWPOINT(to);
90 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
92 if (outline.ptsCnt > 0) {
93 _growOutlineContour(outline, 1);
94 outline.cntrs[outline.cntrsCnt] = outline.ptsCnt - 1;
102 static void _outlineLineTo(SwOutline& outline, const Point* to)
106 _growOutlinePoint(outline, 1);
108 outline.pts[outline.ptsCnt] = TO_SWPOINT(to);
109 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
115 static void _outlineCubicTo(SwOutline& outline, const Point* ctrl1, const Point* ctrl2, const Point* to)
117 assert(ctrl1 && ctrl2 && to);
119 _growOutlinePoint(outline, 3);
121 outline.pts[outline.ptsCnt] = TO_SWPOINT(ctrl1);
122 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
125 outline.pts[outline.ptsCnt] = TO_SWPOINT(ctrl2);
126 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_CUBIC;
129 outline.pts[outline.ptsCnt] = TO_SWPOINT(to);
130 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
135 static bool _outlineClose(SwOutline& outline)
139 if (outline.cntrsCnt > 0) {
140 i = outline.cntrs[outline.cntrsCnt - 1] + 1;
145 //Make sure there is at least one point in the current path
146 if (outline.ptsCnt == i) return false;
149 _growOutlinePoint(outline, 1);
151 outline.pts[outline.ptsCnt] = outline.pts[i];
152 outline.types[outline.ptsCnt] = SW_CURVE_TYPE_POINT;
159 static void _initBBox(SwShape& sdata)
161 sdata.bbox.min.x = sdata.bbox.min.y = 0;
162 sdata.bbox.max.x = sdata.bbox.max.y = 0;
166 static bool _updateBBox(SwShape& sdata)
168 auto outline = sdata.outline;
171 auto pt = outline->pts;
174 if (outline->ptsCnt <= 0) {
186 for(size_t i = 1; i < outline->ptsCnt; ++i, ++pt) {
188 if (xMin > pt->x) xMin = pt->x;
189 if (xMax < pt->x) xMax = pt->x;
190 if (yMin > pt->y) yMin = pt->y;
191 if (yMax < pt->y) yMax = pt->y;
193 sdata.bbox.min.x = xMin >> 6;
194 sdata.bbox.max.x = (xMax + 63) >> 6;
195 sdata.bbox.min.y = yMin >> 6;
196 sdata.bbox.max.y = (yMax + 63) >> 6;
198 if (xMax - xMin < 1 || yMax - yMin < 1) return false;
204 void _deleteRle(SwShape& sdata)
207 if (sdata.rle->spans) free(sdata.rle->spans);
214 /************************************************************************/
215 /* External Class Implementation */
216 /************************************************************************/
218 void shapeTransformOutline(const Shape& shape, SwShape& sdata, const RenderMatrix& transform)
220 auto outline = sdata.outline;
223 for(size_t i = 0; i < outline->ptsCnt; ++i) {
224 auto dx = static_cast<float>(outline->pts[i].x >> 6);
225 auto dy = static_cast<float>(outline->pts[i].y >> 6);
226 auto tx = dx * transform.e11 + dy * transform.e12 + transform.e13;
227 auto ty = dx * transform.e21 + dy * transform.e22 + transform.e23;
228 auto pt = Point{tx + transform.e31, ty + transform.e32};
229 outline->pts[i] = TO_SWPOINT(&pt);
234 bool shapeGenRle(const Shape& shape, SwShape& sdata, const SwSize& clip)
236 if (sdata.outline->ptsCnt == 0 || sdata.outline->cntrsCnt <= 0) goto end;
237 if (!_updateBBox(sdata)) goto end;
240 if ((sdata.bbox.min.x > clip.w || sdata.bbox.min.y > clip.h) ||
241 (sdata.bbox.min.x + sdata.bbox.max.x < 0) ||
242 (sdata.bbox.min.y + sdata.bbox.max.y < 0)) goto end;
244 sdata.rle = rleRender(sdata, clip);
247 if (sdata.rle) return true;
252 void shapeDelOutline(SwShape& sdata)
254 if (!sdata.outline) return;
256 SwOutline* outline = sdata.outline;
257 if (outline->cntrs) free(outline->cntrs);
258 if (outline->pts) free(outline->pts);
259 if (outline->types) free(outline->types);
262 sdata.outline = nullptr;
266 void shapeReset(SwShape& sdata)
268 shapeDelOutline(sdata);
274 bool shapeGenOutline(const Shape& shape, SwShape& sdata)
276 const PathCommand* cmds = nullptr;
277 auto cmdCnt = shape.pathCommands(&cmds);
279 const Point* pts = nullptr;
280 auto ptsCnt = shape.pathCoords(&pts);
282 //No actual shape data
283 if (cmdCnt == 0 || ptsCnt == 0) return false;
286 auto outlinePtsCnt = 0;
287 auto outlineCntrsCnt = 0;
289 for (size_t i = 0; i < cmdCnt; ++i) {
290 switch(*(cmds + i)) {
291 case PathCommand::Close: {
295 case PathCommand::MoveTo: {
300 case PathCommand::LineTo: {
304 case PathCommand::CubicTo: {
311 ++outlinePtsCnt; //for close
312 ++outlineCntrsCnt; //for end
314 SwOutline* outline = sdata.outline;
317 outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline)));
320 cout << "Outline was already allocated? How?" << endl;
323 _growOutlinePoint(*outline, outlinePtsCnt);
324 _growOutlineContour(*outline, outlineCntrsCnt);
327 while (cmdCnt-- > 0) {
329 case PathCommand::Close: {
330 _outlineClose(*outline);
333 case PathCommand::MoveTo: {
334 _outlineMoveTo(*outline, pts);
338 case PathCommand::LineTo: {
339 _outlineLineTo(*outline, pts);
343 case PathCommand::CubicTo: {
344 _outlineCubicTo(*outline, pts, pts + 1, pts + 2);
352 _outlineEnd(*outline);
355 //outline->flags = SwOutline::FillRule::Winding;
357 sdata.outline = outline;
363 #endif /* _TVG_SW_SHAPE_H_ */