EGLUtils.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef ANDROID_UI_EGLUTILS_H
  17. #define ANDROID_UI_EGLUTILS_H
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <system/window.h>
  21. #include <utils/Errors.h>
  22. #include <EGL/egl.h>
  23. // ----------------------------------------------------------------------------
  24. namespace android {
  25. // ----------------------------------------------------------------------------
  26. class EGLUtils
  27. {
  28. public:
  29. static inline const char *strerror(EGLint err);
  30. static inline status_t selectConfigForPixelFormat(
  31. EGLDisplay dpy,
  32. EGLint const* attrs,
  33. int32_t format,
  34. EGLConfig* outConfig);
  35. static inline status_t selectConfigForNativeWindow(
  36. EGLDisplay dpy,
  37. EGLint const* attrs,
  38. EGLNativeWindowType window,
  39. EGLConfig* outConfig);
  40. };
  41. // ----------------------------------------------------------------------------
  42. const char *EGLUtils::strerror(EGLint err)
  43. {
  44. switch (err){
  45. case EGL_SUCCESS: return "EGL_SUCCESS";
  46. case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
  47. case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
  48. case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
  49. case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
  50. case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
  51. case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
  52. case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
  53. case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
  54. case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
  55. case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
  56. case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
  57. case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
  58. case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
  59. case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
  60. default: return "UNKNOWN";
  61. }
  62. }
  63. status_t EGLUtils::selectConfigForPixelFormat(
  64. EGLDisplay dpy,
  65. EGLint const* attrs,
  66. int32_t format,
  67. EGLConfig* outConfig)
  68. {
  69. EGLint numConfigs = -1, n=0;
  70. if (!attrs)
  71. return BAD_VALUE;
  72. if (outConfig == NULL)
  73. return BAD_VALUE;
  74. // Get all the "potential match" configs...
  75. if (eglGetConfigs(dpy, NULL, 0, &numConfigs) == EGL_FALSE)
  76. return BAD_VALUE;
  77. EGLConfig* const configs = (EGLConfig*)malloc(sizeof(EGLConfig)*numConfigs);
  78. if (eglChooseConfig(dpy, attrs, configs, numConfigs, &n) == EGL_FALSE) {
  79. free(configs);
  80. return BAD_VALUE;
  81. }
  82. int i;
  83. EGLConfig config = NULL;
  84. for (i=0 ; i<n ; i++) {
  85. EGLint nativeVisualId = 0;
  86. eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
  87. if (nativeVisualId>0 && format == nativeVisualId) {
  88. config = configs[i];
  89. break;
  90. }
  91. }
  92. free(configs);
  93. if (i<n) {
  94. *outConfig = config;
  95. return NO_ERROR;
  96. }
  97. return NAME_NOT_FOUND;
  98. }
  99. status_t EGLUtils::selectConfigForNativeWindow(
  100. EGLDisplay dpy,
  101. EGLint const* attrs,
  102. EGLNativeWindowType window,
  103. EGLConfig* outConfig)
  104. {
  105. int err;
  106. int format;
  107. if (!window)
  108. return BAD_VALUE;
  109. if ((err = window->query(window, NATIVE_WINDOW_FORMAT, &format)) < 0) {
  110. return err;
  111. }
  112. return selectConfigForPixelFormat(dpy, attrs, format, outConfig);
  113. }
  114. // ----------------------------------------------------------------------------
  115. }; // namespace android
  116. // ----------------------------------------------------------------------------
  117. #endif /* ANDROID_UI_EGLUTILS_H */