blob: 8b02c11911ca4fa48b76f3f59b71a6adadada1b0 [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"
Stan Iliev564ca3e2018-09-04 22:00:00 +000024#include "Properties.h"
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040025#include "renderstate/RenderState.h"
John Reck322b8ab2019-03-14 13:15:28 -070026#include "thread/CommonPool.h"
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040027
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040028#include <GrContextOptions.h>
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040029#include <SkExecutor.h>
Derek Sollenberger0057db22018-03-29 14:18:44 -040030#include <SkGraphics.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>
Stan Iliev43d06132018-12-21 13:14:15 -050034#include <SkMathPriv.h>
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040035
36namespace android {
37namespace uirenderer {
38namespace renderthread {
39
40// This multiplier was selected based on historical review of cache sizes relative
41// to the screen resolution. This is meant to be a conservative default based on
42// that analysis. The 4.0f is used because the default pixel format is assumed to
43// be ARGB_8888.
44#define SURFACE_SIZE_MULTIPLIER (12.0f * 4.0f)
45#define BACKGROUND_RETENTION_PERCENTAGE (0.5f)
46
John Reck1bcacfd2017-11-03 10:12:19 -070047CacheManager::CacheManager(const DisplayInfo& display) : mMaxSurfaceArea(display.w * display.h) {
48 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(
49 mMaxSurfaceArea / 2,
Stan Ilievdd098e82017-08-09 09:42:38 -040050 skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040051}
52
Greg Daniel660d6ec2017-12-08 11:44:27 -050053void CacheManager::reset(sk_sp<GrContext> context) {
54 if (context != mGrContext) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040055 destroy();
56 }
57
58 if (context) {
Greg Daniel660d6ec2017-12-08 11:44:27 -050059 mGrContext = std::move(context);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040060 mGrContext->getResourceCacheLimits(&mMaxResources, nullptr);
61 updateContextCacheSizes();
62 }
63}
64
65void CacheManager::destroy() {
66 // cleanup any caches here as the GrContext is about to go away...
67 mGrContext.reset(nullptr);
John Reck1bcacfd2017-11-03 10:12:19 -070068 mVectorDrawableAtlas = new skiapipeline::VectorDrawableAtlas(
69 mMaxSurfaceArea / 2,
70 skiapipeline::VectorDrawableAtlas::StorageMode::disallowSharedSurface);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040071}
72
73void CacheManager::updateContextCacheSizes() {
74 mMaxResourceBytes = mMaxSurfaceArea * SURFACE_SIZE_MULTIPLIER;
75 mBackgroundResourceBytes = mMaxResourceBytes * BACKGROUND_RETENTION_PERCENTAGE;
76
77 mGrContext->setResourceCacheLimits(mMaxResources, mMaxResourceBytes);
78}
79
John Reck322b8ab2019-03-14 13:15:28 -070080class CommonPoolExecutor : public SkExecutor {
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040081public:
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040082 virtual void add(std::function<void(void)> func) override {
John Reck322b8ab2019-03-14 13:15:28 -070083 CommonPool::post(std::move(func));
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040084 }
85};
86
John Reck322b8ab2019-03-14 13:15:28 -070087static CommonPoolExecutor sDefaultExecutor;
88
Yichi Chen9f959552018-03-29 21:21:54 +080089void CacheManager::configureContext(GrContextOptions* contextOptions, const void* identity, ssize_t size) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040090 contextOptions->fAllowPathMaskCaching = true;
91
Stan Iliev43d06132018-12-21 13:14:15 -050092 // This sets the maximum size for a single texture atlas in the GPU font cache. If necessary,
93 // the cache can allocate additional textures that are counted against the total cache limits
94 // provided to Skia.
95 contextOptions->fGlyphCacheTextureMaximumBytes = GrNextSizePow2(mMaxSurfaceArea);
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040096
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();
116 break;
117 case TrimMemoryMode::UiHidden:
Derek Sollenbergerb1f27aa2018-04-02 13:36:45 -0400118 // Here we purge all the unlocked scratch resources and then toggle the resources cache
119 // limits between the background and max amounts. This causes the unlocked resources
120 // that have persistent data to be purged in LRU order.
121 mGrContext->purgeUnlockedResources(true);
122 mGrContext->setResourceCacheLimits(mMaxResources, mBackgroundResourceBytes);
123 mGrContext->setResourceCacheLimits(mMaxResources, mMaxResourceBytes);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400124 break;
125 }
126}
127
128void CacheManager::trimStaleResources() {
129 if (!mGrContext) {
130 return;
131 }
132 mGrContext->flush();
Derek Sollenberger92a9eb92018-04-12 13:42:19 -0400133 mGrContext->purgeResourcesNotUsedInMs(std::chrono::seconds(30));
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400134}
135
Stan Iliev3310fb12017-03-23 16:56:51 -0400136sp<skiapipeline::VectorDrawableAtlas> CacheManager::acquireVectorDrawableAtlas() {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400137 LOG_ALWAYS_FATAL_IF(mVectorDrawableAtlas.get() == nullptr);
138 LOG_ALWAYS_FATAL_IF(mGrContext == nullptr);
139
140 /**
Stan Iliev3310fb12017-03-23 16:56:51 -0400141 * TODO: define memory conditions where we clear the cache (e.g. surface->reset())
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400142 */
Stan Iliev3310fb12017-03-23 16:56:51 -0400143 return mVectorDrawableAtlas;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400144}
145
146void CacheManager::dumpMemoryUsage(String8& log, const RenderState* renderState) {
147 if (!mGrContext) {
148 log.appendFormat("No valid cache instance.\n");
149 return;
150 }
151
Derek Sollenberger0057db22018-03-29 14:18:44 -0400152 log.appendFormat("Font Cache (CPU):\n");
153 log.appendFormat(" Size: %.2f kB \n", SkGraphics::GetFontCacheUsed() / 1024.0f);
154 log.appendFormat(" Glyph Count: %d \n", SkGraphics::GetFontCacheCountUsed());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400155
Derek Sollenberger0057db22018-03-29 14:18:44 -0400156 log.appendFormat("CPU Caches:\n");
157 std::vector<skiapipeline::ResourcePair> cpuResourceMap = {
158 {"skia/sk_resource_cache/bitmap_", "Bitmaps"},
159 {"skia/sk_resource_cache/rrect-blur_", "Masks"},
160 {"skia/sk_resource_cache/rects-blur_", "Masks"},
161 {"skia/sk_resource_cache/tessellated", "Shadows"},
162 };
163 skiapipeline::SkiaMemoryTracer cpuTracer(cpuResourceMap, false);
164 SkGraphics::DumpMemoryStatistics(&cpuTracer);
165 cpuTracer.logOutput(log);
166
167 log.appendFormat("GPU Caches:\n");
168 skiapipeline::SkiaMemoryTracer gpuTracer("category", true);
169 mGrContext->dumpMemoryStatistics(&gpuTracer);
170 gpuTracer.logOutput(log);
171
172 log.appendFormat("Other Caches:\n");
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400173 log.appendFormat(" Current / Maximum\n");
Derek Sollenberger0057db22018-03-29 14:18:44 -0400174 log.appendFormat(" VectorDrawableAtlas %6.2f kB / %6.2f KB (entries = %zu)\n", 0.0f, 0.0f,
John Reck1bcacfd2017-11-03 10:12:19 -0700175 (size_t)0);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400176
177 if (renderState) {
178 if (renderState->mActiveLayers.size() > 0) {
179 log.appendFormat(" Layer Info:\n");
180 }
181
Stan Iliev564ca3e2018-09-04 22:00:00 +0000182 const char* layerType = Properties::getRenderPipelineType() == RenderPipelineType::SkiaGL
183 ? "GlLayer" : "VkLayer";
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400184 size_t layerMemoryTotal = 0;
185 for (std::set<Layer*>::iterator it = renderState->mActiveLayers.begin();
John Reck1bcacfd2017-11-03 10:12:19 -0700186 it != renderState->mActiveLayers.end(); it++) {
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400187 const Layer* layer = *it;
John Reck1bcacfd2017-11-03 10:12:19 -0700188 log.appendFormat(" %s size %dx%d\n", layerType, layer->getWidth(),
189 layer->getHeight());
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400190 layerMemoryTotal += layer->getWidth() * layer->getHeight() * 4;
191 }
Derek Sollenberger0057db22018-03-29 14:18:44 -0400192 log.appendFormat(" Layers Total %6.2f KB (numLayers = %zu)\n",
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400193 layerMemoryTotal / 1024.0f, renderState->mActiveLayers.size());
194 }
195
Derek Sollenberger0057db22018-03-29 14:18:44 -0400196 log.appendFormat("Total GPU memory usage:\n");
197 gpuTracer.logTotals(log);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -0400198}
199
200} /* namespace renderthread */
201} /* namespace uirenderer */
202} /* namespace android */