blob: 9d53c2fd1c311837c0c88836ded2390c8ae69dc2 [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"
Mike Reed331ccfd2018-10-25 12:36:06 -040015#include "SkTextUtils.h"
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000016#include "GrContext.h"
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000017
18namespace skiagm {
19
20/**
21 * This tests drawing device-covering rects with solid colors and bitmap shaders over a
22 * checkerboard background using different xfermodes.
23 */
24class Xfermodes3GM : public GM {
25public:
26 Xfermodes3GM() {}
27
28protected:
mtklein36352bf2015-03-25 18:17:31 -070029 SkString onShortName() override {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000030 return SkString("xfermodes3");
31 }
32
mtklein36352bf2015-03-25 18:17:31 -070033 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070034 return SkISize::Make(630, 1215);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000035 }
36
mtklein36352bf2015-03-25 18:17:31 -070037 void onDrawBackground(SkCanvas* canvas) override {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000038 SkPaint bgPaint;
caryclark4ec1ac62015-07-21 07:42:45 -070039 bgPaint.setColor(sk_tool_utils::color_to_565(0xFF70D0E0));
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000040 canvas->drawPaint(bgPaint);
41 }
42
mtklein36352bf2015-03-25 18:17:31 -070043 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000044 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
45
46 SkPaint labelP;
47 labelP.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070048 sk_tool_utils::set_portable_typeface(&labelP);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000049
mtkleindbfd7ab2016-09-01 11:24:54 -070050 constexpr SkColor kSolidColors[] = {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000051 SK_ColorTRANSPARENT,
52 SK_ColorBLUE,
53 0x80808000
54 };
55
mtkleindbfd7ab2016-09-01 11:24:54 -070056 constexpr SkColor kBmpAlphas[] = {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000057 0xff,
58 0x80,
59 };
60
Ben Wagnerd3c84ff2018-04-25 14:38:24 -040061 auto tempSurface(this->makeTempSurface(canvas, kSize, kSize));
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000062
63 int test = 0;
64 int x = 0, y = 0;
mtkleindbfd7ab2016-09-01 11:24:54 -070065 constexpr struct { SkPaint::Style fStyle; SkScalar fWidth; } kStrokes[] = {
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +000066 {SkPaint::kFill_Style, 0},
67 {SkPaint::kStroke_Style, SkIntToScalar(kSize) / 2},
68 };
69 for (size_t s = 0; s < SK_ARRAY_COUNT(kStrokes); ++s) {
Mike Reedd4706732016-11-15 16:44:34 -050070 for (size_t m = 0; m <= (size_t)SkBlendMode::kLastMode; ++m) {
reed374772b2016-10-05 17:33:02 -070071 SkBlendMode mode = static_cast<SkBlendMode>(m);
Cary Clark2a475ea2017-04-28 15:35:12 -040072 canvas->drawString(SkBlendMode_Name(mode),
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +000073 SkIntToScalar(x),
74 SkIntToScalar(y + kSize + 3) + labelP.getTextSize(),
75 labelP);
76 for (size_t c = 0; c < SK_ARRAY_COUNT(kSolidColors); ++c) {
77 SkPaint modePaint;
reed374772b2016-10-05 17:33:02 -070078 modePaint.setBlendMode(mode);
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +000079 modePaint.setColor(kSolidColors[c]);
80 modePaint.setStyle(kStrokes[s].fStyle);
81 modePaint.setStrokeWidth(kStrokes[s].fWidth);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000082
reede8f30622016-03-23 18:59:25 -070083 this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface.get());
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000084
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +000085 ++test;
86 x += kSize + 10;
87 if (!(test % kTestsPerRow)) {
88 x = 0;
89 y += kSize + 30;
90 }
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000091 }
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +000092 for (size_t a = 0; a < SK_ARRAY_COUNT(kBmpAlphas); ++a) {
93 SkPaint modePaint;
reed374772b2016-10-05 17:33:02 -070094 modePaint.setBlendMode(mode);
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +000095 modePaint.setAlpha(kBmpAlphas[a]);
96 modePaint.setShader(fBmpShader);
97 modePaint.setStyle(kStrokes[s].fStyle);
98 modePaint.setStrokeWidth(kStrokes[s].fWidth);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000099
reede8f30622016-03-23 18:59:25 -0700100 this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface.get());
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000101
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +0000102 ++test;
103 x += kSize + 10;
104 if (!(test % kTestsPerRow)) {
105 x = 0;
106 y += kSize + 30;
107 }
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000108 }
109 }
110 }
111 }
112
113private:
114 /**
115 * GrContext has optimizations around full rendertarget draws that can be replaced with clears.
116 * We are trying to test those. We could use saveLayer() to create small SkGpuDevices but
117 * saveLayer() uses the texture cache. This means that the actual render target may be larger
118 * than the layer. Because the clip will contain the layer's bounds, no draws will be full-RT.
Ben Wagnerd3c84ff2018-04-25 14:38:24 -0400119 * So explicitly create a temporary canvas with dimensions exactly the layer size.
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000120 */
Ben Wagnerd3c84ff2018-04-25 14:38:24 -0400121 sk_sp<SkSurface> makeTempSurface(SkCanvas* baseCanvas, int w, int h) {
bsalomonafe30052015-01-16 07:32:33 -0800122 SkImageInfo baseInfo = baseCanvas->imageInfo();
123 SkImageInfo info = SkImageInfo::Make(w, h, baseInfo.colorType(), baseInfo.alphaType(),
Mike Reed693fdbd2017-01-12 10:13:40 -0500124 baseInfo.refColorSpace());
Ben Wagnerd3c84ff2018-04-25 14:38:24 -0400125 return baseCanvas->makeSurface(info);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000126 }
127
128 void drawMode(SkCanvas* canvas,
129 int x, int y, int w, int h,
reed48eb08a2015-12-25 12:56:03 -0800130 const SkPaint& modePaint, SkSurface* surface) {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000131 canvas->save();
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000132 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
133
134 SkRect r = SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h));
135
136 SkCanvas* modeCanvas;
reed48eb08a2015-12-25 12:56:03 -0800137 if (nullptr == surface) {
halcanary96fcdcc2015-08-27 07:41:13 -0700138 canvas->saveLayer(&r, nullptr);
Ben Wagnerd3c84ff2018-04-25 14:38:24 -0400139 canvas->clipRect(r);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000140 modeCanvas = canvas;
141 } else {
reed48eb08a2015-12-25 12:56:03 -0800142 modeCanvas = surface->getCanvas();
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000143 }
144
145 SkPaint bgPaint;
146 bgPaint.setAntiAlias(false);
147 bgPaint.setShader(fBGShader);
148 modeCanvas->drawRect(r, bgPaint);
149 modeCanvas->drawRect(r, modePaint);
halcanary96fcdcc2015-08-27 07:41:13 -0700150 modeCanvas = nullptr;
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000151
reed48eb08a2015-12-25 12:56:03 -0800152 if (nullptr == surface) {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000153 canvas->restore();
154 } else {
reed48eb08a2015-12-25 12:56:03 -0800155 surface->draw(canvas, 0, 0, nullptr);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000156 }
157
158 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
159 SkPaint borderPaint;
160 borderPaint.setStyle(SkPaint::kStroke_Style);
161 canvas->drawRect(r, borderPaint);
162
163 canvas->restore();
164 }
165
mtklein36352bf2015-03-25 18:17:31 -0700166 void onOnceBeforeDraw() override {
mtkleindbfd7ab2016-09-01 11:24:54 -0700167 const uint32_t kCheckData[] = {
caryclark4ec1ac62015-07-21 07:42:45 -0700168 SkPackARGB32(0xFF, 0x42, 0x41, 0x42),
169 SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
170 SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
171 SkPackARGB32(0xFF, 0x42, 0x41, 0x42)
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000172 };
173 SkBitmap bg;
reed@google.comeb9a46c2014-01-25 16:46:20 +0000174 bg.allocN32Pixels(2, 2, true);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000175 memcpy(bg.getPixels(), kCheckData, sizeof(kCheckData));
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000176
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000177 SkMatrix lm;
178 lm.setScale(SkIntToScalar(kCheckSize), SkIntToScalar(kCheckSize));
reed1a9b9642016-03-13 14:13:58 -0700179 fBGShader = SkShader::MakeBitmapShader(bg, SkShader::kRepeat_TileMode,
180 SkShader::kRepeat_TileMode, &lm);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000181
182 SkPaint bmpPaint;
reed1a9b9642016-03-13 14:13:58 -0700183 const SkPoint kCenter = { SkIntToScalar(kSize) / 2, SkIntToScalar(kSize) / 2 };
184 const SkColor kColors[] = {
185 SK_ColorTRANSPARENT, 0x80800000, 0xF020F060, SK_ColorWHITE
186 };
187 bmpPaint.setShader(SkGradientShader::MakeRadial(kCenter, 3 * SkIntToScalar(kSize) / 4,
188 kColors, nullptr, SK_ARRAY_COUNT(kColors),
189 SkShader::kRepeat_TileMode));
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000190
191 SkBitmap bmp;
reed@google.comeb9a46c2014-01-25 16:46:20 +0000192 bmp.allocN32Pixels(kSize, kSize);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000193 SkCanvas bmpCanvas(bmp);
194
195 bmpCanvas.clear(SK_ColorTRANSPARENT);
196 SkRect rect = { SkIntToScalar(kSize) / 8, SkIntToScalar(kSize) / 8,
197 7 * SkIntToScalar(kSize) / 8, 7 * SkIntToScalar(kSize) / 8};
198 bmpCanvas.drawRect(rect, bmpPaint);
199
reed1a9b9642016-03-13 14:13:58 -0700200 fBmpShader = SkShader::MakeBitmapShader(bmp, SkShader::kClamp_TileMode,
201 SkShader::kClamp_TileMode);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000202 }
203
204 enum {
205 kCheckSize = 8,
206 kSize = 30,
207 kTestsPerRow = 15,
208 };
209
reed1a9b9642016-03-13 14:13:58 -0700210 sk_sp<SkShader> fBGShader;
211 sk_sp<SkShader> fBmpShader;
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000212
213 typedef GM INHERITED;
214};
215
216//////////////////////////////////////////////////////////////////////////////
217
218DEF_GM(return new Xfermodes3GM;)
219
220}