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