Home | History | Annotate | Download | only in ui
      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 <cutils/log.h>
     21 #include <utils/Errors.h>
     22 
     23 #include <ui/EGLUtils.h>
     24 
     25 #include <EGL/egl.h>
     26 
     27 #include <private/ui/android_natives_priv.h>
     28 
     29 // ----------------------------------------------------------------------------
     30 namespace android {
     31 // ----------------------------------------------------------------------------
     32 
     33 const char *EGLUtils::strerror(EGLint err)
     34 {
     35     switch (err){
     36         case EGL_SUCCESS:           return "EGL_SUCCESS";
     37         case EGL_NOT_INITIALIZED:   return "EGL_NOT_INITIALIZED";
     38         case EGL_BAD_ACCESS:        return "EGL_BAD_ACCESS";
     39         case EGL_BAD_ALLOC:         return "EGL_BAD_ALLOC";
     40         case EGL_BAD_ATTRIBUTE:     return "EGL_BAD_ATTRIBUTE";
     41         case EGL_BAD_CONFIG:        return "EGL_BAD_CONFIG";
     42         case EGL_BAD_CONTEXT:       return "EGL_BAD_CONTEXT";
     43         case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
     44         case EGL_BAD_DISPLAY:       return "EGL_BAD_DISPLAY";
     45         case EGL_BAD_MATCH:         return "EGL_BAD_MATCH";
     46         case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
     47         case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
     48         case EGL_BAD_PARAMETER:     return "EGL_BAD_PARAMETER";
     49         case EGL_BAD_SURFACE:       return "EGL_BAD_SURFACE";
     50         case EGL_CONTEXT_LOST:      return "EGL_CONTEXT_LOST";
     51         default: return "UNKNOWN";
     52     }
     53 }
     54 
     55 status_t EGLUtils::selectConfigForPixelFormat(
     56         EGLDisplay dpy,
     57         EGLint const* attrs,
     58         PixelFormat format,
     59         EGLConfig* outConfig)
     60 {
     61     EGLint numConfigs = -1, n=0;
     62 
     63     if (!attrs)
     64         return BAD_VALUE;
     65 
     66     if (outConfig == NULL)
     67         return BAD_VALUE;
     68 
     69     int err;
     70     PixelFormatInfo fbFormatInfo;
     71     if ((err = getPixelFormatInfo(PixelFormat(format), &fbFormatInfo)) < 0) {
     72         return err;
     73     }
     74 
     75     // Get all the "potential match" configs...
     76     if (eglGetConfigs(dpy, NULL, 0, &numConfigs) == EGL_FALSE)
     77         return BAD_VALUE;
     78 
     79     EGLConfig* const configs = (EGLConfig*)malloc(sizeof(EGLConfig)*numConfigs);
     80     if (eglChooseConfig(dpy, attrs, configs, numConfigs, &n) == EGL_FALSE) {
     81         free(configs);
     82         return BAD_VALUE;
     83     }
     84 
     85     const int fbSzA = fbFormatInfo.getSize(PixelFormatInfo::INDEX_ALPHA);
     86     const int fbSzR = fbFormatInfo.getSize(PixelFormatInfo::INDEX_RED);
     87     const int fbSzG = fbFormatInfo.getSize(PixelFormatInfo::INDEX_GREEN);
     88     const int fbSzB = fbFormatInfo.getSize(PixelFormatInfo::INDEX_BLUE);
     89 
     90     int i;
     91     EGLConfig config = NULL;
     92     for (i=0 ; i<n ; i++) {
     93         EGLint r,g,b,a;
     94         EGLConfig curr = configs[i];
     95         eglGetConfigAttrib(dpy, curr, EGL_RED_SIZE,   &r);
     96         eglGetConfigAttrib(dpy, curr, EGL_GREEN_SIZE, &g);
     97         eglGetConfigAttrib(dpy, curr, EGL_BLUE_SIZE,  &b);
     98         eglGetConfigAttrib(dpy, curr, EGL_ALPHA_SIZE, &a);
     99         if (fbSzA == a && fbSzR == r && fbSzG == g && fbSzB  == b) {
    100             config = curr;
    101             break;
    102         }
    103     }
    104 
    105     free(configs);
    106 
    107     if (i<n) {
    108         *outConfig = config;
    109         return NO_ERROR;
    110     }
    111 
    112     return NAME_NOT_FOUND;
    113 }
    114 
    115 status_t EGLUtils::selectConfigForNativeWindow(
    116         EGLDisplay dpy,
    117         EGLint const* attrs,
    118         EGLNativeWindowType window,
    119         EGLConfig* outConfig)
    120 {
    121     int err;
    122     int format;
    123 
    124     if (!window)
    125         return BAD_VALUE;
    126 
    127     if ((err = window->query(window, NATIVE_WINDOW_FORMAT, &format)) < 0) {
    128         return err;
    129     }
    130 
    131     return selectConfigForPixelFormat(dpy, attrs, format, outConfig);
    132 }
    133 
    134 // ----------------------------------------------------------------------------
    135 }; // namespace android
    136 // ----------------------------------------------------------------------------
    137