1 /*
2  * Copyright 2021 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 
17 #pragma once
18 
19 #include <condition_variable>
20 #include <memory>
21 #include <mutex>
22 #include <vector>
23 #include "FrontEnd/LayerCreationArgs.h"
24 #include "renderengine/ExternalTexture.h"
25 
26 #include <common/FlagManager.h>
27 #include <gui/LayerState.h>
28 #include <system/window.h>
29 
30 namespace android {
31 
32 enum TraverseBuffersReturnValues {
33     CONTINUE_TRAVERSAL,
34     STOP_TRAVERSAL,
35     DELETE_AND_CONTINUE_TRAVERSAL,
36 };
37 
38 // Extends the client side composer state by resolving buffer.
39 class ResolvedComposerState : public ComposerState {
40 public:
41     ResolvedComposerState() = default;
ResolvedComposerState(ComposerState && source)42     ResolvedComposerState(ComposerState&& source) { state = std::move(source.state); }
43     std::shared_ptr<renderengine::ExternalTexture> externalTexture;
44     uint32_t layerId = UNASSIGNED_LAYER_ID;
45     uint32_t parentId = UNASSIGNED_LAYER_ID;
46     uint32_t relativeParentId = UNASSIGNED_LAYER_ID;
47     uint32_t touchCropId = UNASSIGNED_LAYER_ID;
48 };
49 
50 struct TransactionState {
51     TransactionState() = default;
52 
TransactionStateTransactionState53     TransactionState(const FrameTimelineInfo& frameTimelineInfo,
54                      std::vector<ResolvedComposerState>& composerStates,
55                      const Vector<DisplayState>& displayStates, uint32_t transactionFlags,
56                      const sp<IBinder>& applyToken, const InputWindowCommands& inputWindowCommands,
57                      int64_t desiredPresentTime, bool isAutoTimestamp,
58                      std::vector<uint64_t> uncacheBufferIds, int64_t postTime,
59                      bool hasListenerCallbacks, std::vector<ListenerCallbacks> listenerCallbacks,
60                      int originPid, int originUid, uint64_t transactionId,
61                      std::vector<uint64_t> mergedTransactionIds)
62           : frameTimelineInfo(frameTimelineInfo),
63             states(std::move(composerStates)),
64             displays(displayStates),
65             flags(transactionFlags),
66             applyToken(applyToken),
67             inputWindowCommands(inputWindowCommands),
68             desiredPresentTime(desiredPresentTime),
69             isAutoTimestamp(isAutoTimestamp),
70             uncacheBufferIds(std::move(uncacheBufferIds)),
71             postTime(postTime),
72             hasListenerCallbacks(hasListenerCallbacks),
73             listenerCallbacks(listenerCallbacks),
74             originPid(originPid),
75             originUid(originUid),
76             id(transactionId),
77             mergedTransactionIds(std::move(mergedTransactionIds)) {}
78 
79     // Invokes `void(const layer_state_t&)` visitor for matching layers.
80     template <typename Visitor>
traverseStatesWithBuffersTransactionState81     void traverseStatesWithBuffers(Visitor&& visitor) const {
82         for (const auto& state : states) {
83             if (state.state.hasBufferChanges() && state.externalTexture && state.state.surface) {
84                 visitor(state.state);
85             }
86         }
87     }
88 
89     template <typename Visitor>
traverseStatesWithBuffersWhileTrueTransactionState90     void traverseStatesWithBuffersWhileTrue(Visitor&& visitor) NO_THREAD_SAFETY_ANALYSIS {
91         for (auto state = states.begin(); state != states.end();) {
92             if (state->state.hasBufferChanges() && state->externalTexture && state->state.surface) {
93                 int result = visitor(*state);
94                 if (result == STOP_TRAVERSAL) return;
95                 if (result == DELETE_AND_CONTINUE_TRAVERSAL) {
96                     state = states.erase(state);
97                     continue;
98                 }
99             }
100             state++;
101         }
102     }
103 
104     // TODO(b/185535769): Remove FrameHint. Instead, reset the idle timer (of the relevant physical
105     // display) on the main thread if commit leads to composite. Then, RefreshRateOverlay should be
106     // able to setFrameRate once, rather than for each transaction.
isFrameActiveTransactionState107     bool isFrameActive() const {
108         if (!displays.empty()) return true;
109 
110         for (const auto& state : states) {
111             const bool frameRateChanged = state.state.what & layer_state_t::eFrameRateChanged;
112             if (FlagManager::getInstance().vrr_bugfix_24q4()) {
113                 const bool frameRateIsNoVote = frameRateChanged &&
114                         state.state.frameRateCompatibility == ANATIVEWINDOW_FRAME_RATE_NO_VOTE;
115                 const bool frameRateCategoryChanged =
116                         state.state.what & layer_state_t::eFrameRateCategoryChanged;
117                 const bool frameRateCategoryIsNoPreference = frameRateCategoryChanged &&
118                         state.state.frameRateCategory ==
119                                 ANATIVEWINDOW_FRAME_RATE_CATEGORY_NO_PREFERENCE;
120                 if (!frameRateIsNoVote && !frameRateCategoryIsNoPreference) {
121                     return true;
122                 }
123             } else {
124                 if (!frameRateChanged ||
125                     state.state.frameRateCompatibility != ANATIVEWINDOW_FRAME_RATE_NO_VOTE) {
126                     return true;
127                 }
128             }
129         }
130 
131         return false;
132     }
133 
134     FrameTimelineInfo frameTimelineInfo;
135     std::vector<ResolvedComposerState> states;
136     Vector<DisplayState> displays;
137     uint32_t flags;
138     sp<IBinder> applyToken;
139     InputWindowCommands inputWindowCommands;
140     int64_t desiredPresentTime;
141     bool isAutoTimestamp;
142     std::vector<uint64_t> uncacheBufferIds;
143     int64_t postTime;
144     bool hasListenerCallbacks;
145     std::vector<ListenerCallbacks> listenerCallbacks;
146     int originPid;
147     int originUid;
148     uint64_t id;
149     bool sentFenceTimeoutWarning = false;
150     std::vector<uint64_t> mergedTransactionIds;
151 };
152 
153 } // namespace android
154