blob: f24a71d88d9d11d5ec3dfb5a5d46660532d3d65f [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
Mathias Agopian265d9c02009-08-06 16:05:39 -070069 // 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 }
Mathias Agopian265d9c02009-08-06 16:05:39 -070078
Mathias Agopiand7ef08c2009-08-06 17:14:10 -070079 int i;
Mathias Agopian265d9c02009-08-06 16:05:39 -070080 EGLConfig config = NULL;
Mathias Agopiand7ef08c2009-08-06 17:14:10 -070081 for (i=0 ; i<n ; i++) {
Mathias Agopian81ae9652011-01-16 17:57:20 -080082 EGLint nativeVisualId = 0;
83 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
84 if (nativeVisualId>0 && format == nativeVisualId) {
85 config = configs[i];
Mathias Agopian265d9c02009-08-06 16:05:39 -070086 break;
87 }
88 }
89
90 free(configs);
91
Mathias Agopiand7ef08c2009-08-06 17:14:10 -070092 if (i<n) {
Mathias Agopian265d9c02009-08-06 16:05:39 -070093 *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;
Mathias Agopian6693f232009-08-06 20:46:44 -0700108
109 if (!window)
110 return BAD_VALUE;
111
Mathias Agopian265d9c02009-08-06 16:05:39 -0700112 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// ----------------------------------------------------------------------------