blob: ea68352828f1fb5857ccc80e73e5cb692c4ed421 [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; }
Mathias Agopianca99fb82010-04-14 16:43:44 -070093uint32_t DisplayHardware::getMaxTextureSize() const { return mMaxTextureSize; }
94uint32_t DisplayHardware::getMaxViewportDims() const { return mMaxViewportDims; }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095
96void DisplayHardware::init(uint32_t dpy)
97{
Mathias Agopian076b1cc2009-04-10 14:24:30 -070098 mNativeWindow = new FramebufferNativeWindow();
Mathias Agopian0928e312009-08-07 16:38:10 -070099 framebuffer_device_t const * fbDev = mNativeWindow->getDevice();
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700100
101 mOverlayEngine = NULL;
Mathias Agopian0928e312009-08-07 16:38:10 -0700102 hw_module_t const* module;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700103 if (hw_get_module(OVERLAY_HARDWARE_MODULE_ID, &module) == 0) {
104 overlay_control_open(module, &mOverlayEngine);
105 }
106
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 // initialize EGL
Mathias Agopiana5b02e02009-09-04 18:49:03 -0700108 EGLint attribs[] = {
Mathias Agopian0928e312009-08-07 16:38:10 -0700109 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
Mathias Agopiana5b02e02009-09-04 18:49:03 -0700110 EGL_NONE, 0,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 EGL_NONE
112 };
Mathias Agopiana5b02e02009-09-04 18:49:03 -0700113
114 // debug: disable h/w rendering
115 char property[PROPERTY_VALUE_MAX];
116 if (property_get("debug.sf.hw", property, NULL) > 0) {
117 if (atoi(property) == 0) {
118 LOGW("H/W composition disabled");
119 attribs[2] = EGL_CONFIG_CAVEAT;
120 attribs[3] = EGL_SLOW_CONFIG;
121 }
122 }
123
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 EGLint w, h, dummy;
Mathias Agopian0928e312009-08-07 16:38:10 -0700125 EGLint numConfigs=0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126 EGLSurface surface;
127 EGLContext context;
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700128 mFlags = CACHED_BUFFERS;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129
130 // TODO: all the extensions below should be queried through
131 // eglGetProcAddress().
132
133 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
134 eglInitialize(display, NULL, NULL);
135 eglGetConfigs(display, NULL, 0, &numConfigs);
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700136
Mathias Agopian6cf50a72009-08-06 16:05:39 -0700137 EGLConfig config;
Mathias Agopian0928e312009-08-07 16:38:10 -0700138 status_t err = EGLUtils::selectConfigForNativeWindow(
139 display, attribs, mNativeWindow.get(), &config);
Mathias Agopian6cf50a72009-08-06 16:05:39 -0700140 LOGE_IF(err, "couldn't find an EGLConfig matching the screen format");
141
Mathias Agopian0928e312009-08-07 16:38:10 -0700142 EGLint r,g,b,a;
143 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
144 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
145 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
146 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
147
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148 /*
149 * Gather EGL extensions
150 */
151
152 const char* const egl_extensions = eglQueryString(
153 display, EGL_EXTENSIONS);
154
155 LOGI("EGL informations:");
156 LOGI("# of configs : %d", numConfigs);
157 LOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
158 LOGI("version : %s", eglQueryString(display, EGL_VERSION));
159 LOGI("extensions: %s", egl_extensions);
160 LOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
Mathias Agopian0928e312009-08-07 16:38:10 -0700161 LOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
162
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800163
Mathias Agopian1e16b132009-05-07 17:40:23 -0700164 if (mNativeWindow->isUpdateOnDemand()) {
Mathias Agopian95a666b2009-09-24 14:57:26 -0700165 mFlags |= PARTIAL_UPDATES;
Mathias Agopian1e16b132009-05-07 17:40:23 -0700166 }
167
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) {
169 if (dummy == EGL_SLOW_CONFIG)
170 mFlags |= SLOW_CONFIG;
171 }
172
173 /*
174 * Create our main surface
175 */
176
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700177 surface = eglCreateWindowSurface(display, config, mNativeWindow.get(), NULL);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178
Mathias Agopian95a666b2009-09-24 14:57:26 -0700179 if (mFlags & PARTIAL_UPDATES) {
180 // if we have partial updates, we definitely don't need to
181 // preserve the backbuffer, which may be costly.
Mathias Agopian0928bee2009-09-16 20:15:42 -0700182 eglSurfaceAttrib(display, surface,
183 EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED);
184 }
185
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186 if (eglQuerySurface(display, surface, EGL_SWAP_BEHAVIOR, &dummy) == EGL_TRUE) {
187 if (dummy == EGL_BUFFER_PRESERVED) {
188 mFlags |= BUFFER_PRESERVED;
189 }
190 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700191
Mathias Agopian8458a312009-08-12 21:24:53 -0700192 eglQuerySurface(display, surface, EGL_WIDTH, &mWidth);
193 eglQuerySurface(display, surface, EGL_HEIGHT, &mHeight);
194
Mathias Agopian5e78e092009-06-11 17:19:54 -0700195#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiane6bf8b32009-05-06 23:47:08 -0700196 if (strstr(egl_extensions, "EGL_ANDROID_swap_rectangle")) {
Mathias Agopian8458a312009-08-12 21:24:53 -0700197 if (eglSetSwapRectangleANDROID(display, surface,
198 0, 0, mWidth, mHeight) == EGL_TRUE) {
199 // This could fail if this extension is not supported by this
200 // specific surface (of config)
201 mFlags |= SWAP_RECTANGLE;
202 }
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700203 }
Mathias Agopian95a666b2009-09-24 14:57:26 -0700204 // when we have the choice between PARTIAL_UPDATES and SWAP_RECTANGLE
205 // choose PARTIAL_UPDATES, which should be more efficient
206 if (mFlags & PARTIAL_UPDATES)
Mathias Agopian2dd67272009-06-29 18:53:53 -0700207 mFlags &= ~SWAP_RECTANGLE;
Mathias Agopian5e78e092009-06-11 17:19:54 -0700208#endif
Mathias Agopian2dd67272009-06-29 18:53:53 -0700209
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700210
Mathias Agopian8458a312009-08-12 21:24:53 -0700211 LOGI("flags : %08x", mFlags);
212
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700213 mDpiX = mNativeWindow->xdpi;
Mathias Agopian80d7a762009-07-09 22:11:57 -0700214 mDpiY = mNativeWindow->ydpi;
Mathias Agopian1e16b132009-05-07 17:40:23 -0700215 mRefreshRate = fbDev->fps;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216
David 'Digit' Turnerae71acc2009-06-19 04:41:12 +0200217 /* Read density from build-specific ro.sf.lcd_density property
Mathias Agopian24e5f522009-08-12 21:18:15 -0700218 * except if it is overridden by qemu.sf.lcd_density.
David 'Digit' Turnerae71acc2009-06-19 04:41:12 +0200219 */
220 if (property_get("qemu.sf.lcd_density", property, NULL) <= 0) {
221 if (property_get("ro.sf.lcd_density", property, NULL) <= 0) {
222 LOGW("ro.sf.lcd_density not defined, using 160 dpi by default.");
223 strcpy(property, "160");
David 'Digit' Turner694e10b2009-06-18 04:30:32 +0200224 }
David 'Digit' Turner31469e12009-07-29 00:38:58 +0200225 } else {
226 /* for the emulator case, reset the dpi values too */
227 mDpiX = mDpiY = atoi(property);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800228 }
229 mDensity = atoi(property) * (1.0f/160.0f);
230
231
232 /*
233 * Create our OpenGL ES context
234 */
235
236 context = eglCreateContext(display, config, NULL, NULL);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800237
238 /*
239 * Gather OpenGL ES extensions
240 */
241
242 eglMakeCurrent(display, surface, surface, context);
243 const char* const gl_extensions = (const char*)glGetString(GL_EXTENSIONS);
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700244 const char* const gl_renderer = (const char*)glGetString(GL_RENDERER);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245 LOGI("OpenGL informations:");
246 LOGI("vendor : %s", glGetString(GL_VENDOR));
Mathias Agopiana4b740e2009-10-05 18:20:39 -0700247 LOGI("renderer : %s", gl_renderer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248 LOGI("version : %s", glGetString(GL_VERSION));
249 LOGI("extensions: %s", gl_extensions);
250
Mathias Agopianca99fb82010-04-14 16:43:44 -0700251 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
252 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, &mMaxViewportDims);
253 LOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize);
254 LOGI("GL_MAX_VIEWPORT_DIMS = %d", mMaxViewportDims);
255
Dima Zavin4fe6ac32010-03-02 15:39:44 -0800256#if 0
257 // for drivers that don't have proper support for flushing cached buffers
258 // on gralloc unlock, uncomment this block and test for the specific
259 // renderer substring
260 if (strstr(gl_renderer, "<some vendor string>")) {
Mathias Agopian67838bc2009-10-06 22:23:23 -0700261 LOGD("Assuming uncached graphics buffers.");
262 mFlags &= ~CACHED_BUFFERS;
263 }
Dima Zavin4fe6ac32010-03-02 15:39:44 -0800264#endif
Mathias Agopiandcaf29a2009-11-13 18:54:14 -0800265
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266 if (strstr(gl_extensions, "GL_ARB_texture_non_power_of_two")) {
267 mFlags |= NPOT_EXTENSION;
268 }
269 if (strstr(gl_extensions, "GL_OES_draw_texture")) {
270 mFlags |= DRAW_TEXTURE_EXTENSION;
271 }
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700272#ifdef EGL_ANDROID_image_native_buffer
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700273 if (strstr( gl_extensions, "GL_OES_EGL_image") &&
Mathias Agopiane6bf8b32009-05-06 23:47:08 -0700274 (strstr(egl_extensions, "EGL_KHR_image_base") ||
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700275 strstr(egl_extensions, "EGL_KHR_image")) &&
276 strstr(egl_extensions, "EGL_ANDROID_image_native_buffer")) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800277 mFlags |= DIRECT_TEXTURE;
278 }
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700279#else
280#warning "EGL_ANDROID_image_native_buffer not supported"
281#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282
Mathias Agopianca99fb82010-04-14 16:43:44 -0700283
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800284 // Unbind the context from this thread
285 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
286
287 mDisplay = display;
288 mConfig = config;
289 mSurface = surface;
290 mContext = context;
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700291 mFormat = fbDev->format;
292 mPageFlipCount = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800293}
294
295/*
296 * Clean up. Throw out our local state.
297 *
298 * (It's entirely possible we'll never get here, since this is meant
299 * for real hardware, which doesn't restart.)
300 */
301
302void DisplayHardware::fini()
303{
304 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
305 eglTerminate(mDisplay);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306 overlay_control_close(mOverlayEngine);
307}
308
309void DisplayHardware::releaseScreen() const
310{
311 DisplayHardwareBase::releaseScreen();
312}
313
314void DisplayHardware::acquireScreen() const
315{
316 DisplayHardwareBase::acquireScreen();
317}
318
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319uint32_t DisplayHardware::getPageFlipCount() const {
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700320 return mPageFlipCount;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800321}
322
Mathias Agopian74faca22009-09-17 16:18:16 -0700323status_t DisplayHardware::compositionComplete() const {
324 return mNativeWindow->compositionComplete();
325}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800326
327void DisplayHardware::flip(const Region& dirty) const
328{
329 checkGLErrors();
330
331 EGLDisplay dpy = mDisplay;
332 EGLSurface surface = mSurface;
333
Mathias Agopian5e78e092009-06-11 17:19:54 -0700334#ifdef EGL_ANDROID_swap_rectangle
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700335 if (mFlags & SWAP_RECTANGLE) {
Mathias Agopianb8a55602009-06-26 19:06:36 -0700336 const Region newDirty(dirty.intersect(bounds()));
337 const Rect b(newDirty.getBounds());
Mathias Agopiandf3ca302009-05-04 19:29:25 -0700338 eglSetSwapRectangleANDROID(dpy, surface,
339 b.left, b.top, b.width(), b.height());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800340 }
Mathias Agopian5e78e092009-06-11 17:19:54 -0700341#endif
342
Mathias Agopian95a666b2009-09-24 14:57:26 -0700343 if (mFlags & PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -0700344 mNativeWindow->setUpdateRectangle(dirty.getBounds());
Mathias Agopian1e16b132009-05-07 17:40:23 -0700345 }
346
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700347 mPageFlipCount++;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800348 eglSwapBuffers(dpy, surface);
349 checkEGLErrors("eglSwapBuffers");
350
351 // for debugging
352 //glClearColor(1,0,0,0);
353 //glClear(GL_COLOR_BUFFER_BIT);
354}
355
356uint32_t DisplayHardware::getFlags() const
357{
358 return mFlags;
359}
360
361void DisplayHardware::makeCurrent() const
362{
363 eglMakeCurrent(mDisplay, mSurface, mSurface, mContext);
364}