Skip to content

Commit

Permalink
updateRendering() gets stuck at the wrong framerate when thermal thro…
Browse files Browse the repository at this point in the history
…ttling changes

https://bugs.webkit.org/show_bug.cgi?id=260846
rdar://112799115

Reviewed by Wenson Hsieh and Simon Fraser.

It turns out that `maximumRefreshRate` is actually "the maximum frame rate you can
achieve given current throttling behaviors", not "the frame rate of the display".
That means that, when throttling states change, maximumRefreshRate can change too.

We currently do not respond to changes in maximumRefreshRate. This means that we
can get stuck with an incorrect `maximumRefreshRate` (and `preferredFramesPerSecond`,
which is downstream of it).

* Source/WebKit/UIProcess/RemoteLayerTree/ios/RemoteLayerTreeDrawingAreaProxyIOS.mm:
(-[WKDisplayLinkHandler initWithDrawingAreaProxy:]):
(-[WKDisplayLinkHandler invalidate]):
(-[WKDisplayLinkHandler observeValueForKeyPath:ofObject:change:context:]):
(-[WKDisplayLinkHandler didChangeNominalFramesPerSecond]):
When the CADisplayLink's CADisplay's `refreshRate` changes, reconfigure the display, which
results in communicating the new maximum frame rate back to WebCore and also (indirectly)
updating the CADisplayLink's `preferredFramesPerSecond`.

* Source/WebCore/PAL/pal/spi/cocoa/QuartzCoreSPI.h:
Add CADisplay SPI.

Canonical link: https://commits.webkit.org/267440@main
  • Loading branch information
hortont424 committed Aug 30, 2023
1 parent 6845d64 commit a33d2c7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Source/WebCore/PAL/pal/spi/cocoa/QuartzCoreSPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#endif

#if PLATFORM(IOS_FAMILY)
#import <QuartzCore/CADisplay.h>
#import <QuartzCore/CADisplayLinkPrivate.h>
#endif

Expand All @@ -65,8 +66,12 @@
typedef struct _CARenderContext CARenderContext;

#if PLATFORM(IOS_FAMILY)
@interface CADisplay : NSObject
@end

@interface CADisplayLink ()
@property (readonly, nonatomic) CFTimeInterval maximumRefreshRate;
@property (readonly, nonatomic) CADisplay *display;
@end
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ - (void)schedule;

@end

static void* displayRefreshRateObservationContext = &displayRefreshRateObservationContext;

@implementation WKDisplayLinkHandler

- (id)initWithDrawingAreaProxy:(WebKit::RemoteLayerTreeDrawingAreaProxy*)drawingAreaProxy
Expand All @@ -74,6 +76,7 @@ - (id)initWithDrawingAreaProxy:(WebKit::RemoteLayerTreeDrawingAreaProxy*)drawing
if (createDisplayLink) {
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkFired:)];
[_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
[_displayLink.display addObserver:self forKeyPath:@"refreshRate" options:NSKeyValueObservingOptionNew context:displayRefreshRateObservationContext];
_displayLink.paused = YES;

if (drawingAreaProxy && !drawingAreaProxy->page().preferences().preferPageRenderingUpdatesNear60FPSEnabled())
Expand Down Expand Up @@ -112,6 +115,7 @@ - (void)timerFired

- (void)invalidate
{
[_displayLink.display removeObserver:self forKeyPath:@"refreshRate" context:displayRefreshRateObservationContext];
[_displayLink invalidate];
_displayLink = nullptr;

Expand Down Expand Up @@ -139,6 +143,13 @@ - (void)pause
#endif
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (context != displayRefreshRateObservationContext)
return;
[self didChangeNominalFramesPerSecond];
}

- (WebCore::FramesPerSecond)nominalFramesPerSecond
{
auto& page = _drawingAreaProxy->page();
Expand All @@ -151,6 +162,13 @@ - (void)pause
return DisplayLinkFramesPerSecond;
}

- (void)didChangeNominalFramesPerSecond
{
Ref page = _drawingAreaProxy->page();
if (auto displayID = page->displayID())
page->windowScreenDidChange(*displayID);
}

@end

namespace WebKit {
Expand Down

0 comments on commit a33d2c7

Please sign in to comment.