2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "SkRSXform.h"
11 #include "SkSurface.h"
13 // Create a square atlas of:
14 // opaque white | opaque red
15 // ------------------------------------
16 // opaque green | transparent black
18 static sk_sp<SkImage> make_atlas(SkCanvas* caller, int atlasSize) {
19 const int kBlockSize = atlasSize/2;
21 SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize);
22 auto surface(caller->makeSurface(info));
23 if (nullptr == surface) {
24 surface = SkSurface::MakeRaster(info);
26 SkCanvas* canvas = surface->getCanvas();
29 paint.setBlendMode(SkBlendMode::kSrc);
31 paint.setColor(SK_ColorWHITE);
32 SkRect r = SkRect::MakeXYWH(0, 0,
33 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
34 canvas->drawRect(r, paint);
36 paint.setColor(SK_ColorRED);
37 r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), 0,
38 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
39 canvas->drawRect(r, paint);
41 paint.setColor(SK_ColorGREEN);
42 r = SkRect::MakeXYWH(0, SkIntToScalar(kBlockSize),
43 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
44 canvas->drawRect(r, paint);
46 paint.setColor(SK_ColorTRANSPARENT);
47 r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize),
48 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
49 canvas->drawRect(r, paint);
51 return surface->makeImageSnapshot();
54 // This GM tests the drawAtlas API with colors, different xfer modes
55 // and transparency in the atlas image
56 class DrawAtlasColorsGM : public skiagm::GM {
59 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
63 SkString onShortName() override {
64 return SkString("draw-atlas-colors");
67 SkISize onISize() override {
68 return SkISize::Make(kNumXferModes * (kAtlasSize + kPad) + kPad,
69 2 * kNumColors * (kAtlasSize + kPad) + kTextPad + kPad);
72 void onDraw(SkCanvas* canvas) override {
73 const SkRect target = SkRect::MakeWH(SkIntToScalar(kAtlasSize), SkIntToScalar(kAtlasSize));
75 auto atlas = make_atlas(canvas, kAtlasSize);
81 { SkBlendMode::kClear, "Clear" },
82 { SkBlendMode::kSrc, "Src" },
83 { SkBlendMode::kDst, "Dst" },
84 { SkBlendMode::kSrcOver, "SrcOver" },
85 { SkBlendMode::kDstOver, "DstOver" },
86 { SkBlendMode::kSrcIn, "SrcIn" },
87 { SkBlendMode::kDstIn, "DstIn" },
88 { SkBlendMode::kSrcOut, "SrcOut" },
89 { SkBlendMode::kDstOut, "DstOut" },
90 { SkBlendMode::kSrcATop, "SrcATop" },
91 { SkBlendMode::kDstATop, "DstATop" },
92 { SkBlendMode::kXor, "Xor" },
93 { SkBlendMode::kPlus, "Plus" },
94 { SkBlendMode::kModulate, "Mod" },
95 { SkBlendMode::kScreen, "Screen" },
96 { SkBlendMode::kOverlay, "Overlay" },
97 { SkBlendMode::kDarken, "Darken" },
98 { SkBlendMode::kLighten, "Lighten" },
99 { SkBlendMode::kColorDodge, "Dodge" },
100 { SkBlendMode::kColorBurn, "Burn" },
101 { SkBlendMode::kHardLight, "Hard" },
102 { SkBlendMode::kSoftLight, "Soft" },
103 { SkBlendMode::kDifference, "Diff" },
104 { SkBlendMode::kExclusion, "Exclusion" },
105 { SkBlendMode::kMultiply, "Multiply" },
106 { SkBlendMode::kHue, "Hue" },
107 { SkBlendMode::kSaturation, "Sat" },
108 { SkBlendMode::kColor, "Color" },
109 { SkBlendMode::kLuminosity, "Luminosity"},
112 SkColor gColors[] = {
115 0x88888888, // transparent grey
116 0x88000088 // transparent blue
119 const int numModes = SK_ARRAY_COUNT(gModes);
120 SkASSERT(numModes == kNumXferModes);
121 const int numColors = SK_ARRAY_COUNT(gColors);
122 SkASSERT(numColors == kNumColors);
123 SkRSXform xforms[numColors];
124 SkRect rects[numColors];
125 SkColor quadColors[numColors];
128 paint.setAntiAlias(true);
130 for (int i = 0; i < numColors; ++i) {
131 xforms[i].set(1.0f, 0.0f, SkIntToScalar(kPad), i*(target.width()+kPad));
133 quadColors[i] = gColors[i];
137 textP.setTextSize(SkIntToScalar(kTextPad));
138 textP.setAntiAlias(true);
139 sk_tool_utils::set_portable_typeface(&textP, nullptr);
141 for (int i = 0; i < numModes; ++i) {
142 canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel),
143 i*(target.width()+kPad)+kPad, SkIntToScalar(kTextPad),
147 for (int i = 0; i < numModes; ++i) {
149 canvas->translate(SkIntToScalar(i*(target.height()+kPad)),
150 SkIntToScalar(kTextPad+kPad));
152 canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
153 gModes[i].fMode, nullptr, nullptr);
154 canvas->translate(0.0f, numColors*(target.height()+kPad));
156 canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
157 gModes[i].fMode, nullptr, &paint);
163 static constexpr int kNumXferModes = 29;
164 static constexpr int kNumColors = 4;
165 static constexpr int kAtlasSize = 30;
166 static constexpr int kPad = 2;
167 static constexpr int kTextPad = 8;
169 typedef GM INHERITED;
171 DEF_GM( return new DrawAtlasColorsGM; )