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 | |
Zach Reizner | 7642c92 | 2015-10-29 10:11:16 -0700 | [diff] [blame] | 20 | #include "drmhwcomposer.h" |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 21 | #include "drmcomposition.h" |
| 22 | #include "drmcompositorworker.h" |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 23 | #include "drmframebuffer.h" |
Haixia Shi | aa2f4a5 | 2015-11-02 10:54:29 -0800 | [diff] [blame] | 24 | #include "separate_rects.h" |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 25 | |
| 26 | #include <pthread.h> |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 27 | #include <memory> |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 28 | #include <queue> |
| 29 | #include <sstream> |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 30 | #include <tuple> |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 31 | |
| 32 | #include <hardware/hardware.h> |
| 33 | #include <hardware/hwcomposer.h> |
| 34 | |
Sean Paul | 647beb2 | 2015-11-19 13:55:48 -0500 | [diff] [blame] | 35 | // One for the front, one for the back, and one for cases where we need to |
| 36 | // squash a frame that the hw can't display with hw overlays. |
| 37 | #define DRM_DISPLAY_BUFFERS 3 |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 38 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 39 | namespace android { |
| 40 | |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 41 | class GLWorkerCompositor; |
| 42 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 43 | class SquashState { |
| 44 | public: |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 45 | static const unsigned kHistoryLength = 6; // TODO: make this number not magic |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 46 | static const unsigned kMaxLayers = 64; |
| 47 | |
| 48 | struct Region { |
| 49 | DrmHwcRect<int> rect; |
| 50 | std::bitset<kMaxLayers> layer_refs; |
| 51 | std::bitset<kHistoryLength> change_history; |
| 52 | bool squashed = false; |
| 53 | }; |
| 54 | |
| 55 | bool is_stable(int region_index) const { |
| 56 | return valid_history_ >= kHistoryLength && |
| 57 | regions_[region_index].change_history.none(); |
| 58 | } |
| 59 | |
| 60 | const std::vector<Region> ®ions() const { |
| 61 | return regions_; |
| 62 | } |
| 63 | |
| 64 | void Init(DrmHwcLayer *layers, size_t num_layers); |
Zach Reizner | 5757e82 | 2015-10-16 19:06:31 -0700 | [diff] [blame] | 65 | void GenerateHistory(DrmHwcLayer *layers, size_t num_layers, |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 66 | std::vector<bool> &changed_regions) const; |
| 67 | void StableRegionsWithMarginalHistory( |
| 68 | const std::vector<bool> &changed_regions, |
| 69 | std::vector<bool> &stable_regions) const; |
Zach Reizner | 5757e82 | 2015-10-16 19:06:31 -0700 | [diff] [blame] | 70 | void RecordHistory(DrmHwcLayer *layers, size_t num_layers, |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 71 | const std::vector<bool> &changed_regions); |
Zach Reizner | 5757e82 | 2015-10-16 19:06:31 -0700 | [diff] [blame] | 72 | bool RecordAndCompareSquashed(const std::vector<bool> &squashed_regions); |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 73 | |
Zach Reizner | fd6dc33 | 2015-10-13 21:12:48 -0700 | [diff] [blame] | 74 | void Dump(std::ostringstream *out) const; |
| 75 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 76 | private: |
| 77 | size_t generation_number_ = 0; |
| 78 | unsigned valid_history_ = 0; |
| 79 | std::vector<buffer_handle_t> last_handles_; |
| 80 | |
| 81 | std::vector<Region> regions_; |
| 82 | }; |
| 83 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 84 | class DrmDisplayCompositor { |
| 85 | public: |
| 86 | DrmDisplayCompositor(); |
| 87 | ~DrmDisplayCompositor(); |
| 88 | |
| 89 | int Init(DrmResources *drm, int display); |
| 90 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 91 | std::unique_ptr<DrmDisplayComposition> CreateComposition() const; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 92 | int QueueComposition(std::unique_ptr<DrmDisplayComposition> composition); |
| 93 | int Composite(); |
Zach Reizner | bff33ac | 2015-11-16 11:08:46 -0800 | [diff] [blame] | 94 | int SquashAll(); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 95 | void Dump(std::ostringstream *out) const; |
| 96 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 97 | std::tuple<uint32_t, uint32_t, int> GetActiveModeResolution(); |
| 98 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 99 | bool HaveQueuedComposites() const; |
| 100 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 101 | SquashState *squash_state() { |
Zach Reizner | 5757e82 | 2015-10-16 19:06:31 -0700 | [diff] [blame] | 102 | return &squash_state_; |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 105 | private: |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 106 | struct FrameState { |
| 107 | std::unique_ptr<DrmDisplayComposition> composition; |
| 108 | int status = 0; |
| 109 | }; |
| 110 | |
| 111 | class FrameWorker : public Worker { |
| 112 | public: |
| 113 | FrameWorker(DrmDisplayCompositor *compositor); |
Haixia Shi | 479412c | 2015-10-27 10:40:48 -0700 | [diff] [blame] | 114 | ~FrameWorker() override; |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 115 | |
| 116 | int Init(); |
| 117 | void QueueFrame(std::unique_ptr<DrmDisplayComposition> composition, |
| 118 | int status); |
| 119 | |
| 120 | protected: |
Haixia Shi | 479412c | 2015-10-27 10:40:48 -0700 | [diff] [blame] | 121 | void Routine() override; |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 122 | |
| 123 | private: |
| 124 | DrmDisplayCompositor *compositor_; |
| 125 | std::queue<FrameState> frame_queue_; |
| 126 | }; |
| 127 | |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 128 | struct ModeState { |
| 129 | bool needs_modeset = false; |
| 130 | DrmMode mode; |
| 131 | uint32_t blob_id = 0; |
| 132 | uint32_t old_blob_id = 0; |
| 133 | }; |
| 134 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 135 | DrmDisplayCompositor(const DrmDisplayCompositor &) = delete; |
| 136 | |
Sean Paul | 971be15 | 2015-10-13 15:44:45 -0400 | [diff] [blame] | 137 | // We'll wait for acquire fences to fire for kAcquireWaitTimeoutMs, |
| 138 | // kAcquireWaitTries times, logging a warning in between. |
| 139 | static const int kAcquireWaitTries = 5; |
| 140 | static const int kAcquireWaitTimeoutMs = 100; |
Sean Paul | d106b91 | 2015-09-29 00:56:00 -0400 | [diff] [blame] | 141 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame] | 142 | int PrepareFramebuffer(DrmFramebuffer &fb, |
| 143 | DrmDisplayComposition *display_comp); |
Zach Reizner | 5757e82 | 2015-10-16 19:06:31 -0700 | [diff] [blame] | 144 | int ApplySquash(DrmDisplayComposition *display_comp); |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 145 | int ApplyPreComposite(DrmDisplayComposition *display_comp); |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 146 | int PrepareFrame(DrmDisplayComposition *display_comp); |
Sean Paul | c07b211 | 2015-11-17 16:38:10 -0500 | [diff] [blame] | 147 | int CommitFrame(DrmDisplayComposition *display_comp, bool test_only); |
Sean Paul | d51c761 | 2015-11-18 14:12:51 -0500 | [diff] [blame] | 148 | int SquashFrame(DrmDisplayComposition *src, DrmDisplayComposition *dst); |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 149 | int ApplyDpms(DrmDisplayComposition *display_comp); |
Sean Paul | 7b1e4bc | 2015-10-13 15:47:22 -0400 | [diff] [blame] | 150 | int DisablePlanes(DrmDisplayComposition *display_comp); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 151 | |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 152 | void ApplyFrame(std::unique_ptr<DrmDisplayComposition> composition, |
| 153 | int status); |
| 154 | |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 155 | std::tuple<int, uint32_t> CreateModeBlob(const DrmMode &mode); |
| 156 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 157 | DrmResources *drm_; |
| 158 | int display_; |
| 159 | |
| 160 | DrmCompositorWorker worker_; |
Haixia Shi | dda2fab | 2015-10-22 18:12:49 -0700 | [diff] [blame] | 161 | FrameWorker frame_worker_; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 162 | |
| 163 | std::queue<std::unique_ptr<DrmDisplayComposition>> composite_queue_; |
| 164 | std::unique_ptr<DrmDisplayComposition> active_composition_; |
| 165 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 166 | bool initialized_; |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 167 | bool active_; |
Sean Paul | 6c18b3b | 2015-11-25 11:04:25 -0500 | [diff] [blame^] | 168 | bool use_hw_overlays_; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 169 | |
Sean Paul | 35301f4 | 2015-11-17 14:46:56 -0500 | [diff] [blame] | 170 | ModeState mode_; |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 171 | |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 172 | int framebuffer_index_; |
| 173 | DrmFramebuffer framebuffers_[DRM_DISPLAY_BUFFERS]; |
| 174 | std::unique_ptr<GLWorkerCompositor> pre_compositor_; |
| 175 | |
Zach Reizner | 5757e82 | 2015-10-16 19:06:31 -0700 | [diff] [blame] | 176 | SquashState squash_state_; |
| 177 | int squash_framebuffer_index_; |
| 178 | DrmFramebuffer squash_framebuffers_[2]; |
| 179 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 180 | // mutable since we need to acquire in HaveQueuedComposites |
| 181 | mutable pthread_mutex_t lock_; |
| 182 | |
| 183 | // State tracking progress since our last Dump(). These are mutable since |
| 184 | // we need to reset them on every Dump() call. |
| 185 | mutable uint64_t dump_frames_composited_; |
| 186 | mutable uint64_t dump_last_timestamp_ns_; |
| 187 | }; |
| 188 | } |
| 189 | |
| 190 | #endif // ANDROID_DRM_DISPLAY_COMPOSITOR_H_ |