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