blob: cc913cbdf9063a0b402153d7001d38386a019e78 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080017#include <stdlib.h>
18#include <stdio.h>
19#include <string.h>
20#include <math.h>
21
22#include <cutils/properties.h>
23
Mathias Agopian076b1cc2009-04-10 14:24:30 -070024#include <utils/RefBase.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025#include <utils/Log.h>
26
Mathias Agopian076b1cc2009-04-10 14:24:30 -070027#include <ui/PixelFormat.h>
Mathias Agopian0926f502009-05-04 14:17:04 -070028#include <ui/FramebufferNativeWindow.h>
Mathias Agopian6cf50a72009-08-06 16:05:39 -070029#include <ui/EGLUtils.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include <GLES/gl.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070032#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033#include <EGL/eglext.h>
34
Mathias Agopian076b1cc2009-04-10 14:24:30 -070035#include <pixelflinger/pixelflinger.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
37#include "DisplayHardware/DisplayHardware.h"
38
39#include <hardware/copybit.h>
40#include <hardware/overlay.h>
Mathias Agopian076b1cc2009-04-10 14:24:30 -070041#include <hardware/gralloc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
43using namespace android;
44
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045
46static __attribute__((noinline))
47void checkGLErrors()
48{
Mathias Agopiancbb288b2009-09-07 16:32:45 -070049 do {
50 // there could be more than one error flag
51 GLenum error = glGetError();
52 if (error == GL_NO_ERROR)
53 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054 LOGE("GL error 0x%04x", int(error));
Mathias Agopiancbb288b2009-09-07 16:32:45 -070055 } while(true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056}
57
58static __attribute__((noinline))
59void checkEGLErrors(const char* token)
60{
61 EGLint error = eglGetError();
Mathias Agopiancbb288b2009-09-07 16:32:45 -070062 if (error && error != EGL_SUCCESS) {
63 LOGE("%s: EGL error 0x%04x (%s)",
Mathias Agopian0928e312009-08-07 16:38:10 -070064 token, int(error), EGLUtils::strerror(error));
Mathias Agopiancbb288b2009-09-07 16:32:45 -070065 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066}
67
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068/*
69 * Initialize the display to the specified values.
70 *
71 */
72
73DisplayHardware::DisplayHardware(
74 const sp<SurfaceFlinger>& flinger,
75 uint32_t dpy)
76 : DisplayHardwareBase(flinger, dpy)
77{
78 init(dpy);
79}
80
81DisplayHardware::~DisplayHardware()
82{
83 fini();
84}
85
86float DisplayHardware::getDpiX() const { return mDpiX; }
87float DisplayHardware::getDpiY() const { return mDpiY; }
88float DisplayHardware::getDensity() const { return mDensity; }
89float DisplayHardware::getRefreshRate() const { return mRefreshRate; }
90int DisplayHardware::getWidth() const { return mWidth; }
91int DisplayHardware::getHeight() const { return mHeight; }
92PixelFormat DisplayHardware::getFormat() const { return mFormat; }
93
94void DisplayHardware::init(uint32_t dpy)
95{
Mathias Agopian076b1cc2009-04-10 14:24:30 -070096 mNativeWindow = new FramebufferNativeWindow();
Mathias Agopian0928e312009-08-07 16:38:10 -070097 framebuffer_device_t const * fbDev = mNativeWindow->getDevice();
Mathias Agopian076b1cc2009-04-10 14:24:30 -070098
99 mOverlayEngine = NULL;
Mathias Agopian0928e312009-08-07 16:38:10 -0700100 hw_module_t const* module;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700101 if (hw_get_module(OVERLAY_HARDWARE_MODULE_ID, &module) == 0) {
102 overlay_control_open(module, &mOverlayEngine);
103 }
104
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 // initialize EGL
Mathias Agopiana5b02e02009-09-04 18:49:03 -0700106 EGLint attribs[] = {
Mathias Agopian0928e312009-08-07 16:38:10 -0700107 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Mathias Agopiana5b02e02009-09-04 18:49:03 -0700108 EGL_NONE, 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 EGL_NONE
110 };
Mathias Agopiana5b02e02009-09-04 18:49:03 -0700111
112 // debug: disable h/w rendering
113 char property[PROPERTY_VALUE_MAX];
114 if (property_get("debug.sf.hw", property, NULL) > 0) {
115 if (atoi(property) == 0) {
116 LOGW("H/W composition disabled");
117 attribs[2] = EGL_CONFIG_CAVEAT;
118 attribs[3] = EGL_SLOW_CONFIG;
119 }
120 }
121
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122 EGLint w, h, dummy;
Mathias Agopian0928e312009-08-07 16:38:10 -0700123 EGLint numConfigs=0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 EGLSurface surface;
125 EGLContext context;
126 mFlags = 0;
127
128 // TODO: all the extensions below should be queried through
129 // eglGetProcAddress().
130
131 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
132 eglInitialize(display, NULL, NULL);
133 eglGetConfigs(display, NULL, 0, &numConfigs);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700134
Mathias Agopian6cf50a72009-08-06 16:05:39 -0700135 EGLConfig config;
Mathias Agopian0928e312009-08-07 16:38:10 -0700136 status_t err = EGLUtils::selectConfigForNativeWindow(
137 display, attribs, mNativeWindow.get(), &config);
Mathias Agopian6cf50a72009-08-06 16:05:39 -0700138 LOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
139
Mathias Agopian0928e312009-08-07 16:38:10 -0700140 EGLint r,g,b,a;
141 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
142 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
143 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
144 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
145
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 /*
147 * Gather EGL extensions
148 */
149
150 const char* const egl_extensions = eglQueryString(
151 display, EGL_EXTENSIONS);
152
153 LOGI("EGL informations:");
154 LOGI("# of configs : %d", numConfigs);
155 LOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
156 LOGI("version : %s", eglQueryString(display, EGL_VERSION));
157 LOGI("extensions: %s", egl_extensions);
158 LOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
Mathias Agopian0928e312009-08-07 16:38:10 -0700159 LOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
160
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161
Mathias Agopian1e16b132009-05-07 17:40:23 -0700162 if (mNativeWindow->isUpdateOnDemand()) {
163 mFlags |= UPDATE_ON_DEMAND;
164 }
165
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
167 if (dummy == EGL_SLOW_CONFIG)
168 mFlags |= SLOW_CONFIG;
169 }
170
171 /*
172 * Create our main surface
173 */
174
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700175 surface = eglCreateWindowSurface(display, config, mNativeWindow.get(), NULL);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176
Mathias Agopian0928bee2009-09-16 20:15:42 -0700177 if (mFlags & UPDATE_ON_DEMAND) {
178 // if we have update on demand, we definitely don't need to
179 // preserve the backbuffer, which is usually costly.
180 eglSurfaceAttrib(display, surface,
181 EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED);
182 }
183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 if (eglQuerySurface(display, surface, EGL_SWAP_BEHAVIOR, &dummy) == EGL_TRUE) {
185 if (dummy == EGL_BUFFER_PRESERVED) {
186 mFlags |= BUFFER_PRESERVED;
187 }
188 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700189
Mathias Agopian8458a312009-08-12 21:24:53 -0700190 eglQuerySurface(display, surface, EGL_WIDTH, &mWidth);
191 eglQuerySurface(display, surface, EGL_HEIGHT, &mHeight);
192
Mathias Agopian5e78e092009-06-11 17:19:54 -0700193#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiane6bf8b32009-05-06 23:47:08 -0700194 if (strstr(egl_extensions, "EGL_ANDROID_swap_rectangle")) {
Mathias Agopian8458a312009-08-12 21:24:53 -0700195 if (eglSetSwapRectangleANDROID(display, surface,
196 0, 0, mWidth, mHeight) == EGL_TRUE) {
197 // This could fail if this extension is not supported by this
198 // specific surface (of config)
199 mFlags |= SWAP_RECTANGLE;
200 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700201 }
Mathias Agopian2dd67272009-06-29 18:53:53 -0700202 // when we have the choice between UPDATE_ON_DEMAND and SWAP_RECTANGLE
203 // choose UPDATE_ON_DEMAND, which is more efficient
204 if (mFlags & UPDATE_ON_DEMAND)
205 mFlags &= ~SWAP_RECTANGLE;
Mathias Agopian5e78e092009-06-11 17:19:54 -0700206#endif
Mathias Agopian2dd67272009-06-29 18:53:53 -0700207
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700208
Mathias Agopian8458a312009-08-12 21:24:53 -0700209 LOGI("flags : %08x", mFlags);
210
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700211 mDpiX = mNativeWindow->xdpi;
Mathias Agopian80d7a762009-07-09 22:11:57 -0700212 mDpiY = mNativeWindow->ydpi;
Mathias Agopian1e16b132009-05-07 17:40:23 -0700213 mRefreshRate = fbDev->fps;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214
David 'Digit' Turnerae71acc2009-06-19 04:41:12 +0200215 /* Read density from build-specific ro.sf.lcd_density property
Mathias Agopian24e5f522009-08-12 21:18:15 -0700216 * except if it is overridden by qemu.sf.lcd_density.
David 'Digit' Turnerae71acc2009-06-19 04:41:12 +0200217 */
218 if (property_get("qemu.sf.lcd_density", property, NULL) <= 0) {
219 if (property_get("ro.sf.lcd_density", property, NULL) <= 0) {
220 LOGW("ro.sf.lcd_density not defined, using 160 dpi by default.");
221 strcpy(property, "160");
David 'Digit' Turner694e10b2009-06-18 04:30:32 +0200222 }
David 'Digit' Turner31469e12009-07-29 00:38:58 +0200223 } else {
224 /* for the emulator case, reset the dpi values too */
225 mDpiX = mDpiY = atoi(property);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 }
227 mDensity = atoi(property) * (1.0f/160.0f);
228
229
230 /*
231 * Create our OpenGL ES context
232 */
233
234 context = eglCreateContext(display, config, NULL, NULL);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800235
236 /*
237 * Gather OpenGL ES extensions
238 */
239
240 eglMakeCurrent(display, surface, surface, context);
241 const char* const gl_extensions = (const char*)glGetString(GL_EXTENSIONS);
242 LOGI("OpenGL informations:");
243 LOGI("vendor : %s", glGetString(GL_VENDOR));
244 LOGI("renderer : %s", glGetString(GL_RENDERER));
245 LOGI("version : %s", glGetString(GL_VERSION));
246 LOGI("extensions: %s", gl_extensions);
247
248 if (strstr(gl_extensions, "GL_ARB_texture_non_power_of_two")) {
249 mFlags |= NPOT_EXTENSION;
250 }
251 if (strstr(gl_extensions, "GL_OES_draw_texture")) {
252 mFlags |= DRAW_TEXTURE_EXTENSION;
253 }
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700254 if (strstr( gl_extensions, "GL_OES_EGL_image") &&
Mathias Agopiane6bf8b32009-05-06 23:47:08 -0700255 (strstr(egl_extensions, "EGL_KHR_image_base") ||
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700256 strstr(egl_extensions, "EGL_KHR_image")) &&
257 strstr(egl_extensions, "EGL_ANDROID_image_native_buffer")) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800258 mFlags |= DIRECT_TEXTURE;
259 }
260
261 // Unbind the context from this thread
262 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
263
264 mDisplay = display;
265 mConfig = config;
266 mSurface = surface;
267 mContext = context;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700268 mFormat = fbDev->format;
269 mPageFlipCount = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800270}
271
272/*
273 * Clean up. Throw out our local state.
274 *
275 * (It's entirely possible we'll never get here, since this is meant
276 * for real hardware, which doesn't restart.)
277 */
278
279void DisplayHardware::fini()
280{
281 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
282 eglTerminate(mDisplay);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800283 overlay_control_close(mOverlayEngine);
284}
285
286void DisplayHardware::releaseScreen() const
287{
288 DisplayHardwareBase::releaseScreen();
289}
290
291void DisplayHardware::acquireScreen() const
292{
293 DisplayHardwareBase::acquireScreen();
294}
295
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296uint32_t DisplayHardware::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700297 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298}
299
300/*
301 * "Flip" the front and back buffers.
302 */
303
304void DisplayHardware::flip(const Region& dirty) const
305{
306 checkGLErrors();
307
308 EGLDisplay dpy = mDisplay;
309 EGLSurface surface = mSurface;
310
Mathias Agopian5e78e092009-06-11 17:19:54 -0700311#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700312 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700313 const Region newDirty(dirty.intersect(bounds()));
314 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700315 eglSetSwapRectangleANDROID(dpy, surface,
316 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800317 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700318#endif
319
Mathias Agopian1e16b132009-05-07 17:40:23 -0700320 if (mFlags & UPDATE_ON_DEMAND) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700321 mNativeWindow->setUpdateRectangle(dirty.getBounds());
Mathias Agopian1e16b132009-05-07 17:40:23 -0700322 }
323
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700324 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800325 eglSwapBuffers(dpy, surface);
326 checkEGLErrors("eglSwapBuffers");
327
328 // for debugging
329 //glClearColor(1,0,0,0);
330 //glClear(GL_COLOR_BUFFER_BIT);
331}
332
333uint32_t DisplayHardware::getFlags() const
334{
335 return mFlags;
336}
337
338void DisplayHardware::makeCurrent() const
339{
340 eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
341}