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