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