AnimationFrameRate.h [plain text]
#pragma once
#include <wtf/OptionSet.h>
#include <wtf/Seconds.h>
#include <wtf/text/TextStream.h>
namespace WebCore {
using FramesPerSecond = unsigned;
enum class ThrottlingReason {
VisuallyIdle = 1 << 0,
OutsideViewport = 1 << 1,
LowPowerMode = 1 << 2,
NonInteractedCrossOriginFrame = 1 << 3,
};
constexpr const Seconds FullSpeedAnimationInterval { 15_ms };
constexpr const Seconds HalfSpeedThrottlingAnimationInterval { 30_ms };
constexpr const Seconds AggressiveThrottlingAnimationInterval { 10_s };
constexpr const FramesPerSecond FullSpeedFramesPerSecond = 60;
constexpr const FramesPerSecond HalfSpeedThrottlingFramesPerSecond = 30;
constexpr const FramesPerSecond ZeroFramesPerSecond = 0;
inline Seconds preferredFrameInterval(const OptionSet<ThrottlingReason>& reasons)
{
if (reasons.contains(ThrottlingReason::OutsideViewport))
return AggressiveThrottlingAnimationInterval;
if (reasons.containsAny({ ThrottlingReason::LowPowerMode, ThrottlingReason::NonInteractedCrossOriginFrame }))
return HalfSpeedThrottlingAnimationInterval;
return FullSpeedAnimationInterval;
}
inline FramesPerSecond preferredFramesPerSecond(Seconds preferredFrameInterval)
{
if (preferredFrameInterval == FullSpeedAnimationInterval)
return FullSpeedFramesPerSecond;
if (preferredFrameInterval == HalfSpeedThrottlingAnimationInterval)
return HalfSpeedThrottlingFramesPerSecond;
ASSERT_NOT_REACHED();
return ZeroFramesPerSecond;
}
inline TextStream& operator<<(TextStream& ts, const OptionSet<ThrottlingReason>& reasons)
{
bool didAppend = false;
for (auto reason : reasons) {
if (didAppend)
ts << "|";
switch (reason) {
case ThrottlingReason::VisuallyIdle:
ts << "VisuallyIdle";
break;
case ThrottlingReason::OutsideViewport:
ts << "OutsideViewport";
break;
case ThrottlingReason::LowPowerMode:
ts << "LowPowerMode";
break;
case ThrottlingReason::NonInteractedCrossOriginFrame:
ts << "NonInteractiveCrossOriginFrame";
break;
}
didAppend = true;
}
if (reasons.isEmpty())
ts << "[Unthrottled]";
return ts;
}
}