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" |
| 13 | |
| 14 | namespace skiagm { |
| 15 | |
| 16 | class Xfermodes2GM : public GM { |
| 17 | public: |
| 18 | Xfermodes2GM() {} |
| 19 | |
| 20 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 21 | SkString onShortName() override { |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 22 | return SkString("xfermodes2"); |
| 23 | } |
| 24 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 25 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 26 | return SkISize::Make(455, 475); |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 27 | } |
| 28 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | void onDraw(SkCanvas* canvas) override { |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 30 | canvas->translate(SkIntToScalar(10), SkIntToScalar(20)); |
| 31 | |
| 32 | const SkScalar w = SkIntToScalar(kSize); |
| 33 | const SkScalar h = SkIntToScalar(kSize); |
| 34 | |
| 35 | SkPaint labelP; |
| 36 | labelP.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 37 | sk_tool_utils::set_portable_typeface(&labelP); |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 38 | labelP.setTextAlign(SkPaint::kCenter_Align); |
| 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 |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 75 | canvas->drawString(SkBlendMode_Name(mode), |
| 76 | x + w/2, y - labelP.getTextSize()/2, labelP); |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 77 | #endif |
| 78 | x += w + SkIntToScalar(10); |
| 79 | if ((m % W) == W - 1) { |
| 80 | x = 0; |
| 81 | y += h + SkIntToScalar(30); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 87 | void onOnceBeforeDraw() override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 88 | const uint32_t kCheckData[] = { |
caryclark | 4ec1ac6 | 2015-07-21 07:42:45 -0700 | [diff] [blame] | 89 | SkPackARGB32(0xFF, 0x42, 0x41, 0x42), |
| 90 | SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6), |
| 91 | SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6), |
| 92 | SkPackARGB32(0xFF, 0x42, 0x41, 0x42) |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 93 | }; |
| 94 | SkBitmap bg; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 95 | bg.allocN32Pixels(2, 2, true); |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 96 | memcpy(bg.getPixels(), kCheckData, sizeof(kCheckData)); |
skia.committer@gmail.com | cbbdab5 | 2013-04-18 07:01:48 +0000 | [diff] [blame] | 97 | |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 98 | SkMatrix lm; |
| 99 | lm.setScale(SkIntToScalar(16), SkIntToScalar(16)); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 100 | fBG = SkShader::MakeBitmapShader(bg, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode, |
| 101 | &lm); |
skia.committer@gmail.com | cbbdab5 | 2013-04-18 07:01:48 +0000 | [diff] [blame] | 102 | |
cdalton | d3e9191 | 2015-03-30 07:52:57 -0700 | [diff] [blame] | 103 | SkBitmap srcBmp; |
| 104 | srcBmp.allocN32Pixels(kSize, kSize); |
| 105 | SkPMColor* pixels = reinterpret_cast<SkPMColor*>(srcBmp.getPixels()); |
skia.committer@gmail.com | cbbdab5 | 2013-04-18 07:01:48 +0000 | [diff] [blame] | 106 | |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 107 | for (int y = 0; y < kSize; ++y) { |
| 108 | int c = y * (1 << kShift); |
| 109 | SkPMColor rowColor = SkPackARGB32(c, c, 0, c/2); |
| 110 | for (int x = 0; x < kSize; ++x) { |
| 111 | pixels[kSize * y + x] = rowColor; |
| 112 | } |
| 113 | } |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 114 | fSrc = SkShader::MakeBitmapShader(srcBmp, SkShader::kClamp_TileMode, |
| 115 | SkShader::kClamp_TileMode); |
cdalton | d3e9191 | 2015-03-30 07:52:57 -0700 | [diff] [blame] | 116 | SkBitmap dstBmp; |
| 117 | dstBmp.allocN32Pixels(kSize, kSize); |
| 118 | pixels = reinterpret_cast<SkPMColor*>(dstBmp.getPixels()); |
skia.committer@gmail.com | cbbdab5 | 2013-04-18 07:01:48 +0000 | [diff] [blame] | 119 | |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 120 | for (int x = 0; x < kSize; ++x) { |
| 121 | int c = x * (1 << kShift); |
| 122 | SkPMColor colColor = SkPackARGB32(c, 0, c, c/2); |
| 123 | for (int y = 0; y < kSize; ++y) { |
| 124 | pixels[kSize * y + x] = colColor; |
| 125 | } |
| 126 | } |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 127 | fDst = SkShader::MakeBitmapShader(dstBmp, SkShader::kClamp_TileMode, |
| 128 | SkShader::kClamp_TileMode); |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 129 | } |
skia.committer@gmail.com | cbbdab5 | 2013-04-18 07:01:48 +0000 | [diff] [blame] | 130 | |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 131 | enum { |
| 132 | kShift = 2, |
| 133 | kSize = 256 >> kShift, |
| 134 | }; |
skia.committer@gmail.com | cbbdab5 | 2013-04-18 07:01:48 +0000 | [diff] [blame] | 135 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 136 | sk_sp<SkShader> fBG; |
| 137 | sk_sp<SkShader> fSrc; |
| 138 | sk_sp<SkShader> fDst; |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 139 | |
| 140 | typedef GM INHERITED; |
| 141 | }; |
| 142 | |
| 143 | ////////////////////////////////////////////////////////////////////////////// |
| 144 | |
| 145 | static GM* MyFactory(void*) { return new Xfermodes2GM; } |
| 146 | static GMRegistry reg(MyFactory); |
| 147 | |
| 148 | } |