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 | |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 17 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 18 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | #include <stdint.h> |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 20 | #include <sys/types.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include <errno.h> |
| 22 | #include <math.h> |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 23 | |
| 24 | #include <EGL/egl.h> |
| 25 | #include <GLES/gl.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
| 27 | #include <cutils/log.h> |
| 28 | #include <cutils/properties.h> |
| 29 | |
Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 30 | #include <binder/IPCThreadState.h> |
| 31 | #include <binder/IServiceManager.h> |
Mathias Agopian | 7303c6b | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 32 | #include <binder/MemoryHeapBase.h> |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 33 | #include <binder/PermissionCache.h> |
Mathias Agopian | 7303c6b | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 34 | |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 35 | #include <ui/DisplayInfo.h> |
| 36 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 37 | #include <gui/IDisplayEventConnection.h> |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 38 | #include <gui/BitTube.h> |
| 39 | #include <gui/SurfaceTextureClient.h> |
| 40 | |
| 41 | #include <ui/GraphicBufferAllocator.h> |
| 42 | #include <ui/PixelFormat.h> |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 43 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | #include <utils/String8.h> |
| 45 | #include <utils/String16.h> |
| 46 | #include <utils/StopWatch.h> |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 47 | #include <utils/Trace.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 49 | #include <private/android_filesystem_config.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | |
| 51 | #include "clz.h" |
Mathias Agopian | 90ac799 | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 52 | #include "DdmConnection.h" |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 53 | #include "DisplayDevice.h" |
Mathias Agopian | db403e8 | 2012-06-18 16:47:56 -0700 | [diff] [blame] | 54 | #include "Client.h" |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 55 | #include "EventThread.h" |
Mathias Agopian | 1f7bec6 | 2010-06-25 18:02:21 -0700 | [diff] [blame] | 56 | #include "GLExtensions.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | #include "Layer.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | #include "LayerDim.h" |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 59 | #include "LayerScreenshot.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | #include "SurfaceFlinger.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 62 | #include "DisplayHardware/FramebufferSurface.h" |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 63 | #include "DisplayHardware/HWComposer.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 65 | |
Mathias Agopian | bc2d79e | 2011-11-29 17:55:46 -0800 | [diff] [blame] | 66 | #define EGL_VERSION_HW_ANDROID 0x3143 |
| 67 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | #define DISPLAY_COUNT 1 |
| 69 | |
| 70 | namespace android { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | // --------------------------------------------------------------------------- |
| 72 | |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 73 | const String16 sHardwareTest("android.permission.HARDWARE_TEST"); |
| 74 | const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER"); |
| 75 | const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER"); |
| 76 | const String16 sDump("android.permission.DUMP"); |
| 77 | |
| 78 | // --------------------------------------------------------------------------- |
| 79 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | SurfaceFlinger::SurfaceFlinger() |
| 81 | : BnSurfaceComposer(), Thread(false), |
| 82 | mTransactionFlags(0), |
Jamie Gennis | 2837839 | 2011-10-12 17:39:00 -0700 | [diff] [blame] | 83 | mTransationPending(false), |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 84 | mLayersRemoved(false), |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 85 | mRepaintEverything(0), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | mBootTime(systemTime()), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | mVisibleRegionsDirty(false), |
Mathias Agopian | a350ff9 | 2010-08-10 17:14:02 -0700 | [diff] [blame] | 88 | mHwWorkListDirty(false), |
Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 89 | mElectronBeamAnimationMode(0), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | mDebugRegion(0), |
Mathias Agopian | 8afb7e3 | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 91 | mDebugDDMS(0), |
Mathias Agopian | 73d3ba9 | 2010-09-22 18:58:01 -0700 | [diff] [blame] | 92 | mDebugDisableHWC(0), |
Mathias Agopian | a458364 | 2011-08-23 18:03:18 -0700 | [diff] [blame] | 93 | mDebugDisableTransformHint(0), |
Mathias Agopian | 9795c42 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 94 | mDebugInSwapBuffers(0), |
| 95 | mLastSwapBufferTime(0), |
| 96 | mDebugInTransaction(0), |
| 97 | mLastTransactionTime(0), |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 98 | mBootFinished(false), |
Mathias Agopian | 3094df3 | 2012-06-18 18:06:45 -0700 | [diff] [blame] | 99 | mExternalDisplaySurface(EGL_NO_SURFACE) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | { |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 101 | ALOGI("SurfaceFlinger is starting"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | |
| 103 | // debugging stuff... |
| 104 | char value[PROPERTY_VALUE_MAX]; |
Mathias Agopian | 8afb7e3 | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 105 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | property_get("debug.sf.showupdates", value, "0"); |
| 107 | mDebugRegion = atoi(value); |
Mathias Agopian | 8afb7e3 | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 108 | |
Colin Cross | 3854ed5 | 2012-03-23 14:17:18 -0700 | [diff] [blame] | 109 | #ifdef DDMS_DEBUGGING |
Mathias Agopian | 8afb7e3 | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 110 | property_get("debug.sf.ddms", value, "0"); |
| 111 | mDebugDDMS = atoi(value); |
| 112 | if (mDebugDDMS) { |
| 113 | DdmConnection::start(getServiceName()); |
| 114 | } |
Mathias Agopian | 5df9962 | 2012-06-18 17:27:56 -0700 | [diff] [blame] | 115 | #else |
| 116 | #warning "DDMS_DEBUGGING disabled" |
Colin Cross | 3854ed5 | 2012-03-23 14:17:18 -0700 | [diff] [blame] | 117 | #endif |
Mathias Agopian | 8afb7e3 | 2011-08-15 20:44:40 -0700 | [diff] [blame] | 118 | |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 119 | ALOGI_IF(mDebugRegion, "showupdates enabled"); |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 120 | ALOGI_IF(mDebugDDMS, "DDMS debugging enabled"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 123 | void SurfaceFlinger::onFirstRef() |
| 124 | { |
| 125 | mEventQueue.init(this); |
| 126 | |
| 127 | run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY); |
| 128 | |
| 129 | // Wait for the main thread to be done with its initialization |
| 130 | mReadyToRunBarrier.wait(); |
| 131 | } |
| 132 | |
| 133 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | SurfaceFlinger::~SurfaceFlinger() |
| 135 | { |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 136 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 137 | eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 138 | eglTerminate(display); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 141 | void SurfaceFlinger::binderDied(const wp<IBinder>& who) |
| 142 | { |
| 143 | // the window manager died on us. prepare its eulogy. |
| 144 | |
| 145 | // reset screen orientation |
| 146 | Vector<ComposerState> state; |
Mathias Agopian | 8b33f03 | 2012-07-24 20:43:54 -0700 | [diff] [blame] | 147 | Vector<DisplayState> displays; |
| 148 | DisplayState d; |
| 149 | d.orientation = eOrientationDefault; |
| 150 | displays.add(d); |
| 151 | setTransactionState(state, displays, 0); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 152 | |
| 153 | // restart the boot-animation |
Mathias Agopian | a67e418 | 2012-06-19 17:26:12 -0700 | [diff] [blame] | 154 | startBootAnim(); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Mathias Agopian | 7e27f05 | 2010-05-28 14:22:23 -0700 | [diff] [blame] | 157 | sp<ISurfaceComposerClient> SurfaceFlinger::createConnection() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 | { |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 159 | sp<ISurfaceComposerClient> bclient; |
| 160 | sp<Client> client(new Client(this)); |
| 161 | status_t err = client->initCheck(); |
| 162 | if (err == NO_ERROR) { |
| 163 | bclient = client; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | return bclient; |
| 166 | } |
| 167 | |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 168 | sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc() |
| 169 | { |
| 170 | sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc()); |
| 171 | return gba; |
| 172 | } |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 173 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | void SurfaceFlinger::bootFinished() |
| 175 | { |
| 176 | const nsecs_t now = systemTime(); |
| 177 | const nsecs_t duration = now - mBootTime; |
Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 178 | ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) ); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 179 | mBootFinished = true; |
Mathias Agopian | 1f339ff | 2011-07-01 17:08:43 -0700 | [diff] [blame] | 180 | |
| 181 | // wait patiently for the window manager death |
| 182 | const String16 name("window"); |
| 183 | sp<IBinder> window(defaultServiceManager()->getService(name)); |
| 184 | if (window != 0) { |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 185 | window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this)); |
Mathias Agopian | 1f339ff | 2011-07-01 17:08:43 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // stop boot animation |
Mathias Agopian | a67e418 | 2012-06-19 17:26:12 -0700 | [diff] [blame] | 189 | // formerly we would just kill the process, but we now ask it to exit so it |
| 190 | // can choose where to stop the animation. |
| 191 | property_set("service.bootanim.exit", "1"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 194 | void SurfaceFlinger::deleteTextureAsync(GLuint texture) { |
| 195 | class MessageDestroyGLTexture : public MessageBase { |
| 196 | GLuint texture; |
| 197 | public: |
| 198 | MessageDestroyGLTexture(GLuint texture) |
| 199 | : texture(texture) { |
| 200 | } |
| 201 | virtual bool handler() { |
| 202 | glDeleteTextures(1, &texture); |
| 203 | return true; |
| 204 | } |
| 205 | }; |
| 206 | postMessageAsync(new MessageDestroyGLTexture(texture)); |
| 207 | } |
| 208 | |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 209 | status_t SurfaceFlinger::selectConfigForPixelFormat( |
| 210 | EGLDisplay dpy, |
| 211 | EGLint const* attrs, |
| 212 | PixelFormat format, |
| 213 | EGLConfig* outConfig) |
| 214 | { |
| 215 | EGLConfig config = NULL; |
| 216 | EGLint numConfigs = -1, n=0; |
| 217 | eglGetConfigs(dpy, NULL, 0, &numConfigs); |
| 218 | EGLConfig* const configs = new EGLConfig[numConfigs]; |
| 219 | eglChooseConfig(dpy, attrs, configs, numConfigs, &n); |
| 220 | for (int i=0 ; i<n ; i++) { |
| 221 | EGLint nativeVisualId = 0; |
| 222 | eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &nativeVisualId); |
| 223 | if (nativeVisualId>0 && format == nativeVisualId) { |
| 224 | *outConfig = configs[i]; |
| 225 | delete [] configs; |
| 226 | return NO_ERROR; |
| 227 | } |
| 228 | } |
| 229 | delete [] configs; |
| 230 | return NAME_NOT_FOUND; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 233 | EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) { |
| 234 | // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if |
| 235 | // it is to be used with WIFI displays |
| 236 | EGLConfig config; |
| 237 | EGLint dummy; |
| 238 | status_t err; |
| 239 | EGLint attribs[] = { |
| 240 | EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 241 | EGL_RECORDABLE_ANDROID, EGL_TRUE, |
| 242 | EGL_NONE |
| 243 | }; |
| 244 | err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config); |
| 245 | if (err) { |
| 246 | // maybe we failed because of EGL_RECORDABLE_ANDROID |
| 247 | ALOGW("couldn't find an EGLConfig with EGL_RECORDABLE_ANDROID"); |
| 248 | attribs[2] = EGL_NONE; |
| 249 | err = selectConfigForPixelFormat(display, attribs, nativeVisualId, &config); |
| 250 | } |
| 251 | ALOGE_IF(err, "couldn't find an EGLConfig matching the screen format"); |
| 252 | if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy) == EGL_TRUE) { |
| 253 | ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!"); |
| 254 | } |
| 255 | return config; |
| 256 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 258 | EGLContext SurfaceFlinger::createGLContext(EGLDisplay display, EGLConfig config) { |
| 259 | // Also create our EGLContext |
| 260 | EGLint contextAttributes[] = { |
| 261 | #ifdef EGL_IMG_context_priority |
| 262 | #ifdef HAS_CONTEXT_PRIORITY |
| 263 | #warning "using EGL_IMG_context_priority" |
| 264 | EGL_CONTEXT_PRIORITY_LEVEL_IMG, EGL_CONTEXT_PRIORITY_HIGH_IMG, |
| 265 | #endif |
| 266 | #endif |
| 267 | EGL_NONE, EGL_NONE |
| 268 | }; |
| 269 | EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes); |
| 270 | ALOGE_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed"); |
| 271 | return ctxt; |
| 272 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 274 | void SurfaceFlinger::initializeGL(EGLDisplay display, EGLSurface surface) { |
| 275 | EGLBoolean result = eglMakeCurrent(display, surface, surface, mEGLContext); |
| 276 | if (!result) { |
| 277 | ALOGE("Couldn't create a working GLES context. check logs. exiting..."); |
| 278 | exit(0); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | } |
| 280 | |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 281 | GLExtensions& extensions(GLExtensions::getInstance()); |
| 282 | extensions.initWithGLStrings( |
| 283 | glGetString(GL_VENDOR), |
| 284 | glGetString(GL_RENDERER), |
| 285 | glGetString(GL_VERSION), |
| 286 | glGetString(GL_EXTENSIONS), |
| 287 | eglQueryString(display, EGL_VENDOR), |
| 288 | eglQueryString(display, EGL_VERSION), |
| 289 | eglQueryString(display, EGL_EXTENSIONS)); |
Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 290 | |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 291 | EGLint w, h; |
| 292 | eglQuerySurface(display, surface, EGL_WIDTH, &w); |
| 293 | eglQuerySurface(display, surface, EGL_HEIGHT, &h); |
Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 294 | |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 295 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); |
| 296 | glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims); |
Mathias Agopian | 7303c6b | 2009-07-02 18:11:53 -0700 | [diff] [blame] | 297 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 299 | glPixelStorei(GL_PACK_ALIGNMENT, 4); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | glEnableClientState(GL_VERTEX_ARRAY); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | glShadeModel(GL_FLAT); |
| 302 | glDisable(GL_DITHER); |
| 303 | glDisable(GL_CULL_FACE); |
| 304 | |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 305 | struct pack565 { |
| 306 | inline uint16_t operator() (int r, int g, int b) const { |
| 307 | return (r<<11)|(g<<5)|b; |
| 308 | } |
| 309 | } pack565; |
| 310 | |
Jamie Gennis | 9575f60 | 2011-10-07 14:51:16 -0700 | [diff] [blame] | 311 | const uint16_t protTexData[] = { pack565(0x03, 0x03, 0x03) }; |
| 312 | glGenTextures(1, &mProtectedTexName); |
| 313 | glBindTexture(GL_TEXTURE_2D, mProtectedTexName); |
| 314 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 315 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 316 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 317 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 318 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, |
| 319 | GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | |
| 321 | glViewport(0, 0, w, h); |
| 322 | glMatrixMode(GL_PROJECTION); |
| 323 | glLoadIdentity(); |
Mathias Agopian | ffcf465 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 324 | // put the origin in the left-bottom corner |
| 325 | glOrthof(0, w, 0, h, 0, 1); // l=0, r=w ; b=0, t=h |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 327 | // print some debugging info |
| 328 | EGLint r,g,b,a; |
| 329 | eglGetConfigAttrib(display, mEGLConfig, EGL_RED_SIZE, &r); |
| 330 | eglGetConfigAttrib(display, mEGLConfig, EGL_GREEN_SIZE, &g); |
| 331 | eglGetConfigAttrib(display, mEGLConfig, EGL_BLUE_SIZE, &b); |
| 332 | eglGetConfigAttrib(display, mEGLConfig, EGL_ALPHA_SIZE, &a); |
| 333 | ALOGI("EGL informations:"); |
| 334 | ALOGI("vendor : %s", extensions.getEglVendor()); |
| 335 | ALOGI("version : %s", extensions.getEglVersion()); |
| 336 | ALOGI("extensions: %s", extensions.getEglExtension()); |
| 337 | ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported"); |
| 338 | ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig); |
| 339 | ALOGI("OpenGL ES informations:"); |
| 340 | ALOGI("vendor : %s", extensions.getVendor()); |
| 341 | ALOGI("renderer : %s", extensions.getRenderer()); |
| 342 | ALOGI("version : %s", extensions.getVersion()); |
| 343 | ALOGI("extensions: %s", extensions.getExtension()); |
| 344 | ALOGI("GL_MAX_TEXTURE_SIZE = %d", mMaxTextureSize); |
| 345 | ALOGI("GL_MAX_VIEWPORT_DIMS = %d x %d", mMaxViewportDims[0], mMaxViewportDims[1]); |
| 346 | } |
| 347 | |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 348 | status_t SurfaceFlinger::readyToRun() |
| 349 | { |
| 350 | ALOGI( "SurfaceFlinger's main thread ready to run. " |
| 351 | "Initializing graphics H/W..."); |
| 352 | |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 353 | // initialize EGL |
Jesse Hall | 34a09ba | 2012-07-29 22:35:34 -0700 | [diff] [blame] | 354 | mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 355 | eglInitialize(mEGLDisplay, NULL, NULL); |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 356 | |
| 357 | // Initialize the main display |
| 358 | // create native window to main display |
| 359 | sp<FramebufferSurface> anw = FramebufferSurface::create(); |
| 360 | ANativeWindow* const window = anw.get(); |
| 361 | if (!window) { |
| 362 | ALOGE("Display subsystem failed to initialize. check logs. exiting..."); |
| 363 | exit(0); |
| 364 | } |
| 365 | |
| 366 | // initialize the config and context |
| 367 | int format; |
| 368 | window->query(window, NATIVE_WINDOW_FORMAT, &format); |
Jesse Hall | 34a09ba | 2012-07-29 22:35:34 -0700 | [diff] [blame] | 369 | mEGLConfig = selectEGLConfig(mEGLDisplay, format); |
| 370 | mEGLContext = createGLContext(mEGLDisplay, mEGLConfig); |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 371 | |
| 372 | // initialize our main display hardware |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame^] | 373 | mCurrentState.displays.add(DisplayDevice::DISPLAY_ID_MAIN, DisplayDeviceState()); |
| 374 | DisplayDevice hw(this, DisplayDevice::DISPLAY_ID_MAIN, anw, mEGLConfig); |
| 375 | mDisplays.add(DisplayDevice::DISPLAY_ID_MAIN, hw); |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 376 | |
| 377 | // initialize OpenGL ES |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame^] | 378 | EGLSurface surface = hw.getEGLSurface(); |
Jesse Hall | 34a09ba | 2012-07-29 22:35:34 -0700 | [diff] [blame] | 379 | initializeGL(mEGLDisplay, surface); |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 380 | |
Mathias Agopian | 028508c | 2012-07-25 21:12:12 -0700 | [diff] [blame] | 381 | // start the EventThread |
| 382 | mEventThread = new EventThread(this); |
| 383 | mEventQueue.setEventThread(mEventThread); |
| 384 | |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 385 | // initialize the H/W composer |
| 386 | mHwc = new HWComposer(this, |
| 387 | *static_cast<HWComposer::EventHandler *>(this), |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame^] | 388 | hw.getRefreshPeriod()); |
| 389 | |
| 390 | // initialize our drawing state |
| 391 | mDrawingState = mCurrentState; |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 392 | |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 393 | // We're now ready to accept clients... |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 394 | mReadyToRunBarrier.open(); |
| 395 | |
Mathias Agopian | a1ecca9 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 396 | // start boot animation |
Mathias Agopian | a67e418 | 2012-06-19 17:26:12 -0700 | [diff] [blame] | 397 | startBootAnim(); |
Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 398 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | return NO_ERROR; |
| 400 | } |
| 401 | |
Mathias Agopian | a67e418 | 2012-06-19 17:26:12 -0700 | [diff] [blame] | 402 | void SurfaceFlinger::startBootAnim() { |
| 403 | // start boot animation |
| 404 | property_set("service.bootanim.exit", "0"); |
| 405 | property_set("ctl.start", "bootanim"); |
| 406 | } |
| 407 | |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 408 | uint32_t SurfaceFlinger::getMaxTextureSize() const { |
| 409 | return mMaxTextureSize; |
| 410 | } |
| 411 | |
| 412 | uint32_t SurfaceFlinger::getMaxViewportDims() const { |
| 413 | return mMaxViewportDims[0] < mMaxViewportDims[1] ? |
| 414 | mMaxViewportDims[0] : mMaxViewportDims[1]; |
| 415 | } |
| 416 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 417 | // ---------------------------------------------------------------------------- |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 418 | |
Jamie Gennis | 582270d | 2011-08-17 18:19:00 -0700 | [diff] [blame] | 419 | bool SurfaceFlinger::authenticateSurfaceTexture( |
| 420 | const sp<ISurfaceTexture>& surfaceTexture) const { |
Jamie Gennis | 134f042 | 2011-03-08 12:18:54 -0800 | [diff] [blame] | 421 | Mutex::Autolock _l(mStateLock); |
Jamie Gennis | 582270d | 2011-08-17 18:19:00 -0700 | [diff] [blame] | 422 | sp<IBinder> surfaceTextureBinder(surfaceTexture->asBinder()); |
Jamie Gennis | 134f042 | 2011-03-08 12:18:54 -0800 | [diff] [blame] | 423 | |
| 424 | // Check the visible layer list for the ISurface |
| 425 | const LayerVector& currentLayers = mCurrentState.layersSortedByZ; |
| 426 | size_t count = currentLayers.size(); |
| 427 | for (size_t i=0 ; i<count ; i++) { |
| 428 | const sp<LayerBase>& layer(currentLayers[i]); |
| 429 | sp<LayerBaseClient> lbc(layer->getLayerBaseClient()); |
Jamie Gennis | 582270d | 2011-08-17 18:19:00 -0700 | [diff] [blame] | 430 | if (lbc != NULL) { |
| 431 | wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder(); |
| 432 | if (lbcBinder == surfaceTextureBinder) { |
| 433 | return true; |
| 434 | } |
Jamie Gennis | 134f042 | 2011-03-08 12:18:54 -0800 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | // Check the layers in the purgatory. This check is here so that if a |
Jamie Gennis | 582270d | 2011-08-17 18:19:00 -0700 | [diff] [blame] | 439 | // SurfaceTexture gets destroyed before all the clients are done using it, |
| 440 | // the error will not be reported as "surface XYZ is not authenticated", but |
Jamie Gennis | 134f042 | 2011-03-08 12:18:54 -0800 | [diff] [blame] | 441 | // will instead fail later on when the client tries to use the surface, |
| 442 | // which should be reported as "surface XYZ returned an -ENODEV". The |
| 443 | // purgatorized layers are no less authentic than the visible ones, so this |
| 444 | // should not cause any harm. |
| 445 | size_t purgatorySize = mLayerPurgatory.size(); |
| 446 | for (size_t i=0 ; i<purgatorySize ; i++) { |
| 447 | const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i)); |
| 448 | sp<LayerBaseClient> lbc(layer->getLayerBaseClient()); |
Jamie Gennis | 582270d | 2011-08-17 18:19:00 -0700 | [diff] [blame] | 449 | if (lbc != NULL) { |
| 450 | wp<IBinder> lbcBinder = lbc->getSurfaceTextureBinder(); |
| 451 | if (lbcBinder == surfaceTextureBinder) { |
| 452 | return true; |
| 453 | } |
Jamie Gennis | 134f042 | 2011-03-08 12:18:54 -0800 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
| 457 | return false; |
| 458 | } |
| 459 | |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 460 | status_t SurfaceFlinger::getDisplayInfo(DisplayID dpy, DisplayInfo* info) { |
| 461 | if (uint32_t(dpy) >= 2) { |
| 462 | return BAD_INDEX; |
| 463 | } |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 464 | const DisplayDevice& hw(getDefaultDisplayDevice()); |
Mathias Agopian | c666cae | 2012-07-25 18:56:13 -0700 | [diff] [blame] | 465 | return hw.getInfo(info); |
| 466 | } |
| 467 | |
Mathias Agopian | d0566bc | 2011-11-17 17:49:17 -0800 | [diff] [blame] | 468 | // ---------------------------------------------------------------------------- |
| 469 | |
| 470 | sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() { |
Mathias Agopian | 8aedd47 | 2012-01-24 16:39:14 -0800 | [diff] [blame] | 471 | return mEventThread->createEventConnection(); |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Mathias Agopian | 3094df3 | 2012-06-18 18:06:45 -0700 | [diff] [blame] | 474 | void SurfaceFlinger::connectDisplay(const sp<ISurfaceTexture> display) { |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 475 | const DisplayDevice& hw(getDefaultDisplayDevice()); |
Mathias Agopian | 3094df3 | 2012-06-18 18:06:45 -0700 | [diff] [blame] | 476 | EGLSurface result = EGL_NO_SURFACE; |
| 477 | EGLSurface old_surface = EGL_NO_SURFACE; |
| 478 | sp<SurfaceTextureClient> stc; |
| 479 | |
| 480 | if (display != NULL) { |
| 481 | stc = new SurfaceTextureClient(display); |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 482 | result = eglCreateWindowSurface(mEGLDisplay, |
Mathias Agopian | a491260 | 2012-07-12 14:25:33 -0700 | [diff] [blame] | 483 | mEGLConfig, (EGLNativeWindowType)stc.get(), NULL); |
Mathias Agopian | 3094df3 | 2012-06-18 18:06:45 -0700 | [diff] [blame] | 484 | ALOGE_IF(result == EGL_NO_SURFACE, |
| 485 | "eglCreateWindowSurface failed (ISurfaceTexture=%p)", |
| 486 | display.get()); |
| 487 | } |
| 488 | |
| 489 | { // scope for the lock |
| 490 | Mutex::Autolock _l(mStateLock); |
| 491 | old_surface = mExternalDisplaySurface; |
| 492 | mExternalDisplayNativeWindow = stc; |
| 493 | mExternalDisplaySurface = result; |
| 494 | ALOGD("mExternalDisplaySurface = %p", result); |
| 495 | } |
| 496 | |
| 497 | if (old_surface != EGL_NO_SURFACE) { |
| 498 | // Note: EGL allows to destroy an object while its current |
| 499 | // it will fail to become current next time though. |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 500 | eglDestroySurface(mEGLDisplay, old_surface); |
Mathias Agopian | 3094df3 | 2012-06-18 18:06:45 -0700 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | |
| 504 | EGLSurface SurfaceFlinger::getExternalDisplaySurface() const { |
| 505 | Mutex::Autolock _l(mStateLock); |
| 506 | return mExternalDisplaySurface; |
| 507 | } |
| 508 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | // ---------------------------------------------------------------------------- |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 510 | |
| 511 | void SurfaceFlinger::waitForEvent() { |
| 512 | mEventQueue.waitMessage(); |
| 513 | } |
| 514 | |
| 515 | void SurfaceFlinger::signalTransaction() { |
| 516 | mEventQueue.invalidate(); |
| 517 | } |
| 518 | |
| 519 | void SurfaceFlinger::signalLayerUpdate() { |
| 520 | mEventQueue.invalidate(); |
| 521 | } |
| 522 | |
| 523 | void SurfaceFlinger::signalRefresh() { |
| 524 | mEventQueue.refresh(); |
| 525 | } |
| 526 | |
| 527 | status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg, |
| 528 | nsecs_t reltime, uint32_t flags) { |
| 529 | return mEventQueue.postMessage(msg, reltime); |
| 530 | } |
| 531 | |
| 532 | status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg, |
| 533 | nsecs_t reltime, uint32_t flags) { |
| 534 | status_t res = mEventQueue.postMessage(msg, reltime); |
| 535 | if (res == NO_ERROR) { |
| 536 | msg->wait(); |
| 537 | } |
| 538 | return res; |
| 539 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 541 | bool SurfaceFlinger::threadLoop() { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | waitForEvent(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 543 | return true; |
| 544 | } |
| 545 | |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 546 | void SurfaceFlinger::onVSyncReceived(int dpy, nsecs_t timestamp) { |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 547 | mEventThread->onVSyncReceived(dpy, timestamp); |
| 548 | } |
| 549 | |
| 550 | void SurfaceFlinger::eventControl(int event, int enabled) { |
| 551 | getHwComposer().eventControl(event, enabled); |
| 552 | } |
| 553 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 554 | void SurfaceFlinger::onMessageReceived(int32_t what) { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 555 | ATRACE_CALL(); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 556 | switch (what) { |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 557 | case MessageQueue::INVALIDATE: |
| 558 | handleMessageTransaction(); |
| 559 | handleMessageInvalidate(); |
| 560 | signalRefresh(); |
| 561 | break; |
| 562 | case MessageQueue::REFRESH: |
| 563 | handleMessageRefresh(); |
| 564 | break; |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 568 | void SurfaceFlinger::handleMessageTransaction() { |
| 569 | const uint32_t mask = eTransactionNeeded | eTraversalNeeded; |
| 570 | uint32_t transactionFlags = peekTransactionFlags(mask); |
| 571 | if (transactionFlags) { |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 572 | handleTransaction(transactionFlags); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
| 576 | void SurfaceFlinger::handleMessageInvalidate() { |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 577 | handlePageFlip(); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | void SurfaceFlinger::handleMessageRefresh() { |
| 581 | handleRefresh(); |
| 582 | |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 583 | if (CC_UNLIKELY(mVisibleRegionsDirty)) { |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 584 | mVisibleRegionsDirty = false; |
| 585 | invalidateHwcGeometry(); |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 586 | |
| 587 | /* |
| 588 | * rebuild the visible layer list per screen |
| 589 | */ |
| 590 | |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 591 | const LayerVector& currentLayers(mDrawingState.layersSortedByZ); |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame^] | 592 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { |
| 593 | DisplayDevice& hw(mDisplays.editValueAt(dpy)); |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 594 | Region opaqueRegion; |
| 595 | Region dirtyRegion; |
| 596 | computeVisibleRegions(currentLayers, |
| 597 | hw.getLayerStack(), dirtyRegion, opaqueRegion); |
| 598 | hw.dirtyRegion.orSelf(dirtyRegion); |
| 599 | |
| 600 | Vector< sp<LayerBase> > layersSortedByZ; |
| 601 | const size_t count = currentLayers.size(); |
| 602 | for (size_t i=0 ; i<count ; i++) { |
| 603 | const Layer::State& s(currentLayers[i]->drawingState()); |
| 604 | if (s.layerStack == hw.getLayerStack()) { |
| 605 | if (!currentLayers[i]->visibleRegion.isEmpty()) { |
| 606 | layersSortedByZ.add(currentLayers[i]); |
| 607 | } |
| 608 | } |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 609 | } |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 610 | hw.setVisibleLayersSortedByZ(layersSortedByZ); |
| 611 | hw.undefinedRegion.set(hw.getBounds()); |
| 612 | hw.undefinedRegion.subtractSelf(hw.getTransform().transform(opaqueRegion)); |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 613 | } |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 616 | HWComposer& hwc(getHwComposer()); |
| 617 | if (hwc.initCheck() == NO_ERROR) { |
| 618 | // build the h/w work list |
| 619 | const bool workListsDirty = mHwWorkListDirty; |
| 620 | mHwWorkListDirty = false; |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame^] | 621 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { |
| 622 | const DisplayDevice& hw(mDisplays[dpy]); |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 623 | const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ()); |
| 624 | const size_t count = currentLayers.size(); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 625 | |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 626 | hwc.createWorkList(count); // FIXME: the worklist should include enough space for all layer of all displays |
| 627 | |
| 628 | HWComposer::LayerListIterator cur = hwc.begin(); |
| 629 | const HWComposer::LayerListIterator end = hwc.end(); |
| 630 | for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) { |
| 631 | const sp<LayerBase>& layer(currentLayers[i]); |
| 632 | |
| 633 | if (CC_UNLIKELY(workListsDirty)) { |
| 634 | layer->setGeometry(hw, *cur); |
| 635 | if (mDebugDisableHWC || mDebugRegion) { |
| 636 | cur->setSkip(true); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | /* |
| 641 | * update the per-frame h/w composer data for each layer |
| 642 | * and build the transparent region of the FB |
| 643 | */ |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 644 | layer->setPerFrameData(hw, *cur); |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 645 | } |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 646 | } |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 647 | status_t err = hwc.prepare(); |
| 648 | ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err)); |
| 649 | } |
| 650 | |
| 651 | const bool repaintEverything = android_atomic_and(0, &mRepaintEverything); |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame^] | 652 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { |
| 653 | DisplayDevice& hw(mDisplays.editValueAt(dpy)); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 654 | |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 655 | // transform the dirty region into this screen's coordinate space |
| 656 | const Transform& planeTransform(hw.getTransform()); |
| 657 | Region dirtyRegion; |
| 658 | if (repaintEverything) { |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 659 | dirtyRegion.set(hw.bounds()); |
| 660 | } else { |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 661 | dirtyRegion = planeTransform.transform(hw.dirtyRegion); |
| 662 | dirtyRegion.andSelf(hw.bounds()); |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 663 | } |
| 664 | hw.dirtyRegion.clear(); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 665 | |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 666 | if (!dirtyRegion.isEmpty()) { |
| 667 | if (hw.canDraw()) { |
| 668 | // repaint the framebuffer (if needed) |
| 669 | handleRepaint(hw, dirtyRegion); |
| 670 | } |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 671 | } |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 672 | // inform the h/w that we're done compositing |
| 673 | hw.compositionComplete(); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 676 | postFramebuffer(); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 677 | |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 678 | |
| 679 | #if 1 |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 680 | // render to the external display if we have one |
| 681 | EGLSurface externalDisplaySurface = getExternalDisplaySurface(); |
| 682 | if (externalDisplaySurface != EGL_NO_SURFACE) { |
| 683 | EGLSurface cur = eglGetCurrentSurface(EGL_DRAW); |
| 684 | EGLBoolean success = eglMakeCurrent(eglGetCurrentDisplay(), |
| 685 | externalDisplaySurface, externalDisplaySurface, |
| 686 | eglGetCurrentContext()); |
| 687 | |
| 688 | ALOGE_IF(!success, "eglMakeCurrent -> external failed"); |
| 689 | |
| 690 | if (success) { |
| 691 | // redraw the screen entirely... |
| 692 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 693 | glDisable(GL_TEXTURE_2D); |
| 694 | glClearColor(0,0,0,1); |
| 695 | glClear(GL_COLOR_BUFFER_BIT); |
| 696 | glMatrixMode(GL_MODELVIEW); |
| 697 | glLoadIdentity(); |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 698 | |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame^] | 699 | DisplayDevice& hw(const_cast<DisplayDevice&>(getDefaultDisplayDevice())); |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 700 | const Vector< sp<LayerBase> >& layers( hw.getVisibleLayersSortedByZ() ); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 701 | const size_t count = layers.size(); |
| 702 | for (size_t i=0 ; i<count ; ++i) { |
| 703 | const sp<LayerBase>& layer(layers[i]); |
Mathias Agopian | fcb239d | 2012-08-02 16:01:34 -0700 | [diff] [blame] | 704 | layer->draw(hw); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | success = eglSwapBuffers(eglGetCurrentDisplay(), externalDisplaySurface); |
| 708 | ALOGE_IF(!success, "external display eglSwapBuffers failed"); |
| 709 | |
| 710 | hw.compositionComplete(); |
| 711 | } |
| 712 | |
| 713 | success = eglMakeCurrent(eglGetCurrentDisplay(), |
| 714 | cur, cur, eglGetCurrentContext()); |
| 715 | |
| 716 | ALOGE_IF(!success, "eglMakeCurrent -> internal failed"); |
| 717 | } |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 718 | #endif |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 719 | |
| 720 | } |
| 721 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 722 | void SurfaceFlinger::postFramebuffer() |
| 723 | { |
Mathias Agopian | 841cde5 | 2012-03-01 15:44:37 -0800 | [diff] [blame] | 724 | ATRACE_CALL(); |
Mathias Agopian | b048cef | 2012-02-04 15:44:04 -0800 | [diff] [blame] | 725 | |
Mathias Agopian | a44b041 | 2011-10-16 18:46:35 -0700 | [diff] [blame] | 726 | const nsecs_t now = systemTime(); |
| 727 | mDebugInSwapBuffers = now; |
Jesse Hall | c5c5a14 | 2012-07-02 16:49:28 -0700 | [diff] [blame] | 728 | |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 729 | HWComposer& hwc(getHwComposer()); |
| 730 | |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame^] | 731 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { |
| 732 | DisplayDevice& hw(mDisplays.editValueAt(dpy)); |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 733 | if (hwc.initCheck() == NO_ERROR) { |
| 734 | const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ()); |
| 735 | const size_t count = currentLayers.size(); |
| 736 | HWComposer::LayerListIterator cur = hwc.begin(); |
| 737 | const HWComposer::LayerListIterator end = hwc.end(); |
| 738 | for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) { |
| 739 | const sp<LayerBase>& layer(currentLayers[i]); |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 740 | layer->setAcquireFence(hw, *cur); |
Jesse Hall | c5c5a14 | 2012-07-02 16:49:28 -0700 | [diff] [blame] | 741 | } |
| 742 | } |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 743 | hw.flip(hw.swapRegion); |
| 744 | hw.swapRegion.clear(); |
Jesse Hall | c5c5a14 | 2012-07-02 16:49:28 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 747 | if (hwc.initCheck() == NO_ERROR) { |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 748 | // FIXME: eventually commit() won't take arguments |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 749 | hwc.commit(mEGLDisplay, getDefaultDisplayDevice().getEGLSurface()); |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame^] | 752 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { |
| 753 | const DisplayDevice& hw(mDisplays[dpy]); |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 754 | const Vector< sp<LayerBase> >& currentLayers(hw.getVisibleLayersSortedByZ()); |
| 755 | const size_t count = currentLayers.size(); |
| 756 | if (hwc.initCheck() == NO_ERROR) { |
| 757 | HWComposer::LayerListIterator cur = hwc.begin(); |
| 758 | const HWComposer::LayerListIterator end = hwc.end(); |
| 759 | for (size_t i = 0; cur != end && i < count; ++i, ++cur) { |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 760 | currentLayers[i]->onLayerDisplayed(hw, &*cur); |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 761 | } |
| 762 | } else { |
| 763 | eglSwapBuffers(mEGLDisplay, hw.getEGLSurface()); |
| 764 | for (size_t i = 0; i < count; i++) { |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 765 | currentLayers[i]->onLayerDisplayed(hw, NULL); |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 766 | } |
Jesse Hall | ef19414 | 2012-06-14 14:45:17 -0700 | [diff] [blame] | 767 | } |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 768 | |
| 769 | // FIXME: we need to call eglSwapBuffers() on displays that have GL composition |
Jamie Gennis | e8696a4 | 2012-01-15 18:54:57 -0800 | [diff] [blame] | 770 | } |
| 771 | |
Mathias Agopian | a44b041 | 2011-10-16 18:46:35 -0700 | [diff] [blame] | 772 | mLastSwapBufferTime = systemTime() - now; |
| 773 | mDebugInSwapBuffers = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 774 | } |
| 775 | |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 776 | void SurfaceFlinger::handleTransaction(uint32_t transactionFlags) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 777 | { |
Mathias Agopian | 841cde5 | 2012-03-01 15:44:37 -0800 | [diff] [blame] | 778 | ATRACE_CALL(); |
| 779 | |
Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 780 | Mutex::Autolock _l(mStateLock); |
| 781 | const nsecs_t now = systemTime(); |
| 782 | mDebugInTransaction = now; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 783 | |
Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 784 | // Here we're guaranteed that some transaction flags are set |
| 785 | // so we can call handleTransactionLocked() unconditionally. |
| 786 | // We call getTransactionFlags(), which will also clear the flags, |
| 787 | // with mStateLock held to guarantee that mCurrentState won't change |
| 788 | // until the transaction is committed. |
Mathias Agopian | 4da7519 | 2010-08-10 17:19:56 -0700 | [diff] [blame] | 789 | |
Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 790 | const uint32_t mask = eTransactionNeeded | eTraversalNeeded; |
| 791 | transactionFlags = getTransactionFlags(mask); |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 792 | handleTransactionLocked(transactionFlags); |
Mathias Agopian | dea20b1 | 2011-05-03 17:04:02 -0700 | [diff] [blame] | 793 | |
Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 794 | mLastTransactionTime = systemTime() - now; |
| 795 | mDebugInTransaction = 0; |
| 796 | invalidateHwcGeometry(); |
| 797 | // here the transaction has been committed |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 800 | void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags) |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 801 | { |
| 802 | const LayerVector& currentLayers(mCurrentState.layersSortedByZ); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 803 | const size_t count = currentLayers.size(); |
| 804 | |
| 805 | /* |
| 806 | * Traversal of the children |
| 807 | * (perform the transaction for each of them if needed) |
| 808 | */ |
| 809 | |
| 810 | const bool layersNeedTransaction = transactionFlags & eTraversalNeeded; |
| 811 | if (layersNeedTransaction) { |
| 812 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 813 | const sp<LayerBase>& layer = currentLayers[i]; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 814 | uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded); |
| 815 | if (!trFlags) continue; |
| 816 | |
| 817 | const uint32_t flags = layer->doTransaction(0); |
| 818 | if (flags & Layer::eVisibleRegion) |
| 819 | mVisibleRegionsDirty = true; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 820 | } |
| 821 | } |
| 822 | |
| 823 | /* |
| 824 | * Perform our own transaction if needed |
| 825 | */ |
| 826 | |
| 827 | if (transactionFlags & eTransactionNeeded) { |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame^] | 828 | // here we take advantage of Vector's copy-on-write semantics to |
| 829 | // improve performance by skipping the transaction entirely when |
| 830 | // know that the lists are identical |
| 831 | const KeyedVector<int32_t, DisplayDeviceState>& curr(mCurrentState.displays); |
| 832 | const KeyedVector<int32_t, DisplayDeviceState>& draw(mDrawingState.displays); |
| 833 | if (!curr.isIdenticalTo(draw)) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | mVisibleRegionsDirty = true; |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame^] | 835 | const size_t cc = curr.size(); |
| 836 | const size_t dc = draw.size(); |
| 837 | |
| 838 | // find the displays that were removed |
| 839 | // (ie: in drawing state but not in current state) |
| 840 | // also handle displays that changed |
| 841 | // (ie: displays that are in both lists) |
| 842 | for (size_t i=0 ; i<dc ; i++) { |
| 843 | if (curr.indexOfKey(draw[i].id) < 0) { |
| 844 | // in drawing state but not in current state |
| 845 | if (draw[i].id != DisplayDevice::DISPLAY_ID_MAIN) { |
| 846 | mDisplays.editValueFor(draw[i].id).terminate(); |
| 847 | mDisplays.removeItem(draw[i].id); |
| 848 | } else { |
| 849 | ALOGW("trying to remove the main display"); |
| 850 | } |
| 851 | } else { |
| 852 | // this display is in both lists. see if something changed. |
| 853 | const DisplayDeviceState& state(curr[i]); |
| 854 | if (state.layerStack != draw[i].layerStack) { |
| 855 | DisplayDevice& disp(mDisplays.editValueFor(state.id)); |
| 856 | //disp.setLayerStack(state.layerStack); |
| 857 | } |
| 858 | if (curr[i].orientation != draw[i].orientation) { |
| 859 | DisplayDevice& disp(mDisplays.editValueFor(state.id)); |
| 860 | disp.setOrientation(state.orientation); |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | // find displays that were added |
| 866 | // (ie: in current state but not in drawing state) |
| 867 | for (size_t i=0 ; i<cc ; i++) { |
| 868 | if (mDrawingState.displays.indexOfKey(curr[i].id) < 0) { |
| 869 | // FIXME: we need to pass the surface here |
| 870 | DisplayDevice disp(this, curr[i].id, 0, mEGLConfig); |
| 871 | mDisplays.add(curr[i].id, disp); |
| 872 | } |
| 873 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | } |
| 875 | |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 876 | if (currentLayers.size() > mDrawingState.layersSortedByZ.size()) { |
| 877 | // layers have been added |
| 878 | mVisibleRegionsDirty = true; |
| 879 | } |
| 880 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 881 | // some layers might have been removed, so |
| 882 | // we need to update the regions they're exposing. |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 883 | if (mLayersRemoved) { |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 884 | mLayersRemoved = false; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 885 | mVisibleRegionsDirty = true; |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 886 | const LayerVector& previousLayers(mDrawingState.layersSortedByZ); |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 887 | const size_t count = previousLayers.size(); |
| 888 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 889 | const sp<LayerBase>& layer(previousLayers[i]); |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 890 | if (currentLayers.indexOf(layer) < 0) { |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 891 | // this layer is not visible anymore |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 892 | // TODO: we could traverse the tree from front to back and |
| 893 | // compute the actual visible region |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 894 | // TODO: we could cache the transformed region |
| 895 | Layer::State front(layer->drawingState()); |
| 896 | Region visibleReg = front.transform.transform( |
| 897 | Region(Rect(front.active.w, front.active.h))); |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 898 | invalidateLayerStack(front.layerStack, visibleReg); |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 899 | } |
| 900 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 901 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | commitTransaction(); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | void SurfaceFlinger::commitTransaction() |
| 908 | { |
| 909 | if (!mLayersPendingRemoval.isEmpty()) { |
| 910 | // Notify removed layers now that they can't be drawn from |
| 911 | for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) { |
| 912 | mLayersPendingRemoval[i]->onRemoved(); |
| 913 | } |
| 914 | mLayersPendingRemoval.clear(); |
| 915 | } |
| 916 | |
| 917 | mDrawingState = mCurrentState; |
| 918 | mTransationPending = false; |
| 919 | mTransactionCV.broadcast(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 920 | } |
| 921 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | void SurfaceFlinger::computeVisibleRegions( |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 923 | const LayerVector& currentLayers, uint32_t layerStack, |
| 924 | Region& outDirtyRegion, Region& outOpaqueRegion) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | { |
Mathias Agopian | 841cde5 | 2012-03-01 15:44:37 -0800 | [diff] [blame] | 926 | ATRACE_CALL(); |
| 927 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 928 | Region aboveOpaqueLayers; |
| 929 | Region aboveCoveredLayers; |
| 930 | Region dirty; |
| 931 | |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 932 | outDirtyRegion.clear(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 933 | |
| 934 | size_t i = currentLayers.size(); |
| 935 | while (i--) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 936 | const sp<LayerBase>& layer = currentLayers[i]; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 937 | |
| 938 | // start with the whole surface at its current location |
Mathias Agopian | 9701122 | 2009-07-28 10:57:27 -0700 | [diff] [blame] | 939 | const Layer::State& s(layer->drawingState()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 940 | |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 941 | // only consider the layers on the given later stack |
| 942 | if (s.layerStack != layerStack) |
| 943 | continue; |
| 944 | |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 945 | /* |
| 946 | * opaqueRegion: area of a surface that is fully opaque. |
| 947 | */ |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 948 | Region opaqueRegion; |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 949 | |
| 950 | /* |
| 951 | * visibleRegion: area of a surface that is visible on screen |
| 952 | * and not fully transparent. This is essentially the layer's |
| 953 | * footprint minus the opaque regions above it. |
| 954 | * Areas covered by a translucent surface are considered visible. |
| 955 | */ |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 956 | Region visibleRegion; |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 957 | |
| 958 | /* |
| 959 | * coveredRegion: area of a surface that is covered by all |
| 960 | * visible regions above it (which includes the translucent areas). |
| 961 | */ |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 962 | Region coveredRegion; |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 963 | |
| 964 | |
| 965 | // handle hidden surfaces by setting the visible region to empty |
Glenn Kasten | 99ed224 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 966 | if (CC_LIKELY(!(s.flags & ISurfaceComposer::eLayerHidden) && s.alpha)) { |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 967 | const bool translucent = !layer->isOpaque(); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 968 | Rect bounds(layer->computeBounds()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 969 | visibleRegion.set(bounds); |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 970 | if (!visibleRegion.isEmpty()) { |
| 971 | // Remove the transparent area from the visible region |
| 972 | if (translucent) { |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 973 | Region transparentRegionScreen; |
| 974 | const Transform tr(s.transform); |
| 975 | if (tr.transformed()) { |
| 976 | if (tr.preserveRects()) { |
| 977 | // transform the transparent region |
| 978 | transparentRegionScreen = tr.transform(s.transparentRegion); |
| 979 | } else { |
| 980 | // transformation too complex, can't do the |
| 981 | // transparent region optimization. |
| 982 | transparentRegionScreen.clear(); |
| 983 | } |
| 984 | } else { |
| 985 | transparentRegionScreen = s.transparentRegion; |
| 986 | } |
| 987 | visibleRegion.subtractSelf(transparentRegionScreen); |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 988 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 989 | |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 990 | // compute the opaque region |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 991 | const int32_t layerOrientation = s.transform.getOrientation(); |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 992 | if (s.alpha==255 && !translucent && |
| 993 | ((layerOrientation & Transform::ROT_INVALID) == false)) { |
| 994 | // the opaque region is the layer's footprint |
| 995 | opaqueRegion = visibleRegion; |
| 996 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 1000 | // Clip the covered region to the visible region |
| 1001 | coveredRegion = aboveCoveredLayers.intersect(visibleRegion); |
| 1002 | |
| 1003 | // Update aboveCoveredLayers for next (lower) layer |
| 1004 | aboveCoveredLayers.orSelf(visibleRegion); |
| 1005 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1006 | // subtract the opaque region covered by the layers above us |
| 1007 | visibleRegion.subtractSelf(aboveOpaqueLayers); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1008 | |
| 1009 | // compute this layer's dirty region |
| 1010 | if (layer->contentDirty) { |
| 1011 | // we need to invalidate the whole region |
| 1012 | dirty = visibleRegion; |
| 1013 | // as well, as the old visible region |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 1014 | dirty.orSelf(layer->visibleRegion); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1015 | layer->contentDirty = false; |
| 1016 | } else { |
Mathias Agopian | a8d44f7 | 2009-06-28 02:54:16 -0700 | [diff] [blame] | 1017 | /* compute the exposed region: |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 1018 | * the exposed region consists of two components: |
| 1019 | * 1) what's VISIBLE now and was COVERED before |
| 1020 | * 2) what's EXPOSED now less what was EXPOSED before |
| 1021 | * |
| 1022 | * note that (1) is conservative, we start with the whole |
| 1023 | * visible region but only keep what used to be covered by |
| 1024 | * something -- which mean it may have been exposed. |
| 1025 | * |
| 1026 | * (2) handles areas that were not covered by anything but got |
| 1027 | * exposed because of a resize. |
Mathias Agopian | a8d44f7 | 2009-06-28 02:54:16 -0700 | [diff] [blame] | 1028 | */ |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 1029 | const Region newExposed = visibleRegion - coveredRegion; |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 1030 | const Region oldVisibleRegion = layer->visibleRegion; |
| 1031 | const Region oldCoveredRegion = layer->coveredRegion; |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 1032 | const Region oldExposed = oldVisibleRegion - oldCoveredRegion; |
| 1033 | dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1034 | } |
| 1035 | dirty.subtractSelf(aboveOpaqueLayers); |
| 1036 | |
| 1037 | // accumulate to the screen dirty region |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1038 | outDirtyRegion.orSelf(dirty); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1039 | |
Mathias Agopian | ab02873 | 2010-03-16 16:41:46 -0700 | [diff] [blame] | 1040 | // Update aboveOpaqueLayers for next (lower) layer |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1041 | aboveOpaqueLayers.orSelf(opaqueRegion); |
Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1042 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1043 | // Store the visible region is screen space |
| 1044 | layer->setVisibleRegion(visibleRegion); |
| 1045 | layer->setCoveredRegion(coveredRegion); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1046 | } |
| 1047 | |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1048 | outOpaqueRegion = aboveOpaqueLayers; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1049 | } |
| 1050 | |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1051 | void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack, |
| 1052 | const Region& dirty) { |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame^] | 1053 | for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) { |
| 1054 | DisplayDevice& hw(mDisplays.editValueAt(dpy)); |
| 1055 | if (hw.getLayerStack() == layerStack) { |
| 1056 | hw.dirtyRegion.orSelf(dirty); |
| 1057 | } |
| 1058 | } |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | void SurfaceFlinger::handlePageFlip() |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1062 | { |
Jamie Gennis | 1c8e95c | 2012-02-23 19:27:23 -0800 | [diff] [blame] | 1063 | ATRACE_CALL(); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 1064 | Region dirtyRegion; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1065 | |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 1066 | const LayerVector& currentLayers(mDrawingState.layersSortedByZ); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 1067 | |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 1068 | bool visibleRegions = false; |
| 1069 | const size_t count = currentLayers.size(); |
| 1070 | sp<LayerBase> const* layers = currentLayers.array(); |
| 1071 | for (size_t i=0 ; i<count ; i++) { |
| 1072 | const sp<LayerBase>& layer(layers[i]); |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1073 | const Region dirty(layer->latchBuffer(visibleRegions)); |
| 1074 | Layer::State s(layer->drawingState()); |
| 1075 | invalidateLayerStack(s.layerStack, dirty); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 1076 | } |
Mathias Agopian | 4da7519 | 2010-08-10 17:19:56 -0700 | [diff] [blame] | 1077 | |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 1078 | mVisibleRegionsDirty |= visibleRegions; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1079 | } |
| 1080 | |
Mathias Agopian | ad456f9 | 2011-01-13 17:53:01 -0800 | [diff] [blame] | 1081 | void SurfaceFlinger::invalidateHwcGeometry() |
| 1082 | { |
| 1083 | mHwWorkListDirty = true; |
| 1084 | } |
| 1085 | |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 1086 | void SurfaceFlinger::handleRefresh() |
| 1087 | { |
| 1088 | bool needInvalidate = false; |
| 1089 | const LayerVector& currentLayers(mDrawingState.layersSortedByZ); |
| 1090 | const size_t count = currentLayers.size(); |
| 1091 | for (size_t i=0 ; i<count ; i++) { |
| 1092 | const sp<LayerBase>& layer(currentLayers[i]); |
| 1093 | if (layer->onPreComposition()) { |
| 1094 | needInvalidate = true; |
| 1095 | } |
| 1096 | } |
| 1097 | if (needInvalidate) { |
| 1098 | signalLayerUpdate(); |
| 1099 | } |
| 1100 | } |
| 1101 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 1102 | void SurfaceFlinger::handleRepaint(const DisplayDevice& hw, |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1103 | const Region& inDirtyRegion) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1104 | { |
Mathias Agopian | 841cde5 | 2012-03-01 15:44:37 -0800 | [diff] [blame] | 1105 | ATRACE_CALL(); |
| 1106 | |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1107 | Region dirtyRegion(inDirtyRegion); |
| 1108 | |
Mathias Agopian | b8a5560 | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 1109 | // compute the invalid region |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1110 | hw.swapRegion.orSelf(dirtyRegion); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1111 | |
Glenn Kasten | 99ed224 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 1112 | if (CC_UNLIKELY(mDebugRegion)) { |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1113 | debugFlashRegions(hw, dirtyRegion); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1114 | } |
| 1115 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1116 | uint32_t flags = hw.getFlags(); |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 1117 | if (flags & DisplayDevice::SWAP_RECTANGLE) { |
Mathias Agopian | 29d06ac | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 1118 | // we can redraw only what's dirty, but since SWAP_RECTANGLE only |
| 1119 | // takes a rectangle, we must make sure to update that whole |
| 1120 | // rectangle in that case |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1121 | dirtyRegion.set(hw.swapRegion.bounds()); |
Mathias Agopian | 29d06ac | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 1122 | } else { |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 1123 | if (flags & DisplayDevice::PARTIAL_UPDATES) { |
Mathias Agopian | 29d06ac | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 1124 | // We need to redraw the rectangle that will be updated |
| 1125 | // (pushed to the framebuffer). |
Mathias Agopian | 95a666b | 2009-09-24 14:57:26 -0700 | [diff] [blame] | 1126 | // This is needed because PARTIAL_UPDATES only takes one |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 1127 | // rectangle instead of a region (see DisplayDevice::flip()) |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1128 | dirtyRegion.set(hw.swapRegion.bounds()); |
Mathias Agopian | 29d06ac | 2009-06-29 18:49:56 -0700 | [diff] [blame] | 1129 | } else { |
| 1130 | // we need to redraw everything (the whole screen) |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1131 | dirtyRegion.set(hw.bounds()); |
| 1132 | hw.swapRegion = dirtyRegion; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1133 | } |
| 1134 | } |
| 1135 | |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1136 | composeSurfaces(hw, dirtyRegion); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1137 | |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 1138 | const LayerVector& currentLayers(mDrawingState.layersSortedByZ); |
| 1139 | const size_t count = currentLayers.size(); |
| 1140 | for (size_t i=0 ; i<count ; i++) { |
| 1141 | currentLayers[i]->onPostComposition(); |
| 1142 | } |
| 1143 | |
Mathias Agopian | 9c6e297 | 2011-09-20 17:21:56 -0700 | [diff] [blame] | 1144 | // update the swap region and clear the dirty region |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1145 | hw.swapRegion.orSelf(dirtyRegion); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1146 | } |
| 1147 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 1148 | void SurfaceFlinger::composeSurfaces(const DisplayDevice& hw, const Region& dirty) |
Mathias Agopian | f384cc3 | 2011-09-08 18:31:55 -0700 | [diff] [blame] | 1149 | { |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 1150 | HWComposer& hwc(getHwComposer()); |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 1151 | HWComposer::LayerListIterator cur = hwc.begin(); |
| 1152 | const HWComposer::LayerListIterator end = hwc.end(); |
Mathias Agopian | cd20eb0 | 2011-09-22 20:57:04 -0700 | [diff] [blame] | 1153 | |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 1154 | const size_t fbLayerCount = hwc.getLayerCount(HWC_FRAMEBUFFER); // FIXME: this should be per display |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 1155 | if (cur==end || fbLayerCount) { |
Mathias Agopian | f384cc3 | 2011-09-08 18:31:55 -0700 | [diff] [blame] | 1156 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 1157 | DisplayDevice::makeCurrent(hw, mEGLContext); |
Mathias Agopian | 52bbb1a | 2012-07-31 19:01:53 -0700 | [diff] [blame] | 1158 | |
| 1159 | // set the frame buffer |
| 1160 | glMatrixMode(GL_MODELVIEW); |
| 1161 | glLoadIdentity(); |
| 1162 | |
| 1163 | // Never touch the framebuffer if we don't have any framebuffer layers |
| 1164 | if (hwc.getLayerCount(HWC_OVERLAY)) { // FIXME: this should be per display |
Mathias Agopian | b9494d5 | 2012-04-18 02:28:45 -0700 | [diff] [blame] | 1165 | // when using overlays, we assume a fully transparent framebuffer |
| 1166 | // NOTE: we could reduce how much we need to clear, for instance |
| 1167 | // remove where there are opaque FB layers. however, on some |
| 1168 | // GPUs doing a "clean slate" glClear might be more efficient. |
| 1169 | // We'll revisit later if needed. |
| 1170 | glClearColor(0, 0, 0, 0); |
| 1171 | glClear(GL_COLOR_BUFFER_BIT); |
| 1172 | } else { |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1173 | const Region region(hw.undefinedRegion.intersect(dirty)); |
Mathias Agopian | b9494d5 | 2012-04-18 02:28:45 -0700 | [diff] [blame] | 1174 | // screen is already cleared here |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1175 | if (!region.isEmpty()) { |
Mathias Agopian | b9494d5 | 2012-04-18 02:28:45 -0700 | [diff] [blame] | 1176 | // can happen with SurfaceView |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1177 | drawWormhole(region); |
Mathias Agopian | b9494d5 | 2012-04-18 02:28:45 -0700 | [diff] [blame] | 1178 | } |
Mathias Agopian | a2f4e56 | 2012-04-15 23:34:59 -0700 | [diff] [blame] | 1179 | } |
Mathias Agopian | 4b2ba53 | 2012-03-29 12:23:51 -0700 | [diff] [blame] | 1180 | |
Mathias Agopian | a2f4e56 | 2012-04-15 23:34:59 -0700 | [diff] [blame] | 1181 | /* |
| 1182 | * and then, render the layers targeted at the framebuffer |
| 1183 | */ |
Mathias Agopian | 4b2ba53 | 2012-03-29 12:23:51 -0700 | [diff] [blame] | 1184 | |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 1185 | const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ()); |
Mathias Agopian | a2f4e56 | 2012-04-15 23:34:59 -0700 | [diff] [blame] | 1186 | const size_t count = layers.size(); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 1187 | const Transform& tr = hw.getTransform(); |
Jesse Hall | a6b32db | 2012-07-19 16:44:38 -0700 | [diff] [blame] | 1188 | for (size_t i=0 ; i<count ; ++i) { |
Mathias Agopian | a2f4e56 | 2012-04-15 23:34:59 -0700 | [diff] [blame] | 1189 | const sp<LayerBase>& layer(layers[i]); |
Mathias Agopian | 4fec873 | 2012-06-29 14:12:52 -0700 | [diff] [blame] | 1190 | const Region clip(dirty.intersect(tr.transform(layer->visibleRegion))); |
Mathias Agopian | a2f4e56 | 2012-04-15 23:34:59 -0700 | [diff] [blame] | 1191 | if (!clip.isEmpty()) { |
Jesse Hall | a6b32db | 2012-07-19 16:44:38 -0700 | [diff] [blame] | 1192 | if (cur != end && cur->getCompositionType() == HWC_OVERLAY) { |
Mathias Agopian | 3e8b853 | 2012-05-13 20:42:01 -0700 | [diff] [blame] | 1193 | if (i && (cur->getHints() & HWC_HINT_CLEAR_FB) |
Mathias Agopian | a2f4e56 | 2012-04-15 23:34:59 -0700 | [diff] [blame] | 1194 | && layer->isOpaque()) { |
Mathias Agopian | b9494d5 | 2012-04-18 02:28:45 -0700 | [diff] [blame] | 1195 | // never clear the very first layer since we're |
| 1196 | // guaranteed the FB is already cleared |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 1197 | layer->clearWithOpenGL(hw, clip); |
Mathias Agopian | a2f4e56 | 2012-04-15 23:34:59 -0700 | [diff] [blame] | 1198 | } |
Jesse Hall | 6ee93c0 | 2012-07-23 13:11:19 -0700 | [diff] [blame] | 1199 | ++cur; |
Mathias Agopian | a2f4e56 | 2012-04-15 23:34:59 -0700 | [diff] [blame] | 1200 | continue; |
| 1201 | } |
| 1202 | // render the layer |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 1203 | layer->draw(hw, clip); |
Mathias Agopian | 4b2ba53 | 2012-03-29 12:23:51 -0700 | [diff] [blame] | 1204 | } |
Jesse Hall | a6b32db | 2012-07-19 16:44:38 -0700 | [diff] [blame] | 1205 | if (cur != end) { |
| 1206 | ++cur; |
| 1207 | } |
Mathias Agopian | 4b2ba53 | 2012-03-29 12:23:51 -0700 | [diff] [blame] | 1208 | } |
| 1209 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1210 | } |
| 1211 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 1212 | void SurfaceFlinger::debugFlashRegions(const DisplayDevice& hw, |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1213 | const Region& dirtyRegion) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1214 | { |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 1215 | const uint32_t flags = hw.getFlags(); |
Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1216 | const int32_t height = hw.getHeight(); |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1217 | if (hw.swapRegion.isEmpty()) { |
Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1218 | return; |
| 1219 | } |
Mathias Agopian | df3ca30 | 2009-05-04 19:29:25 -0700 | [diff] [blame] | 1220 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 1221 | if (!(flags & DisplayDevice::SWAP_RECTANGLE)) { |
| 1222 | const Region repaint((flags & DisplayDevice::PARTIAL_UPDATES) ? |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1223 | dirtyRegion.bounds() : hw.bounds()); |
Mathias Agopian | 1b03149 | 2012-06-20 17:51:20 -0700 | [diff] [blame] | 1224 | composeSurfaces(hw, repaint); |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
Mathias Agopian | c492e67 | 2011-10-18 14:49:27 -0700 | [diff] [blame] | 1227 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 1228 | glDisable(GL_TEXTURE_2D); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1229 | glDisable(GL_BLEND); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1230 | |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 1231 | static int toggle = 0; |
| 1232 | toggle = 1 - toggle; |
| 1233 | if (toggle) { |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 1234 | glColor4f(1, 0, 1, 1); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 1235 | } else { |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 1236 | glColor4f(1, 1, 0, 1); |
Mathias Agopian | 0926f50 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 1237 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1238 | |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1239 | Region::const_iterator it = dirtyRegion.begin(); |
| 1240 | Region::const_iterator const end = dirtyRegion.end(); |
Mathias Agopian | 20f6878 | 2009-05-11 00:03:41 -0700 | [diff] [blame] | 1241 | while (it != end) { |
| 1242 | const Rect& r = *it++; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1243 | GLfloat vertices[][2] = { |
Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1244 | { r.left, height - r.top }, |
| 1245 | { r.left, height - r.bottom }, |
| 1246 | { r.right, height - r.bottom }, |
| 1247 | { r.right, height - r.top } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1248 | }; |
| 1249 | glVertexPointer(2, GL_FLOAT, 0, vertices); |
| 1250 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 1251 | } |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 1252 | |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1253 | hw.flip(hw.swapRegion); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1254 | |
| 1255 | if (mDebugRegion > 1) |
Mathias Agopian | 0a91775 | 2010-06-14 21:20:00 -0700 | [diff] [blame] | 1256 | usleep(mDebugRegion * 1000); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1257 | } |
| 1258 | |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1259 | void SurfaceFlinger::drawWormhole(const Region& region) const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1260 | { |
Mathias Agopian | f74e8e0 | 2012-04-16 03:14:05 -0700 | [diff] [blame] | 1261 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
Mathias Agopian | b9494d5 | 2012-04-18 02:28:45 -0700 | [diff] [blame] | 1262 | glDisable(GL_TEXTURE_2D); |
Mathias Agopian | f74e8e0 | 2012-04-16 03:14:05 -0700 | [diff] [blame] | 1263 | glDisable(GL_BLEND); |
Mathias Agopian | b9494d5 | 2012-04-18 02:28:45 -0700 | [diff] [blame] | 1264 | glColor4f(0,0,0,0); |
Mathias Agopian | f74e8e0 | 2012-04-16 03:14:05 -0700 | [diff] [blame] | 1265 | |
| 1266 | GLfloat vertices[4][2]; |
| 1267 | glVertexPointer(2, GL_FLOAT, 0, vertices); |
| 1268 | Region::const_iterator it = region.begin(); |
| 1269 | Region::const_iterator const end = region.end(); |
| 1270 | while (it != end) { |
| 1271 | const Rect& r = *it++; |
| 1272 | vertices[0][0] = r.left; |
| 1273 | vertices[0][1] = r.top; |
| 1274 | vertices[1][0] = r.right; |
| 1275 | vertices[1][1] = r.top; |
| 1276 | vertices[2][0] = r.right; |
| 1277 | vertices[2][1] = r.bottom; |
| 1278 | vertices[3][0] = r.left; |
| 1279 | vertices[3][1] = r.bottom; |
| 1280 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 1281 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1282 | } |
| 1283 | |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1284 | ssize_t SurfaceFlinger::addClientLayer(const sp<Client>& client, |
| 1285 | const sp<LayerBaseClient>& lbc) |
| 1286 | { |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1287 | // attach this layer to the client |
Mathias Agopian | 4f11374 | 2011-05-03 16:21:41 -0700 | [diff] [blame] | 1288 | size_t name = client->attachLayer(lbc); |
| 1289 | |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1290 | // add this layer to the current state list |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 1291 | Mutex::Autolock _l(mStateLock); |
| 1292 | mCurrentState.layersSortedByZ.add(lbc); |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1293 | |
Mathias Agopian | 4f11374 | 2011-05-03 16:21:41 -0700 | [diff] [blame] | 1294 | return ssize_t(name); |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1295 | } |
| 1296 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1297 | status_t SurfaceFlinger::removeLayer(const sp<LayerBase>& layer) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1298 | { |
| 1299 | Mutex::Autolock _l(mStateLock); |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1300 | status_t err = purgatorizeLayer_l(layer); |
| 1301 | if (err == NO_ERROR) |
| 1302 | setTransactionFlags(eTransactionNeeded); |
| 1303 | return err; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1304 | } |
| 1305 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1306 | status_t SurfaceFlinger::removeLayer_l(const sp<LayerBase>& layerBase) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1307 | { |
| 1308 | ssize_t index = mCurrentState.layersSortedByZ.remove(layerBase); |
| 1309 | if (index >= 0) { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1310 | mLayersRemoved = true; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1311 | return NO_ERROR; |
| 1312 | } |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1313 | return status_t(index); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1314 | } |
| 1315 | |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1316 | status_t SurfaceFlinger::purgatorizeLayer_l(const sp<LayerBase>& layerBase) |
| 1317 | { |
Mathias Agopian | 76cd4dd | 2011-01-14 17:37:42 -0800 | [diff] [blame] | 1318 | // First add the layer to the purgatory list, which makes sure it won't |
| 1319 | // go away, then remove it from the main list (through a transaction). |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1320 | ssize_t err = removeLayer_l(layerBase); |
Mathias Agopian | 76cd4dd | 2011-01-14 17:37:42 -0800 | [diff] [blame] | 1321 | if (err >= 0) { |
| 1322 | mLayerPurgatory.add(layerBase); |
| 1323 | } |
Mathias Agopian | 8c0a3d7 | 2009-09-23 16:44:00 -0700 | [diff] [blame] | 1324 | |
Jesse Hall | 2f4b68d | 2011-12-02 10:00:00 -0800 | [diff] [blame] | 1325 | mLayersPendingRemoval.push(layerBase); |
Mathias Agopian | 0b3ad46 | 2009-10-02 18:12:30 -0700 | [diff] [blame] | 1326 | |
Mathias Agopian | 3d57964 | 2009-06-04 18:46:21 -0700 | [diff] [blame] | 1327 | // it's possible that we don't find a layer, because it might |
| 1328 | // have been destroyed already -- this is not technically an error |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1329 | // from the user because there is a race between Client::destroySurface(), |
| 1330 | // ~Client() and ~ISurface(). |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1331 | return (err == NAME_NOT_FOUND) ? status_t(NO_ERROR) : err; |
| 1332 | } |
| 1333 | |
Mathias Agopian | dea20b1 | 2011-05-03 17:04:02 -0700 | [diff] [blame] | 1334 | uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags) |
| 1335 | { |
| 1336 | return android_atomic_release_load(&mTransactionFlags); |
| 1337 | } |
| 1338 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1339 | uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) |
| 1340 | { |
| 1341 | return android_atomic_and(~flags, &mTransactionFlags) & flags; |
| 1342 | } |
| 1343 | |
Mathias Agopian | bb64124 | 2010-05-18 17:06:55 -0700 | [diff] [blame] | 1344 | uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1345 | { |
| 1346 | uint32_t old = android_atomic_or(flags, &mTransactionFlags); |
| 1347 | if ((old & flags)==0) { // wake the server up |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 1348 | signalTransaction(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1349 | } |
| 1350 | return old; |
| 1351 | } |
| 1352 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1353 | |
Mathias Agopian | 8b33f03 | 2012-07-24 20:43:54 -0700 | [diff] [blame] | 1354 | void SurfaceFlinger::setTransactionState( |
| 1355 | const Vector<ComposerState>& state, |
| 1356 | const Vector<DisplayState>& displays, |
| 1357 | uint32_t flags) |
| 1358 | { |
Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1359 | Mutex::Autolock _l(mStateLock); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1360 | |
Mathias Agopian | 8b33f03 | 2012-07-24 20:43:54 -0700 | [diff] [blame] | 1361 | int orientation = eOrientationUnchanged; |
| 1362 | if (displays.size()) { |
| 1363 | // TODO: handle all displays |
| 1364 | orientation = displays[0].orientation; |
| 1365 | } |
| 1366 | |
Jamie Gennis | 2837839 | 2011-10-12 17:39:00 -0700 | [diff] [blame] | 1367 | uint32_t transactionFlags = 0; |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame^] | 1368 | // FIXME: don't hardcode display id here |
| 1369 | if (mCurrentState.displays.valueFor(0).orientation != orientation) { |
Jamie Gennis | b8d69a5 | 2011-10-10 15:48:06 -0700 | [diff] [blame] | 1370 | if (uint32_t(orientation)<=eOrientation270 || orientation==42) { |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame^] | 1371 | mCurrentState.displays.editValueFor(0).orientation = orientation; |
Jamie Gennis | 2837839 | 2011-10-12 17:39:00 -0700 | [diff] [blame] | 1372 | transactionFlags |= eTransactionNeeded; |
Jamie Gennis | b8d69a5 | 2011-10-10 15:48:06 -0700 | [diff] [blame] | 1373 | } else if (orientation != eOrientationUnchanged) { |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1374 | ALOGW("setTransactionState: ignoring unrecognized orientation: %d", |
Jamie Gennis | b8d69a5 | 2011-10-10 15:48:06 -0700 | [diff] [blame] | 1375 | orientation); |
| 1376 | } |
| 1377 | } |
| 1378 | |
Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1379 | const size_t count = state.size(); |
| 1380 | for (size_t i=0 ; i<count ; i++) { |
| 1381 | const ComposerState& s(state[i]); |
| 1382 | sp<Client> client( static_cast<Client *>(s.client.get()) ); |
Jamie Gennis | 2837839 | 2011-10-12 17:39:00 -0700 | [diff] [blame] | 1383 | transactionFlags |= setClientStateLocked(client, s.state); |
Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1384 | } |
Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1385 | |
Mathias Agopian | 386aa98 | 2011-11-07 21:58:03 -0800 | [diff] [blame] | 1386 | if (transactionFlags) { |
| 1387 | // this triggers the transaction |
| 1388 | setTransactionFlags(transactionFlags); |
| 1389 | |
| 1390 | // if this is a synchronous transaction, wait for it to take effect |
| 1391 | // before returning. |
| 1392 | if (flags & eSynchronous) { |
| 1393 | mTransationPending = true; |
| 1394 | } |
| 1395 | while (mTransationPending) { |
| 1396 | status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5)); |
| 1397 | if (CC_UNLIKELY(err != NO_ERROR)) { |
| 1398 | // just in case something goes wrong in SF, return to the |
| 1399 | // called after a few seconds. |
Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1400 | ALOGW_IF(err == TIMED_OUT, "closeGlobalTransaction timed out!"); |
Mathias Agopian | 386aa98 | 2011-11-07 21:58:03 -0800 | [diff] [blame] | 1401 | mTransationPending = false; |
| 1402 | break; |
| 1403 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1404 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1405 | } |
| 1406 | } |
| 1407 | |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 1408 | sp<ISurface> SurfaceFlinger::createLayer( |
Mathias Agopian | 0ef4e15 | 2011-04-20 14:19:32 -0700 | [diff] [blame] | 1409 | ISurfaceComposerClient::surface_data_t* params, |
| 1410 | const String8& name, |
| 1411 | const sp<Client>& client, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1412 | DisplayID d, uint32_t w, uint32_t h, PixelFormat format, |
| 1413 | uint32_t flags) |
| 1414 | { |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1415 | sp<LayerBaseClient> layer; |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1416 | sp<ISurface> surfaceHandle; |
Mathias Agopian | 6e2d648 | 2009-07-09 18:16:43 -0700 | [diff] [blame] | 1417 | |
| 1418 | if (int32_t(w|h) < 0) { |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 1419 | ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)", |
Mathias Agopian | 6e2d648 | 2009-07-09 18:16:43 -0700 | [diff] [blame] | 1420 | int(w), int(h)); |
| 1421 | return surfaceHandle; |
| 1422 | } |
Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1423 | |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 1424 | //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1425 | switch (flags & eFXSurfaceMask) { |
| 1426 | case eFXSurfaceNormal: |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 1427 | layer = createNormalLayer(client, d, w, h, flags, format); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1428 | break; |
| 1429 | case eFXSurfaceBlur: |
Mathias Agopian | 1293a8e | 2010-12-08 17:13:19 -0800 | [diff] [blame] | 1430 | // for now we treat Blur as Dim, until we can implement it |
| 1431 | // efficiently. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1432 | case eFXSurfaceDim: |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 1433 | layer = createDimLayer(client, d, w, h, flags); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1434 | break; |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 1435 | case eFXSurfaceScreenshot: |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 1436 | layer = createScreenshotLayer(client, d, w, h, flags); |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 1437 | break; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1438 | } |
| 1439 | |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1440 | if (layer != 0) { |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1441 | layer->initStates(w, h, flags); |
Mathias Agopian | 285dbde | 2010-03-01 16:09:43 -0800 | [diff] [blame] | 1442 | layer->setName(name); |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1443 | ssize_t token = addClientLayer(client, layer); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1444 | surfaceHandle = layer->getSurface(); |
Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1445 | if (surfaceHandle != 0) { |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1446 | params->token = token; |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 1447 | params->identity = layer->getIdentity(); |
Mathias Agopian | 1c97d2e | 2009-08-19 17:46:26 -0700 | [diff] [blame] | 1448 | } |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1449 | setTransactionFlags(eTransactionNeeded); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | return surfaceHandle; |
| 1453 | } |
| 1454 | |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 1455 | sp<Layer> SurfaceFlinger::createNormalLayer( |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1456 | const sp<Client>& client, DisplayID display, |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1457 | uint32_t w, uint32_t h, uint32_t flags, |
Mathias Agopian | 1c97d2e | 2009-08-19 17:46:26 -0700 | [diff] [blame] | 1458 | PixelFormat& format) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1459 | { |
| 1460 | // initialize the surfaces |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame^] | 1461 | switch (format) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1462 | case PIXEL_FORMAT_TRANSPARENT: |
| 1463 | case PIXEL_FORMAT_TRANSLUCENT: |
| 1464 | format = PIXEL_FORMAT_RGBA_8888; |
| 1465 | break; |
| 1466 | case PIXEL_FORMAT_OPAQUE: |
Mathias Agopian | a8f3e4e | 2010-06-30 15:43:47 -0700 | [diff] [blame] | 1467 | #ifdef NO_RGBX_8888 |
| 1468 | format = PIXEL_FORMAT_RGB_565; |
| 1469 | #else |
Mathias Agopian | 8f10540 | 2010-04-05 18:01:24 -0700 | [diff] [blame] | 1470 | format = PIXEL_FORMAT_RGBX_8888; |
Mathias Agopian | a8f3e4e | 2010-06-30 15:43:47 -0700 | [diff] [blame] | 1471 | #endif |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1472 | break; |
| 1473 | } |
| 1474 | |
Mathias Agopian | a8f3e4e | 2010-06-30 15:43:47 -0700 | [diff] [blame] | 1475 | #ifdef NO_RGBX_8888 |
| 1476 | if (format == PIXEL_FORMAT_RGBX_8888) |
| 1477 | format = PIXEL_FORMAT_RGBA_8888; |
| 1478 | #endif |
| 1479 | |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1480 | sp<Layer> layer = new Layer(this, display, client); |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1481 | status_t err = layer->setBuffers(w, h, format, flags); |
Glenn Kasten | 99ed224 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 1482 | if (CC_LIKELY(err != NO_ERROR)) { |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 1483 | ALOGE("createNormalLayer() failed (%s)", strerror(-err)); |
Mathias Agopian | 076b1cc | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 1484 | layer.clear(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1485 | } |
| 1486 | return layer; |
| 1487 | } |
| 1488 | |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 1489 | sp<LayerDim> SurfaceFlinger::createDimLayer( |
Mathias Agopian | f9d9327 | 2009-06-19 17:00:27 -0700 | [diff] [blame] | 1490 | const sp<Client>& client, DisplayID display, |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1491 | uint32_t w, uint32_t h, uint32_t flags) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1492 | { |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1493 | sp<LayerDim> layer = new LayerDim(this, display, client); |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 1494 | return layer; |
| 1495 | } |
| 1496 | |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 1497 | sp<LayerScreenshot> SurfaceFlinger::createScreenshotLayer( |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 1498 | const sp<Client>& client, DisplayID display, |
| 1499 | uint32_t w, uint32_t h, uint32_t flags) |
| 1500 | { |
| 1501 | sp<LayerScreenshot> layer = new LayerScreenshot(this, display, client); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1502 | return layer; |
| 1503 | } |
| 1504 | |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 1505 | status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, SurfaceID sid) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1506 | { |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1507 | /* |
| 1508 | * called by the window manager, when a surface should be marked for |
| 1509 | * destruction. |
Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1510 | * |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 1511 | * The surface is removed from the current and drawing lists, but placed |
| 1512 | * in the purgatory queue, so it's not destroyed right-away (we need |
| 1513 | * to wait for all client's references to go away first). |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1514 | */ |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1515 | |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 1516 | status_t err = NAME_NOT_FOUND; |
Mathias Agopian | 0aa758d | 2009-04-22 15:23:34 -0700 | [diff] [blame] | 1517 | Mutex::Autolock _l(mStateLock); |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1518 | sp<LayerBaseClient> layer = client->getLayerUser(sid); |
Daniel Lam | b267579 | 2012-02-23 14:35:13 -0800 | [diff] [blame] | 1519 | |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 1520 | if (layer != 0) { |
| 1521 | err = purgatorizeLayer_l(layer); |
| 1522 | if (err == NO_ERROR) { |
Mathias Agopian | 48d819a | 2009-09-10 19:41:18 -0700 | [diff] [blame] | 1523 | setTransactionFlags(eTransactionNeeded); |
| 1524 | } |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1525 | } |
| 1526 | return err; |
| 1527 | } |
| 1528 | |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 1529 | status_t SurfaceFlinger::onLayerDestroyed(const wp<LayerBaseClient>& layer) |
Mathias Agopian | 9a11206 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 1530 | { |
Mathias Agopian | 759fdb2 | 2009-07-02 17:33:40 -0700 | [diff] [blame] | 1531 | // called by ~ISurface() when all references are gone |
Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 1532 | status_t err = NO_ERROR; |
| 1533 | sp<LayerBaseClient> l(layer.promote()); |
| 1534 | if (l != NULL) { |
| 1535 | Mutex::Autolock _l(mStateLock); |
| 1536 | err = removeLayer_l(l); |
| 1537 | if (err == NAME_NOT_FOUND) { |
| 1538 | // The surface wasn't in the current list, which means it was |
| 1539 | // removed already, which means it is in the purgatory, |
| 1540 | // and need to be removed from there. |
| 1541 | ssize_t idx = mLayerPurgatory.remove(l); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1542 | ALOGE_IF(idx < 0, |
Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 1543 | "layer=%p is not in the purgatory list", l.get()); |
Mathias Agopian | f1d8e87 | 2009-04-20 19:39:12 -0700 | [diff] [blame] | 1544 | } |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1545 | ALOGE_IF(err<0 && err != NAME_NOT_FOUND, |
Mathias Agopian | ca4d360 | 2011-05-19 15:38:14 -0700 | [diff] [blame] | 1546 | "error removing layer=%p (%s)", l.get(), strerror(-err)); |
| 1547 | } |
| 1548 | return err; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1549 | } |
| 1550 | |
Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1551 | uint32_t SurfaceFlinger::setClientStateLocked( |
Mathias Agopian | 96f0819 | 2010-06-02 23:28:45 -0700 | [diff] [blame] | 1552 | const sp<Client>& client, |
Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1553 | const layer_state_t& s) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1554 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1555 | uint32_t flags = 0; |
Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1556 | sp<LayerBaseClient> layer(client->getLayerUser(s.surface)); |
| 1557 | if (layer != 0) { |
| 1558 | const uint32_t what = s.what; |
| 1559 | if (what & ePositionChanged) { |
| 1560 | if (layer->setPosition(s.x, s.y)) |
| 1561 | flags |= eTraversalNeeded; |
| 1562 | } |
| 1563 | if (what & eLayerChanged) { |
Mathias Agopian | be246f8 | 2012-07-31 22:59:38 -0700 | [diff] [blame] | 1564 | // NOTE: index needs to be calculated before we update the state |
Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1565 | ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer); |
| 1566 | if (layer->setLayer(s.z)) { |
| 1567 | mCurrentState.layersSortedByZ.removeAt(idx); |
| 1568 | mCurrentState.layersSortedByZ.add(layer); |
| 1569 | // we need traversal (state changed) |
| 1570 | // AND transaction (list changed) |
| 1571 | flags |= eTransactionNeeded|eTraversalNeeded; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1572 | } |
| 1573 | } |
Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1574 | if (what & eSizeChanged) { |
| 1575 | if (layer->setSize(s.w, s.h)) { |
| 1576 | flags |= eTraversalNeeded; |
Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1577 | } |
| 1578 | } |
| 1579 | if (what & eAlphaChanged) { |
| 1580 | if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f))) |
| 1581 | flags |= eTraversalNeeded; |
| 1582 | } |
| 1583 | if (what & eMatrixChanged) { |
| 1584 | if (layer->setMatrix(s.matrix)) |
| 1585 | flags |= eTraversalNeeded; |
| 1586 | } |
| 1587 | if (what & eTransparentRegionChanged) { |
| 1588 | if (layer->setTransparentRegionHint(s.transparentRegion)) |
| 1589 | flags |= eTraversalNeeded; |
| 1590 | } |
| 1591 | if (what & eVisibilityChanged) { |
| 1592 | if (layer->setFlags(s.flags, s.mask)) |
| 1593 | flags |= eTraversalNeeded; |
| 1594 | } |
Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 1595 | if (what & eCropChanged) { |
| 1596 | if (layer->setCrop(s.crop)) |
| 1597 | flags |= eTraversalNeeded; |
| 1598 | } |
Mathias Agopian | 8785578 | 2012-07-24 21:41:09 -0700 | [diff] [blame] | 1599 | if (what & eLayerStackChanged) { |
Mathias Agopian | be246f8 | 2012-07-31 22:59:38 -0700 | [diff] [blame] | 1600 | // NOTE: index needs to be calculated before we update the state |
| 1601 | ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer); |
| 1602 | if (layer->setLayerStack(s.layerStack)) { |
| 1603 | mCurrentState.layersSortedByZ.removeAt(idx); |
| 1604 | mCurrentState.layersSortedByZ.add(layer); |
| 1605 | // we need traversal (state changed) |
| 1606 | // AND transaction (list changed) |
| 1607 | flags |= eTransactionNeeded|eTraversalNeeded; |
| 1608 | } |
Mathias Agopian | 8785578 | 2012-07-24 21:41:09 -0700 | [diff] [blame] | 1609 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1610 | } |
Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1611 | return flags; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1612 | } |
| 1613 | |
Mathias Agopian | b60314a | 2012-04-10 22:09:54 -0700 | [diff] [blame] | 1614 | // --------------------------------------------------------------------------- |
| 1615 | |
| 1616 | void SurfaceFlinger::onScreenAcquired() { |
Colin Cross | 8e53306 | 2012-06-07 13:17:52 -0700 | [diff] [blame] | 1617 | ALOGD("Screen about to return, flinger = %p", this); |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 1618 | const DisplayDevice& hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 1619 | getHwComposer().acquire(); |
Mathias Agopian | b60314a | 2012-04-10 22:09:54 -0700 | [diff] [blame] | 1620 | hw.acquireScreen(); |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 1621 | mEventThread->onScreenAcquired(); |
Mathias Agopian | b60314a | 2012-04-10 22:09:54 -0700 | [diff] [blame] | 1622 | // this is a temporary work-around, eventually this should be called |
| 1623 | // by the power-manager |
| 1624 | SurfaceFlinger::turnElectronBeamOn(mElectronBeamAnimationMode); |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 1625 | // from this point on, SF will process updates again |
Mathias Agopian | 8acce20 | 2012-04-13 16:18:55 -0700 | [diff] [blame] | 1626 | repaintEverything(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1627 | } |
| 1628 | |
Mathias Agopian | b60314a | 2012-04-10 22:09:54 -0700 | [diff] [blame] | 1629 | void SurfaceFlinger::onScreenReleased() { |
Colin Cross | 8e53306 | 2012-06-07 13:17:52 -0700 | [diff] [blame] | 1630 | ALOGD("About to give-up screen, flinger = %p", this); |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 1631 | const DisplayDevice& hw(getDefaultDisplayDevice()); // XXX: this should be per DisplayDevice |
Mathias Agopian | b60314a | 2012-04-10 22:09:54 -0700 | [diff] [blame] | 1632 | if (hw.isScreenAcquired()) { |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 1633 | mEventThread->onScreenReleased(); |
Mathias Agopian | b60314a | 2012-04-10 22:09:54 -0700 | [diff] [blame] | 1634 | hw.releaseScreen(); |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 1635 | getHwComposer().release(); |
Mathias Agopian | b60314a | 2012-04-10 22:09:54 -0700 | [diff] [blame] | 1636 | // from this point on, SF will stop drawing |
| 1637 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1638 | } |
| 1639 | |
Colin Cross | 8e53306 | 2012-06-07 13:17:52 -0700 | [diff] [blame] | 1640 | void SurfaceFlinger::unblank() { |
Mathias Agopian | b60314a | 2012-04-10 22:09:54 -0700 | [diff] [blame] | 1641 | class MessageScreenAcquired : public MessageBase { |
| 1642 | SurfaceFlinger* flinger; |
| 1643 | public: |
| 1644 | MessageScreenAcquired(SurfaceFlinger* flinger) : flinger(flinger) { } |
| 1645 | virtual bool handler() { |
| 1646 | flinger->onScreenAcquired(); |
| 1647 | return true; |
| 1648 | } |
| 1649 | }; |
| 1650 | sp<MessageBase> msg = new MessageScreenAcquired(this); |
| 1651 | postMessageSync(msg); |
| 1652 | } |
| 1653 | |
Colin Cross | 8e53306 | 2012-06-07 13:17:52 -0700 | [diff] [blame] | 1654 | void SurfaceFlinger::blank() { |
Mathias Agopian | b60314a | 2012-04-10 22:09:54 -0700 | [diff] [blame] | 1655 | class MessageScreenReleased : public MessageBase { |
| 1656 | SurfaceFlinger* flinger; |
| 1657 | public: |
| 1658 | MessageScreenReleased(SurfaceFlinger* flinger) : flinger(flinger) { } |
| 1659 | virtual bool handler() { |
| 1660 | flinger->onScreenReleased(); |
| 1661 | return true; |
| 1662 | } |
| 1663 | }; |
| 1664 | sp<MessageBase> msg = new MessageScreenReleased(this); |
| 1665 | postMessageSync(msg); |
| 1666 | } |
| 1667 | |
| 1668 | // --------------------------------------------------------------------------- |
| 1669 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1670 | status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args) |
| 1671 | { |
Erik Gilling | 1d21a9c | 2010-12-01 16:38:01 -0800 | [diff] [blame] | 1672 | const size_t SIZE = 4096; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1673 | char buffer[SIZE]; |
| 1674 | String8 result; |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 1675 | |
| 1676 | if (!PermissionCache::checkCallingPermission(sDump)) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1677 | snprintf(buffer, SIZE, "Permission Denial: " |
| 1678 | "can't dump SurfaceFlinger from pid=%d, uid=%d\n", |
| 1679 | IPCThreadState::self()->getCallingPid(), |
| 1680 | IPCThreadState::self()->getCallingUid()); |
| 1681 | result.append(buffer); |
| 1682 | } else { |
Mathias Agopian | 9795c42 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1683 | // Try to get the main lock, but don't insist if we can't |
| 1684 | // (this would indicate SF is stuck, but we want to be able to |
| 1685 | // print something in dumpsys). |
| 1686 | int retry = 3; |
| 1687 | while (mStateLock.tryLock()<0 && --retry>=0) { |
| 1688 | usleep(1000000); |
| 1689 | } |
| 1690 | const bool locked(retry >= 0); |
| 1691 | if (!locked) { |
Andreas Huber | 8b42e8a | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 1692 | snprintf(buffer, SIZE, |
Mathias Agopian | 9795c42 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1693 | "SurfaceFlinger appears to be unresponsive, " |
| 1694 | "dumping anyways (no locks held)\n"); |
| 1695 | result.append(buffer); |
| 1696 | } |
| 1697 | |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 1698 | bool dumpAll = true; |
| 1699 | size_t index = 0; |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 1700 | size_t numArgs = args.size(); |
| 1701 | if (numArgs) { |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 1702 | if ((index < numArgs) && |
| 1703 | (args[index] == String16("--list"))) { |
| 1704 | index++; |
| 1705 | listLayersLocked(args, index, result, buffer, SIZE); |
Mathias Agopian | 35aadd6 | 2012-03-08 22:01:51 -0800 | [diff] [blame] | 1706 | dumpAll = false; |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | if ((index < numArgs) && |
| 1710 | (args[index] == String16("--latency"))) { |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 1711 | index++; |
| 1712 | dumpStatsLocked(args, index, result, buffer, SIZE); |
Mathias Agopian | 35aadd6 | 2012-03-08 22:01:51 -0800 | [diff] [blame] | 1713 | dumpAll = false; |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 1714 | } |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 1715 | |
| 1716 | if ((index < numArgs) && |
| 1717 | (args[index] == String16("--latency-clear"))) { |
| 1718 | index++; |
| 1719 | clearStatsLocked(args, index, result, buffer, SIZE); |
Mathias Agopian | 35aadd6 | 2012-03-08 22:01:51 -0800 | [diff] [blame] | 1720 | dumpAll = false; |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 1721 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1722 | } |
Mathias Agopian | 1b5e102 | 2010-04-20 17:55:49 -0700 | [diff] [blame] | 1723 | |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 1724 | if (dumpAll) { |
| 1725 | dumpAllLocked(result, buffer, SIZE); |
Mathias Agopian | 48b888a | 2011-01-19 16:15:53 -0800 | [diff] [blame] | 1726 | } |
| 1727 | |
Mathias Agopian | 9795c42 | 2009-08-26 16:36:26 -0700 | [diff] [blame] | 1728 | if (locked) { |
| 1729 | mStateLock.unlock(); |
| 1730 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1731 | } |
| 1732 | write(fd, result.string(), result.size()); |
| 1733 | return NO_ERROR; |
| 1734 | } |
| 1735 | |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 1736 | void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index, |
| 1737 | String8& result, char* buffer, size_t SIZE) const |
| 1738 | { |
| 1739 | const LayerVector& currentLayers = mCurrentState.layersSortedByZ; |
| 1740 | const size_t count = currentLayers.size(); |
| 1741 | for (size_t i=0 ; i<count ; i++) { |
| 1742 | const sp<LayerBase>& layer(currentLayers[i]); |
| 1743 | snprintf(buffer, SIZE, "%s\n", layer->getName().string()); |
| 1744 | result.append(buffer); |
| 1745 | } |
| 1746 | } |
| 1747 | |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 1748 | void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index, |
| 1749 | String8& result, char* buffer, size_t SIZE) const |
| 1750 | { |
| 1751 | String8 name; |
| 1752 | if (index < args.size()) { |
| 1753 | name = String8(args[index]); |
| 1754 | index++; |
| 1755 | } |
| 1756 | |
| 1757 | const LayerVector& currentLayers = mCurrentState.layersSortedByZ; |
| 1758 | const size_t count = currentLayers.size(); |
| 1759 | for (size_t i=0 ; i<count ; i++) { |
| 1760 | const sp<LayerBase>& layer(currentLayers[i]); |
| 1761 | if (name.isEmpty()) { |
| 1762 | snprintf(buffer, SIZE, "%s\n", layer->getName().string()); |
| 1763 | result.append(buffer); |
| 1764 | } |
| 1765 | if (name.isEmpty() || (name == layer->getName())) { |
| 1766 | layer->dumpStats(result, buffer, SIZE); |
| 1767 | } |
| 1768 | } |
| 1769 | } |
| 1770 | |
Mathias Agopian | 25e66fc | 2012-01-28 22:31:55 -0800 | [diff] [blame] | 1771 | void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index, |
| 1772 | String8& result, char* buffer, size_t SIZE) const |
| 1773 | { |
| 1774 | String8 name; |
| 1775 | if (index < args.size()) { |
| 1776 | name = String8(args[index]); |
| 1777 | index++; |
| 1778 | } |
| 1779 | |
| 1780 | const LayerVector& currentLayers = mCurrentState.layersSortedByZ; |
| 1781 | const size_t count = currentLayers.size(); |
| 1782 | for (size_t i=0 ; i<count ; i++) { |
| 1783 | const sp<LayerBase>& layer(currentLayers[i]); |
| 1784 | if (name.isEmpty() || (name == layer->getName())) { |
| 1785 | layer->clearStats(); |
| 1786 | } |
| 1787 | } |
| 1788 | } |
| 1789 | |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 1790 | void SurfaceFlinger::dumpAllLocked( |
| 1791 | String8& result, char* buffer, size_t SIZE) const |
| 1792 | { |
| 1793 | // figure out if we're stuck somewhere |
| 1794 | const nsecs_t now = systemTime(); |
| 1795 | const nsecs_t inSwapBuffers(mDebugInSwapBuffers); |
| 1796 | const nsecs_t inTransaction(mDebugInTransaction); |
| 1797 | nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0; |
| 1798 | nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0; |
| 1799 | |
| 1800 | /* |
| 1801 | * Dump the visible layer list |
| 1802 | */ |
| 1803 | const LayerVector& currentLayers = mCurrentState.layersSortedByZ; |
| 1804 | const size_t count = currentLayers.size(); |
| 1805 | snprintf(buffer, SIZE, "Visible layers (count = %d)\n", count); |
| 1806 | result.append(buffer); |
| 1807 | for (size_t i=0 ; i<count ; i++) { |
| 1808 | const sp<LayerBase>& layer(currentLayers[i]); |
| 1809 | layer->dump(result, buffer, SIZE); |
| 1810 | } |
| 1811 | |
| 1812 | /* |
| 1813 | * Dump the layers in the purgatory |
| 1814 | */ |
| 1815 | |
| 1816 | const size_t purgatorySize = mLayerPurgatory.size(); |
| 1817 | snprintf(buffer, SIZE, "Purgatory state (%d entries)\n", purgatorySize); |
| 1818 | result.append(buffer); |
| 1819 | for (size_t i=0 ; i<purgatorySize ; i++) { |
| 1820 | const sp<LayerBase>& layer(mLayerPurgatory.itemAt(i)); |
| 1821 | layer->shortDump(result, buffer, SIZE); |
| 1822 | } |
| 1823 | |
| 1824 | /* |
| 1825 | * Dump SurfaceFlinger global state |
| 1826 | */ |
| 1827 | |
| 1828 | snprintf(buffer, SIZE, "SurfaceFlinger global state:\n"); |
| 1829 | result.append(buffer); |
| 1830 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 1831 | const DisplayDevice& hw(getDefaultDisplayDevice()); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 1832 | const GLExtensions& extensions(GLExtensions::getInstance()); |
| 1833 | snprintf(buffer, SIZE, "GLES: %s, %s, %s\n", |
| 1834 | extensions.getVendor(), |
| 1835 | extensions.getRenderer(), |
| 1836 | extensions.getVersion()); |
| 1837 | result.append(buffer); |
| 1838 | |
| 1839 | snprintf(buffer, SIZE, "EGL : %s\n", |
Mathias Agopian | d3ee231 | 2012-08-02 14:01:42 -0700 | [diff] [blame] | 1840 | eglQueryString(mEGLDisplay, EGL_VERSION_HW_ANDROID)); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 1841 | result.append(buffer); |
| 1842 | |
| 1843 | snprintf(buffer, SIZE, "EXTS: %s\n", extensions.getExtension()); |
| 1844 | result.append(buffer); |
| 1845 | |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 1846 | hw.undefinedRegion.dump(result, "undefinedRegion"); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 1847 | snprintf(buffer, SIZE, |
| 1848 | " orientation=%d, canDraw=%d\n", |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame^] | 1849 | hw.getOrientation(), hw.canDraw()); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 1850 | result.append(buffer); |
| 1851 | snprintf(buffer, SIZE, |
| 1852 | " last eglSwapBuffers() time: %f us\n" |
| 1853 | " last transaction time : %f us\n" |
Mathias Agopian | c95dbdc | 2012-02-05 00:19:27 -0800 | [diff] [blame] | 1854 | " transaction-flags : %08x\n" |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 1855 | " refresh-rate : %f fps\n" |
| 1856 | " x-dpi : %f\n" |
Mathias Agopian | b5dd9c0 | 2012-03-22 12:15:54 -0700 | [diff] [blame] | 1857 | " y-dpi : %f\n" |
| 1858 | " density : %f\n", |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 1859 | mLastSwapBufferTime/1000.0, |
| 1860 | mLastTransactionTime/1000.0, |
Mathias Agopian | c95dbdc | 2012-02-05 00:19:27 -0800 | [diff] [blame] | 1861 | mTransactionFlags, |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 1862 | hw.getRefreshRate(), |
| 1863 | hw.getDpiX(), |
Mathias Agopian | b5dd9c0 | 2012-03-22 12:15:54 -0700 | [diff] [blame] | 1864 | hw.getDpiY(), |
| 1865 | hw.getDensity()); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 1866 | result.append(buffer); |
| 1867 | |
| 1868 | snprintf(buffer, SIZE, " eglSwapBuffers time: %f us\n", |
| 1869 | inSwapBuffersDuration/1000.0); |
| 1870 | result.append(buffer); |
| 1871 | |
| 1872 | snprintf(buffer, SIZE, " transaction time: %f us\n", |
| 1873 | inTransactionDuration/1000.0); |
| 1874 | result.append(buffer); |
| 1875 | |
| 1876 | /* |
| 1877 | * VSYNC state |
| 1878 | */ |
| 1879 | mEventThread->dump(result, buffer, SIZE); |
| 1880 | |
| 1881 | /* |
| 1882 | * Dump HWComposer state |
| 1883 | */ |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 1884 | HWComposer& hwc(getHwComposer()); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 1885 | snprintf(buffer, SIZE, "h/w composer state:\n"); |
| 1886 | result.append(buffer); |
| 1887 | snprintf(buffer, SIZE, " h/w composer %s and %s\n", |
| 1888 | hwc.initCheck()==NO_ERROR ? "present" : "not present", |
| 1889 | (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled"); |
| 1890 | result.append(buffer); |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 1891 | hwc.dump(result, buffer, SIZE, hw.getVisibleLayersSortedByZ()); |
Mathias Agopian | 82d7ab6 | 2012-01-19 18:34:40 -0800 | [diff] [blame] | 1892 | |
| 1893 | /* |
| 1894 | * Dump gralloc state |
| 1895 | */ |
| 1896 | const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get()); |
| 1897 | alloc.dump(result); |
| 1898 | hw.dump(result); |
| 1899 | } |
| 1900 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1901 | status_t SurfaceFlinger::onTransact( |
| 1902 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 1903 | { |
| 1904 | switch (code) { |
| 1905 | case CREATE_CONNECTION: |
Mathias Agopian | 698c087 | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 1906 | case SET_TRANSACTION_STATE: |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1907 | case SET_ORIENTATION: |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1908 | case BOOT_FINISHED: |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 1909 | case TURN_ELECTRON_BEAM_OFF: |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 1910 | case TURN_ELECTRON_BEAM_ON: |
Colin Cross | 8e53306 | 2012-06-07 13:17:52 -0700 | [diff] [blame] | 1911 | case BLANK: |
| 1912 | case UNBLANK: |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1913 | { |
| 1914 | // codes that require permission check |
| 1915 | IPCThreadState* ipc = IPCThreadState::self(); |
| 1916 | const int pid = ipc->getCallingPid(); |
Mathias Agopian | a1ecca9 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 1917 | const int uid = ipc->getCallingUid(); |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 1918 | if ((uid != AID_GRAPHICS) && |
| 1919 | !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1920 | ALOGE("Permission Denial: " |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 1921 | "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid); |
| 1922 | return PERMISSION_DENIED; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1923 | } |
Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 1924 | break; |
| 1925 | } |
| 1926 | case CAPTURE_SCREEN: |
| 1927 | { |
| 1928 | // codes that require permission check |
| 1929 | IPCThreadState* ipc = IPCThreadState::self(); |
| 1930 | const int pid = ipc->getCallingPid(); |
| 1931 | const int uid = ipc->getCallingUid(); |
Mathias Agopian | 99b4984 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 1932 | if ((uid != AID_GRAPHICS) && |
| 1933 | !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) { |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1934 | ALOGE("Permission Denial: " |
Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 1935 | "can't read framebuffer pid=%d, uid=%d", pid, uid); |
| 1936 | return PERMISSION_DENIED; |
| 1937 | } |
| 1938 | break; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1939 | } |
| 1940 | } |
Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 1941 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1942 | status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags); |
| 1943 | if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) { |
Mathias Agopian | b8a5560 | 2009-06-26 19:06:36 -0700 | [diff] [blame] | 1944 | CHECK_INTERFACE(ISurfaceComposer, data, reply); |
Glenn Kasten | 99ed224 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 1945 | if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) { |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 1946 | IPCThreadState* ipc = IPCThreadState::self(); |
| 1947 | const int pid = ipc->getCallingPid(); |
| 1948 | const int uid = ipc->getCallingUid(); |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1949 | ALOGE("Permission Denial: " |
Mathias Agopian | 375f563 | 2009-06-15 18:24:59 -0700 | [diff] [blame] | 1950 | "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1951 | return PERMISSION_DENIED; |
| 1952 | } |
| 1953 | int n; |
| 1954 | switch (code) { |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 1955 | case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE |
Mathias Agopian | 35b48d1 | 2010-09-13 22:57:58 -0700 | [diff] [blame] | 1956 | case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1957 | return NO_ERROR; |
| 1958 | case 1002: // SHOW_UPDATES |
| 1959 | n = data.readInt32(); |
| 1960 | mDebugRegion = n ? n : (mDebugRegion ? 0 : 1); |
Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1961 | invalidateHwcGeometry(); |
| 1962 | repaintEverything(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1963 | return NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1964 | case 1004:{ // repaint everything |
Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1965 | repaintEverything(); |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1966 | return NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1967 | } |
Mathias Agopian | cbb288b | 2009-09-07 16:32:45 -0700 | [diff] [blame] | 1968 | case 1005:{ // force transaction |
| 1969 | setTransactionFlags(eTransactionNeeded|eTraversalNeeded); |
| 1970 | return NO_ERROR; |
| 1971 | } |
Mathias Agopian | 4d143ee | 2012-02-23 20:05:39 -0800 | [diff] [blame] | 1972 | case 1006:{ // send empty update |
| 1973 | signalRefresh(); |
| 1974 | return NO_ERROR; |
| 1975 | } |
Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 1976 | case 1008: // toggle use of hw composer |
| 1977 | n = data.readInt32(); |
| 1978 | mDebugDisableHWC = n ? 1 : 0; |
| 1979 | invalidateHwcGeometry(); |
| 1980 | repaintEverything(); |
| 1981 | return NO_ERROR; |
Mathias Agopian | a458364 | 2011-08-23 18:03:18 -0700 | [diff] [blame] | 1982 | case 1009: // toggle use of transform hint |
| 1983 | n = data.readInt32(); |
| 1984 | mDebugDisableTransformHint = n ? 1 : 0; |
| 1985 | invalidateHwcGeometry(); |
| 1986 | repaintEverything(); |
| 1987 | return NO_ERROR; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1988 | case 1010: // interrogate. |
Mathias Agopian | 01b7668 | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 1989 | reply->writeInt32(0); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1990 | reply->writeInt32(0); |
| 1991 | reply->writeInt32(mDebugRegion); |
Mathias Agopian | b9494d5 | 2012-04-18 02:28:45 -0700 | [diff] [blame] | 1992 | reply->writeInt32(0); |
Dianne Hackborn | 12839be | 2012-02-06 21:21:05 -0800 | [diff] [blame] | 1993 | reply->writeInt32(mDebugDisableHWC); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1994 | return NO_ERROR; |
| 1995 | case 1013: { |
| 1996 | Mutex::Autolock _l(mStateLock); |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 1997 | const DisplayDevice& hw(getDefaultDisplayDevice()); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1998 | reply->writeInt32(hw.getPageFlipCount()); |
| 1999 | } |
| 2000 | return NO_ERROR; |
| 2001 | } |
| 2002 | } |
| 2003 | return err; |
| 2004 | } |
| 2005 | |
Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 2006 | void SurfaceFlinger::repaintEverything() { |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 2007 | android_atomic_or(1, &mRepaintEverything); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 2008 | signalTransaction(); |
Mathias Agopian | 53331da | 2011-08-22 21:44:41 -0700 | [diff] [blame] | 2009 | } |
| 2010 | |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2011 | // --------------------------------------------------------------------------- |
| 2012 | |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 2013 | status_t SurfaceFlinger::renderScreenToTexture(DisplayID dpy, |
| 2014 | GLuint* textureName, GLfloat* uOut, GLfloat* vOut) |
| 2015 | { |
| 2016 | Mutex::Autolock _l(mStateLock); |
| 2017 | return renderScreenToTextureLocked(dpy, textureName, uOut, vOut); |
| 2018 | } |
| 2019 | |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2020 | status_t SurfaceFlinger::renderScreenToTextureLocked(DisplayID dpy, |
| 2021 | GLuint* textureName, GLfloat* uOut, GLfloat* vOut) |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2022 | { |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 2023 | ATRACE_CALL(); |
| 2024 | |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2025 | if (!GLExtensions::getInstance().haveFramebufferObject()) |
| 2026 | return INVALID_OPERATION; |
| 2027 | |
| 2028 | // get screen geometry |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 2029 | const DisplayDevice& hw(getDisplayDevice(dpy)); |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2030 | const uint32_t hw_w = hw.getWidth(); |
| 2031 | const uint32_t hw_h = hw.getHeight(); |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2032 | GLfloat u = 1; |
| 2033 | GLfloat v = 1; |
| 2034 | |
| 2035 | // make sure to clear all GL error flags |
| 2036 | while ( glGetError() != GL_NO_ERROR ) ; |
| 2037 | |
| 2038 | // create a FBO |
| 2039 | GLuint name, tname; |
| 2040 | glGenTextures(1, &tname); |
| 2041 | glBindTexture(GL_TEXTURE_2D, tname); |
Mathias Agopian | a2f4e56 | 2012-04-15 23:34:59 -0700 | [diff] [blame] | 2042 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 2043 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2044 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, |
| 2045 | hw_w, hw_h, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2046 | if (glGetError() != GL_NO_ERROR) { |
Mathias Agopian | 015fb3f | 2010-10-14 12:19:37 -0700 | [diff] [blame] | 2047 | while ( glGetError() != GL_NO_ERROR ) ; |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2048 | GLint tw = (2 << (31 - clz(hw_w))); |
| 2049 | GLint th = (2 << (31 - clz(hw_h))); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2050 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, |
| 2051 | tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2052 | u = GLfloat(hw_w) / tw; |
| 2053 | v = GLfloat(hw_h) / th; |
| 2054 | } |
| 2055 | glGenFramebuffersOES(1, &name); |
| 2056 | glBindFramebufferOES(GL_FRAMEBUFFER_OES, name); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2057 | glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES, |
| 2058 | GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0); |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2059 | |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2060 | // redraw the screen entirely... |
Mathias Agopian | c492e67 | 2011-10-18 14:49:27 -0700 | [diff] [blame] | 2061 | glDisable(GL_TEXTURE_EXTERNAL_OES); |
| 2062 | glDisable(GL_TEXTURE_2D); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2063 | glClearColor(0,0,0,1); |
| 2064 | glClear(GL_COLOR_BUFFER_BIT); |
Mathias Agopian | a9040d0 | 2011-10-10 19:02:07 -0700 | [diff] [blame] | 2065 | glMatrixMode(GL_MODELVIEW); |
| 2066 | glLoadIdentity(); |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 2067 | const Vector< sp<LayerBase> >& layers(hw.getVisibleLayersSortedByZ()); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2068 | const size_t count = layers.size(); |
| 2069 | for (size_t i=0 ; i<count ; ++i) { |
| 2070 | const sp<LayerBase>& layer(layers[i]); |
Mathias Agopian | fcb239d | 2012-08-02 16:01:34 -0700 | [diff] [blame] | 2071 | layer->draw(hw); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2072 | } |
| 2073 | |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 2074 | hw.compositionComplete(); |
| 2075 | |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2076 | // back to main framebuffer |
| 2077 | glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2078 | glDeleteFramebuffersOES(1, &name); |
| 2079 | |
| 2080 | *textureName = tname; |
| 2081 | *uOut = u; |
| 2082 | *vOut = v; |
| 2083 | return NO_ERROR; |
| 2084 | } |
| 2085 | |
| 2086 | // --------------------------------------------------------------------------- |
| 2087 | |
Mathias Agopian | ed9807b | 2012-05-18 14:18:08 -0700 | [diff] [blame] | 2088 | class VSyncWaiter { |
| 2089 | DisplayEventReceiver::Event buffer[4]; |
| 2090 | sp<Looper> looper; |
| 2091 | sp<IDisplayEventConnection> events; |
| 2092 | sp<BitTube> eventTube; |
| 2093 | public: |
| 2094 | VSyncWaiter(const sp<EventThread>& eventThread) { |
| 2095 | looper = new Looper(true); |
| 2096 | events = eventThread->createEventConnection(); |
| 2097 | eventTube = events->getDataChannel(); |
| 2098 | looper->addFd(eventTube->getFd(), 0, ALOOPER_EVENT_INPUT, 0, 0); |
| 2099 | events->requestNextVsync(); |
| 2100 | } |
| 2101 | |
| 2102 | void wait() { |
| 2103 | ssize_t n; |
| 2104 | |
| 2105 | looper->pollOnce(-1); |
| 2106 | // we don't handle any errors here, it doesn't matter |
| 2107 | // and we don't want to take the risk to get stuck. |
| 2108 | |
| 2109 | // drain the events... |
| 2110 | while ((n = DisplayEventReceiver::getEvents( |
| 2111 | eventTube, buffer, 4)) > 0) ; |
| 2112 | |
| 2113 | events->requestNextVsync(); |
| 2114 | } |
| 2115 | }; |
| 2116 | |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2117 | status_t SurfaceFlinger::electronBeamOffAnimationImplLocked() |
| 2118 | { |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2119 | // get screen geometry |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 2120 | const DisplayDevice& hw(getDefaultDisplayDevice()); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2121 | const uint32_t hw_w = hw.getWidth(); |
| 2122 | const uint32_t hw_h = hw.getHeight(); |
Mathias Agopian | a9040d0 | 2011-10-10 19:02:07 -0700 | [diff] [blame] | 2123 | const Region screenBounds(hw.getBounds()); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2124 | |
| 2125 | GLfloat u, v; |
| 2126 | GLuint tname; |
Mathias Agopian | 118d024 | 2011-10-13 16:02:48 -0700 | [diff] [blame] | 2127 | status_t result = renderScreenToTextureLocked(0, &tname, &u, &v); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2128 | if (result != NO_ERROR) { |
| 2129 | return result; |
| 2130 | } |
| 2131 | |
| 2132 | GLfloat vtx[8]; |
Mathias Agopian | a9040d0 | 2011-10-10 19:02:07 -0700 | [diff] [blame] | 2133 | const GLfloat texCoords[4][2] = { {0,0}, {0,v}, {u,v}, {u,0} }; |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2134 | glBindTexture(GL_TEXTURE_2D, tname); |
| 2135 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 2136 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 2137 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
Michael I. Gold | b1d1c6d | 2012-01-13 00:36:45 -0800 | [diff] [blame] | 2138 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 2139 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2140 | glTexCoordPointer(2, GL_FLOAT, 0, texCoords); |
| 2141 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 2142 | glVertexPointer(2, GL_FLOAT, 0, vtx); |
| 2143 | |
Mathias Agopian | ffcf465 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 2144 | /* |
| 2145 | * Texture coordinate mapping |
| 2146 | * |
| 2147 | * u |
| 2148 | * 1 +----------+---+ |
| 2149 | * | | | | image is inverted |
| 2150 | * | V | | w.r.t. the texture |
| 2151 | * 1-v +----------+ | coordinates |
| 2152 | * | | |
| 2153 | * | | |
| 2154 | * | | |
| 2155 | * 0 +--------------+ |
| 2156 | * 0 1 |
| 2157 | * |
| 2158 | */ |
| 2159 | |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2160 | class s_curve_interpolator { |
| 2161 | const float nbFrames, s, v; |
| 2162 | public: |
| 2163 | s_curve_interpolator(int nbFrames, float s) |
| 2164 | : nbFrames(1.0f / (nbFrames-1)), s(s), |
| 2165 | v(1.0f + expf(-s + 0.5f*s)) { |
| 2166 | } |
| 2167 | float operator()(int f) { |
| 2168 | const float x = f * nbFrames; |
| 2169 | return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f; |
| 2170 | } |
| 2171 | }; |
| 2172 | |
| 2173 | class v_stretch { |
| 2174 | const GLfloat hw_w, hw_h; |
| 2175 | public: |
| 2176 | v_stretch(uint32_t hw_w, uint32_t hw_h) |
| 2177 | : hw_w(hw_w), hw_h(hw_h) { |
| 2178 | } |
| 2179 | void operator()(GLfloat* vtx, float v) { |
| 2180 | const GLfloat w = hw_w + (hw_w * v); |
| 2181 | const GLfloat h = hw_h - (hw_h * v); |
| 2182 | const GLfloat x = (hw_w - w) * 0.5f; |
| 2183 | const GLfloat y = (hw_h - h) * 0.5f; |
Mathias Agopian | ffcf465 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 2184 | vtx[0] = x; vtx[1] = y; |
| 2185 | vtx[2] = x; vtx[3] = y + h; |
| 2186 | vtx[4] = x + w; vtx[5] = y + h; |
| 2187 | vtx[6] = x + w; vtx[7] = y; |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2188 | } |
| 2189 | }; |
| 2190 | |
| 2191 | class h_stretch { |
| 2192 | const GLfloat hw_w, hw_h; |
| 2193 | public: |
| 2194 | h_stretch(uint32_t hw_w, uint32_t hw_h) |
| 2195 | : hw_w(hw_w), hw_h(hw_h) { |
| 2196 | } |
| 2197 | void operator()(GLfloat* vtx, float v) { |
| 2198 | const GLfloat w = hw_w - (hw_w * v); |
| 2199 | const GLfloat h = 1.0f; |
| 2200 | const GLfloat x = (hw_w - w) * 0.5f; |
| 2201 | const GLfloat y = (hw_h - h) * 0.5f; |
Mathias Agopian | ffcf465 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 2202 | vtx[0] = x; vtx[1] = y; |
| 2203 | vtx[2] = x; vtx[3] = y + h; |
| 2204 | vtx[4] = x + w; vtx[5] = y + h; |
| 2205 | vtx[6] = x + w; vtx[7] = y; |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2206 | } |
| 2207 | }; |
| 2208 | |
Mathias Agopian | ed9807b | 2012-05-18 14:18:08 -0700 | [diff] [blame] | 2209 | VSyncWaiter vsync(mEventThread); |
| 2210 | |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2211 | // the full animation is 24 frames |
Mathias Agopian | ffcf465 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 2212 | char value[PROPERTY_VALUE_MAX]; |
| 2213 | property_get("debug.sf.electron_frames", value, "24"); |
| 2214 | int nbFrames = (atoi(value) + 1) >> 1; |
| 2215 | if (nbFrames <= 0) // just in case |
| 2216 | nbFrames = 24; |
| 2217 | |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2218 | s_curve_interpolator itr(nbFrames, 7.5f); |
| 2219 | s_curve_interpolator itg(nbFrames, 8.0f); |
| 2220 | s_curve_interpolator itb(nbFrames, 8.5f); |
| 2221 | |
| 2222 | v_stretch vverts(hw_w, hw_h); |
Mathias Agopian | a9040d0 | 2011-10-10 19:02:07 -0700 | [diff] [blame] | 2223 | |
| 2224 | glMatrixMode(GL_TEXTURE); |
| 2225 | glLoadIdentity(); |
| 2226 | glMatrixMode(GL_MODELVIEW); |
| 2227 | glLoadIdentity(); |
| 2228 | |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2229 | glEnable(GL_BLEND); |
| 2230 | glBlendFunc(GL_ONE, GL_ONE); |
| 2231 | for (int i=0 ; i<nbFrames ; i++) { |
| 2232 | float x, y, w, h; |
| 2233 | const float vr = itr(i); |
| 2234 | const float vg = itg(i); |
| 2235 | const float vb = itb(i); |
| 2236 | |
Mathias Agopian | ed9807b | 2012-05-18 14:18:08 -0700 | [diff] [blame] | 2237 | // wait for vsync |
| 2238 | vsync.wait(); |
| 2239 | |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2240 | // clear screen |
| 2241 | glColorMask(1,1,1,1); |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2242 | glClear(GL_COLOR_BUFFER_BIT); |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2243 | glEnable(GL_TEXTURE_2D); |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2244 | |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2245 | // draw the red plane |
| 2246 | vverts(vtx, vr); |
| 2247 | glColorMask(1,0,0,1); |
| 2248 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2249 | |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2250 | // draw the green plane |
| 2251 | vverts(vtx, vg); |
| 2252 | glColorMask(0,1,0,1); |
| 2253 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2254 | |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2255 | // draw the blue plane |
| 2256 | vverts(vtx, vb); |
| 2257 | glColorMask(0,0,1,1); |
| 2258 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2259 | |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2260 | // draw the white highlight (we use the last vertices) |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2261 | glDisable(GL_TEXTURE_2D); |
| 2262 | glColorMask(1,1,1,1); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2263 | glColor4f(vg, vg, vg, 1); |
| 2264 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 2265 | hw.flip(screenBounds); |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2266 | } |
| 2267 | |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2268 | h_stretch hverts(hw_w, hw_h); |
| 2269 | glDisable(GL_BLEND); |
| 2270 | glDisable(GL_TEXTURE_2D); |
| 2271 | glColorMask(1,1,1,1); |
| 2272 | for (int i=0 ; i<nbFrames ; i++) { |
| 2273 | const float v = itg(i); |
| 2274 | hverts(vtx, v); |
Mathias Agopian | ed9807b | 2012-05-18 14:18:08 -0700 | [diff] [blame] | 2275 | |
| 2276 | // wait for vsync |
| 2277 | vsync.wait(); |
| 2278 | |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2279 | glClear(GL_COLOR_BUFFER_BIT); |
| 2280 | glColor4f(1-v, 1-v, 1-v, 1); |
| 2281 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 2282 | hw.flip(screenBounds); |
| 2283 | } |
| 2284 | |
| 2285 | glColorMask(1,1,1,1); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2286 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 2287 | glDeleteTextures(1, &tname); |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2288 | glDisable(GL_TEXTURE_2D); |
Mathias Agopian | c492e67 | 2011-10-18 14:49:27 -0700 | [diff] [blame] | 2289 | glDisable(GL_BLEND); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2290 | return NO_ERROR; |
| 2291 | } |
| 2292 | |
| 2293 | status_t SurfaceFlinger::electronBeamOnAnimationImplLocked() |
| 2294 | { |
| 2295 | status_t result = PERMISSION_DENIED; |
| 2296 | |
| 2297 | if (!GLExtensions::getInstance().haveFramebufferObject()) |
| 2298 | return INVALID_OPERATION; |
| 2299 | |
| 2300 | |
| 2301 | // get screen geometry |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 2302 | const DisplayDevice& hw(getDefaultDisplayDevice()); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2303 | const uint32_t hw_w = hw.getWidth(); |
| 2304 | const uint32_t hw_h = hw.getHeight(); |
| 2305 | const Region screenBounds(hw.bounds()); |
| 2306 | |
| 2307 | GLfloat u, v; |
| 2308 | GLuint tname; |
| 2309 | result = renderScreenToTextureLocked(0, &tname, &u, &v); |
| 2310 | if (result != NO_ERROR) { |
| 2311 | return result; |
| 2312 | } |
| 2313 | |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2314 | GLfloat vtx[8]; |
| 2315 | const GLfloat texCoords[4][2] = { {0,v}, {0,0}, {u,0}, {u,v} }; |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2316 | glBindTexture(GL_TEXTURE_2D, tname); |
| 2317 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); |
| 2318 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 2319 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
Michael I. Gold | b1d1c6d | 2012-01-13 00:36:45 -0800 | [diff] [blame] | 2320 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 2321 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2322 | glTexCoordPointer(2, GL_FLOAT, 0, texCoords); |
| 2323 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 2324 | glVertexPointer(2, GL_FLOAT, 0, vtx); |
| 2325 | |
| 2326 | class s_curve_interpolator { |
| 2327 | const float nbFrames, s, v; |
| 2328 | public: |
| 2329 | s_curve_interpolator(int nbFrames, float s) |
| 2330 | : nbFrames(1.0f / (nbFrames-1)), s(s), |
| 2331 | v(1.0f + expf(-s + 0.5f*s)) { |
| 2332 | } |
| 2333 | float operator()(int f) { |
| 2334 | const float x = f * nbFrames; |
| 2335 | return ((1.0f/(1.0f + expf(-x*s + 0.5f*s))) - 0.5f) * v + 0.5f; |
| 2336 | } |
| 2337 | }; |
| 2338 | |
| 2339 | class v_stretch { |
| 2340 | const GLfloat hw_w, hw_h; |
| 2341 | public: |
| 2342 | v_stretch(uint32_t hw_w, uint32_t hw_h) |
| 2343 | : hw_w(hw_w), hw_h(hw_h) { |
| 2344 | } |
| 2345 | void operator()(GLfloat* vtx, float v) { |
| 2346 | const GLfloat w = hw_w + (hw_w * v); |
| 2347 | const GLfloat h = hw_h - (hw_h * v); |
| 2348 | const GLfloat x = (hw_w - w) * 0.5f; |
| 2349 | const GLfloat y = (hw_h - h) * 0.5f; |
| 2350 | vtx[0] = x; vtx[1] = y; |
| 2351 | vtx[2] = x; vtx[3] = y + h; |
| 2352 | vtx[4] = x + w; vtx[5] = y + h; |
| 2353 | vtx[6] = x + w; vtx[7] = y; |
| 2354 | } |
| 2355 | }; |
| 2356 | |
| 2357 | class h_stretch { |
| 2358 | const GLfloat hw_w, hw_h; |
| 2359 | public: |
| 2360 | h_stretch(uint32_t hw_w, uint32_t hw_h) |
| 2361 | : hw_w(hw_w), hw_h(hw_h) { |
| 2362 | } |
| 2363 | void operator()(GLfloat* vtx, float v) { |
| 2364 | const GLfloat w = hw_w - (hw_w * v); |
| 2365 | const GLfloat h = 1.0f; |
| 2366 | const GLfloat x = (hw_w - w) * 0.5f; |
| 2367 | const GLfloat y = (hw_h - h) * 0.5f; |
| 2368 | vtx[0] = x; vtx[1] = y; |
| 2369 | vtx[2] = x; vtx[3] = y + h; |
| 2370 | vtx[4] = x + w; vtx[5] = y + h; |
| 2371 | vtx[6] = x + w; vtx[7] = y; |
| 2372 | } |
| 2373 | }; |
| 2374 | |
Mathias Agopian | ed9807b | 2012-05-18 14:18:08 -0700 | [diff] [blame] | 2375 | VSyncWaiter vsync(mEventThread); |
| 2376 | |
Mathias Agopian | a6546e5 | 2010-10-14 12:33:07 -0700 | [diff] [blame] | 2377 | // the full animation is 12 frames |
| 2378 | int nbFrames = 8; |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2379 | s_curve_interpolator itr(nbFrames, 7.5f); |
| 2380 | s_curve_interpolator itg(nbFrames, 8.0f); |
| 2381 | s_curve_interpolator itb(nbFrames, 8.5f); |
| 2382 | |
| 2383 | h_stretch hverts(hw_w, hw_h); |
| 2384 | glDisable(GL_BLEND); |
| 2385 | glDisable(GL_TEXTURE_2D); |
| 2386 | glColorMask(1,1,1,1); |
| 2387 | for (int i=nbFrames-1 ; i>=0 ; i--) { |
| 2388 | const float v = itg(i); |
| 2389 | hverts(vtx, v); |
Mathias Agopian | ed9807b | 2012-05-18 14:18:08 -0700 | [diff] [blame] | 2390 | |
| 2391 | // wait for vsync |
| 2392 | vsync.wait(); |
| 2393 | |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2394 | glClear(GL_COLOR_BUFFER_BIT); |
| 2395 | glColor4f(1-v, 1-v, 1-v, 1); |
| 2396 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 2397 | hw.flip(screenBounds); |
| 2398 | } |
| 2399 | |
Mathias Agopian | a6546e5 | 2010-10-14 12:33:07 -0700 | [diff] [blame] | 2400 | nbFrames = 4; |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2401 | v_stretch vverts(hw_w, hw_h); |
| 2402 | glEnable(GL_BLEND); |
| 2403 | glBlendFunc(GL_ONE, GL_ONE); |
| 2404 | for (int i=nbFrames-1 ; i>=0 ; i--) { |
| 2405 | float x, y, w, h; |
| 2406 | const float vr = itr(i); |
| 2407 | const float vg = itg(i); |
| 2408 | const float vb = itb(i); |
| 2409 | |
Mathias Agopian | ed9807b | 2012-05-18 14:18:08 -0700 | [diff] [blame] | 2410 | // wait for vsync |
| 2411 | vsync.wait(); |
| 2412 | |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2413 | // clear screen |
| 2414 | glColorMask(1,1,1,1); |
| 2415 | glClear(GL_COLOR_BUFFER_BIT); |
| 2416 | glEnable(GL_TEXTURE_2D); |
| 2417 | |
| 2418 | // draw the red plane |
| 2419 | vverts(vtx, vr); |
| 2420 | glColorMask(1,0,0,1); |
| 2421 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 2422 | |
| 2423 | // draw the green plane |
| 2424 | vverts(vtx, vg); |
| 2425 | glColorMask(0,1,0,1); |
| 2426 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 2427 | |
| 2428 | // draw the blue plane |
| 2429 | vverts(vtx, vb); |
| 2430 | glColorMask(0,0,1,1); |
| 2431 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 2432 | |
| 2433 | hw.flip(screenBounds); |
| 2434 | } |
| 2435 | |
| 2436 | glColorMask(1,1,1,1); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2437 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2438 | glDeleteTextures(1, &tname); |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2439 | glDisable(GL_TEXTURE_2D); |
Mathias Agopian | c492e67 | 2011-10-18 14:49:27 -0700 | [diff] [blame] | 2440 | glDisable(GL_BLEND); |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2441 | |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2442 | return NO_ERROR; |
| 2443 | } |
| 2444 | |
| 2445 | // --------------------------------------------------------------------------- |
| 2446 | |
Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2447 | status_t SurfaceFlinger::turnElectronBeamOffImplLocked(int32_t mode) |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2448 | { |
Mathias Agopian | 22ffb11 | 2012-04-10 21:04:02 -0700 | [diff] [blame] | 2449 | ATRACE_CALL(); |
| 2450 | |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 2451 | DisplayDevice& hw(const_cast<DisplayDevice&>(getDefaultDisplayDevice())); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2452 | if (!hw.canDraw()) { |
| 2453 | // we're already off |
| 2454 | return NO_ERROR; |
| 2455 | } |
Mathias Agopian | 7ee4cd5 | 2011-09-02 12:22:39 -0700 | [diff] [blame] | 2456 | |
| 2457 | // turn off hwc while we're doing the animation |
Mathias Agopian | 8630320 | 2012-07-24 22:46:10 -0700 | [diff] [blame] | 2458 | getHwComposer().disable(); |
Mathias Agopian | 7ee4cd5 | 2011-09-02 12:22:39 -0700 | [diff] [blame] | 2459 | // and make sure to turn it back on (if needed) next time we compose |
| 2460 | invalidateHwcGeometry(); |
| 2461 | |
Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2462 | if (mode & ISurfaceComposer::eElectronBeamAnimationOff) { |
| 2463 | electronBeamOffAnimationImplLocked(); |
| 2464 | } |
| 2465 | |
| 2466 | // always clear the whole screen at the end of the animation |
| 2467 | glClearColor(0,0,0,1); |
Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2468 | glClear(GL_COLOR_BUFFER_BIT); |
Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2469 | hw.flip( Region(hw.bounds()) ); |
| 2470 | |
Mathias Agopian | 015fb3f | 2010-10-14 12:19:37 -0700 | [diff] [blame] | 2471 | return NO_ERROR; |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | status_t SurfaceFlinger::turnElectronBeamOff(int32_t mode) |
| 2475 | { |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2476 | class MessageTurnElectronBeamOff : public MessageBase { |
| 2477 | SurfaceFlinger* flinger; |
Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2478 | int32_t mode; |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2479 | status_t result; |
| 2480 | public: |
Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2481 | MessageTurnElectronBeamOff(SurfaceFlinger* flinger, int32_t mode) |
| 2482 | : flinger(flinger), mode(mode), result(PERMISSION_DENIED) { |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2483 | } |
| 2484 | status_t getResult() const { |
| 2485 | return result; |
| 2486 | } |
| 2487 | virtual bool handler() { |
| 2488 | Mutex::Autolock _l(flinger->mStateLock); |
Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2489 | result = flinger->turnElectronBeamOffImplLocked(mode); |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2490 | return true; |
| 2491 | } |
| 2492 | }; |
| 2493 | |
Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2494 | sp<MessageBase> msg = new MessageTurnElectronBeamOff(this, mode); |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2495 | status_t res = postMessageSync(msg); |
| 2496 | if (res == NO_ERROR) { |
| 2497 | res = static_cast<MessageTurnElectronBeamOff*>( msg.get() )->getResult(); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2498 | |
| 2499 | // work-around: when the power-manager calls us we activate the |
| 2500 | // animation. eventually, the "on" animation will be called |
| 2501 | // by the power-manager itself |
Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2502 | mElectronBeamAnimationMode = mode; |
Mathias Agopian | 59119e6 | 2010-10-11 12:37:43 -0700 | [diff] [blame] | 2503 | } |
| 2504 | return res; |
| 2505 | } |
| 2506 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2507 | // --------------------------------------------------------------------------- |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 2508 | |
Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2509 | status_t SurfaceFlinger::turnElectronBeamOnImplLocked(int32_t mode) |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2510 | { |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 2511 | DisplayDevice& hw(const_cast<DisplayDevice&>(getDefaultDisplayDevice())); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2512 | if (hw.canDraw()) { |
| 2513 | // we're already on |
| 2514 | return NO_ERROR; |
| 2515 | } |
Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2516 | if (mode & ISurfaceComposer::eElectronBeamAnimationOn) { |
| 2517 | electronBeamOnAnimationImplLocked(); |
| 2518 | } |
Mathias Agopian | a7f0373 | 2010-10-14 12:46:24 -0700 | [diff] [blame] | 2519 | |
| 2520 | // make sure to redraw the whole screen when the animation is done |
Mathias Agopian | 87baae1 | 2012-07-31 12:38:26 -0700 | [diff] [blame] | 2521 | hw.dirtyRegion.set(hw.bounds()); |
Mathias Agopian | 99ce5cd | 2012-01-31 18:24:27 -0800 | [diff] [blame] | 2522 | signalTransaction(); |
Mathias Agopian | a7f0373 | 2010-10-14 12:46:24 -0700 | [diff] [blame] | 2523 | |
Mathias Agopian | 015fb3f | 2010-10-14 12:19:37 -0700 | [diff] [blame] | 2524 | return NO_ERROR; |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2525 | } |
| 2526 | |
| 2527 | status_t SurfaceFlinger::turnElectronBeamOn(int32_t mode) |
| 2528 | { |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2529 | class MessageTurnElectronBeamOn : public MessageBase { |
| 2530 | SurfaceFlinger* flinger; |
Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2531 | int32_t mode; |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2532 | status_t result; |
| 2533 | public: |
Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2534 | MessageTurnElectronBeamOn(SurfaceFlinger* flinger, int32_t mode) |
| 2535 | : flinger(flinger), mode(mode), result(PERMISSION_DENIED) { |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2536 | } |
| 2537 | status_t getResult() const { |
| 2538 | return result; |
| 2539 | } |
| 2540 | virtual bool handler() { |
| 2541 | Mutex::Autolock _l(flinger->mStateLock); |
Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2542 | result = flinger->turnElectronBeamOnImplLocked(mode); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2543 | return true; |
| 2544 | } |
| 2545 | }; |
| 2546 | |
Mathias Agopian | abd671a | 2010-10-14 14:54:06 -0700 | [diff] [blame] | 2547 | postMessageAsync( new MessageTurnElectronBeamOn(this, mode) ); |
Mathias Agopian | 9daa5c9 | 2010-10-12 16:05:48 -0700 | [diff] [blame] | 2548 | return NO_ERROR; |
| 2549 | } |
| 2550 | |
| 2551 | // --------------------------------------------------------------------------- |
| 2552 | |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2553 | status_t SurfaceFlinger::captureScreenImplLocked(DisplayID dpy, |
| 2554 | sp<IMemoryHeap>* heap, |
| 2555 | uint32_t* w, uint32_t* h, PixelFormat* f, |
Mathias Agopian | bf2c6a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2556 | uint32_t sw, uint32_t sh, |
| 2557 | uint32_t minLayerZ, uint32_t maxLayerZ) |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2558 | { |
Mathias Agopian | fddc28d | 2012-03-12 15:18:42 -0400 | [diff] [blame] | 2559 | ATRACE_CALL(); |
| 2560 | |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2561 | status_t result = PERMISSION_DENIED; |
| 2562 | |
| 2563 | // only one display supported for now |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 2564 | if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) { |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2565 | return BAD_VALUE; |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 2566 | } |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2567 | |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 2568 | if (!GLExtensions::getInstance().haveFramebufferObject()) { |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2569 | return INVALID_OPERATION; |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 2570 | } |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2571 | |
| 2572 | // get screen geometry |
Mathias Agopian | 0f2f5ff | 2012-07-31 23:09:07 -0700 | [diff] [blame] | 2573 | const DisplayDevice& hw(getDisplayDevice(dpy)); |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2574 | const uint32_t hw_w = hw.getWidth(); |
| 2575 | const uint32_t hw_h = hw.getHeight(); |
| 2576 | |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 2577 | // if we have secure windows on this display, never allow the screen capture |
| 2578 | if (hw.getSecureLayerVisible()) { |
| 2579 | return PERMISSION_DENIED; |
| 2580 | } |
| 2581 | |
| 2582 | if ((sw > hw_w) || (sh > hw_h)) { |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2583 | return BAD_VALUE; |
Mathias Agopian | 3b1d2b6 | 2012-07-11 13:48:17 -0700 | [diff] [blame] | 2584 | } |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2585 | |
| 2586 | sw = (!sw) ? hw_w : sw; |
| 2587 | sh = (!sh) ? hw_h : sh; |
| 2588 | const size_t size = sw * sh * 4; |
Mathias Agopian | fcb239d | 2012-08-02 16:01:34 -0700 | [diff] [blame] | 2589 | const bool filtering = sw != hw_w || sh != hw_h; |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2590 | |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 2591 | //ALOGD("screenshot: sw=%d, sh=%d, minZ=%d, maxZ=%d", |
Mathias Agopian | 1c71a47 | 2011-03-02 18:45:50 -0800 | [diff] [blame] | 2592 | // sw, sh, minLayerZ, maxLayerZ); |
Mathias Agopian | c1d1b0d | 2011-01-16 14:05:02 -0800 | [diff] [blame] | 2593 | |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2594 | // make sure to clear all GL error flags |
| 2595 | while ( glGetError() != GL_NO_ERROR ) ; |
| 2596 | |
| 2597 | // create a FBO |
| 2598 | GLuint name, tname; |
| 2599 | glGenRenderbuffersOES(1, &tname); |
| 2600 | glBindRenderbufferOES(GL_RENDERBUFFER_OES, tname); |
| 2601 | glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGBA8_OES, sw, sh); |
Mathias Agopian | fddc28d | 2012-03-12 15:18:42 -0400 | [diff] [blame] | 2602 | |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2603 | glGenFramebuffersOES(1, &name); |
| 2604 | glBindFramebufferOES(GL_FRAMEBUFFER_OES, name); |
| 2605 | glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, |
| 2606 | GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, tname); |
| 2607 | |
| 2608 | GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES); |
Mathias Agopian | c1d1b0d | 2011-01-16 14:05:02 -0800 | [diff] [blame] | 2609 | |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2610 | if (status == GL_FRAMEBUFFER_COMPLETE_OES) { |
| 2611 | |
| 2612 | // invert everything, b/c glReadPixel() below will invert the FB |
| 2613 | glViewport(0, 0, sw, sh); |
| 2614 | glMatrixMode(GL_PROJECTION); |
| 2615 | glPushMatrix(); |
| 2616 | glLoadIdentity(); |
Mathias Agopian | ffcf465 | 2011-07-07 17:30:31 -0700 | [diff] [blame] | 2617 | glOrthof(0, hw_w, hw_h, 0, 0, 1); |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2618 | glMatrixMode(GL_MODELVIEW); |
| 2619 | |
| 2620 | // redraw the screen entirely... |
| 2621 | glClearColor(0,0,0,1); |
| 2622 | glClear(GL_COLOR_BUFFER_BIT); |
Mathias Agopian | f653b89 | 2010-12-16 18:46:17 -0800 | [diff] [blame] | 2623 | |
Jamie Gennis | 9575f60 | 2011-10-07 14:51:16 -0700 | [diff] [blame] | 2624 | const LayerVector& layers(mDrawingState.layersSortedByZ); |
| 2625 | const size_t count = layers.size(); |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2626 | for (size_t i=0 ; i<count ; ++i) { |
| 2627 | const sp<LayerBase>& layer(layers[i]); |
Mathias Agopian | b061033 | 2011-08-25 14:36:43 -0700 | [diff] [blame] | 2628 | const uint32_t flags = layer->drawingState().flags; |
| 2629 | if (!(flags & ISurfaceComposer::eLayerHidden)) { |
| 2630 | const uint32_t z = layer->drawingState().z; |
| 2631 | if (z >= minLayerZ && z <= maxLayerZ) { |
Mathias Agopian | fcb239d | 2012-08-02 16:01:34 -0700 | [diff] [blame] | 2632 | if (filtering) layer->setFiltering(true); |
| 2633 | layer->draw(hw); |
| 2634 | if (filtering) layer->setFiltering(false); |
Mathias Agopian | b061033 | 2011-08-25 14:36:43 -0700 | [diff] [blame] | 2635 | } |
Mathias Agopian | bf2c6a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2636 | } |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2637 | } |
| 2638 | |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2639 | // check for errors and return screen capture |
| 2640 | if (glGetError() != GL_NO_ERROR) { |
| 2641 | // error while rendering |
| 2642 | result = INVALID_OPERATION; |
| 2643 | } else { |
| 2644 | // allocate shared memory large enough to hold the |
| 2645 | // screen capture |
| 2646 | sp<MemoryHeapBase> base( |
| 2647 | new MemoryHeapBase(size, 0, "screen-capture") ); |
| 2648 | void* const ptr = base->getBase(); |
| 2649 | if (ptr) { |
| 2650 | // capture the screen with glReadPixels() |
Mathias Agopian | fddc28d | 2012-03-12 15:18:42 -0400 | [diff] [blame] | 2651 | ScopedTrace _t(ATRACE_TAG, "glReadPixels"); |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2652 | glReadPixels(0, 0, sw, sh, GL_RGBA, GL_UNSIGNED_BYTE, ptr); |
| 2653 | if (glGetError() == GL_NO_ERROR) { |
| 2654 | *heap = base; |
| 2655 | *w = sw; |
| 2656 | *h = sh; |
| 2657 | *f = PIXEL_FORMAT_RGBA_8888; |
| 2658 | result = NO_ERROR; |
| 2659 | } |
| 2660 | } else { |
| 2661 | result = NO_MEMORY; |
| 2662 | } |
| 2663 | } |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2664 | glViewport(0, 0, hw_w, hw_h); |
| 2665 | glMatrixMode(GL_PROJECTION); |
| 2666 | glPopMatrix(); |
| 2667 | glMatrixMode(GL_MODELVIEW); |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2668 | } else { |
| 2669 | result = BAD_VALUE; |
| 2670 | } |
| 2671 | |
| 2672 | // release FBO resources |
| 2673 | glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0); |
| 2674 | glDeleteRenderbuffersOES(1, &tname); |
| 2675 | glDeleteFramebuffersOES(1, &name); |
Mathias Agopian | e6f0984 | 2010-12-15 14:41:59 -0800 | [diff] [blame] | 2676 | |
| 2677 | hw.compositionComplete(); |
| 2678 | |
Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 2679 | // ALOGD("screenshot: result = %s", result<0 ? strerror(result) : "OK"); |
Mathias Agopian | c1d1b0d | 2011-01-16 14:05:02 -0800 | [diff] [blame] | 2680 | |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2681 | return result; |
| 2682 | } |
| 2683 | |
| 2684 | |
Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2685 | status_t SurfaceFlinger::captureScreen(DisplayID dpy, |
| 2686 | sp<IMemoryHeap>* heap, |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2687 | uint32_t* width, uint32_t* height, PixelFormat* format, |
Mathias Agopian | bf2c6a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2688 | uint32_t sw, uint32_t sh, |
| 2689 | uint32_t minLayerZ, uint32_t maxLayerZ) |
Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2690 | { |
| 2691 | // only one display supported for now |
Glenn Kasten | 99ed224 | 2011-12-15 09:51:17 -0800 | [diff] [blame] | 2692 | if (CC_UNLIKELY(uint32_t(dpy) >= DISPLAY_COUNT)) |
Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2693 | return BAD_VALUE; |
| 2694 | |
| 2695 | if (!GLExtensions::getInstance().haveFramebufferObject()) |
| 2696 | return INVALID_OPERATION; |
| 2697 | |
| 2698 | class MessageCaptureScreen : public MessageBase { |
| 2699 | SurfaceFlinger* flinger; |
| 2700 | DisplayID dpy; |
| 2701 | sp<IMemoryHeap>* heap; |
| 2702 | uint32_t* w; |
| 2703 | uint32_t* h; |
| 2704 | PixelFormat* f; |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2705 | uint32_t sw; |
| 2706 | uint32_t sh; |
Mathias Agopian | bf2c6a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2707 | uint32_t minLayerZ; |
| 2708 | uint32_t maxLayerZ; |
Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2709 | status_t result; |
| 2710 | public: |
| 2711 | MessageCaptureScreen(SurfaceFlinger* flinger, DisplayID dpy, |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2712 | sp<IMemoryHeap>* heap, uint32_t* w, uint32_t* h, PixelFormat* f, |
Mathias Agopian | bf2c6a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2713 | uint32_t sw, uint32_t sh, |
| 2714 | uint32_t minLayerZ, uint32_t maxLayerZ) |
Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2715 | : flinger(flinger), dpy(dpy), |
Mathias Agopian | bf2c6a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2716 | heap(heap), w(w), h(h), f(f), sw(sw), sh(sh), |
| 2717 | minLayerZ(minLayerZ), maxLayerZ(maxLayerZ), |
| 2718 | result(PERMISSION_DENIED) |
Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2719 | { |
| 2720 | } |
| 2721 | status_t getResult() const { |
| 2722 | return result; |
| 2723 | } |
| 2724 | virtual bool handler() { |
| 2725 | Mutex::Autolock _l(flinger->mStateLock); |
Mathias Agopian | 74c40c0 | 2010-09-29 13:02:36 -0700 | [diff] [blame] | 2726 | result = flinger->captureScreenImplLocked(dpy, |
Mathias Agopian | bf2c6a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2727 | heap, w, h, f, sw, sh, minLayerZ, maxLayerZ); |
Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2728 | return true; |
| 2729 | } |
| 2730 | }; |
| 2731 | |
| 2732 | sp<MessageBase> msg = new MessageCaptureScreen(this, |
Mathias Agopian | bf2c6a6 | 2010-12-10 16:22:31 -0800 | [diff] [blame] | 2733 | dpy, heap, width, height, format, sw, sh, minLayerZ, maxLayerZ); |
Mathias Agopian | 1b0b30d | 2010-09-24 11:26:58 -0700 | [diff] [blame] | 2734 | status_t res = postMessageSync(msg); |
| 2735 | if (res == NO_ERROR) { |
| 2736 | res = static_cast<MessageCaptureScreen*>( msg.get() )->getResult(); |
| 2737 | } |
| 2738 | return res; |
| 2739 | } |
| 2740 | |
| 2741 | // --------------------------------------------------------------------------- |
| 2742 | |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 2743 | SurfaceFlinger::LayerVector::LayerVector() { |
| 2744 | } |
| 2745 | |
| 2746 | SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs) |
| 2747 | : SortedVector<sp<LayerBase> >(rhs) { |
| 2748 | } |
| 2749 | |
| 2750 | int SurfaceFlinger::LayerVector::do_compare(const void* lhs, |
| 2751 | const void* rhs) const |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 2752 | { |
Mathias Agopian | be246f8 | 2012-07-31 22:59:38 -0700 | [diff] [blame] | 2753 | // sort layers per layer-stack, then by z-order and finally by sequence |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 2754 | const sp<LayerBase>& l(*reinterpret_cast<const sp<LayerBase>*>(lhs)); |
| 2755 | const sp<LayerBase>& r(*reinterpret_cast<const sp<LayerBase>*>(rhs)); |
Mathias Agopian | be246f8 | 2012-07-31 22:59:38 -0700 | [diff] [blame] | 2756 | |
| 2757 | uint32_t ls = l->currentState().layerStack; |
| 2758 | uint32_t rs = r->currentState().layerStack; |
| 2759 | if (ls != rs) |
| 2760 | return ls - rs; |
| 2761 | |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 2762 | uint32_t lz = l->currentState().z; |
| 2763 | uint32_t rz = r->currentState().z; |
Mathias Agopian | be246f8 | 2012-07-31 22:59:38 -0700 | [diff] [blame] | 2764 | if (lz != rz) |
| 2765 | return lz - rz; |
| 2766 | |
| 2767 | return l->sequence - r->sequence; |
Mathias Agopian | 921e6ac | 2012-07-23 23:11:29 -0700 | [diff] [blame] | 2768 | } |
| 2769 | |
| 2770 | // --------------------------------------------------------------------------- |
| 2771 | |
Mathias Agopian | 92a979a | 2012-08-02 18:32:23 -0700 | [diff] [blame^] | 2772 | SurfaceFlinger::DisplayDeviceState::DisplayDeviceState() |
| 2773 | : id(DisplayDevice::DISPLAY_ID_MAIN), layerStack(0), orientation(0) { |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 2774 | } |
| 2775 | |
| 2776 | // --------------------------------------------------------------------------- |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2777 | |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 2778 | GraphicBufferAlloc::GraphicBufferAlloc() {} |
| 2779 | |
| 2780 | GraphicBufferAlloc::~GraphicBufferAlloc() {} |
| 2781 | |
| 2782 | sp<GraphicBuffer> GraphicBufferAlloc::createGraphicBuffer(uint32_t w, uint32_t h, |
Mathias Agopian | d9e8c64 | 2011-07-01 14:53:49 -0700 | [diff] [blame] | 2783 | PixelFormat format, uint32_t usage, status_t* error) { |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 2784 | sp<GraphicBuffer> graphicBuffer(new GraphicBuffer(w, h, format, usage)); |
| 2785 | status_t err = graphicBuffer->initCheck(); |
Mathias Agopian | d9e8c64 | 2011-07-01 14:53:49 -0700 | [diff] [blame] | 2786 | *error = err; |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2787 | if (err != 0 || graphicBuffer->handle == 0) { |
Mathias Agopian | d9e8c64 | 2011-07-01 14:53:49 -0700 | [diff] [blame] | 2788 | if (err == NO_MEMORY) { |
| 2789 | GraphicBuffer::dumpAllocationsToSystemLog(); |
| 2790 | } |
Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 2791 | ALOGE("GraphicBufferAlloc::createGraphicBuffer(w=%d, h=%d) " |
Mathias Agopian | a67932f | 2011-04-20 14:20:59 -0700 | [diff] [blame] | 2792 | "failed (%s), handle=%p", |
| 2793 | w, h, strerror(-err), graphicBuffer->handle); |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 2794 | return 0; |
| 2795 | } |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 2796 | return graphicBuffer; |
| 2797 | } |
| 2798 | |
Jamie Gennis | 9a78c90 | 2011-01-12 18:30:40 -0800 | [diff] [blame] | 2799 | // --------------------------------------------------------------------------- |
| 2800 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2801 | }; // namespace android |