blob: 6da80628be60dc08798012bc41e7a633575cb54d [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>
Stan Iliev43d06132018-12-21 13:14:15 -050033#include <SkMathPriv.h>
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040034
35namespace android {
36namespace uirenderer {
37namespace renderthread {
38
39// This multiplier was selected based on historical review of cache sizes relative
40// to the screen resolution. This is meant to be a conservative default based on
41// that analysis. The 4.0f is used because the default pixel format is assumed to
42// be ARGB_8888.
43#define SURFACE_SIZE_MULTIPLIER (12.0f * 4.0f)
44#define BACKGROUND_RETENTION_PERCENTAGE (0.5f)
45
John Reck1bcacfd2017-11-03 10:12:19 -070046CacheManager::CacheManager(const DisplayInfo& display) : mMaxSurfaceArea(display.w * display.h) {
47 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(
48 mMaxSurfaceArea / 2,
Stan Ilievdd098e82017-08-09 09:42:38 -040049 skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040050}
51
Greg Daniel660d6ec2017-12-08 11:44:27 -050052void CacheManager::reset(sk_sp<GrContext> context) {
53 if (context != mGrContext) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040054 destroy();
55 }
56
57 if (context) {
Greg Daniel660d6ec2017-12-08 11:44:27 -050058 mGrContext = std::move(context);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040059 mGrContext->getResourceCacheLimits(&mMaxResources, nullptr);
60 updateContextCacheSizes();
61 }
62}
63
64void CacheManager::destroy() {
65 // cleanup any caches here as the GrContext is about to go away...
66 mGrContext.reset(nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -070067 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(
68 mMaxSurfaceArea / 2,
69 skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040070}
71
72void CacheManager::updateContextCacheSizes() {
73 mMaxResourceBytes = mMaxSurfaceArea * SURFACE_SIZE_MULTIPLIER;
74 mBackgroundResourceBytes = mMaxResourceBytes * BACKGROUND_RETENTION_PERCENTAGE;
75
76 mGrContext->setResourceCacheLimits(mMaxResources, mMaxResourceBytes);
77}
78
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040079class CacheManager::SkiaTaskProcessor : public TaskProcessor<bool>, public SkExecutor {
80public:
81 explicit SkiaTaskProcessor(TaskManager* taskManager) : TaskProcessor<bool>(taskManager) {}
82
83 // This is really a Task<void> but that doesn't really work when Future<>
84 // expects to be able to get/set a value
85 struct SkiaTask : public Task<bool> {
86 std::function<void()> func;
87 };
88
89 virtual void add(std::function<void(void)> func) override {
90 sp<SkiaTask> task(new SkiaTask());
91 task->func = func;
92 TaskProcessor<bool>::add(task);
93 }
94
95 virtual void onProcess(const sp<Task<bool> >& task) override {
96 SkiaTask* t = static_cast<SkiaTask*>(task.get());
97 t->func();
98 task->setResult(true);
99 }
100};
101
Yichi Chen9f959552018-03-29 21:21:54 +0800102void CacheManager::configureContext(GrContextOptions* contextOptions, const void* identity, ssize_t size) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400103 contextOptions->fAllowPathMaskCaching = true;
104
Stan Iliev43d06132018-12-21 13:14:15 -0500105 // This sets the maximum size for a single texture atlas in the GPU font cache. If necessary,
106 // the cache can allocate additional textures that are counted against the total cache limits
107 // provided to Skia.
108 contextOptions->fGlyphCacheTextureMaximumBytes = GrNextSizePow2(mMaxSurfaceArea);
Derek Sollenberger8ec9e882017-08-24 16:36:08 -0400109
110 if (mTaskManager.canRunTasks()) {
111 if (!mTaskProcessor.get()) {
112 mTaskProcessor = new SkiaTaskProcessor(&mTaskManager);
113 }
114 contextOptions->fExecutor = mTaskProcessor.get();
115 }
Stan Ilieve75ef1f2017-11-27 17:22:42 -0500116
Yichi Chen9f959552018-03-29 21:21:54 +0800117 auto& cache = skiapipeline::ShaderCache::get();
118 cache.initShaderDiskCache(identity, size);
119 contextOptions->fPersistentCache = &cache;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400120}
121
122void CacheManager::trimMemory(TrimMemoryMode mode) {
123 if (!mGrContext) {
124 return;
125 }
126
127 mGrContext->flush();
128
129 switch (mode) {
130 case TrimMemoryMode::Complete:
John Reck1bcacfd2017-11-03 10:12:19 -0700131 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(mMaxSurfaceArea / 2);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400132 mGrContext->freeGpuResources();
133 break;
134 case TrimMemoryMode::UiHidden:
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -0400135 // Here we purge all the unlocked scratch resources and then toggle the resources cache
136 // limits between the background and max amounts. This causes the unlocked resources
137 // that have persistent data to be purged in LRU order.
138 mGrContext->purgeUnlockedResources(true);
139 mGrContext->setResourceCacheLimits(mMaxResources, mBackgroundResourceBytes);
140 mGrContext->setResourceCacheLimits(mMaxResources, mMaxResourceBytes);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400141 break;
142 }
143}
144
145void CacheManager::trimStaleResources() {
146 if (!mGrContext) {
147 return;
148 }
149 mGrContext->flush();
Derek Sollenberger92a9eb92018-04-12 13:42:19 -0400150 mGrContext->purgeResourcesNotUsedInMs(std::chrono::seconds(30));
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400151}
152
Stan Iliev3310fb12017-03-23 16:56:51 -0400153sp<skiapipeline::VectorDrawableAtlas> CacheManager::acquireVectorDrawableAtlas() {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400154 LOG_ALWAYS_FATAL_IF(mVectorDrawableAtlas.get() == nullptr);
155 LOG_ALWAYS_FATAL_IF(mGrContext == nullptr);
156
157 /**
Stan Iliev3310fb12017-03-23 16:56:51 -0400158 * TODO: define memory conditions where we clear the cache (e.g. surface->reset())
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400159 */
Stan Iliev3310fb12017-03-23 16:56:51 -0400160 return mVectorDrawableAtlas;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400161}
162
163void CacheManager::dumpMemoryUsage(String8& log, const RenderState* renderState) {
164 if (!mGrContext) {
165 log.appendFormat("No valid cache instance.\n");
166 return;
167 }
168
Derek Sollenberger0057db22018-03-29 14:18:44 -0400169 log.appendFormat("Font Cache (CPU):\n");
170 log.appendFormat(" Size: %.2f kB \n", SkGraphics::GetFontCacheUsed() / 1024.0f);
171 log.appendFormat(" Glyph Count: %d \n", SkGraphics::GetFontCacheCountUsed());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400172
Derek Sollenberger0057db22018-03-29 14:18:44 -0400173 log.appendFormat("CPU Caches:\n");
174 std::vector<skiapipeline::ResourcePair> cpuResourceMap = {
175 {"skia/sk_resource_cache/bitmap_", "Bitmaps"},
176 {"skia/sk_resource_cache/rrect-blur_", "Masks"},
177 {"skia/sk_resource_cache/rects-blur_", "Masks"},
178 {"skia/sk_resource_cache/tessellated", "Shadows"},
179 };
180 skiapipeline::SkiaMemoryTracer cpuTracer(cpuResourceMap, false);
181 SkGraphics::DumpMemoryStatistics(&cpuTracer);
182 cpuTracer.logOutput(log);
183
184 log.appendFormat("GPU Caches:\n");
185 skiapipeline::SkiaMemoryTracer gpuTracer("category", true);
186 mGrContext->dumpMemoryStatistics(&gpuTracer);
187 gpuTracer.logOutput(log);
188
189 log.appendFormat("Other Caches:\n");
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400190 log.appendFormat(" Current / Maximum\n");
Derek Sollenberger0057db22018-03-29 14:18:44 -0400191 log.appendFormat(" VectorDrawableAtlas %6.2f kB / %6.2f KB (entries = %zu)\n", 0.0f, 0.0f,
John Reck1bcacfd2017-11-03 10:12:19 -0700192 (size_t)0);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400193
194 if (renderState) {
195 if (renderState->mActiveLayers.size() > 0) {
196 log.appendFormat(" Layer Info:\n");
197 }
198
Stan Iliev564ca3e2018-09-04 22:00:00 +0000199 const char* layerType = Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL
200 ? "GlLayer" : "VkLayer";
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400201 size_t layerMemoryTotal = 0;
202 for (std::set<Layer*>::iterator it = renderState->mActiveLayers.begin();
John Reck1bcacfd2017-11-03 10:12:19 -0700203 it != renderState->mActiveLayers.end(); it++) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400204 const Layer* layer = *it;
John Reck1bcacfd2017-11-03 10:12:19 -0700205 log.appendFormat(" %s size %dx%d\n", layerType, layer->getWidth(),
206 layer->getHeight());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400207 layerMemoryTotal += layer->getWidth() * layer->getHeight() * 4;
208 }
Derek Sollenberger0057db22018-03-29 14:18:44 -0400209 log.appendFormat(" Layers Total %6.2f KB (numLayers = %zu)\n",
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400210 layerMemoryTotal / 1024.0f, renderState->mActiveLayers.size());
211 }
212
Derek Sollenberger0057db22018-03-29 14:18:44 -0400213 log.appendFormat("Total GPU memory usage:\n");
214 gpuTracer.logTotals(log);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400215}
216
217} /* namespace renderthread */
218} /* namespace uirenderer */
219} /* namespace android */