2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "src/utils/SkPatchUtils.h"
10 #include "include/core/SkAlphaType.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkColorType.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkMatrix.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkTypes.h"
19 #include "include/core/SkVertices.h"
20 #include "include/private/SkColorData.h"
21 #include "include/private/SkFloatingPoint.h"
22 #include "include/private/SkTPin.h"
23 #include "include/private/SkTo.h"
24 #include "include/private/SkVx.h"
25 #include "src/core/SkArenaAlloc.h"
26 #include "src/core/SkColorSpacePriv.h"
27 #include "src/core/SkConvertPixels.h"
28 #include "src/core/SkGeometry.h"
35 kTopP0_CubicCtrlPts = 0,
36 kTopP1_CubicCtrlPts = 1,
37 kTopP2_CubicCtrlPts = 2,
38 kTopP3_CubicCtrlPts = 3,
40 kRightP0_CubicCtrlPts = 3,
41 kRightP1_CubicCtrlPts = 4,
42 kRightP2_CubicCtrlPts = 5,
43 kRightP3_CubicCtrlPts = 6,
45 kBottomP0_CubicCtrlPts = 9,
46 kBottomP1_CubicCtrlPts = 8,
47 kBottomP2_CubicCtrlPts = 7,
48 kBottomP3_CubicCtrlPts = 6,
50 kLeftP0_CubicCtrlPts = 0,
51 kLeftP1_CubicCtrlPts = 11,
52 kLeftP2_CubicCtrlPts = 10,
53 kLeftP3_CubicCtrlPts = 9,
56 // Enum for corner also clockwise.
66 * Evaluator to sample the values of a cubic bezier using forward differences.
67 * Forward differences is a method for evaluating a nth degree polynomial at a uniform step by only
68 * adding precalculated values.
69 * For a linear example we have the function f(t) = m*t+b, then the value of that function at t+h
70 * would be f(t+h) = m*(t+h)+b. If we want to know the uniform step that we must add to the first
71 * evaluation f(t) then we need to substract f(t+h) - f(t) = m*t + m*h + b - m*t + b = mh. After
72 * obtaining this value (mh) we could just add this constant step to our first sampled point
73 * to compute the next one.
75 * For the cubic case the first difference gives as a result a quadratic polynomial to which we can
76 * apply again forward differences and get linear function to which we can apply again forward
77 * differences to get a constant difference. This is why we keep an array of size 4, the 0th
78 * position keeps the sampled value while the next ones keep the quadratic, linear and constant
82 class FwDCubicEvaluator {
87 * Receives the 4 control points of the cubic bezier.
90 explicit FwDCubicEvaluator(const SkPoint points[4])
92 memcpy(fPoints, points, 4 * sizeof(SkPoint));
98 * Restarts the forward differences evaluator to the first value of t = 0.
100 void restart(int divisions) {
101 fDivisions = divisions;
103 fMax = fDivisions + 1;
104 skvx::float2 h = 1.f / fDivisions;
105 skvx::float2 h2 = h * h;
106 skvx::float2 h3 = h2 * h;
107 skvx::float2 fwDiff3 = 6 * fCoefs.fA * h3;
108 fFwDiff[3] = to_point(fwDiff3);
109 fFwDiff[2] = to_point(fwDiff3 + times_2(fCoefs.fB) * h2);
110 fFwDiff[1] = to_point(fCoefs.fA * h3 + fCoefs.fB * h2 + fCoefs.fC * h);
111 fFwDiff[0] = to_point(fCoefs.fD);
115 * Check if the evaluator is still within the range of 0<=t<=1
118 return fCurrent > fMax;
122 * Call next to obtain the SkPoint sampled and move to the next one.
125 SkPoint point = fFwDiff[0];
126 fFwDiff[0] += fFwDiff[1];
127 fFwDiff[1] += fFwDiff[2];
128 fFwDiff[2] += fFwDiff[3];
133 const SkPoint* getCtrlPoints() const {
139 int fMax, fCurrent, fDivisions;
140 SkPoint fFwDiff[4], fPoints[4];
143 ////////////////////////////////////////////////////////////////////////////////
145 // size in pixels of each partition per axis, adjust this knob
146 static const int kPartitionSize = 10;
149 * Calculate the approximate arc length given a bezier curve's control points.
150 * Returns -1 if bad calc (i.e. non-finite)
152 static SkScalar approx_arc_length(const SkPoint points[], int count) {
156 SkScalar arcLength = 0;
157 for (int i = 0; i < count - 1; i++) {
158 arcLength += SkPoint::Distance(points[i], points[i + 1]);
160 return SkScalarIsFinite(arcLength) ? arcLength : -1;
163 static SkScalar bilerp(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar c01,
165 SkScalar a = c00 * (1.f - tx) + c10 * tx;
166 SkScalar b = c01 * (1.f - tx) + c11 * tx;
167 return a * (1.f - ty) + b * ty;
170 static skvx::float4 bilerp(SkScalar tx, SkScalar ty,
171 const skvx::float4& c00,
172 const skvx::float4& c10,
173 const skvx::float4& c01,
174 const skvx::float4& c11) {
175 auto a = c00 * (1.f - tx) + c10 * tx;
176 auto b = c01 * (1.f - tx) + c11 * tx;
177 return a * (1.f - ty) + b * ty;
180 SkISize SkPatchUtils::GetLevelOfDetail(const SkPoint cubics[12], const SkMatrix* matrix) {
181 // Approximate length of each cubic.
182 SkPoint pts[kNumPtsCubic];
183 SkPatchUtils::GetTopCubic(cubics, pts);
184 matrix->mapPoints(pts, kNumPtsCubic);
185 SkScalar topLength = approx_arc_length(pts, kNumPtsCubic);
187 SkPatchUtils::GetBottomCubic(cubics, pts);
188 matrix->mapPoints(pts, kNumPtsCubic);
189 SkScalar bottomLength = approx_arc_length(pts, kNumPtsCubic);
191 SkPatchUtils::GetLeftCubic(cubics, pts);
192 matrix->mapPoints(pts, kNumPtsCubic);
193 SkScalar leftLength = approx_arc_length(pts, kNumPtsCubic);
195 SkPatchUtils::GetRightCubic(cubics, pts);
196 matrix->mapPoints(pts, kNumPtsCubic);
197 SkScalar rightLength = approx_arc_length(pts, kNumPtsCubic);
199 if (topLength < 0 || bottomLength < 0 || leftLength < 0 || rightLength < 0) {
200 return {0, 0}; // negative length is a sentinel for bad length (i.e. non-finite)
203 // Level of detail per axis, based on the larger side between top and bottom or left and right
204 int lodX = static_cast<int>(std::max(topLength, bottomLength) / kPartitionSize);
205 int lodY = static_cast<int>(std::max(leftLength, rightLength) / kPartitionSize);
207 return SkISize::Make(std::max(8, lodX), std::max(8, lodY));
210 void SkPatchUtils::GetTopCubic(const SkPoint cubics[12], SkPoint points[4]) {
211 points[0] = cubics[kTopP0_CubicCtrlPts];
212 points[1] = cubics[kTopP1_CubicCtrlPts];
213 points[2] = cubics[kTopP2_CubicCtrlPts];
214 points[3] = cubics[kTopP3_CubicCtrlPts];
217 void SkPatchUtils::GetBottomCubic(const SkPoint cubics[12], SkPoint points[4]) {
218 points[0] = cubics[kBottomP0_CubicCtrlPts];
219 points[1] = cubics[kBottomP1_CubicCtrlPts];
220 points[2] = cubics[kBottomP2_CubicCtrlPts];
221 points[3] = cubics[kBottomP3_CubicCtrlPts];
224 void SkPatchUtils::GetLeftCubic(const SkPoint cubics[12], SkPoint points[4]) {
225 points[0] = cubics[kLeftP0_CubicCtrlPts];
226 points[1] = cubics[kLeftP1_CubicCtrlPts];
227 points[2] = cubics[kLeftP2_CubicCtrlPts];
228 points[3] = cubics[kLeftP3_CubicCtrlPts];
231 void SkPatchUtils::GetRightCubic(const SkPoint cubics[12], SkPoint points[4]) {
232 points[0] = cubics[kRightP0_CubicCtrlPts];
233 points[1] = cubics[kRightP1_CubicCtrlPts];
234 points[2] = cubics[kRightP2_CubicCtrlPts];
235 points[3] = cubics[kRightP3_CubicCtrlPts];
238 static void skcolor_to_float(SkPMColor4f* dst, const SkColor* src, int count, SkColorSpace* dstCS) {
239 SkImageInfo srcInfo = SkImageInfo::Make(count, 1, kBGRA_8888_SkColorType,
240 kUnpremul_SkAlphaType, SkColorSpace::MakeSRGB());
241 SkImageInfo dstInfo = SkImageInfo::Make(count, 1, kRGBA_F32_SkColorType,
242 kPremul_SkAlphaType, sk_ref_sp(dstCS));
243 SkAssertResult(SkConvertPixels(dstInfo, dst, 0, srcInfo, src, 0));
246 static void float_to_skcolor(SkColor* dst, const SkPMColor4f* src, int count, SkColorSpace* srcCS) {
247 SkImageInfo srcInfo = SkImageInfo::Make(count, 1, kRGBA_F32_SkColorType,
248 kPremul_SkAlphaType, sk_ref_sp(srcCS));
249 SkImageInfo dstInfo = SkImageInfo::Make(count, 1, kBGRA_8888_SkColorType,
250 kUnpremul_SkAlphaType, SkColorSpace::MakeSRGB());
251 SkAssertResult(SkConvertPixels(dstInfo, dst, 0, srcInfo, src, 0));
254 sk_sp<SkVertices> SkPatchUtils::MakeVertices(const SkPoint cubics[12], const SkColor srcColors[4],
255 const SkPoint srcTexCoords[4], int lodX, int lodY,
256 SkColorSpace* colorSpace) {
257 if (lodX < 1 || lodY < 1 || nullptr == cubics) {
261 // check for overflow in multiplication
262 const int64_t lodX64 = (lodX + 1),
264 mult64 = lodX64 * lodY64;
265 if (mult64 > SK_MaxS32) {
269 // Treat null interpolation space as sRGB.
271 colorSpace = sk_srgb_singleton();
274 int vertexCount = SkToS32(mult64);
275 // it is recommended to generate draw calls of no more than 65536 indices, so we never generate
276 // more than 60000 indices. To accomplish that we resize the LOD and vertex count
277 if (vertexCount > 10000 || lodX > 200 || lodY > 200) {
278 float weightX = static_cast<float>(lodX) / (lodX + lodY);
279 float weightY = static_cast<float>(lodY) / (lodX + lodY);
281 // 200 comes from the 100 * 2 which is the max value of vertices because of the limit of
282 // 60000 indices ( sqrt(60000 / 6) that comes from data->fIndexCount = lodX * lodY * 6)
283 // Need a min of 1 since we later divide by lod
284 lodX = std::max(1, sk_float_floor2int_no_saturate(weightX * 200));
285 lodY = std::max(1, sk_float_floor2int_no_saturate(weightY * 200));
286 vertexCount = (lodX + 1) * (lodY + 1);
288 const int indexCount = lodX * lodY * 6;
291 flags |= SkVertices::kHasTexCoords_BuilderFlag;
294 flags |= SkVertices::kHasColors_BuilderFlag;
297 SkSTArenaAlloc<2048> alloc;
298 SkPMColor4f* cornerColors = srcColors ? alloc.makeArray<SkPMColor4f>(4) : nullptr;
299 SkPMColor4f* tmpColors = srcColors ? alloc.makeArray<SkPMColor4f>(vertexCount) : nullptr;
301 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vertexCount, indexCount, flags);
302 SkPoint* pos = builder.positions();
303 SkPoint* texs = builder.texCoords();
304 uint16_t* indices = builder.indices();
307 skcolor_to_float(cornerColors, srcColors, kNumCorners, colorSpace);
310 SkPoint pts[kNumPtsCubic];
311 SkPatchUtils::GetBottomCubic(cubics, pts);
312 FwDCubicEvaluator fBottom(pts);
313 SkPatchUtils::GetTopCubic(cubics, pts);
314 FwDCubicEvaluator fTop(pts);
315 SkPatchUtils::GetLeftCubic(cubics, pts);
316 FwDCubicEvaluator fLeft(pts);
317 SkPatchUtils::GetRightCubic(cubics, pts);
318 FwDCubicEvaluator fRight(pts);
320 fBottom.restart(lodX);
324 int stride = lodY + 1;
325 for (int x = 0; x <= lodX; x++) {
326 SkPoint bottom = fBottom.next(), top = fTop.next();
328 fRight.restart(lodY);
330 for (int y = 0; y <= lodY; y++) {
331 int dataIndex = x * (lodY + 1) + y;
333 SkPoint left = fLeft.next(), right = fRight.next();
335 SkPoint s0 = SkPoint::Make((1.0f - v) * top.x() + v * bottom.x(),
336 (1.0f - v) * top.y() + v * bottom.y());
337 SkPoint s1 = SkPoint::Make((1.0f - u) * left.x() + u * right.x(),
338 (1.0f - u) * left.y() + u * right.y());
339 SkPoint s2 = SkPoint::Make(
340 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPoints()[0].x()
341 + u * fTop.getCtrlPoints()[3].x())
342 + v * ((1.0f - u) * fBottom.getCtrlPoints()[0].x()
343 + u * fBottom.getCtrlPoints()[3].x()),
344 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPoints()[0].y()
345 + u * fTop.getCtrlPoints()[3].y())
346 + v * ((1.0f - u) * fBottom.getCtrlPoints()[0].y()
347 + u * fBottom.getCtrlPoints()[3].y()));
348 pos[dataIndex] = s0 + s1 - s2;
351 bilerp(u, v, skvx::float4::Load(cornerColors[kTopLeft_Corner].vec()),
352 skvx::float4::Load(cornerColors[kTopRight_Corner].vec()),
353 skvx::float4::Load(cornerColors[kBottomLeft_Corner].vec()),
354 skvx::float4::Load(cornerColors[kBottomRight_Corner].vec()))
355 .store(tmpColors[dataIndex].vec());
359 texs[dataIndex] = SkPoint::Make(bilerp(u, v, srcTexCoords[kTopLeft_Corner].x(),
360 srcTexCoords[kTopRight_Corner].x(),
361 srcTexCoords[kBottomLeft_Corner].x(),
362 srcTexCoords[kBottomRight_Corner].x()),
363 bilerp(u, v, srcTexCoords[kTopLeft_Corner].y(),
364 srcTexCoords[kTopRight_Corner].y(),
365 srcTexCoords[kBottomLeft_Corner].y(),
366 srcTexCoords[kBottomRight_Corner].y()));
370 if(x < lodX && y < lodY) {
371 int i = 6 * (x * lodY + y);
372 indices[i] = x * stride + y;
373 indices[i + 1] = x * stride + 1 + y;
374 indices[i + 2] = (x + 1) * stride + 1 + y;
375 indices[i + 3] = indices[i];
376 indices[i + 4] = indices[i + 2];
377 indices[i + 5] = (x + 1) * stride + y;
379 v = SkTPin(v + 1.f / lodY, 0.0f, 1.0f);
381 u = SkTPin(u + 1.f / lodX, 0.0f, 1.0f);
385 float_to_skcolor(builder.colors(), tmpColors, vertexCount, colorSpace);
387 return builder.detach();