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 | |
| 20 | #include <utils/Errors.h> |
| 21 | |
| 22 | #include <ui/EGLUtils.h> |
| 23 | |
| 24 | #include <EGL/egl.h> |
| 25 | |
| 26 | #include <private/ui/android_natives_priv.h> |
| 27 | |
| 28 | // ---------------------------------------------------------------------------- |
| 29 | namespace android { |
| 30 | // ---------------------------------------------------------------------------- |
| 31 | |
| 32 | status_t EGLUtils::selectConfigForPixelFormat( |
| 33 | EGLDisplay dpy, |
| 34 | EGLint const* attrs, |
| 35 | PixelFormat format, |
| 36 | EGLConfig* outConfig) |
| 37 | { |
| 38 | EGLint numConfigs = -1, n=0; |
| 39 | |
| 40 | if (outConfig == NULL) |
| 41 | return BAD_VALUE; |
| 42 | |
| 43 | int err; |
| 44 | PixelFormatInfo fbFormatInfo; |
| 45 | if ((err = getPixelFormatInfo(PixelFormat(format), &fbFormatInfo)) < 0) { |
| 46 | return err; |
| 47 | } |
| 48 | |
| 49 | // Get all the "potential match" configs... |
| 50 | if (eglGetConfigs(dpy, NULL, 0, &numConfigs) == EGL_FALSE) |
| 51 | return BAD_VALUE; |
| 52 | |
| 53 | EGLConfig* const configs = (EGLConfig*)malloc(sizeof(EGLConfig)*numConfigs); |
| 54 | if (eglChooseConfig(dpy, attrs, configs, numConfigs, &n) == EGL_FALSE) { |
| 55 | free(configs); |
| 56 | return BAD_VALUE; |
| 57 | } |
| 58 | |
| 59 | const int fbSzA = fbFormatInfo.getSize(PixelFormatInfo::INDEX_ALPHA); |
| 60 | const int fbSzR = fbFormatInfo.getSize(PixelFormatInfo::INDEX_RED); |
| 61 | const int fbSzG = fbFormatInfo.getSize(PixelFormatInfo::INDEX_GREEN); |
| 62 | const int fbSzB = fbFormatInfo.getSize(PixelFormatInfo::INDEX_BLUE); |
| 63 | |
| 64 | EGLConfig config = NULL; |
| 65 | for (int i=0 ; i<n ; i++) { |
| 66 | EGLint r,g,b,a; |
| 67 | eglGetConfigAttrib(dpy, configs[i], EGL_RED_SIZE, &r); |
| 68 | eglGetConfigAttrib(dpy, configs[i], EGL_GREEN_SIZE, &g); |
| 69 | eglGetConfigAttrib(dpy, configs[i], EGL_BLUE_SIZE, &b); |
| 70 | eglGetConfigAttrib(dpy, configs[i], EGL_ALPHA_SIZE, &a); |
| 71 | if (fbSzA == a && fbSzR == r && fbSzG == g && fbSzB == b) { |
| 72 | config = configs[i]; |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | free(configs); |
| 78 | |
| 79 | if (config) { |
| 80 | *outConfig = config; |
| 81 | return NO_ERROR; |
| 82 | } |
| 83 | |
| 84 | return NAME_NOT_FOUND; |
| 85 | } |
| 86 | |
| 87 | status_t EGLUtils::selectConfigForNativeWindow( |
| 88 | EGLDisplay dpy, |
| 89 | EGLint const* attrs, |
| 90 | EGLNativeWindowType window, |
| 91 | EGLConfig* outConfig) |
| 92 | { |
| 93 | int err; |
| 94 | int format; |
| 95 | if ((err = window->query(window, NATIVE_WINDOW_FORMAT, &format)) < 0) { |
| 96 | return err; |
| 97 | } |
| 98 | |
| 99 | return selectConfigForPixelFormat(dpy, attrs, format, outConfig); |
| 100 | } |
| 101 | |
| 102 | // ---------------------------------------------------------------------------- |
| 103 | }; // namespace android |
| 104 | // ---------------------------------------------------------------------------- |