3 * Copyright 2013 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
11 #include "SkXfermode.h"
12 #include "SkColorPriv.h"
16 class Xfermodes2GM : public GM {
21 SkString onShortName() override {
22 return SkString("xfermodes2");
25 SkISize onISize() override {
26 return SkISize::Make(455, 475);
29 void onDraw(SkCanvas* canvas) override {
30 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
32 const SkScalar w = SkIntToScalar(kSize);
33 const SkScalar h = SkIntToScalar(kSize);
36 labelP.setAntiAlias(true);
37 sk_tool_utils::set_portable_typeface(&labelP);
38 labelP.setTextAlign(SkPaint::kCenter_Align);
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);
50 canvas->translate(x, y);
52 p.setAntiAlias(false);
53 p.setStyle(SkPaint::kFill_Style);
55 SkRect r = SkRect::MakeWH(w, h);
56 canvas->drawRect(r, p);
58 canvas->saveLayer(&r, NULL);
61 canvas->drawRect(r, p);
64 canvas->drawRect(r, p);
68 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
69 p.setStyle(SkPaint::kStroke_Style);
72 canvas->drawRect(r, p);
77 canvas->drawText(SkXfermode::ModeName(mode), strlen(SkXfermode::ModeName(mode)),
78 x + w/2, y - labelP.getTextSize()/2, labelP);
80 x += w + SkIntToScalar(10);
81 if ((m % W) == W - 1) {
83 y += h + SkIntToScalar(30);
89 void onOnceBeforeDraw() override {
90 static const uint32_t kCheckData[] = {
91 SkPackARGB32(0xFF, 0x40, 0x40, 0x40),
92 SkPackARGB32(0xFF, 0xD0, 0xD0, 0xD0),
93 SkPackARGB32(0xFF, 0xD0, 0xD0, 0xD0),
94 SkPackARGB32(0xFF, 0x40, 0x40, 0x40)
97 bg.allocN32Pixels(2, 2, true);
98 memcpy(bg.getPixels(), kCheckData, sizeof(kCheckData));
101 lm.setScale(SkIntToScalar(16), SkIntToScalar(16));
102 fBG.reset(SkShader::CreateBitmapShader(bg,
103 SkShader::kRepeat_TileMode,
104 SkShader::kRepeat_TileMode,
108 dstBmp.allocN32Pixels(kSize, kSize);
109 SkPMColor* pixels = reinterpret_cast<SkPMColor*>(dstBmp.getPixels());
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;
118 fSrc.reset(SkShader::CreateBitmapShader(dstBmp,
119 SkShader::kClamp_TileMode,
120 SkShader::kClamp_TileMode));
122 srcBmp.allocN32Pixels(kSize, kSize);
123 pixels = reinterpret_cast<SkPMColor*>(srcBmp.getPixels());
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;
132 fDst.reset(SkShader::CreateBitmapShader(srcBmp,
133 SkShader::kClamp_TileMode,
134 SkShader::kClamp_TileMode));
139 kSize = 256 >> kShift,
142 SkAutoTUnref<SkShader> fBG;
143 SkAutoTUnref<SkShader> fSrc;
144 SkAutoTUnref<SkShader> fDst;
146 typedef GM INHERITED;
149 //////////////////////////////////////////////////////////////////////////////
151 static GM* MyFactory(void*) { return new Xfermodes2GM; }
152 static GMRegistry reg(MyFactory);