2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkColorPriv.h"
14 #include "include/core/SkFont.h"
15 #include "include/core/SkMatrix.h"
16 #include "include/core/SkPaint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkShader.h"
21 #include "include/core/SkSize.h"
22 #include "include/core/SkString.h"
23 #include "include/core/SkTileMode.h"
24 #include "include/core/SkTypeface.h"
25 #include "include/utils/SkTextUtils.h"
26 #include "tools/ToolUtils.h"
32 class Xfermodes2GM : public GM {
37 SkString onShortName() override {
38 return SkString("xfermodes2");
41 SkISize onISize() override {
42 return SkISize::Make(455, 475);
45 void onDraw(SkCanvas* canvas) override {
46 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
48 const SkScalar w = SkIntToScalar(kSize);
49 const SkScalar h = SkIntToScalar(kSize);
51 SkFont font(ToolUtils::create_portable_typeface());
55 SkScalar x = 0, y = 0;
56 for (size_t m = 0; m < kSkBlendModeCount; m++) {
57 SkBlendMode mode = static_cast<SkBlendMode>(m);
61 canvas->translate(x, y);
63 p.setAntiAlias(false);
64 p.setStyle(SkPaint::kFill_Style);
66 SkRect r = SkRect::MakeWH(w, h);
67 canvas->drawRect(r, p);
69 canvas->saveLayer(&r, nullptr);
72 canvas->drawRect(r, p);
75 canvas->drawRect(r, p);
79 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
80 p.setStyle(SkPaint::kStroke_Style);
82 p.setBlendMode(SkBlendMode::kSrcOver);
83 canvas->drawRect(r, p);
88 SkTextUtils::DrawString(canvas, SkBlendMode_Name(mode), x + w/2, y - font.getSize()/2, font, SkPaint(),
89 SkTextUtils::kCenter_Align);
91 x += w + SkIntToScalar(10);
92 if ((m % W) == W - 1) {
94 y += h + SkIntToScalar(30);
100 void onOnceBeforeDraw() override {
101 const uint32_t kCheckData[] = {
102 SkPackARGB32(0xFF, 0x42, 0x41, 0x42),
103 SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
104 SkPackARGB32(0xFF, 0xD6, 0xD3, 0xD6),
105 SkPackARGB32(0xFF, 0x42, 0x41, 0x42)
108 bg.allocN32Pixels(2, 2, true);
109 memcpy(bg.getPixels(), kCheckData, sizeof(kCheckData));
112 lm.setScale(SkIntToScalar(16), SkIntToScalar(16));
113 fBG = bg.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions(), lm);
116 srcBmp.allocN32Pixels(kSize, kSize);
117 SkPMColor* pixels = reinterpret_cast<SkPMColor*>(srcBmp.getPixels());
119 for (int y = 0; y < kSize; ++y) {
120 int c = y * (1 << kShift);
121 SkPMColor rowColor = SkPackARGB32(c, c, 0, c/2);
122 for (int x = 0; x < kSize; ++x) {
123 pixels[kSize * y + x] = rowColor;
126 fSrc = srcBmp.makeShader(SkSamplingOptions());
128 dstBmp.allocN32Pixels(kSize, kSize);
129 pixels = reinterpret_cast<SkPMColor*>(dstBmp.getPixels());
131 for (int x = 0; x < kSize; ++x) {
132 int c = x * (1 << kShift);
133 SkPMColor colColor = SkPackARGB32(c, 0, c, c/2);
134 for (int y = 0; y < kSize; ++y) {
135 pixels[kSize * y + x] = colColor;
138 fDst = dstBmp.makeShader(SkSamplingOptions());
143 kSize = 256 >> kShift,
147 sk_sp<SkShader> fSrc;
148 sk_sp<SkShader> fDst;
150 using INHERITED = GM;
153 //////////////////////////////////////////////////////////////////////////////
155 DEF_GM( return new Xfermodes2GM; )
157 } // namespace skiagm