xref: /aosp_15_r20/external/skia/tests/RepeatedClippedBlurTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 
8 #include "include/core/SkAlphaType.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorType.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkImageFilter.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkPoint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkSamplingOptions.h"
20 #include "include/core/SkSurface.h"
21 #include "include/core/SkTypes.h"
22 #include "include/effects/SkImageFilters.h"
23 #include "include/gpu/GpuTypes.h"
24 #include "include/gpu/ganesh/GrDirectContext.h"
25 #include "include/gpu/ganesh/GrTypes.h"
26 #include "include/gpu/ganesh/SkImageGanesh.h"
27 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
28 #include "src/gpu/ganesh/GrDirectContextPriv.h"
29 #include "src/gpu/ganesh/GrResourceCache.h"
30 #include "tests/CtsEnforcement.h"
31 #include "tests/Test.h"
32 
33 #include <cstddef>
34 
35 struct 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.
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(RepeatedClippedBlurTest,reporter,ctxInfo,CtsEnforcement::kApiLevel_T)46 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(RepeatedClippedBlurTest,
47                                        reporter,
48                                        ctxInfo,
49                                        CtsEnforcement::kApiLevel_T) {
50     auto dContext = ctxInfo.directContext();
51     GrResourceCache* cache = dContext->priv().getResourceCache();
52 
53     const SkImageInfo ii = SkImageInfo::Make(1024, 600, kRGBA_8888_SkColorType,
54                                              kPremul_SkAlphaType);
55 
56     sk_sp<SkSurface> dst(SkSurfaces::RenderTarget(dContext, skgpu::Budgeted::kNo, ii));
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     {
69         SkImageInfo srcImageII = SkImageInfo::Make(1280, 1280, kRGBA_8888_SkColorType,
70                                                    kPremul_SkAlphaType);
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);
76         bm.eraseColor(SK_ColorRED);
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     {
87         SkImageInfo screenII = SkImageInfo::Make(1024, 600, kRGBA_8888_SkColorType,
88                                                  kPremul_SkAlphaType);
89 
90         sk_sp<SkSurface> s = SkSurfaces::RenderTarget(
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;
118         SkIPoint offset;
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(),
125                                  nullptr, SkCanvas::kStrict_SrcRectConstraint);
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 }
139