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 | |
| 17 | #ifndef ANDROID_HWC2_DEVICE_HWC_DISPLAY_H |
| 18 | #define ANDROID_HWC2_DEVICE_HWC_DISPLAY_H |
| 19 | |
| 20 | #include <hardware/hwcomposer2.h> |
| 21 | |
| 22 | #include <optional> |
| 23 | |
Roman Stratiienko | 0137f86 | 2022-01-04 18:27:40 +0200 | [diff] [blame] | 24 | #include "HwcDisplayConfigs.h" |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 25 | #include "compositor/DrmDisplayCompositor.h" |
| 26 | #include "drm/ResourceManager.h" |
| 27 | #include "drm/VSyncWorker.h" |
| 28 | #include "drmhwcomposer.h" |
| 29 | #include "hwc2_device/HwcLayer.h" |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | class Backend; |
| 34 | class DrmHwcTwo; |
| 35 | |
| 36 | class HwcDisplay { |
| 37 | public: |
| 38 | HwcDisplay(ResourceManager *resource_manager, DrmDevice *drm, |
| 39 | hwc2_display_t handle, HWC2::DisplayType type, DrmHwcTwo *hwc2); |
| 40 | HwcDisplay(const HwcDisplay &) = delete; |
| 41 | HWC2::Error Init(std::vector<DrmPlane *> *planes); |
| 42 | |
| 43 | HWC2::Error CreateComposition(AtomicCommitArgs &a_args); |
| 44 | std::vector<HwcLayer *> GetOrderLayersByZPos(); |
| 45 | |
| 46 | void ClearDisplay(); |
| 47 | |
| 48 | std::string Dump(); |
| 49 | |
| 50 | // HWC Hooks |
| 51 | HWC2::Error AcceptDisplayChanges(); |
| 52 | HWC2::Error CreateLayer(hwc2_layer_t *layer); |
| 53 | HWC2::Error DestroyLayer(hwc2_layer_t layer); |
| 54 | HWC2::Error GetActiveConfig(hwc2_config_t *config) const; |
| 55 | HWC2::Error GetChangedCompositionTypes(uint32_t *num_elements, |
| 56 | hwc2_layer_t *layers, int32_t *types); |
| 57 | HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height, |
| 58 | int32_t format, int32_t dataspace); |
| 59 | HWC2::Error GetColorModes(uint32_t *num_modes, int32_t *modes); |
| 60 | HWC2::Error GetDisplayAttribute(hwc2_config_t config, int32_t attribute, |
| 61 | int32_t *value); |
| 62 | HWC2::Error GetDisplayConfigs(uint32_t *num_configs, hwc2_config_t *configs); |
| 63 | HWC2::Error GetDisplayName(uint32_t *size, char *name); |
| 64 | HWC2::Error GetDisplayRequests(int32_t *display_requests, |
| 65 | uint32_t *num_elements, hwc2_layer_t *layers, |
| 66 | int32_t *layer_requests); |
| 67 | HWC2::Error GetDisplayType(int32_t *type); |
| 68 | #if PLATFORM_SDK_VERSION > 27 |
| 69 | HWC2::Error GetRenderIntents(int32_t mode, uint32_t *outNumIntents, |
| 70 | int32_t *outIntents); |
| 71 | HWC2::Error SetColorModeWithIntent(int32_t mode, int32_t intent); |
| 72 | #endif |
| 73 | #if PLATFORM_SDK_VERSION > 28 |
| 74 | HWC2::Error GetDisplayIdentificationData(uint8_t *outPort, |
| 75 | uint32_t *outDataSize, |
| 76 | uint8_t *outData); |
| 77 | HWC2::Error GetDisplayCapabilities(uint32_t *outNumCapabilities, |
| 78 | uint32_t *outCapabilities); |
| 79 | HWC2::Error GetDisplayBrightnessSupport(bool *supported); |
| 80 | HWC2::Error SetDisplayBrightness(float); |
| 81 | #endif |
| 82 | #if PLATFORM_SDK_VERSION > 29 |
| 83 | HWC2::Error GetDisplayConnectionType(uint32_t *outType); |
| 84 | HWC2::Error GetDisplayVsyncPeriod(hwc2_vsync_period_t *outVsyncPeriod); |
| 85 | |
| 86 | HWC2::Error SetActiveConfigWithConstraints( |
| 87 | hwc2_config_t config, |
| 88 | hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints, |
| 89 | hwc_vsync_period_change_timeline_t *outTimeline); |
| 90 | HWC2::Error SetAutoLowLatencyMode(bool on); |
| 91 | HWC2::Error GetSupportedContentTypes( |
| 92 | uint32_t *outNumSupportedContentTypes, |
| 93 | const uint32_t *outSupportedContentTypes); |
| 94 | |
| 95 | HWC2::Error SetContentType(int32_t contentType); |
| 96 | #endif |
| 97 | |
| 98 | HWC2::Error GetDozeSupport(int32_t *support); |
| 99 | HWC2::Error GetHdrCapabilities(uint32_t *num_types, int32_t *types, |
| 100 | float *max_luminance, |
| 101 | float *max_average_luminance, |
| 102 | float *min_luminance); |
| 103 | HWC2::Error GetReleaseFences(uint32_t *num_elements, hwc2_layer_t *layers, |
| 104 | int32_t *fences); |
| 105 | HWC2::Error PresentDisplay(int32_t *present_fence); |
| 106 | HWC2::Error SetActiveConfig(hwc2_config_t config); |
| 107 | HWC2::Error ChosePreferredConfig(); |
| 108 | HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence, |
| 109 | int32_t dataspace, hwc_region_t damage); |
| 110 | HWC2::Error SetColorMode(int32_t mode); |
| 111 | HWC2::Error SetColorTransform(const float *matrix, int32_t hint); |
| 112 | HWC2::Error SetOutputBuffer(buffer_handle_t buffer, int32_t release_fence); |
| 113 | HWC2::Error SetPowerMode(int32_t mode); |
| 114 | HWC2::Error SetVsyncEnabled(int32_t enabled); |
| 115 | HWC2::Error ValidateDisplay(uint32_t *num_types, uint32_t *num_requests); |
| 116 | HwcLayer *get_layer(hwc2_layer_t layer) { |
| 117 | auto it = layers_.find(layer); |
| 118 | if (it == layers_.end()) |
| 119 | return nullptr; |
| 120 | return &it->second; |
| 121 | } |
| 122 | |
| 123 | /* Statistics */ |
| 124 | struct Stats { |
| 125 | Stats minus(Stats b) const { |
| 126 | return {total_frames_ - b.total_frames_, |
| 127 | total_pixops_ - b.total_pixops_, |
| 128 | gpu_pixops_ - b.gpu_pixops_, |
| 129 | failed_kms_validate_ - b.failed_kms_validate_, |
| 130 | failed_kms_present_ - b.failed_kms_present_, |
| 131 | frames_flattened_ - b.frames_flattened_}; |
| 132 | } |
| 133 | |
| 134 | uint32_t total_frames_ = 0; |
| 135 | uint64_t total_pixops_ = 0; |
| 136 | uint64_t gpu_pixops_ = 0; |
| 137 | uint32_t failed_kms_validate_ = 0; |
| 138 | uint32_t failed_kms_present_ = 0; |
| 139 | uint32_t frames_flattened_ = 0; |
| 140 | }; |
| 141 | |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 142 | const Backend *backend() const; |
| 143 | void set_backend(std::unique_ptr<Backend> backend); |
| 144 | |
| 145 | const std::vector<DrmPlane *> &primary_planes() const { |
| 146 | return primary_planes_; |
| 147 | } |
| 148 | |
| 149 | const std::vector<DrmPlane *> &overlay_planes() const { |
| 150 | return overlay_planes_; |
| 151 | } |
| 152 | |
| 153 | std::map<hwc2_layer_t, HwcLayer> &layers() { |
| 154 | return layers_; |
| 155 | } |
| 156 | |
| 157 | const DrmDisplayCompositor &compositor() const { |
| 158 | return compositor_; |
| 159 | } |
| 160 | |
| 161 | const DrmDevice *drm() const { |
| 162 | return drm_; |
| 163 | } |
| 164 | |
| 165 | const DrmConnector *connector() const { |
| 166 | return connector_; |
| 167 | } |
| 168 | |
| 169 | ResourceManager *resource_manager() const { |
| 170 | return resource_manager_; |
| 171 | } |
| 172 | |
| 173 | android_color_transform_t &color_transform_hint() { |
| 174 | return color_transform_hint_; |
| 175 | } |
| 176 | |
| 177 | Stats &total_stats() { |
| 178 | return total_stats_; |
| 179 | } |
| 180 | |
| 181 | /* returns true if composition should be sent to client */ |
| 182 | bool ProcessClientFlatteningState(bool skip) { |
| 183 | int flattenning_state = flattenning_state_; |
| 184 | if (flattenning_state == ClientFlattenningState::Disabled) { |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | if (skip) { |
| 189 | flattenning_state_ = ClientFlattenningState::NotRequired; |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | if (flattenning_state == ClientFlattenningState::ClientRefreshRequested) { |
| 194 | flattenning_state_ = ClientFlattenningState::Flattened; |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | flattening_vsync_worker_.VSyncControl(true); |
| 199 | flattenning_state_ = ClientFlattenningState::VsyncCountdownMax; |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | private: |
| 204 | enum ClientFlattenningState : int32_t { |
| 205 | Disabled = -3, |
| 206 | NotRequired = -2, |
| 207 | Flattened = -1, |
| 208 | ClientRefreshRequested = 0, |
| 209 | VsyncCountdownMax = 60, /* 1 sec @ 60FPS */ |
| 210 | }; |
| 211 | |
| 212 | std::atomic_int flattenning_state_{ClientFlattenningState::NotRequired}; |
| 213 | VSyncWorker flattening_vsync_worker_; |
| 214 | |
| 215 | constexpr static size_t MATRIX_SIZE = 16; |
| 216 | |
Roman Stratiienko | 0137f86 | 2022-01-04 18:27:40 +0200 | [diff] [blame] | 217 | HwcDisplayConfigs configs_; |
| 218 | |
Roman Stratiienko | 3627beb | 2022-01-04 16:02:55 +0200 | [diff] [blame] | 219 | DrmHwcTwo *hwc2_; |
| 220 | |
| 221 | std::optional<DrmMode> staged_mode; |
| 222 | |
| 223 | ResourceManager *resource_manager_; |
| 224 | DrmDevice *drm_; |
| 225 | DrmDisplayCompositor compositor_; |
| 226 | |
| 227 | std::vector<DrmPlane *> primary_planes_; |
| 228 | std::vector<DrmPlane *> overlay_planes_; |
| 229 | |
| 230 | std::unique_ptr<Backend> backend_; |
| 231 | |
| 232 | VSyncWorker vsync_worker_; |
| 233 | DrmConnector *connector_ = nullptr; |
| 234 | DrmCrtc *crtc_ = nullptr; |
| 235 | hwc2_display_t handle_; |
| 236 | HWC2::DisplayType type_; |
| 237 | uint32_t layer_idx_ = 0; |
| 238 | std::map<hwc2_layer_t, HwcLayer> layers_; |
| 239 | HwcLayer client_layer_; |
| 240 | int32_t color_mode_{}; |
| 241 | std::array<float, MATRIX_SIZE> color_transform_matrix_{}; |
| 242 | android_color_transform_t color_transform_hint_; |
| 243 | |
| 244 | uint32_t frame_no_ = 0; |
| 245 | Stats total_stats_; |
| 246 | Stats prev_stats_; |
| 247 | std::string DumpDelta(HwcDisplay::Stats delta); |
| 248 | }; |
| 249 | |
| 250 | } // namespace android |
| 251 | |
| 252 | #endif |