blob: f0d6b3860938e9c2d9417db01bad45f724fd509e [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
23#include <gui/Surface.h>
24#include <GrContextOptions.h>
25#include <math.h>
26#include <set>
27
28namespace android {
29namespace uirenderer {
30namespace renderthread {
31
32// This multiplier was selected based on historical review of cache sizes relative
33// to the screen resolution. This is meant to be a conservative default based on
34// that analysis. The 4.0f is used because the default pixel format is assumed to
35// be ARGB_8888.
36#define SURFACE_SIZE_MULTIPLIER (12.0f * 4.0f)
37#define BACKGROUND_RETENTION_PERCENTAGE (0.5f)
38
39// for super large fonts we will draw them as paths so no need to keep linearly
40// increasing the font cache size.
41#define FONT_CACHE_MIN_MB (0.5f)
42#define FONT_CACHE_MAX_MB (4.0f)
43
44CacheManager::CacheManager(const DisplayInfo& display)
45 : mMaxSurfaceArea(display.w * display.h) {
46 mVectorDrawableAtlas.reset(new VectorDrawableAtlas);
47}
48
49void CacheManager::reset(GrContext* context) {
50 if (context != mGrContext.get()) {
51 destroy();
52 }
53
54 if (context) {
55 mGrContext = sk_ref_sp(context);
56 mGrContext->getResourceCacheLimits(&mMaxResources, nullptr);
57 updateContextCacheSizes();
58 }
59}
60
61void CacheManager::destroy() {
62 // cleanup any caches here as the GrContext is about to go away...
63 mGrContext.reset(nullptr);
64 mVectorDrawableAtlas.reset(new VectorDrawableAtlas);
65}
66
67void CacheManager::updateContextCacheSizes() {
68 mMaxResourceBytes = mMaxSurfaceArea * SURFACE_SIZE_MULTIPLIER;
69 mBackgroundResourceBytes = mMaxResourceBytes * BACKGROUND_RETENTION_PERCENTAGE;
70
71 mGrContext->setResourceCacheLimits(mMaxResources, mMaxResourceBytes);
72}
73
74void CacheManager::configureContext(GrContextOptions* contextOptions) {
75 contextOptions->fAllowPathMaskCaching = true;
76
77 float screenMP = mMaxSurfaceArea / 1024.0f / 1024.0f;
78 float fontCacheMB = 0;
79 float decimalVal = std::modf(screenMP, &fontCacheMB);
80
81 // This is a basic heuristic to size the cache to a multiple of 512 KB
82 if (decimalVal > 0.8f) {
83 fontCacheMB += 1.0f;
84 } else if (decimalVal > 0.5f) {
85 fontCacheMB += 0.5f;
86 }
87
88 // set limits on min/max size of the cache
89 fontCacheMB = std::max(FONT_CACHE_MIN_MB, std::min(FONT_CACHE_MAX_MB, fontCacheMB));
90
91 // We must currently set the size of the text cache based on the size of the
92 // display even though we like to be dynamicallysizing it to the size of the window.
93 // Skia's implementation doesn't provide a mechanism to resize the font cache due to
94 // the potential cost of recreating the glyphs.
95 contextOptions->fGlyphCacheTextureMaximumBytes = fontCacheMB * 1024 * 1024;
96}
97
98void CacheManager::trimMemory(TrimMemoryMode mode) {
99 if (!mGrContext) {
100 return;
101 }
102
103 mGrContext->flush();
104
105 switch (mode) {
106 case TrimMemoryMode::Complete:
107 mVectorDrawableAtlas.reset(new VectorDrawableAtlas);
108 mGrContext->freeGpuResources();
109 break;
110 case TrimMemoryMode::UiHidden:
111 mGrContext->purgeUnlockedResources(mMaxResourceBytes - mBackgroundResourceBytes, true);
112 break;
113 }
114}
115
116void CacheManager::trimStaleResources() {
117 if (!mGrContext) {
118 return;
119 }
120 mGrContext->flush();
121 mGrContext->purgeResourcesNotUsedInMs(std::chrono::seconds(30));
122}
123
124VectorDrawableAtlas* CacheManager::acquireVectorDrawableAtlas() {
125 LOG_ALWAYS_FATAL_IF(mVectorDrawableAtlas.get() == nullptr);
126 LOG_ALWAYS_FATAL_IF(mGrContext == nullptr);
127
128 /**
129 * TODO LIST:
130 * 1) compute the atlas based on the surfaceArea surface
131 * 2) identify a way to reuse cache entries
132 * 3) add ability to repack the cache?
133 * 4) define memory conditions where we clear the cache (e.g. surface->reset())
134 */
135
136 return mVectorDrawableAtlas.release();
137}
138void CacheManager::releaseVectorDrawableAtlas(VectorDrawableAtlas* atlas) {
139 LOG_ALWAYS_FATAL_IF(mVectorDrawableAtlas.get() != nullptr);
140 mVectorDrawableAtlas.reset(atlas);
141 mVectorDrawableAtlas->isNewAtlas = false;
142}
143
144void CacheManager::dumpMemoryUsage(String8& log, const RenderState* renderState) {
145 if (!mGrContext) {
146 log.appendFormat("No valid cache instance.\n");
147 return;
148 }
149
150 size_t bytesCached;
151 mGrContext->getResourceCacheUsage(nullptr, &bytesCached);
152
153 log.appendFormat("Caches:\n");
154 log.appendFormat(" Current / Maximum\n");
155 log.appendFormat(" VectorDrawableAtlas %6.2f kB / %6.2f kB (entries = %zu)\n",
156 0.0f, 0.0f, (size_t)0);
157
158 if (renderState) {
159 if (renderState->mActiveLayers.size() > 0) {
160 log.appendFormat(" Layer Info:\n");
161 }
162
163 size_t layerMemoryTotal = 0;
164 for (std::set<Layer*>::iterator it = renderState->mActiveLayers.begin();
165 it != renderState->mActiveLayers.end(); it++) {
166 const Layer* layer = *it;
167 const char* layerType = layer->getApi() == Layer::Api::OpenGL ? "GlLayer" : "VkLayer";
168 log.appendFormat(" %s size %dx%d\n", layerType,
169 layer->getWidth(), layer->getHeight());
170 layerMemoryTotal += layer->getWidth() * layer->getHeight() * 4;
171 }
172 log.appendFormat(" Layers Total %6.2f kB (numLayers = %zu)\n",
173 layerMemoryTotal / 1024.0f, renderState->mActiveLayers.size());
174 }
175
176
177 log.appendFormat("Total memory usage:\n");
178 log.appendFormat(" %zu bytes, %.2f MB (%.2f MB is purgeable)\n",
179 bytesCached, bytesCached / 1024.0f / 1024.0f,
180 mGrContext->getResourceCachePurgeableBytes() / 1024.0f / 1024.0f);
181
182
183}
184
185} /* namespace renderthread */
186} /* namespace uirenderer */
187} /* namespace android */