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 #include <GrContext.h>
21 #include <SkSurface.h>
22 #include <ui/DisplayInfo.h>
23 #include <utils/String8.h>
24 #include <vector>
25 
26 #include "pipeline/skia/VectorDrawableAtlas.h"
27 #include "thread/TaskManager.h"
28 #include "thread/TaskProcessor.h"
29 
30 namespace android {
31 
32 class Surface;
33 
34 namespace uirenderer {
35 
36 class RenderState;
37 
38 namespace renderthread {
39 
40 class IRenderPipeline;
41 class RenderThread;
42 
43 class CacheManager {
44 public:
45     enum class TrimMemoryMode { Complete, UiHidden };
46 
47     void configureContext(GrContextOptions* context);
48     void trimMemory(TrimMemoryMode mode);
49     void trimStaleResources();
50     void dumpMemoryUsage(String8& log, const RenderState* renderState = nullptr);
51 
52     sp<skiapipeline::VectorDrawableAtlas> acquireVectorDrawableAtlas();
53 
getCacheSize()54     size_t getCacheSize() const { return mMaxResourceBytes; }
getBackgroundCacheSize()55     size_t getBackgroundCacheSize() const { return mBackgroundResourceBytes; }
56 
getTaskManager()57     TaskManager* getTaskManager() { return &mTaskManager; }
58 
59 private:
60     friend class RenderThread;
61 
62     CacheManager(const DisplayInfo& display);
63 
64     void reset(sk_sp<GrContext> grContext);
65     void destroy();
66     void updateContextCacheSizes();
67 
68     const size_t mMaxSurfaceArea;
69     sk_sp<GrContext> mGrContext;
70 
71     int mMaxResources = 0;
72     size_t mMaxResourceBytes = 0;
73     size_t mBackgroundResourceBytes = 0;
74 
75     struct PipelineProps {
76         const void* pipelineKey = nullptr;
77         size_t surfaceArea = 0;
78     };
79 
80     sp<skiapipeline::VectorDrawableAtlas> mVectorDrawableAtlas;
81 
82     class SkiaTaskProcessor;
83     sp<SkiaTaskProcessor> mTaskProcessor;
84     TaskManager mTaskManager;
85 };
86 
87 } /* namespace renderthread */
88 } /* namespace uirenderer */
89 } /* namespace android */
90 
91 #endif /* CACHEMANAGER_H */
92