blob: 1663313f2e3f5a60c97c880f6818bfade3c5bcbe [file] [log] [blame]
Mathias Agopian265d9c02009-08-06 16:05:39 -07001/*
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 Agopian509dae52009-08-07 16:37:21 -070020#include <cutils/log.h>
Mathias Agopian265d9c02009-08-06 16:05:39 -070021#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
Mathias Agopian509dae52009-08-07 16:37:21 -070033const 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
Mathias Agopian265d9c02009-08-06 16:05:39 -070055status_t EGLUtils::selectConfigForPixelFormat(
56 EGLDisplay dpy,
57 EGLint const* attrs,
58 PixelFormat format,
59 EGLConfig* outConfig)
60{
61 EGLint numConfigs = -1, n=0;
62
Mathias Agopian6693f232009-08-06 20:46:44 -070063 if (!attrs)
64 return BAD_VALUE;
65
Mathias Agopian265d9c02009-08-06 16:05:39 -070066 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
Mathias Agopiand7ef08c2009-08-06 17:14:10 -070090 int i;
Mathias Agopian265d9c02009-08-06 16:05:39 -070091 EGLConfig config = NULL;
Mathias Agopiand7ef08c2009-08-06 17:14:10 -070092 for (i=0 ; i<n ; i++) {
Mathias Agopian265d9c02009-08-06 16:05:39 -070093 EGLint r,g,b,a;
Mathias Agopian509dae52009-08-07 16:37:21 -070094 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);
Mathias Agopian265d9c02009-08-06 16:05:39 -070099 if (fbSzA == a && fbSzR == r && fbSzG == g && fbSzB == b) {
Mathias Agopian509dae52009-08-07 16:37:21 -0700100 config = curr;
Mathias Agopian265d9c02009-08-06 16:05:39 -0700101 break;
102 }
103 }
104
105 free(configs);
106
Mathias Agopiand7ef08c2009-08-06 17:14:10 -0700107 if (i<n) {
Mathias Agopian265d9c02009-08-06 16:05:39 -0700108 *outConfig = config;
109 return NO_ERROR;
110 }
111
112 return NAME_NOT_FOUND;
113}
114
115status_t EGLUtils::selectConfigForNativeWindow(
116 EGLDisplay dpy,
117 EGLint const* attrs,
118 EGLNativeWindowType window,
119 EGLConfig* outConfig)
120{
121 int err;
122 int format;
Mathias Agopian6693f232009-08-06 20:46:44 -0700123
124 if (!window)
125 return BAD_VALUE;
126
Mathias Agopian265d9c02009-08-06 16:05:39 -0700127 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// ----------------------------------------------------------------------------