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