blob: 7acc44ca65b78f51adf6328fc6db824c4824bcb2 [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"
Stan Iliev564ca3e2018-09-04 22:00:00 +000024#include "Properties.h"
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040025#include "renderstate/RenderState.h"
26
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040027#include <GrContextOptions.h>
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040028#include <SkExecutor.h>
Derek Sollenberger0057db22018-03-29 14:18:44 -040029#include <SkGraphics.h>
John Reck1bcacfd2017-11-03 10:12:19 -070030#include <gui/Surface.h>
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040031#include <math.h>
32#include <set>
33
34namespace android {
35namespace uirenderer {
36namespace renderthread {
37
38// This multiplier was selected based on historical review of cache sizes relative
39// to the screen resolution. This is meant to be a conservative default based on
40// that analysis. The 4.0f is used because the default pixel format is assumed to
41// be ARGB_8888.
42#define SURFACE_SIZE_MULTIPLIER (12.0f * 4.0f)
43#define BACKGROUND_RETENTION_PERCENTAGE (0.5f)
44
45// for super large fonts we will draw them as paths so no need to keep linearly
46// increasing the font cache size.
47#define FONT_CACHE_MIN_MB (0.5f)
48#define FONT_CACHE_MAX_MB (4.0f)
49
John Reck1bcacfd2017-11-03 10:12:19 -070050CacheManager::CacheManager(const DisplayInfo& display) : mMaxSurfaceArea(display.w * display.h) {
51 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(
52 mMaxSurfaceArea / 2,
Stan Ilievdd098e82017-08-09 09:42:38 -040053 skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040054}
55
Greg Daniel660d6ec2017-12-08 11:44:27 -050056void CacheManager::reset(sk_sp<GrContext> context) {
57 if (context != mGrContext) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040058 destroy();
59 }
60
61 if (context) {
Greg Daniel660d6ec2017-12-08 11:44:27 -050062 mGrContext = std::move(context);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040063 mGrContext->getResourceCacheLimits(&mMaxResources, nullptr);
64 updateContextCacheSizes();
65 }
66}
67
68void CacheManager::destroy() {
69 // cleanup any caches here as the GrContext is about to go away...
70 mGrContext.reset(nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -070071 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(
72 mMaxSurfaceArea / 2,
73 skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040074}
75
76void CacheManager::updateContextCacheSizes() {
77 mMaxResourceBytes = mMaxSurfaceArea * SURFACE_SIZE_MULTIPLIER;
78 mBackgroundResourceBytes = mMaxResourceBytes * BACKGROUND_RETENTION_PERCENTAGE;
79
80 mGrContext->setResourceCacheLimits(mMaxResources, mMaxResourceBytes);
81}
82
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040083class CacheManager::SkiaTaskProcessor : public TaskProcessor<bool>, public SkExecutor {
84public:
85 explicit SkiaTaskProcessor(TaskManager* taskManager) : TaskProcessor<bool>(taskManager) {}
86
87 // This is really a Task<void> but that doesn't really work when Future<>
88 // expects to be able to get/set a value
89 struct SkiaTask : public Task<bool> {
90 std::function<void()> func;
91 };
92
93 virtual void add(std::function<void(void)> func) override {
94 sp<SkiaTask> task(new SkiaTask());
95 task->func = func;
96 TaskProcessor<bool>::add(task);
97 }
98
99 virtual void onProcess(const sp<Task<bool> >& task) override {
100 SkiaTask* t = static_cast<SkiaTask*>(task.get());
101 t->func();
102 task->setResult(true);
103 }
104};
105
Yichi Chen9f959552018-03-29 21:21:54 +0800106void CacheManager::configureContext(GrContextOptions* contextOptions, const void* identity, ssize_t size) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400107 contextOptions->fAllowPathMaskCaching = true;
108
109 float screenMP = mMaxSurfaceArea / 1024.0f / 1024.0f;
110 float fontCacheMB = 0;
111 float decimalVal = std::modf(screenMP, &fontCacheMB);
112
113 // This is a basic heuristic to size the cache to a multiple of 512 KB
114 if (decimalVal > 0.8f) {
115 fontCacheMB += 1.0f;
116 } else if (decimalVal > 0.5f) {
117 fontCacheMB += 0.5f;
118 }
119
120 // set limits on min/max size of the cache
121 fontCacheMB = std::max(FONT_CACHE_MIN_MB, std::min(FONT_CACHE_MAX_MB, fontCacheMB));
122
123 // We must currently set the size of the text cache based on the size of the
124 // display even though we like to be dynamicallysizing it to the size of the window.
125 // Skia's implementation doesn't provide a mechanism to resize the font cache due to
126 // the potential cost of recreating the glyphs.
127 contextOptions->fGlyphCacheTextureMaximumBytes = fontCacheMB * 1024 * 1024;
Derek Sollenberger8ec9e882017-08-24 16:36:08 -0400128
129 if (mTaskManager.canRunTasks()) {
130 if (!mTaskProcessor.get()) {
131 mTaskProcessor = new SkiaTaskProcessor(&mTaskManager);
132 }
133 contextOptions->fExecutor = mTaskProcessor.get();
134 }
Stan Ilieve75ef1f2017-11-27 17:22:42 -0500135
Yichi Chen9f959552018-03-29 21:21:54 +0800136 auto& cache = skiapipeline::ShaderCache::get();
137 cache.initShaderDiskCache(identity, size);
138 contextOptions->fPersistentCache = &cache;
Stan Iliev5d033482018-07-03 14:47:59 -0400139 contextOptions->fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400140}
141
142void CacheManager::trimMemory(TrimMemoryMode mode) {
143 if (!mGrContext) {
144 return;
145 }
146
147 mGrContext->flush();
148
149 switch (mode) {
150 case TrimMemoryMode::Complete:
John Reck1bcacfd2017-11-03 10:12:19 -0700151 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(mMaxSurfaceArea / 2);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400152 mGrContext->freeGpuResources();
153 break;
154 case TrimMemoryMode::UiHidden:
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -0400155 // Here we purge all the unlocked scratch resources and then toggle the resources cache
156 // limits between the background and max amounts. This causes the unlocked resources
157 // that have persistent data to be purged in LRU order.
158 mGrContext->purgeUnlockedResources(true);
159 mGrContext->setResourceCacheLimits(mMaxResources, mBackgroundResourceBytes);
160 mGrContext->setResourceCacheLimits(mMaxResources, mMaxResourceBytes);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400161 break;
162 }
163}
164
165void CacheManager::trimStaleResources() {
166 if (!mGrContext) {
167 return;
168 }
169 mGrContext->flush();
Derek Sollenberger92a9eb92018-04-12 13:42:19 -0400170 mGrContext->purgeResourcesNotUsedInMs(std::chrono::seconds(30));
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400171}
172
Stan Iliev3310fb12017-03-23 16:56:51 -0400173sp<skiapipeline::VectorDrawableAtlas> CacheManager::acquireVectorDrawableAtlas() {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400174 LOG_ALWAYS_FATAL_IF(mVectorDrawableAtlas.get() == nullptr);
175 LOG_ALWAYS_FATAL_IF(mGrContext == nullptr);
176
177 /**
Stan Iliev3310fb12017-03-23 16:56:51 -0400178 * TODO: define memory conditions where we clear the cache (e.g. surface->reset())
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400179 */
Stan Iliev3310fb12017-03-23 16:56:51 -0400180 return mVectorDrawableAtlas;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400181}
182
183void CacheManager::dumpMemoryUsage(String8& log, const RenderState* renderState) {
184 if (!mGrContext) {
185 log.appendFormat("No valid cache instance.\n");
186 return;
187 }
188
Derek Sollenberger0057db22018-03-29 14:18:44 -0400189 log.appendFormat("Font Cache (CPU):\n");
190 log.appendFormat(" Size: %.2f kB \n", SkGraphics::GetFontCacheUsed() / 1024.0f);
191 log.appendFormat(" Glyph Count: %d \n", SkGraphics::GetFontCacheCountUsed());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400192
Derek Sollenberger0057db22018-03-29 14:18:44 -0400193 log.appendFormat("CPU Caches:\n");
194 std::vector<skiapipeline::ResourcePair> cpuResourceMap = {
195 {"skia/sk_resource_cache/bitmap_", "Bitmaps"},
196 {"skia/sk_resource_cache/rrect-blur_", "Masks"},
197 {"skia/sk_resource_cache/rects-blur_", "Masks"},
198 {"skia/sk_resource_cache/tessellated", "Shadows"},
199 };
200 skiapipeline::SkiaMemoryTracer cpuTracer(cpuResourceMap, false);
201 SkGraphics::DumpMemoryStatistics(&cpuTracer);
202 cpuTracer.logOutput(log);
203
204 log.appendFormat("GPU Caches:\n");
205 skiapipeline::SkiaMemoryTracer gpuTracer("category", true);
206 mGrContext->dumpMemoryStatistics(&gpuTracer);
207 gpuTracer.logOutput(log);
208
209 log.appendFormat("Other Caches:\n");
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400210 log.appendFormat(" Current / Maximum\n");
Derek Sollenberger0057db22018-03-29 14:18:44 -0400211 log.appendFormat(" VectorDrawableAtlas %6.2f kB / %6.2f KB (entries = %zu)\n", 0.0f, 0.0f,
John Reck1bcacfd2017-11-03 10:12:19 -0700212 (size_t)0);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400213
214 if (renderState) {
215 if (renderState->mActiveLayers.size() > 0) {
216 log.appendFormat(" Layer Info:\n");
217 }
218
Stan Iliev564ca3e2018-09-04 22:00:00 +0000219 const char* layerType = Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL
220 ? "GlLayer" : "VkLayer";
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400221 size_t layerMemoryTotal = 0;
222 for (std::set<Layer*>::iterator it = renderState->mActiveLayers.begin();
John Reck1bcacfd2017-11-03 10:12:19 -0700223 it != renderState->mActiveLayers.end(); it++) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400224 const Layer* layer = *it;
John Reck1bcacfd2017-11-03 10:12:19 -0700225 log.appendFormat(" %s size %dx%d\n", layerType, layer->getWidth(),
226 layer->getHeight());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400227 layerMemoryTotal += layer->getWidth() * layer->getHeight() * 4;
228 }
Derek Sollenberger0057db22018-03-29 14:18:44 -0400229 log.appendFormat(" Layers Total %6.2f KB (numLayers = %zu)\n",
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400230 layerMemoryTotal / 1024.0f, renderState->mActiveLayers.size());
231 }
232
Derek Sollenberger0057db22018-03-29 14:18:44 -0400233 log.appendFormat("Total GPU memory usage:\n");
234 gpuTracer.logTotals(log);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400235}
236
237} /* namespace renderthread */
238} /* namespace uirenderer */
239} /* namespace android */