Flutter Engine
The Flutter Engine
SkPatchUtils.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
9
17#include "include/core/SkSize.h"
26#include "src/base/SkVx.h"
29#include "src/core/SkGeometry.h"
30
31#include <algorithm>
32#include <cstdint>
33#include <cstring>
34
35namespace {
36 enum CubicCtrlPts {
37 kTopP0_CubicCtrlPts = 0,
38 kTopP1_CubicCtrlPts = 1,
39 kTopP2_CubicCtrlPts = 2,
40 kTopP3_CubicCtrlPts = 3,
41
42 kRightP0_CubicCtrlPts = 3,
43 kRightP1_CubicCtrlPts = 4,
44 kRightP2_CubicCtrlPts = 5,
45 kRightP3_CubicCtrlPts = 6,
46
47 kBottomP0_CubicCtrlPts = 9,
48 kBottomP1_CubicCtrlPts = 8,
49 kBottomP2_CubicCtrlPts = 7,
50 kBottomP3_CubicCtrlPts = 6,
51
52 kLeftP0_CubicCtrlPts = 0,
53 kLeftP1_CubicCtrlPts = 11,
54 kLeftP2_CubicCtrlPts = 10,
55 kLeftP3_CubicCtrlPts = 9,
56 };
57
58 // Enum for corner also clockwise.
59 enum Corner {
60 kTopLeft_Corner = 0,
61 kTopRight_Corner,
62 kBottomRight_Corner,
63 kBottomLeft_Corner
64 };
65} // namespace
66
67/**
68 * Evaluator to sample the values of a cubic bezier using forward differences.
69 * Forward differences is a method for evaluating a nth degree polynomial at a uniform step by only
70 * adding precalculated values.
71 * For a linear example we have the function f(t) = m*t+b, then the value of that function at t+h
72 * would be f(t+h) = m*(t+h)+b. If we want to know the uniform step that we must add to the first
73 * evaluation f(t) then we need to substract f(t+h) - f(t) = m*t + m*h + b - m*t + b = mh. After
74 * obtaining this value (mh) we could just add this constant step to our first sampled point
75 * to compute the next one.
76 *
77 * For the cubic case the first difference gives as a result a quadratic polynomial to which we can
78 * apply again forward differences and get linear function to which we can apply again forward
79 * differences to get a constant difference. This is why we keep an array of size 4, the 0th
80 * position keeps the sampled value while the next ones keep the quadratic, linear and constant
81 * difference values.
82 */
83
85
86public:
87
88 /**
89 * Receives the 4 control points of the cubic bezier.
90 */
91
92 explicit FwDCubicEvaluator(const SkPoint points[4])
93 : fCoefs(points) {
94 memcpy(fPoints, points, 4 * sizeof(SkPoint));
95
96 this->restart(1);
97 }
98
99 /**
100 * Restarts the forward differences evaluator to the first value of t = 0.
101 */
102 void restart(int divisions) {
103 fDivisions = divisions;
104 fCurrent = 0;
105 fMax = fDivisions + 1;
106 skvx::float2 h = 1.f / fDivisions;
107 skvx::float2 h2 = h * h;
108 skvx::float2 h3 = h2 * h;
109 skvx::float2 fwDiff3 = 6 * fCoefs.fA * h3;
110 fFwDiff[3] = to_point(fwDiff3);
111 fFwDiff[2] = to_point(fwDiff3 + times_2(fCoefs.fB) * h2);
112 fFwDiff[1] = to_point(fCoefs.fA * h3 + fCoefs.fB * h2 + fCoefs.fC * h);
113 fFwDiff[0] = to_point(fCoefs.fD);
114 }
115
116 /**
117 * Check if the evaluator is still within the range of 0<=t<=1
118 */
119 bool done() const {
120 return fCurrent > fMax;
121 }
122
123 /**
124 * Call next to obtain the SkPoint sampled and move to the next one.
125 */
127 SkPoint point = fFwDiff[0];
128 fFwDiff[0] += fFwDiff[1];
129 fFwDiff[1] += fFwDiff[2];
130 fFwDiff[2] += fFwDiff[3];
131 fCurrent++;
132 return point;
133 }
134
135 const SkPoint* getCtrlPoints() const {
136 return fPoints;
137 }
138
139private:
140 SkCubicCoeff fCoefs;
141 int fMax, fCurrent, fDivisions;
142 SkPoint fFwDiff[4], fPoints[4];
143};
144
145////////////////////////////////////////////////////////////////////////////////
146
147// size in pixels of each partition per axis, adjust this knob
148static const int kPartitionSize = 10;
149
150/**
151 * Calculate the approximate arc length given a bezier curve's control points.
152 * Returns -1 if bad calc (i.e. non-finite)
153 */
155 if (count < 2) {
156 return 0;
157 }
158 SkScalar arcLength = 0;
159 for (int i = 0; i < count - 1; i++) {
160 arcLength += SkPoint::Distance(points[i], points[i + 1]);
161 }
162 return SkIsFinite(arcLength) ? arcLength : -1;
163}
164
166 SkScalar c11) {
167 SkScalar a = c00 * (1.f - tx) + c10 * tx;
168 SkScalar b = c01 * (1.f - tx) + c11 * tx;
169 return a * (1.f - ty) + b * ty;
170}
171
173 const skvx::float4& c00,
174 const skvx::float4& c10,
175 const skvx::float4& c01,
176 const skvx::float4& c11) {
177 auto a = c00 * (1.f - tx) + c10 * tx;
178 auto b = c01 * (1.f - tx) + c11 * tx;
179 return a * (1.f - ty) + b * ty;
180}
181
183 // Approximate length of each cubic.
185 SkPatchUtils::GetTopCubic(cubics, pts);
186 matrix->mapPoints(pts, kNumPtsCubic);
187 SkScalar topLength = approx_arc_length(pts, kNumPtsCubic);
188
189 SkPatchUtils::GetBottomCubic(cubics, pts);
190 matrix->mapPoints(pts, kNumPtsCubic);
191 SkScalar bottomLength = approx_arc_length(pts, kNumPtsCubic);
192
193 SkPatchUtils::GetLeftCubic(cubics, pts);
194 matrix->mapPoints(pts, kNumPtsCubic);
195 SkScalar leftLength = approx_arc_length(pts, kNumPtsCubic);
196
197 SkPatchUtils::GetRightCubic(cubics, pts);
198 matrix->mapPoints(pts, kNumPtsCubic);
199 SkScalar rightLength = approx_arc_length(pts, kNumPtsCubic);
200
201 if (topLength < 0 || bottomLength < 0 || leftLength < 0 || rightLength < 0) {
202 return {0, 0}; // negative length is a sentinel for bad length (i.e. non-finite)
203 }
204
205 // Level of detail per axis, based on the larger side between top and bottom or left and right
206 int lodX = static_cast<int>(std::max(topLength, bottomLength) / kPartitionSize);
207 int lodY = static_cast<int>(std::max(leftLength, rightLength) / kPartitionSize);
208
209 return SkISize::Make(std::max(8, lodX), std::max(8, lodY));
210}
211
213 points[0] = cubics[kTopP0_CubicCtrlPts];
214 points[1] = cubics[kTopP1_CubicCtrlPts];
215 points[2] = cubics[kTopP2_CubicCtrlPts];
216 points[3] = cubics[kTopP3_CubicCtrlPts];
217}
218
220 points[0] = cubics[kBottomP0_CubicCtrlPts];
221 points[1] = cubics[kBottomP1_CubicCtrlPts];
222 points[2] = cubics[kBottomP2_CubicCtrlPts];
223 points[3] = cubics[kBottomP3_CubicCtrlPts];
224}
225
227 points[0] = cubics[kLeftP0_CubicCtrlPts];
228 points[1] = cubics[kLeftP1_CubicCtrlPts];
229 points[2] = cubics[kLeftP2_CubicCtrlPts];
230 points[3] = cubics[kLeftP3_CubicCtrlPts];
231}
232
234 points[0] = cubics[kRightP0_CubicCtrlPts];
235 points[1] = cubics[kRightP1_CubicCtrlPts];
236 points[2] = cubics[kRightP2_CubicCtrlPts];
237 points[3] = cubics[kRightP3_CubicCtrlPts];
238}
239
240static void skcolor_to_float(SkPMColor4f* dst, const SkColor* src, int count, SkColorSpace* dstCS) {
245 SkAssertResult(SkConvertPixels(dstInfo, dst, 0, srcInfo, src, 0));
246}
247
248static void float_to_skcolor(SkColor* dst, const SkPMColor4f* src, int count, SkColorSpace* srcCS) {
253 SkAssertResult(SkConvertPixels(dstInfo, dst, 0, srcInfo, src, 0));
254}
255
256sk_sp<SkVertices> SkPatchUtils::MakeVertices(const SkPoint cubics[12], const SkColor srcColors[4],
257 const SkPoint srcTexCoords[4], int lodX, int lodY,
258 SkColorSpace* colorSpace) {
259 if (lodX < 1 || lodY < 1 || nullptr == cubics) {
260 return nullptr;
261 }
262
263 // check for overflow in multiplication
264 const int64_t lodX64 = (lodX + 1),
265 lodY64 = (lodY + 1),
266 mult64 = lodX64 * lodY64;
267 if (mult64 > SK_MaxS32) {
268 return nullptr;
269 }
270
271 // Treat null interpolation space as sRGB.
272 if (!colorSpace) {
273 colorSpace = sk_srgb_singleton();
274 }
275
276 int vertexCount = SkToS32(mult64);
277 // it is recommended to generate draw calls of no more than 65536 indices, so we never generate
278 // more than 60000 indices. To accomplish that we resize the LOD and vertex count
279 if (vertexCount > 10000 || lodX > 200 || lodY > 200) {
280 float weightX = static_cast<float>(lodX) / (lodX + lodY);
281 float weightY = static_cast<float>(lodY) / (lodX + lodY);
282
283 // 200 comes from the 100 * 2 which is the max value of vertices because of the limit of
284 // 60000 indices ( sqrt(60000 / 6) that comes from data->fIndexCount = lodX * lodY * 6)
285 // Need a min of 1 since we later divide by lod
286 lodX = std::max(1, sk_float_floor2int_no_saturate(weightX * 200));
287 lodY = std::max(1, sk_float_floor2int_no_saturate(weightY * 200));
288 vertexCount = (lodX + 1) * (lodY + 1);
289 }
290 const int indexCount = lodX * lodY * 6;
291 uint32_t flags = 0;
292 if (srcTexCoords) {
294 }
295 if (srcColors) {
297 }
298
300 SkPMColor4f* cornerColors = srcColors ? alloc.makeArray<SkPMColor4f>(4) : nullptr;
301 SkPMColor4f* tmpColors = srcColors ? alloc.makeArray<SkPMColor4f>(vertexCount) : nullptr;
302
304 SkPoint* pos = builder.positions();
305 SkPoint* texs = builder.texCoords();
306 uint16_t* indices = builder.indices();
307
308 if (cornerColors) {
309 skcolor_to_float(cornerColors, srcColors, kNumCorners, colorSpace);
310 }
311
313 SkPatchUtils::GetBottomCubic(cubics, pts);
314 FwDCubicEvaluator fBottom(pts);
315 SkPatchUtils::GetTopCubic(cubics, pts);
316 FwDCubicEvaluator fTop(pts);
317 SkPatchUtils::GetLeftCubic(cubics, pts);
318 FwDCubicEvaluator fLeft(pts);
319 SkPatchUtils::GetRightCubic(cubics, pts);
320 FwDCubicEvaluator fRight(pts);
321
322 fBottom.restart(lodX);
323 fTop.restart(lodX);
324
325 SkScalar u = 0.0f;
326 int stride = lodY + 1;
327 for (int x = 0; x <= lodX; x++) {
328 SkPoint bottom = fBottom.next(), top = fTop.next();
329 fLeft.restart(lodY);
330 fRight.restart(lodY);
331 SkScalar v = 0.f;
332 for (int y = 0; y <= lodY; y++) {
333 int dataIndex = x * (lodY + 1) + y;
334
335 SkPoint left = fLeft.next(), right = fRight.next();
336
337 SkPoint s0 = SkPoint::Make((1.0f - v) * top.x() + v * bottom.x(),
338 (1.0f - v) * top.y() + v * bottom.y());
339 SkPoint s1 = SkPoint::Make((1.0f - u) * left.x() + u * right.x(),
340 (1.0f - u) * left.y() + u * right.y());
342 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPoints()[0].x()
343 + u * fTop.getCtrlPoints()[3].x())
344 + v * ((1.0f - u) * fBottom.getCtrlPoints()[0].x()
345 + u * fBottom.getCtrlPoints()[3].x()),
346 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPoints()[0].y()
347 + u * fTop.getCtrlPoints()[3].y())
348 + v * ((1.0f - u) * fBottom.getCtrlPoints()[0].y()
349 + u * fBottom.getCtrlPoints()[3].y()));
350 pos[dataIndex] = s0 + s1 - s2;
351
352 if (cornerColors) {
353 bilerp(u, v, skvx::float4::Load(cornerColors[kTopLeft_Corner].vec()),
354 skvx::float4::Load(cornerColors[kTopRight_Corner].vec()),
355 skvx::float4::Load(cornerColors[kBottomLeft_Corner].vec()),
356 skvx::float4::Load(cornerColors[kBottomRight_Corner].vec()))
357 .store(tmpColors[dataIndex].vec());
358 }
359
360 if (texs) {
361 texs[dataIndex] = SkPoint::Make(bilerp(u, v, srcTexCoords[kTopLeft_Corner].x(),
362 srcTexCoords[kTopRight_Corner].x(),
363 srcTexCoords[kBottomLeft_Corner].x(),
364 srcTexCoords[kBottomRight_Corner].x()),
365 bilerp(u, v, srcTexCoords[kTopLeft_Corner].y(),
366 srcTexCoords[kTopRight_Corner].y(),
367 srcTexCoords[kBottomLeft_Corner].y(),
368 srcTexCoords[kBottomRight_Corner].y()));
369
370 }
371
372 if(x < lodX && y < lodY) {
373 int i = 6 * (x * lodY + y);
374 indices[i] = x * stride + y;
375 indices[i + 1] = x * stride + 1 + y;
376 indices[i + 2] = (x + 1) * stride + 1 + y;
377 indices[i + 3] = indices[i];
378 indices[i + 4] = indices[i + 2];
379 indices[i + 5] = (x + 1) * stride + y;
380 }
381 v = SkTPin(v + 1.f / lodY, 0.0f, 1.0f);
382 }
383 u = SkTPin(u + 1.f / lodX, 0.0f, 1.0f);
384 }
385
386 if (tmpColors) {
387 float_to_skcolor(builder.colors(), tmpColors, vertexCount, colorSpace);
388 }
389 return builder.detach();
390}
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
int count
Definition: FontMgrTest.cpp:50
static const int points[]
SkPoint pos
kUnpremul_SkAlphaType
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
SkColorSpace * sk_srgb_singleton()
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition: SkColorType.h:26
@ kRGBA_F32_SkColorType
pixel using C float for red, green, blue, alpha; in 128-bit word
Definition: SkColorType.h:40
uint32_t SkColor
Definition: SkColor.h:37
bool SkConvertPixels(const SkImageInfo &dstInfo, void *dstPixels, size_t dstRB, const SkImageInfo &srcInfo, const void *srcPixels, size_t srcRB)
#define sk_float_floor2int_no_saturate(x)
static bool SkIsFinite(T x, Pack... values)
static skvx::float2 times_2(const skvx::float2 &value)
Definition: SkGeometry.h:32
static constexpr int32_t SK_MaxS32
Definition: SkMath.h:21
static SkScalar bilerp(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar c01, SkScalar c11)
static const int kPartitionSize
static void skcolor_to_float(SkPMColor4f *dst, const SkColor *src, int count, SkColorSpace *dstCS)
static void float_to_skcolor(SkColor *dst, const SkPMColor4f *src, int count, SkColorSpace *srcCS)
static SkScalar approx_arc_length(const SkPoint points[], int count)
static bool left(const SkPoint &p0, const SkPoint &p1)
static bool right(const SkPoint &p0, const SkPoint &p1)
sk_sp< T > sk_ref_sp(T *obj)
Definition: SkRefCnt.h:381
static constexpr const T & SkTPin(const T &x, const T &lo, const T &hi)
Definition: SkTPin.h:19
constexpr int32_t SkToS32(S x)
Definition: SkTo.h:25
void restart(int divisions)
bool done() const
const SkPoint * getCtrlPoints() const
FwDCubicEvaluator(const SkPoint points[4])
T * makeArray(size_t count)
Definition: SkArenaAlloc.h:181
static sk_sp< SkColorSpace > MakeSRGB()
static void GetTopCubic(const SkPoint cubics[12], SkPoint points[4])
static void GetRightCubic(const SkPoint cubics[12], SkPoint points[4])
static sk_sp< SkVertices > MakeVertices(const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4], int lodX, int lodY, SkColorSpace *colorSpace=nullptr)
static void GetLeftCubic(const SkPoint cubics[12], SkPoint points[4])
static SkISize GetLevelOfDetail(const SkPoint cubics[12], const SkMatrix *matrix)
static void GetBottomCubic(const SkPoint cubics[12], SkPoint points[4])
@ kHasTexCoords_BuilderFlag
Definition: SkVertices.h:63
@ kHasColors_BuilderFlag
Definition: SkVertices.h:64
@ kTriangles_VertexMode
Definition: SkVertices.h:31
static SkPoint to_point(SkIPoint p)
Definition: editor.cpp:75
float SkScalar
Definition: extension.cpp:12
static bool b
struct MyStruct a[10]
FlutterSemanticsFlag flags
static float max(float r, float g, float b)
Definition: hsl.cpp:49
double y
double x
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
PODArray< SkRect > texs
Definition: SkRecords.h:333
dst
Definition: cp.py:12
SkScalar h
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
static float Distance(const SkPoint &a, const SkPoint &b)
Definition: SkPoint_impl.h:508
constexpr float y() const
Definition: SkPoint_impl.h:187
constexpr float x() const
Definition: SkPoint_impl.h:181
Definition: SkVx.h:83
static SKVX_ALWAYS_INLINE Vec Load(const void *ptr)
Definition: SkVx.h:109