The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | #include <stdio.h> |
| 19 | #include <string.h> |
| 20 | #include <math.h> |
| 21 | |
| 22 | #include <cutils/properties.h> |
| 23 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 24 | #include <utils/RefBase.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <utils/Log.h> |
| 26 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 27 | #include <ui/PixelFormat.h> |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 28 | #include <ui/FramebufferNativeWindow.h> |
Mathias Agopian | 6cf50a7 | 2009-08-06 16:05:39 -0700 | [diff] [blame] | 29 | #include <ui/EGLUtils.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
| 31 | #include <GLES/gl.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 32 | #include <EGL/egl.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | #include <EGL/eglext.h> |
| 34 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 35 | #include <pixelflinger/pixelflinger.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | |
| 37 | #include "DisplayHardware/DisplayHardware.h" |
| 38 | |
| 39 | #include <hardware/copybit.h> |
| 40 | #include <hardware/overlay.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 41 | #include <hardware/gralloc.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
| 43 | using namespace android; |
| 44 | |
| 45 | static __attribute__((noinline)) |
| 46 | const char *egl_strerror(EGLint err) |
| 47 | { |
| 48 | switch (err){ |
| 49 | case EGL_SUCCESS: return "EGL_SUCCESS"; |
| 50 | case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED"; |
| 51 | case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS"; |
| 52 | case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC"; |
| 53 | case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE"; |
| 54 | case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG"; |
| 55 | case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT"; |
| 56 | case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE"; |
| 57 | case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY"; |
| 58 | case EGL_BAD_MATCH: return "EGL_BAD_MATCH"; |
| 59 | case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP"; |
| 60 | case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW"; |
| 61 | case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER"; |
| 62 | case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE"; |
| 63 | case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST"; |
| 64 | default: return "UNKNOWN"; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | static __attribute__((noinline)) |
| 69 | void checkGLErrors() |
| 70 | { |
| 71 | GLenum error = glGetError(); |
| 72 | if (error != GL_NO_ERROR) |
| 73 | LOGE("GL error 0x%04x", int(error)); |
| 74 | } |
| 75 | |
| 76 | static __attribute__((noinline)) |
| 77 | void checkEGLErrors(const char* token) |
| 78 | { |
| 79 | EGLint error = eglGetError(); |
| 80 | // GLESonGL seems to be returning 0 when there is no errors? |
| 81 | if (error && error != EGL_SUCCESS) |
| 82 | LOGE("%s error 0x%04x (%s)", |
| 83 | token, int(error), egl_strerror(error)); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /* |
| 88 | * Initialize the display to the specified values. |
| 89 | * |
| 90 | */ |
| 91 | |
| 92 | DisplayHardware::DisplayHardware( |
| 93 | const sp<SurfaceFlinger>& flinger, |
| 94 | uint32_t dpy) |
| 95 | : DisplayHardwareBase(flinger, dpy) |
| 96 | { |
| 97 | init(dpy); |
| 98 | } |
| 99 | |
| 100 | DisplayHardware::~DisplayHardware() |
| 101 | { |
| 102 | fini(); |
| 103 | } |
| 104 | |
| 105 | float DisplayHardware::getDpiX() const { return mDpiX; } |
| 106 | float DisplayHardware::getDpiY() const { return mDpiY; } |
| 107 | float DisplayHardware::getDensity() const { return mDensity; } |
| 108 | float DisplayHardware::getRefreshRate() const { return mRefreshRate; } |
| 109 | int DisplayHardware::getWidth() const { return mWidth; } |
| 110 | int DisplayHardware::getHeight() const { return mHeight; } |
| 111 | PixelFormat DisplayHardware::getFormat() const { return mFormat; } |
| 112 | |
| 113 | void DisplayHardware::init(uint32_t dpy) |
| 114 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 115 | hw_module_t const* module; |
| 116 | |
| 117 | mNativeWindow = new FramebufferNativeWindow(); |
| 118 | |
| 119 | mOverlayEngine = NULL; |
| 120 | if (hw_get_module(OVERLAY_HARDWARE_MODULE_ID, &module) == 0) { |
| 121 | overlay_control_open(module, &mOverlayEngine); |
| 122 | } |
| 123 | |
| 124 | framebuffer_device_t const * fbDev = mNativeWindow->getDevice(); |
| 125 | |
| 126 | PixelFormatInfo fbFormatInfo; |
| 127 | getPixelFormatInfo(PixelFormat(fbDev->format), &fbFormatInfo); |
| 128 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | // initialize EGL |
| 130 | const EGLint attribs[] = { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 131 | EGL_BUFFER_SIZE, fbFormatInfo.bitsPerPixel, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | EGL_DEPTH_SIZE, 0, |
| 133 | EGL_NONE |
| 134 | }; |
| 135 | EGLint w, h, dummy; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 136 | EGLint numConfigs=0, n=0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | EGLSurface surface; |
| 138 | EGLContext context; |
| 139 | mFlags = 0; |
| 140 | |
| 141 | // TODO: all the extensions below should be queried through |
| 142 | // eglGetProcAddress(). |
| 143 | |
| 144 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 145 | eglInitialize(display, NULL, NULL); |
| 146 | eglGetConfigs(display, NULL, 0, &numConfigs); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 147 | |
Mathias Agopian | 6cf50a7 | 2009-08-06 16:05:39 -0700 | [diff] [blame] | 148 | EGLConfig config; |
| 149 | status_t err = EGLUtils::selectConfigForPixelFormat( |
| 150 | display, attribs, fbDev->format, &config); |
| 151 | LOGE_IF(err, "couldn't find an EGLConfig matching the screen format"); |
| 152 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | /* |
| 154 | * Gather EGL extensions |
| 155 | */ |
| 156 | |
| 157 | const char* const egl_extensions = eglQueryString( |
| 158 | display, EGL_EXTENSIONS); |
| 159 | |
| 160 | LOGI("EGL informations:"); |
| 161 | LOGI("# of configs : %d", numConfigs); |
| 162 | LOGI("vendor : %s", eglQueryString(display, EGL_VENDOR)); |
| 163 | LOGI("version : %s", eglQueryString(display, EGL_VERSION)); |
| 164 | LOGI("extensions: %s", egl_extensions); |
| 165 | LOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported"); |
| 166 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | |
Mathias Agopian | 1e16b13 | 2009-05-07 17:40:23 -0700 | [diff] [blame] | 168 | if (mNativeWindow->isUpdateOnDemand()) { |
| 169 | mFlags |= UPDATE_ON_DEMAND; |
| 170 | } |
| 171 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) { |
| 173 | if (dummy == EGL_SLOW_CONFIG) |
| 174 | mFlags |= SLOW_CONFIG; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Create our main surface |
| 179 | */ |
| 180 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 181 | surface = eglCreateWindowSurface(display, config, mNativeWindow.get(), NULL); |
Mathias Agopian | 5e78e09 | 2009-06-11 17:19:54 -0700 | [diff] [blame] | 182 | checkEGLErrors("eglCreateWindowSurface"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | if (eglQuerySurface(display, surface, EGL_SWAP_BEHAVIOR, &dummy) == EGL_TRUE) { |
| 185 | if (dummy == EGL_BUFFER_PRESERVED) { |
| 186 | mFlags |= BUFFER_PRESERVED; |
| 187 | } |
| 188 | } |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 189 | |
Mathias Agopian | 5e78e09 | 2009-06-11 17:19:54 -0700 | [diff] [blame] | 190 | #ifdef EGL_ANDROID_swap_rectangle |
Mathias Agopian | e6bf8b3 | 2009-05-06 23:47:08 -0700 | [diff] [blame] | 191 | if (strstr(egl_extensions, "EGL_ANDROID_swap_rectangle")) { |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 192 | mFlags |= SWAP_RECTANGLE; |
| 193 | } |
Mathias Agopian | 2dd6727 | 2009-06-29 18:53:53 -0700 | [diff] [blame] | 194 | // when we have the choice between UPDATE_ON_DEMAND and SWAP_RECTANGLE |
| 195 | // choose UPDATE_ON_DEMAND, which is more efficient |
| 196 | if (mFlags & UPDATE_ON_DEMAND) |
| 197 | mFlags &= ~SWAP_RECTANGLE; |
Mathias Agopian | 5e78e09 | 2009-06-11 17:19:54 -0700 | [diff] [blame] | 198 | #endif |
Mathias Agopian | 2dd6727 | 2009-06-29 18:53:53 -0700 | [diff] [blame] | 199 | |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 200 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 201 | mDpiX = mNativeWindow->xdpi; |
Mathias Agopian | 80d7a76 | 2009-07-09 22:11:57 -0700 | [diff] [blame] | 202 | mDpiY = mNativeWindow->ydpi; |
Mathias Agopian | 1e16b13 | 2009-05-07 17:40:23 -0700 | [diff] [blame] | 203 | mRefreshRate = fbDev->fps; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | |
| 205 | char property[PROPERTY_VALUE_MAX]; |
David 'Digit' Turner | ae71acc | 2009-06-19 04:41:12 +0200 | [diff] [blame] | 206 | /* Read density from build-specific ro.sf.lcd_density property |
| 207 | * except if it is overriden by qemu.sf.lcd_density. |
| 208 | */ |
| 209 | if (property_get("qemu.sf.lcd_density", property, NULL) <= 0) { |
| 210 | if (property_get("ro.sf.lcd_density", property, NULL) <= 0) { |
| 211 | LOGW("ro.sf.lcd_density not defined, using 160 dpi by default."); |
| 212 | strcpy(property, "160"); |
David 'Digit' Turner | 694e10b | 2009-06-18 04:30:32 +0200 | [diff] [blame] | 213 | } |
David 'Digit' Turner | 31469e1 | 2009-07-29 00:38:58 +0200 | [diff] [blame] | 214 | } else { |
| 215 | /* for the emulator case, reset the dpi values too */ |
| 216 | mDpiX = mDpiY = atoi(property); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | } |
| 218 | mDensity = atoi(property) * (1.0f/160.0f); |
| 219 | |
| 220 | |
| 221 | /* |
| 222 | * Create our OpenGL ES context |
| 223 | */ |
| 224 | |
| 225 | context = eglCreateContext(display, config, NULL, NULL); |
| 226 | //checkEGLErrors("eglCreateContext"); |
| 227 | |
| 228 | eglQuerySurface(display, surface, EGL_WIDTH, &mWidth); |
| 229 | eglQuerySurface(display, surface, EGL_HEIGHT, &mHeight); |
| 230 | |
| 231 | |
| 232 | /* |
| 233 | * Gather OpenGL ES extensions |
| 234 | */ |
| 235 | |
| 236 | eglMakeCurrent(display, surface, surface, context); |
| 237 | const char* const gl_extensions = (const char*)glGetString(GL_EXTENSIONS); |
| 238 | LOGI("OpenGL informations:"); |
| 239 | LOGI("vendor : %s", glGetString(GL_VENDOR)); |
| 240 | LOGI("renderer : %s", glGetString(GL_RENDERER)); |
| 241 | LOGI("version : %s", glGetString(GL_VERSION)); |
| 242 | LOGI("extensions: %s", gl_extensions); |
| 243 | |
| 244 | if (strstr(gl_extensions, "GL_ARB_texture_non_power_of_two")) { |
| 245 | mFlags |= NPOT_EXTENSION; |
| 246 | } |
| 247 | if (strstr(gl_extensions, "GL_OES_draw_texture")) { |
| 248 | mFlags |= DRAW_TEXTURE_EXTENSION; |
| 249 | } |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 250 | if (strstr( gl_extensions, "GL_OES_EGL_image") && |
Mathias Agopian | e6bf8b3 | 2009-05-06 23:47:08 -0700 | [diff] [blame] | 251 | (strstr(egl_extensions, "EGL_KHR_image_base") || |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 252 | strstr(egl_extensions, "EGL_KHR_image")) && |
| 253 | strstr(egl_extensions, "EGL_ANDROID_image_native_buffer")) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | mFlags |= DIRECT_TEXTURE; |
| 255 | } |
| 256 | |
| 257 | // Unbind the context from this thread |
| 258 | eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 259 | |
| 260 | mDisplay = display; |
| 261 | mConfig = config; |
| 262 | mSurface = surface; |
| 263 | mContext = context; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 264 | mFormat = fbDev->format; |
| 265 | mPageFlipCount = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Clean up. Throw out our local state. |
| 270 | * |
| 271 | * (It's entirely possible we'll never get here, since this is meant |
| 272 | * for real hardware, which doesn't restart.) |
| 273 | */ |
| 274 | |
| 275 | void DisplayHardware::fini() |
| 276 | { |
| 277 | eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 278 | eglTerminate(mDisplay); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | overlay_control_close(mOverlayEngine); |
| 280 | } |
| 281 | |
| 282 | void DisplayHardware::releaseScreen() const |
| 283 | { |
| 284 | DisplayHardwareBase::releaseScreen(); |
| 285 | } |
| 286 | |
| 287 | void DisplayHardware::acquireScreen() const |
| 288 | { |
| 289 | DisplayHardwareBase::acquireScreen(); |
| 290 | } |
| 291 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | uint32_t DisplayHardware::getPageFlipCount() const { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 293 | return mPageFlipCount; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /* |
| 297 | * "Flip" the front and back buffers. |
| 298 | */ |
| 299 | |
| 300 | void DisplayHardware::flip(const Region& dirty) const |
| 301 | { |
| 302 | checkGLErrors(); |
| 303 | |
| 304 | EGLDisplay dpy = mDisplay; |
| 305 | EGLSurface surface = mSurface; |
| 306 | |
Mathias Agopian | 5e78e09 | 2009-06-11 17:19:54 -0700 | [diff] [blame] | 307 | #ifdef EGL_ANDROID_swap_rectangle |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 308 | if (mFlags & SWAP_RECTANGLE) { |
Mathias Agopian | b8a5560 | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 309 | const Region newDirty(dirty.intersect(bounds())); |
| 310 | const Rect b(newDirty.getBounds()); |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 311 | eglSetSwapRectangleANDROID(dpy, surface, |
| 312 | b.left, b.top, b.width(), b.height()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | } |
Mathias Agopian | 5e78e09 | 2009-06-11 17:19:54 -0700 | [diff] [blame] | 314 | #endif |
| 315 | |
Mathias Agopian | 1e16b13 | 2009-05-07 17:40:23 -0700 | [diff] [blame] | 316 | if (mFlags & UPDATE_ON_DEMAND) { |
Mathias Agopian | 29d06ac | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 317 | mNativeWindow->setUpdateRectangle(dirty.getBounds()); |
Mathias Agopian | 1e16b13 | 2009-05-07 17:40:23 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 320 | mPageFlipCount++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | eglSwapBuffers(dpy, surface); |
| 322 | checkEGLErrors("eglSwapBuffers"); |
| 323 | |
| 324 | // for debugging |
| 325 | //glClearColor(1,0,0,0); |
| 326 | //glClear(GL_COLOR_BUFFER_BIT); |
| 327 | } |
| 328 | |
| 329 | uint32_t DisplayHardware::getFlags() const |
| 330 | { |
| 331 | return mFlags; |
| 332 | } |
| 333 | |
| 334 | void DisplayHardware::makeCurrent() const |
| 335 | { |
| 336 | eglMakeCurrent(mDisplay, mSurface, mSurface, mContext); |
| 337 | } |