Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
RepeatedClippedBlurTest.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2019 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
17#include "include/core/SkRect.h"
25#include "include/gpu/GrTypes.h"
31#include "tests/Test.h"
32
33#include <cstddef>
34
35struct GrContextOptions;
36
37// This is the repro of a CastOS memory regression bug (b/138674523).
38// The test simply keeps calling SkImages::MakeWithFilter (with a blur image filter) while
39// shrinking the clip.
40// When explicit resource allocation was enabled the last (re-expanded) image in the
41// blur creation process became exact.
42// This meant that its backing texture could no longer be reused.
43// In CastOS' case (and, presumably, Linux desktop) they were only using Ganesh for
44// 2D canvas and compositor image filtering. In this case Chrome doesn't regularly purge
45// the cache. This would result in Ganesh quickly running up to its max cache limit.
48 ctxInfo,
50 auto dContext = ctxInfo.directContext();
51 GrResourceCache* cache = dContext->priv().getResourceCache();
52
55
57 if (!dst) {
58 ERRORF(reporter, "Could not create surfaces for repeated clipped blur test.");
59 return;
60 }
61
62 SkCanvas* dstCanvas = dst->getCanvas();
63
64 sk_sp<SkImage> bigImg;
65
66 // Create the initial big image (this corresponds to the album artwork - which is larger
67 // than the screen)
68 {
71
72 // Make a red ring around a field of green. When rendered the blurred red ring
73 // should still be visible on all sides of the dest image.
74 SkBitmap bm;
75 bm.allocPixels(srcImageII);
77 bm.eraseArea(SkIRect::MakeXYWH(1, 2, 1277, 1274), SK_ColorGREEN);
78
79 sk_sp<SkImage> rasterImg = bm.asImage();
80 bigImg = SkImages::TextureFromImage(dContext, rasterImg);
81 }
82
83 sk_sp<SkImage> smImg;
84
85 // Shrink the album artwork down to the screen's size
86 {
89
91 dContext, skgpu::Budgeted::kYes, screenII, 1, kTopLeft_GrSurfaceOrigin, nullptr);
92 SkCanvas* c = s->getCanvas();
93
94 c->drawImageRect(bigImg, SkRect::MakeWH(1024, 600), SkSamplingOptions());
95
96 smImg = s->makeImageSnapshot();
97 }
98
99 // flush here just to clear the playing field
100 dContext->flushAndSubmit();
101
102 size_t beforeBytes = cache->getResourceBytes();
103
104 // Now draw the screen-sized image, blurred, multiple times with a shrinking clip.
105 // This simulates the swipe away where the screen-sized album artwork is moved off
106 // screen.
107 // Note that the blur has to big enough to kick the blur code into the decimate then
108 // re-expand case.
109 const SkIRect subset = SkIRect::MakeWH(1024, 600);
110 SkIRect clip = SkIRect::MakeWH(1024, 600);
111
112 for (int i = 0; i < 30; ++i) {
113 dstCanvas->clear(SK_ColorBLUE);
114
115 sk_sp<SkImageFilter> blur = SkImageFilters::Blur(20, 20, nullptr);
116
117 SkIRect outSubset;
119 sk_sp<SkImage> filteredImg = SkImages::MakeWithFilter(dContext, smImg, blur.get(), subset,
120 clip, &outSubset, &offset);
121
122 SkRect dstRect = SkRect::MakeXYWH(offset.fX, offset.fY,
123 outSubset.width(), outSubset.height());
124 dstCanvas->drawImageRect(filteredImg, SkRect::Make(outSubset), dstRect, SkSamplingOptions(),
126
127 // Flush here to mimic Chrome's SkiaHelper::ApplyImageFilter
128 dContext->flushAndSubmit();
129
130 clip.fRight -= 16;
131 }
132
133 size_t afterBytes = cache->getResourceBytes();
134
135 // When the bug manifests the resource cache will accumulate ~80MB. If texture recycling
136 // is working as expected the cache size will level off at ~20MB.
137 REPORTER_ASSERT(reporter, afterBytes < beforeBytes + 20000000);
138}
reporter
@ kTopLeft_GrSurfaceOrigin
Definition GrTypes.h:148
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition SkPath.cpp:3824
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
#define ERRORF(r,...)
Definition Test.h:293
#define DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(name, reporter, context_info, ctsEnforcement)
Definition Test.h:434
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition SkBitmap.cpp:258
sk_sp< SkImage > asImage() const
Definition SkBitmap.cpp:645
void eraseArea(const SkIRect &area, SkColor c) const
Definition SkBitmap.h:854
void eraseColor(SkColor4f) const
Definition SkBitmap.cpp:442
@ kStrict_SrcRectConstraint
sample only inside bounds; slower
Definition SkCanvas.h:1542
void clear(SkColor color)
Definition SkCanvas.h:1199
void drawImageRect(const SkImage *, const SkRect &src, const SkRect &dst, const SkSamplingOptions &, const SkPaint *, SrcRectConstraint)
static sk_sp< SkImageFilter > Blur(SkScalar sigmaX, SkScalar sigmaY, SkTileMode tileMode, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
T * get() const
Definition SkRefCnt.h:303
struct MyStruct s
SK_API sk_sp< SkImage > TextureFromImage(GrDirectContext *, const SkImage *, skgpu::Mipmapped=skgpu::Mipmapped::kNo, skgpu::Budgeted=skgpu::Budgeted::kYes)
SK_API sk_sp< SkImage > MakeWithFilter(sk_sp< SkImage > src, const SkImageFilter *filter, const SkIRect &subset, const SkIRect &clipBounds, SkIRect *outSubset, SkIPoint *offset)
SK_API sk_sp< SkSurface > RenderTarget(GrRecordingContext *context, skgpu::Budgeted budgeted, const SkImageInfo &imageInfo, int sampleCount, GrSurfaceOrigin surfaceOrigin, const SkSurfaceProps *surfaceProps, bool shouldCreateWithMips=false, bool isProtected=false)
Point offset
constexpr int32_t height() const
Definition SkRect.h:165
constexpr int32_t width() const
Definition SkRect.h:158
static constexpr SkIRect MakeWH(int32_t w, int32_t h)
Definition SkRect.h:56
static constexpr SkIRect MakeXYWH(int32_t x, int32_t y, int32_t w, int32_t h)
Definition SkRect.h:104
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static SkRect Make(const SkISize &size)
Definition SkRect.h:669
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609