blob: de64caba806aeb1748c970944a774b39692b246c [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
robertphillips29ccdf82015-07-24 10:20:45 -070010#include "SkCanvas.h"
11#include "SkRSXform.h"
12#include "SkSurface.h"
13
14// Create a square atlas of:
15// opaque white | opaque red
16// ------------------------------------
17// opaque green | transparent black
18//
reed9ce9d672016-03-17 10:51:11 -070019static sk_sp<SkImage> make_atlas(SkCanvas* caller, int atlasSize) {
robertphillips29ccdf82015-07-24 10:20:45 -070020 const int kBlockSize = atlasSize/2;
21
22 SkImageInfo info = SkImageInfo::MakeN32Premul(atlasSize, atlasSize);
reede8f30622016-03-23 18:59:25 -070023 auto surface(caller->makeSurface(info));
halcanary96fcdcc2015-08-27 07:41:13 -070024 if (nullptr == surface) {
reede8f30622016-03-23 18:59:25 -070025 surface = SkSurface::MakeRaster(info);
robertphillips29ccdf82015-07-24 10:20:45 -070026 }
27 SkCanvas* canvas = surface->getCanvas();
28
29 SkPaint paint;
reed374772b2016-10-05 17:33:02 -070030 paint.setBlendMode(SkBlendMode::kSrc);
robertphillips29ccdf82015-07-24 10:20:45 -070031
32 paint.setColor(SK_ColorWHITE);
halcanary9d524f22016-03-29 09:03:52 -070033 SkRect r = SkRect::MakeXYWH(0, 0,
robertphillips29ccdf82015-07-24 10:20:45 -070034 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
35 canvas->drawRect(r, paint);
36
37 paint.setColor(SK_ColorRED);
halcanary9d524f22016-03-29 09:03:52 -070038 r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), 0,
robertphillips29ccdf82015-07-24 10:20:45 -070039 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
40 canvas->drawRect(r, paint);
41
42 paint.setColor(SK_ColorGREEN);
halcanary9d524f22016-03-29 09:03:52 -070043 r = SkRect::MakeXYWH(0, SkIntToScalar(kBlockSize),
robertphillips29ccdf82015-07-24 10:20:45 -070044 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
45 canvas->drawRect(r, paint);
46
47 paint.setColor(SK_ColorTRANSPARENT);
halcanary9d524f22016-03-29 09:03:52 -070048 r = SkRect::MakeXYWH(SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize),
robertphillips29ccdf82015-07-24 10:20:45 -070049 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
50 canvas->drawRect(r, paint);
51
reed9ce9d672016-03-17 10:51:11 -070052 return surface->makeImageSnapshot();
robertphillips29ccdf82015-07-24 10:20:45 -070053}
54
55// This GM tests the drawAtlas API with colors, different xfer modes
56// and transparency in the atlas image
57class DrawAtlasColorsGM : public skiagm::GM {
58public:
59 DrawAtlasColorsGM() {
60 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
61 }
halcanary9d524f22016-03-29 09:03:52 -070062
robertphillips29ccdf82015-07-24 10:20:45 -070063protected:
64 SkString onShortName() override {
65 return SkString("draw-atlas-colors");
66 }
halcanary9d524f22016-03-29 09:03:52 -070067
robertphillips29ccdf82015-07-24 10:20:45 -070068 SkISize onISize() override {
69 return SkISize::Make(kNumXferModes * (kAtlasSize + kPad) + kPad,
70 2 * kNumColors * (kAtlasSize + kPad) + kTextPad + kPad);
71 }
halcanary9d524f22016-03-29 09:03:52 -070072
robertphillips29ccdf82015-07-24 10:20:45 -070073 void onDraw(SkCanvas* canvas) override {
74 const SkRect target = SkRect::MakeWH(SkIntToScalar(kAtlasSize), SkIntToScalar(kAtlasSize));
75
brianosman95e8d0a2016-09-29 13:43:49 -070076 auto atlas = make_atlas(canvas, kAtlasSize);
robertphillips29ccdf82015-07-24 10:20:45 -070077
Brian Osmand1e67e72017-03-15 12:19:37 -040078 const SkBlendMode gModes[] = {
79 SkBlendMode::kClear,
80 SkBlendMode::kSrc,
81 SkBlendMode::kDst,
82 SkBlendMode::kSrcOver,
83 SkBlendMode::kDstOver,
84 SkBlendMode::kSrcIn,
85 SkBlendMode::kDstIn,
86 SkBlendMode::kSrcOut,
87 SkBlendMode::kDstOut,
88 SkBlendMode::kSrcATop,
89 SkBlendMode::kDstATop,
90 SkBlendMode::kXor,
91 SkBlendMode::kPlus,
92 SkBlendMode::kModulate,
93 SkBlendMode::kScreen,
94 SkBlendMode::kOverlay,
95 SkBlendMode::kDarken,
96 SkBlendMode::kLighten,
97 SkBlendMode::kColorDodge,
98 SkBlendMode::kColorBurn,
99 SkBlendMode::kHardLight,
100 SkBlendMode::kSoftLight,
101 SkBlendMode::kDifference,
102 SkBlendMode::kExclusion,
103 SkBlendMode::kMultiply,
104 SkBlendMode::kHue,
105 SkBlendMode::kSaturation,
106 SkBlendMode::kColor,
107 SkBlendMode::kLuminosity,
robertphillips29ccdf82015-07-24 10:20:45 -0700108 };
109
110 SkColor gColors[] = {
111 SK_ColorWHITE,
112 SK_ColorRED,
113 0x88888888, // transparent grey
114 0x88000088 // transparent blue
115 };
116
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];
124
125 SkPaint paint;
126 paint.setAntiAlias(true);
127
128 for (int i = 0; i < numColors; ++i) {
129 xforms[i].set(1.0f, 0.0f, SkIntToScalar(kPad), i*(target.width()+kPad));
130 rects[i] = target;
131 quadColors[i] = gColors[i];
132 }
133
134 SkPaint textP;
135 textP.setTextSize(SkIntToScalar(kTextPad));
136 textP.setAntiAlias(true);
halcanary96fcdcc2015-08-27 07:41:13 -0700137 sk_tool_utils::set_portable_typeface(&textP, nullptr);
robertphillips29ccdf82015-07-24 10:20:45 -0700138
139 for (int i = 0; i < numModes; ++i) {
Brian Osmand1e67e72017-03-15 12:19:37 -0400140 const char* label = SkBlendMode_Name(gModes[i]);
141 canvas->drawText(label, strlen(label),
robertphillips29ccdf82015-07-24 10:20:45 -0700142 i*(target.width()+kPad)+kPad, SkIntToScalar(kTextPad),
143 textP);
144 }
145
146 for (int i = 0; i < numModes; ++i) {
halcanary9d524f22016-03-29 09:03:52 -0700147 canvas->save();
robertphillips29ccdf82015-07-24 10:20:45 -0700148 canvas->translate(SkIntToScalar(i*(target.height()+kPad)),
149 SkIntToScalar(kTextPad+kPad));
150 // w/o a paint
brianosman95e8d0a2016-09-29 13:43:49 -0700151 canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
Brian Osmand1e67e72017-03-15 12:19:37 -0400152 gModes[i], nullptr, nullptr);
robertphillips29ccdf82015-07-24 10:20:45 -0700153 canvas->translate(0.0f, numColors*(target.height()+kPad));
154 // w a paint
brianosman95e8d0a2016-09-29 13:43:49 -0700155 canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
Brian Osmand1e67e72017-03-15 12:19:37 -0400156 gModes[i], nullptr, &paint);
halcanary9d524f22016-03-29 09:03:52 -0700157 canvas->restore();
robertphillips29ccdf82015-07-24 10:20:45 -0700158 }
159 }
halcanary9d524f22016-03-29 09:03:52 -0700160
robertphillips29ccdf82015-07-24 10:20:45 -0700161private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700162 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;
robertphillips29ccdf82015-07-24 10:20:45 -0700167
robertphillips29ccdf82015-07-24 10:20:45 -0700168 typedef GM INHERITED;
169};
170DEF_GM( return new DrawAtlasColorsGM; )