blob: e67ffc3031fa34096ddb215af5c84ef94b5b4ebe [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
Brian Osmand1e67e72017-03-15 12:19:37 -040077 const SkBlendMode gModes[] = {
78 SkBlendMode::kClear,
79 SkBlendMode::kSrc,
80 SkBlendMode::kDst,
81 SkBlendMode::kSrcOver,
82 SkBlendMode::kDstOver,
83 SkBlendMode::kSrcIn,
84 SkBlendMode::kDstIn,
85 SkBlendMode::kSrcOut,
86 SkBlendMode::kDstOut,
87 SkBlendMode::kSrcATop,
88 SkBlendMode::kDstATop,
89 SkBlendMode::kXor,
90 SkBlendMode::kPlus,
91 SkBlendMode::kModulate,
92 SkBlendMode::kScreen,
93 SkBlendMode::kOverlay,
94 SkBlendMode::kDarken,
95 SkBlendMode::kLighten,
96 SkBlendMode::kColorDodge,
97 SkBlendMode::kColorBurn,
98 SkBlendMode::kHardLight,
99 SkBlendMode::kSoftLight,
100 SkBlendMode::kDifference,
101 SkBlendMode::kExclusion,
102 SkBlendMode::kMultiply,
103 SkBlendMode::kHue,
104 SkBlendMode::kSaturation,
105 SkBlendMode::kColor,
106 SkBlendMode::kLuminosity,
robertphillips29ccdf82015-07-24 10:20:45 -0700107 };
108
109 SkColor gColors[] = {
110 SK_ColorWHITE,
111 SK_ColorRED,
112 0x88888888, // transparent grey
113 0x88000088 // transparent blue
114 };
115
116 const int numModes = SK_ARRAY_COUNT(gModes);
117 SkASSERT(numModes == kNumXferModes);
118 const int numColors = SK_ARRAY_COUNT(gColors);
119 SkASSERT(numColors == kNumColors);
120 SkRSXform xforms[numColors];
121 SkRect rects[numColors];
122 SkColor quadColors[numColors];
123
124 SkPaint paint;
125 paint.setAntiAlias(true);
126
127 for (int i = 0; i < numColors; ++i) {
128 xforms[i].set(1.0f, 0.0f, SkIntToScalar(kPad), i*(target.width()+kPad));
129 rects[i] = target;
130 quadColors[i] = gColors[i];
131 }
132
133 SkPaint textP;
134 textP.setTextSize(SkIntToScalar(kTextPad));
135 textP.setAntiAlias(true);
halcanary96fcdcc2015-08-27 07:41:13 -0700136 sk_tool_utils::set_portable_typeface(&textP, nullptr);
robertphillips29ccdf82015-07-24 10:20:45 -0700137
138 for (int i = 0; i < numModes; ++i) {
Brian Osmand1e67e72017-03-15 12:19:37 -0400139 const char* label = SkBlendMode_Name(gModes[i]);
140 canvas->drawText(label, strlen(label),
robertphillips29ccdf82015-07-24 10:20:45 -0700141 i*(target.width()+kPad)+kPad, SkIntToScalar(kTextPad),
142 textP);
143 }
144
145 for (int i = 0; i < numModes; ++i) {
halcanary9d524f22016-03-29 09:03:52 -0700146 canvas->save();
robertphillips29ccdf82015-07-24 10:20:45 -0700147 canvas->translate(SkIntToScalar(i*(target.height()+kPad)),
148 SkIntToScalar(kTextPad+kPad));
149 // w/o a paint
brianosman95e8d0a2016-09-29 13:43:49 -0700150 canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
Brian Osmand1e67e72017-03-15 12:19:37 -0400151 gModes[i], nullptr, nullptr);
robertphillips29ccdf82015-07-24 10:20:45 -0700152 canvas->translate(0.0f, numColors*(target.height()+kPad));
153 // w a paint
brianosman95e8d0a2016-09-29 13:43:49 -0700154 canvas->drawAtlas(atlas.get(), xforms, rects, quadColors, numColors,
Brian Osmand1e67e72017-03-15 12:19:37 -0400155 gModes[i], nullptr, &paint);
halcanary9d524f22016-03-29 09:03:52 -0700156 canvas->restore();
robertphillips29ccdf82015-07-24 10:20:45 -0700157 }
158 }
halcanary9d524f22016-03-29 09:03:52 -0700159
robertphillips29ccdf82015-07-24 10:20:45 -0700160private:
mtkleindbfd7ab2016-09-01 11:24:54 -0700161 static constexpr int kNumXferModes = 29;
162 static constexpr int kNumColors = 4;
163 static constexpr int kAtlasSize = 30;
164 static constexpr int kPad = 2;
165 static constexpr int kTextPad = 8;
robertphillips29ccdf82015-07-24 10:20:45 -0700166
robertphillips29ccdf82015-07-24 10:20:45 -0700167 typedef GM INHERITED;
168};
169DEF_GM( return new DrawAtlasColorsGM; )