blob: 5d9541153fb8ac470a52567f09ac9d6391480d78 [file]
/*
* Copyright (C) 2022 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/performance_hint.h>
#include <private/performance_hint_private.h>
#include <future>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "utils/TimeUtils.h"
struct ANativeWindow;
struct ASurfaceControl;
namespace android {
namespace uirenderer {
namespace renderthread {
class RenderThread;
// NOTE also update libs/hwui/platform/host/renderthread/HintSessionWrapper.cpp
class HintSessionWrapper : public std::enable_shared_from_this<HintSessionWrapper> {
public:
friend class HintSessionWrapperTests;
enum class CpuLoadResetReason {
Other = 0,
PointerMove = 1,
FirstFrame = 2,
};
HintSessionWrapper(pid_t uiThreadId, pid_t renderThreadId);
~HintSessionWrapper();
void updateTargetWorkDuration(int64_t targetWorkDurationNanos);
void reportActualWorkDuration(int64_t actualDurationNanos);
void sendCpuLoadResetHint(CpuLoadResetReason reason);
void setName(std::string name) { mName = std::move(name); }
void sendCpuLoadIncreaseHint();
void sendGpuLoadIncreaseHint();
bool init();
void destroy();
void setHintSessionEnabled(bool enabled);
bool alive();
void delayedDestroy(renderthread::RenderThread& rt, nsecs_t delay);
/**
* Binds the rendering window and surface controller to this ADPF performance hint session.
*
* If the hint session is still launching asynchronously, the window and control are cached
* and will be bound automatically once the session creation completes.
*
* @param window The native rendering window target (ANativeWindow).
* @param control The surface controller target (ASurfaceControl).
*/
void setSurfaces(ANativeWindow* window, ASurfaceControl* control);
APerformanceHintSession* getSession() const { return mHintSession; }
// Must be called on Render thread. Otherwise can cause a race condition.
void setActiveFunctorThreads(std::vector<pid_t> threadIds);
using SessionConfigPtr =
std::unique_ptr<ASessionCreationConfig, void (*)(ASessionCreationConfig*)>;
private:
struct SessionCreationResult {
APerformanceHintSession* session = nullptr;
bool usesAutoTiming = false;
};
APerformanceHintSession* mHintSession = nullptr;
// This needs to work concurrently for testing
std::optional<std::shared_future<SessionCreationResult>> mHintSessionFuture;
// This needs to work concurrently for testing
std::optional<std::shared_future<int>> mSetThreadsFuture;
int mResetsSinceLastReport = 0;
nsecs_t mLastTargetWorkDuration = 0;
// Updated to the current timestamp when HintSessionWrapper methods are called. Used in
// delayedDestroy to prevent destroying sessions that are reused before the delay expires.
nsecs_t mLastUpdate = 0;
pid_t mUiThreadId;
pid_t mRenderThreadId;
std::vector<pid_t> mPermanentSessionTids;
std::vector<pid_t> mActiveFunctorTids;
bool mSessionValid = true;
bool mHintSessionEnabled = true;
bool mUsesAutoTiming = false;
std::string mName;
// The native window and surface control associated with this session.
// Used to map this HWUI hint session to the corresponding SurfaceFlinger layers for timing
// telemetry.
ANativeWindow* mWindow = nullptr;
ASurfaceControl* mSurfaceControl = nullptr;
void sendNativeSurfacesInternal();
static constexpr nsecs_t kResetHintTimeout = 100_ms;
static constexpr int64_t kSanityCheckLowerBound = 100_us;
static constexpr int64_t kSanityCheckUpperBound = 10_s;
static constexpr int64_t kDefaultTargetDuration = 16666667;
// Allows easier stub when testing
class HintSessionBinding {
public:
virtual ~HintSessionBinding() = default;
virtual void init();
APerformanceHintManager* (*getManager)();
APerformanceHintSession* (*createSessionInternal)(APerformanceHintManager* manager,
const int32_t* tids, size_t tidCount,
int64_t defaultTarget,
SessionTag tag) = nullptr;
void (*closeSession)(APerformanceHintSession* session) = nullptr;
void (*updateTargetWorkDuration)(APerformanceHintSession* session,
int64_t targetDuration) = nullptr;
void (*reportActualWorkDuration)(APerformanceHintSession* session,
int64_t actualDuration) = nullptr;
void (*sendHint)(APerformanceHintSession* session, int32_t hintId) = nullptr;
int (*setThreads)(APerformanceHintSession* session, const pid_t* tids,
size_t size) = nullptr;
int (*setNativeSurfaces)(APerformanceHintSession* session,
ANativeWindow* const* nativeWindows, size_t nativeWindowsSize,
ASurfaceControl* const* surfaceControls,
size_t surfaceControlsSize) = nullptr;
bool (*isFeatureSupported)(APerformanceHintFeature feature) = nullptr;
ASessionCreationConfig* (*createSessionConfig)() = nullptr;
void (*releaseSessionConfig)(ASessionCreationConfig* config) = nullptr;
void (*sessionConfigSetTids)(ASessionCreationConfig* config, const pid_t* tids,
size_t size) = nullptr;
void (*sessionConfigSetTargetWorkDurationNanos)(ASessionCreationConfig* config,
int64_t targetDurationNanos) = nullptr;
void (*sessionConfigSetGraphicsPipeline)(ASessionCreationConfig* config,
bool enabled) = nullptr;
void (*sessionConfigSetUseAutoTiming)(ASessionCreationConfig* config, bool cpu,
bool gpu) = nullptr;
int (*createSessionUsingConfigInternal)(APerformanceHintManager* manager,
ASessionCreationConfig* config,
APerformanceHintSession** sessionOut,
SessionTag tag) = nullptr;
private:
bool mInitialized = false;
};
std::shared_ptr<HintSessionBinding> mBinding;
};
} /* namespace renderthread */
} /* namespace uirenderer */
} /* namespace android */