blob: e3f4d955dcb0728f5e638fc67c58afa78baa361e [file]
/*
* Copyright 2024 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 <aidl/android/adpf/BnSessionManager.h>
#include <aidl/android/hardware/power/CompositionData.h>
#include <aidl/android/hardware/power/SupportInfo.h>
#include <sys/types.h>
#include <utils/Thread.h>
#include <functional>
#include <optional>
#include <string>
#include <vector>
#include "Common.h"
#include "CompositionDataWrapper.h"
#include "SessionLayerMap.h"
#include "ThreadContext.h"
namespace android {
class Layer;
namespace adpf {
namespace impl {
class PowerAdvisor;
}
// Talks to HMS to manage sessions for PowerHAL
class SessionManager : public BnSessionManager {
public:
// Settings used to configure the class, passed in from PowerAdvisor
struct Settings {
// Maximum batch size before sending an update is required
int32_t maxBatchSize;
// Whether batching is forced to the max size, unless the FPS changes
bool preferBatchingToMaxSize;
// Whether the SessionManager should only care about FPS changes and ignore other updates
bool fpsUpdatesOnly;
};
SessionManager(const uid_t uid = 0, const Settings settings = {});
// ISessionManager binder methods. Apart from these, most methods in this class are expected
// to run on the SF main thread.
ndk::ScopedAStatus trackedSessionsDied(const std::vector<int32_t>& in_sessionId)
EXCLUDES(mSessionManagerMutex) override;
ndk::ScopedAStatus associateSessionToLayers(const int32_t sessionId, const int32_t ownerUid,
const std::vector<::ndk::SpAIBinder>& layers)
EXCLUDES(mSessionManagerMutex) override;
// All methods past here run on SF main
// Update the lifecycles of any tracked sessions or layers. This is intended to accept the
// "destroyedHandles" object from updateLayerSnapshots in SF, and should reflect that type.
// The latch time of the frame is also passed in here to update the composition data
void updateTrackingState(const std::vector<std::pair<uint32_t, std::string>>& destroyedHandles,
const nsecs_t latchTime) EXCLUDES(mSessionManagerMutex)
REQUIRES(kMainThreadContext);
// Returns a batch of updates to send to the HAL
std::vector<hal::CompositionData>& getCompositionDataBatch() REQUIRES(kMainThreadContext);
// Creates an update wrapper for a given SF commit
void initForFrame() EXCLUDES(mSessionManagerMutex) REQUIRES(kMainThreadContext);
// Whether the SessionManager is currently "active"
// SessionManager activates at the start of the first commit after a client uses timeline API,
// and will deactivate if it is no longer tracking the lifecycle of any sessions or layers
bool isActive() const REQUIRES(kMainThreadContext);
// Returns the active per-frame CompositionDataWrapper during composition (or nullptr if
// inactive)
CompositionDataWrapper* getPowerTimeline();
// Update whether or not the session manager should be active
bool updateActive() EXCLUDES(mSessionManagerMutex) REQUIRES(kMainThreadContext);
void setLayersWithUnsignaledFences(std::vector<int32_t>&& layers) REQUIRES(kMainThreadContext);
// Finishes building the composition data object using metadata held
// in the wrapper, then cleans up the wrapper. Returns whether or not
// to send a batch.
bool finalizeCompositionData() REQUIRES(kMainThreadContext);
// Change the settings for the SessionManager. Currently only for testing.
void updateSettings(const Settings& settings);
bool isLayerRelevant(const int32_t layerId) const REQUIRES(kMainThreadContext);
void setParentIdLookupFn(std::function<int32_t(int32_t)> getParentIdFn) {
const std::scoped_lock lock(mSessionManagerMutex);
mGetParentIdFn = std::move(getParentIdFn);
}
private:
bool isLayerRelevantLocked(const int32_t layerId) const
REQUIRES(kMainThreadContext, mSessionManagerMutex);
// Settings used to configure the SessionManager class
Settings mSettings;
mutable std::function<int32_t(int32_t)> mGetParentIdFn GUARDED_BY(mSessionManagerMutex) =
nullptr;
// Session metadata tracked by the mTrackedSessionData map
struct SessionData {
int32_t uid;
};
// Layer metadata tracked by the mTrackedLayerData map
struct LayerData {
int32_t uid;
std::optional<double> fps;
};
struct PendingAssociation {
int32_t sessionId;
int32_t uid;
std::vector<int32_t> layerIds;
};
// Returns whether or not to send the next batch of data to the HAL
bool shouldSendCompositionDataBatch() REQUIRES(kMainThreadContext, mSessionManagerMutex);
// Update whether or not the session should be active
bool updateActiveLocked() REQUIRES(mSessionManagerMutex);
void removeDeadSessions(const std::vector<int32_t>& deadSessions)
REQUIRES(mSessionManagerMutex);
void updateBoundSessions() REQUIRES(mSessionManagerMutex);
std::vector<PendingAssociation> mPendingLayerAssociations GUARDED_BY(mSessionManagerMutex);
mutable std::mutex mSessionManagerMutex;
// State owned by the main thread
std::optional<CompositionDataWrapper> mCompositionDataWrapper;
std::vector<hal::CompositionData> mCompositionDataBatch;
// Tracks active associations between sessions and layers. Items in this map can be thought of
// as "active" connections, and any session or layer not in this map will not receive updates or
// be collected in SurfaceFlinger
SessionLayerMap mMap GUARDED_BY(mSessionManagerMutex);
// Used to prevent any updates if the sessionmanager has not been alive since
// the start of the frame, and tracks whether there are any relevant associations
std::atomic<bool> mActive{false};
// Layers that tried to latch this frame but were unsignaled
std::vector<int32_t> mUnsignaledLayerIds;
// State owned by mSessionManagerMutex
// The list of currently-living layers which have ever been tracked, this is used to persist any
// data we want to track across potential mapping disconnects, and to determine when to send
// death updates
std::unordered_map<int32_t, LayerData> mTrackedLayerData GUARDED_BY(mSessionManagerMutex);
// The list of currently-living sessions which have ever been tracked, this is used to persist
// any data we want to track across mapping disconnects
std::unordered_map<int32_t, SessionData> mTrackedSessionData GUARDED_BY(mSessionManagerMutex);
// The list of sessions that have died since we last called updateTrackingState
std::vector<int32_t> mDeadSessions GUARDED_BY(mSessionManagerMutex);
std::vector<int32_t> mDeadLayersPendingNotification GUARDED_BY(mSessionManagerMutex);
// Recorded during updateTrackingState and applied to mCompositionDataWrapper in initForFrame()
nsecs_t mLastLatchTime GUARDED_BY(mSessionManagerMutex) = 0;
};
} // namespace adpf
} // namespace android