Debian Codegraph @

gm/bleed.cpp

raw
1 /*
2 * Copyright 2013 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/SkBitmap.h"
10 #include "include/core/SkBlurTypes.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageInfo.h"
15 #include "include/core/SkMaskFilter.h"
16 #include "include/core/SkMatrix.h"
17 #include "include/core/SkPaint.h"
18 #include "include/core/SkPoint.h"
19 #include "include/core/SkRect.h"
20 #include "include/core/SkRefCnt.h"
21 #include "include/core/SkScalar.h"
22 #include "include/core/SkShader.h"
23 #include "include/core/SkSize.h"
24 #include "include/core/SkString.h"
25 #include "include/core/SkSurface.h"
26 #include "include/core/SkTileMode.h"
27 #include "include/core/SkTiledImageUtils.h"
28 #include "include/core/SkTypes.h"
29 #include "include/private/base/SkTDArray.h"
30 #include "src/core/SkBlurMask.h"
31 #include "tools/ToolUtils.h"
32
33 #if defined(SK_GANESH)
34 #include "include/gpu/ganesh/GrContextOptions.h"
35 #endif
36
37 #if defined(SK_GRAPHITE)
38 #include "include/gpu/graphite/ContextOptions.h"
39 #include "src/gpu/graphite/ContextOptionsPriv.h"
40 #endif
41
42 /** Creates an image with two one-pixel wide borders around a checkerboard. The checkerboard is 2x2
43 checks where each check has as many pixels as is necessary to fill the interior. It returns
44 the image and a src rect that bounds the checkerboard portion. */
45 std::tuple<sk_sp<SkImage>, SkRect> make_ringed_image(SkCanvas* canvas, int width, int height) {
46
47 // These are kRGBA_8888_SkColorType values.
48 static constexpr uint32_t kOuterRingColor = 0xFFFF0000,
49 kInnerRingColor = 0xFF0000FF,
50 kCheckColor1 = 0xFF000000,
51 kCheckColor2 = 0xFFFFFFFF;
52
53 SkASSERT(0 == width % 2 && 0 == height % 2);
54 SkASSERT(width >= 6 && height >= 6);
55
56 SkImageInfo info = SkImageInfo::Make(width, height, kRGBA_8888_SkColorType,
57 kPremul_SkAlphaType);
58 size_t rowBytes = SkAlign4(info.minRowBytes());
59 SkBitmap bitmap;
60 bitmap.allocPixels(info, rowBytes);
61
62 uint32_t* scanline = bitmap.getAddr32(0, 0);
63 for (int x = 0; x < width; ++x) {
64 scanline[x] = kOuterRingColor;
65 }
66 scanline = bitmap.getAddr32(0, 1);
67 scanline[0] = kOuterRingColor;
68 for (int x = 1; x < width - 1; ++x) {
69 scanline[x] = kInnerRingColor;
70 }
71 scanline[width - 1] = kOuterRingColor;
72
73 for (int y = 2; y < height / 2; ++y) {
74 scanline = bitmap.getAddr32(0, y);
75 scanline[0] = kOuterRingColor;
76 scanline[1] = kInnerRingColor;
77 for (int x = 2; x < width / 2; ++x) {
78 scanline[x] = kCheckColor1;
79 }
80 for (int x = width / 2; x < width - 2; ++x) {
81 scanline[x] = kCheckColor2;
82 }
83 scanline[width - 2] = kInnerRingColor;
84 scanline[width - 1] = kOuterRingColor;
85 }
86
87 for (int y = height / 2; y < height - 2; ++y) {
88 scanline = bitmap.getAddr32(0, y);
89 scanline[0] = kOuterRingColor;
90 scanline[1] = kInnerRingColor;
91 for (int x = 2; x < width / 2; ++x) {
92 scanline[x] = kCheckColor2;
93 }
94 for (int x = width / 2; x < width - 2; ++x) {
95 scanline[x] = kCheckColor1;
96 }
97 scanline[width - 2] = kInnerRingColor;
98 scanline[width - 1] = kOuterRingColor;
99 }
100
101 scanline = bitmap.getAddr32(0, height - 2);
102 scanline[0] = kOuterRingColor;
103 for (int x = 1; x < width - 1; ++x) {
104 scanline[x] = kInnerRingColor;
105 }
106 scanline[width - 1] = kOuterRingColor;
107
108 scanline = bitmap.getAddr32(0, height - 1);
109 for (int x = 0; x < width; ++x) {
110 scanline[x] = kOuterRingColor;
111 }
112 bitmap.setImmutable();
113 return { bitmap.asImage(), SkRect::Make({2, 2, width - 2, height - 2})};
114 }
115
116 /**
117 * These GMs exercise the behavior of the drawImageRect and its SrcRectConstraint parameter. They
118 * tests various matrices, filter qualities, and interaction with mask filters. They also exercise
119 * the tiling image draws of SkGpuDevice by overriding the maximum texture size of the GrContext.
120 */
121 class SrcRectConstraintGM : public skiagm::GM {
122 public:
123 SrcRectConstraintGM(const char* shortName, SkCanvas::SrcRectConstraint constraint, bool manual)
124 : fShortName(shortName)
125 , fConstraint(constraint)
126 , fManual(manual) {
127 // Make sure GPU SkSurfaces can be created for this GM.
128 SkASSERT(this->getISize().width() <= kMaxTextureSize &&
129 this->getISize().height() <= kMaxTextureSize);
130 }
131
132 protected:
133 SkString getName() const override { return fShortName; }
134 SkISize getISize() override { return SkISize::Make(800, 1000); }
135
136 void drawImage(SkCanvas* canvas, sk_sp<SkImage> image, SkRect srcRect, SkRect dstRect,
137 const SkSamplingOptions& sampling, SkPaint* paint) {
138 if (fManual) {
139 SkTiledImageUtils::DrawImageRect(canvas, image.get(), srcRect, dstRect,
140 sampling, paint, fConstraint);
141 } else {
142 canvas->drawImageRect(image.get(), srcRect, dstRect, sampling, paint, fConstraint);
143 }
144 }
145
146 // Draw the area of interest of the small image
147 void drawCase1(SkCanvas* canvas, int transX, int transY, bool aa,
148 const SkSamplingOptions& sampling) {
149 SkRect dst = SkRect::MakeXYWH(SkIntToScalar(transX), SkIntToScalar(transY),
150 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
151
152 SkPaint paint;
153 paint.setColor(SK_ColorBLUE);
154 paint.setAntiAlias(aa);
155
156 this->drawImage(canvas, fSmallImage, fSmallSrcRect, dst, sampling, &paint);
157 }
158
159 // Draw the area of interest of the large image
160 void drawCase2(SkCanvas* canvas, int transX, int transY, bool aa,
161 const SkSamplingOptions& sampling) {
162 SkRect dst = SkRect::MakeXYWH(SkIntToScalar(transX), SkIntToScalar(transY),
163 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
164
165 SkPaint paint;
166 paint.setColor(SK_ColorBLUE);
167 paint.setAntiAlias(aa);
168
169 this->drawImage(canvas, fBigImage, fBigSrcRect, dst, sampling, &paint);
170 }
171
172 // Draw upper-left 1/4 of the area of interest of the large image
173 void drawCase3(SkCanvas* canvas, int transX, int transY, bool aa,
174 const SkSamplingOptions& sampling) {
175 SkRect src = SkRect::MakeXYWH(fBigSrcRect.fLeft,
176 fBigSrcRect.fTop,
177 fBigSrcRect.width()/2,
178 fBigSrcRect.height()/2);
179 SkRect dst = SkRect::MakeXYWH(SkIntToScalar(transX), SkIntToScalar(transY),
180 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
181
182 SkPaint paint;
183 paint.setColor(SK_ColorBLUE);
184 paint.setAntiAlias(aa);
185
186 this->drawImage(canvas, fBigImage, src, dst, sampling, &paint);
187 }
188
189 // Draw the area of interest of the small image with a normal blur
190 void drawCase4(SkCanvas* canvas, int transX, int transY, bool aa,
191 const SkSamplingOptions& sampling) {
192 SkRect dst = SkRect::MakeXYWH(SkIntToScalar(transX), SkIntToScalar(transY),
193 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
194
195 SkPaint paint;
196 paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
197 SkBlurMask::ConvertRadiusToSigma(3)));
198 paint.setColor(SK_ColorBLUE);
199 paint.setAntiAlias(aa);
200
201 this->drawImage(canvas, fSmallImage, fSmallSrcRect, dst, sampling, &paint);
202 }
203
204 // Draw the area of interest of the small image with a outer blur
205 void drawCase5(SkCanvas* canvas, int transX, int transY, bool aa,
206 const SkSamplingOptions& sampling) {
207 SkRect dst = SkRect::MakeXYWH(SkIntToScalar(transX), SkIntToScalar(transY),
208 SkIntToScalar(kBlockSize), SkIntToScalar(kBlockSize));
209
210 SkPaint paint;
211 paint.setMaskFilter(SkMaskFilter::MakeBlur(kOuter_SkBlurStyle,
212 SkBlurMask::ConvertRadiusToSigma(7)));
213 paint.setColor(SK_ColorBLUE);
214 paint.setAntiAlias(aa);
215
216 this->drawImage(canvas, fSmallImage, fSmallSrcRect, dst, sampling, &paint);
217 }
218
219 void onDraw(SkCanvas* canvas) override {
220 if (!fSmallImage) {
221 std::tie(fBigImage, fBigSrcRect) = make_ringed_image(canvas,
222 2*kMaxTextureSize,
223 2*kMaxTextureSize);
224 std::tie(fSmallImage, fSmallSrcRect) = make_ringed_image(canvas,
225 kSmallSize, kSmallSize);
226 }
227
228 canvas->clear(SK_ColorGRAY);
229 std::vector<SkMatrix> matrices;
230 // Draw with identity
231 matrices.push_back(SkMatrix::I());
232
233 // Draw with rotation and scale down in x, up in y.
234 SkMatrix m;
235 constexpr SkScalar kBottom = SkIntToScalar(kRow4Y + kBlockSize + kBlockSpacing);
236 m.setTranslate(0, kBottom);
237 m.preRotate(15.f, 0, kBottom + kBlockSpacing);
238 m.preScale(0.71f, 1.22f);
239 matrices.push_back(m);
240
241 // Align the next set with the middle of the previous in y, translated to the right in x.
242 SkPoint corners[] = {{0, 0}, {0, kBottom}, {kWidth, kBottom}, {kWidth, 0}};
243 matrices.back().mapPoints(corners);
244 m.setTranslate(std::max({corners[0].fX, corners[1].fX, corners[2].fX, corners[3].fX}),
245 (corners[0].fY + corners[1].fY + corners[2].fY + corners[3].fY) / 4);
246 m.preScale(0.2f, 0.2f);
247 matrices.push_back(m);
248
249 const SkSamplingOptions none(SkFilterMode::kNearest);
250 const SkSamplingOptions low(SkFilterMode::kLinear);
251 const SkSamplingOptions high(SkCubicResampler::Mitchell());
252
253 SkScalar maxX = 0;
254 for (bool antiAlias : {false, true}) {
255 canvas->save();
256 canvas->translate(maxX, 0);
257 for (const SkMatrix& matrix : matrices) {
258 canvas->save();
259 canvas->concat(matrix);
260
261 // First draw a column with no filtering
262 this->drawCase1(canvas, kCol0X, kRow0Y, antiAlias, none);
263 this->drawCase2(canvas, kCol0X, kRow1Y, antiAlias, none);
264 this->drawCase3(canvas, kCol0X, kRow2Y, antiAlias, none);
265 this->drawCase4(canvas, kCol0X, kRow3Y, antiAlias, none);
266 this->drawCase5(canvas, kCol0X, kRow4Y, antiAlias, none);
267
268 // Then draw a column with low filtering
269 this->drawCase1(canvas, kCol1X, kRow0Y, antiAlias, low);
270 this->drawCase2(canvas, kCol1X, kRow1Y, antiAlias, low);
271 this->drawCase3(canvas, kCol1X, kRow2Y, antiAlias, low);
272 this->drawCase4(canvas, kCol1X, kRow3Y, antiAlias, low);
273 this->drawCase5(canvas, kCol1X, kRow4Y, antiAlias, low);
274
275 // Then draw a column with high filtering. Skip it if in kStrict mode and MIP
276 // mapping will be used. On GPU we allow bleeding at non-base levels because
277 // building a new MIP chain for the subset is expensive.
278 SkScalar scales[2];
279 SkAssertResult(matrix.getMinMaxScales(scales));
280 if (fConstraint != SkCanvas::kStrict_SrcRectConstraint || scales[0] >= 1.f) {
281 this->drawCase1(canvas, kCol2X, kRow0Y, antiAlias, high);
282 this->drawCase2(canvas, kCol2X, kRow1Y, antiAlias, high);
283 this->drawCase3(canvas, kCol2X, kRow2Y, antiAlias, high);
284 this->drawCase4(canvas, kCol2X, kRow3Y, antiAlias, high);
285 this->drawCase5(canvas, kCol2X, kRow4Y, antiAlias, high);
286 }
287
288 SkPoint innerCorners[] = {{0, 0}, {0, kBottom}, {kWidth, kBottom}, {kWidth, 0}};
289 matrix.mapPoints(innerCorners);
290 SkScalar x = kBlockSize + std::max({innerCorners[0].fX, innerCorners[1].fX,
291 innerCorners[2].fX, innerCorners[3].fX});
292 maxX = std::max(maxX, x);
293 canvas->restore();
294 }
295 canvas->restore();
296 }
297 }
298
299 #if defined(SK_GANESH)
300 void modifyGrContextOptions(GrContextOptions* options) override {
301 options->fMaxTextureSizeOverride = kMaxTextureSize;
302 }
303 #endif
304
305 #if defined(SK_GRAPHITE)
306 void modifyGraphiteContextOptions(skgpu::graphite::ContextOptions* options) const override {
307 SkASSERT(options->fOptionsPriv);
308 options->fOptionsPriv->fMaxTextureSizeOverride = kMaxTextureSize;
309 }
310 #endif
311
312 private:
313 inline static constexpr int kBlockSize = 70;
314 inline static constexpr int kBlockSpacing = 12;
315
316 inline static constexpr int kCol0X = kBlockSpacing;
317 inline static constexpr int kCol1X = 2*kBlockSpacing + kBlockSize;
318 inline static constexpr int kCol2X = 3*kBlockSpacing + 2*kBlockSize;
319 inline static constexpr int kWidth = 4*kBlockSpacing + 3*kBlockSize;
320
321 inline static constexpr int kRow0Y = kBlockSpacing;
322 inline static constexpr int kRow1Y = 2*kBlockSpacing + kBlockSize;
323 inline static constexpr int kRow2Y = 3*kBlockSpacing + 2*kBlockSize;
324 inline static constexpr int kRow3Y = 4*kBlockSpacing + 3*kBlockSize;
325 inline static constexpr int kRow4Y = 5*kBlockSpacing + 4*kBlockSize;
326
327 inline static constexpr int kSmallSize = 6;
328 // This must be at least as large as the GM width and height so that a surface can be made, and
329 // a power-of-2 to account for any approx-fitting that the backend may perform.
330 inline static constexpr int kMaxTextureSize = 1024;
331
332 SkString fShortName;
333 sk_sp<SkImage> fBigImage;
334 sk_sp<SkImage> fSmallImage;
335 SkRect fBigSrcRect;
336 SkRect fSmallSrcRect;
337 SkCanvas::SrcRectConstraint fConstraint;
338 bool fManual;
339 using INHERITED = GM;
340 };
341
342 DEF_GM(return new SrcRectConstraintGM("strict_constraint_no_red_allowed",
343 SkCanvas::kStrict_SrcRectConstraint,
344 /* manual= */ false);)
345 DEF_GM(return new SrcRectConstraintGM("strict_constraint_no_red_allowed_manual",
346 SkCanvas::kStrict_SrcRectConstraint,
347 /* manual= */ true);)
348
349 DEF_GM(return new SrcRectConstraintGM("strict_constraint_batch_no_red_allowed",
350 SkCanvas::kStrict_SrcRectConstraint,
351 /* manual= */ false);)
352 DEF_GM(return new SrcRectConstraintGM("strict_constraint_batch_no_red_allowed_manual",
353 SkCanvas::kStrict_SrcRectConstraint,
354 /* manual= */ true);)
355
356 DEF_GM(return new SrcRectConstraintGM("fast_constraint_red_is_allowed",
357 SkCanvas::kFast_SrcRectConstraint,
358 /* manual= */ false);)
359 DEF_GM(return new SrcRectConstraintGM("fast_constraint_red_is_allowed_manual",
360 SkCanvas::kFast_SrcRectConstraint,
361 /* manual= */ true);)
362
363 ///////////////////////////////////////////////////////////////////////////////////////////////////
364
365 // Construct an image and return the inner "src" rect. Build the image such that the interior is
366 // blue, with a margin of blue (2px) but then an outer margin of red.
367 //
368 // Show that kFast_SrcRectConstraint sees even the red margin (due to mipmapping) when the image
369 // is scaled down far enough.
370 //
371 static sk_sp<SkImage> make_image(SkCanvas* canvas, SkRect* srcR) {
372 // Intentially making the size a power of 2 to avoid the noise from how different GPUs will
373 // produce different mipmap filtering when we have an odd sized texture.
374 const int N = 10 + 2 + 8 + 2 + 10;
375 SkImageInfo info = SkImageInfo::MakeN32Premul(N, N);
376 auto surface = ToolUtils::makeSurface(canvas, info);
377 SkCanvas* c = surface->getCanvas();
378 SkRect r = SkRect::MakeIWH(info.width(), info.height());
379 SkPaint paint;
380
381 paint.setColor(SK_ColorRED);
382 c->drawRect(r, paint);
383 r.inset(10, 10);
384 paint.setColor(SK_ColorBLUE);
385 c->drawRect(r, paint);
386
387 *srcR = r.makeInset(2, 2);
388 return surface->makeImageSnapshot();
389 }
390
391 DEF_SIMPLE_GM(bleed_downscale, canvas, 360, 240) {
392 SkRect src;
393 sk_sp<SkImage> img = make_image(canvas, &src);
394 SkPaint paint;
395
396 canvas->translate(10, 10);
397
398 const SkCanvas::SrcRectConstraint constraints[] = {
399 SkCanvas::kStrict_SrcRectConstraint, SkCanvas::kFast_SrcRectConstraint
400 };
401 const SkSamplingOptions samplings[] = {
402 SkSamplingOptions(SkFilterMode::kNearest),
403 SkSamplingOptions(SkFilterMode::kLinear),
404 SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear),
405 };
406 for (auto constraint : constraints) {
407 canvas->save();
408 for (auto sampling : samplings) {
409 auto surf = ToolUtils::makeSurface(canvas, SkImageInfo::MakeN32Premul(1, 1));
410 surf->getCanvas()->drawImageRect(img, src, SkRect::MakeWH(1, 1), sampling,
411 nullptr, constraint);
412 // now blow up the 1 pixel result
413 canvas->drawImageRect(surf->makeImageSnapshot(), SkRect::MakeWH(100, 100),
414 SkSamplingOptions());
415 canvas->translate(120, 0);
416 }
417 canvas->restore();
418 canvas->translate(0, 120);
419 }
420 }