blob: a31081c9a4514257ee1de85effb6b5873c8746ee [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>
John Reck1bcacfd2017-11-03 10:12:19 -070031#include <gui/Surface.h>
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040032#include <math.h>
33#include <set>
34
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
John Reck322b8ab2019-03-14 13:15:28 -070079class CommonPoolExecutor : public SkExecutor {
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040080public:
John Reck0fa0cbc2019-04-05 16:57:46 -070081 virtual void add(std::function<void(void)> func) override { CommonPool::post(std::move(func)); }
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040082};
83
John Reck322b8ab2019-03-14 13:15:28 -070084static CommonPoolExecutor sDefaultExecutor;
85
John Reck0fa0cbc2019-04-05 16:57:46 -070086void CacheManager::configureContext(GrContextOptions* contextOptions, const void* identity,
87 ssize_t size) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040088 contextOptions->fAllowPathMaskCaching = true;
89
Stan Iliev43d06132018-12-21 13:14:15 -050090 // This sets the maximum size for a single texture atlas in the GPU font cache. If necessary,
91 // the cache can allocate additional textures that are counted against the total cache limits
92 // provided to Skia.
93 contextOptions->fGlyphCacheTextureMaximumBytes = GrNextSizePow2(mMaxSurfaceArea);
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040094
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();
114 break;
115 case TrimMemoryMode::UiHidden:
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -0400116 // Here we purge all the unlocked scratch resources and then toggle the resources cache
117 // limits between the background and max amounts. This causes the unlocked resources
118 // that have persistent data to be purged in LRU order.
119 mGrContext->purgeUnlockedResources(true);
120 mGrContext->setResourceCacheLimits(mMaxResources, mBackgroundResourceBytes);
121 mGrContext->setResourceCacheLimits(mMaxResources, mMaxResourceBytes);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400122 break;
123 }
124}
125
126void CacheManager::trimStaleResources() {
127 if (!mGrContext) {
128 return;
129 }
130 mGrContext->flush();
Derek Sollenberger92a9eb92018-04-12 13:42:19 -0400131 mGrContext->purgeResourcesNotUsedInMs(std::chrono::seconds(30));
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400132}
133
Stan Iliev3310fb12017-03-23 16:56:51 -0400134sp<skiapipeline::VectorDrawableAtlas> CacheManager::acquireVectorDrawableAtlas() {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400135 LOG_ALWAYS_FATAL_IF(mVectorDrawableAtlas.get() == nullptr);
136 LOG_ALWAYS_FATAL_IF(mGrContext == nullptr);
137
138 /**
Stan Iliev3310fb12017-03-23 16:56:51 -0400139 * TODO: define memory conditions where we clear the cache (e.g. surface->reset())
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400140 */
Stan Iliev3310fb12017-03-23 16:56:51 -0400141 return mVectorDrawableAtlas;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400142}
143
144void CacheManager::dumpMemoryUsage(String8& log, const RenderState* renderState) {
145 if (!mGrContext) {
146 log.appendFormat("No valid cache instance.\n");
147 return;
148 }
149
Derek Sollenberger0057db22018-03-29 14:18:44 -0400150 log.appendFormat("Font Cache (CPU):\n");
151 log.appendFormat(" Size: %.2f kB \n", SkGraphics::GetFontCacheUsed() / 1024.0f);
152 log.appendFormat(" Glyph Count: %d \n", SkGraphics::GetFontCacheCountUsed());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400153
Derek Sollenberger0057db22018-03-29 14:18:44 -0400154 log.appendFormat("CPU Caches:\n");
155 std::vector<skiapipeline::ResourcePair> cpuResourceMap = {
156 {"skia/sk_resource_cache/bitmap_", "Bitmaps"},
157 {"skia/sk_resource_cache/rrect-blur_", "Masks"},
158 {"skia/sk_resource_cache/rects-blur_", "Masks"},
159 {"skia/sk_resource_cache/tessellated", "Shadows"},
160 };
161 skiapipeline::SkiaMemoryTracer cpuTracer(cpuResourceMap, false);
162 SkGraphics::DumpMemoryStatistics(&cpuTracer);
163 cpuTracer.logOutput(log);
164
165 log.appendFormat("GPU Caches:\n");
166 skiapipeline::SkiaMemoryTracer gpuTracer("category", true);
167 mGrContext->dumpMemoryStatistics(&gpuTracer);
168 gpuTracer.logOutput(log);
169
170 log.appendFormat("Other Caches:\n");
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400171 log.appendFormat(" Current / Maximum\n");
Derek Sollenberger0057db22018-03-29 14:18:44 -0400172 log.appendFormat(" VectorDrawableAtlas %6.2f kB / %6.2f KB (entries = %zu)\n", 0.0f, 0.0f,
John Reck1bcacfd2017-11-03 10:12:19 -0700173 (size_t)0);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400174
175 if (renderState) {
176 if (renderState->mActiveLayers.size() > 0) {
177 log.appendFormat(" Layer Info:\n");
178 }
179
Stan Iliev564ca3e2018-09-04 22:00:00 +0000180 const char* layerType = Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL
John Reck0fa0cbc2019-04-05 16:57:46 -0700181 ? "GlLayer"
182 : "VkLayer";
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400183 size_t layerMemoryTotal = 0;
184 for (std::set<Layer*>::iterator it = renderState->mActiveLayers.begin();
John Reck1bcacfd2017-11-03 10:12:19 -0700185 it != renderState->mActiveLayers.end(); it++) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400186 const Layer* layer = *it;
John Reck1bcacfd2017-11-03 10:12:19 -0700187 log.appendFormat(" %s size %dx%d\n", layerType, layer->getWidth(),
188 layer->getHeight());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400189 layerMemoryTotal += layer->getWidth() * layer->getHeight() * 4;
190 }
Derek Sollenberger0057db22018-03-29 14:18:44 -0400191 log.appendFormat(" Layers Total %6.2f KB (numLayers = %zu)\n",
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400192 layerMemoryTotal / 1024.0f, renderState->mActiveLayers.size());
193 }
194
Derek Sollenberger0057db22018-03-29 14:18:44 -0400195 log.appendFormat("Total GPU memory usage:\n");
196 gpuTracer.logTotals(log);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400197}
198
199} /* namespace renderthread */
200} /* namespace uirenderer */
201} /* namespace android */