Flutter Engine
The Flutter Engine
Classes | Functions | Variables
vertices.cpp File Reference
#include "gm/gm.h"
#include "include/core/SkBlendMode.h"
#include "include/core/SkCanvas.h"
#include "include/core/SkColor.h"
#include "include/core/SkColorFilter.h"
#include "include/core/SkMatrix.h"
#include "include/core/SkPaint.h"
#include "include/core/SkPoint.h"
#include "include/core/SkRefCnt.h"
#include "include/core/SkScalar.h"
#include "include/core/SkShader.h"
#include "include/core/SkSize.h"
#include "include/core/SkString.h"
#include "include/core/SkTileMode.h"
#include "include/core/SkTypes.h"
#include "include/core/SkVertices.h"
#include "include/effects/SkGradientShader.h"
#include "include/effects/SkRuntimeEffect.h"
#include "include/private/base/SkTDArray.h"
#include "src/base/SkRandom.h"
#include "src/core/SkVerticesPriv.h"
#include "src/shaders/SkLocalMatrixShader.h"
#include "src/utils/SkPatchUtils.h"
#include "tools/DecodeUtils.h"
#include "tools/Resources.h"
#include "tools/ToolUtils.h"
#include <initializer_list>
#include <utility>

Go to the source code of this file.

Classes

class  VerticesGM
 

Functions

static sk_sp< SkShadermake_shader1 (SkScalar shaderScale)
 
static sk_sp< SkShadermake_shader2 ()
 
static sk_sp< SkColorFiltermake_color_filter ()
 
static void fill_mesh (SkPoint pts[kMeshVertexCnt], SkPoint texs[kMeshVertexCnt], SkColor colors[kMeshVertexCnt], SkScalar shaderScale)
 
static void draw_batching (SkCanvas *canvas)
 
 DEF_SIMPLE_GM (vertices_batching, canvas, 100, 500)
 
 DEF_SIMPLE_GM (vertices_perspective, canvas, 256, 256)
 
 DEF_SIMPLE_GM (skbug_13047, canvas, 200, 200)
 
 DEF_SIMPLE_GM_BG (vertices_collapsed, canvas, 50, 50, SK_ColorWHITE)
 

Variables

static constexpr SkScalar kShaderSize = 40
 
static constexpr SkScalar kMeshSize = 30
 
static constexpr uint16_t kMeshFan []
 
static const int kMeshIndexCnt = (int)std::size(kMeshFan)
 
static const int kMeshVertexCnt = 9
 

Function Documentation

◆ DEF_SIMPLE_GM() [1/3]

DEF_SIMPLE_GM ( skbug_13047  ,
canvas  ,
200  ,
200   
)

Definition at line 310 of file vertices.cpp.

310 {
311 auto image = ToolUtils::GetResourceAsImage("images/mandrill_128.png");
312
313 const float w = image->width();
314 const float h = image->height();
315
316 SkPoint verts[] = {{0, 0}, {200, 0}, {200, 200}, {0, 200}};
317 SkPoint texs[] = {{0, 0}, {w, 0}, {w, h}, {0, h}};
318 uint16_t indices[] = {0, 1, 2, 2, 3, 0};
319
320 auto v = SkVertices::MakeCopy(
321 SkVertices::kTriangles_VertexMode, 4, verts, texs, nullptr, 6, indices);
322
323 auto m = SkMatrix::Scale(2, 2); // ignored in CPU ???
325
326 SkPaint p;
327 p.setShader(s);
328
329 canvas->drawVertices(v, SkBlendMode::kModulate, p);
330}
@ kModulate
r = s*d
int width() const
Definition: SkImage.h:285
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkImage.cpp:179
int height() const
Definition: SkImage.h:291
static SkMatrix Scale(SkScalar sx, SkScalar sy)
Definition: SkMatrix.h:75
static sk_sp< SkVertices > MakeCopy(VertexMode mode, int vertexCount, const SkPoint positions[], const SkPoint texs[], const SkColor colors[], int indexCount, const uint16_t indices[])
Definition: SkVertices.cpp:200
@ kTriangles_VertexMode
Definition: SkVertices.h:31
struct MyStruct s
PODArray< SkRect > texs
Definition: SkRecords.h:333
sk_sp< const SkImage > image
Definition: SkRecords.h:269
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition: DecodeUtils.h:25
SkSamplingOptions(SkFilterMode::kLinear))
SkScalar w
SkScalar h

◆ DEF_SIMPLE_GM() [2/3]

DEF_SIMPLE_GM ( vertices_batching  ,
canvas  ,
100  ,
500   
)

Definition at line 265 of file vertices.cpp.

265 {
266 draw_batching(canvas);
267 canvas->translate(50, 0);
268 draw_batching(canvas);
269}
static void draw_batching(SkCanvas *canvas)
Definition: vertices.cpp:211

◆ DEF_SIMPLE_GM() [3/3]

DEF_SIMPLE_GM ( vertices_perspective  ,
canvas  ,
256  ,
256   
)

Definition at line 273 of file vertices.cpp.

273 {
276
277 SkRect r = SkRect::MakeWH(128, 128);
278
279 SkPoint pos[4];
280 r.toQuad(pos);
282
283 SkMatrix persp;
284 persp.setPerspY(SK_Scalar1 / 100);
285
286 canvas->save();
287 canvas->concat(persp);
288 canvas->drawRect(r, paint);
289 canvas->restore();
290
291 canvas->save();
292 canvas->translate(r.width(), 0);
293 canvas->concat(persp);
294 canvas->drawRect(r, paint);
295 canvas->restore();
296
297 canvas->save();
298 canvas->translate(0, r.height());
299 canvas->concat(persp);
300 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
301 canvas->restore();
302
303 canvas->save();
304 canvas->translate(r.width(), r.height());
305 canvas->concat(persp);
306 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
307 canvas->restore();
308}
SkPoint pos
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
#define SK_Scalar1
Definition: SkScalar.h:18
SkMatrix & setPerspY(SkScalar v)
Definition: SkMatrix.h:544
@ kTriangleFan_VertexMode
Definition: SkVertices.h:33
const Paint & paint
Definition: color_source.cc:38
sk_sp< SkShader > create_checkerboard_shader(SkColor c1, SkColor c2, int size)
Definition: ToolUtils.cpp:150
void toQuad(SkPoint quad[4]) const
Definition: SkRect.cpp:50
constexpr float height() const
Definition: SkRect.h:769
constexpr float width() const
Definition: SkRect.h:762
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609

◆ DEF_SIMPLE_GM_BG()

DEF_SIMPLE_GM_BG ( vertices_collapsed  ,
canvas  ,
50  ,
50  ,
SK_ColorWHITE   
)

Definition at line 334 of file vertices.cpp.

334 {
335 SkPoint verts[] = {{5, 5}, {45, 5}, {45, 45}, {5, 45}};
336 SkPoint texs[] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
337 uint16_t indices[] = {0, 1, 2, 2, 3, 0};
338
340 SkVertices::kTriangles_VertexMode, 4, verts, texs, nullptr, 6, indices);
341
343 surf->getCanvas()->clear(SK_ColorGREEN);
346 paint.setShader(shader);
347
348 canvas->drawVertices(v, SkBlendMode::kDst, paint);
349}
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
void clear(SkColor color)
Definition: SkCanvas.h:1199
SkCanvas * getCanvas()
Definition: SkSurface.cpp:82
sk_sp< SkImage > makeImageSnapshot()
Definition: SkSurface.cpp:90
SK_API sk_sp< SkSurface > Raster(const SkImageInfo &imageInfo, size_t rowBytes, const SkSurfaceProps *surfaceProps)
static SkImageInfo MakeN32Premul(int width, int height)

◆ draw_batching()

static void draw_batching ( SkCanvas canvas)
static

Definition at line 211 of file vertices.cpp.

211 {
212 // Triangle fans can't batch so we convert to regular triangles,
213 static constexpr int kNumTris = kMeshIndexCnt - 2;
217
218 SkPoint* pts = builder.positions();
219 SkPoint* texs = builder.texCoords();
220 SkColor* colors = builder.colors();
221 fill_mesh(pts, texs, colors, 1);
222
223 SkTDArray<SkMatrix> matrices;
224 matrices.append()->reset();
225 matrices.append()->setTranslate(0, 40);
226 matrices.append()
227 ->setRotate(45, kMeshSize / 2, kMeshSize / 2)
228 .postScale(1.2f, .8f, kMeshSize / 2, kMeshSize / 2)
229 .postTranslate(0, 80);
230
231 auto shader = make_shader1(1);
232
233 uint16_t* indices = builder.indices();
234 for (size_t i = 0; i < kNumTris; ++i) {
235 indices[3 * i] = kMeshFan[0];
236 indices[3 * i + 1] = kMeshFan[i + 1];
237 indices[3 * i + 2] = kMeshFan[i + 2];
238
239 }
240
241 canvas->save();
242 canvas->translate(10, 10);
243 for (bool useShader : {false, true}) {
244 for (bool useTex : {false, true}) {
245 for (const auto& m : matrices) {
246 canvas->save();
247 canvas->concat(m);
249 paint.setShader(useShader ? shader : nullptr);
250 paint.setColor(SK_ColorWHITE);
251
252 const SkPoint* t = useTex ? texs : nullptr;
254 pts, t, colors, kNumTris * 3, indices);
256 canvas->restore();
257 }
258 canvas->translate(0, 120);
259 }
260 }
261 canvas->restore();
262}
uint32_t SkColor
Definition: SkColor.h:37
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
int save()
Definition: SkCanvas.cpp:447
void concat(const SkMatrix &matrix)
Definition: SkCanvas.cpp:1318
void drawVertices(const SkVertices *vertices, SkBlendMode mode, const SkPaint &paint)
Definition: SkCanvas.cpp:1720
T * append()
Definition: SkTDArray.h:191
@ kHasTexCoords_BuilderFlag
Definition: SkVertices.h:63
@ kHasColors_BuilderFlag
Definition: SkVertices.h:64
PODArray< SkColor > colors
Definition: SkRecords.h:276
static sk_sp< SkShader > make_shader1(SkScalar shaderScale)
Definition: vertices.cpp:39
static const int kMeshIndexCnt
Definition: vertices.cpp:73
static void fill_mesh(SkPoint pts[kMeshVertexCnt], SkPoint texs[kMeshVertexCnt], SkColor colors[kMeshVertexCnt], SkScalar shaderScale)
Definition: vertices.cpp:76
static const int kMeshVertexCnt
Definition: vertices.cpp:74
static constexpr SkScalar kMeshSize
Definition: vertices.cpp:65
static constexpr uint16_t kMeshFan[]
Definition: vertices.cpp:68

◆ fill_mesh()

static void fill_mesh ( SkPoint  pts[kMeshVertexCnt],
SkPoint  texs[kMeshVertexCnt],
SkColor  colors[kMeshVertexCnt],
SkScalar  shaderScale 
)
static

Definition at line 76 of file vertices.cpp.

77 {
78 pts[0].set(0, 0);
79 pts[1].set(kMeshSize / 2, 3);
80 pts[2].set(kMeshSize, 0);
81 pts[3].set(3, kMeshSize / 2);
82 pts[4].set(kMeshSize / 2, kMeshSize / 2);
83 pts[5].set(kMeshSize - 3, kMeshSize / 2);
84 pts[6].set(0, kMeshSize);
85 pts[7].set(kMeshSize / 2, kMeshSize - 3);
86 pts[8].set(kMeshSize, kMeshSize);
87
88 const auto shaderSize = kShaderSize * shaderScale;
89 texs[0].set(0, 0);
90 texs[1].set(shaderSize / 2, 0);
91 texs[2].set(shaderSize, 0);
92 texs[3].set(0, shaderSize / 2);
93 texs[4].set(shaderSize / 2, shaderSize / 2);
94 texs[5].set(shaderSize, shaderSize / 2);
95 texs[6].set(0, shaderSize);
96 texs[7].set(shaderSize / 2, shaderSize);
97 texs[8].set(shaderSize, shaderSize);
98
99 SkRandom rand;
100 for (size_t i = 0; i < kMeshVertexCnt; ++i) {
101 colors[i] = rand.nextU() | 0xFF000000;
102 }
103}
uint32_t nextU()
Definition: SkRandom.h:42
void set(float x, float y)
Definition: SkPoint_impl.h:200
static constexpr SkScalar kShaderSize
Definition: vertices.cpp:38

◆ make_color_filter()

static sk_sp< SkColorFilter > make_color_filter ( )
static

Definition at line 61 of file vertices.cpp.

61 {
63}
@ kDarken
rc = s + d - max(s*da, d*sa), ra = kSrcOver
static sk_sp< SkColorFilter > Blend(const SkColor4f &c, sk_sp< SkColorSpace >, SkBlendMode mode)

◆ make_shader1()

static sk_sp< SkShader > make_shader1 ( SkScalar  shaderScale)
static

Definition at line 39 of file vertices.cpp.

39 {
40 const SkColor colors[] = {
43 };
44 const SkPoint pts[] = {{kShaderSize / 4, 0}, {3 * kShaderSize / 4, kShaderSize}};
45 const SkMatrix localMatrix = SkMatrix::Scale(shaderScale, shaderScale);
46
50 &localMatrix);
51 // Throw in a couple of local matrix wrappers for good measure.
52 return shaderScale == 1 ? grad
55}
constexpr SkColor SK_ColorYELLOW
Definition: SkColor.h:139
constexpr SkColor SK_ColorMAGENTA
Definition: SkColor.h:147
constexpr SkColor SK_ColorCYAN
Definition: SkColor.h:143
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static SkMatrix Translate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.h:91
sk_sp< SkShader > makeWithLocalMatrix(const SkMatrix &) const
Definition: SkShader.cpp:26
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259

◆ make_shader2()

static sk_sp< SkShader > make_shader2 ( )
static

Definition at line 57 of file vertices.cpp.

57 {
59}
SK_API sk_sp< SkShader > Color(SkColor)

Variable Documentation

◆ kMeshFan

constexpr uint16_t kMeshFan[]
staticconstexpr
Initial value:
= {
4,
0, 1, 2, 5, 8, 7, 6, 3, 0
}

Definition at line 68 of file vertices.cpp.

◆ kMeshIndexCnt

const int kMeshIndexCnt = (int)std::size(kMeshFan)
static

Definition at line 73 of file vertices.cpp.

◆ kMeshSize

constexpr SkScalar kMeshSize = 30
staticconstexpr

Definition at line 65 of file vertices.cpp.

◆ kMeshVertexCnt

const int kMeshVertexCnt = 9
static

Definition at line 74 of file vertices.cpp.

◆ kShaderSize

constexpr SkScalar kShaderSize = 40
staticconstexpr

Definition at line 38 of file vertices.cpp.