| /* | |
| * Copyright (C) 2026 The Android Open Source Project | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| #pragma once | |
| #include <android-base/thread_annotations.h> | |
| #include <scheduler/interface/ICompositor.h> | |
| #include <functional> | |
| #include <unordered_map> | |
| #include <vector> | |
| #include "Common.h" | |
| #include "ThreadContext.h" | |
| namespace android { | |
| class Layer; | |
| } | |
| namespace android::adpf { | |
| class SessionLayerMap; | |
| // Wraps CompositionData to make it easier to work with | |
| class CompositionDataWrapper { | |
| public: | |
| // getParentIdFn is used for testing and dependency injection to look up parent layer IDs | |
| // without coupling to the SurfaceFlinger layer hierarchy. | |
| CompositionDataWrapper(hal::CompositionData& data, SessionLayerMap& map, | |
| std::function<int32_t(int32_t)> getParentIdFn = nullptr) | |
| : mOutput(data), mSessionLayerMap(map), mGetParentIdFn(std::move(getParentIdFn)) {} | |
| // Provides targets for when different displays are due to present | |
| void passFrameTargets(const scheduler::FrameTargets& targets); | |
| // Sets the latch time for this composition | |
| void setLatchTime(const nsecs_t latchTime) { mOutput.latchTimestampNanos = latchTime; } | |
| // Directly add an update object for a given producer to the output data | |
| void insertFrameProducer(hal::FrameProducer&& producer, const int32_t layerId); | |
| // Returns the raw underlying data | |
| hal::CompositionData& getCompositionData() const { return mOutput; } | |
| // Sets the timestamp for this message | |
| void setTimestamp(const nsecs_t submissionTime) { mOutput.timestampNanos = submissionTime; } | |
| // Returns whether a given layer is currently associated with an active session. | |
| // Directly querying SessionLayerMap on the main thread during composition avoids lock | |
| // contention with binder threads because layer map updates are synchronized during main thread | |
| // latch. | |
| bool isLayerRelevant(const int32_t layerId) const REQUIRES(android::kMainThreadContext); | |
| // Converts producer IDs to layer IDs. Producer IDs are an opaque type used to uniquely | |
| // represent frame producers | |
| static int64_t getProducerId(const int32_t layerId); | |
| // Converts layer IDs to producer IDs | |
| static int32_t getLayerId(const int64_t producerId); | |
| // Sets the sessions associated with the layer | |
| void setLayerSessions(const int32_t layerId, std::vector<int32_t>&& sessions); | |
| // Informs the HAL of a dead layer | |
| void setDeadLayer(const int32_t layerId); | |
| // Set if we should try to force an update, eg: if a frame likely janks | |
| void setIsForcedUpdate(bool forced) { mForceUpdate = forced; } | |
| // Set if the FPS of a tracked layer has changed this frame | |
| void setFpsChanged(bool changed) { mFpsChanged = changed; } | |
| // If there is something exceptional about the update to warrant bypassing the rate limiter | |
| bool isForcedUpdate() const { return mForceUpdate; } | |
| // True if the FPS changed, which also forces an update | |
| bool hasFpsChanged() const { return mFpsChanged; } | |
| // True if this contains lifecycle updates, which will always force it to send | |
| bool hasLifecycleUpdates() const { return !mLayerIdToUpdateIndexMap.empty(); } | |
| private: | |
| // Gets or creates an object ref for a layer. This ref is ephemeral and only valid until | |
| // "getOrCreateLayerUpdate" is called again | |
| hal::FrameProducerUpdate& getOrCreateLayerUpdate(const int32_t layerId); | |
| hal::CompositionData& mOutput; | |
| // Maps the layerIDs of all layers with updates to the index of the update in producerUpdate | |
| std::unordered_map<int32_t, size_t> mLayerIdToUpdateIndexMap{}; | |
| // Used to keep track of which layers are relevant | |
| SessionLayerMap& mSessionLayerMap; | |
| std::function<int32_t(int32_t)> mGetParentIdFn = nullptr; | |
| // Whether the update is important enough to trigger the rate limiter to send a batch early | |
| bool mForceUpdate = false; | |
| // Whether the update contains an FPS change | |
| bool mFpsChanged = false; | |
| }; | |
| } // namespace android::adpf |