bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +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 | */ |
| 7 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 8 | #include "sk_tool_utils.h" |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 9 | #include "SkBitmap.h" |
| 10 | #include "SkShader.h" |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 11 | #include "SkBlendModePriv.h" |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 12 | #include "SkColorPriv.h" |
Mike Reed | 331ccfd | 2018-10-25 12:36:06 -0400 | [diff] [blame] | 13 | #include "SkTextUtils.h" |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 14 | |
| 15 | namespace skiagm { |
| 16 | |
| 17 | class Xfermodes2GM : public GM { |
| 18 | public: |
| 19 | Xfermodes2GM() {} |
| 20 | |
| 21 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 22 | SkString onShortName() override { |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 23 | return SkString("xfermodes2"); |
| 24 | } |
| 25 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 26 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 27 | return SkISize::Make(455, 475); |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 28 | } |
| 29 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 30 | void onDraw(SkCanvas* canvas) override { |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 31 | canvas->translate(SkIntToScalar(10), SkIntToScalar(20)); |
| 32 | |
| 33 | const SkScalar w = SkIntToScalar(kSize); |
| 34 | const SkScalar h = SkIntToScalar(kSize); |
| 35 | |
| 36 | SkPaint labelP; |
| 37 | labelP.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 38 | sk_tool_utils::set_portable_typeface(&labelP); |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 39 | |
| 40 | const int W = 6; |
| 41 | |
| 42 | SkScalar x = 0, y = 0; |
Mike Reed | d470673 | 2016-11-15 16:44:34 -0500 | [diff] [blame] | 43 | for (size_t m = 0; m <= (size_t)SkBlendMode::kLastMode; m++) { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 44 | SkBlendMode mode = static_cast<SkBlendMode>(m); |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 45 | |
| 46 | canvas->save(); |
| 47 | |
| 48 | canvas->translate(x, y); |
| 49 | SkPaint p; |
| 50 | p.setAntiAlias(false); |
| 51 | p.setStyle(SkPaint::kFill_Style); |
| 52 | p.setShader(fBG); |
| 53 | SkRect r = SkRect::MakeWH(w, h); |
| 54 | canvas->drawRect(r, p); |
| 55 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 56 | canvas->saveLayer(&r, nullptr); |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 57 | |
| 58 | p.setShader(fDst); |
| 59 | canvas->drawRect(r, p); |
| 60 | p.setShader(fSrc); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 61 | p.setBlendMode(mode); |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 62 | canvas->drawRect(r, p); |
| 63 | |
| 64 | canvas->restore(); |
| 65 | |
| 66 | r.inset(-SK_ScalarHalf, -SK_ScalarHalf); |
| 67 | p.setStyle(SkPaint::kStroke_Style); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 68 | p.setShader(nullptr); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 69 | p.setBlendMode(SkBlendMode::kSrcOver); |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 70 | canvas->drawRect(r, p); |
| 71 | |
| 72 | canvas->restore(); |
| 73 | |
| 74 | #if 1 |
Mike Reed | 331ccfd | 2018-10-25 12:36:06 -0400 | [diff] [blame] | 75 | SkTextUtils::DrawString(canvas, SkBlendMode_Name(mode), |
| 76 | x + w/2, y - labelP.getTextSize()/2, labelP, |
Mike Reed | 3a42ec0 | 2018-10-30 12:53:21 -0400 | [diff] [blame] | 77 | SkTextUtils::kCenter_Align); |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 78 | #endif |
| 79 | x += w + SkIntToScalar(10); |
| 80 | if ((m % W) == W - 1) { |
| 81 | x = 0; |
| 82 | y += h + SkIntToScalar(30); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | private: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 88 | void onOnceBeforeDraw() override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 89 | const uint32_t kCheckData[] = { |
caryclark | 4ec1ac6 | 2015-07-21 07:42:45 -0700 | [diff] [blame] | 90 | SkPackARGB32(0xFF, 0x42, 0x41, 0x42), |
| 91 | SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6), |
| 92 | SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6), |
| 93 | SkPackARGB32(0xFF, 0x42, 0x41, 0x42) |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 94 | }; |
| 95 | SkBitmap bg; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 96 | bg.allocN32Pixels(2, 2, true); |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 97 | memcpy(bg.getPixels(), kCheckData, sizeof(kCheckData)); |
skia.committer@gmail.com | cbbdab5 | 2013-04-18 07:01:48 +0000 | [diff] [blame] | 98 | |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 99 | SkMatrix lm; |
| 100 | lm.setScale(SkIntToScalar(16), SkIntToScalar(16)); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 101 | fBG = SkShader::MakeBitmapShader(bg, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, |
| 102 | &lm); |
skia.committer@gmail.com | cbbdab5 | 2013-04-18 07:01:48 +0000 | [diff] [blame] | 103 | |
cdalton | d3e9191 | 2015-03-30 07:52:57 -0700 | [diff] [blame] | 104 | SkBitmap srcBmp; |
| 105 | srcBmp.allocN32Pixels(kSize, kSize); |
| 106 | SkPMColor* pixels = reinterpret_cast<SkPMColor*>(srcBmp.getPixels()); |
skia.committer@gmail.com | cbbdab5 | 2013-04-18 07:01:48 +0000 | [diff] [blame] | 107 | |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 108 | for (int y = 0; y < kSize; ++y) { |
| 109 | int c = y * (1 << kShift); |
| 110 | SkPMColor rowColor = SkPackARGB32(c, c, 0, c/2); |
| 111 | for (int x = 0; x < kSize; ++x) { |
| 112 | pixels[kSize * y + x] = rowColor; |
| 113 | } |
| 114 | } |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 115 | fSrc = SkShader::MakeBitmapShader(srcBmp, SkShader::kClamp_TileMode, |
| 116 | SkShader::kClamp_TileMode); |
cdalton | d3e9191 | 2015-03-30 07:52:57 -0700 | [diff] [blame] | 117 | SkBitmap dstBmp; |
| 118 | dstBmp.allocN32Pixels(kSize, kSize); |
| 119 | pixels = reinterpret_cast<SkPMColor*>(dstBmp.getPixels()); |
skia.committer@gmail.com | cbbdab5 | 2013-04-18 07:01:48 +0000 | [diff] [blame] | 120 | |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 121 | for (int x = 0; x < kSize; ++x) { |
| 122 | int c = x * (1 << kShift); |
| 123 | SkPMColor colColor = SkPackARGB32(c, 0, c, c/2); |
| 124 | for (int y = 0; y < kSize; ++y) { |
| 125 | pixels[kSize * y + x] = colColor; |
| 126 | } |
| 127 | } |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 128 | fDst = SkShader::MakeBitmapShader(dstBmp, SkShader::kClamp_TileMode, |
| 129 | SkShader::kClamp_TileMode); |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 130 | } |
skia.committer@gmail.com | cbbdab5 | 2013-04-18 07:01:48 +0000 | [diff] [blame] | 131 | |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 132 | enum { |
| 133 | kShift = 2, |
| 134 | kSize = 256 >> kShift, |
| 135 | }; |
skia.committer@gmail.com | cbbdab5 | 2013-04-18 07:01:48 +0000 | [diff] [blame] | 136 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 137 | sk_sp<SkShader> fBG; |
| 138 | sk_sp<SkShader> fSrc; |
| 139 | sk_sp<SkShader> fDst; |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 140 | |
| 141 | typedef GM INHERITED; |
| 142 | }; |
| 143 | |
| 144 | ////////////////////////////////////////////////////////////////////////////// |
| 145 | |
| 146 | static GM* MyFactory(void*) { return new Xfermodes2GM; } |
| 147 | static GMRegistry reg(MyFactory); |
| 148 | |
| 149 | } |