blob: 41e708fe69c146ec36c62e394ef2b3bd61a21843 [file] [log] [blame]
Sean Paul98e73c82015-06-24 14:38:49 -07001/*
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 Stratiienkoaa3cd542020-08-29 11:26:16 +030020#include <hardware/hardware.h>
21#include <hardware/hwcomposer.h>
Sean Paul98e73c82015-06-24 14:38:49 -070022#include <pthread.h>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030023
Roman Stratiienkod21071f2021-03-09 21:56:50 +020024#include <functional>
Zach Reizner92f8e632015-10-12 17:47:13 -070025#include <memory>
Roman Stratiienkof81d2c82022-01-11 19:47:24 +020026#include <optional>
Sean Paul98e73c82015-06-24 14:38:49 -070027#include <sstream>
Zach Reizner92f8e632015-10-12 17:47:13 -070028#include <tuple>
Sean Paul98e73c82015-06-24 14:38:49 -070029
Roman Stratiienko13cc3662020-08-29 21:35:39 +030030#include "DrmDisplayComposition.h"
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030031#include "Planner.h"
Roman Stratiienko13cc3662020-08-29 21:35:39 +030032#include "drm/ResourceManager.h"
33#include "drm/VSyncWorker.h"
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030034#include "drmhwcomposer.h"
Sean Paul98e73c82015-06-24 14:38:49 -070035
36namespace android {
37
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030038struct 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 Paul98e73c82015-06-24 14:38:49 -070056class DrmDisplayCompositor {
57 public:
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030058 DrmDisplayCompositor() = default;
59 ~DrmDisplayCompositor() = default;
Roman Stratiienkoe8c06792021-09-30 10:09:31 +030060 auto Init(ResourceManager *resource_manager, int display) -> int;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020061
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010062 std::unique_ptr<DrmDisplayComposition> CreateInitializedComposition() const;
Roman Stratiienko36a7f282021-10-05 18:17:53 +030063
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030064 auto ExecuteAtomicCommit(AtomicCommitArgs &args) -> int;
Sean Paul98e73c82015-06-24 14:38:49 -070065
Sean Paul98e73c82015-06-24 14:38:49 -070066 DrmDisplayCompositor(const DrmDisplayCompositor &) = delete;
67
Roman Stratiienkoe78235c2021-12-23 17:36:12 +020068 private:
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030069 auto CommitFrame(AtomicCommitArgs &args) -> int;
Sean Paul98e73c82015-06-24 14:38:49 -070070
Roman Stratiienko32685ba2021-12-15 13:46:05 +020071 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 Stratiienkodccc6fb2021-10-23 17:35:44 +030078 DrmModeUserPropertyBlobUnique mode_blob;
Roman Stratiienko32685ba2021-12-15 13:46:05 +020079
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 Paul98e73c82015-06-24 14:38:49 -070091
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030092 ResourceManager *resource_manager_ = nullptr;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010093 std::unique_ptr<Planner> planner_;
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030094 bool initialized_{};
95 int display_ = -1;
Sean Paul98e73c82015-06-24 14:38:49 -070096};
Sean Paulf72cccd2018-08-27 13:59:08 -040097} // namespace android
Sean Paul98e73c82015-06-24 14:38:49 -070098
99#endif // ANDROID_DRM_DISPLAY_COMPOSITOR_H_