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" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkGradientShader.h" |
reed | 48eb08a | 2015-12-25 12:56:03 -0800 | [diff] [blame] | 11 | #include "SkSurface.h" |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 12 | #include "SkXfermode.h" |
| 13 | #include "SkColorPriv.h" |
| 14 | |
| 15 | #if SK_SUPPORT_GPU |
| 16 | #include "GrContext.h" |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 17 | #endif |
| 18 | |
| 19 | namespace skiagm { |
| 20 | |
| 21 | /** |
| 22 | * This tests drawing device-covering rects with solid colors and bitmap shaders over a |
| 23 | * checkerboard background using different xfermodes. |
| 24 | */ |
| 25 | class Xfermodes3GM : public GM { |
| 26 | public: |
| 27 | Xfermodes3GM() {} |
| 28 | |
| 29 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 30 | SkString onShortName() override { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 31 | return SkString("xfermodes3"); |
| 32 | } |
| 33 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 35 | return SkISize::Make(630, 1215); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 36 | } |
| 37 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 38 | void onDrawBackground(SkCanvas* canvas) override { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 39 | SkPaint bgPaint; |
caryclark | 4ec1ac6 | 2015-07-21 07:42:45 -0700 | [diff] [blame] | 40 | bgPaint.setColor(sk_tool_utils::color_to_565(0xFF70D0E0)); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 41 | canvas->drawPaint(bgPaint); |
| 42 | } |
| 43 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 44 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 45 | canvas->translate(SkIntToScalar(10), SkIntToScalar(20)); |
| 46 | |
| 47 | SkPaint labelP; |
| 48 | labelP.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 49 | sk_tool_utils::set_portable_typeface(&labelP); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 50 | |
| 51 | static const SkColor kSolidColors[] = { |
| 52 | SK_ColorTRANSPARENT, |
| 53 | SK_ColorBLUE, |
| 54 | 0x80808000 |
| 55 | }; |
| 56 | |
| 57 | static const SkColor kBmpAlphas[] = { |
| 58 | 0xff, |
| 59 | 0x80, |
| 60 | }; |
| 61 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 62 | auto tempSurface(this->possiblyCreateTempSurface(canvas, kSize, kSize)); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 63 | |
| 64 | int test = 0; |
| 65 | int x = 0, y = 0; |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 66 | static const struct { SkPaint::Style fStyle; SkScalar fWidth; } kStrokes[] = { |
| 67 | {SkPaint::kFill_Style, 0}, |
| 68 | {SkPaint::kStroke_Style, SkIntToScalar(kSize) / 2}, |
| 69 | }; |
| 70 | for (size_t s = 0; s < SK_ARRAY_COUNT(kStrokes); ++s) { |
| 71 | for (size_t m = 0; m <= SkXfermode::kLastMode; ++m) { |
| 72 | SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(m); |
| 73 | canvas->drawText(SkXfermode::ModeName(mode), |
| 74 | strlen(SkXfermode::ModeName(mode)), |
| 75 | SkIntToScalar(x), |
| 76 | SkIntToScalar(y + kSize + 3) + labelP.getTextSize(), |
| 77 | labelP); |
| 78 | for (size_t c = 0; c < SK_ARRAY_COUNT(kSolidColors); ++c) { |
| 79 | SkPaint modePaint; |
| 80 | modePaint.setXfermodeMode(mode); |
| 81 | modePaint.setColor(kSolidColors[c]); |
| 82 | modePaint.setStyle(kStrokes[s].fStyle); |
| 83 | modePaint.setStrokeWidth(kStrokes[s].fWidth); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 84 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 85 | this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface.get()); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 86 | |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 87 | ++test; |
| 88 | x += kSize + 10; |
| 89 | if (!(test % kTestsPerRow)) { |
| 90 | x = 0; |
| 91 | y += kSize + 30; |
| 92 | } |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 93 | } |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 94 | for (size_t a = 0; a < SK_ARRAY_COUNT(kBmpAlphas); ++a) { |
| 95 | SkPaint modePaint; |
| 96 | modePaint.setXfermodeMode(mode); |
| 97 | modePaint.setAlpha(kBmpAlphas[a]); |
| 98 | modePaint.setShader(fBmpShader); |
| 99 | modePaint.setStyle(kStrokes[s].fStyle); |
| 100 | modePaint.setStrokeWidth(kStrokes[s].fWidth); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 101 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 102 | this->drawMode(canvas, x, y, kSize, kSize, modePaint, tempSurface.get()); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 103 | |
commit-bot@chromium.org | de1559c | 2013-08-14 19:20:45 +0000 | [diff] [blame] | 104 | ++test; |
| 105 | x += kSize + 10; |
| 106 | if (!(test % kTestsPerRow)) { |
| 107 | x = 0; |
| 108 | y += kSize + 30; |
| 109 | } |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private: |
| 116 | /** |
| 117 | * GrContext has optimizations around full rendertarget draws that can be replaced with clears. |
| 118 | * We are trying to test those. We could use saveLayer() to create small SkGpuDevices but |
| 119 | * saveLayer() uses the texture cache. This means that the actual render target may be larger |
| 120 | * than the layer. Because the clip will contain the layer's bounds, no draws will be full-RT. |
| 121 | * So when running on a GPU canvas we explicitly create a temporary canvas using a texture with |
| 122 | * dimensions exactly matching the layer size. |
| 123 | */ |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 124 | sk_sp<SkSurface> possiblyCreateTempSurface(SkCanvas* baseCanvas, int w, int h) { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 125 | #if SK_SUPPORT_GPU |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 126 | GrContext* context = baseCanvas->getGrContext(); |
bsalomon | afe3005 | 2015-01-16 07:32:33 -0800 | [diff] [blame] | 127 | SkImageInfo baseInfo = baseCanvas->imageInfo(); |
| 128 | SkImageInfo info = SkImageInfo::Make(w, h, baseInfo.colorType(), baseInfo.alphaType(), |
| 129 | baseInfo.profileType()); |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 130 | SkSurfaceProps canvasProps(SkSurfaceProps::kLegacyFontHost_InitType); |
| 131 | baseCanvas->getProps(&canvasProps); |
| 132 | return SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, &canvasProps); |
reed | 48eb08a | 2015-12-25 12:56:03 -0800 | [diff] [blame] | 133 | #else |
| 134 | return nullptr; |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 135 | #endif |
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); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 149 | modeCanvas = canvas; |
| 150 | } else { |
reed | 48eb08a | 2015-12-25 12:56:03 -0800 | [diff] [blame] | 151 | modeCanvas = surface->getCanvas(); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | SkPaint bgPaint; |
| 155 | bgPaint.setAntiAlias(false); |
| 156 | bgPaint.setShader(fBGShader); |
| 157 | modeCanvas->drawRect(r, bgPaint); |
| 158 | modeCanvas->drawRect(r, modePaint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 159 | modeCanvas = nullptr; |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 160 | |
reed | 48eb08a | 2015-12-25 12:56:03 -0800 | [diff] [blame] | 161 | if (nullptr == surface) { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 162 | canvas->restore(); |
| 163 | } else { |
reed | 48eb08a | 2015-12-25 12:56:03 -0800 | [diff] [blame] | 164 | surface->draw(canvas, 0, 0, nullptr); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | r.inset(-SK_ScalarHalf, -SK_ScalarHalf); |
| 168 | SkPaint borderPaint; |
| 169 | borderPaint.setStyle(SkPaint::kStroke_Style); |
| 170 | canvas->drawRect(r, borderPaint); |
| 171 | |
| 172 | canvas->restore(); |
| 173 | } |
| 174 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 175 | void onOnceBeforeDraw() override { |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 176 | static const uint32_t kCheckData[] = { |
caryclark | 4ec1ac6 | 2015-07-21 07:42:45 -0700 | [diff] [blame] | 177 | SkPackARGB32(0xFF, 0x42, 0x41, 0x42), |
| 178 | SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6), |
| 179 | SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6), |
| 180 | SkPackARGB32(0xFF, 0x42, 0x41, 0x42) |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 181 | }; |
| 182 | SkBitmap bg; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 183 | bg.allocN32Pixels(2, 2, true); |
commit-bot@chromium.org | 103f2d0 | 2013-08-08 21:13:38 +0000 | [diff] [blame] | 184 | SkAutoLockPixels bgAlp(bg); |
| 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 | } |