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