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 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 39 | #include <hardware/gralloc.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 41 | #include "GLExtensions.h" |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 42 | #include "HWComposer.h" |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 43 | #include "SurfaceFlinger.h" |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 44 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | using namespace android; |
| 46 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | |
| 48 | static __attribute__((noinline)) |
| 49 | void checkGLErrors() |
| 50 | { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 51 | do { |
| 52 | // there could be more than one error flag |
| 53 | GLenum error = glGetError(); |
| 54 | if (error == GL_NO_ERROR) |
| 55 | break; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | LOGE("GL error 0x%04x", int(error)); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 57 | } while(true); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static __attribute__((noinline)) |
| 61 | void checkEGLErrors(const char* token) |
| 62 | { |
| 63 | EGLint error = eglGetError(); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 64 | if (error && error != EGL_SUCCESS) { |
| 65 | LOGE("%s: EGL error 0x%04x (%s)", |
Mathias Agopian | 0928e31 | 2009-08-07 16:38:10 -0700 | [diff] [blame] | 66 | token, int(error), EGLUtils::strerror(error)); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 67 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | } |
| 69 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | /* |
| 71 | * Initialize the display to the specified values. |
| 72 | * |
| 73 | */ |
| 74 | |
| 75 | DisplayHardware::DisplayHardware( |
| 76 | const sp<SurfaceFlinger>& flinger, |
| 77 | uint32_t dpy) |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 78 | : DisplayHardwareBase(flinger, dpy), |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 79 | mFlinger(flinger), mFlags(0), mHwc(0) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | { |
| 81 | init(dpy); |
| 82 | } |
| 83 | |
| 84 | DisplayHardware::~DisplayHardware() |
| 85 | { |
| 86 | fini(); |
| 87 | } |
| 88 | |
| 89 | float DisplayHardware::getDpiX() const { return mDpiX; } |
| 90 | float DisplayHardware::getDpiY() const { return mDpiY; } |
| 91 | float DisplayHardware::getDensity() const { return mDensity; } |
| 92 | float DisplayHardware::getRefreshRate() const { return mRefreshRate; } |
| 93 | int DisplayHardware::getWidth() const { return mWidth; } |
| 94 | int DisplayHardware::getHeight() const { return mHeight; } |
| 95 | PixelFormat DisplayHardware::getFormat() const { return mFormat; } |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 96 | uint32_t DisplayHardware::getMaxTextureSize() const { return mMaxTextureSize; } |
Mathias Agopian | 3d64e73 | 2011-04-18 15:59:24 -0700 | [diff] [blame] | 97 | |
| 98 | uint32_t DisplayHardware::getMaxViewportDims() const { |
| 99 | return mMaxViewportDims[0] < mMaxViewportDims[1] ? |
| 100 | mMaxViewportDims[0] : mMaxViewportDims[1]; |
| 101 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | |
Mathias Agopian | 6163091 | 2011-07-06 16:35:30 -0700 | [diff] [blame] | 103 | static status_t selectConfigForPixelFormat( |
| 104 | EGLDisplay dpy, |
| 105 | EGLint const* attrs, |
| 106 | PixelFormat format, |
| 107 | EGLConfig* outConfig) |
| 108 | { |
| 109 | EGLConfig config = NULL; |
| 110 | EGLint numConfigs = -1, n=0; |
| 111 | eglGetConfigs(dpy, NULL, 0, &numConfigs); |
| 112 | EGLConfig* const configs = new EGLConfig[numConfigs]; |
| 113 | eglChooseConfig(dpy, attrs, configs, numConfigs, &n); |
| 114 | for (int i=0 ; i<n ; i++) { |
| 115 | EGLint nativeVisualId = 0; |
| 116 | eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId); |
| 117 | if (nativeVisualId>0 && format == nativeVisualId) { |
| 118 | *outConfig = configs[i]; |
| 119 | delete [] configs; |
| 120 | return NO_ERROR; |
| 121 | } |
| 122 | } |
| 123 | delete [] configs; |
| 124 | return NAME_NOT_FOUND; |
| 125 | } |
| 126 | |
| 127 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | void DisplayHardware::init(uint32_t dpy) |
| 129 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 130 | mNativeWindow = new FramebufferNativeWindow(); |
Mathias Agopian | 0928e31 | 2009-08-07 16:38:10 -0700 | [diff] [blame] | 131 | framebuffer_device_t const * fbDev = mNativeWindow->getDevice(); |
Mathias Agopian | 1f339ff | 2011-07-01 17:08:43 -0700 | [diff] [blame] | 132 | if (!fbDev) { |
| 133 | LOGE("Display subsystem failed to initialize. check logs. exiting..."); |
| 134 | exit(0); |
| 135 | } |
| 136 | |
Mathias Agopian | 6163091 | 2011-07-06 16:35:30 -0700 | [diff] [blame] | 137 | int format; |
| 138 | ANativeWindow const * const window = mNativeWindow.get(); |
| 139 | window->query(window, NATIVE_WINDOW_FORMAT, &format); |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 140 | mDpiX = mNativeWindow->xdpi; |
| 141 | mDpiY = mNativeWindow->ydpi; |
| 142 | mRefreshRate = fbDev->fps; |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame^] | 143 | mNextFakeVSync = 0; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 144 | |
Mathias Agopian | 385977f | 2011-11-04 18:46:11 -0700 | [diff] [blame] | 145 | |
| 146 | /* FIXME: this is a temporary HACK until we are able to report the refresh rate |
| 147 | * properly from the HAL. The WindowManagerService now relies on this value. |
| 148 | */ |
| 149 | #ifndef REFRESH_RATE |
| 150 | mRefreshRate = fbDev->fps; |
| 151 | #else |
| 152 | mRefreshRate = REFRESH_RATE; |
| 153 | #warning "refresh rate set via makefile to REFRESH_RATE" |
| 154 | #endif |
| 155 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame^] | 156 | mRefreshPeriod = nsecs_t(1e9 / mRefreshRate); |
| 157 | |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 158 | EGLint w, h, dummy; |
| 159 | EGLint numConfigs=0; |
| 160 | EGLSurface surface; |
| 161 | EGLContext context; |
Mathias Agopian | 6163091 | 2011-07-06 16:35:30 -0700 | [diff] [blame] | 162 | EGLBoolean result; |
| 163 | status_t err; |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 164 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | // initialize EGL |
Mathias Agopian | a5b02e0 | 2009-09-04 18:49:03 -0700 | [diff] [blame] | 166 | EGLint attribs[] = { |
Mathias Agopian | 6163091 | 2011-07-06 16:35:30 -0700 | [diff] [blame] | 167 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 168 | EGL_NONE, 0, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | EGL_NONE |
| 170 | }; |
Mathias Agopian | a5b02e0 | 2009-09-04 18:49:03 -0700 | [diff] [blame] | 171 | |
| 172 | // debug: disable h/w rendering |
| 173 | char property[PROPERTY_VALUE_MAX]; |
| 174 | if (property_get("debug.sf.hw", property, NULL) > 0) { |
| 175 | if (atoi(property) == 0) { |
| 176 | LOGW("H/W composition disabled"); |
| 177 | attribs[2] = EGL_CONFIG_CAVEAT; |
| 178 | attribs[3] = EGL_SLOW_CONFIG; |
| 179 | } |
| 180 | } |
| 181 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | // TODO: all the extensions below should be queried through |
| 183 | // eglGetProcAddress(). |
| 184 | |
| 185 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 186 | eglInitialize(display, NULL, NULL); |
| 187 | eglGetConfigs(display, NULL, 0, &numConfigs); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 188 | |
Mathias Agopian | 6163091 | 2011-07-06 16:35:30 -0700 | [diff] [blame] | 189 | EGLConfig config = NULL; |
| 190 | err = selectConfigForPixelFormat(display, attribs, format, &config); |
Mathias Agopian | 6cf50a7 | 2009-08-06 16:05:39 -0700 | [diff] [blame] | 191 | LOGE_IF(err, "couldn't find an EGLConfig matching the screen format"); |
| 192 | |
Mathias Agopian | 0928e31 | 2009-08-07 16:38:10 -0700 | [diff] [blame] | 193 | EGLint r,g,b,a; |
| 194 | eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r); |
| 195 | eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g); |
| 196 | eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b); |
| 197 | eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a); |
| 198 | |
Mathias Agopian | 1e16b13 | 2009-05-07 17:40:23 -0700 | [diff] [blame] | 199 | if (mNativeWindow->isUpdateOnDemand()) { |
Mathias Agopian | 95a666b | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 200 | mFlags |= PARTIAL_UPDATES; |
Mathias Agopian | 1e16b13 | 2009-05-07 17:40:23 -0700 | [diff] [blame] | 201 | } |
| 202 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) { |
| 204 | if (dummy == EGL_SLOW_CONFIG) |
| 205 | mFlags |= SLOW_CONFIG; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Create our main surface |
| 210 | */ |
| 211 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 212 | surface = eglCreateWindowSurface(display, config, mNativeWindow.get(), NULL); |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 213 | eglQuerySurface(display, surface, EGL_WIDTH, &mWidth); |
| 214 | eglQuerySurface(display, surface, EGL_HEIGHT, &mHeight); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | |
Mathias Agopian | 95a666b | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 216 | if (mFlags & PARTIAL_UPDATES) { |
| 217 | // if we have partial updates, we definitely don't need to |
| 218 | // preserve the backbuffer, which may be costly. |
Mathias Agopian | 0928bee | 2009-09-16 20:15:42 -0700 | [diff] [blame] | 219 | eglSurfaceAttrib(display, surface, |
| 220 | EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED); |
| 221 | } |
| 222 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | if (eglQuerySurface(display, surface, EGL_SWAP_BEHAVIOR, &dummy) == EGL_TRUE) { |
| 224 | if (dummy == EGL_BUFFER_PRESERVED) { |
| 225 | mFlags |= BUFFER_PRESERVED; |
| 226 | } |
| 227 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | |
David 'Digit' Turner | ae71acc | 2009-06-19 04:41:12 +0200 | [diff] [blame] | 229 | /* Read density from build-specific ro.sf.lcd_density property |
Mathias Agopian | 24e5f52 | 2009-08-12 21:18:15 -0700 | [diff] [blame] | 230 | * except if it is overridden by qemu.sf.lcd_density. |
David 'Digit' Turner | ae71acc | 2009-06-19 04:41:12 +0200 | [diff] [blame] | 231 | */ |
| 232 | if (property_get("qemu.sf.lcd_density", property, NULL) <= 0) { |
| 233 | if (property_get("ro.sf.lcd_density", property, NULL) <= 0) { |
| 234 | LOGW("ro.sf.lcd_density not defined, using 160 dpi by default."); |
| 235 | strcpy(property, "160"); |
David 'Digit' Turner | 694e10b | 2009-06-18 04:30:32 +0200 | [diff] [blame] | 236 | } |
David 'Digit' Turner | 31469e1 | 2009-07-29 00:38:58 +0200 | [diff] [blame] | 237 | } else { |
| 238 | /* for the emulator case, reset the dpi values too */ |
| 239 | mDpiX = mDpiY = atoi(property); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | } |
| 241 | mDensity = atoi(property) * (1.0f/160.0f); |
| 242 | |
| 243 | |
| 244 | /* |
| 245 | * Create our OpenGL ES context |
| 246 | */ |
| 247 | |
Mathias Agopian | 6722681 | 2010-10-11 17:54:43 -0700 | [diff] [blame] | 248 | |
| 249 | EGLint contextAttributes[] = { |
| 250 | #ifdef EGL_IMG_context_priority |
| 251 | #ifdef HAS_CONTEXT_PRIORITY |
| 252 | #warning "using EGL_IMG_context_priority" |
| 253 | EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG, |
| 254 | #endif |
| 255 | #endif |
| 256 | EGL_NONE, EGL_NONE |
| 257 | }; |
| 258 | context = eglCreateContext(display, config, NULL, contextAttributes); |
| 259 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 266 | |
| 267 | /* |
| 268 | * Gather OpenGL ES extensions |
| 269 | */ |
| 270 | |
Mathias Agopian | 6163091 | 2011-07-06 16:35:30 -0700 | [diff] [blame] | 271 | result = eglMakeCurrent(display, surface, surface, context); |
| 272 | if (!result) { |
| 273 | LOGE("Couldn't create a working GLES context. check logs. exiting..."); |
| 274 | exit(0); |
| 275 | } |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 276 | |
| 277 | GLExtensions& extensions(GLExtensions::getInstance()); |
| 278 | extensions.initWithGLStrings( |
| 279 | glGetString(GL_VENDOR), |
| 280 | glGetString(GL_RENDERER), |
| 281 | glGetString(GL_VERSION), |
| 282 | glGetString(GL_EXTENSIONS), |
| 283 | eglQueryString(display, EGL_VENDOR), |
| 284 | eglQueryString(display, EGL_VERSION), |
| 285 | eglQueryString(display, EGL_EXTENSIONS)); |
| 286 | |
| 287 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); |
Mathias Agopian | 3d64e73 | 2011-04-18 15:59:24 -0700 | [diff] [blame] | 288 | glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims); |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 289 | |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 290 | LOGI("EGL informations:"); |
| 291 | LOGI("# of configs : %d", numConfigs); |
| 292 | LOGI("vendor : %s", extensions.getEglVendor()); |
| 293 | LOGI("version : %s", extensions.getEglVersion()); |
| 294 | LOGI("extensions: %s", extensions.getEglExtension()); |
| 295 | LOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported"); |
| 296 | LOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config); |
| 297 | |
| 298 | LOGI("OpenGL informations:"); |
| 299 | LOGI("vendor : %s", extensions.getVendor()); |
| 300 | LOGI("renderer : %s", extensions.getRenderer()); |
| 301 | LOGI("version : %s", extensions.getVersion()); |
| 302 | LOGI("extensions: %s", extensions.getExtension()); |
| 303 | LOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize); |
Mathias Agopian | 3d64e73 | 2011-04-18 15:59:24 -0700 | [diff] [blame] | 304 | LOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]); |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 305 | LOGI("flags = %08x", mFlags); |
| 306 | |
| 307 | // Unbind the context from this thread |
| 308 | eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 309 | |
| 310 | |
| 311 | // initialize the H/W composer |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 312 | mHwc = new HWComposer(mFlinger); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 313 | if (mHwc->initCheck() == NO_ERROR) { |
| 314 | mHwc->setFrameBuffer(mDisplay, mSurface); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | HWComposer& DisplayHardware::getHwComposer() const { |
| 319 | return *mHwc; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /* |
| 323 | * Clean up. Throw out our local state. |
| 324 | * |
| 325 | * (It's entirely possible we'll never get here, since this is meant |
| 326 | * for real hardware, which doesn't restart.) |
| 327 | */ |
| 328 | |
| 329 | void DisplayHardware::fini() |
| 330 | { |
| 331 | eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 332 | eglTerminate(mDisplay); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | void DisplayHardware::releaseScreen() const |
| 336 | { |
| 337 | DisplayHardwareBase::releaseScreen(); |
Antti Hatala | f5f2712 | 2010-09-09 02:33:05 -0700 | [diff] [blame] | 338 | if (mHwc->initCheck() == NO_ERROR) { |
| 339 | mHwc->release(); |
| 340 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | void DisplayHardware::acquireScreen() const |
| 344 | { |
| 345 | DisplayHardwareBase::acquireScreen(); |
| 346 | } |
| 347 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | uint32_t DisplayHardware::getPageFlipCount() const { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 349 | return mPageFlipCount; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame^] | 352 | // this needs to be thread safe |
| 353 | nsecs_t DisplayHardware::waitForVSync() const { |
| 354 | nsecs_t timestamp; |
| 355 | if (mVSync.wait(×tamp) < 0) { |
| 356 | // vsync not supported! |
| 357 | usleep( getDelayToNextVSyncUs(×tamp) ); |
| 358 | } |
| 359 | return timestamp; |
| 360 | } |
| 361 | |
| 362 | int32_t DisplayHardware::getDelayToNextVSyncUs(nsecs_t* timestamp) const { |
| 363 | Mutex::Autolock _l(mFakeVSyncMutex); |
| 364 | const nsecs_t period = mRefreshPeriod; |
| 365 | const nsecs_t now = systemTime(CLOCK_MONOTONIC); |
| 366 | nsecs_t next_vsync = mNextFakeVSync; |
| 367 | nsecs_t sleep = next_vsync - now; |
| 368 | if (sleep < 0) { |
| 369 | // we missed, find where the next vsync should be |
| 370 | sleep = (period - ((now - next_vsync) % period)); |
| 371 | next_vsync = now + sleep; |
| 372 | } |
| 373 | mNextFakeVSync = next_vsync + period; |
| 374 | timestamp[0] = next_vsync; |
| 375 | |
| 376 | // round to next microsecond |
| 377 | int32_t sleep_us = (sleep + 999LL) / 1000LL; |
| 378 | |
| 379 | // guaranteed to be > 0 |
| 380 | return sleep_us; |
| 381 | } |
| 382 | |
Mathias Agopian | 74faca2 | 2009-09-17 16:18:16 -0700 | [diff] [blame] | 383 | status_t DisplayHardware::compositionComplete() const { |
| 384 | return mNativeWindow->compositionComplete(); |
| 385 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | |
Mathias Agopian | 35b48d1 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 387 | int DisplayHardware::getCurrentBufferIndex() const { |
| 388 | return mNativeWindow->getCurrentBufferIndex(); |
| 389 | } |
| 390 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | void DisplayHardware::flip(const Region& dirty) const |
| 392 | { |
| 393 | checkGLErrors(); |
| 394 | |
| 395 | EGLDisplay dpy = mDisplay; |
| 396 | EGLSurface surface = mSurface; |
| 397 | |
Mathias Agopian | 5e78e09 | 2009-06-11 17:19:54 -0700 | [diff] [blame] | 398 | #ifdef EGL_ANDROID_swap_rectangle |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 399 | if (mFlags & SWAP_RECTANGLE) { |
Mathias Agopian | b8a5560 | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 400 | const Region newDirty(dirty.intersect(bounds())); |
| 401 | const Rect b(newDirty.getBounds()); |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 402 | eglSetSwapRectangleANDROID(dpy, surface, |
| 403 | b.left, b.top, b.width(), b.height()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | } |
Mathias Agopian | 5e78e09 | 2009-06-11 17:19:54 -0700 | [diff] [blame] | 405 | #endif |
| 406 | |
Mathias Agopian | 95a666b | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 407 | if (mFlags & PARTIAL_UPDATES) { |
Mathias Agopian | 29d06ac | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 408 | mNativeWindow->setUpdateRectangle(dirty.getBounds()); |
Mathias Agopian | 1e16b13 | 2009-05-07 17:40:23 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 411 | mPageFlipCount++; |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 412 | |
| 413 | if (mHwc->initCheck() == NO_ERROR) { |
| 414 | mHwc->commit(); |
| 415 | } else { |
| 416 | eglSwapBuffers(dpy, surface); |
| 417 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | checkEGLErrors("eglSwapBuffers"); |
| 419 | |
| 420 | // for debugging |
| 421 | //glClearColor(1,0,0,0); |
| 422 | //glClear(GL_COLOR_BUFFER_BIT); |
| 423 | } |
| 424 | |
| 425 | uint32_t DisplayHardware::getFlags() const |
| 426 | { |
| 427 | return mFlags; |
| 428 | } |
| 429 | |
| 430 | void DisplayHardware::makeCurrent() const |
| 431 | { |
| 432 | eglMakeCurrent(mDisplay, mSurface, mSurface, mContext); |
| 433 | } |
Erik Gilling | 1d21a9c | 2010-12-01 16:38:01 -0800 | [diff] [blame] | 434 | |
| 435 | void DisplayHardware::dump(String8& res) const |
| 436 | { |
| 437 | mNativeWindow->dump(res); |
| 438 | } |