• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/GrDirectContext.h"
25 #include "include/gpu/GrTypes.h"
26 #include "src/gpu/ganesh/GrDirectContextPriv.h"
27 #include "src/gpu/ganesh/GrResourceCache.h"
28 #include "tests/CtsEnforcement.h"
29 #include "tests/Test.h"
30 
31 #include <cstddef>
32 
33 struct GrContextOptions;
34 
35 // This is the repro of a CastOS memory regression bug (b/138674523).
36 // The test simply keeps calling SkImage::makeWithFilter (with a blur image filter) while
37 // shrinking the clip.
38 // When explicit resource allocation was enabled the last (re-expanded) image in the
39 // blur creation process became exact.
40 // This meant that its backing texture could no longer be reused.
41 // In CastOS' case (and, presumably, Linux desktop) they were only using Ganesh for
42 // 2D canvas and compositor image filtering. In this case Chrome doesn't regularly purge
43 // 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)44 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(RepeatedClippedBlurTest,
45                                        reporter,
46                                        ctxInfo,
47                                        CtsEnforcement::kApiLevel_T) {
48     auto dContext = ctxInfo.directContext();
49     GrResourceCache* cache = dContext->priv().getResourceCache();
50 
51     const SkImageInfo ii = SkImageInfo::Make(1024, 600, kRGBA_8888_SkColorType,
52                                              kPremul_SkAlphaType);
53 
54     sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(dContext, skgpu::Budgeted::kNo, ii));
55     if (!dst) {
56         ERRORF(reporter, "Could not create surfaces for repeated clipped blur test.");
57         return;
58     }
59 
60     SkCanvas* dstCanvas = dst->getCanvas();
61 
62     sk_sp<SkImage> bigImg;
63 
64     // Create the initial big image (this corresponds to the album artwork - which is larger
65     // than the screen)
66     {
67         SkImageInfo srcImageII = SkImageInfo::Make(1280, 1280, kRGBA_8888_SkColorType,
68                                                    kPremul_SkAlphaType);
69 
70         // Make a red ring around a field of green. When rendered the blurred red ring
71         // should still be visible on all sides of the dest image.
72         SkBitmap bm;
73         bm.allocPixels(srcImageII);
74         bm.eraseColor(SK_ColorRED);
75         bm.eraseArea(SkIRect::MakeXYWH(1, 2, 1277, 1274), SK_ColorGREEN);
76 
77         sk_sp<SkImage> rasterImg = bm.asImage();
78         bigImg = rasterImg->makeTextureImage(dContext);
79     }
80 
81     sk_sp<SkImage> smImg;
82 
83     // Shrink the album artwork down to the screen's size
84     {
85         SkImageInfo screenII = SkImageInfo::Make(1024, 600, kRGBA_8888_SkColorType,
86                                                  kPremul_SkAlphaType);
87 
88         sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(
89                 dContext, skgpu::Budgeted::kYes, screenII, 1, kTopLeft_GrSurfaceOrigin, nullptr);
90         SkCanvas* c = s->getCanvas();
91 
92         c->drawImageRect(bigImg, SkRect::MakeWH(1024, 600), SkSamplingOptions());
93 
94         smImg = s->makeImageSnapshot();
95     }
96 
97     // flush here just to clear the playing field
98     dContext->flushAndSubmit();
99 
100     size_t beforeBytes = cache->getResourceBytes();
101 
102     // Now draw the screen-sized image, blurred, multiple times with a shrinking clip.
103     // This simulates the swipe away where the screen-sized album artwork is moved off
104     // screen.
105     // Note that the blur has to big enough to kick the blur code into the decimate then
106     // re-expand case.
107     const SkIRect subset = SkIRect::MakeWH(1024, 600);
108     SkIRect clip = SkIRect::MakeWH(1024, 600);
109 
110     for (int i = 0; i < 30; ++i) {
111         dstCanvas->clear(SK_ColorBLUE);
112 
113         sk_sp<SkImageFilter> blur = SkImageFilters::Blur(20, 20, nullptr);
114 
115         SkIRect outSubset;
116         SkIPoint offset;
117         sk_sp<SkImage> filteredImg = smImg->makeWithFilter(dContext, blur.get(), subset, clip,
118                                                            &outSubset, &offset);
119 
120         SkRect dstRect = SkRect::MakeXYWH(offset.fX, offset.fY,
121                                           outSubset.width(), outSubset.height());
122         dstCanvas->drawImageRect(filteredImg, SkRect::Make(outSubset), dstRect, SkSamplingOptions(),
123                                  nullptr, SkCanvas::kStrict_SrcRectConstraint);
124 
125         // Flush here to mimic Chrome's SkiaHelper::ApplyImageFilter
126         dContext->flushAndSubmit();
127 
128         clip.fRight -= 16;
129     }
130 
131     size_t afterBytes = cache->getResourceBytes();
132 
133     // When the bug manifests the resource cache will accumulate ~80MB. If texture recycling
134     // is working as expected the cache size will level off at ~20MB.
135     REPORTER_ASSERT(reporter, afterBytes < beforeBytes + 20000000);
136 }
137