blob: bec80b1e6011900fcc608aee3c92b3cb2ffa77ef [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);
John Reck1072fff2018-04-12 15:20:09 -070053 skiapipeline::ShaderCache::get().initShaderDiskCache();
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
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400106void CacheManager::configureContext(GrContextOptions* contextOptions) {
107 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
136 contextOptions->fPersistentCache = &skiapipeline::ShaderCache::get();
Stan Iliev5d033482018-07-03 14:47:59 -0400137 contextOptions->fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400138}
139
140void CacheManager::trimMemory(TrimMemoryMode mode) {
141 if (!mGrContext) {
142 return;
143 }
144
145 mGrContext->flush();
146
147 switch (mode) {
148 case TrimMemoryMode::Complete:
John Reck1bcacfd2017-11-03 10:12:19 -0700149 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(mMaxSurfaceArea / 2);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400150 mGrContext->freeGpuResources();
151 break;
152 case TrimMemoryMode::UiHidden:
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -0400153 // Here we purge all the unlocked scratch resources and then toggle the resources cache
154 // limits between the background and max amounts. This causes the unlocked resources
155 // that have persistent data to be purged in LRU order.
156 mGrContext->purgeUnlockedResources(true);
157 mGrContext->setResourceCacheLimits(mMaxResources, mBackgroundResourceBytes);
158 mGrContext->setResourceCacheLimits(mMaxResources, mMaxResourceBytes);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400159 break;
160 }
161}
162
163void CacheManager::trimStaleResources() {
164 if (!mGrContext) {
165 return;
166 }
167 mGrContext->flush();
Derek Sollenberger92a9eb92018-04-12 13:42:19 -0400168 mGrContext->purgeResourcesNotUsedInMs(std::chrono::seconds(30));
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400169}
170
Stan Iliev3310fb12017-03-23 16:56:51 -0400171sp<skiapipeline::VectorDrawableAtlas> CacheManager::acquireVectorDrawableAtlas() {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400172 LOG_ALWAYS_FATAL_IF(mVectorDrawableAtlas.get() == nullptr);
173 LOG_ALWAYS_FATAL_IF(mGrContext == nullptr);
174
175 /**
Stan Iliev3310fb12017-03-23 16:56:51 -0400176 * TODO: define memory conditions where we clear the cache (e.g. surface->reset())
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400177 */
Stan Iliev3310fb12017-03-23 16:56:51 -0400178 return mVectorDrawableAtlas;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400179}
180
181void CacheManager::dumpMemoryUsage(String8& log, const RenderState* renderState) {
182 if (!mGrContext) {
183 log.appendFormat("No valid cache instance.\n");
184 return;
185 }
186
Derek Sollenberger0057db22018-03-29 14:18:44 -0400187 log.appendFormat("Font Cache (CPU):\n");
188 log.appendFormat(" Size: %.2f kB \n", SkGraphics::GetFontCacheUsed() / 1024.0f);
189 log.appendFormat(" Glyph Count: %d \n", SkGraphics::GetFontCacheCountUsed());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400190
Derek Sollenberger0057db22018-03-29 14:18:44 -0400191 log.appendFormat("CPU Caches:\n");
192 std::vector<skiapipeline::ResourcePair> cpuResourceMap = {
193 {"skia/sk_resource_cache/bitmap_", "Bitmaps"},
194 {"skia/sk_resource_cache/rrect-blur_", "Masks"},
195 {"skia/sk_resource_cache/rects-blur_", "Masks"},
196 {"skia/sk_resource_cache/tessellated", "Shadows"},
197 };
198 skiapipeline::SkiaMemoryTracer cpuTracer(cpuResourceMap, false);
199 SkGraphics::DumpMemoryStatistics(&cpuTracer);
200 cpuTracer.logOutput(log);
201
202 log.appendFormat("GPU Caches:\n");
203 skiapipeline::SkiaMemoryTracer gpuTracer("category", true);
204 mGrContext->dumpMemoryStatistics(&gpuTracer);
205 gpuTracer.logOutput(log);
206
207 log.appendFormat("Other Caches:\n");
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400208 log.appendFormat(" Current / Maximum\n");
Derek Sollenberger0057db22018-03-29 14:18:44 -0400209 log.appendFormat(" VectorDrawableAtlas %6.2f kB / %6.2f KB (entries = %zu)\n", 0.0f, 0.0f,
John Reck1bcacfd2017-11-03 10:12:19 -0700210 (size_t)0);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400211
212 if (renderState) {
213 if (renderState->mActiveLayers.size() > 0) {
214 log.appendFormat(" Layer Info:\n");
215 }
216
217 size_t layerMemoryTotal = 0;
218 for (std::set<Layer*>::iterator it = renderState->mActiveLayers.begin();
John Reck1bcacfd2017-11-03 10:12:19 -0700219 it != renderState->mActiveLayers.end(); it++) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400220 const Layer* layer = *it;
221 const char* layerType = layer->getApi() == Layer::Api::OpenGL ? "GlLayer" : "VkLayer";
John Reck1bcacfd2017-11-03 10:12:19 -0700222 log.appendFormat(" %s size %dx%d\n", layerType, layer->getWidth(),
223 layer->getHeight());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400224 layerMemoryTotal += layer->getWidth() * layer->getHeight() * 4;
225 }
Derek Sollenberger0057db22018-03-29 14:18:44 -0400226 log.appendFormat(" Layers Total %6.2f KB (numLayers = %zu)\n",
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400227 layerMemoryTotal / 1024.0f, renderState->mActiveLayers.size());
228 }
229
Derek Sollenberger0057db22018-03-29 14:18:44 -0400230 log.appendFormat("Total GPU memory usage:\n");
231 gpuTracer.logTotals(log);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400232}
233
234} /* namespace renderthread */
235} /* namespace uirenderer */
236} /* namespace android */