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