Flutter Engine
The Flutter Engine
shaderpath.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2011 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 "gm/gm.h"
12#include "include/core/SkFont.h"
16#include "include/core/SkPath.h"
20#include "include/core/SkSize.h"
25#include "tools/ToolUtils.h"
26
27#include <string.h>
28
29namespace skiagm {
30
31static void makebm(SkBitmap* bm, int w, int h) {
32 bm->allocN32Pixels(w, h);
34
35 SkCanvas canvas(*bm);
37 const SkPoint kPts0[] = { { 0, 0 }, { s, s } };
38 const SkPoint kPts1[] = { { s/2, 0 }, { s/2, s } };
39 const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
40 const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
41 const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 };
42
43
45
46 paint.setShader(SkGradientShader::MakeLinear(kPts0, kColors0, kPos,
47 std::size(kColors0), SkTileMode::kClamp));
48 canvas.drawPaint(paint);
49 paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos,
50 std::size(kColors1), SkTileMode::kClamp));
51 canvas.drawPaint(paint);
52}
53
54///////////////////////////////////////////////////////////////////////////////
55
58 const char* fLabel;
59};
60
61constexpr int kPointSize = 300;
62
63class ShaderPathGM : public GM {
64public:
66 this->setBGColor(0xFFDDDDDD);
67 }
68
69protected:
70 SkString getName() const override { return SkString("shaderpath"); }
71
72 SkISize getISize() override { return SkISize::Make(820, 930); }
73
74 void onOnceBeforeDraw() override {
75 makebm(&fBmp, kPointSize / 4, kPointSize / 4);
76 }
77
78 void onDraw(SkCanvas* canvas) override {
79
80 SkPaint bmpPaint;
81 bmpPaint.setAntiAlias(true);
82 bmpPaint.setAlphaf(0.5f);
84
85 canvas->drawImage(fBmp.asImage(), 5.f, 5.f, sampling, &bmpPaint);
86
87 SkPaint outlinePaint;
88 outlinePaint.setStyle(SkPaint::kStroke_Style);
89 outlinePaint.setStrokeWidth(0.f);
90
91 canvas->translate(15.f, 15.f);
92 canvas->scale(2.f, 2.f);
93
94 constexpr SkTileMode kTileModes[] = {
97 };
98
99 // position the baseline of the first path
100 canvas->translate(0.f, 2.25);
101
102 SkPath path;
103 path.moveTo(0, 40).cubicTo(10, 70, 20, 10, 30, 40);
104
105 canvas->save();
106 int i = 0;
107 for (size_t tm0 = 0; tm0 < std::size(kTileModes); ++tm0) {
108 for (size_t tm1 = 0; tm1 < std::size(kTileModes); ++tm1) {
109 SkMatrix localM;
110 localM.setTranslate(5.f, 5.f);
111 localM.postRotate(20);
112 localM.postScale(1.15f, .85f);
113
114 SkPaint fillPaint;
115 fillPaint.setAntiAlias(true);
116 fillPaint.setShader(fBmp.makeShader(kTileModes[tm0], kTileModes[tm1],
117 sampling, localM));
118
119 canvas->drawPath(path, fillPaint);
120 canvas->drawPath(path, outlinePaint);
121 canvas->translate(50.f, 0.f);
122 ++i;
123 if (!(i % 2)) {
124 canvas->restore();
125 canvas->translate(0, 22.5f);
126 canvas->save();
127 }
128 }
129 }
130 canvas->restore();
131 }
132
133private:
134 SkBitmap fBmp;
135 using INHERITED = GM;
136};
137
138///////////////////////////////////////////////////////////////////////////////
139
140DEF_GM( return new ShaderPathGM; )
141} // namespace skiagm
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorTRANSPARENT
Definition: SkColor.h:99
#define SK_Scalar1
Definition: SkScalar.h:18
#define SkIntToScalar(x)
Definition: SkScalar.h:57
SkTileMode
Definition: SkTileMode.h:13
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkBitmap.cpp:669
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition: SkBitmap.cpp:232
void eraseColor(SkColor4f) const
Definition: SkBitmap.cpp:442
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawPaint(const SkPaint &paint)
Definition: SkCanvas.cpp:1668
int save()
Definition: SkCanvas.cpp:447
void drawPath(const SkPath &path, const SkPaint &paint)
Definition: SkCanvas.cpp:1747
void scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
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)
SkMatrix & postRotate(SkScalar degrees, SkScalar px, SkScalar py)
Definition: SkMatrix.cpp:474
SkMatrix & postScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py)
Definition: SkMatrix.cpp:360
SkMatrix & setTranslate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.cpp:254
void setStyle(Style style)
Definition: SkPaint.cpp:105
void setAntiAlias(bool aa)
Definition: SkPaint.h:170
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
void setShader(sk_sp< SkShader > shader)
void setStrokeWidth(SkScalar width)
Definition: SkPaint.cpp:159
void setAlphaf(float a)
Definition: SkPaint.cpp:130
Definition: SkPath.h:59
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
void setBGColor(SkColor)
Definition: gm.cpp:159
void onOnceBeforeDraw() override
Definition: shaderpath.cpp:74
SkISize getISize() override
Definition: shaderpath.cpp:72
void onDraw(SkCanvas *canvas) override
Definition: shaderpath.cpp:78
SkString getName() const override
Definition: shaderpath.cpp:70
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
struct MyStruct s
static float min(float r, float g, float b)
Definition: hsl.cpp:48
SkSamplingOptions sampling
Definition: SkRecords.h:337
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
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
constexpr int kPointSize
Definition: shaderpath.cpp:61
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
static void makebm(SkBitmap *bm, int w, int h)
Definition: shaderpath.cpp:31
SkScalar w
SkScalar h
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
const char * fLabel
Definition: shaderpath.cpp:58