blob: 923927d76ed8f24fbfb789e8beb74e99f59af534 [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
Roman Stratiienko4e994052022-02-09 17:40:35 +020024#include "compositor/DrmKmsPlan.h"
Roman Stratiienko4b2cc482022-02-21 14:53:58 +020025#include "compositor/LayerData.h"
Roman Stratiienko9362cef2022-02-02 09:53:50 +020026#include "drm/DrmPlane.h"
Roman Stratiienko13cc3662020-08-29 21:35:39 +030027#include "drm/ResourceManager.h"
28#include "drm/VSyncWorker.h"
Sean Paul98e73c82015-06-24 14:38:49 -070029
30namespace android {
31
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030032struct AtomicCommitArgs {
33 /* inputs. All fields are optional, but at least one has to be specified */
34 bool test_only = false;
35 std::optional<DrmMode> display_mode;
36 std::optional<bool> active;
Roman Stratiienko9362cef2022-02-02 09:53:50 +020037 std::shared_ptr<DrmKmsPlan> composition;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020038 std::shared_ptr<drm_color_ctm> color_matrix;
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030039
Roman Stratiienkof2c060f2023-09-18 22:46:08 +030040 std::shared_ptr<DrmFbIdHandle> writeback_fb;
41 SharedFd writeback_release_fence;
42
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030043 /* out */
Roman Stratiienko76892782023-01-16 17:15:53 +020044 SharedFd out_fence;
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030045
46 /* helpers */
Roman Stratiienko2a93e4c2022-12-19 18:24:47 +020047 auto HasInputs() const -> bool {
Roman Stratiienkoef5348b2022-02-09 17:19:56 +020048 return display_mode || active || composition;
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030049 }
50};
51
Roman Stratiienko59bb4812022-04-09 16:13:09 +030052class DrmAtomicStateManager {
Roman Stratiienko59bb4812022-04-09 16:13:09 +030053 public:
Roman Stratiienkof818d4c2022-12-28 20:12:19 +020054 static auto CreateInstance(DrmDisplayPipeline *pipe)
55 -> std::shared_ptr<DrmAtomicStateManager>;
Roman Stratiienko59bb4812022-04-09 16:13:09 +030056
Roman Stratiienkof818d4c2022-12-28 20:12:19 +020057 ~DrmAtomicStateManager() = default;
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020058
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030059 auto ExecuteAtomicCommit(AtomicCommitArgs &args) -> int;
Roman Stratiienkod37b3082022-01-13 16:37:27 +020060 auto ActivateDisplayUsingDPMS() -> int;
61
Roman Stratiienkof818d4c2022-12-28 20:12:19 +020062 void StopThread() {
63 {
64 const std::unique_lock lock(mutex_);
65 exit_thread_ = true;
66 }
67 cv_.notify_all();
68 }
69
Roman Stratiienkoe78235c2021-12-23 17:36:12 +020070 private:
Roman Stratiienkof818d4c2022-12-28 20:12:19 +020071 DrmAtomicStateManager() = default;
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030072 auto CommitFrame(AtomicCommitArgs &args) -> int;
Sean Paul98e73c82015-06-24 14:38:49 -070073
Roman Stratiienko32685ba2021-12-15 13:46:05 +020074 struct KmsState {
75 /* Required to cleanup unused planes */
Roman Stratiienko9362cef2022-02-02 09:53:50 +020076 std::vector<std::shared_ptr<BindingOwner<DrmPlane>>> used_planes;
Roman Stratiienko32685ba2021-12-15 13:46:05 +020077 /* We have to hold a reference to framebuffer while displaying it ,
78 * otherwise picture will blink */
79 std::vector<std::shared_ptr<DrmFbIdHandle>> used_framebuffers;
80
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +030081 DrmModeUserPropertyBlobUnique mode_blob;
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020082 DrmModeUserPropertyBlobUnique ctm_blob;
Roman Stratiienko32685ba2021-12-15 13:46:05 +020083
Roman Stratiienko59bb4812022-04-09 16:13:09 +030084 int release_fence_pt_index{};
85
Roman Stratiienko32685ba2021-12-15 13:46:05 +020086 /* To avoid setting the inactive state twice, which will fail the commit */
87 bool crtc_active_state{};
Roman Stratiienkof815d382021-12-30 19:23:14 +020088 } active_frame_state_;
Roman Stratiienko32685ba2021-12-15 13:46:05 +020089
90 auto NewFrameState() -> KmsState {
Roman Stratiienko59bb4812022-04-09 16:13:09 +030091 auto *prev_frame_state = &active_frame_state_;
Roman Stratiienko32685ba2021-12-15 13:46:05 +020092 return (KmsState){
Roman Stratiienko59bb4812022-04-09 16:13:09 +030093 .used_planes = prev_frame_state->used_planes,
94 .crtc_active_state = prev_frame_state->crtc_active_state,
Roman Stratiienko32685ba2021-12-15 13:46:05 +020095 };
96 }
Sean Paul98e73c82015-06-24 14:38:49 -070097
Roman Stratiienkof818d4c2022-12-28 20:12:19 +020098 DrmDisplayPipeline *pipe_{};
Roman Stratiienko59bb4812022-04-09 16:13:09 +030099
100 void CleanupPriorFrameResources();
101
Roman Stratiienko59bb4812022-04-09 16:13:09 +0300102 KmsState staged_frame_state_;
Roman Stratiienko76892782023-01-16 17:15:53 +0200103 SharedFd last_present_fence_;
Roman Stratiienko59bb4812022-04-09 16:13:09 +0300104 int frames_staged_{};
105 int frames_tracked_{};
Roman Stratiienkof818d4c2022-12-28 20:12:19 +0200106
107 void ThreadFn(const std::shared_ptr<DrmAtomicStateManager> &dasm);
108 std::condition_variable cv_;
109 std::mutex mutex_;
110 bool exit_thread_{};
Sean Paul98e73c82015-06-24 14:38:49 -0700111};
Roman Stratiienko59bb4812022-04-09 16:13:09 +0300112
Sean Paulf72cccd2018-08-27 13:59:08 -0400113} // namespace android