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 | |
| 16 | #if SK_SUPPORT_GPU |
| 17 | #include "GrContext.h" |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 18 | #endif |
| 19 | |
| 20 | namespace skiagm { |
| 21 | |
| 22 | /** |
| 23 | * This tests drawing device-covering rects with solid colors and bitmap shaders over a |
| 24 | * checkerboard background using different xfermodes. |
| 25 | */ |
| 26 | class Xfermodes3GM : public GM { |
| 27 | public: |
| 28 | Xfermodes3GM() {} |
| 29 | |
| 30 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 31 | SkString onShortName() override { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 32 | return SkString("xfermodes3"); |
| 33 | } |
| 34 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 35 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 36 | return SkISize::Make(630, 1215); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 37 | } |
| 38 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 39 | void onDrawBackground(SkCanvas* canvas) override { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 40 | SkPaint bgPaint; |
caryclark | 4ec1ac6 | 2015-07-21 07:42:45 -0700 | [diff] [blame] | 41 | bgPaint.setColor(sk_tool_utils::color_to_565(0xFF70D0E0)); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 42 | canvas->drawPaint(bgPaint); |
| 43 | } |
| 44 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 45 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 46 | canvas->translate(SkIntToScalar(10), SkIntToScalar(20)); |
| 47 | |
| 48 | SkPaint labelP; |
| 49 | labelP.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 50 | sk_tool_utils::set_portable_typeface(&labelP); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 51 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 52 | constexpr SkColor kSolidColors[] = { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 53 | SK_ColorTRANSPARENT, |
| 54 | SK_ColorBLUE, |
| 55 | 0x80808000 |
| 56 | }; |
| 57 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 58 | constexpr SkColor kBmpAlphas[] = { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 59 | 0xff, |
| 60 | 0x80, |
| 61 | }; |
| 62 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 63 | auto tempSurface(this->possiblyCreateTempSurface(canvas, kSize, kSize)); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 64 | |
| 65 | int test = 0; |
| 66 | int x = 0, y = 0; |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 67 | constexpr struct { SkPaint::Style fStyle; SkScalar fWidth; } kStrokes[] = { |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 68 | {SkPaint::kFill_Style, 0}, |
| 69 | {SkPaint::kStroke_Style, SkIntToScalar(kSize) / 2}, |
| 70 | }; |
| 71 | for (size_t s = 0; s < SK_ARRAY_COUNT(kStrokes); ++s) { |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 72 | for (size_t m = 0; m <= (size_t)SkBlendMode::kLastMode; ++m) { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 73 | SkBlendMode mode = static_cast<SkBlendMode>(m); |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 74 | canvas->drawText(SkBlendMode_Name(mode), |
| 75 | strlen(SkBlendMode_Name(mode)), |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 76 | SkIntToScalar(x), |
| 77 | SkIntToScalar(y + kSize + 3) + labelP.getTextSize(), |
| 78 | labelP); |
| 79 | for (size_t c = 0; c < SK_ARRAY_COUNT(kSolidColors); ++c) { |
| 80 | SkPaint modePaint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 81 | modePaint.setBlendMode(mode); |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 82 | modePaint.setColor(kSolidColors[c]); |
| 83 | modePaint.setStyle(kStrokes[s].fStyle); |
| 84 | modePaint.setStrokeWidth(kStrokes[s].fWidth); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 85 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 86 | this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface.get()); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 87 | |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 88 | ++test; |
| 89 | x += kSize + 10; |
| 90 | if (!(test % kTestsPerRow)) { |
| 91 | x = 0; |
| 92 | y += kSize + 30; |
| 93 | } |
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 | for (size_t a = 0; a < SK_ARRAY_COUNT(kBmpAlphas); ++a) { |
| 96 | SkPaint modePaint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 97 | modePaint.setBlendMode(mode); |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 98 | modePaint.setAlpha(kBmpAlphas[a]); |
| 99 | modePaint.setShader(fBmpShader); |
| 100 | modePaint.setStyle(kStrokes[s].fStyle); |
| 101 | modePaint.setStrokeWidth(kStrokes[s].fWidth); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 102 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 103 | this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface.get()); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 104 | |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 105 | ++test; |
| 106 | x += kSize + 10; |
| 107 | if (!(test % kTestsPerRow)) { |
| 108 | x = 0; |
| 109 | y += kSize + 30; |
| 110 | } |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | private: |
| 117 | /** |
| 118 | * GrContext has optimizations around full rendertarget draws that can be replaced with clears. |
| 119 | * We are trying to test those. We could use saveLayer() to create small SkGpuDevices but |
| 120 | * saveLayer() uses the texture cache. This means that the actual render target may be larger |
| 121 | * than the layer. Because the clip will contain the layer's bounds, no draws will be full-RT. |
| 122 | * So when running on a GPU canvas we explicitly create a temporary canvas using a texture with |
| 123 | * dimensions exactly matching the layer size. |
| 124 | */ |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 125 | sk_sp<SkSurface> possiblyCreateTempSurface(SkCanvas* baseCanvas, int w, int h) { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 126 | #if SK_SUPPORT_GPU |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 127 | GrContext* context = baseCanvas->getGrContext(); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 128 | SkImageInfo baseInfo = baseCanvas->imageInfo(); |
| 129 | SkImageInfo info = SkImageInfo::Make(w, h, baseInfo.colorType(), baseInfo.alphaType(), |
Mike Reed | 693fdbd | 2017-01-12 10:13:40 -0500 | [diff] [blame] | 130 | baseInfo.refColorSpace()); |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 131 | SkSurfaceProps canvasProps(SkSurfaceProps::kLegacyFontHost_InitType); |
| 132 | baseCanvas->getProps(&canvasProps); |
| 133 | return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, &canvasProps); |
reed | 48eb08a | 2015-12-25 12:56:03 -0800 | [diff] [blame] | 134 | #else |
| 135 | return nullptr; |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 136 | #endif |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void drawMode(SkCanvas* canvas, |
| 140 | int x, int y, int w, int h, |
reed | 48eb08a | 2015-12-25 12:56:03 -0800 | [diff] [blame] | 141 | const SkPaint& modePaint, SkSurface* surface) { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 142 | canvas->save(); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 143 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 144 | |
| 145 | SkRect r = SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)); |
| 146 | |
| 147 | SkCanvas* modeCanvas; |
reed | 48eb08a | 2015-12-25 12:56:03 -0800 | [diff] [blame] | 148 | if (nullptr == surface) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 149 | canvas->saveLayer(&r, nullptr); |
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 { |
reed | 48eb08a | 2015-12-25 12:56:03 -0800 | [diff] [blame] | 165 | surface->draw(canvas, 0, 0, nullptr); |
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)); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 189 | fBGShader = SkShader::MakeBitmapShader(bg, SkShader::kRepeat_TileMode, |
| 190 | SkShader::kRepeat_TileMode, &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), |
| 199 | SkShader::kRepeat_TileMode)); |
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 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 210 | fBmpShader = SkShader::MakeBitmapShader(bmp, SkShader::kClamp_TileMode, |
| 211 | SkShader::kClamp_TileMode); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | enum { |
| 215 | kCheckSize = 8, |
| 216 | kSize = 30, |
| 217 | kTestsPerRow = 15, |
| 218 | }; |
| 219 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 220 | sk_sp<SkShader> fBGShader; |
| 221 | sk_sp<SkShader> fBmpShader; |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 222 | |
| 223 | typedef GM INHERITED; |
| 224 | }; |
| 225 | |
| 226 | ////////////////////////////////////////////////////////////////////////////// |
| 227 | |
| 228 | DEF_GM(return new Xfermodes3GM;) |
| 229 | |
| 230 | } |