• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     bool areAllContextsStopped();
68 
69 private:
70     friend class RenderThread;
71 
72     explicit CacheManager(RenderThread& thread);
73     void setupCacheLimits();
74     void checkUiHidden();
75     void scheduleDestroyContext();
76     void cancelDestroyContext();
77 
78 #ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
79     void reset(sk_sp<GrDirectContext> grContext);
80 #endif
81     void destroy();
82 
83     RenderThread& mRenderThread;
84     const MemoryPolicy& mMemoryPolicy;
85 #ifdef __ANDROID__ // Layoutlib does not support hardware acceleration
86     sk_sp<GrDirectContext> mGrContext;
87 #endif
88 
89     size_t mMaxSurfaceArea = 0;
90 
91     size_t mMaxResourceBytes = 0;
92     size_t mBackgroundResourceBytes = 0;
93 
94     size_t mMaxGpuFontAtlasBytes = 0;
95     size_t mMaxCpuFontCacheBytes = 0;
96     size_t mBackgroundCpuFontCacheBytes = 0;
97 
98     std::vector<CanvasContext*> mCanvasContexts;
99     RingBuffer<uint64_t, 100> mFrameCompletions;
100 
101     nsecs_t mLastDeferredCleanup = 0;
102     bool mIsDestructionPending = false;
103     uint32_t mGenerationId = 0;
104 };
105 
106 } /* namespace renderthread */
107 } /* namespace uirenderer */
108 } /* namespace android */
109 
110 #endif /* CACHEMANAGER_H */
111