1 /*
2  * Copyright (C) 2012 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 /**
18  * Project HWC 2.0 Design
19  */
20 
21 #ifndef _EXYNOSRESOURCEMANAGER_H
22 #define _EXYNOSRESOURCEMANAGER_H
23 
24 #include <unordered_map>
25 #include "ExynosDevice.h"
26 #include "ExynosDisplay.h"
27 #include "ExynosHWCHelper.h"
28 #include "ExynosMPPModule.h"
29 #include "ExynosResourceRestriction.h"
30 
31 using namespace android;
32 
33 class ExynosDevice;
34 class ExynosDisplay;
35 class ExynosMPP;
36 
37 #define ASSIGN_RESOURCE_TRY_COUNT   100
38 
39 #define MAX_OVERLAY_LAYER_NUM       20
40 
41 #ifndef USE_MODULE_SW_FEATURE
42 const std::map<mpp_phycal_type_t, uint64_t> sw_feature_table =
43 {
44     {MPP_DPP_G, MPP_ATTR_DIM},
45     {MPP_DPP_GF, MPP_ATTR_DIM},
46     {MPP_DPP_VG, MPP_ATTR_DIM},
47     {MPP_DPP_VGS, MPP_ATTR_DIM},
48     {MPP_DPP_VGF, MPP_ATTR_DIM},
49     {MPP_DPP_VGFS, MPP_ATTR_DIM},
50     {MPP_DPP_VGRFS, MPP_ATTR_DIM},
51 };
52 #endif
53 
54 #ifndef USE_MODULE_DPU_ATTR_MAP
55 const dpu_attr_map_t dpu_attr_map_table [] =
56 {
57     {DPP_ATTR_AFBC, MPP_ATTR_AFBC},
58     {DPP_ATTR_BLOCK, MPP_ATTR_BLOCK_MODE},
59     {DPP_ATTR_FLIP, MPP_ATTR_FLIP_H | MPP_ATTR_FLIP_V},
60     {DPP_ATTR_ROT, MPP_ATTR_ROT_90},
61     {DPP_ATTR_SCALE, MPP_ATTR_SCALE},
62     {DPP_ATTR_HDR, MPP_ATTR_HDR10},
63     {DPP_ATTR_C_HDR, MPP_ATTR_HDR10},
64     {DPP_ATTR_C_HDR10_PLUS, MPP_ATTR_HDR10PLUS},
65     {DPP_ATTR_WCG, MPP_ATTR_WCG},
66 };
67 #endif
68 
69 /* Based on multi-resolution feature */
70 enum dst_realloc_state {
71     DST_REALLOC_DONE = 0,
72     DST_REALLOC_START,
73     DST_REALLOC_GOING,
74 };
75 
76 class ExynosMPPVector : public android::SortedVector< ExynosMPP* > {
77     public:
78         ExynosMPPVector();
79         ExynosMPPVector(const ExynosMPPVector& rhs);
80         virtual int do_compare(const void* lhs, const void* rhs) const;
81 };
82 
83 class ExynosResourceManager {
84     private:
85     class DstBufMgrThread: public Thread {
86         private:
87             ExynosResourceManager *mExynosResourceManager;
88             Condition mCondition;
89         public:
90             bool mRunning;
91             Mutex mMutex;
92             Mutex mStateMutex;
93             Mutex mResInfoMutex;
94             uint32_t mBufXres;
95             uint32_t mBufYres;
96             void reallocDstBufs(uint32_t Xres, uint32_t Yres);
97             bool needDstRealloc(uint32_t Xres, uint32_t Yres, ExynosMPP *m2mMPP);
98             DstBufMgrThread(ExynosResourceManager *exynosResourceManager);
99             ~DstBufMgrThread();
100             virtual bool threadLoop();
101     };
102 
103     public:
104         uint32_t mForceReallocState;
105         ExynosDevice *mDevice;
106         bool hasHdrLayer;
107         bool hasDrmLayer;
108         bool isHdrExternal;
109 
110         uint32_t mFormatRestrictionCnt;
111         uint32_t mSizeRestrictionCnt[RESTRICTION_MAX];
112         restriction_key_t mFormatRestrictions[RESTRICTION_CNT_MAX];
113         restriction_size_element_t mSizeRestrictions[RESTRICTION_MAX][RESTRICTION_CNT_MAX];
114 
115         std::unordered_map<uint32_t /* physical type */, uint64_t /* attribute */> mMPPAttrs;
116 
117         ExynosResourceManager(ExynosDevice *device);
118         virtual ~ExynosResourceManager();
119         void reloadResourceForHWFC();
120         void setTargetDisplayLuminance(uint16_t min, uint16_t max);
121         void setTargetDisplayDevice(int device);
122         int32_t doPreProcessing();
123         void doReallocDstBufs(uint32_t Xres, uint32_t Yres);
124         int32_t doAllocDstBufs(uint32_t mXres, uint32_t mYres);
125         int32_t assignResource(ExynosDisplay *display);
126         int32_t assignResourceInternal(ExynosDisplay *display);
127         static ExynosMPP* getExynosMPP(uint32_t type);
128         static ExynosMPP* getExynosMPP(uint32_t physicalType, uint32_t physicalIndex);
129         static void enableMPP(uint32_t physicalType, uint32_t physicalIndex, uint32_t logicalIndex, uint32_t enable);
130         static void setScaleDownRatio(uint32_t physicalType,
131                 uint32_t physicalIndex, uint32_t logicalIndex,
132                 uint32_t scaleDownRatio);
133         int32_t updateSupportedMPPFlag(ExynosDisplay * display);
134         int32_t resetResources();
135         int32_t preAssignResources();
136         void preAssignWindows();
137         int32_t preProcessLayer(ExynosDisplay *display);
138         int32_t resetAssignedResources(ExynosDisplay *display, bool forceReset = false);
139         virtual int32_t assignCompositionTarget(ExynosDisplay *display, uint32_t targetType);
140         int32_t validateLayer(uint32_t index, ExynosDisplay *display, ExynosLayer *layer);
141         int32_t assignLayers(ExynosDisplay *display, uint32_t priority);
142         virtual int32_t assignLayer(ExynosDisplay *display, ExynosLayer *layer, uint32_t layer_index,
143                 exynos_image &m2m_out_img, ExynosMPP **m2mMPP, ExynosMPP **otfMPP, uint32_t &overlayInfo);
144         virtual int32_t checkScenario(ExynosDisplay *display);
145         virtual int32_t assignWindow(ExynosDisplay *display);
146         int32_t updateResourceState();
147         static float getResourceUsedCapa(ExynosMPP &mpp);
148         int32_t updateExynosComposition(ExynosDisplay *display);
149         int32_t updateClientComposition(ExynosDisplay *display);
150         int32_t getCandidateM2mMPPOutImages(ExynosDisplay *display,
151                 ExynosLayer *layer, std::vector<exynos_image> &image_lists);
152         int32_t setResourcePriority(ExynosDisplay *display);
153         int32_t deliverPerformanceInfo();
154         int32_t prepareResources();
155         int32_t finishAssignResourceWork();
156         int32_t initResourcesState(ExynosDisplay *display);
157 
getOtfMPPSize()158         uint32_t getOtfMPPSize() {return (uint32_t)mOtfMPPs.size();};
getOtfMPP(uint32_t index)159         ExynosMPP* getOtfMPP(uint32_t index) {return mOtfMPPs[index];};
160 
getM2mMPPSize()161         uint32_t getM2mMPPSize() {return (uint32_t)mM2mMPPs.size();};
getM2mMPP(uint32_t index)162         ExynosMPP* getM2mMPP(uint32_t index) {return mM2mMPPs[index];};
163 
164         virtual void makeAcrylRestrictions(mpp_phycal_type_t type);
165         void makeSizeRestrictions(uint32_t mppId, const restriction_size_t &size,
166                                   restriction_classification_t format);
167         void makeFormatRestrictions(restriction_key_t table);
168 
169         void updateRestrictions();
170 
171         mpp_phycal_type_t getPhysicalType(int ch) const;
172         ExynosMPP* getOtfMPPWithChannel(int ch);
173         uint32_t getFeatureTableSize() const;
getOtfMPPs()174         const static ExynosMPPVector& getOtfMPPs() { return mOtfMPPs; };
175         float getM2MCapa(uint32_t physicalType);
176         virtual bool hasHDR10PlusMPP();
177         float getAssignedCapacity(uint32_t physicalType);
178 
179         void dump(String8 &result) const;
180         void setM2MCapa(uint32_t physicalType, uint32_t capa);
181 
182     private:
183         int32_t changeLayerFromClientToDevice(ExynosDisplay *display, ExynosLayer *layer,
184                 uint32_t layer_index, exynos_image m2m_out_img, ExynosMPP *m2mMPP, ExynosMPP *otfMPP);
185         void dump(const restriction_classification_t, String8 &result) const;
186 
187         sp<DstBufMgrThread> mDstBufMgrThread;
188 
189     protected:
190         virtual void setFrameRateForPerformance(ExynosMPP &mpp, AcrylicPerformanceRequestFrame *frame);
191         void getCandidateScalingM2mMPPOutImages(const ExynosDisplay *display,
192                                                 const exynos_image &src_img,
193                                                 const exynos_image &dst_img,
194                                                 std::vector<exynos_image> &image_lists);
195         exynos_image getAlignedImage(exynos_image image, const ExynosMPP *m2mMpp,
196                                      const ExynosMPP *otfMpp) const;
197         static ExynosMPPVector mOtfMPPs;
198         static ExynosMPPVector mM2mMPPs;
199         uint32_t mResourceReserved; /* Set MPP logical type for bit operation */
200 };
201 
202 #endif //_EXYNOSRESOURCEMANAGER_H
203