• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
Xfermodes2GM()18     Xfermodes2GM() {}
19 
20 protected:
onShortName()21     SkString onShortName() override {
22         return SkString("xfermodes2");
23     }
24 
onISize()25     SkISize onISize() override {
26         return SkISize::Make(455, 475);
27     }
28 
onDraw(SkCanvas * canvas)29     void onDraw(SkCanvas* canvas) override {
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);
37         sk_tool_utils::set_portable_typeface(&labelP);
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 
58             canvas->saveLayer(&r, nullptr);
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(nullptr);
71             p.setXfermode(nullptr);
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:
onOnceBeforeDraw()89     void onOnceBeforeDraw() override {
90         static const uint32_t kCheckData[] = {
91             SkPackARGB32(0xFF, 0x42, 0x41, 0x42),
92             SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
93             SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
94             SkPackARGB32(0xFF, 0x42, 0x41, 0x42)
95         };
96         SkBitmap bg;
97         bg.allocN32Pixels(2, 2, true);
98         memcpy(bg.getPixels(), kCheckData, sizeof(kCheckData));
99 
100         SkMatrix lm;
101         lm.setScale(SkIntToScalar(16), SkIntToScalar(16));
102         fBG.reset(SkShader::CreateBitmapShader(bg,
103                                                SkShader::kRepeat_TileMode,
104                                                SkShader::kRepeat_TileMode,
105                                                &lm));
106 
107         SkBitmap srcBmp;
108         srcBmp.allocN32Pixels(kSize, kSize);
109         SkPMColor* pixels = reinterpret_cast<SkPMColor*>(srcBmp.getPixels());
110 
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         }
118         fSrc.reset(SkShader::CreateBitmapShader(srcBmp,
119                                                 SkShader::kClamp_TileMode,
120                                                 SkShader::kClamp_TileMode));
121         SkBitmap dstBmp;
122         dstBmp.allocN32Pixels(kSize, kSize);
123         pixels = reinterpret_cast<SkPMColor*>(dstBmp.getPixels());
124 
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         }
132         fDst.reset(SkShader::CreateBitmapShader(dstBmp,
133                                                 SkShader::kClamp_TileMode,
134                                                 SkShader::kClamp_TileMode));
135     }
136 
137     enum {
138         kShift = 2,
139         kSize = 256 >> kShift,
140     };
141 
142     SkAutoTUnref<SkShader> fBG;
143     SkAutoTUnref<SkShader> fSrc;
144     SkAutoTUnref<SkShader> fDst;
145 
146     typedef GM INHERITED;
147 };
148 
149 //////////////////////////////////////////////////////////////////////////////
150 
MyFactory(void *)151 static GM* MyFactory(void*) { return new Xfermodes2GM; }
152 static GMRegistry reg(MyFactory);
153 
154 }
155