blob: a33b2874e7a506088df6c67cc2e83a6f086b528d [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"
21#include "renderstate/RenderState.h"
22
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040023#include <GrContextOptions.h>
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040024#include <SkExecutor.h>
John Reck1bcacfd2017-11-03 10:12:19 -070025#include <gui/Surface.h>
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040026#include <math.h>
27#include <set>
28
29namespace android {
30namespace uirenderer {
31namespace 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
John Reck1bcacfd2017-11-03 10:12:19 -070045CacheManager::CacheManager(const DisplayInfo& display) : mMaxSurfaceArea(display.w * display.h) {
46 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(
47 mMaxSurfaceArea / 2,
Stan Ilievdd098e82017-08-09 09:42:38 -040048 skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040049}
50
51void 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
63void CacheManager::destroy() {
64 // cleanup any caches here as the GrContext is about to go away...
65 mGrContext.reset(nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -070066 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(
67 mMaxSurfaceArea / 2,
68 skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040069}
70
71void CacheManager::updateContextCacheSizes() {
72 mMaxResourceBytes = mMaxSurfaceArea * SURFACE_SIZE_MULTIPLIER;
73 mBackgroundResourceBytes = mMaxResourceBytes * BACKGROUND_RETENTION_PERCENTAGE;
74
75 mGrContext->setResourceCacheLimits(mMaxResources, mMaxResourceBytes);
76}
77
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040078class CacheManager::SkiaTaskProcessor : public TaskProcessor<bool>, public SkExecutor {
79public:
80 explicit SkiaTaskProcessor(TaskManager* taskManager) : TaskProcessor<bool>(taskManager) {}
81
82 // This is really a Task<void> but that doesn't really work when Future<>
83 // expects to be able to get/set a value
84 struct SkiaTask : public Task<bool> {
85 std::function<void()> func;
86 };
87
88 virtual void add(std::function<void(void)> func) override {
89 sp<SkiaTask> task(new SkiaTask());
90 task->func = func;
91 TaskProcessor<bool>::add(task);
92 }
93
94 virtual void onProcess(const sp<Task<bool> >& task) override {
95 SkiaTask* t = static_cast<SkiaTask*>(task.get());
96 t->func();
97 task->setResult(true);
98 }
99};
100
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400101void CacheManager::configureContext(GrContextOptions* contextOptions) {
102 contextOptions->fAllowPathMaskCaching = true;
103
104 float screenMP = mMaxSurfaceArea / 1024.0f / 1024.0f;
105 float fontCacheMB = 0;
106 float decimalVal = std::modf(screenMP, &fontCacheMB);
107
108 // This is a basic heuristic to size the cache to a multiple of 512 KB
109 if (decimalVal > 0.8f) {
110 fontCacheMB += 1.0f;
111 } else if (decimalVal > 0.5f) {
112 fontCacheMB += 0.5f;
113 }
114
115 // set limits on min/max size of the cache
116 fontCacheMB = std::max(FONT_CACHE_MIN_MB, std::min(FONT_CACHE_MAX_MB, fontCacheMB));
117
118 // We must currently set the size of the text cache based on the size of the
119 // display even though we like to be dynamicallysizing it to the size of the window.
120 // Skia's implementation doesn't provide a mechanism to resize the font cache due to
121 // the potential cost of recreating the glyphs.
122 contextOptions->fGlyphCacheTextureMaximumBytes = fontCacheMB * 1024 * 1024;
Derek Sollenberger8ec9e882017-08-24 16:36:08 -0400123
124 if (mTaskManager.canRunTasks()) {
125 if (!mTaskProcessor.get()) {
126 mTaskProcessor = new SkiaTaskProcessor(&mTaskManager);
127 }
128 contextOptions->fExecutor = mTaskProcessor.get();
129 }
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400130}
131
132void CacheManager::trimMemory(TrimMemoryMode mode) {
133 if (!mGrContext) {
134 return;
135 }
136
137 mGrContext->flush();
138
139 switch (mode) {
140 case TrimMemoryMode::Complete:
John Reck1bcacfd2017-11-03 10:12:19 -0700141 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(mMaxSurfaceArea / 2);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400142 mGrContext->freeGpuResources();
143 break;
144 case TrimMemoryMode::UiHidden:
145 mGrContext->purgeUnlockedResources(mMaxResourceBytes - mBackgroundResourceBytes, true);
146 break;
147 }
148}
149
150void CacheManager::trimStaleResources() {
151 if (!mGrContext) {
152 return;
153 }
154 mGrContext->flush();
155 mGrContext->purgeResourcesNotUsedInMs(std::chrono::seconds(30));
156}
157
Stan Iliev3310fb12017-03-23 16:56:51 -0400158sp<skiapipeline::VectorDrawableAtlas> CacheManager::acquireVectorDrawableAtlas() {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400159 LOG_ALWAYS_FATAL_IF(mVectorDrawableAtlas.get() == nullptr);
160 LOG_ALWAYS_FATAL_IF(mGrContext == nullptr);
161
162 /**
Stan Iliev3310fb12017-03-23 16:56:51 -0400163 * TODO: define memory conditions where we clear the cache (e.g. surface->reset())
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400164 */
Stan Iliev3310fb12017-03-23 16:56:51 -0400165 return mVectorDrawableAtlas;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400166}
167
168void CacheManager::dumpMemoryUsage(String8& log, const RenderState* renderState) {
169 if (!mGrContext) {
170 log.appendFormat("No valid cache instance.\n");
171 return;
172 }
173
174 size_t bytesCached;
175 mGrContext->getResourceCacheUsage(nullptr, &bytesCached);
176
177 log.appendFormat("Caches:\n");
178 log.appendFormat(" Current / Maximum\n");
John Reck1bcacfd2017-11-03 10:12:19 -0700179 log.appendFormat(" VectorDrawableAtlas %6.2f kB / %6.2f kB (entries = %zu)\n", 0.0f, 0.0f,
180 (size_t)0);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400181
182 if (renderState) {
183 if (renderState->mActiveLayers.size() > 0) {
184 log.appendFormat(" Layer Info:\n");
185 }
186
187 size_t layerMemoryTotal = 0;
188 for (std::set<Layer*>::iterator it = renderState->mActiveLayers.begin();
John Reck1bcacfd2017-11-03 10:12:19 -0700189 it != renderState->mActiveLayers.end(); it++) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400190 const Layer* layer = *it;
191 const char* layerType = layer->getApi() == Layer::Api::OpenGL ? "GlLayer" : "VkLayer";
John Reck1bcacfd2017-11-03 10:12:19 -0700192 log.appendFormat(" %s size %dx%d\n", layerType, layer->getWidth(),
193 layer->getHeight());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400194 layerMemoryTotal += layer->getWidth() * layer->getHeight() * 4;
195 }
196 log.appendFormat(" Layers Total %6.2f kB (numLayers = %zu)\n",
197 layerMemoryTotal / 1024.0f, renderState->mActiveLayers.size());
198 }
199
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400200 log.appendFormat("Total memory usage:\n");
John Reck1bcacfd2017-11-03 10:12:19 -0700201 log.appendFormat(" %zu bytes, %.2f MB (%.2f MB is purgeable)\n", bytesCached,
202 bytesCached / 1024.0f / 1024.0f,
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400203 mGrContext->getResourceCachePurgeableBytes() / 1024.0f / 1024.0f);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400204}
205
206} /* namespace renderthread */
207} /* namespace uirenderer */
208} /* namespace android */