blob: f59e80f757ca72c14dee8834e5e0e455d45fa38f [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"
9#include "SkBitmap.h"
10#include "SkGradientShader.h"
reed48eb08a2015-12-25 12:56:03 -080011#include "SkSurface.h"
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000012#include "SkXfermode.h"
13#include "SkColorPriv.h"
14
15#if SK_SUPPORT_GPU
16#include "GrContext.h"
17#include "SkGpuDevice.h"
18#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
52 static const SkColor kSolidColors[] = {
53 SK_ColorTRANSPARENT,
54 SK_ColorBLUE,
55 0x80808000
56 };
57
58 static const SkColor kBmpAlphas[] = {
59 0xff,
60 0x80,
61 };
62
reed48eb08a2015-12-25 12:56:03 -080063 SkAutoTUnref<SkSurface> 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;
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +000067 static const struct { SkPaint::Style fStyle; SkScalar fWidth; } kStrokes[] = {
68 {SkPaint::kFill_Style, 0},
69 {SkPaint::kStroke_Style, SkIntToScalar(kSize) / 2},
70 };
71 for (size_t s = 0; s < SK_ARRAY_COUNT(kStrokes); ++s) {
72 for (size_t m = 0; m <= SkXfermode::kLastMode; ++m) {
73 SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(m);
74 canvas->drawText(SkXfermode::ModeName(mode),
75 strlen(SkXfermode::ModeName(mode)),
76 SkIntToScalar(x),
77 SkIntToScalar(y + kSize + 3) + labelP.getTextSize(),
78 labelP);
79 for (size_t c = 0; c < SK_ARRAY_COUNT(kSolidColors); ++c) {
80 SkPaint modePaint;
81 modePaint.setXfermodeMode(mode);
82 modePaint.setColor(kSolidColors[c]);
83 modePaint.setStyle(kStrokes[s].fStyle);
84 modePaint.setStrokeWidth(kStrokes[s].fWidth);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000085
reed48eb08a2015-12-25 12:56:03 -080086 this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000087
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +000088 ++test;
89 x += kSize + 10;
90 if (!(test % kTestsPerRow)) {
91 x = 0;
92 y += kSize + 30;
93 }
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000094 }
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +000095 for (size_t a = 0; a < SK_ARRAY_COUNT(kBmpAlphas); ++a) {
96 SkPaint modePaint;
97 modePaint.setXfermodeMode(mode);
98 modePaint.setAlpha(kBmpAlphas[a]);
99 modePaint.setShader(fBmpShader);
100 modePaint.setStyle(kStrokes[s].fStyle);
101 modePaint.setStrokeWidth(kStrokes[s].fWidth);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000102
reed48eb08a2015-12-25 12:56:03 -0800103 this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000104
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +0000105 ++test;
106 x += kSize + 10;
107 if (!(test % kTestsPerRow)) {
108 x = 0;
109 y += kSize + 30;
110 }
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000111 }
112 }
113 }
114 }
115
116private:
117 /**
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.
124 */
reed48eb08a2015-12-25 12:56:03 -0800125 SkSurface* possiblyCreateTempSurface(SkCanvas* baseCanvas, int w, int h) {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000126#if SK_SUPPORT_GPU
reed52d9ac62014-06-30 09:05:34 -0700127 GrContext* context = baseCanvas->getGrContext();
bsalomonafe30052015-01-16 07:32:33 -0800128 SkImageInfo baseInfo = baseCanvas->imageInfo();
129 SkImageInfo info = SkImageInfo::Make(w, h, baseInfo.colorType(), baseInfo.alphaType(),
130 baseInfo.profileType());
reed48eb08a2015-12-25 12:56:03 -0800131 return SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info, 0, nullptr);
132#else
133 return nullptr;
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000134#endif
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000135 }
136
137 void drawMode(SkCanvas* canvas,
138 int x, int y, int w, int h,
reed48eb08a2015-12-25 12:56:03 -0800139 const SkPaint& modePaint, SkSurface* surface) {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000140 canvas->save();
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000141 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
142
143 SkRect r = SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h));
144
145 SkCanvas* modeCanvas;
reed48eb08a2015-12-25 12:56:03 -0800146 if (nullptr == surface) {
halcanary96fcdcc2015-08-27 07:41:13 -0700147 canvas->saveLayer(&r, nullptr);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000148 modeCanvas = canvas;
149 } else {
reed48eb08a2015-12-25 12:56:03 -0800150 modeCanvas = surface->getCanvas();
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000151 }
152
153 SkPaint bgPaint;
154 bgPaint.setAntiAlias(false);
155 bgPaint.setShader(fBGShader);
156 modeCanvas->drawRect(r, bgPaint);
157 modeCanvas->drawRect(r, modePaint);
halcanary96fcdcc2015-08-27 07:41:13 -0700158 modeCanvas = nullptr;
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000159
reed48eb08a2015-12-25 12:56:03 -0800160 if (nullptr == surface) {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000161 canvas->restore();
162 } else {
reed48eb08a2015-12-25 12:56:03 -0800163 surface->draw(canvas, 0, 0, nullptr);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000164 }
165
166 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
167 SkPaint borderPaint;
168 borderPaint.setStyle(SkPaint::kStroke_Style);
169 canvas->drawRect(r, borderPaint);
170
171 canvas->restore();
172 }
173
mtklein36352bf2015-03-25 18:17:31 -0700174 void onOnceBeforeDraw() override {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000175 static const uint32_t kCheckData[] = {
caryclark4ec1ac62015-07-21 07:42:45 -0700176 SkPackARGB32(0xFF, 0x42, 0x41, 0x42),
177 SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
178 SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
179 SkPackARGB32(0xFF, 0x42, 0x41, 0x42)
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000180 };
181 SkBitmap bg;
reed@google.comeb9a46c2014-01-25 16:46:20 +0000182 bg.allocN32Pixels(2, 2, true);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000183 SkAutoLockPixels bgAlp(bg);
184 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));
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000188 fBGShader.reset(SkShader::CreateBitmapShader(bg,
189 SkShader::kRepeat_TileMode,
190 SkShader::kRepeat_TileMode,
191 &lm));
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000192
193 SkPaint bmpPaint;
194 static const SkPoint kCenter = { SkIntToScalar(kSize) / 2, SkIntToScalar(kSize) / 2 };
195 static const SkColor kColors[] = { SK_ColorTRANSPARENT, 0x80800000,
196 0xF020F060, SK_ColorWHITE };
197 bmpPaint.setShader(SkGradientShader::CreateRadial(kCenter,
198 3 * SkIntToScalar(kSize) / 4,
199 kColors,
halcanary96fcdcc2015-08-27 07:41:13 -0700200 nullptr,
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000201 SK_ARRAY_COUNT(kColors),
202 SkShader::kRepeat_TileMode))->unref();
203
204 SkBitmap bmp;
reed@google.comeb9a46c2014-01-25 16:46:20 +0000205 bmp.allocN32Pixels(kSize, kSize);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000206 SkCanvas bmpCanvas(bmp);
207
208 bmpCanvas.clear(SK_ColorTRANSPARENT);
209 SkRect rect = { SkIntToScalar(kSize) / 8, SkIntToScalar(kSize) / 8,
210 7 * SkIntToScalar(kSize) / 8, 7 * SkIntToScalar(kSize) / 8};
211 bmpCanvas.drawRect(rect, bmpPaint);
212
213 fBmpShader.reset(SkShader::CreateBitmapShader(bmp,
214 SkShader::kClamp_TileMode,
215 SkShader::kClamp_TileMode));
216 }
217
218 enum {
219 kCheckSize = 8,
220 kSize = 30,
221 kTestsPerRow = 15,
222 };
223
224 SkAutoTUnref<SkShader> fBGShader;
225 SkAutoTUnref<SkShader> fBmpShader;
226
227 typedef GM INHERITED;
228};
229
230//////////////////////////////////////////////////////////////////////////////
231
232DEF_GM(return new Xfermodes3GM;)
233
234}