Flutter Engine
The Flutter Engine
Public Types | Static Public Member Functions | List of all members
SkPatchUtils Class Reference

#include <SkPatchUtils.h>

Public Types

enum  { kNumCtrlPts = 12 , kNumCorners = 4 , kNumPtsCubic = 4 }
 

Static Public Member Functions

static void GetTopCubic (const SkPoint cubics[12], SkPoint points[4])
 
static void GetBottomCubic (const SkPoint cubics[12], SkPoint points[4])
 
static void GetLeftCubic (const SkPoint cubics[12], SkPoint points[4])
 
static void GetRightCubic (const SkPoint cubics[12], SkPoint points[4])
 
static SkISize GetLevelOfDetail (const SkPoint cubics[12], const SkMatrix *matrix)
 
static sk_sp< SkVerticesMakeVertices (const SkPoint cubics[12], const SkColor colors[4], const SkPoint texCoords[4], int lodX, int lodY, SkColorSpace *colorSpace=nullptr)
 

Detailed Description

Definition at line 20 of file SkPatchUtils.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
kNumCtrlPts 
kNumCorners 
kNumPtsCubic 

Definition at line 24 of file SkPatchUtils.h.

24 {
25 kNumCtrlPts = 12,
26 kNumCorners = 4,
27 kNumPtsCubic = 4
28 };

Member Function Documentation

◆ GetBottomCubic()

void SkPatchUtils::GetBottomCubic ( const SkPoint  cubics[12],
SkPoint  points[4] 
)
static

Get the points corresponding to the bottom cubic of cubics.

Definition at line 219 of file SkPatchUtils.cpp.

219 {
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}
static const int points[]

◆ GetLeftCubic()

void SkPatchUtils::GetLeftCubic ( const SkPoint  cubics[12],
SkPoint  points[4] 
)
static

Get the points corresponding to the left cubic of cubics.

Definition at line 226 of file SkPatchUtils.cpp.

226 {
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}

◆ GetLevelOfDetail()

SkISize SkPatchUtils::GetLevelOfDetail ( const SkPoint  cubics[12],
const SkMatrix matrix 
)
static

Method that calculates a level of detail (number of subdivisions) for a patch in both axis.

Definition at line 182 of file SkPatchUtils.cpp.

182 {
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}
static const int kPartitionSize
static SkScalar approx_arc_length(const SkPoint points[], int count)
static void GetTopCubic(const SkPoint cubics[12], SkPoint points[4])
static void GetRightCubic(const SkPoint cubics[12], SkPoint points[4])
static void GetLeftCubic(const SkPoint cubics[12], SkPoint points[4])
static void GetBottomCubic(const SkPoint cubics[12], SkPoint points[4])
float SkScalar
Definition: extension.cpp:12
static float max(float r, float g, float b)
Definition: hsl.cpp:49
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20

◆ GetRightCubic()

void SkPatchUtils::GetRightCubic ( const SkPoint  cubics[12],
SkPoint  points[4] 
)
static

Get the points corresponding to the right cubic of cubics.

Definition at line 233 of file SkPatchUtils.cpp.

233 {
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}

◆ GetTopCubic()

void SkPatchUtils::GetTopCubic ( const SkPoint  cubics[12],
SkPoint  points[4] 
)
static

Get the points corresponding to the top cubic of cubics.

Definition at line 212 of file SkPatchUtils.cpp.

212 {
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}

◆ MakeVertices()

sk_sp< SkVertices > SkPatchUtils::MakeVertices ( const SkPoint  cubics[12],
const SkColor  colors[4],
const SkPoint  texCoords[4],
int  lodX,
int  lodY,
SkColorSpace colorSpace = nullptr 
)
static

Definition at line 256 of file SkPatchUtils.cpp.

258 {
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}
SkPoint pos
SkColorSpace * sk_srgb_singleton()
#define sk_float_floor2int_no_saturate(x)
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 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 bool left(const SkPoint &p0, const SkPoint &p1)
static bool right(const SkPoint &p0, const SkPoint &p1)
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
T * makeArray(size_t count)
Definition: SkArenaAlloc.h:181
@ kHasTexCoords_BuilderFlag
Definition: SkVertices.h:63
@ kHasColors_BuilderFlag
Definition: SkVertices.h:64
@ kTriangles_VertexMode
Definition: SkVertices.h:31
FlutterSemanticsFlag flags
double y
double x
PODArray< SkRect > texs
Definition: SkRecords.h:333
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
constexpr float y() const
Definition: SkPoint_impl.h:187
constexpr float x() const
Definition: SkPoint_impl.h:181
static SKVX_ALWAYS_INLINE Vec Load(const void *ptr)
Definition: SkVx.h:109

The documentation for this class was generated from the following files: