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