Debian Codegraph @

gm/anisotropic.cpp

raw
1 /*
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/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkImage.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkScalar.h"
16 #include "include/core/SkShader.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkString.h"
19 #include "include/core/SkSurface.h"
20 #include "include/gpu/GpuTypes.h"
21
22 #if defined(SK_GANESH)
23 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
24 #endif
25
26 namespace skiagm {
27
28 // This GM exercises anisotropic image scaling.
29 class AnisotropicGM : public GM {
30 public:
31 enum class Mode { kLinear, kMip, kAniso };
32
33 AnisotropicGM(Mode mode) : fMode(mode) {
34 switch (fMode) {
35 case Mode::kLinear:
36 fSampling = SkSamplingOptions(SkFilterMode::kLinear);
37 break;
38 case Mode::kMip:
39 fSampling = SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear);
40 break;
41 case Mode::kAniso:
42 fSampling = SkSamplingOptions::Aniso(16);
43 break;
44 }
45 this->setBGColor(0xFFCCCCCC);
46 }
47
48 protected:
49 SkString getName() const override {
50 SkString name("anisotropic_image_scale_");
51 switch (fMode) {
52 case Mode::kLinear:
53 name += "linear";
54 break;
55 case Mode::kMip:
56 name += "mip";
57 break;
58 case Mode::kAniso:
59 name += "aniso";
60 break;
61 }
62 return name;
63 }
64
65 SkISize getISize() override {
66 return SkISize::Make(2*kImageSize + 3*kSpacer,
67 kNumVertImages*kImageSize + (kNumVertImages+1)*kSpacer);
68 }
69
70 // Create an image consisting of lines radiating from its center
71 void onOnceBeforeDraw() override {
72 constexpr int kNumLines = 100;
73 constexpr SkScalar kAngleStep = 360.0f / kNumLines;
74 constexpr int kInnerOffset = 10;
75
76 auto info = SkImageInfo::MakeN32(kImageSize, kImageSize, kOpaque_SkAlphaType);
77 auto surf = SkSurfaces::Raster(info);
78 auto canvas = surf->getCanvas();
79
80 canvas->clear(SK_ColorWHITE);
81
82 SkPaint p;
83 p.setAntiAlias(true);
84
85 SkScalar angle = 0.0f, sin, cos;
86
87 canvas->translate(kImageSize/2.0f, kImageSize/2.0f);
88 for (int i = 0; i < kNumLines; ++i, angle += kAngleStep) {
89 sin = SkScalarSin(angle);
90 cos = SkScalarCos(angle);
91 canvas->drawLine(cos * kInnerOffset, sin * kInnerOffset,
92 cos * kImageSize/2, sin * kImageSize/2, p);
93 }
94 fImage = surf->makeImageSnapshot();
95 }
96
97 void draw(SkCanvas* canvas, int x, int y, int xSize, int ySize) {
98 SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
99 SkIntToScalar(xSize), SkIntToScalar(ySize));
100 canvas->drawImageRect(fImage, r, fSampling);
101 }
102
103 void onDraw(SkCanvas* canvas) override {
104 SkScalar gScales[] = { 0.9f, 0.8f, 0.75f, 0.6f, 0.5f, 0.4f, 0.25f, 0.2f, 0.1f };
105
106 SkASSERT(kNumVertImages-1 == (int)std::size(gScales)/2);
107
108 // Minimize vertically
109 for (int i = 0; i < (int)std::size(gScales); ++i) {
110 int height = SkScalarFloorToInt(fImage->height() * gScales[i]);
111
112 int yOff;
113 if (i <= (int)std::size(gScales)/2) {
114 yOff = kSpacer + i * (fImage->height() + kSpacer);
115 } else {
116 // Position the more highly squashed images with their less squashed counterparts
117 yOff = (std::size(gScales) - i) * (fImage->height() + kSpacer) - height;
118 }
119
120 this->draw(canvas, kSpacer, yOff, fImage->width(), height);
121 }
122
123 // Minimize horizontally
124 for (int i = 0; i < (int)std::size(gScales); ++i) {
125 int width = SkScalarFloorToInt(fImage->width() * gScales[i]);
126
127 int xOff, yOff;
128 if (i <= (int)std::size(gScales)/2) {
129 xOff = fImage->width() + 2*kSpacer;
130 yOff = kSpacer + i * (fImage->height() + kSpacer);
131 } else {
132 // Position the more highly squashed images with their less squashed counterparts
133 xOff = fImage->width() + 2*kSpacer + fImage->width() - width;
134 yOff = kSpacer + (std::size(gScales) - i - 1) * (fImage->height() + kSpacer);
135 }
136
137 this->draw(canvas, xOff, yOff, width, fImage->height());
138 }
139 }
140
141 private:
142 inline static constexpr int kImageSize = 256;
143 inline static constexpr int kSpacer = 10;
144 inline static constexpr int kNumVertImages = 5;
145
146 sk_sp<SkImage> fImage;
147 SkSamplingOptions fSampling;
148 Mode fMode;
149
150 using INHERITED = GM;
151 };
152
153 //////////////////////////////////////////////////////////////////////////////
154
155 DEF_GM(return new AnisotropicGM(AnisotropicGM::Mode::kLinear);)
156 DEF_GM(return new AnisotropicGM(AnisotropicGM::Mode::kMip);)
157 DEF_GM(return new AnisotropicGM(AnisotropicGM::Mode::kAniso);)
158
159 //////////////////////////////////////////////////////////////////////////////
160
161 class AnisoMipsGM : public GM {
162 public:
163 AnisoMipsGM() = default;
164
165 protected:
166 SkString getName() const override { return SkString("anisomips"); }
167
168 SkISize getISize() override { return SkISize::Make(520, 260); }
169
170 sk_sp<SkImage> updateImage(SkSurface* surf, SkColor color) {
171 surf->getCanvas()->clear(color);
172 SkPaint paint;
173 paint.setColor(~color | 0xFF000000);
174 surf->getCanvas()->drawRect(SkRect::MakeLTRB(surf->width() *2/5.f,
175 surf->height()*2/5.f,
176 surf->width() *3/5.f,
177 surf->height()*3/5.f),
178 paint);
179 return surf->makeImageSnapshot()->withDefaultMipmaps();
180 }
181
182 void onDraw(SkCanvas* canvas) override {
183 auto ct = canvas->imageInfo().colorType() == kUnknown_SkColorType
184 ? kRGBA_8888_SkColorType
185 : canvas->imageInfo().colorType();
186 auto ii = SkImageInfo::Make(kImageSize,
187 kImageSize,
188 ct,
189 kPremul_SkAlphaType,
190 canvas->imageInfo().refColorSpace());
191 // In GPU mode we want a surface that is created with mipmaps to ensure that we exercise the
192 // case where the SkSurface and SkImage share a texture. If the surface texture isn't
193 // created with MIPs then asking for a mipmapped image will cause a copy to a mipped
194 // texture.
195 sk_sp<SkSurface> surface;
196 #if defined(SK_GANESH)
197 if (auto rc = canvas->recordingContext()) {
198 surface = SkSurfaces::RenderTarget(rc,
199 skgpu::Budgeted::kYes,
200 ii,
201 /* sampleCount= */ 1,
202 kTopLeft_GrSurfaceOrigin,
203 /*surfaceProps=*/nullptr,
204 /*shouldCreateWithMips=*/true);
205 if (!surface) {
206 // We could be in an abandoned context situation.
207 return;
208 }
209 } else
210 #endif
211 {
212 surface = canvas->makeSurface(ii);
213 if (!surface) { // could be a recording canvas.
214 surface = SkSurfaces::Raster(ii);
215 }
216 }
217
218 static constexpr float kScales[] = {1.f, 0.5f, 0.25f, 0.125f};
219 SkColor kColors[] = {0xFFF0F0F0, SK_ColorBLUE, SK_ColorGREEN, SK_ColorRED};
220 static const SkSamplingOptions kSampling = SkSamplingOptions::Aniso(16);
221
222 for (bool shader : {false, true}) {
223 int c = 0;
224 canvas->save();
225 for (float sy : kScales) {
226 canvas->save();
227 for (float sx : kScales) {
228 canvas->save();
229 canvas->scale(sx, sy);
230 auto image = this->updateImage(surface.get(), kColors[c]);
231 if (shader) {
232 SkPaint paint;
233 paint.setShader(image->makeShader(kSampling));
234 canvas->drawRect(SkRect::Make(image->dimensions()), paint);
235 } else {
236 canvas->drawImage(image, 0, 0, kSampling);
237 }
238 canvas->restore();
239 canvas->translate(ii.width() * sx + kPad, 0);
240 c = (c + 1) % std::size(kColors);
241 }
242 canvas->restore();
243 canvas->translate(0, ii.width() * sy + kPad);
244 }
245 canvas->restore();
246 for (float sx : kScales) {
247 canvas->translate(ii.width() * sx + kPad, 0);
248 }
249 }
250 }
251
252 private:
253 inline static constexpr int kImageSize = 128;
254 inline static constexpr int kPad = 5;
255
256 using INHERITED = GM;
257 };
258
259 //////////////////////////////////////////////////////////////////////////////
260
261 DEF_GM(return new AnisoMipsGM();)
262
263 } // namespace skiagm