| 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 |  | 
| Kevin Lubick | 9367108 | 2023-03-13 18:30:55 +0000 | [diff] [blame] | 23 | #include <SkImageAndroid.h> | 
| Kevin Lubick | 8099b67 | 2023-01-09 14:48:45 +0000 | [diff] [blame] | 24 | #include "include/gpu/GpuTypes.h" // from Skia | 
| Derek Sollenberger | b1f27aa | 2018-04-02 13:36:45 -0400 | [diff] [blame] | 25 |  | 
| Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 26 | using namespace android; | 
|  | 27 | using namespace android::uirenderer; | 
|  | 28 | using namespace android::uirenderer::renderthread; | 
|  | 29 |  | 
| Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 30 | static size_t getCacheUsage(GrDirectContext* grContext) { | 
| Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 31 | size_t cacheUsage; | 
|  | 32 | grContext->getResourceCacheUsage(nullptr, &cacheUsage); | 
|  | 33 | return cacheUsage; | 
|  | 34 | } | 
|  | 35 |  | 
| Nolan Scobie | 81787fb | 2022-11-14 19:35:58 -0500 | [diff] [blame] | 36 | // TOOD(258700630): fix this test and re-enable | 
|  | 37 | RENDERTHREAD_SKIA_PIPELINE_TEST(CacheManager, DISABLED_trimMemory) { | 
| Alec Mouri | 22d753f | 2019-09-05 17:11:45 -0700 | [diff] [blame] | 38 | int32_t width = DeviceInfo::get()->getWidth(); | 
|  | 39 | int32_t height = DeviceInfo::get()->getHeight(); | 
| Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 40 | GrDirectContext* grContext = renderThread.getGrContext(); | 
| Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 41 | ASSERT_TRUE(grContext != nullptr); | 
|  | 42 |  | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 43 | // create pairs of offscreen render targets and images until we exceed the | 
|  | 44 | // backgroundCacheSizeLimit | 
| Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 45 | std::vector<sk_sp<SkSurface>> surfaces; | 
|  | 46 |  | 
|  | 47 | while (getCacheUsage(grContext) <= renderThread.cacheManager().getBackgroundCacheSize()) { | 
| Alec Mouri | 22d753f | 2019-09-05 17:11:45 -0700 | [diff] [blame] | 48 | SkImageInfo info = SkImageInfo::MakeA8(width, height); | 
| Kevin Lubick | 8099b67 | 2023-01-09 14:48:45 +0000 | [diff] [blame] | 49 | sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(grContext, skgpu::Budgeted::kYes, | 
|  | 50 | info); | 
| Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 51 | surface->getCanvas()->drawColor(SK_AlphaTRANSPARENT); | 
|  | 52 |  | 
| Greg Daniel | c7ad408 | 2020-05-14 15:38:26 -0400 | [diff] [blame] | 53 | grContext->flushAndSubmit(); | 
| Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 54 |  | 
|  | 55 | surfaces.push_back(surface); | 
|  | 56 | } | 
|  | 57 |  | 
| Derek Sollenberger | b1f27aa | 2018-04-02 13:36:45 -0400 | [diff] [blame] | 58 | // 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] | 59 | sk_sp<Bitmap> bitmap = Bitmap::allocateHeapBitmap(SkImageInfo::MakeA8(width, height)); | 
| Kevin Lubick | 9367108 | 2023-03-13 18:30:55 +0000 | [diff] [blame] | 60 | sk_sp<SkImage> image = bitmap->makeImage(); // calls skgpu::ganesh::PinAsTexture under the hood. | 
|  | 61 | ASSERT_TRUE(skgpu::ganesh::PinAsTexture(grContext, image.get())); | 
| Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 62 | // attempt to trim all memory while we still hold strong refs | 
| John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 63 | renderThread.cacheManager().trimMemory(TrimLevel::COMPLETE); | 
| Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 64 | ASSERT_TRUE(0 == grContext->getResourceCachePurgeableBytes()); | 
|  | 65 |  | 
|  | 66 | // free the surfaces | 
|  | 67 | for (size_t i = 0; i < surfaces.size(); i++) { | 
|  | 68 | ASSERT_TRUE(surfaces[i]->unique()); | 
|  | 69 | surfaces[i].reset(); | 
|  | 70 | } | 
|  | 71 |  | 
| Derek Sollenberger | b1f27aa | 2018-04-02 13:36:45 -0400 | [diff] [blame] | 72 | // unpin the image which should add a unique purgeable key to the cache | 
| Kevin Lubick | 9367108 | 2023-03-13 18:30:55 +0000 | [diff] [blame] | 73 | skgpu::ganesh::UnpinTexture(grContext, image.get()); | 
| Derek Sollenberger | b1f27aa | 2018-04-02 13:36:45 -0400 | [diff] [blame] | 74 |  | 
| Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 75 | // verify that we have enough purgeable bytes | 
|  | 76 | const size_t purgeableBytes = grContext->getResourceCachePurgeableBytes(); | 
|  | 77 | ASSERT_TRUE(renderThread.cacheManager().getBackgroundCacheSize() < purgeableBytes); | 
|  | 78 |  | 
| Derek Sollenberger | b1f27aa | 2018-04-02 13:36:45 -0400 | [diff] [blame] | 79 | // UI hidden and make sure only some got purged (unique should remain) | 
| John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 80 | renderThread.cacheManager().trimMemory(TrimLevel::UI_HIDDEN); | 
| Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 81 | ASSERT_TRUE(0 < grContext->getResourceCachePurgeableBytes()); | 
|  | 82 | ASSERT_TRUE(renderThread.cacheManager().getBackgroundCacheSize() > getCacheUsage(grContext)); | 
|  | 83 |  | 
|  | 84 | // complete and make sure all get purged | 
| John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 85 | renderThread.cacheManager().trimMemory(TrimLevel::COMPLETE); | 
| Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 86 | ASSERT_TRUE(0 == grContext->getResourceCachePurgeableBytes()); | 
|  | 87 | } |