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