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