UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
mac_gl_common.m
Go to the documentation of this file.
1
5 /*
6 * Copyright (c) 2012-2023 CESNET z.s.p.o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include "config.h"
39#include "config_unix.h"
40#include "debug.h"
41#include "host.h"
42
43#include "mac_gl_common.h"
44
45#include <Availability.h>
46#import <Cocoa/Cocoa.h>
47#include <OpenGL/gl.h>
48#include <sys/param.h>
49#include <sys/sysctl.h>
50#include <string.h>
51#include <stdlib.h>
52
53#ifndef __MAC_10_7
54#warning "You are compling on pre-10.7 Mac OS X version. Core OpenGL 3.2 profile won't work"
55#warning "in case you'll try to use this binary with Lion or higher (only legacy OpenGL)."
56#endif
57
58#define MAC_GL_MAGIC 0xa23f4f28u
59
60struct state_mac_gl;
61
63{
64 int mib[2];
65 size_t len;
66 char *kernelVersion;
67
68 // Get the kernel's version as a string called "kernelVersion":
69 mib[0] = CTL_KERN;
70 mib[1] = KERN_OSRELEASE;
71 sysctl(mib, 2, NULL, &len, NULL, 0);
72 kernelVersion = malloc(len * sizeof(char));
73 sysctl(mib, 2, kernelVersion, &len, NULL, 0);
74
75 return atoi(kernelVersion);
76}
77
78@interface UltraGridOpenGLView : NSOpenGLView
79{
80 NSWindow *window;
81 @public
82 NSOpenGLContext *context;
83}
84
85-(void) initialize: (struct state_mac_gl *) s;
86@end
87
89{
90 uint32_t magic;
92 NSAutoreleasePool *autoreleasePool;
93 NSOpenGLPixelFormat * pixFmt;
94};
95
97{
98 struct state_mac_gl *s;
99 NSOpenGLPixelFormatAttribute attrs[3];
100 int mac_version_major;
101
102 mac_version_major = get_mac_kernel_version_major();
103
104 if(ogl_version == MAC_GL_PROFILE_3_2) { /* Lion or later */
105#ifdef __MAC_10_7
106 if(mac_version_major < 11) {
107 fprintf(stderr, "[Mac OpenGL] Unable to activate OpenGL 3.2 for pre-Lion Macs.\n");
108 return NULL;
109 }
110 attrs[0] = kCGLPFAOpenGLProfile;
111 attrs[1] = kCGLOGLPVersion_3_2_Core; // kCGLOGLPVersion_Legacy;
112 attrs[2] = 0;
113#else
114 fprintf(stderr, "[Mac OpenGL] OpenGL 3.2 support was not compiled in! Did you compile with an older Mac?\n");
115 return NULL;
116#endif
117 } else if(ogl_version == MAC_GL_PROFILE_LEGACY) {
118#ifdef __MAC_10_7
119 if(mac_version_major >= 11) {
120 attrs[0] = kCGLPFAOpenGLProfile;
121 attrs[1] = kCGLOGLPVersion_Legacy; // kCGLOGLPVersion_Legacy;
122 attrs[2] = 0;
123 }
124#else
125 attrs[0] = 0;
126#endif
127 }
128
129 s = (struct state_mac_gl *) malloc(sizeof(struct state_mac_gl));
130 s->magic = MAC_GL_MAGIC;
131
132 NSApplicationLoad();
133
134 NSApp = [NSApplication sharedApplication];
135
136 s->autoreleasePool = [[NSAutoreleasePool alloc] init];
137
138 if(mac_version_major >= 11) {
139 s->pixFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes: attrs];
140 } else {
141 s->pixFmt = [NSOpenGLView defaultPixelFormat];
142 }
143
144 if(!s->pixFmt) {
145 fprintf(stderr, "[Mac OpenGL] Failed to acquire pixel format.\n");
146 return NULL;
147 }
148
149 s->view = [[UltraGridOpenGLView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100) pixelFormat: s->pixFmt];
150
151 [ s->view display];
152 [ s->view initialize: s];
153
154 return s;
155}
156
157void mac_gl_free(void * state)
158{
159 struct state_mac_gl *s = (struct state_mac_gl *) state;
160
161 [ s->view release ];
162 [ s->autoreleasePool release ];
163 free(s);
164}
165
167{
168 struct state_mac_gl *s = (struct state_mac_gl *) state;
169
170 if(state) {
171 [s->view->context makeCurrentContext];
172 } else {
173 [NSOpenGLContext clearCurrentContext];
174 }
175}
176
177@implementation UltraGridOpenGLView
178-(void) initialize: (struct state_mac_gl *) s
179{
180 window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 100, 100)
181#ifdef __MAC_10_12
182 styleMask:NSWindowStyleMaskBorderless
183#else
184 styleMask:NSBorderlessWindowMask
185#endif
186 backing:NSBackingStoreBuffered
187 defer:NO];
188 [window autorelease];
189 //[window setTitle:@"UltraGrid OpenGL view"];
190
191 context = [[NSOpenGLContext alloc] initWithFormat: s->pixFmt
192 shareContext:nil];
193 [self setOpenGLContext:context];
194 [context makeCurrentContext];
195 [context release];
196 printf("OpenGL renderer: %s %s (GLSL: %s)\n", glGetString(GL_RENDERER), glGetString(GL_VERSION), glGetString(GL_SHADING_LANGUAGE_VERSION));
197
198 [window setContentSize:NSMakeSize(100, 100)];
199}
200@end
201
void ** state
int void size_t * len
NSOpenGLContext * context
#define MAC_GL_PROFILE_LEGACY
#define MAC_GL_PROFILE_3_2
int mac_opengl_version_t
void * mac_gl_init(mac_opengl_version_t ogl_version)
void mac_gl_free(void *state)
int get_mac_kernel_version_major()
void mac_gl_make_current(void *state)
#define MAC_GL_MAGIC
static bool initialize(struct vidcap_screen_osx_state *s)
Definition screen_osx.c:108
UltraGridOpenGLView * view
NSOpenGLPixelFormat * pixFmt
NSAutoreleasePool * autoreleasePool
uint32_t magic