blob: 2db824d95b64689d7226ea633deb7a51bc8f859a [file] [log] [blame]
bsalomon@google.com8c463b02013-04-17 14:19:44 +00001/*
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 Klein33d20552017-03-22 13:47:51 -04008#include "sk_tool_utils.h"
bsalomon@google.com8c463b02013-04-17 14:19:44 +00009#include "SkBitmap.h"
10#include "SkShader.h"
Mike Reedd4706732016-11-15 16:44:34 -050011#include "SkBlendModePriv.h"
bsalomon@google.com8c463b02013-04-17 14:19:44 +000012#include "SkColorPriv.h"
13
14namespace skiagm {
15
16class Xfermodes2GM : public GM {
17public:
18 Xfermodes2GM() {}
19
20protected:
mtklein36352bf2015-03-25 18:17:31 -070021 SkString onShortName() override {
bsalomon@google.com8c463b02013-04-17 14:19:44 +000022 return SkString("xfermodes2");
23 }
24
mtklein36352bf2015-03-25 18:17:31 -070025 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070026 return SkISize::Make(455, 475);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000027 }
28
mtklein36352bf2015-03-25 18:17:31 -070029 void onDraw(SkCanvas* canvas) override {
bsalomon@google.com8c463b02013-04-17 14:19:44 +000030 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);
caryclark1818acb2015-07-24 12:09:25 -070037 sk_tool_utils::set_portable_typeface(&labelP);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000038 labelP.setTextAlign(SkPaint::kCenter_Align);
39
40 const int W = 6;
41
42 SkScalar x = 0, y = 0;
Mike Reedd4706732016-11-15 16:44:34 -050043 for (size_t m = 0; m <= (size_t)SkBlendMode::kLastMode; m++) {
reed374772b2016-10-05 17:33:02 -070044 SkBlendMode mode = static_cast<SkBlendMode>(m);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000045
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
halcanary96fcdcc2015-08-27 07:41:13 -070056 canvas->saveLayer(&r, nullptr);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000057
58 p.setShader(fDst);
59 canvas->drawRect(r, p);
60 p.setShader(fSrc);
reed374772b2016-10-05 17:33:02 -070061 p.setBlendMode(mode);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000062 canvas->drawRect(r, p);
63
64 canvas->restore();
65
66 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
67 p.setStyle(SkPaint::kStroke_Style);
halcanary96fcdcc2015-08-27 07:41:13 -070068 p.setShader(nullptr);
reed374772b2016-10-05 17:33:02 -070069 p.setBlendMode(SkBlendMode::kSrcOver);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000070 canvas->drawRect(r, p);
71
72 canvas->restore();
73
74#if 1
Cary Clark2a475ea2017-04-28 15:35:12 -040075 canvas->drawString(SkBlendMode_Name(mode),
76 x + w/2, y - labelP.getTextSize()/2, labelP);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000077#endif
78 x += w + SkIntToScalar(10);
79 if ((m % W) == W - 1) {
80 x = 0;
81 y += h + SkIntToScalar(30);
82 }
83 }
84 }
85
86private:
mtklein36352bf2015-03-25 18:17:31 -070087 void onOnceBeforeDraw() override {
mtkleindbfd7ab2016-09-01 11:24:54 -070088 const uint32_t kCheckData[] = {
caryclark4ec1ac62015-07-21 07:42:45 -070089 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.com8c463b02013-04-17 14:19:44 +000093 };
94 SkBitmap bg;
reed@google.comeb9a46c2014-01-25 16:46:20 +000095 bg.allocN32Pixels(2, 2, true);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000096 memcpy(bg.getPixels(), kCheckData, sizeof(kCheckData));
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +000097
bsalomon@google.com8c463b02013-04-17 14:19:44 +000098 SkMatrix lm;
99 lm.setScale(SkIntToScalar(16), SkIntToScalar(16));
reed1a9b9642016-03-13 14:13:58 -0700100 fBG = SkShader::MakeBitmapShader(bg, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode,
101 &lm);
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +0000102
cdaltond3e91912015-03-30 07:52:57 -0700103 SkBitmap srcBmp;
104 srcBmp.allocN32Pixels(kSize, kSize);
105 SkPMColor* pixels = reinterpret_cast<SkPMColor*>(srcBmp.getPixels());
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +0000106
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000107 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 }
reed1a9b9642016-03-13 14:13:58 -0700114 fSrc = SkShader::MakeBitmapShader(srcBmp, SkShader::kClamp_TileMode,
115 SkShader::kClamp_TileMode);
cdaltond3e91912015-03-30 07:52:57 -0700116 SkBitmap dstBmp;
117 dstBmp.allocN32Pixels(kSize, kSize);
118 pixels = reinterpret_cast<SkPMColor*>(dstBmp.getPixels());
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +0000119
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000120 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 }
reed1a9b9642016-03-13 14:13:58 -0700127 fDst = SkShader::MakeBitmapShader(dstBmp, SkShader::kClamp_TileMode,
128 SkShader::kClamp_TileMode);
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000129 }
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +0000130
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000131 enum {
132 kShift = 2,
133 kSize = 256 >> kShift,
134 };
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +0000135
reed1a9b9642016-03-13 14:13:58 -0700136 sk_sp<SkShader> fBG;
137 sk_sp<SkShader> fSrc;
138 sk_sp<SkShader> fDst;
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000139
140 typedef GM INHERITED;
141};
142
143//////////////////////////////////////////////////////////////////////////////
144
145static GM* MyFactory(void*) { return new Xfermodes2GM; }
146static GMRegistry reg(MyFactory);
147
148}