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 | |
| 20 | #include "drm_hwcomposer.h" |
| 21 | #include "drmcomposition.h" |
| 22 | #include "drmcompositorworker.h" |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 23 | #include "drmframebuffer.h" |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame^] | 24 | #include "seperate_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 | |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 35 | #define DRM_DISPLAY_BUFFERS 2 |
| 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: |
| 43 | static const unsigned kHistoryLength = 6; |
| 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); |
| 63 | void GenerateHistory(DrmHwcLayer *layers, |
| 64 | std::vector<bool> &changed_regions) const; |
| 65 | void StableRegionsWithMarginalHistory( |
| 66 | const std::vector<bool> &changed_regions, |
| 67 | std::vector<bool> &stable_regions) const; |
| 68 | void RecordHistory(DrmHwcLayer *layers, |
| 69 | const std::vector<bool> &changed_regions); |
| 70 | void RecordSquashed(const std::vector<bool> &squashed_regions); |
| 71 | |
| 72 | private: |
| 73 | size_t generation_number_ = 0; |
| 74 | unsigned valid_history_ = 0; |
| 75 | std::vector<buffer_handle_t> last_handles_; |
| 76 | |
| 77 | std::vector<Region> regions_; |
| 78 | }; |
| 79 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 80 | class DrmDisplayCompositor { |
| 81 | public: |
| 82 | DrmDisplayCompositor(); |
| 83 | ~DrmDisplayCompositor(); |
| 84 | |
| 85 | int Init(DrmResources *drm, int display); |
| 86 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame^] | 87 | std::unique_ptr<DrmDisplayComposition> CreateComposition() const; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 88 | int QueueComposition(std::unique_ptr<DrmDisplayComposition> composition); |
| 89 | int Composite(); |
| 90 | void Dump(std::ostringstream *out) const; |
| 91 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame^] | 92 | std::tuple<uint32_t, uint32_t, int> GetActiveModeResolution(); |
| 93 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 94 | bool HaveQueuedComposites() const; |
| 95 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame^] | 96 | SquashState *squash_state() { |
| 97 | return NULL; |
| 98 | } |
| 99 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 100 | private: |
| 101 | DrmDisplayCompositor(const DrmDisplayCompositor &) = delete; |
| 102 | |
Sean Paul | 971be15 | 2015-10-13 15:44:45 -0400 | [diff] [blame] | 103 | // We'll wait for acquire fences to fire for kAcquireWaitTimeoutMs, |
| 104 | // kAcquireWaitTries times, logging a warning in between. |
| 105 | static const int kAcquireWaitTries = 5; |
| 106 | static const int kAcquireWaitTimeoutMs = 100; |
Sean Paul | d106b91 | 2015-09-29 00:56:00 -0400 | [diff] [blame] | 107 | |
Zach Reizner | 92f8e63 | 2015-10-12 17:47:13 -0700 | [diff] [blame^] | 108 | int PrepareFramebuffer(DrmFramebuffer &fb, |
| 109 | DrmDisplayComposition *display_comp); |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 110 | int ApplyPreComposite(DrmDisplayComposition *display_comp); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 111 | int ApplyFrame(DrmDisplayComposition *display_comp); |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 112 | int ApplyDpms(DrmDisplayComposition *display_comp); |
Sean Paul | 7b1e4bc | 2015-10-13 15:47:22 -0400 | [diff] [blame] | 113 | int DisablePlanes(DrmDisplayComposition *display_comp); |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 114 | |
| 115 | DrmResources *drm_; |
| 116 | int display_; |
| 117 | |
| 118 | DrmCompositorWorker worker_; |
| 119 | |
| 120 | std::queue<std::unique_ptr<DrmDisplayComposition>> composite_queue_; |
| 121 | std::unique_ptr<DrmDisplayComposition> active_composition_; |
| 122 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 123 | bool initialized_; |
Sean Paul | db7a17d | 2015-06-24 18:46:05 -0700 | [diff] [blame] | 124 | bool active_; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 125 | |
Sean Paul | 5735541 | 2015-09-19 09:14:34 -0400 | [diff] [blame] | 126 | DrmMode next_mode_; |
| 127 | bool needs_modeset_; |
| 128 | |
Zach Reizner | 713a678 | 2015-07-31 15:12:44 -0700 | [diff] [blame] | 129 | int framebuffer_index_; |
| 130 | DrmFramebuffer framebuffers_[DRM_DISPLAY_BUFFERS]; |
| 131 | std::unique_ptr<GLWorkerCompositor> pre_compositor_; |
| 132 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 133 | // mutable since we need to acquire in HaveQueuedComposites |
| 134 | mutable pthread_mutex_t lock_; |
| 135 | |
| 136 | // State tracking progress since our last Dump(). These are mutable since |
| 137 | // we need to reset them on every Dump() call. |
| 138 | mutable uint64_t dump_frames_composited_; |
| 139 | mutable uint64_t dump_last_timestamp_ns_; |
| 140 | }; |
| 141 | } |
| 142 | |
| 143 | #endif // ANDROID_DRM_DISPLAY_COMPOSITOR_H_ |