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