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 "CacheManager.h" |
| 18 | |
John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 19 | #include <GrContextOptions.h> |
| 20 | #include <SkExecutor.h> |
| 21 | #include <SkGraphics.h> |
John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 22 | #include <math.h> |
| 23 | #include <utils/Trace.h> |
| 24 | |
| 25 | #include <set> |
| 26 | |
| 27 | #include "CanvasContext.h" |
Alec Mouri | 22d753f | 2019-09-05 17:11:45 -0700 | [diff] [blame] | 28 | #include "DeviceInfo.h" |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 29 | #include "Layer.h" |
Stan Iliev | e75ef1f | 2017-11-27 17:22:42 -0500 | [diff] [blame] | 30 | #include "Properties.h" |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 31 | #include "RenderThread.h" |
John Reck | 9fc3d27 | 2023-05-01 16:33:22 -0400 | [diff] [blame^] | 32 | #include "VulkanManager.h" |
Stan Iliev | e0fae23 | 2020-01-07 17:21:49 -0500 | [diff] [blame] | 33 | #include "pipeline/skia/ATraceMemoryDump.h" |
Stan Iliev | e75ef1f | 2017-11-27 17:22:42 -0500 | [diff] [blame] | 34 | #include "pipeline/skia/ShaderCache.h" |
Derek Sollenberger | 0057db2 | 2018-03-29 14:18:44 -0400 | [diff] [blame] | 35 | #include "pipeline/skia/SkiaMemoryTracer.h" |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 36 | #include "renderstate/RenderState.h" |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 37 | #include "thread/CommonPool.h" |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 38 | |
| 39 | namespace android { |
| 40 | namespace uirenderer { |
| 41 | namespace renderthread { |
| 42 | |
John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 43 | CacheManager::CacheManager(RenderThread& thread) |
| 44 | : mRenderThread(thread), mMemoryPolicy(loadMemoryPolicy()) { |
| 45 | mMaxSurfaceArea = static_cast<size_t>((DeviceInfo::getWidth() * DeviceInfo::getHeight()) * |
| 46 | mMemoryPolicy.initialMaxSurfaceAreaScale); |
| 47 | setupCacheLimits(); |
| 48 | } |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 49 | |
Kevin Lubick | 80beb6f | 2023-01-20 17:57:00 +0000 | [diff] [blame] | 50 | static inline int countLeadingZeros(uint32_t mask) { |
| 51 | // __builtin_clz(0) is undefined, so we have to detect that case. |
| 52 | return mask ? __builtin_clz(mask) : 32; |
| 53 | } |
| 54 | |
| 55 | // Return the smallest power-of-2 >= n. |
| 56 | static inline uint32_t nextPowerOfTwo(uint32_t n) { |
| 57 | return n ? (1 << (32 - countLeadingZeros(n - 1))) : 1; |
| 58 | } |
| 59 | |
John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 60 | void CacheManager::setupCacheLimits() { |
| 61 | mMaxResourceBytes = mMaxSurfaceArea * mMemoryPolicy.surfaceSizeMultiplier; |
| 62 | mBackgroundResourceBytes = mMaxResourceBytes * mMemoryPolicy.backgroundRetentionPercent; |
| 63 | // This sets the maximum size for a single texture atlas in the GPU font cache. If |
| 64 | // necessary, the cache can allocate additional textures that are counted against the |
| 65 | // total cache limits provided to Skia. |
Kevin Lubick | 80beb6f | 2023-01-20 17:57:00 +0000 | [diff] [blame] | 66 | mMaxGpuFontAtlasBytes = nextPowerOfTwo(mMaxSurfaceArea); |
John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 67 | // This sets the maximum size of the CPU font cache to be at least the same size as the |
| 68 | // total number of GPU font caches (i.e. 4 separate GPU atlases). |
| 69 | mMaxCpuFontCacheBytes = std::max(mMaxGpuFontAtlasBytes * 4, SkGraphics::GetFontCacheLimit()); |
| 70 | mBackgroundCpuFontCacheBytes = mMaxCpuFontCacheBytes * mMemoryPolicy.backgroundRetentionPercent; |
| 71 | |
Derek Sollenberger | b9e296e | 2019-04-18 16:21:42 -0400 | [diff] [blame] | 72 | SkGraphics::SetFontCacheLimit(mMaxCpuFontCacheBytes); |
John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 73 | if (mGrContext) { |
| 74 | mGrContext->setResourceCacheLimit(mMaxResourceBytes); |
| 75 | } |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 76 | } |
| 77 | |
Adlai Holler | d234521 | 2020-10-07 14:16:40 -0400 | [diff] [blame] | 78 | void CacheManager::reset(sk_sp<GrDirectContext> context) { |
Greg Daniel | 660d6ec | 2017-12-08 11:44:27 -0500 | [diff] [blame] | 79 | if (context != mGrContext) { |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 80 | destroy(); |
| 81 | } |
| 82 | |
| 83 | if (context) { |
Greg Daniel | 660d6ec | 2017-12-08 11:44:27 -0500 | [diff] [blame] | 84 | mGrContext = std::move(context); |
Robert Phillips | 57bb0bf | 2019-09-06 13:18:17 -0400 | [diff] [blame] | 85 | mGrContext->setResourceCacheLimit(mMaxResourceBytes); |
John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 86 | mLastDeferredCleanup = systemTime(CLOCK_MONOTONIC); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | void CacheManager::destroy() { |
| 91 | // cleanup any caches here as the GrContext is about to go away... |
| 92 | mGrContext.reset(nullptr); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 93 | } |
| 94 | |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 95 | class CommonPoolExecutor : public SkExecutor { |
Derek Sollenberger | 8ec9e88 | 2017-08-24 16:36:08 -0400 | [diff] [blame] | 96 | public: |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 97 | virtual void add(std::function<void(void)> func) override { CommonPool::post(std::move(func)); } |
Derek Sollenberger | 8ec9e88 | 2017-08-24 16:36:08 -0400 | [diff] [blame] | 98 | }; |
| 99 | |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 100 | static CommonPoolExecutor sDefaultExecutor; |
| 101 | |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 102 | void CacheManager::configureContext(GrContextOptions* contextOptions, const void* identity, |
| 103 | ssize_t size) { |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 104 | contextOptions->fAllowPathMaskCaching = true; |
Derek Sollenberger | b9e296e | 2019-04-18 16:21:42 -0400 | [diff] [blame] | 105 | contextOptions->fGlyphCacheTextureMaximumBytes = mMaxGpuFontAtlasBytes; |
John Reck | 322b8ab | 2019-03-14 13:15:28 -0700 | [diff] [blame] | 106 | contextOptions->fExecutor = &sDefaultExecutor; |
Stan Iliev | e75ef1f | 2017-11-27 17:22:42 -0500 | [diff] [blame] | 107 | |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 108 | auto& cache = skiapipeline::ShaderCache::get(); |
| 109 | cache.initShaderDiskCache(identity, size); |
| 110 | contextOptions->fPersistentCache = &cache; |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 111 | } |
| 112 | |
John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 113 | void CacheManager::trimMemory(TrimLevel mode) { |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 114 | if (!mGrContext) { |
| 115 | return; |
| 116 | } |
| 117 | |
Greg Daniel | 41ef566 | 2021-02-01 14:25:48 -0500 | [diff] [blame] | 118 | // flush and submit all work to the gpu and wait for it to finish |
| 119 | mGrContext->flushAndSubmit(/*syncCpu=*/true); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 120 | |
| 121 | switch (mode) { |
Tim Murray | 328ff43 | 2023-04-03 18:47:03 -0700 | [diff] [blame] | 122 | case TrimLevel::BACKGROUND: |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 123 | mGrContext->freeGpuResources(); |
Derek Sollenberger | b9e296e | 2019-04-18 16:21:42 -0400 | [diff] [blame] | 124 | SkGraphics::PurgeAllCaches(); |
John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 125 | mRenderThread.destroyRenderingContext(); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 126 | break; |
John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 127 | case TrimLevel::UI_HIDDEN: |
Derek Sollenberger | b1f27aa | 2018-04-02 13:36:45 -0400 | [diff] [blame] | 128 | // Here we purge all the unlocked scratch resources and then toggle the resources cache |
| 129 | // limits between the background and max amounts. This causes the unlocked resources |
| 130 | // that have persistent data to be purged in LRU order. |
Robert Phillips | 57bb0bf | 2019-09-06 13:18:17 -0400 | [diff] [blame] | 131 | mGrContext->setResourceCacheLimit(mBackgroundResourceBytes); |
Derek Sollenberger | b9e296e | 2019-04-18 16:21:42 -0400 | [diff] [blame] | 132 | SkGraphics::SetFontCacheLimit(mBackgroundCpuFontCacheBytes); |
John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 133 | mGrContext->purgeUnlockedResources(mMemoryPolicy.purgeScratchOnly); |
| 134 | mGrContext->setResourceCacheLimit(mMaxResourceBytes); |
Derek Sollenberger | b9e296e | 2019-04-18 16:21:42 -0400 | [diff] [blame] | 135 | SkGraphics::SetFontCacheLimit(mMaxCpuFontCacheBytes); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 136 | break; |
John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 137 | default: |
| 138 | break; |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | void CacheManager::trimStaleResources() { |
| 143 | if (!mGrContext) { |
| 144 | return; |
| 145 | } |
Greg Daniel | c7ad408 | 2020-05-14 15:38:26 -0400 | [diff] [blame] | 146 | mGrContext->flushAndSubmit(); |
John Reck | f846aee | 2019-10-08 23:28:41 +0000 | [diff] [blame] | 147 | mGrContext->purgeResourcesNotUsedInMs(std::chrono::seconds(30)); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 148 | } |
| 149 | |
John Reck | 3920768 | 2021-05-12 19:10:47 -0400 | [diff] [blame] | 150 | void CacheManager::getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage) { |
| 151 | *cpuUsage = 0; |
| 152 | *gpuUsage = 0; |
| 153 | if (!mGrContext) { |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | skiapipeline::SkiaMemoryTracer cpuTracer("category", true); |
| 158 | SkGraphics::DumpMemoryStatistics(&cpuTracer); |
| 159 | *cpuUsage += cpuTracer.total(); |
| 160 | |
| 161 | skiapipeline::SkiaMemoryTracer gpuTracer("category", true); |
| 162 | mGrContext->dumpMemoryStatistics(&gpuTracer); |
| 163 | *gpuUsage += gpuTracer.total(); |
| 164 | } |
| 165 | |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 166 | void CacheManager::dumpMemoryUsage(String8& log, const RenderState* renderState) { |
John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 167 | log.appendFormat(R"(Memory policy: |
| 168 | Max surface area: %zu |
| 169 | Max resource usage: %.2fMB (x%.0f) |
| 170 | Background retention: %.0f%% (altUiHidden = %s) |
| 171 | )", |
| 172 | mMaxSurfaceArea, mMaxResourceBytes / 1000000.f, |
| 173 | mMemoryPolicy.surfaceSizeMultiplier, |
| 174 | mMemoryPolicy.backgroundRetentionPercent * 100.0f, |
| 175 | mMemoryPolicy.useAlternativeUiHidden ? "true" : "false"); |
| 176 | if (Properties::isSystemOrPersistent) { |
| 177 | log.appendFormat(" IsSystemOrPersistent\n"); |
| 178 | } |
| 179 | log.appendFormat(" GPU Context timeout: %" PRIu64 "\n", ns2s(mMemoryPolicy.contextTimeout)); |
| 180 | size_t stoppedContexts = 0; |
| 181 | for (auto context : mCanvasContexts) { |
| 182 | if (context->isStopped()) stoppedContexts++; |
| 183 | } |
| 184 | log.appendFormat("Contexts: %zu (stopped = %zu)\n", mCanvasContexts.size(), stoppedContexts); |
| 185 | |
John Reck | 9fc3d27 | 2023-05-01 16:33:22 -0400 | [diff] [blame^] | 186 | auto vkInstance = VulkanManager::peekInstance(); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 187 | if (!mGrContext) { |
John Reck | 9fc3d27 | 2023-05-01 16:33:22 -0400 | [diff] [blame^] | 188 | if (!vkInstance) { |
| 189 | log.appendFormat("No GPU context.\n"); |
| 190 | } else { |
| 191 | log.appendFormat("No GrContext; however %d remaining Vulkan refs", |
| 192 | vkInstance->getStrongCount() - 1); |
| 193 | } |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 194 | return; |
| 195 | } |
Derek Sollenberger | 0057db2 | 2018-03-29 14:18:44 -0400 | [diff] [blame] | 196 | std::vector<skiapipeline::ResourcePair> cpuResourceMap = { |
| 197 | {"skia/sk_resource_cache/bitmap_", "Bitmaps"}, |
| 198 | {"skia/sk_resource_cache/rrect-blur_", "Masks"}, |
| 199 | {"skia/sk_resource_cache/rects-blur_", "Masks"}, |
| 200 | {"skia/sk_resource_cache/tessellated", "Shadows"}, |
John Reck | 3920768 | 2021-05-12 19:10:47 -0400 | [diff] [blame] | 201 | {"skia/sk_glyph_cache", "Glyph Cache"}, |
Derek Sollenberger | 0057db2 | 2018-03-29 14:18:44 -0400 | [diff] [blame] | 202 | }; |
| 203 | skiapipeline::SkiaMemoryTracer cpuTracer(cpuResourceMap, false); |
| 204 | SkGraphics::DumpMemoryStatistics(&cpuTracer); |
John Reck | 66e06d4 | 2021-05-11 17:04:54 -0400 | [diff] [blame] | 205 | if (cpuTracer.hasOutput()) { |
| 206 | log.appendFormat("CPU Caches:\n"); |
| 207 | cpuTracer.logOutput(log); |
John Reck | 3920768 | 2021-05-12 19:10:47 -0400 | [diff] [blame] | 208 | log.appendFormat(" Glyph Count: %d \n", SkGraphics::GetFontCacheCountUsed()); |
| 209 | log.appendFormat("Total CPU memory usage:\n"); |
| 210 | cpuTracer.logTotals(log); |
John Reck | 66e06d4 | 2021-05-11 17:04:54 -0400 | [diff] [blame] | 211 | } |
Derek Sollenberger | 0057db2 | 2018-03-29 14:18:44 -0400 | [diff] [blame] | 212 | |
Derek Sollenberger | 0057db2 | 2018-03-29 14:18:44 -0400 | [diff] [blame] | 213 | skiapipeline::SkiaMemoryTracer gpuTracer("category", true); |
| 214 | mGrContext->dumpMemoryStatistics(&gpuTracer); |
John Reck | 66e06d4 | 2021-05-11 17:04:54 -0400 | [diff] [blame] | 215 | if (gpuTracer.hasOutput()) { |
| 216 | log.appendFormat("GPU Caches:\n"); |
| 217 | gpuTracer.logOutput(log); |
| 218 | } |
Derek Sollenberger | 0057db2 | 2018-03-29 14:18:44 -0400 | [diff] [blame] | 219 | |
John Reck | 66e06d4 | 2021-05-11 17:04:54 -0400 | [diff] [blame] | 220 | if (renderState && renderState->mActiveLayers.size() > 0) { |
| 221 | log.appendFormat("Layer Info:\n"); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 222 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 223 | const char* layerType = Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 224 | ? "GlLayer" |
| 225 | : "VkLayer"; |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 226 | size_t layerMemoryTotal = 0; |
| 227 | for (std::set<Layer*>::iterator it = renderState->mActiveLayers.begin(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 228 | it != renderState->mActiveLayers.end(); it++) { |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 229 | const Layer* layer = *it; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 230 | log.appendFormat(" %s size %dx%d\n", layerType, layer->getWidth(), |
| 231 | layer->getHeight()); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 232 | layerMemoryTotal += layer->getWidth() * layer->getHeight() * 4; |
| 233 | } |
Derek Sollenberger | 0057db2 | 2018-03-29 14:18:44 -0400 | [diff] [blame] | 234 | log.appendFormat(" Layers Total %6.2f KB (numLayers = %zu)\n", |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 235 | layerMemoryTotal / 1024.0f, renderState->mActiveLayers.size()); |
| 236 | } |
| 237 | |
Derek Sollenberger | 0057db2 | 2018-03-29 14:18:44 -0400 | [diff] [blame] | 238 | log.appendFormat("Total GPU memory usage:\n"); |
| 239 | gpuTracer.logTotals(log); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 240 | } |
| 241 | |
Stan Iliev | e0fae23 | 2020-01-07 17:21:49 -0500 | [diff] [blame] | 242 | void CacheManager::onFrameCompleted() { |
John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 243 | cancelDestroyContext(); |
| 244 | mFrameCompletions.next() = systemTime(CLOCK_MONOTONIC); |
Stan Iliev | e0fae23 | 2020-01-07 17:21:49 -0500 | [diff] [blame] | 245 | if (ATRACE_ENABLED()) { |
| 246 | static skiapipeline::ATraceMemoryDump tracer; |
| 247 | tracer.startFrame(); |
| 248 | SkGraphics::DumpMemoryStatistics(&tracer); |
| 249 | if (mGrContext) { |
| 250 | mGrContext->dumpMemoryStatistics(&tracer); |
| 251 | } |
| 252 | tracer.logTraces(); |
| 253 | } |
| 254 | } |
| 255 | |
John Reck | 5f66fb8 | 2022-09-23 17:49:23 -0400 | [diff] [blame] | 256 | void CacheManager::onThreadIdle() { |
| 257 | if (!mGrContext || mFrameCompletions.size() == 0) return; |
| 258 | |
| 259 | const nsecs_t now = systemTime(CLOCK_MONOTONIC); |
| 260 | // Rate limiting |
| 261 | if ((now - mLastDeferredCleanup) < 25_ms) { |
| 262 | mLastDeferredCleanup = now; |
| 263 | const nsecs_t frameCompleteNanos = mFrameCompletions[0]; |
| 264 | const nsecs_t frameDiffNanos = now - frameCompleteNanos; |
| 265 | const nsecs_t cleanupMillis = |
| 266 | ns2ms(std::max(frameDiffNanos, mMemoryPolicy.minimumResourceRetention)); |
| 267 | mGrContext->performDeferredCleanup(std::chrono::milliseconds(cleanupMillis), |
| 268 | mMemoryPolicy.purgeScratchOnly); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void CacheManager::scheduleDestroyContext() { |
| 273 | if (mMemoryPolicy.contextTimeout > 0) { |
| 274 | mRenderThread.queue().postDelayed(mMemoryPolicy.contextTimeout, |
| 275 | [this, genId = mGenerationId] { |
| 276 | if (mGenerationId != genId) return; |
| 277 | // GenID should have already stopped this, but just in |
| 278 | // case |
| 279 | if (!areAllContextsStopped()) return; |
| 280 | mRenderThread.destroyRenderingContext(); |
| 281 | }); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | void CacheManager::cancelDestroyContext() { |
| 286 | if (mIsDestructionPending) { |
| 287 | mIsDestructionPending = false; |
| 288 | mGenerationId++; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | bool CacheManager::areAllContextsStopped() { |
| 293 | for (auto context : mCanvasContexts) { |
| 294 | if (!context->isStopped()) return false; |
| 295 | } |
| 296 | return true; |
| 297 | } |
| 298 | |
| 299 | void CacheManager::checkUiHidden() { |
| 300 | if (!mGrContext) return; |
| 301 | |
| 302 | if (mMemoryPolicy.useAlternativeUiHidden && areAllContextsStopped()) { |
| 303 | trimMemory(TrimLevel::UI_HIDDEN); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | void CacheManager::registerCanvasContext(CanvasContext* context) { |
| 308 | mCanvasContexts.push_back(context); |
| 309 | cancelDestroyContext(); |
| 310 | } |
| 311 | |
| 312 | void CacheManager::unregisterCanvasContext(CanvasContext* context) { |
| 313 | std::erase(mCanvasContexts, context); |
| 314 | checkUiHidden(); |
| 315 | if (mCanvasContexts.empty()) { |
| 316 | scheduleDestroyContext(); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | void CacheManager::onContextStopped(CanvasContext* context) { |
| 321 | checkUiHidden(); |
| 322 | if (mMemoryPolicy.releaseContextOnStoppedOnly && areAllContextsStopped()) { |
| 323 | scheduleDestroyContext(); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | void CacheManager::notifyNextFrameSize(int width, int height) { |
| 328 | int frameArea = width * height; |
| 329 | if (frameArea > mMaxSurfaceArea) { |
| 330 | mMaxSurfaceArea = frameArea; |
| 331 | setupCacheLimits(); |
Nader Jawad | dd1fcab | 2021-06-10 18:54:23 -0700 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 335 | } /* namespace renderthread */ |
| 336 | } /* namespace uirenderer */ |
| 337 | } /* namespace android */ |