blob: af82672c6f23b4e5d708ce4337aadbaa4c915ee3 [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#ifndef CACHEMANAGER_H
18#define CACHEMANAGER_H
19
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +010020#ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
Adlai Hollerd2345212020-10-07 14:16:40 -040021#include <GrDirectContext.h>
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +010022#endif
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040023#include <SkSurface.h>
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040024#include <utils/String8.h>
25#include <vector>
Nader Jawaddd1fcab2021-06-10 18:54:23 -070026#include "utils/TimeUtils.h"
Derek Sollenberger8ec9e882017-08-24 16:36:08 -040027
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040028namespace android {
29
30class Surface;
31
32namespace uirenderer {
33
34class RenderState;
35
36namespace renderthread {
37
38class IRenderPipeline;
39class RenderThread;
40
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040041class CacheManager {
42public:
John Reck1bcacfd2017-11-03 10:12:19 -070043 enum class TrimMemoryMode { Complete, UiHidden };
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040044
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +010045#ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
Yichi Chen9f959552018-03-29 21:21:54 +080046 void configureContext(GrContextOptions* context, const void* identity, ssize_t size);
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +010047#endif
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040048 void trimMemory(TrimMemoryMode mode);
49 void trimStaleResources();
50 void dumpMemoryUsage(String8& log, const RenderState* renderState = nullptr);
John Reck39207682021-05-12 19:10:47 -040051 void getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage);
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040052
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040053 size_t getCacheSize() const { return mMaxResourceBytes; }
54 size_t getBackgroundCacheSize() const { return mBackgroundResourceBytes; }
Stan Ilieve0fae232020-01-07 17:21:49 -050055 void onFrameCompleted();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040056
Nader Jawaddd1fcab2021-06-10 18:54:23 -070057 void performDeferredCleanup(nsecs_t cleanupOlderThanMillis);
58
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040059private:
60 friend class RenderThread;
61
Alec Mouri22d753f2019-09-05 17:11:45 -070062 explicit CacheManager();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040063
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +010064#ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
Adlai Hollerd2345212020-10-07 14:16:40 -040065 void reset(sk_sp<GrDirectContext> grContext);
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +010066#endif
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040067 void destroy();
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040068
69 const size_t mMaxSurfaceArea;
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +010070#ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
Adlai Hollerd2345212020-10-07 14:16:40 -040071 sk_sp<GrDirectContext> mGrContext;
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +010072#endif
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040073
Derek Sollenbergerb9e296e2019-04-18 16:21:42 -040074 const size_t mMaxResourceBytes;
75 const size_t mBackgroundResourceBytes;
76
77 const size_t mMaxGpuFontAtlasBytes;
78 const size_t mMaxCpuFontCacheBytes;
79 const size_t mBackgroundCpuFontCacheBytes;
Derek Sollenbergerf9e45d12017-06-01 13:07:39 -040080};
81
82} /* namespace renderthread */
83} /* namespace uirenderer */
84} /* namespace android */
85
86#endif /* CACHEMANAGER_H */