aboutsummaryrefslogtreecommitdiffstats
path: root/src/native/jogl/MacOSXWindowSystemInterface.m
blob: 42d3d8e5d692e5f35738dfc4f2f68728663fbb5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#import <Cocoa/Cocoa.h>
#import <OpenGL/gl.h>
#import <OpenGL/CGLTypes.h>
#import <jni.h>
#import "ContextUpdater.h"

// see MacOSXPbufferGLContext.java createPbuffer
#define USE_GL_TEXTURE_RECTANGLE_EXT

#ifdef USE_GL_TEXTURE_RECTANGLE_EXT
    #ifndef GL_TEXTURE_RECTANGLE_EXT
            #define GL_TEXTURE_RECTANGLE_EXT 0x84F5
    #endif
#endif

// kCGLPFAColorFloat is equivalent to NSOpenGLPFAColorFloat, but the
// latter is only available on 10.4 and we need to compile under
// 10.3
#ifndef NSOpenGLPFAColorFloat
    #define NSOpenGLPFAColorFloat kCGLPFAColorFloat
#endif


typedef int Bool;

static Bool rendererInfoInitialized = false;
static int bufferDepthsLength = 17;
static int bufferDepths[] = {kCGL128Bit, kCGL96Bit, kCGL64Bit, kCGL48Bit, kCGL32Bit, kCGL24Bit, kCGL16Bit, kCGL12Bit, kCGL10Bit, kCGL8Bit, kCGL6Bit, kCGL5Bit, kCGL4Bit, kCGL3Bit, kCGL2Bit, kCGL1Bit, kCGL0Bit};
static int bufferDepthsBits[] = {128, 96, 64, 48, 32, 24, 16, 12, 10, 8, 6, 5, 4, 3, 2, 1, 0};
static int accRenderID = 0; // the ID of the accelerated renderer
static int maxColorSize = kCGL128Bit; // max depth of color buffer
static int maxDepthSize = kCGL128Bit; // max depth of depth buffer
static int maxAccumSize = kCGL128Bit; // max depth of accum buffer
static int maxStencilSize = kCGL128Bit; // max depth of stencil buffer
void getRendererInfo()
{
	if (rendererInfoInitialized == false)
	{
		rendererInfoInitialized = true;
		
		CGLRendererInfoObj info;
		long numRenderers = 0;
		CGLError err = CGLQueryRendererInfo(CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay), &info, &numRenderers);
		if (err == kCGLNoError)
		{
			CGLDescribeRenderer(info, 0, kCGLRPRendererCount, &numRenderers);
			long j;
			for (j=0; j<numRenderers; j++)
			{
				unsigned long accRenderer = 0;
				CGLDescribeRenderer (info, j, kCGLRPAccelerated, &accRenderer);
				if (accRenderer != 0)
				{
					// get the accelerated renderer ID
					CGLDescribeRenderer(info, j, kCGLRPRendererID, &accRenderID);
					
					// get the max color buffer depth
					unsigned long colorModes = 0;
					CGLDescribeRenderer(info, j, kCGLRPColorModes, &colorModes);
					int i;
					for (i=0; i<bufferDepthsLength; i++)
					{
						if ((colorModes & bufferDepths[i]) != 0)
						{
							maxColorSize = bufferDepthsBits[i];
							break;
						}
					}
					
					// get the max depth buffer depth
					unsigned long depthModes = 0;
					CGLDescribeRenderer(info, j, kCGLRPDepthModes, &depthModes);
					for (i=0; i<bufferDepthsLength; i++)
					{
						if ((depthModes & bufferDepths[i]) != 0)
						{
							maxDepthSize = bufferDepthsBits[i];
							break;
						}
					}
					
					// get the max accum buffer depth
					unsigned long accumModes = 0;
					CGLDescribeRenderer(info, j, kCGLRPAccumModes, &accumModes);
					for (i=0; i<bufferDepthsLength; i++)
					{
						if ((accumModes & bufferDepths[i]) != 0)
						{
							maxAccumSize = bufferDepthsBits[i];
							break;
						}
					}
					
					// get the max stencil buffer depth
					unsigned long stencilModes = 0;
					CGLDescribeRenderer(info, j, kCGLRPStencilModes, &stencilModes);
					for (i=0; i<bufferDepthsLength; i++)
					{
						if ((stencilModes & bufferDepths[i]) != 0)
						{
							maxStencilSize = bufferDepthsBits[i];
							break;
						}
					}
					
					break;
				}
			}
			//fprintf(stderr, "maxColorSize=%d\n", maxColorSize);
			//fprintf(stderr, "maxDepthSize=%d\n", maxDepthSize);
			//fprintf(stderr, "maxAccumSize=%d\n", maxAccumSize);
			//fprintf(stderr, "maxStencilSize=%d\n", maxStencilSize);
		}
		CGLDestroyRendererInfo (info);
	}
}

void* createContext(void* shareContext, void* view,
                    int doubleBuffer,
                    int stereo,
                    int redBits,
                    int greenBits,
                    int blueBits,
                    int alphaBits,
                    int depthBits,
                    int stencilBits,
                    int accumRedBits,
                    int accumGreenBits,
                    int accumBlueBits,
                    int accumAlphaBits,
                    int sampleBuffers,
                    int numSamples,
                    int pbuffer,
                    int floatingPoint,
                    int* viewNotReady)
{
  getRendererInfo();
  
  int colorSize = redBits + greenBits + blueBits;
  if (colorSize > maxColorSize) {
	colorSize = maxColorSize;
  }
  
  int accumSize = accumRedBits + accumGreenBits + accumBlueBits;
  if (accumSize > maxAccumSize) {
	accumSize = maxAccumSize;
  }
  
  if (depthBits > maxDepthSize) {
	depthBits = maxDepthSize;
  }
  
  if (stencilBits > maxStencilSize) {
	stencilBits = maxStencilSize;
  }
  
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

  NSOpenGLContext *nsChareCtx = (NSOpenGLContext*)shareContext;
  NSView *nsView = (NSView*)view;
	
  if (nsView != NULL) {
    Bool viewReady = true;
    
    if ([nsView lockFocusIfCanDraw] == NO) {
      viewReady = false;
    } else {
      NSRect frame = [nsView frame];
      if ((frame.size.width == 0) || (frame.size.height == 0)) {
        [nsView unlockFocus];		
        viewReady = false;
      }
    }

    if (!viewReady) {
      if (viewNotReady != NULL) {
        *viewNotReady = 1;
      }
            
      // the view is not ready yet
      [pool release];
      return NULL;
    }
  }

  NSOpenGLPixelFormatAttribute attribs[256];
  int idx = 0;
  if (pbuffer)       attribs[idx++] = NSOpenGLPFAPixelBuffer;
  if (floatingPoint) attribs[idx++] = NSOpenGLPFAColorFloat;
  if (doubleBuffer)  attribs[idx++] = NSOpenGLPFADoubleBuffer;
  if (stereo)        attribs[idx++] = NSOpenGLPFAStereo;
  attribs[idx++] = NSOpenGLPFAColorSize;     attribs[idx++] = colorSize;
  attribs[idx++] = NSOpenGLPFAAlphaSize;     attribs[idx++] = alphaBits;
  attribs[idx++] = NSOpenGLPFADepthSize;     attribs[idx++] = depthBits;
  attribs[idx++] = NSOpenGLPFAStencilSize;   attribs[idx++] = stencilBits;
  attribs[idx++] = NSOpenGLPFAAccumSize;     attribs[idx++] = accumSize;
  if (sampleBuffers != 0) {
    attribs[idx++] = NSOpenGLPFASampleBuffers; attribs[idx++] = sampleBuffers;
    attribs[idx++] = NSOpenGLPFASamples;       attribs[idx++] = numSamples;
  }
  attribs[idx++] = 0;
	
  NSOpenGLPixelFormat* fmt = [[NSOpenGLPixelFormat alloc]
                               initWithAttributes:attribs];
  NSOpenGLContext* nsContext = [[NSOpenGLContext alloc]
                                 initWithFormat:fmt
                                 shareContext:nsChareCtx];
  [fmt release];
        
  if (nsView != nil) {
    [nsContext setView:nsView];
    [nsView unlockFocus];		
  }

  [pool release];
  return nsContext;
}

Bool makeCurrentContext(void* context, void* view) {
  NSOpenGLContext *nsContext = (NSOpenGLContext*)context;
	
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  [nsContext makeCurrentContext];
  [pool release];
  return true;
}

Bool clearCurrentContext(void* context, void* view) {
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  [NSOpenGLContext clearCurrentContext];
  [pool release];
  return true;
}

Bool deleteContext(void* context, void* view) {
  NSOpenGLContext *nsContext = (NSOpenGLContext*)context;
	
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  [nsContext clearDrawable];
  [nsContext release];
  [pool release];
  return true;
}

Bool flushBuffer(void* context, void* view) {
  NSOpenGLContext *nsContext = (NSOpenGLContext*)context;
	
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  [nsContext flushBuffer];
  [pool release];
  return true;
}

void updateContext(void* context, void* view) {
  NSOpenGLContext *nsContext = (NSOpenGLContext*)context;
	
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  [nsContext update];
  [pool release];
}

void* updateContextRegister(void* context, void* view) {
  NSOpenGLContext *nsContext = (NSOpenGLContext*)context;
  NSView *nsView = (NSView*)view;
	
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  ContextUpdater *contextUpdater = [[ContextUpdater alloc] init];
  [contextUpdater registerFor:nsContext with:nsView];
  [pool release];
  return NULL;
}

void updateContextUnregister(void* context, void* view, void* updater) {
  ContextUpdater *contextUpdater = (ContextUpdater *)updater;
	
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  [contextUpdater release];
  [pool release];
}

void* createPBuffer(int renderTarget, int internalFormat, int width, int height) {
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  NSOpenGLPixelBuffer* pBuffer = [[NSOpenGLPixelBuffer alloc]
                                   initWithTextureTarget:renderTarget
                                   textureInternalFormat:internalFormat
                                   textureMaxMipMapLevel:0
                                   pixelsWide:width
                                   pixelsHigh:height];
  [pool release];
  return pBuffer;
}

Bool destroyPBuffer(void* context, void* buffer) {
  /* FIXME: not clear whether we need to perform the clearDrawable below */
  /* FIXME: remove the context argument -- don't need it any more */
  /*  NSOpenGLContext *nsContext = (NSOpenGLContext*)context; */
  NSOpenGLPixelBuffer *pBuffer = (NSOpenGLPixelBuffer*)buffer;
	
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  /*
  if (nsContext != NULL) {
    [nsContext clearDrawable];
  }
  */
  [pBuffer release];
  [pool release];
	
  return true;
}

void setContextPBuffer(void* context, void* buffer) {
  NSOpenGLContext *nsContext = (NSOpenGLContext*)context;
  NSOpenGLPixelBuffer *pBuffer = (NSOpenGLPixelBuffer*)buffer;

  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  [nsContext setPixelBuffer: pBuffer
             cubeMapFace: 0
             mipMapLevel: 0
             currentVirtualScreen: [nsContext currentVirtualScreen]];
  [pool release];
}

void setContextTextureImageToPBuffer(void* context, void* buffer, int colorBuffer) {
  NSOpenGLContext *nsContext = (NSOpenGLContext*)context;
  NSOpenGLPixelBuffer *pBuffer = (NSOpenGLPixelBuffer*)buffer;

  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  [nsContext setTextureImageToPixelBuffer: pBuffer
             colorBuffer: (unsigned long) colorBuffer];
  [pool release];
}

#include <mach-o/dyld.h>
Bool imagesInitialized = false;
static char libGLStr[] = "/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib";
static char libGLUStr[] = "/System/Library/Frameworks/OpenGL.framework/Libraries/libGLU.dylib";
static const struct mach_header *libGLImage;
static const struct mach_header *libGLUImage;
void* getProcAddress(const char *procname) {
  if (imagesInitialized == false) {
    imagesInitialized = true;
    unsigned long options = NSADDIMAGE_OPTION_RETURN_ON_ERROR;
    libGLImage = NSAddImage(libGLStr, options);
    libGLUImage = NSAddImage(libGLUStr, options);
  }
	
  unsigned long options = NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR;
  char underscoreName[512] = "_";
  strcat(underscoreName, procname);
	
  if (NSIsSymbolNameDefinedInImage(libGLImage, underscoreName) == YES) {
    NSSymbol sym = NSLookupSymbolInImage(libGLImage, underscoreName, options);
    return NSAddressOfSymbol(sym);
  }
	
  if (NSIsSymbolNameDefinedInImage(libGLUImage, underscoreName) == YES)	{
    NSSymbol sym = NSLookupSymbolInImage(libGLUImage, underscoreName, options);
    return NSAddressOfSymbol(sym);
  }
	
  if (NSIsSymbolNameDefinedWithHint(underscoreName, "GL")) {
    NSSymbol sym = NSLookupAndBindSymbol(underscoreName);
    return NSAddressOfSymbol(sym);
  }
  
  return NULL;
}

void setSwapInterval(void* context, int interval) {
  NSOpenGLContext *nsContext = (NSOpenGLContext*)context;
  long swapInterval = interval;
  [nsContext setValues: &swapInterval forParameter: NSOpenGLCPSwapInterval];
}