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