blob: f3ee4cc67abc8968f2f5103a4cfa06e0aba8c2e5 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkBitmap.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkBlendMode.h"
11#include "include/core/SkCanvas.h"
12#include "include/core/SkColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkColorPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkColorSpace.h"
15#include "include/core/SkFont.h"
16#include "include/core/SkImageInfo.h"
17#include "include/core/SkMatrix.h"
18#include "include/core/SkPaint.h"
19#include "include/core/SkPoint.h"
20#include "include/core/SkRect.h"
21#include "include/core/SkRefCnt.h"
22#include "include/core/SkScalar.h"
23#include "include/core/SkShader.h"
24#include "include/core/SkSize.h"
25#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040027#include "include/core/SkTileMode.h"
28#include "include/core/SkTypeface.h"
29#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "include/effects/SkGradientShader.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040032#include "tools/fonts/FontToolUtils.h"
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000033
Ben Wagner7fde8e12019-05-01 17:28:53 -040034#include <string.h>
35
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000036namespace 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:
Mike Kleinea3f0142019-03-20 11:12:10 -050044 Xfermodes3GM() { this->setBGColor(ToolUtils::color_to_565(0xFF70D0E0)); }
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000045
46protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000047 SkString getName() const override { return SkString("xfermodes3"); }
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000048
Leandro Lovisolo8f023882023-08-15 21:13:52 +000049 SkISize getISize() override { return SkISize::Make(630, 1215); }
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000050
mtklein36352bf2015-03-25 18:17:31 -070051 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000052 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
53
Kevin Lubicke836c3a2023-10-20 06:55:35 -040054 SkFont font = ToolUtils::DefaultPortableFont();
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000055 SkPaint labelP;
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000056
mtkleindbfd7ab2016-09-01 11:24:54 -070057 constexpr SkColor kSolidColors[] = {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000058 SK_ColorTRANSPARENT,
59 SK_ColorBLUE,
60 0x80808000
61 };
62
mtkleindbfd7ab2016-09-01 11:24:54 -070063 constexpr SkColor kBmpAlphas[] = {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000064 0xff,
65 0x80,
66 };
67
Ben Wagnerd3c84ff2018-04-25 14:38:24 -040068 auto tempSurface(this->makeTempSurface(canvas, kSize, kSize));
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000069
70 int test = 0;
71 int x = 0, y = 0;
mtkleindbfd7ab2016-09-01 11:24:54 -070072 constexpr struct { SkPaint::Style fStyle; SkScalar fWidth; } kStrokes[] = {
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +000073 {SkPaint::kFill_Style, 0},
74 {SkPaint::kStroke_Style, SkIntToScalar(kSize) / 2},
75 };
Herb Derbyc37b3862022-06-21 09:49:17 -040076 for (size_t s = 0; s < std::size(kStrokes); ++s) {
Robert Phillips5bde3f42022-05-13 14:01:57 -040077 for (size_t m = 0; m < kSkBlendModeCount; ++m) {
reed374772b2016-10-05 17:33:02 -070078 SkBlendMode mode = static_cast<SkBlendMode>(m);
Cary Clark2a475ea2017-04-28 15:35:12 -040079 canvas->drawString(SkBlendMode_Name(mode),
Hal Canarydf2d27e2019-01-08 09:38:02 -050080 SkIntToScalar(x),
81 SkIntToScalar(y + kSize + 3) + font.getSize(),
82 font, labelP);
Herb Derbyc37b3862022-06-21 09:49:17 -040083 for (size_t c = 0; c < std::size(kSolidColors); ++c) {
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +000084 SkPaint modePaint;
reed374772b2016-10-05 17:33:02 -070085 modePaint.setBlendMode(mode);
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +000086 modePaint.setColor(kSolidColors[c]);
87 modePaint.setStyle(kStrokes[s].fStyle);
88 modePaint.setStrokeWidth(kStrokes[s].fWidth);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000089
reede8f30622016-03-23 18:59:25 -070090 this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface.get());
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000091
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +000092 ++test;
93 x += kSize + 10;
94 if (!(test % kTestsPerRow)) {
95 x = 0;
96 y += kSize + 30;
97 }
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +000098 }
Herb Derbyc37b3862022-06-21 09:49:17 -040099 for (size_t a = 0; a < std::size(kBmpAlphas); ++a) {
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +0000100 SkPaint modePaint;
reed374772b2016-10-05 17:33:02 -0700101 modePaint.setBlendMode(mode);
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +0000102 modePaint.setAlpha(kBmpAlphas[a]);
103 modePaint.setShader(fBmpShader);
104 modePaint.setStyle(kStrokes[s].fStyle);
105 modePaint.setStrokeWidth(kStrokes[s].fWidth);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000106
reede8f30622016-03-23 18:59:25 -0700107 this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface.get());
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000108
commit-bot@chromium.orgde1559c2013-08-14 19:20:45 +0000109 ++test;
110 x += kSize + 10;
111 if (!(test % kTestsPerRow)) {
112 x = 0;
113 y += kSize + 30;
114 }
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000115 }
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.
Ben Wagnerd3c84ff2018-04-25 14:38:24 -0400126 * So explicitly create a temporary canvas with dimensions exactly the layer size.
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000127 */
Ben Wagnerd3c84ff2018-04-25 14:38:24 -0400128 sk_sp<SkSurface> makeTempSurface(SkCanvas* baseCanvas, int w, int h) {
bsalomonafe30052015-01-16 07:32:33 -0800129 SkImageInfo baseInfo = baseCanvas->imageInfo();
130 SkImageInfo info = SkImageInfo::Make(w, h, baseInfo.colorType(), baseInfo.alphaType(),
Mike Reed693fdbd2017-01-12 10:13:40 -0500131 baseInfo.refColorSpace());
Ben Wagnerd3c84ff2018-04-25 14:38:24 -0400132 return baseCanvas->makeSurface(info);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000133 }
134
135 void drawMode(SkCanvas* canvas,
136 int x, int y, int w, int h,
reed48eb08a2015-12-25 12:56:03 -0800137 const SkPaint& modePaint, SkSurface* surface) {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000138 canvas->save();
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000139 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
140
141 SkRect r = SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h));
142
143 SkCanvas* modeCanvas;
reed48eb08a2015-12-25 12:56:03 -0800144 if (nullptr == surface) {
halcanary96fcdcc2015-08-27 07:41:13 -0700145 canvas->saveLayer(&r, nullptr);
Ben Wagnerd3c84ff2018-04-25 14:38:24 -0400146 canvas->clipRect(r);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000147 modeCanvas = canvas;
148 } else {
reed48eb08a2015-12-25 12:56:03 -0800149 modeCanvas = surface->getCanvas();
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000150 }
151
152 SkPaint bgPaint;
153 bgPaint.setAntiAlias(false);
154 bgPaint.setShader(fBGShader);
155 modeCanvas->drawRect(r, bgPaint);
156 modeCanvas->drawRect(r, modePaint);
halcanary96fcdcc2015-08-27 07:41:13 -0700157 modeCanvas = nullptr;
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000158
reed48eb08a2015-12-25 12:56:03 -0800159 if (nullptr == surface) {
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000160 canvas->restore();
161 } else {
Mike Reedb746b1f2021-01-06 08:43:51 -0500162 surface->draw(canvas, 0, 0);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000163 }
164
165 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
166 SkPaint borderPaint;
167 borderPaint.setStyle(SkPaint::kStroke_Style);
168 canvas->drawRect(r, borderPaint);
169
170 canvas->restore();
171 }
172
mtklein36352bf2015-03-25 18:17:31 -0700173 void onOnceBeforeDraw() override {
mtkleindbfd7ab2016-09-01 11:24:54 -0700174 const uint32_t kCheckData[] = {
caryclark4ec1ac62015-07-21 07:42:45 -0700175 SkPackARGB32(0xFF, 0x42, 0x41, 0x42),
176 SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
177 SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
178 SkPackARGB32(0xFF, 0x42, 0x41, 0x42)
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000179 };
180 SkBitmap bg;
reed@google.comeb9a46c2014-01-25 16:46:20 +0000181 bg.allocN32Pixels(2, 2, true);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000182 memcpy(bg.getPixels(), kCheckData, sizeof(kCheckData));
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000183
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000184 SkMatrix lm;
185 lm.setScale(SkIntToScalar(kCheckSize), SkIntToScalar(kCheckSize));
Mike Reedb41bd152020-12-12 11:18:31 -0500186 fBGShader = bg.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
187 SkSamplingOptions(), lm);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000188
189 SkPaint bmpPaint;
reed1a9b9642016-03-13 14:13:58 -0700190 const SkPoint kCenter = { SkIntToScalar(kSize) / 2, SkIntToScalar(kSize) / 2 };
191 const SkColor kColors[] = {
192 SK_ColorTRANSPARENT, 0x80800000, 0xF020F060, SK_ColorWHITE
193 };
194 bmpPaint.setShader(SkGradientShader::MakeRadial(kCenter, 3 * SkIntToScalar(kSize) / 4,
Herb Derbyc37b3862022-06-21 09:49:17 -0400195 kColors, nullptr, std::size(kColors),
Mike Reedfae8fce2019-04-03 10:27:45 -0400196 SkTileMode::kRepeat));
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000197
198 SkBitmap bmp;
reed@google.comeb9a46c2014-01-25 16:46:20 +0000199 bmp.allocN32Pixels(kSize, kSize);
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000200 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
Mike Reedb41bd152020-12-12 11:18:31 -0500207 fBmpShader = bmp.makeShader(SkSamplingOptions());
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000208 }
209
210 enum {
211 kCheckSize = 8,
212 kSize = 30,
213 kTestsPerRow = 15,
214 };
215
reed1a9b9642016-03-13 14:13:58 -0700216 sk_sp<SkShader> fBGShader;
217 sk_sp<SkShader> fBmpShader;
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000218
John Stiles7571f9e2020-09-02 22:42:33 -0400219 using INHERITED = GM;
commit-bot@chromium.org103f2d02013-08-08 21:13:38 +0000220};
221
222//////////////////////////////////////////////////////////////////////////////
223
224DEF_GM(return new Xfermodes3GM;)
225
John Stilesa6841be2020-08-06 14:11:56 -0400226} // namespace skiagm