SkPatchUtils.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. #include "src/utils/SkPatchUtils.h"
  8. #include "include/private/SkColorData.h"
  9. #include "include/private/SkTo.h"
  10. #include "src/core/SkArenaAlloc.h"
  11. #include "src/core/SkColorSpacePriv.h"
  12. #include "src/core/SkConvertPixels.h"
  13. #include "src/core/SkGeometry.h"
  14. namespace {
  15. enum CubicCtrlPts {
  16. kTopP0_CubicCtrlPts = 0,
  17. kTopP1_CubicCtrlPts = 1,
  18. kTopP2_CubicCtrlPts = 2,
  19. kTopP3_CubicCtrlPts = 3,
  20. kRightP0_CubicCtrlPts = 3,
  21. kRightP1_CubicCtrlPts = 4,
  22. kRightP2_CubicCtrlPts = 5,
  23. kRightP3_CubicCtrlPts = 6,
  24. kBottomP0_CubicCtrlPts = 9,
  25. kBottomP1_CubicCtrlPts = 8,
  26. kBottomP2_CubicCtrlPts = 7,
  27. kBottomP3_CubicCtrlPts = 6,
  28. kLeftP0_CubicCtrlPts = 0,
  29. kLeftP1_CubicCtrlPts = 11,
  30. kLeftP2_CubicCtrlPts = 10,
  31. kLeftP3_CubicCtrlPts = 9,
  32. };
  33. // Enum for corner also clockwise.
  34. enum Corner {
  35. kTopLeft_Corner = 0,
  36. kTopRight_Corner,
  37. kBottomRight_Corner,
  38. kBottomLeft_Corner
  39. };
  40. }
  41. /**
  42. * Evaluator to sample the values of a cubic bezier using forward differences.
  43. * Forward differences is a method for evaluating a nth degree polynomial at a uniform step by only
  44. * adding precalculated values.
  45. * For a linear example we have the function f(t) = m*t+b, then the value of that function at t+h
  46. * would be f(t+h) = m*(t+h)+b. If we want to know the uniform step that we must add to the first
  47. * evaluation f(t) then we need to substract f(t+h) - f(t) = m*t + m*h + b - m*t + b = mh. After
  48. * obtaining this value (mh) we could just add this constant step to our first sampled point
  49. * to compute the next one.
  50. *
  51. * For the cubic case the first difference gives as a result a quadratic polynomial to which we can
  52. * apply again forward differences and get linear function to which we can apply again forward
  53. * differences to get a constant difference. This is why we keep an array of size 4, the 0th
  54. * position keeps the sampled value while the next ones keep the quadratic, linear and constant
  55. * difference values.
  56. */
  57. class FwDCubicEvaluator {
  58. public:
  59. /**
  60. * Receives the 4 control points of the cubic bezier.
  61. */
  62. explicit FwDCubicEvaluator(const SkPoint points[4])
  63. : fCoefs(points) {
  64. memcpy(fPoints, points, 4 * sizeof(SkPoint));
  65. this->restart(1);
  66. }
  67. /**
  68. * Restarts the forward differences evaluator to the first value of t = 0.
  69. */
  70. void restart(int divisions) {
  71. fDivisions = divisions;
  72. fCurrent = 0;
  73. fMax = fDivisions + 1;
  74. Sk2s h = Sk2s(1.f / fDivisions);
  75. Sk2s h2 = h * h;
  76. Sk2s h3 = h2 * h;
  77. Sk2s fwDiff3 = Sk2s(6) * fCoefs.fA * h3;
  78. fFwDiff[3] = to_point(fwDiff3);
  79. fFwDiff[2] = to_point(fwDiff3 + times_2(fCoefs.fB) * h2);
  80. fFwDiff[1] = to_point(fCoefs.fA * h3 + fCoefs.fB * h2 + fCoefs.fC * h);
  81. fFwDiff[0] = to_point(fCoefs.fD);
  82. }
  83. /**
  84. * Check if the evaluator is still within the range of 0<=t<=1
  85. */
  86. bool done() const {
  87. return fCurrent > fMax;
  88. }
  89. /**
  90. * Call next to obtain the SkPoint sampled and move to the next one.
  91. */
  92. SkPoint next() {
  93. SkPoint point = fFwDiff[0];
  94. fFwDiff[0] += fFwDiff[1];
  95. fFwDiff[1] += fFwDiff[2];
  96. fFwDiff[2] += fFwDiff[3];
  97. fCurrent++;
  98. return point;
  99. }
  100. const SkPoint* getCtrlPoints() const {
  101. return fPoints;
  102. }
  103. private:
  104. SkCubicCoeff fCoefs;
  105. int fMax, fCurrent, fDivisions;
  106. SkPoint fFwDiff[4], fPoints[4];
  107. };
  108. ////////////////////////////////////////////////////////////////////////////////
  109. // size in pixels of each partition per axis, adjust this knob
  110. static const int kPartitionSize = 10;
  111. /**
  112. * Calculate the approximate arc length given a bezier curve's control points.
  113. * Returns -1 if bad calc (i.e. non-finite)
  114. */
  115. static SkScalar approx_arc_length(const SkPoint points[], int count) {
  116. if (count < 2) {
  117. return 0;
  118. }
  119. SkScalar arcLength = 0;
  120. for (int i = 0; i < count - 1; i++) {
  121. arcLength += SkPoint::Distance(points[i], points[i + 1]);
  122. }
  123. return SkScalarIsFinite(arcLength) ? arcLength : -1;
  124. }
  125. static SkScalar bilerp(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar c01,
  126. SkScalar c11) {
  127. SkScalar a = c00 * (1.f - tx) + c10 * tx;
  128. SkScalar b = c01 * (1.f - tx) + c11 * tx;
  129. return a * (1.f - ty) + b * ty;
  130. }
  131. static Sk4f bilerp(SkScalar tx, SkScalar ty,
  132. const Sk4f& c00, const Sk4f& c10, const Sk4f& c01, const Sk4f& c11) {
  133. Sk4f a = c00 * (1.f - tx) + c10 * tx;
  134. Sk4f b = c01 * (1.f - tx) + c11 * tx;
  135. return a * (1.f - ty) + b * ty;
  136. }
  137. SkISize SkPatchUtils::GetLevelOfDetail(const SkPoint cubics[12], const SkMatrix* matrix) {
  138. // Approximate length of each cubic.
  139. SkPoint pts[kNumPtsCubic];
  140. SkPatchUtils::GetTopCubic(cubics, pts);
  141. matrix->mapPoints(pts, kNumPtsCubic);
  142. SkScalar topLength = approx_arc_length(pts, kNumPtsCubic);
  143. SkPatchUtils::GetBottomCubic(cubics, pts);
  144. matrix->mapPoints(pts, kNumPtsCubic);
  145. SkScalar bottomLength = approx_arc_length(pts, kNumPtsCubic);
  146. SkPatchUtils::GetLeftCubic(cubics, pts);
  147. matrix->mapPoints(pts, kNumPtsCubic);
  148. SkScalar leftLength = approx_arc_length(pts, kNumPtsCubic);
  149. SkPatchUtils::GetRightCubic(cubics, pts);
  150. matrix->mapPoints(pts, kNumPtsCubic);
  151. SkScalar rightLength = approx_arc_length(pts, kNumPtsCubic);
  152. if (topLength < 0 || bottomLength < 0 || leftLength < 0 || rightLength < 0) {
  153. return {0, 0}; // negative length is a sentinel for bad length (i.e. non-finite)
  154. }
  155. // Level of detail per axis, based on the larger side between top and bottom or left and right
  156. int lodX = static_cast<int>(SkMaxScalar(topLength, bottomLength) / kPartitionSize);
  157. int lodY = static_cast<int>(SkMaxScalar(leftLength, rightLength) / kPartitionSize);
  158. return SkISize::Make(SkMax32(8, lodX), SkMax32(8, lodY));
  159. }
  160. void SkPatchUtils::GetTopCubic(const SkPoint cubics[12], SkPoint points[4]) {
  161. points[0] = cubics[kTopP0_CubicCtrlPts];
  162. points[1] = cubics[kTopP1_CubicCtrlPts];
  163. points[2] = cubics[kTopP2_CubicCtrlPts];
  164. points[3] = cubics[kTopP3_CubicCtrlPts];
  165. }
  166. void SkPatchUtils::GetBottomCubic(const SkPoint cubics[12], SkPoint points[4]) {
  167. points[0] = cubics[kBottomP0_CubicCtrlPts];
  168. points[1] = cubics[kBottomP1_CubicCtrlPts];
  169. points[2] = cubics[kBottomP2_CubicCtrlPts];
  170. points[3] = cubics[kBottomP3_CubicCtrlPts];
  171. }
  172. void SkPatchUtils::GetLeftCubic(const SkPoint cubics[12], SkPoint points[4]) {
  173. points[0] = cubics[kLeftP0_CubicCtrlPts];
  174. points[1] = cubics[kLeftP1_CubicCtrlPts];
  175. points[2] = cubics[kLeftP2_CubicCtrlPts];
  176. points[3] = cubics[kLeftP3_CubicCtrlPts];
  177. }
  178. void SkPatchUtils::GetRightCubic(const SkPoint cubics[12], SkPoint points[4]) {
  179. points[0] = cubics[kRightP0_CubicCtrlPts];
  180. points[1] = cubics[kRightP1_CubicCtrlPts];
  181. points[2] = cubics[kRightP2_CubicCtrlPts];
  182. points[3] = cubics[kRightP3_CubicCtrlPts];
  183. }
  184. static void skcolor_to_float(SkPMColor4f* dst, const SkColor* src, int count, SkColorSpace* dstCS) {
  185. SkImageInfo srcInfo = SkImageInfo::Make(count, 1, kBGRA_8888_SkColorType,
  186. kUnpremul_SkAlphaType, SkColorSpace::MakeSRGB());
  187. SkImageInfo dstInfo = SkImageInfo::Make(count, 1, kRGBA_F32_SkColorType,
  188. kPremul_SkAlphaType, sk_ref_sp(dstCS));
  189. SkConvertPixels(dstInfo, dst, 0, srcInfo, src, 0);
  190. }
  191. static void float_to_skcolor(SkColor* dst, const SkPMColor4f* src, int count, SkColorSpace* srcCS) {
  192. SkImageInfo srcInfo = SkImageInfo::Make(count, 1, kRGBA_F32_SkColorType,
  193. kPremul_SkAlphaType, sk_ref_sp(srcCS));
  194. SkImageInfo dstInfo = SkImageInfo::Make(count, 1, kBGRA_8888_SkColorType,
  195. kUnpremul_SkAlphaType, SkColorSpace::MakeSRGB());
  196. SkConvertPixels(dstInfo, dst, 0, srcInfo, src, 0);
  197. }
  198. sk_sp<SkVertices> SkPatchUtils::MakeVertices(const SkPoint cubics[12], const SkColor srcColors[4],
  199. const SkPoint srcTexCoords[4], int lodX, int lodY,
  200. SkColorSpace* colorSpace) {
  201. if (lodX < 1 || lodY < 1 || nullptr == cubics) {
  202. return nullptr;
  203. }
  204. // check for overflow in multiplication
  205. const int64_t lodX64 = (lodX + 1),
  206. lodY64 = (lodY + 1),
  207. mult64 = lodX64 * lodY64;
  208. if (mult64 > SK_MaxS32) {
  209. return nullptr;
  210. }
  211. // Treat null interpolation space as sRGB.
  212. if (!colorSpace) {
  213. colorSpace = sk_srgb_singleton();
  214. }
  215. int vertexCount = SkToS32(mult64);
  216. // it is recommended to generate draw calls of no more than 65536 indices, so we never generate
  217. // more than 60000 indices. To accomplish that we resize the LOD and vertex count
  218. if (vertexCount > 10000 || lodX > 200 || lodY > 200) {
  219. float weightX = static_cast<float>(lodX) / (lodX + lodY);
  220. float weightY = static_cast<float>(lodY) / (lodX + lodY);
  221. // 200 comes from the 100 * 2 which is the max value of vertices because of the limit of
  222. // 60000 indices ( sqrt(60000 / 6) that comes from data->fIndexCount = lodX * lodY * 6)
  223. // Need a min of 1 since we later divide by lod
  224. lodX = std::max(1, sk_float_floor2int_no_saturate(weightX * 200));
  225. lodY = std::max(1, sk_float_floor2int_no_saturate(weightY * 200));
  226. vertexCount = (lodX + 1) * (lodY + 1);
  227. }
  228. const int indexCount = lodX * lodY * 6;
  229. uint32_t flags = 0;
  230. if (srcTexCoords) {
  231. flags |= SkVertices::kHasTexCoords_BuilderFlag;
  232. }
  233. if (srcColors) {
  234. flags |= SkVertices::kHasColors_BuilderFlag;
  235. }
  236. SkSTArenaAlloc<2048> alloc;
  237. SkPMColor4f* cornerColors = srcColors ? alloc.makeArray<SkPMColor4f>(4) : nullptr;
  238. SkPMColor4f* tmpColors = srcColors ? alloc.makeArray<SkPMColor4f>(vertexCount) : nullptr;
  239. SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vertexCount, indexCount, flags);
  240. SkPoint* pos = builder.positions();
  241. SkPoint* texs = builder.texCoords();
  242. uint16_t* indices = builder.indices();
  243. if (cornerColors) {
  244. skcolor_to_float(cornerColors, srcColors, kNumCorners, colorSpace);
  245. }
  246. SkPoint pts[kNumPtsCubic];
  247. SkPatchUtils::GetBottomCubic(cubics, pts);
  248. FwDCubicEvaluator fBottom(pts);
  249. SkPatchUtils::GetTopCubic(cubics, pts);
  250. FwDCubicEvaluator fTop(pts);
  251. SkPatchUtils::GetLeftCubic(cubics, pts);
  252. FwDCubicEvaluator fLeft(pts);
  253. SkPatchUtils::GetRightCubic(cubics, pts);
  254. FwDCubicEvaluator fRight(pts);
  255. fBottom.restart(lodX);
  256. fTop.restart(lodX);
  257. SkScalar u = 0.0f;
  258. int stride = lodY + 1;
  259. for (int x = 0; x <= lodX; x++) {
  260. SkPoint bottom = fBottom.next(), top = fTop.next();
  261. fLeft.restart(lodY);
  262. fRight.restart(lodY);
  263. SkScalar v = 0.f;
  264. for (int y = 0; y <= lodY; y++) {
  265. int dataIndex = x * (lodY + 1) + y;
  266. SkPoint left = fLeft.next(), right = fRight.next();
  267. SkPoint s0 = SkPoint::Make((1.0f - v) * top.x() + v * bottom.x(),
  268. (1.0f - v) * top.y() + v * bottom.y());
  269. SkPoint s1 = SkPoint::Make((1.0f - u) * left.x() + u * right.x(),
  270. (1.0f - u) * left.y() + u * right.y());
  271. SkPoint s2 = SkPoint::Make(
  272. (1.0f - v) * ((1.0f - u) * fTop.getCtrlPoints()[0].x()
  273. + u * fTop.getCtrlPoints()[3].x())
  274. + v * ((1.0f - u) * fBottom.getCtrlPoints()[0].x()
  275. + u * fBottom.getCtrlPoints()[3].x()),
  276. (1.0f - v) * ((1.0f - u) * fTop.getCtrlPoints()[0].y()
  277. + u * fTop.getCtrlPoints()[3].y())
  278. + v * ((1.0f - u) * fBottom.getCtrlPoints()[0].y()
  279. + u * fBottom.getCtrlPoints()[3].y()));
  280. pos[dataIndex] = s0 + s1 - s2;
  281. if (cornerColors) {
  282. bilerp(u, v, Sk4f::Load(cornerColors[kTopLeft_Corner].vec()),
  283. Sk4f::Load(cornerColors[kTopRight_Corner].vec()),
  284. Sk4f::Load(cornerColors[kBottomLeft_Corner].vec()),
  285. Sk4f::Load(cornerColors[kBottomRight_Corner].vec()))
  286. .store(tmpColors[dataIndex].vec());
  287. }
  288. if (texs) {
  289. texs[dataIndex] = SkPoint::Make(bilerp(u, v, srcTexCoords[kTopLeft_Corner].x(),
  290. srcTexCoords[kTopRight_Corner].x(),
  291. srcTexCoords[kBottomLeft_Corner].x(),
  292. srcTexCoords[kBottomRight_Corner].x()),
  293. bilerp(u, v, srcTexCoords[kTopLeft_Corner].y(),
  294. srcTexCoords[kTopRight_Corner].y(),
  295. srcTexCoords[kBottomLeft_Corner].y(),
  296. srcTexCoords[kBottomRight_Corner].y()));
  297. }
  298. if(x < lodX && y < lodY) {
  299. int i = 6 * (x * lodY + y);
  300. indices[i] = x * stride + y;
  301. indices[i + 1] = x * stride + 1 + y;
  302. indices[i + 2] = (x + 1) * stride + 1 + y;
  303. indices[i + 3] = indices[i];
  304. indices[i + 4] = indices[i + 2];
  305. indices[i + 5] = (x + 1) * stride + y;
  306. }
  307. v = SkScalarClampMax(v + 1.f / lodY, 1);
  308. }
  309. u = SkScalarClampMax(u + 1.f / lodX, 1);
  310. }
  311. if (tmpColors) {
  312. float_to_skcolor(builder.colors(), tmpColors, vertexCount, colorSpace);
  313. }
  314. return builder.detach();
  315. }