Mathias Agopian | 6cf50a7 | 2009-08-06 16:05:39 -0700 | [diff] [blame] | 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 | |
| 17 | |
| 18 | #define LOG_TAG "EGLUtils" |
| 19 | |
Mathias Agopian | 8c12c7a | 2009-08-07 16:37:21 -0700 | [diff] [blame] | 20 | #include <cutils/log.h> |
Mathias Agopian | 6cf50a7 | 2009-08-06 16:05:39 -0700 | [diff] [blame] | 21 | #include <utils/Errors.h> |
| 22 | |
| 23 | #include <ui/EGLUtils.h> |
| 24 | |
| 25 | #include <EGL/egl.h> |
| 26 | |
Mathias Agopian | 6163091 | 2011-07-06 16:35:30 -0700 | [diff] [blame] | 27 | #include <system/graphics.h> |
| 28 | |
Mathias Agopian | 6cf50a7 | 2009-08-06 16:05:39 -0700 | [diff] [blame] | 29 | #include <private/ui/android_natives_priv.h> |
| 30 | |
| 31 | // ---------------------------------------------------------------------------- |
| 32 | namespace android { |
| 33 | // ---------------------------------------------------------------------------- |
| 34 | |
Mathias Agopian | 8c12c7a | 2009-08-07 16:37:21 -0700 | [diff] [blame] | 35 | const char *EGLUtils::strerror(EGLint err) |
| 36 | { |
| 37 | switch (err){ |
| 38 | case EGL_SUCCESS: return "EGL_SUCCESS"; |
| 39 | case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED"; |
| 40 | case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS"; |
| 41 | case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC"; |
| 42 | case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE"; |
| 43 | case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG"; |
| 44 | case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT"; |
| 45 | case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE"; |
| 46 | case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY"; |
| 47 | case EGL_BAD_MATCH: return "EGL_BAD_MATCH"; |
| 48 | case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP"; |
| 49 | case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW"; |
| 50 | case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER"; |
| 51 | case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE"; |
| 52 | case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST"; |
| 53 | default: return "UNKNOWN"; |
| 54 | } |
| 55 | } |
| 56 | |
Mathias Agopian | 6cf50a7 | 2009-08-06 16:05:39 -0700 | [diff] [blame] | 57 | status_t EGLUtils::selectConfigForPixelFormat( |
| 58 | EGLDisplay dpy, |
| 59 | EGLint const* attrs, |
| 60 | PixelFormat format, |
| 61 | EGLConfig* outConfig) |
| 62 | { |
| 63 | EGLint numConfigs = -1, n=0; |
| 64 | |
Mathias Agopian | 42db9dc | 2009-08-06 20:46:44 -0700 | [diff] [blame] | 65 | if (!attrs) |
| 66 | return BAD_VALUE; |
| 67 | |
Mathias Agopian | 6cf50a7 | 2009-08-06 16:05:39 -0700 | [diff] [blame] | 68 | if (outConfig == NULL) |
| 69 | return BAD_VALUE; |
| 70 | |
Mathias Agopian | 6cf50a7 | 2009-08-06 16:05:39 -0700 | [diff] [blame] | 71 | // Get all the "potential match" configs... |
Mathias Agopian | 6163091 | 2011-07-06 16:35:30 -0700 | [diff] [blame] | 72 | if (eglChooseConfig(dpy, attrs, 0, 0, &numConfigs) == EGL_FALSE) |
Mathias Agopian | 6cf50a7 | 2009-08-06 16:05:39 -0700 | [diff] [blame] | 73 | return BAD_VALUE; |
| 74 | |
Mathias Agopian | 6163091 | 2011-07-06 16:35:30 -0700 | [diff] [blame] | 75 | if (numConfigs) { |
| 76 | EGLConfig* const configs = new EGLConfig[numConfigs]; |
| 77 | if (eglChooseConfig(dpy, attrs, configs, numConfigs, &n) == EGL_FALSE) { |
| 78 | delete [] configs; |
| 79 | return BAD_VALUE; |
| 80 | } |
| 81 | |
| 82 | bool hasAlpha = false; |
| 83 | switch (format) { |
| 84 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 85 | case HAL_PIXEL_FORMAT_BGRA_8888: |
| 86 | case HAL_PIXEL_FORMAT_RGBA_5551: |
| 87 | case HAL_PIXEL_FORMAT_RGBA_4444: |
| 88 | hasAlpha = true; |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | // The first config is guaranteed to over-satisfy the constraints |
| 93 | EGLConfig config = configs[0]; |
| 94 | |
| 95 | // go through the list and skip configs that over-satisfy our needs |
| 96 | int i; |
| 97 | for (i=0 ; i<n ; i++) { |
| 98 | if (!hasAlpha) { |
| 99 | EGLint alphaSize; |
| 100 | eglGetConfigAttrib(dpy, configs[i], EGL_ALPHA_SIZE, &alphaSize); |
| 101 | if (alphaSize > 0) { |
| 102 | continue; |
| 103 | } |
| 104 | } |
Mathias Agopian | d5ea3db | 2011-01-16 17:57:20 -0800 | [diff] [blame] | 105 | config = configs[i]; |
Mathias Agopian | 6cf50a7 | 2009-08-06 16:05:39 -0700 | [diff] [blame] | 106 | break; |
| 107 | } |
Mathias Agopian | 6cf50a7 | 2009-08-06 16:05:39 -0700 | [diff] [blame] | 108 | |
Mathias Agopian | 6163091 | 2011-07-06 16:35:30 -0700 | [diff] [blame] | 109 | delete [] configs; |
| 110 | |
| 111 | if (i<n) { |
| 112 | *outConfig = config; |
| 113 | return NO_ERROR; |
| 114 | } |
Mathias Agopian | 6cf50a7 | 2009-08-06 16:05:39 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | return NAME_NOT_FOUND; |
| 118 | } |
| 119 | |
| 120 | status_t EGLUtils::selectConfigForNativeWindow( |
| 121 | EGLDisplay dpy, |
| 122 | EGLint const* attrs, |
| 123 | EGLNativeWindowType window, |
| 124 | EGLConfig* outConfig) |
| 125 | { |
| 126 | int err; |
| 127 | int format; |
Mathias Agopian | 42db9dc | 2009-08-06 20:46:44 -0700 | [diff] [blame] | 128 | |
| 129 | if (!window) |
| 130 | return BAD_VALUE; |
| 131 | |
Mathias Agopian | 6cf50a7 | 2009-08-06 16:05:39 -0700 | [diff] [blame] | 132 | if ((err = window->query(window, NATIVE_WINDOW_FORMAT, &format)) < 0) { |
| 133 | return err; |
| 134 | } |
| 135 | |
| 136 | return selectConfigForPixelFormat(dpy, attrs, format, outConfig); |
| 137 | } |
| 138 | |
| 139 | // ---------------------------------------------------------------------------- |
| 140 | }; // namespace android |
| 141 | // ---------------------------------------------------------------------------- |