blob: fc268138e071a016ddad26262a4f259cf08a9df9 [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.
42#define SURFACE_SIZE_MULTIPLIER (12.0f * 4.0f)
43#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);
59
John Reck1bcacfd2017-11-03 10:12:19 -070060 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(
61 mMaxSurfaceArea / 2,
Stan Ilievdd098e82017-08-09 09:42:38 -040062 skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040063}
64
Greg Daniel660d6ec2017-12-08 11:44:27 -050065void CacheManager::reset(sk_sp<GrContext> context) {
66 if (context != mGrContext) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040067 destroy();
68 }
69
70 if (context) {
Greg Daniel660d6ec2017-12-08 11:44:27 -050071 mGrContext = std::move(context);
Robert Phillips57bb0bf2019-09-06 13:18:17 -040072 mGrContext->setResourceCacheLimit(mMaxResourceBytes);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040073 }
74}
75
76void CacheManager::destroy() {
77 // cleanup any caches here as the GrContext is about to go away...
78 mGrContext.reset(nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -070079 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(
80 mMaxSurfaceArea / 2,
81 skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040082}
83
John Reck322b8ab2019-03-14 13:15:28 -070084class CommonPoolExecutor : public SkExecutor {
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040085public:
John Reck0fa0cbc2019-04-05 16:57:46 -070086 virtual void add(std::function<void(void)> func) override { CommonPool::post(std::move(func)); }
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040087};
88
John Reck322b8ab2019-03-14 13:15:28 -070089static CommonPoolExecutor sDefaultExecutor;
90
John Reck0fa0cbc2019-04-05 16:57:46 -070091void CacheManager::configureContext(GrContextOptions* contextOptions, const void* identity,
92 ssize_t size) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040093 contextOptions->fAllowPathMaskCaching = true;
Derek Sollenbergerb9e296e2019-04-18 16:21:42 -040094 contextOptions->fGlyphCacheTextureMaximumBytes = mMaxGpuFontAtlasBytes;
John Reck322b8ab2019-03-14 13:15:28 -070095 contextOptions->fExecutor = &sDefaultExecutor;
Stan Ilieve75ef1f2017-11-27 17:22:42 -050096
Yichi Chen9f959552018-03-29 21:21:54 +080097 auto& cache = skiapipeline::ShaderCache::get();
98 cache.initShaderDiskCache(identity, size);
99 contextOptions->fPersistentCache = &cache;
Christopher Dalton3fcb6972019-02-21 20:09:22 +0000100 contextOptions->fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400101}
102
103void CacheManager::trimMemory(TrimMemoryMode mode) {
104 if (!mGrContext) {
105 return;
106 }
107
108 mGrContext->flush();
109
110 switch (mode) {
111 case TrimMemoryMode::Complete:
John Reck1bcacfd2017-11-03 10:12:19 -0700112 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(mMaxSurfaceArea / 2);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400113 mGrContext->freeGpuResources();
Derek Sollenbergerb9e296e2019-04-18 16:21:42 -0400114 SkGraphics::PurgeAllCaches();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400115 break;
116 case TrimMemoryMode::UiHidden:
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -0400117 // Here we purge all the unlocked scratch resources and then toggle the resources cache
118 // limits between the background and max amounts. This causes the unlocked resources
119 // that have persistent data to be purged in LRU order.
120 mGrContext->purgeUnlockedResources(true);
Robert Phillips57bb0bf2019-09-06 13:18:17 -0400121 mGrContext->setResourceCacheLimit(mBackgroundResourceBytes);
122 mGrContext->setResourceCacheLimit(mMaxResourceBytes);
Derek Sollenbergerb9e296e2019-04-18 16:21:42 -0400123 SkGraphics::SetFontCacheLimit(mBackgroundCpuFontCacheBytes);
124 SkGraphics::SetFontCacheLimit(mMaxCpuFontCacheBytes);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400125 break;
126 }
Greg Daniel1d857f02019-04-17 12:18:50 -0400127
128 // We must sync the cpu to make sure deletions of resources still queued up on the GPU actually
129 // happen.
130 mGrContext->flush(kSyncCpu_GrFlushFlag, 0, nullptr);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400131}
132
133void CacheManager::trimStaleResources() {
134 if (!mGrContext) {
135 return;
136 }
137 mGrContext->flush();
John Reckf846aee2019-10-08 23:28:41 +0000138 mGrContext->purgeResourcesNotUsedInMs(std::chrono::seconds(30));
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400139}
140
Stan Iliev3310fb12017-03-23 16:56:51 -0400141sp<skiapipeline::VectorDrawableAtlas> CacheManager::acquireVectorDrawableAtlas() {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400142 LOG_ALWAYS_FATAL_IF(mVectorDrawableAtlas.get() == nullptr);
143 LOG_ALWAYS_FATAL_IF(mGrContext == nullptr);
144
145 /**
Stan Iliev3310fb12017-03-23 16:56:51 -0400146 * TODO: define memory conditions where we clear the cache (e.g. surface->reset())
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400147 */
Stan Iliev3310fb12017-03-23 16:56:51 -0400148 return mVectorDrawableAtlas;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400149}
150
151void CacheManager::dumpMemoryUsage(String8& log, const RenderState* renderState) {
152 if (!mGrContext) {
153 log.appendFormat("No valid cache instance.\n");
154 return;
155 }
156
Derek Sollenberger0057db22018-03-29 14:18:44 -0400157 log.appendFormat("Font Cache (CPU):\n");
158 log.appendFormat(" Size: %.2f kB \n", SkGraphics::GetFontCacheUsed() / 1024.0f);
159 log.appendFormat(" Glyph Count: %d \n", SkGraphics::GetFontCacheCountUsed());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400160
Derek Sollenberger0057db22018-03-29 14:18:44 -0400161 log.appendFormat("CPU Caches:\n");
162 std::vector<skiapipeline::ResourcePair> cpuResourceMap = {
163 {"skia/sk_resource_cache/bitmap_", "Bitmaps"},
164 {"skia/sk_resource_cache/rrect-blur_", "Masks"},
165 {"skia/sk_resource_cache/rects-blur_", "Masks"},
166 {"skia/sk_resource_cache/tessellated", "Shadows"},
167 };
168 skiapipeline::SkiaMemoryTracer cpuTracer(cpuResourceMap, false);
169 SkGraphics::DumpMemoryStatistics(&cpuTracer);
170 cpuTracer.logOutput(log);
171
172 log.appendFormat("GPU Caches:\n");
173 skiapipeline::SkiaMemoryTracer gpuTracer("category", true);
174 mGrContext->dumpMemoryStatistics(&gpuTracer);
175 gpuTracer.logOutput(log);
176
177 log.appendFormat("Other Caches:\n");
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400178 log.appendFormat(" Current / Maximum\n");
Derek Sollenberger0057db22018-03-29 14:18:44 -0400179 log.appendFormat(" VectorDrawableAtlas %6.2f kB / %6.2f KB (entries = %zu)\n", 0.0f, 0.0f,
John Reck1bcacfd2017-11-03 10:12:19 -0700180 (size_t)0);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400181
182 if (renderState) {
183 if (renderState->mActiveLayers.size() > 0) {
184 log.appendFormat(" Layer Info:\n");
185 }
186
Stan Iliev564ca3e2018-09-04 22:00:00 +0000187 const char* layerType = Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL
John Reck0fa0cbc2019-04-05 16:57:46 -0700188 ? "GlLayer"
189 : "VkLayer";
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400190 size_t layerMemoryTotal = 0;
191 for (std::set<Layer*>::iterator it = renderState->mActiveLayers.begin();
John Reck1bcacfd2017-11-03 10:12:19 -0700192 it != renderState->mActiveLayers.end(); it++) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400193 const Layer* layer = *it;
John Reck1bcacfd2017-11-03 10:12:19 -0700194 log.appendFormat(" %s size %dx%d\n", layerType, layer->getWidth(),
195 layer->getHeight());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400196 layerMemoryTotal += layer->getWidth() * layer->getHeight() * 4;
197 }
Derek Sollenberger0057db22018-03-29 14:18:44 -0400198 log.appendFormat(" Layers Total %6.2f KB (numLayers = %zu)\n",
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400199 layerMemoryTotal / 1024.0f, renderState->mActiveLayers.size());
200 }
201
Derek Sollenberger0057db22018-03-29 14:18:44 -0400202 log.appendFormat("Total GPU memory usage:\n");
203 gpuTracer.logTotals(log);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400204}
205
206} /* namespace renderthread */
207} /* namespace uirenderer */
208} /* namespace android */