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