Flutter Engine
The Flutter Engine
tilemodes_scaled.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/SkRect.h"
20#include "include/core/SkSize.h"
27#include "tools/ToolUtils.h"
29
36};
37
38static void makebm(SkBitmap* bm, SkColorType ct, int w, int h) {
41
42 SkCanvas canvas(*bm);
43 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(w), SkIntToScalar(h)} };
45 SkScalar pos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
47
48 paint.setDither(true);
51 canvas.drawPaint(paint);
52}
53
54static void setup(SkPaint* paint, const SkBitmap& bm, const SkSamplingOptions& sampling,
56 paint->setShader(bm.makeShader(tmx, tmy, sampling));
57}
58
59constexpr SkColorType gColorTypes[] = {
60 kN32_SkColorType,
62};
63
64class ScaledTilingGM : public skiagm::GM {
65public:
66 ScaledTilingGM(bool powerOfTwoSize)
67 : fPowerOfTwoSize(powerOfTwoSize) {
68 }
69
71
72protected:
73 static constexpr int kPOTSize = 4;
74 static constexpr int kNPOTSize = 3;
75
76 SkString getName() const override {
77 SkString name("scaled_tilemodes");
78 if (!fPowerOfTwoSize) {
79 name.append("_npot");
80 }
81 return name;
82 }
83
84 SkISize getISize() override { return SkISize::Make(880, 880); }
85
86 void onOnceBeforeDraw() override {
87 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
88 for (size_t i = 0; i < std::size(gColorTypes); i++) {
90 }
91 }
92
93 void onDraw(SkCanvas* canvas) override {
94 SkPaint textPaint;
96
97 float scale = 32.f/kPOTSize;
98
99 int size = fPowerOfTwoSize ? kPOTSize : kNPOTSize;
100
101 SkRect r = { 0, 0, SkIntToScalar(size*2), SkIntToScalar(size*2) };
102
103 const char* gColorTypeNames[] = { "8888", "565" };
104
105 const char* gFilterNames[] = { "Nearest", "Linear", "Trilinear", "Mitchell", "Aniso" };
106
107 constexpr SkTileMode gModes[] = {
109 const char* gModeNames[] = { "C", "R", "M" };
110
113
114 for (size_t kx = 0; kx < std::size(gModes); kx++) {
115 for (size_t ky = 0; ky < std::size(gModes); ky++) {
116 SkString str;
117 str.printf("[%s,%s]", gModeNames[kx], gModeNames[ky]);
118
119 SkTextUtils::DrawString(canvas, str.c_str(), scale*(x + r.width()/2), y, font, SkPaint(),
121
122 x += r.width() * 4 / 3;
123 }
124 }
125
126 y = SkIntToScalar(40) / scale;
127
128 for (size_t i = 0; i < std::size(gColorTypes); i++) {
129 for (size_t j = 0; j < std::size(gSamplings); j++) {
130 x = SkIntToScalar(10)/scale;
131 for (size_t kx = 0; kx < std::size(gModes); kx++) {
132 for (size_t ky = 0; ky < std::size(gModes); ky++) {
134#if 1 // Temporary change to regen bitmap before each draw. This may help tracking down an issue
135 // on SGX where resizing NPOT textures to POT textures exhibits a driver bug.
136 if (!fPowerOfTwoSize) {
138 }
139#endif
140 setup(&paint, fTexture[i], gSamplings[j], gModes[kx], gModes[ky]);
141 paint.setDither(true);
142
143 canvas->save();
144 canvas->scale(scale,scale);
145 canvas->translate(x, y);
146 canvas->drawRect(r, paint);
147 canvas->restore();
148
149 x += r.width() * 4 / 3;
150 }
151 }
152 canvas->drawString(SkStringPrintf("%s, %s", gColorTypeNames[i], gFilterNames[j]),
153 scale * x, scale * (y + r.height() * 2 / 3), font, textPaint);
154
155 y += r.height() * 4 / 3;
156 }
157 }
158 }
159
160private:
161 bool fPowerOfTwoSize;
162 using INHERITED = skiagm::GM;
163};
164
165constexpr int gWidth = 32;
166constexpr int gHeight = 32;
167
169 SkBitmap bm;
170 makebm(&bm, kN32_SkColorType, gWidth, gHeight);
171 return bm.makeShader(tx, ty, SkSamplingOptions());
172}
173
175 SkPoint pts[] = { { 0, 0 }, { SkIntToScalar(gWidth), SkIntToScalar(gHeight)} };
178 SkColor colors[] = {0xFFFF0000, ToolUtils::color_to_565(0xFF0044FF)};
179
180 int index = (int)ty;
181 switch (index % 3) {
182 case 0:
183 return SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors), tx);
184 case 1:
185 return SkGradientShader::MakeRadial(center, rad, colors, nullptr, std::size(colors), tx);
186 case 2:
187 return SkGradientShader::MakeSweep(center.fX, center.fY, colors, nullptr,
188 std::size(colors), tx, 135, 225, 0, nullptr);
189 }
190
191 return nullptr;
192}
193
195
197 ShaderProc fProc;
198 const char* fName;
199public:
200 ScaledTiling2GM(ShaderProc proc, const char name[]) : fProc(proc), fName(name) {}
201
202private:
203 SkString getName() const override { return SkString(fName); }
204
205 SkISize getISize() override { return SkISize::Make(650, 610); }
206
207 void onDraw(SkCanvas* canvas) override {
208 canvas->scale(SkIntToScalar(3)/2, SkIntToScalar(3)/2);
209
212 SkRect r = { -w, -h, w*2, h*2 };
213
214 constexpr SkTileMode gModes[] = {
216 };
217 const char* gModeNames[] = {
218 "Clamp", "Repeat", "Mirror"
219 };
220
223
225
226 for (size_t kx = 0; kx < std::size(gModes); kx++) {
227 SkString str(gModeNames[kx]);
228 SkTextUtils::DrawString(canvas, str.c_str(), x + r.width()/2, y, font, SkPaint(),
230 x += r.width() * 4 / 3;
231 }
232
233 y += SkIntToScalar(16) + h;
234
235 for (size_t ky = 0; ky < std::size(gModes); ky++) {
236 x = SkIntToScalar(16) + w;
237
238 SkString str(gModeNames[ky]);
239 SkTextUtils::DrawString(canvas, str.c_str(), x, y + h/2, font, SkPaint(), SkTextUtils::kRight_Align);
240
241 x += SkIntToScalar(50);
242 for (size_t kx = 0; kx < std::size(gModes); kx++) {
244 paint.setShader(fProc(gModes[kx], gModes[ky]));
245
246 canvas->save();
247 canvas->translate(x, y);
248 canvas->drawRect(r, paint);
249 canvas->restore();
250
251 x += r.width() * 4 / 3;
252 }
253 y += r.height() * 4 / 3;
254 }
255 }
256};
257
258//////////////////////////////////////////////////////////////////////////////
259
260DEF_GM( return new ScaledTilingGM(true); )
261DEF_GM( return new ScaledTilingGM(false); )
262DEF_GM( return new ScaledTiling2GM(make_bm, "scaled_tilemode_bitmap"); )
263DEF_GM( return new ScaledTiling2GM(make_grad, "scaled_tilemode_gradient"); )
const char * fName
SkPoint pos
IsFiniteProc fProc
Definition: MathBench.cpp:219
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition: SkAlphaType.h:29
SkColorType
Definition: SkColorType.h:19
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition: SkColorType.h:22
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorTRANSPARENT
Definition: SkColor.h:99
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
#define SK_Scalar1
Definition: SkScalar.h:18
#define SkIntToScalar(x)
Definition: SkScalar.h:57
SK_API SkString SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
SkTileMode
Definition: SkTileMode.h:13
static SkScalar center(float pos0, float pos1)
constexpr SkBlendMode gModes[]
Definition: aarectmodes.cpp:73
ScaledTiling2GM(ShaderProc proc, const char name[])
void onDraw(SkCanvas *canvas) override
SkString getName() const override
ScaledTilingGM(bool powerOfTwoSize)
void onOnceBeforeDraw() override
static constexpr int kPOTSize
SkBitmap fTexture[std::size(gColorTypes)]
SkISize getISize() override
static constexpr int kNPOTSize
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:258
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition: SkBitmap.cpp:669
void eraseColor(SkColor4f) const
Definition: SkBitmap.cpp:442
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
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
Definition: SkFont.h:35
static sk_sp< SkShader > MakeSweep(SkScalar cx, SkScalar cy, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, SkScalar startAngle, SkScalar endAngle, uint32_t flags, const SkMatrix *localMatrix)
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
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)
void printf(const char format[],...) SK_PRINTF_LIKE(2
Definition: SkString.cpp:534
const char * c_str() const
Definition: SkString.h:133
static void DrawString(SkCanvas *canvas, const char text[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint, Align align=kLeft_Align)
Definition: SkTextUtils.h:34
Definition: gm.h:110
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
#define DEF_GM(CODE)
Definition: gm.h:40
double y
double x
PODArray< SkColor > colors
Definition: SkRecords.h:276
SkSamplingOptions sampling
Definition: SkRecords.h:337
sk_sp< SkTypeface > DefaultPortableTypeface()
SkFont DefaultPortableFont()
SkColor color_to_565(SkColor color)
Definition: ToolUtils.cpp:139
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
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.
SkSamplingOptions(SkFilterMode::kLinear))
SkTileMode tmy
SkTileMode tmx
SkScalar w
SkScalar h
const Scalar scale
static constexpr SkCubicResampler Mitchell()
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
constexpr float height() const
Definition: SkRect.h:769
constexpr float width() const
Definition: SkRect.h:762
static constexpr SkSamplingOptions Aniso(int maxAniso)
static sk_sp< SkShader > make_grad(SkTileMode tx, SkTileMode ty)
static void makebm(SkBitmap *bm, SkColorType ct, int w, int h)
static sk_sp< SkShader > make_bm(SkTileMode tx, SkTileMode ty)
constexpr int gHeight
constexpr SkColorType gColorTypes[]
static void setup(SkPaint *paint, const SkBitmap &bm, const SkSamplingOptions &sampling, SkTileMode tmx, SkTileMode tmy)
constexpr int gWidth
const SkSamplingOptions gSamplings[]
sk_sp< SkShader >(* ShaderProc)(SkTileMode, SkTileMode)