blob: 4d6b5e7fe936b29ed3d881ea31f010442bd0b409 [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"
8#include "SkBitmap.h"
9#include "SkShader.h"
Mike Reedd4706732016-11-15 16:44:34 -050010#include "SkBlendModePriv.h"
bsalomon@google.com8c463b02013-04-17 14:19:44 +000011#include "SkColorPriv.h"
12
13namespace skiagm {
14
15class Xfermodes2GM : public GM {
16public:
17 Xfermodes2GM() {}
18
19protected:
mtklein36352bf2015-03-25 18:17:31 -070020 SkString onShortName() override {
bsalomon@google.com8c463b02013-04-17 14:19:44 +000021 return SkString("xfermodes2");
22 }
23
mtklein36352bf2015-03-25 18:17:31 -070024 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070025 return SkISize::Make(455, 475);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000026 }
27
mtklein36352bf2015-03-25 18:17:31 -070028 void onDraw(SkCanvas* canvas) override {
bsalomon@google.com8c463b02013-04-17 14:19:44 +000029 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);
caryclark1818acb2015-07-24 12:09:25 -070036 sk_tool_utils::set_portable_typeface(&labelP);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000037 labelP.setTextAlign(SkPaint::kCenter_Align);
38
39 const int W = 6;
40
41 SkScalar x = 0, y = 0;
Mike Reedd4706732016-11-15 16:44:34 -050042 for (size_t m = 0; m <= (size_t)SkBlendMode::kLastMode; m++) {
reed374772b2016-10-05 17:33:02 -070043 SkBlendMode mode = static_cast<SkBlendMode>(m);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000044
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
halcanary96fcdcc2015-08-27 07:41:13 -070055 canvas->saveLayer(&r, nullptr);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000056
57 p.setShader(fDst);
58 canvas->drawRect(r, p);
59 p.setShader(fSrc);
reed374772b2016-10-05 17:33:02 -070060 p.setBlendMode(mode);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000061 canvas->drawRect(r, p);
62
63 canvas->restore();
64
65 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
66 p.setStyle(SkPaint::kStroke_Style);
halcanary96fcdcc2015-08-27 07:41:13 -070067 p.setShader(nullptr);
reed374772b2016-10-05 17:33:02 -070068 p.setBlendMode(SkBlendMode::kSrcOver);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000069 canvas->drawRect(r, p);
70
71 canvas->restore();
72
73#if 1
Mike Reedd4706732016-11-15 16:44:34 -050074 canvas->drawText(SkBlendMode_Name(mode), strlen(SkBlendMode_Name(mode)),
bsalomon@google.com8c463b02013-04-17 14:19:44 +000075 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
85private:
mtklein36352bf2015-03-25 18:17:31 -070086 void onOnceBeforeDraw() override {
mtkleindbfd7ab2016-09-01 11:24:54 -070087 const uint32_t kCheckData[] = {
caryclark4ec1ac62015-07-21 07:42:45 -070088 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.com8c463b02013-04-17 14:19:44 +000092 };
93 SkBitmap bg;
reed@google.comeb9a46c2014-01-25 16:46:20 +000094 bg.allocN32Pixels(2, 2, true);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000095 memcpy(bg.getPixels(), kCheckData, sizeof(kCheckData));
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +000096
bsalomon@google.com8c463b02013-04-17 14:19:44 +000097 SkMatrix lm;
98 lm.setScale(SkIntToScalar(16), SkIntToScalar(16));
reed1a9b9642016-03-13 14:13:58 -070099 fBG = SkShader::MakeBitmapShader(bg, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode,
100 &lm);
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +0000101
cdaltond3e91912015-03-30 07:52:57 -0700102 SkBitmap srcBmp;
103 srcBmp.allocN32Pixels(kSize, kSize);
104 SkPMColor* pixels = reinterpret_cast<SkPMColor*>(srcBmp.getPixels());
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +0000105
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000106 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 }
reed1a9b9642016-03-13 14:13:58 -0700113 fSrc = SkShader::MakeBitmapShader(srcBmp, SkShader::kClamp_TileMode,
114 SkShader::kClamp_TileMode);
cdaltond3e91912015-03-30 07:52:57 -0700115 SkBitmap dstBmp;
116 dstBmp.allocN32Pixels(kSize, kSize);
117 pixels = reinterpret_cast<SkPMColor*>(dstBmp.getPixels());
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +0000118
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000119 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 }
reed1a9b9642016-03-13 14:13:58 -0700126 fDst = SkShader::MakeBitmapShader(dstBmp, SkShader::kClamp_TileMode,
127 SkShader::kClamp_TileMode);
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000128 }
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +0000129
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000130 enum {
131 kShift = 2,
132 kSize = 256 >> kShift,
133 };
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +0000134
reed1a9b9642016-03-13 14:13:58 -0700135 sk_sp<SkShader> fBG;
136 sk_sp<SkShader> fSrc;
137 sk_sp<SkShader> fDst;
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000138
139 typedef GM INHERITED;
140};
141
142//////////////////////////////////////////////////////////////////////////////
143
144static GM* MyFactory(void*) { return new Xfermodes2GM; }
145static GMRegistry reg(MyFactory);
146
147}