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