blob: 6798a87ad98a8dfac6ac4843a297b4bf5af2a7f9 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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 Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Greg Hackmann86efcc02014-03-07 12:44:02 -080024#include <inttypes.h>
Jesse Hallb154c422014-07-13 12:47:02 -070025#include <stdatomic.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070026
27#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036
Mathias Agopianc666cae2012-07-25 18:56:13 -070037#include <ui/DisplayInfo.h>
Lajos Molnar67d8bd62014-09-11 14:58:45 -070038#include <ui/DisplayStatInfo.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070039
Mathias Agopian921e6ac2012-07-23 23:11:29 -070040#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070041#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070042#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070043#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080044#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080045#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070046
47#include <ui/GraphicBufferAllocator.h>
48#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070049#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080050
Mathias Agopiancde87a32012-09-13 14:09:01 -070051#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include <utils/String8.h>
53#include <utils/String16.h>
54#include <utils/StopWatch.h>
Yusuke Sato0a688f52015-07-30 23:05:39 -070055#include <utils/Timers.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080056#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
Mathias Agopian921e6ac2012-07-23 23:11:29 -070058#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070059#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
Mathias Agopian3e25fd82013-04-22 17:52:16 +020061#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020063#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080064#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070065#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070066#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070067#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080068#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072
Mathias Agopiana4912602012-07-12 14:25:33 -070073#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070074#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070075#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076
Mathias Agopianff2ed702013-09-01 21:36:12 -070077#include "Effects/Daltonizer.h"
78
Mathias Agopian875d8e12013-06-07 15:35:48 -070079#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070080#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070081
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082#define DISPLAY_COUNT 1
83
Mathias Agopianfee2b462013-07-03 12:34:01 -070084/*
85 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
86 * black pixels.
87 */
88#define DEBUG_SCREENSHOTS false
89
Mathias Agopianca088332013-03-28 17:44:13 -070090EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
91
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070093
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070094// This is the phase offset in nanoseconds of the software vsync event
95// relative to the vsync event reported by HWComposer. The software vsync
96// event is when SurfaceFlinger and Choreographer-based applications run each
97// frame.
98//
99// This phase offset allows adjustment of the minimum latency from application
100// wake-up (by Choregographer) time to the time at which the resulting window
101// image is displayed. This value may be either positive (after the HW vsync)
102// or negative (before the HW vsync). Setting it to 0 will result in a
103// minimum latency of two vsync periods because the app and SurfaceFlinger
104// will run just after the HW vsync. Setting it to a positive number will
105// result in the minimum latency being:
106//
107// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
108//
109// Note that reducing this latency makes it more likely for the applications
110// to not have their window content image ready in time. When this happens
111// the latency will end up being an additional vsync period, and animations
112// will hiccup. Therefore, this latency should be tuned somewhat
113// conservatively (or at least with awareness of the trade-off being made).
114static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
115
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700116// This is the phase offset at which SurfaceFlinger's composition runs.
117static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
118
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119// ---------------------------------------------------------------------------
120
Mathias Agopian99b49842011-06-27 16:05:52 -0700121const String16 sHardwareTest("android.permission.HARDWARE_TEST");
122const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
123const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
124const String16 sDump("android.permission.DUMP");
125
126// ---------------------------------------------------------------------------
127
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700129 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700131 mTransactionPending(false),
132 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700133 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700134 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700135 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700138 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800139 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700141 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700142 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700143 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700144 mDebugInSwapBuffers(0),
145 mLastSwapBufferTime(0),
146 mDebugInTransaction(0),
147 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700148 mBootFinished(false),
Dan Stozaee44edd2015-03-23 15:50:23 -0700149 mForceFullDamage(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700150 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700151 mHWVsyncAvailable(false),
Alan Viverette9c5a3332013-09-12 20:04:35 -0700152 mDaltonize(false),
Dan Stozab90cf072015-03-05 11:05:59 -0800153 mHasColorMatrix(false),
154 mHasPoweredOff(false),
155 mFrameBuckets(),
156 mTotalTime(0),
157 mLastSwapTime(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158{
Steve Blocka19954a2012-01-04 20:05:49 +0000159 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160
161 // debugging stuff...
162 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700163
Mathias Agopianb4b17302013-03-20 18:36:41 -0700164 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700165 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700166
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 property_get("debug.sf.showupdates", value, "0");
168 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700169
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700170 property_get("debug.sf.ddms", value, "0");
171 mDebugDDMS = atoi(value);
172 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700173 if (!startDdmConnection()) {
174 // start failed, and DDMS debugging not enabled
175 mDebugDDMS = 0;
176 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700177 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700178 ALOGI_IF(mDebugRegion, "showupdates enabled");
179 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180}
181
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800182void SurfaceFlinger::onFirstRef()
183{
184 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800185}
186
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187SurfaceFlinger::~SurfaceFlinger()
188{
Mathias Agopiana4912602012-07-12 14:25:33 -0700189 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
190 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
191 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192}
193
Dan Stozac7014012014-02-14 15:03:43 -0800194void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800195{
196 // the window manager died on us. prepare its eulogy.
197
Andy McFadden13a082e2012-08-24 10:16:42 -0700198 // restore initial conditions (default device unblank, etc)
199 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800200
201 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700202 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800203}
204
Mathias Agopian7e27f052010-05-28 14:22:23 -0700205sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206{
Mathias Agopian96f08192010-06-02 23:28:45 -0700207 sp<ISurfaceComposerClient> bclient;
208 sp<Client> client(new Client(this));
209 status_t err = client->initCheck();
210 if (err == NO_ERROR) {
211 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 return bclient;
214}
215
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700216sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
217 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700218{
219 class DisplayToken : public BBinder {
220 sp<SurfaceFlinger> flinger;
221 virtual ~DisplayToken() {
222 // no more references, this display must be terminated
223 Mutex::Autolock _l(flinger->mStateLock);
224 flinger->mCurrentState.displays.removeItem(this);
225 flinger->setTransactionFlags(eDisplayTransactionNeeded);
226 }
227 public:
228 DisplayToken(const sp<SurfaceFlinger>& flinger)
229 : flinger(flinger) {
230 }
231 };
232
233 sp<BBinder> token = new DisplayToken(this);
234
235 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700236 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700237 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700238 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700239 mCurrentState.displays.add(token, info);
240
241 return token;
242}
243
Jesse Hall6c913be2013-08-08 12:15:49 -0700244void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
245 Mutex::Autolock _l(mStateLock);
246
247 ssize_t idx = mCurrentState.displays.indexOfKey(display);
248 if (idx < 0) {
249 ALOGW("destroyDisplay: invalid display token");
250 return;
251 }
252
253 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
254 if (!info.isVirtualDisplay()) {
255 ALOGE("destroyDisplay called for non-virtual display");
256 return;
257 }
258
259 mCurrentState.displays.removeItemsAt(idx);
260 setTransactionFlags(eDisplayTransactionNeeded);
261}
262
Jesse Hall692c7232012-11-08 15:41:56 -0800263void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
264 ALOGW_IF(mBuiltinDisplays[type],
265 "Overwriting display token for display type %d", type);
266 mBuiltinDisplays[type] = new BBinder();
267 DisplayDeviceState info(type);
268 // All non-virtual displays are currently considered secure.
269 info.isSecure = true;
270 mCurrentState.displays.add(mBuiltinDisplays[type], info);
271}
272
Mathias Agopiane57f2922012-08-09 16:29:12 -0700273sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700274 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700275 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
276 return NULL;
277 }
Jesse Hall692c7232012-11-08 15:41:56 -0800278 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700279}
280
Jamie Gennis9a78c902011-01-12 18:30:40 -0800281sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
282{
283 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
284 return gba;
285}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700286
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287void SurfaceFlinger::bootFinished()
288{
289 const nsecs_t now = systemTime();
290 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000291 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700292 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700293
294 // wait patiently for the window manager death
295 const String16 name("window");
296 sp<IBinder> window(defaultServiceManager()->getService(name));
297 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700298 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700299 }
300
301 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700302 // formerly we would just kill the process, but we now ask it to exit so it
303 // can choose where to stop the animation.
304 property_set("service.bootanim.exit", "1");
Yusuke Sato0a688f52015-07-30 23:05:39 -0700305
306 const int LOGTAG_SF_STOP_BOOTANIM = 60110;
307 LOG_EVENT_LONG(LOGTAG_SF_STOP_BOOTANIM,
308 ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800309}
310
Mathias Agopian3f844832013-08-07 21:24:32 -0700311void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700312 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700313 RenderEngine& engine;
314 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700315 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700316 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
317 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700318 }
319 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700320 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700321 return true;
322 }
323 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700324 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700325}
326
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700327class DispSyncSource : public VSyncSource, private DispSync::Callback {
328public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700329 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
330 const char* label) :
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700331 mValue(0),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700332 mTraceVsync(traceVsync),
Andy McFadden5167ec62014-05-22 13:08:43 -0700333 mVsyncOnLabel(String8::format("VsyncOn-%s", label)),
334 mVsyncEventLabel(String8::format("VSYNC-%s", label)),
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700335 mDispSync(dispSync),
336 mCallbackMutex(),
337 mCallback(),
338 mVsyncMutex(),
339 mPhaseOffset(phaseOffset),
340 mEnabled(false) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700341
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700342 virtual ~DispSyncSource() {}
343
344 virtual void setVSyncEnabled(bool enable) {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700345 Mutex::Autolock lock(mVsyncMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700346 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700347 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700348 static_cast<DispSync::Callback*>(this));
349 if (err != NO_ERROR) {
350 ALOGE("error registering vsync callback: %s (%d)",
351 strerror(-err), err);
352 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700353 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700354 } else {
355 status_t err = mDispSync->removeEventListener(
356 static_cast<DispSync::Callback*>(this));
357 if (err != NO_ERROR) {
358 ALOGE("error unregistering vsync callback: %s (%d)",
359 strerror(-err), err);
360 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700361 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700362 }
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700363 mEnabled = enable;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700364 }
365
366 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700367 Mutex::Autolock lock(mCallbackMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700368 mCallback = callback;
369 }
370
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700371 virtual void setPhaseOffset(nsecs_t phaseOffset) {
372 Mutex::Autolock lock(mVsyncMutex);
373
374 // Normalize phaseOffset to [0, period)
375 auto period = mDispSync->getPeriod();
376 phaseOffset %= period;
377 if (phaseOffset < 0) {
378 // If we're here, then phaseOffset is in (-period, 0). After this
379 // operation, it will be in (0, period)
380 phaseOffset += period;
381 }
382 mPhaseOffset = phaseOffset;
383
384 // If we're not enabled, we don't need to mess with the listeners
385 if (!mEnabled) {
386 return;
387 }
388
389 // Remove the listener with the old offset
390 status_t err = mDispSync->removeEventListener(
391 static_cast<DispSync::Callback*>(this));
392 if (err != NO_ERROR) {
393 ALOGE("error unregistering vsync callback: %s (%d)",
394 strerror(-err), err);
395 }
396
397 // Add a listener with the new offset
398 err = mDispSync->addEventListener(mPhaseOffset,
399 static_cast<DispSync::Callback*>(this));
400 if (err != NO_ERROR) {
401 ALOGE("error registering vsync callback: %s (%d)",
402 strerror(-err), err);
403 }
404 }
405
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700406private:
407 virtual void onDispSyncEvent(nsecs_t when) {
408 sp<VSyncSource::Callback> callback;
409 {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700410 Mutex::Autolock lock(mCallbackMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700411 callback = mCallback;
412
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700413 if (mTraceVsync) {
414 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700415 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700416 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700417 }
418
419 if (callback != NULL) {
420 callback->onVSyncEvent(when);
421 }
422 }
423
424 int mValue;
425
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700426 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700427 const String8 mVsyncOnLabel;
428 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700429
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700430 DispSync* mDispSync;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700431
432 Mutex mCallbackMutex; // Protects the following
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700433 sp<VSyncSource::Callback> mCallback;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700434
435 Mutex mVsyncMutex; // Protects the following
436 nsecs_t mPhaseOffset;
437 bool mEnabled;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700438};
439
440void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700441 ALOGI( "SurfaceFlinger's main thread ready to run. "
442 "Initializing graphics H/W...");
443
Jesse Hall692c7232012-11-08 15:41:56 -0800444 Mutex::Autolock _l(mStateLock);
445
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700446 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700447 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
448 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700449
Saurabh Shahf9481052015-06-15 17:55:51 -0700450 // start the EventThread
451 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
452 vsyncPhaseOffsetNs, true, "app");
453 mEventThread = new EventThread(vsyncSrc);
454 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
455 sfVsyncPhaseOffsetNs, true, "sf");
456 mSFEventThread = new EventThread(sfVsyncSrc);
457 mEventQueue.setEventThread(mSFEventThread);
458
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700459 // Initialize the H/W composer object. There may or may not be an
460 // actual hardware composer underneath.
461 mHwc = new HWComposer(this,
462 *static_cast<HWComposer::EventHandler *>(this));
463
Mathias Agopian875d8e12013-06-07 15:35:48 -0700464 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800465 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700466
467 // retrieve the EGL context that was selected/created
468 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700469
Mathias Agopianda27af92012-09-13 18:17:13 -0700470 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
471 "couldn't create EGLContext");
472
Mathias Agopiancde87a32012-09-13 14:09:01 -0700473 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700474 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700475 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700476 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700477 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700478 // All non-virtual displays are currently considered secure.
479 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800480 createBuiltinDisplayLocked(type);
481 wp<IBinder> token = mBuiltinDisplays[i];
482
Dan Stozab9b08832014-03-13 11:55:57 -0700483 sp<IGraphicBufferProducer> producer;
484 sp<IGraphicBufferConsumer> consumer;
485 BufferQueue::createBufferQueue(&producer, &consumer,
486 new GraphicBufferAlloc());
487
488 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
489 consumer);
Jesse Hall19e87292013-12-23 21:02:15 -0800490 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700491 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800492 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Dan Stozab9b08832014-03-13 11:55:57 -0700493 fbs, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -0800494 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700495 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700496 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700497 // for displays other than the main display, so we always
498 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800499 ALOGD("marking display %zu as acquired/unblanked", i);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700500 hw->setPowerMode(HWC_POWER_MODE_NORMAL);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700501 }
502 mDisplays.add(token, hw);
503 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700504 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700505
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700506 // make the GLContext current so that we can create textures when creating Layers
507 // (which may happens before we render something)
508 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
509
Jamie Gennisd1700752013-10-14 12:22:52 -0700510 mEventControlThread = new EventControlThread(this);
511 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
512
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700513 // set a fake vsync period if there is no HWComposer
514 if (mHwc->initCheck() != NO_ERROR) {
515 mPrimaryDispSync.setPeriod(16666667);
516 }
517
Mathias Agopian92a979a2012-08-02 18:32:23 -0700518 // initialize our drawing state
519 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700520
Andy McFadden13a082e2012-08-24 10:16:42 -0700521 // set initial conditions (e.g. unblank default device)
522 initializeDisplays();
523
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700524 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700525 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800526}
527
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700528int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700529 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700530 type : mHwc->allocateDisplayId();
531}
532
Mathias Agopiana67e4182012-06-19 17:26:12 -0700533void SurfaceFlinger::startBootAnim() {
534 // start boot animation
535 property_set("service.bootanim.exit", "0");
536 property_set("ctl.start", "bootanim");
537}
538
Mathias Agopian875d8e12013-06-07 15:35:48 -0700539size_t SurfaceFlinger::getMaxTextureSize() const {
540 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700541}
542
Mathias Agopian875d8e12013-06-07 15:35:48 -0700543size_t SurfaceFlinger::getMaxViewportDims() const {
544 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700545}
546
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800548
Jamie Gennis582270d2011-08-17 18:19:00 -0700549bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800550 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800551 Mutex::Autolock _l(mStateLock);
Marco Nelissen097ca272014-11-14 08:01:01 -0800552 sp<IBinder> surfaceTextureBinder(IInterface::asBinder(bufferProducer));
Mathias Agopian67106042013-03-14 19:18:13 -0700553 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800554}
555
Dan Stoza7f7da322014-05-02 15:26:25 -0700556status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
557 Vector<DisplayInfo>* configs) {
Tatenda Chipeperekwa23e16bb2014-10-29 16:47:19 -0700558 if ((configs == NULL) || (display.get() == NULL)) {
Dan Stoza7f7da322014-05-02 15:26:25 -0700559 return BAD_VALUE;
560 }
561
Naseer Ahmed7aa0c472014-11-03 14:49:23 -0500562 if (!display.get())
563 return NAME_NOT_FOUND;
564
Jesse Hall692c7232012-11-08 15:41:56 -0800565 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700566 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800567 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700568 type = i;
569 break;
570 }
571 }
572
573 if (type < 0) {
574 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700575 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700576
Mathias Agopian8b736f12012-08-13 17:54:26 -0700577 // TODO: Not sure if display density should handled by SF any longer
578 class Density {
579 static int getDensityFromProperty(char const* propName) {
580 char property[PROPERTY_VALUE_MAX];
581 int density = 0;
582 if (property_get(propName, property, NULL) > 0) {
583 density = atoi(property);
584 }
585 return density;
586 }
587 public:
588 static int getEmuDensity() {
589 return getDensityFromProperty("qemu.sf.lcd_density"); }
590 static int getBuildDensity() {
591 return getDensityFromProperty("ro.sf.lcd_density"); }
592 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700593
Dan Stoza7f7da322014-05-02 15:26:25 -0700594 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700595
Dan Stoza7f7da322014-05-02 15:26:25 -0700596 const Vector<HWComposer::DisplayConfig>& hwConfigs =
597 getHwComposer().getConfigs(type);
598 for (size_t c = 0; c < hwConfigs.size(); ++c) {
599 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
600 DisplayInfo info = DisplayInfo();
601
602 float xdpi = hwConfig.xdpi;
603 float ydpi = hwConfig.ydpi;
604
605 if (type == DisplayDevice::DISPLAY_PRIMARY) {
606 // The density of the device is provided by a build property
607 float density = Density::getBuildDensity() / 160.0f;
608 if (density == 0) {
609 // the build doesn't provide a density -- this is wrong!
610 // use xdpi instead
611 ALOGE("ro.sf.lcd_density must be defined as a build property");
612 density = xdpi / 160.0f;
613 }
614 if (Density::getEmuDensity()) {
615 // if "qemu.sf.lcd_density" is specified, it overrides everything
616 xdpi = ydpi = density = Density::getEmuDensity();
617 density /= 160.0f;
618 }
619 info.density = density;
620
621 // TODO: this needs to go away (currently needed only by webkit)
622 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
623 info.orientation = hw->getOrientation();
624 } else {
625 // TODO: where should this value come from?
626 static const int TV_DENSITY = 213;
627 info.density = TV_DENSITY / 160.0f;
628 info.orientation = 0;
629 }
630
631 info.w = hwConfig.width;
632 info.h = hwConfig.height;
633 info.xdpi = xdpi;
634 info.ydpi = ydpi;
635 info.fps = float(1e9 / hwConfig.refresh);
Andy McFadden91b2ca82014-06-13 14:04:23 -0700636 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
637
638 // This is how far in advance a buffer must be queued for
639 // presentation at a given time. If you want a buffer to appear
640 // on the screen at time N, you must submit the buffer before
641 // (N - presentationDeadline).
642 //
643 // Normally it's one full refresh period (to give SF a chance to
644 // latch the buffer), but this can be reduced by configuring a
645 // DispSync offset. Any additional delays introduced by the hardware
646 // composer or panel must be accounted for here.
647 //
648 // We add an additional 1ms to allow for processing time and
649 // differences between the ideal and actual refresh rate.
650 info.presentationDeadline =
651 hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700652
653 // All non-virtual displays are currently considered secure.
654 info.secure = true;
655
656 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700657 }
658
Dan Stoza7f7da322014-05-02 15:26:25 -0700659 return NO_ERROR;
660}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700661
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800662status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& /* display */,
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700663 DisplayStatInfo* stats) {
664 if (stats == NULL) {
665 return BAD_VALUE;
666 }
667
668 // FIXME for now we always return stats for the primary display
669 memset(stats, 0, sizeof(*stats));
670 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
671 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
672 return NO_ERROR;
673}
674
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700675int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
Dan Stoza24a42e92015-03-09 10:04:11 -0700676 sp<DisplayDevice> device(getDisplayDevice(display));
677 if (device != NULL) {
678 return device->getActiveConfig();
679 }
680 return BAD_VALUE;
Dan Stoza7f7da322014-05-02 15:26:25 -0700681}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700682
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700683void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
684 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
685 this);
686 int32_t type = hw->getDisplayType();
687 int currentMode = hw->getActiveConfig();
688
689 if (mode == currentMode) {
690 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
691 return;
692 }
693
694 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
695 ALOGW("Trying to set config for virtual display");
696 return;
697 }
698
699 hw->setActiveConfig(mode);
700 getHwComposer().setActiveConfig(type, mode);
701}
702
703status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
704 class MessageSetActiveConfig: public MessageBase {
705 SurfaceFlinger& mFlinger;
706 sp<IBinder> mDisplay;
707 int mMode;
708 public:
709 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
710 int mode) :
711 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
712 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700713 Vector<DisplayInfo> configs;
714 mFlinger.getDisplayConfigs(mDisplay, &configs);
Jesse Hall78442112014-08-07 22:43:06 -0700715 if (mMode < 0 || mMode >= static_cast<int>(configs.size())) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700716 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700717 mMode, configs.size());
718 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700719 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
720 if (hw == NULL) {
721 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700722 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700723 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
724 ALOGW("Attempt to set active config = %d for virtual display",
725 mMode);
726 } else {
727 mFlinger.setActiveConfigInternal(hw, mMode);
728 }
729 return true;
730 }
731 };
732 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
733 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700734 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700735}
736
Svetoslavd85084b2014-03-20 10:28:31 -0700737status_t SurfaceFlinger::clearAnimationFrameStats() {
738 Mutex::Autolock _l(mStateLock);
739 mAnimFrameTracker.clearStats();
740 return NO_ERROR;
741}
742
743status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
744 Mutex::Autolock _l(mStateLock);
745 mAnimFrameTracker.getStats(outStats);
746 return NO_ERROR;
747}
748
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800749// ----------------------------------------------------------------------------
750
751sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800752 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700753}
754
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800755// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800756
757void SurfaceFlinger::waitForEvent() {
758 mEventQueue.waitMessage();
759}
760
761void SurfaceFlinger::signalTransaction() {
762 mEventQueue.invalidate();
763}
764
765void SurfaceFlinger::signalLayerUpdate() {
766 mEventQueue.invalidate();
767}
768
769void SurfaceFlinger::signalRefresh() {
770 mEventQueue.refresh();
771}
772
773status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800774 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800775 return mEventQueue.postMessage(msg, reltime);
776}
777
778status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800779 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800780 status_t res = mEventQueue.postMessage(msg, reltime);
781 if (res == NO_ERROR) {
782 msg->wait();
783 }
784 return res;
785}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800786
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700787void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700788 do {
789 waitForEvent();
790 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800791}
792
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700793void SurfaceFlinger::enableHardwareVsync() {
794 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700795 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700796 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700797 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
798 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700799 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700800 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700801}
802
Jesse Hall948fe0c2013-10-14 12:56:09 -0700803void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700804 Mutex::Autolock _l(mHWVsyncLock);
805
Jesse Hall948fe0c2013-10-14 12:56:09 -0700806 if (makeAvailable) {
807 mHWVsyncAvailable = true;
808 } else if (!mHWVsyncAvailable) {
809 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
810 return;
811 }
812
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700813 const nsecs_t period =
814 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
815
816 mPrimaryDispSync.reset();
817 mPrimaryDispSync.setPeriod(period);
818
819 if (!mPrimaryHWVsyncEnabled) {
820 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700821 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
822 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700823 mPrimaryHWVsyncEnabled = true;
824 }
825}
826
Jesse Hall948fe0c2013-10-14 12:56:09 -0700827void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700828 Mutex::Autolock _l(mHWVsyncLock);
829 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700830 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
831 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700832 mPrimaryDispSync.endResync();
833 mPrimaryHWVsyncEnabled = false;
834 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700835 if (makeUnavailable) {
836 mHWVsyncAvailable = false;
837 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700838}
839
840void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700841 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700842
Jamie Gennisd1700752013-10-14 12:22:52 -0700843 { // Scope for the lock
844 Mutex::Autolock _l(mHWVsyncLock);
845 if (type == 0 && mPrimaryHWVsyncEnabled) {
846 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700847 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700848 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700849
850 if (needsHwVsync) {
851 enableHardwareVsync();
852 } else {
853 disableHardwareVsync(false);
854 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700855}
856
857void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
858 if (mEventThread == NULL) {
859 // This is a temporary workaround for b/7145521. A non-null pointer
860 // does not mean EventThread has finished initializing, so this
861 // is not a correct fix.
862 ALOGW("WARNING: EventThread not started, ignoring hotplug");
863 return;
864 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700865
Jesse Hall9e663de2013-08-16 14:28:37 -0700866 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700867 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800868 if (connected) {
869 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700870 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800871 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
872 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700873 }
874 setTransactionFlags(eDisplayTransactionNeeded);
875
Andy McFadden9e9689c2012-10-10 18:17:51 -0700876 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700877 }
Mathias Agopian86303202012-07-24 22:46:10 -0700878}
879
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700880void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700881 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700882 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700883}
884
Mathias Agopian4fec8732012-06-29 14:12:52 -0700885void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800886 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800887 switch (what) {
Dan Stoza6b9454d2014-11-07 16:00:59 -0800888 case MessageQueue::TRANSACTION: {
889 handleMessageTransaction();
890 break;
891 }
892 case MessageQueue::INVALIDATE: {
893 bool refreshNeeded = handleMessageTransaction();
894 refreshNeeded |= handleMessageInvalidate();
Dan Stoza58784442014-12-02 16:58:17 -0800895 refreshNeeded |= mRepaintEverything;
Dan Stoza6b9454d2014-11-07 16:00:59 -0800896 if (refreshNeeded) {
Dan Stoza58784442014-12-02 16:58:17 -0800897 // Signal a refresh if a transaction modified the window state,
898 // a new buffer was latched, or if HWC has requested a full
899 // repaint
Dan Stoza6b9454d2014-11-07 16:00:59 -0800900 signalRefresh();
901 }
902 break;
903 }
904 case MessageQueue::REFRESH: {
905 handleMessageRefresh();
906 break;
907 }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800908 }
909}
910
Dan Stoza6b9454d2014-11-07 16:00:59 -0800911bool SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700912 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700913 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700914 handleTransaction(transactionFlags);
Dan Stoza6b9454d2014-11-07 16:00:59 -0800915 return true;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700916 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800917 return false;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700918}
919
Dan Stoza6b9454d2014-11-07 16:00:59 -0800920bool SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700921 ATRACE_CALL();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800922 return handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700923}
924
925void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700926 ATRACE_CALL();
927 preComposition();
928 rebuildLayerStacks();
929 setUpHWComposer();
930 doDebugFlashRegions();
931 doComposition();
932 postComposition();
933}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700934
Mathias Agopiancd60f992012-08-16 16:28:27 -0700935void SurfaceFlinger::doDebugFlashRegions()
936{
937 // is debugging enabled
938 if (CC_LIKELY(!mDebugRegion))
939 return;
940
941 const bool repaintEverything = mRepaintEverything;
942 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
943 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700944 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700945 // transform the dirty region into this screen's coordinate space
946 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
947 if (!dirtyRegion.isEmpty()) {
948 // redraw the whole screen
949 doComposeSurfaces(hw, Region(hw->bounds()));
950
951 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700952 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700953 RenderEngine& engine(getRenderEngine());
954 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
955
Mathias Agopiancd60f992012-08-16 16:28:27 -0700956 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700957 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700958 }
959 }
960 }
961
962 postFramebuffer();
963
964 if (mDebugRegion > 1) {
965 usleep(mDebugRegion * 1000);
966 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700967
968 HWComposer& hwc(getHwComposer());
969 if (hwc.initCheck() == NO_ERROR) {
970 status_t err = hwc.prepare();
971 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
972 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700973}
974
975void SurfaceFlinger::preComposition()
976{
977 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700978 const LayerVector& layers(mDrawingState.layersSortedByZ);
979 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700980 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700981 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700982 needExtraInvalidate = true;
983 }
984 }
985 if (needExtraInvalidate) {
986 signalLayerUpdate();
987 }
988}
989
990void SurfaceFlinger::postComposition()
991{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700992 const LayerVector& layers(mDrawingState.layersSortedByZ);
993 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700994 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700995 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700996 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800997
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700998 const HWComposer& hwc = getHwComposer();
999 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
1000
1001 if (presentFence->isValid()) {
1002 if (mPrimaryDispSync.addPresentFence(presentFence)) {
1003 enableHardwareVsync();
1004 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -07001005 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001006 }
1007 }
1008
Dan Stozab90cf072015-03-05 11:05:59 -08001009 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Andy McFadden5167ec62014-05-22 13:08:43 -07001010 if (kIgnorePresentFences) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001011 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001012 enableHardwareVsync();
1013 }
1014 }
1015
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001016 if (mAnimCompositionPending) {
1017 mAnimCompositionPending = false;
1018
Jesse Halla9a1b002013-02-27 16:39:25 -08001019 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001020 mAnimFrameTracker.setActualPresentFence(presentFence);
1021 } else {
1022 // The HWC doesn't support present fences, so use the refresh
1023 // timestamp instead.
1024 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1025 mAnimFrameTracker.setActualPresentTime(presentTime);
1026 }
1027 mAnimFrameTracker.advanceFrame();
1028 }
Dan Stozab90cf072015-03-05 11:05:59 -08001029
1030 if (hw->getPowerMode() == HWC_POWER_MODE_OFF) {
1031 return;
1032 }
1033
1034 nsecs_t currentTime = systemTime();
1035 if (mHasPoweredOff) {
1036 mHasPoweredOff = false;
1037 } else {
1038 nsecs_t period = mPrimaryDispSync.getPeriod();
1039 nsecs_t elapsedTime = currentTime - mLastSwapTime;
1040 size_t numPeriods = static_cast<size_t>(elapsedTime / period);
1041 if (numPeriods < NUM_BUCKETS - 1) {
1042 mFrameBuckets[numPeriods] += elapsedTime;
1043 } else {
1044 mFrameBuckets[NUM_BUCKETS - 1] += elapsedTime;
1045 }
1046 mTotalTime += elapsedTime;
1047 }
1048 mLastSwapTime = currentTime;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001049}
1050
1051void SurfaceFlinger::rebuildLayerStacks() {
1052 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001053 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001054 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -07001055 mVisibleRegionsDirty = false;
1056 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001057
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001058 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001059 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001060 Region opaqueRegion;
1061 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -08001062 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -07001063 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001064 const Transform& tr(hw->getTransform());
1065 const Rect bounds(hw->getBounds());
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001066 if (hw->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001067 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001068 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001069
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001070 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001071 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001072 const sp<Layer>& layer(layers[i]);
1073 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001074 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -07001075 Region drawRegion(tr.transform(
1076 layer->visibleNonTransparentRegion));
1077 drawRegion.andSelf(bounds);
1078 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001079 layersSortedByZ.add(layer);
1080 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001081 }
1082 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001083 }
Mathias Agopian42977342012-08-05 00:40:46 -07001084 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001085 hw->undefinedRegion.set(bounds);
1086 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
1087 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001088 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001089 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001090}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001091
Mathias Agopiancd60f992012-08-16 16:28:27 -07001092void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001093 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001094 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1095 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1096 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1097
1098 // If nothing has changed (!dirty), don't recompose.
1099 // If something changed, but we don't currently have any visible layers,
1100 // and didn't when we last did a composition, then skip it this time.
1101 // The second rule does two things:
1102 // - When all layers are removed from a display, we'll emit one black
1103 // frame, then nothing more until we get new layers.
1104 // - When a display is created with a private layer stack, we won't
1105 // emit any black frames until a layer is added to the layer stack.
1106 bool mustRecompose = dirty && !(empty && wasEmpty);
1107
1108 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1109 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1110 mustRecompose ? "doing" : "skipping",
1111 dirty ? "+" : "-",
1112 empty ? "+" : "-",
1113 wasEmpty ? "+" : "-");
1114
Dan Stoza71433162014-02-04 16:22:36 -08001115 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001116
1117 if (mustRecompose) {
1118 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1119 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001120 }
1121
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001122 HWComposer& hwc(getHwComposer());
1123 if (hwc.initCheck() == NO_ERROR) {
1124 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001125 if (CC_UNLIKELY(mHwWorkListDirty)) {
1126 mHwWorkListDirty = false;
1127 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1128 sp<const DisplayDevice> hw(mDisplays[dpy]);
1129 const int32_t id = hw->getHwcDisplayId();
1130 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001131 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001132 hw->getVisibleLayersSortedByZ());
1133 const size_t count = currentLayers.size();
1134 if (hwc.createWorkList(id, count) == NO_ERROR) {
1135 HWComposer::LayerListIterator cur = hwc.begin(id);
1136 const HWComposer::LayerListIterator end = hwc.end(id);
1137 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001138 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001139 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001140 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001141 cur->setSkip(true);
1142 }
1143 }
1144 }
1145 }
1146 }
1147 }
1148
1149 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001150 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001151 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001152 const int32_t id = hw->getHwcDisplayId();
1153 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001154 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001155 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001156 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001157 HWComposer::LayerListIterator cur = hwc.begin(id);
1158 const HWComposer::LayerListIterator end = hwc.end(id);
1159 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1160 /*
1161 * update the per-frame h/w composer data for each layer
1162 * and build the transparent region of the FB
1163 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001164 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001165 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -07001166 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001167 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001168 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001169
Riley Andrews03414a12014-07-01 14:22:59 -07001170 // If possible, attempt to use the cursor overlay on each display.
1171 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1172 sp<const DisplayDevice> hw(mDisplays[dpy]);
1173 const int32_t id = hw->getHwcDisplayId();
1174 if (id >= 0) {
1175 const Vector< sp<Layer> >& currentLayers(
1176 hw->getVisibleLayersSortedByZ());
1177 const size_t count = currentLayers.size();
1178 HWComposer::LayerListIterator cur = hwc.begin(id);
1179 const HWComposer::LayerListIterator end = hwc.end(id);
1180 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1181 const sp<Layer>& layer(currentLayers[i]);
1182 if (layer->isPotentialCursor()) {
1183 cur->setIsCursorLayerHint();
1184 break;
1185 }
1186 }
1187 }
1188 }
1189
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001190 status_t err = hwc.prepare();
1191 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001192
1193 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1194 sp<const DisplayDevice> hw(mDisplays[dpy]);
1195 hw->prepareFrame(hwc);
1196 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001197 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001198}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001199
Mathias Agopiancd60f992012-08-16 16:28:27 -07001200void SurfaceFlinger::doComposition() {
1201 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001202 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001203 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001204 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001205 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001206 // transform the dirty region into this screen's coordinate space
1207 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001208
1209 // repaint the framebuffer (if needed)
1210 doDisplayComposition(hw, dirtyRegion);
1211
Mathias Agopiancd60f992012-08-16 16:28:27 -07001212 hw->dirtyRegion.clear();
1213 hw->flip(hw->swapRegion);
1214 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001215 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001216 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001217 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001218 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001219 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001220}
1221
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222void SurfaceFlinger::postFramebuffer()
1223{
Mathias Agopian841cde52012-03-01 15:44:37 -08001224 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001225
Mathias Agopiana44b0412011-10-16 18:46:35 -07001226 const nsecs_t now = systemTime();
1227 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001228
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001229 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001230 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001231 if (!hwc.supportsFramebufferTarget()) {
1232 // EGL spec says:
1233 // "surface must be bound to the calling thread's current context,
1234 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001235 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001236 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001237 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001238 }
1239
Mathias Agopian6da15f42013-09-25 20:40:07 -07001240 // make the default display current because the VirtualDisplayDevice code cannot
1241 // deal with dequeueBuffer() being called outside of the composition loop; however
1242 // the code below can call glFlush() which is allowed (and does in some case) call
1243 // dequeueBuffer().
1244 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1245
Mathias Agopian92a979a2012-08-02 18:32:23 -07001246 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001247 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001248 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001249 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001250 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001251 int32_t id = hw->getHwcDisplayId();
1252 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001253 HWComposer::LayerListIterator cur = hwc.begin(id);
1254 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001255 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001256 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001257 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001258 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001259 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001260 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001261 }
Jesse Hallef194142012-06-14 14:45:17 -07001262 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001263 }
1264
Mathias Agopiana44b0412011-10-16 18:46:35 -07001265 mLastSwapBufferTime = systemTime() - now;
1266 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001267
1268 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1269 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1270 logFrameStats();
1271 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001272}
1273
Mathias Agopian87baae12012-07-31 12:38:26 -07001274void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001275{
Mathias Agopian841cde52012-03-01 15:44:37 -08001276 ATRACE_CALL();
1277
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001278 // here we keep a copy of the drawing state (that is the state that's
1279 // going to be overwritten by handleTransactionLocked()) outside of
1280 // mStateLock so that the side-effects of the State assignment
1281 // don't happen with mStateLock held (which can cause deadlocks).
1282 State drawingState(mDrawingState);
1283
Mathias Agopianca4d3602011-05-19 15:38:14 -07001284 Mutex::Autolock _l(mStateLock);
1285 const nsecs_t now = systemTime();
1286 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287
Mathias Agopianca4d3602011-05-19 15:38:14 -07001288 // Here we're guaranteed that some transaction flags are set
1289 // so we can call handleTransactionLocked() unconditionally.
1290 // We call getTransactionFlags(), which will also clear the flags,
1291 // with mStateLock held to guarantee that mCurrentState won't change
1292 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001293
Mathias Agopiane57f2922012-08-09 16:29:12 -07001294 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001295 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001296
Mathias Agopianca4d3602011-05-19 15:38:14 -07001297 mLastTransactionTime = systemTime() - now;
1298 mDebugInTransaction = 0;
1299 invalidateHwcGeometry();
1300 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001301}
1302
Mathias Agopian87baae12012-07-31 12:38:26 -07001303void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001304{
1305 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306 const size_t count = currentLayers.size();
1307
1308 /*
1309 * Traversal of the children
1310 * (perform the transaction for each of them if needed)
1311 */
1312
Mathias Agopian3559b072012-08-15 13:46:03 -07001313 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001315 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001316 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1317 if (!trFlags) continue;
1318
1319 const uint32_t flags = layer->doTransaction(0);
1320 if (flags & Layer::eVisibleRegion)
1321 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322 }
1323 }
1324
1325 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001326 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001327 */
1328
Mathias Agopiane57f2922012-08-09 16:29:12 -07001329 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001330 // here we take advantage of Vector's copy-on-write semantics to
1331 // improve performance by skipping the transaction entirely when
1332 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001333 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1334 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001335 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001336 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001337 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001338 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001339
1340 // find the displays that were removed
1341 // (ie: in drawing state but not in current state)
1342 // also handle displays that changed
1343 // (ie: displays that are in both lists)
1344 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001345 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1346 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001347 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001348 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001349 // Call makeCurrent() on the primary display so we can
1350 // be sure that nothing associated with this display
1351 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001352 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001353 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001354 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1355 if (hw != NULL)
1356 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001357 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001358 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001359 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001360 } else {
1361 ALOGW("trying to remove the main display");
1362 }
1363 } else {
1364 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001365 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001366 const wp<IBinder>& display(curr.keyAt(j));
Marco Nelissen097ca272014-11-14 08:01:01 -08001367 const sp<IBinder> state_binder = IInterface::asBinder(state.surface);
1368 const sp<IBinder> draw_binder = IInterface::asBinder(draw[i].surface);
Dan Albert1474f882014-09-08 18:59:09 -07001369 if (state_binder != draw_binder) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001370 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001371 // recreating the DisplayDevice, so we just remove it
1372 // from the drawing state, so that it get re-added
1373 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001374 sp<DisplayDevice> hw(getDisplayDevice(display));
1375 if (hw != NULL)
1376 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001377 mDisplays.removeItem(display);
1378 mDrawingState.displays.removeItemsAt(i);
1379 dc--; i--;
1380 // at this point we must loop to the next item
1381 continue;
1382 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001383
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001384 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001385 if (disp != NULL) {
1386 if (state.layerStack != draw[i].layerStack) {
1387 disp->setLayerStack(state.layerStack);
1388 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001389 if ((state.orientation != draw[i].orientation)
1390 || (state.viewport != draw[i].viewport)
1391 || (state.frame != draw[i].frame))
1392 {
1393 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001394 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001395 }
Michael Lentine47e45402014-07-18 15:34:25 -07001396 if (state.width != draw[i].width || state.height != draw[i].height) {
1397 disp->setDisplaySize(state.width, state.height);
1398 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001399 }
1400 }
1401 }
1402
1403 // find displays that were added
1404 // (ie: in current state but not in drawing state)
1405 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001406 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001407 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001408
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001409 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001410 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001411 sp<IGraphicBufferProducer> bqProducer;
1412 sp<IGraphicBufferConsumer> bqConsumer;
1413 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1414 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001415
Jesse Hall02d86562013-03-25 14:43:23 -07001416 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001417 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001418 // Virtual displays without a surface are dormant:
1419 // they have external state (layer stack, projection,
1420 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001421 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001422
Dan Stoza1f3efb12014-10-15 16:34:55 -07001423 int width = 0;
1424 int status = state.surface->query(
1425 NATIVE_WINDOW_WIDTH, &width);
1426 ALOGE_IF(status != NO_ERROR,
1427 "Unable to query width (%d)", status);
1428 int height = 0;
1429 status = state.surface->query(
1430 NATIVE_WINDOW_HEIGHT, &height);
1431 ALOGE_IF(status != NO_ERROR,
1432 "Unable to query height (%d)", status);
1433 if (MAX_VIRTUAL_DISPLAY_DIMENSION == 0 ||
1434 (width <= MAX_VIRTUAL_DISPLAY_DIMENSION &&
1435 height <= MAX_VIRTUAL_DISPLAY_DIMENSION)) {
1436 hwcDisplayId = allocateHwcDisplayId(state.type);
1437 }
1438
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001439 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
Dan Stozab9b08832014-03-13 11:55:57 -07001440 *mHwc, hwcDisplayId, state.surface,
1441 bqProducer, bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001442
1443 dispSurface = vds;
Michael Lentine47e45402014-07-18 15:34:25 -07001444 producer = vds;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001445 }
1446 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001447 ALOGE_IF(state.surface!=NULL,
1448 "adding a supported display, but rendering "
1449 "surface is provided (%p), ignoring it",
1450 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001451 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001452 // for supported (by hwc) displays we provide our
1453 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001454 dispSurface = new FramebufferSurface(*mHwc, state.type,
1455 bqConsumer);
1456 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001457 }
1458
1459 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001460 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001461 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001462 state.type, hwcDisplayId,
1463 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001464 display, dispSurface, producer,
1465 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001466 hw->setLayerStack(state.layerStack);
1467 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001468 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001469 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001470 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001471 if (state.isVirtualDisplay()) {
1472 if (hwcDisplayId >= 0) {
1473 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1474 hw->getWidth(), hw->getHeight(),
1475 hw->getFormat());
1476 }
1477 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001478 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001479 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001480 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001481 }
1482 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001484 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001485
Mathias Agopian84300952012-11-21 16:02:13 -08001486 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1487 // The transform hint might have changed for some layers
1488 // (either because a display has changed, or because a layer
1489 // as changed).
1490 //
1491 // Walk through all the layers in currentLayers,
1492 // and update their transform hint.
1493 //
1494 // If a layer is visible only on a single display, then that
1495 // display is used to calculate the hint, otherwise we use the
1496 // default display.
1497 //
1498 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1499 // the hint is set before we acquire a buffer from the surface texture.
1500 //
1501 // NOTE: layer transactions have taken place already, so we use their
1502 // drawing state. However, SurfaceFlinger's own transaction has not
1503 // happened yet, so we must use the current state layer list
1504 // (soon to become the drawing state list).
1505 //
1506 sp<const DisplayDevice> disp;
1507 uint32_t currentlayerStack = 0;
1508 for (size_t i=0; i<count; i++) {
1509 // NOTE: we rely on the fact that layers are sorted by
1510 // layerStack first (so we don't have to traverse the list
1511 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001512 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001513 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001514 if (i==0 || currentlayerStack != layerStack) {
1515 currentlayerStack = layerStack;
1516 // figure out if this layerstack is mirrored
1517 // (more than one display) if so, pick the default display,
1518 // if not, pick the only display it's on.
1519 disp.clear();
1520 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1521 sp<const DisplayDevice> hw(mDisplays[dpy]);
1522 if (hw->getLayerStack() == currentlayerStack) {
1523 if (disp == NULL) {
1524 disp = hw;
1525 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001526 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001527 break;
1528 }
1529 }
1530 }
1531 }
Chet Haase91d25932013-04-11 15:24:55 -07001532 if (disp == NULL) {
1533 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1534 // redraw after transform hint changes. See bug 8508397.
1535
1536 // could be null when this layer is using a layerStack
1537 // that is not visible on any display. Also can occur at
1538 // screen off/on times.
1539 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001540 }
Chet Haase91d25932013-04-11 15:24:55 -07001541 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001542 }
1543 }
1544
1545
Mathias Agopian3559b072012-08-15 13:46:03 -07001546 /*
1547 * Perform our own transaction if needed
1548 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001549
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001550 const LayerVector& layers(mDrawingState.layersSortedByZ);
1551 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001552 // layers have been added
1553 mVisibleRegionsDirty = true;
1554 }
1555
1556 // some layers might have been removed, so
1557 // we need to update the regions they're exposing.
1558 if (mLayersRemoved) {
1559 mLayersRemoved = false;
1560 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001561 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001562 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001563 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001564 if (currentLayers.indexOf(layer) < 0) {
1565 // this layer is not visible anymore
1566 // TODO: we could traverse the tree from front to back and
1567 // compute the actual visible region
1568 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001569 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001570 Region visibleReg = s.transform.transform(
1571 Region(Rect(s.active.w, s.active.h)));
1572 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001573 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001574 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001575 }
1576
1577 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001578
1579 updateCursorAsync();
1580}
1581
1582void SurfaceFlinger::updateCursorAsync()
1583{
1584 HWComposer& hwc(getHwComposer());
1585 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1586 sp<const DisplayDevice> hw(mDisplays[dpy]);
1587 const int32_t id = hw->getHwcDisplayId();
1588 if (id < 0) {
1589 continue;
1590 }
1591 const Vector< sp<Layer> >& currentLayers(
1592 hw->getVisibleLayersSortedByZ());
1593 const size_t count = currentLayers.size();
1594 HWComposer::LayerListIterator cur = hwc.begin(id);
1595 const HWComposer::LayerListIterator end = hwc.end(id);
1596 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1597 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1598 continue;
1599 }
1600 const sp<Layer>& layer(currentLayers[i]);
1601 Rect cursorPos = layer->getPosition(hw);
1602 hwc.setCursorPositionAsync(id, cursorPos);
1603 break;
1604 }
1605 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001606}
1607
1608void SurfaceFlinger::commitTransaction()
1609{
1610 if (!mLayersPendingRemoval.isEmpty()) {
1611 // Notify removed layers now that they can't be drawn from
1612 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1613 mLayersPendingRemoval[i]->onRemoved();
1614 }
1615 mLayersPendingRemoval.clear();
1616 }
1617
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001618 // If this transaction is part of a window animation then the next frame
1619 // we composite should be considered an animation as well.
1620 mAnimCompositionPending = mAnimTransactionPending;
1621
Mathias Agopian4fec8732012-06-29 14:12:52 -07001622 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001623 mTransactionPending = false;
1624 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001625 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001626}
1627
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001628void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001629 const LayerVector& currentLayers, uint32_t layerStack,
1630 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001631{
Mathias Agopian841cde52012-03-01 15:44:37 -08001632 ATRACE_CALL();
1633
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001634 Region aboveOpaqueLayers;
1635 Region aboveCoveredLayers;
1636 Region dirty;
1637
Mathias Agopian87baae12012-07-31 12:38:26 -07001638 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001639
1640 size_t i = currentLayers.size();
1641 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001642 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001643
1644 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001645 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001646
Jesse Hall01e29052013-02-19 16:13:35 -08001647 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001648 if (s.layerStack != layerStack)
1649 continue;
1650
Mathias Agopianab028732010-03-16 16:41:46 -07001651 /*
1652 * opaqueRegion: area of a surface that is fully opaque.
1653 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001655
1656 /*
1657 * visibleRegion: area of a surface that is visible on screen
1658 * and not fully transparent. This is essentially the layer's
1659 * footprint minus the opaque regions above it.
1660 * Areas covered by a translucent surface are considered visible.
1661 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001662 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001663
1664 /*
1665 * coveredRegion: area of a surface that is covered by all
1666 * visible regions above it (which includes the translucent areas).
1667 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001668 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001669
Jesse Halla8026d22012-09-25 13:25:04 -07001670 /*
1671 * transparentRegion: area of a surface that is hinted to be completely
1672 * transparent. This is only used to tell when the layer has no visible
1673 * non-transparent regions and can be removed from the layer list. It
1674 * does not affect the visibleRegion of this layer or any layers
1675 * beneath it. The hint may not be correct if apps don't respect the
1676 * SurfaceView restrictions (which, sadly, some don't).
1677 */
1678 Region transparentRegion;
1679
Mathias Agopianab028732010-03-16 16:41:46 -07001680
1681 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001682 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001683 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001684 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001685 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001686 if (!visibleRegion.isEmpty()) {
1687 // Remove the transparent area from the visible region
1688 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001689 const Transform tr(s.transform);
1690 if (tr.transformed()) {
1691 if (tr.preserveRects()) {
1692 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001693 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001694 } else {
1695 // transformation too complex, can't do the
1696 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001697 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001698 }
1699 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001700 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001701 }
Mathias Agopianab028732010-03-16 16:41:46 -07001702 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001703
Mathias Agopianab028732010-03-16 16:41:46 -07001704 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001705 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001706 if (s.alpha==255 && !translucent &&
1707 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1708 // the opaque region is the layer's footprint
1709 opaqueRegion = visibleRegion;
1710 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001711 }
1712 }
1713
Mathias Agopianab028732010-03-16 16:41:46 -07001714 // Clip the covered region to the visible region
1715 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1716
1717 // Update aboveCoveredLayers for next (lower) layer
1718 aboveCoveredLayers.orSelf(visibleRegion);
1719
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001720 // subtract the opaque region covered by the layers above us
1721 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001722
1723 // compute this layer's dirty region
1724 if (layer->contentDirty) {
1725 // we need to invalidate the whole region
1726 dirty = visibleRegion;
1727 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001728 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001729 layer->contentDirty = false;
1730 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001731 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001732 * the exposed region consists of two components:
1733 * 1) what's VISIBLE now and was COVERED before
1734 * 2) what's EXPOSED now less what was EXPOSED before
1735 *
1736 * note that (1) is conservative, we start with the whole
1737 * visible region but only keep what used to be covered by
1738 * something -- which mean it may have been exposed.
1739 *
1740 * (2) handles areas that were not covered by anything but got
1741 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001742 */
Mathias Agopianab028732010-03-16 16:41:46 -07001743 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001744 const Region oldVisibleRegion = layer->visibleRegion;
1745 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001746 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1747 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001748 }
1749 dirty.subtractSelf(aboveOpaqueLayers);
1750
1751 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001752 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001753
Mathias Agopianab028732010-03-16 16:41:46 -07001754 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001755 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001756
Jesse Halla8026d22012-09-25 13:25:04 -07001757 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001758 layer->setVisibleRegion(visibleRegion);
1759 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001760 layer->setVisibleNonTransparentRegion(
1761 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001762 }
1763
Mathias Agopian87baae12012-07-31 12:38:26 -07001764 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001765}
1766
Mathias Agopian87baae12012-07-31 12:38:26 -07001767void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1768 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001769 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001770 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1771 if (hw->getLayerStack() == layerStack) {
1772 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001773 }
1774 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001775}
1776
Dan Stoza6b9454d2014-11-07 16:00:59 -08001777bool SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001778{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001779 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001780
Mathias Agopian4fec8732012-06-29 14:12:52 -07001781 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001782 const LayerVector& layers(mDrawingState.layersSortedByZ);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001783 bool frameQueued = false;
Eric Penner51c59cd2014-07-28 19:51:58 -07001784
1785 // Store the set of layers that need updates. This set must not change as
1786 // buffers are being latched, as this could result in a deadlock.
1787 // Example: Two producers share the same command stream and:
1788 // 1.) Layer 0 is latched
1789 // 2.) Layer 0 gets a new frame
1790 // 2.) Layer 1 gets a new frame
1791 // 3.) Layer 1 is latched.
1792 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1793 // second frame. But layer 0's second frame could be waiting on display.
1794 Vector<Layer*> layersWithQueuedFrames;
1795 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001796 const sp<Layer>& layer(layers[i]);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001797 if (layer->hasQueuedFrame()) {
1798 frameQueued = true;
1799 if (layer->shouldPresentNow(mPrimaryDispSync)) {
1800 layersWithQueuedFrames.push_back(layer.get());
Dan Stozaee44edd2015-03-23 15:50:23 -07001801 } else {
1802 layer->useEmptyDamage();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001803 }
Dan Stozaee44edd2015-03-23 15:50:23 -07001804 } else {
1805 layer->useEmptyDamage();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001806 }
Eric Penner51c59cd2014-07-28 19:51:58 -07001807 }
1808 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
1809 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07001810 const Region dirty(layer->latchBuffer(visibleRegions));
Dan Stozaee44edd2015-03-23 15:50:23 -07001811 layer->useSurfaceDamage();
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001812 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001813 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001814 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001815
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001816 mVisibleRegionsDirty |= visibleRegions;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001817
1818 // If we will need to wake up at some time in the future to deal with a
1819 // queued frame that shouldn't be displayed during this vsync period, wake
1820 // up during the next vsync period to check again.
1821 if (frameQueued && layersWithQueuedFrames.empty()) {
1822 signalLayerUpdate();
1823 }
1824
1825 // Only continue with the refresh if there is actually new work to do
1826 return !layersWithQueuedFrames.empty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001827}
1828
Mathias Agopianad456f92011-01-13 17:53:01 -08001829void SurfaceFlinger::invalidateHwcGeometry()
1830{
1831 mHwWorkListDirty = true;
1832}
1833
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001834
Mathias Agopiancd60f992012-08-16 16:28:27 -07001835void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001836 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001837{
Dan Stoza71433162014-02-04 16:22:36 -08001838 // We only need to actually compose the display if:
1839 // 1) It is being handled by hardware composer, which may need this to
1840 // keep its virtual display state machine in sync, or
1841 // 2) There is work to be done (the dirty region isn't empty)
1842 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1843 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1844 return;
1845 }
1846
Mathias Agopian87baae12012-07-31 12:38:26 -07001847 Region dirtyRegion(inDirtyRegion);
1848
Mathias Agopianb8a55602009-06-26 19:06:36 -07001849 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001850 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001851
Mathias Agopian42977342012-08-05 00:40:46 -07001852 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001853 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001854 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1855 // takes a rectangle, we must make sure to update that whole
1856 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001857 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001858 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001859 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001860 // We need to redraw the rectangle that will be updated
1861 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001862 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001863 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001864 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001865 } else {
1866 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001867 dirtyRegion.set(hw->bounds());
1868 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001869 }
1870 }
1871
Alan Viverette9c5a3332013-09-12 20:04:35 -07001872 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Michael Lentine3f121fc2014-10-01 11:17:28 -07001873 if (!doComposeSurfaces(hw, dirtyRegion)) return;
Mathias Agopianff2ed702013-09-01 21:36:12 -07001874 } else {
1875 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001876 mat4 colorMatrix = mColorMatrix;
1877 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001878 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001879 }
Dan Stozaf0087992014-10-20 15:46:09 -07001880 mat4 oldMatrix = engine.setupColorTransform(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001881 doComposeSurfaces(hw, dirtyRegion);
Dan Stozaf0087992014-10-20 15:46:09 -07001882 engine.setupColorTransform(oldMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001883 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001884
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001885 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001886 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001887
1888 // swap buffers (presentation)
1889 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001890}
1891
Michael Lentine3f121fc2014-10-01 11:17:28 -07001892bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001893{
Mathias Agopian3f844832013-08-07 21:24:32 -07001894 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001895 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001896 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001897 HWComposer::LayerListIterator cur = hwc.begin(id);
1898 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001899
Jesse Halld05a17f2013-09-30 16:49:30 -07001900 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001901 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001902 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001903 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1904 hw->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07001905 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1906 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
1907 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
1908 }
1909 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08001910 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001911
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001912 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001913 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001914 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001915 // when using overlays, we assume a fully transparent framebuffer
1916 // NOTE: we could reduce how much we need to clear, for instance
1917 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001918 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001919 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001920 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001921 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001922 // we start with the whole screen area
1923 const Region bounds(hw->getBounds());
1924
1925 // we remove the scissor part
1926 // we're left with the letterbox region
1927 // (common case is that letterbox ends-up being empty)
1928 const Region letterbox(bounds.subtract(hw->getScissor()));
1929
1930 // compute the area to clear
1931 Region region(hw->undefinedRegion.merge(letterbox));
1932
1933 // but limit it to the dirty region
1934 region.andSelf(dirty);
1935
Mathias Agopianb9494d52012-04-18 02:28:45 -07001936 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001937 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001938 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001939 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001940 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001941 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001942
Mathias Agopian766dc492012-10-30 18:08:06 -07001943 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1944 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001945 // scissor on the main display. It should never be needed
1946 // anyways (though in theory it could since the API allows it).
1947 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001948 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001949 if (scissor != bounds) {
1950 // scissor doesn't match the screen's dimensions, so we
1951 // need to clear everything outside of it and enable
1952 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001953
Mathias Agopianf45c5102012-10-24 16:29:17 -07001954 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001955 const uint32_t height = hw->getHeight();
1956 engine.setScissor(scissor.left, height - scissor.bottom,
1957 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001958 }
1959 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001960 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001961
Mathias Agopian85d751c2012-08-29 16:59:24 -07001962 /*
1963 * and then, render the layers targeted at the framebuffer
1964 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001965
Mathias Agopian13127d82013-03-05 17:47:11 -08001966 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001967 const size_t count = layers.size();
1968 const Transform& tr = hw->getTransform();
1969 if (cur != end) {
1970 // we're using h/w composer
1971 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001972 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001973 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001974 if (!clip.isEmpty()) {
1975 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07001976 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07001977 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001978 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001979 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1980 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001981 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001982 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001983 // never clear the very first layer since we're
1984 // guaranteed the FB is already cleared
1985 layer->clearWithOpenGL(hw, clip);
1986 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001987 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001988 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001989 case HWC_FRAMEBUFFER: {
1990 layer->draw(hw, clip);
1991 break;
1992 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001993 case HWC_FRAMEBUFFER_TARGET: {
1994 // this should not happen as the iterator shouldn't
1995 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001996 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001997 break;
1998 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001999 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002000 }
2001 layer->setAcquireFence(hw, *cur);
2002 }
2003 } else {
2004 // we're not using h/w composer
2005 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002006 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002007 const Region clip(dirty.intersect(
2008 tr.transform(layer->visibleRegion)));
2009 if (!clip.isEmpty()) {
2010 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07002011 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002012 }
2013 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002014
2015 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002016 engine.disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07002017 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002018}
2019
Mathias Agopian3f844832013-08-07 21:24:32 -07002020void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07002021 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07002022 RenderEngine& engine(getRenderEngine());
2023 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002024}
2025
Dan Stoza7d89d062015-04-30 13:29:25 -07002026status_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -08002027 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07002028 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08002029 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07002030{
Dan Stoza7d89d062015-04-30 13:29:25 -07002031 // add this layer to the current state list
2032 {
2033 Mutex::Autolock _l(mStateLock);
2034 if (mCurrentState.layersSortedByZ.size() >= MAX_LAYERS) {
2035 return NO_MEMORY;
2036 }
2037 mCurrentState.layersSortedByZ.add(lbc);
2038 mGraphicBufferProducerList.add(IInterface::asBinder(gbc));
2039 }
2040
Mathias Agopian96f08192010-06-02 23:28:45 -07002041 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08002042 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07002043
Dan Stoza7d89d062015-04-30 13:29:25 -07002044 return NO_ERROR;
Mathias Agopian96f08192010-06-02 23:28:45 -07002045}
2046
Mathias Agopian3f844832013-08-07 21:24:32 -07002047status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002048 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08002049 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002050 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07002051 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002052 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07002053 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002054 return NO_ERROR;
2055 }
Mathias Agopian3d579642009-06-04 18:46:21 -07002056 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002057}
2058
Dan Stozac7014012014-02-14 15:03:43 -08002059uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07002060 return android_atomic_release_load(&mTransactionFlags);
2061}
2062
Mathias Agopian3f844832013-08-07 21:24:32 -07002063uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002064 return android_atomic_and(~flags, &mTransactionFlags) & flags;
2065}
2066
Mathias Agopian3f844832013-08-07 21:24:32 -07002067uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002068 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
2069 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002070 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002071 }
2072 return old;
2073}
2074
Mathias Agopian8b33f032012-07-24 20:43:54 -07002075void SurfaceFlinger::setTransactionState(
2076 const Vector<ComposerState>& state,
2077 const Vector<DisplayState>& displays,
2078 uint32_t flags)
2079{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002080 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07002081 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07002082 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002083
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002084 if (flags & eAnimation) {
2085 // For window updates that are part of an animation we must wait for
2086 // previous animation "frames" to be handled.
2087 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002088 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002089 if (CC_UNLIKELY(err != NO_ERROR)) {
2090 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002091 // caller after a few seconds.
2092 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
2093 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002094 mAnimTransactionPending = false;
2095 break;
2096 }
2097 }
2098 }
2099
Mathias Agopiane57f2922012-08-09 16:29:12 -07002100 size_t count = displays.size();
2101 for (size_t i=0 ; i<count ; i++) {
2102 const DisplayState& s(displays[i]);
2103 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002104 }
2105
Mathias Agopiane57f2922012-08-09 16:29:12 -07002106 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002107 for (size_t i=0 ; i<count ; i++) {
2108 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002109 // Here we need to check that the interface we're given is indeed
2110 // one of our own. A malicious client could give us a NULL
2111 // IInterface, or one of its own or even one of our own but a
2112 // different type. All these situations would cause us to crash.
2113 //
2114 // NOTE: it would be better to use RTTI as we could directly check
2115 // that we have a Client*. however, RTTI is disabled in Android.
2116 if (s.client != NULL) {
Marco Nelissen097ca272014-11-14 08:01:01 -08002117 sp<IBinder> binder = IInterface::asBinder(s.client);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002118 if (binder != NULL) {
2119 String16 desc(binder->getInterfaceDescriptor());
2120 if (desc == ISurfaceComposerClient::descriptor) {
2121 sp<Client> client( static_cast<Client *>(s.client.get()) );
2122 transactionFlags |= setClientStateLocked(client, s.state);
2123 }
2124 }
2125 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002126 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002127
Mathias Agopian386aa982011-11-07 21:58:03 -08002128 if (transactionFlags) {
2129 // this triggers the transaction
2130 setTransactionFlags(transactionFlags);
2131
2132 // if this is a synchronous transaction, wait for it to take effect
2133 // before returning.
2134 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002135 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002136 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002137 if (flags & eAnimation) {
2138 mAnimTransactionPending = true;
2139 }
2140 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002141 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2142 if (CC_UNLIKELY(err != NO_ERROR)) {
2143 // just in case something goes wrong in SF, return to the
2144 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002145 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2146 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002147 break;
2148 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002149 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002150 }
2151}
2152
Mathias Agopiane57f2922012-08-09 16:29:12 -07002153uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2154{
Jesse Hall9a143922012-10-04 16:29:19 -07002155 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2156 if (dpyIdx < 0)
2157 return 0;
2158
Mathias Agopiane57f2922012-08-09 16:29:12 -07002159 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002160 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002161 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002162 const uint32_t what = s.what;
2163 if (what & DisplayState::eSurfaceChanged) {
Marco Nelissen097ca272014-11-14 08:01:01 -08002164 if (IInterface::asBinder(disp.surface) != IInterface::asBinder(s.surface)) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002165 disp.surface = s.surface;
2166 flags |= eDisplayTransactionNeeded;
2167 }
2168 }
2169 if (what & DisplayState::eLayerStackChanged) {
2170 if (disp.layerStack != s.layerStack) {
2171 disp.layerStack = s.layerStack;
2172 flags |= eDisplayTransactionNeeded;
2173 }
2174 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002175 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002176 if (disp.orientation != s.orientation) {
2177 disp.orientation = s.orientation;
2178 flags |= eDisplayTransactionNeeded;
2179 }
2180 if (disp.frame != s.frame) {
2181 disp.frame = s.frame;
2182 flags |= eDisplayTransactionNeeded;
2183 }
2184 if (disp.viewport != s.viewport) {
2185 disp.viewport = s.viewport;
2186 flags |= eDisplayTransactionNeeded;
2187 }
2188 }
Michael Lentine47e45402014-07-18 15:34:25 -07002189 if (what & DisplayState::eDisplaySizeChanged) {
2190 if (disp.width != s.width) {
2191 disp.width = s.width;
2192 flags |= eDisplayTransactionNeeded;
2193 }
2194 if (disp.height != s.height) {
2195 disp.height = s.height;
2196 flags |= eDisplayTransactionNeeded;
2197 }
2198 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002199 }
2200 return flags;
2201}
2202
2203uint32_t SurfaceFlinger::setClientStateLocked(
2204 const sp<Client>& client,
2205 const layer_state_t& s)
2206{
2207 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002208 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002209 if (layer != 0) {
2210 const uint32_t what = s.what;
2211 if (what & layer_state_t::ePositionChanged) {
2212 if (layer->setPosition(s.x, s.y))
2213 flags |= eTraversalNeeded;
2214 }
2215 if (what & layer_state_t::eLayerChanged) {
2216 // NOTE: index needs to be calculated before we update the state
2217 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2218 if (layer->setLayer(s.z)) {
2219 mCurrentState.layersSortedByZ.removeAt(idx);
2220 mCurrentState.layersSortedByZ.add(layer);
2221 // we need traversal (state changed)
2222 // AND transaction (list changed)
2223 flags |= eTransactionNeeded|eTraversalNeeded;
2224 }
2225 }
2226 if (what & layer_state_t::eSizeChanged) {
2227 if (layer->setSize(s.w, s.h)) {
2228 flags |= eTraversalNeeded;
2229 }
2230 }
2231 if (what & layer_state_t::eAlphaChanged) {
2232 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2233 flags |= eTraversalNeeded;
2234 }
2235 if (what & layer_state_t::eMatrixChanged) {
2236 if (layer->setMatrix(s.matrix))
2237 flags |= eTraversalNeeded;
2238 }
2239 if (what & layer_state_t::eTransparentRegionChanged) {
2240 if (layer->setTransparentRegionHint(s.transparentRegion))
2241 flags |= eTraversalNeeded;
2242 }
Dan Stoza23116082015-06-18 14:58:39 -07002243 if (what & layer_state_t::eFlagsChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002244 if (layer->setFlags(s.flags, s.mask))
2245 flags |= eTraversalNeeded;
2246 }
2247 if (what & layer_state_t::eCropChanged) {
2248 if (layer->setCrop(s.crop))
2249 flags |= eTraversalNeeded;
2250 }
2251 if (what & layer_state_t::eLayerStackChanged) {
2252 // NOTE: index needs to be calculated before we update the state
2253 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2254 if (layer->setLayerStack(s.layerStack)) {
2255 mCurrentState.layersSortedByZ.removeAt(idx);
2256 mCurrentState.layersSortedByZ.add(layer);
2257 // we need traversal (state changed)
2258 // AND transaction (list changed)
2259 flags |= eTransactionNeeded|eTraversalNeeded;
2260 }
2261 }
2262 }
2263 return flags;
2264}
2265
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002266status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002267 const String8& name,
2268 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002269 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2270 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002271{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002272 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002273 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002274 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002275 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002276 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002277 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002278
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002279 status_t result = NO_ERROR;
2280
2281 sp<Layer> layer;
2282
Mathias Agopian3165cc22012-08-08 19:42:09 -07002283 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2284 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002285 result = createNormalLayer(client,
2286 name, w, h, flags, format,
2287 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002288 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002289 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002290 result = createDimLayer(client,
2291 name, w, h, flags,
2292 handle, gbp, &layer);
2293 break;
2294 default:
2295 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002296 break;
2297 }
2298
Dan Stoza7d89d062015-04-30 13:29:25 -07002299 if (result != NO_ERROR) {
2300 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002301 }
Dan Stoza7d89d062015-04-30 13:29:25 -07002302
2303 result = addClientLayer(client, *handle, *gbp, layer);
2304 if (result != NO_ERROR) {
2305 return result;
2306 }
2307
2308 setTransactionFlags(eTransactionNeeded);
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002309 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002310}
2311
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002312status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2313 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2314 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002315{
2316 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002317 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002318 case PIXEL_FORMAT_TRANSPARENT:
2319 case PIXEL_FORMAT_TRANSLUCENT:
2320 format = PIXEL_FORMAT_RGBA_8888;
2321 break;
2322 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002323 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002324 break;
2325 }
2326
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002327 *outLayer = new Layer(this, client, name, w, h, flags);
2328 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2329 if (err == NO_ERROR) {
2330 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002331 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002332 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002333
2334 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2335 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002336}
2337
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002338status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2339 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2340 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002341{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002342 *outLayer = new LayerDim(this, client, name, w, h, flags);
2343 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002344 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002345 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002346}
2347
Mathias Agopianac9fa422013-02-11 16:40:36 -08002348status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002349{
Mathias Agopian67106042013-03-14 19:18:13 -07002350 // called by the window manager when it wants to remove a Layer
2351 status_t err = NO_ERROR;
2352 sp<Layer> l(client->getLayerUser(handle));
2353 if (l != NULL) {
2354 err = removeLayer(l);
2355 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2356 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002357 }
2358 return err;
2359}
2360
Mathias Agopian13127d82013-03-05 17:47:11 -08002361status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002362{
Mathias Agopian67106042013-03-14 19:18:13 -07002363 // called by ~LayerCleaner() when all references to the IBinder (handle)
2364 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002365 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002366 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002367 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002368 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002369 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002370 "error removing layer=%p (%s)", l.get(), strerror(-err));
2371 }
2372 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002373}
2374
Mathias Agopianb60314a2012-04-10 22:09:54 -07002375// ---------------------------------------------------------------------------
2376
Andy McFadden13a082e2012-08-24 10:16:42 -07002377void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002378 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002379 Vector<ComposerState> state;
2380 Vector<DisplayState> displays;
2381 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002382 d.what = DisplayState::eDisplayProjectionChanged |
2383 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002384 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002385 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002386 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002387 d.frame.makeInvalid();
2388 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002389 d.width = 0;
2390 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002391 displays.add(d);
2392 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002393 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002394
2395 const nsecs_t period =
2396 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2397 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002398}
2399
2400void SurfaceFlinger::initializeDisplays() {
2401 class MessageScreenInitialized : public MessageBase {
2402 SurfaceFlinger* flinger;
2403 public:
2404 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2405 virtual bool handler() {
2406 flinger->onInitializeDisplays();
2407 return true;
2408 }
2409 };
2410 sp<MessageBase> msg = new MessageScreenInitialized(this);
2411 postMessageAsync(msg); // we may be called from main thread, use async message
2412}
2413
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002414void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2415 int mode) {
2416 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2417 this);
2418 int32_t type = hw->getDisplayType();
2419 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002420
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002421 if (mode == currentMode) {
2422 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002423 return;
2424 }
2425
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002426 hw->setPowerMode(mode);
2427 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2428 ALOGW("Trying to set power mode for virtual display");
2429 return;
2430 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002431
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002432 if (currentMode == HWC_POWER_MODE_OFF) {
2433 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002434 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2435 // FIXME: eventthread only knows about the main display right now
2436 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002437 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002438 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002439
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002440 mVisibleRegionsDirty = true;
Dan Stozab90cf072015-03-05 11:05:59 -08002441 mHasPoweredOff = true;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002442 repaintEverything();
2443 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002444 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002445 disableHardwareVsync(true); // also cancels any in-progress resync
2446
Mathias Agopiancde87a32012-09-13 14:09:01 -07002447 // FIXME: eventthread only knows about the main display right now
2448 mEventThread->onScreenReleased();
2449 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002450
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002451 getHwComposer().setPowerMode(type, mode);
2452 mVisibleRegionsDirty = true;
2453 // from this point on, SF will stop drawing on this display
2454 } else {
2455 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002456 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002457}
2458
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002459void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2460 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002461 SurfaceFlinger& mFlinger;
2462 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002463 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002464 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002465 MessageSetPowerMode(SurfaceFlinger& flinger,
2466 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2467 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002468 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002469 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002470 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002471 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002472 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002473 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002474 ALOGW("Attempt to set power mode = %d for virtual display",
2475 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002476 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002477 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002478 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002479 return true;
2480 }
2481 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002482 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002483 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002484}
2485
2486// ---------------------------------------------------------------------------
2487
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002488status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2489{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002490 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002491
Mathias Agopianbd115332013-04-18 16:41:04 -07002492 IPCThreadState* ipc = IPCThreadState::self();
2493 const int pid = ipc->getCallingPid();
2494 const int uid = ipc->getCallingUid();
2495 if ((uid != AID_SHELL) &&
2496 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002497 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002498 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002499 } else {
Jesse Hallfcd15b42014-12-23 13:57:23 -08002500 // Try to get the main lock, but give up after one second
Mathias Agopian9795c422009-08-26 16:36:26 -07002501 // (this would indicate SF is stuck, but we want to be able to
2502 // print something in dumpsys).
Jesse Hallfcd15b42014-12-23 13:57:23 -08002503 status_t err = mStateLock.timedLock(s2ns(1));
2504 bool locked = (err == NO_ERROR);
Mathias Agopian9795c422009-08-26 16:36:26 -07002505 if (!locked) {
Jesse Hallfcd15b42014-12-23 13:57:23 -08002506 result.appendFormat(
2507 "SurfaceFlinger appears to be unresponsive (%s [%d]), "
2508 "dumping anyways (no locks held)\n", strerror(-err), err);
Mathias Agopian9795c422009-08-26 16:36:26 -07002509 }
2510
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002511 bool dumpAll = true;
2512 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002513 size_t numArgs = args.size();
2514 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002515 if ((index < numArgs) &&
2516 (args[index] == String16("--list"))) {
2517 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002518 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002519 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002520 }
2521
2522 if ((index < numArgs) &&
2523 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002524 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002525 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002526 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002527 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002528
2529 if ((index < numArgs) &&
2530 (args[index] == String16("--latency-clear"))) {
2531 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002532 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002533 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002534 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002535
2536 if ((index < numArgs) &&
2537 (args[index] == String16("--dispsync"))) {
2538 index++;
2539 mPrimaryDispSync.dump(result);
2540 dumpAll = false;
2541 }
Dan Stozab90cf072015-03-05 11:05:59 -08002542
2543 if ((index < numArgs) &&
2544 (args[index] == String16("--static-screen"))) {
2545 index++;
2546 dumpStaticScreenStats(result);
2547 dumpAll = false;
2548 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002549 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002550
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002551 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002552 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002553 }
2554
Mathias Agopian9795c422009-08-26 16:36:26 -07002555 if (locked) {
2556 mStateLock.unlock();
2557 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002558 }
2559 write(fd, result.string(), result.size());
2560 return NO_ERROR;
2561}
2562
Dan Stozac7014012014-02-14 15:03:43 -08002563void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2564 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002565{
2566 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2567 const size_t count = currentLayers.size();
2568 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002569 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002570 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002571 }
2572}
2573
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002574void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002575 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002576{
2577 String8 name;
2578 if (index < args.size()) {
2579 name = String8(args[index]);
2580 index++;
2581 }
2582
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002583 const nsecs_t period =
2584 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002585 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002586
2587 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002588 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002589 } else {
2590 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2591 const size_t count = currentLayers.size();
2592 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002593 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002594 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002595 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002596 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002597 }
2598 }
2599}
2600
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002601void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002602 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002603{
2604 String8 name;
2605 if (index < args.size()) {
2606 name = String8(args[index]);
2607 index++;
2608 }
2609
2610 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2611 const size_t count = currentLayers.size();
2612 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002613 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002614 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002615 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002616 }
2617 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002618
Svetoslavd85084b2014-03-20 10:28:31 -07002619 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002620}
2621
Jamie Gennis6547ff42013-07-16 20:12:42 -07002622// This should only be called from the main thread. Otherwise it would need
2623// the lock and should use mCurrentState rather than mDrawingState.
2624void SurfaceFlinger::logFrameStats() {
2625 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2626 const size_t count = drawingLayers.size();
2627 for (size_t i=0 ; i<count ; i++) {
2628 const sp<Layer>& layer(drawingLayers[i]);
2629 layer->logFrameStats();
2630 }
2631
2632 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2633}
2634
Andy McFadden4803b742012-09-24 19:07:20 -07002635/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2636{
2637 static const char* config =
2638 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002639#ifdef HAS_CONTEXT_PRIORITY
2640 " HAS_CONTEXT_PRIORITY"
2641#endif
2642#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2643 " NEVER_DEFAULT_TO_ASYNC_MODE"
2644#endif
2645#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2646 " TARGET_DISABLE_TRIPLE_BUFFERING"
2647#endif
2648 "]";
2649 result.append(config);
2650}
2651
Dan Stozab90cf072015-03-05 11:05:59 -08002652void SurfaceFlinger::dumpStaticScreenStats(String8& result) const
2653{
2654 result.appendFormat("Static screen stats:\n");
2655 for (size_t b = 0; b < NUM_BUCKETS - 1; ++b) {
2656 float bucketTimeSec = mFrameBuckets[b] / 1e9;
2657 float percent = 100.0f *
2658 static_cast<float>(mFrameBuckets[b]) / mTotalTime;
2659 result.appendFormat(" < %zd frames: %.3f s (%.1f%%)\n",
2660 b + 1, bucketTimeSec, percent);
2661 }
2662 float bucketTimeSec = mFrameBuckets[NUM_BUCKETS - 1] / 1e9;
2663 float percent = 100.0f *
2664 static_cast<float>(mFrameBuckets[NUM_BUCKETS - 1]) / mTotalTime;
2665 result.appendFormat(" %zd+ frames: %.3f s (%.1f%%)\n",
2666 NUM_BUCKETS - 1, bucketTimeSec, percent);
2667}
2668
Mathias Agopian74d211a2013-04-22 16:55:35 +02002669void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2670 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002671{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002672 bool colorize = false;
2673 if (index < args.size()
2674 && (args[index] == String16("--color"))) {
2675 colorize = true;
2676 index++;
2677 }
2678
2679 Colorizer colorizer(colorize);
2680
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002681 // figure out if we're stuck somewhere
2682 const nsecs_t now = systemTime();
2683 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2684 const nsecs_t inTransaction(mDebugInTransaction);
2685 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2686 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2687
2688 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002689 * Dump library configuration.
2690 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002691
2692 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002693 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002694 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002695 appendSfConfigString(result);
2696 appendUiConfigString(result);
2697 appendGuiConfigString(result);
2698 result.append("\n");
2699
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002700 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002701 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002702 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002703 result.append(SyncFeatures::getInstance().toString());
2704 result.append("\n");
2705
Andy McFadden41d67d72014-04-25 16:58:34 -07002706 colorizer.bold(result);
2707 result.append("DispSync configuration: ");
2708 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002709 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2710 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002711 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2712 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2713 result.append("\n");
2714
Dan Stozab90cf072015-03-05 11:05:59 -08002715 // Dump static screen stats
2716 result.append("\n");
2717 dumpStaticScreenStats(result);
2718 result.append("\n");
2719
Andy McFadden4803b742012-09-24 19:07:20 -07002720 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002721 * Dump the visible layer list
2722 */
2723 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2724 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002725 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002726 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002727 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002728 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002729 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002730 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002731 }
2732
2733 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002734 * Dump Display state
2735 */
2736
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002737 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002738 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002739 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002740 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2741 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002742 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002743 }
2744
2745 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002746 * Dump SurfaceFlinger global state
2747 */
2748
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002749 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002750 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002751 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002752
Mathias Agopian888c8222012-08-04 21:10:38 -07002753 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002754 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002755
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002756 colorizer.bold(result);
2757 result.appendFormat("EGL implementation : %s\n",
2758 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2759 colorizer.reset(result);
2760 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002761 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002762
Mathias Agopian875d8e12013-06-07 15:35:48 -07002763 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002764
Mathias Agopian42977342012-08-05 00:40:46 -07002765 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002766 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2767 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002768 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002769 " last eglSwapBuffers() time: %f us\n"
2770 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002771 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002772 " refresh-rate : %f fps\n"
2773 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002774 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002775 " gpu_to_cpu_unsupported : %d\n"
2776 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002777 mLastSwapBufferTime/1000.0,
2778 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002779 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002780 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2781 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002782 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002783 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002784
Mathias Agopian74d211a2013-04-22 16:55:35 +02002785 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002786 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002787
Mathias Agopian74d211a2013-04-22 16:55:35 +02002788 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002789 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002790
2791 /*
2792 * VSYNC state
2793 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002794 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002795
2796 /*
2797 * Dump HWComposer state
2798 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002799 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002800 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002801 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002802 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002803 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002804 (mDebugDisableHWC || mDebugRegion || mDaltonize
2805 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002806 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002807
2808 /*
2809 * Dump gralloc state
2810 */
2811 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2812 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002813}
2814
Mathias Agopian13127d82013-03-05 17:47:11 -08002815const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002816SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002817 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002818 wp<IBinder> dpy;
2819 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2820 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2821 dpy = mDisplays.keyAt(i);
2822 break;
2823 }
2824 }
2825 if (dpy == NULL) {
2826 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2827 // Just use the primary display so we have something to return
2828 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2829 }
2830 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002831}
2832
Keun young Park63f165f2012-08-31 10:53:36 -07002833bool SurfaceFlinger::startDdmConnection()
2834{
2835 void* libddmconnection_dso =
2836 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2837 if (!libddmconnection_dso) {
2838 return false;
2839 }
2840 void (*DdmConnection_start)(const char* name);
2841 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07002842 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07002843 if (!DdmConnection_start) {
2844 dlclose(libddmconnection_dso);
2845 return false;
2846 }
2847 (*DdmConnection_start)(getServiceName());
2848 return true;
2849}
2850
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002851status_t SurfaceFlinger::onTransact(
2852 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2853{
2854 switch (code) {
2855 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002856 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002857 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002858 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002859 case CLEAR_ANIMATION_FRAME_STATS:
2860 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002861 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002862 {
2863 // codes that require permission check
2864 IPCThreadState* ipc = IPCThreadState::self();
2865 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002866 const int uid = ipc->getCallingUid();
Jeff Brown3bfe51d2015-04-10 20:20:13 -07002867 if ((uid != AID_GRAPHICS && uid != AID_SYSTEM) &&
Mathias Agopian99b49842011-06-27 16:05:52 -07002868 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002869 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002870 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2871 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002872 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002873 break;
2874 }
2875 case CAPTURE_SCREEN:
2876 {
2877 // codes that require permission check
2878 IPCThreadState* ipc = IPCThreadState::self();
2879 const int pid = ipc->getCallingPid();
2880 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002881 if ((uid != AID_GRAPHICS) &&
2882 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002883 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002884 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2885 return PERMISSION_DENIED;
2886 }
2887 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002888 }
2889 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002890
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002891 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2892 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002893 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002894 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002895 IPCThreadState* ipc = IPCThreadState::self();
2896 const int pid = ipc->getCallingPid();
2897 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002898 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002899 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002900 return PERMISSION_DENIED;
2901 }
2902 int n;
2903 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002904 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002905 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002906 return NO_ERROR;
2907 case 1002: // SHOW_UPDATES
2908 n = data.readInt32();
2909 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002910 invalidateHwcGeometry();
2911 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002912 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002913 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002914 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002915 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002916 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002917 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002918 setTransactionFlags(
2919 eTransactionNeeded|
2920 eDisplayTransactionNeeded|
2921 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002922 return NO_ERROR;
2923 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002924 case 1006:{ // send empty update
2925 signalRefresh();
2926 return NO_ERROR;
2927 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002928 case 1008: // toggle use of hw composer
2929 n = data.readInt32();
2930 mDebugDisableHWC = n ? 1 : 0;
2931 invalidateHwcGeometry();
2932 repaintEverything();
2933 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002934 case 1009: // toggle use of transform hint
2935 n = data.readInt32();
2936 mDebugDisableTransformHint = n ? 1 : 0;
2937 invalidateHwcGeometry();
2938 repaintEverything();
2939 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002940 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002941 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002942 reply->writeInt32(0);
2943 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002944 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002945 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002946 return NO_ERROR;
2947 case 1013: {
2948 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002949 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2950 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002951 return NO_ERROR;
2952 }
2953 case 1014: {
2954 // daltonize
2955 n = data.readInt32();
2956 switch (n % 10) {
2957 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2958 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2959 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2960 }
2961 if (n >= 10) {
2962 mDaltonizer.setMode(Daltonizer::correction);
2963 } else {
2964 mDaltonizer.setMode(Daltonizer::simulation);
2965 }
2966 mDaltonize = n > 0;
2967 invalidateHwcGeometry();
2968 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002969 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002970 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002971 case 1015: {
2972 // apply a color matrix
2973 n = data.readInt32();
2974 mHasColorMatrix = n ? 1 : 0;
2975 if (n) {
2976 // color matrix is sent as mat3 matrix followed by vec3
2977 // offset, then packed into a mat4 where the last row is
2978 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002979 for (size_t i = 0 ; i < 4; i++) {
2980 for (size_t j = 0; j < 4; j++) {
2981 mColorMatrix[i][j] = data.readFloat();
2982 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002983 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002984 } else {
2985 mColorMatrix = mat4();
2986 }
2987 invalidateHwcGeometry();
2988 repaintEverything();
2989 return NO_ERROR;
2990 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002991 // This is an experimental interface
2992 // Needs to be shifted to proper binder interface when we productize
2993 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07002994 n = data.readInt32();
2995 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002996 return NO_ERROR;
2997 }
Dan Stozaee44edd2015-03-23 15:50:23 -07002998 case 1017: {
2999 n = data.readInt32();
3000 mForceFullDamage = static_cast<bool>(n);
3001 return NO_ERROR;
3002 }
Dan Stozadb4ac3c2015-04-14 11:34:01 -07003003 case 1018: { // Modify Choreographer's phase offset
3004 n = data.readInt32();
3005 mEventThread->setPhaseOffset(static_cast<nsecs_t>(n));
3006 return NO_ERROR;
3007 }
3008 case 1019: { // Modify SurfaceFlinger's phase offset
3009 n = data.readInt32();
3010 mSFEventThread->setPhaseOffset(static_cast<nsecs_t>(n));
3011 return NO_ERROR;
3012 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003013 }
3014 }
3015 return err;
3016}
3017
Mathias Agopian53331da2011-08-22 21:44:41 -07003018void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07003019 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08003020 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07003021}
3022
Mathias Agopian59119e62010-10-11 12:37:43 -07003023// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003024// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07003025// ---------------------------------------------------------------------------
3026
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003027/* The code below is here to handle b/8734824
3028 *
3029 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07003030 * from the surfaceflinger thread to the calling binder thread, where they
3031 * are executed. This allows the calling thread in the calling process to be
3032 * reused and not depend on having "enough" binder threads to handle the
3033 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003034 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003035class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07003036 /* Parts of GraphicProducerWrapper are run on two different threads,
3037 * communicating by sending messages via Looper but also by shared member
3038 * data. Coherence maintenance is subtle and in places implicit (ugh).
3039 *
3040 * Don't rely on Looper's sendMessage/handleMessage providing
3041 * release/acquire semantics for any data not actually in the Message.
3042 * Data going from surfaceflinger to binder threads needs to be
3043 * synchronized explicitly.
3044 *
3045 * Barrier open/wait do provide release/acquire semantics. This provides
3046 * implicit synchronization for data coming back from binder to
3047 * surfaceflinger threads.
3048 */
3049
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003050 sp<IGraphicBufferProducer> impl;
3051 sp<Looper> looper;
3052 status_t result;
3053 bool exitPending;
3054 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07003055 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003056 uint32_t code;
3057 Parcel const* data;
3058 Parcel* reply;
3059
3060 enum {
3061 MSG_API_CALL,
3062 MSG_EXIT
3063 };
3064
3065 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003066 * Called on surfaceflinger thread. This is called by our "fake"
3067 * BpGraphicBufferProducer. We package the data and reply Parcel and
3068 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003069 */
3070 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08003071 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003072 this->code = code;
3073 this->data = &data;
3074 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003075 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07003076 // if we've exited, we run the message synchronously right here.
3077 // note (JH): as far as I can tell from looking at the code, this
3078 // never actually happens. if it does, i'm not sure if it happens
3079 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003080 handleMessage(Message(MSG_API_CALL));
3081 } else {
3082 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07003083 // Prevent stores to this->{code, data, reply} from being
3084 // reordered later than the construction of Message.
3085 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003086 looper->sendMessage(this, Message(MSG_API_CALL));
3087 barrier.wait();
3088 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08003089 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003090 }
3091
3092 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003093 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003094 * call the real BpGraphicBufferProducer.
3095 */
3096 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07003097 int what = message.what;
3098 // Prevent reads below from happening before the read from Message
3099 atomic_thread_fence(memory_order_acquire);
3100 if (what == MSG_API_CALL) {
Marco Nelissen097ca272014-11-14 08:01:01 -08003101 result = IInterface::asBinder(impl)->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003102 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07003103 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003104 exitRequested = true;
3105 }
3106 }
3107
3108public:
Jesse Hallb154c422014-07-13 12:47:02 -07003109 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
3110 : impl(impl),
3111 looper(new Looper(true)),
3112 exitPending(false),
3113 exitRequested(false)
3114 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003115
Jesse Hallb154c422014-07-13 12:47:02 -07003116 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003117 status_t waitForResponse() {
3118 do {
3119 looper->pollOnce(-1);
3120 } while (!exitRequested);
3121 return result;
3122 }
3123
Jesse Hallb154c422014-07-13 12:47:02 -07003124 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003125 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07003126 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003127 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07003128 // Ensure this->result is visible to the binder thread before it
3129 // handles the message.
3130 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003131 looper->sendMessage(this, Message(MSG_EXIT));
3132 }
3133};
3134
3135
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003136status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
3137 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003138 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003139 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003140 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003141
3142 if (CC_UNLIKELY(display == 0))
3143 return BAD_VALUE;
3144
3145 if (CC_UNLIKELY(producer == 0))
3146 return BAD_VALUE;
3147
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003148 // if we have secure windows on this display, never allow the screen capture
3149 // unless the producer interface is local (i.e.: we can take a screenshot for
3150 // ourselves).
Marco Nelissen097ca272014-11-14 08:01:01 -08003151 if (!IInterface::asBinder(producer)->localBinder()) {
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003152 Mutex::Autolock _l(mStateLock);
3153 sp<const DisplayDevice> hw(getDisplayDevice(display));
3154 if (hw->getSecureLayerVisible()) {
3155 ALOGW("FB is protected: PERMISSION_DENIED");
3156 return PERMISSION_DENIED;
3157 }
3158 }
3159
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003160 // Convert to surfaceflinger's internal rotation type.
3161 Transform::orientation_flags rotationFlags;
3162 switch (rotation) {
3163 case ISurfaceComposer::eRotateNone:
3164 rotationFlags = Transform::ROT_0;
3165 break;
3166 case ISurfaceComposer::eRotate90:
3167 rotationFlags = Transform::ROT_90;
3168 break;
3169 case ISurfaceComposer::eRotate180:
3170 rotationFlags = Transform::ROT_180;
3171 break;
3172 case ISurfaceComposer::eRotate270:
3173 rotationFlags = Transform::ROT_270;
3174 break;
3175 default:
3176 rotationFlags = Transform::ROT_0;
3177 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3178 break;
3179 }
3180
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003181 class MessageCaptureScreen : public MessageBase {
3182 SurfaceFlinger* flinger;
3183 sp<IBinder> display;
3184 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003185 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003186 uint32_t reqWidth, reqHeight;
3187 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003188 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003189 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003190 status_t result;
3191 public:
3192 MessageCaptureScreen(SurfaceFlinger* flinger,
3193 const sp<IBinder>& display,
3194 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003195 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003196 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003197 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003198 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003199 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003200 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003201 useIdentityTransform(useIdentityTransform),
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003202 rotation(rotation),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003203 result(PERMISSION_DENIED)
3204 {
3205 }
3206 status_t getResult() const {
3207 return result;
3208 }
3209 virtual bool handler() {
3210 Mutex::Autolock _l(flinger->mStateLock);
3211 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003212 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003213 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003214 useIdentityTransform, rotation);
Marco Nelissen097ca272014-11-14 08:01:01 -08003215 static_cast<GraphicProducerWrapper*>(IInterface::asBinder(producer).get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003216 return true;
3217 }
3218 };
3219
Mathias Agopian9eb1f052013-04-10 16:27:17 -07003220 // make sure to process transactions before screenshots -- a transaction
3221 // might already be pending but scheduled for VSYNC; this guarantees we
3222 // will handle it before the screenshot. When VSYNC finally arrives
3223 // the scheduled transaction will be a no-op. If no transactions are
3224 // scheduled at this time, this will end-up being a no-op as well.
3225 mEventQueue.invalidateTransactionNow();
3226
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003227 // this creates a "fake" BBinder which will serve as a "fake" remote
3228 // binder to receive the marshaled calls and forward them to the
3229 // real remote (a BpGraphicBufferProducer)
3230 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3231
3232 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3233 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003234 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003235 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003236 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003237 useIdentityTransform, rotationFlags);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003238
3239 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003240 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003241 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003242 }
3243 return res;
3244}
3245
Mathias Agopian180f10d2013-04-10 22:55:41 -07003246
3247void SurfaceFlinger::renderScreenImplLocked(
3248 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003249 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003250 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003251 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003252{
3253 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003254 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003255
3256 // get screen geometry
Andreas Gampe89fd4f72014-11-13 14:18:56 -08003257 const int32_t hw_w = hw->getWidth();
3258 const int32_t hw_h = hw->getHeight();
3259 const bool filtering = static_cast<int32_t>(reqWidth) != hw_w ||
Christopher Ferris0e749792015-03-23 14:32:15 -07003260 static_cast<int32_t>(reqHeight) != hw_h;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003261
Dan Stozac1879002014-05-22 15:59:05 -07003262 // if a default or invalid sourceCrop is passed in, set reasonable values
3263 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3264 !sourceCrop.isValid()) {
3265 sourceCrop.setLeftTop(Point(0, 0));
3266 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3267 }
3268
3269 // ensure that sourceCrop is inside screen
3270 if (sourceCrop.left < 0) {
3271 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3272 }
Dan Stozabe31f442014-06-11 11:20:54 -07003273 if (sourceCrop.right > hw_w) {
3274 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003275 }
3276 if (sourceCrop.top < 0) {
3277 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3278 }
Dan Stozabe31f442014-06-11 11:20:54 -07003279 if (sourceCrop.bottom > hw_h) {
3280 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003281 }
3282
Mathias Agopian180f10d2013-04-10 22:55:41 -07003283 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003284 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003285
3286 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003287 engine.setViewportAndProjection(
3288 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003289 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003290
3291 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003292 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003293
3294 const LayerVector& layers( mDrawingState.layersSortedByZ );
3295 const size_t count = layers.size();
3296 for (size_t i=0 ; i<count ; ++i) {
3297 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003298 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003299 if (state.layerStack == hw->getLayerStack()) {
3300 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3301 if (layer->isVisible()) {
3302 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08003303 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003304 if (filtering) layer->setFiltering(false);
3305 }
3306 }
3307 }
3308 }
3309
3310 // compositionComplete is needed for older driver
3311 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003312 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003313}
3314
3315
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003316status_t SurfaceFlinger::captureScreenImplLocked(
3317 const sp<const DisplayDevice>& hw,
3318 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003319 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003320 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003321 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003322{
3323 ATRACE_CALL();
3324
Mathias Agopian180f10d2013-04-10 22:55:41 -07003325 // get screen geometry
Dan Stoza35024162015-06-09 16:44:40 -07003326 uint32_t hw_w = hw->getWidth();
3327 uint32_t hw_h = hw->getHeight();
3328
3329 if (rotation & Transform::ROT_90) {
3330 std::swap(hw_w, hw_h);
3331 }
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003332
Mathias Agopian180f10d2013-04-10 22:55:41 -07003333 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3334 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3335 reqWidth, reqHeight, hw_w, hw_h);
3336 return BAD_VALUE;
3337 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003338
Mathias Agopian180f10d2013-04-10 22:55:41 -07003339 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3340 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3341
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003342 // create a surface (because we're a producer, and we need to
3343 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003344 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003345 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003346
Michael Lentine5a16a622015-05-21 13:48:24 -07003347 status_t result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
3348 if (result == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003349 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3350 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003351
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003352 int err = 0;
3353 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003354 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003355 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3356 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003357
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003358 if (err == NO_ERROR) {
3359 ANativeWindowBuffer* buffer;
3360 /* TODO: Once we have the sync framework everywhere this can use
3361 * server-side waits on the fence that dequeueBuffer returns.
3362 */
3363 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3364 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003365 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003366 // create an EGLImage from the buffer so we can later
3367 // turn it into a texture
3368 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3369 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3370 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003371 // this binds the given EGLImage as a framebuffer for the
3372 // duration of this scope.
3373 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3374 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003375 // this will in fact render into our dequeued buffer
3376 // via an FBO, which means we didn't have to create
3377 // an EGLSurface and therefore we're not
3378 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003379 renderScreenImplLocked(
3380 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3381 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003382
Riley Andrews86639902014-08-15 12:27:24 -07003383 // Attempt to create a sync khr object that can produce a sync point. If that
3384 // isn't available, create a non-dupable sync object in the fallback path and
3385 // wait on it directly.
3386 EGLSyncKHR sync;
3387 if (!DEBUG_SCREENSHOTS) {
3388 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Riley Andrews9707f4d2014-10-23 16:17:04 -07003389 // native fence fd will not be populated until flush() is done.
3390 getRenderEngine().flush();
Andy McFadden2d8d1202013-10-09 16:38:02 -07003391 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003392 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003393 }
Riley Andrews86639902014-08-15 12:27:24 -07003394 if (sync != EGL_NO_SYNC_KHR) {
3395 // get the sync fd
3396 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3397 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3398 ALOGW("captureScreen: failed to dup sync khr object");
3399 syncFd = -1;
3400 }
3401 eglDestroySyncKHR(mEGLDisplay, sync);
3402 } else {
3403 // fallback path
3404 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3405 if (sync != EGL_NO_SYNC_KHR) {
3406 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3407 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3408 EGLint eglErr = eglGetError();
3409 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3410 ALOGW("captureScreen: fence wait timed out");
3411 } else {
3412 ALOGW_IF(eglErr != EGL_SUCCESS,
3413 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3414 }
3415 eglDestroySyncKHR(mEGLDisplay, sync);
3416 } else {
3417 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3418 }
3419 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003420 if (DEBUG_SCREENSHOTS) {
3421 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3422 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3423 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3424 hw, minLayerZ, maxLayerZ);
3425 delete [] pixels;
3426 }
3427
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003428 } else {
3429 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3430 result = INVALID_OPERATION;
3431 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003432 // destroy our image
3433 eglDestroyImageKHR(mEGLDisplay, image);
3434 } else {
3435 result = BAD_VALUE;
3436 }
Jesse Hallafe2b1f2014-10-21 11:09:17 -07003437 // queueBuffer takes ownership of syncFd
Michael Lentine5a16a622015-05-21 13:48:24 -07003438 result = window->queueBuffer(window, buffer, syncFd);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003439 }
3440 } else {
3441 result = BAD_VALUE;
3442 }
3443 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3444 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003445
Mathias Agopian74c40c02010-09-29 13:02:36 -07003446 return result;
3447}
3448
Mathias Agopiand5556842013-09-19 17:08:37 -07003449void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3450 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003451 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003452 for (size_t y=0 ; y<h ; y++) {
3453 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3454 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003455 if (p[x] != 0xFF000000) return;
3456 }
3457 }
3458 ALOGE("*** we just took a black screenshot ***\n"
3459 "requested minz=%d, maxz=%d, layerStack=%d",
3460 minLayerZ, maxLayerZ, hw->getLayerStack());
3461 const LayerVector& layers( mDrawingState.layersSortedByZ );
3462 const size_t count = layers.size();
3463 for (size_t i=0 ; i<count ; ++i) {
3464 const sp<Layer>& layer(layers[i]);
3465 const Layer::State& state(layer->getDrawingState());
3466 const bool visible = (state.layerStack == hw->getLayerStack())
3467 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3468 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003469 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003470 visible ? '+' : '-',
3471 i, layer->getName().string(), state.layerStack, state.z,
3472 layer->isVisible(), state.flags, state.alpha);
3473 }
3474 }
3475}
3476
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003477// ---------------------------------------------------------------------------
3478
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003479SurfaceFlinger::LayerVector::LayerVector() {
3480}
3481
3482SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003483 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003484}
3485
3486int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3487 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003488{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003489 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003490 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3491 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003492
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003493 uint32_t ls = l->getCurrentState().layerStack;
3494 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003495 if (ls != rs)
3496 return ls - rs;
3497
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003498 uint32_t lz = l->getCurrentState().z;
3499 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003500 if (lz != rz)
3501 return lz - rz;
3502
3503 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003504}
3505
3506// ---------------------------------------------------------------------------
3507
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003508SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Michael Lentine47e45402014-07-18 15:34:25 -07003509 : type(DisplayDevice::DISPLAY_ID_INVALID), width(0), height(0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003510}
3511
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003512SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Michael Lentine47e45402014-07-18 15:34:25 -07003513 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0), width(0), height(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003514 viewport.makeInvalid();
3515 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003516}
3517
3518// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003519
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003520}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003521
3522
3523#if defined(__gl_h_)
3524#error "don't include gl/gl.h in this file"
3525#endif
3526
3527#if defined(__gl2_h_)
3528#error "don't include gl2/gl2.h in this file"
3529#endif