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> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
| 29 | #include <GLES/gl.h> |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 30 | #include <EGL/egl.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | #include <EGL/eglext.h> |
| 32 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 33 | #include <hardware/gralloc.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 35 | #include "DisplayHardware/FramebufferSurface.h" |
| 36 | #include "DisplayHardware/DisplayHardwareBase.h" |
| 37 | #include "DisplayHardware/HWComposer.h" |
| 38 | |
| 39 | #include "DisplayHardware.h" |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 40 | #include "GLExtensions.h" |
Mathias Agopian | c7d14e2 | 2011-08-01 16:32:21 -0700 | [diff] [blame] | 41 | #include "SurfaceFlinger.h" |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 42 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | { |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 49 | do { |
| 50 | // there could be more than one error flag |
| 51 | GLenum error = glGetError(); |
| 52 | if (error == GL_NO_ERROR) |
| 53 | break; |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 54 | ALOGE("GL error 0x%04x", int(error)); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 55 | } while(true); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static __attribute__((noinline)) |
| 59 | void checkEGLErrors(const char* token) |
| 60 | { |
Mathias Agopian | 870b8aa | 2012-02-24 16:42:46 -0800 | [diff] [blame] | 61 | struct EGLUtils { |
| 62 | static const char *strerror(EGLint err) { |
| 63 | switch (err){ |
| 64 | case EGL_SUCCESS: return "EGL_SUCCESS"; |
| 65 | case EGL_NOT_INITIALIZED: return "EGL_NOT_INITIALIZED"; |
| 66 | case EGL_BAD_ACCESS: return "EGL_BAD_ACCESS"; |
| 67 | case EGL_BAD_ALLOC: return "EGL_BAD_ALLOC"; |
| 68 | case EGL_BAD_ATTRIBUTE: return "EGL_BAD_ATTRIBUTE"; |
| 69 | case EGL_BAD_CONFIG: return "EGL_BAD_CONFIG"; |
| 70 | case EGL_BAD_CONTEXT: return "EGL_BAD_CONTEXT"; |
| 71 | case EGL_BAD_CURRENT_SURFACE: return "EGL_BAD_CURRENT_SURFACE"; |
| 72 | case EGL_BAD_DISPLAY: return "EGL_BAD_DISPLAY"; |
| 73 | case EGL_BAD_MATCH: return "EGL_BAD_MATCH"; |
| 74 | case EGL_BAD_NATIVE_PIXMAP: return "EGL_BAD_NATIVE_PIXMAP"; |
| 75 | case EGL_BAD_NATIVE_WINDOW: return "EGL_BAD_NATIVE_WINDOW"; |
| 76 | case EGL_BAD_PARAMETER: return "EGL_BAD_PARAMETER"; |
| 77 | case EGL_BAD_SURFACE: return "EGL_BAD_SURFACE"; |
| 78 | case EGL_CONTEXT_LOST: return "EGL_CONTEXT_LOST"; |
| 79 | default: return "UNKNOWN"; |
| 80 | } |
| 81 | } |
| 82 | }; |
| 83 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | EGLint error = eglGetError(); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 85 | if (error && error != EGL_SUCCESS) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 86 | ALOGE("%s: EGL error 0x%04x (%s)", |
Mathias Agopian | 0928e31 | 2009-08-07 16:38:10 -0700 | [diff] [blame] | 87 | token, int(error), EGLUtils::strerror(error)); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 88 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | } |
| 90 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | /* |
| 92 | * Initialize the display to the specified values. |
| 93 | * |
| 94 | */ |
| 95 | |
| 96 | DisplayHardware::DisplayHardware( |
| 97 | const sp<SurfaceFlinger>& flinger, |
| 98 | uint32_t dpy) |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 99 | : DisplayHardwareBase(flinger, dpy), |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame^] | 100 | mFlinger(flinger), mFlags(0), mHwc(0), mSecureLayerVisible(false) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | { |
| 102 | init(dpy); |
| 103 | } |
| 104 | |
| 105 | DisplayHardware::~DisplayHardware() |
| 106 | { |
| 107 | fini(); |
| 108 | } |
| 109 | |
| 110 | float DisplayHardware::getDpiX() const { return mDpiX; } |
| 111 | float DisplayHardware::getDpiY() const { return mDpiY; } |
| 112 | float DisplayHardware::getDensity() const { return mDensity; } |
| 113 | float DisplayHardware::getRefreshRate() const { return mRefreshRate; } |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 114 | int DisplayHardware::getWidth() const { return mDisplayWidth; } |
| 115 | int DisplayHardware::getHeight() const { return mDisplayHeight; } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | PixelFormat DisplayHardware::getFormat() const { return mFormat; } |
Mathias Agopian | ca99fb8 | 2010-04-14 16:43:44 -0700 | [diff] [blame] | 117 | uint32_t DisplayHardware::getMaxTextureSize() const { return mMaxTextureSize; } |
Mathias Agopian | 3d64e73 | 2011-04-18 15:59:24 -0700 | [diff] [blame] | 118 | |
| 119 | uint32_t DisplayHardware::getMaxViewportDims() const { |
| 120 | return mMaxViewportDims[0] < mMaxViewportDims[1] ? |
| 121 | mMaxViewportDims[0] : mMaxViewportDims[1]; |
| 122 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | |
Mathias Agopian | 6163091 | 2011-07-06 16:35:30 -0700 | [diff] [blame] | 124 | static status_t selectConfigForPixelFormat( |
| 125 | EGLDisplay dpy, |
| 126 | EGLint const* attrs, |
| 127 | PixelFormat format, |
| 128 | EGLConfig* outConfig) |
| 129 | { |
| 130 | EGLConfig config = NULL; |
| 131 | EGLint numConfigs = -1, n=0; |
| 132 | eglGetConfigs(dpy, NULL, 0, &numConfigs); |
| 133 | EGLConfig* const configs = new EGLConfig[numConfigs]; |
| 134 | eglChooseConfig(dpy, attrs, configs, numConfigs, &n); |
| 135 | for (int i=0 ; i<n ; i++) { |
| 136 | EGLint nativeVisualId = 0; |
| 137 | eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId); |
| 138 | if (nativeVisualId>0 && format == nativeVisualId) { |
| 139 | *outConfig = configs[i]; |
| 140 | delete [] configs; |
| 141 | return NO_ERROR; |
| 142 | } |
| 143 | } |
| 144 | delete [] configs; |
| 145 | return NAME_NOT_FOUND; |
| 146 | } |
| 147 | |
| 148 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | void DisplayHardware::init(uint32_t dpy) |
| 150 | { |
Mathias Agopian | 3e87601 | 2012-06-07 17:52:54 -0700 | [diff] [blame] | 151 | mNativeWindow = new FramebufferSurface(); |
Mathias Agopian | 0928e31 | 2009-08-07 16:38:10 -0700 | [diff] [blame] | 152 | framebuffer_device_t const * fbDev = mNativeWindow->getDevice(); |
Mathias Agopian | 1f339ff | 2011-07-01 17:08:43 -0700 | [diff] [blame] | 153 | if (!fbDev) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 154 | ALOGE("Display subsystem failed to initialize. check logs. exiting..."); |
Mathias Agopian | 1f339ff | 2011-07-01 17:08:43 -0700 | [diff] [blame] | 155 | exit(0); |
| 156 | } |
| 157 | |
Mathias Agopian | 6163091 | 2011-07-06 16:35:30 -0700 | [diff] [blame] | 158 | int format; |
| 159 | ANativeWindow const * const window = mNativeWindow.get(); |
| 160 | window->query(window, NATIVE_WINDOW_FORMAT, &format); |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 161 | mDpiX = mNativeWindow->xdpi; |
| 162 | mDpiY = mNativeWindow->ydpi; |
| 163 | mRefreshRate = fbDev->fps; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 164 | |
Mathias Agopian | b5dd9c0 | 2012-03-22 12:15:54 -0700 | [diff] [blame] | 165 | if (mDpiX == 0 || mDpiY == 0) { |
| 166 | ALOGE("invalid screen resolution from fb HAL (xdpi=%f, ydpi=%f), " |
| 167 | "defaulting to 160 dpi", mDpiX, mDpiY); |
| 168 | mDpiX = mDpiY = 160; |
| 169 | } |
Mathias Agopian | 385977f | 2011-11-04 18:46:11 -0700 | [diff] [blame] | 170 | |
Mathias Agopian | b5dd9c0 | 2012-03-22 12:15:54 -0700 | [diff] [blame] | 171 | class Density { |
| 172 | static int getDensityFromProperty(char const* propName) { |
| 173 | char property[PROPERTY_VALUE_MAX]; |
| 174 | int density = 0; |
| 175 | if (property_get(propName, property, NULL) > 0) { |
| 176 | density = atoi(property); |
| 177 | } |
| 178 | return density; |
| 179 | } |
| 180 | public: |
| 181 | static int getEmuDensity() { |
| 182 | return getDensityFromProperty("qemu.sf.lcd_density"); } |
| 183 | static int getBuildDensity() { |
| 184 | return getDensityFromProperty("ro.sf.lcd_density"); } |
| 185 | }; |
| 186 | |
| 187 | |
| 188 | // The density of the device is provided by a build property |
| 189 | mDensity = Density::getBuildDensity() / 160.0f; |
| 190 | |
| 191 | if (mDensity == 0) { |
| 192 | // the build doesn't provide a density -- this is wrong! |
| 193 | // use xdpi instead |
| 194 | ALOGE("ro.sf.lcd_density must be defined as a build property"); |
| 195 | mDensity = mDpiX / 160.0f; |
| 196 | } |
| 197 | |
| 198 | if (Density::getEmuDensity()) { |
| 199 | // if "qemu.sf.lcd_density" is specified, it overrides everything |
| 200 | mDpiX = mDpiY = mDensity = Density::getEmuDensity(); |
| 201 | mDensity /= 160.0f; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | |
| 206 | /* FIXME: this is a temporary HACK until we are able to report the refresh rate |
| 207 | * properly from the HAL. The WindowManagerService now relies on this value. |
| 208 | */ |
Mathias Agopian | 385977f | 2011-11-04 18:46:11 -0700 | [diff] [blame] | 209 | #ifndef REFRESH_RATE |
| 210 | mRefreshRate = fbDev->fps; |
| 211 | #else |
| 212 | mRefreshRate = REFRESH_RATE; |
| 213 | #warning "refresh rate set via makefile to REFRESH_RATE" |
| 214 | #endif |
| 215 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 216 | mRefreshPeriod = nsecs_t(1e9 / mRefreshRate); |
| 217 | |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 218 | EGLint w, h, dummy; |
| 219 | EGLint numConfigs=0; |
| 220 | EGLSurface surface; |
| 221 | EGLContext context; |
Mathias Agopian | 6163091 | 2011-07-06 16:35:30 -0700 | [diff] [blame] | 222 | EGLBoolean result; |
| 223 | status_t err; |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 224 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | // initialize EGL |
Mathias Agopian | a5b02e0 | 2009-09-04 18:49:03 -0700 | [diff] [blame] | 226 | EGLint attribs[] = { |
Mathias Agopian | 3094df3 | 2012-06-18 18:06:45 -0700 | [diff] [blame] | 227 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 228 | EGL_RECORDABLE_ANDROID, EGL_TRUE, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | EGL_NONE |
| 230 | }; |
Mathias Agopian | a5b02e0 | 2009-09-04 18:49:03 -0700 | [diff] [blame] | 231 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | // TODO: all the extensions below should be queried through |
| 233 | // eglGetProcAddress(). |
| 234 | |
| 235 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 236 | eglInitialize(display, NULL, NULL); |
| 237 | eglGetConfigs(display, NULL, 0, &numConfigs); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 238 | |
Mathias Agopian | 6163091 | 2011-07-06 16:35:30 -0700 | [diff] [blame] | 239 | EGLConfig config = NULL; |
| 240 | err = selectConfigForPixelFormat(display, attribs, format, &config); |
Mathias Agopian | 3094df3 | 2012-06-18 18:06:45 -0700 | [diff] [blame] | 241 | if (err) { |
| 242 | // maybe we failed because of EGL_RECORDABLE_ANDROID |
| 243 | ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID"); |
| 244 | attribs[2] = EGL_NONE; |
| 245 | err = selectConfigForPixelFormat(display, attribs, format, &config); |
| 246 | } |
| 247 | |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 248 | ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format"); |
Mathias Agopian | 6cf50a7 | 2009-08-06 16:05:39 -0700 | [diff] [blame] | 249 | |
Mathias Agopian | 0928e31 | 2009-08-07 16:38:10 -0700 | [diff] [blame] | 250 | EGLint r,g,b,a; |
| 251 | eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r); |
| 252 | eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g); |
| 253 | eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b); |
| 254 | eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a); |
| 255 | |
Mathias Agopian | 1e16b13 | 2009-05-07 17:40:23 -0700 | [diff] [blame] | 256 | if (mNativeWindow->isUpdateOnDemand()) { |
Mathias Agopian | 95a666b | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 257 | mFlags |= PARTIAL_UPDATES; |
Mathias Agopian | 1e16b13 | 2009-05-07 17:40:23 -0700 | [diff] [blame] | 258 | } |
| 259 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) { |
| 261 | if (dummy == EGL_SLOW_CONFIG) |
| 262 | mFlags |= SLOW_CONFIG; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Create our main surface |
| 267 | */ |
| 268 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 269 | surface = eglCreateWindowSurface(display, config, mNativeWindow.get(), NULL); |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 270 | eglQuerySurface(display, surface, EGL_WIDTH, &mDisplayWidth); |
| 271 | eglQuerySurface(display, surface, EGL_HEIGHT, &mDisplayHeight); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | |
Mathias Agopian | 95a666b | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 273 | if (mFlags & PARTIAL_UPDATES) { |
| 274 | // if we have partial updates, we definitely don't need to |
| 275 | // preserve the backbuffer, which may be costly. |
Mathias Agopian | 0928bee | 2009-09-16 20:15:42 -0700 | [diff] [blame] | 276 | eglSurfaceAttrib(display, surface, |
| 277 | EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED); |
| 278 | } |
| 279 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | /* |
| 281 | * Create our OpenGL ES context |
| 282 | */ |
| 283 | |
Mathias Agopian | 6722681 | 2010-10-11 17:54:43 -0700 | [diff] [blame] | 284 | EGLint contextAttributes[] = { |
| 285 | #ifdef EGL_IMG_context_priority |
| 286 | #ifdef HAS_CONTEXT_PRIORITY |
| 287 | #warning "using EGL_IMG_context_priority" |
| 288 | EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG, |
| 289 | #endif |
| 290 | #endif |
| 291 | EGL_NONE, EGL_NONE |
| 292 | }; |
| 293 | context = eglCreateContext(display, config, NULL, contextAttributes); |
| 294 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 295 | mDisplay = display; |
| 296 | mConfig = config; |
| 297 | mSurface = surface; |
| 298 | mContext = context; |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 299 | mFormat = fbDev->format; |
| 300 | mPageFlipCount = 0; |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 301 | |
| 302 | /* |
| 303 | * Gather OpenGL ES extensions |
| 304 | */ |
| 305 | |
Mathias Agopian | 6163091 | 2011-07-06 16:35:30 -0700 | [diff] [blame] | 306 | result = eglMakeCurrent(display, surface, surface, context); |
| 307 | if (!result) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 308 | ALOGE("Couldn't create a working GLES context. check logs. exiting..."); |
Mathias Agopian | 6163091 | 2011-07-06 16:35:30 -0700 | [diff] [blame] | 309 | exit(0); |
| 310 | } |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 311 | |
| 312 | GLExtensions& extensions(GLExtensions::getInstance()); |
| 313 | extensions.initWithGLStrings( |
| 314 | glGetString(GL_VENDOR), |
| 315 | glGetString(GL_RENDERER), |
| 316 | glGetString(GL_VERSION), |
| 317 | glGetString(GL_EXTENSIONS), |
| 318 | eglQueryString(display, EGL_VENDOR), |
| 319 | eglQueryString(display, EGL_VERSION), |
| 320 | eglQueryString(display, EGL_EXTENSIONS)); |
| 321 | |
| 322 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); |
Mathias Agopian | 3d64e73 | 2011-04-18 15:59:24 -0700 | [diff] [blame] | 323 | glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims); |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 324 | |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 325 | ALOGI("EGL informations:"); |
| 326 | ALOGI("# of configs : %d", numConfigs); |
| 327 | ALOGI("vendor : %s", extensions.getEglVendor()); |
| 328 | ALOGI("version : %s", extensions.getEglVersion()); |
| 329 | ALOGI("extensions: %s", extensions.getEglExtension()); |
| 330 | ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported"); |
| 331 | 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] | 332 | |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 333 | ALOGI("OpenGL informations:"); |
| 334 | ALOGI("vendor : %s", extensions.getVendor()); |
| 335 | ALOGI("renderer : %s", extensions.getRenderer()); |
| 336 | ALOGI("version : %s", extensions.getVersion()); |
| 337 | ALOGI("extensions: %s", extensions.getExtension()); |
| 338 | ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize); |
| 339 | ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]); |
| 340 | ALOGI("flags = %08x", mFlags); |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 341 | |
| 342 | // Unbind the context from this thread |
| 343 | eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 344 | |
| 345 | |
| 346 | // initialize the H/W composer |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 347 | mHwc = new HWComposer(mFlinger, *this, mRefreshPeriod); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 348 | if (mHwc->initCheck() == NO_ERROR) { |
| 349 | mHwc->setFrameBuffer(mDisplay, mSurface); |
| 350 | } |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 351 | |
| 352 | |
| 353 | // initialize the display orientation transform. |
| 354 | // it's a constant that should come from the display driver. |
| 355 | int displayOrientation = ISurfaceComposer::eOrientationDefault; |
| 356 | char property[PROPERTY_VALUE_MAX]; |
| 357 | if (property_get("ro.sf.hwrotation", property, NULL) > 0) { |
| 358 | //displayOrientation |
| 359 | switch (atoi(property)) { |
| 360 | case 90: |
| 361 | displayOrientation = ISurfaceComposer::eOrientation90; |
| 362 | break; |
| 363 | case 270: |
| 364 | displayOrientation = ISurfaceComposer::eOrientation270; |
| 365 | break; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | w = mDisplayWidth; |
| 370 | h = mDisplayHeight; |
| 371 | DisplayHardware::orientationToTransfrom(displayOrientation, w, h, |
| 372 | &mDisplayTransform); |
| 373 | if (displayOrientation & ISurfaceComposer::eOrientationSwapMask) { |
| 374 | mLogicalDisplayWidth = h; |
| 375 | mLogicalDisplayHeight = w; |
| 376 | } else { |
| 377 | mLogicalDisplayWidth = w; |
| 378 | mLogicalDisplayHeight = h; |
| 379 | } |
| 380 | DisplayHardware::setOrientation(ISurfaceComposer::eOrientationDefault); |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 383 | void DisplayHardware::setVSyncHandler(const sp<VSyncHandler>& handler) { |
| 384 | Mutex::Autolock _l(mLock); |
| 385 | mVSyncHandler = handler; |
| 386 | } |
| 387 | |
Mathias Agopian | 03e4072 | 2012-04-26 16:11:59 -0700 | [diff] [blame] | 388 | void DisplayHardware::eventControl(int event, int enabled) { |
| 389 | if (event == EVENT_VSYNC) { |
| 390 | mPowerHAL.vsyncHint(enabled); |
| 391 | } |
| 392 | mHwc->eventControl(event, enabled); |
| 393 | } |
| 394 | |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 395 | void DisplayHardware::onVSyncReceived(int dpy, nsecs_t timestamp) { |
| 396 | sp<VSyncHandler> handler; |
| 397 | { // scope for the lock |
| 398 | Mutex::Autolock _l(mLock); |
| 399 | mLastHwVSync = timestamp; |
| 400 | if (mVSyncHandler != NULL) { |
| 401 | handler = mVSyncHandler.promote(); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | if (handler != NULL) { |
| 406 | handler->onVSyncReceived(dpy, timestamp); |
| 407 | } |
| 408 | } |
| 409 | |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 410 | HWComposer& DisplayHardware::getHwComposer() const { |
| 411 | return *mHwc; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /* |
| 415 | * Clean up. Throw out our local state. |
| 416 | * |
| 417 | * (It's entirely possible we'll never get here, since this is meant |
| 418 | * for real hardware, which doesn't restart.) |
| 419 | */ |
| 420 | |
| 421 | void DisplayHardware::fini() |
| 422 | { |
| 423 | eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 424 | eglTerminate(mDisplay); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | void DisplayHardware::releaseScreen() const |
| 428 | { |
| 429 | DisplayHardwareBase::releaseScreen(); |
Antti Hatala | f5f2712 | 2010-09-09 02:33:05 -0700 | [diff] [blame] | 430 | if (mHwc->initCheck() == NO_ERROR) { |
| 431 | mHwc->release(); |
| 432 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void DisplayHardware::acquireScreen() const |
| 436 | { |
Colin Cross | 10fbdb6 | 2012-07-12 17:56:34 -0700 | [diff] [blame] | 437 | if (mHwc->initCheck() == NO_ERROR) { |
| 438 | mHwc->acquire(); |
| 439 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | DisplayHardwareBase::acquireScreen(); |
| 441 | } |
| 442 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 443 | uint32_t DisplayHardware::getPageFlipCount() const { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 444 | return mPageFlipCount; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 445 | } |
| 446 | |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 447 | nsecs_t DisplayHardware::getRefreshTimestamp() const { |
| 448 | // this returns the last refresh timestamp. |
| 449 | // if the last one is not available, we estimate it based on |
| 450 | // the refresh period and whatever closest timestamp we have. |
Mathias Agopian | 3eb38cb | 2012-04-03 22:09:52 -0700 | [diff] [blame] | 451 | Mutex::Autolock _l(mLock); |
| 452 | nsecs_t now = systemTime(CLOCK_MONOTONIC); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 453 | return now - ((now - mLastHwVSync) % mRefreshPeriod); |
| 454 | } |
| 455 | |
| 456 | nsecs_t DisplayHardware::getRefreshPeriod() const { |
| 457 | return mRefreshPeriod; |
| 458 | } |
| 459 | |
Mathias Agopian | 74faca2 | 2009-09-17 16:18:16 -0700 | [diff] [blame] | 460 | status_t DisplayHardware::compositionComplete() const { |
| 461 | return mNativeWindow->compositionComplete(); |
| 462 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | |
| 464 | void DisplayHardware::flip(const Region& dirty) const |
| 465 | { |
| 466 | checkGLErrors(); |
| 467 | |
| 468 | EGLDisplay dpy = mDisplay; |
| 469 | EGLSurface surface = mSurface; |
| 470 | |
Mathias Agopian | 5e78e09 | 2009-06-11 17:19:54 -0700 | [diff] [blame] | 471 | #ifdef EGL_ANDROID_swap_rectangle |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 472 | if (mFlags & SWAP_RECTANGLE) { |
Mathias Agopian | b8a5560 | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 473 | const Region newDirty(dirty.intersect(bounds())); |
| 474 | const Rect b(newDirty.getBounds()); |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 475 | eglSetSwapRectangleANDROID(dpy, surface, |
| 476 | b.left, b.top, b.width(), b.height()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | } |
Mathias Agopian | 5e78e09 | 2009-06-11 17:19:54 -0700 | [diff] [blame] | 478 | #endif |
| 479 | |
Mathias Agopian | 95a666b | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 480 | if (mFlags & PARTIAL_UPDATES) { |
Mathias Agopian | 29d06ac | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 481 | mNativeWindow->setUpdateRectangle(dirty.getBounds()); |
Mathias Agopian | 1e16b13 | 2009-05-07 17:40:23 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 484 | mPageFlipCount++; |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 485 | |
| 486 | if (mHwc->initCheck() == NO_ERROR) { |
| 487 | mHwc->commit(); |
| 488 | } else { |
| 489 | eglSwapBuffers(dpy, surface); |
| 490 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | checkEGLErrors("eglSwapBuffers"); |
| 492 | |
| 493 | // for debugging |
| 494 | //glClearColor(1,0,0,0); |
| 495 | //glClear(GL_COLOR_BUFFER_BIT); |
| 496 | } |
| 497 | |
| 498 | uint32_t DisplayHardware::getFlags() const |
| 499 | { |
| 500 | return mFlags; |
| 501 | } |
| 502 | |
| 503 | void DisplayHardware::makeCurrent() const |
| 504 | { |
| 505 | eglMakeCurrent(mDisplay, mSurface, mSurface, mContext); |
| 506 | } |
Erik Gilling | 1d21a9c | 2010-12-01 16:38:01 -0800 | [diff] [blame] | 507 | |
| 508 | void DisplayHardware::dump(String8& res) const |
| 509 | { |
| 510 | mNativeWindow->dump(res); |
| 511 | } |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 512 | |
| 513 | // ---------------------------------------------------------------------------- |
| 514 | |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame^] | 515 | void DisplayHardware::setVisibleLayersSortedByZ(const Vector< sp<LayerBase> >& layers) { |
| 516 | mVisibleLayersSortedByZ = layers; |
| 517 | size_t count = layers.size(); |
| 518 | for (size_t i=0 ; i<count ; i++) { |
| 519 | if (layers[i]->isSecure()) { |
| 520 | mSecureLayerVisible = true; |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | Vector< sp<LayerBase> > DisplayHardware::getVisibleLayersSortedByZ() const { |
| 526 | return mVisibleLayersSortedByZ; |
| 527 | } |
| 528 | |
| 529 | bool DisplayHardware::getSecureLayerVisible() const { |
| 530 | return mSecureLayerVisible; |
| 531 | } |
| 532 | |
| 533 | // ---------------------------------------------------------------------------- |
| 534 | |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 535 | status_t DisplayHardware::orientationToTransfrom( |
| 536 | int orientation, int w, int h, Transform* tr) |
| 537 | { |
| 538 | uint32_t flags = 0; |
| 539 | switch (orientation) { |
| 540 | case ISurfaceComposer::eOrientationDefault: |
| 541 | flags = Transform::ROT_0; |
| 542 | break; |
| 543 | case ISurfaceComposer::eOrientation90: |
| 544 | flags = Transform::ROT_90; |
| 545 | break; |
| 546 | case ISurfaceComposer::eOrientation180: |
| 547 | flags = Transform::ROT_180; |
| 548 | break; |
| 549 | case ISurfaceComposer::eOrientation270: |
| 550 | flags = Transform::ROT_270; |
| 551 | break; |
| 552 | default: |
| 553 | return BAD_VALUE; |
| 554 | } |
| 555 | tr->set(flags, w, h); |
| 556 | return NO_ERROR; |
| 557 | } |
| 558 | |
| 559 | status_t DisplayHardware::setOrientation(int orientation) |
| 560 | { |
| 561 | // If the rotation can be handled in hardware, this is where |
| 562 | // the magic should happen. |
| 563 | |
| 564 | const int w = mLogicalDisplayWidth; |
| 565 | const int h = mLogicalDisplayHeight; |
| 566 | mUserDisplayWidth = w; |
| 567 | mUserDisplayHeight = h; |
| 568 | |
| 569 | Transform orientationTransform; |
| 570 | DisplayHardware::orientationToTransfrom(orientation, w, h, |
| 571 | &orientationTransform); |
| 572 | if (orientation & ISurfaceComposer::eOrientationSwapMask) { |
| 573 | mUserDisplayWidth = h; |
| 574 | mUserDisplayHeight = w; |
| 575 | } |
| 576 | |
| 577 | mOrientation = orientation; |
| 578 | mGlobalTransform = mDisplayTransform * orientationTransform; |
| 579 | return NO_ERROR; |
| 580 | } |