Context.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Error.h>
  7. #include <LibAccelGfx/Context.h>
  8. #ifdef AK_OS_MACOS
  9. # define GL_SILENCE_DEPRECATION
  10. # include <OpenGL/CGLRenderers.h>
  11. # include <OpenGL/CGLTypes.h>
  12. # include <OpenGL/OpenGL.h>
  13. # include <OpenGL/gl3.h>
  14. #endif
  15. namespace AccelGfx {
  16. #ifdef AK_OS_MACOS
  17. class CGLContextWrapper : public Context {
  18. public:
  19. CGLContextWrapper(CGLContextObj context)
  20. : m_context(context)
  21. {
  22. }
  23. virtual void activate() override
  24. {
  25. CGLSetCurrentContext(m_context);
  26. }
  27. ~CGLContextWrapper()
  28. {
  29. CGLReleaseContext(m_context);
  30. }
  31. private:
  32. CGLContextObj m_context;
  33. };
  34. #elif !defined(AK_OS_SERENITY)
  35. class EGLContextWrapper : public Context {
  36. public:
  37. EGLContextWrapper(EGLContext context)
  38. : m_context(context)
  39. {
  40. }
  41. ~EGLContextWrapper()
  42. {
  43. eglDestroyContext(eglGetCurrentDisplay(), m_context);
  44. }
  45. virtual void activate() override
  46. {
  47. eglMakeCurrent(eglGetCurrentDisplay(), EGL_NO_SURFACE, EGL_NO_SURFACE, m_context);
  48. }
  49. private:
  50. EGLContext m_context;
  51. };
  52. #endif
  53. #ifdef AK_OS_MACOS
  54. static ErrorOr<NonnullOwnPtr<CGLContextWrapper>> make_context_cgl()
  55. {
  56. CGLContextObj context = NULL;
  57. CGLPixelFormatAttribute attributes[4] = {
  58. kCGLPFAOpenGLProfile,
  59. (CGLPixelFormatAttribute)kCGLOGLPVersion_3_2_Core,
  60. kCGLPFAAccelerated,
  61. (CGLPixelFormatAttribute)0
  62. };
  63. CGLPixelFormatObj pixelFormat = NULL;
  64. GLint numPixelFormats = 0;
  65. CGLError error = CGLChoosePixelFormat(attributes, &pixelFormat, &numPixelFormats);
  66. if (error) {
  67. auto const* cgl_error_string = CGLErrorString(error);
  68. return Error::from_string_view(StringView { cgl_error_string, strlen(cgl_error_string) });
  69. }
  70. error = CGLCreateContext(pixelFormat, NULL, &context);
  71. if (error) {
  72. auto const* cgl_error_string = CGLErrorString(error);
  73. return Error::from_string_view(StringView { cgl_error_string, strlen(cgl_error_string) });
  74. }
  75. error = CGLSetCurrentContext(context);
  76. if (error) {
  77. auto const* cgl_error_string = CGLErrorString(error);
  78. return Error::from_string_view(StringView { cgl_error_string, strlen(cgl_error_string) });
  79. }
  80. VERIFY(glGetError() == GL_NO_ERROR);
  81. return make<CGLContextWrapper>(context);
  82. }
  83. #elif !defined(AK_OS_SERENITY)
  84. static StringView format_egl_error(EGLint error)
  85. {
  86. switch (error) {
  87. case EGL_SUCCESS:
  88. return "EGL_SUCCESS"sv;
  89. case EGL_NOT_INITIALIZED:
  90. return "EGL_NOT_INITIALIZED"sv;
  91. case EGL_BAD_ACCESS:
  92. return "EGL_BAD_ACCESS"sv;
  93. case EGL_BAD_ALLOC:
  94. return "EGL_BAD_ALLOC"sv;
  95. case EGL_BAD_ATTRIBUTE:
  96. return "EGL_BAD_ATTRIBUTE"sv;
  97. case EGL_BAD_CONTEXT:
  98. return "EGL_BAD_CONTEXT"sv;
  99. case EGL_BAD_CONFIG:
  100. return "EGL_BAD_CONFIG"sv;
  101. case EGL_BAD_CURRENT_SURFACE:
  102. return "EGL_BAD_CURRENT_SURFACE"sv;
  103. case EGL_BAD_DISPLAY:
  104. return "EGL_BAD_DISPLAY"sv;
  105. case EGL_BAD_SURFACE:
  106. return "EGL_BAD_SURFACE"sv;
  107. case EGL_BAD_MATCH:
  108. return "EGL_BAD_MATCH"sv;
  109. case EGL_BAD_PARAMETER:
  110. return "EGL_BAD_PARAMETER"sv;
  111. case EGL_BAD_NATIVE_PIXMAP:
  112. return "EGL_BAD_NATIVE_PIXMAP"sv;
  113. case EGL_BAD_NATIVE_WINDOW:
  114. return "EGL_BAD_NATIVE_WINDOW"sv;
  115. case EGL_CONTEXT_LOST:
  116. return "EGL_CONTEXT_LOST"sv;
  117. default:
  118. return "Unknown error"sv;
  119. }
  120. }
  121. static ErrorOr<NonnullOwnPtr<EGLContextWrapper>> make_context_egl()
  122. {
  123. EGLDisplay egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  124. EGLint major;
  125. EGLint minor;
  126. eglInitialize(egl_display, &major, &minor);
  127. EGLBoolean ok = eglBindAPI(EGL_OPENGL_API);
  128. if (ok == EGL_FALSE) {
  129. dbgln("eglBindAPI failed");
  130. VERIFY_NOT_REACHED();
  131. }
  132. static EGLint const config_attributes[] = {
  133. EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
  134. EGL_BLUE_SIZE, 8,
  135. EGL_GREEN_SIZE, 8,
  136. EGL_RED_SIZE, 8,
  137. EGL_DEPTH_SIZE, 8,
  138. EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
  139. EGL_NONE
  140. };
  141. EGLConfig egl_config;
  142. EGLint num_configs;
  143. eglChooseConfig(egl_display, config_attributes, &egl_config, 1, &num_configs);
  144. static EGLint const context_attributes[] = {
  145. EGL_CONTEXT_MAJOR_VERSION, 3,
  146. EGL_CONTEXT_MINOR_VERSION, 3,
  147. EGL_NONE
  148. };
  149. EGLContext egl_context = eglCreateContext(egl_display, egl_config, EGL_NO_CONTEXT, context_attributes);
  150. if (egl_context == EGL_FALSE) {
  151. return Error::from_string_view(format_egl_error(eglGetError()));
  152. }
  153. EGLBoolean result = eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, egl_context);
  154. if (result == EGL_FALSE) {
  155. return Error::from_string_view(format_egl_error(eglGetError()));
  156. }
  157. return make<EGLContextWrapper>(egl_context);
  158. }
  159. #endif
  160. ErrorOr<NonnullOwnPtr<Context>> Context::create()
  161. {
  162. #ifdef AK_OS_MACOS
  163. return make_context_cgl();
  164. #else
  165. return make_context_egl();
  166. #endif
  167. }
  168. }