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
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <Cocoa/Cocoa.h>
#include <OpenGL/OpenGL.h>
#include "MacPixelFormatsAttribs.h"
//
// EmuGLContext inherit from NSOpenGLContext
// and adds binding state for the context to know
// if it was last bounded to a pbuffer or a window.
// This is because after the context was bounded to
// a Pbuffer, before we bind it to a window we must
// release it form the pbuffer by calling the
// clearDrawable method. We do not want to call clearDrawable
// more than really needed since when it is called at a time
// that a window is bounded to the context it will clear the
// window content causing flickering effect.
// Thererfore we call clearDrawable only when we bind the context
// to a window and it was previously bound to a Pbuffer.
//
@interface EmuGLContext : NSOpenGLContext {
@private
int boundToPbuffer;
int boundToWin;
}
- (id) initWithFormat:(NSOpenGLPixelFormat *)pixelFormat shareContext:(NSOpenGLContext *)share;
- (void) preBind:(int)forPbuffer;
@end
@implementation EmuGLContext
- (id) initWithFormat:(NSOpenGLPixelFormat *)pixelFormat shareContext:(NSOpenGLContext *)share
{
self = [super initWithFormat:pixelFormat shareContext:share];
if (self != nil) {
boundToPbuffer = 0;
boundToWin = 0;
}
return self;
}
- (void) preBind:(int)forPbuffer
{
if ((!forPbuffer && boundToPbuffer)) {
[self clearDrawable];
}
boundToPbuffer = forPbuffer;
boundToWin = !boundToPbuffer;
}
@end
int getNumPixelFormats(){
int size;
NSOpenGLPixelFormatAttribute** attrib_lists = getPixelFormatsAttributes(&size);
return size;
}
void* getPixelFormat(int i){
int size;
NSOpenGLPixelFormatAttribute** attrib_lists = getPixelFormatsAttributes(&size);
return [[NSOpenGLPixelFormat alloc] initWithAttributes:attrib_lists[i]];
}
void getPixelFormatAttrib(void* pixelFormat,int attrib,int* val){
NSOpenGLPixelFormat *frmt = (NSOpenGLPixelFormat *)pixelFormat;
[frmt getValues:val forAttribute:attrib forVirtualScreen:0];
}
void* nsCreateContext(void* format,void* share){
NSOpenGLPixelFormat* frmt = (NSOpenGLPixelFormat*)format;
return [[EmuGLContext alloc] initWithFormat:frmt shareContext:share];
}
void nsPBufferMakeCurrent(void* context,void* nativePBuffer,int level){
EmuGLContext* ctx = (EmuGLContext *)context;
NSOpenGLPixelBuffer* pbuff = (NSOpenGLPixelBuffer *)nativePBuffer;
if(ctx == nil){
[NSOpenGLContext clearCurrentContext];
} else {
if(pbuff != nil){
[ctx preBind:1];
[ctx setPixelBuffer:pbuff cubeMapFace:0 mipMapLevel:level currentVirtualScreen:0];
[ctx makeCurrentContext];
}
}
}
void nsWindowMakeCurrent(void* context,void* nativeWin){
EmuGLContext* ctx = (EmuGLContext *)context;
NSView* win = (NSView *)nativeWin;
if(ctx == nil){
[NSOpenGLContext clearCurrentContext];
} else if (win != nil) {
[ctx preBind:0];
[ctx setView: win];
[ctx makeCurrentContext];
}
}
void nsSwapBuffers(){
NSOpenGLContext* ctx = [NSOpenGLContext currentContext];
if(ctx != nil){
[ctx flushBuffer];
}
}
void nsSwapInterval(int *interval){
NSOpenGLContext* ctx = [NSOpenGLContext currentContext];
if( ctx != nil){
[ctx setValues:interval forParameter:NSOpenGLCPSwapInterval];
}
}
void nsDestroyContext(void* context){
EmuGLContext *ctx = (EmuGLContext*)context;
if(ctx != nil){
[ctx release];
}
}
void* nsCreatePBuffer(GLenum target,GLenum format,int maxMip,int width,int height){
return [[NSOpenGLPixelBuffer alloc] initWithTextureTarget:target
textureInternalFormat:format
textureMaxMipMapLevel:maxMip
pixelsWide:width pixelsHigh:height];
}
void nsDestroyPBuffer(void* pbuffer){
NSOpenGLPixelBuffer *pbuf = (NSOpenGLPixelBuffer*)pbuffer;
if(pbuf != nil){
[pbuf release];
}
}
bool nsGetWinDims(void* win,unsigned int* width,unsigned int* height){
NSView* view = (NSView*)win;
if(view != nil){
NSRect rect = [view bounds];
*width = rect.size.width;
*height = rect.size.height;
return true;
}
return false;
}
bool nsCheckColor(void* win,int colorSize){
NSView* view = (NSView*)win;
if(view != nil){
NSWindow* wnd = [view window];
if(wnd != nil){
NSWindowDepth limit = [wnd depthLimit];
NSWindowDepth defaultLimit = [NSWindow defaultDepthLimit];
int depth = (limit != 0) ? NSBitsPerPixelFromDepth(limit):
NSBitsPerPixelFromDepth(defaultLimit);
return depth >= colorSize;
}
}
return false;
}
|