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