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