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