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