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 | |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 8 | #include "include/core/SkBitmap.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
Robert Phillips | 702b37b | 2019-08-09 11:04:44 -0400 | [diff] [blame] | 10 | #include "include/core/SkSurface.h" |
| 11 | #include "include/effects/SkImageFilters.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 12 | #include "include/gpu/GrDirectContext.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrDirectContextPriv.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrResourceCache.h" |
Robert Phillips | 702b37b | 2019-08-09 11:04:44 -0400 | [diff] [blame] | 15 | #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. |
| 26 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(RepeatedClippedBlurTest, reporter, ctxInfo) { |
Adlai Holler | 247835b | 2020-07-21 16:50:06 -0400 | [diff] [blame] | 27 | auto dContext = ctxInfo.directContext(); |
| 28 | GrResourceCache* cache = dContext->priv().getResourceCache(); |
Robert Phillips | 702b37b | 2019-08-09 11:04:44 -0400 | [diff] [blame] | 29 | |
| 30 | const SkImageInfo ii = SkImageInfo::Make(1024, 600, kRGBA_8888_SkColorType, |
| 31 | kPremul_SkAlphaType); |
| 32 | |
Adlai Holler | 247835b | 2020-07-21 16:50:06 -0400 | [diff] [blame] | 33 | sk_sp<SkSurface> dst(SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, ii)); |
Robert Phillips | 702b37b | 2019-08-09 11:04:44 -0400 | [diff] [blame] | 34 | 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 Reed | dc607e3 | 2020-12-23 11:50:36 -0500 | [diff] [blame^] | 56 | sk_sp<SkImage> rasterImg = bm.asImage(); |
Adlai Holler | 247835b | 2020-07-21 16:50:06 -0400 | [diff] [blame] | 57 | bigImg = rasterImg->makeTextureImage(dContext); |
Robert Phillips | 702b37b | 2019-08-09 11:04:44 -0400 | [diff] [blame] | 58 | } |
| 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 Holler | 247835b | 2020-07-21 16:50:06 -0400 | [diff] [blame] | 67 | sk_sp<SkSurface> s = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kYes, |
Robert Phillips | 702b37b | 2019-08-09 11:04:44 -0400 | [diff] [blame] | 68 | screenII, 1, kTopLeft_GrSurfaceOrigin, |
| 69 | nullptr); |
| 70 | SkCanvas* c = s->getCanvas(); |
| 71 | |
| 72 | c->drawImageRect(bigImg, SkRect::MakeWH(1024, 600), nullptr); |
| 73 | |
| 74 | smImg = s->makeImageSnapshot(); |
| 75 | } |
| 76 | |
| 77 | // flush here just to clear the playing field |
Adlai Holler | 247835b | 2020-07-21 16:50:06 -0400 | [diff] [blame] | 78 | dContext->flushAndSubmit(); |
Robert Phillips | 702b37b | 2019-08-09 11:04:44 -0400 | [diff] [blame] | 79 | |
| 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 Holler | 247835b | 2020-07-21 16:50:06 -0400 | [diff] [blame] | 97 | sk_sp<SkImage> filteredImg = smImg->makeWithFilter(dContext, blur.get(), subset, clip, |
Robert Phillips | 702b37b | 2019-08-09 11:04:44 -0400 | [diff] [blame] | 98 | &outSubset, &offset); |
| 99 | |
| 100 | SkRect dstRect = SkRect::MakeXYWH(offset.fX, offset.fY, |
| 101 | outSubset.width(), outSubset.height()); |
| 102 | dstCanvas->drawImageRect(filteredImg, outSubset, dstRect, nullptr); |
| 103 | |
| 104 | // Flush here to mimic Chrome's SkiaHelper::ApplyImageFilter |
Adlai Holler | 247835b | 2020-07-21 16:50:06 -0400 | [diff] [blame] | 105 | dContext->flushAndSubmit(); |
Robert Phillips | 702b37b | 2019-08-09 11:04:44 -0400 | [diff] [blame] | 106 | |
| 107 | clip.fRight -= 16; |
| 108 | } |
| 109 | |
| 110 | size_t afterBytes = cache->getResourceBytes(); |
| 111 | |
| 112 | // When the bug manifests the resource cache will accumulate ~80MB. If texture recycling |
| 113 | // is working as expected the cache size will level off at ~20MB. |
| 114 | REPORTER_ASSERT(reporter, afterBytes < beforeBytes + 20000000); |
| 115 | } |