blob: 3ca92953e5f7224dc38ced9509ada0186e4826db [file] [log] [blame]
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -04001/*
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 Ilieve75ef1f2017-11-27 17:22:42 -050020#include "Properties.h"
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040021#include "RenderThread.h"
Stan Ilieve75ef1f2017-11-27 17:22:42 -050022#include "pipeline/skia/ShaderCache.h"
Derek Sollenberger0057db22018-03-29 14:18:44 -040023#include "pipeline/skia/SkiaMemoryTracer.h"
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040024#include "renderstate/RenderState.h"
25
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040026#include <GrContextOptions.h>
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040027#include <SkExecutor.h>
Derek Sollenberger0057db22018-03-29 14:18:44 -040028#include <SkGraphics.h>
John Reck1bcacfd2017-11-03 10:12:19 -070029#include <gui/Surface.h>
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040030#include <math.h>
31#include <set>
32
33namespace android {
34namespace uirenderer {
35namespace 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 Reck1bcacfd2017-11-03 10:12:19 -070049CacheManager::CacheManager(const DisplayInfo& display) : mMaxSurfaceArea(display.w * display.h) {
50 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(
51 mMaxSurfaceArea / 2,
Stan Ilievdd098e82017-08-09 09:42:38 -040052 skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface);
Stan Ilieve75ef1f2017-11-27 17:22:42 -050053 if (Properties::isSkiaEnabled()) {
54 skiapipeline::ShaderCache::get().initShaderDiskCache();
55 }
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040056}
57
Greg Daniel660d6ec2017-12-08 11:44:27 -050058void CacheManager::reset(sk_sp<GrContext> context) {
59 if (context != mGrContext) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040060 destroy();
61 }
62
63 if (context) {
Greg Daniel660d6ec2017-12-08 11:44:27 -050064 mGrContext = std::move(context);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040065 mGrContext->getResourceCacheLimits(&mMaxResources, nullptr);
66 updateContextCacheSizes();
67 }
68}
69
70void CacheManager::destroy() {
71 // cleanup any caches here as the GrContext is about to go away...
72 mGrContext.reset(nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -070073 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(
74 mMaxSurfaceArea / 2,
75 skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040076}
77
78void CacheManager::updateContextCacheSizes() {
79 mMaxResourceBytes = mMaxSurfaceArea * SURFACE_SIZE_MULTIPLIER;
80 mBackgroundResourceBytes = mMaxResourceBytes * BACKGROUND_RETENTION_PERCENTAGE;
81
82 mGrContext->setResourceCacheLimits(mMaxResources, mMaxResourceBytes);
83}
84
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040085class CacheManager::SkiaTaskProcessor : public TaskProcessor<bool>, public SkExecutor {
86public:
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 Sollenbergerf9e45d12017-06-01 13:07:39 -0400108void 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 Sollenberger8ec9e882017-08-24 16:36:08 -0400130
131 if (mTaskManager.canRunTasks()) {
132 if (!mTaskProcessor.get()) {
133 mTaskProcessor = new SkiaTaskProcessor(&mTaskManager);
134 }
135 contextOptions->fExecutor = mTaskProcessor.get();
136 }
Stan Ilieve75ef1f2017-11-27 17:22:42 -0500137
138 contextOptions->fPersistentCache = &skiapipeline::ShaderCache::get();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400139}
140
141void CacheManager::trimMemory(TrimMemoryMode mode) {
142 if (!mGrContext) {
143 return;
144 }
145
146 mGrContext->flush();
147
148 switch (mode) {
149 case TrimMemoryMode::Complete:
John Reck1bcacfd2017-11-03 10:12:19 -0700150 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(mMaxSurfaceArea / 2);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400151 mGrContext->freeGpuResources();
152 break;
153 case TrimMemoryMode::UiHidden:
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -0400154 // 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 Sollenbergerf9e45d12017-06-01 13:07:39 -0400160 break;
161 }
162}
163
164void CacheManager::trimStaleResources() {
165 if (!mGrContext) {
166 return;
167 }
168 mGrContext->flush();
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -0400169 // 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 Sollenbergerf9e45d12017-06-01 13:07:39 -0400173}
174
Stan Iliev3310fb12017-03-23 16:56:51 -0400175sp<skiapipeline::VectorDrawableAtlas> CacheManager::acquireVectorDrawableAtlas() {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400176 LOG_ALWAYS_FATAL_IF(mVectorDrawableAtlas.get() == nullptr);
177 LOG_ALWAYS_FATAL_IF(mGrContext == nullptr);
178
179 /**
Stan Iliev3310fb12017-03-23 16:56:51 -0400180 * TODO: define memory conditions where we clear the cache (e.g. surface->reset())
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400181 */
Stan Iliev3310fb12017-03-23 16:56:51 -0400182 return mVectorDrawableAtlas;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400183}
184
185void CacheManager::dumpMemoryUsage(String8& log, const RenderState* renderState) {
186 if (!mGrContext) {
187 log.appendFormat("No valid cache instance.\n");
188 return;
189 }
190
Derek Sollenberger0057db22018-03-29 14:18:44 -0400191 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 Sollenbergerf9e45d12017-06-01 13:07:39 -0400194
Derek Sollenberger0057db22018-03-29 14:18:44 -0400195 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 Sollenbergerf9e45d12017-06-01 13:07:39 -0400212 log.appendFormat(" Current / Maximum\n");
Derek Sollenberger0057db22018-03-29 14:18:44 -0400213 log.appendFormat(" VectorDrawableAtlas %6.2f kB / %6.2f KB (entries = %zu)\n", 0.0f, 0.0f,
John Reck1bcacfd2017-11-03 10:12:19 -0700214 (size_t)0);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400215
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 Reck1bcacfd2017-11-03 10:12:19 -0700223 it != renderState->mActiveLayers.end(); it++) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400224 const Layer* layer = *it;
225 const char* layerType = layer->getApi() == Layer::Api::OpenGL ? "GlLayer" : "VkLayer";
John Reck1bcacfd2017-11-03 10:12:19 -0700226 log.appendFormat(" %s size %dx%d\n", layerType, layer->getWidth(),
227 layer->getHeight());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400228 layerMemoryTotal += layer->getWidth() * layer->getHeight() * 4;
229 }
Derek Sollenberger0057db22018-03-29 14:18:44 -0400230 log.appendFormat(" Layers Total %6.2f KB (numLayers = %zu)\n",
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400231 layerMemoryTotal / 1024.0f, renderState->mActiveLayers.size());
232 }
233
Derek Sollenberger0057db22018-03-29 14:18:44 -0400234 log.appendFormat("Total GPU memory usage:\n");
235 gpuTracer.logTotals(log);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400236}
237
238} /* namespace renderthread */
239} /* namespace uirenderer */
240} /* namespace android */