blob: 3050d1cd7ac78419ce32d94084257546e49fe4b7 [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");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182}
183
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800184void SurfaceFlinger::onFirstRef()
185{
186 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800187}
188
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189SurfaceFlinger::~SurfaceFlinger()
190{
Mathias Agopiana4912602012-07-12 14:25:33 -0700191 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
192 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
193 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194}
195
Dan Stozac7014012014-02-14 15:03:43 -0800196void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800197{
198 // the window manager died on us. prepare its eulogy.
199
Andy McFadden13a082e2012-08-24 10:16:42 -0700200 // restore initial conditions (default device unblank, etc)
201 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800202
203 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700204 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800205}
206
Mathias Agopian7e27f052010-05-28 14:22:23 -0700207sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208{
Mathias Agopian96f08192010-06-02 23:28:45 -0700209 sp<ISurfaceComposerClient> bclient;
210 sp<Client> client(new Client(this));
211 status_t err = client->initCheck();
212 if (err == NO_ERROR) {
213 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 return bclient;
216}
217
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700218sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
219 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700220{
221 class DisplayToken : public BBinder {
222 sp<SurfaceFlinger> flinger;
223 virtual ~DisplayToken() {
224 // no more references, this display must be terminated
225 Mutex::Autolock _l(flinger->mStateLock);
226 flinger->mCurrentState.displays.removeItem(this);
227 flinger->setTransactionFlags(eDisplayTransactionNeeded);
228 }
229 public:
230 DisplayToken(const sp<SurfaceFlinger>& flinger)
231 : flinger(flinger) {
232 }
233 };
234
235 sp<BBinder> token = new DisplayToken(this);
236
237 Mutex::Autolock _l(mStateLock);
Pablo Ceballos53390e12015-08-04 11:25:59 -0700238 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL, secure);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700239 info.displayName = displayName;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700240 mCurrentState.displays.add(token, info);
241
242 return token;
243}
244
Jesse Hall6c913be2013-08-08 12:15:49 -0700245void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
246 Mutex::Autolock _l(mStateLock);
247
248 ssize_t idx = mCurrentState.displays.indexOfKey(display);
249 if (idx < 0) {
250 ALOGW("destroyDisplay: invalid display token");
251 return;
252 }
253
254 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
255 if (!info.isVirtualDisplay()) {
256 ALOGE("destroyDisplay called for non-virtual display");
257 return;
258 }
259
260 mCurrentState.displays.removeItemsAt(idx);
261 setTransactionFlags(eDisplayTransactionNeeded);
262}
263
Jesse Hall692c7232012-11-08 15:41:56 -0800264void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800265 ALOGV("createBuiltinDisplayLocked(%d)", type);
Jesse Hall692c7232012-11-08 15:41:56 -0800266 ALOGW_IF(mBuiltinDisplays[type],
267 "Overwriting display token for display type %d", type);
268 mBuiltinDisplays[type] = new BBinder();
Jesse Hall692c7232012-11-08 15:41:56 -0800269 // All non-virtual displays are currently considered secure.
Pablo Ceballos53390e12015-08-04 11:25:59 -0700270 DisplayDeviceState info(type, true);
Jesse Hall692c7232012-11-08 15:41:56 -0800271 mCurrentState.displays.add(mBuiltinDisplays[type], info);
272}
273
Mathias Agopiane57f2922012-08-09 16:29:12 -0700274sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700275 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700276 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
277 return NULL;
278 }
Jesse Hall692c7232012-11-08 15:41:56 -0800279 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700280}
281
Jamie Gennis9a78c902011-01-12 18:30:40 -0800282sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
283{
284 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
285 return gba;
286}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700287
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288void SurfaceFlinger::bootFinished()
289{
290 const nsecs_t now = systemTime();
291 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000292 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700293 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700294
295 // wait patiently for the window manager death
296 const String16 name("window");
297 sp<IBinder> window(defaultServiceManager()->getService(name));
298 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700299 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700300 }
301
302 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700303 // formerly we would just kill the process, but we now ask it to exit so it
304 // can choose where to stop the animation.
305 property_set("service.bootanim.exit", "1");
Yusuke Sato0a688f52015-07-30 23:05:39 -0700306
307 const int LOGTAG_SF_STOP_BOOTANIM = 60110;
308 LOG_EVENT_LONG(LOGTAG_SF_STOP_BOOTANIM,
309 ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310}
311
Mathias Agopian3f844832013-08-07 21:24:32 -0700312void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700313 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700314 RenderEngine& engine;
315 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700316 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700317 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
318 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700319 }
320 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700321 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700322 return true;
323 }
324 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700325 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700326}
327
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700328class DispSyncSource : public VSyncSource, private DispSync::Callback {
329public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700330 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
Tim Murray4a4e4a22016-04-19 16:29:23 +0000331 const char* name) :
332 mName(name),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700333 mValue(0),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700334 mTraceVsync(traceVsync),
Tim Murray4a4e4a22016-04-19 16:29:23 +0000335 mVsyncOnLabel(String8::format("VsyncOn-%s", name)),
336 mVsyncEventLabel(String8::format("VSYNC-%s", name)),
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700337 mDispSync(dispSync),
338 mCallbackMutex(),
339 mCallback(),
340 mVsyncMutex(),
341 mPhaseOffset(phaseOffset),
342 mEnabled(false) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700343
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700344 virtual ~DispSyncSource() {}
345
346 virtual void setVSyncEnabled(bool enable) {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700347 Mutex::Autolock lock(mVsyncMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700348 if (enable) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000349 status_t err = mDispSync->addEventListener(mName, mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700350 static_cast<DispSync::Callback*>(this));
351 if (err != NO_ERROR) {
352 ALOGE("error registering vsync callback: %s (%d)",
353 strerror(-err), err);
354 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700355 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700356 } else {
357 status_t err = mDispSync->removeEventListener(
358 static_cast<DispSync::Callback*>(this));
359 if (err != NO_ERROR) {
360 ALOGE("error unregistering vsync callback: %s (%d)",
361 strerror(-err), err);
362 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700363 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700364 }
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700365 mEnabled = enable;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700366 }
367
368 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700369 Mutex::Autolock lock(mCallbackMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700370 mCallback = callback;
371 }
372
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700373 virtual void setPhaseOffset(nsecs_t phaseOffset) {
374 Mutex::Autolock lock(mVsyncMutex);
375
376 // Normalize phaseOffset to [0, period)
377 auto period = mDispSync->getPeriod();
378 phaseOffset %= period;
379 if (phaseOffset < 0) {
380 // If we're here, then phaseOffset is in (-period, 0). After this
381 // operation, it will be in (0, period)
382 phaseOffset += period;
383 }
384 mPhaseOffset = phaseOffset;
385
386 // If we're not enabled, we don't need to mess with the listeners
387 if (!mEnabled) {
388 return;
389 }
390
391 // Remove the listener with the old offset
392 status_t err = mDispSync->removeEventListener(
393 static_cast<DispSync::Callback*>(this));
394 if (err != NO_ERROR) {
395 ALOGE("error unregistering vsync callback: %s (%d)",
396 strerror(-err), err);
397 }
398
399 // Add a listener with the new offset
Tim Murray4a4e4a22016-04-19 16:29:23 +0000400 err = mDispSync->addEventListener(mName, mPhaseOffset,
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700401 static_cast<DispSync::Callback*>(this));
402 if (err != NO_ERROR) {
403 ALOGE("error registering vsync callback: %s (%d)",
404 strerror(-err), err);
405 }
406 }
407
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700408private:
409 virtual void onDispSyncEvent(nsecs_t when) {
410 sp<VSyncSource::Callback> callback;
411 {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700412 Mutex::Autolock lock(mCallbackMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700413 callback = mCallback;
414
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700415 if (mTraceVsync) {
416 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700417 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700418 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700419 }
420
421 if (callback != NULL) {
422 callback->onVSyncEvent(when);
423 }
424 }
425
Tim Murray4a4e4a22016-04-19 16:29:23 +0000426 const char* const mName;
427
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700428 int mValue;
429
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700430 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700431 const String8 mVsyncOnLabel;
432 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700433
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700434 DispSync* mDispSync;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700435
436 Mutex mCallbackMutex; // Protects the following
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700437 sp<VSyncSource::Callback> mCallback;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700438
439 Mutex mVsyncMutex; // Protects the following
440 nsecs_t mPhaseOffset;
441 bool mEnabled;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700442};
443
444void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700445 ALOGI( "SurfaceFlinger's main thread ready to run. "
446 "Initializing graphics H/W...");
447
Dan Stoza9e56aa02015-11-02 13:00:03 -0800448 { // Autolock scope
449 Mutex::Autolock _l(mStateLock);
450
451 // initialize EGL for the default display
452 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
453 eglInitialize(mEGLDisplay, NULL, NULL);
454
455 // start the EventThread
456 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
457 vsyncPhaseOffsetNs, true, "app");
Tim Murray4a4e4a22016-04-19 16:29:23 +0000458 mEventThread = new EventThread(vsyncSrc, *this);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800459 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
460 sfVsyncPhaseOffsetNs, true, "sf");
Tim Murray4a4e4a22016-04-19 16:29:23 +0000461 mSFEventThread = new EventThread(sfVsyncSrc, *this);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800462 mEventQueue.setEventThread(mSFEventThread);
463
Tim Murray41a38532016-06-22 12:42:10 -0700464 // set EventThread and SFEventThread to SCHED_FIFO for minimum jitter
465 struct sched_param param = {0};
466 param.sched_priority = 1;
467 if (sched_setscheduler(mEventThread->getTid(), SCHED_FIFO, &param) != 0) {
468 ALOGE("Couldn't set SCHED_FIFO for EventThread");
469 }
470
471 if (sched_setscheduler(mSFEventThread->getTid(), SCHED_FIFO, &param) != 0) {
472 ALOGE("Couldn't set SCHED_FIFO for SFEventThread");
473 }
474
475
Dan Stoza9e56aa02015-11-02 13:00:03 -0800476 // Get a RenderEngine for the given display / config (can't fail)
477 mRenderEngine = RenderEngine::create(mEGLDisplay,
478 HAL_PIXEL_FORMAT_RGBA_8888);
479 }
480
481 // Drop the state lock while we initialize the hardware composer. We drop
482 // the lock because on creation, it will call back into SurfaceFlinger to
483 // initialize the primary display.
484 mHwc = new HWComposer(this);
485 mHwc->setEventHandler(static_cast<HWComposer::EventHandler*>(this));
486
Jesse Hall692c7232012-11-08 15:41:56 -0800487 Mutex::Autolock _l(mStateLock);
488
Mathias Agopian875d8e12013-06-07 15:35:48 -0700489 // retrieve the EGL context that was selected/created
490 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700491
Mathias Agopianda27af92012-09-13 18:17:13 -0700492 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
493 "couldn't create EGLContext");
494
Dan Stoza9e56aa02015-11-02 13:00:03 -0800495 // make the GLContext current so that we can create textures when creating
496 // Layers (which may happens before we render something)
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700497 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
498
Jamie Gennisd1700752013-10-14 12:22:52 -0700499 mEventControlThread = new EventControlThread(this);
500 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
501
Mathias Agopian92a979a2012-08-02 18:32:23 -0700502 // initialize our drawing state
503 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700504
Andy McFadden13a082e2012-08-24 10:16:42 -0700505 // set initial conditions (e.g. unblank default device)
506 initializeDisplays();
507
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700508 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700509 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800510
Dan Stoza9e56aa02015-11-02 13:00:03 -0800511 ALOGV("Done initializing");
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700512}
513
Mathias Agopiana67e4182012-06-19 17:26:12 -0700514void SurfaceFlinger::startBootAnim() {
515 // start boot animation
516 property_set("service.bootanim.exit", "0");
517 property_set("ctl.start", "bootanim");
518}
519
Mathias Agopian875d8e12013-06-07 15:35:48 -0700520size_t SurfaceFlinger::getMaxTextureSize() const {
521 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700522}
523
Mathias Agopian875d8e12013-06-07 15:35:48 -0700524size_t SurfaceFlinger::getMaxViewportDims() const {
525 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700526}
527
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800528// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800529
Jamie Gennis582270d2011-08-17 18:19:00 -0700530bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800531 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800532 Mutex::Autolock _l(mStateLock);
Marco Nelissen097ca272014-11-14 08:01:01 -0800533 sp<IBinder> surfaceTextureBinder(IInterface::asBinder(bufferProducer));
Mathias Agopian67106042013-03-14 19:18:13 -0700534 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800535}
536
Dan Stoza7f7da322014-05-02 15:26:25 -0700537status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
538 Vector<DisplayInfo>* configs) {
Tatenda Chipeperekwa23e16bb2014-10-29 16:47:19 -0700539 if ((configs == NULL) || (display.get() == NULL)) {
Dan Stoza7f7da322014-05-02 15:26:25 -0700540 return BAD_VALUE;
541 }
542
Naseer Ahmed7aa0c472014-11-03 14:49:23 -0500543 if (!display.get())
544 return NAME_NOT_FOUND;
545
Jesse Hall692c7232012-11-08 15:41:56 -0800546 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700547 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800548 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700549 type = i;
550 break;
551 }
552 }
553
554 if (type < 0) {
555 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700556 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700557
Mathias Agopian8b736f12012-08-13 17:54:26 -0700558 // TODO: Not sure if display density should handled by SF any longer
559 class Density {
560 static int getDensityFromProperty(char const* propName) {
561 char property[PROPERTY_VALUE_MAX];
562 int density = 0;
563 if (property_get(propName, property, NULL) > 0) {
564 density = atoi(property);
565 }
566 return density;
567 }
568 public:
569 static int getEmuDensity() {
570 return getDensityFromProperty("qemu.sf.lcd_density"); }
571 static int getBuildDensity() {
572 return getDensityFromProperty("ro.sf.lcd_density"); }
573 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700574
Dan Stoza7f7da322014-05-02 15:26:25 -0700575 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700576
Dan Stoza9e56aa02015-11-02 13:00:03 -0800577 for (const auto& hwConfig : getHwComposer().getConfigs(type)) {
Dan Stoza7f7da322014-05-02 15:26:25 -0700578 DisplayInfo info = DisplayInfo();
579
Dan Stoza9e56aa02015-11-02 13:00:03 -0800580 float xdpi = hwConfig->getDpiX();
581 float ydpi = hwConfig->getDpiY();
Dan Stoza7f7da322014-05-02 15:26:25 -0700582
583 if (type == DisplayDevice::DISPLAY_PRIMARY) {
584 // The density of the device is provided by a build property
585 float density = Density::getBuildDensity() / 160.0f;
586 if (density == 0) {
587 // the build doesn't provide a density -- this is wrong!
588 // use xdpi instead
589 ALOGE("ro.sf.lcd_density must be defined as a build property");
590 density = xdpi / 160.0f;
591 }
592 if (Density::getEmuDensity()) {
593 // if "qemu.sf.lcd_density" is specified, it overrides everything
594 xdpi = ydpi = density = Density::getEmuDensity();
595 density /= 160.0f;
596 }
597 info.density = density;
598
599 // TODO: this needs to go away (currently needed only by webkit)
600 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
601 info.orientation = hw->getOrientation();
602 } else {
603 // TODO: where should this value come from?
604 static const int TV_DENSITY = 213;
605 info.density = TV_DENSITY / 160.0f;
606 info.orientation = 0;
607 }
608
Dan Stoza9e56aa02015-11-02 13:00:03 -0800609 info.w = hwConfig->getWidth();
610 info.h = hwConfig->getHeight();
Dan Stoza7f7da322014-05-02 15:26:25 -0700611 info.xdpi = xdpi;
612 info.ydpi = ydpi;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800613 info.fps = 1e9 / hwConfig->getVsyncPeriod();
Andy McFadden91b2ca82014-06-13 14:04:23 -0700614 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800615
Andy McFadden91b2ca82014-06-13 14:04:23 -0700616 // This is how far in advance a buffer must be queued for
617 // presentation at a given time. If you want a buffer to appear
618 // on the screen at time N, you must submit the buffer before
619 // (N - presentationDeadline).
620 //
621 // Normally it's one full refresh period (to give SF a chance to
622 // latch the buffer), but this can be reduced by configuring a
623 // DispSync offset. Any additional delays introduced by the hardware
624 // composer or panel must be accounted for here.
625 //
626 // We add an additional 1ms to allow for processing time and
627 // differences between the ideal and actual refresh rate.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800628 info.presentationDeadline = hwConfig->getVsyncPeriod() -
629 SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700630
631 // All non-virtual displays are currently considered secure.
632 info.secure = true;
633
Michael Wright28f24d02016-07-12 13:30:53 -0700634 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700635 }
636
Dan Stoza7f7da322014-05-02 15:26:25 -0700637 return NO_ERROR;
638}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700639
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800640status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& /* display */,
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700641 DisplayStatInfo* stats) {
642 if (stats == NULL) {
643 return BAD_VALUE;
644 }
645
646 // FIXME for now we always return stats for the primary display
647 memset(stats, 0, sizeof(*stats));
648 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
649 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
650 return NO_ERROR;
651}
652
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700653int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
Dan Stoza24a42e92015-03-09 10:04:11 -0700654 sp<DisplayDevice> device(getDisplayDevice(display));
655 if (device != NULL) {
656 return device->getActiveConfig();
657 }
658 return BAD_VALUE;
Dan Stoza7f7da322014-05-02 15:26:25 -0700659}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700660
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700661void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
662 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
663 this);
664 int32_t type = hw->getDisplayType();
665 int currentMode = hw->getActiveConfig();
666
667 if (mode == currentMode) {
668 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
669 return;
670 }
671
672 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
673 ALOGW("Trying to set config for virtual display");
674 return;
675 }
676
677 hw->setActiveConfig(mode);
678 getHwComposer().setActiveConfig(type, mode);
679}
680
681status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
682 class MessageSetActiveConfig: public MessageBase {
683 SurfaceFlinger& mFlinger;
684 sp<IBinder> mDisplay;
685 int mMode;
686 public:
687 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
688 int mode) :
689 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
690 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700691 Vector<DisplayInfo> configs;
692 mFlinger.getDisplayConfigs(mDisplay, &configs);
Jesse Hall78442112014-08-07 22:43:06 -0700693 if (mMode < 0 || mMode >= static_cast<int>(configs.size())) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700694 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700695 mMode, configs.size());
Michael Wright28f24d02016-07-12 13:30:53 -0700696 return true;
Michael Lentine7306c672014-07-30 13:00:37 -0700697 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700698 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
699 if (hw == NULL) {
700 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700701 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700702 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
703 ALOGW("Attempt to set active config = %d for virtual display",
704 mMode);
705 } else {
706 mFlinger.setActiveConfigInternal(hw, mMode);
707 }
708 return true;
709 }
710 };
711 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
712 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700713 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700714}
Michael Wright28f24d02016-07-12 13:30:53 -0700715status_t SurfaceFlinger::getDisplayColorModes(const sp<IBinder>& display,
716 Vector<android_color_mode_t>* outColorModes) {
717 if ((outColorModes == nullptr) || (display.get() == nullptr)) {
718 return BAD_VALUE;
719 }
720
721 if (!display.get()) {
722 return NAME_NOT_FOUND;
723 }
724
725 int32_t type = NAME_NOT_FOUND;
726 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
727 if (display == mBuiltinDisplays[i]) {
728 type = i;
729 break;
730 }
731 }
732
733 if (type < 0) {
734 return type;
735 }
736
737 std::vector<android_color_mode_t> modes = getHwComposer().getColorModes(type);
738 outColorModes->clear();
739 std::copy(modes.cbegin(), modes.cend(), std::back_inserter(*outColorModes));
740
741 return NO_ERROR;
742}
743
744android_color_mode_t SurfaceFlinger::getActiveColorMode(const sp<IBinder>& display) {
745 sp<DisplayDevice> device(getDisplayDevice(display));
746 if (device != nullptr) {
747 return device->getActiveColorMode();
748 }
749 return static_cast<android_color_mode_t>(BAD_VALUE);
750}
751
752void SurfaceFlinger::setActiveColorModeInternal(const sp<DisplayDevice>& hw,
753 android_color_mode_t mode) {
754 ALOGD("Set active color mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
755 this);
756 int32_t type = hw->getDisplayType();
757 android_color_mode_t currentMode = hw->getActiveColorMode();
758
759 if (mode == currentMode) {
760 ALOGD("Screen type=%d is already in color mode=%d", hw->getDisplayType(), mode);
761 return;
762 }
763
764 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
765 ALOGW("Trying to set config for virtual display");
766 return;
767 }
768
769 hw->setActiveColorMode(mode);
770 getHwComposer().setActiveColorMode(type, mode);
771}
772
773
774status_t SurfaceFlinger::setActiveColorMode(const sp<IBinder>& display,
775 android_color_mode_t colorMode) {
776 class MessageSetActiveColorMode: public MessageBase {
777 SurfaceFlinger& mFlinger;
778 sp<IBinder> mDisplay;
779 android_color_mode_t mMode;
780 public:
781 MessageSetActiveColorMode(SurfaceFlinger& flinger, const sp<IBinder>& disp,
782 android_color_mode_t mode) :
783 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
784 virtual bool handler() {
785 Vector<android_color_mode_t> modes;
786 mFlinger.getDisplayColorModes(mDisplay, &modes);
787 bool exists = std::find(std::begin(modes), std::end(modes), mMode) != std::end(modes);
788 if (mMode < 0 || !exists) {
789 ALOGE("Attempt to set invalid active color mode = %d for display %p", mMode,
790 mDisplay.get());
791 return true;
792 }
793 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
794 if (hw == nullptr) {
795 ALOGE("Attempt to set active color mode = %d for null display %p",
796 mMode, mDisplay.get());
797 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
798 ALOGW("Attempt to set active color mode= %d for virtual display",
799 mMode);
800 } else {
801 mFlinger.setActiveColorModeInternal(hw, mMode);
802 }
803 return true;
804 }
805 };
806 sp<MessageBase> msg = new MessageSetActiveColorMode(*this, display, colorMode);
807 postMessageSync(msg);
808 return NO_ERROR;
809}
Mathias Agopianc666cae2012-07-25 18:56:13 -0700810
Svetoslavd85084b2014-03-20 10:28:31 -0700811status_t SurfaceFlinger::clearAnimationFrameStats() {
812 Mutex::Autolock _l(mStateLock);
813 mAnimFrameTracker.clearStats();
814 return NO_ERROR;
815}
816
817status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
818 Mutex::Autolock _l(mStateLock);
819 mAnimFrameTracker.getStats(outStats);
820 return NO_ERROR;
821}
822
Dan Stozac4f471e2016-03-24 09:31:08 -0700823status_t SurfaceFlinger::getHdrCapabilities(const sp<IBinder>& display,
824 HdrCapabilities* outCapabilities) const {
825 Mutex::Autolock _l(mStateLock);
826
827 sp<const DisplayDevice> displayDevice(getDisplayDevice(display));
828 if (displayDevice == nullptr) {
829 ALOGE("getHdrCapabilities: Invalid display %p", displayDevice.get());
830 return BAD_VALUE;
831 }
832
833 std::unique_ptr<HdrCapabilities> capabilities =
834 mHwc->getHdrCapabilities(displayDevice->getHwcDisplayId());
835 if (capabilities) {
836 std::swap(*outCapabilities, *capabilities);
837 } else {
838 return BAD_VALUE;
839 }
840
841 return NO_ERROR;
842}
843
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800844// ----------------------------------------------------------------------------
845
846sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800847 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700848}
849
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800850// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800851
852void SurfaceFlinger::waitForEvent() {
853 mEventQueue.waitMessage();
854}
855
856void SurfaceFlinger::signalTransaction() {
857 mEventQueue.invalidate();
858}
859
860void SurfaceFlinger::signalLayerUpdate() {
861 mEventQueue.invalidate();
862}
863
864void SurfaceFlinger::signalRefresh() {
865 mEventQueue.refresh();
866}
867
868status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800869 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800870 return mEventQueue.postMessage(msg, reltime);
871}
872
873status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800874 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800875 status_t res = mEventQueue.postMessage(msg, reltime);
876 if (res == NO_ERROR) {
877 msg->wait();
878 }
879 return res;
880}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800881
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700882void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700883 do {
884 waitForEvent();
885 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800886}
887
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700888void SurfaceFlinger::enableHardwareVsync() {
889 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700890 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700891 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700892 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
893 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700894 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700895 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700896}
897
Jesse Hall948fe0c2013-10-14 12:56:09 -0700898void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700899 Mutex::Autolock _l(mHWVsyncLock);
900
Jesse Hall948fe0c2013-10-14 12:56:09 -0700901 if (makeAvailable) {
902 mHWVsyncAvailable = true;
903 } else if (!mHWVsyncAvailable) {
Dan Stoza0a3c4d62016-04-19 11:56:20 -0700904 // Hardware vsync is not currently available, so abort the resync
905 // attempt for now
Jesse Hall948fe0c2013-10-14 12:56:09 -0700906 return;
907 }
908
Dan Stoza9e56aa02015-11-02 13:00:03 -0800909 const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
910 const nsecs_t period = activeConfig->getVsyncPeriod();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700911
912 mPrimaryDispSync.reset();
913 mPrimaryDispSync.setPeriod(period);
914
915 if (!mPrimaryHWVsyncEnabled) {
916 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700917 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
918 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700919 mPrimaryHWVsyncEnabled = true;
920 }
921}
922
Jesse Hall948fe0c2013-10-14 12:56:09 -0700923void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700924 Mutex::Autolock _l(mHWVsyncLock);
925 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700926 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
927 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700928 mPrimaryDispSync.endResync();
929 mPrimaryHWVsyncEnabled = false;
930 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700931 if (makeUnavailable) {
932 mHWVsyncAvailable = false;
933 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700934}
935
Tim Murray4a4e4a22016-04-19 16:29:23 +0000936void SurfaceFlinger::resyncWithRateLimit() {
937 static constexpr nsecs_t kIgnoreDelay = ms2ns(500);
938 if (systemTime() - mLastSwapTime > kIgnoreDelay) {
Dan Stoza0a3c4d62016-04-19 11:56:20 -0700939 resyncToHardwareVsync(false);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000940 }
941}
942
Dan Stoza9e56aa02015-11-02 13:00:03 -0800943void SurfaceFlinger::onVSyncReceived(int32_t type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700944 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700945
Jamie Gennisd1700752013-10-14 12:22:52 -0700946 { // Scope for the lock
947 Mutex::Autolock _l(mHWVsyncLock);
948 if (type == 0 && mPrimaryHWVsyncEnabled) {
949 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700950 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700951 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700952
953 if (needsHwVsync) {
954 enableHardwareVsync();
955 } else {
956 disableHardwareVsync(false);
957 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700958}
959
Dan Stoza9e56aa02015-11-02 13:00:03 -0800960void SurfaceFlinger::onHotplugReceived(int32_t disp, bool connected) {
961 ALOGV("onHotplugReceived(%d, %s)", disp, connected ? "true" : "false");
962 if (disp == DisplayDevice::DISPLAY_PRIMARY) {
963 Mutex::Autolock lock(mStateLock);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700964
Dan Stoza9e56aa02015-11-02 13:00:03 -0800965 // All non-virtual displays are currently considered secure.
966 bool isSecure = true;
967
968 int32_t type = DisplayDevice::DISPLAY_PRIMARY;
969 createBuiltinDisplayLocked(DisplayDevice::DISPLAY_PRIMARY);
970 wp<IBinder> token = mBuiltinDisplays[type];
971
972 sp<IGraphicBufferProducer> producer;
973 sp<IGraphicBufferConsumer> consumer;
974 BufferQueue::createBufferQueue(&producer, &consumer,
975 new GraphicBufferAlloc());
976
977 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc,
978 DisplayDevice::DISPLAY_PRIMARY, consumer);
979 sp<DisplayDevice> hw = new DisplayDevice(this,
980 DisplayDevice::DISPLAY_PRIMARY, disp, isSecure, token, fbs,
981 producer, mRenderEngine->getEGLConfig());
982 mDisplays.add(token, hw);
983 } else {
984 auto type = DisplayDevice::DISPLAY_EXTERNAL;
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700985 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800986 if (connected) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800987 createBuiltinDisplayLocked(type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700988 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800989 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
990 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700991 }
992 setTransactionFlags(eDisplayTransactionNeeded);
993
Andy McFadden9e9689c2012-10-10 18:17:51 -0700994 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700995 }
Mathias Agopian86303202012-07-24 22:46:10 -0700996}
997
Dan Stoza9e56aa02015-11-02 13:00:03 -0800998void SurfaceFlinger::setVsyncEnabled(int disp, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700999 ATRACE_CALL();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001000 getHwComposer().setVsyncEnabled(disp,
1001 enabled ? HWC2::Vsync::Enable : HWC2::Vsync::Disable);
Mathias Agopian86303202012-07-24 22:46:10 -07001002}
1003
Mathias Agopian4fec8732012-06-29 14:12:52 -07001004void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001005 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001006 switch (what) {
Dan Stoza6b9454d2014-11-07 16:00:59 -08001007 case MessageQueue::INVALIDATE: {
Dan Stoza50182882016-07-08 12:02:20 -07001008 bool frameMissed = !mHadClientComposition &&
1009 mPreviousPresentFence != Fence::NO_FENCE &&
1010 mPreviousPresentFence->getSignalTime() == INT64_MAX;
1011 ATRACE_INT("FrameMissed", static_cast<int>(frameMissed));
1012 if (frameMissed) {
1013 signalLayerUpdate();
1014 break;
1015 }
1016
Dan Stoza6b9454d2014-11-07 16:00:59 -08001017 bool refreshNeeded = handleMessageTransaction();
1018 refreshNeeded |= handleMessageInvalidate();
Dan Stoza58784442014-12-02 16:58:17 -08001019 refreshNeeded |= mRepaintEverything;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001020 if (refreshNeeded) {
Dan Stoza58784442014-12-02 16:58:17 -08001021 // Signal a refresh if a transaction modified the window state,
1022 // a new buffer was latched, or if HWC has requested a full
1023 // repaint
Dan Stoza6b9454d2014-11-07 16:00:59 -08001024 signalRefresh();
1025 }
1026 break;
1027 }
1028 case MessageQueue::REFRESH: {
1029 handleMessageRefresh();
1030 break;
1031 }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001032 }
1033}
1034
Dan Stoza6b9454d2014-11-07 16:00:59 -08001035bool SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001036 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001037 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -07001038 handleTransaction(transactionFlags);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001039 return true;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001040 }
Dan Stoza6b9454d2014-11-07 16:00:59 -08001041 return false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001042}
1043
Dan Stoza6b9454d2014-11-07 16:00:59 -08001044bool SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001045 ATRACE_CALL();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001046 return handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001047}
1048
1049void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001050 ATRACE_CALL();
Dan Stoza14cd37c2015-07-09 12:43:33 -07001051
Pablo Ceballos40845df2016-01-25 17:41:15 -08001052 nsecs_t refreshStartTime = systemTime(SYSTEM_TIME_MONOTONIC);
Dan Stoza14cd37c2015-07-09 12:43:33 -07001053
Dan Stoza05dacfb2016-07-01 13:33:38 -07001054 preComposition();
1055 rebuildLayerStacks();
1056 setUpHWComposer();
1057 doDebugFlashRegions();
1058 doComposition();
1059 postComposition(refreshStartTime);
1060
1061 mPreviousPresentFence = mHwc->getRetireFence(HWC_DISPLAY_PRIMARY);
1062 mHadClientComposition = mHwc->hasClientComposition(HWC_DISPLAY_PRIMARY);
Dan Stoza14cd37c2015-07-09 12:43:33 -07001063
Dan Stoza9e56aa02015-11-02 13:00:03 -08001064 // Release any buffers which were replaced this frame
1065 for (auto& layer : mLayersWithQueuedFrames) {
1066 layer->releasePendingBuffer();
1067 }
1068 mLayersWithQueuedFrames.clear();
Mathias Agopiancd60f992012-08-16 16:28:27 -07001069}
Mathias Agopian4fec8732012-06-29 14:12:52 -07001070
Mathias Agopiancd60f992012-08-16 16:28:27 -07001071void SurfaceFlinger::doDebugFlashRegions()
1072{
1073 // is debugging enabled
1074 if (CC_LIKELY(!mDebugRegion))
1075 return;
1076
1077 const bool repaintEverything = mRepaintEverything;
1078 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1079 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001080 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001081 // transform the dirty region into this screen's coordinate space
1082 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
1083 if (!dirtyRegion.isEmpty()) {
1084 // redraw the whole screen
1085 doComposeSurfaces(hw, Region(hw->bounds()));
1086
1087 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -07001088 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001089 RenderEngine& engine(getRenderEngine());
1090 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
1091
Mathias Agopianda27af92012-09-13 18:17:13 -07001092 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -07001093 }
1094 }
1095 }
1096
1097 postFramebuffer();
1098
1099 if (mDebugRegion > 1) {
1100 usleep(mDebugRegion * 1000);
1101 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -07001102
Dan Stoza9e56aa02015-11-02 13:00:03 -08001103 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
Dan Stoza7bdf55a2016-06-17 11:29:01 -07001104 auto& displayDevice = mDisplays[displayId];
1105 if (!displayDevice->isDisplayOn()) {
1106 continue;
1107 }
1108
1109 status_t result = displayDevice->prepareFrame(*mHwc);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001110 ALOGE_IF(result != NO_ERROR, "prepareFrame for display %zd failed:"
1111 " %d (%s)", displayId, result, strerror(-result));
Mathias Agopianbb53b0e2012-09-24 21:27:29 -07001112 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001113}
1114
1115void SurfaceFlinger::preComposition()
1116{
Dan Stoza9e56aa02015-11-02 13:00:03 -08001117 ATRACE_CALL();
1118 ALOGV("preComposition");
1119
Mathias Agopiancd60f992012-08-16 16:28:27 -07001120 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001121 const LayerVector& layers(mDrawingState.layersSortedByZ);
1122 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -07001123 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001124 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001125 needExtraInvalidate = true;
1126 }
1127 }
1128 if (needExtraInvalidate) {
1129 signalLayerUpdate();
1130 }
1131}
1132
Pablo Ceballos40845df2016-01-25 17:41:15 -08001133void SurfaceFlinger::postComposition(nsecs_t refreshStartTime)
Mathias Agopiancd60f992012-08-16 16:28:27 -07001134{
Dan Stoza9e56aa02015-11-02 13:00:03 -08001135 ATRACE_CALL();
1136 ALOGV("postComposition");
1137
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001138 const LayerVector& layers(mDrawingState.layersSortedByZ);
1139 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -07001140 for (size_t i=0 ; i<count ; i++) {
Dan Stozae77c7662016-05-13 11:37:28 -07001141 bool frameLatched = layers[i]->onPostComposition();
1142 if (frameLatched) {
1143 recordBufferingStats(layers[i]->getName().string(),
1144 layers[i]->getOccupancyHistory(false));
1145 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001146 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001147
Dan Stoza9e56aa02015-11-02 13:00:03 -08001148 sp<Fence> presentFence = mHwc->getRetireFence(HWC_DISPLAY_PRIMARY);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001149
1150 if (presentFence->isValid()) {
1151 if (mPrimaryDispSync.addPresentFence(presentFence)) {
1152 enableHardwareVsync();
1153 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -07001154 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001155 }
1156 }
1157
Dan Stozab90cf072015-03-05 11:05:59 -08001158 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Andy McFadden5167ec62014-05-22 13:08:43 -07001159 if (kIgnorePresentFences) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001160 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001161 enableHardwareVsync();
1162 }
1163 }
1164
Pablo Ceballos40845df2016-01-25 17:41:15 -08001165 mFenceTracker.addFrame(refreshStartTime, presentFence,
1166 hw->getVisibleLayersSortedByZ(), hw->getClientTargetAcquireFence());
1167
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001168 if (mAnimCompositionPending) {
1169 mAnimCompositionPending = false;
1170
Jesse Halla9a1b002013-02-27 16:39:25 -08001171 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001172 mAnimFrameTracker.setActualPresentFence(presentFence);
1173 } else {
1174 // The HWC doesn't support present fences, so use the refresh
1175 // timestamp instead.
Dan Stoza9e56aa02015-11-02 13:00:03 -08001176 nsecs_t presentTime =
1177 mHwc->getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001178 mAnimFrameTracker.setActualPresentTime(presentTime);
1179 }
1180 mAnimFrameTracker.advanceFrame();
1181 }
Dan Stozab90cf072015-03-05 11:05:59 -08001182
1183 if (hw->getPowerMode() == HWC_POWER_MODE_OFF) {
1184 return;
1185 }
1186
1187 nsecs_t currentTime = systemTime();
1188 if (mHasPoweredOff) {
1189 mHasPoweredOff = false;
1190 } else {
1191 nsecs_t period = mPrimaryDispSync.getPeriod();
1192 nsecs_t elapsedTime = currentTime - mLastSwapTime;
1193 size_t numPeriods = static_cast<size_t>(elapsedTime / period);
1194 if (numPeriods < NUM_BUCKETS - 1) {
1195 mFrameBuckets[numPeriods] += elapsedTime;
1196 } else {
1197 mFrameBuckets[NUM_BUCKETS - 1] += elapsedTime;
1198 }
1199 mTotalTime += elapsedTime;
1200 }
1201 mLastSwapTime = currentTime;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001202}
1203
1204void SurfaceFlinger::rebuildLayerStacks() {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001205 ATRACE_CALL();
1206 ALOGV("rebuildLayerStacks");
1207
Mathias Agopiancd60f992012-08-16 16:28:27 -07001208 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001209 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001210 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -07001211 mVisibleRegionsDirty = false;
1212 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001213
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001214 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001215 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001216 Region opaqueRegion;
1217 Region dirtyRegion;
Dan Stoza9e56aa02015-11-02 13:00:03 -08001218 Vector<sp<Layer>> layersSortedByZ;
1219 const sp<DisplayDevice>& displayDevice(mDisplays[dpy]);
1220 const Transform& tr(displayDevice->getTransform());
1221 const Rect bounds(displayDevice->getBounds());
1222 if (displayDevice->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001223 SurfaceFlinger::computeVisibleRegions(layers,
Dan Stoza9e56aa02015-11-02 13:00:03 -08001224 displayDevice->getLayerStack(), dirtyRegion,
1225 opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001226
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001227 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001228 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001229 const sp<Layer>& layer(layers[i]);
1230 const Layer::State& s(layer->getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -08001231 if (s.layerStack == displayDevice->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -07001232 Region drawRegion(tr.transform(
1233 layer->visibleNonTransparentRegion));
1234 drawRegion.andSelf(bounds);
1235 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001236 layersSortedByZ.add(layer);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001237 } else {
1238 // Clear out the HWC layer if this layer was
1239 // previously visible, but no longer is
1240 layer->setHwcLayer(displayDevice->getHwcDisplayId(),
1241 nullptr);
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001242 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001243 }
1244 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001245 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001246 displayDevice->setVisibleLayersSortedByZ(layersSortedByZ);
1247 displayDevice->undefinedRegion.set(bounds);
1248 displayDevice->undefinedRegion.subtractSelf(
1249 tr.transform(opaqueRegion));
1250 displayDevice->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001251 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001252 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001253}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001254
Mathias Agopiancd60f992012-08-16 16:28:27 -07001255void SurfaceFlinger::setUpHWComposer() {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001256 ATRACE_CALL();
1257 ALOGV("setUpHWComposer");
1258
Jesse Hall028dc8f2013-08-20 16:35:32 -07001259 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001260 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1261 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1262 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1263
1264 // If nothing has changed (!dirty), don't recompose.
1265 // If something changed, but we don't currently have any visible layers,
1266 // and didn't when we last did a composition, then skip it this time.
1267 // The second rule does two things:
1268 // - When all layers are removed from a display, we'll emit one black
1269 // frame, then nothing more until we get new layers.
1270 // - When a display is created with a private layer stack, we won't
1271 // emit any black frames until a layer is added to the layer stack.
1272 bool mustRecompose = dirty && !(empty && wasEmpty);
1273
1274 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1275 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1276 mustRecompose ? "doing" : "skipping",
1277 dirty ? "+" : "-",
1278 empty ? "+" : "-",
1279 wasEmpty ? "+" : "-");
1280
Dan Stoza71433162014-02-04 16:22:36 -08001281 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001282
1283 if (mustRecompose) {
1284 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1285 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001286 }
1287
Dan Stoza9e56aa02015-11-02 13:00:03 -08001288 // build the h/w work list
1289 if (CC_UNLIKELY(mGeometryInvalid)) {
1290 mGeometryInvalid = false;
1291 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1292 sp<const DisplayDevice> displayDevice(mDisplays[dpy]);
1293 const auto hwcId = displayDevice->getHwcDisplayId();
1294 if (hwcId >= 0) {
1295 const Vector<sp<Layer>>& currentLayers(
1296 displayDevice->getVisibleLayersSortedByZ());
1297 bool foundLayerWithoutHwc = false;
1298 for (auto& layer : currentLayers) {
1299 if (!layer->hasHwcLayer(hwcId)) {
1300 auto hwcLayer = mHwc->createLayer(hwcId);
1301 if (hwcLayer) {
1302 layer->setHwcLayer(hwcId, std::move(hwcLayer));
1303 } else {
1304 layer->forceClientComposition(hwcId);
1305 foundLayerWithoutHwc = true;
1306 continue;
Jamie Gennisa4310c82012-09-25 20:26:00 -07001307 }
1308 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001309
Dan Stoza9e56aa02015-11-02 13:00:03 -08001310 layer->setGeometry(displayDevice);
Dan Stoza9f26a9c2016-06-22 14:51:09 -07001311 if (mDebugDisableHWC || mDebugRegion) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001312 layer->forceClientComposition(hwcId);
Riley Andrews03414a12014-07-01 14:22:59 -07001313 }
1314 }
1315 }
1316 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001317 }
Riley Andrews03414a12014-07-01 14:22:59 -07001318
Dan Stoza9f26a9c2016-06-22 14:51:09 -07001319
1320 mat4 colorMatrix = mColorMatrix * mDaltonizer();
1321
Dan Stoza9e56aa02015-11-02 13:00:03 -08001322 // Set the per-frame data
1323 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
1324 auto& displayDevice = mDisplays[displayId];
1325 const auto hwcId = displayDevice->getHwcDisplayId();
1326 if (hwcId < 0) {
1327 continue;
Jesse Hall38efe862013-04-06 23:12:29 -07001328 }
Dan Stoza9f26a9c2016-06-22 14:51:09 -07001329 if (colorMatrix != mPreviousColorMatrix) {
1330 status_t result = mHwc->setColorTransform(hwcId, colorMatrix);
1331 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on "
1332 "display %zd: %d", displayId, result);
1333 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001334 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
1335 layer->setPerFrameData(displayDevice);
1336 }
1337 }
1338
Dan Stoza9f26a9c2016-06-22 14:51:09 -07001339 mPreviousColorMatrix = colorMatrix;
1340
Dan Stoza9e56aa02015-11-02 13:00:03 -08001341 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
Dan Stoza7bdf55a2016-06-17 11:29:01 -07001342 auto& displayDevice = mDisplays[displayId];
1343 if (!displayDevice->isDisplayOn()) {
1344 continue;
1345 }
1346
1347 status_t result = displayDevice->prepareFrame(*mHwc);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001348 ALOGE_IF(result != NO_ERROR, "prepareFrame for display %zd failed:"
1349 " %d (%s)", displayId, result, strerror(-result));
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001350 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001351}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001352
Mathias Agopiancd60f992012-08-16 16:28:27 -07001353void SurfaceFlinger::doComposition() {
1354 ATRACE_CALL();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001355 ALOGV("doComposition");
1356
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001357 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001358 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001359 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001360 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001361 // transform the dirty region into this screen's coordinate space
1362 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001363
1364 // repaint the framebuffer (if needed)
1365 doDisplayComposition(hw, dirtyRegion);
1366
Mathias Agopiancd60f992012-08-16 16:28:27 -07001367 hw->dirtyRegion.clear();
1368 hw->flip(hw->swapRegion);
1369 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001370 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001371 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001372 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001373}
1374
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375void SurfaceFlinger::postFramebuffer()
1376{
Mathias Agopian841cde52012-03-01 15:44:37 -08001377 ATRACE_CALL();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001378 ALOGV("postFramebuffer");
Mathias Agopianb048cef2012-02-04 15:44:04 -08001379
Mathias Agopiana44b0412011-10-16 18:46:35 -07001380 const nsecs_t now = systemTime();
1381 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001382
Dan Stoza9e56aa02015-11-02 13:00:03 -08001383 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
1384 auto& displayDevice = mDisplays[displayId];
Dan Stoza7bdf55a2016-06-17 11:29:01 -07001385 if (!displayDevice->isDisplayOn()) {
1386 continue;
1387 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001388 const auto hwcId = displayDevice->getHwcDisplayId();
1389 if (hwcId >= 0) {
1390 mHwc->commit(hwcId);
Mathias Agopian2a231842012-09-24 18:12:35 -07001391 }
Dan Stoza2dc3be82016-04-06 14:05:37 -07001392 displayDevice->onSwapBuffersCompleted();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001393 if (displayId == 0) {
1394 // Make the default display current because the VirtualDisplayDevice
1395 // code cannot deal with dequeueBuffer() being called outside of the
1396 // composition loop; however the code below can call glFlush() which
1397 // is allowed to (and does in some case) call dequeueBuffer().
1398 displayDevice->makeCurrent(mEGLDisplay, mEGLContext);
1399 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001400 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
1401 sp<Fence> releaseFence = Fence::NO_FENCE;
1402 if (layer->getCompositionType(hwcId) == HWC2::Composition::Client) {
1403 releaseFence = displayDevice->getClientTargetAcquireFence();
1404 } else {
1405 auto hwcLayer = layer->getHwcLayer(hwcId);
1406 releaseFence = mHwc->getLayerReleaseFence(hwcId, hwcLayer);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001407 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001408 layer->onLayerDisplayed(releaseFence);
1409 }
1410 if (hwcId >= 0) {
1411 mHwc->clearReleaseFences(hwcId);
Jesse Hallef194142012-06-14 14:45:17 -07001412 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001413 }
1414
Mathias Agopiana44b0412011-10-16 18:46:35 -07001415 mLastSwapBufferTime = systemTime() - now;
1416 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001417
1418 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1419 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1420 logFrameStats();
1421 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422}
1423
Mathias Agopian87baae12012-07-31 12:38:26 -07001424void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425{
Mathias Agopian841cde52012-03-01 15:44:37 -08001426 ATRACE_CALL();
1427
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001428 // here we keep a copy of the drawing state (that is the state that's
1429 // going to be overwritten by handleTransactionLocked()) outside of
1430 // mStateLock so that the side-effects of the State assignment
1431 // don't happen with mStateLock held (which can cause deadlocks).
1432 State drawingState(mDrawingState);
1433
Mathias Agopianca4d3602011-05-19 15:38:14 -07001434 Mutex::Autolock _l(mStateLock);
1435 const nsecs_t now = systemTime();
1436 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001437
Mathias Agopianca4d3602011-05-19 15:38:14 -07001438 // Here we're guaranteed that some transaction flags are set
1439 // so we can call handleTransactionLocked() unconditionally.
1440 // We call getTransactionFlags(), which will also clear the flags,
1441 // with mStateLock held to guarantee that mCurrentState won't change
1442 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001443
Mathias Agopiane57f2922012-08-09 16:29:12 -07001444 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001445 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001446
Mathias Agopianca4d3602011-05-19 15:38:14 -07001447 mLastTransactionTime = systemTime() - now;
1448 mDebugInTransaction = 0;
1449 invalidateHwcGeometry();
1450 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001451}
1452
Mathias Agopian87baae12012-07-31 12:38:26 -07001453void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001454{
1455 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001456 const size_t count = currentLayers.size();
1457
Dan Stoza7dde5992015-05-22 09:51:44 -07001458 // Notify all layers of available frames
1459 for (size_t i = 0; i < count; ++i) {
1460 currentLayers[i]->notifyAvailableFrames();
1461 }
1462
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463 /*
1464 * Traversal of the children
1465 * (perform the transaction for each of them if needed)
1466 */
1467
Mathias Agopian3559b072012-08-15 13:46:03 -07001468 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001469 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001470 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001471 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1472 if (!trFlags) continue;
1473
1474 const uint32_t flags = layer->doTransaction(0);
1475 if (flags & Layer::eVisibleRegion)
1476 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001477 }
1478 }
1479
1480 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001481 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001482 */
1483
Mathias Agopiane57f2922012-08-09 16:29:12 -07001484 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001485 // here we take advantage of Vector's copy-on-write semantics to
1486 // improve performance by skipping the transaction entirely when
1487 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001488 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1489 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001490 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001491 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001492 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001493 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001494
1495 // find the displays that were removed
1496 // (ie: in drawing state but not in current state)
1497 // also handle displays that changed
1498 // (ie: displays that are in both lists)
1499 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001500 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1501 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001502 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001503 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001504 // Call makeCurrent() on the primary display so we can
1505 // be sure that nothing associated with this display
1506 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001507 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001508 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001509 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1510 if (hw != NULL)
1511 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001512 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001513 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001514 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001515 } else {
1516 ALOGW("trying to remove the main display");
1517 }
1518 } else {
1519 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001520 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001521 const wp<IBinder>& display(curr.keyAt(j));
Marco Nelissen097ca272014-11-14 08:01:01 -08001522 const sp<IBinder> state_binder = IInterface::asBinder(state.surface);
1523 const sp<IBinder> draw_binder = IInterface::asBinder(draw[i].surface);
Dan Albert1474f882014-09-08 18:59:09 -07001524 if (state_binder != draw_binder) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001525 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001526 // recreating the DisplayDevice, so we just remove it
1527 // from the drawing state, so that it get re-added
1528 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001529 sp<DisplayDevice> hw(getDisplayDevice(display));
1530 if (hw != NULL)
1531 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001532 mDisplays.removeItem(display);
1533 mDrawingState.displays.removeItemsAt(i);
1534 dc--; i--;
1535 // at this point we must loop to the next item
1536 continue;
1537 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001538
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001539 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001540 if (disp != NULL) {
1541 if (state.layerStack != draw[i].layerStack) {
1542 disp->setLayerStack(state.layerStack);
1543 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001544 if ((state.orientation != draw[i].orientation)
1545 || (state.viewport != draw[i].viewport)
1546 || (state.frame != draw[i].frame))
1547 {
1548 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001549 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001550 }
Michael Lentine47e45402014-07-18 15:34:25 -07001551 if (state.width != draw[i].width || state.height != draw[i].height) {
1552 disp->setDisplaySize(state.width, state.height);
1553 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001554 }
1555 }
1556 }
1557
1558 // find displays that were added
1559 // (ie: in current state but not in drawing state)
1560 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001561 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001562 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001563
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001564 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001565 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001566 sp<IGraphicBufferProducer> bqProducer;
1567 sp<IGraphicBufferConsumer> bqConsumer;
Dan Stoza70982a52016-01-11 23:40:44 +00001568 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1569 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001570
Dan Stoza9e56aa02015-11-02 13:00:03 -08001571 int32_t hwcId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001572 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001573 // Virtual displays without a surface are dormant:
1574 // they have external state (layer stack, projection,
1575 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001576 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001577
Dan Stoza1f3efb12014-10-15 16:34:55 -07001578 int width = 0;
1579 int status = state.surface->query(
1580 NATIVE_WINDOW_WIDTH, &width);
1581 ALOGE_IF(status != NO_ERROR,
1582 "Unable to query width (%d)", status);
1583 int height = 0;
1584 status = state.surface->query(
1585 NATIVE_WINDOW_HEIGHT, &height);
1586 ALOGE_IF(status != NO_ERROR,
1587 "Unable to query height (%d)", status);
Dan Stoza5cf424b2016-05-20 14:02:39 -07001588 int intFormat = 0;
1589 status = state.surface->query(
1590 NATIVE_WINDOW_FORMAT, &intFormat);
1591 ALOGE_IF(status != NO_ERROR,
1592 "Unable to query format (%d)", status);
1593 auto format = static_cast<android_pixel_format_t>(
1594 intFormat);
Dan Stoza1f3efb12014-10-15 16:34:55 -07001595
Dan Stoza5cf424b2016-05-20 14:02:39 -07001596 mHwc->allocateVirtualDisplay(width, height, &format,
Dan Stoza9e56aa02015-11-02 13:00:03 -08001597 &hwcId);
1598
Dan Stoza5cf424b2016-05-20 14:02:39 -07001599 // TODO: Plumb requested format back up to consumer
1600
Dan Stoza9e56aa02015-11-02 13:00:03 -08001601 sp<VirtualDisplaySurface> vds =
1602 new VirtualDisplaySurface(*mHwc,
1603 hwcId, state.surface, bqProducer,
1604 bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001605
1606 dispSurface = vds;
Michael Lentine47e45402014-07-18 15:34:25 -07001607 producer = vds;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001608 }
1609 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001610 ALOGE_IF(state.surface!=NULL,
1611 "adding a supported display, but rendering "
1612 "surface is provided (%p), ignoring it",
1613 state.surface.get());
Dan Stoza9e56aa02015-11-02 13:00:03 -08001614 if (state.type == DisplayDevice::DISPLAY_EXTERNAL) {
1615 hwcId = DisplayDevice::DISPLAY_EXTERNAL;
1616 dispSurface = new FramebufferSurface(*mHwc,
1617 DisplayDevice::DISPLAY_EXTERNAL,
1618 bqConsumer);
1619 producer = bqProducer;
1620 } else {
1621 ALOGE("Attempted to add non-external non-virtual"
1622 " display");
1623 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07001624 }
1625
1626 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001627 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001628 sp<DisplayDevice> hw = new DisplayDevice(this,
Dan Stoza9e56aa02015-11-02 13:00:03 -08001629 state.type, hwcId, state.isSecure, display,
1630 dispSurface, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -08001631 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001632 hw->setLayerStack(state.layerStack);
1633 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001634 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001635 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001636 mDisplays.add(display, hw);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001637 if (!state.isVirtualDisplay()) {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001638 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001639 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001640 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001641 }
1642 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001643 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001644 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001645
Mathias Agopian84300952012-11-21 16:02:13 -08001646 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1647 // The transform hint might have changed for some layers
1648 // (either because a display has changed, or because a layer
1649 // as changed).
1650 //
1651 // Walk through all the layers in currentLayers,
1652 // and update their transform hint.
1653 //
1654 // If a layer is visible only on a single display, then that
1655 // display is used to calculate the hint, otherwise we use the
1656 // default display.
1657 //
1658 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1659 // the hint is set before we acquire a buffer from the surface texture.
1660 //
1661 // NOTE: layer transactions have taken place already, so we use their
1662 // drawing state. However, SurfaceFlinger's own transaction has not
1663 // happened yet, so we must use the current state layer list
1664 // (soon to become the drawing state list).
1665 //
1666 sp<const DisplayDevice> disp;
1667 uint32_t currentlayerStack = 0;
1668 for (size_t i=0; i<count; i++) {
1669 // NOTE: we rely on the fact that layers are sorted by
1670 // layerStack first (so we don't have to traverse the list
1671 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001672 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001673 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001674 if (i==0 || currentlayerStack != layerStack) {
1675 currentlayerStack = layerStack;
1676 // figure out if this layerstack is mirrored
1677 // (more than one display) if so, pick the default display,
1678 // if not, pick the only display it's on.
1679 disp.clear();
1680 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1681 sp<const DisplayDevice> hw(mDisplays[dpy]);
1682 if (hw->getLayerStack() == currentlayerStack) {
1683 if (disp == NULL) {
1684 disp = hw;
1685 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001686 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001687 break;
1688 }
1689 }
1690 }
1691 }
Chet Haase91d25932013-04-11 15:24:55 -07001692 if (disp == NULL) {
1693 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1694 // redraw after transform hint changes. See bug 8508397.
1695
1696 // could be null when this layer is using a layerStack
1697 // that is not visible on any display. Also can occur at
1698 // screen off/on times.
1699 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001700 }
Chet Haase91d25932013-04-11 15:24:55 -07001701 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001702 }
1703 }
1704
1705
Mathias Agopian3559b072012-08-15 13:46:03 -07001706 /*
1707 * Perform our own transaction if needed
1708 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001709
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001710 const LayerVector& layers(mDrawingState.layersSortedByZ);
1711 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001712 // layers have been added
1713 mVisibleRegionsDirty = true;
1714 }
1715
1716 // some layers might have been removed, so
1717 // we need to update the regions they're exposing.
1718 if (mLayersRemoved) {
1719 mLayersRemoved = false;
1720 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001721 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001722 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001723 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001724 if (currentLayers.indexOf(layer) < 0) {
1725 // this layer is not visible anymore
1726 // TODO: we could traverse the tree from front to back and
1727 // compute the actual visible region
1728 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001729 const Layer::State& s(layer->getDrawingState());
Robert Carr3dcabfa2016-03-01 18:36:58 -08001730 Region visibleReg = s.active.transform.transform(
Mathias Agopian1501d542012-09-04 21:04:09 -07001731 Region(Rect(s.active.w, s.active.h)));
1732 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001733 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001734 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001735 }
1736
1737 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001738
1739 updateCursorAsync();
1740}
1741
1742void SurfaceFlinger::updateCursorAsync()
1743{
Dan Stoza9e56aa02015-11-02 13:00:03 -08001744 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
1745 auto& displayDevice = mDisplays[displayId];
1746 if (displayDevice->getHwcDisplayId() < 0) {
Riley Andrews03414a12014-07-01 14:22:59 -07001747 continue;
1748 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001749
1750 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
1751 layer->updateCursorPosition(displayDevice);
Riley Andrews03414a12014-07-01 14:22:59 -07001752 }
1753 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001754}
1755
1756void SurfaceFlinger::commitTransaction()
1757{
1758 if (!mLayersPendingRemoval.isEmpty()) {
1759 // Notify removed layers now that they can't be drawn from
1760 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
Dan Stozae77c7662016-05-13 11:37:28 -07001761 recordBufferingStats(mLayersPendingRemoval[i]->getName().string(),
1762 mLayersPendingRemoval[i]->getOccupancyHistory(true));
Mathias Agopian4fec8732012-06-29 14:12:52 -07001763 mLayersPendingRemoval[i]->onRemoved();
1764 }
1765 mLayersPendingRemoval.clear();
1766 }
1767
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001768 // If this transaction is part of a window animation then the next frame
1769 // we composite should be considered an animation as well.
1770 mAnimCompositionPending = mAnimTransactionPending;
1771
Mathias Agopian4fec8732012-06-29 14:12:52 -07001772 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001773 mTransactionPending = false;
1774 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001775 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001776}
1777
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001778void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001779 const LayerVector& currentLayers, uint32_t layerStack,
1780 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001781{
Mathias Agopian841cde52012-03-01 15:44:37 -08001782 ATRACE_CALL();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001783 ALOGV("computeVisibleRegions");
Mathias Agopian841cde52012-03-01 15:44:37 -08001784
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001785 Region aboveOpaqueLayers;
1786 Region aboveCoveredLayers;
1787 Region dirty;
1788
Mathias Agopian87baae12012-07-31 12:38:26 -07001789 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001790
1791 size_t i = currentLayers.size();
1792 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001793 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001794
1795 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001796 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001797
Jesse Hall01e29052013-02-19 16:13:35 -08001798 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001799 if (s.layerStack != layerStack)
1800 continue;
1801
Mathias Agopianab028732010-03-16 16:41:46 -07001802 /*
1803 * opaqueRegion: area of a surface that is fully opaque.
1804 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001805 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001806
1807 /*
1808 * visibleRegion: area of a surface that is visible on screen
1809 * and not fully transparent. This is essentially the layer's
1810 * footprint minus the opaque regions above it.
1811 * Areas covered by a translucent surface are considered visible.
1812 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001813 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001814
1815 /*
1816 * coveredRegion: area of a surface that is covered by all
1817 * visible regions above it (which includes the translucent areas).
1818 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001819 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001820
Jesse Halla8026d22012-09-25 13:25:04 -07001821 /*
1822 * transparentRegion: area of a surface that is hinted to be completely
1823 * transparent. This is only used to tell when the layer has no visible
1824 * non-transparent regions and can be removed from the layer list. It
1825 * does not affect the visibleRegion of this layer or any layers
1826 * beneath it. The hint may not be correct if apps don't respect the
1827 * SurfaceView restrictions (which, sadly, some don't).
1828 */
1829 Region transparentRegion;
1830
Mathias Agopianab028732010-03-16 16:41:46 -07001831
1832 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001833 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001834 const bool translucent = !layer->isOpaque(s);
Robert Carr3dcabfa2016-03-01 18:36:58 -08001835 Rect bounds(s.active.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001836 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001837 if (!visibleRegion.isEmpty()) {
1838 // Remove the transparent area from the visible region
1839 if (translucent) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001840 const Transform tr(s.active.transform);
Dan Stoza22f7fc42016-05-10 16:19:53 -07001841 if (tr.preserveRects()) {
1842 // transform the transparent region
1843 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001844 } else {
Dan Stoza22f7fc42016-05-10 16:19:53 -07001845 // transformation too complex, can't do the
1846 // transparent region optimization.
1847 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001848 }
Mathias Agopianab028732010-03-16 16:41:46 -07001849 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001850
Mathias Agopianab028732010-03-16 16:41:46 -07001851 // compute the opaque region
Robert Carr3dcabfa2016-03-01 18:36:58 -08001852 const int32_t layerOrientation = s.active.transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001853 if (s.alpha == 1.0f && !translucent &&
Mathias Agopianab028732010-03-16 16:41:46 -07001854 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1855 // the opaque region is the layer's footprint
1856 opaqueRegion = visibleRegion;
1857 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001858 }
1859 }
1860
Mathias Agopianab028732010-03-16 16:41:46 -07001861 // Clip the covered region to the visible region
1862 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1863
1864 // Update aboveCoveredLayers for next (lower) layer
1865 aboveCoveredLayers.orSelf(visibleRegion);
1866
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001867 // subtract the opaque region covered by the layers above us
1868 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001869
1870 // compute this layer's dirty region
1871 if (layer->contentDirty) {
1872 // we need to invalidate the whole region
1873 dirty = visibleRegion;
1874 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001875 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001876 layer->contentDirty = false;
1877 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001878 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001879 * the exposed region consists of two components:
1880 * 1) what's VISIBLE now and was COVERED before
1881 * 2) what's EXPOSED now less what was EXPOSED before
1882 *
1883 * note that (1) is conservative, we start with the whole
1884 * visible region but only keep what used to be covered by
1885 * something -- which mean it may have been exposed.
1886 *
1887 * (2) handles areas that were not covered by anything but got
1888 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001889 */
Mathias Agopianab028732010-03-16 16:41:46 -07001890 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001891 const Region oldVisibleRegion = layer->visibleRegion;
1892 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001893 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1894 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001895 }
1896 dirty.subtractSelf(aboveOpaqueLayers);
1897
1898 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001899 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001900
Mathias Agopianab028732010-03-16 16:41:46 -07001901 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001902 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001903
Jesse Halla8026d22012-09-25 13:25:04 -07001904 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001905 layer->setVisibleRegion(visibleRegion);
1906 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001907 layer->setVisibleNonTransparentRegion(
1908 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001909 }
1910
Mathias Agopian87baae12012-07-31 12:38:26 -07001911 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001912}
1913
Mathias Agopian87baae12012-07-31 12:38:26 -07001914void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1915 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001916 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001917 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1918 if (hw->getLayerStack() == layerStack) {
1919 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001920 }
1921 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001922}
1923
Dan Stoza6b9454d2014-11-07 16:00:59 -08001924bool SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001925{
Dan Stoza9e56aa02015-11-02 13:00:03 -08001926 ALOGV("handlePageFlip");
1927
Mathias Agopian4fec8732012-06-29 14:12:52 -07001928 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001929
Mathias Agopian4fec8732012-06-29 14:12:52 -07001930 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001931 const LayerVector& layers(mDrawingState.layersSortedByZ);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001932 bool frameQueued = false;
Eric Penner51c59cd2014-07-28 19:51:58 -07001933
1934 // Store the set of layers that need updates. This set must not change as
1935 // buffers are being latched, as this could result in a deadlock.
1936 // Example: Two producers share the same command stream and:
1937 // 1.) Layer 0 is latched
1938 // 2.) Layer 0 gets a new frame
1939 // 2.) Layer 1 gets a new frame
1940 // 3.) Layer 1 is latched.
1941 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1942 // second frame. But layer 0's second frame could be waiting on display.
Eric Penner51c59cd2014-07-28 19:51:58 -07001943 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001944 const sp<Layer>& layer(layers[i]);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001945 if (layer->hasQueuedFrame()) {
1946 frameQueued = true;
1947 if (layer->shouldPresentNow(mPrimaryDispSync)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001948 mLayersWithQueuedFrames.push_back(layer.get());
Dan Stozaee44edd2015-03-23 15:50:23 -07001949 } else {
1950 layer->useEmptyDamage();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001951 }
Dan Stozaee44edd2015-03-23 15:50:23 -07001952 } else {
1953 layer->useEmptyDamage();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001954 }
Eric Penner51c59cd2014-07-28 19:51:58 -07001955 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001956 for (auto& layer : mLayersWithQueuedFrames) {
Mathias Agopian87baae12012-07-31 12:38:26 -07001957 const Region dirty(layer->latchBuffer(visibleRegions));
Dan Stozaee44edd2015-03-23 15:50:23 -07001958 layer->useSurfaceDamage();
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001959 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001960 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001961 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001962
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001963 mVisibleRegionsDirty |= visibleRegions;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001964
1965 // If we will need to wake up at some time in the future to deal with a
1966 // queued frame that shouldn't be displayed during this vsync period, wake
1967 // up during the next vsync period to check again.
Dan Stoza9e56aa02015-11-02 13:00:03 -08001968 if (frameQueued && mLayersWithQueuedFrames.empty()) {
Dan Stoza6b9454d2014-11-07 16:00:59 -08001969 signalLayerUpdate();
1970 }
1971
1972 // Only continue with the refresh if there is actually new work to do
Dan Stoza9e56aa02015-11-02 13:00:03 -08001973 return !mLayersWithQueuedFrames.empty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001974}
1975
Mathias Agopianad456f92011-01-13 17:53:01 -08001976void SurfaceFlinger::invalidateHwcGeometry()
1977{
Dan Stoza9e56aa02015-11-02 13:00:03 -08001978 mGeometryInvalid = true;
Mathias Agopianad456f92011-01-13 17:53:01 -08001979}
1980
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001981
Mathias Agopiancd60f992012-08-16 16:28:27 -07001982void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001983 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001984{
Dan Stoza71433162014-02-04 16:22:36 -08001985 // We only need to actually compose the display if:
1986 // 1) It is being handled by hardware composer, which may need this to
1987 // keep its virtual display state machine in sync, or
1988 // 2) There is work to be done (the dirty region isn't empty)
1989 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1990 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001991 ALOGV("Skipping display composition");
Dan Stoza71433162014-02-04 16:22:36 -08001992 return;
1993 }
1994
Dan Stoza9e56aa02015-11-02 13:00:03 -08001995 ALOGV("doDisplayComposition");
1996
Mathias Agopian87baae12012-07-31 12:38:26 -07001997 Region dirtyRegion(inDirtyRegion);
1998
Mathias Agopianb8a55602009-06-26 19:06:36 -07001999 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07002000 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002001
Mathias Agopian42977342012-08-05 00:40:46 -07002002 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002003 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002004 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
2005 // takes a rectangle, we must make sure to update that whole
2006 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07002007 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002008 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002009 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002010 // We need to redraw the rectangle that will be updated
2011 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07002012 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002013 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07002014 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002015 } else {
2016 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07002017 dirtyRegion.set(hw->bounds());
2018 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002019 }
2020 }
2021
Dan Stoza9f26a9c2016-06-22 14:51:09 -07002022 if (!doComposeSurfaces(hw, dirtyRegion)) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002023
Mathias Agopian9c6e2972011-09-20 17:21:56 -07002024 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07002025 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07002026
2027 // swap buffers (presentation)
2028 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002029}
2030
Dan Stoza9e56aa02015-11-02 13:00:03 -08002031bool SurfaceFlinger::doComposeSurfaces(
2032 const sp<const DisplayDevice>& displayDevice, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07002033{
Dan Stoza9e56aa02015-11-02 13:00:03 -08002034 ALOGV("doComposeSurfaces");
Mathias Agopiancd20eb02011-09-22 20:57:04 -07002035
Dan Stoza9e56aa02015-11-02 13:00:03 -08002036 const auto hwcId = displayDevice->getHwcDisplayId();
Dan Stoza9f26a9c2016-06-22 14:51:09 -07002037
2038 mat4 oldColorMatrix;
2039 const bool applyColorMatrix = !mHwc->hasDeviceComposition(hwcId) &&
2040 !mHwc->hasCapability(HWC2::Capability::SkipClientColorTransform);
2041 if (applyColorMatrix) {
2042 mat4 colorMatrix = mColorMatrix * mDaltonizer();
2043 oldColorMatrix = getRenderEngine().setupColorTransform(colorMatrix);
2044 }
2045
Dan Stoza9e56aa02015-11-02 13:00:03 -08002046 bool hasClientComposition = mHwc->hasClientComposition(hwcId);
2047 if (hasClientComposition) {
2048 ALOGV("hasClientComposition");
2049
2050 if (!displayDevice->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08002051 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
Dan Stoza9e56aa02015-11-02 13:00:03 -08002052 displayDevice->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07002053 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
2054 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
2055 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
2056 }
2057 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08002058 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07002059
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07002060 // Never touch the framebuffer if we don't have any framebuffer layers
Dan Stoza9e56aa02015-11-02 13:00:03 -08002061 const bool hasDeviceComposition = mHwc->hasDeviceComposition(hwcId);
2062 if (hasDeviceComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07002063 // when using overlays, we assume a fully transparent framebuffer
2064 // NOTE: we could reduce how much we need to clear, for instance
2065 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07002066 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07002067 // We'll revisit later if needed.
Dan Stoza9e56aa02015-11-02 13:00:03 -08002068 mRenderEngine->clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002069 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07002070 // we start with the whole screen area
Dan Stoza9e56aa02015-11-02 13:00:03 -08002071 const Region bounds(displayDevice->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07002072
2073 // we remove the scissor part
2074 // we're left with the letterbox region
2075 // (common case is that letterbox ends-up being empty)
Dan Stoza9e56aa02015-11-02 13:00:03 -08002076 const Region letterbox(bounds.subtract(displayDevice->getScissor()));
Mathias Agopian766dc492012-10-30 18:08:06 -07002077
2078 // compute the area to clear
Dan Stoza9e56aa02015-11-02 13:00:03 -08002079 Region region(displayDevice->undefinedRegion.merge(letterbox));
Mathias Agopian766dc492012-10-30 18:08:06 -07002080
2081 // but limit it to the dirty region
2082 region.andSelf(dirty);
2083
Mathias Agopianb9494d52012-04-18 02:28:45 -07002084 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07002085 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07002086 // can happen with SurfaceView
Dan Stoza9e56aa02015-11-02 13:00:03 -08002087 drawWormhole(displayDevice, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002088 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002089 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002090
Dan Stoza9e56aa02015-11-02 13:00:03 -08002091 if (displayDevice->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopian766dc492012-10-30 18:08:06 -07002092 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07002093 // scissor on the main display. It should never be needed
2094 // anyways (though in theory it could since the API allows it).
Dan Stoza9e56aa02015-11-02 13:00:03 -08002095 const Rect& bounds(displayDevice->getBounds());
2096 const Rect& scissor(displayDevice->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002097 if (scissor != bounds) {
2098 // scissor doesn't match the screen's dimensions, so we
2099 // need to clear everything outside of it and enable
2100 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07002101
Mathias Agopianf45c5102012-10-24 16:29:17 -07002102 // enable scissor for this frame
Dan Stoza9e56aa02015-11-02 13:00:03 -08002103 const uint32_t height = displayDevice->getHeight();
2104 mRenderEngine->setScissor(scissor.left, height - scissor.bottom,
Mathias Agopian3f844832013-08-07 21:24:32 -07002105 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002106 }
2107 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002108 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002109
Mathias Agopian85d751c2012-08-29 16:59:24 -07002110 /*
2111 * and then, render the layers targeted at the framebuffer
2112 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002113
Dan Stoza9e56aa02015-11-02 13:00:03 -08002114 ALOGV("Rendering client layers");
2115 const Transform& displayTransform = displayDevice->getTransform();
2116 if (hwcId >= 0) {
Mathias Agopian85d751c2012-08-29 16:59:24 -07002117 // we're using h/w composer
Dan Stoza9e56aa02015-11-02 13:00:03 -08002118 bool firstLayer = true;
2119 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
2120 const Region clip(dirty.intersect(
2121 displayTransform.transform(layer->visibleRegion)));
2122 ALOGV("Layer: %s", layer->getName().string());
2123 ALOGV(" Composition type: %s",
2124 to_string(layer->getCompositionType(hwcId)).c_str());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002125 if (!clip.isEmpty()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08002126 switch (layer->getCompositionType(hwcId)) {
2127 case HWC2::Composition::Cursor:
2128 case HWC2::Composition::Device:
2129 case HWC2::Composition::SolidColor: {
Mathias Agopianac683022013-10-01 15:36:52 -07002130 const Layer::State& state(layer->getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -08002131 if (layer->getClearClientTarget(hwcId) && !firstLayer &&
2132 layer->isOpaque(state) && (state.alpha == 1.0f)
2133 && hasClientComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07002134 // never clear the very first layer since we're
2135 // guaranteed the FB is already cleared
Dan Stoza9e56aa02015-11-02 13:00:03 -08002136 layer->clearWithOpenGL(displayDevice, clip);
Mathias Agopiancd60f992012-08-16 16:28:27 -07002137 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002138 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07002139 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08002140 case HWC2::Composition::Client: {
2141 layer->draw(displayDevice, clip);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002142 break;
2143 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08002144 default:
Mathias Agopianda27af92012-09-13 18:17:13 -07002145 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07002146 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08002147 } else {
2148 ALOGV(" Skipping for empty clip");
Mathias Agopian85d751c2012-08-29 16:59:24 -07002149 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08002150 firstLayer = false;
Mathias Agopian85d751c2012-08-29 16:59:24 -07002151 }
2152 } else {
2153 // we're not using h/w composer
Dan Stoza9e56aa02015-11-02 13:00:03 -08002154 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
Mathias Agopian85d751c2012-08-29 16:59:24 -07002155 const Region clip(dirty.intersect(
Dan Stoza9e56aa02015-11-02 13:00:03 -08002156 displayTransform.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07002157 if (!clip.isEmpty()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08002158 layer->draw(displayDevice, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07002159 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002160 }
2161 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002162
Dan Stoza9f26a9c2016-06-22 14:51:09 -07002163 if (applyColorMatrix) {
2164 getRenderEngine().setupColorTransform(oldColorMatrix);
2165 }
2166
Mathias Agopianf45c5102012-10-24 16:29:17 -07002167 // disable scissor at the end of the frame
Dan Stoza9e56aa02015-11-02 13:00:03 -08002168 mRenderEngine->disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07002169 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002170}
2171
Mathias Agopian3f844832013-08-07 21:24:32 -07002172void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07002173 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07002174 RenderEngine& engine(getRenderEngine());
2175 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002176}
2177
Dan Stoza7d89d062015-04-30 13:29:25 -07002178status_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -08002179 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07002180 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08002181 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07002182{
Dan Stoza7d89d062015-04-30 13:29:25 -07002183 // add this layer to the current state list
2184 {
2185 Mutex::Autolock _l(mStateLock);
2186 if (mCurrentState.layersSortedByZ.size() >= MAX_LAYERS) {
2187 return NO_MEMORY;
2188 }
2189 mCurrentState.layersSortedByZ.add(lbc);
2190 mGraphicBufferProducerList.add(IInterface::asBinder(gbc));
2191 }
2192
Mathias Agopian96f08192010-06-02 23:28:45 -07002193 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08002194 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07002195
Dan Stoza7d89d062015-04-30 13:29:25 -07002196 return NO_ERROR;
Mathias Agopian96f08192010-06-02 23:28:45 -07002197}
2198
Mathias Agopian3f844832013-08-07 21:24:32 -07002199status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002200 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08002201 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002202 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07002203 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002204 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07002205 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002206 return NO_ERROR;
2207 }
Mathias Agopian3d579642009-06-04 18:46:21 -07002208 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002209}
2210
Dan Stozac7014012014-02-14 15:03:43 -08002211uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07002212 return android_atomic_release_load(&mTransactionFlags);
2213}
2214
Mathias Agopian3f844832013-08-07 21:24:32 -07002215uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002216 return android_atomic_and(~flags, &mTransactionFlags) & flags;
2217}
2218
Mathias Agopian3f844832013-08-07 21:24:32 -07002219uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002220 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
2221 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002222 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002223 }
2224 return old;
2225}
2226
Mathias Agopian8b33f032012-07-24 20:43:54 -07002227void SurfaceFlinger::setTransactionState(
2228 const Vector<ComposerState>& state,
2229 const Vector<DisplayState>& displays,
2230 uint32_t flags)
2231{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002232 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07002233 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07002234 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002235
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002236 if (flags & eAnimation) {
2237 // For window updates that are part of an animation we must wait for
2238 // previous animation "frames" to be handled.
2239 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002240 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002241 if (CC_UNLIKELY(err != NO_ERROR)) {
2242 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002243 // caller after a few seconds.
2244 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
2245 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002246 mAnimTransactionPending = false;
2247 break;
2248 }
2249 }
2250 }
2251
Mathias Agopiane57f2922012-08-09 16:29:12 -07002252 size_t count = displays.size();
2253 for (size_t i=0 ; i<count ; i++) {
2254 const DisplayState& s(displays[i]);
2255 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002256 }
2257
Mathias Agopiane57f2922012-08-09 16:29:12 -07002258 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002259 for (size_t i=0 ; i<count ; i++) {
2260 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002261 // Here we need to check that the interface we're given is indeed
2262 // one of our own. A malicious client could give us a NULL
2263 // IInterface, or one of its own or even one of our own but a
2264 // different type. All these situations would cause us to crash.
2265 //
2266 // NOTE: it would be better to use RTTI as we could directly check
2267 // that we have a Client*. however, RTTI is disabled in Android.
2268 if (s.client != NULL) {
Marco Nelissen097ca272014-11-14 08:01:01 -08002269 sp<IBinder> binder = IInterface::asBinder(s.client);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002270 if (binder != NULL) {
2271 String16 desc(binder->getInterfaceDescriptor());
2272 if (desc == ISurfaceComposerClient::descriptor) {
2273 sp<Client> client( static_cast<Client *>(s.client.get()) );
2274 transactionFlags |= setClientStateLocked(client, s.state);
2275 }
2276 }
2277 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002278 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002279
Robert Carr2a7dbb42016-05-24 11:41:28 -07002280 // If a synchronous transaction is explicitly requested without any changes,
2281 // force a transaction anyway. This can be used as a flush mechanism for
2282 // previous async transactions.
2283 if (transactionFlags == 0 && (flags & eSynchronous)) {
2284 transactionFlags = eTransactionNeeded;
2285 }
2286
Mathias Agopian386aa982011-11-07 21:58:03 -08002287 if (transactionFlags) {
2288 // this triggers the transaction
2289 setTransactionFlags(transactionFlags);
2290
2291 // if this is a synchronous transaction, wait for it to take effect
2292 // before returning.
2293 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002294 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002295 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002296 if (flags & eAnimation) {
2297 mAnimTransactionPending = true;
2298 }
2299 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002300 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2301 if (CC_UNLIKELY(err != NO_ERROR)) {
2302 // just in case something goes wrong in SF, return to the
2303 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002304 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2305 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002306 break;
2307 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002308 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002309 }
2310}
2311
Mathias Agopiane57f2922012-08-09 16:29:12 -07002312uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2313{
Jesse Hall9a143922012-10-04 16:29:19 -07002314 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2315 if (dpyIdx < 0)
2316 return 0;
2317
Mathias Agopiane57f2922012-08-09 16:29:12 -07002318 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002319 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002320 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002321 const uint32_t what = s.what;
2322 if (what & DisplayState::eSurfaceChanged) {
Marco Nelissen097ca272014-11-14 08:01:01 -08002323 if (IInterface::asBinder(disp.surface) != IInterface::asBinder(s.surface)) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002324 disp.surface = s.surface;
2325 flags |= eDisplayTransactionNeeded;
2326 }
2327 }
2328 if (what & DisplayState::eLayerStackChanged) {
2329 if (disp.layerStack != s.layerStack) {
2330 disp.layerStack = s.layerStack;
2331 flags |= eDisplayTransactionNeeded;
2332 }
2333 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002334 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002335 if (disp.orientation != s.orientation) {
2336 disp.orientation = s.orientation;
2337 flags |= eDisplayTransactionNeeded;
2338 }
2339 if (disp.frame != s.frame) {
2340 disp.frame = s.frame;
2341 flags |= eDisplayTransactionNeeded;
2342 }
2343 if (disp.viewport != s.viewport) {
2344 disp.viewport = s.viewport;
2345 flags |= eDisplayTransactionNeeded;
2346 }
2347 }
Michael Lentine47e45402014-07-18 15:34:25 -07002348 if (what & DisplayState::eDisplaySizeChanged) {
2349 if (disp.width != s.width) {
2350 disp.width = s.width;
2351 flags |= eDisplayTransactionNeeded;
2352 }
2353 if (disp.height != s.height) {
2354 disp.height = s.height;
2355 flags |= eDisplayTransactionNeeded;
2356 }
2357 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002358 }
2359 return flags;
2360}
2361
2362uint32_t SurfaceFlinger::setClientStateLocked(
2363 const sp<Client>& client,
2364 const layer_state_t& s)
2365{
2366 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002367 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002368 if (layer != 0) {
2369 const uint32_t what = s.what;
Robert Carr99e27f02016-06-16 15:18:02 -07002370 bool geometryAppliesWithResize =
2371 what & layer_state_t::eGeometryAppliesWithResize;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002372 if (what & layer_state_t::ePositionChanged) {
Robert Carr99e27f02016-06-16 15:18:02 -07002373 if (layer->setPosition(s.x, s.y, !geometryAppliesWithResize)) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002374 flags |= eTraversalNeeded;
Robert Carr82364e32016-05-15 11:27:47 -07002375 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002376 }
2377 if (what & layer_state_t::eLayerChanged) {
2378 // NOTE: index needs to be calculated before we update the state
2379 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
Pablo Ceballos47db6eb2015-12-08 13:53:59 -08002380 if (layer->setLayer(s.z) && idx >= 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002381 mCurrentState.layersSortedByZ.removeAt(idx);
2382 mCurrentState.layersSortedByZ.add(layer);
2383 // we need traversal (state changed)
2384 // AND transaction (list changed)
2385 flags |= eTransactionNeeded|eTraversalNeeded;
2386 }
2387 }
2388 if (what & layer_state_t::eSizeChanged) {
2389 if (layer->setSize(s.w, s.h)) {
2390 flags |= eTraversalNeeded;
2391 }
2392 }
2393 if (what & layer_state_t::eAlphaChanged) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08002394 if (layer->setAlpha(s.alpha))
Mathias Agopiane57f2922012-08-09 16:29:12 -07002395 flags |= eTraversalNeeded;
2396 }
2397 if (what & layer_state_t::eMatrixChanged) {
2398 if (layer->setMatrix(s.matrix))
2399 flags |= eTraversalNeeded;
2400 }
2401 if (what & layer_state_t::eTransparentRegionChanged) {
2402 if (layer->setTransparentRegionHint(s.transparentRegion))
2403 flags |= eTraversalNeeded;
2404 }
Dan Stoza23116082015-06-18 14:58:39 -07002405 if (what & layer_state_t::eFlagsChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002406 if (layer->setFlags(s.flags, s.mask))
2407 flags |= eTraversalNeeded;
2408 }
2409 if (what & layer_state_t::eCropChanged) {
Robert Carr99e27f02016-06-16 15:18:02 -07002410 if (layer->setCrop(s.crop, !geometryAppliesWithResize))
Mathias Agopiane57f2922012-08-09 16:29:12 -07002411 flags |= eTraversalNeeded;
2412 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002413 if (what & layer_state_t::eFinalCropChanged) {
2414 if (layer->setFinalCrop(s.finalCrop))
2415 flags |= eTraversalNeeded;
2416 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002417 if (what & layer_state_t::eLayerStackChanged) {
2418 // NOTE: index needs to be calculated before we update the state
2419 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
Pablo Ceballos47db6eb2015-12-08 13:53:59 -08002420 if (layer->setLayerStack(s.layerStack) && idx >= 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002421 mCurrentState.layersSortedByZ.removeAt(idx);
2422 mCurrentState.layersSortedByZ.add(layer);
2423 // we need traversal (state changed)
2424 // AND transaction (list changed)
2425 flags |= eTransactionNeeded|eTraversalNeeded;
2426 }
2427 }
Dan Stoza7dde5992015-05-22 09:51:44 -07002428 if (what & layer_state_t::eDeferTransaction) {
2429 layer->deferTransactionUntil(s.handle, s.frameNumber);
2430 // We don't trigger a traversal here because if no other state is
2431 // changed, we don't want this to cause any more work
2432 }
Robert Carrc3574f72016-03-24 12:19:32 -07002433 if (what & layer_state_t::eOverrideScalingModeChanged) {
2434 layer->setOverrideScalingMode(s.overrideScalingMode);
2435 // We don't trigger a traversal here because if no other state is
2436 // changed, we don't want this to cause any more work
2437 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002438 }
2439 return flags;
2440}
2441
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002442status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002443 const String8& name,
2444 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002445 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2446 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002447{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002448 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002449 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002450 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002451 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002452 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002453 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002454
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002455 status_t result = NO_ERROR;
2456
2457 sp<Layer> layer;
2458
Mathias Agopian3165cc22012-08-08 19:42:09 -07002459 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2460 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002461 result = createNormalLayer(client,
2462 name, w, h, flags, format,
2463 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002464 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002465 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002466 result = createDimLayer(client,
2467 name, w, h, flags,
2468 handle, gbp, &layer);
2469 break;
2470 default:
2471 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002472 break;
2473 }
2474
Dan Stoza7d89d062015-04-30 13:29:25 -07002475 if (result != NO_ERROR) {
2476 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002477 }
Dan Stoza7d89d062015-04-30 13:29:25 -07002478
2479 result = addClientLayer(client, *handle, *gbp, layer);
2480 if (result != NO_ERROR) {
2481 return result;
2482 }
2483
2484 setTransactionFlags(eTransactionNeeded);
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002485 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002486}
2487
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002488status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2489 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2490 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002491{
2492 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002493 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002494 case PIXEL_FORMAT_TRANSPARENT:
2495 case PIXEL_FORMAT_TRANSLUCENT:
2496 format = PIXEL_FORMAT_RGBA_8888;
2497 break;
2498 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002499 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002500 break;
2501 }
2502
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002503 *outLayer = new Layer(this, client, name, w, h, flags);
2504 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2505 if (err == NO_ERROR) {
2506 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002507 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002508 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002509
2510 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2511 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002512}
2513
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002514status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2515 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2516 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002517{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002518 *outLayer = new LayerDim(this, client, name, w, h, flags);
2519 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002520 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002521 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002522}
2523
Mathias Agopianac9fa422013-02-11 16:40:36 -08002524status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002525{
Mathias Agopian67106042013-03-14 19:18:13 -07002526 // called by the window manager when it wants to remove a Layer
2527 status_t err = NO_ERROR;
2528 sp<Layer> l(client->getLayerUser(handle));
2529 if (l != NULL) {
2530 err = removeLayer(l);
2531 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2532 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002533 }
2534 return err;
2535}
2536
Mathias Agopian13127d82013-03-05 17:47:11 -08002537status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002538{
Mathias Agopian67106042013-03-14 19:18:13 -07002539 // called by ~LayerCleaner() when all references to the IBinder (handle)
2540 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002541 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002542 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002543 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002544 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002545 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002546 "error removing layer=%p (%s)", l.get(), strerror(-err));
2547 }
2548 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002549}
2550
Mathias Agopianb60314a2012-04-10 22:09:54 -07002551// ---------------------------------------------------------------------------
2552
Andy McFadden13a082e2012-08-24 10:16:42 -07002553void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002554 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002555 Vector<ComposerState> state;
2556 Vector<DisplayState> displays;
2557 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002558 d.what = DisplayState::eDisplayProjectionChanged |
2559 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002560 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002561 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002562 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002563 d.frame.makeInvalid();
2564 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002565 d.width = 0;
2566 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002567 displays.add(d);
2568 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002569 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002570
Dan Stoza9e56aa02015-11-02 13:00:03 -08002571 const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
2572 const nsecs_t period = activeConfig->getVsyncPeriod();
Jamie Gennis6547ff42013-07-16 20:12:42 -07002573 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002574}
2575
2576void SurfaceFlinger::initializeDisplays() {
2577 class MessageScreenInitialized : public MessageBase {
2578 SurfaceFlinger* flinger;
2579 public:
2580 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2581 virtual bool handler() {
2582 flinger->onInitializeDisplays();
2583 return true;
2584 }
2585 };
2586 sp<MessageBase> msg = new MessageScreenInitialized(this);
2587 postMessageAsync(msg); // we may be called from main thread, use async message
2588}
2589
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002590void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2591 int mode) {
2592 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2593 this);
2594 int32_t type = hw->getDisplayType();
2595 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002596
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002597 if (mode == currentMode) {
2598 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002599 return;
2600 }
2601
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002602 hw->setPowerMode(mode);
2603 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2604 ALOGW("Trying to set power mode for virtual display");
2605 return;
2606 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002607
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002608 if (currentMode == HWC_POWER_MODE_OFF) {
2609 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002610 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2611 // FIXME: eventthread only knows about the main display right now
2612 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002613 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002614 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002615
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002616 mVisibleRegionsDirty = true;
Dan Stozab90cf072015-03-05 11:05:59 -08002617 mHasPoweredOff = true;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002618 repaintEverything();
2619 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002620 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002621 disableHardwareVsync(true); // also cancels any in-progress resync
2622
Mathias Agopiancde87a32012-09-13 14:09:01 -07002623 // FIXME: eventthread only knows about the main display right now
2624 mEventThread->onScreenReleased();
2625 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002626
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002627 getHwComposer().setPowerMode(type, mode);
2628 mVisibleRegionsDirty = true;
2629 // from this point on, SF will stop drawing on this display
2630 } else {
2631 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002632 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002633}
2634
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002635void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2636 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002637 SurfaceFlinger& mFlinger;
2638 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002639 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002640 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002641 MessageSetPowerMode(SurfaceFlinger& flinger,
2642 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2643 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002644 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002645 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002646 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002647 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002648 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002649 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002650 ALOGW("Attempt to set power mode = %d for virtual display",
2651 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002652 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002653 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002654 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002655 return true;
2656 }
2657 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002658 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002659 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002660}
2661
2662// ---------------------------------------------------------------------------
2663
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002664status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2665{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002666 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002667
Mathias Agopianbd115332013-04-18 16:41:04 -07002668 IPCThreadState* ipc = IPCThreadState::self();
2669 const int pid = ipc->getCallingPid();
2670 const int uid = ipc->getCallingUid();
2671 if ((uid != AID_SHELL) &&
2672 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002673 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002674 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002675 } else {
Jesse Hallfcd15b42014-12-23 13:57:23 -08002676 // Try to get the main lock, but give up after one second
Mathias Agopian9795c422009-08-26 16:36:26 -07002677 // (this would indicate SF is stuck, but we want to be able to
2678 // print something in dumpsys).
Jesse Hallfcd15b42014-12-23 13:57:23 -08002679 status_t err = mStateLock.timedLock(s2ns(1));
2680 bool locked = (err == NO_ERROR);
Mathias Agopian9795c422009-08-26 16:36:26 -07002681 if (!locked) {
Jesse Hallfcd15b42014-12-23 13:57:23 -08002682 result.appendFormat(
2683 "SurfaceFlinger appears to be unresponsive (%s [%d]), "
2684 "dumping anyways (no locks held)\n", strerror(-err), err);
Mathias Agopian9795c422009-08-26 16:36:26 -07002685 }
2686
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002687 bool dumpAll = true;
2688 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002689 size_t numArgs = args.size();
2690 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002691 if ((index < numArgs) &&
2692 (args[index] == String16("--list"))) {
2693 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002694 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002695 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002696 }
2697
2698 if ((index < numArgs) &&
2699 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002700 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002701 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002702 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002703 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002704
2705 if ((index < numArgs) &&
2706 (args[index] == String16("--latency-clear"))) {
2707 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002708 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002709 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002710 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002711
2712 if ((index < numArgs) &&
2713 (args[index] == String16("--dispsync"))) {
2714 index++;
2715 mPrimaryDispSync.dump(result);
2716 dumpAll = false;
2717 }
Dan Stozab90cf072015-03-05 11:05:59 -08002718
2719 if ((index < numArgs) &&
2720 (args[index] == String16("--static-screen"))) {
2721 index++;
2722 dumpStaticScreenStats(result);
2723 dumpAll = false;
2724 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08002725
2726 if ((index < numArgs) &&
2727 (args[index] == String16("--fences"))) {
2728 index++;
2729 mFenceTracker.dump(&result);
2730 dumpAll = false;
2731 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002732 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002733
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002734 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002735 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002736 }
2737
Mathias Agopian9795c422009-08-26 16:36:26 -07002738 if (locked) {
2739 mStateLock.unlock();
2740 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002741 }
2742 write(fd, result.string(), result.size());
2743 return NO_ERROR;
2744}
2745
Dan Stozac7014012014-02-14 15:03:43 -08002746void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2747 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002748{
2749 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2750 const size_t count = currentLayers.size();
2751 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002752 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002753 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002754 }
2755}
2756
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002757void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002758 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002759{
2760 String8 name;
2761 if (index < args.size()) {
2762 name = String8(args[index]);
2763 index++;
2764 }
2765
Dan Stoza9e56aa02015-11-02 13:00:03 -08002766 const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
2767 const nsecs_t period = activeConfig->getVsyncPeriod();
Greg Hackmann86efcc02014-03-07 12:44:02 -08002768 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002769
2770 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002771 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002772 } else {
2773 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2774 const size_t count = currentLayers.size();
2775 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002776 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002777 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002778 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002779 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002780 }
2781 }
2782}
2783
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002784void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002785 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002786{
2787 String8 name;
2788 if (index < args.size()) {
2789 name = String8(args[index]);
2790 index++;
2791 }
2792
2793 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2794 const size_t count = currentLayers.size();
2795 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002796 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002797 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002798 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002799 }
2800 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002801
Svetoslavd85084b2014-03-20 10:28:31 -07002802 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002803}
2804
Jamie Gennis6547ff42013-07-16 20:12:42 -07002805// This should only be called from the main thread. Otherwise it would need
2806// the lock and should use mCurrentState rather than mDrawingState.
2807void SurfaceFlinger::logFrameStats() {
2808 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2809 const size_t count = drawingLayers.size();
2810 for (size_t i=0 ; i<count ; i++) {
2811 const sp<Layer>& layer(drawingLayers[i]);
2812 layer->logFrameStats();
2813 }
2814
2815 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2816}
2817
Andy McFadden4803b742012-09-24 19:07:20 -07002818/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2819{
2820 static const char* config =
2821 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002822#ifdef HAS_CONTEXT_PRIORITY
2823 " HAS_CONTEXT_PRIORITY"
2824#endif
2825#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2826 " NEVER_DEFAULT_TO_ASYNC_MODE"
2827#endif
2828#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2829 " TARGET_DISABLE_TRIPLE_BUFFERING"
2830#endif
2831 "]";
2832 result.append(config);
2833}
2834
Dan Stozab90cf072015-03-05 11:05:59 -08002835void SurfaceFlinger::dumpStaticScreenStats(String8& result) const
2836{
2837 result.appendFormat("Static screen stats:\n");
2838 for (size_t b = 0; b < NUM_BUCKETS - 1; ++b) {
2839 float bucketTimeSec = mFrameBuckets[b] / 1e9;
2840 float percent = 100.0f *
2841 static_cast<float>(mFrameBuckets[b]) / mTotalTime;
2842 result.appendFormat(" < %zd frames: %.3f s (%.1f%%)\n",
2843 b + 1, bucketTimeSec, percent);
2844 }
2845 float bucketTimeSec = mFrameBuckets[NUM_BUCKETS - 1] / 1e9;
2846 float percent = 100.0f *
2847 static_cast<float>(mFrameBuckets[NUM_BUCKETS - 1]) / mTotalTime;
2848 result.appendFormat(" %zd+ frames: %.3f s (%.1f%%)\n",
2849 NUM_BUCKETS - 1, bucketTimeSec, percent);
2850}
2851
Dan Stozae77c7662016-05-13 11:37:28 -07002852void SurfaceFlinger::recordBufferingStats(const char* layerName,
2853 std::vector<OccupancyTracker::Segment>&& history) {
2854 Mutex::Autolock lock(mBufferingStatsMutex);
2855 auto& stats = mBufferingStats[layerName];
2856 for (const auto& segment : history) {
2857 if (!segment.usedThirdBuffer) {
2858 stats.twoBufferTime += segment.totalTime;
2859 }
2860 if (segment.occupancyAverage < 1.0f) {
2861 stats.doubleBufferedTime += segment.totalTime;
2862 } else if (segment.occupancyAverage < 2.0f) {
2863 stats.tripleBufferedTime += segment.totalTime;
2864 }
2865 ++stats.numSegments;
2866 stats.totalTime += segment.totalTime;
2867 }
2868}
2869
2870void SurfaceFlinger::dumpBufferingStats(String8& result) const {
2871 result.append("Buffering stats:\n");
2872 result.append(" [Layer name] <Active time> <Two buffer> "
2873 "<Double buffered> <Triple buffered>\n");
2874 Mutex::Autolock lock(mBufferingStatsMutex);
2875 typedef std::tuple<std::string, float, float, float> BufferTuple;
2876 std::map<float, BufferTuple, std::greater<float>> sorted;
2877 for (const auto& statsPair : mBufferingStats) {
2878 const char* name = statsPair.first.c_str();
2879 const BufferingStats& stats = statsPair.second;
2880 if (stats.numSegments == 0) {
2881 continue;
2882 }
2883 float activeTime = ns2ms(stats.totalTime) / 1000.0f;
2884 float twoBufferRatio = static_cast<float>(stats.twoBufferTime) /
2885 stats.totalTime;
2886 float doubleBufferRatio = static_cast<float>(
2887 stats.doubleBufferedTime) / stats.totalTime;
2888 float tripleBufferRatio = static_cast<float>(
2889 stats.tripleBufferedTime) / stats.totalTime;
2890 sorted.insert({activeTime, {name, twoBufferRatio,
2891 doubleBufferRatio, tripleBufferRatio}});
2892 }
2893 for (const auto& sortedPair : sorted) {
2894 float activeTime = sortedPair.first;
2895 const BufferTuple& values = sortedPair.second;
2896 result.appendFormat(" [%s] %.2f %.3f %.3f %.3f\n",
2897 std::get<0>(values).c_str(), activeTime,
2898 std::get<1>(values), std::get<2>(values),
2899 std::get<3>(values));
2900 }
2901 result.append("\n");
2902}
2903
2904
Mathias Agopian74d211a2013-04-22 16:55:35 +02002905void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2906 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002907{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002908 bool colorize = false;
2909 if (index < args.size()
2910 && (args[index] == String16("--color"))) {
2911 colorize = true;
2912 index++;
2913 }
2914
2915 Colorizer colorizer(colorize);
2916
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002917 // figure out if we're stuck somewhere
2918 const nsecs_t now = systemTime();
2919 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2920 const nsecs_t inTransaction(mDebugInTransaction);
2921 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2922 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2923
2924 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002925 * Dump library configuration.
2926 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002927
2928 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002929 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002930 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002931 appendSfConfigString(result);
2932 appendUiConfigString(result);
2933 appendGuiConfigString(result);
2934 result.append("\n");
2935
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002936 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002937 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002938 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002939 result.append(SyncFeatures::getInstance().toString());
2940 result.append("\n");
2941
Dan Stoza9e56aa02015-11-02 13:00:03 -08002942 const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
2943
Andy McFadden41d67d72014-04-25 16:58:34 -07002944 colorizer.bold(result);
2945 result.append("DispSync configuration: ");
2946 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002947 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2948 "present offset %d ns (refresh %" PRId64 " ns)",
Dan Stoza9e56aa02015-11-02 13:00:03 -08002949 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs,
2950 PRESENT_TIME_OFFSET_FROM_VSYNC_NS, activeConfig->getVsyncPeriod());
Andy McFadden41d67d72014-04-25 16:58:34 -07002951 result.append("\n");
2952
Dan Stozab90cf072015-03-05 11:05:59 -08002953 // Dump static screen stats
2954 result.append("\n");
2955 dumpStaticScreenStats(result);
2956 result.append("\n");
2957
Dan Stozae77c7662016-05-13 11:37:28 -07002958 dumpBufferingStats(result);
2959
Andy McFadden4803b742012-09-24 19:07:20 -07002960 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002961 * Dump the visible layer list
2962 */
2963 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2964 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002965 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002966 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002967 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002968 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002969 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002970 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002971 }
2972
2973 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002974 * Dump Display state
2975 */
2976
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002977 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002978 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002979 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002980 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2981 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002982 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002983 }
2984
2985 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002986 * Dump SurfaceFlinger global state
2987 */
2988
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002989 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002990 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002991 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002992
Mathias Agopian888c8222012-08-04 21:10:38 -07002993 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002994 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002995
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002996 colorizer.bold(result);
2997 result.appendFormat("EGL implementation : %s\n",
2998 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2999 colorizer.reset(result);
3000 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07003001 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07003002
Mathias Agopian875d8e12013-06-07 15:35:48 -07003003 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003004
Mathias Agopian42977342012-08-05 00:40:46 -07003005 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07003006 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
3007 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02003008 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003009 " last eglSwapBuffers() time: %f us\n"
3010 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08003011 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003012 " refresh-rate : %f fps\n"
3013 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07003014 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07003015 " gpu_to_cpu_unsupported : %d\n"
3016 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003017 mLastSwapBufferTime/1000.0,
3018 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08003019 mTransactionFlags,
Dan Stoza9e56aa02015-11-02 13:00:03 -08003020 1e9 / activeConfig->getVsyncPeriod(),
3021 activeConfig->getDpiX(),
3022 activeConfig->getDpiY(),
Mathias Agopianed985572013-03-22 00:24:39 -07003023 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003024
Mathias Agopian74d211a2013-04-22 16:55:35 +02003025 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003026 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003027
Mathias Agopian74d211a2013-04-22 16:55:35 +02003028 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003029 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003030
3031 /*
3032 * VSYNC state
3033 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02003034 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003035
3036 /*
3037 * Dump HWComposer state
3038 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003039 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02003040 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003041 colorizer.reset(result);
Dan Stoza9f26a9c2016-06-22 14:51:09 -07003042 bool hwcDisabled = mDebugDisableHWC || mDebugRegion;
Dan Stoza9e56aa02015-11-02 13:00:03 -08003043 result.appendFormat(" h/w composer %s\n",
3044 hwcDisabled ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02003045 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003046
3047 /*
3048 * Dump gralloc state
3049 */
3050 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
3051 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003052}
3053
Mathias Agopian13127d82013-03-05 17:47:11 -08003054const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07003055SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07003056 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07003057 wp<IBinder> dpy;
3058 for (size_t i=0 ; i<mDisplays.size() ; i++) {
3059 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
3060 dpy = mDisplays.keyAt(i);
3061 break;
3062 }
3063 }
3064 if (dpy == NULL) {
3065 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
3066 // Just use the primary display so we have something to return
3067 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
3068 }
3069 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07003070}
3071
Keun young Park63f165f2012-08-31 10:53:36 -07003072bool SurfaceFlinger::startDdmConnection()
3073{
3074 void* libddmconnection_dso =
3075 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
3076 if (!libddmconnection_dso) {
3077 return false;
3078 }
3079 void (*DdmConnection_start)(const char* name);
3080 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07003081 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07003082 if (!DdmConnection_start) {
3083 dlclose(libddmconnection_dso);
3084 return false;
3085 }
3086 (*DdmConnection_start)(getServiceName());
3087 return true;
3088}
3089
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003090status_t SurfaceFlinger::onTransact(
3091 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3092{
3093 switch (code) {
3094 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07003095 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07003096 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003097 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07003098 case CLEAR_ANIMATION_FRAME_STATS:
3099 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07003100 case SET_POWER_MODE:
Dan Stozac4f471e2016-03-24 09:31:08 -07003101 case GET_HDR_CAPABILITIES:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003102 {
3103 // codes that require permission check
3104 IPCThreadState* ipc = IPCThreadState::self();
3105 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07003106 const int uid = ipc->getCallingUid();
Jeff Brown3bfe51d2015-04-10 20:20:13 -07003107 if ((uid != AID_GRAPHICS && uid != AID_SYSTEM) &&
Mathias Agopian99b49842011-06-27 16:05:52 -07003108 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00003109 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07003110 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
3111 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003112 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003113 break;
3114 }
3115 case CAPTURE_SCREEN:
3116 {
3117 // codes that require permission check
3118 IPCThreadState* ipc = IPCThreadState::self();
3119 const int pid = ipc->getCallingPid();
3120 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07003121 if ((uid != AID_GRAPHICS) &&
3122 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00003123 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003124 "can't read framebuffer pid=%d, uid=%d", pid, uid);
3125 return PERMISSION_DENIED;
3126 }
3127 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003128 }
3129 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003130
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003131 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
3132 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07003133 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08003134 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07003135 IPCThreadState* ipc = IPCThreadState::self();
3136 const int pid = ipc->getCallingPid();
3137 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00003138 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07003139 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003140 return PERMISSION_DENIED;
3141 }
3142 int n;
3143 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07003144 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07003145 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003146 return NO_ERROR;
3147 case 1002: // SHOW_UPDATES
3148 n = data.readInt32();
3149 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07003150 invalidateHwcGeometry();
3151 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003152 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003153 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07003154 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003155 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003156 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003157 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07003158 setTransactionFlags(
3159 eTransactionNeeded|
3160 eDisplayTransactionNeeded|
3161 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003162 return NO_ERROR;
3163 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08003164 case 1006:{ // send empty update
3165 signalRefresh();
3166 return NO_ERROR;
3167 }
Mathias Agopian53331da2011-08-22 21:44:41 -07003168 case 1008: // toggle use of hw composer
3169 n = data.readInt32();
3170 mDebugDisableHWC = n ? 1 : 0;
3171 invalidateHwcGeometry();
3172 repaintEverything();
3173 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07003174 case 1009: // toggle use of transform hint
3175 n = data.readInt32();
3176 mDebugDisableTransformHint = n ? 1 : 0;
3177 invalidateHwcGeometry();
3178 repaintEverything();
3179 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003180 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07003181 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003182 reply->writeInt32(0);
3183 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07003184 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08003185 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003186 return NO_ERROR;
3187 case 1013: {
3188 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07003189 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
3190 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07003191 return NO_ERROR;
3192 }
3193 case 1014: {
3194 // daltonize
3195 n = data.readInt32();
3196 switch (n % 10) {
Dan Stoza9f26a9c2016-06-22 14:51:09 -07003197 case 1:
3198 mDaltonizer.setType(ColorBlindnessType::Protanomaly);
3199 break;
3200 case 2:
3201 mDaltonizer.setType(ColorBlindnessType::Deuteranomaly);
3202 break;
3203 case 3:
3204 mDaltonizer.setType(ColorBlindnessType::Tritanomaly);
3205 break;
3206 default:
3207 mDaltonizer.setType(ColorBlindnessType::None);
3208 break;
Mathias Agopianff2ed702013-09-01 21:36:12 -07003209 }
3210 if (n >= 10) {
Dan Stoza9f26a9c2016-06-22 14:51:09 -07003211 mDaltonizer.setMode(ColorBlindnessMode::Correction);
Mathias Agopianff2ed702013-09-01 21:36:12 -07003212 } else {
Dan Stoza9f26a9c2016-06-22 14:51:09 -07003213 mDaltonizer.setMode(ColorBlindnessMode::Simulation);
Mathias Agopianff2ed702013-09-01 21:36:12 -07003214 }
Mathias Agopianff2ed702013-09-01 21:36:12 -07003215 invalidateHwcGeometry();
3216 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07003217 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003218 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003219 case 1015: {
3220 // apply a color matrix
3221 n = data.readInt32();
Alan Viverette9c5a3332013-09-12 20:04:35 -07003222 if (n) {
3223 // color matrix is sent as mat3 matrix followed by vec3
3224 // offset, then packed into a mat4 where the last row is
3225 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07003226 for (size_t i = 0 ; i < 4; i++) {
Dan Stoza9f26a9c2016-06-22 14:51:09 -07003227 for (size_t j = 0; j < 4; j++) {
3228 mColorMatrix[i][j] = data.readFloat();
3229 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003230 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003231 } else {
3232 mColorMatrix = mat4();
3233 }
3234 invalidateHwcGeometry();
3235 repaintEverything();
3236 return NO_ERROR;
3237 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003238 // This is an experimental interface
3239 // Needs to be shifted to proper binder interface when we productize
3240 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07003241 n = data.readInt32();
3242 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003243 return NO_ERROR;
3244 }
Dan Stozaee44edd2015-03-23 15:50:23 -07003245 case 1017: {
3246 n = data.readInt32();
3247 mForceFullDamage = static_cast<bool>(n);
3248 return NO_ERROR;
3249 }
Dan Stozadb4ac3c2015-04-14 11:34:01 -07003250 case 1018: { // Modify Choreographer's phase offset
3251 n = data.readInt32();
3252 mEventThread->setPhaseOffset(static_cast<nsecs_t>(n));
3253 return NO_ERROR;
3254 }
3255 case 1019: { // Modify SurfaceFlinger's phase offset
3256 n = data.readInt32();
3257 mSFEventThread->setPhaseOffset(static_cast<nsecs_t>(n));
3258 return NO_ERROR;
3259 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003260 }
3261 }
3262 return err;
3263}
3264
Mathias Agopian53331da2011-08-22 21:44:41 -07003265void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07003266 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08003267 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07003268}
3269
Mathias Agopian59119e62010-10-11 12:37:43 -07003270// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003271// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07003272// ---------------------------------------------------------------------------
3273
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003274/* The code below is here to handle b/8734824
3275 *
3276 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07003277 * from the surfaceflinger thread to the calling binder thread, where they
3278 * are executed. This allows the calling thread in the calling process to be
3279 * reused and not depend on having "enough" binder threads to handle the
3280 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003281 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003282class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07003283 /* Parts of GraphicProducerWrapper are run on two different threads,
3284 * communicating by sending messages via Looper but also by shared member
3285 * data. Coherence maintenance is subtle and in places implicit (ugh).
3286 *
3287 * Don't rely on Looper's sendMessage/handleMessage providing
3288 * release/acquire semantics for any data not actually in the Message.
3289 * Data going from surfaceflinger to binder threads needs to be
3290 * synchronized explicitly.
3291 *
3292 * Barrier open/wait do provide release/acquire semantics. This provides
3293 * implicit synchronization for data coming back from binder to
3294 * surfaceflinger threads.
3295 */
3296
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003297 sp<IGraphicBufferProducer> impl;
3298 sp<Looper> looper;
3299 status_t result;
3300 bool exitPending;
3301 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07003302 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003303 uint32_t code;
3304 Parcel const* data;
3305 Parcel* reply;
3306
3307 enum {
3308 MSG_API_CALL,
3309 MSG_EXIT
3310 };
3311
3312 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003313 * Called on surfaceflinger thread. This is called by our "fake"
3314 * BpGraphicBufferProducer. We package the data and reply Parcel and
3315 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003316 */
3317 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08003318 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003319 this->code = code;
3320 this->data = &data;
3321 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003322 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07003323 // if we've exited, we run the message synchronously right here.
3324 // note (JH): as far as I can tell from looking at the code, this
3325 // never actually happens. if it does, i'm not sure if it happens
3326 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003327 handleMessage(Message(MSG_API_CALL));
3328 } else {
3329 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07003330 // Prevent stores to this->{code, data, reply} from being
3331 // reordered later than the construction of Message.
3332 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003333 looper->sendMessage(this, Message(MSG_API_CALL));
3334 barrier.wait();
3335 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08003336 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003337 }
3338
3339 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003340 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003341 * call the real BpGraphicBufferProducer.
3342 */
3343 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07003344 int what = message.what;
3345 // Prevent reads below from happening before the read from Message
3346 atomic_thread_fence(memory_order_acquire);
3347 if (what == MSG_API_CALL) {
Marco Nelissen097ca272014-11-14 08:01:01 -08003348 result = IInterface::asBinder(impl)->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003349 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07003350 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003351 exitRequested = true;
3352 }
3353 }
3354
3355public:
Jesse Hallb154c422014-07-13 12:47:02 -07003356 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
3357 : impl(impl),
3358 looper(new Looper(true)),
Pablo Ceballos53390e12015-08-04 11:25:59 -07003359 result(NO_ERROR),
Jesse Hallb154c422014-07-13 12:47:02 -07003360 exitPending(false),
Pablo Ceballos53390e12015-08-04 11:25:59 -07003361 exitRequested(false),
3362 code(0),
3363 data(NULL),
3364 reply(NULL)
Jesse Hallb154c422014-07-13 12:47:02 -07003365 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003366
Jesse Hallb154c422014-07-13 12:47:02 -07003367 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003368 status_t waitForResponse() {
3369 do {
3370 looper->pollOnce(-1);
3371 } while (!exitRequested);
3372 return result;
3373 }
3374
Jesse Hallb154c422014-07-13 12:47:02 -07003375 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003376 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07003377 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003378 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07003379 // Ensure this->result is visible to the binder thread before it
3380 // handles the message.
3381 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003382 looper->sendMessage(this, Message(MSG_EXIT));
3383 }
3384};
3385
3386
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003387status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
3388 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003389 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003390 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003391 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003392
3393 if (CC_UNLIKELY(display == 0))
3394 return BAD_VALUE;
3395
3396 if (CC_UNLIKELY(producer == 0))
3397 return BAD_VALUE;
3398
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003399 // if we have secure windows on this display, never allow the screen capture
3400 // unless the producer interface is local (i.e.: we can take a screenshot for
3401 // ourselves).
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003402 bool isLocalScreenshot = IInterface::asBinder(producer)->localBinder();
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003403
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003404 // Convert to surfaceflinger's internal rotation type.
3405 Transform::orientation_flags rotationFlags;
3406 switch (rotation) {
3407 case ISurfaceComposer::eRotateNone:
3408 rotationFlags = Transform::ROT_0;
3409 break;
3410 case ISurfaceComposer::eRotate90:
3411 rotationFlags = Transform::ROT_90;
3412 break;
3413 case ISurfaceComposer::eRotate180:
3414 rotationFlags = Transform::ROT_180;
3415 break;
3416 case ISurfaceComposer::eRotate270:
3417 rotationFlags = Transform::ROT_270;
3418 break;
3419 default:
3420 rotationFlags = Transform::ROT_0;
3421 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3422 break;
3423 }
3424
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003425 class MessageCaptureScreen : public MessageBase {
3426 SurfaceFlinger* flinger;
3427 sp<IBinder> display;
3428 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003429 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003430 uint32_t reqWidth, reqHeight;
3431 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003432 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003433 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003434 status_t result;
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003435 bool isLocalScreenshot;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003436 public:
3437 MessageCaptureScreen(SurfaceFlinger* flinger,
3438 const sp<IBinder>& display,
3439 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003440 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003441 uint32_t minLayerZ, uint32_t maxLayerZ,
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003442 bool useIdentityTransform,
3443 Transform::orientation_flags rotation,
3444 bool isLocalScreenshot)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003445 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003446 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003447 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003448 useIdentityTransform(useIdentityTransform),
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003449 rotation(rotation), result(PERMISSION_DENIED),
3450 isLocalScreenshot(isLocalScreenshot)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003451 {
3452 }
3453 status_t getResult() const {
3454 return result;
3455 }
3456 virtual bool handler() {
3457 Mutex::Autolock _l(flinger->mStateLock);
3458 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003459 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003460 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003461 useIdentityTransform, rotation, isLocalScreenshot);
Marco Nelissen097ca272014-11-14 08:01:01 -08003462 static_cast<GraphicProducerWrapper*>(IInterface::asBinder(producer).get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003463 return true;
3464 }
3465 };
3466
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003467 // this creates a "fake" BBinder which will serve as a "fake" remote
3468 // binder to receive the marshaled calls and forward them to the
3469 // real remote (a BpGraphicBufferProducer)
3470 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3471
3472 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3473 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003474 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003475 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003476 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003477 useIdentityTransform, rotationFlags, isLocalScreenshot);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003478
3479 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003480 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003481 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003482 }
3483 return res;
3484}
3485
Mathias Agopian180f10d2013-04-10 22:55:41 -07003486
3487void SurfaceFlinger::renderScreenImplLocked(
3488 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003489 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003490 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003491 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003492{
3493 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003494 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003495
3496 // get screen geometry
Andreas Gampe89fd4f72014-11-13 14:18:56 -08003497 const int32_t hw_w = hw->getWidth();
3498 const int32_t hw_h = hw->getHeight();
3499 const bool filtering = static_cast<int32_t>(reqWidth) != hw_w ||
Christopher Ferris0e749792015-03-23 14:32:15 -07003500 static_cast<int32_t>(reqHeight) != hw_h;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003501
Dan Stozac1879002014-05-22 15:59:05 -07003502 // if a default or invalid sourceCrop is passed in, set reasonable values
3503 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3504 !sourceCrop.isValid()) {
3505 sourceCrop.setLeftTop(Point(0, 0));
3506 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3507 }
3508
3509 // ensure that sourceCrop is inside screen
3510 if (sourceCrop.left < 0) {
3511 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3512 }
Dan Stozabe31f442014-06-11 11:20:54 -07003513 if (sourceCrop.right > hw_w) {
3514 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003515 }
3516 if (sourceCrop.top < 0) {
3517 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3518 }
Dan Stozabe31f442014-06-11 11:20:54 -07003519 if (sourceCrop.bottom > hw_h) {
3520 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003521 }
3522
Mathias Agopian180f10d2013-04-10 22:55:41 -07003523 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003524 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003525
3526 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003527 engine.setViewportAndProjection(
3528 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003529 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003530
3531 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003532 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003533
3534 const LayerVector& layers( mDrawingState.layersSortedByZ );
3535 const size_t count = layers.size();
3536 for (size_t i=0 ; i<count ; ++i) {
3537 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003538 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003539 if (state.layerStack == hw->getLayerStack()) {
3540 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3541 if (layer->isVisible()) {
3542 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08003543 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003544 if (filtering) layer->setFiltering(false);
3545 }
3546 }
3547 }
3548 }
3549
Mathias Agopian931bda12013-08-28 18:11:46 -07003550 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003551}
3552
3553
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003554status_t SurfaceFlinger::captureScreenImplLocked(
3555 const sp<const DisplayDevice>& hw,
3556 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003557 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003558 uint32_t minLayerZ, uint32_t maxLayerZ,
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003559 bool useIdentityTransform, Transform::orientation_flags rotation,
3560 bool isLocalScreenshot)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003561{
3562 ATRACE_CALL();
3563
Mathias Agopian180f10d2013-04-10 22:55:41 -07003564 // get screen geometry
Dan Stoza35024162015-06-09 16:44:40 -07003565 uint32_t hw_w = hw->getWidth();
3566 uint32_t hw_h = hw->getHeight();
3567
3568 if (rotation & Transform::ROT_90) {
3569 std::swap(hw_w, hw_h);
3570 }
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003571
Mathias Agopian180f10d2013-04-10 22:55:41 -07003572 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3573 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3574 reqWidth, reqHeight, hw_w, hw_h);
3575 return BAD_VALUE;
3576 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003577
Mathias Agopian180f10d2013-04-10 22:55:41 -07003578 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3579 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3580
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003581 bool secureLayerIsVisible = false;
3582 const LayerVector& layers(mDrawingState.layersSortedByZ);
3583 const size_t count = layers.size();
3584 for (size_t i = 0 ; i < count ; ++i) {
3585 const sp<Layer>& layer(layers[i]);
3586 const Layer::State& state(layer->getDrawingState());
3587 if (state.layerStack == hw->getLayerStack() && state.z >= minLayerZ &&
3588 state.z <= maxLayerZ && layer->isVisible() &&
3589 layer->isSecure()) {
3590 secureLayerIsVisible = true;
3591 }
3592 }
3593
3594 if (!isLocalScreenshot && secureLayerIsVisible) {
3595 ALOGW("FB is protected: PERMISSION_DENIED");
3596 return PERMISSION_DENIED;
3597 }
3598
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003599 // create a surface (because we're a producer, and we need to
3600 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003601 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003602 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003603
Michael Lentine5a16a622015-05-21 13:48:24 -07003604 status_t result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
3605 if (result == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003606 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3607 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003608
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003609 int err = 0;
3610 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003611 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003612 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3613 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003614
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003615 if (err == NO_ERROR) {
3616 ANativeWindowBuffer* buffer;
3617 /* TODO: Once we have the sync framework everywhere this can use
3618 * server-side waits on the fence that dequeueBuffer returns.
3619 */
3620 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3621 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003622 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003623 // create an EGLImage from the buffer so we can later
3624 // turn it into a texture
3625 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3626 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3627 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003628 // this binds the given EGLImage as a framebuffer for the
3629 // duration of this scope.
3630 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3631 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003632 // this will in fact render into our dequeued buffer
3633 // via an FBO, which means we didn't have to create
3634 // an EGLSurface and therefore we're not
3635 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003636 renderScreenImplLocked(
3637 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3638 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003639
Riley Andrews86639902014-08-15 12:27:24 -07003640 // Attempt to create a sync khr object that can produce a sync point. If that
3641 // isn't available, create a non-dupable sync object in the fallback path and
3642 // wait on it directly.
3643 EGLSyncKHR sync;
3644 if (!DEBUG_SCREENSHOTS) {
3645 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Riley Andrews9707f4d2014-10-23 16:17:04 -07003646 // native fence fd will not be populated until flush() is done.
3647 getRenderEngine().flush();
Andy McFadden2d8d1202013-10-09 16:38:02 -07003648 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003649 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003650 }
Riley Andrews86639902014-08-15 12:27:24 -07003651 if (sync != EGL_NO_SYNC_KHR) {
3652 // get the sync fd
3653 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3654 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3655 ALOGW("captureScreen: failed to dup sync khr object");
3656 syncFd = -1;
3657 }
3658 eglDestroySyncKHR(mEGLDisplay, sync);
3659 } else {
3660 // fallback path
3661 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3662 if (sync != EGL_NO_SYNC_KHR) {
3663 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3664 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3665 EGLint eglErr = eglGetError();
3666 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3667 ALOGW("captureScreen: fence wait timed out");
3668 } else {
3669 ALOGW_IF(eglErr != EGL_SUCCESS,
3670 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3671 }
3672 eglDestroySyncKHR(mEGLDisplay, sync);
3673 } else {
3674 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3675 }
3676 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003677 if (DEBUG_SCREENSHOTS) {
3678 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3679 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3680 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3681 hw, minLayerZ, maxLayerZ);
3682 delete [] pixels;
3683 }
3684
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003685 } else {
3686 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3687 result = INVALID_OPERATION;
Prathmesh Prabhuf3209b02016-03-09 16:54:45 -08003688 window->cancelBuffer(window, buffer, syncFd);
3689 buffer = NULL;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003690 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003691 // destroy our image
3692 eglDestroyImageKHR(mEGLDisplay, image);
3693 } else {
3694 result = BAD_VALUE;
3695 }
Prathmesh Prabhuf3209b02016-03-09 16:54:45 -08003696 if (buffer) {
3697 // queueBuffer takes ownership of syncFd
3698 result = window->queueBuffer(window, buffer, syncFd);
3699 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003700 }
3701 } else {
3702 result = BAD_VALUE;
3703 }
3704 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3705 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003706
Mathias Agopian74c40c02010-09-29 13:02:36 -07003707 return result;
3708}
3709
Mathias Agopiand5556842013-09-19 17:08:37 -07003710void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3711 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003712 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003713 for (size_t y=0 ; y<h ; y++) {
3714 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3715 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003716 if (p[x] != 0xFF000000) return;
3717 }
3718 }
3719 ALOGE("*** we just took a black screenshot ***\n"
3720 "requested minz=%d, maxz=%d, layerStack=%d",
3721 minLayerZ, maxLayerZ, hw->getLayerStack());
3722 const LayerVector& layers( mDrawingState.layersSortedByZ );
3723 const size_t count = layers.size();
3724 for (size_t i=0 ; i<count ; ++i) {
3725 const sp<Layer>& layer(layers[i]);
3726 const Layer::State& state(layer->getDrawingState());
3727 const bool visible = (state.layerStack == hw->getLayerStack())
3728 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3729 && (layer->isVisible());
Dan Stoza9e56aa02015-11-02 13:00:03 -08003730 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%.3f",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003731 visible ? '+' : '-',
3732 i, layer->getName().string(), state.layerStack, state.z,
3733 layer->isVisible(), state.flags, state.alpha);
3734 }
3735 }
3736}
3737
Pablo Ceballosce796e72016-02-04 19:10:51 -08003738bool SurfaceFlinger::getFrameTimestamps(const Layer& layer,
3739 uint64_t frameNumber, FrameTimestamps* outTimestamps) {
3740 return mFenceTracker.getFrameTimestamps(layer, frameNumber, outTimestamps);
3741}
3742
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003743// ---------------------------------------------------------------------------
3744
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003745SurfaceFlinger::LayerVector::LayerVector() {
3746}
3747
3748SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003749 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003750}
3751
3752int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3753 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003754{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003755 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003756 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3757 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003758
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003759 uint32_t ls = l->getCurrentState().layerStack;
3760 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003761 if (ls != rs)
3762 return ls - rs;
3763
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003764 uint32_t lz = l->getCurrentState().z;
3765 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003766 if (lz != rz)
3767 return lz - rz;
3768
3769 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003770}
3771
3772// ---------------------------------------------------------------------------
3773
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003774SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Pablo Ceballos53390e12015-08-04 11:25:59 -07003775 : type(DisplayDevice::DISPLAY_ID_INVALID),
3776 layerStack(DisplayDevice::NO_LAYER_STACK),
3777 orientation(0),
3778 width(0),
3779 height(0),
3780 isSecure(false) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003781}
3782
Pablo Ceballos53390e12015-08-04 11:25:59 -07003783SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(
3784 DisplayDevice::DisplayType type, bool isSecure)
3785 : type(type),
3786 layerStack(DisplayDevice::NO_LAYER_STACK),
3787 orientation(0),
3788 width(0),
3789 height(0),
3790 isSecure(isSecure) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003791 viewport.makeInvalid();
3792 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003793}
3794
3795// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003796
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003797}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003798
3799
3800#if defined(__gl_h_)
3801#error "don't include gl/gl.h in this file"
3802#endif
3803
3804#if defined(__gl2_h_)
3805#error "don't include gl2/gl2.h in this file"
3806#endif