xref: /aosp_15_r20/frameworks/native/services/surfaceflinger/TransactionState.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2021 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #pragma once
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include <condition_variable>
20*38e8c45fSAndroid Build Coastguard Worker #include <memory>
21*38e8c45fSAndroid Build Coastguard Worker #include <mutex>
22*38e8c45fSAndroid Build Coastguard Worker #include <vector>
23*38e8c45fSAndroid Build Coastguard Worker #include "FrontEnd/LayerCreationArgs.h"
24*38e8c45fSAndroid Build Coastguard Worker #include "renderengine/ExternalTexture.h"
25*38e8c45fSAndroid Build Coastguard Worker 
26*38e8c45fSAndroid Build Coastguard Worker #include <common/FlagManager.h>
27*38e8c45fSAndroid Build Coastguard Worker #include <gui/LayerState.h>
28*38e8c45fSAndroid Build Coastguard Worker #include <system/window.h>
29*38e8c45fSAndroid Build Coastguard Worker 
30*38e8c45fSAndroid Build Coastguard Worker namespace android {
31*38e8c45fSAndroid Build Coastguard Worker 
32*38e8c45fSAndroid Build Coastguard Worker enum TraverseBuffersReturnValues {
33*38e8c45fSAndroid Build Coastguard Worker     CONTINUE_TRAVERSAL,
34*38e8c45fSAndroid Build Coastguard Worker     STOP_TRAVERSAL,
35*38e8c45fSAndroid Build Coastguard Worker     DELETE_AND_CONTINUE_TRAVERSAL,
36*38e8c45fSAndroid Build Coastguard Worker };
37*38e8c45fSAndroid Build Coastguard Worker 
38*38e8c45fSAndroid Build Coastguard Worker // Extends the client side composer state by resolving buffer.
39*38e8c45fSAndroid Build Coastguard Worker class ResolvedComposerState : public ComposerState {
40*38e8c45fSAndroid Build Coastguard Worker public:
41*38e8c45fSAndroid Build Coastguard Worker     ResolvedComposerState() = default;
ResolvedComposerState(ComposerState && source)42*38e8c45fSAndroid Build Coastguard Worker     ResolvedComposerState(ComposerState&& source) { state = std::move(source.state); }
43*38e8c45fSAndroid Build Coastguard Worker     std::shared_ptr<renderengine::ExternalTexture> externalTexture;
44*38e8c45fSAndroid Build Coastguard Worker     uint32_t layerId = UNASSIGNED_LAYER_ID;
45*38e8c45fSAndroid Build Coastguard Worker     uint32_t parentId = UNASSIGNED_LAYER_ID;
46*38e8c45fSAndroid Build Coastguard Worker     uint32_t relativeParentId = UNASSIGNED_LAYER_ID;
47*38e8c45fSAndroid Build Coastguard Worker     uint32_t touchCropId = UNASSIGNED_LAYER_ID;
48*38e8c45fSAndroid Build Coastguard Worker };
49*38e8c45fSAndroid Build Coastguard Worker 
50*38e8c45fSAndroid Build Coastguard Worker struct TransactionState {
51*38e8c45fSAndroid Build Coastguard Worker     TransactionState() = default;
52*38e8c45fSAndroid Build Coastguard Worker 
TransactionStateTransactionState53*38e8c45fSAndroid Build Coastguard Worker     TransactionState(const FrameTimelineInfo& frameTimelineInfo,
54*38e8c45fSAndroid Build Coastguard Worker                      std::vector<ResolvedComposerState>& composerStates,
55*38e8c45fSAndroid Build Coastguard Worker                      const Vector<DisplayState>& displayStates, uint32_t transactionFlags,
56*38e8c45fSAndroid Build Coastguard Worker                      const sp<IBinder>& applyToken, const InputWindowCommands& inputWindowCommands,
57*38e8c45fSAndroid Build Coastguard Worker                      int64_t desiredPresentTime, bool isAutoTimestamp,
58*38e8c45fSAndroid Build Coastguard Worker                      std::vector<uint64_t> uncacheBufferIds, int64_t postTime,
59*38e8c45fSAndroid Build Coastguard Worker                      bool hasListenerCallbacks, std::vector<ListenerCallbacks> listenerCallbacks,
60*38e8c45fSAndroid Build Coastguard Worker                      int originPid, int originUid, uint64_t transactionId,
61*38e8c45fSAndroid Build Coastguard Worker                      std::vector<uint64_t> mergedTransactionIds)
62*38e8c45fSAndroid Build Coastguard Worker           : frameTimelineInfo(frameTimelineInfo),
63*38e8c45fSAndroid Build Coastguard Worker             states(std::move(composerStates)),
64*38e8c45fSAndroid Build Coastguard Worker             displays(displayStates),
65*38e8c45fSAndroid Build Coastguard Worker             flags(transactionFlags),
66*38e8c45fSAndroid Build Coastguard Worker             applyToken(applyToken),
67*38e8c45fSAndroid Build Coastguard Worker             inputWindowCommands(inputWindowCommands),
68*38e8c45fSAndroid Build Coastguard Worker             desiredPresentTime(desiredPresentTime),
69*38e8c45fSAndroid Build Coastguard Worker             isAutoTimestamp(isAutoTimestamp),
70*38e8c45fSAndroid Build Coastguard Worker             uncacheBufferIds(std::move(uncacheBufferIds)),
71*38e8c45fSAndroid Build Coastguard Worker             postTime(postTime),
72*38e8c45fSAndroid Build Coastguard Worker             hasListenerCallbacks(hasListenerCallbacks),
73*38e8c45fSAndroid Build Coastguard Worker             listenerCallbacks(listenerCallbacks),
74*38e8c45fSAndroid Build Coastguard Worker             originPid(originPid),
75*38e8c45fSAndroid Build Coastguard Worker             originUid(originUid),
76*38e8c45fSAndroid Build Coastguard Worker             id(transactionId),
77*38e8c45fSAndroid Build Coastguard Worker             mergedTransactionIds(std::move(mergedTransactionIds)) {}
78*38e8c45fSAndroid Build Coastguard Worker 
79*38e8c45fSAndroid Build Coastguard Worker     // Invokes `void(const layer_state_t&)` visitor for matching layers.
80*38e8c45fSAndroid Build Coastguard Worker     template <typename Visitor>
traverseStatesWithBuffersTransactionState81*38e8c45fSAndroid Build Coastguard Worker     void traverseStatesWithBuffers(Visitor&& visitor) const {
82*38e8c45fSAndroid Build Coastguard Worker         for (const auto& state : states) {
83*38e8c45fSAndroid Build Coastguard Worker             if (state.state.hasBufferChanges() && state.externalTexture && state.state.surface) {
84*38e8c45fSAndroid Build Coastguard Worker                 visitor(state.state);
85*38e8c45fSAndroid Build Coastguard Worker             }
86*38e8c45fSAndroid Build Coastguard Worker         }
87*38e8c45fSAndroid Build Coastguard Worker     }
88*38e8c45fSAndroid Build Coastguard Worker 
89*38e8c45fSAndroid Build Coastguard Worker     template <typename Visitor>
traverseStatesWithBuffersWhileTrueTransactionState90*38e8c45fSAndroid Build Coastguard Worker     void traverseStatesWithBuffersWhileTrue(Visitor&& visitor) NO_THREAD_SAFETY_ANALYSIS {
91*38e8c45fSAndroid Build Coastguard Worker         for (auto state = states.begin(); state != states.end();) {
92*38e8c45fSAndroid Build Coastguard Worker             if (state->state.hasBufferChanges() && state->externalTexture && state->state.surface) {
93*38e8c45fSAndroid Build Coastguard Worker                 int result = visitor(*state);
94*38e8c45fSAndroid Build Coastguard Worker                 if (result == STOP_TRAVERSAL) return;
95*38e8c45fSAndroid Build Coastguard Worker                 if (result == DELETE_AND_CONTINUE_TRAVERSAL) {
96*38e8c45fSAndroid Build Coastguard Worker                     state = states.erase(state);
97*38e8c45fSAndroid Build Coastguard Worker                     continue;
98*38e8c45fSAndroid Build Coastguard Worker                 }
99*38e8c45fSAndroid Build Coastguard Worker             }
100*38e8c45fSAndroid Build Coastguard Worker             state++;
101*38e8c45fSAndroid Build Coastguard Worker         }
102*38e8c45fSAndroid Build Coastguard Worker     }
103*38e8c45fSAndroid Build Coastguard Worker 
104*38e8c45fSAndroid Build Coastguard Worker     // TODO(b/185535769): Remove FrameHint. Instead, reset the idle timer (of the relevant physical
105*38e8c45fSAndroid Build Coastguard Worker     // display) on the main thread if commit leads to composite. Then, RefreshRateOverlay should be
106*38e8c45fSAndroid Build Coastguard Worker     // able to setFrameRate once, rather than for each transaction.
isFrameActiveTransactionState107*38e8c45fSAndroid Build Coastguard Worker     bool isFrameActive() const {
108*38e8c45fSAndroid Build Coastguard Worker         if (!displays.empty()) return true;
109*38e8c45fSAndroid Build Coastguard Worker 
110*38e8c45fSAndroid Build Coastguard Worker         for (const auto& state : states) {
111*38e8c45fSAndroid Build Coastguard Worker             const bool frameRateChanged = state.state.what & layer_state_t::eFrameRateChanged;
112*38e8c45fSAndroid Build Coastguard Worker             if (FlagManager::getInstance().vrr_bugfix_24q4()) {
113*38e8c45fSAndroid Build Coastguard Worker                 const bool frameRateIsNoVote = frameRateChanged &&
114*38e8c45fSAndroid Build Coastguard Worker                         state.state.frameRateCompatibility == ANATIVEWINDOW_FRAME_RATE_NO_VOTE;
115*38e8c45fSAndroid Build Coastguard Worker                 const bool frameRateCategoryChanged =
116*38e8c45fSAndroid Build Coastguard Worker                         state.state.what & layer_state_t::eFrameRateCategoryChanged;
117*38e8c45fSAndroid Build Coastguard Worker                 const bool frameRateCategoryIsNoPreference = frameRateCategoryChanged &&
118*38e8c45fSAndroid Build Coastguard Worker                         state.state.frameRateCategory ==
119*38e8c45fSAndroid Build Coastguard Worker                                 ANATIVEWINDOW_FRAME_RATE_CATEGORY_NO_PREFERENCE;
120*38e8c45fSAndroid Build Coastguard Worker                 if (!frameRateIsNoVote && !frameRateCategoryIsNoPreference) {
121*38e8c45fSAndroid Build Coastguard Worker                     return true;
122*38e8c45fSAndroid Build Coastguard Worker                 }
123*38e8c45fSAndroid Build Coastguard Worker             } else {
124*38e8c45fSAndroid Build Coastguard Worker                 if (!frameRateChanged ||
125*38e8c45fSAndroid Build Coastguard Worker                     state.state.frameRateCompatibility != ANATIVEWINDOW_FRAME_RATE_NO_VOTE) {
126*38e8c45fSAndroid Build Coastguard Worker                     return true;
127*38e8c45fSAndroid Build Coastguard Worker                 }
128*38e8c45fSAndroid Build Coastguard Worker             }
129*38e8c45fSAndroid Build Coastguard Worker         }
130*38e8c45fSAndroid Build Coastguard Worker 
131*38e8c45fSAndroid Build Coastguard Worker         return false;
132*38e8c45fSAndroid Build Coastguard Worker     }
133*38e8c45fSAndroid Build Coastguard Worker 
134*38e8c45fSAndroid Build Coastguard Worker     FrameTimelineInfo frameTimelineInfo;
135*38e8c45fSAndroid Build Coastguard Worker     std::vector<ResolvedComposerState> states;
136*38e8c45fSAndroid Build Coastguard Worker     Vector<DisplayState> displays;
137*38e8c45fSAndroid Build Coastguard Worker     uint32_t flags;
138*38e8c45fSAndroid Build Coastguard Worker     sp<IBinder> applyToken;
139*38e8c45fSAndroid Build Coastguard Worker     InputWindowCommands inputWindowCommands;
140*38e8c45fSAndroid Build Coastguard Worker     int64_t desiredPresentTime;
141*38e8c45fSAndroid Build Coastguard Worker     bool isAutoTimestamp;
142*38e8c45fSAndroid Build Coastguard Worker     std::vector<uint64_t> uncacheBufferIds;
143*38e8c45fSAndroid Build Coastguard Worker     int64_t postTime;
144*38e8c45fSAndroid Build Coastguard Worker     bool hasListenerCallbacks;
145*38e8c45fSAndroid Build Coastguard Worker     std::vector<ListenerCallbacks> listenerCallbacks;
146*38e8c45fSAndroid Build Coastguard Worker     int originPid;
147*38e8c45fSAndroid Build Coastguard Worker     int originUid;
148*38e8c45fSAndroid Build Coastguard Worker     uint64_t id;
149*38e8c45fSAndroid Build Coastguard Worker     bool sentFenceTimeoutWarning = false;
150*38e8c45fSAndroid Build Coastguard Worker     std::vector<uint64_t> mergedTransactionIds;
151*38e8c45fSAndroid Build Coastguard Worker };
152*38e8c45fSAndroid Build Coastguard Worker 
153*38e8c45fSAndroid Build Coastguard Worker } // namespace android
154