Bug 43250 - NSOpenGLPixelFormat doesn't support NSOpenGLProfile argument
Summary: NSOpenGLPixelFormat doesn't support NSOpenGLProfile argument
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Library (Xamarin.Mac.dll) ()
Version: Master
Hardware: PC Mac OS
: Normal normal
Target Milestone: (C9)
Assignee: Timothy Risi
Depends on:
Reported: 2016-08-10 19:36 UTC by lineket
Modified: 2017-02-08 18:42 UTC (History)
7 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description lineket 2016-08-10 19:36:33 UTC
Hi there,
I'm trying to convert the following lines of objective c into c# using Xamarin.Mac.

NSOpenGLPixelFormatAttribute attributes[] = {
NSOpenGLPFADepthSize, 24,
NSOpenGLProfileVersion3_2Core, //problem 1
0 //problem 2
NSOpenGLPixelFormat* pixelFormat =
[[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];

The problem occurs on //problem1 and //problem2. Here are the conversion to c#.

var _attribs = new object [] { NSOpenGLPixelFormatAttribute.DoubleBuffer, 
NSOpenGLPixelFormatAttribute.DepthSize, 24, 
NSOpenGLProfile.Version3_2Core, 0 };
NSOpenGLPixelFormat pixelFormat = new NSOpenGLPixelFormat (_attribs);

At runtime Xamarin will try to cast my array of objects into NSOpenGLPixelFormatAttribute and because I have a NSOpenGLProfile it will fail with the following message: "Specified cast is invalid"


at AppKit.NSOpenGLPixelFormat.ConvertToAttributes (System.Object[] args) [0x0000f] in /Users/builder/data/lanes/3539/f37444ae/source/maccore/src/AppKit/NSOpenGLPixelFormat.cs:61 
at AppKit.NSOpenGLPixelFormat..ctor (System.Object[] attribs) [0x00002] in /Users/builder/data/lanes/3539/f37444ae/source/maccore/src/AppKit/NSOpenGLPixelFormat.cs:121 
at Stream.RTC.Shared.AppleMediaDevice.CreateVideoRenderer () [0x00047] in /Users/Shared/dev/Stream.RTC.Apple/AppleMediaDevice.cs:69 
at Stream.Services.Services.MediaDeviceBO.StartAsync (Boolean createRenderer) [0x00037] in /Users/Shared/dev/Stream.Services/Services/MediaDeviceBO.cs:109 
at Stream.End2EndTest.Common.ViewModel.WebCamViewModel`2[T,U].Open (Boolean createDefaultRenderer) [0x0000f] in /Users/Shared/dev/Stream.End2EndTest.Common/ViewModel/WebCamViewModel.cs:32 
at Stream.End2EndTest.Common.ViewModel.DeviceViewModel.DeviceEnableChanged () [0x0002e] in /Users/Shared/dev/Stream.End2EndTest.Common/ViewModel/DeviceViewModel.cs:80 
at Stream.End2EndTest.Common.ViewModel.DeviceViewModel.set_Enabled (Boolean value) [0x00021] in /Users/Shared/dev/Stream.End2EndTest.Common/ViewModel/DeviceViewModel.cs:42 
at Stream.End2EndTest.macOS.ViewController.m__0 () [0x00024] in /Users/Shared/dev/Stream.End2EndTest.macOS/ViewController.cs:50 
at Foundation.NSActionDispatcher.Apply () [0x00007] in /Users/builder/data/lanes/3539/f37444ae/source/maccore/src/Foundation/NSAction.cs:57 
at (wrapper managed-to-native) ObjCRuntime.Messaging:void_objc_msgSend_IntPtr_IntPtr_bool (intptr,intptr,intptr,intptr,bool)
at Foundation.NSObject.InvokeOnMainThread (System.Action action) [0x00025] in /Users/builder/data/lanes/3539/f37444ae/source/maccore/src/Foundation/NSObject2.cs:553 
at Stream.End2EndTest.macOS.ViewController.ActivateButton (Foundation.NSObject sender) [0x0000e] in /Users/Shared/dev/Stream.End2EndTest.macOS/ViewController.cs:47 
at (wrapper managed-to-native) AppKit.NSApplication:NSApplicationMain (int,string[])
at AppKit.NSApplication.Main (System.String[] args) [0x00041] in /Users/builder/data/lanes/3539/f37444ae/source/maccore/src/AppKit/NSApplication.cs:94 
at Stream.End2EndTest.macOS.MainClass.Main (System.String[] args) [0x00007] in /Users/Shared/dev/Stream.End2EndTest.macOS/Main.cs:10

Comment 1 Timothy Risi 2016-11-18 01:22:18 UTC

It looks like NSOpenGLPixelFormatAttribute and NSOpenGLProfile are both uint32 type enums.  In objective-c you can just use them as uints and pass them all together like you have in the sample code.  In C#, it considers the enums to be two different types so it doesn't work doing it exactly the same. 

If you cast the profile argument and the ints to NSOpenGLPixelFormatAttribute it will run successfully:

var _attribs = new object [] { NSOpenGLPixelFormatAttribute.DoubleBuffer,
				(NSOpenGLPixelFormatAttribute) 24,
				(NSOpenGLPixelFormatAttribute) NSOpenGLProfile.Version3_2Core, 
				(NSOpenGLPixelFormatAttribute) 0 };

Can you try that and see if it behaves as expected?
Comment 2 Chris Hamons 2016-11-18 15:31:31 UTC
We'll have to look at fixing the API (might not be able to until next API break) but ^ is a reasonable ish work around for now.
Comment 3 Sebastien Pouliot 2016-12-07 21:01:48 UTC
Not one of my favourite patterns but the type has a .ctor that accept this, i.e.

> public NSOpenGLPixelFormat (params object [] attribs)

and the rest is internal stuff so it seems this can be fixed without an API break, e.g. change

> static NSOpenGLPixelFormatAttribute [] ConvertToAttributes (object [] args)

to return `uint[]`.

OTOH it seems a better (additional) API would be to have something similar to the strong dictionaries, with a lot of `bool?` and a few other, strongly typed, properties.
Comment 4 Manuel de la Peña [MSFT] 2017-01-03 09:58:56 UTC
@Chamos any comments as per @Sebastien view?
Comment 5 Chris Hamons 2017-01-03 12:39:39 UTC
@Manuel - @risi has been handling this bug. Let's chat about this today later.
Comment 6 Timothy Risi 2017-02-01 22:08:49 UTC
Fixed on master - https://github.com/xamarin/xamarin-macios/pull/1539
Comment 8 Shruti 2017-02-08 05:11:01 UTC
Please fix the commit in Cycle 9.
Comment 9 Timothy Risi 2017-02-08 17:31:09 UTC
It's been merged into cycle9 as well: https://github.com/xamarin/xamarin-macios/pull/1613