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> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | |
| 30 | #include <GLES/gl.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 31 | #include <EGL/egl.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | #include <EGL/eglext.h> |
| 33 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 34 | #include <pixelflinger/pixelflinger.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
| 36 | #include "DisplayHardware/DisplayHardware.h" |
| 37 | |
| 38 | #include <hardware/copybit.h> |
| 39 | #include <hardware/overlay.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 40 | #include <hardware/gralloc.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
| 42 | using namespace android; |
| 43 | |
| 44 | static __attribute__((noinline)) |
| 45 | const char *egl_strerror(EGLint err) |
| 46 | { |
| 47 | switch (err){ |
| 48 | case EGL_SUCCESS: return "EGL_SUCCESS"; |
| 49 | case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED"; |
| 50 | case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS"; |
| 51 | case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC"; |
| 52 | case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE"; |
| 53 | case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG"; |
| 54 | case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT"; |
| 55 | case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE"; |
| 56 | case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY"; |
| 57 | case EGL_BAD_MATCH: return "EGL_BAD_MATCH"; |
| 58 | case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP"; |
| 59 | case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW"; |
| 60 | case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER"; |
| 61 | case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE"; |
| 62 | case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST"; |
| 63 | default: return "UNKNOWN"; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | static __attribute__((noinline)) |
| 68 | void checkGLErrors() |
| 69 | { |
| 70 | GLenum error = glGetError(); |
| 71 | if (error != GL_NO_ERROR) |
| 72 | LOGE("GL error 0x%04x", int(error)); |
| 73 | } |
| 74 | |
| 75 | static __attribute__((noinline)) |
| 76 | void checkEGLErrors(const char* token) |
| 77 | { |
| 78 | EGLint error = eglGetError(); |
| 79 | // GLESonGL seems to be returning 0 when there is no errors? |
| 80 | if (error && error != EGL_SUCCESS) |
| 81 | LOGE("%s error 0x%04x (%s)", |
| 82 | token, int(error), egl_strerror(error)); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /* |
| 87 | * Initialize the display to the specified values. |
| 88 | * |
| 89 | */ |
| 90 | |
| 91 | DisplayHardware::DisplayHardware( |
| 92 | const sp<SurfaceFlinger>& flinger, |
| 93 | uint32_t dpy) |
| 94 | : DisplayHardwareBase(flinger, dpy) |
| 95 | { |
| 96 | init(dpy); |
| 97 | } |
| 98 | |
| 99 | DisplayHardware::~DisplayHardware() |
| 100 | { |
| 101 | fini(); |
| 102 | } |
| 103 | |
| 104 | float DisplayHardware::getDpiX() const { return mDpiX; } |
| 105 | float DisplayHardware::getDpiY() const { return mDpiY; } |
| 106 | float DisplayHardware::getDensity() const { return mDensity; } |
| 107 | float DisplayHardware::getRefreshRate() const { return mRefreshRate; } |
| 108 | int DisplayHardware::getWidth() const { return mWidth; } |
| 109 | int DisplayHardware::getHeight() const { return mHeight; } |
| 110 | PixelFormat DisplayHardware::getFormat() const { return mFormat; } |
| 111 | |
| 112 | void DisplayHardware::init(uint32_t dpy) |
| 113 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 114 | hw_module_t const* module; |
| 115 | |
| 116 | mNativeWindow = new FramebufferNativeWindow(); |
| 117 | |
| 118 | mOverlayEngine = NULL; |
| 119 | if (hw_get_module(OVERLAY_HARDWARE_MODULE_ID, &module) == 0) { |
| 120 | overlay_control_open(module, &mOverlayEngine); |
| 121 | } |
| 122 | |
| 123 | framebuffer_device_t const * fbDev = mNativeWindow->getDevice(); |
| 124 | |
| 125 | PixelFormatInfo fbFormatInfo; |
| 126 | getPixelFormatInfo(PixelFormat(fbDev->format), &fbFormatInfo); |
| 127 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | // initialize EGL |
| 129 | const EGLint attribs[] = { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 130 | EGL_BUFFER_SIZE, fbFormatInfo.bitsPerPixel, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | EGL_DEPTH_SIZE, 0, |
| 132 | EGL_NONE |
| 133 | }; |
| 134 | EGLint w, h, dummy; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 135 | EGLint numConfigs=0, n=0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | EGLSurface surface; |
| 137 | EGLContext context; |
| 138 | mFlags = 0; |
| 139 | |
| 140 | // TODO: all the extensions below should be queried through |
| 141 | // eglGetProcAddress(). |
| 142 | |
| 143 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 144 | eglInitialize(display, NULL, NULL); |
| 145 | eglGetConfigs(display, NULL, 0, &numConfigs); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 146 | |
| 147 | // Get all the "potential match" configs... |
| 148 | EGLConfig* const configs = new EGLConfig[numConfigs]; |
| 149 | eglChooseConfig(display, attribs, configs, numConfigs, &n); |
| 150 | LOGE_IF(n<=0, "no EGLConfig available!"); |
| 151 | EGLConfig config = configs[0]; |
| 152 | if (n > 1) { |
| 153 | // if there is more than one candidate, go through the list |
| 154 | // and pick one that matches our framebuffer format |
| 155 | int fbSzA = fbFormatInfo.getSize(PixelFormatInfo::INDEX_ALPHA); |
| 156 | int fbSzR = fbFormatInfo.getSize(PixelFormatInfo::INDEX_RED); |
| 157 | int fbSzG = fbFormatInfo.getSize(PixelFormatInfo::INDEX_GREEN); |
| 158 | int fbSzB = fbFormatInfo.getSize(PixelFormatInfo::INDEX_BLUE); |
| 159 | for (int i=0 ; i<n ; i++) { |
| 160 | EGLint r,g,b,a; |
| 161 | eglGetConfigAttrib(display, configs[i], EGL_RED_SIZE, &r); |
| 162 | eglGetConfigAttrib(display, configs[i], EGL_GREEN_SIZE, &g); |
| 163 | eglGetConfigAttrib(display, configs[i], EGL_BLUE_SIZE, &b); |
| 164 | eglGetConfigAttrib(display, configs[i], EGL_ALPHA_SIZE, &a); |
| 165 | if (fbSzA == a && fbSzR == r && fbSzG == g && fbSzB == b) { |
| 166 | config = configs[i]; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | delete [] configs; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | |
| 172 | /* |
| 173 | * Gather EGL extensions |
| 174 | */ |
| 175 | |
| 176 | const char* const egl_extensions = eglQueryString( |
| 177 | display, EGL_EXTENSIONS); |
| 178 | |
| 179 | LOGI("EGL informations:"); |
| 180 | LOGI("# of configs : %d", numConfigs); |
| 181 | LOGI("vendor : %s", eglQueryString(display, EGL_VENDOR)); |
| 182 | LOGI("version : %s", eglQueryString(display, EGL_VERSION)); |
| 183 | LOGI("extensions: %s", egl_extensions); |
| 184 | LOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported"); |
| 185 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | |
Mathias Agopian | 1e16b13 | 2009-05-07 17:40:23 -0700 | [diff] [blame^] | 187 | if (mNativeWindow->isUpdateOnDemand()) { |
| 188 | mFlags |= UPDATE_ON_DEMAND; |
| 189 | } |
| 190 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) { |
| 192 | if (dummy == EGL_SLOW_CONFIG) |
| 193 | mFlags |= SLOW_CONFIG; |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Create our main surface |
| 198 | */ |
| 199 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 200 | surface = eglCreateWindowSurface(display, config, mNativeWindow.get(), NULL); |
| 201 | checkEGLErrors("eglCreateDisplaySurfaceANDROID"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | if (eglQuerySurface(display, surface, EGL_SWAP_BEHAVIOR, &dummy) == EGL_TRUE) { |
| 204 | if (dummy == EGL_BUFFER_PRESERVED) { |
| 205 | mFlags |= BUFFER_PRESERVED; |
| 206 | } |
| 207 | } |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 208 | |
Mathias Agopian | e6bf8b3 | 2009-05-06 23:47:08 -0700 | [diff] [blame] | 209 | if (strstr(egl_extensions, "EGL_ANDROID_swap_rectangle")) { |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 210 | mFlags |= SWAP_RECTANGLE; |
| 211 | } |
| 212 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 213 | mDpiX = mNativeWindow->xdpi; |
| 214 | mDpiX = mNativeWindow->ydpi; |
Mathias Agopian | 1e16b13 | 2009-05-07 17:40:23 -0700 | [diff] [blame^] | 215 | mRefreshRate = fbDev->fps; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | |
| 217 | char property[PROPERTY_VALUE_MAX]; |
| 218 | if (property_get("ro.sf.lcd_density", property, NULL) <= 0) { |
| 219 | LOGW("ro.sf.lcd_density not defined, using 160 dpi by default."); |
| 220 | strcpy(property, "160"); |
| 221 | } |
| 222 | mDensity = atoi(property) * (1.0f/160.0f); |
| 223 | |
| 224 | |
| 225 | /* |
| 226 | * Create our OpenGL ES context |
| 227 | */ |
| 228 | |
| 229 | context = eglCreateContext(display, config, NULL, NULL); |
| 230 | //checkEGLErrors("eglCreateContext"); |
| 231 | |
| 232 | eglQuerySurface(display, surface, EGL_WIDTH, &mWidth); |
| 233 | eglQuerySurface(display, surface, EGL_HEIGHT, &mHeight); |
| 234 | |
| 235 | |
| 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 254 | if (strstr( gl_extensions, "GL_OES_EGL_image") && |
Mathias Agopian | e6bf8b3 | 2009-05-06 23:47:08 -0700 | [diff] [blame] | 255 | (strstr(egl_extensions, "EGL_KHR_image_base") || |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 256 | strstr(egl_extensions, "EGL_KHR_image")) && |
| 257 | strstr(egl_extensions, "EGL_ANDROID_image_native_buffer")) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | 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 Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 268 | mFormat = fbDev->format; |
| 269 | mPageFlipCount = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | } |
| 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 | |
| 279 | void DisplayHardware::fini() |
| 280 | { |
| 281 | eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 282 | eglTerminate(mDisplay); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | overlay_control_close(mOverlayEngine); |
| 284 | } |
| 285 | |
| 286 | void DisplayHardware::releaseScreen() const |
| 287 | { |
| 288 | DisplayHardwareBase::releaseScreen(); |
| 289 | } |
| 290 | |
| 291 | void DisplayHardware::acquireScreen() const |
| 292 | { |
| 293 | DisplayHardwareBase::acquireScreen(); |
| 294 | } |
| 295 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | uint32_t DisplayHardware::getPageFlipCount() const { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 297 | return mPageFlipCount; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* |
| 301 | * "Flip" the front and back buffers. |
| 302 | */ |
| 303 | |
| 304 | void DisplayHardware::flip(const Region& dirty) const |
| 305 | { |
| 306 | checkGLErrors(); |
| 307 | |
| 308 | EGLDisplay dpy = mDisplay; |
| 309 | EGLSurface surface = mSurface; |
| 310 | |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 311 | if (mFlags & SWAP_RECTANGLE) { |
Mathias Agopian | b2dd686 | 2009-05-04 19:38:43 -0700 | [diff] [blame] | 312 | Region newDirty(dirty); |
| 313 | newDirty.andSelf(Rect(mWidth, mHeight)); |
| 314 | const Rect& b(newDirty.bounds()); |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 315 | eglSetSwapRectangleANDROID(dpy, surface, |
| 316 | b.left, b.top, b.width(), b.height()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Mathias Agopian | 1e16b13 | 2009-05-07 17:40:23 -0700 | [diff] [blame^] | 319 | if (mFlags & UPDATE_ON_DEMAND) { |
| 320 | mNativeWindow->setUpdateRectangle(dirty.bounds()); |
| 321 | } |
| 322 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 323 | mPageFlipCount++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 324 | eglSwapBuffers(dpy, surface); |
| 325 | checkEGLErrors("eglSwapBuffers"); |
| 326 | |
| 327 | // for debugging |
| 328 | //glClearColor(1,0,0,0); |
| 329 | //glClear(GL_COLOR_BUFFER_BIT); |
| 330 | } |
| 331 | |
| 332 | uint32_t DisplayHardware::getFlags() const |
| 333 | { |
| 334 | return mFlags; |
| 335 | } |
| 336 | |
| 337 | void DisplayHardware::makeCurrent() const |
| 338 | { |
| 339 | eglMakeCurrent(mDisplay, mSurface, mSurface, mContext); |
| 340 | } |
| 341 | |
| 342 | void DisplayHardware::copyFrontToImage(const copybit_image_t& front) const { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 343 | // FIXME: we need to get rid of this |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | void DisplayHardware::copyBackToImage(const copybit_image_t& front) const { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 347 | // FIXME: we need to get rid of this |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | } |