diff mbox series

[v2,RFT] cocoa: Remove deprecated CVDisplayLinkCreateWithCGDisplay() calls

Message ID 20241106175051.561352-2-pbonzini@redhat.com (mailing list archive)
State New
Headers show
Series [v2,RFT] cocoa: Remove deprecated CVDisplayLinkCreateWithCGDisplay() calls | expand

Commit Message

Paolo Bonzini Nov. 6, 2024, 5:50 p.m. UTC
When building on macOS 15 we get:

../../ui/cocoa.m:662:14: error: 'CVDisplayLinkCreateWithCGDisplay' is deprecated:
    first deprecated in macOS 15.0
    - use NSView.displayLink(target:selector:), NSWindow.displayLink(target:selector:),
      or NSScreen.displayLink(target:selector:)
    [-Werror,-Wdeprecated-declarations]
  662 |         if (!CVDisplayLinkCreateWithCGDisplay(display, &displayLink)) {
      |              ^

Instead get the refresh rate from either CGDisplayModeGetRefreshRate or IOKit,
following the model of https://github.com/gwm17/glfw/commit/4ec7daf3e92.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2575
Cc: Philippe Mathieu-Daudé <philmd@linaro.org>
Cc: Pierrick Bouvier <pierrick.bouvier@linaro.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
v1->v2: use kIOMainPortDefault

 meson.build |  2 +-
 ui/cocoa.m  | 90 ++++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 79 insertions(+), 13 deletions(-)

Comments

Philippe Mathieu-Daudé Nov. 6, 2024, 10:59 p.m. UTC | #1
+Phil & Akihiko

On 6/11/24 17:50, Paolo Bonzini wrote:
> When building on macOS 15 we get:
> 
> ../../ui/cocoa.m:662:14: error: 'CVDisplayLinkCreateWithCGDisplay' is deprecated:
>      first deprecated in macOS 15.0
>      - use NSView.displayLink(target:selector:), NSWindow.displayLink(target:selector:),
>        or NSScreen.displayLink(target:selector:)
>      [-Werror,-Wdeprecated-declarations]
>    662 |         if (!CVDisplayLinkCreateWithCGDisplay(display, &displayLink)) {
>        |              ^
> 
> Instead get the refresh rate from either CGDisplayModeGetRefreshRate or IOKit,
> following the model of https://github.com/gwm17/glfw/commit/4ec7daf3e92.
> 
> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2575
> Cc: Philippe Mathieu-Daudé <philmd@linaro.org>
> Cc: Pierrick Bouvier <pierrick.bouvier@linaro.org>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
> v1->v2: use kIOMainPortDefault
> 
>   meson.build |  2 +-
>   ui/cocoa.m  | 90 ++++++++++++++++++++++++++++++++++++++++++++++-------
>   2 files changed, 79 insertions(+), 13 deletions(-)
> 
> diff --git a/meson.build b/meson.build
> index c386593c527..b12ccc12223 100644
> --- a/meson.build
> +++ b/meson.build
> @@ -1135,7 +1135,7 @@ if get_option('attr').allowed()
>   endif
>   
>   cocoa = dependency('appleframeworks',
> -                   modules: ['Cocoa', 'CoreVideo', 'QuartzCore'],
> +                   modules: ['Cocoa', 'IOKit', 'QuartzCore'],
>                      required: get_option('cocoa'))
>   
>   vmnet = dependency('appleframeworks', modules: 'vmnet', required: get_option('vmnet'))
> diff --git a/ui/cocoa.m b/ui/cocoa.m
> index 4c2dd335323..c3fa55477fd 100644
> --- a/ui/cocoa.m
> +++ b/ui/cocoa.m
> @@ -25,6 +25,7 @@
>   #include "qemu/osdep.h"
>   
>   #import <Cocoa/Cocoa.h>
> +#import <IOKit/IOKitLib.h>
>   #import <QuartzCore/QuartzCore.h>
>   #include <crt_externs.h>
>   
> @@ -292,6 +293,75 @@ static void handleAnyDeviceErrors(Error * err)
>       }
>   }
>   
> +static bool get_fallback_refresh_rate(CGDirectDisplayID displayID, double *p_rate)
> +{
> +    bool found = false;
> +    io_iterator_t it;
> +    io_service_t service;
> +    CFNumberRef indexRef, clockRef, countRef;
> +    uint32_t clock, count;
> +
> +    if (IOServiceGetMatchingServices(kIOMainPortDefault,
> +                                     IOServiceMatching("IOFramebuffer"),
> +                                     &it) != 0) {
> +        return false;
> +    }
> +    while ((service = IOIteratorNext(it)) != 0) {
> +        uint32_t index;
> +        bool found_display_id;
> +        indexRef = IORegistryEntryCreateCFProperty(service,
> +                                                   CFSTR("IOFramebufferOpenGLIndex"),
> +                                                   kCFAllocatorDefault,
> +                                                   kNilOptions);
> +        if (!indexRef) {
> +            continue;
> +        }
> +        found_display_id =
> +            CFNumberGetValue(indexRef, kCFNumberIntType, &index) &&
> +            CGOpenGLDisplayMaskToDisplayID(1 << index) == displayID;
> +        CFRelease(indexRef);
> +        if (found_display_id) {
> +            break;
> +        }
> +    }
> +    if (!service) {
> +        goto out;
> +    }
> +
> +    clockRef = IORegistryEntryCreateCFProperty(service,
> +                                               CFSTR("IOFBCurrentPixelClock"),
> +                                               kCFAllocatorDefault,
> +                                               kNilOptions);
> +    if (!clockRef) {
> +        goto out;
> +    }
> +    if (!CFNumberGetValue(clockRef, kCFNumberIntType, &clock) || !clock) {
> +        goto out_clock_ref;
> +    }
> +
> +    countRef = IORegistryEntryCreateCFProperty(service,
> +                                               CFSTR("IOFBCurrentPixelCount"),
> +                                               kCFAllocatorDefault,
> +                                               kNilOptions);
> +    if (!countRef) {
> +        goto out_clock_ref;
> +    }
> +    if (!CFNumberGetValue(countRef, kCFNumberIntType, &count) || !count) {
> +        goto out_count_ref;
> +    }
> +
> +    *p_rate = clock / (double) count;
> +    found = true;
> +
> +out_count_ref:
> +    CFRelease(countRef);
> +out_clock_ref:
> +    CFRelease(clockRef);
> +out:
> +    IOObjectRelease(it);
> +    return found;
> +}
> +
>   /*
>    ------------------------------------------------------
>       QemuCocoaView
> @@ -655,20 +725,16 @@ - (void) updateUIInfoLocked
>           NSSize screenSize = [[[self window] screen] frame].size;
>           CGSize screenPhysicalSize = CGDisplayScreenSize(display);
>           bool isFullscreen = ([[self window] styleMask] & NSWindowStyleMaskFullScreen) != 0;
> -        CVDisplayLinkRef displayLink;
> +        CGDisplayModeRef mode = CGDisplayCopyDisplayMode(display);
> +        double rate = CGDisplayModeGetRefreshRate(mode);
> +
> +        if (rate != 0.0 || get_fallback_refresh_rate(display, &rate)) {
> +            update_displaychangelistener(&dcl, 1000 / rate);
> +            info.refresh_rate = (int64_t)1000 * rate;
> +        }
> +        CGDisplayModeRelease(mode);
>   
>           frameSize = isFullscreen ? [self screenSafeAreaSize] : [self frame].size;
> -
> -        if (!CVDisplayLinkCreateWithCGDisplay(display, &displayLink)) {
> -            CVTime period = CVDisplayLinkGetNominalOutputVideoRefreshPeriod(displayLink);
> -            CVDisplayLinkRelease(displayLink);
> -            if (!(period.flags & kCVTimeIsIndefinite)) {
> -                update_displaychangelistener(&dcl,
> -                                             1000 * period.timeValue / period.timeScale);
> -                info.refresh_rate = (int64_t)1000 * period.timeScale / period.timeValue;
> -            }
> -        }
> -
>           info.width_mm = frameSize.width / screenSize.width * screenPhysicalSize.width;
>           info.height_mm = frameSize.height / screenSize.height * screenPhysicalSize.height;
>       } else {
Akihiko Odaki Nov. 7, 2024, 4:59 a.m. UTC | #2
On 2024/11/07 7:59, Philippe Mathieu-Daudé wrote:
> +Phil & Akihiko
> 
> On 6/11/24 17:50, Paolo Bonzini wrote:
>> When building on macOS 15 we get:
>>
>> ../../ui/cocoa.m:662:14: error: 'CVDisplayLinkCreateWithCGDisplay' is 
>> deprecated:
>>      first deprecated in macOS 15.0
>>      - use NSView.displayLink(target:selector:), 
>> NSWindow.displayLink(target:selector:),
>>        or NSScreen.displayLink(target:selector:)
>>      [-Werror,-Wdeprecated-declarations]
>>    662 |         if (!CVDisplayLinkCreateWithCGDisplay(display, 
>> &displayLink)) {
>>        |              ^
>>
>> Instead get the refresh rate from either CGDisplayModeGetRefreshRate 
>> or IOKit,
>> following the model of https://github.com/gwm17/glfw/commit/4ec7daf3e92.

It looks complicated. We can use [-NSScreen maximumFramesPerSecond] and 
[-NSScreen minimumRefreshInterval] instead as they are available since 
macOS 12.0 and cover all versions we support.

>>
>> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2575
>> Cc: Philippe Mathieu-Daudé <philmd@linaro.org>
>> Cc: Pierrick Bouvier <pierrick.bouvier@linaro.org>
>> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
>> ---
>> v1->v2: use kIOMainPortDefault
>>
>>   meson.build |  2 +-
>>   ui/cocoa.m  | 90 ++++++++++++++++++++++++++++++++++++++++++++++-------
>>   2 files changed, 79 insertions(+), 13 deletions(-)
>>
>> diff --git a/meson.build b/meson.build
>> index c386593c527..b12ccc12223 100644
>> --- a/meson.build
>> +++ b/meson.build
>> @@ -1135,7 +1135,7 @@ if get_option('attr').allowed()
>>   endif
>>   cocoa = dependency('appleframeworks',
>> -                   modules: ['Cocoa', 'CoreVideo', 'QuartzCore'],
>> +                   modules: ['Cocoa', 'IOKit', 'QuartzCore'],
>>                      required: get_option('cocoa'))
>>   vmnet = dependency('appleframeworks', modules: 'vmnet', required: 
>> get_option('vmnet'))
>> diff --git a/ui/cocoa.m b/ui/cocoa.m
>> index 4c2dd335323..c3fa55477fd 100644
>> --- a/ui/cocoa.m
>> +++ b/ui/cocoa.m
>> @@ -25,6 +25,7 @@
>>   #include "qemu/osdep.h"
>>   #import <Cocoa/Cocoa.h>
>> +#import <IOKit/IOKitLib.h>
>>   #import <QuartzCore/QuartzCore.h>
>>   #include <crt_externs.h>
>> @@ -292,6 +293,75 @@ static void handleAnyDeviceErrors(Error * err)
>>       }
>>   }
>> +static bool get_fallback_refresh_rate(CGDirectDisplayID displayID, 
>> double *p_rate)
>> +{
>> +    bool found = false;
>> +    io_iterator_t it;
>> +    io_service_t service;
>> +    CFNumberRef indexRef, clockRef, countRef;
>> +    uint32_t clock, count;
>> +
>> +    if (IOServiceGetMatchingServices(kIOMainPortDefault,
>> +                                     IOServiceMatching("IOFramebuffer"),
>> +                                     &it) != 0) {
>> +        return false;
>> +    }
>> +    while ((service = IOIteratorNext(it)) != 0) {
>> +        uint32_t index;
>> +        bool found_display_id;
>> +        indexRef = IORegistryEntryCreateCFProperty(service,
>> +                                                   
>> CFSTR("IOFramebufferOpenGLIndex"),
>> +                                                   kCFAllocatorDefault,
>> +                                                   kNilOptions);
>> +        if (!indexRef) {
>> +            continue;
>> +        }
>> +        found_display_id =
>> +            CFNumberGetValue(indexRef, kCFNumberIntType, &index) &&
>> +            CGOpenGLDisplayMaskToDisplayID(1 << index) == displayID;
>> +        CFRelease(indexRef);
>> +        if (found_display_id) {
>> +            break;
>> +        }
>> +    }
>> +    if (!service) {
>> +        goto out;
>> +    }
>> +
>> +    clockRef = IORegistryEntryCreateCFProperty(service,
>> +                                               
>> CFSTR("IOFBCurrentPixelClock"),
>> +                                               kCFAllocatorDefault,
>> +                                               kNilOptions);
>> +    if (!clockRef) {
>> +        goto out;
>> +    }
>> +    if (!CFNumberGetValue(clockRef, kCFNumberIntType, &clock) || ! 
>> clock) {
>> +        goto out_clock_ref;
>> +    }
>> +
>> +    countRef = IORegistryEntryCreateCFProperty(service,
>> +                                               
>> CFSTR("IOFBCurrentPixelCount"),
>> +                                               kCFAllocatorDefault,
>> +                                               kNilOptions);
>> +    if (!countRef) {
>> +        goto out_clock_ref;
>> +    }
>> +    if (!CFNumberGetValue(countRef, kCFNumberIntType, &count) || ! 
>> count) {
>> +        goto out_count_ref;
>> +    }
>> +
>> +    *p_rate = clock / (double) count;
>> +    found = true;
>> +
>> +out_count_ref:
>> +    CFRelease(countRef);
>> +out_clock_ref:
>> +    CFRelease(clockRef);
>> +out:
>> +    IOObjectRelease(it);
>> +    return found;
>> +}
>> +
>>   /*
>>    ------------------------------------------------------
>>       QemuCocoaView
>> @@ -655,20 +725,16 @@ - (void) updateUIInfoLocked
>>           NSSize screenSize = [[[self window] screen] frame].size;
>>           CGSize screenPhysicalSize = CGDisplayScreenSize(display);
>>           bool isFullscreen = ([[self window] styleMask] & 
>> NSWindowStyleMaskFullScreen) != 0;
>> -        CVDisplayLinkRef displayLink;
>> +        CGDisplayModeRef mode = CGDisplayCopyDisplayMode(display);
>> +        double rate = CGDisplayModeGetRefreshRate(mode);
>> +
>> +        if (rate != 0.0 || get_fallback_refresh_rate(display, &rate)) {
>> +            update_displaychangelistener(&dcl, 1000 / rate);
>> +            info.refresh_rate = (int64_t)1000 * rate;
>> +        }
>> +        CGDisplayModeRelease(mode);
>>           frameSize = isFullscreen ? [self screenSafeAreaSize] : [self 
>> frame].size;
>> -
>> -        if (!CVDisplayLinkCreateWithCGDisplay(display, &displayLink)) {
>> -            CVTime period = 
>> CVDisplayLinkGetNominalOutputVideoRefreshPeriod(displayLink);
>> -            CVDisplayLinkRelease(displayLink);
>> -            if (!(period.flags & kCVTimeIsIndefinite)) {
>> -                update_displaychangelistener(&dcl,
>> -                                             1000 * 
>> period.timeValue / period.timeScale);
>> -                info.refresh_rate = (int64_t)1000 * 
>> period.timeScale / period.timeValue;
>> -            }
>> -        }
>> -
>>           info.width_mm = frameSize.width / screenSize.width * 
>> screenPhysicalSize.width;
>>           info.height_mm = frameSize.height / screenSize.height * 
>> screenPhysicalSize.height;
>>       } else {
>
diff mbox series

Patch

diff --git a/meson.build b/meson.build
index c386593c527..b12ccc12223 100644
--- a/meson.build
+++ b/meson.build
@@ -1135,7 +1135,7 @@  if get_option('attr').allowed()
 endif
 
 cocoa = dependency('appleframeworks',
-                   modules: ['Cocoa', 'CoreVideo', 'QuartzCore'],
+                   modules: ['Cocoa', 'IOKit', 'QuartzCore'],
                    required: get_option('cocoa'))
 
 vmnet = dependency('appleframeworks', modules: 'vmnet', required: get_option('vmnet'))
diff --git a/ui/cocoa.m b/ui/cocoa.m
index 4c2dd335323..c3fa55477fd 100644
--- a/ui/cocoa.m
+++ b/ui/cocoa.m
@@ -25,6 +25,7 @@ 
 #include "qemu/osdep.h"
 
 #import <Cocoa/Cocoa.h>
+#import <IOKit/IOKitLib.h>
 #import <QuartzCore/QuartzCore.h>
 #include <crt_externs.h>
 
@@ -292,6 +293,75 @@  static void handleAnyDeviceErrors(Error * err)
     }
 }
 
+static bool get_fallback_refresh_rate(CGDirectDisplayID displayID, double *p_rate)
+{
+    bool found = false;
+    io_iterator_t it;
+    io_service_t service;
+    CFNumberRef indexRef, clockRef, countRef;
+    uint32_t clock, count;
+
+    if (IOServiceGetMatchingServices(kIOMainPortDefault,
+                                     IOServiceMatching("IOFramebuffer"),
+                                     &it) != 0) {
+        return false;
+    }
+    while ((service = IOIteratorNext(it)) != 0) {
+        uint32_t index;
+        bool found_display_id;
+        indexRef = IORegistryEntryCreateCFProperty(service,
+                                                   CFSTR("IOFramebufferOpenGLIndex"),
+                                                   kCFAllocatorDefault,
+                                                   kNilOptions);
+        if (!indexRef) {
+            continue;
+        }
+        found_display_id =
+            CFNumberGetValue(indexRef, kCFNumberIntType, &index) &&
+            CGOpenGLDisplayMaskToDisplayID(1 << index) == displayID;
+        CFRelease(indexRef);
+        if (found_display_id) {
+            break;
+        }
+    }
+    if (!service) {
+        goto out;
+    }
+
+    clockRef = IORegistryEntryCreateCFProperty(service,
+                                               CFSTR("IOFBCurrentPixelClock"),
+                                               kCFAllocatorDefault,
+                                               kNilOptions);
+    if (!clockRef) {
+        goto out;
+    }
+    if (!CFNumberGetValue(clockRef, kCFNumberIntType, &clock) || !clock) {
+        goto out_clock_ref;
+    }
+
+    countRef = IORegistryEntryCreateCFProperty(service,
+                                               CFSTR("IOFBCurrentPixelCount"),
+                                               kCFAllocatorDefault,
+                                               kNilOptions);
+    if (!countRef) {
+        goto out_clock_ref;
+    }
+    if (!CFNumberGetValue(countRef, kCFNumberIntType, &count) || !count) {
+        goto out_count_ref;
+    }
+
+    *p_rate = clock / (double) count;
+    found = true;
+
+out_count_ref:
+    CFRelease(countRef);
+out_clock_ref:
+    CFRelease(clockRef);
+out:
+    IOObjectRelease(it);
+    return found;
+}
+
 /*
  ------------------------------------------------------
     QemuCocoaView
@@ -655,20 +725,16 @@  - (void) updateUIInfoLocked
         NSSize screenSize = [[[self window] screen] frame].size;
         CGSize screenPhysicalSize = CGDisplayScreenSize(display);
         bool isFullscreen = ([[self window] styleMask] & NSWindowStyleMaskFullScreen) != 0;
-        CVDisplayLinkRef displayLink;
+        CGDisplayModeRef mode = CGDisplayCopyDisplayMode(display);
+        double rate = CGDisplayModeGetRefreshRate(mode);
+
+        if (rate != 0.0 || get_fallback_refresh_rate(display, &rate)) {
+            update_displaychangelistener(&dcl, 1000 / rate);
+            info.refresh_rate = (int64_t)1000 * rate;
+        }
+        CGDisplayModeRelease(mode);
 
         frameSize = isFullscreen ? [self screenSafeAreaSize] : [self frame].size;
-
-        if (!CVDisplayLinkCreateWithCGDisplay(display, &displayLink)) {
-            CVTime period = CVDisplayLinkGetNominalOutputVideoRefreshPeriod(displayLink);
-            CVDisplayLinkRelease(displayLink);
-            if (!(period.flags & kCVTimeIsIndefinite)) {
-                update_displaychangelistener(&dcl,
-                                             1000 * period.timeValue / period.timeScale);
-                info.refresh_rate = (int64_t)1000 * period.timeScale / period.timeValue;
-            }
-        }
-
         info.width_mm = frameSize.width / screenSize.width * screenPhysicalSize.width;
         info.height_mm = frameSize.height / screenSize.height * screenPhysicalSize.height;
     } else {