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 | |
Roman Stratiienko | d21071f | 2021-03-09 21:56:50 +0200 | [diff] [blame] | 24 | #include <functional> |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 25 | #include <memory> |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 26 | #include <sstream> |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 27 | #include <tuple> |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 28 | |
Roman Stratiienko | 13cc366 | 2020-08-29 21:35:39 +0300 | [diff] [blame] | 29 | #include "DrmDisplayComposition.h" |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 30 | #include "Planner.h" |
Roman Stratiienko | 13cc366 | 2020-08-29 21:35:39 +0300 | [diff] [blame] | 31 | #include "drm/ResourceManager.h" |
| 32 | #include "drm/VSyncWorker.h" |
Roman Stratiienko | aa3cd54 | 2020-08-29 11:26:16 +0300 | [diff] [blame] | 33 | #include "drmhwcomposer.h" |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 34 | |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 35 | // If a scene is still for this number of vblanks flatten it to reduce power |
| 36 | // consumption. |
| 37 | #define FLATTEN_COUNTDOWN_INIT 60 |
| 38 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 39 | namespace android { |
| 40 | |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 41 | enum class FlatteningState { |
| 42 | kNone, |
| 43 | kNotNeeded, |
| 44 | kClientRequested, |
| 45 | kClientDone, |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 46 | }; |
| 47 | |
Roman Kovalivskyi | 9170b31 | 2020-02-03 18:13:57 +0200 | [diff] [blame] | 48 | std::ostream &operator<<(std::ostream &str, FlatteningState state); |
| 49 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 50 | class DrmDisplayCompositor { |
| 51 | public: |
| 52 | DrmDisplayCompositor(); |
| 53 | ~DrmDisplayCompositor(); |
| 54 | |
Roman Stratiienko | 863a3c2 | 2021-09-29 13:00:29 +0300 | [diff] [blame^] | 55 | auto Init(ResourceManager *resource_manager, int display, |
| 56 | std::function<void()> client_refresh_callback) -> int; |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 57 | |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 58 | std::unique_ptr<DrmDisplayComposition> CreateInitializedComposition() const; |
Sean Paul | ed45a8e | 2017-02-28 13:17:34 -0500 | [diff] [blame] | 59 | int ApplyComposition(std::unique_ptr<DrmDisplayComposition> composition); |
Rob Herring | 4f6c62e | 2018-05-17 14:33:02 -0500 | [diff] [blame] | 60 | int TestComposition(DrmDisplayComposition *composition); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 61 | int Composite(); |
| 62 | void Dump(std::ostringstream *out) const; |
Andrii Chepurnyi | 495e4cc | 2018-08-01 17:42:56 +0300 | [diff] [blame] | 63 | void ClearDisplay(); |
Roman Stratiienko | 0fade37 | 2021-02-20 13:59:55 +0200 | [diff] [blame] | 64 | UniqueFd TakeOutFence() { |
| 65 | if (!active_composition_) { |
| 66 | return UniqueFd(); |
| 67 | } |
| 68 | return std::move(active_composition_->out_fence_); |
Matteo Franchin | c56eede | 2019-12-03 17:10:38 +0000 | [diff] [blame] | 69 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 70 | |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 71 | FlatteningState GetFlatteningState() const; |
Roman Kovalivskyi | 9170b31 | 2020-02-03 18:13:57 +0200 | [diff] [blame] | 72 | uint32_t GetFlattenedFramesCount() const; |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 73 | bool ShouldFlattenOnClient() const; |
| 74 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 75 | std::tuple<uint32_t, uint32_t, int> GetActiveModeResolution(); |
| 76 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 77 | private: |
Roman Stratiienko | 863a3c2 | 2021-09-29 13:00:29 +0300 | [diff] [blame^] | 78 | std::function<void()> client_refresh_callback_; |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 79 | struct ModeState { |
| 80 | bool needs_modeset = false; |
| 81 | DrmMode mode; |
| 82 | uint32_t blob_id = 0; |
| 83 | uint32_t old_blob_id = 0; |
| 84 | }; |
| 85 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 86 | DrmDisplayCompositor(const DrmDisplayCompositor &) = delete; |
| 87 | |
Sean Paul | 971be15 | 2015-10-13 15:44:45 -0400 | [diff] [blame] | 88 | // We'll wait for acquire fences to fire for kAcquireWaitTimeoutMs, |
| 89 | // kAcquireWaitTries times, logging a warning in between. |
| 90 | static const int kAcquireWaitTries = 5; |
| 91 | static const int kAcquireWaitTimeoutMs = 100; |
Sean Paul | d106b91 | 2015-09-29 00:56:00 -0400 | [diff] [blame] | 92 | |
Roman Stratiienko | 7477471 | 2021-02-05 16:32:47 +0200 | [diff] [blame] | 93 | int CommitFrame(DrmDisplayComposition *display_comp, bool test_only); |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 94 | int ApplyDpms(DrmDisplayComposition *display_comp); |
Sean Paul | 7b1e4bc | 2015-10-13 15:47:22 -0400 | [diff] [blame] | 95 | int DisablePlanes(DrmDisplayComposition *display_comp); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 96 | |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 97 | void ApplyFrame(std::unique_ptr<DrmDisplayComposition> composition, |
Roman Stratiienko | 7477471 | 2021-02-05 16:32:47 +0200 | [diff] [blame] | 98 | int status); |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 99 | |
Roman Kovalivskyi | 9170b31 | 2020-02-03 18:13:57 +0200 | [diff] [blame] | 100 | void SetFlattening(FlatteningState new_state); |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 101 | bool IsFlatteningNeeded() const; |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 102 | int FlattenActiveComposition(); |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 103 | int FlattenOnClient(); |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 104 | |
| 105 | bool CountdownExpired() const; |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 106 | |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 107 | std::tuple<int, uint32_t> CreateModeBlob(const DrmMode &mode); |
| 108 | |
Alexandru Gheorghe | 6f0030f | 2018-05-01 17:25:48 +0100 | [diff] [blame] | 109 | ResourceManager *resource_manager_; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 110 | int display_; |
| 111 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 112 | std::unique_ptr<DrmDisplayComposition> active_composition_; |
| 113 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 114 | bool initialized_; |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 115 | bool active_; |
Sean Paul | 6c18b3b | 2015-11-25 11:04:25 -0500 | [diff] [blame] | 116 | bool use_hw_overlays_; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 117 | |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 118 | ModeState mode_; |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 119 | |
Sean Paul | ed45a8e | 2017-02-28 13:17:34 -0500 | [diff] [blame] | 120 | // mutable since we need to acquire in Dump() |
Roman Stratiienko | b3b5c1e | 2021-02-15 13:44:19 +0200 | [diff] [blame] | 121 | mutable pthread_mutex_t lock_{}; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 122 | |
| 123 | // State tracking progress since our last Dump(). These are mutable since |
| 124 | // we need to reset them on every Dump() call. |
| 125 | mutable uint64_t dump_frames_composited_; |
| 126 | mutable uint64_t dump_last_timestamp_ns_; |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 127 | VSyncWorker vsync_worker_; |
| 128 | int64_t flatten_countdown_; |
| 129 | std::unique_ptr<Planner> planner_; |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 130 | |
| 131 | FlatteningState flattening_state_; |
Roman Kovalivskyi | 9170b31 | 2020-02-03 18:13:57 +0200 | [diff] [blame] | 132 | uint32_t frames_flattened_; |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 133 | |
| 134 | std::function<void(int)> refresh_display_cb_; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 135 | }; |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 136 | } // namespace android |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 137 | |
| 138 | #endif // ANDROID_DRM_DISPLAY_COMPOSITOR_H_ |