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 23 using namespace android; 24 using namespace android::uirenderer; 25 using namespace android::uirenderer::renderthread; 26 27 static size_t getCacheUsage(GrContext* grContext) { 28 size_t cacheUsage; 29 grContext->getResourceCacheUsage(nullptr, &cacheUsage); 30 return cacheUsage; 31 } 32 33 RENDERTHREAD_SKIA_PIPELINE_TEST(CacheManager, trimMemory) { 34 DisplayInfo displayInfo = renderThread.mainDisplayInfo(); 35 GrContext* grContext = renderThread.getGrContext(); 36 ASSERT_TRUE(grContext != nullptr); 37 38 // create pairs of offscreen render targets and images until we exceed the backgroundCacheSizeLimit 39 std::vector<sk_sp<SkSurface>> surfaces; 40 41 while (getCacheUsage(grContext) <= renderThread.cacheManager().getBackgroundCacheSize()) { 42 SkImageInfo info = SkImageInfo::MakeA8(displayInfo.w, displayInfo.h); 43 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(grContext, SkBudgeted::kYes, info); 44 surface->getCanvas()->drawColor(SK_AlphaTRANSPARENT); 45 46 grContext->flush(); 47 48 surfaces.push_back(surface); 49 } 50 51 ASSERT_TRUE(1 < surfaces.size()); 52 53 // attempt to trim all memory while we still hold strong refs 54 renderThread.cacheManager().trimMemory(CacheManager::TrimMemoryMode::Complete); 55 ASSERT_TRUE(0 == grContext->getResourceCachePurgeableBytes()); 56 57 // free the surfaces 58 for (size_t i = 0; i < surfaces.size(); i++) { 59 ASSERT_TRUE(surfaces[i]->unique()); 60 surfaces[i].reset(); 61 } 62 63 // verify that we have enough purgeable bytes 64 const size_t purgeableBytes = grContext->getResourceCachePurgeableBytes(); 65 ASSERT_TRUE(renderThread.cacheManager().getBackgroundCacheSize() < purgeableBytes); 66 67 // UI hidden and make sure only some got purged 68 renderThread.cacheManager().trimMemory(CacheManager::TrimMemoryMode::UiHidden); 69 ASSERT_TRUE(0 < grContext->getResourceCachePurgeableBytes()); 70 ASSERT_TRUE(renderThread.cacheManager().getBackgroundCacheSize() > getCacheUsage(grContext)); 71 72 // complete and make sure all get purged 73 renderThread.cacheManager().trimMemory(CacheManager::TrimMemoryMode::Complete); 74 ASSERT_TRUE(0 == grContext->getResourceCachePurgeableBytes()); 75 } 76