Context.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibAccelGfx/Context.h>
  7. #ifdef AK_OS_MACOS
  8. # define GL_SILENCE_DEPRECATION
  9. # include <OpenGL/CGLRenderers.h>
  10. # include <OpenGL/CGLTypes.h>
  11. # include <OpenGL/OpenGL.h>
  12. # include <OpenGL/gl3.h>
  13. #endif
  14. namespace AccelGfx {
  15. Context& Context::the()
  16. {
  17. static OwnPtr<Context> s_the;
  18. if (!s_the)
  19. s_the = Context::create();
  20. return *s_the;
  21. }
  22. #ifdef AK_OS_MACOS
  23. static void make_context_cgl()
  24. {
  25. CGLContextObj context = NULL;
  26. CGLPixelFormatAttribute attributes[4] = {
  27. kCGLPFAOpenGLProfile,
  28. (CGLPixelFormatAttribute)kCGLOGLPVersion_3_2_Core,
  29. kCGLPFAAccelerated,
  30. (CGLPixelFormatAttribute)0
  31. };
  32. CGLPixelFormatObj pixelFormat = NULL;
  33. GLint numPixelFormats = 0;
  34. CGLError error = CGLChoosePixelFormat(attributes, &pixelFormat, &numPixelFormats);
  35. if (error) {
  36. VERIFY_NOT_REACHED();
  37. }
  38. error = CGLCreateContext(pixelFormat, NULL, &context);
  39. if (error) {
  40. VERIFY_NOT_REACHED();
  41. }
  42. error = CGLSetCurrentContext(context);
  43. if (error) {
  44. VERIFY_NOT_REACHED();
  45. }
  46. VERIFY(glGetError() == GL_NO_ERROR);
  47. }
  48. #else
  49. static void make_context_egl()
  50. {
  51. EGLDisplay egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  52. EGLint major;
  53. EGLint minor;
  54. eglInitialize(egl_display, &major, &minor);
  55. EGLBoolean ok = eglBindAPI(EGL_OPENGL_API);
  56. if (ok == EGL_FALSE) {
  57. dbgln("eglBindAPI failed");
  58. VERIFY_NOT_REACHED();
  59. }
  60. static EGLint const config_attributes[] = {
  61. EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
  62. EGL_BLUE_SIZE, 8,
  63. EGL_GREEN_SIZE, 8,
  64. EGL_RED_SIZE, 8,
  65. EGL_DEPTH_SIZE, 8,
  66. EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
  67. EGL_NONE
  68. };
  69. EGLConfig egl_config;
  70. EGLint num_configs;
  71. eglChooseConfig(egl_display, config_attributes, &egl_config, 1, &num_configs);
  72. static EGLint const context_attributes[] = {
  73. EGL_CONTEXT_MAJOR_VERSION, 3,
  74. EGL_CONTEXT_MINOR_VERSION, 3,
  75. EGL_NONE
  76. };
  77. EGLContext egl_context = eglCreateContext(egl_display, egl_config, EGL_NO_CONTEXT, context_attributes);
  78. if (egl_context == EGL_FALSE) {
  79. dbgln("eglCreateContext failed");
  80. VERIFY_NOT_REACHED();
  81. }
  82. EGLBoolean result = eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, egl_context);
  83. if (result == EGL_FALSE) {
  84. dbgln("eglMakeCurrent failed");
  85. VERIFY_NOT_REACHED();
  86. }
  87. }
  88. #endif
  89. OwnPtr<Context> Context::create()
  90. {
  91. #ifdef AK_OS_MACOS
  92. make_context_cgl();
  93. #else
  94. make_context_egl();
  95. #endif
  96. return make<Context>();
  97. }
  98. }