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