1 /* 2 * Copyright (C) 2016 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_DRM_HWC_TWO_H_ 18 #define ANDROID_DRM_HWC_TWO_H_ 19 20 #include <hardware/hwcomposer2.h> 21 #include <math.h> 22 23 #include <array> 24 #include <map> 25 26 #include "compositor/DrmDisplayCompositor.h" 27 #include "compositor/Planner.h" 28 #include "drm/DrmGenericImporter.h" 29 #include "drm/ResourceManager.h" 30 #include "drm/VSyncWorker.h" 31 #include "drmhwcomposer.h" 32 33 namespace android { 34 35 class Backend; 36 37 class DrmHwcTwo : public hwc2_device_t { 38 public: 39 static int HookDevOpen(const struct hw_module_t *module, const char *name, 40 struct hw_device_t **dev); 41 42 DrmHwcTwo(); 43 44 HWC2::Error Init(); 45 46 hwc2_callback_data_t hotplug_callback_data_ = NULL; 47 HWC2_PFN_HOTPLUG hotplug_callback_hook_ = NULL; 48 std::mutex hotplug_callback_lock; 49 SetHotplugCallback(hwc2_callback_data_t data,hwc2_function_pointer_t hook)50 void SetHotplugCallback(hwc2_callback_data_t data, 51 hwc2_function_pointer_t hook) { 52 const std::lock_guard<std::mutex> lock(hotplug_callback_lock); 53 hotplug_callback_data_ = data; 54 hotplug_callback_hook_ = reinterpret_cast<HWC2_PFN_HOTPLUG>(hook); 55 } 56 57 class HwcLayer { 58 public: sf_type()59 HWC2::Composition sf_type() const { 60 return sf_type_; 61 } validated_type()62 HWC2::Composition validated_type() const { 63 return validated_type_; 64 } accept_type_change()65 void accept_type_change() { 66 sf_type_ = validated_type_; 67 } set_validated_type(HWC2::Composition type)68 void set_validated_type(HWC2::Composition type) { 69 validated_type_ = type; 70 } type_changed()71 bool type_changed() const { 72 return sf_type_ != validated_type_; 73 } 74 z_order()75 uint32_t z_order() const { 76 return z_order_; 77 } 78 buffer()79 buffer_handle_t buffer() { 80 return buffer_; 81 } set_buffer(buffer_handle_t buffer)82 void set_buffer(buffer_handle_t buffer) { 83 buffer_ = buffer; 84 } 85 take_acquire_fence()86 int take_acquire_fence() { 87 return acquire_fence_.Release(); 88 } set_acquire_fence(int acquire_fence)89 void set_acquire_fence(int acquire_fence) { 90 acquire_fence_.Set(dup(acquire_fence)); 91 } 92 release_fence()93 int release_fence() { 94 return release_fence_.get(); 95 } take_release_fence()96 int take_release_fence() { 97 return release_fence_.Release(); 98 } manage_release_fence()99 void manage_release_fence() { 100 release_fence_.Set(release_fence_raw_); 101 release_fence_raw_ = -1; 102 } release_fence_output()103 OutputFd release_fence_output() { 104 return OutputFd(&release_fence_raw_); 105 } 106 display_frame()107 hwc_rect_t display_frame() { 108 return display_frame_; 109 } 110 111 void PopulateDrmLayer(DrmHwcLayer *layer); 112 RequireScalingOrPhasing()113 bool RequireScalingOrPhasing() { 114 float src_width = source_crop_.right - source_crop_.left; 115 float src_height = source_crop_.bottom - source_crop_.top; 116 117 float dest_width = display_frame_.right - display_frame_.left; 118 float dest_height = display_frame_.bottom - display_frame_.top; 119 120 bool scaling = src_width != dest_width || src_height != dest_height; 121 bool phasing = (source_crop_.left - floor(source_crop_.left) != 0) || 122 (source_crop_.top - floor(source_crop_.top) != 0); 123 return scaling || phasing; 124 } 125 126 // Layer hooks 127 HWC2::Error SetCursorPosition(int32_t x, int32_t y); 128 HWC2::Error SetLayerBlendMode(int32_t mode); 129 HWC2::Error SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence); 130 HWC2::Error SetLayerColor(hwc_color_t color); 131 HWC2::Error SetLayerCompositionType(int32_t type); 132 HWC2::Error SetLayerDataspace(int32_t dataspace); 133 HWC2::Error SetLayerDisplayFrame(hwc_rect_t frame); 134 HWC2::Error SetLayerPlaneAlpha(float alpha); 135 HWC2::Error SetLayerSidebandStream(const native_handle_t *stream); 136 HWC2::Error SetLayerSourceCrop(hwc_frect_t crop); 137 HWC2::Error SetLayerSurfaceDamage(hwc_region_t damage); 138 HWC2::Error SetLayerTransform(int32_t transform); 139 HWC2::Error SetLayerVisibleRegion(hwc_region_t visible); 140 HWC2::Error SetLayerZOrder(uint32_t z); 141 142 private: 143 // sf_type_ stores the initial type given to us by surfaceflinger, 144 // validated_type_ stores the type after running ValidateDisplay 145 HWC2::Composition sf_type_ = HWC2::Composition::Invalid; 146 HWC2::Composition validated_type_ = HWC2::Composition::Invalid; 147 148 HWC2::BlendMode blending_ = HWC2::BlendMode::None; 149 buffer_handle_t buffer_ = NULL; 150 UniqueFd acquire_fence_; 151 int release_fence_raw_ = -1; 152 UniqueFd release_fence_; 153 hwc_rect_t display_frame_; 154 float alpha_ = 1.0f; 155 hwc_frect_t source_crop_; 156 int32_t cursor_x_; 157 int32_t cursor_y_; 158 hwc_color_t layer_color_; 159 HWC2::Transform transform_ = HWC2::Transform::None; 160 uint32_t z_order_ = 0; 161 android_dataspace_t dataspace_ = HAL_DATASPACE_UNKNOWN; 162 }; 163 164 class HwcDisplay { 165 public: 166 HwcDisplay(ResourceManager *resource_manager, DrmDevice *drm, 167 std::shared_ptr<Importer> importer, hwc2_display_t handle, 168 HWC2::DisplayType type); 169 HwcDisplay(const HwcDisplay &) = delete; 170 HWC2::Error Init(std::vector<DrmPlane *> *planes); 171 172 void RegisterVsyncCallback(hwc2_callback_data_t data, 173 hwc2_function_pointer_t func); 174 void RegisterRefreshCallback(hwc2_callback_data_t data, 175 hwc2_function_pointer_t func); 176 HWC2::Error CreateComposition(bool test); 177 bool HardwareSupportsLayerType(HWC2::Composition comp_type); 178 uint32_t CalcPixOps(std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, 179 size_t first_z, size_t size); 180 void MarkValidated(std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, 181 size_t client_first_z, size_t client_size); 182 183 void ClearDisplay(); 184 185 std::string Dump(); 186 187 // HWC Hooks 188 HWC2::Error AcceptDisplayChanges(); 189 HWC2::Error CreateLayer(hwc2_layer_t *layer); 190 HWC2::Error DestroyLayer(hwc2_layer_t layer); 191 HWC2::Error GetActiveConfig(hwc2_config_t *config); 192 HWC2::Error GetChangedCompositionTypes(uint32_t *num_elements, 193 hwc2_layer_t *layers, 194 int32_t *types); 195 HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height, 196 int32_t format, int32_t dataspace); 197 HWC2::Error GetColorModes(uint32_t *num_modes, int32_t *modes); 198 HWC2::Error GetDisplayAttribute(hwc2_config_t config, int32_t attribute, 199 int32_t *value); 200 HWC2::Error GetDisplayConfigs(uint32_t *num_configs, 201 hwc2_config_t *configs); 202 HWC2::Error GetDisplayName(uint32_t *size, char *name); 203 HWC2::Error GetDisplayRequests(int32_t *display_requests, 204 uint32_t *num_elements, hwc2_layer_t *layers, 205 int32_t *layer_requests); 206 HWC2::Error GetDisplayType(int32_t *type); 207 #if PLATFORM_SDK_VERSION > 27 208 HWC2::Error GetRenderIntents(int32_t mode, uint32_t *outNumIntents, 209 int32_t *outIntents); 210 HWC2::Error SetColorModeWithIntent(int32_t mode, int32_t intent); 211 #endif 212 #if PLATFORM_SDK_VERSION > 28 213 HWC2::Error GetDisplayIdentificationData(uint8_t *outPort, 214 uint32_t *outDataSize, 215 uint8_t *outData); 216 HWC2::Error GetDisplayCapabilities(uint32_t *outNumCapabilities, 217 uint32_t *outCapabilities); 218 HWC2::Error GetDisplayBrightnessSupport(bool *supported); 219 HWC2::Error SetDisplayBrightness(float); 220 #endif 221 HWC2::Error GetDozeSupport(int32_t *support); 222 HWC2::Error GetHdrCapabilities(uint32_t *num_types, int32_t *types, 223 float *max_luminance, 224 float *max_average_luminance, 225 float *min_luminance); 226 HWC2::Error GetReleaseFences(uint32_t *num_elements, hwc2_layer_t *layers, 227 int32_t *fences); 228 HWC2::Error PresentDisplay(int32_t *present_fence); 229 HWC2::Error SetActiveConfig(hwc2_config_t config); 230 HWC2::Error ChosePreferredConfig(); 231 HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence, 232 int32_t dataspace, hwc_region_t damage); 233 HWC2::Error SetColorMode(int32_t mode); 234 HWC2::Error SetColorTransform(const float *matrix, int32_t hint); 235 HWC2::Error SetOutputBuffer(buffer_handle_t buffer, int32_t release_fence); 236 HWC2::Error SetPowerMode(int32_t mode); 237 HWC2::Error SetVsyncEnabled(int32_t enabled); 238 HWC2::Error ValidateDisplay(uint32_t *num_types, uint32_t *num_requests); get_layer(hwc2_layer_t layer)239 HwcLayer *get_layer(hwc2_layer_t layer) { 240 auto it = layers_.find(layer); 241 if (it == layers_.end()) 242 return nullptr; 243 return &it->second; 244 } 245 246 /* Statistics */ 247 struct Stats { minusStats248 Stats minus(Stats b) { 249 return {total_frames_ - b.total_frames_, 250 total_pixops_ - b.total_pixops_, 251 gpu_pixops_ - b.gpu_pixops_, 252 failed_kms_validate_ - b.failed_kms_validate_, 253 failed_kms_present_ - b.failed_kms_present_, 254 frames_flattened_ - b.frames_flattened_}; 255 } 256 257 uint32_t total_frames_ = 0; 258 uint64_t total_pixops_ = 0; 259 uint64_t gpu_pixops_ = 0; 260 uint32_t failed_kms_validate_ = 0; 261 uint32_t failed_kms_present_ = 0; 262 uint32_t frames_flattened_ = 0; 263 }; 264 backend()265 const Backend *backend() const { 266 return backend_.get(); 267 } set_backend(std::unique_ptr<Backend> backend)268 void set_backend(std::unique_ptr<Backend> backend) { 269 backend_ = std::move(backend); 270 } 271 primary_planes()272 const std::vector<DrmPlane *> &primary_planes() const { 273 return primary_planes_; 274 } 275 overlay_planes()276 const std::vector<DrmPlane *> &overlay_planes() const { 277 return overlay_planes_; 278 } 279 layers()280 std::map<hwc2_layer_t, HwcLayer> &layers() { 281 return layers_; 282 } 283 compositor()284 const DrmDisplayCompositor &compositor() const { 285 return compositor_; 286 } 287 drm()288 const DrmDevice *drm() const { 289 return drm_; 290 } 291 connector()292 const DrmConnector *connector() const { 293 return connector_; 294 } 295 importer()296 const std::shared_ptr<Importer> &importer() const { 297 return importer_; 298 } 299 resource_manager()300 ResourceManager *resource_manager() const { 301 return resource_manager_; 302 } 303 color_transform_hint()304 android_color_transform_t &color_transform_hint() { 305 return color_transform_hint_; 306 } 307 total_stats()308 Stats &total_stats() { 309 return total_stats_; 310 } 311 312 private: 313 void AddFenceToPresentFence(int fd); 314 315 constexpr static size_t MATRIX_SIZE = 16; 316 317 ResourceManager *resource_manager_; 318 DrmDevice *drm_; 319 DrmDisplayCompositor compositor_; 320 std::shared_ptr<Importer> importer_; 321 std::unique_ptr<Planner> planner_; 322 323 std::vector<DrmPlane *> primary_planes_; 324 std::vector<DrmPlane *> overlay_planes_; 325 326 std::unique_ptr<Backend> backend_; 327 328 VSyncWorker vsync_worker_; 329 DrmConnector *connector_ = NULL; 330 DrmCrtc *crtc_ = NULL; 331 hwc2_display_t handle_; 332 HWC2::DisplayType type_; 333 uint32_t layer_idx_ = 0; 334 std::map<hwc2_layer_t, HwcLayer> layers_; 335 HwcLayer client_layer_; 336 UniqueFd present_fence_; 337 int32_t color_mode_; 338 std::array<float, MATRIX_SIZE> color_transform_matrix_; 339 android_color_transform_t color_transform_hint_; 340 341 uint32_t frame_no_ = 0; 342 Stats total_stats_; 343 Stats prev_stats_; 344 std::string DumpDelta(DrmHwcTwo::HwcDisplay::Stats delta); 345 }; 346 347 class DrmHotplugHandler : public DrmEventHandler { 348 public: DrmHotplugHandler(DrmHwcTwo * hwc2,DrmDevice * drm)349 DrmHotplugHandler(DrmHwcTwo *hwc2, DrmDevice *drm) 350 : hwc2_(hwc2), drm_(drm) { 351 } 352 void HandleEvent(uint64_t timestamp_us); 353 354 private: 355 DrmHwcTwo *hwc2_; 356 DrmDevice *drm_; 357 }; 358 359 private: toDrmHwcTwo(hwc2_device_t * dev)360 static DrmHwcTwo *toDrmHwcTwo(hwc2_device_t *dev) { 361 return static_cast<DrmHwcTwo *>(dev); 362 } 363 364 template <typename PFN, typename T> ToHook(T function)365 static hwc2_function_pointer_t ToHook(T function) { 366 static_assert(std::is_same<PFN, T>::value, "Incompatible fn pointer"); 367 return reinterpret_cast<hwc2_function_pointer_t>(function); 368 } 369 370 template <typename T, typename HookType, HookType func, typename... Args> DeviceHook(hwc2_device_t * dev,Args...args)371 static T DeviceHook(hwc2_device_t *dev, Args... args) { 372 DrmHwcTwo *hwc = toDrmHwcTwo(dev); 373 return static_cast<T>(((*hwc).*func)(std::forward<Args>(args)...)); 374 } 375 GetDisplay(DrmHwcTwo * hwc,hwc2_display_t display_handle)376 static HwcDisplay *GetDisplay(DrmHwcTwo *hwc, hwc2_display_t display_handle) { 377 auto it = hwc->displays_.find(display_handle); 378 if (it == hwc->displays_.end()) 379 return nullptr; 380 381 return &it->second; 382 } 383 384 template <typename HookType, HookType func, typename... Args> DisplayHook(hwc2_device_t * dev,hwc2_display_t display_handle,Args...args)385 static int32_t DisplayHook(hwc2_device_t *dev, hwc2_display_t display_handle, 386 Args... args) { 387 HwcDisplay *display = GetDisplay(toDrmHwcTwo(dev), display_handle); 388 if (!display) 389 return static_cast<int32_t>(HWC2::Error::BadDisplay); 390 391 return static_cast<int32_t>((display->*func)(std::forward<Args>(args)...)); 392 } 393 394 template <typename HookType, HookType func, typename... Args> LayerHook(hwc2_device_t * dev,hwc2_display_t display_handle,hwc2_layer_t layer_handle,Args...args)395 static int32_t LayerHook(hwc2_device_t *dev, hwc2_display_t display_handle, 396 hwc2_layer_t layer_handle, Args... args) { 397 HwcDisplay *display = GetDisplay(toDrmHwcTwo(dev), display_handle); 398 if (!display) 399 return static_cast<int32_t>(HWC2::Error::BadDisplay); 400 401 HwcLayer *layer = display->get_layer(layer_handle); 402 if (!layer) 403 return static_cast<int32_t>(HWC2::Error::BadLayer); 404 405 return static_cast<int32_t>((layer->*func)(std::forward<Args>(args)...)); 406 } 407 408 // hwc2_device_t hooks 409 static int HookDevClose(hw_device_t *dev); 410 static void HookDevGetCapabilities(hwc2_device_t *dev, uint32_t *out_count, 411 int32_t *out_capabilities); 412 static hwc2_function_pointer_t HookDevGetFunction(struct hwc2_device *device, 413 int32_t descriptor); 414 415 // Device functions 416 HWC2::Error CreateVirtualDisplay(uint32_t width, uint32_t height, 417 int32_t *format, hwc2_display_t *display); 418 HWC2::Error DestroyVirtualDisplay(hwc2_display_t display); 419 void Dump(uint32_t *outSize, char *outBuffer); 420 uint32_t GetMaxVirtualDisplayCount(); 421 HWC2::Error RegisterCallback(int32_t descriptor, hwc2_callback_data_t data, 422 hwc2_function_pointer_t function); 423 HWC2::Error CreateDisplay(hwc2_display_t displ, HWC2::DisplayType type); 424 void HandleDisplayHotplug(hwc2_display_t displayid, int state); 425 void HandleInitialHotplugState(DrmDevice *drmDevice); 426 427 ResourceManager resource_manager_; 428 std::map<hwc2_display_t, HwcDisplay> displays_; 429 430 std::string mDumpString; 431 }; 432 } // namespace android 433 434 #endif 435