Flutter Engine
The Flutter Engine
shadertext3.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"
19#include "include/core/SkSize.h"
25#include "tools/ToolUtils.h"
27
28#include <string.h>
29
30namespace skiagm {
31
32static void makebm(SkBitmap* bm, int w, int h) {
33 bm->allocN32Pixels(w, h);
35
36 SkCanvas canvas(*bm);
38 const SkPoint kPts0[] = { { 0, 0 }, { s, s } };
39 const SkPoint kPts1[] = { { s/2, 0 }, { s/2, s } };
40 const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
41 const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
42 const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 };
43
44
46
47 paint.setShader(SkGradientShader::MakeLinear(kPts0, kColors0, kPos,
48 std::size(kColors0), SkTileMode::kClamp));
49 canvas.drawPaint(paint);
50 paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos,
51 std::size(kColors1), SkTileMode::kClamp));
52 canvas.drawPaint(paint);
53}
54
55///////////////////////////////////////////////////////////////////////////////
56
57struct LabeledMatrix {
59 const char* fLabel;
60};
61
62constexpr int kPointSize = 300;
63
64class ShaderText3GM : public GM {
65public:
67 this->setBGColor(0xFFDDDDDD);
68 }
69
70protected:
71 SkString getName() const override { return SkString("shadertext3"); }
72
73 SkISize getISize() override { return SkISize::Make(820, 930); }
74
75 void onOnceBeforeDraw() override {
76 makebm(&fBmp, kPointSize / 4, kPointSize / 4);
77 }
78
79 void onDraw(SkCanvas* canvas) override {
80
81 SkPaint bmpPaint;
82 bmpPaint.setAntiAlias(true);
83 bmpPaint.setAlphaf(0.5f);
85
86 canvas->drawImage(fBmp.asImage(), 5.f, 5.f, sampling, &bmpPaint);
87
89 SkPaint outlinePaint;
90 outlinePaint.setStyle(SkPaint::kStroke_Style);
91 outlinePaint.setStrokeWidth(0.f);
92
93 canvas->translate(15.f, 15.f);
94
95 // draw glyphs scaled up
96 canvas->scale(2.f, 2.f);
97
98 constexpr SkTileMode kTileModes[] = {
101 };
102
103 // position the baseline of the first run
104 canvas->translate(0.f, 0.75f * kPointSize);
105
106 canvas->save();
107 int i = 0;
108 for (size_t tm0 = 0; tm0 < std::size(kTileModes); ++tm0) {
109 for (size_t tm1 = 0; tm1 < std::size(kTileModes); ++tm1) {
110 SkMatrix localM;
111 localM.setTranslate(5.f, 5.f);
112 localM.postRotate(20);
113 localM.postScale(1.15f, .85f);
114
115 SkPaint fillPaint;
116 fillPaint.setAntiAlias(true);
117 fillPaint.setShader(fBmp.makeShader(kTileModes[tm0], kTileModes[tm1],
118 sampling, localM));
119
120 constexpr char kText[] = "B";
121 canvas->drawString(kText, 0, 0, font, fillPaint);
122 canvas->drawString(kText, 0, 0, font, outlinePaint);
123 SkScalar w = font.measureText(kText, strlen(kText), SkTextEncoding::kUTF8);
124 canvas->translate(w + 10.f, 0.f);
125 ++i;
126 if (!(i % 2)) {
127 canvas->restore();
128 canvas->translate(0, 0.75f * kPointSize);
129 canvas->save();
130 }
131 }
132 }
133 canvas->restore();
134 }
135
136private:
137 SkBitmap fBmp;
138 using INHERITED = GM;
139};
140
141///////////////////////////////////////////////////////////////////////////////
142
143DEF_GM( return new ShaderText3GM; )
144} // namespace skiagm
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorTRANSPARENT
Definition: SkColor.h:99
@ kUTF8
uses bytes to represent UTF-8 or ASCII
#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 scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.h:1803
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
Definition: SkFont.h:35
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: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
void setBGColor(SkColor)
Definition: gm.cpp:159
SkISize getISize() override
Definition: shadertext3.cpp:73
SkString getName() const override
Definition: shadertext3.cpp:71
void onDraw(SkCanvas *canvas) override
Definition: shadertext3.cpp:79
void onOnceBeforeDraw() override
Definition: shadertext3.cpp:75
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
struct MyStruct s
constexpr char kText[]
Definition: glyph_pos.cpp:28
static float min(float r, float g, float b)
Definition: hsl.cpp:48
SkSamplingOptions sampling
Definition: SkRecords.h:337
sk_sp< SkTypeface > DefaultPortableTypeface()
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
font
Font Metadata and Metrics.
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