Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | #include "renderthread/CacheManager.h" |
| 20 | #include "renderthread/EglManager.h" |
| 21 | #include "tests/common/TestUtils.h" |
| 22 | |
Derek Sollenberger | b1f27aa | 2018-04-02 13:36:45 -0400 | [diff] [blame] | 23 | #include <SkImagePriv.h> |
| 24 | |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 25 | using namespace android; |
| 26 | using namespace android::uirenderer; |
| 27 | using namespace android::uirenderer::renderthread; |
| 28 | |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 29 | static size_t getCacheUsage(GrDirectContext* grContext) { |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 30 | size_t cacheUsage; |
| 31 | grContext->getResourceCacheUsage(nullptr, &cacheUsage); |
| 32 | return cacheUsage; |
| 33 | } |
| 34 | |
| 35 | RENDERTHREAD_SKIA_PIPELINE_TEST(CacheManager, trimMemory) { |
Alec Mouri | 22d753f | 2019-09-05 17:11:45 -0700 | [diff] [blame] | 36 | int32_t width = DeviceInfo::get()->getWidth(); |
| 37 | int32_t height = DeviceInfo::get()->getHeight(); |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 38 | GrDirectContext* grContext = renderThread.getGrContext(); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 39 | ASSERT_TRUE(grContext != nullptr); |
| 40 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 41 | // create pairs of offscreen render targets and images until we exceed the |
| 42 | // backgroundCacheSizeLimit |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 43 | std::vector<sk_sp<SkSurface>> surfaces; |
| 44 | |
| 45 | while (getCacheUsage(grContext) <= renderThread.cacheManager().getBackgroundCacheSize()) { |
Alec Mouri | 22d753f | 2019-09-05 17:11:45 -0700 | [diff] [blame] | 46 | SkImageInfo info = SkImageInfo::MakeA8(width, height); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 47 | sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(grContext, SkBudgeted::kYes, info); |
| 48 | surface->getCanvas()->drawColor(SK_AlphaTRANSPARENT); |
| 49 | |
Greg Daniel | c7ad408 | 2020-05-14 15:38:26 -0400 | [diff] [blame] | 50 | grContext->flushAndSubmit(); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 51 | |
| 52 | surfaces.push_back(surface); |
| 53 | } |
| 54 | |
Derek Sollenberger | b1f27aa | 2018-04-02 13:36:45 -0400 | [diff] [blame] | 55 | // create an image and pin it so that we have something with a unique key in the cache |
Alec Mouri | 22d753f | 2019-09-05 17:11:45 -0700 | [diff] [blame] | 56 | sk_sp<Bitmap> bitmap = Bitmap::allocateHeapBitmap(SkImageInfo::MakeA8(width, height)); |
Derek Sollenberger | d01b591 | 2018-10-19 15:55:33 -0400 | [diff] [blame] | 57 | sk_sp<SkImage> image = bitmap->makeImage(); |
Derek Sollenberger | b1f27aa | 2018-04-02 13:36:45 -0400 | [diff] [blame] | 58 | ASSERT_TRUE(SkImage_pinAsTexture(image.get(), grContext)); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 59 | |
| 60 | // attempt to trim all memory while we still hold strong refs |
| 61 | renderThread.cacheManager().trimMemory(CacheManager::TrimMemoryMode::Complete); |
| 62 | ASSERT_TRUE(0 == grContext->getResourceCachePurgeableBytes()); |
| 63 | |
| 64 | // free the surfaces |
| 65 | for (size_t i = 0; i < surfaces.size(); i++) { |
| 66 | ASSERT_TRUE(surfaces[i]->unique()); |
| 67 | surfaces[i].reset(); |
| 68 | } |
| 69 | |
Derek Sollenberger | b1f27aa | 2018-04-02 13:36:45 -0400 | [diff] [blame] | 70 | // unpin the image which should add a unique purgeable key to the cache |
| 71 | SkImage_unpinAsTexture(image.get(), grContext); |
| 72 | |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 73 | // verify that we have enough purgeable bytes |
| 74 | const size_t purgeableBytes = grContext->getResourceCachePurgeableBytes(); |
| 75 | ASSERT_TRUE(renderThread.cacheManager().getBackgroundCacheSize() < purgeableBytes); |
| 76 | |
Derek Sollenberger | b1f27aa | 2018-04-02 13:36:45 -0400 | [diff] [blame] | 77 | // UI hidden and make sure only some got purged (unique should remain) |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 78 | renderThread.cacheManager().trimMemory(CacheManager::TrimMemoryMode::UiHidden); |
| 79 | ASSERT_TRUE(0 < grContext->getResourceCachePurgeableBytes()); |
| 80 | ASSERT_TRUE(renderThread.cacheManager().getBackgroundCacheSize() > getCacheUsage(grContext)); |
| 81 | |
| 82 | // complete and make sure all get purged |
| 83 | renderThread.cacheManager().trimMemory(CacheManager::TrimMemoryMode::Complete); |
| 84 | ASSERT_TRUE(0 == grContext->getResourceCachePurgeableBytes()); |
| 85 | } |