blob: c45eedad775c3b6dfe300b12ccbe469aa9e297f6 [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 Iliev85f90962018-08-31 18:35:06 +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);
John Reck1072fff2018-04-12 15:20:09 -070054 skiapipeline::ShaderCache::get().initShaderDiskCache();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040055}
56
Greg Daniel660d6ec2017-12-08 11:44:27 -050057void CacheManager::reset(sk_sp<GrContext> context) {
58 if (context != mGrContext) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040059 destroy();
60 }
61
62 if (context) {
Greg Daniel660d6ec2017-12-08 11:44:27 -050063 mGrContext = std::move(context);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040064 mGrContext->getResourceCacheLimits(&mMaxResources, nullptr);
65 updateContextCacheSizes();
66 }
67}
68
69void CacheManager::destroy() {
70 // cleanup any caches here as the GrContext is about to go away...
71 mGrContext.reset(nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -070072 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(
73 mMaxSurfaceArea / 2,
74 skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040075}
76
77void CacheManager::updateContextCacheSizes() {
78 mMaxResourceBytes = mMaxSurfaceArea * SURFACE_SIZE_MULTIPLIER;
79 mBackgroundResourceBytes = mMaxResourceBytes * BACKGROUND_RETENTION_PERCENTAGE;
80
81 mGrContext->setResourceCacheLimits(mMaxResources, mMaxResourceBytes);
82}
83
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040084class CacheManager::SkiaTaskProcessor : public TaskProcessor<bool>, public SkExecutor {
85public:
86 explicit SkiaTaskProcessor(TaskManager* taskManager) : TaskProcessor<bool>(taskManager) {}
87
88 // This is really a Task<void> but that doesn't really work when Future<>
89 // expects to be able to get/set a value
90 struct SkiaTask : public Task<bool> {
91 std::function<void()> func;
92 };
93
94 virtual void add(std::function<void(void)> func) override {
95 sp<SkiaTask> task(new SkiaTask());
96 task->func = func;
97 TaskProcessor<bool>::add(task);
98 }
99
100 virtual void onProcess(const sp<Task<bool> >& task) override {
101 SkiaTask* t = static_cast<SkiaTask*>(task.get());
102 t->func();
103 task->setResult(true);
104 }
105};
106
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400107void CacheManager::configureContext(GrContextOptions* contextOptions) {
108 contextOptions->fAllowPathMaskCaching = true;
109
110 float screenMP = mMaxSurfaceArea / 1024.0f / 1024.0f;
111 float fontCacheMB = 0;
112 float decimalVal = std::modf(screenMP, &fontCacheMB);
113
114 // This is a basic heuristic to size the cache to a multiple of 512 KB
115 if (decimalVal > 0.8f) {
116 fontCacheMB += 1.0f;
117 } else if (decimalVal > 0.5f) {
118 fontCacheMB += 0.5f;
119 }
120
121 // set limits on min/max size of the cache
122 fontCacheMB = std::max(FONT_CACHE_MIN_MB, std::min(FONT_CACHE_MAX_MB, fontCacheMB));
123
124 // We must currently set the size of the text cache based on the size of the
125 // display even though we like to be dynamicallysizing it to the size of the window.
126 // Skia's implementation doesn't provide a mechanism to resize the font cache due to
127 // the potential cost of recreating the glyphs.
128 contextOptions->fGlyphCacheTextureMaximumBytes = fontCacheMB * 1024 * 1024;
Derek Sollenberger8ec9e882017-08-24 16:36:08 -0400129
130 if (mTaskManager.canRunTasks()) {
131 if (!mTaskProcessor.get()) {
132 mTaskProcessor = new SkiaTaskProcessor(&mTaskManager);
133 }
134 contextOptions->fExecutor = mTaskProcessor.get();
135 }
Stan Ilieve75ef1f2017-11-27 17:22:42 -0500136
137 contextOptions->fPersistentCache = &skiapipeline::ShaderCache::get();
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
Stan Iliev85f90962018-08-31 18:35:06 +0000218 const char* layerType = Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL
219 ? "GlLayer" : "VkLayer";
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400220 size_t layerMemoryTotal = 0;
221 for (std::set<Layer*>::iterator it = renderState->mActiveLayers.begin();
John Reck1bcacfd2017-11-03 10:12:19 -0700222 it != renderState->mActiveLayers.end(); it++) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400223 const Layer* layer = *it;
John Reck1bcacfd2017-11-03 10:12:19 -0700224 log.appendFormat(" %s size %dx%d\n", layerType, layer->getWidth(),
225 layer->getHeight());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400226 layerMemoryTotal += layer->getWidth() * layer->getHeight() * 4;
227 }
Derek Sollenberger0057db22018-03-29 14:18:44 -0400228 log.appendFormat(" Layers Total %6.2f KB (numLayers = %zu)\n",
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400229 layerMemoryTotal / 1024.0f, renderState->mActiveLayers.size());
230 }
231
Derek Sollenberger0057db22018-03-29 14:18:44 -0400232 log.appendFormat("Total GPU memory usage:\n");
233 gpuTracer.logTotals(log);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400234}
235
236} /* namespace renderthread */
237} /* namespace uirenderer */
238} /* namespace android */