Loading services/surfaceflinger/Clock.h 0 → 100644 +42 −0 Original line number Diff line number Diff line /* * Copyright 2021 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 <chrono> namespace android { // Abstract interface for timekeeping which can be injected for unit tests. class Clock { public: Clock() = default; virtual ~Clock() = default; // Returns the current time virtual std::chrono::steady_clock::time_point now() const = 0; }; class SteadyClock : public Clock { public: virtual ~SteadyClock() = default; std::chrono::steady_clock::time_point now() const override { return std::chrono::steady_clock::now(); } }; } // namespace android No newline at end of file services/surfaceflinger/FpsReporter.cpp +13 −3 Original line number Diff line number Diff line Loading @@ -26,10 +26,18 @@ namespace android { FpsReporter::FpsReporter(frametimeline::FrameTimeline& frameTimeline, SurfaceFlinger& flinger) : mFrameTimeline(frameTimeline), mFlinger(flinger) {} FpsReporter::FpsReporter(frametimeline::FrameTimeline& frameTimeline, SurfaceFlinger& flinger, std::unique_ptr<Clock> clock) : mFrameTimeline(frameTimeline), mFlinger(flinger), mClock(std::move(clock)) { LOG_ALWAYS_FATAL_IF(mClock == nullptr, "Passed in null clock when constructing FpsReporter!"); } void FpsReporter::dispatchLayerFps() { const auto now = mClock->now(); if (now - mLastDispatch < kMinDispatchDuration) { return; } void FpsReporter::dispatchLayerFps() const { std::vector<TrackedListener> localListeners; { std::scoped_lock lock(mMutex); Loading Loading @@ -71,6 +79,8 @@ void FpsReporter::dispatchLayerFps() const { listener.listener->onFpsReported(mFrameTimeline.computeFps(layerIds)); } mLastDispatch = now; } void FpsReporter::binderDied(const wp<IBinder>& who) { Loading services/surfaceflinger/FpsReporter.h +8 −2 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ #include <unordered_map> #include "Clock.h" #include "FrameTimeline/FrameTimeline.h" namespace android { Loading @@ -31,12 +32,13 @@ class SurfaceFlinger; class FpsReporter : public IBinder::DeathRecipient { public: FpsReporter(frametimeline::FrameTimeline& frameTimeline, SurfaceFlinger& flinger); FpsReporter(frametimeline::FrameTimeline& frameTimeline, SurfaceFlinger& flinger, std::unique_ptr<Clock> clock = std::make_unique<SteadyClock>()); // Dispatches updated layer fps values for the registered listeners // This method promotes Layer weak pointers and performs layer stack traversals, so mStateLock // must be held when calling this method. void dispatchLayerFps() const EXCLUDES(mMutex); void dispatchLayerFps() EXCLUDES(mMutex); // Override for IBinder::DeathRecipient void binderDied(const wp<IBinder>&) override; Loading @@ -61,6 +63,10 @@ private: frametimeline::FrameTimeline& mFrameTimeline; SurfaceFlinger& mFlinger; static const constexpr std::chrono::steady_clock::duration kMinDispatchDuration = std::chrono::milliseconds(500); std::unique_ptr<Clock> mClock; std::chrono::steady_clock::time_point mLastDispatch; std::unordered_map<wp<IBinder>, TrackedListener, WpHash> mListeners GUARDED_BY(mMutex); }; Loading services/surfaceflinger/Scheduler/OneShotTimer.cpp +1 −7 Original line number Diff line number Diff line Loading @@ -15,7 +15,6 @@ */ #include "OneShotTimer.h" #include <utils/Log.h> #include <utils/Timers.h> #include <chrono> Loading @@ -40,14 +39,9 @@ void calculateTimeoutTime(std::chrono::nanoseconds timestamp, timespec* spec) { namespace android { namespace scheduler { std::chrono::steady_clock::time_point OneShotTimer::Clock::now() const { return std::chrono::steady_clock::now(); } OneShotTimer::OneShotTimer(std::string name, const Interval& interval, const ResetCallback& resetCallback, const TimeoutCallback& timeoutCallback, std::unique_ptr<OneShotTimer::Clock> clock) const TimeoutCallback& timeoutCallback, std::unique_ptr<Clock> clock) : mClock(std::move(clock)), mName(std::move(name)), mInterval(interval), Loading services/surfaceflinger/Scheduler/OneShotTimer.h +2 −9 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ #include <chrono> #include <condition_variable> #include <thread> #include "../Clock.h" #include <android-base/thread_annotations.h> Loading @@ -36,17 +37,9 @@ public: using ResetCallback = std::function<void()>; using TimeoutCallback = std::function<void()>; class Clock { public: Clock() = default; virtual ~Clock() = default; virtual std::chrono::steady_clock::time_point now() const; }; OneShotTimer(std::string name, const Interval& interval, const ResetCallback& resetCallback, const TimeoutCallback& timeoutCallback, std::unique_ptr<OneShotTimer::Clock> = std::make_unique<OneShotTimer::Clock>()); std::unique_ptr<Clock> clock = std::make_unique<SteadyClock>()); ~OneShotTimer(); // Initializes and turns on the idle timer. Loading Loading
services/surfaceflinger/Clock.h 0 → 100644 +42 −0 Original line number Diff line number Diff line /* * Copyright 2021 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 <chrono> namespace android { // Abstract interface for timekeeping which can be injected for unit tests. class Clock { public: Clock() = default; virtual ~Clock() = default; // Returns the current time virtual std::chrono::steady_clock::time_point now() const = 0; }; class SteadyClock : public Clock { public: virtual ~SteadyClock() = default; std::chrono::steady_clock::time_point now() const override { return std::chrono::steady_clock::now(); } }; } // namespace android No newline at end of file
services/surfaceflinger/FpsReporter.cpp +13 −3 Original line number Diff line number Diff line Loading @@ -26,10 +26,18 @@ namespace android { FpsReporter::FpsReporter(frametimeline::FrameTimeline& frameTimeline, SurfaceFlinger& flinger) : mFrameTimeline(frameTimeline), mFlinger(flinger) {} FpsReporter::FpsReporter(frametimeline::FrameTimeline& frameTimeline, SurfaceFlinger& flinger, std::unique_ptr<Clock> clock) : mFrameTimeline(frameTimeline), mFlinger(flinger), mClock(std::move(clock)) { LOG_ALWAYS_FATAL_IF(mClock == nullptr, "Passed in null clock when constructing FpsReporter!"); } void FpsReporter::dispatchLayerFps() { const auto now = mClock->now(); if (now - mLastDispatch < kMinDispatchDuration) { return; } void FpsReporter::dispatchLayerFps() const { std::vector<TrackedListener> localListeners; { std::scoped_lock lock(mMutex); Loading Loading @@ -71,6 +79,8 @@ void FpsReporter::dispatchLayerFps() const { listener.listener->onFpsReported(mFrameTimeline.computeFps(layerIds)); } mLastDispatch = now; } void FpsReporter::binderDied(const wp<IBinder>& who) { Loading
services/surfaceflinger/FpsReporter.h +8 −2 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ #include <unordered_map> #include "Clock.h" #include "FrameTimeline/FrameTimeline.h" namespace android { Loading @@ -31,12 +32,13 @@ class SurfaceFlinger; class FpsReporter : public IBinder::DeathRecipient { public: FpsReporter(frametimeline::FrameTimeline& frameTimeline, SurfaceFlinger& flinger); FpsReporter(frametimeline::FrameTimeline& frameTimeline, SurfaceFlinger& flinger, std::unique_ptr<Clock> clock = std::make_unique<SteadyClock>()); // Dispatches updated layer fps values for the registered listeners // This method promotes Layer weak pointers and performs layer stack traversals, so mStateLock // must be held when calling this method. void dispatchLayerFps() const EXCLUDES(mMutex); void dispatchLayerFps() EXCLUDES(mMutex); // Override for IBinder::DeathRecipient void binderDied(const wp<IBinder>&) override; Loading @@ -61,6 +63,10 @@ private: frametimeline::FrameTimeline& mFrameTimeline; SurfaceFlinger& mFlinger; static const constexpr std::chrono::steady_clock::duration kMinDispatchDuration = std::chrono::milliseconds(500); std::unique_ptr<Clock> mClock; std::chrono::steady_clock::time_point mLastDispatch; std::unordered_map<wp<IBinder>, TrackedListener, WpHash> mListeners GUARDED_BY(mMutex); }; Loading
services/surfaceflinger/Scheduler/OneShotTimer.cpp +1 −7 Original line number Diff line number Diff line Loading @@ -15,7 +15,6 @@ */ #include "OneShotTimer.h" #include <utils/Log.h> #include <utils/Timers.h> #include <chrono> Loading @@ -40,14 +39,9 @@ void calculateTimeoutTime(std::chrono::nanoseconds timestamp, timespec* spec) { namespace android { namespace scheduler { std::chrono::steady_clock::time_point OneShotTimer::Clock::now() const { return std::chrono::steady_clock::now(); } OneShotTimer::OneShotTimer(std::string name, const Interval& interval, const ResetCallback& resetCallback, const TimeoutCallback& timeoutCallback, std::unique_ptr<OneShotTimer::Clock> clock) const TimeoutCallback& timeoutCallback, std::unique_ptr<Clock> clock) : mClock(std::move(clock)), mName(std::move(name)), mInterval(interval), Loading
services/surfaceflinger/Scheduler/OneShotTimer.h +2 −9 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ #include <chrono> #include <condition_variable> #include <thread> #include "../Clock.h" #include <android-base/thread_annotations.h> Loading @@ -36,17 +37,9 @@ public: using ResetCallback = std::function<void()>; using TimeoutCallback = std::function<void()>; class Clock { public: Clock() = default; virtual ~Clock() = default; virtual std::chrono::steady_clock::time_point now() const; }; OneShotTimer(std::string name, const Interval& interval, const ResetCallback& resetCallback, const TimeoutCallback& timeoutCallback, std::unique_ptr<OneShotTimer::Clock> = std::make_unique<OneShotTimer::Clock>()); std::unique_ptr<Clock> clock = std::make_unique<SteadyClock>()); ~OneShotTimer(); // Initializes and turns on the idle timer. Loading