Skip to content

Commit

Permalink
Timers are not coalesced during thermal throttling; should match low …
Browse files Browse the repository at this point in the history
…power mode

https://bugs.webkit.org/show_bug.cgi?id=260931
rdar://112725366

Reviewed by Aditya Keerthi.

* Source/WebCore/platform/ThermalMitigationNotifier.cpp: Added.
(WebCore::ThermalMitigationNotifier::ThermalMitigationNotifier):
(WebCore::ThermalMitigationNotifier::thermalMitigationEnabled const):
* Source/WebCore/platform/ThermalMitigationNotifier.h: Added.
* Source/WebCore/platform/cocoa/ThermalMitigationNotifier.mm: Added.
(-[WebThermalMitigationObserver initWithNotifier:]):
(-[WebThermalMitigationObserver dealloc]):
(-[WebThermalMitigationObserver thermalStateDidChange]):
(-[WebThermalMitigationObserver thermalMitigationEnabled]):
(WebCore::ThermalMitigationNotifier::ThermalMitigationNotifier):
(WebCore::ThermalMitigationNotifier::~ThermalMitigationNotifier):
(WebCore::ThermalMitigationNotifier::thermalMitigationEnabled const):
(WebCore::ThermalMitigationNotifier::notifyThermalMitigationChanged):
(WebCore::notifyThermalMitigationChanged):
Add an observer for thermal state. Assume that we want to mitigate our contribution
to high thermals when the state is Serious or above.
This has the same shape as LowPowerModeNotifier.

* Source/WTF/wtf/PlatformHave.h:
* Source/WebCore/Sources.txt:
* Source/WebCore/SourcesCocoa.txt:
* Source/WebCore/WebCore.xcodeproj/project.pbxproj:
* Source/WebCore/page/DOMTimer.h:
* Source/WebCore/page/Page.cpp:
(WebCore::m_thermalMitigationNotifier):
(WebCore::m_badgeClient):
(WebCore::Page::didChangeThrottlingReasons):
(WebCore::Page::setIsVisuallyIdleInternal):
(WebCore::Page::handleLowModePowerChange):
(WebCore::Page::handleThermalMitigationChange):
(WebCore::Page::updateDOMTimerAlignmentInterval):
* Source/WebCore/page/Page.h:
(WebCore::Page::isThermalMitigationEnabled const):
* Source/WebCore/platform/graphics/AnimationFrameRate.cpp:
(WebCore::operator<<):
* Source/WebCore/platform/graphics/AnimationFrameRate.h:
When thermal mitigation is enabled, align DOM timers to 30ms like we do in low power mode.

* Source/WebCore/platform/graphics/avfoundation/objc/AVAssetMIMETypeCache.mm:
* Source/WebCore/platform/graphics/avfoundation/objc/LocalSampleBufferDisplayLayer.mm:
(WebCore::transformationMatrixForVideoFrame):
(WebCore::LocalSampleBufferDisplayLayer::enqueueVideoFrame):
(WebCore::videoTransformationMatrix): Deleted.
* Source/WebCore/platform/network/mac/UTIUtilities.mm:
* Source/WebCore/platform/audio/cocoa/AudioUtilitiesCocoa.h:
Unrelated unified sources fixes.

Canonical link: https://commits.webkit.org/267480@main
  • Loading branch information
hortont424 committed Aug 31, 2023
1 parent acfa97d commit 3088454
Show file tree
Hide file tree
Showing 17 changed files with 260 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Source/WTF/wtf/PlatformHave.h
Original file line number Diff line number Diff line change
Expand Up @@ -1504,3 +1504,7 @@
|| (PLATFORM(WATCHOS) && __WATCH_OS_VERSION_MIN_REQUIRED >= 100000)
#define HAVE_AVSPEECHSYNTHESIS_VOICES_CHANGE_NOTIFICATION 1
#endif

#if PLATFORM(VISION)
#define HAVE_APPLE_THERMAL_MITIGATION_SUPPORT 1
#endif
1 change: 1 addition & 0 deletions Source/WebCore/Sources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2110,6 +2110,7 @@ platform/SystemSoundManager.cpp
platform/StaticPasteboard.cpp
platform/Theme.cpp
platform/ThemeTypes.cpp
platform/ThermalMitigationNotifier.cpp
platform/ThreadGlobalData.cpp
platform/ThreadTimers.cpp
platform/Timer.cpp
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/SourcesCocoa.txt
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ platform/cocoa/SystemVersion.mm
platform/cocoa/TelephoneNumberDetectorCocoa.cpp
platform/cocoa/TextRecognitionResultCocoa.mm
platform/cocoa/ThemeCocoa.mm
platform/cocoa/ThermalMitigationNotifier.mm
platform/cocoa/UserAgentCocoa.mm
platform/cocoa/VideoFullscreenModelVideoElement.mm
platform/cocoa/VideoToolboxSoftLink.cpp
Expand Down
6 changes: 6 additions & 0 deletions Source/WebCore/WebCore.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9033,6 +9033,9 @@
2D8D3BEB23847AC900657075 /* gesture-recognizer.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = "gesture-recognizer.js"; sourceTree = "<group>"; };
2D8D3BEC23847AC900657075 /* pinch.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = pinch.js; sourceTree = "<group>"; };
2D8D3BED23847AC900657075 /* tap.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = tap.js; sourceTree = "<group>"; };
2D8EF8112A9D5101000118F8 /* ThermalMitigationNotifier.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = ThermalMitigationNotifier.mm; sourceTree = "<group>"; };
2D8EF8122A9D511F000118F8 /* ThermalMitigationNotifier.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ThermalMitigationNotifier.h; sourceTree = "<group>"; };
2D8EF8132A9D511F000118F8 /* ThermalMitigationNotifier.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ThermalMitigationNotifier.cpp; sourceTree = "<group>"; };
2D8FEBDA143E3EF70072502B /* CSSCrossfadeValue.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CSSCrossfadeValue.cpp; sourceTree = "<group>"; };
2D8FEBDB143E3EF70072502B /* CSSCrossfadeValue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CSSCrossfadeValue.h; sourceTree = "<group>"; };
2D9066040BE141D400956998 /* RenderLayoutState.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = RenderLayoutState.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -29859,6 +29862,7 @@
F4DE26CC27B743AE0071FD6E /* TextRecognitionResultCocoa.mm */,
310D71941B335C9D009C7B73 /* ThemeCocoa.h */,
310D71931B335C9D009C7B73 /* ThemeCocoa.mm */,
2D8EF8112A9D5101000118F8 /* ThermalMitigationNotifier.mm */,
A3AF9D81203252EE006CAD06 /* UserAgentCocoa.mm */,
E300511429DB848100E1C3A4 /* VideoFullscreenCaptions.h */,
E300511529DB92D300E1C3A4 /* VideoFullscreenCaptions.mm */,
Expand Down Expand Up @@ -33299,6 +33303,8 @@
BCE658FE0EA9248A007E4533 /* Theme.h */,
0F283A8D235582F8004794CA /* ThemeTypes.cpp */,
BCE659A80EA927B9007E4533 /* ThemeTypes.h */,
2D8EF8132A9D511F000118F8 /* ThermalMitigationNotifier.cpp */,
2D8EF8122A9D511F000118F8 /* ThermalMitigationNotifier.h */,
51DF6D7D0B92A16D00C2DC85 /* ThreadCheck.h */,
E1FF57A50F01256B00891EBB /* ThreadGlobalData.cpp */,
E1FF57A20F01255B00891EBB /* ThreadGlobalData.h */,
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/page/DOMTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class DOMTimer final : public RefCounted<DOMTimer>, public ActiveDOMObject, publ

static Seconds defaultMinimumInterval() { return 4_ms; }
static Seconds defaultAlignmentInterval() { return 0_s; }
static Seconds defaultAlignmentIntervalInLowPowerMode() { return 30_ms; }
static Seconds defaultAlignmentIntervalInLowPowerOrThermallyMitigatedMode() { return 30_ms; }
static Seconds nonInteractedCrossOriginFrameAlignmentInterval() { return 30_ms; }
static Seconds hiddenPageAlignmentInterval() { return 1_s; }

Expand Down
27 changes: 23 additions & 4 deletions Source/WebCore/page/Page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
#include "TextIterator.h"
#include "TextRecognitionResult.h"
#include "TextResourceDecoder.h"
#include "ThermalMitigationNotifier.h"
#include "UserContentProvider.h"
#include "UserContentURLPattern.h"
#include "UserScript.h"
Expand Down Expand Up @@ -342,6 +343,7 @@ Page::Page(PageConfiguration&& pageConfiguration)
, m_isUtilityPage(isUtilityPageChromeClient(chrome().client()))
, m_performanceMonitor(isUtilityPage() ? nullptr : makeUnique<PerformanceMonitor>(*this))
, m_lowPowerModeNotifier(makeUnique<LowPowerModeNotifier>([this](bool isLowPowerModeEnabled) { handleLowModePowerChange(isLowPowerModeEnabled); }))
, m_thermalMitigationNotifier(makeUnique<ThermalMitigationNotifier>([this](bool thermalMitigationEnabled) { handleThermalMitigationChange(thermalMitigationEnabled); }))
, m_performanceLogging(makeUnique<PerformanceLogging>(*this))
#if PLATFORM(MAC) && (ENABLE(SERVICE_CONTROLS) || ENABLE(TELEPHONE_NUMBER_DETECTION))
, m_servicesOverlayController(makeUnique<ServicesOverlayController>(*this))
Expand Down Expand Up @@ -417,6 +419,9 @@ Page::Page(PageConfiguration&& pageConfiguration)

if (m_lowPowerModeNotifier->isLowPowerModeEnabled())
m_throttlingReasons.add(ThrottlingReason::LowPowerMode);

if (m_thermalMitigationNotifier->thermalMitigationEnabled())
m_throttlingReasons.add(ThrottlingReason::ThermalMitigation);
}

Page::~Page()
Expand Down Expand Up @@ -2269,6 +2274,19 @@ void Page::handleLowModePowerChange(bool isLowPowerModeEnabled)
updateDOMTimerAlignmentInterval();
}

void Page::handleThermalMitigationChange(bool thermalMitigationEnabled)
{
if (!canUpdateThrottlingReason(ThrottlingReason::ThermalMitigation))
return;

if (thermalMitigationEnabled == m_throttlingReasons.contains(ThrottlingReason::ThermalMitigation))
return;

m_throttlingReasons.set(ThrottlingReason::ThermalMitigation, thermalMitigationEnabled);

updateDOMTimerAlignmentInterval();
}

void Page::userStyleSheetLocationChanged()
{
// FIXME: Eventually we will move to a model of just being handed the sheet
Expand Down Expand Up @@ -2484,10 +2502,11 @@ void Page::updateDOMTimerAlignmentInterval()
bool needsIncreaseTimer = false;

switch (m_timerThrottlingState) {
case TimerThrottlingState::Disabled:
m_domTimerAlignmentInterval = isLowPowerModeEnabled() ? DOMTimer::defaultAlignmentIntervalInLowPowerMode() : DOMTimer::defaultAlignmentInterval();
case TimerThrottlingState::Disabled: {
bool isInLowPowerOrThermallyMitigatedMode = isLowPowerModeEnabled() || isThermalMitigationEnabled();
m_domTimerAlignmentInterval = isInLowPowerOrThermallyMitigatedMode ? DOMTimer::defaultAlignmentIntervalInLowPowerOrThermallyMitigatedMode() : DOMTimer::defaultAlignmentInterval();
break;

}
case TimerThrottlingState::Enabled:
m_domTimerAlignmentInterval = DOMTimer::hiddenPageAlignmentInterval();
break;
Expand All @@ -2511,7 +2530,7 @@ void Page::updateDOMTimerAlignmentInterval()

// If throttling is enabled, auto-increasing of throttling is enabled, and the auto-increase
// limit has not yet been reached, and then arm the timer to consider an increase. Time to wait
// between increases is equal to the current throttle time. Since alinment interval increases
// between increases is equal to the current throttle time. Since alignment interval increases
// exponentially, time between steps is exponential too.
if (!needsIncreaseTimer)
m_domTimerAlignmentIntervalIncreaseTimer.stop();
Expand Down
4 changes: 4 additions & 0 deletions Source/WebCore/page/Page.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class SpeechRecognitionConnection;
class StorageNamespace;
class StorageNamespaceProvider;
class StorageProvider;
class ThermalMitigationNotifier;
class UserContentProvider;
class UserContentURLPattern;
class UserScript;
Expand Down Expand Up @@ -946,6 +947,7 @@ class Page : public Supplementable<Page>, public CanMakeWeakPtr<Page> {
ShouldRelaxThirdPartyCookieBlocking shouldRelaxThirdPartyCookieBlocking() const { return m_shouldRelaxThirdPartyCookieBlocking; }

bool isLowPowerModeEnabled() const { return m_throttlingReasons.contains(ThrottlingReason::LowPowerMode); }
bool isThermalMitigationEnabled() const { return m_throttlingReasons.contains(ThrottlingReason::ThermalMitigation); }
bool canUpdateThrottlingReason(ThrottlingReason reason) const { return !m_throttlingReasonsOverridenForTesting.contains(reason); }
WEBCORE_EXPORT void setLowPowerModeEnabledOverrideForTesting(std::optional<bool>);
WEBCORE_EXPORT void setOutsideViewportThrottlingEnabledForTesting(bool);
Expand Down Expand Up @@ -1082,6 +1084,7 @@ class Page : public Supplementable<Page>, public CanMakeWeakPtr<Page> {
#endif

void handleLowModePowerChange(bool);
void handleThermalMitigationChange(bool);

enum class TimerThrottlingState { Disabled, Enabled, EnabledIncreasing };
void hiddenPageDOMTimerThrottlingStateChanged();
Expand Down Expand Up @@ -1328,6 +1331,7 @@ class Page : public Supplementable<Page>, public CanMakeWeakPtr<Page> {

std::unique_ptr<PerformanceMonitor> m_performanceMonitor;
std::unique_ptr<LowPowerModeNotifier> m_lowPowerModeNotifier;
std::unique_ptr<ThermalMitigationNotifier> m_thermalMitigationNotifier;
OptionSet<ThrottlingReason> m_throttlingReasons;
OptionSet<ThrottlingReason> m_throttlingReasonsOverridenForTesting;

Expand Down
46 changes: 46 additions & 0 deletions Source/WebCore/platform/ThermalMitigationNotifier.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "config.h"
#include "ThermalMitigationNotifier.h"

namespace WebCore {

#if !HAVE(APPLE_THERMAL_MITIGATION_SUPPORT)

ThermalMitigationNotifier::ThermalMitigationNotifier(ThermalMitigationChangeCallback&&)
{
}

ThermalMitigationNotifier::~ThermalMitigationNotifier() = default;

bool ThermalMitigationNotifier::thermalMitigationEnabled() const
{
return false;
}

#endif

} // namespace WebCore
57 changes: 57 additions & 0 deletions Source/WebCore/platform/ThermalMitigationNotifier.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#include <wtf/Function.h>
#include <wtf/WeakPtr.h>

#if HAVE(APPLE_THERMAL_MITIGATION_SUPPORT)
#include <wtf/RetainPtr.h>
OBJC_CLASS WebThermalMitigationObserver;
#endif

namespace WebCore {

class ThermalMitigationNotifier : public CanMakeWeakPtr<ThermalMitigationNotifier> {
WTF_MAKE_FAST_ALLOCATED;
public:
using ThermalMitigationChangeCallback = Function<void(bool thermalMitigationEnabled)>;
WEBCORE_EXPORT explicit ThermalMitigationNotifier(ThermalMitigationChangeCallback&&);
WEBCORE_EXPORT ~ThermalMitigationNotifier();

WEBCORE_EXPORT bool thermalMitigationEnabled() const;

private:
#if HAVE(APPLE_THERMAL_MITIGATION_SUPPORT)
void notifyThermalMitigationChanged(bool);
friend void notifyThermalMitigationChanged(ThermalMitigationNotifier&, bool);

RetainPtr<WebThermalMitigationObserver> m_observer;
ThermalMitigationChangeCallback m_callback;
#endif
};

}
2 changes: 2 additions & 0 deletions Source/WebCore/platform/audio/cocoa/AudioUtilitiesCocoa.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#if ENABLE(WEB_AUDIO)

#include "AudioStreamDescription.h"

namespace WebCore {
class AudioBus;

Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/platform/cocoa/LowPowerModeNotifier.mm
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,4 @@ void notifyLowPowerModeChanged(LowPowerModeNotifier& notifier, bool enabled)

} // namespace WebCore

#endif // PLATFORM(APPLE_LOW_POWER_MODE_SUPPORT)
#endif // HAVE(APPLE_LOW_POWER_MODE_SUPPORT)
105 changes: 105 additions & 0 deletions Source/WebCore/platform/cocoa/ThermalMitigationNotifier.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (C) 2023 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#import "config.h"
#import "ThermalMitigationNotifier.h"

#if HAVE(APPLE_THERMAL_MITIGATION_SUPPORT)

#import "Logging.h"
#import <Foundation/NSProcessInfo.h>
#import <wtf/MainThread.h>

@interface WebThermalMitigationObserver : NSObject
@property (nonatomic) WeakPtr<WebCore::ThermalMitigationNotifier> notifier;
@property (nonatomic, readonly) BOOL thermalMitigationEnabled;
@end

@implementation WebThermalMitigationObserver

- (WebThermalMitigationObserver *)initWithNotifier:(WebCore::ThermalMitigationNotifier&)notifier
{
self = [super init];
if (!self)
return nil;

_notifier = &notifier;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(thermalStateDidChange) name:NSProcessInfoThermalStateDidChangeNotification object:nil];
return self;
}

- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSProcessInfoThermalStateDidChangeNotification object:nil];
[super dealloc];
}

- (void)thermalStateDidChange
{
callOnMainThread([self, protectedSelf = RetainPtr<WebThermalMitigationObserver>(self)] {
if (_notifier)
notifyThermalMitigationChanged(*_notifier, self.thermalMitigationEnabled);
});
}

- (BOOL)thermalMitigationEnabled
{
return NSProcessInfo.processInfo.thermalState >= NSProcessInfoThermalStateSerious;
}

@end

namespace WebCore {

ThermalMitigationNotifier::ThermalMitigationNotifier(ThermalMitigationChangeCallback&& callback)
: m_observer(adoptNS([[WebThermalMitigationObserver alloc] initWithNotifier:*this]))
, m_callback(WTFMove(callback))
{
}

ThermalMitigationNotifier::~ThermalMitigationNotifier()
{
[m_observer setNotifier:nil];
}

bool ThermalMitigationNotifier::thermalMitigationEnabled() const
{
return [m_observer thermalMitigationEnabled];
}

void ThermalMitigationNotifier::notifyThermalMitigationChanged(bool enabled)
{
m_callback(enabled);
}

void notifyThermalMitigationChanged(ThermalMitigationNotifier& notifier, bool enabled)
{
RELEASE_LOG(PerformanceLogging, "Thermal mitigation is enabled: %d", enabled);
notifier.notifyThermalMitigationChanged(enabled);
}

} // namespace WebCore

#endif // HAVE(APPLE_THERMAL_MITIGATION_SUPPORT)
5 changes: 4 additions & 1 deletion Source/WebCore/platform/graphics/AnimationFrameRate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ TextStream& operator<<(TextStream& ts, const OptionSet<ThrottlingReason>& reason
ts << "LowPowerMode";
break;
case ThrottlingReason::NonInteractedCrossOriginFrame:
ts << "NonInteractiveCrossOriginFrame";
ts << "NonInteractedCrossOriginFrame";
break;
case ThrottlingReason::ThermalMitigation:
ts << "ThermalMitigation";
break;
}
didAppend = true;
Expand Down

0 comments on commit 3088454

Please sign in to comment.