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 #ifndef _EXYNOSDEVICE_H
18 #define _EXYNOSDEVICE_H
19 
20 #include <aidl/com/google/hardware/pixel/display/BnDisplay.h>
21 #include <cutils/atomic.h>
22 #include <displaycolor/displaycolor.h>
23 #include <hardware/hwcomposer2.h>
24 #include <hardware_legacy/uevent.h>
25 #include <sys/resource.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <utils/Condition.h>
30 #include <utils/Mutex.h>
31 #include <utils/Trace.h>
32 #include <utils/Vector.h>
33 
34 #include <atomic>
35 #include <thread>
36 
37 #include "ExynosHWC.h"
38 #include "ExynosHWCHelper.h"
39 #include "ExynosHWCModule.h"
40 
41 #define MAX_DEV_NAME 128
42 #define ERROR_LOG_PATH0 "/data/vendor/log/hwc"
43 #define ERROR_LOG_PATH1 "/data/log"
44 #define ERR_LOG_SIZE    (1024*1024)     // 1MB
45 #define FENCE_ERR_LOG_SIZE    (1024*1024)     // 1MB
46 
47 #ifndef DOZE_VSYNC_PERIOD
48 #define DOZE_VSYNC_PERIOD 33333333 // 30fps
49 #endif
50 
51 #ifndef DRM_DEVICE_PATH
52 #define DRM_DEVICE_PATH "/dev/dri/card0"
53 #endif
54 
55 #ifndef WRITEBACK_CAPTURE_PATH
56 #define WRITEBACK_CAPTURE_PATH "/data/vendor/log/hwc"
57 #endif
58 
59 using HbmState = ::aidl::com::google::hardware::pixel::display::HbmState;
60 using LbeState = ::aidl::com::google::hardware::pixel::display::LbeState;
61 
62 using namespace android;
63 
64 struct exynos_callback_info_t {
65     hwc2_callback_data_t callbackData;
66     hwc2_function_pointer_t funcPointer;
67 };
68 
69 typedef struct exynos_hwc_control {
70     uint32_t forceGpu;
71     uint32_t windowUpdate;
72     uint32_t forcePanic;
73     uint32_t skipStaticLayers;
74     uint32_t skipM2mProcessing;
75     uint32_t skipResourceAssign;
76     uint32_t multiResolution;
77     uint32_t dumpMidBuf;
78     uint32_t displayMode;
79     uint32_t setDDIScaler;
80     uint32_t useDynamicRecomp;
81     uint32_t skipWinConfig;
82     uint32_t skipValidate;
83     uint32_t doFenceFileDump;
84     uint32_t fenceTracer;
85     uint32_t sysFenceLogging;
86 } exynos_hwc_control_t;
87 
88 typedef struct update_time_info {
89     struct timeval lastUeventTime;
90     struct timeval lastEnableVsyncTime;
91     struct timeval lastDisableVsyncTime;
92     struct timeval lastValidateTime;
93     struct timeval lastPresentTime;
94 } update_time_info_t;
95 
96 enum {
97     GEOMETRY_LAYER_TYPE_CHANGED             = 1ULL << 0,
98     GEOMETRY_LAYER_DATASPACE_CHANGED        = 1ULL << 1,
99     GEOMETRY_LAYER_DISPLAYFRAME_CHANGED     = 1ULL << 2,
100     GEOMETRY_LAYER_SOURCECROP_CHANGED       = 1ULL << 3,
101     GEOMETRY_LAYER_TRANSFORM_CHANGED        = 1ULL << 4,
102     GEOMETRY_LAYER_ZORDER_CHANGED           = 1ULL << 5,
103     GEOMETRY_LAYER_FPS_CHANGED              = 1ULL << 6,
104     GEOMETRY_LAYER_FLAG_CHANGED             = 1ULL << 7,
105     GEOMETRY_LAYER_PRIORITY_CHANGED         = 1ULL << 8,
106     GEOMETRY_LAYER_COMPRESSED_CHANGED       = 1ULL << 9,
107     GEOMETRY_LAYER_BLEND_CHANGED            = 1ULL << 10,
108     GEOMETRY_LAYER_FORMAT_CHANGED           = 1ULL << 11,
109     GEOMETRY_LAYER_DRM_CHANGED              = 1ULL << 12,
110     GEOMETRY_LAYER_IGNORE_CHANGED           = 1ULL << 13,
111     GEOMETRY_LAYER_UNKNOWN_CHANGED          = 1ULL << 14,
112     /* 1ULL << 14 */
113     /* 1ULL << 15 */
114     /* 1ULL << 16 */
115     /* 1ULL << 17 */
116     /* 1ULL << 18 */
117     /* 1ULL << 19 */
118     GEOMETRY_DISPLAY_LAYER_ADDED            = 1ULL << 20,
119     GEOMETRY_DISPLAY_LAYER_REMOVED          = 1ULL << 21,
120     GEOMETRY_DISPLAY_CONFIG_CHANGED         = 1ULL << 22,
121     GEOMETRY_DISPLAY_RESOLUTION_CHANGED     = 1ULL << 23,
122     GEOMETRY_DISPLAY_SINGLEBUF_CHANGED      = 1ULL << 24,
123     GEOMETRY_DISPLAY_FORCE_VALIDATE         = 1ULL << 25,
124     GEOMETRY_DISPLAY_COLOR_MODE_CHANGED     = 1ULL << 26,
125     GEOMETRY_DISPLAY_DYNAMIC_RECOMPOSITION  = 1ULL << 27,
126     GEOMETRY_DISPLAY_POWER_ON               = 1ULL << 28,
127     GEOMETRY_DISPLAY_POWER_OFF              = 1ULL << 29,
128     GEOMETRY_DISPLAY_COLOR_TRANSFORM_CHANGED= 1ULL << 30,
129     GEOMETRY_DISPLAY_DATASPACE_CHANGED      = 1ULL << 31,
130     /* 1ULL << 32 */
131     /* 1ULL << 33 */
132     /* 1ULL << 34 */
133     /* 1ULL << 35 */
134     GEOMETRY_DEVICE_DISPLAY_ADDED           = 1ULL << 36,
135     GEOMETRY_DEVICE_DISPLAY_REMOVED         = 1ULL << 37,
136     GEOMETRY_DEVICE_CONFIG_CHANGED          = 1ULL << 38,
137     GEOMETRY_DEVICE_DISP_MODE_CHAGED        = 1ULL << 39,
138     GEOMETRY_DEVICE_SCENARIO_CHANGED        = 1ULL << 40,
139 
140     GEOMETRY_ERROR_CASE                     = 1ULL << 63,
141 };
142 
143 class ExynosDevice;
144 class ExynosDisplay;
145 class ExynosResourceManager;
146 class ExynosDeviceInterface;
147 
148 class ExynosDevice {
149     public:
150         /**
151          * TODO : Should be defined as ExynosDisplay type
152          * Display list that managed by Device.
153          */
154         android::Vector< ExynosDisplay* > mDisplays;
155 
156         int mNumVirtualDisplay;
157 
158         /**
159          * Resource manager object that is used to manage HW resources and assign resources to each layers
160          */
161         ExynosResourceManager *mResourceManager;
162 
163         /**
164          * Geometry change will be saved by bit map.
165          * ex) Display create/destory.
166          */
167         uint64_t mGeometryChanged;
168 
169         /**
170          * If Panel has not self-refresh feature, dynamic recomposition will be enabled.
171          */
172         std::thread mDRThread;
173         volatile int32_t mDRThreadStatus;
174         std::atomic<bool> mDRLoopStatus;
175         bool mPrimaryBlank;
176 
177         /**
178          * Callback informations those are used by SurfaceFlinger.
179          * - VsyncCallback: Vsync detect callback.
180          * - RefreshCallback: Callback by refresh request from HWC.
181          * - HotplugCallback: Hot plug event by new display hardware.
182          */
183 
184         /** TODO : Array size shuld be checked */
185         exynos_callback_info_t mCallbackInfos[HWC2_CALLBACK_SEAMLESS_POSSIBLE + 1];
186 
187         /**
188          * Thread variables
189          */
190         int mVsyncFd;
191         int mExtVsyncFd;
192 
193         /**
194          * mDisplayId of display that has the slowest fps.
195          * HWC uses vsync of display that has the slowest fps to main vsync.
196          */
197         uint32_t mVsyncDisplayId;
198         uint64_t mTimestamp;
199 
200         uint32_t mDisplayMode;
201 
202         // Variable for fence tracer
203         hwc_fence_info mFenceInfo[MAX_FD_NUM];
204 
205         /**
206          * This will be initialized with differnt class
207          * that inherits ExynosDeviceInterface according to
208          * interface type.
209          */
210         std::unique_ptr<ExynosDeviceInterface> mDeviceInterface;
211 
212         // Con/Destructors
213         ExynosDevice();
214         virtual ~ExynosDevice();
215 
216         bool isFirstValidate();
217         bool isLastValidate(ExynosDisplay *display);
218 
219         /**
220          * @param outSize
221          * @param * outBuffer
222          */
223 
224         void dynamicRecompositionThreadCreate();
225         static void* dynamicRecompositionThreadLoop(void *data);
226 
227 
228         /**
229          * @param display
230          */
231         ExynosDisplay* getDisplay(uint32_t display);
232 
233         /**
234          * Device Functions for HWC 2.0
235          */
236 
237         /**
238          * Descriptor: HWC2_FUNCTION_CREATE_VIRTUAL_DISPLAY
239          * HWC2_PFN_CREATE_VIRTUAL_DISPLAY
240          */
241         int32_t createVirtualDisplay(
242                 uint32_t width, uint32_t height, int32_t *format, ExynosDisplay *display);
243 
244         /**
245          * Descriptor: HWC2_FUNCTION_DESTROY_VIRTUAL_DISPLAY
246          * HWC2_PFN_DESTROY_VIRTUAL_DISPLAY
247          */
248         int32_t destroyVirtualDisplay(
249                 ExynosDisplay *display);
250 
251         /**
252          * Descriptor: HWC2_FUNCTION_DUMP
253          * HWC2_PFN_DUMP
254          */
255         void dump(uint32_t *outSize, char *outBuffer);
256 
257         /**
258          * Descriptor: HWC2_FUNCTION_GET_MAX_VIRTUAL_DISPLAY_COUNT
259          * HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT
260          */
261         /* TODO overide check!! */
262         uint32_t getMaxVirtualDisplayCount();
263 
264          /* Descriptor: HWC2_FUNCTION_GET_LAYER_GENERIC_METADATA_KEY
265          */
266         void getLayerGenericMetadataKey(uint32_t __unused keyIndex,
267                 uint32_t* __unused outKeyLength, char* __unused outKey, bool* __unused outMandatory);
268 
269         /**
270          * Descriptor: HWC2_FUNCTION_REGISTER_CALLBACK
271          * HWC2_PFN_REGISTER_CALLBACK
272          */
273         int32_t registerCallback (
274                 int32_t descriptor, hwc2_callback_data_t callbackData, hwc2_function_pointer_t point);
275 
276         void invalidate();
277 
278         void setHWCDebug(unsigned int debug);
279         uint32_t getHWCDebug();
280         void setHWCFenceDebug(uint32_t ipNum, uint32_t typeNum, uint32_t mode);
281         void getHWCFenceDebug();
282         void setHWCControl(uint32_t display, uint32_t ctrl, int32_t val);
283         void setDisplayMode(uint32_t displayMode);
284         bool checkDisplayConnection(uint32_t displayId);
285         bool checkNonInternalConnection();
286         void getCapabilities(uint32_t *outCount, int32_t* outCapabilities);
setGeometryChanged(uint64_t changedBit)287         void setGeometryChanged(uint64_t changedBit) { mGeometryChanged|= changedBit;};
288         void clearGeometryChanged();
289         void setDynamicRecomposition(unsigned int on);
290         bool canSkipValidate();
291         bool validateFences(ExynosDisplay *display);
292         void compareVsyncPeriod();
293         bool isDynamicRecompositionThreadAlive();
294         void checkDynamicRecompositionThread();
295         int32_t setDisplayDeviceMode(int32_t display_id, int32_t mode);
296         int32_t setPanelGammaTableSource(int32_t display_id, int32_t type, int32_t source);
297 
298         class captureReadbackClass {
299             public:
300                 captureReadbackClass(ExynosDevice *device);
301                 ~captureReadbackClass();
302                 int32_t allocBuffer(uint32_t format, uint32_t w, uint32_t h);
getBuffer()303                 buffer_handle_t& getBuffer() { return mBuffer; };
304                 void saveToFile(const String8 &fileName);
305             private:
306                 buffer_handle_t mBuffer = nullptr;
307                 ExynosDevice* mDevice = nullptr;
308         };
309         void captureScreenWithReadback(uint32_t displayType);
310         void cleanupCaptureScreen(void *buffer);
311         void signalReadbackDone();
clearWaitingReadbackReqDone()312         void clearWaitingReadbackReqDone() {
313             mIsWaitingReadbackReqDone = false;
314         };
315 
316     protected:
317         void initDeviceInterface(uint32_t interfaceType);
318     protected:
319         uint32_t mInterfaceType;
320     private:
321         Mutex mCaptureMutex;
322         Condition mCaptureCondition;
323         std::atomic<bool> mIsWaitingReadbackReqDone = false;
324         void setVBlankOffDelay(int vblankOffDelay);
325 
326     public:
327         bool isLbeSupported();
328         void setLbeState(LbeState state);
329         void setLbeAmbientLight(int value);
330         LbeState getLbeState();
331 
332         bool isLhbmSupported();
333         int32_t setLhbmState(bool enabled);
334         bool getLhbmState();
335         int setMinIdleRefreshRate(const int fps);
336         int setRefreshRateThrottle(const int delayMs);
337 
338         bool isColorCalibratedByDevice();
339 
340     public:
enterToTUI()341         void enterToTUI() { mIsInTUI = true; };
exitFromTUI()342         void exitFromTUI() { mIsInTUI = false; };
isInTUI()343         bool isInTUI() { return mIsInTUI; };
344 
345     private:
346         bool mLbeSupported;
347         bool mIsInTUI;
348 };
349 
350 #endif //_EXYNOSDEVICE_H
351