blob: 21e6f2e146251e0dc8f05724be78e2d8408047fd [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"
Mike Reed331ccfd2018-10-25 12:36:06 -040013#include "SkTextUtils.h"
bsalomon@google.com8c463b02013-04-17 14:19:44 +000014
15namespace skiagm {
16
17class Xfermodes2GM : public GM {
18public:
19 Xfermodes2GM() {}
20
21protected:
mtklein36352bf2015-03-25 18:17:31 -070022 SkString onShortName() override {
bsalomon@google.com8c463b02013-04-17 14:19:44 +000023 return SkString("xfermodes2");
24 }
25
mtklein36352bf2015-03-25 18:17:31 -070026 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070027 return SkISize::Make(455, 475);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000028 }
29
mtklein36352bf2015-03-25 18:17:31 -070030 void onDraw(SkCanvas* canvas) override {
bsalomon@google.com8c463b02013-04-17 14:19:44 +000031 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
32
33 const SkScalar w = SkIntToScalar(kSize);
34 const SkScalar h = SkIntToScalar(kSize);
35
36 SkPaint labelP;
37 labelP.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070038 sk_tool_utils::set_portable_typeface(&labelP);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000039
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
Mike Reed331ccfd2018-10-25 12:36:06 -040075 SkTextUtils::DrawString(canvas, SkBlendMode_Name(mode),
76 x + w/2, y - labelP.getTextSize()/2, labelP,
Mike Reed3a42ec02018-10-30 12:53:21 -040077 SkTextUtils::kCenter_Align);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000078#endif
79 x += w + SkIntToScalar(10);
80 if ((m % W) == W - 1) {
81 x = 0;
82 y += h + SkIntToScalar(30);
83 }
84 }
85 }
86
87private:
mtklein36352bf2015-03-25 18:17:31 -070088 void onOnceBeforeDraw() override {
mtkleindbfd7ab2016-09-01 11:24:54 -070089 const uint32_t kCheckData[] = {
caryclark4ec1ac62015-07-21 07:42:45 -070090 SkPackARGB32(0xFF, 0x42, 0x41, 0x42),
91 SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
92 SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
93 SkPackARGB32(0xFF, 0x42, 0x41, 0x42)
bsalomon@google.com8c463b02013-04-17 14:19:44 +000094 };
95 SkBitmap bg;
reed@google.comeb9a46c2014-01-25 16:46:20 +000096 bg.allocN32Pixels(2, 2, true);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000097 memcpy(bg.getPixels(), kCheckData, sizeof(kCheckData));
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +000098
bsalomon@google.com8c463b02013-04-17 14:19:44 +000099 SkMatrix lm;
100 lm.setScale(SkIntToScalar(16), SkIntToScalar(16));
reed1a9b9642016-03-13 14:13:58 -0700101 fBG = SkShader::MakeBitmapShader(bg, SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode,
102 &lm);
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +0000103
cdaltond3e91912015-03-30 07:52:57 -0700104 SkBitmap srcBmp;
105 srcBmp.allocN32Pixels(kSize, kSize);
106 SkPMColor* pixels = reinterpret_cast<SkPMColor*>(srcBmp.getPixels());
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +0000107
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000108 for (int y = 0; y < kSize; ++y) {
109 int c = y * (1 << kShift);
110 SkPMColor rowColor = SkPackARGB32(c, c, 0, c/2);
111 for (int x = 0; x < kSize; ++x) {
112 pixels[kSize * y + x] = rowColor;
113 }
114 }
reed1a9b9642016-03-13 14:13:58 -0700115 fSrc = SkShader::MakeBitmapShader(srcBmp, SkShader::kClamp_TileMode,
116 SkShader::kClamp_TileMode);
cdaltond3e91912015-03-30 07:52:57 -0700117 SkBitmap dstBmp;
118 dstBmp.allocN32Pixels(kSize, kSize);
119 pixels = reinterpret_cast<SkPMColor*>(dstBmp.getPixels());
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +0000120
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000121 for (int x = 0; x < kSize; ++x) {
122 int c = x * (1 << kShift);
123 SkPMColor colColor = SkPackARGB32(c, 0, c, c/2);
124 for (int y = 0; y < kSize; ++y) {
125 pixels[kSize * y + x] = colColor;
126 }
127 }
reed1a9b9642016-03-13 14:13:58 -0700128 fDst = SkShader::MakeBitmapShader(dstBmp, SkShader::kClamp_TileMode,
129 SkShader::kClamp_TileMode);
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000130 }
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +0000131
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000132 enum {
133 kShift = 2,
134 kSize = 256 >> kShift,
135 };
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +0000136
reed1a9b9642016-03-13 14:13:58 -0700137 sk_sp<SkShader> fBG;
138 sk_sp<SkShader> fSrc;
139 sk_sp<SkShader> fDst;
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000140
141 typedef GM INHERITED;
142};
143
144//////////////////////////////////////////////////////////////////////////////
145
146static GM* MyFactory(void*) { return new Xfermodes2GM; }
147static GMRegistry reg(MyFactory);
148
149}