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