blob: b366a80f918a033dd8867ad707f1873bcdea251b [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"
John Reck322b8ab2019-03-14 13:15:28 -070025#include "thread/CommonPool.h"
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040026
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 Reck0fa0cbc2019-04-05 16:57:46 -070030#include <SkMathPriv.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.
John Reck05ac5162019-10-04 13:47:55 -070042#define SURFACE_SIZE_MULTIPLIER (5.0f * 4.0f)
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040043#define BACKGROUND_RETENTION_PERCENTAGE (0.5f)
44
Derek Sollenbergerb9e296e2019-04-18 16:21:42 -040045CacheManager::CacheManager(const DisplayInfo& display)
46 : mMaxSurfaceArea(display.w * display.h)
47 , mMaxResourceBytes(mMaxSurfaceArea * SURFACE_SIZE_MULTIPLIER)
48 , mBackgroundResourceBytes(mMaxResourceBytes * BACKGROUND_RETENTION_PERCENTAGE)
49 // This sets the maximum size for a single texture atlas in the GPU font cache. If
50 // necessary, the cache can allocate additional textures that are counted against the
51 // total cache limits provided to Skia.
52 , mMaxGpuFontAtlasBytes(GrNextSizePow2(mMaxSurfaceArea))
53 // This sets the maximum size of the CPU font cache to be at least the same size as the
54 // total number of GPU font caches (i.e. 4 separate GPU atlases).
55 , mMaxCpuFontCacheBytes(std::max(mMaxGpuFontAtlasBytes*4, SkGraphics::GetFontCacheLimit()))
56 , mBackgroundCpuFontCacheBytes(mMaxCpuFontCacheBytes * BACKGROUND_RETENTION_PERCENTAGE) {
57
58 SkGraphics::SetFontCacheLimit(mMaxCpuFontCacheBytes);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040059}
60
Greg Daniel660d6ec2017-12-08 11:44:27 -050061void CacheManager::reset(sk_sp<GrContext> context) {
62 if (context != mGrContext) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040063 destroy();
64 }
65
66 if (context) {
Greg Daniel660d6ec2017-12-08 11:44:27 -050067 mGrContext = std::move(context);
Robert Phillips57bb0bf2019-09-06 13:18:17 -040068 mGrContext->setResourceCacheLimit(mMaxResourceBytes);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040069 }
70}
71
72void CacheManager::destroy() {
73 // cleanup any caches here as the GrContext is about to go away...
74 mGrContext.reset(nullptr);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040075}
76
John Reck322b8ab2019-03-14 13:15:28 -070077class CommonPoolExecutor : public SkExecutor {
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040078public:
John Reck0fa0cbc2019-04-05 16:57:46 -070079 virtual void add(std::function<void(void)> func) override { CommonPool::post(std::move(func)); }
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040080};
81
John Reck322b8ab2019-03-14 13:15:28 -070082static CommonPoolExecutor sDefaultExecutor;
83
John Reck0fa0cbc2019-04-05 16:57:46 -070084void CacheManager::configureContext(GrContextOptions* contextOptions, const void* identity,
85 ssize_t size) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040086 contextOptions->fAllowPathMaskCaching = true;
Derek Sollenbergerb9e296e2019-04-18 16:21:42 -040087 contextOptions->fGlyphCacheTextureMaximumBytes = mMaxGpuFontAtlasBytes;
John Reck322b8ab2019-03-14 13:15:28 -070088 contextOptions->fExecutor = &sDefaultExecutor;
Stan Ilieve75ef1f2017-11-27 17:22:42 -050089
Yichi Chen9f959552018-03-29 21:21:54 +080090 auto& cache = skiapipeline::ShaderCache::get();
91 cache.initShaderDiskCache(identity, size);
92 contextOptions->fPersistentCache = &cache;
Christopher Dalton3fcb6972019-02-21 20:09:22 +000093 contextOptions->fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040094}
95
96void CacheManager::trimMemory(TrimMemoryMode mode) {
97 if (!mGrContext) {
98 return;
99 }
100
101 mGrContext->flush();
102
103 switch (mode) {
104 case TrimMemoryMode::Complete:
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400105 mGrContext->freeGpuResources();
Derek Sollenbergerb9e296e2019-04-18 16:21:42 -0400106 SkGraphics::PurgeAllCaches();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400107 break;
108 case TrimMemoryMode::UiHidden:
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -0400109 // Here we purge all the unlocked scratch resources and then toggle the resources cache
110 // limits between the background and max amounts. This causes the unlocked resources
111 // that have persistent data to be purged in LRU order.
112 mGrContext->purgeUnlockedResources(true);
Robert Phillips57bb0bf2019-09-06 13:18:17 -0400113 mGrContext->setResourceCacheLimit(mBackgroundResourceBytes);
114 mGrContext->setResourceCacheLimit(mMaxResourceBytes);
Derek Sollenbergerb9e296e2019-04-18 16:21:42 -0400115 SkGraphics::SetFontCacheLimit(mBackgroundCpuFontCacheBytes);
116 SkGraphics::SetFontCacheLimit(mMaxCpuFontCacheBytes);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400117 break;
118 }
Greg Daniel1d857f02019-04-17 12:18:50 -0400119
120 // We must sync the cpu to make sure deletions of resources still queued up on the GPU actually
121 // happen.
122 mGrContext->flush(kSyncCpu_GrFlushFlag, 0, nullptr);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400123}
124
125void CacheManager::trimStaleResources() {
126 if (!mGrContext) {
127 return;
128 }
129 mGrContext->flush();
John Reckf846aee2019-10-08 23:28:41 +0000130 mGrContext->purgeResourcesNotUsedInMs(std::chrono::seconds(30));
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400131}
132
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400133void CacheManager::dumpMemoryUsage(String8& log, const RenderState* renderState) {
134 if (!mGrContext) {
135 log.appendFormat("No valid cache instance.\n");
136 return;
137 }
138
Derek Sollenberger0057db22018-03-29 14:18:44 -0400139 log.appendFormat("Font Cache (CPU):\n");
140 log.appendFormat(" Size: %.2f kB \n", SkGraphics::GetFontCacheUsed() / 1024.0f);
141 log.appendFormat(" Glyph Count: %d \n", SkGraphics::GetFontCacheCountUsed());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400142
Derek Sollenberger0057db22018-03-29 14:18:44 -0400143 log.appendFormat("CPU Caches:\n");
144 std::vector<skiapipeline::ResourcePair> cpuResourceMap = {
145 {"skia/sk_resource_cache/bitmap_", "Bitmaps"},
146 {"skia/sk_resource_cache/rrect-blur_", "Masks"},
147 {"skia/sk_resource_cache/rects-blur_", "Masks"},
148 {"skia/sk_resource_cache/tessellated", "Shadows"},
149 };
150 skiapipeline::SkiaMemoryTracer cpuTracer(cpuResourceMap, false);
151 SkGraphics::DumpMemoryStatistics(&cpuTracer);
152 cpuTracer.logOutput(log);
153
154 log.appendFormat("GPU Caches:\n");
155 skiapipeline::SkiaMemoryTracer gpuTracer("category", true);
156 mGrContext->dumpMemoryStatistics(&gpuTracer);
157 gpuTracer.logOutput(log);
158
159 log.appendFormat("Other Caches:\n");
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400160 log.appendFormat(" Current / Maximum\n");
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400161
162 if (renderState) {
163 if (renderState->mActiveLayers.size() > 0) {
164 log.appendFormat(" Layer Info:\n");
165 }
166
Stan Iliev564ca3e2018-09-04 22:00:00 +0000167 const char* layerType = Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL
John Reck0fa0cbc2019-04-05 16:57:46 -0700168 ? "GlLayer"
169 : "VkLayer";
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400170 size_t layerMemoryTotal = 0;
171 for (std::set<Layer*>::iterator it = renderState->mActiveLayers.begin();
John Reck1bcacfd2017-11-03 10:12:19 -0700172 it != renderState->mActiveLayers.end(); it++) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400173 const Layer* layer = *it;
John Reck1bcacfd2017-11-03 10:12:19 -0700174 log.appendFormat(" %s size %dx%d\n", layerType, layer->getWidth(),
175 layer->getHeight());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400176 layerMemoryTotal += layer->getWidth() * layer->getHeight() * 4;
177 }
Derek Sollenberger0057db22018-03-29 14:18:44 -0400178 log.appendFormat(" Layers Total %6.2f KB (numLayers = %zu)\n",
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400179 layerMemoryTotal / 1024.0f, renderState->mActiveLayers.size());
180 }
181
Derek Sollenberger0057db22018-03-29 14:18:44 -0400182 log.appendFormat("Total GPU memory usage:\n");
183 gpuTracer.logTotals(log);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400184}
185
186} /* namespace renderthread */
187} /* namespace uirenderer */
188} /* namespace android */