Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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_DISPLAY_COMPOSITOR_H_ |
| 18 | #define ANDROID_DRM_DISPLAY_COMPOSITOR_H_ |
| 19 | |
Roman Stratiienko | aa3cd54 | 2020-08-29 11:26:16 +0300 | [diff] [blame^] | 20 | #include <hardware/hardware.h> |
| 21 | #include <hardware/hwcomposer.h> |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 22 | #include <pthread.h> |
Roman Stratiienko | aa3cd54 | 2020-08-29 11:26:16 +0300 | [diff] [blame^] | 23 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 24 | #include <memory> |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 25 | #include <sstream> |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 26 | #include <tuple> |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 27 | |
Roman Stratiienko | aa3cd54 | 2020-08-29 11:26:16 +0300 | [diff] [blame^] | 28 | #include "drm/resourcemanager.h" |
| 29 | #include "drm/vsyncworker.h" |
| 30 | #include "drmdisplaycomposition.h" |
| 31 | #include "drmframebuffer.h" |
| 32 | #include "drmhwcomposer.h" |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 33 | |
Sean Paul | 647beb2 | 2015-11-19 13:55:48 -0500 | [diff] [blame] | 34 | // One for the front, one for the back, and one for cases where we need to |
| 35 | // squash a frame that the hw can't display with hw overlays. |
| 36 | #define DRM_DISPLAY_BUFFERS 3 |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 37 | |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 38 | // If a scene is still for this number of vblanks flatten it to reduce power |
| 39 | // consumption. |
| 40 | #define FLATTEN_COUNTDOWN_INIT 60 |
| 41 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 42 | namespace android { |
| 43 | |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 44 | enum class FlatteningState { |
| 45 | kNone, |
| 46 | kNotNeeded, |
| 47 | kClientRequested, |
| 48 | kClientDone, |
| 49 | kSerial, |
| 50 | kConcurrent |
| 51 | }; |
| 52 | |
Roman Kovalivskyi | 9170b31 | 2020-02-03 18:13:57 +0200 | [diff] [blame] | 53 | std::ostream &operator<<(std::ostream &str, FlatteningState state); |
| 54 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 55 | class DrmDisplayCompositor { |
| 56 | public: |
| 57 | DrmDisplayCompositor(); |
| 58 | ~DrmDisplayCompositor(); |
| 59 | |
Alexandru Gheorghe | 62e2d2c | 2018-05-11 11:40:53 +0100 | [diff] [blame] | 60 | int Init(ResourceManager *resource_manager, int display); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 61 | |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 62 | template <typename Fn> |
| 63 | void SetRefreshCallback(Fn &&refresh_cb) { |
| 64 | refresh_display_cb_ = std::forward<Fn>(refresh_cb); |
| 65 | } |
| 66 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 67 | std::unique_ptr<DrmDisplayComposition> CreateComposition() const; |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 68 | std::unique_ptr<DrmDisplayComposition> CreateInitializedComposition() const; |
Sean Paul | ed45a8e | 2017-02-28 13:17:34 -0500 | [diff] [blame] | 69 | int ApplyComposition(std::unique_ptr<DrmDisplayComposition> composition); |
Rob Herring | 4f6c62e | 2018-05-17 14:33:02 -0500 | [diff] [blame] | 70 | int TestComposition(DrmDisplayComposition *composition); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 71 | int Composite(); |
| 72 | void Dump(std::ostringstream *out) const; |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 73 | void Vsync(int display, int64_t timestamp); |
Andrii Chepurnyi | 495e4cc | 2018-08-01 17:42:56 +0300 | [diff] [blame] | 74 | void ClearDisplay(); |
Matteo Franchin | c56eede | 2019-12-03 17:10:38 +0000 | [diff] [blame] | 75 | int TakeOutFence() { |
| 76 | if (!active_composition_) |
| 77 | return -1; |
| 78 | return active_composition_->take_out_fence(); |
| 79 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 80 | |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 81 | FlatteningState GetFlatteningState() const; |
Roman Kovalivskyi | 9170b31 | 2020-02-03 18:13:57 +0200 | [diff] [blame] | 82 | uint32_t GetFlattenedFramesCount() const; |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 83 | bool ShouldFlattenOnClient() const; |
| 84 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 85 | std::tuple<uint32_t, uint32_t, int> GetActiveModeResolution(); |
| 86 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 87 | private: |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 88 | struct ModeState { |
| 89 | bool needs_modeset = false; |
| 90 | DrmMode mode; |
| 91 | uint32_t blob_id = 0; |
| 92 | uint32_t old_blob_id = 0; |
| 93 | }; |
| 94 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 95 | DrmDisplayCompositor(const DrmDisplayCompositor &) = delete; |
| 96 | |
Sean Paul | 971be15 | 2015-10-13 15:44:45 -0400 | [diff] [blame] | 97 | // We'll wait for acquire fences to fire for kAcquireWaitTimeoutMs, |
| 98 | // kAcquireWaitTries times, logging a warning in between. |
| 99 | static const int kAcquireWaitTries = 5; |
| 100 | static const int kAcquireWaitTimeoutMs = 100; |
Sean Paul | d106b91 | 2015-09-29 00:56:00 -0400 | [diff] [blame] | 101 | |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 102 | int CommitFrame(DrmDisplayComposition *display_comp, bool test_only, |
| 103 | DrmConnector *writeback_conn = NULL, |
| 104 | DrmHwcBuffer *writeback_buffer = NULL); |
| 105 | int SetupWritebackCommit(drmModeAtomicReqPtr pset, uint32_t crtc_id, |
| 106 | DrmConnector *writeback_conn, |
| 107 | DrmHwcBuffer *writeback_buffer); |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 108 | int ApplyDpms(DrmDisplayComposition *display_comp); |
Sean Paul | 7b1e4bc | 2015-10-13 15:47:22 -0400 | [diff] [blame] | 109 | int DisablePlanes(DrmDisplayComposition *display_comp); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 110 | |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 111 | void ApplyFrame(std::unique_ptr<DrmDisplayComposition> composition, |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 112 | int status, bool writeback = false); |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 113 | |
Roman Kovalivskyi | 9170b31 | 2020-02-03 18:13:57 +0200 | [diff] [blame] | 114 | void SetFlattening(FlatteningState new_state); |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 115 | bool IsFlatteningNeeded() const; |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 116 | int FlattenActiveComposition(); |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 117 | int FlattenOnClient(); |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 118 | int FlattenSerial(DrmConnector *writeback_conn); |
| 119 | int FlattenConcurrent(DrmConnector *writeback_conn); |
| 120 | int FlattenOnDisplay(std::unique_ptr<DrmDisplayComposition> &src, |
| 121 | DrmConnector *writeback_conn, DrmMode &src_mode, |
| 122 | DrmHwcLayer *writeback_layer); |
| 123 | |
| 124 | bool CountdownExpired() const; |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 125 | |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 126 | std::tuple<int, uint32_t> CreateModeBlob(const DrmMode &mode); |
| 127 | |
Alexandru Gheorghe | 6f0030f | 2018-05-01 17:25:48 +0100 | [diff] [blame] | 128 | ResourceManager *resource_manager_; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 129 | int display_; |
| 130 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 131 | std::unique_ptr<DrmDisplayComposition> active_composition_; |
| 132 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 133 | bool initialized_; |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 134 | bool active_; |
Sean Paul | 6c18b3b | 2015-11-25 11:04:25 -0500 | [diff] [blame] | 135 | bool use_hw_overlays_; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 136 | |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 137 | ModeState mode_; |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 138 | |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 139 | int framebuffer_index_; |
| 140 | DrmFramebuffer framebuffers_[DRM_DISPLAY_BUFFERS]; |
Zach Reizner | 5757e82 | 2015-10-16 19:06:31 -0700 | [diff] [blame] | 141 | |
Sean Paul | ed45a8e | 2017-02-28 13:17:34 -0500 | [diff] [blame] | 142 | // mutable since we need to acquire in Dump() |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 143 | mutable pthread_mutex_t lock_; |
| 144 | |
| 145 | // State tracking progress since our last Dump(). These are mutable since |
| 146 | // we need to reset them on every Dump() call. |
| 147 | mutable uint64_t dump_frames_composited_; |
| 148 | mutable uint64_t dump_last_timestamp_ns_; |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 149 | VSyncWorker vsync_worker_; |
| 150 | int64_t flatten_countdown_; |
| 151 | std::unique_ptr<Planner> planner_; |
| 152 | int writeback_fence_; |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 153 | |
| 154 | FlatteningState flattening_state_; |
Roman Kovalivskyi | 9170b31 | 2020-02-03 18:13:57 +0200 | [diff] [blame] | 155 | uint32_t frames_flattened_; |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 156 | |
| 157 | std::function<void(int)> refresh_display_cb_; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 158 | }; |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 159 | } // namespace android |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 160 | |
| 161 | #endif // ANDROID_DRM_DISPLAY_COMPOSITOR_H_ |