blob: 40d06a8a8c83b39e3b2ad87f0858e9c35793fbd6 [file] [log] [blame]
Vishnu Nair6b591152021-10-08 11:45:14 -07001/*
Dominik Laskowski1f6fc702022-03-21 08:34:50 -07002 * Copyright 2021 The Android Open Source Project
Vishnu Nair6b591152021-10-08 11:45:14 -07003 *
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#pragma once
18
Dominik Laskowski1f6fc702022-03-21 08:34:50 -070019#include <condition_variable>
20#include <memory>
21#include <mutex>
22#include <vector>
Vishnu Nair40fff5c2022-11-04 02:46:28 +000023#include "renderengine/ExternalTexture.h"
Dominik Laskowski1f6fc702022-03-21 08:34:50 -070024
Vishnu Nair6b591152021-10-08 11:45:14 -070025#include <gui/LayerState.h>
Dominik Laskowski1f6fc702022-03-21 08:34:50 -070026#include <system/window.h>
Vishnu Nair6b591152021-10-08 11:45:14 -070027
28namespace android {
Dominik Laskowski1f6fc702022-03-21 08:34:50 -070029
liulijuneb489f62022-10-17 22:02:14 +080030enum TraverseBuffersReturnValues {
31 CONTINUE_TRAVERSAL,
32 STOP_TRAVERSAL,
33 DELETE_AND_CONTINUE_TRAVERSAL,
34};
35
Brian Lindahl439afad2022-11-14 11:16:55 -070036// Extends the client side composer state by resolving buffer.
Vishnu Nair40fff5c2022-11-04 02:46:28 +000037class ResolvedComposerState : public ComposerState {
38public:
39 ResolvedComposerState() = default;
40 ResolvedComposerState(ComposerState&& source) { state = std::move(source.state); }
41 std::shared_ptr<renderengine::ExternalTexture> externalTexture;
42};
43
Vishnu Nair6b591152021-10-08 11:45:14 -070044struct TransactionState {
Dominik Laskowski1f6fc702022-03-21 08:34:50 -070045 TransactionState() = default;
46
Vishnu Nair6b591152021-10-08 11:45:14 -070047 TransactionState(const FrameTimelineInfo& frameTimelineInfo,
Vishnu Nair40fff5c2022-11-04 02:46:28 +000048 std::vector<ResolvedComposerState>& composerStates,
Vishnu Nair6b591152021-10-08 11:45:14 -070049 const Vector<DisplayState>& displayStates, uint32_t transactionFlags,
50 const sp<IBinder>& applyToken, const InputWindowCommands& inputWindowCommands,
51 int64_t desiredPresentTime, bool isAutoTimestamp,
Patrick Williams6c6dd3b2023-02-13 22:53:06 +000052 std::vector<uint64_t> uncacheBufferIds, int64_t postTime, uint32_t permissions,
Vishnu Nair6b591152021-10-08 11:45:14 -070053 bool hasListenerCallbacks, std::vector<ListenerCallbacks> listenerCallbacks,
54 int originPid, int originUid, uint64_t transactionId)
55 : frameTimelineInfo(frameTimelineInfo),
Vishnu Nair40fff5c2022-11-04 02:46:28 +000056 states(std::move(composerStates)),
Vishnu Nair6b591152021-10-08 11:45:14 -070057 displays(displayStates),
58 flags(transactionFlags),
59 applyToken(applyToken),
60 inputWindowCommands(inputWindowCommands),
61 desiredPresentTime(desiredPresentTime),
62 isAutoTimestamp(isAutoTimestamp),
Patrick Williams6c6dd3b2023-02-13 22:53:06 +000063 uncacheBufferIds(std::move(uncacheBufferIds)),
Vishnu Nair6b591152021-10-08 11:45:14 -070064 postTime(postTime),
65 permissions(permissions),
66 hasListenerCallbacks(hasListenerCallbacks),
67 listenerCallbacks(listenerCallbacks),
68 originPid(originPid),
69 originUid(originUid),
70 id(transactionId) {}
71
Dominik Laskowski1f6fc702022-03-21 08:34:50 -070072 // Invokes `void(const layer_state_t&)` visitor for matching layers.
73 template <typename Visitor>
74 void traverseStatesWithBuffers(Visitor&& visitor) const {
Vishnu Nair40fff5c2022-11-04 02:46:28 +000075 for (const auto& state : states) {
76 if (state.state.hasBufferChanges() && state.state.hasValidBuffer() &&
77 state.state.surface) {
78 visitor(state.state);
Dominik Laskowski1f6fc702022-03-21 08:34:50 -070079 }
80 }
81 }
Vishnu Nair6b591152021-10-08 11:45:14 -070082
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070083 template <typename Visitor>
liulijuneb489f62022-10-17 22:02:14 +080084 void traverseStatesWithBuffersWhileTrue(Visitor&& visitor) {
85 for (auto state = states.begin(); state != states.end();) {
86 if (state->state.hasBufferChanges() && state->state.hasValidBuffer() &&
87 state->state.surface) {
Chavi Weingartenc8bcb682023-03-06 22:20:42 +000088 int result = visitor(state->state, state->externalTexture);
liulijuneb489f62022-10-17 22:02:14 +080089 if (result == STOP_TRAVERSAL) return;
90 if (result == DELETE_AND_CONTINUE_TRAVERSAL) {
91 state = states.erase(state);
92 continue;
93 }
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070094 }
liulijuneb489f62022-10-17 22:02:14 +080095 state++;
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070096 }
97 }
98
Dominik Laskowski1f6fc702022-03-21 08:34:50 -070099 // TODO(b/185535769): Remove FrameHint. Instead, reset the idle timer (of the relevant physical
100 // display) on the main thread if commit leads to composite. Then, RefreshRateOverlay should be
101 // able to setFrameRate once, rather than for each transaction.
102 bool isFrameActive() const {
103 if (!displays.empty()) return true;
104
Vishnu Nair40fff5c2022-11-04 02:46:28 +0000105 for (const auto& state : states) {
Ady Abrahamceb72e82023-01-17 17:40:21 -0800106 const bool frameRateChanged = state.state.what & layer_state_t::eFrameRateChanged;
107 if (!frameRateChanged ||
108 state.state.frameRateCompatibility != ANATIVEWINDOW_FRAME_RATE_NO_VOTE) {
Dominik Laskowski1f6fc702022-03-21 08:34:50 -0700109 return true;
110 }
111 }
112
113 return false;
114 }
Vishnu Nair6b591152021-10-08 11:45:14 -0700115
116 FrameTimelineInfo frameTimelineInfo;
Vishnu Nair40fff5c2022-11-04 02:46:28 +0000117 std::vector<ResolvedComposerState> states;
Vishnu Nair6b591152021-10-08 11:45:14 -0700118 Vector<DisplayState> displays;
119 uint32_t flags;
120 sp<IBinder> applyToken;
121 InputWindowCommands inputWindowCommands;
122 int64_t desiredPresentTime;
123 bool isAutoTimestamp;
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000124 std::vector<uint64_t> uncacheBufferIds;
Vishnu Nair6b591152021-10-08 11:45:14 -0700125 int64_t postTime;
126 uint32_t permissions;
127 bool hasListenerCallbacks;
128 std::vector<ListenerCallbacks> listenerCallbacks;
129 int originPid;
130 int originUid;
131 uint64_t id;
Robert Carr4c1b6462021-12-21 10:30:50 -0800132 bool sentFenceTimeoutWarning = false;
Vishnu Nair6b591152021-10-08 11:45:14 -0700133};
134
Vishnu Nair6b591152021-10-08 11:45:14 -0700135} // namespace android