blob: e4fff56195649b03fed848b096ee95a80e5a21d7 [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
Roman Stratiienkof818d4c2022-12-28 20:12:19 +020017#pragma once
Sean Paul98e73c82015-06-24 14:38:49 -070018
Sean Paul98e73c82015-06-24 14:38:49 -070019#include <pthread.h>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030020
Zach Reizner92f8e632015-10-12 17:47:13 -070021#include <memory>
Roman Stratiienkof81d2c82022-01-11 19:47:24 +020022#include <optional>
Sean Paul98e73c82015-06-24 14:38:49 -070023
Tim Van Pattena2f3efa2024-10-15 17:44:54 -060024#include "compositor/DisplayInfo.h"
Roman Stratiienko4e994052022-02-09 17:40:35 +020025#include "compositor/DrmKmsPlan.h"
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020026#include "compositor/LayerData.h"
Roman Stratiienko9362cef2022-02-02 09:53:50 +020027#include "drm/DrmPlane.h"
Roman Stratiienko13cc3662020-08-29 21:35:39 +030028#include "drm/ResourceManager.h"
29#include "drm/VSyncWorker.h"
Sean Paul98e73c82015-06-24 14:38:49 -070030
31namespace android {
32
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030033struct AtomicCommitArgs {
34 /* inputs. All fields are optional, but at least one has to be specified */
35 bool test_only = false;
Drew Davenportbcc587d2024-11-07 10:41:14 -070036 bool blocking = false;
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030037 std::optional<DrmMode> display_mode;
38 std::optional<bool> active;
Roman Stratiienko9362cef2022-02-02 09:53:50 +020039 std::shared_ptr<DrmKmsPlan> composition;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020040 std::shared_ptr<drm_color_ctm> color_matrix;
Sasha McIntosh5294f092024-09-18 18:14:54 -040041 std::optional<Colorspace> colorspace;
Sasha McIntosh173247b2024-09-18 18:06:52 -040042 std::optional<int32_t> content_type;
Sasha McIntoshf9062b62024-11-12 10:55:06 -050043 std::shared_ptr<hdr_output_metadata> hdr_metadata;
Sasha McIntoshdfcc8ed2024-11-07 14:40:45 -050044 std::optional<int32_t> min_bpc;
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030045
Roman Stratiienkof2c060f2023-09-18 22:46:08 +030046 std::shared_ptr<DrmFbIdHandle> writeback_fb;
47 SharedFd writeback_release_fence;
48
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030049 /* out */
Roman Stratiienko76892782023-01-16 17:15:53 +020050 SharedFd out_fence;
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030051
52 /* helpers */
Roman Stratiienko2a93e4c2022-12-19 18:24:47 +020053 auto HasInputs() const -> bool {
Roman Stratiienkoef5348b2022-02-09 17:19:56 +020054 return display_mode || active || composition;
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030055 }
56};
57
Roman Stratiienko59bb4812022-04-09 16:13:09 +030058class DrmAtomicStateManager {
Roman Stratiienko59bb4812022-04-09 16:13:09 +030059 public:
Roman Stratiienkof818d4c2022-12-28 20:12:19 +020060 static auto CreateInstance(DrmDisplayPipeline *pipe)
61 -> std::shared_ptr<DrmAtomicStateManager>;
Roman Stratiienko59bb4812022-04-09 16:13:09 +030062
Roman Stratiienkof818d4c2022-12-28 20:12:19 +020063 ~DrmAtomicStateManager() = default;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020064
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030065 auto ExecuteAtomicCommit(AtomicCommitArgs &args) -> int;
Roman Stratiienkod37b3082022-01-13 16:37:27 +020066 auto ActivateDisplayUsingDPMS() -> int;
67
Roman Stratiienkof818d4c2022-12-28 20:12:19 +020068 void StopThread() {
69 {
70 const std::unique_lock lock(mutex_);
71 exit_thread_ = true;
72 }
73 cv_.notify_all();
74 }
75
Roman Stratiienkoe78235c2021-12-23 17:36:12 +020076 private:
Roman Stratiienkof818d4c2022-12-28 20:12:19 +020077 DrmAtomicStateManager() = default;
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030078 auto CommitFrame(AtomicCommitArgs &args) -> int;
Sean Paul98e73c82015-06-24 14:38:49 -070079
Roman Stratiienko32685ba2021-12-15 13:46:05 +020080 struct KmsState {
81 /* Required to cleanup unused planes */
Roman Stratiienko9362cef2022-02-02 09:53:50 +020082 std::vector<std::shared_ptr<BindingOwner<DrmPlane>>> used_planes;
Roman Stratiienko32685ba2021-12-15 13:46:05 +020083 /* We have to hold a reference to framebuffer while displaying it ,
84 * otherwise picture will blink */
85 std::vector<std::shared_ptr<DrmFbIdHandle>> used_framebuffers;
86
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030087 DrmModeUserPropertyBlobUnique mode_blob;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020088 DrmModeUserPropertyBlobUnique ctm_blob;
Sasha McIntoshf9062b62024-11-12 10:55:06 -050089 DrmModeUserPropertyBlobUnique hdr_metadata_blob;
Roman Stratiienko32685ba2021-12-15 13:46:05 +020090
Roman Stratiienko59bb4812022-04-09 16:13:09 +030091 int release_fence_pt_index{};
92
Roman Stratiienko32685ba2021-12-15 13:46:05 +020093 /* To avoid setting the inactive state twice, which will fail the commit */
94 bool crtc_active_state{};
Roman Stratiienkof815d382021-12-30 19:23:14 +020095 } active_frame_state_;
Roman Stratiienko32685ba2021-12-15 13:46:05 +020096
97 auto NewFrameState() -> KmsState {
Roman Stratiienko59bb4812022-04-09 16:13:09 +030098 auto *prev_frame_state = &active_frame_state_;
Roman Stratiienko32685ba2021-12-15 13:46:05 +020099 return (KmsState){
Roman Stratiienko59bb4812022-04-09 16:13:09 +0300100 .used_planes = prev_frame_state->used_planes,
101 .crtc_active_state = prev_frame_state->crtc_active_state,
Roman Stratiienko32685ba2021-12-15 13:46:05 +0200102 };
103 }
Sean Paul98e73c82015-06-24 14:38:49 -0700104
Roman Stratiienkof818d4c2022-12-28 20:12:19 +0200105 DrmDisplayPipeline *pipe_{};
Roman Stratiienko59bb4812022-04-09 16:13:09 +0300106
107 void CleanupPriorFrameResources();
108
Roman Stratiienko59bb4812022-04-09 16:13:09 +0300109 KmsState staged_frame_state_;
Roman Stratiienko76892782023-01-16 17:15:53 +0200110 SharedFd last_present_fence_;
Roman Stratiienko59bb4812022-04-09 16:13:09 +0300111 int frames_staged_{};
112 int frames_tracked_{};
Roman Stratiienkof818d4c2022-12-28 20:12:19 +0200113
Roman Stratiienko4e15bfc2025-01-23 01:55:21 +0200114 DstRectInfo whole_display_rect_{};
115
Roman Stratiienkof818d4c2022-12-28 20:12:19 +0200116 void ThreadFn(const std::shared_ptr<DrmAtomicStateManager> &dasm);
117 std::condition_variable cv_;
118 std::mutex mutex_;
119 bool exit_thread_{};
Sean Paul98e73c82015-06-24 14:38:49 -0700120};
Roman Stratiienko59bb4812022-04-09 16:13:09 +0300121
Sean Paulf72cccd2018-08-27 13:59:08 -0400122} // namespace android