blob: 014c2611ae1ff22f091d7572f86d7afb529b4f1c [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
Mathias Agopiancc0eaa62012-02-24 16:42:46 -080018#ifndef ANDROID_UI_EGLUTILS_H
19#define ANDROID_UI_EGLUTILS_H
Mathias Agopian265d9c02009-08-06 16:05:39 -070020
Mathias Agopiancc0eaa62012-02-24 16:42:46 -080021#include <stdint.h>
22#include <stdlib.h>
23
24#include <system/window.h>
Mathias Agopian265d9c02009-08-06 16:05:39 -070025#include <utils/Errors.h>
Mathias Agopian265d9c02009-08-06 16:05:39 -070026#include <EGL/egl.h>
27
Mathias Agopian265d9c02009-08-06 16:05:39 -070028
29// ----------------------------------------------------------------------------
30namespace android {
31// ----------------------------------------------------------------------------
32
Mathias Agopiancc0eaa62012-02-24 16:42:46 -080033class EGLUtils
34{
35public:
36
37 static inline const char *strerror(EGLint err);
38
39 static inline status_t selectConfigForPixelFormat(
40 EGLDisplay dpy,
41 EGLint const* attrs,
42 int32_t format,
43 EGLConfig* outConfig);
44
45 static inline status_t selectConfigForNativeWindow(
46 EGLDisplay dpy,
47 EGLint const* attrs,
48 EGLNativeWindowType window,
49 EGLConfig* outConfig);
50};
51
52// ----------------------------------------------------------------------------
53
Mathias Agopian509dae52009-08-07 16:37:21 -070054const char *EGLUtils::strerror(EGLint err)
55{
56 switch (err){
57 case EGL_SUCCESS: return "EGL_SUCCESS";
58 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
59 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
60 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
61 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
62 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
63 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
64 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
65 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
66 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
67 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
68 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
69 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
70 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
71 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
72 default: return "UNKNOWN";
73 }
74}
75
Mathias Agopian265d9c02009-08-06 16:05:39 -070076status_t EGLUtils::selectConfigForPixelFormat(
77 EGLDisplay dpy,
78 EGLint const* attrs,
Mathias Agopiancc0eaa62012-02-24 16:42:46 -080079 int32_t format,
Mathias Agopian265d9c02009-08-06 16:05:39 -070080 EGLConfig* outConfig)
81{
82 EGLint numConfigs = -1, n=0;
83
Mathias Agopian6693f232009-08-06 20:46:44 -070084 if (!attrs)
85 return BAD_VALUE;
86
Mathias Agopian265d9c02009-08-06 16:05:39 -070087 if (outConfig == NULL)
88 return BAD_VALUE;
Mathias Agopiancc0eaa62012-02-24 16:42:46 -080089
Mathias Agopian265d9c02009-08-06 16:05:39 -070090 // Get all the "potential match" configs...
Mathias Agopian47b21292011-08-15 15:15:40 -070091 if (eglGetConfigs(dpy, NULL, 0, &numConfigs) == EGL_FALSE)
Mathias Agopian265d9c02009-08-06 16:05:39 -070092 return BAD_VALUE;
93
Mathias Agopian47b21292011-08-15 15:15:40 -070094 EGLConfig* const configs = (EGLConfig*)malloc(sizeof(EGLConfig)*numConfigs);
95 if (eglChooseConfig(dpy, attrs, configs, numConfigs, &n) == EGL_FALSE) {
96 free(configs);
97 return BAD_VALUE;
98 }
Mathias Agopiancc0eaa62012-02-24 16:42:46 -080099
Mathias Agopian47b21292011-08-15 15:15:40 -0700100 int i;
101 EGLConfig config = NULL;
102 for (i=0 ; i<n ; i++) {
103 EGLint nativeVisualId = 0;
104 eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId);
105 if (nativeVisualId>0 && format == nativeVisualId) {
Mathias Agopian81ae9652011-01-16 17:57:20 -0800106 config = configs[i];
Mathias Agopian265d9c02009-08-06 16:05:39 -0700107 break;
108 }
Mathias Agopian47b21292011-08-15 15:15:40 -0700109 }
Mathias Agopian265d9c02009-08-06 16:05:39 -0700110
Mathias Agopian47b21292011-08-15 15:15:40 -0700111 free(configs);
Mathias Agopiancc0eaa62012-02-24 16:42:46 -0800112
Mathias Agopian47b21292011-08-15 15:15:40 -0700113 if (i<n) {
114 *outConfig = config;
115 return NO_ERROR;
Mathias Agopian265d9c02009-08-06 16:05:39 -0700116 }
117
118 return NAME_NOT_FOUND;
119}
120
121status_t EGLUtils::selectConfigForNativeWindow(
122 EGLDisplay dpy,
123 EGLint const* attrs,
124 EGLNativeWindowType window,
125 EGLConfig* outConfig)
126{
127 int err;
128 int format;
Mathias Agopiancc0eaa62012-02-24 16:42:46 -0800129
Mathias Agopian6693f232009-08-06 20:46:44 -0700130 if (!window)
131 return BAD_VALUE;
Mathias Agopiancc0eaa62012-02-24 16:42:46 -0800132
Mathias Agopian265d9c02009-08-06 16:05:39 -0700133 if ((err = window->query(window, NATIVE_WINDOW_FORMAT, &format)) < 0) {
134 return err;
135 }
136
137 return selectConfigForPixelFormat(dpy, attrs, format, outConfig);
138}
139
140// ----------------------------------------------------------------------------
141}; // namespace android
142// ----------------------------------------------------------------------------
Mathias Agopiancc0eaa62012-02-24 16:42:46 -0800143
144#endif /* ANDROID_UI_EGLUTILS_H */