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