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