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