blob: 35c8b6c6477ac743ec88f112c9d65f392c14f566 [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 Nair1391de22023-03-05 19:56:14 -080023#include "FrontEnd/LayerCreationArgs.h"
Vishnu Nair40fff5c2022-11-04 02:46:28 +000024#include "renderengine/ExternalTexture.h"
Dominik Laskowski1f6fc702022-03-21 08:34:50 -070025
Vishnu Nair6b591152021-10-08 11:45:14 -070026#include <gui/LayerState.h>
Dominik Laskowski1f6fc702022-03-21 08:34:50 -070027#include <system/window.h>
Vishnu Nair6b591152021-10-08 11:45:14 -070028
29namespace android {
Dominik Laskowski1f6fc702022-03-21 08:34:50 -070030
liulijuneb489f62022-10-17 22:02:14 +080031enum TraverseBuffersReturnValues {
32 CONTINUE_TRAVERSAL,
33 STOP_TRAVERSAL,
34 DELETE_AND_CONTINUE_TRAVERSAL,
35};
36
Brian Lindahl439afad2022-11-14 11:16:55 -070037// Extends the client side composer state by resolving buffer.
Vishnu Nair40fff5c2022-11-04 02:46:28 +000038class ResolvedComposerState : public ComposerState {
39public:
40 ResolvedComposerState() = default;
41 ResolvedComposerState(ComposerState&& source) { state = std::move(source.state); }
42 std::shared_ptr<renderengine::ExternalTexture> externalTexture;
Vishnu Nair1391de22023-03-05 19:56:14 -080043 uint32_t layerId = UNASSIGNED_LAYER_ID;
44 uint32_t parentId = UNASSIGNED_LAYER_ID;
45 uint32_t relativeParentId = UNASSIGNED_LAYER_ID;
46 uint32_t touchCropId = UNASSIGNED_LAYER_ID;
Vishnu Nair40fff5c2022-11-04 02:46:28 +000047};
48
Vishnu Nair6b591152021-10-08 11:45:14 -070049struct TransactionState {
Dominik Laskowski1f6fc702022-03-21 08:34:50 -070050 TransactionState() = default;
51
Vishnu Nair6b591152021-10-08 11:45:14 -070052 TransactionState(const FrameTimelineInfo& frameTimelineInfo,
Vishnu Nair40fff5c2022-11-04 02:46:28 +000053 std::vector<ResolvedComposerState>& composerStates,
Vishnu Nair6b591152021-10-08 11:45:14 -070054 const Vector<DisplayState>& displayStates, uint32_t transactionFlags,
55 const sp<IBinder>& applyToken, const InputWindowCommands& inputWindowCommands,
56 int64_t desiredPresentTime, bool isAutoTimestamp,
Patrick Williams6c6dd3b2023-02-13 22:53:06 +000057 std::vector<uint64_t> uncacheBufferIds, int64_t postTime, uint32_t permissions,
Vishnu Nair6b591152021-10-08 11:45:14 -070058 bool hasListenerCallbacks, std::vector<ListenerCallbacks> listenerCallbacks,
59 int originPid, int originUid, uint64_t transactionId)
60 : frameTimelineInfo(frameTimelineInfo),
Vishnu Nair40fff5c2022-11-04 02:46:28 +000061 states(std::move(composerStates)),
Vishnu Nair6b591152021-10-08 11:45:14 -070062 displays(displayStates),
63 flags(transactionFlags),
64 applyToken(applyToken),
65 inputWindowCommands(inputWindowCommands),
66 desiredPresentTime(desiredPresentTime),
67 isAutoTimestamp(isAutoTimestamp),
Patrick Williams6c6dd3b2023-02-13 22:53:06 +000068 uncacheBufferIds(std::move(uncacheBufferIds)),
Vishnu Nair6b591152021-10-08 11:45:14 -070069 postTime(postTime),
70 permissions(permissions),
71 hasListenerCallbacks(hasListenerCallbacks),
72 listenerCallbacks(listenerCallbacks),
73 originPid(originPid),
74 originUid(originUid),
75 id(transactionId) {}
76
Dominik Laskowski1f6fc702022-03-21 08:34:50 -070077 // Invokes `void(const layer_state_t&)` visitor for matching layers.
78 template <typename Visitor>
79 void traverseStatesWithBuffers(Visitor&& visitor) const {
Vishnu Nair40fff5c2022-11-04 02:46:28 +000080 for (const auto& state : states) {
Vishnu Nair7ee4f462023-04-19 09:54:09 -070081 if (state.state.hasBufferChanges() && state.externalTexture && state.state.surface) {
Vishnu Nair40fff5c2022-11-04 02:46:28 +000082 visitor(state.state);
Dominik Laskowski1f6fc702022-03-21 08:34:50 -070083 }
84 }
85 }
Vishnu Nair6b591152021-10-08 11:45:14 -070086
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070087 template <typename Visitor>
liulijuneb489f62022-10-17 22:02:14 +080088 void traverseStatesWithBuffersWhileTrue(Visitor&& visitor) {
89 for (auto state = states.begin(); state != states.end();) {
Vishnu Nair7ee4f462023-04-19 09:54:09 -070090 if (state->state.hasBufferChanges() && state->externalTexture && state->state.surface) {
Chavi Weingartenc8bcb682023-03-06 22:20:42 +000091 int result = visitor(state->state, state->externalTexture);
liulijuneb489f62022-10-17 22:02:14 +080092 if (result == STOP_TRAVERSAL) return;
93 if (result == DELETE_AND_CONTINUE_TRAVERSAL) {
94 state = states.erase(state);
95 continue;
96 }
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070097 }
liulijuneb489f62022-10-17 22:02:14 +080098 state++;
Vishnu Nair59f6d2d2022-10-05 16:59:56 -070099 }
100 }
101
Dominik Laskowski1f6fc702022-03-21 08:34:50 -0700102 // TODO(b/185535769): Remove FrameHint. Instead, reset the idle timer (of the relevant physical
103 // display) on the main thread if commit leads to composite. Then, RefreshRateOverlay should be
104 // able to setFrameRate once, rather than for each transaction.
105 bool isFrameActive() const {
106 if (!displays.empty()) return true;
107
Vishnu Nair40fff5c2022-11-04 02:46:28 +0000108 for (const auto& state : states) {
Ady Abrahamceb72e82023-01-17 17:40:21 -0800109 const bool frameRateChanged = state.state.what & layer_state_t::eFrameRateChanged;
110 if (!frameRateChanged ||
111 state.state.frameRateCompatibility != ANATIVEWINDOW_FRAME_RATE_NO_VOTE) {
Dominik Laskowski1f6fc702022-03-21 08:34:50 -0700112 return true;
113 }
114 }
115
116 return false;
117 }
Vishnu Nair6b591152021-10-08 11:45:14 -0700118
119 FrameTimelineInfo frameTimelineInfo;
Vishnu Nair40fff5c2022-11-04 02:46:28 +0000120 std::vector<ResolvedComposerState> states;
Vishnu Nair6b591152021-10-08 11:45:14 -0700121 Vector<DisplayState> displays;
122 uint32_t flags;
123 sp<IBinder> applyToken;
124 InputWindowCommands inputWindowCommands;
125 int64_t desiredPresentTime;
126 bool isAutoTimestamp;
Patrick Williams6c6dd3b2023-02-13 22:53:06 +0000127 std::vector<uint64_t> uncacheBufferIds;
Vishnu Nair6b591152021-10-08 11:45:14 -0700128 int64_t postTime;
129 uint32_t permissions;
130 bool hasListenerCallbacks;
131 std::vector<ListenerCallbacks> listenerCallbacks;
132 int originPid;
133 int originUid;
134 uint64_t id;
Robert Carr4c1b6462021-12-21 10:30:50 -0800135 bool sentFenceTimeoutWarning = false;
Vishnu Nair6b591152021-10-08 11:45:14 -0700136};
137
Vishnu Nair6b591152021-10-08 11:45:14 -0700138} // namespace android