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" |
| 15 | |
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: |
| 26 | Xfermodes3GM() {} |
| 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 onDrawBackground(SkCanvas* canvas) override { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 38 | SkPaint bgPaint; |
caryclark | 4ec1ac6 | 2015-07-21 07:42:45 -0700 | [diff] [blame] | 39 | bgPaint.setColor(sk_tool_utils::color_to_565(0xFF70D0E0)); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 40 | canvas->drawPaint(bgPaint); |
| 41 | } |
| 42 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 43 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 44 | canvas->translate(SkIntToScalar(10), SkIntToScalar(20)); |
| 45 | |
| 46 | SkPaint labelP; |
| 47 | labelP.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 48 | sk_tool_utils::set_portable_typeface(&labelP); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 49 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 50 | constexpr SkColor kSolidColors[] = { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 51 | SK_ColorTRANSPARENT, |
| 52 | SK_ColorBLUE, |
| 53 | 0x80808000 |
| 54 | }; |
| 55 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 56 | constexpr SkColor kBmpAlphas[] = { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 57 | 0xff, |
| 58 | 0x80, |
| 59 | }; |
| 60 | |
Ben Wagner | d3c84ff | 2018-04-25 14:38:24 -0400 | [diff] [blame] | 61 | auto tempSurface(this->makeTempSurface(canvas, kSize, kSize)); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 62 | |
| 63 | int test = 0; |
| 64 | int x = 0, y = 0; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 65 | constexpr struct { SkPaint::Style fStyle; SkScalar fWidth; } kStrokes[] = { |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 66 | {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 Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 70 | for (size_t m = 0; m <= (size_t)SkBlendMode::kLastMode; ++m) { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 71 | SkBlendMode mode = static_cast<SkBlendMode>(m); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 72 | canvas->drawString(SkBlendMode_Name(mode), |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 73 | 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; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 78 | modePaint.setBlendMode(mode); |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 79 | modePaint.setColor(kSolidColors[c]); |
| 80 | modePaint.setStyle(kStrokes[s].fStyle); |
| 81 | modePaint.setStrokeWidth(kStrokes[s].fWidth); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 82 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 83 | this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface.get()); |
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 | ++test; |
| 86 | x += kSize + 10; |
| 87 | if (!(test % kTestsPerRow)) { |
| 88 | x = 0; |
| 89 | y += kSize + 30; |
| 90 | } |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 91 | } |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 92 | for (size_t a = 0; a < SK_ARRAY_COUNT(kBmpAlphas); ++a) { |
| 93 | SkPaint modePaint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 94 | modePaint.setBlendMode(mode); |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 95 | modePaint.setAlpha(kBmpAlphas[a]); |
| 96 | modePaint.setShader(fBmpShader); |
| 97 | modePaint.setStyle(kStrokes[s].fStyle); |
| 98 | modePaint.setStrokeWidth(kStrokes[s].fWidth); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 99 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 100 | this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface.get()); |
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 | ++test; |
| 103 | x += kSize + 10; |
| 104 | if (!(test % kTestsPerRow)) { |
| 105 | x = 0; |
| 106 | y += kSize + 30; |
| 107 | } |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | private: |
| 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 Wagner | d3c84ff | 2018-04-25 14:38:24 -0400 | [diff] [blame] | 119 | * 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] | 120 | */ |
Ben Wagner | d3c84ff | 2018-04-25 14:38:24 -0400 | [diff] [blame] | 121 | sk_sp<SkSurface> makeTempSurface(SkCanvas* baseCanvas, int w, int h) { |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 122 | SkImageInfo baseInfo = baseCanvas->imageInfo(); |
| 123 | SkImageInfo info = SkImageInfo::Make(w, h, baseInfo.colorType(), baseInfo.alphaType(), |
Mike Reed | 693fdbd | 2017-01-12 10:13:40 -0500 | [diff] [blame] | 124 | baseInfo.refColorSpace()); |
Ben Wagner | d3c84ff | 2018-04-25 14:38:24 -0400 | [diff] [blame] | 125 | return baseCanvas->makeSurface(info); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void drawMode(SkCanvas* canvas, |
| 129 | int x, int y, int w, int h, |
reed | 48eb08a | 2015-12-25 12:56:03 -0800 | [diff] [blame] | 130 | const SkPaint& modePaint, SkSurface* surface) { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 131 | canvas->save(); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 132 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 133 | |
| 134 | SkRect r = SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)); |
| 135 | |
| 136 | SkCanvas* modeCanvas; |
reed | 48eb08a | 2015-12-25 12:56:03 -0800 | [diff] [blame] | 137 | if (nullptr == surface) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 138 | canvas->saveLayer(&r, nullptr); |
Ben Wagner | d3c84ff | 2018-04-25 14:38:24 -0400 | [diff] [blame] | 139 | canvas->clipRect(r); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 140 | modeCanvas = canvas; |
| 141 | } else { |
reed | 48eb08a | 2015-12-25 12:56:03 -0800 | [diff] [blame] | 142 | modeCanvas = surface->getCanvas(); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | SkPaint bgPaint; |
| 146 | bgPaint.setAntiAlias(false); |
| 147 | bgPaint.setShader(fBGShader); |
| 148 | modeCanvas->drawRect(r, bgPaint); |
| 149 | modeCanvas->drawRect(r, modePaint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 150 | modeCanvas = nullptr; |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 151 | |
reed | 48eb08a | 2015-12-25 12:56:03 -0800 | [diff] [blame] | 152 | if (nullptr == surface) { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 153 | canvas->restore(); |
| 154 | } else { |
reed | 48eb08a | 2015-12-25 12:56:03 -0800 | [diff] [blame] | 155 | surface->draw(canvas, 0, 0, nullptr); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 156 | } |
| 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 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 166 | void onOnceBeforeDraw() override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 167 | const uint32_t kCheckData[] = { |
caryclark | 4ec1ac6 | 2015-07-21 07:42:45 -0700 | [diff] [blame] | 168 | 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.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 172 | }; |
| 173 | SkBitmap bg; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 174 | bg.allocN32Pixels(2, 2, true); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 175 | memcpy(bg.getPixels(), kCheckData, sizeof(kCheckData)); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 176 | |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 177 | SkMatrix lm; |
| 178 | lm.setScale(SkIntToScalar(kCheckSize), SkIntToScalar(kCheckSize)); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 179 | fBGShader = SkShader::MakeBitmapShader(bg, SkShader::kRepeat_TileMode, |
| 180 | SkShader::kRepeat_TileMode, &lm); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 181 | |
| 182 | SkPaint bmpPaint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 183 | 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.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 190 | |
| 191 | SkBitmap bmp; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 192 | bmp.allocN32Pixels(kSize, kSize); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 193 | 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 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 200 | fBmpShader = SkShader::MakeBitmapShader(bmp, SkShader::kClamp_TileMode, |
| 201 | SkShader::kClamp_TileMode); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | enum { |
| 205 | kCheckSize = 8, |
| 206 | kSize = 30, |
| 207 | kTestsPerRow = 15, |
| 208 | }; |
| 209 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 210 | sk_sp<SkShader> fBGShader; |
| 211 | sk_sp<SkShader> fBmpShader; |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 212 | |
| 213 | typedef GM INHERITED; |
| 214 | }; |
| 215 | |
| 216 | ////////////////////////////////////////////////////////////////////////////// |
| 217 | |
| 218 | DEF_GM(return new Xfermodes3GM;) |
| 219 | |
| 220 | } |