2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "sk_tool_utils.h"
11 #include "SkGradientShader.h"
12 #include "SkSurface.h"
13 #include "SkBlendModePriv.h"
14 #include "SkColorPriv.h"
17 #include "GrContext.h"
23 * This tests drawing device-covering rects with solid colors and bitmap shaders over a
24 * checkerboard background using different xfermodes.
26 class Xfermodes3GM : public GM {
31 SkString onShortName() override {
32 return SkString("xfermodes3");
35 SkISize onISize() override {
36 return SkISize::Make(630, 1215);
39 void onDrawBackground(SkCanvas* canvas) override {
41 bgPaint.setColor(sk_tool_utils::color_to_565(0xFF70D0E0));
42 canvas->drawPaint(bgPaint);
45 void onDraw(SkCanvas* canvas) override {
46 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
49 labelP.setAntiAlias(true);
50 sk_tool_utils::set_portable_typeface(&labelP);
52 constexpr SkColor kSolidColors[] = {
58 constexpr SkColor kBmpAlphas[] = {
63 auto tempSurface(this->possiblyCreateTempSurface(canvas, kSize, kSize));
67 constexpr struct { SkPaint::Style fStyle; SkScalar fWidth; } kStrokes[] = {
68 {SkPaint::kFill_Style, 0},
69 {SkPaint::kStroke_Style, SkIntToScalar(kSize) / 2},
71 for (size_t s = 0; s < SK_ARRAY_COUNT(kStrokes); ++s) {
72 for (size_t m = 0; m <= (size_t)SkBlendMode::kLastMode; ++m) {
73 SkBlendMode mode = static_cast<SkBlendMode>(m);
74 canvas->drawText(SkBlendMode_Name(mode),
75 strlen(SkBlendMode_Name(mode)),
77 SkIntToScalar(y + kSize + 3) + labelP.getTextSize(),
79 for (size_t c = 0; c < SK_ARRAY_COUNT(kSolidColors); ++c) {
81 modePaint.setBlendMode(mode);
82 modePaint.setColor(kSolidColors[c]);
83 modePaint.setStyle(kStrokes[s].fStyle);
84 modePaint.setStrokeWidth(kStrokes[s].fWidth);
86 this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface.get());
90 if (!(test % kTestsPerRow)) {
95 for (size_t a = 0; a < SK_ARRAY_COUNT(kBmpAlphas); ++a) {
97 modePaint.setBlendMode(mode);
98 modePaint.setAlpha(kBmpAlphas[a]);
99 modePaint.setShader(fBmpShader);
100 modePaint.setStyle(kStrokes[s].fStyle);
101 modePaint.setStrokeWidth(kStrokes[s].fWidth);
103 this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface.get());
107 if (!(test % kTestsPerRow)) {
118 * GrContext has optimizations around full rendertarget draws that can be replaced with clears.
119 * We are trying to test those. We could use saveLayer() to create small SkGpuDevices but
120 * saveLayer() uses the texture cache. This means that the actual render target may be larger
121 * than the layer. Because the clip will contain the layer's bounds, no draws will be full-RT.
122 * So when running on a GPU canvas we explicitly create a temporary canvas using a texture with
123 * dimensions exactly matching the layer size.
125 sk_sp<SkSurface> possiblyCreateTempSurface(SkCanvas* baseCanvas, int w, int h) {
127 GrContext* context = baseCanvas->getGrContext();
128 SkImageInfo baseInfo = baseCanvas->imageInfo();
129 SkImageInfo info = SkImageInfo::Make(w, h, baseInfo.colorType(), baseInfo.alphaType(),
130 baseInfo.refColorSpace());
131 SkSurfaceProps canvasProps(SkSurfaceProps::kLegacyFontHost_InitType);
132 baseCanvas->getProps(&canvasProps);
133 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, &canvasProps);
139 void drawMode(SkCanvas* canvas,
140 int x, int y, int w, int h,
141 const SkPaint& modePaint, SkSurface* surface) {
143 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
145 SkRect r = SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h));
147 SkCanvas* modeCanvas;
148 if (nullptr == surface) {
149 canvas->saveLayer(&r, nullptr);
152 modeCanvas = surface->getCanvas();
156 bgPaint.setAntiAlias(false);
157 bgPaint.setShader(fBGShader);
158 modeCanvas->drawRect(r, bgPaint);
159 modeCanvas->drawRect(r, modePaint);
160 modeCanvas = nullptr;
162 if (nullptr == surface) {
165 surface->draw(canvas, 0, 0, nullptr);
168 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
170 borderPaint.setStyle(SkPaint::kStroke_Style);
171 canvas->drawRect(r, borderPaint);
176 void onOnceBeforeDraw() override {
177 const uint32_t kCheckData[] = {
178 SkPackARGB32(0xFF, 0x42, 0x41, 0x42),
179 SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
180 SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
181 SkPackARGB32(0xFF, 0x42, 0x41, 0x42)
184 bg.allocN32Pixels(2, 2, true);
185 memcpy(bg.getPixels(), kCheckData, sizeof(kCheckData));
188 lm.setScale(SkIntToScalar(kCheckSize), SkIntToScalar(kCheckSize));
189 fBGShader = SkShader::MakeBitmapShader(bg, SkShader::kRepeat_TileMode,
190 SkShader::kRepeat_TileMode, &lm);
193 const SkPoint kCenter = { SkIntToScalar(kSize) / 2, SkIntToScalar(kSize) / 2 };
194 const SkColor kColors[] = {
195 SK_ColorTRANSPARENT, 0x80800000, 0xF020F060, SK_ColorWHITE
197 bmpPaint.setShader(SkGradientShader::MakeRadial(kCenter, 3 * SkIntToScalar(kSize) / 4,
198 kColors, nullptr, SK_ARRAY_COUNT(kColors),
199 SkShader::kRepeat_TileMode));
202 bmp.allocN32Pixels(kSize, kSize);
203 SkCanvas bmpCanvas(bmp);
205 bmpCanvas.clear(SK_ColorTRANSPARENT);
206 SkRect rect = { SkIntToScalar(kSize) / 8, SkIntToScalar(kSize) / 8,
207 7 * SkIntToScalar(kSize) / 8, 7 * SkIntToScalar(kSize) / 8};
208 bmpCanvas.drawRect(rect, bmpPaint);
210 fBmpShader = SkShader::MakeBitmapShader(bmp, SkShader::kClamp_TileMode,
211 SkShader::kClamp_TileMode);
220 sk_sp<SkShader> fBGShader;
221 sk_sp<SkShader> fBmpShader;
223 typedef GM INHERITED;
226 //////////////////////////////////////////////////////////////////////////////
228 DEF_GM(return new Xfermodes3GM;)