• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gui/LayerState.h>
27 #include <system/window.h>
28 
29 namespace android {
30 
31 enum TraverseBuffersReturnValues {
32     CONTINUE_TRAVERSAL,
33     STOP_TRAVERSAL,
34     DELETE_AND_CONTINUE_TRAVERSAL,
35 };
36 
37 // Extends the client side composer state by resolving buffer.
38 class ResolvedComposerState : public ComposerState {
39 public:
40     ResolvedComposerState() = default;
ResolvedComposerState(ComposerState && source)41     ResolvedComposerState(ComposerState&& source) { state = std::move(source.state); }
42     std::shared_ptr<renderengine::ExternalTexture> externalTexture;
43     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;
47 };
48 
49 struct TransactionState {
50     TransactionState() = default;
51 
TransactionStateTransactionState52     TransactionState(const FrameTimelineInfo& frameTimelineInfo,
53                      std::vector<ResolvedComposerState>& composerStates,
54                      const Vector<DisplayState>& displayStates, uint32_t transactionFlags,
55                      const sp<IBinder>& applyToken, const InputWindowCommands& inputWindowCommands,
56                      int64_t desiredPresentTime, bool isAutoTimestamp,
57                      std::vector<uint64_t> uncacheBufferIds, int64_t postTime,
58                      bool hasListenerCallbacks, std::vector<ListenerCallbacks> listenerCallbacks,
59                      int originPid, int originUid, uint64_t transactionId,
60                      std::vector<uint64_t> mergedTransactionIds)
61           : frameTimelineInfo(frameTimelineInfo),
62             states(std::move(composerStates)),
63             displays(displayStates),
64             flags(transactionFlags),
65             applyToken(applyToken),
66             inputWindowCommands(inputWindowCommands),
67             desiredPresentTime(desiredPresentTime),
68             isAutoTimestamp(isAutoTimestamp),
69             uncacheBufferIds(std::move(uncacheBufferIds)),
70             postTime(postTime),
71             hasListenerCallbacks(hasListenerCallbacks),
72             listenerCallbacks(listenerCallbacks),
73             originPid(originPid),
74             originUid(originUid),
75             id(transactionId),
76             mergedTransactionIds(std::move(mergedTransactionIds)) {}
77 
78     // Invokes `void(const layer_state_t&)` visitor for matching layers.
79     template <typename Visitor>
traverseStatesWithBuffersTransactionState80     void traverseStatesWithBuffers(Visitor&& visitor) const {
81         for (const auto& state : states) {
82             if (state.state.hasBufferChanges() && state.externalTexture && state.state.surface) {
83                 visitor(state.state);
84             }
85         }
86     }
87 
88     template <typename Visitor>
traverseStatesWithBuffersWhileTrueTransactionState89     void traverseStatesWithBuffersWhileTrue(Visitor&& visitor) {
90         for (auto state = states.begin(); state != states.end();) {
91             if (state->state.hasBufferChanges() && state->externalTexture && state->state.surface) {
92                 int result = visitor(state->state, state->externalTexture);
93                 if (result == STOP_TRAVERSAL) return;
94                 if (result == DELETE_AND_CONTINUE_TRAVERSAL) {
95                     state = states.erase(state);
96                     continue;
97                 }
98             }
99             state++;
100         }
101     }
102 
103     // TODO(b/185535769): Remove FrameHint. Instead, reset the idle timer (of the relevant physical
104     // display) on the main thread if commit leads to composite. Then, RefreshRateOverlay should be
105     // able to setFrameRate once, rather than for each transaction.
isFrameActiveTransactionState106     bool isFrameActive() const {
107         if (!displays.empty()) return true;
108 
109         for (const auto& state : states) {
110             const bool frameRateChanged = state.state.what & layer_state_t::eFrameRateChanged;
111             if (!frameRateChanged ||
112                 state.state.frameRateCompatibility != ANATIVEWINDOW_FRAME_RATE_NO_VOTE) {
113                 return true;
114             }
115         }
116 
117         return false;
118     }
119 
120     FrameTimelineInfo frameTimelineInfo;
121     std::vector<ResolvedComposerState> states;
122     Vector<DisplayState> displays;
123     uint32_t flags;
124     sp<IBinder> applyToken;
125     InputWindowCommands inputWindowCommands;
126     int64_t desiredPresentTime;
127     bool isAutoTimestamp;
128     std::vector<uint64_t> uncacheBufferIds;
129     int64_t postTime;
130     bool hasListenerCallbacks;
131     std::vector<ListenerCallbacks> listenerCallbacks;
132     int originPid;
133     int originUid;
134     uint64_t id;
135     bool sentFenceTimeoutWarning = false;
136     std::vector<uint64_t> mergedTransactionIds;
137 };
138 
139 } // namespace android
140