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 | |
| 19 | #include "Layer.h" |
Stan Iliev | e75ef1f | 2017-11-27 17:22:42 -0500 | [diff] [blame] | 20 | #include "Properties.h" |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 21 | #include "RenderThread.h" |
Stan Iliev | e75ef1f | 2017-11-27 17:22:42 -0500 | [diff] [blame] | 22 | #include "pipeline/skia/ShaderCache.h" |
Derek Sollenberger | 0057db2 | 2018-03-29 14:18:44 -0400 | [diff] [blame] | 23 | #include "pipeline/skia/SkiaMemoryTracer.h" |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 24 | #include "Properties.h" |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 25 | #include "renderstate/RenderState.h" |
| 26 | |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 27 | #include <GrContextOptions.h> |
Derek Sollenberger | 8ec9e88 | 2017-08-24 16:36:08 -0400 | [diff] [blame] | 28 | #include <SkExecutor.h> |
Derek Sollenberger | 0057db2 | 2018-03-29 14:18:44 -0400 | [diff] [blame] | 29 | #include <SkGraphics.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 30 | #include <gui/Surface.h> |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 31 | #include <math.h> |
| 32 | #include <set> |
| 33 | |
| 34 | namespace android { |
| 35 | namespace uirenderer { |
| 36 | namespace renderthread { |
| 37 | |
| 38 | // This multiplier was selected based on historical review of cache sizes relative |
| 39 | // to the screen resolution. This is meant to be a conservative default based on |
| 40 | // that analysis. The 4.0f is used because the default pixel format is assumed to |
| 41 | // be ARGB_8888. |
| 42 | #define SURFACE_SIZE_MULTIPLIER (12.0f * 4.0f) |
| 43 | #define BACKGROUND_RETENTION_PERCENTAGE (0.5f) |
| 44 | |
| 45 | // for super large fonts we will draw them as paths so no need to keep linearly |
| 46 | // increasing the font cache size. |
| 47 | #define FONT_CACHE_MIN_MB (0.5f) |
| 48 | #define FONT_CACHE_MAX_MB (4.0f) |
| 49 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 50 | CacheManager::CacheManager(const DisplayInfo& display) : mMaxSurfaceArea(display.w * display.h) { |
| 51 | mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas( |
| 52 | mMaxSurfaceArea / 2, |
Stan Iliev | dd098e8 | 2017-08-09 09:42:38 -0400 | [diff] [blame] | 53 | skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 54 | } |
| 55 | |
Greg Daniel | 660d6ec | 2017-12-08 11:44:27 -0500 | [diff] [blame] | 56 | void CacheManager::reset(sk_sp<GrContext> context) { |
| 57 | if (context != mGrContext) { |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 58 | destroy(); |
| 59 | } |
| 60 | |
| 61 | if (context) { |
Greg Daniel | 660d6ec | 2017-12-08 11:44:27 -0500 | [diff] [blame] | 62 | mGrContext = std::move(context); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 63 | mGrContext->getResourceCacheLimits(&mMaxResources, nullptr); |
| 64 | updateContextCacheSizes(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void CacheManager::destroy() { |
| 69 | // cleanup any caches here as the GrContext is about to go away... |
| 70 | mGrContext.reset(nullptr); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 71 | mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas( |
| 72 | mMaxSurfaceArea / 2, |
| 73 | skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void CacheManager::updateContextCacheSizes() { |
| 77 | mMaxResourceBytes = mMaxSurfaceArea * SURFACE_SIZE_MULTIPLIER; |
| 78 | mBackgroundResourceBytes = mMaxResourceBytes * BACKGROUND_RETENTION_PERCENTAGE; |
| 79 | |
| 80 | mGrContext->setResourceCacheLimits(mMaxResources, mMaxResourceBytes); |
| 81 | } |
| 82 | |
Derek Sollenberger | 8ec9e88 | 2017-08-24 16:36:08 -0400 | [diff] [blame] | 83 | class CacheManager::SkiaTaskProcessor : public TaskProcessor<bool>, public SkExecutor { |
| 84 | public: |
| 85 | explicit SkiaTaskProcessor(TaskManager* taskManager) : TaskProcessor<bool>(taskManager) {} |
| 86 | |
| 87 | // This is really a Task<void> but that doesn't really work when Future<> |
| 88 | // expects to be able to get/set a value |
| 89 | struct SkiaTask : public Task<bool> { |
| 90 | std::function<void()> func; |
| 91 | }; |
| 92 | |
| 93 | virtual void add(std::function<void(void)> func) override { |
| 94 | sp<SkiaTask> task(new SkiaTask()); |
| 95 | task->func = func; |
| 96 | TaskProcessor<bool>::add(task); |
| 97 | } |
| 98 | |
| 99 | virtual void onProcess(const sp<Task<bool> >& task) override { |
| 100 | SkiaTask* t = static_cast<SkiaTask*>(task.get()); |
| 101 | t->func(); |
| 102 | task->setResult(true); |
| 103 | } |
| 104 | }; |
| 105 | |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 106 | void CacheManager::configureContext(GrContextOptions* contextOptions, const void* identity, ssize_t size) { |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 107 | contextOptions->fAllowPathMaskCaching = true; |
| 108 | |
| 109 | float screenMP = mMaxSurfaceArea / 1024.0f / 1024.0f; |
| 110 | float fontCacheMB = 0; |
| 111 | float decimalVal = std::modf(screenMP, &fontCacheMB); |
| 112 | |
| 113 | // This is a basic heuristic to size the cache to a multiple of 512 KB |
| 114 | if (decimalVal > 0.8f) { |
| 115 | fontCacheMB += 1.0f; |
| 116 | } else if (decimalVal > 0.5f) { |
| 117 | fontCacheMB += 0.5f; |
| 118 | } |
| 119 | |
| 120 | // set limits on min/max size of the cache |
| 121 | fontCacheMB = std::max(FONT_CACHE_MIN_MB, std::min(FONT_CACHE_MAX_MB, fontCacheMB)); |
| 122 | |
| 123 | // We must currently set the size of the text cache based on the size of the |
| 124 | // display even though we like to be dynamicallysizing it to the size of the window. |
| 125 | // Skia's implementation doesn't provide a mechanism to resize the font cache due to |
| 126 | // the potential cost of recreating the glyphs. |
| 127 | contextOptions->fGlyphCacheTextureMaximumBytes = fontCacheMB * 1024 * 1024; |
Derek Sollenberger | 8ec9e88 | 2017-08-24 16:36:08 -0400 | [diff] [blame] | 128 | |
| 129 | if (mTaskManager.canRunTasks()) { |
| 130 | if (!mTaskProcessor.get()) { |
| 131 | mTaskProcessor = new SkiaTaskProcessor(&mTaskManager); |
| 132 | } |
| 133 | contextOptions->fExecutor = mTaskProcessor.get(); |
| 134 | } |
Stan Iliev | e75ef1f | 2017-11-27 17:22:42 -0500 | [diff] [blame] | 135 | |
Yichi Chen | 9f95955 | 2018-03-29 21:21:54 +0800 | [diff] [blame] | 136 | auto& cache = skiapipeline::ShaderCache::get(); |
| 137 | cache.initShaderDiskCache(identity, size); |
| 138 | contextOptions->fPersistentCache = &cache; |
Stan Iliev | 5d03348 | 2018-07-03 14:47:59 -0400 | [diff] [blame] | 139 | contextOptions->fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting; |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void CacheManager::trimMemory(TrimMemoryMode mode) { |
| 143 | if (!mGrContext) { |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | mGrContext->flush(); |
| 148 | |
| 149 | switch (mode) { |
| 150 | case TrimMemoryMode::Complete: |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 151 | mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(mMaxSurfaceArea / 2); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 152 | mGrContext->freeGpuResources(); |
| 153 | break; |
| 154 | case TrimMemoryMode::UiHidden: |
Derek Sollenberger | b1f27aa | 2018-04-02 13:36:45 -0400 | [diff] [blame] | 155 | // Here we purge all the unlocked scratch resources and then toggle the resources cache |
| 156 | // limits between the background and max amounts. This causes the unlocked resources |
| 157 | // that have persistent data to be purged in LRU order. |
| 158 | mGrContext->purgeUnlockedResources(true); |
| 159 | mGrContext->setResourceCacheLimits(mMaxResources, mBackgroundResourceBytes); |
| 160 | mGrContext->setResourceCacheLimits(mMaxResources, mMaxResourceBytes); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 161 | break; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void CacheManager::trimStaleResources() { |
| 166 | if (!mGrContext) { |
| 167 | return; |
| 168 | } |
| 169 | mGrContext->flush(); |
Derek Sollenberger | 92a9eb9 | 2018-04-12 13:42:19 -0400 | [diff] [blame] | 170 | mGrContext->purgeResourcesNotUsedInMs(std::chrono::seconds(30)); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 171 | } |
| 172 | |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 173 | sp<skiapipeline::VectorDrawableAtlas> CacheManager::acquireVectorDrawableAtlas() { |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 174 | LOG_ALWAYS_FATAL_IF(mVectorDrawableAtlas.get() == nullptr); |
| 175 | LOG_ALWAYS_FATAL_IF(mGrContext == nullptr); |
| 176 | |
| 177 | /** |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 178 | * TODO: define memory conditions where we clear the cache (e.g. surface->reset()) |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 179 | */ |
Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 180 | return mVectorDrawableAtlas; |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void CacheManager::dumpMemoryUsage(String8& log, const RenderState* renderState) { |
| 184 | if (!mGrContext) { |
| 185 | log.appendFormat("No valid cache instance.\n"); |
| 186 | return; |
| 187 | } |
| 188 | |
Derek Sollenberger | 0057db2 | 2018-03-29 14:18:44 -0400 | [diff] [blame] | 189 | log.appendFormat("Font Cache (CPU):\n"); |
| 190 | log.appendFormat(" Size: %.2f kB \n", SkGraphics::GetFontCacheUsed() / 1024.0f); |
| 191 | log.appendFormat(" Glyph Count: %d \n", SkGraphics::GetFontCacheCountUsed()); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 192 | |
Derek Sollenberger | 0057db2 | 2018-03-29 14:18:44 -0400 | [diff] [blame] | 193 | log.appendFormat("CPU Caches:\n"); |
| 194 | std::vector<skiapipeline::ResourcePair> cpuResourceMap = { |
| 195 | {"skia/sk_resource_cache/bitmap_", "Bitmaps"}, |
| 196 | {"skia/sk_resource_cache/rrect-blur_", "Masks"}, |
| 197 | {"skia/sk_resource_cache/rects-blur_", "Masks"}, |
| 198 | {"skia/sk_resource_cache/tessellated", "Shadows"}, |
| 199 | }; |
| 200 | skiapipeline::SkiaMemoryTracer cpuTracer(cpuResourceMap, false); |
| 201 | SkGraphics::DumpMemoryStatistics(&cpuTracer); |
| 202 | cpuTracer.logOutput(log); |
| 203 | |
| 204 | log.appendFormat("GPU Caches:\n"); |
| 205 | skiapipeline::SkiaMemoryTracer gpuTracer("category", true); |
| 206 | mGrContext->dumpMemoryStatistics(&gpuTracer); |
| 207 | gpuTracer.logOutput(log); |
| 208 | |
| 209 | log.appendFormat("Other Caches:\n"); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 210 | log.appendFormat(" Current / Maximum\n"); |
Derek Sollenberger | 0057db2 | 2018-03-29 14:18:44 -0400 | [diff] [blame] | 211 | log.appendFormat(" VectorDrawableAtlas %6.2f kB / %6.2f KB (entries = %zu)\n", 0.0f, 0.0f, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 212 | (size_t)0); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 213 | |
| 214 | if (renderState) { |
| 215 | if (renderState->mActiveLayers.size() > 0) { |
| 216 | log.appendFormat(" Layer Info:\n"); |
| 217 | } |
| 218 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 219 | const char* layerType = Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL |
| 220 | ? "GlLayer" : "VkLayer"; |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 221 | size_t layerMemoryTotal = 0; |
| 222 | for (std::set<Layer*>::iterator it = renderState->mActiveLayers.begin(); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 223 | it != renderState->mActiveLayers.end(); it++) { |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 224 | const Layer* layer = *it; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 225 | log.appendFormat(" %s size %dx%d\n", layerType, layer->getWidth(), |
| 226 | layer->getHeight()); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 227 | layerMemoryTotal += layer->getWidth() * layer->getHeight() * 4; |
| 228 | } |
Derek Sollenberger | 0057db2 | 2018-03-29 14:18:44 -0400 | [diff] [blame] | 229 | log.appendFormat(" Layers Total %6.2f KB (numLayers = %zu)\n", |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 230 | layerMemoryTotal / 1024.0f, renderState->mActiveLayers.size()); |
| 231 | } |
| 232 | |
Derek Sollenberger | 0057db2 | 2018-03-29 14:18:44 -0400 | [diff] [blame] | 233 | log.appendFormat("Total GPU memory usage:\n"); |
| 234 | gpuTracer.logTotals(log); |
Derek Sollenberger | f9e45d1 | 2017-06-01 13:07:39 -0400 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | } /* namespace renderthread */ |
| 238 | } /* namespace uirenderer */ |
| 239 | } /* namespace android */ |