blob: 1e5877356e8dd638c129fe885da992de8c45277c [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
Alec Mouri22d753f2019-09-05 17:11:45 -070019#include "DeviceInfo.h"
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040020#include "Layer.h"
Stan Ilieve75ef1f2017-11-27 17:22:42 -050021#include "Properties.h"
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040022#include "RenderThread.h"
Stan Ilieve0fae232020-01-07 17:21:49 -050023#include "pipeline/skia/ATraceMemoryDump.h"
Stan Ilieve75ef1f2017-11-27 17:22:42 -050024#include "pipeline/skia/ShaderCache.h"
Derek Sollenberger0057db22018-03-29 14:18:44 -040025#include "pipeline/skia/SkiaMemoryTracer.h"
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040026#include "renderstate/RenderState.h"
John Reck322b8ab2019-03-14 13:15:28 -070027#include "thread/CommonPool.h"
Stan Ilieve0fae232020-01-07 17:21:49 -050028#include <utils/Trace.h>
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040029
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040030#include <GrContextOptions.h>
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040031#include <SkExecutor.h>
Derek Sollenberger0057db22018-03-29 14:18:44 -040032#include <SkGraphics.h>
John Reck0fa0cbc2019-04-05 16:57:46 -070033#include <SkMathPriv.h>
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040034#include <math.h>
35#include <set>
36
37namespace android {
38namespace uirenderer {
39namespace renderthread {
40
41// This multiplier was selected based on historical review of cache sizes relative
42// to the screen resolution. This is meant to be a conservative default based on
43// that analysis. The 4.0f is used because the default pixel format is assumed to
44// be ARGB_8888.
Leon Scroggins30d36782020-06-11 15:22:36 +000045#define SURFACE_SIZE_MULTIPLIER (12.0f * 4.0f)
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040046#define BACKGROUND_RETENTION_PERCENTAGE (0.5f)
47
Alec Mouri22d753f2019-09-05 17:11:45 -070048CacheManager::CacheManager()
49 : mMaxSurfaceArea(DeviceInfo::getWidth() * DeviceInfo::getHeight())
Derek Sollenbergerb9e296e2019-04-18 16:21:42 -040050 , mMaxResourceBytes(mMaxSurfaceArea * SURFACE_SIZE_MULTIPLIER)
51 , mBackgroundResourceBytes(mMaxResourceBytes * BACKGROUND_RETENTION_PERCENTAGE)
52 // This sets the maximum size for a single texture atlas in the GPU font cache. If
53 // necessary, the cache can allocate additional textures that are counted against the
54 // total cache limits provided to Skia.
55 , mMaxGpuFontAtlasBytes(GrNextSizePow2(mMaxSurfaceArea))
56 // This sets the maximum size of the CPU font cache to be at least the same size as the
57 // total number of GPU font caches (i.e. 4 separate GPU atlases).
Alec Mouri22d753f2019-09-05 17:11:45 -070058 , mMaxCpuFontCacheBytes(
59 std::max(mMaxGpuFontAtlasBytes * 4, SkGraphics::GetFontCacheLimit()))
Derek Sollenbergerb9e296e2019-04-18 16:21:42 -040060 , mBackgroundCpuFontCacheBytes(mMaxCpuFontCacheBytes * BACKGROUND_RETENTION_PERCENTAGE) {
Derek Sollenbergerb9e296e2019-04-18 16:21:42 -040061 SkGraphics::SetFontCacheLimit(mMaxCpuFontCacheBytes);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040062}
63
Greg Daniel660d6ec2017-12-08 11:44:27 -050064void CacheManager::reset(sk_sp<GrContext> context) {
65 if (context != mGrContext) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040066 destroy();
67 }
68
69 if (context) {
Greg Daniel660d6ec2017-12-08 11:44:27 -050070 mGrContext = std::move(context);
Robert Phillips57bb0bf2019-09-06 13:18:17 -040071 mGrContext->setResourceCacheLimit(mMaxResourceBytes);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040072 }
73}
74
75void CacheManager::destroy() {
76 // cleanup any caches here as the GrContext is about to go away...
77 mGrContext.reset(nullptr);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040078}
79
John Reck322b8ab2019-03-14 13:15:28 -070080class CommonPoolExecutor : public SkExecutor {
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040081public:
John Reck0fa0cbc2019-04-05 16:57:46 -070082 virtual void add(std::function<void(void)> func) override { CommonPool::post(std::move(func)); }
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040083};
84
John Reck322b8ab2019-03-14 13:15:28 -070085static CommonPoolExecutor sDefaultExecutor;
86
John Reck0fa0cbc2019-04-05 16:57:46 -070087void CacheManager::configureContext(GrContextOptions* contextOptions, const void* identity,
88 ssize_t size) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040089 contextOptions->fAllowPathMaskCaching = true;
Derek Sollenbergerb9e296e2019-04-18 16:21:42 -040090 contextOptions->fGlyphCacheTextureMaximumBytes = mMaxGpuFontAtlasBytes;
John Reck322b8ab2019-03-14 13:15:28 -070091 contextOptions->fExecutor = &sDefaultExecutor;
Stan Ilieve75ef1f2017-11-27 17:22:42 -050092
Yichi Chen9f959552018-03-29 21:21:54 +080093 auto& cache = skiapipeline::ShaderCache::get();
94 cache.initShaderDiskCache(identity, size);
95 contextOptions->fPersistentCache = &cache;
Christopher Dalton3fcb6972019-02-21 20:09:22 +000096 contextOptions->fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040097}
98
99void CacheManager::trimMemory(TrimMemoryMode mode) {
100 if (!mGrContext) {
101 return;
102 }
103
104 mGrContext->flush();
105
106 switch (mode) {
107 case TrimMemoryMode::Complete:
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400108 mGrContext->freeGpuResources();
Derek Sollenbergerb9e296e2019-04-18 16:21:42 -0400109 SkGraphics::PurgeAllCaches();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400110 break;
111 case TrimMemoryMode::UiHidden:
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -0400112 // Here we purge all the unlocked scratch resources and then toggle the resources cache
113 // limits between the background and max amounts. This causes the unlocked resources
114 // that have persistent data to be purged in LRU order.
115 mGrContext->purgeUnlockedResources(true);
Robert Phillips57bb0bf2019-09-06 13:18:17 -0400116 mGrContext->setResourceCacheLimit(mBackgroundResourceBytes);
117 mGrContext->setResourceCacheLimit(mMaxResourceBytes);
Derek Sollenbergerb9e296e2019-04-18 16:21:42 -0400118 SkGraphics::SetFontCacheLimit(mBackgroundCpuFontCacheBytes);
119 SkGraphics::SetFontCacheLimit(mMaxCpuFontCacheBytes);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400120 break;
121 }
Greg Daniel1d857f02019-04-17 12:18:50 -0400122
123 // We must sync the cpu to make sure deletions of resources still queued up on the GPU actually
124 // happen.
125 mGrContext->flush(kSyncCpu_GrFlushFlag, 0, nullptr);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400126}
127
128void CacheManager::trimStaleResources() {
129 if (!mGrContext) {
130 return;
131 }
132 mGrContext->flush();
John Reckf846aee2019-10-08 23:28:41 +0000133 mGrContext->purgeResourcesNotUsedInMs(std::chrono::seconds(30));
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400134}
135
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400136void CacheManager::dumpMemoryUsage(String8& log, const RenderState* renderState) {
137 if (!mGrContext) {
138 log.appendFormat("No valid cache instance.\n");
139 return;
140 }
141
Derek Sollenberger0057db22018-03-29 14:18:44 -0400142 log.appendFormat("Font Cache (CPU):\n");
143 log.appendFormat(" Size: %.2f kB \n", SkGraphics::GetFontCacheUsed() / 1024.0f);
144 log.appendFormat(" Glyph Count: %d \n", SkGraphics::GetFontCacheCountUsed());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400145
Derek Sollenberger0057db22018-03-29 14:18:44 -0400146 log.appendFormat("CPU Caches:\n");
147 std::vector<skiapipeline::ResourcePair> cpuResourceMap = {
148 {"skia/sk_resource_cache/bitmap_", "Bitmaps"},
149 {"skia/sk_resource_cache/rrect-blur_", "Masks"},
150 {"skia/sk_resource_cache/rects-blur_", "Masks"},
151 {"skia/sk_resource_cache/tessellated", "Shadows"},
152 };
153 skiapipeline::SkiaMemoryTracer cpuTracer(cpuResourceMap, false);
154 SkGraphics::DumpMemoryStatistics(&cpuTracer);
155 cpuTracer.logOutput(log);
156
157 log.appendFormat("GPU Caches:\n");
158 skiapipeline::SkiaMemoryTracer gpuTracer("category", true);
159 mGrContext->dumpMemoryStatistics(&gpuTracer);
160 gpuTracer.logOutput(log);
161
162 log.appendFormat("Other Caches:\n");
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400163 log.appendFormat(" Current / Maximum\n");
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400164
165 if (renderState) {
166 if (renderState->mActiveLayers.size() > 0) {
167 log.appendFormat(" Layer Info:\n");
168 }
169
Stan Iliev564ca3e2018-09-04 22:00:00 +0000170 const char* layerType = Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL
John Reck0fa0cbc2019-04-05 16:57:46 -0700171 ? "GlLayer"
172 : "VkLayer";
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400173 size_t layerMemoryTotal = 0;
174 for (std::set<Layer*>::iterator it = renderState->mActiveLayers.begin();
John Reck1bcacfd2017-11-03 10:12:19 -0700175 it != renderState->mActiveLayers.end(); it++) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400176 const Layer* layer = *it;
John Reck1bcacfd2017-11-03 10:12:19 -0700177 log.appendFormat(" %s size %dx%d\n", layerType, layer->getWidth(),
178 layer->getHeight());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400179 layerMemoryTotal += layer->getWidth() * layer->getHeight() * 4;
180 }
Derek Sollenberger0057db22018-03-29 14:18:44 -0400181 log.appendFormat(" Layers Total %6.2f KB (numLayers = %zu)\n",
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400182 layerMemoryTotal / 1024.0f, renderState->mActiveLayers.size());
183 }
184
Derek Sollenberger0057db22018-03-29 14:18:44 -0400185 log.appendFormat("Total GPU memory usage:\n");
186 gpuTracer.logTotals(log);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400187}
188
Stan Ilieve0fae232020-01-07 17:21:49 -0500189void CacheManager::onFrameCompleted() {
190 if (ATRACE_ENABLED()) {
191 static skiapipeline::ATraceMemoryDump tracer;
192 tracer.startFrame();
193 SkGraphics::DumpMemoryStatistics(&tracer);
194 if (mGrContext) {
195 mGrContext->dumpMemoryStatistics(&tracer);
196 }
197 tracer.logTraces();
198 }
199}
200
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400201} /* namespace renderthread */
202} /* namespace uirenderer */
203} /* namespace android */