1 /* 2 * Copyright (C) 2022 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 #pragma once 18 19 #include <hardware/hwcomposer2.h> 20 21 #include <atomic> 22 #include <optional> 23 #include <sstream> 24 25 #include "HwcDisplayConfigs.h" 26 #include "compositor/FlatteningController.h" 27 #include "compositor/LayerData.h" 28 #include "drm/DrmAtomicStateManager.h" 29 #include "drm/ResourceManager.h" 30 #include "drm/VSyncWorker.h" 31 #include "hwc2_device/HwcLayer.h" 32 33 namespace android { 34 35 class Backend; 36 class DrmHwcTwo; 37 38 inline constexpr uint32_t kPrimaryDisplay = 0; 39 40 // NOLINTNEXTLINE 41 class HwcDisplay { 42 public: 43 HwcDisplay(hwc2_display_t handle, HWC2::DisplayType type, DrmHwcTwo *hwc2); 44 HwcDisplay(const HwcDisplay &) = delete; 45 ~HwcDisplay(); 46 47 /* SetPipeline should be carefully used only by DrmHwcTwo hotplug handlers */ 48 void SetPipeline(std::shared_ptr<DrmDisplayPipeline> pipeline); 49 50 HWC2::Error CreateComposition(AtomicCommitArgs &a_args); 51 std::vector<HwcLayer *> GetOrderLayersByZPos(); 52 53 void ClearDisplay(); 54 55 std::string Dump(); 56 57 // HWC Hooks 58 HWC2::Error AcceptDisplayChanges(); 59 HWC2::Error CreateLayer(hwc2_layer_t *layer); 60 HWC2::Error DestroyLayer(hwc2_layer_t layer); 61 HWC2::Error GetActiveConfig(hwc2_config_t *config) const; 62 HWC2::Error GetChangedCompositionTypes(uint32_t *num_elements, 63 hwc2_layer_t *layers, int32_t *types); 64 HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height, 65 int32_t format, int32_t dataspace); 66 HWC2::Error GetColorModes(uint32_t *num_modes, int32_t *modes); 67 HWC2::Error GetDisplayAttribute(hwc2_config_t config, int32_t attribute, 68 int32_t *value); 69 HWC2::Error GetDisplayConfigs(uint32_t *num_configs, hwc2_config_t *configs); 70 HWC2::Error GetDisplayName(uint32_t *size, char *name); 71 HWC2::Error GetDisplayRequests(int32_t *display_requests, 72 uint32_t *num_elements, hwc2_layer_t *layers, 73 int32_t *layer_requests); 74 HWC2::Error GetDisplayType(int32_t *type); 75 #if __ANDROID_API__ > 27 76 HWC2::Error GetRenderIntents(int32_t mode, uint32_t *outNumIntents, 77 int32_t *outIntents); 78 HWC2::Error SetColorModeWithIntent(int32_t mode, int32_t intent); 79 #endif 80 #if __ANDROID_API__ > 28 81 HWC2::Error GetDisplayIdentificationData(uint8_t *outPort, 82 uint32_t *outDataSize, 83 uint8_t *outData); 84 HWC2::Error GetDisplayCapabilities(uint32_t *outNumCapabilities, 85 uint32_t *outCapabilities); 86 HWC2::Error GetDisplayBrightnessSupport(bool *supported); 87 HWC2::Error SetDisplayBrightness(float); 88 #endif 89 #if __ANDROID_API__ > 29 90 HWC2::Error GetDisplayConnectionType(uint32_t *outType); 91 92 HWC2::Error SetActiveConfigWithConstraints( 93 hwc2_config_t config, 94 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints, 95 hwc_vsync_period_change_timeline_t *outTimeline); 96 HWC2::Error SetAutoLowLatencyMode(bool on); 97 HWC2::Error GetSupportedContentTypes( 98 uint32_t *outNumSupportedContentTypes, 99 const uint32_t *outSupportedContentTypes); 100 101 HWC2::Error SetContentType(int32_t contentType); 102 #endif 103 HWC2::Error GetDisplayVsyncPeriod(uint32_t *outVsyncPeriod); 104 105 HWC2::Error GetDozeSupport(int32_t *support); 106 HWC2::Error GetHdrCapabilities(uint32_t *num_types, int32_t *types, 107 float *max_luminance, 108 float *max_average_luminance, 109 float *min_luminance); 110 HWC2::Error GetReleaseFences(uint32_t *num_elements, hwc2_layer_t *layers, 111 int32_t *fences); 112 HWC2::Error PresentDisplay(int32_t *out_present_fence); 113 HWC2::Error SetActiveConfig(hwc2_config_t config); 114 HWC2::Error ChosePreferredConfig(); 115 HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence, 116 int32_t dataspace, hwc_region_t damage); 117 HWC2::Error SetColorMode(int32_t mode); 118 HWC2::Error SetColorTransform(const float *matrix, int32_t hint); 119 HWC2::Error SetOutputBuffer(buffer_handle_t buffer, int32_t release_fence); 120 HWC2::Error SetPowerMode(int32_t mode); 121 HWC2::Error SetVsyncEnabled(int32_t enabled); 122 HWC2::Error ValidateDisplay(uint32_t *num_types, uint32_t *num_requests); get_layer(hwc2_layer_t layer)123 HwcLayer *get_layer(hwc2_layer_t layer) { 124 auto it = layers_.find(layer); 125 if (it == layers_.end()) 126 return nullptr; 127 return &it->second; 128 } 129 130 /* Statistics */ 131 struct Stats { minusStats132 Stats minus(Stats b) const { 133 return {total_frames_ - b.total_frames_, 134 total_pixops_ - b.total_pixops_, 135 gpu_pixops_ - b.gpu_pixops_, 136 failed_kms_validate_ - b.failed_kms_validate_, 137 failed_kms_present_ - b.failed_kms_present_, 138 frames_flattened_ - b.frames_flattened_}; 139 } 140 141 uint32_t total_frames_ = 0; 142 uint64_t total_pixops_ = 0; 143 uint64_t gpu_pixops_ = 0; 144 uint32_t failed_kms_validate_ = 0; 145 uint32_t failed_kms_present_ = 0; 146 uint32_t frames_flattened_ = 0; 147 }; 148 149 const Backend *backend() const; 150 void set_backend(std::unique_ptr<Backend> backend); 151 GetHwc2()152 auto GetHwc2() { 153 return hwc2_; 154 } 155 layers()156 std::map<hwc2_layer_t, HwcLayer> &layers() { 157 return layers_; 158 } 159 GetPipe()160 auto &GetPipe() { 161 return *pipeline_; 162 } 163 164 bool CtmByGpu(); 165 total_stats()166 Stats &total_stats() { 167 return total_stats_; 168 } 169 170 /* Headless mode required to keep SurfaceFlinger alive when all display are 171 * disconnected, Without headless mode Android will continuously crash. 172 * Only single internal (primary) display is required to be in HEADLESS mode 173 * to prevent the crash. See: 174 * https://source.android.com/devices/graphics/hotplug#handling-common-scenarios 175 */ IsInHeadlessMode()176 bool IsInHeadlessMode() { 177 return !pipeline_; 178 } 179 180 void Deinit(); 181 GetFlatCon()182 auto GetFlatCon() { 183 return flatcon_; 184 } 185 GetWritebackLayer()186 auto &GetWritebackLayer() { 187 return writeback_layer_; 188 } 189 SetVirtualDisplayResolution(uint16_t width,uint16_t height)190 void SetVirtualDisplayResolution(uint16_t width, uint16_t height) { 191 virtual_disp_width_ = width; 192 virtual_disp_height_ = height; 193 } 194 195 private: 196 HwcDisplayConfigs configs_; 197 198 DrmHwcTwo *const hwc2_; 199 200 SharedFd present_fence_; 201 202 std::optional<DrmMode> staged_mode_; 203 int64_t staged_mode_change_time_{}; 204 uint32_t staged_mode_config_id_{}; 205 206 std::shared_ptr<DrmDisplayPipeline> pipeline_; 207 208 std::unique_ptr<Backend> backend_; 209 std::shared_ptr<FlatteningController> flatcon_; 210 211 std::shared_ptr<VSyncWorker> vsync_worker_; 212 bool vsync_event_en_{}; 213 bool vsync_tracking_en_{}; 214 int64_t last_vsync_ts_{}; 215 216 const hwc2_display_t handle_; 217 HWC2::DisplayType type_; 218 219 uint32_t layer_idx_{}; 220 221 std::map<hwc2_layer_t, HwcLayer> layers_; 222 HwcLayer client_layer_; 223 std::unique_ptr<HwcLayer> writeback_layer_; 224 uint16_t virtual_disp_width_{}; 225 uint16_t virtual_disp_height_{}; 226 int32_t color_mode_{}; 227 static constexpr int kCtmRows = 3; 228 static constexpr int kCtmCols = 3; 229 std::shared_ptr<drm_color_ctm> color_matrix_; 230 android_color_transform_t color_transform_hint_{}; 231 232 std::shared_ptr<DrmKmsPlan> current_plan_; 233 234 uint32_t frame_no_ = 0; 235 Stats total_stats_; 236 Stats prev_stats_; 237 std::string DumpDelta(HwcDisplay::Stats delta); 238 239 void SetColorMarixToIdentity(); 240 241 HWC2::Error Init(); 242 243 HWC2::Error SetActiveConfigInternal(uint32_t config, int64_t change_time); 244 }; 245 246 } // namespace android 247