blob: 1b638c12ac7b51c6838be85d814d8730da1230f0 [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
Derek Sollenbergerb9e296e2019-04-18 16:21:42 -040046CacheManager::CacheManager(const DisplayInfo& display)
47 : mMaxSurfaceArea(display.w * display.h)
48 , mMaxResourceBytes(mMaxSurfaceArea * SURFACE_SIZE_MULTIPLIER)
49 , mBackgroundResourceBytes(mMaxResourceBytes * BACKGROUND_RETENTION_PERCENTAGE)
50 // This sets the maximum size for a single texture atlas in the GPU font cache. If
51 // necessary, the cache can allocate additional textures that are counted against the
52 // total cache limits provided to Skia.
53 , mMaxGpuFontAtlasBytes(GrNextSizePow2(mMaxSurfaceArea))
54 // This sets the maximum size of the CPU font cache to be at least the same size as the
55 // total number of GPU font caches (i.e. 4 separate GPU atlases).
56 , mMaxCpuFontCacheBytes(std::max(mMaxGpuFontAtlasBytes*4, SkGraphics::GetFontCacheLimit()))
57 , mBackgroundCpuFontCacheBytes(mMaxCpuFontCacheBytes * BACKGROUND_RETENTION_PERCENTAGE) {
58
59 SkGraphics::SetFontCacheLimit(mMaxCpuFontCacheBytes);
60
John Reck1bcacfd2017-11-03 10:12:19 -070061 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(
62 mMaxSurfaceArea / 2,
Stan Ilievdd098e82017-08-09 09:42:38 -040063 skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040064}
65
Greg Daniel660d6ec2017-12-08 11:44:27 -050066void CacheManager::reset(sk_sp<GrContext> context) {
67 if (context != mGrContext) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040068 destroy();
69 }
70
71 if (context) {
Greg Daniel660d6ec2017-12-08 11:44:27 -050072 mGrContext = std::move(context);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040073 mGrContext->getResourceCacheLimits(&mMaxResources, nullptr);
Derek Sollenbergerb9e296e2019-04-18 16:21:42 -040074 mGrContext->setResourceCacheLimits(mMaxResources, mMaxResourceBytes);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040075 }
76}
77
78void CacheManager::destroy() {
79 // cleanup any caches here as the GrContext is about to go away...
80 mGrContext.reset(nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -070081 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(
82 mMaxSurfaceArea / 2,
83 skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040084}
85
John Reck322b8ab2019-03-14 13:15:28 -070086class CommonPoolExecutor : public SkExecutor {
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040087public:
John Reck0fa0cbc2019-04-05 16:57:46 -070088 virtual void add(std::function<void(void)> func) override { CommonPool::post(std::move(func)); }
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040089};
90
John Reck322b8ab2019-03-14 13:15:28 -070091static CommonPoolExecutor sDefaultExecutor;
92
John Reck0fa0cbc2019-04-05 16:57:46 -070093void CacheManager::configureContext(GrContextOptions* contextOptions, const void* identity,
94 ssize_t size) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040095 contextOptions->fAllowPathMaskCaching = true;
Derek Sollenbergerb9e296e2019-04-18 16:21:42 -040096 contextOptions->fGlyphCacheTextureMaximumBytes = mMaxGpuFontAtlasBytes;
John Reck322b8ab2019-03-14 13:15:28 -070097 contextOptions->fExecutor = &sDefaultExecutor;
Stan Ilieve75ef1f2017-11-27 17:22:42 -050098
Yichi Chen9f959552018-03-29 21:21:54 +080099 auto& cache = skiapipeline::ShaderCache::get();
100 cache.initShaderDiskCache(identity, size);
101 contextOptions->fPersistentCache = &cache;
Christopher Dalton3fcb6972019-02-21 20:09:22 +0000102 contextOptions->fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400103}
104
105void CacheManager::trimMemory(TrimMemoryMode mode) {
106 if (!mGrContext) {
107 return;
108 }
109
110 mGrContext->flush();
111
112 switch (mode) {
113 case TrimMemoryMode::Complete:
John Reck1bcacfd2017-11-03 10:12:19 -0700114 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(mMaxSurfaceArea / 2);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400115 mGrContext->freeGpuResources();
Derek Sollenbergerb9e296e2019-04-18 16:21:42 -0400116 SkGraphics::PurgeAllCaches();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400117 break;
118 case TrimMemoryMode::UiHidden:
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -0400119 // Here we purge all the unlocked scratch resources and then toggle the resources cache
120 // limits between the background and max amounts. This causes the unlocked resources
121 // that have persistent data to be purged in LRU order.
122 mGrContext->purgeUnlockedResources(true);
123 mGrContext->setResourceCacheLimits(mMaxResources, mBackgroundResourceBytes);
124 mGrContext->setResourceCacheLimits(mMaxResources, mMaxResourceBytes);
Derek Sollenbergerb9e296e2019-04-18 16:21:42 -0400125 SkGraphics::SetFontCacheLimit(mBackgroundCpuFontCacheBytes);
126 SkGraphics::SetFontCacheLimit(mMaxCpuFontCacheBytes);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400127 break;
128 }
Greg Daniel1d857f02019-04-17 12:18:50 -0400129
130 // We must sync the cpu to make sure deletions of resources still queued up on the GPU actually
131 // happen.
132 mGrContext->flush(kSyncCpu_GrFlushFlag, 0, nullptr);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400133}
134
135void CacheManager::trimStaleResources() {
136 if (!mGrContext) {
137 return;
138 }
139 mGrContext->flush();
Derek Sollenberger92a9eb92018-04-12 13:42:19 -0400140 mGrContext->purgeResourcesNotUsedInMs(std::chrono::seconds(30));
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400141}
142
Stan Iliev3310fb12017-03-23 16:56:51 -0400143sp<skiapipeline::VectorDrawableAtlas> CacheManager::acquireVectorDrawableAtlas() {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400144 LOG_ALWAYS_FATAL_IF(mVectorDrawableAtlas.get() == nullptr);
145 LOG_ALWAYS_FATAL_IF(mGrContext == nullptr);
146
147 /**
Stan Iliev3310fb12017-03-23 16:56:51 -0400148 * TODO: define memory conditions where we clear the cache (e.g. surface->reset())
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400149 */
Stan Iliev3310fb12017-03-23 16:56:51 -0400150 return mVectorDrawableAtlas;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400151}
152
153void CacheManager::dumpMemoryUsage(String8& log, const RenderState* renderState) {
154 if (!mGrContext) {
155 log.appendFormat("No valid cache instance.\n");
156 return;
157 }
158
Derek Sollenberger0057db22018-03-29 14:18:44 -0400159 log.appendFormat("Font Cache (CPU):\n");
160 log.appendFormat(" Size: %.2f kB \n", SkGraphics::GetFontCacheUsed() / 1024.0f);
161 log.appendFormat(" Glyph Count: %d \n", SkGraphics::GetFontCacheCountUsed());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400162
Derek Sollenberger0057db22018-03-29 14:18:44 -0400163 log.appendFormat("CPU Caches:\n");
164 std::vector<skiapipeline::ResourcePair> cpuResourceMap = {
165 {"skia/sk_resource_cache/bitmap_", "Bitmaps"},
166 {"skia/sk_resource_cache/rrect-blur_", "Masks"},
167 {"skia/sk_resource_cache/rects-blur_", "Masks"},
168 {"skia/sk_resource_cache/tessellated", "Shadows"},
169 };
170 skiapipeline::SkiaMemoryTracer cpuTracer(cpuResourceMap, false);
171 SkGraphics::DumpMemoryStatistics(&cpuTracer);
172 cpuTracer.logOutput(log);
173
174 log.appendFormat("GPU Caches:\n");
175 skiapipeline::SkiaMemoryTracer gpuTracer("category", true);
176 mGrContext->dumpMemoryStatistics(&gpuTracer);
177 gpuTracer.logOutput(log);
178
179 log.appendFormat("Other Caches:\n");
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400180 log.appendFormat(" Current / Maximum\n");
Derek Sollenberger0057db22018-03-29 14:18:44 -0400181 log.appendFormat(" VectorDrawableAtlas %6.2f kB / %6.2f KB (entries = %zu)\n", 0.0f, 0.0f,
John Reck1bcacfd2017-11-03 10:12:19 -0700182 (size_t)0);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400183
184 if (renderState) {
185 if (renderState->mActiveLayers.size() > 0) {
186 log.appendFormat(" Layer Info:\n");
187 }
188
Stan Iliev564ca3e2018-09-04 22:00:00 +0000189 const char* layerType = Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL
John Reck0fa0cbc2019-04-05 16:57:46 -0700190 ? "GlLayer"
191 : "VkLayer";
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400192 size_t layerMemoryTotal = 0;
193 for (std::set<Layer*>::iterator it = renderState->mActiveLayers.begin();
John Reck1bcacfd2017-11-03 10:12:19 -0700194 it != renderState->mActiveLayers.end(); it++) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400195 const Layer* layer = *it;
John Reck1bcacfd2017-11-03 10:12:19 -0700196 log.appendFormat(" %s size %dx%d\n", layerType, layer->getWidth(),
197 layer->getHeight());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400198 layerMemoryTotal += layer->getWidth() * layer->getHeight() * 4;
199 }
Derek Sollenberger0057db22018-03-29 14:18:44 -0400200 log.appendFormat(" Layers Total %6.2f KB (numLayers = %zu)\n",
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400201 layerMemoryTotal / 1024.0f, renderState->mActiveLayers.size());
202 }
203
Derek Sollenberger0057db22018-03-29 14:18:44 -0400204 log.appendFormat("Total GPU memory usage:\n");
205 gpuTracer.logTotals(log);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400206}
207
208} /* namespace renderthread */
209} /* namespace uirenderer */
210} /* namespace android */