blob: 020646b9c41666c636095a1f077d6d48c11f23d3 [file] [log] [blame]
Mathias Agopian6cf50a72009-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 Agopian8c12c7a2009-08-07 16:37:21 -070020#include <cutils/log.h>
Mathias Agopian6cf50a72009-08-06 16:05:39 -070021#include <utils/Errors.h>
22
23#include <ui/EGLUtils.h>
24
25#include <EGL/egl.h>
26
Mathias Agopian61630912011-07-06 16:35:30 -070027#include <system/graphics.h>
28
Mathias Agopian6cf50a72009-08-06 16:05:39 -070029#include <private/ui/android_natives_priv.h>
30
31// ----------------------------------------------------------------------------
32namespace android {
33// ----------------------------------------------------------------------------
34
Mathias Agopian8c12c7a2009-08-07 16:37:21 -070035const char *EGLUtils::strerror(EGLint err)
36{
37 switch (err){
38 case EGL_SUCCESS: return "EGL_SUCCESS";
39 case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED";
40 case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS";
41 case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC";
42 case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE";
43 case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG";
44 case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT";
45 case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE";
46 case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY";
47 case EGL_BAD_MATCH: return "EGL_BAD_MATCH";
48 case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP";
49 case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW";
50 case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER";
51 case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE";
52 case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST";
53 default: return "UNKNOWN";
54 }
55}
56
Mathias Agopian6cf50a72009-08-06 16:05:39 -070057status_t EGLUtils::selectConfigForPixelFormat(
58 EGLDisplay dpy,
59 EGLint const* attrs,
60 PixelFormat format,
61 EGLConfig* outConfig)
62{
63 EGLint numConfigs = -1, n=0;
64
Mathias Agopian42db9dc2009-08-06 20:46:44 -070065 if (!attrs)
66 return BAD_VALUE;
67
Mathias Agopian6cf50a72009-08-06 16:05:39 -070068 if (outConfig == NULL)
69 return BAD_VALUE;
70
Mathias Agopian6cf50a72009-08-06 16:05:39 -070071 // Get all the "potential match" configs...
Mathias Agopian61630912011-07-06 16:35:30 -070072 if (eglChooseConfig(dpy, attrs, 0, 0, &numConfigs) == EGL_FALSE)
Mathias Agopian6cf50a72009-08-06 16:05:39 -070073 return BAD_VALUE;
74
Mathias Agopian61630912011-07-06 16:35:30 -070075 if (numConfigs) {
76 EGLConfig* const configs = new EGLConfig[numConfigs];
77 if (eglChooseConfig(dpy, attrs, configs, numConfigs, &n) == EGL_FALSE) {
78 delete [] configs;
79 return BAD_VALUE;
80 }
81
82 bool hasAlpha = false;
83 switch (format) {
84 case HAL_PIXEL_FORMAT_RGBA_8888:
85 case HAL_PIXEL_FORMAT_BGRA_8888:
86 case HAL_PIXEL_FORMAT_RGBA_5551:
87 case HAL_PIXEL_FORMAT_RGBA_4444:
88 hasAlpha = true;
89 break;
90 }
91
92 // The first config is guaranteed to over-satisfy the constraints
93 EGLConfig config = configs[0];
94
95 // go through the list and skip configs that over-satisfy our needs
96 int i;
97 for (i=0 ; i<n ; i++) {
98 if (!hasAlpha) {
99 EGLint alphaSize;
100 eglGetConfigAttrib(dpy, configs[i], EGL_ALPHA_SIZE, &alphaSize);
101 if (alphaSize > 0) {
102 continue;
103 }
104 }
Mathias Agopiand5ea3db2011-01-16 17:57:20 -0800105 config = configs[i];
Mathias Agopian6cf50a72009-08-06 16:05:39 -0700106 break;
107 }
Mathias Agopian6cf50a72009-08-06 16:05:39 -0700108
Mathias Agopian61630912011-07-06 16:35:30 -0700109 delete [] configs;
110
111 if (i<n) {
112 *outConfig = config;
113 return NO_ERROR;
114 }
Mathias Agopian6cf50a72009-08-06 16:05:39 -0700115 }
116
117 return NAME_NOT_FOUND;
118}
119
120status_t EGLUtils::selectConfigForNativeWindow(
121 EGLDisplay dpy,
122 EGLint const* attrs,
123 EGLNativeWindowType window,
124 EGLConfig* outConfig)
125{
126 int err;
127 int format;
Mathias Agopian42db9dc2009-08-06 20:46:44 -0700128
129 if (!window)
130 return BAD_VALUE;
131
Mathias Agopian6cf50a72009-08-06 16:05:39 -0700132 if ((err = window->query(window, NATIVE_WINDOW_FORMAT, &format)) < 0) {
133 return err;
134 }
135
136 return selectConfigForPixelFormat(dpy, attrs, format, outConfig);
137}
138
139// ----------------------------------------------------------------------------
140}; // namespace android
141// ----------------------------------------------------------------------------