blob: d17ccc17d4dea55e97ddb7d0f134f010981561a5 [file]
/*
* 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.
*/
#include "CompositionDataWrapper.h"
#include <scheduler/FrameTargeter.h>
#include <ui/DisplayId.h>
#include "SessionLayerMap.h"
namespace android::adpf {
void CompositionDataWrapper::passFrameTargets(const scheduler::FrameTargets& targets) {
mOutput.outputIds.reserve(mOutput.outputIds.size() + targets.size());
mOutput.scheduledPresentTimestampsNanos.reserve(mOutput.scheduledPresentTimestampsNanos.size() +
targets.size());
for (const auto& target : targets) {
if (target.second != nullptr) {
mOutput.outputIds.push_back(static_cast<int64_t>(asDisplayId(target.first).value));
mOutput.scheduledPresentTimestampsNanos.push_back(
target.second->expectedPresentTime().ns());
}
}
}
int64_t CompositionDataWrapper::getProducerId(const int32_t layerId) {
// Shift 1 into the higher 32 bits to namespace SurfaceFlinger-owned layer IDs,
// preventing collisions with standard application/window-level producer IDs.
return (int64_t(1) << 32) | static_cast<uint32_t>(layerId);
}
int32_t CompositionDataWrapper::getLayerId(const int64_t producerId) {
return static_cast<int32_t>(producerId);
}
void CompositionDataWrapper::insertFrameProducer(hal::FrameProducer&& producer,
const int32_t layerId) {
mOutput.producers.emplace_back(std::move(producer));
mOutput.producers.back().producerId = getProducerId(layerId);
}
bool CompositionDataWrapper::isLayerRelevant(const int32_t layerId) const
REQUIRES(android::kMainThreadContext) {
int32_t currentId = layerId;
int depth = 0;
constexpr int kMaxParentDepth = 32;
while (currentId >= 0 && depth++ < kMaxParentDepth) {
if (mSessionLayerMap.isLayerRelevant(currentId)) {
return true;
}
if (!mGetParentIdFn) {
break;
}
int32_t parentId = mGetParentIdFn(currentId);
if (parentId == currentId) {
break;
}
currentId = parentId;
}
return false;
}
hal::FrameProducerUpdate& CompositionDataWrapper::getOrCreateLayerUpdate(const int32_t layerId) {
if (!mOutput.updateData.has_value()) {
mOutput.updateData = hal::CompositionUpdate{};
}
auto& updates = mOutput.updateData->producerUpdates;
auto [iterator, inserted] = mLayerIdToUpdateIndexMap.try_emplace(layerId, updates.size());
if (inserted) {
updates.push_back({
.producerId = getProducerId(layerId),
.isDead = false,
});
}
return updates[iterator->second];
}
void CompositionDataWrapper::setLayerSessions(const int32_t layerId,
std::vector<int32_t>&& sessions) {
hal::FrameProducerUpdate& update = getOrCreateLayerUpdate(layerId);
update.sessions = std::move(sessions);
}
void CompositionDataWrapper::setDeadLayer(const int32_t layerId) {
hal::FrameProducerUpdate& update = getOrCreateLayerUpdate(layerId);
update.isDead = true;
}
} // namespace android::adpf