commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
reed | 48eb08a | 2015-12-25 12:56:03 -0800 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkBitmap.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkBlendMode.h" |
| 11 | #include "include/core/SkCanvas.h" |
| 12 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkColorPriv.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 14 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 26 | #include "include/core/SkSurface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 27 | #include "include/core/SkTileMode.h" |
| 28 | #include "include/core/SkTypeface.h" |
| 29 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 30 | #include "include/effects/SkGradientShader.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 31 | #include "tools/ToolUtils.h" |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 32 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 33 | #include <string.h> |
| 34 | |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 35 | namespace skiagm { |
| 36 | |
| 37 | /** |
| 38 | * This tests drawing device-covering rects with solid colors and bitmap shaders over a |
| 39 | * checkerboard background using different xfermodes. |
| 40 | */ |
| 41 | class Xfermodes3GM : public GM { |
| 42 | public: |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 43 | Xfermodes3GM() { this->setBGColor(ToolUtils::color_to_565(0xFF70D0E0)); } |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 44 | |
| 45 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 46 | SkString onShortName() override { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 47 | return SkString("xfermodes3"); |
| 48 | } |
| 49 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 50 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 51 | return SkISize::Make(630, 1215); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 52 | } |
| 53 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 54 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 55 | canvas->translate(SkIntToScalar(10), SkIntToScalar(20)); |
| 56 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 57 | SkFont font(ToolUtils::create_portable_typeface()); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 58 | SkPaint labelP; |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 59 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 60 | constexpr SkColor kSolidColors[] = { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 61 | SK_ColorTRANSPARENT, |
| 62 | SK_ColorBLUE, |
| 63 | 0x80808000 |
| 64 | }; |
| 65 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 66 | constexpr SkColor kBmpAlphas[] = { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 67 | 0xff, |
| 68 | 0x80, |
| 69 | }; |
| 70 | |
Ben Wagner | d3c84ff | 2018-04-25 14:38:24 -0400 | [diff] [blame] | 71 | auto tempSurface(this->makeTempSurface(canvas, kSize, kSize)); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 72 | |
| 73 | int test = 0; |
| 74 | int x = 0, y = 0; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 75 | constexpr struct { SkPaint::Style fStyle; SkScalar fWidth; } kStrokes[] = { |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 76 | {SkPaint::kFill_Style, 0}, |
| 77 | {SkPaint::kStroke_Style, SkIntToScalar(kSize) / 2}, |
| 78 | }; |
| 79 | for (size_t s = 0; s < SK_ARRAY_COUNT(kStrokes); ++s) { |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 80 | for (size_t m = 0; m <= (size_t)SkBlendMode::kLastMode; ++m) { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 81 | SkBlendMode mode = static_cast<SkBlendMode>(m); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 82 | canvas->drawString(SkBlendMode_Name(mode), |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 83 | SkIntToScalar(x), |
| 84 | SkIntToScalar(y + kSize + 3) + font.getSize(), |
| 85 | font, labelP); |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 86 | for (size_t c = 0; c < SK_ARRAY_COUNT(kSolidColors); ++c) { |
| 87 | SkPaint modePaint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 88 | modePaint.setBlendMode(mode); |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 89 | modePaint.setColor(kSolidColors[c]); |
| 90 | modePaint.setStyle(kStrokes[s].fStyle); |
| 91 | modePaint.setStrokeWidth(kStrokes[s].fWidth); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 92 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 93 | this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface.get()); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 94 | |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 95 | ++test; |
| 96 | x += kSize + 10; |
| 97 | if (!(test % kTestsPerRow)) { |
| 98 | x = 0; |
| 99 | y += kSize + 30; |
| 100 | } |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 101 | } |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 102 | for (size_t a = 0; a < SK_ARRAY_COUNT(kBmpAlphas); ++a) { |
| 103 | SkPaint modePaint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 104 | modePaint.setBlendMode(mode); |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 105 | modePaint.setAlpha(kBmpAlphas[a]); |
| 106 | modePaint.setShader(fBmpShader); |
| 107 | modePaint.setStyle(kStrokes[s].fStyle); |
| 108 | modePaint.setStrokeWidth(kStrokes[s].fWidth); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 109 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 110 | this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface.get()); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 111 | |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 112 | ++test; |
| 113 | x += kSize + 10; |
| 114 | if (!(test % kTestsPerRow)) { |
| 115 | x = 0; |
| 116 | y += kSize + 30; |
| 117 | } |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | private: |
| 124 | /** |
| 125 | * GrContext has optimizations around full rendertarget draws that can be replaced with clears. |
| 126 | * We are trying to test those. We could use saveLayer() to create small SkGpuDevices but |
| 127 | * saveLayer() uses the texture cache. This means that the actual render target may be larger |
| 128 | * than the layer. Because the clip will contain the layer's bounds, no draws will be full-RT. |
Ben Wagner | d3c84ff | 2018-04-25 14:38:24 -0400 | [diff] [blame] | 129 | * So explicitly create a temporary canvas with dimensions exactly the layer size. |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 130 | */ |
Ben Wagner | d3c84ff | 2018-04-25 14:38:24 -0400 | [diff] [blame] | 131 | sk_sp<SkSurface> makeTempSurface(SkCanvas* baseCanvas, int w, int h) { |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 132 | SkImageInfo baseInfo = baseCanvas->imageInfo(); |
| 133 | SkImageInfo info = SkImageInfo::Make(w, h, baseInfo.colorType(), baseInfo.alphaType(), |
Mike Reed | 693fdbd | 2017-01-12 10:13:40 -0500 | [diff] [blame] | 134 | baseInfo.refColorSpace()); |
Ben Wagner | d3c84ff | 2018-04-25 14:38:24 -0400 | [diff] [blame] | 135 | return baseCanvas->makeSurface(info); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void drawMode(SkCanvas* canvas, |
| 139 | int x, int y, int w, int h, |
reed | 48eb08a | 2015-12-25 12:56:03 -0800 | [diff] [blame] | 140 | const SkPaint& modePaint, SkSurface* surface) { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 141 | canvas->save(); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 142 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 143 | |
| 144 | SkRect r = SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)); |
| 145 | |
| 146 | SkCanvas* modeCanvas; |
reed | 48eb08a | 2015-12-25 12:56:03 -0800 | [diff] [blame] | 147 | if (nullptr == surface) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 148 | canvas->saveLayer(&r, nullptr); |
Ben Wagner | d3c84ff | 2018-04-25 14:38:24 -0400 | [diff] [blame] | 149 | canvas->clipRect(r); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 150 | modeCanvas = canvas; |
| 151 | } else { |
reed | 48eb08a | 2015-12-25 12:56:03 -0800 | [diff] [blame] | 152 | modeCanvas = surface->getCanvas(); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | SkPaint bgPaint; |
| 156 | bgPaint.setAntiAlias(false); |
| 157 | bgPaint.setShader(fBGShader); |
| 158 | modeCanvas->drawRect(r, bgPaint); |
| 159 | modeCanvas->drawRect(r, modePaint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 160 | modeCanvas = nullptr; |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 161 | |
reed | 48eb08a | 2015-12-25 12:56:03 -0800 | [diff] [blame] | 162 | if (nullptr == surface) { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 163 | canvas->restore(); |
| 164 | } else { |
Mike Reed | b746b1f | 2021-01-06 08:43:51 -0500 | [diff] [blame] | 165 | surface->draw(canvas, 0, 0); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | r.inset(-SK_ScalarHalf, -SK_ScalarHalf); |
| 169 | SkPaint borderPaint; |
| 170 | borderPaint.setStyle(SkPaint::kStroke_Style); |
| 171 | canvas->drawRect(r, borderPaint); |
| 172 | |
| 173 | canvas->restore(); |
| 174 | } |
| 175 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 176 | void onOnceBeforeDraw() override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 177 | const uint32_t kCheckData[] = { |
caryclark | 4ec1ac6 | 2015-07-21 07:42:45 -0700 | [diff] [blame] | 178 | SkPackARGB32(0xFF, 0x42, 0x41, 0x42), |
| 179 | SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6), |
| 180 | SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6), |
| 181 | SkPackARGB32(0xFF, 0x42, 0x41, 0x42) |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 182 | }; |
| 183 | SkBitmap bg; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 184 | bg.allocN32Pixels(2, 2, true); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 185 | memcpy(bg.getPixels(), kCheckData, sizeof(kCheckData)); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 186 | |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 187 | SkMatrix lm; |
| 188 | lm.setScale(SkIntToScalar(kCheckSize), SkIntToScalar(kCheckSize)); |
Mike Reed | b41bd15 | 2020-12-12 11:18:31 -0500 | [diff] [blame] | 189 | fBGShader = bg.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, |
| 190 | SkSamplingOptions(), lm); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 191 | |
| 192 | SkPaint bmpPaint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 193 | const SkPoint kCenter = { SkIntToScalar(kSize) / 2, SkIntToScalar(kSize) / 2 }; |
| 194 | const SkColor kColors[] = { |
| 195 | SK_ColorTRANSPARENT, 0x80800000, 0xF020F060, SK_ColorWHITE |
| 196 | }; |
| 197 | bmpPaint.setShader(SkGradientShader::MakeRadial(kCenter, 3 * SkIntToScalar(kSize) / 4, |
| 198 | kColors, nullptr, SK_ARRAY_COUNT(kColors), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 199 | SkTileMode::kRepeat)); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 200 | |
| 201 | SkBitmap bmp; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 202 | bmp.allocN32Pixels(kSize, kSize); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 203 | SkCanvas bmpCanvas(bmp); |
| 204 | |
| 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); |
| 209 | |
Mike Reed | b41bd15 | 2020-12-12 11:18:31 -0500 | [diff] [blame] | 210 | fBmpShader = bmp.makeShader(SkSamplingOptions()); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | enum { |
| 214 | kCheckSize = 8, |
| 215 | kSize = 30, |
| 216 | kTestsPerRow = 15, |
| 217 | }; |
| 218 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 219 | sk_sp<SkShader> fBGShader; |
| 220 | sk_sp<SkShader> fBmpShader; |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 221 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 222 | using INHERITED = GM; |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | ////////////////////////////////////////////////////////////////////////////// |
| 226 | |
| 227 | DEF_GM(return new Xfermodes3GM;) |
| 228 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 229 | } // namespace skiagm |