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