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