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