blob: 4d82c1ca7aeddebf0c279b02492dbac2b1d147eb [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
Dan Stoza9e56aa02015-11-02 13:00:03 -080017// #define LOG_NDEBUG 0
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070021#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <errno.h>
23#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070024#include <dlfcn.h>
Greg Hackmann86efcc02014-03-07 12:44:02 -080025#include <inttypes.h>
Jesse Hallb154c422014-07-13 12:47:02 -070026#include <stdatomic.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070027
28#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029
30#include <cutils/log.h>
31#include <cutils/properties.h>
32
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070033#include <binder/IPCThreadState.h>
34#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070035#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070036#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070037
Mathias Agopianc666cae2012-07-25 18:56:13 -070038#include <ui/DisplayInfo.h>
Lajos Molnar67d8bd62014-09-11 14:58:45 -070039#include <ui/DisplayStatInfo.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070040
Mathias Agopian921e6ac2012-07-23 23:11:29 -070041#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070042#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070043#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070044#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080045#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080046#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070047
48#include <ui/GraphicBufferAllocator.h>
49#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070050#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080051
Mathias Agopiancde87a32012-09-13 14:09:01 -070052#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053#include <utils/String8.h>
54#include <utils/String16.h>
55#include <utils/StopWatch.h>
Yusuke Sato0a688f52015-07-30 23:05:39 -070056#include <utils/Timers.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080057#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
Mathias Agopian921e6ac2012-07-23 23:11:29 -070059#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070060#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
Mathias Agopian3e25fd82013-04-22 17:52:16 +020062#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020064#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080065#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070066#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070067#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070068#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080069#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073
Mathias Agopiana4912602012-07-12 14:25:33 -070074#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070075#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070076#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077
Mathias Agopianff2ed702013-09-01 21:36:12 -070078#include "Effects/Daltonizer.h"
79
Mathias Agopian875d8e12013-06-07 15:35:48 -070080#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070081#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070082
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083#define DISPLAY_COUNT 1
84
Mathias Agopianfee2b462013-07-03 12:34:01 -070085/*
86 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
87 * black pixels.
88 */
89#define DEBUG_SCREENSHOTS false
90
Mathias Agopianca088332013-03-28 17:44:13 -070091EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
92
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070094
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070095// This is the phase offset in nanoseconds of the software vsync event
96// relative to the vsync event reported by HWComposer. The software vsync
97// event is when SurfaceFlinger and Choreographer-based applications run each
98// frame.
99//
100// This phase offset allows adjustment of the minimum latency from application
101// wake-up (by Choregographer) time to the time at which the resulting window
102// image is displayed. This value may be either positive (after the HW vsync)
103// or negative (before the HW vsync). Setting it to 0 will result in a
104// minimum latency of two vsync periods because the app and SurfaceFlinger
105// will run just after the HW vsync. Setting it to a positive number will
106// result in the minimum latency being:
107//
108// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
109//
110// Note that reducing this latency makes it more likely for the applications
111// to not have their window content image ready in time. When this happens
112// the latency will end up being an additional vsync period, and animations
113// will hiccup. Therefore, this latency should be tuned somewhat
114// conservatively (or at least with awareness of the trade-off being made).
115static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
116
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700117// This is the phase offset at which SurfaceFlinger's composition runs.
118static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
119
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120// ---------------------------------------------------------------------------
121
Mathias Agopian99b49842011-06-27 16:05:52 -0700122const String16 sHardwareTest("android.permission.HARDWARE_TEST");
123const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
124const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
125const String16 sDump("android.permission.DUMP");
126
127// ---------------------------------------------------------------------------
128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700130 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700132 mTransactionPending(false),
133 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700134 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700135 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700136 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 mBootTime(systemTime()),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800138 mBuiltinDisplays(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 mVisibleRegionsDirty(false),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800140 mGeometryInvalid(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800141 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700143 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700144 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700145 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700146 mDebugInSwapBuffers(0),
147 mLastSwapBufferTime(0),
148 mDebugInTransaction(0),
149 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700150 mBootFinished(false),
Dan Stozaee44edd2015-03-23 15:50:23 -0700151 mForceFullDamage(false),
Tim Murray4a4e4a22016-04-19 16:29:23 +0000152 mPrimaryDispSync("PrimaryDispSync"),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700153 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700154 mHWVsyncAvailable(false),
Dan Stozab90cf072015-03-05 11:05:59 -0800155 mHasColorMatrix(false),
156 mHasPoweredOff(false),
157 mFrameBuckets(),
158 mTotalTime(0),
159 mLastSwapTime(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160{
Steve Blocka19954a2012-01-04 20:05:49 +0000161 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162
163 // debugging stuff...
164 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700165
Mathias Agopianb4b17302013-03-20 18:36:41 -0700166 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700167 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700168
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 property_get("debug.sf.showupdates", value, "0");
170 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700171
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700172 property_get("debug.sf.ddms", value, "0");
173 mDebugDDMS = atoi(value);
174 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700175 if (!startDdmConnection()) {
176 // start failed, and DDMS debugging not enabled
177 mDebugDDMS = 0;
178 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700179 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700180 ALOGI_IF(mDebugRegion, "showupdates enabled");
181 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
Dan Stozac5da2712016-07-20 15:38:12 -0700182
183 property_get("debug.sf.disable_backpressure", value, "0");
184 mPropagateBackpressure = !atoi(value);
185 ALOGI_IF(!mPropagateBackpressure, "Disabling backpressure propagation");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186}
187
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800188void SurfaceFlinger::onFirstRef()
189{
190 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800191}
192
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193SurfaceFlinger::~SurfaceFlinger()
194{
Mathias Agopiana4912602012-07-12 14:25:33 -0700195 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
196 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
197 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198}
199
Dan Stozac7014012014-02-14 15:03:43 -0800200void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800201{
202 // the window manager died on us. prepare its eulogy.
203
Andy McFadden13a082e2012-08-24 10:16:42 -0700204 // restore initial conditions (default device unblank, etc)
205 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800206
207 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700208 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800209}
210
Mathias Agopian7e27f052010-05-28 14:22:23 -0700211sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212{
Mathias Agopian96f08192010-06-02 23:28:45 -0700213 sp<ISurfaceComposerClient> bclient;
214 sp<Client> client(new Client(this));
215 status_t err = client->initCheck();
216 if (err == NO_ERROR) {
217 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 return bclient;
220}
221
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700222sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
223 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700224{
225 class DisplayToken : public BBinder {
226 sp<SurfaceFlinger> flinger;
227 virtual ~DisplayToken() {
228 // no more references, this display must be terminated
229 Mutex::Autolock _l(flinger->mStateLock);
230 flinger->mCurrentState.displays.removeItem(this);
231 flinger->setTransactionFlags(eDisplayTransactionNeeded);
232 }
233 public:
234 DisplayToken(const sp<SurfaceFlinger>& flinger)
235 : flinger(flinger) {
236 }
237 };
238
239 sp<BBinder> token = new DisplayToken(this);
240
241 Mutex::Autolock _l(mStateLock);
Pablo Ceballos53390e12015-08-04 11:25:59 -0700242 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL, secure);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700243 info.displayName = displayName;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700244 mCurrentState.displays.add(token, info);
245
246 return token;
247}
248
Jesse Hall6c913be2013-08-08 12:15:49 -0700249void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
250 Mutex::Autolock _l(mStateLock);
251
252 ssize_t idx = mCurrentState.displays.indexOfKey(display);
253 if (idx < 0) {
254 ALOGW("destroyDisplay: invalid display token");
255 return;
256 }
257
258 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
259 if (!info.isVirtualDisplay()) {
260 ALOGE("destroyDisplay called for non-virtual display");
261 return;
262 }
263
264 mCurrentState.displays.removeItemsAt(idx);
265 setTransactionFlags(eDisplayTransactionNeeded);
266}
267
Jesse Hall692c7232012-11-08 15:41:56 -0800268void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800269 ALOGV("createBuiltinDisplayLocked(%d)", type);
Jesse Hall692c7232012-11-08 15:41:56 -0800270 ALOGW_IF(mBuiltinDisplays[type],
271 "Overwriting display token for display type %d", type);
272 mBuiltinDisplays[type] = new BBinder();
Jesse Hall692c7232012-11-08 15:41:56 -0800273 // All non-virtual displays are currently considered secure.
Pablo Ceballos53390e12015-08-04 11:25:59 -0700274 DisplayDeviceState info(type, true);
Jesse Hall692c7232012-11-08 15:41:56 -0800275 mCurrentState.displays.add(mBuiltinDisplays[type], info);
276}
277
Mathias Agopiane57f2922012-08-09 16:29:12 -0700278sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700279 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700280 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
281 return NULL;
282 }
Jesse Hall692c7232012-11-08 15:41:56 -0800283 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700284}
285
Jamie Gennis9a78c902011-01-12 18:30:40 -0800286sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
287{
288 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
289 return gba;
290}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700291
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292void SurfaceFlinger::bootFinished()
293{
294 const nsecs_t now = systemTime();
295 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000296 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700297 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700298
299 // wait patiently for the window manager death
300 const String16 name("window");
301 sp<IBinder> window(defaultServiceManager()->getService(name));
302 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700303 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700304 }
305
306 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700307 // formerly we would just kill the process, but we now ask it to exit so it
308 // can choose where to stop the animation.
309 property_set("service.bootanim.exit", "1");
Yusuke Sato0a688f52015-07-30 23:05:39 -0700310
311 const int LOGTAG_SF_STOP_BOOTANIM = 60110;
312 LOG_EVENT_LONG(LOGTAG_SF_STOP_BOOTANIM,
313 ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314}
315
Mathias Agopian3f844832013-08-07 21:24:32 -0700316void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700317 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700318 RenderEngine& engine;
319 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700320 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700321 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
322 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700323 }
324 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700325 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700326 return true;
327 }
328 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700329 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700330}
331
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700332class DispSyncSource : public VSyncSource, private DispSync::Callback {
333public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700334 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
Tim Murray4a4e4a22016-04-19 16:29:23 +0000335 const char* name) :
336 mName(name),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700337 mValue(0),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700338 mTraceVsync(traceVsync),
Tim Murray4a4e4a22016-04-19 16:29:23 +0000339 mVsyncOnLabel(String8::format("VsyncOn-%s", name)),
340 mVsyncEventLabel(String8::format("VSYNC-%s", name)),
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700341 mDispSync(dispSync),
342 mCallbackMutex(),
343 mCallback(),
344 mVsyncMutex(),
345 mPhaseOffset(phaseOffset),
346 mEnabled(false) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700347
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700348 virtual ~DispSyncSource() {}
349
350 virtual void setVSyncEnabled(bool enable) {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700351 Mutex::Autolock lock(mVsyncMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700352 if (enable) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000353 status_t err = mDispSync->addEventListener(mName, mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700354 static_cast<DispSync::Callback*>(this));
355 if (err != NO_ERROR) {
356 ALOGE("error registering vsync callback: %s (%d)",
357 strerror(-err), err);
358 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700359 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700360 } else {
361 status_t err = mDispSync->removeEventListener(
362 static_cast<DispSync::Callback*>(this));
363 if (err != NO_ERROR) {
364 ALOGE("error unregistering vsync callback: %s (%d)",
365 strerror(-err), err);
366 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700367 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700368 }
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700369 mEnabled = enable;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700370 }
371
372 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700373 Mutex::Autolock lock(mCallbackMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700374 mCallback = callback;
375 }
376
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700377 virtual void setPhaseOffset(nsecs_t phaseOffset) {
378 Mutex::Autolock lock(mVsyncMutex);
379
380 // Normalize phaseOffset to [0, period)
381 auto period = mDispSync->getPeriod();
382 phaseOffset %= period;
383 if (phaseOffset < 0) {
384 // If we're here, then phaseOffset is in (-period, 0). After this
385 // operation, it will be in (0, period)
386 phaseOffset += period;
387 }
388 mPhaseOffset = phaseOffset;
389
390 // If we're not enabled, we don't need to mess with the listeners
391 if (!mEnabled) {
392 return;
393 }
394
395 // Remove the listener with the old offset
396 status_t err = mDispSync->removeEventListener(
397 static_cast<DispSync::Callback*>(this));
398 if (err != NO_ERROR) {
399 ALOGE("error unregistering vsync callback: %s (%d)",
400 strerror(-err), err);
401 }
402
403 // Add a listener with the new offset
Tim Murray4a4e4a22016-04-19 16:29:23 +0000404 err = mDispSync->addEventListener(mName, mPhaseOffset,
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700405 static_cast<DispSync::Callback*>(this));
406 if (err != NO_ERROR) {
407 ALOGE("error registering vsync callback: %s (%d)",
408 strerror(-err), err);
409 }
410 }
411
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700412private:
413 virtual void onDispSyncEvent(nsecs_t when) {
414 sp<VSyncSource::Callback> callback;
415 {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700416 Mutex::Autolock lock(mCallbackMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700417 callback = mCallback;
418
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700419 if (mTraceVsync) {
420 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700421 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700422 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700423 }
424
425 if (callback != NULL) {
426 callback->onVSyncEvent(when);
427 }
428 }
429
Tim Murray4a4e4a22016-04-19 16:29:23 +0000430 const char* const mName;
431
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700432 int mValue;
433
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700434 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700435 const String8 mVsyncOnLabel;
436 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700437
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700438 DispSync* mDispSync;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700439
440 Mutex mCallbackMutex; // Protects the following
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700441 sp<VSyncSource::Callback> mCallback;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700442
443 Mutex mVsyncMutex; // Protects the following
444 nsecs_t mPhaseOffset;
445 bool mEnabled;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700446};
447
448void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700449 ALOGI( "SurfaceFlinger's main thread ready to run. "
450 "Initializing graphics H/W...");
451
Dan Stoza9e56aa02015-11-02 13:00:03 -0800452 { // Autolock scope
453 Mutex::Autolock _l(mStateLock);
454
455 // initialize EGL for the default display
456 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
457 eglInitialize(mEGLDisplay, NULL, NULL);
458
459 // start the EventThread
460 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
461 vsyncPhaseOffsetNs, true, "app");
Tim Murray4a4e4a22016-04-19 16:29:23 +0000462 mEventThread = new EventThread(vsyncSrc, *this);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800463 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
464 sfVsyncPhaseOffsetNs, true, "sf");
Tim Murray4a4e4a22016-04-19 16:29:23 +0000465 mSFEventThread = new EventThread(sfVsyncSrc, *this);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800466 mEventQueue.setEventThread(mSFEventThread);
467
Tim Murray41a38532016-06-22 12:42:10 -0700468 // set EventThread and SFEventThread to SCHED_FIFO for minimum jitter
469 struct sched_param param = {0};
470 param.sched_priority = 1;
471 if (sched_setscheduler(mEventThread->getTid(), SCHED_FIFO, &param) != 0) {
472 ALOGE("Couldn't set SCHED_FIFO for EventThread");
473 }
474
475 if (sched_setscheduler(mSFEventThread->getTid(), SCHED_FIFO, &param) != 0) {
476 ALOGE("Couldn't set SCHED_FIFO for SFEventThread");
477 }
478
479
Dan Stoza9e56aa02015-11-02 13:00:03 -0800480 // Get a RenderEngine for the given display / config (can't fail)
481 mRenderEngine = RenderEngine::create(mEGLDisplay,
482 HAL_PIXEL_FORMAT_RGBA_8888);
483 }
484
485 // Drop the state lock while we initialize the hardware composer. We drop
486 // the lock because on creation, it will call back into SurfaceFlinger to
487 // initialize the primary display.
488 mHwc = new HWComposer(this);
489 mHwc->setEventHandler(static_cast<HWComposer::EventHandler*>(this));
490
Jesse Hall692c7232012-11-08 15:41:56 -0800491 Mutex::Autolock _l(mStateLock);
492
Mathias Agopian875d8e12013-06-07 15:35:48 -0700493 // retrieve the EGL context that was selected/created
494 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700495
Mathias Agopianda27af92012-09-13 18:17:13 -0700496 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
497 "couldn't create EGLContext");
498
Dan Stoza9e56aa02015-11-02 13:00:03 -0800499 // make the GLContext current so that we can create textures when creating
500 // Layers (which may happens before we render something)
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700501 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
502
Jamie Gennisd1700752013-10-14 12:22:52 -0700503 mEventControlThread = new EventControlThread(this);
504 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
505
Mathias Agopian92a979a2012-08-02 18:32:23 -0700506 // initialize our drawing state
507 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700508
Andy McFadden13a082e2012-08-24 10:16:42 -0700509 // set initial conditions (e.g. unblank default device)
510 initializeDisplays();
511
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700512 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700513 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514
Dan Stoza9e56aa02015-11-02 13:00:03 -0800515 ALOGV("Done initializing");
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700516}
517
Mathias Agopiana67e4182012-06-19 17:26:12 -0700518void SurfaceFlinger::startBootAnim() {
519 // start boot animation
520 property_set("service.bootanim.exit", "0");
521 property_set("ctl.start", "bootanim");
522}
523
Mathias Agopian875d8e12013-06-07 15:35:48 -0700524size_t SurfaceFlinger::getMaxTextureSize() const {
525 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700526}
527
Mathias Agopian875d8e12013-06-07 15:35:48 -0700528size_t SurfaceFlinger::getMaxViewportDims() const {
529 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700530}
531
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800532// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800533
Jamie Gennis582270d2011-08-17 18:19:00 -0700534bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800535 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800536 Mutex::Autolock _l(mStateLock);
Marco Nelissen097ca272014-11-14 08:01:01 -0800537 sp<IBinder> surfaceTextureBinder(IInterface::asBinder(bufferProducer));
Mathias Agopian67106042013-03-14 19:18:13 -0700538 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800539}
540
Dan Stoza7f7da322014-05-02 15:26:25 -0700541status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
542 Vector<DisplayInfo>* configs) {
Tatenda Chipeperekwa23e16bb2014-10-29 16:47:19 -0700543 if ((configs == NULL) || (display.get() == NULL)) {
Dan Stoza7f7da322014-05-02 15:26:25 -0700544 return BAD_VALUE;
545 }
546
Naseer Ahmed7aa0c472014-11-03 14:49:23 -0500547 if (!display.get())
548 return NAME_NOT_FOUND;
549
Jesse Hall692c7232012-11-08 15:41:56 -0800550 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700551 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800552 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700553 type = i;
554 break;
555 }
556 }
557
558 if (type < 0) {
559 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700560 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700561
Mathias Agopian8b736f12012-08-13 17:54:26 -0700562 // TODO: Not sure if display density should handled by SF any longer
563 class Density {
564 static int getDensityFromProperty(char const* propName) {
565 char property[PROPERTY_VALUE_MAX];
566 int density = 0;
567 if (property_get(propName, property, NULL) > 0) {
568 density = atoi(property);
569 }
570 return density;
571 }
572 public:
573 static int getEmuDensity() {
574 return getDensityFromProperty("qemu.sf.lcd_density"); }
575 static int getBuildDensity() {
576 return getDensityFromProperty("ro.sf.lcd_density"); }
577 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700578
Dan Stoza7f7da322014-05-02 15:26:25 -0700579 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700580
Dan Stoza9e56aa02015-11-02 13:00:03 -0800581 for (const auto& hwConfig : getHwComposer().getConfigs(type)) {
Dan Stoza7f7da322014-05-02 15:26:25 -0700582 DisplayInfo info = DisplayInfo();
583
Dan Stoza9e56aa02015-11-02 13:00:03 -0800584 float xdpi = hwConfig->getDpiX();
585 float ydpi = hwConfig->getDpiY();
Dan Stoza7f7da322014-05-02 15:26:25 -0700586
587 if (type == DisplayDevice::DISPLAY_PRIMARY) {
588 // The density of the device is provided by a build property
589 float density = Density::getBuildDensity() / 160.0f;
590 if (density == 0) {
591 // the build doesn't provide a density -- this is wrong!
592 // use xdpi instead
593 ALOGE("ro.sf.lcd_density must be defined as a build property");
594 density = xdpi / 160.0f;
595 }
596 if (Density::getEmuDensity()) {
597 // if "qemu.sf.lcd_density" is specified, it overrides everything
598 xdpi = ydpi = density = Density::getEmuDensity();
599 density /= 160.0f;
600 }
601 info.density = density;
602
603 // TODO: this needs to go away (currently needed only by webkit)
604 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
605 info.orientation = hw->getOrientation();
606 } else {
607 // TODO: where should this value come from?
608 static const int TV_DENSITY = 213;
609 info.density = TV_DENSITY / 160.0f;
610 info.orientation = 0;
611 }
612
Dan Stoza9e56aa02015-11-02 13:00:03 -0800613 info.w = hwConfig->getWidth();
614 info.h = hwConfig->getHeight();
Dan Stoza7f7da322014-05-02 15:26:25 -0700615 info.xdpi = xdpi;
616 info.ydpi = ydpi;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800617 info.fps = 1e9 / hwConfig->getVsyncPeriod();
Andy McFadden91b2ca82014-06-13 14:04:23 -0700618 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800619
Andy McFadden91b2ca82014-06-13 14:04:23 -0700620 // This is how far in advance a buffer must be queued for
621 // presentation at a given time. If you want a buffer to appear
622 // on the screen at time N, you must submit the buffer before
623 // (N - presentationDeadline).
624 //
625 // Normally it's one full refresh period (to give SF a chance to
626 // latch the buffer), but this can be reduced by configuring a
627 // DispSync offset. Any additional delays introduced by the hardware
628 // composer or panel must be accounted for here.
629 //
630 // We add an additional 1ms to allow for processing time and
631 // differences between the ideal and actual refresh rate.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800632 info.presentationDeadline = hwConfig->getVsyncPeriod() -
633 SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700634
635 // All non-virtual displays are currently considered secure.
636 info.secure = true;
637
Michael Wright28f24d02016-07-12 13:30:53 -0700638 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700639 }
640
Dan Stoza7f7da322014-05-02 15:26:25 -0700641 return NO_ERROR;
642}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700643
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800644status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& /* display */,
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700645 DisplayStatInfo* stats) {
646 if (stats == NULL) {
647 return BAD_VALUE;
648 }
649
650 // FIXME for now we always return stats for the primary display
651 memset(stats, 0, sizeof(*stats));
652 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
653 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
654 return NO_ERROR;
655}
656
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700657int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
Dan Stoza24a42e92015-03-09 10:04:11 -0700658 sp<DisplayDevice> device(getDisplayDevice(display));
659 if (device != NULL) {
660 return device->getActiveConfig();
661 }
662 return BAD_VALUE;
Dan Stoza7f7da322014-05-02 15:26:25 -0700663}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700664
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700665void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
666 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
667 this);
668 int32_t type = hw->getDisplayType();
669 int currentMode = hw->getActiveConfig();
670
671 if (mode == currentMode) {
672 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
673 return;
674 }
675
676 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
677 ALOGW("Trying to set config for virtual display");
678 return;
679 }
680
681 hw->setActiveConfig(mode);
682 getHwComposer().setActiveConfig(type, mode);
683}
684
685status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
686 class MessageSetActiveConfig: public MessageBase {
687 SurfaceFlinger& mFlinger;
688 sp<IBinder> mDisplay;
689 int mMode;
690 public:
691 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
692 int mode) :
693 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
694 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700695 Vector<DisplayInfo> configs;
696 mFlinger.getDisplayConfigs(mDisplay, &configs);
Jesse Hall78442112014-08-07 22:43:06 -0700697 if (mMode < 0 || mMode >= static_cast<int>(configs.size())) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700698 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700699 mMode, configs.size());
Michael Wright28f24d02016-07-12 13:30:53 -0700700 return true;
Michael Lentine7306c672014-07-30 13:00:37 -0700701 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700702 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
703 if (hw == NULL) {
704 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700705 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700706 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
707 ALOGW("Attempt to set active config = %d for virtual display",
708 mMode);
709 } else {
710 mFlinger.setActiveConfigInternal(hw, mMode);
711 }
712 return true;
713 }
714 };
715 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
716 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700717 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700718}
Michael Wright28f24d02016-07-12 13:30:53 -0700719status_t SurfaceFlinger::getDisplayColorModes(const sp<IBinder>& display,
720 Vector<android_color_mode_t>* outColorModes) {
721 if ((outColorModes == nullptr) || (display.get() == nullptr)) {
722 return BAD_VALUE;
723 }
724
725 if (!display.get()) {
726 return NAME_NOT_FOUND;
727 }
728
729 int32_t type = NAME_NOT_FOUND;
730 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
731 if (display == mBuiltinDisplays[i]) {
732 type = i;
733 break;
734 }
735 }
736
737 if (type < 0) {
738 return type;
739 }
740
741 std::vector<android_color_mode_t> modes = getHwComposer().getColorModes(type);
742 outColorModes->clear();
743 std::copy(modes.cbegin(), modes.cend(), std::back_inserter(*outColorModes));
744
745 return NO_ERROR;
746}
747
748android_color_mode_t SurfaceFlinger::getActiveColorMode(const sp<IBinder>& display) {
749 sp<DisplayDevice> device(getDisplayDevice(display));
750 if (device != nullptr) {
751 return device->getActiveColorMode();
752 }
753 return static_cast<android_color_mode_t>(BAD_VALUE);
754}
755
756void SurfaceFlinger::setActiveColorModeInternal(const sp<DisplayDevice>& hw,
757 android_color_mode_t mode) {
758 ALOGD("Set active color mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
759 this);
760 int32_t type = hw->getDisplayType();
761 android_color_mode_t currentMode = hw->getActiveColorMode();
762
763 if (mode == currentMode) {
764 ALOGD("Screen type=%d is already in color mode=%d", hw->getDisplayType(), mode);
765 return;
766 }
767
768 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
769 ALOGW("Trying to set config for virtual display");
770 return;
771 }
772
773 hw->setActiveColorMode(mode);
774 getHwComposer().setActiveColorMode(type, mode);
775}
776
777
778status_t SurfaceFlinger::setActiveColorMode(const sp<IBinder>& display,
779 android_color_mode_t colorMode) {
780 class MessageSetActiveColorMode: public MessageBase {
781 SurfaceFlinger& mFlinger;
782 sp<IBinder> mDisplay;
783 android_color_mode_t mMode;
784 public:
785 MessageSetActiveColorMode(SurfaceFlinger& flinger, const sp<IBinder>& disp,
786 android_color_mode_t mode) :
787 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
788 virtual bool handler() {
789 Vector<android_color_mode_t> modes;
790 mFlinger.getDisplayColorModes(mDisplay, &modes);
791 bool exists = std::find(std::begin(modes), std::end(modes), mMode) != std::end(modes);
792 if (mMode < 0 || !exists) {
793 ALOGE("Attempt to set invalid active color mode = %d for display %p", mMode,
794 mDisplay.get());
795 return true;
796 }
797 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
798 if (hw == nullptr) {
799 ALOGE("Attempt to set active color mode = %d for null display %p",
800 mMode, mDisplay.get());
801 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
802 ALOGW("Attempt to set active color mode= %d for virtual display",
803 mMode);
804 } else {
805 mFlinger.setActiveColorModeInternal(hw, mMode);
806 }
807 return true;
808 }
809 };
810 sp<MessageBase> msg = new MessageSetActiveColorMode(*this, display, colorMode);
811 postMessageSync(msg);
812 return NO_ERROR;
813}
Mathias Agopianc666cae2012-07-25 18:56:13 -0700814
Svetoslavd85084b2014-03-20 10:28:31 -0700815status_t SurfaceFlinger::clearAnimationFrameStats() {
816 Mutex::Autolock _l(mStateLock);
817 mAnimFrameTracker.clearStats();
818 return NO_ERROR;
819}
820
821status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
822 Mutex::Autolock _l(mStateLock);
823 mAnimFrameTracker.getStats(outStats);
824 return NO_ERROR;
825}
826
Dan Stozac4f471e2016-03-24 09:31:08 -0700827status_t SurfaceFlinger::getHdrCapabilities(const sp<IBinder>& display,
828 HdrCapabilities* outCapabilities) const {
829 Mutex::Autolock _l(mStateLock);
830
831 sp<const DisplayDevice> displayDevice(getDisplayDevice(display));
832 if (displayDevice == nullptr) {
833 ALOGE("getHdrCapabilities: Invalid display %p", displayDevice.get());
834 return BAD_VALUE;
835 }
836
837 std::unique_ptr<HdrCapabilities> capabilities =
838 mHwc->getHdrCapabilities(displayDevice->getHwcDisplayId());
839 if (capabilities) {
840 std::swap(*outCapabilities, *capabilities);
841 } else {
842 return BAD_VALUE;
843 }
844
845 return NO_ERROR;
846}
847
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800848// ----------------------------------------------------------------------------
849
850sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800851 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700852}
853
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800854// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800855
856void SurfaceFlinger::waitForEvent() {
857 mEventQueue.waitMessage();
858}
859
860void SurfaceFlinger::signalTransaction() {
861 mEventQueue.invalidate();
862}
863
864void SurfaceFlinger::signalLayerUpdate() {
865 mEventQueue.invalidate();
866}
867
868void SurfaceFlinger::signalRefresh() {
869 mEventQueue.refresh();
870}
871
872status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800873 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800874 return mEventQueue.postMessage(msg, reltime);
875}
876
877status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800878 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800879 status_t res = mEventQueue.postMessage(msg, reltime);
880 if (res == NO_ERROR) {
881 msg->wait();
882 }
883 return res;
884}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800885
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700886void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700887 do {
888 waitForEvent();
889 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800890}
891
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700892void SurfaceFlinger::enableHardwareVsync() {
893 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700894 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700895 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700896 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
897 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700898 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700899 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700900}
901
Jesse Hall948fe0c2013-10-14 12:56:09 -0700902void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700903 Mutex::Autolock _l(mHWVsyncLock);
904
Jesse Hall948fe0c2013-10-14 12:56:09 -0700905 if (makeAvailable) {
906 mHWVsyncAvailable = true;
907 } else if (!mHWVsyncAvailable) {
Dan Stoza0a3c4d62016-04-19 11:56:20 -0700908 // Hardware vsync is not currently available, so abort the resync
909 // attempt for now
Jesse Hall948fe0c2013-10-14 12:56:09 -0700910 return;
911 }
912
Dan Stoza9e56aa02015-11-02 13:00:03 -0800913 const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
914 const nsecs_t period = activeConfig->getVsyncPeriod();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700915
916 mPrimaryDispSync.reset();
917 mPrimaryDispSync.setPeriod(period);
918
919 if (!mPrimaryHWVsyncEnabled) {
920 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700921 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
922 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700923 mPrimaryHWVsyncEnabled = true;
924 }
925}
926
Jesse Hall948fe0c2013-10-14 12:56:09 -0700927void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700928 Mutex::Autolock _l(mHWVsyncLock);
929 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700930 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
931 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700932 mPrimaryDispSync.endResync();
933 mPrimaryHWVsyncEnabled = false;
934 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700935 if (makeUnavailable) {
936 mHWVsyncAvailable = false;
937 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700938}
939
Tim Murray4a4e4a22016-04-19 16:29:23 +0000940void SurfaceFlinger::resyncWithRateLimit() {
941 static constexpr nsecs_t kIgnoreDelay = ms2ns(500);
942 if (systemTime() - mLastSwapTime > kIgnoreDelay) {
Dan Stoza0a3c4d62016-04-19 11:56:20 -0700943 resyncToHardwareVsync(false);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000944 }
945}
946
Dan Stoza9e56aa02015-11-02 13:00:03 -0800947void SurfaceFlinger::onVSyncReceived(int32_t type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700948 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700949
Jamie Gennisd1700752013-10-14 12:22:52 -0700950 { // Scope for the lock
951 Mutex::Autolock _l(mHWVsyncLock);
952 if (type == 0 && mPrimaryHWVsyncEnabled) {
953 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700954 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700955 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700956
957 if (needsHwVsync) {
958 enableHardwareVsync();
959 } else {
960 disableHardwareVsync(false);
961 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700962}
963
Dan Stoza9e56aa02015-11-02 13:00:03 -0800964void SurfaceFlinger::onHotplugReceived(int32_t disp, bool connected) {
965 ALOGV("onHotplugReceived(%d, %s)", disp, connected ? "true" : "false");
966 if (disp == DisplayDevice::DISPLAY_PRIMARY) {
967 Mutex::Autolock lock(mStateLock);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700968
Dan Stoza9e56aa02015-11-02 13:00:03 -0800969 // All non-virtual displays are currently considered secure.
970 bool isSecure = true;
971
972 int32_t type = DisplayDevice::DISPLAY_PRIMARY;
973 createBuiltinDisplayLocked(DisplayDevice::DISPLAY_PRIMARY);
974 wp<IBinder> token = mBuiltinDisplays[type];
975
976 sp<IGraphicBufferProducer> producer;
977 sp<IGraphicBufferConsumer> consumer;
978 BufferQueue::createBufferQueue(&producer, &consumer,
979 new GraphicBufferAlloc());
980
981 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc,
982 DisplayDevice::DISPLAY_PRIMARY, consumer);
983 sp<DisplayDevice> hw = new DisplayDevice(this,
984 DisplayDevice::DISPLAY_PRIMARY, disp, isSecure, token, fbs,
985 producer, mRenderEngine->getEGLConfig());
986 mDisplays.add(token, hw);
987 } else {
988 auto type = DisplayDevice::DISPLAY_EXTERNAL;
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700989 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800990 if (connected) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800991 createBuiltinDisplayLocked(type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700992 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800993 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
994 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700995 }
996 setTransactionFlags(eDisplayTransactionNeeded);
997
Andy McFadden9e9689c2012-10-10 18:17:51 -0700998 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700999 }
Mathias Agopian86303202012-07-24 22:46:10 -07001000}
1001
Dan Stoza9e56aa02015-11-02 13:00:03 -08001002void SurfaceFlinger::setVsyncEnabled(int disp, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001003 ATRACE_CALL();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001004 getHwComposer().setVsyncEnabled(disp,
1005 enabled ? HWC2::Vsync::Enable : HWC2::Vsync::Disable);
Mathias Agopian86303202012-07-24 22:46:10 -07001006}
1007
Mathias Agopian4fec8732012-06-29 14:12:52 -07001008void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001009 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001010 switch (what) {
Dan Stoza6b9454d2014-11-07 16:00:59 -08001011 case MessageQueue::INVALIDATE: {
Dan Stoza50182882016-07-08 12:02:20 -07001012 bool frameMissed = !mHadClientComposition &&
1013 mPreviousPresentFence != Fence::NO_FENCE &&
1014 mPreviousPresentFence->getSignalTime() == INT64_MAX;
1015 ATRACE_INT("FrameMissed", static_cast<int>(frameMissed));
Dan Stozac5da2712016-07-20 15:38:12 -07001016 if (mPropagateBackpressure && frameMissed) {
Dan Stoza50182882016-07-08 12:02:20 -07001017 signalLayerUpdate();
1018 break;
1019 }
1020
Dan Stoza6b9454d2014-11-07 16:00:59 -08001021 bool refreshNeeded = handleMessageTransaction();
1022 refreshNeeded |= handleMessageInvalidate();
Dan Stoza58784442014-12-02 16:58:17 -08001023 refreshNeeded |= mRepaintEverything;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001024 if (refreshNeeded) {
Dan Stoza58784442014-12-02 16:58:17 -08001025 // Signal a refresh if a transaction modified the window state,
1026 // a new buffer was latched, or if HWC has requested a full
1027 // repaint
Dan Stoza6b9454d2014-11-07 16:00:59 -08001028 signalRefresh();
1029 }
1030 break;
1031 }
1032 case MessageQueue::REFRESH: {
1033 handleMessageRefresh();
1034 break;
1035 }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001036 }
1037}
1038
Dan Stoza6b9454d2014-11-07 16:00:59 -08001039bool SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001040 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001041 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -07001042 handleTransaction(transactionFlags);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001043 return true;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001044 }
Dan Stoza6b9454d2014-11-07 16:00:59 -08001045 return false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001046}
1047
Dan Stoza6b9454d2014-11-07 16:00:59 -08001048bool SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001049 ATRACE_CALL();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001050 return handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001051}
1052
1053void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001054 ATRACE_CALL();
Dan Stoza14cd37c2015-07-09 12:43:33 -07001055
Pablo Ceballos40845df2016-01-25 17:41:15 -08001056 nsecs_t refreshStartTime = systemTime(SYSTEM_TIME_MONOTONIC);
Dan Stoza14cd37c2015-07-09 12:43:33 -07001057
Dan Stoza05dacfb2016-07-01 13:33:38 -07001058 preComposition();
1059 rebuildLayerStacks();
1060 setUpHWComposer();
1061 doDebugFlashRegions();
1062 doComposition();
1063 postComposition(refreshStartTime);
1064
1065 mPreviousPresentFence = mHwc->getRetireFence(HWC_DISPLAY_PRIMARY);
1066 mHadClientComposition = mHwc->hasClientComposition(HWC_DISPLAY_PRIMARY);
Dan Stoza14cd37c2015-07-09 12:43:33 -07001067
Dan Stoza9e56aa02015-11-02 13:00:03 -08001068 // Release any buffers which were replaced this frame
1069 for (auto& layer : mLayersWithQueuedFrames) {
1070 layer->releasePendingBuffer();
1071 }
1072 mLayersWithQueuedFrames.clear();
Mathias Agopiancd60f992012-08-16 16:28:27 -07001073}
Mathias Agopian4fec8732012-06-29 14:12:52 -07001074
Mathias Agopiancd60f992012-08-16 16:28:27 -07001075void SurfaceFlinger::doDebugFlashRegions()
1076{
1077 // is debugging enabled
1078 if (CC_LIKELY(!mDebugRegion))
1079 return;
1080
1081 const bool repaintEverything = mRepaintEverything;
1082 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1083 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001084 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001085 // transform the dirty region into this screen's coordinate space
1086 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
1087 if (!dirtyRegion.isEmpty()) {
1088 // redraw the whole screen
1089 doComposeSurfaces(hw, Region(hw->bounds()));
1090
1091 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -07001092 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001093 RenderEngine& engine(getRenderEngine());
1094 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
1095
Mathias Agopianda27af92012-09-13 18:17:13 -07001096 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -07001097 }
1098 }
1099 }
1100
1101 postFramebuffer();
1102
1103 if (mDebugRegion > 1) {
1104 usleep(mDebugRegion * 1000);
1105 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -07001106
Dan Stoza9e56aa02015-11-02 13:00:03 -08001107 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
Dan Stoza7bdf55a2016-06-17 11:29:01 -07001108 auto& displayDevice = mDisplays[displayId];
1109 if (!displayDevice->isDisplayOn()) {
1110 continue;
1111 }
1112
1113 status_t result = displayDevice->prepareFrame(*mHwc);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001114 ALOGE_IF(result != NO_ERROR, "prepareFrame for display %zd failed:"
1115 " %d (%s)", displayId, result, strerror(-result));
Mathias Agopianbb53b0e2012-09-24 21:27:29 -07001116 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001117}
1118
1119void SurfaceFlinger::preComposition()
1120{
Dan Stoza9e56aa02015-11-02 13:00:03 -08001121 ATRACE_CALL();
1122 ALOGV("preComposition");
1123
Mathias Agopiancd60f992012-08-16 16:28:27 -07001124 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001125 const LayerVector& layers(mDrawingState.layersSortedByZ);
1126 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -07001127 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001128 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001129 needExtraInvalidate = true;
1130 }
1131 }
1132 if (needExtraInvalidate) {
1133 signalLayerUpdate();
1134 }
1135}
1136
Pablo Ceballos40845df2016-01-25 17:41:15 -08001137void SurfaceFlinger::postComposition(nsecs_t refreshStartTime)
Mathias Agopiancd60f992012-08-16 16:28:27 -07001138{
Dan Stoza9e56aa02015-11-02 13:00:03 -08001139 ATRACE_CALL();
1140 ALOGV("postComposition");
1141
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001142 const LayerVector& layers(mDrawingState.layersSortedByZ);
1143 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -07001144 for (size_t i=0 ; i<count ; i++) {
Dan Stozae77c7662016-05-13 11:37:28 -07001145 bool frameLatched = layers[i]->onPostComposition();
1146 if (frameLatched) {
1147 recordBufferingStats(layers[i]->getName().string(),
1148 layers[i]->getOccupancyHistory(false));
1149 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001150 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001151
Dan Stoza9e56aa02015-11-02 13:00:03 -08001152 sp<Fence> presentFence = mHwc->getRetireFence(HWC_DISPLAY_PRIMARY);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001153
1154 if (presentFence->isValid()) {
1155 if (mPrimaryDispSync.addPresentFence(presentFence)) {
1156 enableHardwareVsync();
1157 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -07001158 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001159 }
1160 }
1161
Dan Stozab90cf072015-03-05 11:05:59 -08001162 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Andy McFadden5167ec62014-05-22 13:08:43 -07001163 if (kIgnorePresentFences) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001164 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001165 enableHardwareVsync();
1166 }
1167 }
1168
Pablo Ceballos40845df2016-01-25 17:41:15 -08001169 mFenceTracker.addFrame(refreshStartTime, presentFence,
1170 hw->getVisibleLayersSortedByZ(), hw->getClientTargetAcquireFence());
1171
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001172 if (mAnimCompositionPending) {
1173 mAnimCompositionPending = false;
1174
Jesse Halla9a1b002013-02-27 16:39:25 -08001175 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001176 mAnimFrameTracker.setActualPresentFence(presentFence);
1177 } else {
1178 // The HWC doesn't support present fences, so use the refresh
1179 // timestamp instead.
Dan Stoza9e56aa02015-11-02 13:00:03 -08001180 nsecs_t presentTime =
1181 mHwc->getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001182 mAnimFrameTracker.setActualPresentTime(presentTime);
1183 }
1184 mAnimFrameTracker.advanceFrame();
1185 }
Dan Stozab90cf072015-03-05 11:05:59 -08001186
1187 if (hw->getPowerMode() == HWC_POWER_MODE_OFF) {
1188 return;
1189 }
1190
1191 nsecs_t currentTime = systemTime();
1192 if (mHasPoweredOff) {
1193 mHasPoweredOff = false;
1194 } else {
1195 nsecs_t period = mPrimaryDispSync.getPeriod();
1196 nsecs_t elapsedTime = currentTime - mLastSwapTime;
1197 size_t numPeriods = static_cast<size_t>(elapsedTime / period);
1198 if (numPeriods < NUM_BUCKETS - 1) {
1199 mFrameBuckets[numPeriods] += elapsedTime;
1200 } else {
1201 mFrameBuckets[NUM_BUCKETS - 1] += elapsedTime;
1202 }
1203 mTotalTime += elapsedTime;
1204 }
1205 mLastSwapTime = currentTime;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001206}
1207
1208void SurfaceFlinger::rebuildLayerStacks() {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001209 ATRACE_CALL();
1210 ALOGV("rebuildLayerStacks");
1211
Mathias Agopiancd60f992012-08-16 16:28:27 -07001212 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001213 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001214 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -07001215 mVisibleRegionsDirty = false;
1216 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001217
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001218 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001219 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001220 Region opaqueRegion;
1221 Region dirtyRegion;
Dan Stoza9e56aa02015-11-02 13:00:03 -08001222 Vector<sp<Layer>> layersSortedByZ;
1223 const sp<DisplayDevice>& displayDevice(mDisplays[dpy]);
1224 const Transform& tr(displayDevice->getTransform());
1225 const Rect bounds(displayDevice->getBounds());
1226 if (displayDevice->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001227 SurfaceFlinger::computeVisibleRegions(layers,
Dan Stoza9e56aa02015-11-02 13:00:03 -08001228 displayDevice->getLayerStack(), dirtyRegion,
1229 opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001230
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001231 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001232 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001233 const sp<Layer>& layer(layers[i]);
1234 const Layer::State& s(layer->getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -08001235 if (s.layerStack == displayDevice->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -07001236 Region drawRegion(tr.transform(
1237 layer->visibleNonTransparentRegion));
1238 drawRegion.andSelf(bounds);
1239 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001240 layersSortedByZ.add(layer);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001241 } else {
1242 // Clear out the HWC layer if this layer was
1243 // previously visible, but no longer is
1244 layer->setHwcLayer(displayDevice->getHwcDisplayId(),
1245 nullptr);
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001246 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001247 }
1248 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001249 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001250 displayDevice->setVisibleLayersSortedByZ(layersSortedByZ);
1251 displayDevice->undefinedRegion.set(bounds);
1252 displayDevice->undefinedRegion.subtractSelf(
1253 tr.transform(opaqueRegion));
1254 displayDevice->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001255 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001256 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001257}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001258
Mathias Agopiancd60f992012-08-16 16:28:27 -07001259void SurfaceFlinger::setUpHWComposer() {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001260 ATRACE_CALL();
1261 ALOGV("setUpHWComposer");
1262
Jesse Hall028dc8f2013-08-20 16:35:32 -07001263 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001264 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1265 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1266 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1267
1268 // If nothing has changed (!dirty), don't recompose.
1269 // If something changed, but we don't currently have any visible layers,
1270 // and didn't when we last did a composition, then skip it this time.
1271 // The second rule does two things:
1272 // - When all layers are removed from a display, we'll emit one black
1273 // frame, then nothing more until we get new layers.
1274 // - When a display is created with a private layer stack, we won't
1275 // emit any black frames until a layer is added to the layer stack.
1276 bool mustRecompose = dirty && !(empty && wasEmpty);
1277
1278 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1279 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1280 mustRecompose ? "doing" : "skipping",
1281 dirty ? "+" : "-",
1282 empty ? "+" : "-",
1283 wasEmpty ? "+" : "-");
1284
Dan Stoza71433162014-02-04 16:22:36 -08001285 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001286
1287 if (mustRecompose) {
1288 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1289 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001290 }
1291
Dan Stoza9e56aa02015-11-02 13:00:03 -08001292 // build the h/w work list
1293 if (CC_UNLIKELY(mGeometryInvalid)) {
1294 mGeometryInvalid = false;
1295 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1296 sp<const DisplayDevice> displayDevice(mDisplays[dpy]);
1297 const auto hwcId = displayDevice->getHwcDisplayId();
1298 if (hwcId >= 0) {
1299 const Vector<sp<Layer>>& currentLayers(
1300 displayDevice->getVisibleLayersSortedByZ());
1301 bool foundLayerWithoutHwc = false;
1302 for (auto& layer : currentLayers) {
1303 if (!layer->hasHwcLayer(hwcId)) {
1304 auto hwcLayer = mHwc->createLayer(hwcId);
1305 if (hwcLayer) {
1306 layer->setHwcLayer(hwcId, std::move(hwcLayer));
1307 } else {
1308 layer->forceClientComposition(hwcId);
1309 foundLayerWithoutHwc = true;
1310 continue;
Jamie Gennisa4310c82012-09-25 20:26:00 -07001311 }
1312 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001313
Dan Stoza9e56aa02015-11-02 13:00:03 -08001314 layer->setGeometry(displayDevice);
Dan Stoza9f26a9c2016-06-22 14:51:09 -07001315 if (mDebugDisableHWC || mDebugRegion) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001316 layer->forceClientComposition(hwcId);
Riley Andrews03414a12014-07-01 14:22:59 -07001317 }
1318 }
1319 }
1320 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001321 }
Riley Andrews03414a12014-07-01 14:22:59 -07001322
Dan Stoza9f26a9c2016-06-22 14:51:09 -07001323
1324 mat4 colorMatrix = mColorMatrix * mDaltonizer();
1325
Dan Stoza9e56aa02015-11-02 13:00:03 -08001326 // Set the per-frame data
1327 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
1328 auto& displayDevice = mDisplays[displayId];
1329 const auto hwcId = displayDevice->getHwcDisplayId();
1330 if (hwcId < 0) {
1331 continue;
Jesse Hall38efe862013-04-06 23:12:29 -07001332 }
Dan Stoza9f26a9c2016-06-22 14:51:09 -07001333 if (colorMatrix != mPreviousColorMatrix) {
1334 status_t result = mHwc->setColorTransform(hwcId, colorMatrix);
1335 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on "
1336 "display %zd: %d", displayId, result);
1337 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001338 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
1339 layer->setPerFrameData(displayDevice);
1340 }
1341 }
1342
Dan Stoza9f26a9c2016-06-22 14:51:09 -07001343 mPreviousColorMatrix = colorMatrix;
1344
Dan Stoza9e56aa02015-11-02 13:00:03 -08001345 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
Dan Stoza7bdf55a2016-06-17 11:29:01 -07001346 auto& displayDevice = mDisplays[displayId];
1347 if (!displayDevice->isDisplayOn()) {
1348 continue;
1349 }
1350
1351 status_t result = displayDevice->prepareFrame(*mHwc);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001352 ALOGE_IF(result != NO_ERROR, "prepareFrame for display %zd failed:"
1353 " %d (%s)", displayId, result, strerror(-result));
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001354 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001355}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001356
Mathias Agopiancd60f992012-08-16 16:28:27 -07001357void SurfaceFlinger::doComposition() {
1358 ATRACE_CALL();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001359 ALOGV("doComposition");
1360
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001361 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001362 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001363 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001364 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001365 // transform the dirty region into this screen's coordinate space
1366 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001367
1368 // repaint the framebuffer (if needed)
1369 doDisplayComposition(hw, dirtyRegion);
1370
Mathias Agopiancd60f992012-08-16 16:28:27 -07001371 hw->dirtyRegion.clear();
1372 hw->flip(hw->swapRegion);
1373 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001374 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001375 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001376 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001377}
1378
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001379void SurfaceFlinger::postFramebuffer()
1380{
Mathias Agopian841cde52012-03-01 15:44:37 -08001381 ATRACE_CALL();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001382 ALOGV("postFramebuffer");
Mathias Agopianb048cef2012-02-04 15:44:04 -08001383
Mathias Agopiana44b0412011-10-16 18:46:35 -07001384 const nsecs_t now = systemTime();
1385 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001386
Dan Stoza9e56aa02015-11-02 13:00:03 -08001387 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
1388 auto& displayDevice = mDisplays[displayId];
Dan Stoza7bdf55a2016-06-17 11:29:01 -07001389 if (!displayDevice->isDisplayOn()) {
1390 continue;
1391 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001392 const auto hwcId = displayDevice->getHwcDisplayId();
1393 if (hwcId >= 0) {
1394 mHwc->commit(hwcId);
Mathias Agopian2a231842012-09-24 18:12:35 -07001395 }
Dan Stoza2dc3be82016-04-06 14:05:37 -07001396 displayDevice->onSwapBuffersCompleted();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001397 if (displayId == 0) {
1398 // Make the default display current because the VirtualDisplayDevice
1399 // code cannot deal with dequeueBuffer() being called outside of the
1400 // composition loop; however the code below can call glFlush() which
1401 // is allowed to (and does in some case) call dequeueBuffer().
1402 displayDevice->makeCurrent(mEGLDisplay, mEGLContext);
1403 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001404 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
1405 sp<Fence> releaseFence = Fence::NO_FENCE;
1406 if (layer->getCompositionType(hwcId) == HWC2::Composition::Client) {
1407 releaseFence = displayDevice->getClientTargetAcquireFence();
1408 } else {
1409 auto hwcLayer = layer->getHwcLayer(hwcId);
1410 releaseFence = mHwc->getLayerReleaseFence(hwcId, hwcLayer);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001411 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001412 layer->onLayerDisplayed(releaseFence);
1413 }
1414 if (hwcId >= 0) {
1415 mHwc->clearReleaseFences(hwcId);
Jesse Hallef194142012-06-14 14:45:17 -07001416 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001417 }
1418
Mathias Agopiana44b0412011-10-16 18:46:35 -07001419 mLastSwapBufferTime = systemTime() - now;
1420 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001421
1422 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1423 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1424 logFrameStats();
1425 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426}
1427
Mathias Agopian87baae12012-07-31 12:38:26 -07001428void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001429{
Mathias Agopian841cde52012-03-01 15:44:37 -08001430 ATRACE_CALL();
1431
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001432 // here we keep a copy of the drawing state (that is the state that's
1433 // going to be overwritten by handleTransactionLocked()) outside of
1434 // mStateLock so that the side-effects of the State assignment
1435 // don't happen with mStateLock held (which can cause deadlocks).
1436 State drawingState(mDrawingState);
1437
Mathias Agopianca4d3602011-05-19 15:38:14 -07001438 Mutex::Autolock _l(mStateLock);
1439 const nsecs_t now = systemTime();
1440 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001441
Mathias Agopianca4d3602011-05-19 15:38:14 -07001442 // Here we're guaranteed that some transaction flags are set
1443 // so we can call handleTransactionLocked() unconditionally.
1444 // We call getTransactionFlags(), which will also clear the flags,
1445 // with mStateLock held to guarantee that mCurrentState won't change
1446 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001447
Mathias Agopiane57f2922012-08-09 16:29:12 -07001448 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001449 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001450
Mathias Agopianca4d3602011-05-19 15:38:14 -07001451 mLastTransactionTime = systemTime() - now;
1452 mDebugInTransaction = 0;
1453 invalidateHwcGeometry();
1454 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001455}
1456
Mathias Agopian87baae12012-07-31 12:38:26 -07001457void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001458{
1459 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001460 const size_t count = currentLayers.size();
1461
Dan Stoza7dde5992015-05-22 09:51:44 -07001462 // Notify all layers of available frames
1463 for (size_t i = 0; i < count; ++i) {
1464 currentLayers[i]->notifyAvailableFrames();
1465 }
1466
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467 /*
1468 * Traversal of the children
1469 * (perform the transaction for each of them if needed)
1470 */
1471
Mathias Agopian3559b072012-08-15 13:46:03 -07001472 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001473 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001474 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001475 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1476 if (!trFlags) continue;
1477
1478 const uint32_t flags = layer->doTransaction(0);
1479 if (flags & Layer::eVisibleRegion)
1480 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481 }
1482 }
1483
1484 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001485 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486 */
1487
Mathias Agopiane57f2922012-08-09 16:29:12 -07001488 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001489 // here we take advantage of Vector's copy-on-write semantics to
1490 // improve performance by skipping the transaction entirely when
1491 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001492 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1493 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001494 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001496 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001497 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001498
1499 // find the displays that were removed
1500 // (ie: in drawing state but not in current state)
1501 // also handle displays that changed
1502 // (ie: displays that are in both lists)
1503 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001504 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1505 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001506 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001507 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001508 // Call makeCurrent() on the primary display so we can
1509 // be sure that nothing associated with this display
1510 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001511 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001512 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001513 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1514 if (hw != NULL)
1515 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001516 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001517 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001518 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001519 } else {
1520 ALOGW("trying to remove the main display");
1521 }
1522 } else {
1523 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001524 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001525 const wp<IBinder>& display(curr.keyAt(j));
Marco Nelissen097ca272014-11-14 08:01:01 -08001526 const sp<IBinder> state_binder = IInterface::asBinder(state.surface);
1527 const sp<IBinder> draw_binder = IInterface::asBinder(draw[i].surface);
Dan Albert1474f882014-09-08 18:59:09 -07001528 if (state_binder != draw_binder) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001529 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001530 // recreating the DisplayDevice, so we just remove it
1531 // from the drawing state, so that it get re-added
1532 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001533 sp<DisplayDevice> hw(getDisplayDevice(display));
1534 if (hw != NULL)
1535 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001536 mDisplays.removeItem(display);
1537 mDrawingState.displays.removeItemsAt(i);
1538 dc--; i--;
1539 // at this point we must loop to the next item
1540 continue;
1541 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001542
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001543 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001544 if (disp != NULL) {
1545 if (state.layerStack != draw[i].layerStack) {
1546 disp->setLayerStack(state.layerStack);
1547 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001548 if ((state.orientation != draw[i].orientation)
1549 || (state.viewport != draw[i].viewport)
1550 || (state.frame != draw[i].frame))
1551 {
1552 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001553 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001554 }
Michael Lentine47e45402014-07-18 15:34:25 -07001555 if (state.width != draw[i].width || state.height != draw[i].height) {
1556 disp->setDisplaySize(state.width, state.height);
1557 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001558 }
1559 }
1560 }
1561
1562 // find displays that were added
1563 // (ie: in current state but not in drawing state)
1564 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001565 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001566 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001567
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001568 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001569 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001570 sp<IGraphicBufferProducer> bqProducer;
1571 sp<IGraphicBufferConsumer> bqConsumer;
Dan Stoza70982a52016-01-11 23:40:44 +00001572 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1573 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001574
Dan Stoza9e56aa02015-11-02 13:00:03 -08001575 int32_t hwcId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001576 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001577 // Virtual displays without a surface are dormant:
1578 // they have external state (layer stack, projection,
1579 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001580 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001581
Dan Stoza1f3efb12014-10-15 16:34:55 -07001582 int width = 0;
1583 int status = state.surface->query(
1584 NATIVE_WINDOW_WIDTH, &width);
1585 ALOGE_IF(status != NO_ERROR,
1586 "Unable to query width (%d)", status);
1587 int height = 0;
1588 status = state.surface->query(
1589 NATIVE_WINDOW_HEIGHT, &height);
1590 ALOGE_IF(status != NO_ERROR,
1591 "Unable to query height (%d)", status);
Dan Stoza5cf424b2016-05-20 14:02:39 -07001592 int intFormat = 0;
1593 status = state.surface->query(
1594 NATIVE_WINDOW_FORMAT, &intFormat);
1595 ALOGE_IF(status != NO_ERROR,
1596 "Unable to query format (%d)", status);
1597 auto format = static_cast<android_pixel_format_t>(
1598 intFormat);
Dan Stoza1f3efb12014-10-15 16:34:55 -07001599
Dan Stoza5cf424b2016-05-20 14:02:39 -07001600 mHwc->allocateVirtualDisplay(width, height, &format,
Dan Stoza9e56aa02015-11-02 13:00:03 -08001601 &hwcId);
1602
Dan Stoza5cf424b2016-05-20 14:02:39 -07001603 // TODO: Plumb requested format back up to consumer
1604
Dan Stoza9e56aa02015-11-02 13:00:03 -08001605 sp<VirtualDisplaySurface> vds =
1606 new VirtualDisplaySurface(*mHwc,
1607 hwcId, state.surface, bqProducer,
1608 bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001609
1610 dispSurface = vds;
Michael Lentine47e45402014-07-18 15:34:25 -07001611 producer = vds;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001612 }
1613 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001614 ALOGE_IF(state.surface!=NULL,
1615 "adding a supported display, but rendering "
1616 "surface is provided (%p), ignoring it",
1617 state.surface.get());
Dan Stoza9e56aa02015-11-02 13:00:03 -08001618 if (state.type == DisplayDevice::DISPLAY_EXTERNAL) {
1619 hwcId = DisplayDevice::DISPLAY_EXTERNAL;
1620 dispSurface = new FramebufferSurface(*mHwc,
1621 DisplayDevice::DISPLAY_EXTERNAL,
1622 bqConsumer);
1623 producer = bqProducer;
1624 } else {
1625 ALOGE("Attempted to add non-external non-virtual"
1626 " display");
1627 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07001628 }
1629
1630 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001631 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001632 sp<DisplayDevice> hw = new DisplayDevice(this,
Dan Stoza9e56aa02015-11-02 13:00:03 -08001633 state.type, hwcId, state.isSecure, display,
1634 dispSurface, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -08001635 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001636 hw->setLayerStack(state.layerStack);
1637 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001638 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001639 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001640 mDisplays.add(display, hw);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001641 if (!state.isVirtualDisplay()) {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001642 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001643 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001644 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001645 }
1646 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001647 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001648 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001649
Mathias Agopian84300952012-11-21 16:02:13 -08001650 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1651 // The transform hint might have changed for some layers
1652 // (either because a display has changed, or because a layer
1653 // as changed).
1654 //
1655 // Walk through all the layers in currentLayers,
1656 // and update their transform hint.
1657 //
1658 // If a layer is visible only on a single display, then that
1659 // display is used to calculate the hint, otherwise we use the
1660 // default display.
1661 //
1662 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1663 // the hint is set before we acquire a buffer from the surface texture.
1664 //
1665 // NOTE: layer transactions have taken place already, so we use their
1666 // drawing state. However, SurfaceFlinger's own transaction has not
1667 // happened yet, so we must use the current state layer list
1668 // (soon to become the drawing state list).
1669 //
1670 sp<const DisplayDevice> disp;
1671 uint32_t currentlayerStack = 0;
1672 for (size_t i=0; i<count; i++) {
1673 // NOTE: we rely on the fact that layers are sorted by
1674 // layerStack first (so we don't have to traverse the list
1675 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001676 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001677 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001678 if (i==0 || currentlayerStack != layerStack) {
1679 currentlayerStack = layerStack;
1680 // figure out if this layerstack is mirrored
1681 // (more than one display) if so, pick the default display,
1682 // if not, pick the only display it's on.
1683 disp.clear();
1684 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1685 sp<const DisplayDevice> hw(mDisplays[dpy]);
1686 if (hw->getLayerStack() == currentlayerStack) {
1687 if (disp == NULL) {
1688 disp = hw;
1689 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001690 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001691 break;
1692 }
1693 }
1694 }
1695 }
Chet Haase91d25932013-04-11 15:24:55 -07001696 if (disp == NULL) {
1697 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1698 // redraw after transform hint changes. See bug 8508397.
1699
1700 // could be null when this layer is using a layerStack
1701 // that is not visible on any display. Also can occur at
1702 // screen off/on times.
1703 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001704 }
Chet Haase91d25932013-04-11 15:24:55 -07001705 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001706 }
1707 }
1708
1709
Mathias Agopian3559b072012-08-15 13:46:03 -07001710 /*
1711 * Perform our own transaction if needed
1712 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001713
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001714 const LayerVector& layers(mDrawingState.layersSortedByZ);
1715 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001716 // layers have been added
1717 mVisibleRegionsDirty = true;
1718 }
1719
1720 // some layers might have been removed, so
1721 // we need to update the regions they're exposing.
1722 if (mLayersRemoved) {
1723 mLayersRemoved = false;
1724 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001725 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001726 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001727 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001728 if (currentLayers.indexOf(layer) < 0) {
1729 // this layer is not visible anymore
1730 // TODO: we could traverse the tree from front to back and
1731 // compute the actual visible region
1732 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001733 const Layer::State& s(layer->getDrawingState());
Robert Carr3dcabfa2016-03-01 18:36:58 -08001734 Region visibleReg = s.active.transform.transform(
Mathias Agopian1501d542012-09-04 21:04:09 -07001735 Region(Rect(s.active.w, s.active.h)));
1736 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001737 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001738 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001739 }
1740
1741 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001742
1743 updateCursorAsync();
1744}
1745
1746void SurfaceFlinger::updateCursorAsync()
1747{
Dan Stoza9e56aa02015-11-02 13:00:03 -08001748 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
1749 auto& displayDevice = mDisplays[displayId];
1750 if (displayDevice->getHwcDisplayId() < 0) {
Riley Andrews03414a12014-07-01 14:22:59 -07001751 continue;
1752 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001753
1754 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
1755 layer->updateCursorPosition(displayDevice);
Riley Andrews03414a12014-07-01 14:22:59 -07001756 }
1757 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001758}
1759
1760void SurfaceFlinger::commitTransaction()
1761{
1762 if (!mLayersPendingRemoval.isEmpty()) {
1763 // Notify removed layers now that they can't be drawn from
1764 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
Dan Stozae77c7662016-05-13 11:37:28 -07001765 recordBufferingStats(mLayersPendingRemoval[i]->getName().string(),
1766 mLayersPendingRemoval[i]->getOccupancyHistory(true));
Mathias Agopian4fec8732012-06-29 14:12:52 -07001767 mLayersPendingRemoval[i]->onRemoved();
1768 }
1769 mLayersPendingRemoval.clear();
1770 }
1771
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001772 // If this transaction is part of a window animation then the next frame
1773 // we composite should be considered an animation as well.
1774 mAnimCompositionPending = mAnimTransactionPending;
1775
Mathias Agopian4fec8732012-06-29 14:12:52 -07001776 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001777 mTransactionPending = false;
1778 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001779 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001780}
1781
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001782void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001783 const LayerVector& currentLayers, uint32_t layerStack,
1784 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001785{
Mathias Agopian841cde52012-03-01 15:44:37 -08001786 ATRACE_CALL();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001787 ALOGV("computeVisibleRegions");
Mathias Agopian841cde52012-03-01 15:44:37 -08001788
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001789 Region aboveOpaqueLayers;
1790 Region aboveCoveredLayers;
1791 Region dirty;
1792
Mathias Agopian87baae12012-07-31 12:38:26 -07001793 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001794
1795 size_t i = currentLayers.size();
1796 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001797 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001798
1799 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001800 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001801
Jesse Hall01e29052013-02-19 16:13:35 -08001802 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001803 if (s.layerStack != layerStack)
1804 continue;
1805
Mathias Agopianab028732010-03-16 16:41:46 -07001806 /*
1807 * opaqueRegion: area of a surface that is fully opaque.
1808 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001809 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001810
1811 /*
1812 * visibleRegion: area of a surface that is visible on screen
1813 * and not fully transparent. This is essentially the layer's
1814 * footprint minus the opaque regions above it.
1815 * Areas covered by a translucent surface are considered visible.
1816 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001817 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001818
1819 /*
1820 * coveredRegion: area of a surface that is covered by all
1821 * visible regions above it (which includes the translucent areas).
1822 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001823 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001824
Jesse Halla8026d22012-09-25 13:25:04 -07001825 /*
1826 * transparentRegion: area of a surface that is hinted to be completely
1827 * transparent. This is only used to tell when the layer has no visible
1828 * non-transparent regions and can be removed from the layer list. It
1829 * does not affect the visibleRegion of this layer or any layers
1830 * beneath it. The hint may not be correct if apps don't respect the
1831 * SurfaceView restrictions (which, sadly, some don't).
1832 */
1833 Region transparentRegion;
1834
Mathias Agopianab028732010-03-16 16:41:46 -07001835
1836 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001837 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001838 const bool translucent = !layer->isOpaque(s);
Robert Carr3dcabfa2016-03-01 18:36:58 -08001839 Rect bounds(s.active.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001840 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001841 if (!visibleRegion.isEmpty()) {
1842 // Remove the transparent area from the visible region
1843 if (translucent) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001844 const Transform tr(s.active.transform);
Dan Stoza22f7fc42016-05-10 16:19:53 -07001845 if (tr.preserveRects()) {
1846 // transform the transparent region
1847 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001848 } else {
Dan Stoza22f7fc42016-05-10 16:19:53 -07001849 // transformation too complex, can't do the
1850 // transparent region optimization.
1851 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001852 }
Mathias Agopianab028732010-03-16 16:41:46 -07001853 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001854
Mathias Agopianab028732010-03-16 16:41:46 -07001855 // compute the opaque region
Robert Carr3dcabfa2016-03-01 18:36:58 -08001856 const int32_t layerOrientation = s.active.transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001857 if (s.alpha == 1.0f && !translucent &&
Mathias Agopianab028732010-03-16 16:41:46 -07001858 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1859 // the opaque region is the layer's footprint
1860 opaqueRegion = visibleRegion;
1861 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001862 }
1863 }
1864
Mathias Agopianab028732010-03-16 16:41:46 -07001865 // Clip the covered region to the visible region
1866 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1867
1868 // Update aboveCoveredLayers for next (lower) layer
1869 aboveCoveredLayers.orSelf(visibleRegion);
1870
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001871 // subtract the opaque region covered by the layers above us
1872 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001873
1874 // compute this layer's dirty region
1875 if (layer->contentDirty) {
1876 // we need to invalidate the whole region
1877 dirty = visibleRegion;
1878 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001879 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001880 layer->contentDirty = false;
1881 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001882 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001883 * the exposed region consists of two components:
1884 * 1) what's VISIBLE now and was COVERED before
1885 * 2) what's EXPOSED now less what was EXPOSED before
1886 *
1887 * note that (1) is conservative, we start with the whole
1888 * visible region but only keep what used to be covered by
1889 * something -- which mean it may have been exposed.
1890 *
1891 * (2) handles areas that were not covered by anything but got
1892 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001893 */
Mathias Agopianab028732010-03-16 16:41:46 -07001894 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001895 const Region oldVisibleRegion = layer->visibleRegion;
1896 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001897 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1898 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001899 }
1900 dirty.subtractSelf(aboveOpaqueLayers);
1901
1902 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001903 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001904
Mathias Agopianab028732010-03-16 16:41:46 -07001905 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001906 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001907
Jesse Halla8026d22012-09-25 13:25:04 -07001908 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001909 layer->setVisibleRegion(visibleRegion);
1910 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001911 layer->setVisibleNonTransparentRegion(
1912 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001913 }
1914
Mathias Agopian87baae12012-07-31 12:38:26 -07001915 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001916}
1917
Mathias Agopian87baae12012-07-31 12:38:26 -07001918void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1919 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001920 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001921 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1922 if (hw->getLayerStack() == layerStack) {
1923 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001924 }
1925 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001926}
1927
Dan Stoza6b9454d2014-11-07 16:00:59 -08001928bool SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001929{
Dan Stoza9e56aa02015-11-02 13:00:03 -08001930 ALOGV("handlePageFlip");
1931
Mathias Agopian4fec8732012-06-29 14:12:52 -07001932 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001933
Mathias Agopian4fec8732012-06-29 14:12:52 -07001934 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001935 const LayerVector& layers(mDrawingState.layersSortedByZ);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001936 bool frameQueued = false;
Eric Penner51c59cd2014-07-28 19:51:58 -07001937
1938 // Store the set of layers that need updates. This set must not change as
1939 // buffers are being latched, as this could result in a deadlock.
1940 // Example: Two producers share the same command stream and:
1941 // 1.) Layer 0 is latched
1942 // 2.) Layer 0 gets a new frame
1943 // 2.) Layer 1 gets a new frame
1944 // 3.) Layer 1 is latched.
1945 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1946 // second frame. But layer 0's second frame could be waiting on display.
Eric Penner51c59cd2014-07-28 19:51:58 -07001947 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001948 const sp<Layer>& layer(layers[i]);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001949 if (layer->hasQueuedFrame()) {
1950 frameQueued = true;
1951 if (layer->shouldPresentNow(mPrimaryDispSync)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001952 mLayersWithQueuedFrames.push_back(layer.get());
Dan Stozaee44edd2015-03-23 15:50:23 -07001953 } else {
1954 layer->useEmptyDamage();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001955 }
Dan Stozaee44edd2015-03-23 15:50:23 -07001956 } else {
1957 layer->useEmptyDamage();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001958 }
Eric Penner51c59cd2014-07-28 19:51:58 -07001959 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001960 for (auto& layer : mLayersWithQueuedFrames) {
Mathias Agopian87baae12012-07-31 12:38:26 -07001961 const Region dirty(layer->latchBuffer(visibleRegions));
Dan Stozaee44edd2015-03-23 15:50:23 -07001962 layer->useSurfaceDamage();
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001963 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001964 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001965 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001966
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001967 mVisibleRegionsDirty |= visibleRegions;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001968
1969 // If we will need to wake up at some time in the future to deal with a
1970 // queued frame that shouldn't be displayed during this vsync period, wake
1971 // up during the next vsync period to check again.
Dan Stoza9e56aa02015-11-02 13:00:03 -08001972 if (frameQueued && mLayersWithQueuedFrames.empty()) {
Dan Stoza6b9454d2014-11-07 16:00:59 -08001973 signalLayerUpdate();
1974 }
1975
1976 // Only continue with the refresh if there is actually new work to do
Dan Stoza9e56aa02015-11-02 13:00:03 -08001977 return !mLayersWithQueuedFrames.empty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001978}
1979
Mathias Agopianad456f92011-01-13 17:53:01 -08001980void SurfaceFlinger::invalidateHwcGeometry()
1981{
Dan Stoza9e56aa02015-11-02 13:00:03 -08001982 mGeometryInvalid = true;
Mathias Agopianad456f92011-01-13 17:53:01 -08001983}
1984
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001985
Mathias Agopiancd60f992012-08-16 16:28:27 -07001986void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001987 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001988{
Dan Stoza71433162014-02-04 16:22:36 -08001989 // We only need to actually compose the display if:
1990 // 1) It is being handled by hardware composer, which may need this to
1991 // keep its virtual display state machine in sync, or
1992 // 2) There is work to be done (the dirty region isn't empty)
1993 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1994 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001995 ALOGV("Skipping display composition");
Dan Stoza71433162014-02-04 16:22:36 -08001996 return;
1997 }
1998
Dan Stoza9e56aa02015-11-02 13:00:03 -08001999 ALOGV("doDisplayComposition");
2000
Mathias Agopian87baae12012-07-31 12:38:26 -07002001 Region dirtyRegion(inDirtyRegion);
2002
Mathias Agopianb8a55602009-06-26 19:06:36 -07002003 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07002004 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002005
Mathias Agopian42977342012-08-05 00:40:46 -07002006 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002007 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002008 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
2009 // takes a rectangle, we must make sure to update that whole
2010 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07002011 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002012 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002013 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002014 // We need to redraw the rectangle that will be updated
2015 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07002016 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002017 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07002018 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002019 } else {
2020 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07002021 dirtyRegion.set(hw->bounds());
2022 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002023 }
2024 }
2025
Dan Stoza9f26a9c2016-06-22 14:51:09 -07002026 if (!doComposeSurfaces(hw, dirtyRegion)) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002027
Mathias Agopian9c6e2972011-09-20 17:21:56 -07002028 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07002029 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07002030
2031 // swap buffers (presentation)
2032 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002033}
2034
Dan Stoza9e56aa02015-11-02 13:00:03 -08002035bool SurfaceFlinger::doComposeSurfaces(
2036 const sp<const DisplayDevice>& displayDevice, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07002037{
Dan Stoza9e56aa02015-11-02 13:00:03 -08002038 ALOGV("doComposeSurfaces");
Mathias Agopiancd20eb02011-09-22 20:57:04 -07002039
Dan Stoza9e56aa02015-11-02 13:00:03 -08002040 const auto hwcId = displayDevice->getHwcDisplayId();
Dan Stoza9f26a9c2016-06-22 14:51:09 -07002041
2042 mat4 oldColorMatrix;
2043 const bool applyColorMatrix = !mHwc->hasDeviceComposition(hwcId) &&
2044 !mHwc->hasCapability(HWC2::Capability::SkipClientColorTransform);
2045 if (applyColorMatrix) {
2046 mat4 colorMatrix = mColorMatrix * mDaltonizer();
2047 oldColorMatrix = getRenderEngine().setupColorTransform(colorMatrix);
2048 }
2049
Dan Stoza9e56aa02015-11-02 13:00:03 -08002050 bool hasClientComposition = mHwc->hasClientComposition(hwcId);
2051 if (hasClientComposition) {
2052 ALOGV("hasClientComposition");
2053
2054 if (!displayDevice->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08002055 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
Dan Stoza9e56aa02015-11-02 13:00:03 -08002056 displayDevice->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07002057 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
2058 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
2059 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
2060 }
2061 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08002062 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07002063
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07002064 // Never touch the framebuffer if we don't have any framebuffer layers
Dan Stoza9e56aa02015-11-02 13:00:03 -08002065 const bool hasDeviceComposition = mHwc->hasDeviceComposition(hwcId);
2066 if (hasDeviceComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07002067 // when using overlays, we assume a fully transparent framebuffer
2068 // NOTE: we could reduce how much we need to clear, for instance
2069 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07002070 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07002071 // We'll revisit later if needed.
Dan Stoza9e56aa02015-11-02 13:00:03 -08002072 mRenderEngine->clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002073 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07002074 // we start with the whole screen area
Dan Stoza9e56aa02015-11-02 13:00:03 -08002075 const Region bounds(displayDevice->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07002076
2077 // we remove the scissor part
2078 // we're left with the letterbox region
2079 // (common case is that letterbox ends-up being empty)
Dan Stoza9e56aa02015-11-02 13:00:03 -08002080 const Region letterbox(bounds.subtract(displayDevice->getScissor()));
Mathias Agopian766dc492012-10-30 18:08:06 -07002081
2082 // compute the area to clear
Dan Stoza9e56aa02015-11-02 13:00:03 -08002083 Region region(displayDevice->undefinedRegion.merge(letterbox));
Mathias Agopian766dc492012-10-30 18:08:06 -07002084
2085 // but limit it to the dirty region
2086 region.andSelf(dirty);
2087
Mathias Agopianb9494d52012-04-18 02:28:45 -07002088 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07002089 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07002090 // can happen with SurfaceView
Dan Stoza9e56aa02015-11-02 13:00:03 -08002091 drawWormhole(displayDevice, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002092 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002093 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002094
Dan Stoza9e56aa02015-11-02 13:00:03 -08002095 if (displayDevice->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopian766dc492012-10-30 18:08:06 -07002096 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07002097 // scissor on the main display. It should never be needed
2098 // anyways (though in theory it could since the API allows it).
Dan Stoza9e56aa02015-11-02 13:00:03 -08002099 const Rect& bounds(displayDevice->getBounds());
2100 const Rect& scissor(displayDevice->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002101 if (scissor != bounds) {
2102 // scissor doesn't match the screen's dimensions, so we
2103 // need to clear everything outside of it and enable
2104 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07002105
Mathias Agopianf45c5102012-10-24 16:29:17 -07002106 // enable scissor for this frame
Dan Stoza9e56aa02015-11-02 13:00:03 -08002107 const uint32_t height = displayDevice->getHeight();
2108 mRenderEngine->setScissor(scissor.left, height - scissor.bottom,
Mathias Agopian3f844832013-08-07 21:24:32 -07002109 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002110 }
2111 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002112 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002113
Mathias Agopian85d751c2012-08-29 16:59:24 -07002114 /*
2115 * and then, render the layers targeted at the framebuffer
2116 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002117
Dan Stoza9e56aa02015-11-02 13:00:03 -08002118 ALOGV("Rendering client layers");
2119 const Transform& displayTransform = displayDevice->getTransform();
2120 if (hwcId >= 0) {
Mathias Agopian85d751c2012-08-29 16:59:24 -07002121 // we're using h/w composer
Dan Stoza9e56aa02015-11-02 13:00:03 -08002122 bool firstLayer = true;
2123 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
2124 const Region clip(dirty.intersect(
2125 displayTransform.transform(layer->visibleRegion)));
2126 ALOGV("Layer: %s", layer->getName().string());
2127 ALOGV(" Composition type: %s",
2128 to_string(layer->getCompositionType(hwcId)).c_str());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002129 if (!clip.isEmpty()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08002130 switch (layer->getCompositionType(hwcId)) {
2131 case HWC2::Composition::Cursor:
2132 case HWC2::Composition::Device:
2133 case HWC2::Composition::SolidColor: {
Mathias Agopianac683022013-10-01 15:36:52 -07002134 const Layer::State& state(layer->getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -08002135 if (layer->getClearClientTarget(hwcId) && !firstLayer &&
2136 layer->isOpaque(state) && (state.alpha == 1.0f)
2137 && hasClientComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07002138 // never clear the very first layer since we're
2139 // guaranteed the FB is already cleared
Dan Stoza9e56aa02015-11-02 13:00:03 -08002140 layer->clearWithOpenGL(displayDevice, clip);
Mathias Agopiancd60f992012-08-16 16:28:27 -07002141 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002142 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07002143 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08002144 case HWC2::Composition::Client: {
2145 layer->draw(displayDevice, clip);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002146 break;
2147 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08002148 default:
Mathias Agopianda27af92012-09-13 18:17:13 -07002149 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07002150 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08002151 } else {
2152 ALOGV(" Skipping for empty clip");
Mathias Agopian85d751c2012-08-29 16:59:24 -07002153 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08002154 firstLayer = false;
Mathias Agopian85d751c2012-08-29 16:59:24 -07002155 }
2156 } else {
2157 // we're not using h/w composer
Dan Stoza9e56aa02015-11-02 13:00:03 -08002158 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
Mathias Agopian85d751c2012-08-29 16:59:24 -07002159 const Region clip(dirty.intersect(
Dan Stoza9e56aa02015-11-02 13:00:03 -08002160 displayTransform.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07002161 if (!clip.isEmpty()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08002162 layer->draw(displayDevice, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07002163 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002164 }
2165 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002166
Dan Stoza9f26a9c2016-06-22 14:51:09 -07002167 if (applyColorMatrix) {
2168 getRenderEngine().setupColorTransform(oldColorMatrix);
2169 }
2170
Mathias Agopianf45c5102012-10-24 16:29:17 -07002171 // disable scissor at the end of the frame
Dan Stoza9e56aa02015-11-02 13:00:03 -08002172 mRenderEngine->disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07002173 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002174}
2175
Mathias Agopian3f844832013-08-07 21:24:32 -07002176void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07002177 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07002178 RenderEngine& engine(getRenderEngine());
2179 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002180}
2181
Dan Stoza7d89d062015-04-30 13:29:25 -07002182status_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -08002183 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07002184 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08002185 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07002186{
Dan Stoza7d89d062015-04-30 13:29:25 -07002187 // add this layer to the current state list
2188 {
2189 Mutex::Autolock _l(mStateLock);
2190 if (mCurrentState.layersSortedByZ.size() >= MAX_LAYERS) {
2191 return NO_MEMORY;
2192 }
2193 mCurrentState.layersSortedByZ.add(lbc);
2194 mGraphicBufferProducerList.add(IInterface::asBinder(gbc));
2195 }
2196
Mathias Agopian96f08192010-06-02 23:28:45 -07002197 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08002198 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07002199
Dan Stoza7d89d062015-04-30 13:29:25 -07002200 return NO_ERROR;
Mathias Agopian96f08192010-06-02 23:28:45 -07002201}
2202
Mathias Agopian3f844832013-08-07 21:24:32 -07002203status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002204 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08002205 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002206 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07002207 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002208 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07002209 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002210 return NO_ERROR;
2211 }
Mathias Agopian3d579642009-06-04 18:46:21 -07002212 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002213}
2214
Dan Stozac7014012014-02-14 15:03:43 -08002215uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07002216 return android_atomic_release_load(&mTransactionFlags);
2217}
2218
Mathias Agopian3f844832013-08-07 21:24:32 -07002219uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002220 return android_atomic_and(~flags, &mTransactionFlags) & flags;
2221}
2222
Mathias Agopian3f844832013-08-07 21:24:32 -07002223uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002224 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
2225 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002226 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002227 }
2228 return old;
2229}
2230
Mathias Agopian8b33f032012-07-24 20:43:54 -07002231void SurfaceFlinger::setTransactionState(
2232 const Vector<ComposerState>& state,
2233 const Vector<DisplayState>& displays,
2234 uint32_t flags)
2235{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002236 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07002237 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07002238 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002239
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002240 if (flags & eAnimation) {
2241 // For window updates that are part of an animation we must wait for
2242 // previous animation "frames" to be handled.
2243 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002244 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002245 if (CC_UNLIKELY(err != NO_ERROR)) {
2246 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002247 // caller after a few seconds.
2248 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
2249 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002250 mAnimTransactionPending = false;
2251 break;
2252 }
2253 }
2254 }
2255
Mathias Agopiane57f2922012-08-09 16:29:12 -07002256 size_t count = displays.size();
2257 for (size_t i=0 ; i<count ; i++) {
2258 const DisplayState& s(displays[i]);
2259 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002260 }
2261
Mathias Agopiane57f2922012-08-09 16:29:12 -07002262 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002263 for (size_t i=0 ; i<count ; i++) {
2264 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002265 // Here we need to check that the interface we're given is indeed
2266 // one of our own. A malicious client could give us a NULL
2267 // IInterface, or one of its own or even one of our own but a
2268 // different type. All these situations would cause us to crash.
2269 //
2270 // NOTE: it would be better to use RTTI as we could directly check
2271 // that we have a Client*. however, RTTI is disabled in Android.
2272 if (s.client != NULL) {
Marco Nelissen097ca272014-11-14 08:01:01 -08002273 sp<IBinder> binder = IInterface::asBinder(s.client);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002274 if (binder != NULL) {
2275 String16 desc(binder->getInterfaceDescriptor());
2276 if (desc == ISurfaceComposerClient::descriptor) {
2277 sp<Client> client( static_cast<Client *>(s.client.get()) );
2278 transactionFlags |= setClientStateLocked(client, s.state);
2279 }
2280 }
2281 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002282 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002283
Robert Carr2a7dbb42016-05-24 11:41:28 -07002284 // If a synchronous transaction is explicitly requested without any changes,
2285 // force a transaction anyway. This can be used as a flush mechanism for
2286 // previous async transactions.
2287 if (transactionFlags == 0 && (flags & eSynchronous)) {
2288 transactionFlags = eTransactionNeeded;
2289 }
2290
Mathias Agopian386aa982011-11-07 21:58:03 -08002291 if (transactionFlags) {
2292 // this triggers the transaction
2293 setTransactionFlags(transactionFlags);
2294
2295 // if this is a synchronous transaction, wait for it to take effect
2296 // before returning.
2297 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002298 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002299 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002300 if (flags & eAnimation) {
2301 mAnimTransactionPending = true;
2302 }
2303 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002304 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2305 if (CC_UNLIKELY(err != NO_ERROR)) {
2306 // just in case something goes wrong in SF, return to the
2307 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002308 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2309 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002310 break;
2311 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002312 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002313 }
2314}
2315
Mathias Agopiane57f2922012-08-09 16:29:12 -07002316uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2317{
Jesse Hall9a143922012-10-04 16:29:19 -07002318 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2319 if (dpyIdx < 0)
2320 return 0;
2321
Mathias Agopiane57f2922012-08-09 16:29:12 -07002322 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002323 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002324 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002325 const uint32_t what = s.what;
2326 if (what & DisplayState::eSurfaceChanged) {
Marco Nelissen097ca272014-11-14 08:01:01 -08002327 if (IInterface::asBinder(disp.surface) != IInterface::asBinder(s.surface)) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002328 disp.surface = s.surface;
2329 flags |= eDisplayTransactionNeeded;
2330 }
2331 }
2332 if (what & DisplayState::eLayerStackChanged) {
2333 if (disp.layerStack != s.layerStack) {
2334 disp.layerStack = s.layerStack;
2335 flags |= eDisplayTransactionNeeded;
2336 }
2337 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002338 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002339 if (disp.orientation != s.orientation) {
2340 disp.orientation = s.orientation;
2341 flags |= eDisplayTransactionNeeded;
2342 }
2343 if (disp.frame != s.frame) {
2344 disp.frame = s.frame;
2345 flags |= eDisplayTransactionNeeded;
2346 }
2347 if (disp.viewport != s.viewport) {
2348 disp.viewport = s.viewport;
2349 flags |= eDisplayTransactionNeeded;
2350 }
2351 }
Michael Lentine47e45402014-07-18 15:34:25 -07002352 if (what & DisplayState::eDisplaySizeChanged) {
2353 if (disp.width != s.width) {
2354 disp.width = s.width;
2355 flags |= eDisplayTransactionNeeded;
2356 }
2357 if (disp.height != s.height) {
2358 disp.height = s.height;
2359 flags |= eDisplayTransactionNeeded;
2360 }
2361 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002362 }
2363 return flags;
2364}
2365
2366uint32_t SurfaceFlinger::setClientStateLocked(
2367 const sp<Client>& client,
2368 const layer_state_t& s)
2369{
2370 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002371 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002372 if (layer != 0) {
2373 const uint32_t what = s.what;
Robert Carr99e27f02016-06-16 15:18:02 -07002374 bool geometryAppliesWithResize =
2375 what & layer_state_t::eGeometryAppliesWithResize;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002376 if (what & layer_state_t::ePositionChanged) {
Robert Carr99e27f02016-06-16 15:18:02 -07002377 if (layer->setPosition(s.x, s.y, !geometryAppliesWithResize)) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002378 flags |= eTraversalNeeded;
Robert Carr82364e32016-05-15 11:27:47 -07002379 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002380 }
2381 if (what & layer_state_t::eLayerChanged) {
2382 // NOTE: index needs to be calculated before we update the state
2383 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
Pablo Ceballos47db6eb2015-12-08 13:53:59 -08002384 if (layer->setLayer(s.z) && idx >= 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002385 mCurrentState.layersSortedByZ.removeAt(idx);
2386 mCurrentState.layersSortedByZ.add(layer);
2387 // we need traversal (state changed)
2388 // AND transaction (list changed)
2389 flags |= eTransactionNeeded|eTraversalNeeded;
2390 }
2391 }
2392 if (what & layer_state_t::eSizeChanged) {
2393 if (layer->setSize(s.w, s.h)) {
2394 flags |= eTraversalNeeded;
2395 }
2396 }
2397 if (what & layer_state_t::eAlphaChanged) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08002398 if (layer->setAlpha(s.alpha))
Mathias Agopiane57f2922012-08-09 16:29:12 -07002399 flags |= eTraversalNeeded;
2400 }
2401 if (what & layer_state_t::eMatrixChanged) {
2402 if (layer->setMatrix(s.matrix))
2403 flags |= eTraversalNeeded;
2404 }
2405 if (what & layer_state_t::eTransparentRegionChanged) {
2406 if (layer->setTransparentRegionHint(s.transparentRegion))
2407 flags |= eTraversalNeeded;
2408 }
Dan Stoza23116082015-06-18 14:58:39 -07002409 if (what & layer_state_t::eFlagsChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002410 if (layer->setFlags(s.flags, s.mask))
2411 flags |= eTraversalNeeded;
2412 }
2413 if (what & layer_state_t::eCropChanged) {
Robert Carr99e27f02016-06-16 15:18:02 -07002414 if (layer->setCrop(s.crop, !geometryAppliesWithResize))
Mathias Agopiane57f2922012-08-09 16:29:12 -07002415 flags |= eTraversalNeeded;
2416 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002417 if (what & layer_state_t::eFinalCropChanged) {
2418 if (layer->setFinalCrop(s.finalCrop))
2419 flags |= eTraversalNeeded;
2420 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002421 if (what & layer_state_t::eLayerStackChanged) {
2422 // NOTE: index needs to be calculated before we update the state
2423 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
Pablo Ceballos47db6eb2015-12-08 13:53:59 -08002424 if (layer->setLayerStack(s.layerStack) && idx >= 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002425 mCurrentState.layersSortedByZ.removeAt(idx);
2426 mCurrentState.layersSortedByZ.add(layer);
2427 // we need traversal (state changed)
2428 // AND transaction (list changed)
2429 flags |= eTransactionNeeded|eTraversalNeeded;
2430 }
2431 }
Dan Stoza7dde5992015-05-22 09:51:44 -07002432 if (what & layer_state_t::eDeferTransaction) {
2433 layer->deferTransactionUntil(s.handle, s.frameNumber);
2434 // We don't trigger a traversal here because if no other state is
2435 // changed, we don't want this to cause any more work
2436 }
Robert Carrc3574f72016-03-24 12:19:32 -07002437 if (what & layer_state_t::eOverrideScalingModeChanged) {
2438 layer->setOverrideScalingMode(s.overrideScalingMode);
2439 // We don't trigger a traversal here because if no other state is
2440 // changed, we don't want this to cause any more work
2441 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002442 }
2443 return flags;
2444}
2445
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002446status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002447 const String8& name,
2448 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002449 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2450 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002451{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002452 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002453 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002454 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002455 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002456 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002457 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002458
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002459 status_t result = NO_ERROR;
2460
2461 sp<Layer> layer;
2462
Mathias Agopian3165cc22012-08-08 19:42:09 -07002463 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2464 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002465 result = createNormalLayer(client,
2466 name, w, h, flags, format,
2467 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002468 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002469 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002470 result = createDimLayer(client,
2471 name, w, h, flags,
2472 handle, gbp, &layer);
2473 break;
2474 default:
2475 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002476 break;
2477 }
2478
Dan Stoza7d89d062015-04-30 13:29:25 -07002479 if (result != NO_ERROR) {
2480 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002481 }
Dan Stoza7d89d062015-04-30 13:29:25 -07002482
2483 result = addClientLayer(client, *handle, *gbp, layer);
2484 if (result != NO_ERROR) {
2485 return result;
2486 }
2487
2488 setTransactionFlags(eTransactionNeeded);
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002489 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002490}
2491
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002492status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2493 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2494 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002495{
2496 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002497 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002498 case PIXEL_FORMAT_TRANSPARENT:
2499 case PIXEL_FORMAT_TRANSLUCENT:
2500 format = PIXEL_FORMAT_RGBA_8888;
2501 break;
2502 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002503 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002504 break;
2505 }
2506
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002507 *outLayer = new Layer(this, client, name, w, h, flags);
2508 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2509 if (err == NO_ERROR) {
2510 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002511 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002512 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002513
2514 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2515 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002516}
2517
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002518status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2519 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2520 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002521{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002522 *outLayer = new LayerDim(this, client, name, w, h, flags);
2523 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002524 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002525 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002526}
2527
Mathias Agopianac9fa422013-02-11 16:40:36 -08002528status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002529{
Mathias Agopian67106042013-03-14 19:18:13 -07002530 // called by the window manager when it wants to remove a Layer
2531 status_t err = NO_ERROR;
2532 sp<Layer> l(client->getLayerUser(handle));
2533 if (l != NULL) {
2534 err = removeLayer(l);
2535 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2536 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002537 }
2538 return err;
2539}
2540
Mathias Agopian13127d82013-03-05 17:47:11 -08002541status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002542{
Mathias Agopian67106042013-03-14 19:18:13 -07002543 // called by ~LayerCleaner() when all references to the IBinder (handle)
2544 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002545 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002546 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002547 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002548 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002549 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002550 "error removing layer=%p (%s)", l.get(), strerror(-err));
2551 }
2552 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002553}
2554
Mathias Agopianb60314a2012-04-10 22:09:54 -07002555// ---------------------------------------------------------------------------
2556
Andy McFadden13a082e2012-08-24 10:16:42 -07002557void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002558 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002559 Vector<ComposerState> state;
2560 Vector<DisplayState> displays;
2561 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002562 d.what = DisplayState::eDisplayProjectionChanged |
2563 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002564 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002565 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002566 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002567 d.frame.makeInvalid();
2568 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002569 d.width = 0;
2570 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002571 displays.add(d);
2572 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002573 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002574
Dan Stoza9e56aa02015-11-02 13:00:03 -08002575 const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
2576 const nsecs_t period = activeConfig->getVsyncPeriod();
Jamie Gennis6547ff42013-07-16 20:12:42 -07002577 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002578}
2579
2580void SurfaceFlinger::initializeDisplays() {
2581 class MessageScreenInitialized : public MessageBase {
2582 SurfaceFlinger* flinger;
2583 public:
2584 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2585 virtual bool handler() {
2586 flinger->onInitializeDisplays();
2587 return true;
2588 }
2589 };
2590 sp<MessageBase> msg = new MessageScreenInitialized(this);
2591 postMessageAsync(msg); // we may be called from main thread, use async message
2592}
2593
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002594void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2595 int mode) {
2596 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2597 this);
2598 int32_t type = hw->getDisplayType();
2599 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002600
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002601 if (mode == currentMode) {
2602 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002603 return;
2604 }
2605
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002606 hw->setPowerMode(mode);
2607 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2608 ALOGW("Trying to set power mode for virtual display");
2609 return;
2610 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002611
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002612 if (currentMode == HWC_POWER_MODE_OFF) {
2613 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002614 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2615 // FIXME: eventthread only knows about the main display right now
2616 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002617 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002618 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002619
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002620 mVisibleRegionsDirty = true;
Dan Stozab90cf072015-03-05 11:05:59 -08002621 mHasPoweredOff = true;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002622 repaintEverything();
2623 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002624 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002625 disableHardwareVsync(true); // also cancels any in-progress resync
2626
Mathias Agopiancde87a32012-09-13 14:09:01 -07002627 // FIXME: eventthread only knows about the main display right now
2628 mEventThread->onScreenReleased();
2629 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002630
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002631 getHwComposer().setPowerMode(type, mode);
2632 mVisibleRegionsDirty = true;
2633 // from this point on, SF will stop drawing on this display
2634 } else {
2635 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002636 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002637}
2638
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002639void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2640 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002641 SurfaceFlinger& mFlinger;
2642 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002643 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002644 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002645 MessageSetPowerMode(SurfaceFlinger& flinger,
2646 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2647 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002648 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002649 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002650 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002651 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002652 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002653 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002654 ALOGW("Attempt to set power mode = %d for virtual display",
2655 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002656 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002657 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002658 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002659 return true;
2660 }
2661 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002662 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002663 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002664}
2665
2666// ---------------------------------------------------------------------------
2667
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002668status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2669{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002670 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002671
Mathias Agopianbd115332013-04-18 16:41:04 -07002672 IPCThreadState* ipc = IPCThreadState::self();
2673 const int pid = ipc->getCallingPid();
2674 const int uid = ipc->getCallingUid();
2675 if ((uid != AID_SHELL) &&
2676 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002677 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002678 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002679 } else {
Jesse Hallfcd15b42014-12-23 13:57:23 -08002680 // Try to get the main lock, but give up after one second
Mathias Agopian9795c422009-08-26 16:36:26 -07002681 // (this would indicate SF is stuck, but we want to be able to
2682 // print something in dumpsys).
Jesse Hallfcd15b42014-12-23 13:57:23 -08002683 status_t err = mStateLock.timedLock(s2ns(1));
2684 bool locked = (err == NO_ERROR);
Mathias Agopian9795c422009-08-26 16:36:26 -07002685 if (!locked) {
Jesse Hallfcd15b42014-12-23 13:57:23 -08002686 result.appendFormat(
2687 "SurfaceFlinger appears to be unresponsive (%s [%d]), "
2688 "dumping anyways (no locks held)\n", strerror(-err), err);
Mathias Agopian9795c422009-08-26 16:36:26 -07002689 }
2690
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002691 bool dumpAll = true;
2692 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002693 size_t numArgs = args.size();
2694 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002695 if ((index < numArgs) &&
2696 (args[index] == String16("--list"))) {
2697 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002698 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002699 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002700 }
2701
2702 if ((index < numArgs) &&
2703 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002704 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002705 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002706 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002707 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002708
2709 if ((index < numArgs) &&
2710 (args[index] == String16("--latency-clear"))) {
2711 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002712 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002713 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002714 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002715
2716 if ((index < numArgs) &&
2717 (args[index] == String16("--dispsync"))) {
2718 index++;
2719 mPrimaryDispSync.dump(result);
2720 dumpAll = false;
2721 }
Dan Stozab90cf072015-03-05 11:05:59 -08002722
2723 if ((index < numArgs) &&
2724 (args[index] == String16("--static-screen"))) {
2725 index++;
2726 dumpStaticScreenStats(result);
2727 dumpAll = false;
2728 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08002729
2730 if ((index < numArgs) &&
2731 (args[index] == String16("--fences"))) {
2732 index++;
2733 mFenceTracker.dump(&result);
2734 dumpAll = false;
2735 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002736 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002737
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002738 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002739 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002740 }
2741
Mathias Agopian9795c422009-08-26 16:36:26 -07002742 if (locked) {
2743 mStateLock.unlock();
2744 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002745 }
2746 write(fd, result.string(), result.size());
2747 return NO_ERROR;
2748}
2749
Dan Stozac7014012014-02-14 15:03:43 -08002750void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2751 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002752{
2753 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2754 const size_t count = currentLayers.size();
2755 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002756 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002757 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002758 }
2759}
2760
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002761void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002762 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002763{
2764 String8 name;
2765 if (index < args.size()) {
2766 name = String8(args[index]);
2767 index++;
2768 }
2769
Dan Stoza9e56aa02015-11-02 13:00:03 -08002770 const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
2771 const nsecs_t period = activeConfig->getVsyncPeriod();
Greg Hackmann86efcc02014-03-07 12:44:02 -08002772 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002773
2774 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002775 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002776 } else {
2777 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2778 const size_t count = currentLayers.size();
2779 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002780 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002781 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002782 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002783 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002784 }
2785 }
2786}
2787
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002788void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002789 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002790{
2791 String8 name;
2792 if (index < args.size()) {
2793 name = String8(args[index]);
2794 index++;
2795 }
2796
2797 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2798 const size_t count = currentLayers.size();
2799 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002800 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002801 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002802 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002803 }
2804 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002805
Svetoslavd85084b2014-03-20 10:28:31 -07002806 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002807}
2808
Jamie Gennis6547ff42013-07-16 20:12:42 -07002809// This should only be called from the main thread. Otherwise it would need
2810// the lock and should use mCurrentState rather than mDrawingState.
2811void SurfaceFlinger::logFrameStats() {
2812 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2813 const size_t count = drawingLayers.size();
2814 for (size_t i=0 ; i<count ; i++) {
2815 const sp<Layer>& layer(drawingLayers[i]);
2816 layer->logFrameStats();
2817 }
2818
2819 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2820}
2821
Andy McFadden4803b742012-09-24 19:07:20 -07002822/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2823{
2824 static const char* config =
2825 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002826#ifdef HAS_CONTEXT_PRIORITY
2827 " HAS_CONTEXT_PRIORITY"
2828#endif
2829#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2830 " NEVER_DEFAULT_TO_ASYNC_MODE"
2831#endif
2832#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2833 " TARGET_DISABLE_TRIPLE_BUFFERING"
2834#endif
2835 "]";
2836 result.append(config);
2837}
2838
Dan Stozab90cf072015-03-05 11:05:59 -08002839void SurfaceFlinger::dumpStaticScreenStats(String8& result) const
2840{
2841 result.appendFormat("Static screen stats:\n");
2842 for (size_t b = 0; b < NUM_BUCKETS - 1; ++b) {
2843 float bucketTimeSec = mFrameBuckets[b] / 1e9;
2844 float percent = 100.0f *
2845 static_cast<float>(mFrameBuckets[b]) / mTotalTime;
2846 result.appendFormat(" < %zd frames: %.3f s (%.1f%%)\n",
2847 b + 1, bucketTimeSec, percent);
2848 }
2849 float bucketTimeSec = mFrameBuckets[NUM_BUCKETS - 1] / 1e9;
2850 float percent = 100.0f *
2851 static_cast<float>(mFrameBuckets[NUM_BUCKETS - 1]) / mTotalTime;
2852 result.appendFormat(" %zd+ frames: %.3f s (%.1f%%)\n",
2853 NUM_BUCKETS - 1, bucketTimeSec, percent);
2854}
2855
Dan Stozae77c7662016-05-13 11:37:28 -07002856void SurfaceFlinger::recordBufferingStats(const char* layerName,
2857 std::vector<OccupancyTracker::Segment>&& history) {
2858 Mutex::Autolock lock(mBufferingStatsMutex);
2859 auto& stats = mBufferingStats[layerName];
2860 for (const auto& segment : history) {
2861 if (!segment.usedThirdBuffer) {
2862 stats.twoBufferTime += segment.totalTime;
2863 }
2864 if (segment.occupancyAverage < 1.0f) {
2865 stats.doubleBufferedTime += segment.totalTime;
2866 } else if (segment.occupancyAverage < 2.0f) {
2867 stats.tripleBufferedTime += segment.totalTime;
2868 }
2869 ++stats.numSegments;
2870 stats.totalTime += segment.totalTime;
2871 }
2872}
2873
2874void SurfaceFlinger::dumpBufferingStats(String8& result) const {
2875 result.append("Buffering stats:\n");
2876 result.append(" [Layer name] <Active time> <Two buffer> "
2877 "<Double buffered> <Triple buffered>\n");
2878 Mutex::Autolock lock(mBufferingStatsMutex);
2879 typedef std::tuple<std::string, float, float, float> BufferTuple;
2880 std::map<float, BufferTuple, std::greater<float>> sorted;
2881 for (const auto& statsPair : mBufferingStats) {
2882 const char* name = statsPair.first.c_str();
2883 const BufferingStats& stats = statsPair.second;
2884 if (stats.numSegments == 0) {
2885 continue;
2886 }
2887 float activeTime = ns2ms(stats.totalTime) / 1000.0f;
2888 float twoBufferRatio = static_cast<float>(stats.twoBufferTime) /
2889 stats.totalTime;
2890 float doubleBufferRatio = static_cast<float>(
2891 stats.doubleBufferedTime) / stats.totalTime;
2892 float tripleBufferRatio = static_cast<float>(
2893 stats.tripleBufferedTime) / stats.totalTime;
2894 sorted.insert({activeTime, {name, twoBufferRatio,
2895 doubleBufferRatio, tripleBufferRatio}});
2896 }
2897 for (const auto& sortedPair : sorted) {
2898 float activeTime = sortedPair.first;
2899 const BufferTuple& values = sortedPair.second;
2900 result.appendFormat(" [%s] %.2f %.3f %.3f %.3f\n",
2901 std::get<0>(values).c_str(), activeTime,
2902 std::get<1>(values), std::get<2>(values),
2903 std::get<3>(values));
2904 }
2905 result.append("\n");
2906}
2907
2908
Mathias Agopian74d211a2013-04-22 16:55:35 +02002909void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2910 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002911{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002912 bool colorize = false;
2913 if (index < args.size()
2914 && (args[index] == String16("--color"))) {
2915 colorize = true;
2916 index++;
2917 }
2918
2919 Colorizer colorizer(colorize);
2920
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002921 // figure out if we're stuck somewhere
2922 const nsecs_t now = systemTime();
2923 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2924 const nsecs_t inTransaction(mDebugInTransaction);
2925 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2926 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2927
2928 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002929 * Dump library configuration.
2930 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002931
2932 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002933 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002934 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002935 appendSfConfigString(result);
2936 appendUiConfigString(result);
2937 appendGuiConfigString(result);
2938 result.append("\n");
2939
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002940 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002941 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002942 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002943 result.append(SyncFeatures::getInstance().toString());
2944 result.append("\n");
2945
Dan Stoza9e56aa02015-11-02 13:00:03 -08002946 const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
2947
Andy McFadden41d67d72014-04-25 16:58:34 -07002948 colorizer.bold(result);
2949 result.append("DispSync configuration: ");
2950 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002951 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2952 "present offset %d ns (refresh %" PRId64 " ns)",
Dan Stoza9e56aa02015-11-02 13:00:03 -08002953 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs,
2954 PRESENT_TIME_OFFSET_FROM_VSYNC_NS, activeConfig->getVsyncPeriod());
Andy McFadden41d67d72014-04-25 16:58:34 -07002955 result.append("\n");
2956
Dan Stozab90cf072015-03-05 11:05:59 -08002957 // Dump static screen stats
2958 result.append("\n");
2959 dumpStaticScreenStats(result);
2960 result.append("\n");
2961
Dan Stozae77c7662016-05-13 11:37:28 -07002962 dumpBufferingStats(result);
2963
Andy McFadden4803b742012-09-24 19:07:20 -07002964 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002965 * Dump the visible layer list
2966 */
2967 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2968 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002969 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002970 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002971 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002972 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002973 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002974 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002975 }
2976
2977 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002978 * Dump Display state
2979 */
2980
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002981 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002982 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002983 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002984 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2985 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002986 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002987 }
2988
2989 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002990 * Dump SurfaceFlinger global state
2991 */
2992
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002993 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002994 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002995 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002996
Mathias Agopian888c8222012-08-04 21:10:38 -07002997 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002998 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002999
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003000 colorizer.bold(result);
3001 result.appendFormat("EGL implementation : %s\n",
3002 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
3003 colorizer.reset(result);
3004 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07003005 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07003006
Mathias Agopian875d8e12013-06-07 15:35:48 -07003007 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003008
Mathias Agopian42977342012-08-05 00:40:46 -07003009 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07003010 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
3011 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02003012 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003013 " last eglSwapBuffers() time: %f us\n"
3014 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08003015 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003016 " refresh-rate : %f fps\n"
3017 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07003018 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07003019 " gpu_to_cpu_unsupported : %d\n"
3020 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003021 mLastSwapBufferTime/1000.0,
3022 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08003023 mTransactionFlags,
Dan Stoza9e56aa02015-11-02 13:00:03 -08003024 1e9 / activeConfig->getVsyncPeriod(),
3025 activeConfig->getDpiX(),
3026 activeConfig->getDpiY(),
Mathias Agopianed985572013-03-22 00:24:39 -07003027 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003028
Mathias Agopian74d211a2013-04-22 16:55:35 +02003029 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003030 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003031
Mathias Agopian74d211a2013-04-22 16:55:35 +02003032 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003033 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003034
3035 /*
3036 * VSYNC state
3037 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02003038 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003039
3040 /*
3041 * Dump HWComposer state
3042 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003043 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02003044 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003045 colorizer.reset(result);
Dan Stoza9f26a9c2016-06-22 14:51:09 -07003046 bool hwcDisabled = mDebugDisableHWC || mDebugRegion;
Dan Stoza9e56aa02015-11-02 13:00:03 -08003047 result.appendFormat(" h/w composer %s\n",
3048 hwcDisabled ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02003049 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003050
3051 /*
3052 * Dump gralloc state
3053 */
3054 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
3055 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003056}
3057
Mathias Agopian13127d82013-03-05 17:47:11 -08003058const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07003059SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07003060 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07003061 wp<IBinder> dpy;
3062 for (size_t i=0 ; i<mDisplays.size() ; i++) {
3063 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
3064 dpy = mDisplays.keyAt(i);
3065 break;
3066 }
3067 }
3068 if (dpy == NULL) {
3069 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
3070 // Just use the primary display so we have something to return
3071 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
3072 }
3073 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07003074}
3075
Keun young Park63f165f2012-08-31 10:53:36 -07003076bool SurfaceFlinger::startDdmConnection()
3077{
3078 void* libddmconnection_dso =
3079 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
3080 if (!libddmconnection_dso) {
3081 return false;
3082 }
3083 void (*DdmConnection_start)(const char* name);
3084 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07003085 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07003086 if (!DdmConnection_start) {
3087 dlclose(libddmconnection_dso);
3088 return false;
3089 }
3090 (*DdmConnection_start)(getServiceName());
3091 return true;
3092}
3093
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003094status_t SurfaceFlinger::onTransact(
3095 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3096{
3097 switch (code) {
3098 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07003099 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07003100 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003101 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07003102 case CLEAR_ANIMATION_FRAME_STATS:
3103 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07003104 case SET_POWER_MODE:
Dan Stozac4f471e2016-03-24 09:31:08 -07003105 case GET_HDR_CAPABILITIES:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003106 {
3107 // codes that require permission check
3108 IPCThreadState* ipc = IPCThreadState::self();
3109 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07003110 const int uid = ipc->getCallingUid();
Jeff Brown3bfe51d2015-04-10 20:20:13 -07003111 if ((uid != AID_GRAPHICS && uid != AID_SYSTEM) &&
Mathias Agopian99b49842011-06-27 16:05:52 -07003112 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00003113 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07003114 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
3115 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003116 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003117 break;
3118 }
3119 case CAPTURE_SCREEN:
3120 {
3121 // codes that require permission check
3122 IPCThreadState* ipc = IPCThreadState::self();
3123 const int pid = ipc->getCallingPid();
3124 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07003125 if ((uid != AID_GRAPHICS) &&
3126 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00003127 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003128 "can't read framebuffer pid=%d, uid=%d", pid, uid);
3129 return PERMISSION_DENIED;
3130 }
3131 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003132 }
3133 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003134
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003135 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
3136 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07003137 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08003138 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07003139 IPCThreadState* ipc = IPCThreadState::self();
3140 const int pid = ipc->getCallingPid();
3141 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00003142 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07003143 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003144 return PERMISSION_DENIED;
3145 }
3146 int n;
3147 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07003148 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07003149 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003150 return NO_ERROR;
3151 case 1002: // SHOW_UPDATES
3152 n = data.readInt32();
3153 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07003154 invalidateHwcGeometry();
3155 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003156 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003157 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07003158 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003159 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003160 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003161 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07003162 setTransactionFlags(
3163 eTransactionNeeded|
3164 eDisplayTransactionNeeded|
3165 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003166 return NO_ERROR;
3167 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08003168 case 1006:{ // send empty update
3169 signalRefresh();
3170 return NO_ERROR;
3171 }
Mathias Agopian53331da2011-08-22 21:44:41 -07003172 case 1008: // toggle use of hw composer
3173 n = data.readInt32();
3174 mDebugDisableHWC = n ? 1 : 0;
3175 invalidateHwcGeometry();
3176 repaintEverything();
3177 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07003178 case 1009: // toggle use of transform hint
3179 n = data.readInt32();
3180 mDebugDisableTransformHint = n ? 1 : 0;
3181 invalidateHwcGeometry();
3182 repaintEverything();
3183 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003184 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07003185 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003186 reply->writeInt32(0);
3187 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07003188 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08003189 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003190 return NO_ERROR;
3191 case 1013: {
3192 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07003193 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
3194 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07003195 return NO_ERROR;
3196 }
3197 case 1014: {
3198 // daltonize
3199 n = data.readInt32();
3200 switch (n % 10) {
Dan Stoza9f26a9c2016-06-22 14:51:09 -07003201 case 1:
3202 mDaltonizer.setType(ColorBlindnessType::Protanomaly);
3203 break;
3204 case 2:
3205 mDaltonizer.setType(ColorBlindnessType::Deuteranomaly);
3206 break;
3207 case 3:
3208 mDaltonizer.setType(ColorBlindnessType::Tritanomaly);
3209 break;
3210 default:
3211 mDaltonizer.setType(ColorBlindnessType::None);
3212 break;
Mathias Agopianff2ed702013-09-01 21:36:12 -07003213 }
3214 if (n >= 10) {
Dan Stoza9f26a9c2016-06-22 14:51:09 -07003215 mDaltonizer.setMode(ColorBlindnessMode::Correction);
Mathias Agopianff2ed702013-09-01 21:36:12 -07003216 } else {
Dan Stoza9f26a9c2016-06-22 14:51:09 -07003217 mDaltonizer.setMode(ColorBlindnessMode::Simulation);
Mathias Agopianff2ed702013-09-01 21:36:12 -07003218 }
Mathias Agopianff2ed702013-09-01 21:36:12 -07003219 invalidateHwcGeometry();
3220 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07003221 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003222 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003223 case 1015: {
3224 // apply a color matrix
3225 n = data.readInt32();
Alan Viverette9c5a3332013-09-12 20:04:35 -07003226 if (n) {
3227 // color matrix is sent as mat3 matrix followed by vec3
3228 // offset, then packed into a mat4 where the last row is
3229 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07003230 for (size_t i = 0 ; i < 4; i++) {
Dan Stoza9f26a9c2016-06-22 14:51:09 -07003231 for (size_t j = 0; j < 4; j++) {
3232 mColorMatrix[i][j] = data.readFloat();
3233 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003234 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003235 } else {
3236 mColorMatrix = mat4();
3237 }
3238 invalidateHwcGeometry();
3239 repaintEverything();
3240 return NO_ERROR;
3241 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003242 // This is an experimental interface
3243 // Needs to be shifted to proper binder interface when we productize
3244 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07003245 n = data.readInt32();
3246 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003247 return NO_ERROR;
3248 }
Dan Stozaee44edd2015-03-23 15:50:23 -07003249 case 1017: {
3250 n = data.readInt32();
3251 mForceFullDamage = static_cast<bool>(n);
3252 return NO_ERROR;
3253 }
Dan Stozadb4ac3c2015-04-14 11:34:01 -07003254 case 1018: { // Modify Choreographer's phase offset
3255 n = data.readInt32();
3256 mEventThread->setPhaseOffset(static_cast<nsecs_t>(n));
3257 return NO_ERROR;
3258 }
3259 case 1019: { // Modify SurfaceFlinger's phase offset
3260 n = data.readInt32();
3261 mSFEventThread->setPhaseOffset(static_cast<nsecs_t>(n));
3262 return NO_ERROR;
3263 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003264 }
3265 }
3266 return err;
3267}
3268
Mathias Agopian53331da2011-08-22 21:44:41 -07003269void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07003270 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08003271 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07003272}
3273
Mathias Agopian59119e62010-10-11 12:37:43 -07003274// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003275// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07003276// ---------------------------------------------------------------------------
3277
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003278/* The code below is here to handle b/8734824
3279 *
3280 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07003281 * from the surfaceflinger thread to the calling binder thread, where they
3282 * are executed. This allows the calling thread in the calling process to be
3283 * reused and not depend on having "enough" binder threads to handle the
3284 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003285 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003286class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07003287 /* Parts of GraphicProducerWrapper are run on two different threads,
3288 * communicating by sending messages via Looper but also by shared member
3289 * data. Coherence maintenance is subtle and in places implicit (ugh).
3290 *
3291 * Don't rely on Looper's sendMessage/handleMessage providing
3292 * release/acquire semantics for any data not actually in the Message.
3293 * Data going from surfaceflinger to binder threads needs to be
3294 * synchronized explicitly.
3295 *
3296 * Barrier open/wait do provide release/acquire semantics. This provides
3297 * implicit synchronization for data coming back from binder to
3298 * surfaceflinger threads.
3299 */
3300
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003301 sp<IGraphicBufferProducer> impl;
3302 sp<Looper> looper;
3303 status_t result;
3304 bool exitPending;
3305 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07003306 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003307 uint32_t code;
3308 Parcel const* data;
3309 Parcel* reply;
3310
3311 enum {
3312 MSG_API_CALL,
3313 MSG_EXIT
3314 };
3315
3316 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003317 * Called on surfaceflinger thread. This is called by our "fake"
3318 * BpGraphicBufferProducer. We package the data and reply Parcel and
3319 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003320 */
3321 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08003322 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003323 this->code = code;
3324 this->data = &data;
3325 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003326 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07003327 // if we've exited, we run the message synchronously right here.
3328 // note (JH): as far as I can tell from looking at the code, this
3329 // never actually happens. if it does, i'm not sure if it happens
3330 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003331 handleMessage(Message(MSG_API_CALL));
3332 } else {
3333 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07003334 // Prevent stores to this->{code, data, reply} from being
3335 // reordered later than the construction of Message.
3336 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003337 looper->sendMessage(this, Message(MSG_API_CALL));
3338 barrier.wait();
3339 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08003340 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003341 }
3342
3343 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003344 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003345 * call the real BpGraphicBufferProducer.
3346 */
3347 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07003348 int what = message.what;
3349 // Prevent reads below from happening before the read from Message
3350 atomic_thread_fence(memory_order_acquire);
3351 if (what == MSG_API_CALL) {
Marco Nelissen097ca272014-11-14 08:01:01 -08003352 result = IInterface::asBinder(impl)->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003353 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07003354 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003355 exitRequested = true;
3356 }
3357 }
3358
3359public:
Jesse Hallb154c422014-07-13 12:47:02 -07003360 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
3361 : impl(impl),
3362 looper(new Looper(true)),
Pablo Ceballos53390e12015-08-04 11:25:59 -07003363 result(NO_ERROR),
Jesse Hallb154c422014-07-13 12:47:02 -07003364 exitPending(false),
Pablo Ceballos53390e12015-08-04 11:25:59 -07003365 exitRequested(false),
3366 code(0),
3367 data(NULL),
3368 reply(NULL)
Jesse Hallb154c422014-07-13 12:47:02 -07003369 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003370
Jesse Hallb154c422014-07-13 12:47:02 -07003371 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003372 status_t waitForResponse() {
3373 do {
3374 looper->pollOnce(-1);
3375 } while (!exitRequested);
3376 return result;
3377 }
3378
Jesse Hallb154c422014-07-13 12:47:02 -07003379 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003380 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07003381 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003382 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07003383 // Ensure this->result is visible to the binder thread before it
3384 // handles the message.
3385 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003386 looper->sendMessage(this, Message(MSG_EXIT));
3387 }
3388};
3389
3390
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003391status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
3392 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003393 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003394 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003395 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003396
3397 if (CC_UNLIKELY(display == 0))
3398 return BAD_VALUE;
3399
3400 if (CC_UNLIKELY(producer == 0))
3401 return BAD_VALUE;
3402
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003403 // if we have secure windows on this display, never allow the screen capture
3404 // unless the producer interface is local (i.e.: we can take a screenshot for
3405 // ourselves).
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003406 bool isLocalScreenshot = IInterface::asBinder(producer)->localBinder();
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003407
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003408 // Convert to surfaceflinger's internal rotation type.
3409 Transform::orientation_flags rotationFlags;
3410 switch (rotation) {
3411 case ISurfaceComposer::eRotateNone:
3412 rotationFlags = Transform::ROT_0;
3413 break;
3414 case ISurfaceComposer::eRotate90:
3415 rotationFlags = Transform::ROT_90;
3416 break;
3417 case ISurfaceComposer::eRotate180:
3418 rotationFlags = Transform::ROT_180;
3419 break;
3420 case ISurfaceComposer::eRotate270:
3421 rotationFlags = Transform::ROT_270;
3422 break;
3423 default:
3424 rotationFlags = Transform::ROT_0;
3425 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3426 break;
3427 }
3428
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003429 class MessageCaptureScreen : public MessageBase {
3430 SurfaceFlinger* flinger;
3431 sp<IBinder> display;
3432 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003433 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003434 uint32_t reqWidth, reqHeight;
3435 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003436 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003437 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003438 status_t result;
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003439 bool isLocalScreenshot;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003440 public:
3441 MessageCaptureScreen(SurfaceFlinger* flinger,
3442 const sp<IBinder>& display,
3443 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003444 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003445 uint32_t minLayerZ, uint32_t maxLayerZ,
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003446 bool useIdentityTransform,
3447 Transform::orientation_flags rotation,
3448 bool isLocalScreenshot)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003449 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003450 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003451 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003452 useIdentityTransform(useIdentityTransform),
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003453 rotation(rotation), result(PERMISSION_DENIED),
3454 isLocalScreenshot(isLocalScreenshot)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003455 {
3456 }
3457 status_t getResult() const {
3458 return result;
3459 }
3460 virtual bool handler() {
3461 Mutex::Autolock _l(flinger->mStateLock);
3462 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003463 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003464 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003465 useIdentityTransform, rotation, isLocalScreenshot);
Marco Nelissen097ca272014-11-14 08:01:01 -08003466 static_cast<GraphicProducerWrapper*>(IInterface::asBinder(producer).get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003467 return true;
3468 }
3469 };
3470
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003471 // this creates a "fake" BBinder which will serve as a "fake" remote
3472 // binder to receive the marshaled calls and forward them to the
3473 // real remote (a BpGraphicBufferProducer)
3474 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3475
3476 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3477 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003478 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003479 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003480 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003481 useIdentityTransform, rotationFlags, isLocalScreenshot);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003482
3483 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003484 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003485 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003486 }
3487 return res;
3488}
3489
Mathias Agopian180f10d2013-04-10 22:55:41 -07003490
3491void SurfaceFlinger::renderScreenImplLocked(
3492 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003493 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003494 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003495 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003496{
3497 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003498 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003499
3500 // get screen geometry
Andreas Gampe89fd4f72014-11-13 14:18:56 -08003501 const int32_t hw_w = hw->getWidth();
3502 const int32_t hw_h = hw->getHeight();
3503 const bool filtering = static_cast<int32_t>(reqWidth) != hw_w ||
Christopher Ferris0e749792015-03-23 14:32:15 -07003504 static_cast<int32_t>(reqHeight) != hw_h;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003505
Dan Stozac1879002014-05-22 15:59:05 -07003506 // if a default or invalid sourceCrop is passed in, set reasonable values
3507 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3508 !sourceCrop.isValid()) {
3509 sourceCrop.setLeftTop(Point(0, 0));
3510 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3511 }
3512
3513 // ensure that sourceCrop is inside screen
3514 if (sourceCrop.left < 0) {
3515 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3516 }
Dan Stozabe31f442014-06-11 11:20:54 -07003517 if (sourceCrop.right > hw_w) {
3518 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003519 }
3520 if (sourceCrop.top < 0) {
3521 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3522 }
Dan Stozabe31f442014-06-11 11:20:54 -07003523 if (sourceCrop.bottom > hw_h) {
3524 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003525 }
3526
Mathias Agopian180f10d2013-04-10 22:55:41 -07003527 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003528 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003529
3530 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003531 engine.setViewportAndProjection(
3532 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003533 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003534
3535 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003536 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003537
3538 const LayerVector& layers( mDrawingState.layersSortedByZ );
3539 const size_t count = layers.size();
3540 for (size_t i=0 ; i<count ; ++i) {
3541 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003542 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003543 if (state.layerStack == hw->getLayerStack()) {
3544 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3545 if (layer->isVisible()) {
3546 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08003547 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003548 if (filtering) layer->setFiltering(false);
3549 }
3550 }
3551 }
3552 }
3553
Mathias Agopian931bda12013-08-28 18:11:46 -07003554 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003555}
3556
3557
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003558status_t SurfaceFlinger::captureScreenImplLocked(
3559 const sp<const DisplayDevice>& hw,
3560 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003561 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003562 uint32_t minLayerZ, uint32_t maxLayerZ,
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003563 bool useIdentityTransform, Transform::orientation_flags rotation,
3564 bool isLocalScreenshot)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003565{
3566 ATRACE_CALL();
3567
Mathias Agopian180f10d2013-04-10 22:55:41 -07003568 // get screen geometry
Dan Stoza35024162015-06-09 16:44:40 -07003569 uint32_t hw_w = hw->getWidth();
3570 uint32_t hw_h = hw->getHeight();
3571
3572 if (rotation & Transform::ROT_90) {
3573 std::swap(hw_w, hw_h);
3574 }
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003575
Mathias Agopian180f10d2013-04-10 22:55:41 -07003576 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3577 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3578 reqWidth, reqHeight, hw_w, hw_h);
3579 return BAD_VALUE;
3580 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003581
Mathias Agopian180f10d2013-04-10 22:55:41 -07003582 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3583 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3584
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003585 bool secureLayerIsVisible = false;
3586 const LayerVector& layers(mDrawingState.layersSortedByZ);
3587 const size_t count = layers.size();
3588 for (size_t i = 0 ; i < count ; ++i) {
3589 const sp<Layer>& layer(layers[i]);
3590 const Layer::State& state(layer->getDrawingState());
3591 if (state.layerStack == hw->getLayerStack() && state.z >= minLayerZ &&
3592 state.z <= maxLayerZ && layer->isVisible() &&
3593 layer->isSecure()) {
3594 secureLayerIsVisible = true;
3595 }
3596 }
3597
3598 if (!isLocalScreenshot && secureLayerIsVisible) {
3599 ALOGW("FB is protected: PERMISSION_DENIED");
3600 return PERMISSION_DENIED;
3601 }
3602
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003603 // create a surface (because we're a producer, and we need to
3604 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003605 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003606 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003607
Michael Lentine5a16a622015-05-21 13:48:24 -07003608 status_t result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
3609 if (result == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003610 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3611 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003612
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003613 int err = 0;
3614 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003615 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003616 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3617 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003618
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003619 if (err == NO_ERROR) {
3620 ANativeWindowBuffer* buffer;
3621 /* TODO: Once we have the sync framework everywhere this can use
3622 * server-side waits on the fence that dequeueBuffer returns.
3623 */
3624 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3625 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003626 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003627 // create an EGLImage from the buffer so we can later
3628 // turn it into a texture
3629 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3630 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3631 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003632 // this binds the given EGLImage as a framebuffer for the
3633 // duration of this scope.
3634 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3635 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003636 // this will in fact render into our dequeued buffer
3637 // via an FBO, which means we didn't have to create
3638 // an EGLSurface and therefore we're not
3639 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003640 renderScreenImplLocked(
3641 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3642 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003643
Riley Andrews86639902014-08-15 12:27:24 -07003644 // Attempt to create a sync khr object that can produce a sync point. If that
3645 // isn't available, create a non-dupable sync object in the fallback path and
3646 // wait on it directly.
3647 EGLSyncKHR sync;
3648 if (!DEBUG_SCREENSHOTS) {
3649 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Riley Andrews9707f4d2014-10-23 16:17:04 -07003650 // native fence fd will not be populated until flush() is done.
3651 getRenderEngine().flush();
Andy McFadden2d8d1202013-10-09 16:38:02 -07003652 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003653 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003654 }
Riley Andrews86639902014-08-15 12:27:24 -07003655 if (sync != EGL_NO_SYNC_KHR) {
3656 // get the sync fd
3657 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3658 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3659 ALOGW("captureScreen: failed to dup sync khr object");
3660 syncFd = -1;
3661 }
3662 eglDestroySyncKHR(mEGLDisplay, sync);
3663 } else {
3664 // fallback path
3665 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3666 if (sync != EGL_NO_SYNC_KHR) {
3667 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3668 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3669 EGLint eglErr = eglGetError();
3670 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3671 ALOGW("captureScreen: fence wait timed out");
3672 } else {
3673 ALOGW_IF(eglErr != EGL_SUCCESS,
3674 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3675 }
3676 eglDestroySyncKHR(mEGLDisplay, sync);
3677 } else {
3678 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3679 }
3680 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003681 if (DEBUG_SCREENSHOTS) {
3682 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3683 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3684 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3685 hw, minLayerZ, maxLayerZ);
3686 delete [] pixels;
3687 }
3688
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003689 } else {
3690 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3691 result = INVALID_OPERATION;
Prathmesh Prabhuf3209b02016-03-09 16:54:45 -08003692 window->cancelBuffer(window, buffer, syncFd);
3693 buffer = NULL;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003694 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003695 // destroy our image
3696 eglDestroyImageKHR(mEGLDisplay, image);
3697 } else {
3698 result = BAD_VALUE;
3699 }
Prathmesh Prabhuf3209b02016-03-09 16:54:45 -08003700 if (buffer) {
3701 // queueBuffer takes ownership of syncFd
3702 result = window->queueBuffer(window, buffer, syncFd);
3703 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003704 }
3705 } else {
3706 result = BAD_VALUE;
3707 }
3708 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3709 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003710
Mathias Agopian74c40c02010-09-29 13:02:36 -07003711 return result;
3712}
3713
Mathias Agopiand5556842013-09-19 17:08:37 -07003714void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3715 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003716 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003717 for (size_t y=0 ; y<h ; y++) {
3718 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3719 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003720 if (p[x] != 0xFF000000) return;
3721 }
3722 }
3723 ALOGE("*** we just took a black screenshot ***\n"
3724 "requested minz=%d, maxz=%d, layerStack=%d",
3725 minLayerZ, maxLayerZ, hw->getLayerStack());
3726 const LayerVector& layers( mDrawingState.layersSortedByZ );
3727 const size_t count = layers.size();
3728 for (size_t i=0 ; i<count ; ++i) {
3729 const sp<Layer>& layer(layers[i]);
3730 const Layer::State& state(layer->getDrawingState());
3731 const bool visible = (state.layerStack == hw->getLayerStack())
3732 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3733 && (layer->isVisible());
Dan Stoza9e56aa02015-11-02 13:00:03 -08003734 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%.3f",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003735 visible ? '+' : '-',
3736 i, layer->getName().string(), state.layerStack, state.z,
3737 layer->isVisible(), state.flags, state.alpha);
3738 }
3739 }
3740}
3741
Pablo Ceballosce796e72016-02-04 19:10:51 -08003742bool SurfaceFlinger::getFrameTimestamps(const Layer& layer,
3743 uint64_t frameNumber, FrameTimestamps* outTimestamps) {
3744 return mFenceTracker.getFrameTimestamps(layer, frameNumber, outTimestamps);
3745}
3746
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003747// ---------------------------------------------------------------------------
3748
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003749SurfaceFlinger::LayerVector::LayerVector() {
3750}
3751
3752SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003753 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003754}
3755
3756int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3757 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003758{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003759 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003760 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3761 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003762
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003763 uint32_t ls = l->getCurrentState().layerStack;
3764 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003765 if (ls != rs)
3766 return ls - rs;
3767
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003768 uint32_t lz = l->getCurrentState().z;
3769 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003770 if (lz != rz)
3771 return lz - rz;
3772
3773 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003774}
3775
3776// ---------------------------------------------------------------------------
3777
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003778SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Pablo Ceballos53390e12015-08-04 11:25:59 -07003779 : type(DisplayDevice::DISPLAY_ID_INVALID),
3780 layerStack(DisplayDevice::NO_LAYER_STACK),
3781 orientation(0),
3782 width(0),
3783 height(0),
3784 isSecure(false) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003785}
3786
Pablo Ceballos53390e12015-08-04 11:25:59 -07003787SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(
3788 DisplayDevice::DisplayType type, bool isSecure)
3789 : type(type),
3790 layerStack(DisplayDevice::NO_LAYER_STACK),
3791 orientation(0),
3792 width(0),
3793 height(0),
3794 isSecure(isSecure) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003795 viewport.makeInvalid();
3796 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003797}
3798
3799// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003800
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003801}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003802
3803
3804#if defined(__gl_h_)
3805#error "don't include gl/gl.h in this file"
3806#endif
3807
3808#if defined(__gl2_h_)
3809#error "don't include gl2/gl2.h in this file"
3810#endif