blob: 9c78658ae7a96f993571aeb44367c0b15194ea29 [file] [log] [blame]
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +00001/*
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 */
reed48eb08a2015-12-25 12:56:03 -08007
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +00008#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000010#include "SkBitmap.h"
11#include "SkGradientShader.h"
reed48eb08a2015-12-25 12:56:03 -080012#include "SkSurface.h"
Mike Reedd4706732016-11-15 16:44:34 -050013#include "SkBlendModePriv.h"
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000014#include "SkColorPriv.h"
15
16#if SK_SUPPORT_GPU
17#include "GrContext.h"
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000018#endif
19
20namespace skiagm {
21
22/**
23 * This tests drawing device-covering rects with solid colors and bitmap shaders over a
24 * checkerboard background using different xfermodes.
25 */
26class Xfermodes3GM : public GM {
27public:
28 Xfermodes3GM() {}
29
30protected:
mtklein36352bf2015-03-25 18:17:31 -070031 SkString onShortName() override {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000032 return SkString("xfermodes3");
33 }
34
mtklein36352bf2015-03-25 18:17:31 -070035 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070036 return SkISize::Make(630, 1215);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000037 }
38
mtklein36352bf2015-03-25 18:17:31 -070039 void onDrawBackground(SkCanvas* canvas) override {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000040 SkPaint bgPaint;
caryclark4ec1ac62015-07-21 07:42:45 -070041 bgPaint.setColor(sk_tool_utils::color_to_565(0xFF70D0E0));
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000042 canvas->drawPaint(bgPaint);
43 }
44
mtklein36352bf2015-03-25 18:17:31 -070045 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000046 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
47
48 SkPaint labelP;
49 labelP.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070050 sk_tool_utils::set_portable_typeface(&labelP);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000051
mtkleindbfd7ab2016-09-01 11:24:54 -070052 constexpr SkColor kSolidColors[] = {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000053 SK_ColorTRANSPARENT,
54 SK_ColorBLUE,
55 0x80808000
56 };
57
mtkleindbfd7ab2016-09-01 11:24:54 -070058 constexpr SkColor kBmpAlphas[] = {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000059 0xff,
60 0x80,
61 };
62
reede8f30622016-03-23 18:59:25 -070063 auto tempSurface(this->possiblyCreateTempSurface(canvas, kSize, kSize));
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000064
65 int test = 0;
66 int x = 0, y = 0;
mtkleindbfd7ab2016-09-01 11:24:54 -070067 constexpr struct { SkPaint::Style fStyle; SkScalar fWidth; } kStrokes[] = {
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +000068 {SkPaint::kFill_Style, 0},
69 {SkPaint::kStroke_Style, SkIntToScalar(kSize) / 2},
70 };
71 for (size_t s = 0; s < SK_ARRAY_COUNT(kStrokes); ++s) {
Mike Reedd4706732016-11-15 16:44:34 -050072 for (size_t m = 0; m <= (size_t)SkBlendMode::kLastMode; ++m) {
reed374772b2016-10-05 17:33:02 -070073 SkBlendMode mode = static_cast<SkBlendMode>(m);
Cary Clark2a475ea2017-04-28 15:35:12 -040074 canvas->drawString(SkBlendMode_Name(mode),
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +000075 SkIntToScalar(x),
76 SkIntToScalar(y + kSize + 3) + labelP.getTextSize(),
77 labelP);
78 for (size_t c = 0; c < SK_ARRAY_COUNT(kSolidColors); ++c) {
79 SkPaint modePaint;
reed374772b2016-10-05 17:33:02 -070080 modePaint.setBlendMode(mode);
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +000081 modePaint.setColor(kSolidColors[c]);
82 modePaint.setStyle(kStrokes[s].fStyle);
83 modePaint.setStrokeWidth(kStrokes[s].fWidth);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000084
reede8f30622016-03-23 18:59:25 -070085 this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface.get());
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000086
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +000087 ++test;
88 x += kSize + 10;
89 if (!(test % kTestsPerRow)) {
90 x = 0;
91 y += kSize + 30;
92 }
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000093 }
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +000094 for (size_t a = 0; a < SK_ARRAY_COUNT(kBmpAlphas); ++a) {
95 SkPaint modePaint;
reed374772b2016-10-05 17:33:02 -070096 modePaint.setBlendMode(mode);
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +000097 modePaint.setAlpha(kBmpAlphas[a]);
98 modePaint.setShader(fBmpShader);
99 modePaint.setStyle(kStrokes[s].fStyle);
100 modePaint.setStrokeWidth(kStrokes[s].fWidth);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000101
reede8f30622016-03-23 18:59:25 -0700102 this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface.get());
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000103
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +0000104 ++test;
105 x += kSize + 10;
106 if (!(test % kTestsPerRow)) {
107 x = 0;
108 y += kSize + 30;
109 }
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000110 }
111 }
112 }
113 }
114
115private:
116 /**
117 * GrContext has optimizations around full rendertarget draws that can be replaced with clears.
118 * We are trying to test those. We could use saveLayer() to create small SkGpuDevices but
119 * saveLayer() uses the texture cache. This means that the actual render target may be larger
120 * than the layer. Because the clip will contain the layer's bounds, no draws will be full-RT.
121 * So when running on a GPU canvas we explicitly create a temporary canvas using a texture with
122 * dimensions exactly matching the layer size.
123 */
reede8f30622016-03-23 18:59:25 -0700124 sk_sp<SkSurface> possiblyCreateTempSurface(SkCanvas* baseCanvas, int w, int h) {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000125#if SK_SUPPORT_GPU
reed52d9ac62014-06-30 09:05:34 -0700126 GrContext* context = baseCanvas->getGrContext();
bsalomonafe30052015-01-16 07:32:33 -0800127 SkImageInfo baseInfo = baseCanvas->imageInfo();
128 SkImageInfo info = SkImageInfo::Make(w, h, baseInfo.colorType(), baseInfo.alphaType(),
Mike Reed693fdbd2017-01-12 10:13:40 -0500129 baseInfo.refColorSpace());
brianosman898235c2016-04-06 07:38:23 -0700130 SkSurfaceProps canvasProps(SkSurfaceProps::kLegacyFontHost_InitType);
131 baseCanvas->getProps(&canvasProps);
132 return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, &canvasProps);
reed48eb08a2015-12-25 12:56:03 -0800133#else
134 return nullptr;
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000135#endif
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000136 }
137
138 void drawMode(SkCanvas* canvas,
139 int x, int y, int w, int h,
reed48eb08a2015-12-25 12:56:03 -0800140 const SkPaint& modePaint, SkSurface* surface) {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000141 canvas->save();
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000142 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
143
144 SkRect r = SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h));
145
146 SkCanvas* modeCanvas;
reed48eb08a2015-12-25 12:56:03 -0800147 if (nullptr == surface) {
halcanary96fcdcc2015-08-27 07:41:13 -0700148 canvas->saveLayer(&r, nullptr);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000149 modeCanvas = canvas;
150 } else {
reed48eb08a2015-12-25 12:56:03 -0800151 modeCanvas = surface->getCanvas();
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000152 }
153
154 SkPaint bgPaint;
155 bgPaint.setAntiAlias(false);
156 bgPaint.setShader(fBGShader);
157 modeCanvas->drawRect(r, bgPaint);
158 modeCanvas->drawRect(r, modePaint);
halcanary96fcdcc2015-08-27 07:41:13 -0700159 modeCanvas = nullptr;
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000160
reed48eb08a2015-12-25 12:56:03 -0800161 if (nullptr == surface) {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000162 canvas->restore();
163 } else {
reed48eb08a2015-12-25 12:56:03 -0800164 surface->draw(canvas, 0, 0, nullptr);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000165 }
166
167 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
168 SkPaint borderPaint;
169 borderPaint.setStyle(SkPaint::kStroke_Style);
170 canvas->drawRect(r, borderPaint);
171
172 canvas->restore();
173 }
174
mtklein36352bf2015-03-25 18:17:31 -0700175 void onOnceBeforeDraw() override {
mtkleindbfd7ab2016-09-01 11:24:54 -0700176 const uint32_t kCheckData[] = {
caryclark4ec1ac62015-07-21 07:42:45 -0700177 SkPackARGB32(0xFF, 0x42, 0x41, 0x42),
178 SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
179 SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
180 SkPackARGB32(0xFF, 0x42, 0x41, 0x42)
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000181 };
182 SkBitmap bg;
reed@google.comeb9a46c2014-01-25 16:46:20 +0000183 bg.allocN32Pixels(2, 2, true);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000184 memcpy(bg.getPixels(), kCheckData, sizeof(kCheckData));
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000185
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000186 SkMatrix lm;
187 lm.setScale(SkIntToScalar(kCheckSize), SkIntToScalar(kCheckSize));
reed1a9b9642016-03-13 14:13:58 -0700188 fBGShader = SkShader::MakeBitmapShader(bg, SkShader::kRepeat_TileMode,
189 SkShader::kRepeat_TileMode, &lm);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000190
191 SkPaint bmpPaint;
reed1a9b9642016-03-13 14:13:58 -0700192 const SkPoint kCenter = { SkIntToScalar(kSize) / 2, SkIntToScalar(kSize) / 2 };
193 const SkColor kColors[] = {
194 SK_ColorTRANSPARENT, 0x80800000, 0xF020F060, SK_ColorWHITE
195 };
196 bmpPaint.setShader(SkGradientShader::MakeRadial(kCenter, 3 * SkIntToScalar(kSize) / 4,
197 kColors, nullptr, SK_ARRAY_COUNT(kColors),
198 SkShader::kRepeat_TileMode));
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000199
200 SkBitmap bmp;
reed@google.comeb9a46c2014-01-25 16:46:20 +0000201 bmp.allocN32Pixels(kSize, kSize);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000202 SkCanvas bmpCanvas(bmp);
203
204 bmpCanvas.clear(SK_ColorTRANSPARENT);
205 SkRect rect = { SkIntToScalar(kSize) / 8, SkIntToScalar(kSize) / 8,
206 7 * SkIntToScalar(kSize) / 8, 7 * SkIntToScalar(kSize) / 8};
207 bmpCanvas.drawRect(rect, bmpPaint);
208
reed1a9b9642016-03-13 14:13:58 -0700209 fBmpShader = SkShader::MakeBitmapShader(bmp, SkShader::kClamp_TileMode,
210 SkShader::kClamp_TileMode);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000211 }
212
213 enum {
214 kCheckSize = 8,
215 kSize = 30,
216 kTestsPerRow = 15,
217 };
218
reed1a9b9642016-03-13 14:13:58 -0700219 sk_sp<SkShader> fBGShader;
220 sk_sp<SkShader> fBmpShader;
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000221
222 typedef GM INHERITED;
223};
224
225//////////////////////////////////////////////////////////////////////////////
226
227DEF_GM(return new Xfermodes3GM;)
228
229}