Robert Phillips | 702b37b | 2019-08-09 11:04:44 -0400 | [diff] [blame] | 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 | |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
Robert Phillips | 702b37b | 2019-08-09 11:04:44 -0400 | [diff] [blame] | 9 | #include "include/core/SkSurface.h" |
| 10 | #include "include/effects/SkImageFilters.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 11 | #include "include/gpu/GrDirectContext.h" |
Robert Phillips | 702b37b | 2019-08-09 11:04:44 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrContextPriv.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrResourceCache.h" |
Robert Phillips | 702b37b | 2019-08-09 11:04:44 -0400 | [diff] [blame] | 14 | #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. |
| 25 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RepeatedClippedBlurTest, reporter, ctxInfo) { |
Adlai Holler | d13b97f | 2020-07-21 20:01:57 +0000 | [diff] [blame] | 26 | auto context = ctxInfo.directContext(); |
| 27 | GrResourceCache* cache = context->priv().getResourceCache(); |
Robert Phillips | 702b37b | 2019-08-09 11:04:44 -0400 | [diff] [blame] | 28 | |
| 29 | const SkImageInfo ii = SkImageInfo::Make(1024, 600, kRGBA_8888_SkColorType, |
| 30 | kPremul_SkAlphaType); |
| 31 | |
Adlai Holler | d13b97f | 2020-07-21 20:01:57 +0000 | [diff] [blame] | 32 | sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, ii)); |
Robert Phillips | 702b37b | 2019-08-09 11:04:44 -0400 | [diff] [blame] | 33 | 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 Holler | d13b97f | 2020-07-21 20:01:57 +0000 | [diff] [blame] | 56 | bigImg = rasterImg->makeTextureImage(context); |
Robert Phillips | 702b37b | 2019-08-09 11:04:44 -0400 | [diff] [blame] | 57 | } |
| 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 Holler | d13b97f | 2020-07-21 20:01:57 +0000 | [diff] [blame] | 66 | sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, |
Robert Phillips | 702b37b | 2019-08-09 11:04:44 -0400 | [diff] [blame] | 67 | 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 Holler | d13b97f | 2020-07-21 20:01:57 +0000 | [diff] [blame] | 77 | context->flushAndSubmit(); |
Robert Phillips | 702b37b | 2019-08-09 11:04:44 -0400 | [diff] [blame] | 78 | |
| 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 Holler | d13b97f | 2020-07-21 20:01:57 +0000 | [diff] [blame] | 96 | sk_sp<SkImage> filteredImg = smImg->makeWithFilter(context, blur.get(), subset, clip, |
Robert Phillips | 702b37b | 2019-08-09 11:04:44 -0400 | [diff] [blame] | 97 | &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 Holler | d13b97f | 2020-07-21 20:01:57 +0000 | [diff] [blame] | 104 | context->flushAndSubmit(); |
Robert Phillips | 702b37b | 2019-08-09 11:04:44 -0400 | [diff] [blame] | 105 | |
| 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 | } |