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