glgraphics.macos.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #include <AppKit/AppKit.h>
  2. #include <Carbon/Carbon.h>
  3. #include <OpenGL/gl.h>
  4. #include <OpenGL/OpenGL.h>
  5. #include <brl.mod/systemdefault.mod/system.h>
  6. enum{
  7. FLAGS_BACKBUFFER= 0x2,
  8. FLAGS_ALPHABUFFER= 0x4,
  9. FLAGS_DEPTHBUFFER= 0x8,
  10. FLAGS_STENCILBUFFER=0x10,
  11. FLAGS_ACCUMBUFFER= 0x20,
  12. FLAGS_BORDERLESS= 0x40,
  13. FLAGS_FULLSCREEN_DESKTOP= 0x80,
  14. FLAGS_FULLSCREEN= 0x80000000
  15. };
  16. enum{
  17. MODE_WIDGET= 1,
  18. MODE_WINDOW= 2,
  19. MODE_DISPLAY= 3
  20. };
  21. @interface BBGLWindow : NSWindow{
  22. }
  23. @end
  24. @implementation BBGLWindow
  25. -(void)sendEvent:(NSEvent*)event{
  26. bbSystemEmitOSEvent( event,[self contentView],&bbNullObject );
  27. switch( [event type] ){
  28. case NSKeyDown:case NSKeyUp:
  29. //prevent 'beeps'!
  30. return;
  31. }
  32. [super sendEvent:event];
  33. }
  34. -(BOOL)windowShouldClose:(id)sender{
  35. bbSystemEmitEvent( BBEVENT_APPTERMINATE,&bbNullObject,0,0,0,0,&bbNullObject );
  36. return NO;
  37. }
  38. - (BOOL)canBecomeKeyWindow{
  39. return YES;
  40. }
  41. @end
  42. typedef struct BBGLContext BBGLContext;
  43. struct BBGLContext{
  44. int mode,width,height,depth,hertz;
  45. BBInt64 flags;
  46. int sync;
  47. NSView *view;
  48. BBGLWindow *window;
  49. NSOpenGLContext *glContext;
  50. };
  51. static BBGLContext *_currentContext;
  52. static BBGLContext *_displayContext;
  53. static CFDictionaryRef oldDisplayMode;
  54. extern void bbFlushAutoreleasePool();
  55. void bbGLGraphicsClose( BBGLContext *context );
  56. void bbGLGraphicsGetSettings( BBGLContext *context,int *width,int *height,int *depth,int *hertz,BBInt64 *flags );
  57. void bbGLGraphicsSetGraphics( BBGLContext *context );
  58. static int _initAttrs( CGLPixelFormatAttribute attrs[16],BBInt64 flags ){
  59. int n=0;
  60. if( flags & FLAGS_BACKBUFFER ) attrs[n++]=kCGLPFADoubleBuffer;
  61. if( flags & FLAGS_ALPHABUFFER ){ attrs[n++]=kCGLPFAAlphaSize;attrs[n++]=1; }
  62. if( flags & FLAGS_DEPTHBUFFER ){ attrs[n++]=kCGLPFADepthSize;attrs[n++]=24; }
  63. if( flags & FLAGS_STENCILBUFFER ){ attrs[n++]=kCGLPFAStencilSize;attrs[n++]=1; }
  64. if( flags & FLAGS_ACCUMBUFFER ){ attrs[n++]=kCGLPFAAccumSize;attrs[n++]=1; }
  65. if( flags & FLAGS_FULLSCREEN ){
  66. attrs[n++]=kCGLPFAFullScreen;
  67. attrs[n++]=kCGLPFADisplayMask;
  68. attrs[n++]=CGDisplayIDToOpenGLDisplayMask( kCGDirectMainDisplay );
  69. }else{
  70. attrs[n++]=kCGLPFANoRecovery;
  71. }
  72. attrs[n]=0;
  73. return n;
  74. }
  75. static NSOpenGLContext *_sharedContext;
  76. static void _validateSize( BBGLContext *context ){
  77. NSRect rect;
  78. if( !context || context->mode!=MODE_WIDGET ) return;
  79. rect=[context->view bounds];
  80. if( rect.size.width==context->width && rect.size.height==context->height ) return;
  81. context->width=rect.size.width;
  82. context->height=rect.size.height;
  83. if( context->glContext ) [context->glContext update];
  84. }
  85. static void _validateContext( BBGLContext *context ){
  86. BBInt64 flags;
  87. NSOpenGLContext *shared;
  88. NSOpenGLContext *glContext;
  89. NSOpenGLPixelFormat *glFormat;
  90. CGLPixelFormatAttribute attrs[16];
  91. if( !context || context->glContext ) return;
  92. flags=context->flags;
  93. // if( context->mode==MODE_DISPLAY ) flags|=FLAGS_FULLSCREEN;
  94. _initAttrs( attrs,flags );
  95. glFormat=[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
  96. glContext=[[NSOpenGLContext alloc] initWithFormat:glFormat shareContext:_sharedContext];
  97. [glFormat release];
  98. if( !glContext ) bbExThrowCString( "Unable to create GL Context" );
  99. switch( context->mode ){
  100. case MODE_WIDGET:
  101. [glContext setView:context->view];
  102. break;
  103. case MODE_WINDOW:
  104. case MODE_DISPLAY:
  105. [glContext setView:[context->window contentView]];
  106. break;
  107. }
  108. context->glContext=glContext;
  109. }
  110. void bbGLGraphicsShareContexts(){
  111. NSOpenGLPixelFormat *glFormat;
  112. CGLPixelFormatAttribute attrs[16];
  113. if( _sharedContext ) return;
  114. _initAttrs( attrs,0 );
  115. glFormat=[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
  116. _sharedContext=[[NSOpenGLContext alloc] initWithFormat:glFormat shareContext:0];
  117. [glFormat release];
  118. }
  119. int bbGLGraphicsGraphicsModes( int *modes,int count ){
  120. int i=0,n=0,sz;
  121. CFArrayRef displayModeArray;
  122. displayModeArray = CGDisplayCopyAllDisplayModes(kCGDirectMainDisplay, NULL);
  123. sz=CFArrayGetCount( displayModeArray );
  124. while( i<sz && n<count ){
  125. CGDisplayModeRef displayMode;
  126. int width,height,depth,hertz;
  127. CFStringRef format;
  128. displayMode=(CGDisplayModeRef)CFArrayGetValueAtIndex( displayModeArray,i++ );
  129. format = CGDisplayModeCopyPixelEncoding(displayMode);
  130. width = CGDisplayModeGetWidth(displayMode);
  131. height = CGDisplayModeGetHeight(displayMode);
  132. hertz = (CGDisplayModeGetRefreshRate(displayMode) + 0.5);
  133. if (CFStringCompare(format, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
  134. depth = 32;
  135. } else if (CFStringCompare(format, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
  136. depth = 16;
  137. } else if (CFStringCompare(format, CFSTR(kIO30BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
  138. depth = 30;
  139. } else if (CFStringCompare(format, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) {
  140. depth = 8;
  141. } else {
  142. depth = 0;
  143. }
  144. CFRelease(format);
  145. if (hertz == 0) {
  146. CVDisplayLinkRef link = NULL;
  147. CVDisplayLinkCreateWithCGDisplay(kCGDirectMainDisplay, &link);
  148. if (link != NULL) {
  149. CVTime time = CVDisplayLinkGetNominalOutputVideoRefreshPeriod(link);
  150. if ((time.flags & kCVTimeIsIndefinite) == 0 && time.timeValue != 0) {
  151. hertz = (long) ((time.timeScale / (double) time.timeValue) + 0.5);
  152. }
  153. }
  154. }
  155. *modes++=width;
  156. *modes++=height;
  157. *modes++=depth;
  158. *modes++=hertz;
  159. ++n;
  160. }
  161. return n;
  162. }
  163. BBGLContext *bbGLGraphicsAttachGraphics( NSView *view,BBInt64 flags ){
  164. NSRect rect;
  165. BBGLContext *context;
  166. rect=[view bounds];
  167. context=(BBGLContext*)malloc( sizeof(BBGLContext) );
  168. memset( context,0,sizeof(BBGLContext) );
  169. context->mode=MODE_WIDGET;
  170. context->width=rect.size.width;
  171. context->height=rect.size.height;
  172. context->flags=flags;
  173. context->sync=-1;
  174. context->view=view;
  175. return context;
  176. }
  177. BBGLContext *bbGLGraphicsCreateGraphics( int width,int height,int depth,int hertz,BBInt64 flags, int x, int y ){
  178. int mode;
  179. BBGLWindow *window=0;
  180. BBGLContext *context;
  181. int sysv=0;
  182. Gestalt( 'sysv',&sysv );
  183. if( depth ){
  184. CFDictionaryRef displayMode;
  185. CGCaptureAllDisplays();
  186. oldDisplayMode=CGDisplayCurrentMode( kCGDirectMainDisplay );
  187. CFRetain( (CFTypeRef)oldDisplayMode );
  188. if( !hertz ){
  189. displayMode=CGDisplayBestModeForParameters( kCGDirectMainDisplay,depth,width,height,0 );
  190. }else{
  191. displayMode=CGDisplayBestModeForParametersAndRefreshRate( kCGDirectMainDisplay,depth,width,height,hertz,0 );
  192. }
  193. if( CGDisplaySwitchToMode( kCGDirectMainDisplay,displayMode ) ){
  194. CFRelease( (CFTypeRef)oldDisplayMode );
  195. bbExThrowCString( "Unable to set display mode" );
  196. }
  197. #if MAC_OS_X_VERSION_MAX_ALLOWED < 101500
  198. HideMenuBar();
  199. #endif
  200. window=[[NSWindow alloc]
  201. initWithContentRect:NSMakeRect( 0,0,width,height )
  202. styleMask:NSBorderlessWindowMask
  203. backing:NSBackingStoreBuffered
  204. willUseFullScreenPresentationOptions:NSApplicationPresentationAutoHideToolbar | NSApplicationPresentationAutoHideMenuBar | NSApplicationPresentationFullScreen
  205. defer:YES];
  206. [window setOpaque:YES];
  207. [window setBackgroundColor:[NSColor blackColor]];
  208. [window setLevel:CGShieldingWindowLevel()];
  209. [window makeKeyAndOrderFront:NSApp];
  210. mode=MODE_DISPLAY;
  211. }else{
  212. if (x < 0) x = 0;
  213. if (y < 0) y = 0;
  214. NSWindowStyleMask mask = (flags & FLAGS_BORDERLESS) ? 0 : NSTitledWindowMask|NSClosableWindowMask;
  215. if (flags & FLAGS_FULLSCREEN_DESKTOP) mask |= NSWindowStyleMaskFullScreen;
  216. window=[[BBGLWindow alloc]
  217. initWithContentRect:NSMakeRect( x, y,width,height )
  218. styleMask:mask
  219. backing:NSBackingStoreBuffered
  220. defer:YES];
  221. if( !window ) return 0;
  222. [window setDelegate:window];
  223. [window setAcceptsMouseMovedEvents:YES];
  224. char *p=bbStringToUTF8String(bbAppTitle);
  225. [window setTitle:[NSString stringWithUTF8String:p]];
  226. [window center];
  227. bbMemFree(p);
  228. [window makeKeyAndOrderFront:NSApp];
  229. mode=MODE_WINDOW;
  230. }
  231. context=(BBGLContext*)malloc( sizeof(BBGLContext) );
  232. memset( context,0,sizeof(BBGLContext) );
  233. context->mode=mode;
  234. context->width=width;
  235. context->height=height;
  236. context->depth=depth;
  237. context->hertz=hertz;
  238. context->flags=flags;
  239. context->sync=-1;
  240. context->window=window;
  241. if( mode==MODE_DISPLAY ) _displayContext=context;
  242. return context;
  243. }
  244. void bbGLGraphicsGetSettings( BBGLContext *context,int *width,int *height,int *depth,int *hertz, BBInt64 *flags ){
  245. _validateSize( context );
  246. *width=context->width;
  247. *height=context->height;
  248. *depth=context->depth;
  249. *hertz=context->hertz;
  250. *flags=context->flags;
  251. }
  252. void bbGLGraphicsClose( BBGLContext *context ){
  253. if( context==_currentContext ) bbGLGraphicsSetGraphics( 0 );
  254. [context->glContext clearDrawable];
  255. [context->glContext release];
  256. switch( context->mode ){
  257. case MODE_WINDOW:
  258. case MODE_DISPLAY:
  259. bbSystemViewClosed( [context->window contentView] );
  260. [context->window close];
  261. break;
  262. }
  263. if( context==_displayContext ){
  264. CGDisplaySwitchToMode( kCGDirectMainDisplay,oldDisplayMode );
  265. CFRelease( (CFTypeRef)oldDisplayMode );
  266. CGReleaseAllDisplays();
  267. CGDisplayShowCursor( kCGDirectMainDisplay );
  268. #if MAC_OS_X_VERSION_MAX_ALLOWED < 101500
  269. ShowMenuBar();
  270. #endif
  271. _displayContext=0;
  272. }
  273. free( context );
  274. }
  275. void bbGLGraphicsSetGraphics( BBGLContext *context ){
  276. if( context ){
  277. _validateSize( context );
  278. _validateContext( context );
  279. [context->glContext makeCurrentContext];
  280. }else{
  281. [NSOpenGLContext clearCurrentContext];
  282. }
  283. _currentContext=context;
  284. }
  285. static int updated;
  286. void bbGLGraphicsFlip( int sync ){
  287. if( !_currentContext ) return;
  288. sync=sync ? 1 : 0;
  289. static int _sync=-1;
  290. if( sync!=_currentContext->sync ){
  291. _currentContext->sync=sync;
  292. [_currentContext->glContext setValues:(long*)&sync forParameter:kCGLCPSwapInterval];
  293. }
  294. [_currentContext->glContext flushBuffer];
  295. // update the context, at least once - mojave needs this or nothing is rendered.
  296. if (!updated) {
  297. updated = 1;
  298. [_currentContext->glContext update];
  299. }
  300. }