Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
xfermodes3.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2013 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"
15#include "include/core/SkFont.h"
20#include "include/core/SkRect.h"
24#include "include/core/SkSize.h"
31#include "tools/ToolUtils.h"
33
34#include <string.h>
35
36namespace skiagm {
37
38/**
39 * This tests drawing device-covering rects with solid colors and bitmap shaders over a
40 * checkerboard background using different xfermodes.
41 */
42class Xfermodes3GM : public GM {
43public:
45
46protected:
47 SkString getName() const override { return SkString("xfermodes3"); }
48
49 SkISize getISize() override { return SkISize::Make(630, 1215); }
50
51 void onDraw(SkCanvas* canvas) override {
52 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
53
55 SkPaint labelP;
56
57 constexpr SkColor kSolidColors[] = {
60 0x80808000
61 };
62
63 constexpr SkColor kBmpAlphas[] = {
64 0xff,
65 0x80,
66 };
67
68 auto tempSurface(this->makeTempSurface(canvas, kSize, kSize));
69
70 int test = 0;
71 int x = 0, y = 0;
72 constexpr struct { SkPaint::Style fStyle; SkScalar fWidth; } kStrokes[] = {
75 };
76 for (size_t s = 0; s < std::size(kStrokes); ++s) {
77 for (size_t m = 0; m < kSkBlendModeCount; ++m) {
78 SkBlendMode mode = static_cast<SkBlendMode>(m);
79 canvas->drawString(SkBlendMode_Name(mode),
81 SkIntToScalar(y + kSize + 3) + font.getSize(),
82 font, labelP);
83 for (size_t c = 0; c < std::size(kSolidColors); ++c) {
84 SkPaint modePaint;
85 modePaint.setBlendMode(mode);
86 modePaint.setColor(kSolidColors[c]);
87 modePaint.setStyle(kStrokes[s].fStyle);
88 modePaint.setStrokeWidth(kStrokes[s].fWidth);
89
90 this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface.get());
91
92 ++test;
93 x += kSize + 10;
94 if (!(test % kTestsPerRow)) {
95 x = 0;
96 y += kSize + 30;
97 }
98 }
99 for (size_t a = 0; a < std::size(kBmpAlphas); ++a) {
100 SkPaint modePaint;
101 modePaint.setBlendMode(mode);
102 modePaint.setAlpha(kBmpAlphas[a]);
103 modePaint.setShader(fBmpShader);
104 modePaint.setStyle(kStrokes[s].fStyle);
105 modePaint.setStrokeWidth(kStrokes[s].fWidth);
106
107 this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface.get());
108
109 ++test;
110 x += kSize + 10;
111 if (!(test % kTestsPerRow)) {
112 x = 0;
113 y += kSize + 30;
114 }
115 }
116 }
117 }
118 }
119
120private:
121 /**
122 * GrContext has optimizations around full rendertarget draws that can be replaced with clears.
123 * We are trying to test those. We could use saveLayer() to create small SkGpuDevices but
124 * saveLayer() uses the texture cache. This means that the actual render target may be larger
125 * than the layer. Because the clip will contain the layer's bounds, no draws will be full-RT.
126 * So explicitly create a temporary canvas with dimensions exactly the layer size.
127 */
128 sk_sp<SkSurface> makeTempSurface(SkCanvas* baseCanvas, int w, int h) {
129 SkImageInfo baseInfo = baseCanvas->imageInfo();
130 SkImageInfo info = SkImageInfo::Make(w, h, baseInfo.colorType(), baseInfo.alphaType(),
131 baseInfo.refColorSpace());
132 return baseCanvas->makeSurface(info);
133 }
134
135 void drawMode(SkCanvas* canvas,
136 int x, int y, int w, int h,
137 const SkPaint& modePaint, SkSurface* surface) {
138 canvas->save();
140
142
143 SkCanvas* modeCanvas;
144 if (nullptr == surface) {
145 canvas->saveLayer(&r, nullptr);
146 canvas->clipRect(r);
147 modeCanvas = canvas;
148 } else {
149 modeCanvas = surface->getCanvas();
150 }
151
152 SkPaint bgPaint;
153 bgPaint.setAntiAlias(false);
154 bgPaint.setShader(fBGShader);
155 modeCanvas->drawRect(r, bgPaint);
156 modeCanvas->drawRect(r, modePaint);
157 modeCanvas = nullptr;
158
159 if (nullptr == surface) {
160 canvas->restore();
161 } else {
162 surface->draw(canvas, 0, 0);
163 }
164
166 SkPaint borderPaint;
167 borderPaint.setStyle(SkPaint::kStroke_Style);
168 canvas->drawRect(r, borderPaint);
169
170 canvas->restore();
171 }
172
173 void onOnceBeforeDraw() override {
174 const uint32_t kCheckData[] = {
175 SkPackARGB32(0xFF, 0x42, 0x41, 0x42),
176 SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
177 SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
178 SkPackARGB32(0xFF, 0x42, 0x41, 0x42)
179 };
180 SkBitmap bg;
181 bg.allocN32Pixels(2, 2, true);
182 memcpy(bg.getPixels(), kCheckData, sizeof(kCheckData));
183
184 SkMatrix lm;
185 lm.setScale(SkIntToScalar(kCheckSize), SkIntToScalar(kCheckSize));
187 SkSamplingOptions(), lm);
188
189 SkPaint bmpPaint;
190 const SkPoint kCenter = { SkIntToScalar(kSize) / 2, SkIntToScalar(kSize) / 2 };
191 const SkColor kColors[] = {
192 SK_ColorTRANSPARENT, 0x80800000, 0xF020F060, SK_ColorWHITE
193 };
195 kColors, nullptr, std::size(kColors),
197
198 SkBitmap bmp;
199 bmp.allocN32Pixels(kSize, kSize);
200 SkCanvas bmpCanvas(bmp);
201
202 bmpCanvas.clear(SK_ColorTRANSPARENT);
203 SkRect rect = { SkIntToScalar(kSize) / 8, SkIntToScalar(kSize) / 8,
204 7 * SkIntToScalar(kSize) / 8, 7 * SkIntToScalar(kSize) / 8};
205 bmpCanvas.drawRect(rect, bmpPaint);
206
207 fBmpShader = bmp.makeShader(SkSamplingOptions());
208 }
209
210 enum {
211 kCheckSize = 8,
212 kSize = 30,
213 kTestsPerRow = 15,
214 };
215
216 sk_sp<SkShader> fBGShader;
217 sk_sp<SkShader> fBmpShader;
218
219 using INHERITED = GM;
220};
221
222//////////////////////////////////////////////////////////////////////////////
223
224DEF_GM(return new Xfermodes3GM;)
225
226} // namespace skiagm
SkStrokeRec::Style fStyle
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
#define test(name)
SK_API const char * SkBlendMode_Name(SkBlendMode blendMode)
static constexpr int kSkBlendModeCount
Definition SkBlendMode.h:76
SkBlendMode
Definition SkBlendMode.h:38
static SkPMColor SkPackARGB32(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
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_ColorWHITE
Definition SkColor.h:122
#define SK_ScalarHalf
Definition SkScalar.h:19
#define SkIntToScalar(x)
Definition SkScalar.h:57
void * getPixels() const
Definition SkBitmap.h:283
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
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition SkCanvas.cpp:500
void drawRect(const SkRect &rect, const SkPaint &paint)
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
sk_sp< SkSurface > makeSurface(const SkImageInfo &info, const SkSurfaceProps *props=nullptr)
void clear(SkColor color)
Definition SkCanvas.h:1199
int save()
Definition SkCanvas.cpp:451
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition SkCanvas.h:1803
SkImageInfo imageInfo() const
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)
SkMatrix & setScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py)
Definition SkMatrix.cpp:296
void setStyle(Style style)
Definition SkPaint.cpp:105
void setColor(SkColor color)
Definition SkPaint.cpp:119
void setAntiAlias(bool aa)
Definition SkPaint.h:170
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
void setAlpha(U8CPU a)
Definition SkPaint.h:279
void setShader(sk_sp< SkShader > shader)
void setBlendMode(SkBlendMode mode)
Definition SkPaint.cpp:151
void setStrokeWidth(SkScalar width)
Definition SkPaint.cpp:159
void setBGColor(SkColor)
Definition gm.cpp:159
SkString getName() const override
SkISize getISize() override
void onOnceBeforeDraw() override
void onDraw(SkCanvas *canvas) override
VkSurfaceKHR surface
Definition main.cc:49
float SkScalar
Definition extension.cpp:12
struct MyStruct s
struct MyStruct a[10]
#define DEF_GM(CODE)
Definition gm.h:40
double y
double x
SkFont DefaultPortableFont()
SkColor color_to_565(SkColor color)
SkScalar w
SkScalar h
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
sk_sp< SkColorSpace > refColorSpace() const
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
SkAlphaType alphaType() const
SkColorType colorType() const
void inset(float dx, float dy)
Definition SkRect.h:1060
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609