blob: 2ceb2fc65065c58c82672923b3d1866f2500fd02 [file] [log] [blame]
bsalomon@google.com8c463b02013-04-17 14:19:44 +00001
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
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);
Cary Clark992c7b02014-07-31 08:58:44 -040037 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;
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.org091a5942014-04-18 14:19:31 +000058 canvas->saveLayer(&r, NULL);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000059
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
88private:
mtklein36352bf2015-03-25 18:17:31 -070089 void onOnceBeforeDraw() override {
jvanverth@google.com051be8e2013-04-19 13:59:52 +000090 static const uint32_t kCheckData[] = {
bsalomon@google.com8c463b02013-04-17 14:19:44 +000091 SkPackARGB32(0xFF, 0x40, 0x40, 0x40),
92 SkPackARGB32(0xFF, 0xD0, 0xD0, 0xD0),
93 SkPackARGB32(0xFF, 0xD0, 0xD0, 0xD0),
94 SkPackARGB32(0xFF, 0x40, 0x40, 0x40)
95 };
96 SkBitmap bg;
reed@google.comeb9a46c2014-01-25 16:46:20 +000097 bg.allocN32Pixels(2, 2, true);
bsalomon@google.com8c463b02013-04-17 14:19:44 +000098 memcpy(bg.getPixels(), kCheckData, sizeof(kCheckData));
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +000099
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000100 SkMatrix lm;
101 lm.setScale(SkIntToScalar(16), SkIntToScalar(16));
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +0000102 fBG.reset(SkShader::CreateBitmapShader(bg,
103 SkShader::kRepeat_TileMode,
104 SkShader::kRepeat_TileMode,
105 &lm));
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +0000106
cdaltond3e91912015-03-30 07:52:57 -0700107 SkBitmap srcBmp;
108 srcBmp.allocN32Pixels(kSize, kSize);
109 SkPMColor* pixels = reinterpret_cast<SkPMColor*>(srcBmp.getPixels());
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +0000110
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000111 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 }
cdaltond3e91912015-03-30 07:52:57 -0700118 fSrc.reset(SkShader::CreateBitmapShader(srcBmp,
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000119 SkShader::kClamp_TileMode,
120 SkShader::kClamp_TileMode));
cdaltond3e91912015-03-30 07:52:57 -0700121 SkBitmap dstBmp;
122 dstBmp.allocN32Pixels(kSize, kSize);
123 pixels = reinterpret_cast<SkPMColor*>(dstBmp.getPixels());
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +0000124
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000125 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 }
cdaltond3e91912015-03-30 07:52:57 -0700132 fDst.reset(SkShader::CreateBitmapShader(dstBmp,
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000133 SkShader::kClamp_TileMode,
134 SkShader::kClamp_TileMode));
135 }
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +0000136
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000137 enum {
138 kShift = 2,
139 kSize = 256 >> kShift,
140 };
skia.committer@gmail.comcbbdab52013-04-18 07:01:48 +0000141
bsalomon@google.com8c463b02013-04-17 14:19:44 +0000142 SkAutoTUnref<SkShader> fBG;
143 SkAutoTUnref<SkShader> fSrc;
144 SkAutoTUnref<SkShader> fDst;
145
146 typedef GM INHERITED;
147};
148
149//////////////////////////////////////////////////////////////////////////////
150
151static GM* MyFactory(void*) { return new Xfermodes2GM; }
152static GMRegistry reg(MyFactory);
153
154}