blob: 82bfc49435262882537a9407c0306a43292a3354 [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 Sollenberger0057db22018-03-29 14:18:44 -040023#include "pipeline/skia/SkiaMemoryTracer.h"
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040024#include "renderstate/RenderState.h"
25
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040026#include <GrContextOptions.h>
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040027#include <SkExecutor.h>
Derek Sollenberger0057db22018-03-29 14:18:44 -040028#include <SkGraphics.h>
John Reck1bcacfd2017-11-03 10:12:19 -070029#include <gui/Surface.h>
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040030#include <math.h>
31#include <set>
32
33namespace android {
34namespace uirenderer {
35namespace renderthread {
36
37// This multiplier was selected based on historical review of cache sizes relative
38// to the screen resolution. This is meant to be a conservative default based on
39// that analysis. The 4.0f is used because the default pixel format is assumed to
40// be ARGB_8888.
41#define SURFACE_SIZE_MULTIPLIER (12.0f * 4.0f)
42#define BACKGROUND_RETENTION_PERCENTAGE (0.5f)
43
44// for super large fonts we will draw them as paths so no need to keep linearly
45// increasing the font cache size.
46#define FONT_CACHE_MIN_MB (0.5f)
47#define FONT_CACHE_MAX_MB (4.0f)
48
John Reck1bcacfd2017-11-03 10:12:19 -070049CacheManager::CacheManager(const DisplayInfo& display) : mMaxSurfaceArea(display.w * display.h) {
50 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(
51 mMaxSurfaceArea / 2,
Stan Ilievdd098e82017-08-09 09:42:38 -040052 skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040053}
54
Greg Daniel660d6ec2017-12-08 11:44:27 -050055void CacheManager::reset(sk_sp<GrContext> context) {
56 if (context != mGrContext) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040057 destroy();
58 }
59
60 if (context) {
Greg Daniel660d6ec2017-12-08 11:44:27 -050061 mGrContext = std::move(context);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040062 mGrContext->getResourceCacheLimits(&mMaxResources, nullptr);
63 updateContextCacheSizes();
64 }
65}
66
67void CacheManager::destroy() {
68 // cleanup any caches here as the GrContext is about to go away...
69 mGrContext.reset(nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -070070 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(
71 mMaxSurfaceArea / 2,
72 skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040073}
74
75void CacheManager::updateContextCacheSizes() {
76 mMaxResourceBytes = mMaxSurfaceArea * SURFACE_SIZE_MULTIPLIER;
77 mBackgroundResourceBytes = mMaxResourceBytes * BACKGROUND_RETENTION_PERCENTAGE;
78
79 mGrContext->setResourceCacheLimits(mMaxResources, mMaxResourceBytes);
80}
81
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040082class CacheManager::SkiaTaskProcessor : public TaskProcessor<bool>, public SkExecutor {
83public:
84 explicit SkiaTaskProcessor(TaskManager* taskManager) : TaskProcessor<bool>(taskManager) {}
85
86 // This is really a Task<void> but that doesn't really work when Future<>
87 // expects to be able to get/set a value
88 struct SkiaTask : public Task<bool> {
89 std::function<void()> func;
90 };
91
92 virtual void add(std::function<void(void)> func) override {
93 sp<SkiaTask> task(new SkiaTask());
94 task->func = func;
95 TaskProcessor<bool>::add(task);
96 }
97
98 virtual void onProcess(const sp<Task<bool> >& task) override {
99 SkiaTask* t = static_cast<SkiaTask*>(task.get());
100 t->func();
101 task->setResult(true);
102 }
103};
104
Yichi Chen9f959552018-03-29 21:21:54 +0800105void CacheManager::configureContext(GrContextOptions* contextOptions, const void* identity, ssize_t size) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400106 contextOptions->fAllowPathMaskCaching = true;
107
108 float screenMP = mMaxSurfaceArea / 1024.0f / 1024.0f;
109 float fontCacheMB = 0;
110 float decimalVal = std::modf(screenMP, &fontCacheMB);
111
112 // This is a basic heuristic to size the cache to a multiple of 512 KB
113 if (decimalVal > 0.8f) {
114 fontCacheMB += 1.0f;
115 } else if (decimalVal > 0.5f) {
116 fontCacheMB += 0.5f;
117 }
118
119 // set limits on min/max size of the cache
120 fontCacheMB = std::max(FONT_CACHE_MIN_MB, std::min(FONT_CACHE_MAX_MB, fontCacheMB));
121
122 // We must currently set the size of the text cache based on the size of the
123 // display even though we like to be dynamicallysizing it to the size of the window.
124 // Skia's implementation doesn't provide a mechanism to resize the font cache due to
125 // the potential cost of recreating the glyphs.
126 contextOptions->fGlyphCacheTextureMaximumBytes = fontCacheMB * 1024 * 1024;
Derek Sollenberger8ec9e882017-08-24 16:36:08 -0400127
128 if (mTaskManager.canRunTasks()) {
129 if (!mTaskProcessor.get()) {
130 mTaskProcessor = new SkiaTaskProcessor(&mTaskManager);
131 }
132 contextOptions->fExecutor = mTaskProcessor.get();
133 }
Stan Ilieve75ef1f2017-11-27 17:22:42 -0500134
Yichi Chen9f959552018-03-29 21:21:54 +0800135 auto& cache = skiapipeline::ShaderCache::get();
136 cache.initShaderDiskCache(identity, size);
137 contextOptions->fPersistentCache = &cache;
Stan Iliev5d033482018-07-03 14:47:59 -0400138 contextOptions->fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400139}
140
141void CacheManager::trimMemory(TrimMemoryMode mode) {
142 if (!mGrContext) {
143 return;
144 }
145
146 mGrContext->flush();
147
148 switch (mode) {
149 case TrimMemoryMode::Complete:
John Reck1bcacfd2017-11-03 10:12:19 -0700150 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(mMaxSurfaceArea / 2);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400151 mGrContext->freeGpuResources();
152 break;
153 case TrimMemoryMode::UiHidden:
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -0400154 // Here we purge all the unlocked scratch resources and then toggle the resources cache
155 // limits between the background and max amounts. This causes the unlocked resources
156 // that have persistent data to be purged in LRU order.
157 mGrContext->purgeUnlockedResources(true);
158 mGrContext->setResourceCacheLimits(mMaxResources, mBackgroundResourceBytes);
159 mGrContext->setResourceCacheLimits(mMaxResources, mMaxResourceBytes);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400160 break;
161 }
162}
163
164void CacheManager::trimStaleResources() {
165 if (!mGrContext) {
166 return;
167 }
168 mGrContext->flush();
Derek Sollenberger92a9eb92018-04-12 13:42:19 -0400169 mGrContext->purgeResourcesNotUsedInMs(std::chrono::seconds(30));
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400170}
171
Stan Iliev3310fb12017-03-23 16:56:51 -0400172sp<skiapipeline::VectorDrawableAtlas> CacheManager::acquireVectorDrawableAtlas() {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400173 LOG_ALWAYS_FATAL_IF(mVectorDrawableAtlas.get() == nullptr);
174 LOG_ALWAYS_FATAL_IF(mGrContext == nullptr);
175
176 /**
Stan Iliev3310fb12017-03-23 16:56:51 -0400177 * TODO: define memory conditions where we clear the cache (e.g. surface->reset())
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400178 */
Stan Iliev3310fb12017-03-23 16:56:51 -0400179 return mVectorDrawableAtlas;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400180}
181
182void CacheManager::dumpMemoryUsage(String8& log, const RenderState* renderState) {
183 if (!mGrContext) {
184 log.appendFormat("No valid cache instance.\n");
185 return;
186 }
187
Derek Sollenberger0057db22018-03-29 14:18:44 -0400188 log.appendFormat("Font Cache (CPU):\n");
189 log.appendFormat(" Size: %.2f kB \n", SkGraphics::GetFontCacheUsed() / 1024.0f);
190 log.appendFormat(" Glyph Count: %d \n", SkGraphics::GetFontCacheCountUsed());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400191
Derek Sollenberger0057db22018-03-29 14:18:44 -0400192 log.appendFormat("CPU Caches:\n");
193 std::vector<skiapipeline::ResourcePair> cpuResourceMap = {
194 {"skia/sk_resource_cache/bitmap_", "Bitmaps"},
195 {"skia/sk_resource_cache/rrect-blur_", "Masks"},
196 {"skia/sk_resource_cache/rects-blur_", "Masks"},
197 {"skia/sk_resource_cache/tessellated", "Shadows"},
198 };
199 skiapipeline::SkiaMemoryTracer cpuTracer(cpuResourceMap, false);
200 SkGraphics::DumpMemoryStatistics(&cpuTracer);
201 cpuTracer.logOutput(log);
202
203 log.appendFormat("GPU Caches:\n");
204 skiapipeline::SkiaMemoryTracer gpuTracer("category", true);
205 mGrContext->dumpMemoryStatistics(&gpuTracer);
206 gpuTracer.logOutput(log);
207
208 log.appendFormat("Other Caches:\n");
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400209 log.appendFormat(" Current / Maximum\n");
Derek Sollenberger0057db22018-03-29 14:18:44 -0400210 log.appendFormat(" VectorDrawableAtlas %6.2f kB / %6.2f KB (entries = %zu)\n", 0.0f, 0.0f,
John Reck1bcacfd2017-11-03 10:12:19 -0700211 (size_t)0);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400212
213 if (renderState) {
214 if (renderState->mActiveLayers.size() > 0) {
215 log.appendFormat(" Layer Info:\n");
216 }
217
218 size_t layerMemoryTotal = 0;
219 for (std::set<Layer*>::iterator it = renderState->mActiveLayers.begin();
John Reck1bcacfd2017-11-03 10:12:19 -0700220 it != renderState->mActiveLayers.end(); it++) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400221 const Layer* layer = *it;
John Reck867c43d2018-08-30 16:47:59 +0000222 const char* layerType = layer->getApi() == Layer::Api::OpenGL ? "GlLayer" : "VkLayer";
John Reck1bcacfd2017-11-03 10:12:19 -0700223 log.appendFormat(" %s size %dx%d\n", layerType, layer->getWidth(),
224 layer->getHeight());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400225 layerMemoryTotal += layer->getWidth() * layer->getHeight() * 4;
226 }
Derek Sollenberger0057db22018-03-29 14:18:44 -0400227 log.appendFormat(" Layers Total %6.2f KB (numLayers = %zu)\n",
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400228 layerMemoryTotal / 1024.0f, renderState->mActiveLayers.size());
229 }
230
Derek Sollenberger0057db22018-03-29 14:18:44 -0400231 log.appendFormat("Total GPU memory usage:\n");
232 gpuTracer.logTotals(log);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400233}
234
235} /* namespace renderthread */
236} /* namespace uirenderer */
237} /* namespace android */