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