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