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> |
Roman Stratiienko | f81d2c8 | 2022-01-11 19:47:24 +0200 | [diff] [blame^] | 26 | #include <optional> |
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 | |
Roman Stratiienko | 13cc366 | 2020-08-29 21:35:39 +0300 | [diff] [blame] | 30 | #include "DrmDisplayComposition.h" |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 31 | #include "Planner.h" |
Roman Stratiienko | 13cc366 | 2020-08-29 21:35:39 +0300 | [diff] [blame] | 32 | #include "drm/ResourceManager.h" |
| 33 | #include "drm/VSyncWorker.h" |
Roman Stratiienko | aa3cd54 | 2020-08-29 11:26:16 +0300 | [diff] [blame] | 34 | #include "drmhwcomposer.h" |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 35 | |
| 36 | namespace android { |
| 37 | |
Roman Stratiienko | dccc6fb | 2021-10-23 17:35:44 +0300 | [diff] [blame] | 38 | struct AtomicCommitArgs { |
| 39 | /* inputs. All fields are optional, but at least one has to be specified */ |
| 40 | bool test_only = false; |
| 41 | std::optional<DrmMode> display_mode; |
| 42 | std::optional<bool> active; |
| 43 | std::shared_ptr<DrmDisplayComposition> composition; |
| 44 | /* 'clear' should never be used together with 'composition' */ |
| 45 | bool clear_active_composition = false; |
| 46 | |
| 47 | /* out */ |
| 48 | UniqueFd out_fence; |
| 49 | |
| 50 | /* helpers */ |
| 51 | auto HasInputs() -> bool { |
| 52 | return display_mode || active || composition || clear_active_composition; |
| 53 | } |
| 54 | }; |
| 55 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 56 | class DrmDisplayCompositor { |
| 57 | public: |
Roman Stratiienko | dccc6fb | 2021-10-23 17:35:44 +0300 | [diff] [blame] | 58 | DrmDisplayCompositor() = default; |
| 59 | ~DrmDisplayCompositor() = default; |
Roman Stratiienko | e8c0679 | 2021-09-30 10:09:31 +0300 | [diff] [blame] | 60 | auto Init(ResourceManager *resource_manager, int display) -> int; |
Roman Kovalivskyi | 8fae156 | 2020-01-30 20:20:47 +0200 | [diff] [blame] | 61 | |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 62 | std::unique_ptr<DrmDisplayComposition> CreateInitializedComposition() const; |
Roman Stratiienko | 36a7f28 | 2021-10-05 18:17:53 +0300 | [diff] [blame] | 63 | |
Roman Stratiienko | dccc6fb | 2021-10-23 17:35:44 +0300 | [diff] [blame] | 64 | auto ExecuteAtomicCommit(AtomicCommitArgs &args) -> int; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 65 | |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 66 | private: |
| 67 | DrmDisplayCompositor(const DrmDisplayCompositor &) = delete; |
| 68 | |
Roman Stratiienko | dccc6fb | 2021-10-23 17:35:44 +0300 | [diff] [blame] | 69 | auto CommitFrame(AtomicCommitArgs &args) -> int; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 70 | |
Roman Stratiienko | 32685ba | 2021-12-15 13:46:05 +0200 | [diff] [blame] | 71 | struct KmsState { |
| 72 | /* Required to cleanup unused planes */ |
| 73 | std::vector<DrmPlane *> used_planes; |
| 74 | /* We have to hold a reference to framebuffer while displaying it , |
| 75 | * otherwise picture will blink */ |
| 76 | std::vector<std::shared_ptr<DrmFbIdHandle>> used_framebuffers; |
| 77 | |
Roman Stratiienko | dccc6fb | 2021-10-23 17:35:44 +0300 | [diff] [blame] | 78 | DrmModeUserPropertyBlobUnique mode_blob; |
Roman Stratiienko | 32685ba | 2021-12-15 13:46:05 +0200 | [diff] [blame] | 79 | |
| 80 | /* To avoid setting the inactive state twice, which will fail the commit */ |
| 81 | bool crtc_active_state{}; |
| 82 | } active_frame_state; |
| 83 | |
| 84 | auto NewFrameState() -> KmsState { |
| 85 | return (KmsState){ |
| 86 | .used_planes = active_frame_state.used_planes, |
| 87 | .used_framebuffers = active_frame_state.used_framebuffers, |
| 88 | .crtc_active_state = active_frame_state.crtc_active_state, |
| 89 | }; |
| 90 | } |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 91 | |
Roman Stratiienko | dccc6fb | 2021-10-23 17:35:44 +0300 | [diff] [blame] | 92 | ResourceManager *resource_manager_ = nullptr; |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 93 | std::unique_ptr<Planner> planner_; |
Roman Stratiienko | dccc6fb | 2021-10-23 17:35:44 +0300 | [diff] [blame] | 94 | bool initialized_{}; |
| 95 | int display_ = -1; |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 96 | }; |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 97 | } // namespace android |
Sean Paul | 98e73c8 | 2015-06-24 14:38:49 -0700 | [diff] [blame] | 98 | |
| 99 | #endif // ANDROID_DRM_DISPLAY_COMPOSITOR_H_ |