blob: f6b23e1a07233dae4db525085ad9a0db1e30ccf3 [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) {
Stan Iliev3310fb12017-03-23 16:56:51 -040046 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(mMaxSurfaceArea/2,
Stan Ilievdd098e82017-08-09 09:42:38 -040047 skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040048}
49
50void CacheManager::reset(GrContext* context) {
51 if (context != mGrContext.get()) {
52 destroy();
53 }
54
55 if (context) {
56 mGrContext = sk_ref_sp(context);
57 mGrContext->getResourceCacheLimits(&mMaxResources, nullptr);
58 updateContextCacheSizes();
59 }
60}
61
62void CacheManager::destroy() {
63 // cleanup any caches here as the GrContext is about to go away...
64 mGrContext.reset(nullptr);
Stan Ilievdd098e82017-08-09 09:42:38 -040065 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(mMaxSurfaceArea/2,
66 skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040067}
68
69void CacheManager::updateContextCacheSizes() {
70 mMaxResourceBytes = mMaxSurfaceArea * SURFACE_SIZE_MULTIPLIER;
71 mBackgroundResourceBytes = mMaxResourceBytes * BACKGROUND_RETENTION_PERCENTAGE;
72
73 mGrContext->setResourceCacheLimits(mMaxResources, mMaxResourceBytes);
74}
75
76void CacheManager::configureContext(GrContextOptions* contextOptions) {
77 contextOptions->fAllowPathMaskCaching = true;
78
79 float screenMP = mMaxSurfaceArea / 1024.0f / 1024.0f;
80 float fontCacheMB = 0;
81 float decimalVal = std::modf(screenMP, &fontCacheMB);
82
83 // This is a basic heuristic to size the cache to a multiple of 512 KB
84 if (decimalVal > 0.8f) {
85 fontCacheMB += 1.0f;
86 } else if (decimalVal > 0.5f) {
87 fontCacheMB += 0.5f;
88 }
89
90 // set limits on min/max size of the cache
91 fontCacheMB = std::max(FONT_CACHE_MIN_MB, std::min(FONT_CACHE_MAX_MB, fontCacheMB));
92
93 // We must currently set the size of the text cache based on the size of the
94 // display even though we like to be dynamicallysizing it to the size of the window.
95 // Skia's implementation doesn't provide a mechanism to resize the font cache due to
96 // the potential cost of recreating the glyphs.
97 contextOptions->fGlyphCacheTextureMaximumBytes = fontCacheMB * 1024 * 1024;
98}
99
100void CacheManager::trimMemory(TrimMemoryMode mode) {
101 if (!mGrContext) {
102 return;
103 }
104
105 mGrContext->flush();
106
107 switch (mode) {
108 case TrimMemoryMode::Complete:
Stan Iliev3310fb12017-03-23 16:56:51 -0400109 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(mMaxSurfaceArea/2);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400110 mGrContext->freeGpuResources();
111 break;
112 case TrimMemoryMode::UiHidden:
113 mGrContext->purgeUnlockedResources(mMaxResourceBytes - mBackgroundResourceBytes, true);
114 break;
115 }
116}
117
118void CacheManager::trimStaleResources() {
119 if (!mGrContext) {
120 return;
121 }
122 mGrContext->flush();
123 mGrContext->purgeResourcesNotUsedInMs(std::chrono::seconds(30));
124}
125
Stan Iliev3310fb12017-03-23 16:56:51 -0400126sp<skiapipeline::VectorDrawableAtlas> CacheManager::acquireVectorDrawableAtlas() {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400127 LOG_ALWAYS_FATAL_IF(mVectorDrawableAtlas.get() == nullptr);
128 LOG_ALWAYS_FATAL_IF(mGrContext == nullptr);
129
130 /**
Stan Iliev3310fb12017-03-23 16:56:51 -0400131 * TODO: define memory conditions where we clear the cache (e.g. surface->reset())
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400132 */
Stan Iliev3310fb12017-03-23 16:56:51 -0400133 return mVectorDrawableAtlas;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400134}
135
136void CacheManager::dumpMemoryUsage(String8& log, const RenderState* renderState) {
137 if (!mGrContext) {
138 log.appendFormat("No valid cache instance.\n");
139 return;
140 }
141
142 size_t bytesCached;
143 mGrContext->getResourceCacheUsage(nullptr, &bytesCached);
144
145 log.appendFormat("Caches:\n");
146 log.appendFormat(" Current / Maximum\n");
147 log.appendFormat(" VectorDrawableAtlas %6.2f kB / %6.2f kB (entries = %zu)\n",
148 0.0f, 0.0f, (size_t)0);
149
150 if (renderState) {
151 if (renderState->mActiveLayers.size() > 0) {
152 log.appendFormat(" Layer Info:\n");
153 }
154
155 size_t layerMemoryTotal = 0;
156 for (std::set<Layer*>::iterator it = renderState->mActiveLayers.begin();
157 it != renderState->mActiveLayers.end(); it++) {
158 const Layer* layer = *it;
159 const char* layerType = layer->getApi() == Layer::Api::OpenGL ? "GlLayer" : "VkLayer";
160 log.appendFormat(" %s size %dx%d\n", layerType,
161 layer->getWidth(), layer->getHeight());
162 layerMemoryTotal += layer->getWidth() * layer->getHeight() * 4;
163 }
164 log.appendFormat(" Layers Total %6.2f kB (numLayers = %zu)\n",
165 layerMemoryTotal / 1024.0f, renderState->mActiveLayers.size());
166 }
167
168
169 log.appendFormat("Total memory usage:\n");
170 log.appendFormat(" %zu bytes, %.2f MB (%.2f MB is purgeable)\n",
171 bytesCached, bytesCached / 1024.0f / 1024.0f,
172 mGrContext->getResourceCachePurgeableBytes() / 1024.0f / 1024.0f);
173
174
175}
176
177} /* namespace renderthread */
178} /* namespace uirenderer */
179} /* namespace android */