blob: 990bdcb693065914ed6960b6d0614447e2cd92ed [file] [log] [blame]
robertphillips29ccdf82015-07-24 10:20:45 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm.h"
9#include "SkCanvas.h"
10#include "SkRSXform.h"
11#include "SkSurface.h"
12
13// Create a square atlas of:
14// opaque white | opaque red
15// ------------------------------------
16// opaque green | transparent black
17//
reed9ce9d672016-03-17 10:51:11 -070018static sk_sp<SkImage> make_atlas(SkCanvas* caller, int atlasSize) {
robertphillips29ccdf82015-07-24 10:20:45 -070019 const int kBlockSize = atlasSize/2;
20
21 SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize);
reede8f30622016-03-23 18:59:25 -070022 auto surface(caller->makeSurface(info));
halcanary96fcdcc2015-08-27 07:41:13 -070023 if (nullptr == surface) {
reede8f30622016-03-23 18:59:25 -070024 surface = SkSurface::MakeRaster(info);
robertphillips29ccdf82015-07-24 10:20:45 -070025 }
26 SkCanvas* canvas = surface->getCanvas();
27
28 SkPaint paint;
reed374772b2016-10-05 17:33:02 -070029 paint.setBlendMode(SkBlendMode::kSrc);
robertphillips29ccdf82015-07-24 10:20:45 -070030
31 paint.setColor(SK_ColorWHITE);
halcanary9d524f22016-03-29 09:03:52 -070032 SkRect r = SkRect::MakeXYWH(0, 0,
robertphillips29ccdf82015-07-24 10:20:45 -070033 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
34 canvas->drawRect(r, paint);
35
36 paint.setColor(SK_ColorRED);
halcanary9d524f22016-03-29 09:03:52 -070037 r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), 0,
robertphillips29ccdf82015-07-24 10:20:45 -070038 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
39 canvas->drawRect(r, paint);
40
41 paint.setColor(SK_ColorGREEN);
halcanary9d524f22016-03-29 09:03:52 -070042 r = SkRect::MakeXYWH(0, SkIntToScalar(kBlockSize),
robertphillips29ccdf82015-07-24 10:20:45 -070043 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
44 canvas->drawRect(r, paint);
45
46 paint.setColor(SK_ColorTRANSPARENT);
halcanary9d524f22016-03-29 09:03:52 -070047 r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize),
robertphillips29ccdf82015-07-24 10:20:45 -070048 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
49 canvas->drawRect(r, paint);
50
reed9ce9d672016-03-17 10:51:11 -070051 return surface->makeImageSnapshot();
robertphillips29ccdf82015-07-24 10:20:45 -070052}
53
54// This GM tests the drawAtlas API with colors, different xfer modes
55// and transparency in the atlas image
56class DrawAtlasColorsGM : public skiagm::GM {
57public:
58 DrawAtlasColorsGM() {
59 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
60 }
halcanary9d524f22016-03-29 09:03:52 -070061
robertphillips29ccdf82015-07-24 10:20:45 -070062protected:
63 SkString onShortName() override {
64 return SkString("draw-atlas-colors");
65 }
halcanary9d524f22016-03-29 09:03:52 -070066
robertphillips29ccdf82015-07-24 10:20:45 -070067 SkISize onISize() override {
68 return SkISize::Make(kNumXferModes * (kAtlasSize + kPad) + kPad,
69 2 * kNumColors * (kAtlasSize + kPad) + kTextPad + kPad);
70 }
halcanary9d524f22016-03-29 09:03:52 -070071
robertphillips29ccdf82015-07-24 10:20:45 -070072 void onDraw(SkCanvas* canvas) override {
73 const SkRect target = SkRect::MakeWH(SkIntToScalar(kAtlasSize), SkIntToScalar(kAtlasSize));
74
brianosman95e8d0a2016-09-29 13:43:49 -070075 auto atlas = make_atlas(canvas, kAtlasSize);
robertphillips29ccdf82015-07-24 10:20:45 -070076
77 const struct {
Mike Reed7d954ad2016-10-28 15:42:34 -040078 SkBlendMode fMode;
79 const char* fLabel;
robertphillips29ccdf82015-07-24 10:20:45 -070080 } gModes[] = {
Mike Reed7d954ad2016-10-28 15:42:34 -040081 { 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"},
robertphillips29ccdf82015-07-24 10:20:45 -0700110 };
111
112 SkColor gColors[] = {
113 SK_ColorWHITE,
114 SK_ColorRED,
115 0x88888888, // transparent grey
116 0x88000088 // transparent blue
117 };
118
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];
126
127 SkPaint paint;
128 paint.setAntiAlias(true);
129
130 for (int i = 0; i < numColors; ++i) {
131 xforms[i].set(1.0f, 0.0f, SkIntToScalar(kPad), i*(target.width()+kPad));
132 rects[i] = target;
133 quadColors[i] = gColors[i];
134 }
135
136 SkPaint textP;
137 textP.setTextSize(SkIntToScalar(kTextPad));
138 textP.setAntiAlias(true);
halcanary96fcdcc2015-08-27 07:41:13 -0700139 sk_tool_utils::set_portable_typeface(&textP, nullptr);
robertphillips29ccdf82015-07-24 10:20:45 -0700140
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),
144 textP);
145 }
146
147 for (int i = 0; i < numModes; ++i) {
halcanary9d524f22016-03-29 09:03:52 -0700148 canvas->save();
robertphillips29ccdf82015-07-24 10:20:45 -0700149 canvas->translate(SkIntToScalar(i*(target.height()+kPad)),
150 SkIntToScalar(kTextPad+kPad));
151 // w/o a paint
brianosman95e8d0a2016-09-29 13:43:49 -0700152 canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
halcanary96fcdcc2015-08-27 07:41:13 -0700153 gModes[i].fMode, nullptr, nullptr);
robertphillips29ccdf82015-07-24 10:20:45 -0700154 canvas->translate(0.0f, numColors*(target.height()+kPad));
155 // w a paint
brianosman95e8d0a2016-09-29 13:43:49 -0700156 canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
halcanary96fcdcc2015-08-27 07:41:13 -0700157 gModes[i].fMode, nullptr, &paint);
halcanary9d524f22016-03-29 09:03:52 -0700158 canvas->restore();
robertphillips29ccdf82015-07-24 10:20:45 -0700159 }
160 }
halcanary9d524f22016-03-29 09:03:52 -0700161
robertphillips29ccdf82015-07-24 10:20:45 -0700162private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700163 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;
robertphillips29ccdf82015-07-24 10:20:45 -0700168
robertphillips29ccdf82015-07-24 10:20:45 -0700169 typedef GM INHERITED;
170};
171DEF_GM( return new DrawAtlasColorsGM; )