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.
9 #include "sk_tool_utils.h"
11 #include "SkRSXform.h"
12 #include "SkSurface.h"
14 // Create a square atlas of:
15 // opaque white | opaque red
16 // ------------------------------------
17 // opaque green | transparent black
19 static sk_sp<SkImage> make_atlas(SkCanvas* caller, int atlasSize) {
20 const int kBlockSize = atlasSize/2;
22 SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize);
23 auto surface(caller->makeSurface(info));
24 if (nullptr == surface) {
25 surface = SkSurface::MakeRaster(info);
27 SkCanvas* canvas = surface->getCanvas();
30 paint.setBlendMode(SkBlendMode::kSrc);
32 paint.setColor(SK_ColorWHITE);
33 SkRect r = SkRect::MakeXYWH(0, 0,
34 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
35 canvas->drawRect(r, paint);
37 paint.setColor(SK_ColorRED);
38 r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), 0,
39 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
40 canvas->drawRect(r, paint);
42 paint.setColor(SK_ColorGREEN);
43 r = SkRect::MakeXYWH(0, SkIntToScalar(kBlockSize),
44 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
45 canvas->drawRect(r, paint);
47 paint.setColor(SK_ColorTRANSPARENT);
48 r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize),
49 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
50 canvas->drawRect(r, paint);
52 return surface->makeImageSnapshot();
55 // This GM tests the drawAtlas API with colors, different xfer modes
56 // and transparency in the atlas image
57 class DrawAtlasColorsGM : public skiagm::GM {
60 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
64 SkString onShortName() override {
65 return SkString("draw-atlas-colors");
68 SkISize onISize() override {
69 return SkISize::Make(kNumXferModes * (kAtlasSize + kPad) + kPad,
70 2 * kNumColors * (kAtlasSize + kPad) + kTextPad + kPad);
73 void onDraw(SkCanvas* canvas) override {
74 const SkRect target = SkRect::MakeWH(SkIntToScalar(kAtlasSize), SkIntToScalar(kAtlasSize));
76 auto atlas = make_atlas(canvas, kAtlasSize);
78 const SkBlendMode gModes[] = {
82 SkBlendMode::kSrcOver,
83 SkBlendMode::kDstOver,
88 SkBlendMode::kSrcATop,
89 SkBlendMode::kDstATop,
92 SkBlendMode::kModulate,
94 SkBlendMode::kOverlay,
96 SkBlendMode::kLighten,
97 SkBlendMode::kColorDodge,
98 SkBlendMode::kColorBurn,
99 SkBlendMode::kHardLight,
100 SkBlendMode::kSoftLight,
101 SkBlendMode::kDifference,
102 SkBlendMode::kExclusion,
103 SkBlendMode::kMultiply,
105 SkBlendMode::kSaturation,
107 SkBlendMode::kLuminosity,
110 SkColor gColors[] = {
113 0x88888888, // transparent grey
114 0x88000088 // transparent blue
117 const int numModes = SK_ARRAY_COUNT(gModes);
118 SkASSERT(numModes == kNumXferModes);
119 const int numColors = SK_ARRAY_COUNT(gColors);
120 SkASSERT(numColors == kNumColors);
121 SkRSXform xforms[numColors];
122 SkRect rects[numColors];
123 SkColor quadColors[numColors];
126 paint.setAntiAlias(true);
128 for (int i = 0; i < numColors; ++i) {
129 xforms[i].set(1.0f, 0.0f, SkIntToScalar(kPad), i*(target.width()+kPad));
131 quadColors[i] = gColors[i];
135 textP.setTextSize(SkIntToScalar(kTextPad));
136 textP.setAntiAlias(true);
137 sk_tool_utils::set_portable_typeface(&textP, nullptr);
139 for (int i = 0; i < numModes; ++i) {
140 const char* label = SkBlendMode_Name(gModes[i]);
141 canvas->drawString(label,
142 i*(target.width()+kPad)+kPad, SkIntToScalar(kTextPad),
146 for (int i = 0; i < numModes; ++i) {
148 canvas->translate(SkIntToScalar(i*(target.height()+kPad)),
149 SkIntToScalar(kTextPad+kPad));
151 canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
152 gModes[i], nullptr, nullptr);
153 canvas->translate(0.0f, numColors*(target.height()+kPad));
155 canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
156 gModes[i], nullptr, &paint);
162 static constexpr int kNumXferModes = 29;
163 static constexpr int kNumColors = 4;
164 static constexpr int kAtlasSize = 30;
165 static constexpr int kPad = 2;
166 static constexpr int kTextPad = 8;
168 typedef GM INHERITED;
170 DEF_GM( return new DrawAtlasColorsGM; )