1 /*
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 
20 #ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
21 #include <GrDirectContext.h>
22 #endif
23 #include <SkSurface.h>
24 #include <utils/String8.h>
25 
26 #include <vector>
27 
28 #include "MemoryPolicy.h"
29 #include "utils/RingBuffer.h"
30 #include "utils/TimeUtils.h"
31 
32 namespace android {
33 
34 class Surface;
35 
36 namespace uirenderer {
37 
38 class RenderState;
39 
40 namespace renderthread {
41 
42 class RenderThread;
43 class CanvasContext;
44 
45 class CacheManager {
46 public:
47 #ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
48     void configureContext(GrContextOptions* context, const void* identity, ssize_t size);
49 #endif
50     void trimMemory(TrimLevel mode);
51     void trimCaches(CacheTrimLevel mode);
52     void trimStaleResources();
53     void dumpMemoryUsage(String8& log, const RenderState* renderState = nullptr);
54     void getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage);
55 
getCacheSize()56     size_t getCacheSize() const { return mMaxResourceBytes; }
getBackgroundCacheSize()57     size_t getBackgroundCacheSize() const { return mBackgroundResourceBytes; }
58     void onFrameCompleted();
59     void notifyNextFrameSize(int width, int height);
60 
61     void onThreadIdle();
62 
63     void registerCanvasContext(CanvasContext* context);
64     void unregisterCanvasContext(CanvasContext* context);
65     void onContextStopped(CanvasContext* context);
66 
67 private:
68     friend class RenderThread;
69 
70     explicit CacheManager(RenderThread& thread);
71     void setupCacheLimits();
72     bool areAllContextsStopped();
73     void checkUiHidden();
74     void scheduleDestroyContext();
75     void cancelDestroyContext();
76 
77 #ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
78     void reset(sk_sp<GrDirectContext> grContext);
79 #endif
80     void destroy();
81 
82     RenderThread& mRenderThread;
83     const MemoryPolicy& mMemoryPolicy;
84 #ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
85     sk_sp<GrDirectContext> mGrContext;
86 #endif
87 
88     size_t mMaxSurfaceArea = 0;
89 
90     size_t mMaxResourceBytes = 0;
91     size_t mBackgroundResourceBytes = 0;
92 
93     size_t mMaxGpuFontAtlasBytes = 0;
94     size_t mMaxCpuFontCacheBytes = 0;
95     size_t mBackgroundCpuFontCacheBytes = 0;
96 
97     std::vector<CanvasContext*> mCanvasContexts;
98     RingBuffer<uint64_t, 100> mFrameCompletions;
99 
100     nsecs_t mLastDeferredCleanup = 0;
101     bool mIsDestructionPending = false;
102     uint32_t mGenerationId = 0;
103 };
104 
105 } /* namespace renderthread */
106 } /* namespace uirenderer */
107 } /* namespace android */
108 
109 #endif /* CACHEMANAGER_H */
110