blob: 32d4c77c5fc312d7565a61ab98d6b29616dc83fe [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:
154 mGrContext->purgeUnlockedResources(mMaxResourceBytes - mBackgroundResourceBytes, true);
155 break;
156 }
157}
158
159void CacheManager::trimStaleResources() {
160 if (!mGrContext) {
161 return;
162 }
163 mGrContext->flush();
164 mGrContext->purgeResourcesNotUsedInMs(std::chrono::seconds(30));
165}
166
Stan Iliev3310fb12017-03-23 16:56:51 -0400167sp<skiapipeline::VectorDrawableAtlas> CacheManager::acquireVectorDrawableAtlas() {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400168 LOG_ALWAYS_FATAL_IF(mVectorDrawableAtlas.get() == nullptr);
169 LOG_ALWAYS_FATAL_IF(mGrContext == nullptr);
170
171 /**
Stan Iliev3310fb12017-03-23 16:56:51 -0400172 * TODO: define memory conditions where we clear the cache (e.g. surface->reset())
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400173 */
Stan Iliev3310fb12017-03-23 16:56:51 -0400174 return mVectorDrawableAtlas;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400175}
176
177void CacheManager::dumpMemoryUsage(String8& log, const RenderState* renderState) {
178 if (!mGrContext) {
179 log.appendFormat("No valid cache instance.\n");
180 return;
181 }
182
Derek Sollenberger0057db22018-03-29 14:18:44 -0400183 log.appendFormat("Font Cache (CPU):\n");
184 log.appendFormat(" Size: %.2f kB \n", SkGraphics::GetFontCacheUsed() / 1024.0f);
185 log.appendFormat(" Glyph Count: %d \n", SkGraphics::GetFontCacheCountUsed());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400186
Derek Sollenberger0057db22018-03-29 14:18:44 -0400187 log.appendFormat("CPU Caches:\n");
188 std::vector<skiapipeline::ResourcePair> cpuResourceMap = {
189 {"skia/sk_resource_cache/bitmap_", "Bitmaps"},
190 {"skia/sk_resource_cache/rrect-blur_", "Masks"},
191 {"skia/sk_resource_cache/rects-blur_", "Masks"},
192 {"skia/sk_resource_cache/tessellated", "Shadows"},
193 };
194 skiapipeline::SkiaMemoryTracer cpuTracer(cpuResourceMap, false);
195 SkGraphics::DumpMemoryStatistics(&cpuTracer);
196 cpuTracer.logOutput(log);
197
198 log.appendFormat("GPU Caches:\n");
199 skiapipeline::SkiaMemoryTracer gpuTracer("category", true);
200 mGrContext->dumpMemoryStatistics(&gpuTracer);
201 gpuTracer.logOutput(log);
202
203 log.appendFormat("Other Caches:\n");
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400204 log.appendFormat(" Current / Maximum\n");
Derek Sollenberger0057db22018-03-29 14:18:44 -0400205 log.appendFormat(" VectorDrawableAtlas %6.2f kB / %6.2f KB (entries = %zu)\n", 0.0f, 0.0f,
John Reck1bcacfd2017-11-03 10:12:19 -0700206 (size_t)0);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400207
208 if (renderState) {
209 if (renderState->mActiveLayers.size() > 0) {
210 log.appendFormat(" Layer Info:\n");
211 }
212
213 size_t layerMemoryTotal = 0;
214 for (std::set<Layer*>::iterator it = renderState->mActiveLayers.begin();
John Reck1bcacfd2017-11-03 10:12:19 -0700215 it != renderState->mActiveLayers.end(); it++) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400216 const Layer* layer = *it;
217 const char* layerType = layer->getApi() == Layer::Api::OpenGL ? "GlLayer" : "VkLayer";
John Reck1bcacfd2017-11-03 10:12:19 -0700218 log.appendFormat(" %s size %dx%d\n", layerType, layer->getWidth(),
219 layer->getHeight());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400220 layerMemoryTotal += layer->getWidth() * layer->getHeight() * 4;
221 }
Derek Sollenberger0057db22018-03-29 14:18:44 -0400222 log.appendFormat(" Layers Total %6.2f KB (numLayers = %zu)\n",
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400223 layerMemoryTotal / 1024.0f, renderState->mActiveLayers.size());
224 }
225
Derek Sollenberger0057db22018-03-29 14:18:44 -0400226 log.appendFormat("Total GPU memory usage:\n");
227 gpuTracer.logTotals(log);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400228}
229
230} /* namespace renderthread */
231} /* namespace uirenderer */
232} /* namespace android */