bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2013 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | #include "gm.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkShader.h" |
| 11 | #include "SkXfermode.h" |
| 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; |
| 43 | for (size_t m = 0; m <= SkXfermode::kLastMode; m++) { |
| 44 | SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(m); |
| 45 | SkXfermode* xm = SkXfermode::Create(mode); |
| 46 | SkAutoUnref aur(xm); |
| 47 | |
| 48 | canvas->save(); |
| 49 | |
| 50 | canvas->translate(x, y); |
| 51 | SkPaint p; |
| 52 | p.setAntiAlias(false); |
| 53 | p.setStyle(SkPaint::kFill_Style); |
| 54 | p.setShader(fBG); |
| 55 | SkRect r = SkRect::MakeWH(w, h); |
| 56 | canvas->drawRect(r, p); |
| 57 | |
commit-bot@chromium.org | 091a594 | 2014-04-18 14:19:31 +0000 | [diff] [blame] | 58 | canvas->saveLayer(&r, NULL); |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 59 | |
| 60 | p.setShader(fDst); |
| 61 | canvas->drawRect(r, p); |
| 62 | p.setShader(fSrc); |
| 63 | p.setXfermode(xm); |
| 64 | canvas->drawRect(r, p); |
| 65 | |
| 66 | canvas->restore(); |
| 67 | |
| 68 | r.inset(-SK_ScalarHalf, -SK_ScalarHalf); |
| 69 | p.setStyle(SkPaint::kStroke_Style); |
| 70 | p.setShader(NULL); |
| 71 | p.setXfermode(NULL); |
| 72 | canvas->drawRect(r, p); |
| 73 | |
| 74 | canvas->restore(); |
| 75 | |
| 76 | #if 1 |
| 77 | canvas->drawText(SkXfermode::ModeName(mode), strlen(SkXfermode::ModeName(mode)), |
| 78 | x + w/2, y - labelP.getTextSize()/2, labelP); |
| 79 | #endif |
| 80 | x += w + SkIntToScalar(10); |
| 81 | if ((m % W) == W - 1) { |
| 82 | x = 0; |
| 83 | y += h + SkIntToScalar(30); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | private: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 89 | void onOnceBeforeDraw() override { |
jvanverth@google.com | 051be8e | 2013-04-19 13:59:52 +0000 | [diff] [blame] | 90 | static const uint32_t kCheckData[] = { |
caryclark | 4ec1ac6 | 2015-07-21 07:42:45 -0700 | [diff] [blame] | 91 | SkPackARGB32(0xFF, 0x42, 0x41, 0x42), |
| 92 | SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6), |
| 93 | SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6), |
| 94 | SkPackARGB32(0xFF, 0x42, 0x41, 0x42) |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 95 | }; |
| 96 | SkBitmap bg; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 97 | bg.allocN32Pixels(2, 2, true); |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 98 | memcpy(bg.getPixels(), kCheckData, sizeof(kCheckData)); |
skia.committer@gmail.com | cbbdab5 | 2013-04-18 07:01:48 +0000 | [diff] [blame] | 99 | |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 100 | SkMatrix lm; |
| 101 | lm.setScale(SkIntToScalar(16), SkIntToScalar(16)); |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 102 | fBG.reset(SkShader::CreateBitmapShader(bg, |
| 103 | SkShader::kRepeat_TileMode, |
| 104 | SkShader::kRepeat_TileMode, |
| 105 | &lm)); |
skia.committer@gmail.com | cbbdab5 | 2013-04-18 07:01:48 +0000 | [diff] [blame] | 106 | |
cdalton | d3e9191 | 2015-03-30 07:52:57 -0700 | [diff] [blame] | 107 | SkBitmap srcBmp; |
| 108 | srcBmp.allocN32Pixels(kSize, kSize); |
| 109 | SkPMColor* pixels = reinterpret_cast<SkPMColor*>(srcBmp.getPixels()); |
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 | for (int y = 0; y < kSize; ++y) { |
| 112 | int c = y * (1 << kShift); |
| 113 | SkPMColor rowColor = SkPackARGB32(c, c, 0, c/2); |
| 114 | for (int x = 0; x < kSize; ++x) { |
| 115 | pixels[kSize * y + x] = rowColor; |
| 116 | } |
| 117 | } |
cdalton | d3e9191 | 2015-03-30 07:52:57 -0700 | [diff] [blame] | 118 | fSrc.reset(SkShader::CreateBitmapShader(srcBmp, |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 119 | SkShader::kClamp_TileMode, |
| 120 | SkShader::kClamp_TileMode)); |
cdalton | d3e9191 | 2015-03-30 07:52:57 -0700 | [diff] [blame] | 121 | SkBitmap dstBmp; |
| 122 | dstBmp.allocN32Pixels(kSize, kSize); |
| 123 | pixels = reinterpret_cast<SkPMColor*>(dstBmp.getPixels()); |
skia.committer@gmail.com | cbbdab5 | 2013-04-18 07:01:48 +0000 | [diff] [blame] | 124 | |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 125 | for (int x = 0; x < kSize; ++x) { |
| 126 | int c = x * (1 << kShift); |
| 127 | SkPMColor colColor = SkPackARGB32(c, 0, c, c/2); |
| 128 | for (int y = 0; y < kSize; ++y) { |
| 129 | pixels[kSize * y + x] = colColor; |
| 130 | } |
| 131 | } |
cdalton | d3e9191 | 2015-03-30 07:52:57 -0700 | [diff] [blame] | 132 | fDst.reset(SkShader::CreateBitmapShader(dstBmp, |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 133 | SkShader::kClamp_TileMode, |
| 134 | SkShader::kClamp_TileMode)); |
| 135 | } |
skia.committer@gmail.com | cbbdab5 | 2013-04-18 07:01:48 +0000 | [diff] [blame] | 136 | |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 137 | enum { |
| 138 | kShift = 2, |
| 139 | kSize = 256 >> kShift, |
| 140 | }; |
skia.committer@gmail.com | cbbdab5 | 2013-04-18 07:01:48 +0000 | [diff] [blame] | 141 | |
bsalomon@google.com | 8c463b0 | 2013-04-17 14:19:44 +0000 | [diff] [blame] | 142 | SkAutoTUnref<SkShader> fBG; |
| 143 | SkAutoTUnref<SkShader> fSrc; |
| 144 | SkAutoTUnref<SkShader> fDst; |
| 145 | |
| 146 | typedef GM INHERITED; |
| 147 | }; |
| 148 | |
| 149 | ////////////////////////////////////////////////////////////////////////////// |
| 150 | |
| 151 | static GM* MyFactory(void*) { return new Xfermodes2GM; } |
| 152 | static GMRegistry reg(MyFactory); |
| 153 | |
| 154 | } |