EGLUtils.h revision d5ea3db6a3049d6b66a619be08002e90aa38f99f
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// ----------------------------------------------------------------------------
30namespace android {
31// ----------------------------------------------------------------------------
32
33const 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
55status_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    // Get all the "potential match" configs...
70    if (eglGetConfigs(dpy, NULL, 0, &numConfigs) == EGL_FALSE)
71        return BAD_VALUE;
72
73    EGLConfig* const configs = (EGLConfig*)malloc(sizeof(EGLConfig)*numConfigs);
74    if (eglChooseConfig(dpy, attrs, configs, numConfigs, &n) == EGL_FALSE) {
75        free(configs);
76        return BAD_VALUE;
77    }
78
79    int i;
80    EGLConfig config = NULL;
81    for (i=0 ; i<n ; i++) {
82        EGLint nativeVisualId = 0;
83        eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
84        if (nativeVisualId>0 && format == nativeVisualId) {
85            config = configs[i];
86            break;
87        }
88    }
89
90    free(configs);
91
92    if (i<n) {
93        *outConfig = config;
94        return NO_ERROR;
95    }
96
97    return NAME_NOT_FOUND;
98}
99
100status_t EGLUtils::selectConfigForNativeWindow(
101        EGLDisplay dpy,
102        EGLint const* attrs,
103        EGLNativeWindowType window,
104        EGLConfig* outConfig)
105{
106    int err;
107    int format;
108
109    if (!window)
110        return BAD_VALUE;
111
112    if ((err = window->query(window, NATIVE_WINDOW_FORMAT, &format)) < 0) {
113        return err;
114    }
115
116    return selectConfigForPixelFormat(dpy, attrs, format, outConfig);
117}
118
119// ----------------------------------------------------------------------------
120}; // namespace android
121// ----------------------------------------------------------------------------
122