1 /* 2 * Copyright (c) 2017, The Linux Foundation. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above 10 * copyright notice, this list of conditions and the following 11 * disclaimer in the documentation and/or other materials provided 12 * with the distribution. 13 * * Neither the name of The Linux Foundation nor the names of its 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #ifndef __HW_DEVICE_DRM_H__ 31 #define __HW_DEVICE_DRM_H__ 32 33 #include <drm_interface.h> 34 #include <errno.h> 35 #include <pthread.h> 36 #include <xf86drmMode.h> 37 #include <string> 38 #include <unordered_map> 39 #include <vector> 40 41 #include "hw_interface.h" 42 #include "hw_scale_drm.h" 43 44 #define IOCTL_LOGE(ioctl, type) \ 45 DLOGE("ioctl %s, device = %d errno = %d, desc = %s", #ioctl, type, errno, strerror(errno)) 46 47 namespace sdm { 48 class HWInfoInterface; 49 50 class HWDeviceDRM : public HWInterface { 51 public: 52 HWDeviceDRM(BufferSyncHandler *buffer_sync_handler, BufferAllocator *buffer_allocator, 53 HWInfoInterface *hw_info_intf); 54 virtual ~HWDeviceDRM() {} 55 virtual DisplayError Init(); 56 virtual DisplayError Deinit(); 57 58 protected: 59 // From HWInterface 60 virtual DisplayError GetActiveConfig(uint32_t *active_config); 61 virtual DisplayError GetNumDisplayAttributes(uint32_t *count); 62 virtual DisplayError GetDisplayAttributes(uint32_t index, 63 HWDisplayAttributes *display_attributes); 64 virtual DisplayError GetHWPanelInfo(HWPanelInfo *panel_info); 65 virtual DisplayError SetDisplayAttributes(uint32_t index); 66 virtual DisplayError SetDisplayAttributes(const HWDisplayAttributes &display_attributes); 67 virtual DisplayError GetConfigIndex(uint32_t mode, uint32_t *index); 68 virtual DisplayError PowerOn(); 69 virtual DisplayError PowerOff(); 70 virtual DisplayError Doze(); 71 virtual DisplayError DozeSuspend(); 72 virtual DisplayError Standby(); 73 virtual DisplayError Validate(HWLayers *hw_layers); 74 virtual DisplayError Commit(HWLayers *hw_layers); 75 virtual DisplayError Flush(); 76 virtual DisplayError GetPPFeaturesVersion(PPFeatureVersion *vers); 77 virtual DisplayError SetPPFeatures(PPFeaturesConfig *feature_list); 78 // This API is no longer supported, expectation is to call the correct API on HWEvents 79 virtual DisplayError SetVSyncState(bool enable); 80 virtual void SetIdleTimeoutMs(uint32_t timeout_ms); 81 virtual DisplayError SetDisplayMode(const HWDisplayMode hw_display_mode); 82 virtual DisplayError SetRefreshRate(uint32_t refresh_rate); 83 virtual DisplayError SetPanelBrightness(int level); 84 virtual DisplayError CachePanelBrightness(int level); 85 virtual DisplayError GetHWScanInfo(HWScanInfo *scan_info); 86 virtual DisplayError GetVideoFormat(uint32_t config_index, uint32_t *video_format); 87 virtual DisplayError GetMaxCEAFormat(uint32_t *max_cea_format); 88 virtual DisplayError SetCursorPosition(HWLayers *hw_layers, int x, int y); 89 virtual DisplayError OnMinHdcpEncryptionLevelChange(uint32_t min_enc_level); 90 virtual DisplayError GetPanelBrightness(int *level); 91 virtual DisplayError SetAutoRefresh(bool enable) { return kErrorNone; } 92 virtual DisplayError SetS3DMode(HWS3DMode s3d_mode); 93 virtual DisplayError SetScaleLutConfig(HWScaleLutInfo *lut_info); 94 virtual DisplayError SetMixerAttributes(const HWMixerAttributes &mixer_attributes); 95 virtual DisplayError GetMixerAttributes(HWMixerAttributes *mixer_attributes); 96 virtual void InitializeConfigs(); 97 98 enum { 99 kHWEventVSync, 100 kHWEventBlank, 101 }; 102 103 static const int kMaxStringLength = 1024; 104 static const int kNumPhysicalDisplays = 2; 105 static const int kMaxSysfsCommandLength = 12; 106 static constexpr const char *kBrightnessNode = 107 "/sys/class/backlight/panel0-backlight/brightness"; 108 109 DisplayError SetFormat(const LayerBufferFormat &source, uint32_t *target); 110 DisplayError SetStride(HWDeviceType device_type, LayerBufferFormat format, uint32_t width, 111 uint32_t *target); 112 DisplayError PopulateDisplayAttributes(); 113 void PopulateHWPanelInfo(); 114 void GetHWDisplayPortAndMode(); 115 void GetHWPanelMaxBrightness(); 116 void ResetDisplayParams(); 117 bool EnableHotPlugDetection(int enable); 118 void UpdateMixerAttributes(); 119 void SetBlending(const LayerBlending &source, sde_drm::DRMBlendType *target); 120 void SetSrcConfig(const LayerBuffer &input_buffer, uint32_t *config); 121 void SetRect(const LayerRect &source, sde_drm::DRMRect *target); 122 DisplayError DefaultCommit(HWLayers *hw_layers); 123 DisplayError AtomicCommit(HWLayers *hw_layers); 124 void SetupAtomic(HWLayers *hw_layers, bool validate); 125 126 class Registry { 127 public: 128 explicit Registry(BufferAllocator *buffer_allocator) : buffer_allocator_(buffer_allocator) {} 129 // Call on each validate and commit to register layer buffers 130 void RegisterCurrent(HWLayers *hw_layers); 131 // Call at the end of draw cycle to clear the next slot for business 132 void UnregisterNext(); 133 // Call on display disconnect to release all gem handles and fb_ids 134 void Clear(); 135 // Maps given fd to FB ID 136 void MapBufferToFbId(LayerBuffer* buffer); 137 // Finds an fb_id corresponding to an fd in current map 138 uint32_t GetFbId(int fd); 139 140 private: 141 static const int kCycleDelay = 3; // N cycle delay before destroy 142 // fd to fb_id map. fd is used as key only for a single draw cycle between 143 // prepare and commit. It should not be used for caching in future due to fd recycling 144 std::unordered_map<int, uint32_t> hashmap_[kCycleDelay] {}; 145 int current_index_ = 0; 146 BufferAllocator *buffer_allocator_ = {}; 147 }; 148 149 protected: 150 const char *device_name_ = {}; 151 bool deferred_initialize_ = false; 152 sde_drm::DRMDisplayType disp_type_ = {}; 153 HWInfoInterface *hw_info_intf_ = {}; 154 BufferSyncHandler *buffer_sync_handler_ = {}; 155 int dev_fd_ = -1; 156 Registry registry_; 157 sde_drm::DRMDisplayToken token_ = {}; 158 HWResourceInfo hw_resource_ = {}; 159 HWPanelInfo hw_panel_info_ = {}; 160 HWDeviceType device_type_ = {}; 161 sde_drm::DRMManagerInterface *drm_mgr_intf_ = {}; 162 sde_drm::DRMAtomicReqInterface *drm_atomic_intf_ = {}; 163 sde_drm::DRMConnectorInfo connector_info_ = {}; 164 drmModeModeInfo current_mode_ = {}; 165 HWDisplayAttributes display_attributes_ = {}; 166 167 private: 168 bool synchronous_commit_ = false; 169 HWMixerAttributes mixer_attributes_ = {}; 170 bool default_mode_ = false; 171 std::string interface_str_ = "DSI"; 172 HWScaleDRM *hw_scale_ = {}; 173 }; 174 175 } // namespace sdm 176 177 #endif // __HW_DEVICE_DRM_H__ 178