gl_macos.m 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-License-Identifier: Unlicense OR MIT
  2. // +build darwin,!ios,nometal
  3. #import <AppKit/AppKit.h>
  4. #include <CoreFoundation/CoreFoundation.h>
  5. #include <OpenGL/OpenGL.h>
  6. #include "_cgo_export.h"
  7. CALayer *gio_layerFactory(BOOL presentWithTrans) {
  8. @autoreleasepool {
  9. return [CALayer layer];
  10. }
  11. }
  12. CFTypeRef gio_createGLContext(void) {
  13. @autoreleasepool {
  14. NSOpenGLPixelFormatAttribute attr[] = {
  15. NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
  16. NSOpenGLPFAColorSize, 24,
  17. NSOpenGLPFAAccelerated,
  18. // Opt-in to automatic GPU switching. CGL-only property.
  19. kCGLPFASupportsAutomaticGraphicsSwitching,
  20. NSOpenGLPFAAllowOfflineRenderers,
  21. 0
  22. };
  23. NSOpenGLPixelFormat *pixFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr];
  24. NSOpenGLContext *ctx = [[NSOpenGLContext alloc] initWithFormat:pixFormat shareContext: nil];
  25. return CFBridgingRetain(ctx);
  26. }
  27. }
  28. void gio_setContextView(CFTypeRef ctxRef, CFTypeRef viewRef) {
  29. NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
  30. NSView *view = (__bridge NSView *)viewRef;
  31. [view setWantsBestResolutionOpenGLSurface:YES];
  32. [ctx setView:view];
  33. }
  34. void gio_clearCurrentContext(void) {
  35. @autoreleasepool {
  36. [NSOpenGLContext clearCurrentContext];
  37. }
  38. }
  39. void gio_updateContext(CFTypeRef ctxRef) {
  40. @autoreleasepool {
  41. NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
  42. [ctx update];
  43. }
  44. }
  45. void gio_makeCurrentContext(CFTypeRef ctxRef) {
  46. @autoreleasepool {
  47. NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
  48. [ctx makeCurrentContext];
  49. }
  50. }
  51. void gio_lockContext(CFTypeRef ctxRef) {
  52. @autoreleasepool {
  53. NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
  54. CGLLockContext([ctx CGLContextObj]);
  55. }
  56. }
  57. void gio_unlockContext(CFTypeRef ctxRef) {
  58. @autoreleasepool {
  59. NSOpenGLContext *ctx = (__bridge NSOpenGLContext *)ctxRef;
  60. CGLUnlockContext([ctx CGLContextObj]);
  61. }
  62. }