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