| 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 |
|
| 8 |
#include "src/utils/SkPatchUtils.h" |
| 9 |
|
| 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/base/SkFloatingPoint.h" |
| 21 |
#include "include/private/base/SkMath.h" |
| 22 |
#include "include/private/base/SkTPin.h" |
| 23 |
#include "include/private/base/SkTo.h" |
| 24 |
#include "src/base/SkArenaAlloc.h" |
| 25 |
#include "src/base/SkVx.h" |
| 26 |
#include "src/core/SkColorData.h" |
| 27 |
#include "src/core/SkColorSpacePriv.h" |
| 28 |
#include "src/core/SkConvertPixels.h" |
| 29 |
#include "src/core/SkGeometry.h" |
| 30 |
|
| 31 |
#include <algorithm> |
| 32 |
#include <cstdint> |
| 33 |
#include <cstring> |
| 34 |
|
| 35 |
namespace { |
| 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 |
|
| 84 |
class FwDCubicEvaluator { |
| 85 |
|
| 86 |
public: |
| 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 |
*/ |
| 126 |
SkPoint next() { |
| 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 |
|
| 139 |
private: |
| 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 |
| 148 |
static 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 |
*/ |
| 154 |
static SkScalar approx_arc_length(const SkPoint points[], int count) { |
| 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 |
|
| 165 |
static SkScalar bilerp(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar c01, |
| 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 |
|
| 172 |
static skvx::float4 bilerp(SkScalar tx, SkScalar ty, |
| 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 |
|
| 182 |
SkISize SkPatchUtils::GetLevelOfDetail(const SkPoint cubics[12], const SkMatrix* matrix) { |
| 183 |
// Approximate length of each cubic. |
| 184 |
SkPoint pts[kNumPtsCubic]; |
| 185 |
SkPatchUtils::GetTopCubic(cubics, pts); |
| 186 |
matrix->mapPoints(pts); |
| 187 |
SkScalar topLength = approx_arc_length(pts, kNumPtsCubic); |
| 188 |
|
| 189 |
SkPatchUtils::GetBottomCubic(cubics, pts); |
| 190 |
matrix->mapPoints(pts); |
| 191 |
SkScalar bottomLength = approx_arc_length(pts, kNumPtsCubic); |
| 192 |
|
| 193 |
SkPatchUtils::GetLeftCubic(cubics, pts); |
| 194 |
matrix->mapPoints(pts); |
| 195 |
SkScalar leftLength = approx_arc_length(pts, kNumPtsCubic); |
| 196 |
|
| 197 |
SkPatchUtils::GetRightCubic(cubics, pts); |
| 198 |
matrix->mapPoints(pts); |
| 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 |
|
| 212 |
void SkPatchUtils::GetTopCubic(const SkPoint cubics[12], SkPoint points[4]) { |
| 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 |
|
| 219 |
void SkPatchUtils::GetBottomCubic(const SkPoint cubics[12], SkPoint points[4]) { |
| 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 |
|
| 226 |
void SkPatchUtils::GetLeftCubic(const SkPoint cubics[12], SkPoint points[4]) { |
| 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 |
|
| 233 |
void SkPatchUtils::GetRightCubic(const SkPoint cubics[12], SkPoint points[4]) { |
| 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 |
|
| 240 |
static void skcolor_to_float(SkPMColor4f* dst, const SkColor* src, int count, SkColorSpace* dstCS) { |
| 241 |
SkImageInfo srcInfo = SkImageInfo::Make(count, 1, kBGRA_8888_SkColorType, |
| 242 |
kUnpremul_SkAlphaType, SkColorSpace::MakeSRGB()); |
| 243 |
SkImageInfo dstInfo = SkImageInfo::Make(count, 1, kRGBA_F32_SkColorType, |
| 244 |
kPremul_SkAlphaType, sk_ref_sp(dstCS)); |
| 245 |
SkAssertResult(SkConvertPixels(dstInfo, dst, 0, srcInfo, src, 0)); |
| 246 |
} |
| 247 |
|
| 248 |
static void float_to_skcolor(SkColor* dst, const SkPMColor4f* src, int count, SkColorSpace* srcCS) { |
| 249 |
SkImageInfo srcInfo = SkImageInfo::Make(count, 1, kRGBA_F32_SkColorType, |
| 250 |
kPremul_SkAlphaType, sk_ref_sp(srcCS)); |
| 251 |
SkImageInfo dstInfo = SkImageInfo::Make(count, 1, kBGRA_8888_SkColorType, |
| 252 |
kUnpremul_SkAlphaType, SkColorSpace::MakeSRGB()); |
| 253 |
SkAssertResult(SkConvertPixels(dstInfo, dst, 0, srcInfo, src, 0)); |
| 254 |
} |
| 255 |
|
| 256 |
sk_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) { |
| 293 |
flags |= SkVertices::kHasTexCoords_BuilderFlag; |
| 294 |
} |
| 295 |
if (srcColors) { |
| 296 |
flags |= SkVertices::kHasColors_BuilderFlag; |
| 297 |
} |
| 298 |
|
| 299 |
SkSTArenaAlloc<2048> alloc; |
| 300 |
SkPMColor4f* cornerColors = srcColors ? alloc.makeArray<SkPMColor4f>(4) : nullptr; |
| 301 |
SkPMColor4f* tmpColors = srcColors ? alloc.makeArray<SkPMColor4f>(vertexCount) : nullptr; |
| 302 |
|
| 303 |
SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vertexCount, indexCount, flags); |
| 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 |
|
| 312 |
SkPoint pts[kNumPtsCubic]; |
| 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()); |
| 341 |
SkPoint s2 = SkPoint::Make( |
| 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 |
} |