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