blob: 5a0f7069ba4e791857565d0a1529a3b1ac1f5ab3 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dan Stoza9e56aa02015-11-02 13:00:03 -080017// #define LOG_NDEBUG 0
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080018#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070021#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <errno.h>
23#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070024#include <dlfcn.h>
Greg Hackmann86efcc02014-03-07 12:44:02 -080025#include <inttypes.h>
Jesse Hallb154c422014-07-13 12:47:02 -070026#include <stdatomic.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070027
28#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029
30#include <cutils/log.h>
31#include <cutils/properties.h>
32
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070033#include <binder/IPCThreadState.h>
34#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070035#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070036#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070037
Mathias Agopianc666cae2012-07-25 18:56:13 -070038#include <ui/DisplayInfo.h>
Lajos Molnar67d8bd62014-09-11 14:58:45 -070039#include <ui/DisplayStatInfo.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070040
Mathias Agopian921e6ac2012-07-23 23:11:29 -070041#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070042#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070043#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070044#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080045#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080046#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070047
48#include <ui/GraphicBufferAllocator.h>
49#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070050#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080051
Mathias Agopiancde87a32012-09-13 14:09:01 -070052#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053#include <utils/String8.h>
54#include <utils/String16.h>
55#include <utils/StopWatch.h>
Yusuke Sato0a688f52015-07-30 23:05:39 -070056#include <utils/Timers.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080057#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
Mathias Agopian921e6ac2012-07-23 23:11:29 -070059#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070060#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061
Mathias Agopian3e25fd82013-04-22 17:52:16 +020062#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020064#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080065#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070066#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070067#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070068#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080069#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073
Mathias Agopiana4912602012-07-12 14:25:33 -070074#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070075#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070076#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077
Mathias Agopianff2ed702013-09-01 21:36:12 -070078#include "Effects/Daltonizer.h"
79
Mathias Agopian875d8e12013-06-07 15:35:48 -070080#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070081#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070082
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083#define DISPLAY_COUNT 1
84
Mathias Agopianfee2b462013-07-03 12:34:01 -070085/*
86 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
87 * black pixels.
88 */
89#define DEBUG_SCREENSHOTS false
90
Mathias Agopianca088332013-03-28 17:44:13 -070091EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
92
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070094
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070095// This is the phase offset in nanoseconds of the software vsync event
96// relative to the vsync event reported by HWComposer. The software vsync
97// event is when SurfaceFlinger and Choreographer-based applications run each
98// frame.
99//
100// This phase offset allows adjustment of the minimum latency from application
101// wake-up (by Choregographer) time to the time at which the resulting window
102// image is displayed. This value may be either positive (after the HW vsync)
103// or negative (before the HW vsync). Setting it to 0 will result in a
104// minimum latency of two vsync periods because the app and SurfaceFlinger
105// will run just after the HW vsync. Setting it to a positive number will
106// result in the minimum latency being:
107//
108// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
109//
110// Note that reducing this latency makes it more likely for the applications
111// to not have their window content image ready in time. When this happens
112// the latency will end up being an additional vsync period, and animations
113// will hiccup. Therefore, this latency should be tuned somewhat
114// conservatively (or at least with awareness of the trade-off being made).
115static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
116
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700117// This is the phase offset at which SurfaceFlinger's composition runs.
118static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
119
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120// ---------------------------------------------------------------------------
121
Mathias Agopian99b49842011-06-27 16:05:52 -0700122const String16 sHardwareTest("android.permission.HARDWARE_TEST");
123const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
124const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
125const String16 sDump("android.permission.DUMP");
126
127// ---------------------------------------------------------------------------
128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700130 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700132 mTransactionPending(false),
133 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700134 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700135 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700136 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 mBootTime(systemTime()),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800138 mBuiltinDisplays(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 mVisibleRegionsDirty(false),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800140 mGeometryInvalid(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800141 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700143 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700144 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700145 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700146 mDebugInSwapBuffers(0),
147 mLastSwapBufferTime(0),
148 mDebugInTransaction(0),
149 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700150 mBootFinished(false),
Dan Stozaee44edd2015-03-23 15:50:23 -0700151 mForceFullDamage(false),
Tim Murray4a4e4a22016-04-19 16:29:23 +0000152 mPrimaryDispSync("PrimaryDispSync"),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700153 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700154 mHWVsyncAvailable(false),
Dan Stozab90cf072015-03-05 11:05:59 -0800155 mHasColorMatrix(false),
156 mHasPoweredOff(false),
157 mFrameBuckets(),
158 mTotalTime(0),
159 mLastSwapTime(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160{
Steve Blocka19954a2012-01-04 20:05:49 +0000161 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162
163 // debugging stuff...
164 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700165
Mathias Agopianb4b17302013-03-20 18:36:41 -0700166 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700167 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700168
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 property_get("debug.sf.showupdates", value, "0");
170 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700171
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700172 property_get("debug.sf.ddms", value, "0");
173 mDebugDDMS = atoi(value);
174 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700175 if (!startDdmConnection()) {
176 // start failed, and DDMS debugging not enabled
177 mDebugDDMS = 0;
178 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700179 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700180 ALOGI_IF(mDebugRegion, "showupdates enabled");
181 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
Dan Stozac5da2712016-07-20 15:38:12 -0700182
183 property_get("debug.sf.disable_backpressure", value, "0");
184 mPropagateBackpressure = !atoi(value);
185 ALOGI_IF(!mPropagateBackpressure, "Disabling backpressure propagation");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186}
187
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800188void SurfaceFlinger::onFirstRef()
189{
190 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800191}
192
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193SurfaceFlinger::~SurfaceFlinger()
194{
Mathias Agopiana4912602012-07-12 14:25:33 -0700195 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
196 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
197 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198}
199
Dan Stozac7014012014-02-14 15:03:43 -0800200void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800201{
202 // the window manager died on us. prepare its eulogy.
203
Andy McFadden13a082e2012-08-24 10:16:42 -0700204 // restore initial conditions (default device unblank, etc)
205 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800206
207 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700208 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800209}
210
Mathias Agopian7e27f052010-05-28 14:22:23 -0700211sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212{
Mathias Agopian96f08192010-06-02 23:28:45 -0700213 sp<ISurfaceComposerClient> bclient;
214 sp<Client> client(new Client(this));
215 status_t err = client->initCheck();
216 if (err == NO_ERROR) {
217 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 return bclient;
220}
221
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700222sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
223 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700224{
225 class DisplayToken : public BBinder {
226 sp<SurfaceFlinger> flinger;
227 virtual ~DisplayToken() {
228 // no more references, this display must be terminated
229 Mutex::Autolock _l(flinger->mStateLock);
230 flinger->mCurrentState.displays.removeItem(this);
231 flinger->setTransactionFlags(eDisplayTransactionNeeded);
232 }
233 public:
234 DisplayToken(const sp<SurfaceFlinger>& flinger)
235 : flinger(flinger) {
236 }
237 };
238
239 sp<BBinder> token = new DisplayToken(this);
240
241 Mutex::Autolock _l(mStateLock);
Pablo Ceballos53390e12015-08-04 11:25:59 -0700242 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL, secure);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700243 info.displayName = displayName;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700244 mCurrentState.displays.add(token, info);
245
246 return token;
247}
248
Jesse Hall6c913be2013-08-08 12:15:49 -0700249void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
250 Mutex::Autolock _l(mStateLock);
251
252 ssize_t idx = mCurrentState.displays.indexOfKey(display);
253 if (idx < 0) {
254 ALOGW("destroyDisplay: invalid display token");
255 return;
256 }
257
258 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
259 if (!info.isVirtualDisplay()) {
260 ALOGE("destroyDisplay called for non-virtual display");
261 return;
262 }
263
264 mCurrentState.displays.removeItemsAt(idx);
265 setTransactionFlags(eDisplayTransactionNeeded);
266}
267
Jesse Hall692c7232012-11-08 15:41:56 -0800268void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800269 ALOGV("createBuiltinDisplayLocked(%d)", type);
Jesse Hall692c7232012-11-08 15:41:56 -0800270 ALOGW_IF(mBuiltinDisplays[type],
271 "Overwriting display token for display type %d", type);
272 mBuiltinDisplays[type] = new BBinder();
Jesse Hall692c7232012-11-08 15:41:56 -0800273 // All non-virtual displays are currently considered secure.
Pablo Ceballos53390e12015-08-04 11:25:59 -0700274 DisplayDeviceState info(type, true);
Jesse Hall692c7232012-11-08 15:41:56 -0800275 mCurrentState.displays.add(mBuiltinDisplays[type], info);
276}
277
Mathias Agopiane57f2922012-08-09 16:29:12 -0700278sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700279 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700280 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
281 return NULL;
282 }
Jesse Hall692c7232012-11-08 15:41:56 -0800283 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700284}
285
Jamie Gennis9a78c902011-01-12 18:30:40 -0800286sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
287{
288 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
289 return gba;
290}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700291
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800292void SurfaceFlinger::bootFinished()
293{
294 const nsecs_t now = systemTime();
295 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000296 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700297 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700298
299 // wait patiently for the window manager death
300 const String16 name("window");
301 sp<IBinder> window(defaultServiceManager()->getService(name));
302 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700303 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700304 }
305
306 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700307 // formerly we would just kill the process, but we now ask it to exit so it
308 // can choose where to stop the animation.
309 property_set("service.bootanim.exit", "1");
Yusuke Sato0a688f52015-07-30 23:05:39 -0700310
311 const int LOGTAG_SF_STOP_BOOTANIM = 60110;
312 LOG_EVENT_LONG(LOGTAG_SF_STOP_BOOTANIM,
313 ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314}
315
Mathias Agopian3f844832013-08-07 21:24:32 -0700316void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700317 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700318 RenderEngine& engine;
319 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700320 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700321 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
322 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700323 }
324 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700325 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700326 return true;
327 }
328 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700329 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700330}
331
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700332class DispSyncSource : public VSyncSource, private DispSync::Callback {
333public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700334 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
Tim Murray4a4e4a22016-04-19 16:29:23 +0000335 const char* name) :
336 mName(name),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700337 mValue(0),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700338 mTraceVsync(traceVsync),
Tim Murray4a4e4a22016-04-19 16:29:23 +0000339 mVsyncOnLabel(String8::format("VsyncOn-%s", name)),
340 mVsyncEventLabel(String8::format("VSYNC-%s", name)),
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700341 mDispSync(dispSync),
342 mCallbackMutex(),
343 mCallback(),
344 mVsyncMutex(),
345 mPhaseOffset(phaseOffset),
346 mEnabled(false) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700347
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700348 virtual ~DispSyncSource() {}
349
350 virtual void setVSyncEnabled(bool enable) {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700351 Mutex::Autolock lock(mVsyncMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700352 if (enable) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000353 status_t err = mDispSync->addEventListener(mName, mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700354 static_cast<DispSync::Callback*>(this));
355 if (err != NO_ERROR) {
356 ALOGE("error registering vsync callback: %s (%d)",
357 strerror(-err), err);
358 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700359 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700360 } else {
361 status_t err = mDispSync->removeEventListener(
362 static_cast<DispSync::Callback*>(this));
363 if (err != NO_ERROR) {
364 ALOGE("error unregistering vsync callback: %s (%d)",
365 strerror(-err), err);
366 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700367 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700368 }
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700369 mEnabled = enable;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700370 }
371
372 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700373 Mutex::Autolock lock(mCallbackMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700374 mCallback = callback;
375 }
376
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700377 virtual void setPhaseOffset(nsecs_t phaseOffset) {
378 Mutex::Autolock lock(mVsyncMutex);
379
380 // Normalize phaseOffset to [0, period)
381 auto period = mDispSync->getPeriod();
382 phaseOffset %= period;
383 if (phaseOffset < 0) {
384 // If we're here, then phaseOffset is in (-period, 0). After this
385 // operation, it will be in (0, period)
386 phaseOffset += period;
387 }
388 mPhaseOffset = phaseOffset;
389
390 // If we're not enabled, we don't need to mess with the listeners
391 if (!mEnabled) {
392 return;
393 }
394
395 // Remove the listener with the old offset
396 status_t err = mDispSync->removeEventListener(
397 static_cast<DispSync::Callback*>(this));
398 if (err != NO_ERROR) {
399 ALOGE("error unregistering vsync callback: %s (%d)",
400 strerror(-err), err);
401 }
402
403 // Add a listener with the new offset
Tim Murray4a4e4a22016-04-19 16:29:23 +0000404 err = mDispSync->addEventListener(mName, mPhaseOffset,
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700405 static_cast<DispSync::Callback*>(this));
406 if (err != NO_ERROR) {
407 ALOGE("error registering vsync callback: %s (%d)",
408 strerror(-err), err);
409 }
410 }
411
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700412private:
413 virtual void onDispSyncEvent(nsecs_t when) {
414 sp<VSyncSource::Callback> callback;
415 {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700416 Mutex::Autolock lock(mCallbackMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700417 callback = mCallback;
418
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700419 if (mTraceVsync) {
420 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700421 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700422 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700423 }
424
425 if (callback != NULL) {
426 callback->onVSyncEvent(when);
427 }
428 }
429
Tim Murray4a4e4a22016-04-19 16:29:23 +0000430 const char* const mName;
431
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700432 int mValue;
433
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700434 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700435 const String8 mVsyncOnLabel;
436 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700437
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700438 DispSync* mDispSync;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700439
440 Mutex mCallbackMutex; // Protects the following
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700441 sp<VSyncSource::Callback> mCallback;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700442
443 Mutex mVsyncMutex; // Protects the following
444 nsecs_t mPhaseOffset;
445 bool mEnabled;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700446};
447
448void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700449 ALOGI( "SurfaceFlinger's main thread ready to run. "
450 "Initializing graphics H/W...");
451
Dan Stoza9e56aa02015-11-02 13:00:03 -0800452 { // Autolock scope
453 Mutex::Autolock _l(mStateLock);
454
455 // initialize EGL for the default display
456 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
457 eglInitialize(mEGLDisplay, NULL, NULL);
458
459 // start the EventThread
460 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
461 vsyncPhaseOffsetNs, true, "app");
Tim Murray4a4e4a22016-04-19 16:29:23 +0000462 mEventThread = new EventThread(vsyncSrc, *this);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800463 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
464 sfVsyncPhaseOffsetNs, true, "sf");
Tim Murray4a4e4a22016-04-19 16:29:23 +0000465 mSFEventThread = new EventThread(sfVsyncSrc, *this);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800466 mEventQueue.setEventThread(mSFEventThread);
467
Tim Murray41a38532016-06-22 12:42:10 -0700468 // set EventThread and SFEventThread to SCHED_FIFO for minimum jitter
469 struct sched_param param = {0};
470 param.sched_priority = 1;
471 if (sched_setscheduler(mEventThread->getTid(), SCHED_FIFO, &param) != 0) {
472 ALOGE("Couldn't set SCHED_FIFO for EventThread");
473 }
474
475 if (sched_setscheduler(mSFEventThread->getTid(), SCHED_FIFO, &param) != 0) {
476 ALOGE("Couldn't set SCHED_FIFO for SFEventThread");
477 }
478
479
Dan Stoza9e56aa02015-11-02 13:00:03 -0800480 // Get a RenderEngine for the given display / config (can't fail)
481 mRenderEngine = RenderEngine::create(mEGLDisplay,
482 HAL_PIXEL_FORMAT_RGBA_8888);
483 }
484
485 // Drop the state lock while we initialize the hardware composer. We drop
486 // the lock because on creation, it will call back into SurfaceFlinger to
487 // initialize the primary display.
488 mHwc = new HWComposer(this);
489 mHwc->setEventHandler(static_cast<HWComposer::EventHandler*>(this));
490
Jesse Hall692c7232012-11-08 15:41:56 -0800491 Mutex::Autolock _l(mStateLock);
492
Mathias Agopian875d8e12013-06-07 15:35:48 -0700493 // retrieve the EGL context that was selected/created
494 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700495
Mathias Agopianda27af92012-09-13 18:17:13 -0700496 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
497 "couldn't create EGLContext");
498
Dan Stoza9e56aa02015-11-02 13:00:03 -0800499 // make the GLContext current so that we can create textures when creating
500 // Layers (which may happens before we render something)
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700501 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
502
Jamie Gennisd1700752013-10-14 12:22:52 -0700503 mEventControlThread = new EventControlThread(this);
504 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
505
Mathias Agopian92a979a2012-08-02 18:32:23 -0700506 // initialize our drawing state
507 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700508
Andy McFadden13a082e2012-08-24 10:16:42 -0700509 // set initial conditions (e.g. unblank default device)
510 initializeDisplays();
511
Dan Stoza4e637772016-07-28 13:31:51 -0700512 mRenderEngine->primeCache();
513
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700514 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700515 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800516
Dan Stoza9e56aa02015-11-02 13:00:03 -0800517 ALOGV("Done initializing");
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700518}
519
Mathias Agopiana67e4182012-06-19 17:26:12 -0700520void SurfaceFlinger::startBootAnim() {
521 // start boot animation
522 property_set("service.bootanim.exit", "0");
523 property_set("ctl.start", "bootanim");
524}
525
Mathias Agopian875d8e12013-06-07 15:35:48 -0700526size_t SurfaceFlinger::getMaxTextureSize() const {
527 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700528}
529
Mathias Agopian875d8e12013-06-07 15:35:48 -0700530size_t SurfaceFlinger::getMaxViewportDims() const {
531 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700532}
533
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800534// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800535
Jamie Gennis582270d2011-08-17 18:19:00 -0700536bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800537 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800538 Mutex::Autolock _l(mStateLock);
Marco Nelissen097ca272014-11-14 08:01:01 -0800539 sp<IBinder> surfaceTextureBinder(IInterface::asBinder(bufferProducer));
Mathias Agopian67106042013-03-14 19:18:13 -0700540 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800541}
542
Dan Stoza7f7da322014-05-02 15:26:25 -0700543status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
544 Vector<DisplayInfo>* configs) {
Tatenda Chipeperekwa23e16bb2014-10-29 16:47:19 -0700545 if ((configs == NULL) || (display.get() == NULL)) {
Dan Stoza7f7da322014-05-02 15:26:25 -0700546 return BAD_VALUE;
547 }
548
Naseer Ahmed7aa0c472014-11-03 14:49:23 -0500549 if (!display.get())
550 return NAME_NOT_FOUND;
551
Jesse Hall692c7232012-11-08 15:41:56 -0800552 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700553 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800554 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700555 type = i;
556 break;
557 }
558 }
559
560 if (type < 0) {
561 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700562 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700563
Mathias Agopian8b736f12012-08-13 17:54:26 -0700564 // TODO: Not sure if display density should handled by SF any longer
565 class Density {
566 static int getDensityFromProperty(char const* propName) {
567 char property[PROPERTY_VALUE_MAX];
568 int density = 0;
569 if (property_get(propName, property, NULL) > 0) {
570 density = atoi(property);
571 }
572 return density;
573 }
574 public:
575 static int getEmuDensity() {
576 return getDensityFromProperty("qemu.sf.lcd_density"); }
577 static int getBuildDensity() {
578 return getDensityFromProperty("ro.sf.lcd_density"); }
579 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700580
Dan Stoza7f7da322014-05-02 15:26:25 -0700581 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700582
Dan Stoza9e56aa02015-11-02 13:00:03 -0800583 for (const auto& hwConfig : getHwComposer().getConfigs(type)) {
Dan Stoza7f7da322014-05-02 15:26:25 -0700584 DisplayInfo info = DisplayInfo();
585
Dan Stoza9e56aa02015-11-02 13:00:03 -0800586 float xdpi = hwConfig->getDpiX();
587 float ydpi = hwConfig->getDpiY();
Dan Stoza7f7da322014-05-02 15:26:25 -0700588
589 if (type == DisplayDevice::DISPLAY_PRIMARY) {
590 // The density of the device is provided by a build property
591 float density = Density::getBuildDensity() / 160.0f;
592 if (density == 0) {
593 // the build doesn't provide a density -- this is wrong!
594 // use xdpi instead
595 ALOGE("ro.sf.lcd_density must be defined as a build property");
596 density = xdpi / 160.0f;
597 }
598 if (Density::getEmuDensity()) {
599 // if "qemu.sf.lcd_density" is specified, it overrides everything
600 xdpi = ydpi = density = Density::getEmuDensity();
601 density /= 160.0f;
602 }
603 info.density = density;
604
605 // TODO: this needs to go away (currently needed only by webkit)
606 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
607 info.orientation = hw->getOrientation();
608 } else {
609 // TODO: where should this value come from?
610 static const int TV_DENSITY = 213;
611 info.density = TV_DENSITY / 160.0f;
612 info.orientation = 0;
613 }
614
Dan Stoza9e56aa02015-11-02 13:00:03 -0800615 info.w = hwConfig->getWidth();
616 info.h = hwConfig->getHeight();
Dan Stoza7f7da322014-05-02 15:26:25 -0700617 info.xdpi = xdpi;
618 info.ydpi = ydpi;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800619 info.fps = 1e9 / hwConfig->getVsyncPeriod();
Andy McFadden91b2ca82014-06-13 14:04:23 -0700620 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800621
Andy McFadden91b2ca82014-06-13 14:04:23 -0700622 // This is how far in advance a buffer must be queued for
623 // presentation at a given time. If you want a buffer to appear
624 // on the screen at time N, you must submit the buffer before
625 // (N - presentationDeadline).
626 //
627 // Normally it's one full refresh period (to give SF a chance to
628 // latch the buffer), but this can be reduced by configuring a
629 // DispSync offset. Any additional delays introduced by the hardware
630 // composer or panel must be accounted for here.
631 //
632 // We add an additional 1ms to allow for processing time and
633 // differences between the ideal and actual refresh rate.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800634 info.presentationDeadline = hwConfig->getVsyncPeriod() -
635 SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700636
637 // All non-virtual displays are currently considered secure.
638 info.secure = true;
639
Michael Wright28f24d02016-07-12 13:30:53 -0700640 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700641 }
642
Dan Stoza7f7da322014-05-02 15:26:25 -0700643 return NO_ERROR;
644}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700645
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800646status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& /* display */,
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700647 DisplayStatInfo* stats) {
648 if (stats == NULL) {
649 return BAD_VALUE;
650 }
651
652 // FIXME for now we always return stats for the primary display
653 memset(stats, 0, sizeof(*stats));
654 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
655 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
656 return NO_ERROR;
657}
658
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700659int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
Dan Stoza24a42e92015-03-09 10:04:11 -0700660 sp<DisplayDevice> device(getDisplayDevice(display));
661 if (device != NULL) {
662 return device->getActiveConfig();
663 }
664 return BAD_VALUE;
Dan Stoza7f7da322014-05-02 15:26:25 -0700665}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700666
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700667void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
668 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
669 this);
670 int32_t type = hw->getDisplayType();
671 int currentMode = hw->getActiveConfig();
672
673 if (mode == currentMode) {
674 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
675 return;
676 }
677
678 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
679 ALOGW("Trying to set config for virtual display");
680 return;
681 }
682
683 hw->setActiveConfig(mode);
684 getHwComposer().setActiveConfig(type, mode);
685}
686
687status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
688 class MessageSetActiveConfig: public MessageBase {
689 SurfaceFlinger& mFlinger;
690 sp<IBinder> mDisplay;
691 int mMode;
692 public:
693 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
694 int mode) :
695 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
696 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700697 Vector<DisplayInfo> configs;
698 mFlinger.getDisplayConfigs(mDisplay, &configs);
Jesse Hall78442112014-08-07 22:43:06 -0700699 if (mMode < 0 || mMode >= static_cast<int>(configs.size())) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700700 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700701 mMode, configs.size());
Michael Wright28f24d02016-07-12 13:30:53 -0700702 return true;
Michael Lentine7306c672014-07-30 13:00:37 -0700703 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700704 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
705 if (hw == NULL) {
706 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700707 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700708 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
709 ALOGW("Attempt to set active config = %d for virtual display",
710 mMode);
711 } else {
712 mFlinger.setActiveConfigInternal(hw, mMode);
713 }
714 return true;
715 }
716 };
717 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
718 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700719 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700720}
Michael Wright28f24d02016-07-12 13:30:53 -0700721status_t SurfaceFlinger::getDisplayColorModes(const sp<IBinder>& display,
722 Vector<android_color_mode_t>* outColorModes) {
723 if ((outColorModes == nullptr) || (display.get() == nullptr)) {
724 return BAD_VALUE;
725 }
726
727 if (!display.get()) {
728 return NAME_NOT_FOUND;
729 }
730
731 int32_t type = NAME_NOT_FOUND;
732 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
733 if (display == mBuiltinDisplays[i]) {
734 type = i;
735 break;
736 }
737 }
738
739 if (type < 0) {
740 return type;
741 }
742
743 std::vector<android_color_mode_t> modes = getHwComposer().getColorModes(type);
744 outColorModes->clear();
745 std::copy(modes.cbegin(), modes.cend(), std::back_inserter(*outColorModes));
746
747 return NO_ERROR;
748}
749
750android_color_mode_t SurfaceFlinger::getActiveColorMode(const sp<IBinder>& display) {
751 sp<DisplayDevice> device(getDisplayDevice(display));
752 if (device != nullptr) {
753 return device->getActiveColorMode();
754 }
755 return static_cast<android_color_mode_t>(BAD_VALUE);
756}
757
758void SurfaceFlinger::setActiveColorModeInternal(const sp<DisplayDevice>& hw,
759 android_color_mode_t mode) {
760 ALOGD("Set active color mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
761 this);
762 int32_t type = hw->getDisplayType();
763 android_color_mode_t currentMode = hw->getActiveColorMode();
764
765 if (mode == currentMode) {
766 ALOGD("Screen type=%d is already in color mode=%d", hw->getDisplayType(), mode);
767 return;
768 }
769
770 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
771 ALOGW("Trying to set config for virtual display");
772 return;
773 }
774
775 hw->setActiveColorMode(mode);
776 getHwComposer().setActiveColorMode(type, mode);
777}
778
779
780status_t SurfaceFlinger::setActiveColorMode(const sp<IBinder>& display,
781 android_color_mode_t colorMode) {
782 class MessageSetActiveColorMode: public MessageBase {
783 SurfaceFlinger& mFlinger;
784 sp<IBinder> mDisplay;
785 android_color_mode_t mMode;
786 public:
787 MessageSetActiveColorMode(SurfaceFlinger& flinger, const sp<IBinder>& disp,
788 android_color_mode_t mode) :
789 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
790 virtual bool handler() {
791 Vector<android_color_mode_t> modes;
792 mFlinger.getDisplayColorModes(mDisplay, &modes);
793 bool exists = std::find(std::begin(modes), std::end(modes), mMode) != std::end(modes);
794 if (mMode < 0 || !exists) {
795 ALOGE("Attempt to set invalid active color mode = %d for display %p", mMode,
796 mDisplay.get());
797 return true;
798 }
799 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
800 if (hw == nullptr) {
801 ALOGE("Attempt to set active color mode = %d for null display %p",
802 mMode, mDisplay.get());
803 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
804 ALOGW("Attempt to set active color mode= %d for virtual display",
805 mMode);
806 } else {
807 mFlinger.setActiveColorModeInternal(hw, mMode);
808 }
809 return true;
810 }
811 };
812 sp<MessageBase> msg = new MessageSetActiveColorMode(*this, display, colorMode);
813 postMessageSync(msg);
814 return NO_ERROR;
815}
Mathias Agopianc666cae2012-07-25 18:56:13 -0700816
Svetoslavd85084b2014-03-20 10:28:31 -0700817status_t SurfaceFlinger::clearAnimationFrameStats() {
818 Mutex::Autolock _l(mStateLock);
819 mAnimFrameTracker.clearStats();
820 return NO_ERROR;
821}
822
823status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
824 Mutex::Autolock _l(mStateLock);
825 mAnimFrameTracker.getStats(outStats);
826 return NO_ERROR;
827}
828
Dan Stozac4f471e2016-03-24 09:31:08 -0700829status_t SurfaceFlinger::getHdrCapabilities(const sp<IBinder>& display,
830 HdrCapabilities* outCapabilities) const {
831 Mutex::Autolock _l(mStateLock);
832
833 sp<const DisplayDevice> displayDevice(getDisplayDevice(display));
834 if (displayDevice == nullptr) {
835 ALOGE("getHdrCapabilities: Invalid display %p", displayDevice.get());
836 return BAD_VALUE;
837 }
838
839 std::unique_ptr<HdrCapabilities> capabilities =
840 mHwc->getHdrCapabilities(displayDevice->getHwcDisplayId());
841 if (capabilities) {
842 std::swap(*outCapabilities, *capabilities);
843 } else {
844 return BAD_VALUE;
845 }
846
847 return NO_ERROR;
848}
849
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800850// ----------------------------------------------------------------------------
851
852sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800853 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700854}
855
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800856// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800857
858void SurfaceFlinger::waitForEvent() {
859 mEventQueue.waitMessage();
860}
861
862void SurfaceFlinger::signalTransaction() {
863 mEventQueue.invalidate();
864}
865
866void SurfaceFlinger::signalLayerUpdate() {
867 mEventQueue.invalidate();
868}
869
870void SurfaceFlinger::signalRefresh() {
871 mEventQueue.refresh();
872}
873
874status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800875 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800876 return mEventQueue.postMessage(msg, reltime);
877}
878
879status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800880 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800881 status_t res = mEventQueue.postMessage(msg, reltime);
882 if (res == NO_ERROR) {
883 msg->wait();
884 }
885 return res;
886}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800887
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700888void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700889 do {
890 waitForEvent();
891 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800892}
893
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700894void SurfaceFlinger::enableHardwareVsync() {
895 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700896 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700897 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700898 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
899 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700900 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700901 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700902}
903
Jesse Hall948fe0c2013-10-14 12:56:09 -0700904void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700905 Mutex::Autolock _l(mHWVsyncLock);
906
Jesse Hall948fe0c2013-10-14 12:56:09 -0700907 if (makeAvailable) {
908 mHWVsyncAvailable = true;
909 } else if (!mHWVsyncAvailable) {
Dan Stoza0a3c4d62016-04-19 11:56:20 -0700910 // Hardware vsync is not currently available, so abort the resync
911 // attempt for now
Jesse Hall948fe0c2013-10-14 12:56:09 -0700912 return;
913 }
914
Dan Stoza9e56aa02015-11-02 13:00:03 -0800915 const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
916 const nsecs_t period = activeConfig->getVsyncPeriod();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700917
918 mPrimaryDispSync.reset();
919 mPrimaryDispSync.setPeriod(period);
920
921 if (!mPrimaryHWVsyncEnabled) {
922 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700923 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
924 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700925 mPrimaryHWVsyncEnabled = true;
926 }
927}
928
Jesse Hall948fe0c2013-10-14 12:56:09 -0700929void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700930 Mutex::Autolock _l(mHWVsyncLock);
931 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700932 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
933 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700934 mPrimaryDispSync.endResync();
935 mPrimaryHWVsyncEnabled = false;
936 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700937 if (makeUnavailable) {
938 mHWVsyncAvailable = false;
939 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700940}
941
Tim Murray4a4e4a22016-04-19 16:29:23 +0000942void SurfaceFlinger::resyncWithRateLimit() {
943 static constexpr nsecs_t kIgnoreDelay = ms2ns(500);
944 if (systemTime() - mLastSwapTime > kIgnoreDelay) {
Dan Stoza0a3c4d62016-04-19 11:56:20 -0700945 resyncToHardwareVsync(false);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000946 }
947}
948
Dan Stoza9e56aa02015-11-02 13:00:03 -0800949void SurfaceFlinger::onVSyncReceived(int32_t type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700950 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700951
Jamie Gennisd1700752013-10-14 12:22:52 -0700952 { // Scope for the lock
953 Mutex::Autolock _l(mHWVsyncLock);
954 if (type == 0 && mPrimaryHWVsyncEnabled) {
955 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700956 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700957 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700958
959 if (needsHwVsync) {
960 enableHardwareVsync();
961 } else {
962 disableHardwareVsync(false);
963 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700964}
965
Dan Stoza9e56aa02015-11-02 13:00:03 -0800966void SurfaceFlinger::onHotplugReceived(int32_t disp, bool connected) {
967 ALOGV("onHotplugReceived(%d, %s)", disp, connected ? "true" : "false");
968 if (disp == DisplayDevice::DISPLAY_PRIMARY) {
969 Mutex::Autolock lock(mStateLock);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700970
Dan Stoza9e56aa02015-11-02 13:00:03 -0800971 // All non-virtual displays are currently considered secure.
972 bool isSecure = true;
973
974 int32_t type = DisplayDevice::DISPLAY_PRIMARY;
975 createBuiltinDisplayLocked(DisplayDevice::DISPLAY_PRIMARY);
976 wp<IBinder> token = mBuiltinDisplays[type];
977
978 sp<IGraphicBufferProducer> producer;
979 sp<IGraphicBufferConsumer> consumer;
980 BufferQueue::createBufferQueue(&producer, &consumer,
981 new GraphicBufferAlloc());
982
983 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc,
984 DisplayDevice::DISPLAY_PRIMARY, consumer);
985 sp<DisplayDevice> hw = new DisplayDevice(this,
986 DisplayDevice::DISPLAY_PRIMARY, disp, isSecure, token, fbs,
987 producer, mRenderEngine->getEGLConfig());
988 mDisplays.add(token, hw);
989 } else {
990 auto type = DisplayDevice::DISPLAY_EXTERNAL;
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700991 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800992 if (connected) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800993 createBuiltinDisplayLocked(type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700994 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800995 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
996 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700997 }
998 setTransactionFlags(eDisplayTransactionNeeded);
999
Andy McFadden9e9689c2012-10-10 18:17:51 -07001000 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001001 }
Mathias Agopian86303202012-07-24 22:46:10 -07001002}
1003
Dan Stoza9e56aa02015-11-02 13:00:03 -08001004void SurfaceFlinger::setVsyncEnabled(int disp, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001005 ATRACE_CALL();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001006 getHwComposer().setVsyncEnabled(disp,
1007 enabled ? HWC2::Vsync::Enable : HWC2::Vsync::Disable);
Mathias Agopian86303202012-07-24 22:46:10 -07001008}
1009
Mathias Agopian4fec8732012-06-29 14:12:52 -07001010void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001011 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001012 switch (what) {
Dan Stoza6b9454d2014-11-07 16:00:59 -08001013 case MessageQueue::INVALIDATE: {
Dan Stoza50182882016-07-08 12:02:20 -07001014 bool frameMissed = !mHadClientComposition &&
1015 mPreviousPresentFence != Fence::NO_FENCE &&
1016 mPreviousPresentFence->getSignalTime() == INT64_MAX;
1017 ATRACE_INT("FrameMissed", static_cast<int>(frameMissed));
Dan Stozac5da2712016-07-20 15:38:12 -07001018 if (mPropagateBackpressure && frameMissed) {
Dan Stoza50182882016-07-08 12:02:20 -07001019 signalLayerUpdate();
1020 break;
1021 }
1022
Dan Stoza6b9454d2014-11-07 16:00:59 -08001023 bool refreshNeeded = handleMessageTransaction();
1024 refreshNeeded |= handleMessageInvalidate();
Dan Stoza58784442014-12-02 16:58:17 -08001025 refreshNeeded |= mRepaintEverything;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001026 if (refreshNeeded) {
Dan Stoza58784442014-12-02 16:58:17 -08001027 // Signal a refresh if a transaction modified the window state,
1028 // a new buffer was latched, or if HWC has requested a full
1029 // repaint
Dan Stoza6b9454d2014-11-07 16:00:59 -08001030 signalRefresh();
1031 }
1032 break;
1033 }
1034 case MessageQueue::REFRESH: {
1035 handleMessageRefresh();
1036 break;
1037 }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001038 }
1039}
1040
Dan Stoza6b9454d2014-11-07 16:00:59 -08001041bool SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001042 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001043 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -07001044 handleTransaction(transactionFlags);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001045 return true;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001046 }
Dan Stoza6b9454d2014-11-07 16:00:59 -08001047 return false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001048}
1049
Dan Stoza6b9454d2014-11-07 16:00:59 -08001050bool SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001051 ATRACE_CALL();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001052 return handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001053}
1054
1055void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001056 ATRACE_CALL();
Dan Stoza14cd37c2015-07-09 12:43:33 -07001057
Pablo Ceballos40845df2016-01-25 17:41:15 -08001058 nsecs_t refreshStartTime = systemTime(SYSTEM_TIME_MONOTONIC);
Dan Stoza14cd37c2015-07-09 12:43:33 -07001059
Dan Stoza05dacfb2016-07-01 13:33:38 -07001060 preComposition();
1061 rebuildLayerStacks();
1062 setUpHWComposer();
1063 doDebugFlashRegions();
1064 doComposition();
1065 postComposition(refreshStartTime);
1066
1067 mPreviousPresentFence = mHwc->getRetireFence(HWC_DISPLAY_PRIMARY);
Dan Stozabfbffeb2016-07-21 14:49:33 -07001068
1069 mHadClientComposition = false;
1070 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
1071 const sp<DisplayDevice>& displayDevice = mDisplays[displayId];
1072 mHadClientComposition = mHadClientComposition ||
1073 mHwc->hasClientComposition(displayDevice->getHwcDisplayId());
1074 }
Dan Stoza14cd37c2015-07-09 12:43:33 -07001075
Dan Stoza9e56aa02015-11-02 13:00:03 -08001076 // Release any buffers which were replaced this frame
1077 for (auto& layer : mLayersWithQueuedFrames) {
1078 layer->releasePendingBuffer();
1079 }
1080 mLayersWithQueuedFrames.clear();
Mathias Agopiancd60f992012-08-16 16:28:27 -07001081}
Mathias Agopian4fec8732012-06-29 14:12:52 -07001082
Mathias Agopiancd60f992012-08-16 16:28:27 -07001083void SurfaceFlinger::doDebugFlashRegions()
1084{
1085 // is debugging enabled
1086 if (CC_LIKELY(!mDebugRegion))
1087 return;
1088
1089 const bool repaintEverything = mRepaintEverything;
1090 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1091 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001092 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001093 // transform the dirty region into this screen's coordinate space
1094 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
1095 if (!dirtyRegion.isEmpty()) {
1096 // redraw the whole screen
1097 doComposeSurfaces(hw, Region(hw->bounds()));
1098
1099 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -07001100 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001101 RenderEngine& engine(getRenderEngine());
1102 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
1103
Mathias Agopianda27af92012-09-13 18:17:13 -07001104 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -07001105 }
1106 }
1107 }
1108
1109 postFramebuffer();
1110
1111 if (mDebugRegion > 1) {
1112 usleep(mDebugRegion * 1000);
1113 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -07001114
Dan Stoza9e56aa02015-11-02 13:00:03 -08001115 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
Dan Stoza7bdf55a2016-06-17 11:29:01 -07001116 auto& displayDevice = mDisplays[displayId];
1117 if (!displayDevice->isDisplayOn()) {
1118 continue;
1119 }
1120
1121 status_t result = displayDevice->prepareFrame(*mHwc);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001122 ALOGE_IF(result != NO_ERROR, "prepareFrame for display %zd failed:"
1123 " %d (%s)", displayId, result, strerror(-result));
Mathias Agopianbb53b0e2012-09-24 21:27:29 -07001124 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001125}
1126
1127void SurfaceFlinger::preComposition()
1128{
Dan Stoza9e56aa02015-11-02 13:00:03 -08001129 ATRACE_CALL();
1130 ALOGV("preComposition");
1131
Mathias Agopiancd60f992012-08-16 16:28:27 -07001132 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001133 const LayerVector& layers(mDrawingState.layersSortedByZ);
1134 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -07001135 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001136 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001137 needExtraInvalidate = true;
1138 }
1139 }
1140 if (needExtraInvalidate) {
1141 signalLayerUpdate();
1142 }
1143}
1144
Pablo Ceballos40845df2016-01-25 17:41:15 -08001145void SurfaceFlinger::postComposition(nsecs_t refreshStartTime)
Mathias Agopiancd60f992012-08-16 16:28:27 -07001146{
Dan Stoza9e56aa02015-11-02 13:00:03 -08001147 ATRACE_CALL();
1148 ALOGV("postComposition");
1149
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001150 const LayerVector& layers(mDrawingState.layersSortedByZ);
1151 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -07001152 for (size_t i=0 ; i<count ; i++) {
Dan Stozae77c7662016-05-13 11:37:28 -07001153 bool frameLatched = layers[i]->onPostComposition();
1154 if (frameLatched) {
1155 recordBufferingStats(layers[i]->getName().string(),
1156 layers[i]->getOccupancyHistory(false));
1157 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001158 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001159
Dan Stoza9e56aa02015-11-02 13:00:03 -08001160 sp<Fence> presentFence = mHwc->getRetireFence(HWC_DISPLAY_PRIMARY);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001161
1162 if (presentFence->isValid()) {
1163 if (mPrimaryDispSync.addPresentFence(presentFence)) {
1164 enableHardwareVsync();
1165 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -07001166 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001167 }
1168 }
1169
Dan Stozab90cf072015-03-05 11:05:59 -08001170 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Andy McFadden5167ec62014-05-22 13:08:43 -07001171 if (kIgnorePresentFences) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001172 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001173 enableHardwareVsync();
1174 }
1175 }
1176
Pablo Ceballos40845df2016-01-25 17:41:15 -08001177 mFenceTracker.addFrame(refreshStartTime, presentFence,
1178 hw->getVisibleLayersSortedByZ(), hw->getClientTargetAcquireFence());
1179
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001180 if (mAnimCompositionPending) {
1181 mAnimCompositionPending = false;
1182
Jesse Halla9a1b002013-02-27 16:39:25 -08001183 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001184 mAnimFrameTracker.setActualPresentFence(presentFence);
1185 } else {
1186 // The HWC doesn't support present fences, so use the refresh
1187 // timestamp instead.
Dan Stoza9e56aa02015-11-02 13:00:03 -08001188 nsecs_t presentTime =
1189 mHwc->getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001190 mAnimFrameTracker.setActualPresentTime(presentTime);
1191 }
1192 mAnimFrameTracker.advanceFrame();
1193 }
Dan Stozab90cf072015-03-05 11:05:59 -08001194
1195 if (hw->getPowerMode() == HWC_POWER_MODE_OFF) {
1196 return;
1197 }
1198
1199 nsecs_t currentTime = systemTime();
1200 if (mHasPoweredOff) {
1201 mHasPoweredOff = false;
1202 } else {
1203 nsecs_t period = mPrimaryDispSync.getPeriod();
1204 nsecs_t elapsedTime = currentTime - mLastSwapTime;
1205 size_t numPeriods = static_cast<size_t>(elapsedTime / period);
1206 if (numPeriods < NUM_BUCKETS - 1) {
1207 mFrameBuckets[numPeriods] += elapsedTime;
1208 } else {
1209 mFrameBuckets[NUM_BUCKETS - 1] += elapsedTime;
1210 }
1211 mTotalTime += elapsedTime;
1212 }
1213 mLastSwapTime = currentTime;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001214}
1215
1216void SurfaceFlinger::rebuildLayerStacks() {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001217 ATRACE_CALL();
1218 ALOGV("rebuildLayerStacks");
1219
Mathias Agopiancd60f992012-08-16 16:28:27 -07001220 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001221 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001222 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -07001223 mVisibleRegionsDirty = false;
1224 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001225
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001226 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001227 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001228 Region opaqueRegion;
1229 Region dirtyRegion;
Dan Stoza9e56aa02015-11-02 13:00:03 -08001230 Vector<sp<Layer>> layersSortedByZ;
1231 const sp<DisplayDevice>& displayDevice(mDisplays[dpy]);
1232 const Transform& tr(displayDevice->getTransform());
1233 const Rect bounds(displayDevice->getBounds());
1234 if (displayDevice->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001235 SurfaceFlinger::computeVisibleRegions(layers,
Dan Stoza9e56aa02015-11-02 13:00:03 -08001236 displayDevice->getLayerStack(), dirtyRegion,
1237 opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001238
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001239 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001240 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001241 const sp<Layer>& layer(layers[i]);
1242 const Layer::State& s(layer->getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -08001243 if (s.layerStack == displayDevice->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -07001244 Region drawRegion(tr.transform(
1245 layer->visibleNonTransparentRegion));
1246 drawRegion.andSelf(bounds);
1247 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001248 layersSortedByZ.add(layer);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001249 } else {
1250 // Clear out the HWC layer if this layer was
1251 // previously visible, but no longer is
1252 layer->setHwcLayer(displayDevice->getHwcDisplayId(),
1253 nullptr);
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001254 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001255 }
1256 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001257 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001258 displayDevice->setVisibleLayersSortedByZ(layersSortedByZ);
1259 displayDevice->undefinedRegion.set(bounds);
1260 displayDevice->undefinedRegion.subtractSelf(
1261 tr.transform(opaqueRegion));
1262 displayDevice->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001263 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001264 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001265}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001266
Mathias Agopiancd60f992012-08-16 16:28:27 -07001267void SurfaceFlinger::setUpHWComposer() {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001268 ATRACE_CALL();
1269 ALOGV("setUpHWComposer");
1270
Jesse Hall028dc8f2013-08-20 16:35:32 -07001271 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001272 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1273 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1274 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1275
1276 // If nothing has changed (!dirty), don't recompose.
1277 // If something changed, but we don't currently have any visible layers,
1278 // and didn't when we last did a composition, then skip it this time.
1279 // The second rule does two things:
1280 // - When all layers are removed from a display, we'll emit one black
1281 // frame, then nothing more until we get new layers.
1282 // - When a display is created with a private layer stack, we won't
1283 // emit any black frames until a layer is added to the layer stack.
1284 bool mustRecompose = dirty && !(empty && wasEmpty);
1285
1286 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1287 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1288 mustRecompose ? "doing" : "skipping",
1289 dirty ? "+" : "-",
1290 empty ? "+" : "-",
1291 wasEmpty ? "+" : "-");
1292
Dan Stoza71433162014-02-04 16:22:36 -08001293 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001294
1295 if (mustRecompose) {
1296 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1297 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001298 }
1299
Dan Stoza9e56aa02015-11-02 13:00:03 -08001300 // build the h/w work list
1301 if (CC_UNLIKELY(mGeometryInvalid)) {
1302 mGeometryInvalid = false;
1303 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1304 sp<const DisplayDevice> displayDevice(mDisplays[dpy]);
1305 const auto hwcId = displayDevice->getHwcDisplayId();
1306 if (hwcId >= 0) {
1307 const Vector<sp<Layer>>& currentLayers(
1308 displayDevice->getVisibleLayersSortedByZ());
1309 bool foundLayerWithoutHwc = false;
1310 for (auto& layer : currentLayers) {
1311 if (!layer->hasHwcLayer(hwcId)) {
1312 auto hwcLayer = mHwc->createLayer(hwcId);
1313 if (hwcLayer) {
1314 layer->setHwcLayer(hwcId, std::move(hwcLayer));
1315 } else {
1316 layer->forceClientComposition(hwcId);
1317 foundLayerWithoutHwc = true;
1318 continue;
Jamie Gennisa4310c82012-09-25 20:26:00 -07001319 }
1320 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001321
Dan Stoza9e56aa02015-11-02 13:00:03 -08001322 layer->setGeometry(displayDevice);
Dan Stoza9f26a9c2016-06-22 14:51:09 -07001323 if (mDebugDisableHWC || mDebugRegion) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001324 layer->forceClientComposition(hwcId);
Riley Andrews03414a12014-07-01 14:22:59 -07001325 }
1326 }
1327 }
1328 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001329 }
Riley Andrews03414a12014-07-01 14:22:59 -07001330
Dan Stoza9f26a9c2016-06-22 14:51:09 -07001331
1332 mat4 colorMatrix = mColorMatrix * mDaltonizer();
1333
Dan Stoza9e56aa02015-11-02 13:00:03 -08001334 // Set the per-frame data
1335 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
1336 auto& displayDevice = mDisplays[displayId];
1337 const auto hwcId = displayDevice->getHwcDisplayId();
1338 if (hwcId < 0) {
1339 continue;
Jesse Hall38efe862013-04-06 23:12:29 -07001340 }
Dan Stoza9f26a9c2016-06-22 14:51:09 -07001341 if (colorMatrix != mPreviousColorMatrix) {
1342 status_t result = mHwc->setColorTransform(hwcId, colorMatrix);
1343 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on "
1344 "display %zd: %d", displayId, result);
1345 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001346 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
1347 layer->setPerFrameData(displayDevice);
1348 }
1349 }
1350
Dan Stoza9f26a9c2016-06-22 14:51:09 -07001351 mPreviousColorMatrix = colorMatrix;
1352
Dan Stoza9e56aa02015-11-02 13:00:03 -08001353 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
Dan Stoza7bdf55a2016-06-17 11:29:01 -07001354 auto& displayDevice = mDisplays[displayId];
1355 if (!displayDevice->isDisplayOn()) {
1356 continue;
1357 }
1358
1359 status_t result = displayDevice->prepareFrame(*mHwc);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001360 ALOGE_IF(result != NO_ERROR, "prepareFrame for display %zd failed:"
1361 " %d (%s)", displayId, result, strerror(-result));
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001362 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001363}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001364
Mathias Agopiancd60f992012-08-16 16:28:27 -07001365void SurfaceFlinger::doComposition() {
1366 ATRACE_CALL();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001367 ALOGV("doComposition");
1368
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001369 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001370 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001371 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001372 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001373 // transform the dirty region into this screen's coordinate space
1374 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001375
1376 // repaint the framebuffer (if needed)
1377 doDisplayComposition(hw, dirtyRegion);
1378
Mathias Agopiancd60f992012-08-16 16:28:27 -07001379 hw->dirtyRegion.clear();
1380 hw->flip(hw->swapRegion);
1381 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001382 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001383 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001384 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001385}
1386
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001387void SurfaceFlinger::postFramebuffer()
1388{
Mathias Agopian841cde52012-03-01 15:44:37 -08001389 ATRACE_CALL();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001390 ALOGV("postFramebuffer");
Mathias Agopianb048cef2012-02-04 15:44:04 -08001391
Mathias Agopiana44b0412011-10-16 18:46:35 -07001392 const nsecs_t now = systemTime();
1393 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001394
Dan Stoza9e56aa02015-11-02 13:00:03 -08001395 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
1396 auto& displayDevice = mDisplays[displayId];
Dan Stoza7bdf55a2016-06-17 11:29:01 -07001397 if (!displayDevice->isDisplayOn()) {
1398 continue;
1399 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001400 const auto hwcId = displayDevice->getHwcDisplayId();
1401 if (hwcId >= 0) {
1402 mHwc->commit(hwcId);
Mathias Agopian2a231842012-09-24 18:12:35 -07001403 }
Dan Stoza2dc3be82016-04-06 14:05:37 -07001404 displayDevice->onSwapBuffersCompleted();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001405 if (displayId == 0) {
1406 // Make the default display current because the VirtualDisplayDevice
1407 // code cannot deal with dequeueBuffer() being called outside of the
1408 // composition loop; however the code below can call glFlush() which
1409 // is allowed to (and does in some case) call dequeueBuffer().
1410 displayDevice->makeCurrent(mEGLDisplay, mEGLContext);
1411 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001412 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
1413 sp<Fence> releaseFence = Fence::NO_FENCE;
1414 if (layer->getCompositionType(hwcId) == HWC2::Composition::Client) {
1415 releaseFence = displayDevice->getClientTargetAcquireFence();
1416 } else {
1417 auto hwcLayer = layer->getHwcLayer(hwcId);
1418 releaseFence = mHwc->getLayerReleaseFence(hwcId, hwcLayer);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001419 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001420 layer->onLayerDisplayed(releaseFence);
1421 }
1422 if (hwcId >= 0) {
1423 mHwc->clearReleaseFences(hwcId);
Jesse Hallef194142012-06-14 14:45:17 -07001424 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001425 }
1426
Mathias Agopiana44b0412011-10-16 18:46:35 -07001427 mLastSwapBufferTime = systemTime() - now;
1428 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001429
1430 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1431 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1432 logFrameStats();
1433 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001434}
1435
Mathias Agopian87baae12012-07-31 12:38:26 -07001436void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001437{
Mathias Agopian841cde52012-03-01 15:44:37 -08001438 ATRACE_CALL();
1439
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001440 // here we keep a copy of the drawing state (that is the state that's
1441 // going to be overwritten by handleTransactionLocked()) outside of
1442 // mStateLock so that the side-effects of the State assignment
1443 // don't happen with mStateLock held (which can cause deadlocks).
1444 State drawingState(mDrawingState);
1445
Mathias Agopianca4d3602011-05-19 15:38:14 -07001446 Mutex::Autolock _l(mStateLock);
1447 const nsecs_t now = systemTime();
1448 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001449
Mathias Agopianca4d3602011-05-19 15:38:14 -07001450 // Here we're guaranteed that some transaction flags are set
1451 // so we can call handleTransactionLocked() unconditionally.
1452 // We call getTransactionFlags(), which will also clear the flags,
1453 // with mStateLock held to guarantee that mCurrentState won't change
1454 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001455
Mathias Agopiane57f2922012-08-09 16:29:12 -07001456 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001457 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001458
Mathias Agopianca4d3602011-05-19 15:38:14 -07001459 mLastTransactionTime = systemTime() - now;
1460 mDebugInTransaction = 0;
1461 invalidateHwcGeometry();
1462 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001463}
1464
Mathias Agopian87baae12012-07-31 12:38:26 -07001465void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001466{
1467 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001468 const size_t count = currentLayers.size();
1469
Dan Stoza7dde5992015-05-22 09:51:44 -07001470 // Notify all layers of available frames
1471 for (size_t i = 0; i < count; ++i) {
1472 currentLayers[i]->notifyAvailableFrames();
1473 }
1474
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001475 /*
1476 * Traversal of the children
1477 * (perform the transaction for each of them if needed)
1478 */
1479
Mathias Agopian3559b072012-08-15 13:46:03 -07001480 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001482 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1484 if (!trFlags) continue;
1485
1486 const uint32_t flags = layer->doTransaction(0);
1487 if (flags & Layer::eVisibleRegion)
1488 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489 }
1490 }
1491
1492 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001493 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001494 */
1495
Mathias Agopiane57f2922012-08-09 16:29:12 -07001496 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001497 // here we take advantage of Vector's copy-on-write semantics to
1498 // improve performance by skipping the transaction entirely when
1499 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001500 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1501 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001502 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001503 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001504 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001505 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001506
1507 // find the displays that were removed
1508 // (ie: in drawing state but not in current state)
1509 // also handle displays that changed
1510 // (ie: displays that are in both lists)
1511 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001512 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1513 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001514 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001515 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001516 // Call makeCurrent() on the primary display so we can
1517 // be sure that nothing associated with this display
1518 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001519 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001520 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001521 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1522 if (hw != NULL)
1523 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001524 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001525 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001526 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001527 } else {
1528 ALOGW("trying to remove the main display");
1529 }
1530 } else {
1531 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001532 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001533 const wp<IBinder>& display(curr.keyAt(j));
Marco Nelissen097ca272014-11-14 08:01:01 -08001534 const sp<IBinder> state_binder = IInterface::asBinder(state.surface);
1535 const sp<IBinder> draw_binder = IInterface::asBinder(draw[i].surface);
Dan Albert1474f882014-09-08 18:59:09 -07001536 if (state_binder != draw_binder) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001537 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001538 // recreating the DisplayDevice, so we just remove it
1539 // from the drawing state, so that it get re-added
1540 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001541 sp<DisplayDevice> hw(getDisplayDevice(display));
1542 if (hw != NULL)
1543 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001544 mDisplays.removeItem(display);
1545 mDrawingState.displays.removeItemsAt(i);
1546 dc--; i--;
1547 // at this point we must loop to the next item
1548 continue;
1549 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001550
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001551 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001552 if (disp != NULL) {
1553 if (state.layerStack != draw[i].layerStack) {
1554 disp->setLayerStack(state.layerStack);
1555 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001556 if ((state.orientation != draw[i].orientation)
1557 || (state.viewport != draw[i].viewport)
1558 || (state.frame != draw[i].frame))
1559 {
1560 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001561 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001562 }
Michael Lentine47e45402014-07-18 15:34:25 -07001563 if (state.width != draw[i].width || state.height != draw[i].height) {
1564 disp->setDisplaySize(state.width, state.height);
1565 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001566 }
1567 }
1568 }
1569
1570 // find displays that were added
1571 // (ie: in current state but not in drawing state)
1572 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001573 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001574 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001575
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001576 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001577 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001578 sp<IGraphicBufferProducer> bqProducer;
1579 sp<IGraphicBufferConsumer> bqConsumer;
Dan Stoza70982a52016-01-11 23:40:44 +00001580 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1581 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001582
Dan Stoza9e56aa02015-11-02 13:00:03 -08001583 int32_t hwcId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001584 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001585 // Virtual displays without a surface are dormant:
1586 // they have external state (layer stack, projection,
1587 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001588 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001589
Dan Stoza1f3efb12014-10-15 16:34:55 -07001590 int width = 0;
1591 int status = state.surface->query(
1592 NATIVE_WINDOW_WIDTH, &width);
1593 ALOGE_IF(status != NO_ERROR,
1594 "Unable to query width (%d)", status);
1595 int height = 0;
1596 status = state.surface->query(
1597 NATIVE_WINDOW_HEIGHT, &height);
1598 ALOGE_IF(status != NO_ERROR,
1599 "Unable to query height (%d)", status);
Dan Stoza5cf424b2016-05-20 14:02:39 -07001600 int intFormat = 0;
1601 status = state.surface->query(
1602 NATIVE_WINDOW_FORMAT, &intFormat);
1603 ALOGE_IF(status != NO_ERROR,
1604 "Unable to query format (%d)", status);
1605 auto format = static_cast<android_pixel_format_t>(
1606 intFormat);
Dan Stoza1f3efb12014-10-15 16:34:55 -07001607
Dan Stoza5cf424b2016-05-20 14:02:39 -07001608 mHwc->allocateVirtualDisplay(width, height, &format,
Dan Stoza9e56aa02015-11-02 13:00:03 -08001609 &hwcId);
1610
Dan Stoza5cf424b2016-05-20 14:02:39 -07001611 // TODO: Plumb requested format back up to consumer
1612
Dan Stoza9e56aa02015-11-02 13:00:03 -08001613 sp<VirtualDisplaySurface> vds =
1614 new VirtualDisplaySurface(*mHwc,
1615 hwcId, state.surface, bqProducer,
1616 bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001617
1618 dispSurface = vds;
Michael Lentine47e45402014-07-18 15:34:25 -07001619 producer = vds;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001620 }
1621 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001622 ALOGE_IF(state.surface!=NULL,
1623 "adding a supported display, but rendering "
1624 "surface is provided (%p), ignoring it",
1625 state.surface.get());
Dan Stoza9e56aa02015-11-02 13:00:03 -08001626 if (state.type == DisplayDevice::DISPLAY_EXTERNAL) {
1627 hwcId = DisplayDevice::DISPLAY_EXTERNAL;
1628 dispSurface = new FramebufferSurface(*mHwc,
1629 DisplayDevice::DISPLAY_EXTERNAL,
1630 bqConsumer);
1631 producer = bqProducer;
1632 } else {
1633 ALOGE("Attempted to add non-external non-virtual"
1634 " display");
1635 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07001636 }
1637
1638 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001639 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001640 sp<DisplayDevice> hw = new DisplayDevice(this,
Dan Stoza9e56aa02015-11-02 13:00:03 -08001641 state.type, hwcId, state.isSecure, display,
1642 dispSurface, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -08001643 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001644 hw->setLayerStack(state.layerStack);
1645 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001646 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001647 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001648 mDisplays.add(display, hw);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001649 if (!state.isVirtualDisplay()) {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001650 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001651 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001652 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001653 }
1654 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001655 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001656 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001657
Mathias Agopian84300952012-11-21 16:02:13 -08001658 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1659 // The transform hint might have changed for some layers
1660 // (either because a display has changed, or because a layer
1661 // as changed).
1662 //
1663 // Walk through all the layers in currentLayers,
1664 // and update their transform hint.
1665 //
1666 // If a layer is visible only on a single display, then that
1667 // display is used to calculate the hint, otherwise we use the
1668 // default display.
1669 //
1670 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1671 // the hint is set before we acquire a buffer from the surface texture.
1672 //
1673 // NOTE: layer transactions have taken place already, so we use their
1674 // drawing state. However, SurfaceFlinger's own transaction has not
1675 // happened yet, so we must use the current state layer list
1676 // (soon to become the drawing state list).
1677 //
1678 sp<const DisplayDevice> disp;
1679 uint32_t currentlayerStack = 0;
1680 for (size_t i=0; i<count; i++) {
1681 // NOTE: we rely on the fact that layers are sorted by
1682 // layerStack first (so we don't have to traverse the list
1683 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001684 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001685 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001686 if (i==0 || currentlayerStack != layerStack) {
1687 currentlayerStack = layerStack;
1688 // figure out if this layerstack is mirrored
1689 // (more than one display) if so, pick the default display,
1690 // if not, pick the only display it's on.
1691 disp.clear();
1692 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1693 sp<const DisplayDevice> hw(mDisplays[dpy]);
1694 if (hw->getLayerStack() == currentlayerStack) {
1695 if (disp == NULL) {
1696 disp = hw;
1697 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001698 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001699 break;
1700 }
1701 }
1702 }
1703 }
Chet Haase91d25932013-04-11 15:24:55 -07001704 if (disp == NULL) {
1705 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1706 // redraw after transform hint changes. See bug 8508397.
1707
1708 // could be null when this layer is using a layerStack
1709 // that is not visible on any display. Also can occur at
1710 // screen off/on times.
1711 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001712 }
Chet Haase91d25932013-04-11 15:24:55 -07001713 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001714 }
1715 }
1716
1717
Mathias Agopian3559b072012-08-15 13:46:03 -07001718 /*
1719 * Perform our own transaction if needed
1720 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001721
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001722 const LayerVector& layers(mDrawingState.layersSortedByZ);
1723 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001724 // layers have been added
1725 mVisibleRegionsDirty = true;
1726 }
1727
1728 // some layers might have been removed, so
1729 // we need to update the regions they're exposing.
1730 if (mLayersRemoved) {
1731 mLayersRemoved = false;
1732 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001733 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001734 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001735 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001736 if (currentLayers.indexOf(layer) < 0) {
1737 // this layer is not visible anymore
1738 // TODO: we could traverse the tree from front to back and
1739 // compute the actual visible region
1740 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001741 const Layer::State& s(layer->getDrawingState());
Robert Carr3dcabfa2016-03-01 18:36:58 -08001742 Region visibleReg = s.active.transform.transform(
Mathias Agopian1501d542012-09-04 21:04:09 -07001743 Region(Rect(s.active.w, s.active.h)));
1744 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001745 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001746 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001747 }
1748
1749 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001750
1751 updateCursorAsync();
1752}
1753
1754void SurfaceFlinger::updateCursorAsync()
1755{
Dan Stoza9e56aa02015-11-02 13:00:03 -08001756 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
1757 auto& displayDevice = mDisplays[displayId];
1758 if (displayDevice->getHwcDisplayId() < 0) {
Riley Andrews03414a12014-07-01 14:22:59 -07001759 continue;
1760 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001761
1762 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
1763 layer->updateCursorPosition(displayDevice);
Riley Andrews03414a12014-07-01 14:22:59 -07001764 }
1765 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001766}
1767
1768void SurfaceFlinger::commitTransaction()
1769{
1770 if (!mLayersPendingRemoval.isEmpty()) {
1771 // Notify removed layers now that they can't be drawn from
1772 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
Dan Stozae77c7662016-05-13 11:37:28 -07001773 recordBufferingStats(mLayersPendingRemoval[i]->getName().string(),
1774 mLayersPendingRemoval[i]->getOccupancyHistory(true));
Mathias Agopian4fec8732012-06-29 14:12:52 -07001775 mLayersPendingRemoval[i]->onRemoved();
1776 }
1777 mLayersPendingRemoval.clear();
1778 }
1779
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001780 // If this transaction is part of a window animation then the next frame
1781 // we composite should be considered an animation as well.
1782 mAnimCompositionPending = mAnimTransactionPending;
1783
Mathias Agopian4fec8732012-06-29 14:12:52 -07001784 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001785 mTransactionPending = false;
1786 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001787 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001788}
1789
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001790void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001791 const LayerVector& currentLayers, uint32_t layerStack,
1792 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001793{
Mathias Agopian841cde52012-03-01 15:44:37 -08001794 ATRACE_CALL();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001795 ALOGV("computeVisibleRegions");
Mathias Agopian841cde52012-03-01 15:44:37 -08001796
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001797 Region aboveOpaqueLayers;
1798 Region aboveCoveredLayers;
1799 Region dirty;
1800
Mathias Agopian87baae12012-07-31 12:38:26 -07001801 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001802
1803 size_t i = currentLayers.size();
1804 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001805 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001806
1807 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001808 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001809
Jesse Hall01e29052013-02-19 16:13:35 -08001810 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001811 if (s.layerStack != layerStack)
1812 continue;
1813
Mathias Agopianab028732010-03-16 16:41:46 -07001814 /*
1815 * opaqueRegion: area of a surface that is fully opaque.
1816 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001817 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001818
1819 /*
1820 * visibleRegion: area of a surface that is visible on screen
1821 * and not fully transparent. This is essentially the layer's
1822 * footprint minus the opaque regions above it.
1823 * Areas covered by a translucent surface are considered visible.
1824 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001825 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001826
1827 /*
1828 * coveredRegion: area of a surface that is covered by all
1829 * visible regions above it (which includes the translucent areas).
1830 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001831 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001832
Jesse Halla8026d22012-09-25 13:25:04 -07001833 /*
1834 * transparentRegion: area of a surface that is hinted to be completely
1835 * transparent. This is only used to tell when the layer has no visible
1836 * non-transparent regions and can be removed from the layer list. It
1837 * does not affect the visibleRegion of this layer or any layers
1838 * beneath it. The hint may not be correct if apps don't respect the
1839 * SurfaceView restrictions (which, sadly, some don't).
1840 */
1841 Region transparentRegion;
1842
Mathias Agopianab028732010-03-16 16:41:46 -07001843
1844 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001845 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001846 const bool translucent = !layer->isOpaque(s);
Robert Carr3dcabfa2016-03-01 18:36:58 -08001847 Rect bounds(s.active.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001848 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001849 if (!visibleRegion.isEmpty()) {
1850 // Remove the transparent area from the visible region
1851 if (translucent) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001852 const Transform tr(s.active.transform);
Dan Stoza22f7fc42016-05-10 16:19:53 -07001853 if (tr.preserveRects()) {
1854 // transform the transparent region
1855 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001856 } else {
Dan Stoza22f7fc42016-05-10 16:19:53 -07001857 // transformation too complex, can't do the
1858 // transparent region optimization.
1859 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001860 }
Mathias Agopianab028732010-03-16 16:41:46 -07001861 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001862
Mathias Agopianab028732010-03-16 16:41:46 -07001863 // compute the opaque region
Robert Carr3dcabfa2016-03-01 18:36:58 -08001864 const int32_t layerOrientation = s.active.transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001865 if (s.alpha == 1.0f && !translucent &&
Mathias Agopianab028732010-03-16 16:41:46 -07001866 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1867 // the opaque region is the layer's footprint
1868 opaqueRegion = visibleRegion;
1869 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001870 }
1871 }
1872
Mathias Agopianab028732010-03-16 16:41:46 -07001873 // Clip the covered region to the visible region
1874 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1875
1876 // Update aboveCoveredLayers for next (lower) layer
1877 aboveCoveredLayers.orSelf(visibleRegion);
1878
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001879 // subtract the opaque region covered by the layers above us
1880 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001881
1882 // compute this layer's dirty region
1883 if (layer->contentDirty) {
1884 // we need to invalidate the whole region
1885 dirty = visibleRegion;
1886 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001887 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001888 layer->contentDirty = false;
1889 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001890 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001891 * the exposed region consists of two components:
1892 * 1) what's VISIBLE now and was COVERED before
1893 * 2) what's EXPOSED now less what was EXPOSED before
1894 *
1895 * note that (1) is conservative, we start with the whole
1896 * visible region but only keep what used to be covered by
1897 * something -- which mean it may have been exposed.
1898 *
1899 * (2) handles areas that were not covered by anything but got
1900 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001901 */
Mathias Agopianab028732010-03-16 16:41:46 -07001902 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001903 const Region oldVisibleRegion = layer->visibleRegion;
1904 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001905 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1906 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001907 }
1908 dirty.subtractSelf(aboveOpaqueLayers);
1909
1910 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001911 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001912
Mathias Agopianab028732010-03-16 16:41:46 -07001913 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001914 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001915
Jesse Halla8026d22012-09-25 13:25:04 -07001916 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001917 layer->setVisibleRegion(visibleRegion);
1918 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001919 layer->setVisibleNonTransparentRegion(
1920 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001921 }
1922
Mathias Agopian87baae12012-07-31 12:38:26 -07001923 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001924}
1925
Mathias Agopian87baae12012-07-31 12:38:26 -07001926void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1927 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001928 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001929 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1930 if (hw->getLayerStack() == layerStack) {
1931 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001932 }
1933 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001934}
1935
Dan Stoza6b9454d2014-11-07 16:00:59 -08001936bool SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001937{
Dan Stoza9e56aa02015-11-02 13:00:03 -08001938 ALOGV("handlePageFlip");
1939
Mathias Agopian4fec8732012-06-29 14:12:52 -07001940 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001941
Mathias Agopian4fec8732012-06-29 14:12:52 -07001942 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001943 const LayerVector& layers(mDrawingState.layersSortedByZ);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001944 bool frameQueued = false;
Eric Penner51c59cd2014-07-28 19:51:58 -07001945
1946 // Store the set of layers that need updates. This set must not change as
1947 // buffers are being latched, as this could result in a deadlock.
1948 // Example: Two producers share the same command stream and:
1949 // 1.) Layer 0 is latched
1950 // 2.) Layer 0 gets a new frame
1951 // 2.) Layer 1 gets a new frame
1952 // 3.) Layer 1 is latched.
1953 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1954 // second frame. But layer 0's second frame could be waiting on display.
Eric Penner51c59cd2014-07-28 19:51:58 -07001955 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001956 const sp<Layer>& layer(layers[i]);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001957 if (layer->hasQueuedFrame()) {
1958 frameQueued = true;
1959 if (layer->shouldPresentNow(mPrimaryDispSync)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001960 mLayersWithQueuedFrames.push_back(layer.get());
Dan Stozaee44edd2015-03-23 15:50:23 -07001961 } else {
1962 layer->useEmptyDamage();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001963 }
Dan Stozaee44edd2015-03-23 15:50:23 -07001964 } else {
1965 layer->useEmptyDamage();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001966 }
Eric Penner51c59cd2014-07-28 19:51:58 -07001967 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001968 for (auto& layer : mLayersWithQueuedFrames) {
Mathias Agopian87baae12012-07-31 12:38:26 -07001969 const Region dirty(layer->latchBuffer(visibleRegions));
Dan Stozaee44edd2015-03-23 15:50:23 -07001970 layer->useSurfaceDamage();
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001971 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001972 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001973 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001974
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001975 mVisibleRegionsDirty |= visibleRegions;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001976
1977 // If we will need to wake up at some time in the future to deal with a
1978 // queued frame that shouldn't be displayed during this vsync period, wake
1979 // up during the next vsync period to check again.
Dan Stoza9e56aa02015-11-02 13:00:03 -08001980 if (frameQueued && mLayersWithQueuedFrames.empty()) {
Dan Stoza6b9454d2014-11-07 16:00:59 -08001981 signalLayerUpdate();
1982 }
1983
1984 // Only continue with the refresh if there is actually new work to do
Dan Stoza9e56aa02015-11-02 13:00:03 -08001985 return !mLayersWithQueuedFrames.empty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001986}
1987
Mathias Agopianad456f92011-01-13 17:53:01 -08001988void SurfaceFlinger::invalidateHwcGeometry()
1989{
Dan Stoza9e56aa02015-11-02 13:00:03 -08001990 mGeometryInvalid = true;
Mathias Agopianad456f92011-01-13 17:53:01 -08001991}
1992
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001993
Mathias Agopiancd60f992012-08-16 16:28:27 -07001994void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001995 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001996{
Dan Stoza71433162014-02-04 16:22:36 -08001997 // We only need to actually compose the display if:
1998 // 1) It is being handled by hardware composer, which may need this to
1999 // keep its virtual display state machine in sync, or
2000 // 2) There is work to be done (the dirty region isn't empty)
2001 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
2002 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08002003 ALOGV("Skipping display composition");
Dan Stoza71433162014-02-04 16:22:36 -08002004 return;
2005 }
2006
Dan Stoza9e56aa02015-11-02 13:00:03 -08002007 ALOGV("doDisplayComposition");
2008
Mathias Agopian87baae12012-07-31 12:38:26 -07002009 Region dirtyRegion(inDirtyRegion);
2010
Mathias Agopianb8a55602009-06-26 19:06:36 -07002011 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07002012 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002013
Mathias Agopian42977342012-08-05 00:40:46 -07002014 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002015 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002016 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
2017 // takes a rectangle, we must make sure to update that whole
2018 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07002019 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002020 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002021 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002022 // We need to redraw the rectangle that will be updated
2023 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07002024 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002025 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07002026 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002027 } else {
2028 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07002029 dirtyRegion.set(hw->bounds());
2030 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002031 }
2032 }
2033
Dan Stoza9f26a9c2016-06-22 14:51:09 -07002034 if (!doComposeSurfaces(hw, dirtyRegion)) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002035
Mathias Agopian9c6e2972011-09-20 17:21:56 -07002036 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07002037 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07002038
2039 // swap buffers (presentation)
2040 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002041}
2042
Dan Stoza9e56aa02015-11-02 13:00:03 -08002043bool SurfaceFlinger::doComposeSurfaces(
2044 const sp<const DisplayDevice>& displayDevice, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07002045{
Dan Stoza9e56aa02015-11-02 13:00:03 -08002046 ALOGV("doComposeSurfaces");
Mathias Agopiancd20eb02011-09-22 20:57:04 -07002047
Dan Stoza9e56aa02015-11-02 13:00:03 -08002048 const auto hwcId = displayDevice->getHwcDisplayId();
Dan Stoza9f26a9c2016-06-22 14:51:09 -07002049
2050 mat4 oldColorMatrix;
2051 const bool applyColorMatrix = !mHwc->hasDeviceComposition(hwcId) &&
2052 !mHwc->hasCapability(HWC2::Capability::SkipClientColorTransform);
2053 if (applyColorMatrix) {
2054 mat4 colorMatrix = mColorMatrix * mDaltonizer();
2055 oldColorMatrix = getRenderEngine().setupColorTransform(colorMatrix);
2056 }
2057
Dan Stoza9e56aa02015-11-02 13:00:03 -08002058 bool hasClientComposition = mHwc->hasClientComposition(hwcId);
2059 if (hasClientComposition) {
2060 ALOGV("hasClientComposition");
2061
2062 if (!displayDevice->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08002063 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
Dan Stoza9e56aa02015-11-02 13:00:03 -08002064 displayDevice->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07002065 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
2066 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
2067 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
2068 }
2069 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08002070 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07002071
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07002072 // Never touch the framebuffer if we don't have any framebuffer layers
Dan Stoza9e56aa02015-11-02 13:00:03 -08002073 const bool hasDeviceComposition = mHwc->hasDeviceComposition(hwcId);
2074 if (hasDeviceComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07002075 // when using overlays, we assume a fully transparent framebuffer
2076 // NOTE: we could reduce how much we need to clear, for instance
2077 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07002078 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07002079 // We'll revisit later if needed.
Dan Stoza9e56aa02015-11-02 13:00:03 -08002080 mRenderEngine->clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002081 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07002082 // we start with the whole screen area
Dan Stoza9e56aa02015-11-02 13:00:03 -08002083 const Region bounds(displayDevice->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07002084
2085 // we remove the scissor part
2086 // we're left with the letterbox region
2087 // (common case is that letterbox ends-up being empty)
Dan Stoza9e56aa02015-11-02 13:00:03 -08002088 const Region letterbox(bounds.subtract(displayDevice->getScissor()));
Mathias Agopian766dc492012-10-30 18:08:06 -07002089
2090 // compute the area to clear
Dan Stoza9e56aa02015-11-02 13:00:03 -08002091 Region region(displayDevice->undefinedRegion.merge(letterbox));
Mathias Agopian766dc492012-10-30 18:08:06 -07002092
2093 // but limit it to the dirty region
2094 region.andSelf(dirty);
2095
Mathias Agopianb9494d52012-04-18 02:28:45 -07002096 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07002097 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07002098 // can happen with SurfaceView
Dan Stoza9e56aa02015-11-02 13:00:03 -08002099 drawWormhole(displayDevice, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002100 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002101 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002102
Dan Stoza9e56aa02015-11-02 13:00:03 -08002103 if (displayDevice->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopian766dc492012-10-30 18:08:06 -07002104 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07002105 // scissor on the main display. It should never be needed
2106 // anyways (though in theory it could since the API allows it).
Dan Stoza9e56aa02015-11-02 13:00:03 -08002107 const Rect& bounds(displayDevice->getBounds());
2108 const Rect& scissor(displayDevice->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002109 if (scissor != bounds) {
2110 // scissor doesn't match the screen's dimensions, so we
2111 // need to clear everything outside of it and enable
2112 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07002113
Mathias Agopianf45c5102012-10-24 16:29:17 -07002114 // enable scissor for this frame
Dan Stoza9e56aa02015-11-02 13:00:03 -08002115 const uint32_t height = displayDevice->getHeight();
2116 mRenderEngine->setScissor(scissor.left, height - scissor.bottom,
Mathias Agopian3f844832013-08-07 21:24:32 -07002117 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002118 }
2119 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002120 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002121
Mathias Agopian85d751c2012-08-29 16:59:24 -07002122 /*
2123 * and then, render the layers targeted at the framebuffer
2124 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002125
Dan Stoza9e56aa02015-11-02 13:00:03 -08002126 ALOGV("Rendering client layers");
2127 const Transform& displayTransform = displayDevice->getTransform();
2128 if (hwcId >= 0) {
Mathias Agopian85d751c2012-08-29 16:59:24 -07002129 // we're using h/w composer
Dan Stoza9e56aa02015-11-02 13:00:03 -08002130 bool firstLayer = true;
2131 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
2132 const Region clip(dirty.intersect(
2133 displayTransform.transform(layer->visibleRegion)));
2134 ALOGV("Layer: %s", layer->getName().string());
2135 ALOGV(" Composition type: %s",
2136 to_string(layer->getCompositionType(hwcId)).c_str());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002137 if (!clip.isEmpty()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08002138 switch (layer->getCompositionType(hwcId)) {
2139 case HWC2::Composition::Cursor:
2140 case HWC2::Composition::Device:
2141 case HWC2::Composition::SolidColor: {
Mathias Agopianac683022013-10-01 15:36:52 -07002142 const Layer::State& state(layer->getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -08002143 if (layer->getClearClientTarget(hwcId) && !firstLayer &&
2144 layer->isOpaque(state) && (state.alpha == 1.0f)
2145 && hasClientComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07002146 // never clear the very first layer since we're
2147 // guaranteed the FB is already cleared
Dan Stoza9e56aa02015-11-02 13:00:03 -08002148 layer->clearWithOpenGL(displayDevice, clip);
Mathias Agopiancd60f992012-08-16 16:28:27 -07002149 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002150 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07002151 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08002152 case HWC2::Composition::Client: {
2153 layer->draw(displayDevice, clip);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002154 break;
2155 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08002156 default:
Mathias Agopianda27af92012-09-13 18:17:13 -07002157 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07002158 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08002159 } else {
2160 ALOGV(" Skipping for empty clip");
Mathias Agopian85d751c2012-08-29 16:59:24 -07002161 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08002162 firstLayer = false;
Mathias Agopian85d751c2012-08-29 16:59:24 -07002163 }
2164 } else {
2165 // we're not using h/w composer
Dan Stoza9e56aa02015-11-02 13:00:03 -08002166 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
Mathias Agopian85d751c2012-08-29 16:59:24 -07002167 const Region clip(dirty.intersect(
Dan Stoza9e56aa02015-11-02 13:00:03 -08002168 displayTransform.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07002169 if (!clip.isEmpty()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08002170 layer->draw(displayDevice, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07002171 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002172 }
2173 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002174
Dan Stoza9f26a9c2016-06-22 14:51:09 -07002175 if (applyColorMatrix) {
2176 getRenderEngine().setupColorTransform(oldColorMatrix);
2177 }
2178
Mathias Agopianf45c5102012-10-24 16:29:17 -07002179 // disable scissor at the end of the frame
Dan Stoza9e56aa02015-11-02 13:00:03 -08002180 mRenderEngine->disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07002181 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002182}
2183
Mathias Agopian3f844832013-08-07 21:24:32 -07002184void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07002185 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07002186 RenderEngine& engine(getRenderEngine());
2187 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002188}
2189
Dan Stoza7d89d062015-04-30 13:29:25 -07002190status_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -08002191 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07002192 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08002193 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07002194{
Dan Stoza7d89d062015-04-30 13:29:25 -07002195 // add this layer to the current state list
2196 {
2197 Mutex::Autolock _l(mStateLock);
2198 if (mCurrentState.layersSortedByZ.size() >= MAX_LAYERS) {
2199 return NO_MEMORY;
2200 }
2201 mCurrentState.layersSortedByZ.add(lbc);
2202 mGraphicBufferProducerList.add(IInterface::asBinder(gbc));
2203 }
2204
Mathias Agopian96f08192010-06-02 23:28:45 -07002205 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08002206 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07002207
Dan Stoza7d89d062015-04-30 13:29:25 -07002208 return NO_ERROR;
Mathias Agopian96f08192010-06-02 23:28:45 -07002209}
2210
Mathias Agopian3f844832013-08-07 21:24:32 -07002211status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002212 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08002213 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002214 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07002215 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002216 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07002217 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002218 return NO_ERROR;
2219 }
Mathias Agopian3d579642009-06-04 18:46:21 -07002220 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002221}
2222
Dan Stozac7014012014-02-14 15:03:43 -08002223uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07002224 return android_atomic_release_load(&mTransactionFlags);
2225}
2226
Mathias Agopian3f844832013-08-07 21:24:32 -07002227uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002228 return android_atomic_and(~flags, &mTransactionFlags) & flags;
2229}
2230
Mathias Agopian3f844832013-08-07 21:24:32 -07002231uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002232 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
2233 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002234 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002235 }
2236 return old;
2237}
2238
Mathias Agopian8b33f032012-07-24 20:43:54 -07002239void SurfaceFlinger::setTransactionState(
2240 const Vector<ComposerState>& state,
2241 const Vector<DisplayState>& displays,
2242 uint32_t flags)
2243{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002244 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07002245 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07002246 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002247
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002248 if (flags & eAnimation) {
2249 // For window updates that are part of an animation we must wait for
2250 // previous animation "frames" to be handled.
2251 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002252 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002253 if (CC_UNLIKELY(err != NO_ERROR)) {
2254 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002255 // caller after a few seconds.
2256 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
2257 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002258 mAnimTransactionPending = false;
2259 break;
2260 }
2261 }
2262 }
2263
Mathias Agopiane57f2922012-08-09 16:29:12 -07002264 size_t count = displays.size();
2265 for (size_t i=0 ; i<count ; i++) {
2266 const DisplayState& s(displays[i]);
2267 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002268 }
2269
Mathias Agopiane57f2922012-08-09 16:29:12 -07002270 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002271 for (size_t i=0 ; i<count ; i++) {
2272 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002273 // Here we need to check that the interface we're given is indeed
2274 // one of our own. A malicious client could give us a NULL
2275 // IInterface, or one of its own or even one of our own but a
2276 // different type. All these situations would cause us to crash.
2277 //
2278 // NOTE: it would be better to use RTTI as we could directly check
2279 // that we have a Client*. however, RTTI is disabled in Android.
2280 if (s.client != NULL) {
Marco Nelissen097ca272014-11-14 08:01:01 -08002281 sp<IBinder> binder = IInterface::asBinder(s.client);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002282 if (binder != NULL) {
2283 String16 desc(binder->getInterfaceDescriptor());
2284 if (desc == ISurfaceComposerClient::descriptor) {
2285 sp<Client> client( static_cast<Client *>(s.client.get()) );
2286 transactionFlags |= setClientStateLocked(client, s.state);
2287 }
2288 }
2289 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002290 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002291
Robert Carr2a7dbb42016-05-24 11:41:28 -07002292 // If a synchronous transaction is explicitly requested without any changes,
2293 // force a transaction anyway. This can be used as a flush mechanism for
2294 // previous async transactions.
2295 if (transactionFlags == 0 && (flags & eSynchronous)) {
2296 transactionFlags = eTransactionNeeded;
2297 }
2298
Mathias Agopian386aa982011-11-07 21:58:03 -08002299 if (transactionFlags) {
2300 // this triggers the transaction
2301 setTransactionFlags(transactionFlags);
2302
2303 // if this is a synchronous transaction, wait for it to take effect
2304 // before returning.
2305 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002306 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002307 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002308 if (flags & eAnimation) {
2309 mAnimTransactionPending = true;
2310 }
2311 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002312 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2313 if (CC_UNLIKELY(err != NO_ERROR)) {
2314 // just in case something goes wrong in SF, return to the
2315 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002316 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2317 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002318 break;
2319 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002320 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002321 }
2322}
2323
Mathias Agopiane57f2922012-08-09 16:29:12 -07002324uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2325{
Jesse Hall9a143922012-10-04 16:29:19 -07002326 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2327 if (dpyIdx < 0)
2328 return 0;
2329
Mathias Agopiane57f2922012-08-09 16:29:12 -07002330 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002331 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002332 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002333 const uint32_t what = s.what;
2334 if (what & DisplayState::eSurfaceChanged) {
Marco Nelissen097ca272014-11-14 08:01:01 -08002335 if (IInterface::asBinder(disp.surface) != IInterface::asBinder(s.surface)) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002336 disp.surface = s.surface;
2337 flags |= eDisplayTransactionNeeded;
2338 }
2339 }
2340 if (what & DisplayState::eLayerStackChanged) {
2341 if (disp.layerStack != s.layerStack) {
2342 disp.layerStack = s.layerStack;
2343 flags |= eDisplayTransactionNeeded;
2344 }
2345 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002346 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002347 if (disp.orientation != s.orientation) {
2348 disp.orientation = s.orientation;
2349 flags |= eDisplayTransactionNeeded;
2350 }
2351 if (disp.frame != s.frame) {
2352 disp.frame = s.frame;
2353 flags |= eDisplayTransactionNeeded;
2354 }
2355 if (disp.viewport != s.viewport) {
2356 disp.viewport = s.viewport;
2357 flags |= eDisplayTransactionNeeded;
2358 }
2359 }
Michael Lentine47e45402014-07-18 15:34:25 -07002360 if (what & DisplayState::eDisplaySizeChanged) {
2361 if (disp.width != s.width) {
2362 disp.width = s.width;
2363 flags |= eDisplayTransactionNeeded;
2364 }
2365 if (disp.height != s.height) {
2366 disp.height = s.height;
2367 flags |= eDisplayTransactionNeeded;
2368 }
2369 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002370 }
2371 return flags;
2372}
2373
2374uint32_t SurfaceFlinger::setClientStateLocked(
2375 const sp<Client>& client,
2376 const layer_state_t& s)
2377{
2378 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002379 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002380 if (layer != 0) {
2381 const uint32_t what = s.what;
Robert Carr99e27f02016-06-16 15:18:02 -07002382 bool geometryAppliesWithResize =
2383 what & layer_state_t::eGeometryAppliesWithResize;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002384 if (what & layer_state_t::ePositionChanged) {
Robert Carr99e27f02016-06-16 15:18:02 -07002385 if (layer->setPosition(s.x, s.y, !geometryAppliesWithResize)) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002386 flags |= eTraversalNeeded;
Robert Carr82364e32016-05-15 11:27:47 -07002387 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002388 }
2389 if (what & layer_state_t::eLayerChanged) {
2390 // NOTE: index needs to be calculated before we update the state
2391 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
Pablo Ceballos47db6eb2015-12-08 13:53:59 -08002392 if (layer->setLayer(s.z) && idx >= 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002393 mCurrentState.layersSortedByZ.removeAt(idx);
2394 mCurrentState.layersSortedByZ.add(layer);
2395 // we need traversal (state changed)
2396 // AND transaction (list changed)
2397 flags |= eTransactionNeeded|eTraversalNeeded;
2398 }
2399 }
2400 if (what & layer_state_t::eSizeChanged) {
2401 if (layer->setSize(s.w, s.h)) {
2402 flags |= eTraversalNeeded;
2403 }
2404 }
2405 if (what & layer_state_t::eAlphaChanged) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08002406 if (layer->setAlpha(s.alpha))
Mathias Agopiane57f2922012-08-09 16:29:12 -07002407 flags |= eTraversalNeeded;
2408 }
2409 if (what & layer_state_t::eMatrixChanged) {
2410 if (layer->setMatrix(s.matrix))
2411 flags |= eTraversalNeeded;
2412 }
2413 if (what & layer_state_t::eTransparentRegionChanged) {
2414 if (layer->setTransparentRegionHint(s.transparentRegion))
2415 flags |= eTraversalNeeded;
2416 }
Dan Stoza23116082015-06-18 14:58:39 -07002417 if (what & layer_state_t::eFlagsChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002418 if (layer->setFlags(s.flags, s.mask))
2419 flags |= eTraversalNeeded;
2420 }
2421 if (what & layer_state_t::eCropChanged) {
Robert Carr99e27f02016-06-16 15:18:02 -07002422 if (layer->setCrop(s.crop, !geometryAppliesWithResize))
Mathias Agopiane57f2922012-08-09 16:29:12 -07002423 flags |= eTraversalNeeded;
2424 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002425 if (what & layer_state_t::eFinalCropChanged) {
2426 if (layer->setFinalCrop(s.finalCrop))
2427 flags |= eTraversalNeeded;
2428 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002429 if (what & layer_state_t::eLayerStackChanged) {
2430 // NOTE: index needs to be calculated before we update the state
2431 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
Pablo Ceballos47db6eb2015-12-08 13:53:59 -08002432 if (layer->setLayerStack(s.layerStack) && idx >= 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002433 mCurrentState.layersSortedByZ.removeAt(idx);
2434 mCurrentState.layersSortedByZ.add(layer);
2435 // we need traversal (state changed)
2436 // AND transaction (list changed)
2437 flags |= eTransactionNeeded|eTraversalNeeded;
2438 }
2439 }
Dan Stoza7dde5992015-05-22 09:51:44 -07002440 if (what & layer_state_t::eDeferTransaction) {
2441 layer->deferTransactionUntil(s.handle, s.frameNumber);
2442 // We don't trigger a traversal here because if no other state is
2443 // changed, we don't want this to cause any more work
2444 }
Robert Carrc3574f72016-03-24 12:19:32 -07002445 if (what & layer_state_t::eOverrideScalingModeChanged) {
2446 layer->setOverrideScalingMode(s.overrideScalingMode);
2447 // We don't trigger a traversal here because if no other state is
2448 // changed, we don't want this to cause any more work
2449 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002450 }
2451 return flags;
2452}
2453
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002454status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002455 const String8& name,
2456 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002457 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2458 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002459{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002460 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002461 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002462 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002463 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002464 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002465 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002466
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002467 status_t result = NO_ERROR;
2468
2469 sp<Layer> layer;
2470
Mathias Agopian3165cc22012-08-08 19:42:09 -07002471 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2472 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002473 result = createNormalLayer(client,
2474 name, w, h, flags, format,
2475 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002476 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002477 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002478 result = createDimLayer(client,
2479 name, w, h, flags,
2480 handle, gbp, &layer);
2481 break;
2482 default:
2483 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002484 break;
2485 }
2486
Dan Stoza7d89d062015-04-30 13:29:25 -07002487 if (result != NO_ERROR) {
2488 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002489 }
Dan Stoza7d89d062015-04-30 13:29:25 -07002490
2491 result = addClientLayer(client, *handle, *gbp, layer);
2492 if (result != NO_ERROR) {
2493 return result;
2494 }
2495
2496 setTransactionFlags(eTransactionNeeded);
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002497 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002498}
2499
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002500status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2501 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2502 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002503{
2504 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002505 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002506 case PIXEL_FORMAT_TRANSPARENT:
2507 case PIXEL_FORMAT_TRANSLUCENT:
2508 format = PIXEL_FORMAT_RGBA_8888;
2509 break;
2510 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002511 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002512 break;
2513 }
2514
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002515 *outLayer = new Layer(this, client, name, w, h, flags);
2516 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2517 if (err == NO_ERROR) {
2518 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002519 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002520 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002521
2522 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2523 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002524}
2525
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002526status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2527 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2528 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002529{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002530 *outLayer = new LayerDim(this, client, name, w, h, flags);
2531 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002532 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002533 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002534}
2535
Mathias Agopianac9fa422013-02-11 16:40:36 -08002536status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002537{
Mathias Agopian67106042013-03-14 19:18:13 -07002538 // called by the window manager when it wants to remove a Layer
2539 status_t err = NO_ERROR;
2540 sp<Layer> l(client->getLayerUser(handle));
2541 if (l != NULL) {
2542 err = removeLayer(l);
2543 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2544 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002545 }
2546 return err;
2547}
2548
Mathias Agopian13127d82013-03-05 17:47:11 -08002549status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002550{
Mathias Agopian67106042013-03-14 19:18:13 -07002551 // called by ~LayerCleaner() when all references to the IBinder (handle)
2552 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002553 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002554 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002555 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002556 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002557 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002558 "error removing layer=%p (%s)", l.get(), strerror(-err));
2559 }
2560 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002561}
2562
Mathias Agopianb60314a2012-04-10 22:09:54 -07002563// ---------------------------------------------------------------------------
2564
Andy McFadden13a082e2012-08-24 10:16:42 -07002565void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002566 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002567 Vector<ComposerState> state;
2568 Vector<DisplayState> displays;
2569 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002570 d.what = DisplayState::eDisplayProjectionChanged |
2571 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002572 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002573 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002574 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002575 d.frame.makeInvalid();
2576 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002577 d.width = 0;
2578 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002579 displays.add(d);
2580 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002581 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002582
Dan Stoza9e56aa02015-11-02 13:00:03 -08002583 const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
2584 const nsecs_t period = activeConfig->getVsyncPeriod();
Jamie Gennis6547ff42013-07-16 20:12:42 -07002585 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002586}
2587
2588void SurfaceFlinger::initializeDisplays() {
2589 class MessageScreenInitialized : public MessageBase {
2590 SurfaceFlinger* flinger;
2591 public:
2592 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2593 virtual bool handler() {
2594 flinger->onInitializeDisplays();
2595 return true;
2596 }
2597 };
2598 sp<MessageBase> msg = new MessageScreenInitialized(this);
2599 postMessageAsync(msg); // we may be called from main thread, use async message
2600}
2601
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002602void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2603 int mode) {
2604 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2605 this);
2606 int32_t type = hw->getDisplayType();
2607 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002608
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002609 if (mode == currentMode) {
2610 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002611 return;
2612 }
2613
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002614 hw->setPowerMode(mode);
2615 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2616 ALOGW("Trying to set power mode for virtual display");
2617 return;
2618 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002619
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002620 if (currentMode == HWC_POWER_MODE_OFF) {
2621 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002622 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2623 // FIXME: eventthread only knows about the main display right now
2624 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002625 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002626 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002627
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002628 mVisibleRegionsDirty = true;
Dan Stozab90cf072015-03-05 11:05:59 -08002629 mHasPoweredOff = true;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002630 repaintEverything();
2631 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002632 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002633 disableHardwareVsync(true); // also cancels any in-progress resync
2634
Mathias Agopiancde87a32012-09-13 14:09:01 -07002635 // FIXME: eventthread only knows about the main display right now
2636 mEventThread->onScreenReleased();
2637 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002638
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002639 getHwComposer().setPowerMode(type, mode);
2640 mVisibleRegionsDirty = true;
2641 // from this point on, SF will stop drawing on this display
2642 } else {
2643 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002644 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002645}
2646
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002647void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2648 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002649 SurfaceFlinger& mFlinger;
2650 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002651 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002652 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002653 MessageSetPowerMode(SurfaceFlinger& flinger,
2654 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2655 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002656 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002657 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002658 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002659 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002660 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002661 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002662 ALOGW("Attempt to set power mode = %d for virtual display",
2663 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002664 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002665 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002666 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002667 return true;
2668 }
2669 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002670 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002671 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002672}
2673
2674// ---------------------------------------------------------------------------
2675
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002676status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2677{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002678 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002679
Mathias Agopianbd115332013-04-18 16:41:04 -07002680 IPCThreadState* ipc = IPCThreadState::self();
2681 const int pid = ipc->getCallingPid();
2682 const int uid = ipc->getCallingUid();
2683 if ((uid != AID_SHELL) &&
2684 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002685 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002686 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002687 } else {
Jesse Hallfcd15b42014-12-23 13:57:23 -08002688 // Try to get the main lock, but give up after one second
Mathias Agopian9795c422009-08-26 16:36:26 -07002689 // (this would indicate SF is stuck, but we want to be able to
2690 // print something in dumpsys).
Jesse Hallfcd15b42014-12-23 13:57:23 -08002691 status_t err = mStateLock.timedLock(s2ns(1));
2692 bool locked = (err == NO_ERROR);
Mathias Agopian9795c422009-08-26 16:36:26 -07002693 if (!locked) {
Jesse Hallfcd15b42014-12-23 13:57:23 -08002694 result.appendFormat(
2695 "SurfaceFlinger appears to be unresponsive (%s [%d]), "
2696 "dumping anyways (no locks held)\n", strerror(-err), err);
Mathias Agopian9795c422009-08-26 16:36:26 -07002697 }
2698
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002699 bool dumpAll = true;
2700 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002701 size_t numArgs = args.size();
2702 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002703 if ((index < numArgs) &&
2704 (args[index] == String16("--list"))) {
2705 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002706 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002707 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002708 }
2709
2710 if ((index < numArgs) &&
2711 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002712 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002713 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002714 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002715 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002716
2717 if ((index < numArgs) &&
2718 (args[index] == String16("--latency-clear"))) {
2719 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002720 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002721 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002722 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002723
2724 if ((index < numArgs) &&
2725 (args[index] == String16("--dispsync"))) {
2726 index++;
2727 mPrimaryDispSync.dump(result);
2728 dumpAll = false;
2729 }
Dan Stozab90cf072015-03-05 11:05:59 -08002730
2731 if ((index < numArgs) &&
2732 (args[index] == String16("--static-screen"))) {
2733 index++;
2734 dumpStaticScreenStats(result);
2735 dumpAll = false;
2736 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08002737
2738 if ((index < numArgs) &&
2739 (args[index] == String16("--fences"))) {
2740 index++;
2741 mFenceTracker.dump(&result);
2742 dumpAll = false;
2743 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002744 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002745
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002746 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002747 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002748 }
2749
Mathias Agopian9795c422009-08-26 16:36:26 -07002750 if (locked) {
2751 mStateLock.unlock();
2752 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002753 }
2754 write(fd, result.string(), result.size());
2755 return NO_ERROR;
2756}
2757
Dan Stozac7014012014-02-14 15:03:43 -08002758void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2759 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002760{
2761 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2762 const size_t count = currentLayers.size();
2763 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002764 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002765 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002766 }
2767}
2768
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002769void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002770 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002771{
2772 String8 name;
2773 if (index < args.size()) {
2774 name = String8(args[index]);
2775 index++;
2776 }
2777
Dan Stoza9e56aa02015-11-02 13:00:03 -08002778 const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
2779 const nsecs_t period = activeConfig->getVsyncPeriod();
Greg Hackmann86efcc02014-03-07 12:44:02 -08002780 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002781
2782 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002783 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002784 } else {
2785 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2786 const size_t count = currentLayers.size();
2787 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002788 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002789 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002790 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002791 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002792 }
2793 }
2794}
2795
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002796void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002797 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002798{
2799 String8 name;
2800 if (index < args.size()) {
2801 name = String8(args[index]);
2802 index++;
2803 }
2804
2805 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2806 const size_t count = currentLayers.size();
2807 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002808 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002809 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002810 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002811 }
2812 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002813
Svetoslavd85084b2014-03-20 10:28:31 -07002814 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002815}
2816
Jamie Gennis6547ff42013-07-16 20:12:42 -07002817// This should only be called from the main thread. Otherwise it would need
2818// the lock and should use mCurrentState rather than mDrawingState.
2819void SurfaceFlinger::logFrameStats() {
2820 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2821 const size_t count = drawingLayers.size();
2822 for (size_t i=0 ; i<count ; i++) {
2823 const sp<Layer>& layer(drawingLayers[i]);
2824 layer->logFrameStats();
2825 }
2826
2827 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2828}
2829
Andy McFadden4803b742012-09-24 19:07:20 -07002830/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2831{
2832 static const char* config =
2833 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002834#ifdef HAS_CONTEXT_PRIORITY
2835 " HAS_CONTEXT_PRIORITY"
2836#endif
2837#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2838 " NEVER_DEFAULT_TO_ASYNC_MODE"
2839#endif
2840#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2841 " TARGET_DISABLE_TRIPLE_BUFFERING"
2842#endif
2843 "]";
2844 result.append(config);
2845}
2846
Dan Stozab90cf072015-03-05 11:05:59 -08002847void SurfaceFlinger::dumpStaticScreenStats(String8& result) const
2848{
2849 result.appendFormat("Static screen stats:\n");
2850 for (size_t b = 0; b < NUM_BUCKETS - 1; ++b) {
2851 float bucketTimeSec = mFrameBuckets[b] / 1e9;
2852 float percent = 100.0f *
2853 static_cast<float>(mFrameBuckets[b]) / mTotalTime;
2854 result.appendFormat(" < %zd frames: %.3f s (%.1f%%)\n",
2855 b + 1, bucketTimeSec, percent);
2856 }
2857 float bucketTimeSec = mFrameBuckets[NUM_BUCKETS - 1] / 1e9;
2858 float percent = 100.0f *
2859 static_cast<float>(mFrameBuckets[NUM_BUCKETS - 1]) / mTotalTime;
2860 result.appendFormat(" %zd+ frames: %.3f s (%.1f%%)\n",
2861 NUM_BUCKETS - 1, bucketTimeSec, percent);
2862}
2863
Dan Stozae77c7662016-05-13 11:37:28 -07002864void SurfaceFlinger::recordBufferingStats(const char* layerName,
2865 std::vector<OccupancyTracker::Segment>&& history) {
2866 Mutex::Autolock lock(mBufferingStatsMutex);
2867 auto& stats = mBufferingStats[layerName];
2868 for (const auto& segment : history) {
2869 if (!segment.usedThirdBuffer) {
2870 stats.twoBufferTime += segment.totalTime;
2871 }
2872 if (segment.occupancyAverage < 1.0f) {
2873 stats.doubleBufferedTime += segment.totalTime;
2874 } else if (segment.occupancyAverage < 2.0f) {
2875 stats.tripleBufferedTime += segment.totalTime;
2876 }
2877 ++stats.numSegments;
2878 stats.totalTime += segment.totalTime;
2879 }
2880}
2881
2882void SurfaceFlinger::dumpBufferingStats(String8& result) const {
2883 result.append("Buffering stats:\n");
2884 result.append(" [Layer name] <Active time> <Two buffer> "
2885 "<Double buffered> <Triple buffered>\n");
2886 Mutex::Autolock lock(mBufferingStatsMutex);
2887 typedef std::tuple<std::string, float, float, float> BufferTuple;
2888 std::map<float, BufferTuple, std::greater<float>> sorted;
2889 for (const auto& statsPair : mBufferingStats) {
2890 const char* name = statsPair.first.c_str();
2891 const BufferingStats& stats = statsPair.second;
2892 if (stats.numSegments == 0) {
2893 continue;
2894 }
2895 float activeTime = ns2ms(stats.totalTime) / 1000.0f;
2896 float twoBufferRatio = static_cast<float>(stats.twoBufferTime) /
2897 stats.totalTime;
2898 float doubleBufferRatio = static_cast<float>(
2899 stats.doubleBufferedTime) / stats.totalTime;
2900 float tripleBufferRatio = static_cast<float>(
2901 stats.tripleBufferedTime) / stats.totalTime;
2902 sorted.insert({activeTime, {name, twoBufferRatio,
2903 doubleBufferRatio, tripleBufferRatio}});
2904 }
2905 for (const auto& sortedPair : sorted) {
2906 float activeTime = sortedPair.first;
2907 const BufferTuple& values = sortedPair.second;
2908 result.appendFormat(" [%s] %.2f %.3f %.3f %.3f\n",
2909 std::get<0>(values).c_str(), activeTime,
2910 std::get<1>(values), std::get<2>(values),
2911 std::get<3>(values));
2912 }
2913 result.append("\n");
2914}
2915
2916
Mathias Agopian74d211a2013-04-22 16:55:35 +02002917void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2918 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002919{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002920 bool colorize = false;
2921 if (index < args.size()
2922 && (args[index] == String16("--color"))) {
2923 colorize = true;
2924 index++;
2925 }
2926
2927 Colorizer colorizer(colorize);
2928
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002929 // figure out if we're stuck somewhere
2930 const nsecs_t now = systemTime();
2931 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2932 const nsecs_t inTransaction(mDebugInTransaction);
2933 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2934 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2935
2936 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002937 * Dump library configuration.
2938 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002939
2940 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002941 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002942 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002943 appendSfConfigString(result);
2944 appendUiConfigString(result);
2945 appendGuiConfigString(result);
2946 result.append("\n");
2947
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002948 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002949 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002950 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002951 result.append(SyncFeatures::getInstance().toString());
2952 result.append("\n");
2953
Dan Stoza9e56aa02015-11-02 13:00:03 -08002954 const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
2955
Andy McFadden41d67d72014-04-25 16:58:34 -07002956 colorizer.bold(result);
2957 result.append("DispSync configuration: ");
2958 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002959 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2960 "present offset %d ns (refresh %" PRId64 " ns)",
Dan Stoza9e56aa02015-11-02 13:00:03 -08002961 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs,
2962 PRESENT_TIME_OFFSET_FROM_VSYNC_NS, activeConfig->getVsyncPeriod());
Andy McFadden41d67d72014-04-25 16:58:34 -07002963 result.append("\n");
2964
Dan Stozab90cf072015-03-05 11:05:59 -08002965 // Dump static screen stats
2966 result.append("\n");
2967 dumpStaticScreenStats(result);
2968 result.append("\n");
2969
Dan Stozae77c7662016-05-13 11:37:28 -07002970 dumpBufferingStats(result);
2971
Andy McFadden4803b742012-09-24 19:07:20 -07002972 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002973 * Dump the visible layer list
2974 */
2975 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2976 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002977 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002978 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002979 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002980 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002981 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002982 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002983 }
2984
2985 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002986 * Dump Display state
2987 */
2988
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002989 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002990 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002991 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002992 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2993 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002994 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002995 }
2996
2997 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002998 * Dump SurfaceFlinger global state
2999 */
3000
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003001 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02003002 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003003 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003004
Mathias Agopian888c8222012-08-04 21:10:38 -07003005 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07003006 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07003007
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003008 colorizer.bold(result);
3009 result.appendFormat("EGL implementation : %s\n",
3010 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
3011 colorizer.reset(result);
3012 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07003013 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07003014
Mathias Agopian875d8e12013-06-07 15:35:48 -07003015 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003016
Mathias Agopian42977342012-08-05 00:40:46 -07003017 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07003018 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
3019 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02003020 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003021 " last eglSwapBuffers() time: %f us\n"
3022 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08003023 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003024 " refresh-rate : %f fps\n"
3025 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07003026 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07003027 " gpu_to_cpu_unsupported : %d\n"
3028 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003029 mLastSwapBufferTime/1000.0,
3030 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08003031 mTransactionFlags,
Dan Stoza9e56aa02015-11-02 13:00:03 -08003032 1e9 / activeConfig->getVsyncPeriod(),
3033 activeConfig->getDpiX(),
3034 activeConfig->getDpiY(),
Mathias Agopianed985572013-03-22 00:24:39 -07003035 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003036
Mathias Agopian74d211a2013-04-22 16:55:35 +02003037 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003038 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003039
Mathias Agopian74d211a2013-04-22 16:55:35 +02003040 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003041 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003042
3043 /*
3044 * VSYNC state
3045 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02003046 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003047
3048 /*
3049 * Dump HWComposer state
3050 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003051 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02003052 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003053 colorizer.reset(result);
Dan Stoza9f26a9c2016-06-22 14:51:09 -07003054 bool hwcDisabled = mDebugDisableHWC || mDebugRegion;
Dan Stoza9e56aa02015-11-02 13:00:03 -08003055 result.appendFormat(" h/w composer %s\n",
3056 hwcDisabled ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02003057 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003058
3059 /*
3060 * Dump gralloc state
3061 */
3062 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
3063 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003064}
3065
Mathias Agopian13127d82013-03-05 17:47:11 -08003066const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07003067SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07003068 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07003069 wp<IBinder> dpy;
3070 for (size_t i=0 ; i<mDisplays.size() ; i++) {
3071 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
3072 dpy = mDisplays.keyAt(i);
3073 break;
3074 }
3075 }
3076 if (dpy == NULL) {
3077 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
3078 // Just use the primary display so we have something to return
3079 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
3080 }
3081 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07003082}
3083
Keun young Park63f165f2012-08-31 10:53:36 -07003084bool SurfaceFlinger::startDdmConnection()
3085{
3086 void* libddmconnection_dso =
3087 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
3088 if (!libddmconnection_dso) {
3089 return false;
3090 }
3091 void (*DdmConnection_start)(const char* name);
3092 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07003093 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07003094 if (!DdmConnection_start) {
3095 dlclose(libddmconnection_dso);
3096 return false;
3097 }
3098 (*DdmConnection_start)(getServiceName());
3099 return true;
3100}
3101
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003102status_t SurfaceFlinger::onTransact(
3103 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3104{
3105 switch (code) {
3106 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07003107 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07003108 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003109 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07003110 case CLEAR_ANIMATION_FRAME_STATS:
3111 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07003112 case SET_POWER_MODE:
Dan Stozac4f471e2016-03-24 09:31:08 -07003113 case GET_HDR_CAPABILITIES:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003114 {
3115 // codes that require permission check
3116 IPCThreadState* ipc = IPCThreadState::self();
3117 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07003118 const int uid = ipc->getCallingUid();
Jeff Brown3bfe51d2015-04-10 20:20:13 -07003119 if ((uid != AID_GRAPHICS && uid != AID_SYSTEM) &&
Mathias Agopian99b49842011-06-27 16:05:52 -07003120 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00003121 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07003122 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
3123 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003124 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003125 break;
3126 }
3127 case CAPTURE_SCREEN:
3128 {
3129 // codes that require permission check
3130 IPCThreadState* ipc = IPCThreadState::self();
3131 const int pid = ipc->getCallingPid();
3132 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07003133 if ((uid != AID_GRAPHICS) &&
3134 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00003135 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003136 "can't read framebuffer pid=%d, uid=%d", pid, uid);
3137 return PERMISSION_DENIED;
3138 }
3139 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003140 }
3141 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003142
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003143 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
3144 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07003145 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08003146 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07003147 IPCThreadState* ipc = IPCThreadState::self();
3148 const int pid = ipc->getCallingPid();
3149 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00003150 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07003151 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003152 return PERMISSION_DENIED;
3153 }
3154 int n;
3155 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07003156 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07003157 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003158 return NO_ERROR;
3159 case 1002: // SHOW_UPDATES
3160 n = data.readInt32();
3161 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07003162 invalidateHwcGeometry();
3163 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003164 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003165 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07003166 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003167 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003168 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003169 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07003170 setTransactionFlags(
3171 eTransactionNeeded|
3172 eDisplayTransactionNeeded|
3173 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003174 return NO_ERROR;
3175 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08003176 case 1006:{ // send empty update
3177 signalRefresh();
3178 return NO_ERROR;
3179 }
Mathias Agopian53331da2011-08-22 21:44:41 -07003180 case 1008: // toggle use of hw composer
3181 n = data.readInt32();
3182 mDebugDisableHWC = n ? 1 : 0;
3183 invalidateHwcGeometry();
3184 repaintEverything();
3185 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07003186 case 1009: // toggle use of transform hint
3187 n = data.readInt32();
3188 mDebugDisableTransformHint = n ? 1 : 0;
3189 invalidateHwcGeometry();
3190 repaintEverything();
3191 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003192 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07003193 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003194 reply->writeInt32(0);
3195 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07003196 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08003197 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003198 return NO_ERROR;
3199 case 1013: {
3200 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07003201 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
3202 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07003203 return NO_ERROR;
3204 }
3205 case 1014: {
3206 // daltonize
3207 n = data.readInt32();
3208 switch (n % 10) {
Dan Stoza9f26a9c2016-06-22 14:51:09 -07003209 case 1:
3210 mDaltonizer.setType(ColorBlindnessType::Protanomaly);
3211 break;
3212 case 2:
3213 mDaltonizer.setType(ColorBlindnessType::Deuteranomaly);
3214 break;
3215 case 3:
3216 mDaltonizer.setType(ColorBlindnessType::Tritanomaly);
3217 break;
3218 default:
3219 mDaltonizer.setType(ColorBlindnessType::None);
3220 break;
Mathias Agopianff2ed702013-09-01 21:36:12 -07003221 }
3222 if (n >= 10) {
Dan Stoza9f26a9c2016-06-22 14:51:09 -07003223 mDaltonizer.setMode(ColorBlindnessMode::Correction);
Mathias Agopianff2ed702013-09-01 21:36:12 -07003224 } else {
Dan Stoza9f26a9c2016-06-22 14:51:09 -07003225 mDaltonizer.setMode(ColorBlindnessMode::Simulation);
Mathias Agopianff2ed702013-09-01 21:36:12 -07003226 }
Mathias Agopianff2ed702013-09-01 21:36:12 -07003227 invalidateHwcGeometry();
3228 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07003229 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003230 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003231 case 1015: {
3232 // apply a color matrix
3233 n = data.readInt32();
Alan Viverette9c5a3332013-09-12 20:04:35 -07003234 if (n) {
3235 // color matrix is sent as mat3 matrix followed by vec3
3236 // offset, then packed into a mat4 where the last row is
3237 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07003238 for (size_t i = 0 ; i < 4; i++) {
Dan Stoza9f26a9c2016-06-22 14:51:09 -07003239 for (size_t j = 0; j < 4; j++) {
3240 mColorMatrix[i][j] = data.readFloat();
3241 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003242 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003243 } else {
3244 mColorMatrix = mat4();
3245 }
3246 invalidateHwcGeometry();
3247 repaintEverything();
3248 return NO_ERROR;
3249 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003250 // This is an experimental interface
3251 // Needs to be shifted to proper binder interface when we productize
3252 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07003253 n = data.readInt32();
3254 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003255 return NO_ERROR;
3256 }
Dan Stozaee44edd2015-03-23 15:50:23 -07003257 case 1017: {
3258 n = data.readInt32();
3259 mForceFullDamage = static_cast<bool>(n);
3260 return NO_ERROR;
3261 }
Dan Stozadb4ac3c2015-04-14 11:34:01 -07003262 case 1018: { // Modify Choreographer's phase offset
3263 n = data.readInt32();
3264 mEventThread->setPhaseOffset(static_cast<nsecs_t>(n));
3265 return NO_ERROR;
3266 }
3267 case 1019: { // Modify SurfaceFlinger's phase offset
3268 n = data.readInt32();
3269 mSFEventThread->setPhaseOffset(static_cast<nsecs_t>(n));
3270 return NO_ERROR;
3271 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003272 }
3273 }
3274 return err;
3275}
3276
Mathias Agopian53331da2011-08-22 21:44:41 -07003277void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07003278 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08003279 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07003280}
3281
Mathias Agopian59119e62010-10-11 12:37:43 -07003282// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003283// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07003284// ---------------------------------------------------------------------------
3285
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003286/* The code below is here to handle b/8734824
3287 *
3288 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07003289 * from the surfaceflinger thread to the calling binder thread, where they
3290 * are executed. This allows the calling thread in the calling process to be
3291 * reused and not depend on having "enough" binder threads to handle the
3292 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003293 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003294class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07003295 /* Parts of GraphicProducerWrapper are run on two different threads,
3296 * communicating by sending messages via Looper but also by shared member
3297 * data. Coherence maintenance is subtle and in places implicit (ugh).
3298 *
3299 * Don't rely on Looper's sendMessage/handleMessage providing
3300 * release/acquire semantics for any data not actually in the Message.
3301 * Data going from surfaceflinger to binder threads needs to be
3302 * synchronized explicitly.
3303 *
3304 * Barrier open/wait do provide release/acquire semantics. This provides
3305 * implicit synchronization for data coming back from binder to
3306 * surfaceflinger threads.
3307 */
3308
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003309 sp<IGraphicBufferProducer> impl;
3310 sp<Looper> looper;
3311 status_t result;
3312 bool exitPending;
3313 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07003314 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003315 uint32_t code;
3316 Parcel const* data;
3317 Parcel* reply;
3318
3319 enum {
3320 MSG_API_CALL,
3321 MSG_EXIT
3322 };
3323
3324 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003325 * Called on surfaceflinger thread. This is called by our "fake"
3326 * BpGraphicBufferProducer. We package the data and reply Parcel and
3327 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003328 */
3329 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08003330 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003331 this->code = code;
3332 this->data = &data;
3333 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003334 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07003335 // if we've exited, we run the message synchronously right here.
3336 // note (JH): as far as I can tell from looking at the code, this
3337 // never actually happens. if it does, i'm not sure if it happens
3338 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003339 handleMessage(Message(MSG_API_CALL));
3340 } else {
3341 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07003342 // Prevent stores to this->{code, data, reply} from being
3343 // reordered later than the construction of Message.
3344 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003345 looper->sendMessage(this, Message(MSG_API_CALL));
3346 barrier.wait();
3347 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08003348 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003349 }
3350
3351 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003352 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003353 * call the real BpGraphicBufferProducer.
3354 */
3355 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07003356 int what = message.what;
3357 // Prevent reads below from happening before the read from Message
3358 atomic_thread_fence(memory_order_acquire);
3359 if (what == MSG_API_CALL) {
Marco Nelissen097ca272014-11-14 08:01:01 -08003360 result = IInterface::asBinder(impl)->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003361 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07003362 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003363 exitRequested = true;
3364 }
3365 }
3366
3367public:
Jesse Hallb154c422014-07-13 12:47:02 -07003368 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
3369 : impl(impl),
3370 looper(new Looper(true)),
Pablo Ceballos53390e12015-08-04 11:25:59 -07003371 result(NO_ERROR),
Jesse Hallb154c422014-07-13 12:47:02 -07003372 exitPending(false),
Pablo Ceballos53390e12015-08-04 11:25:59 -07003373 exitRequested(false),
3374 code(0),
3375 data(NULL),
3376 reply(NULL)
Jesse Hallb154c422014-07-13 12:47:02 -07003377 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003378
Jesse Hallb154c422014-07-13 12:47:02 -07003379 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003380 status_t waitForResponse() {
3381 do {
3382 looper->pollOnce(-1);
3383 } while (!exitRequested);
3384 return result;
3385 }
3386
Jesse Hallb154c422014-07-13 12:47:02 -07003387 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003388 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07003389 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003390 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07003391 // Ensure this->result is visible to the binder thread before it
3392 // handles the message.
3393 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003394 looper->sendMessage(this, Message(MSG_EXIT));
3395 }
3396};
3397
3398
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003399status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
3400 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003401 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003402 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003403 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003404
3405 if (CC_UNLIKELY(display == 0))
3406 return BAD_VALUE;
3407
3408 if (CC_UNLIKELY(producer == 0))
3409 return BAD_VALUE;
3410
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003411 // if we have secure windows on this display, never allow the screen capture
3412 // unless the producer interface is local (i.e.: we can take a screenshot for
3413 // ourselves).
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003414 bool isLocalScreenshot = IInterface::asBinder(producer)->localBinder();
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003415
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003416 // Convert to surfaceflinger's internal rotation type.
3417 Transform::orientation_flags rotationFlags;
3418 switch (rotation) {
3419 case ISurfaceComposer::eRotateNone:
3420 rotationFlags = Transform::ROT_0;
3421 break;
3422 case ISurfaceComposer::eRotate90:
3423 rotationFlags = Transform::ROT_90;
3424 break;
3425 case ISurfaceComposer::eRotate180:
3426 rotationFlags = Transform::ROT_180;
3427 break;
3428 case ISurfaceComposer::eRotate270:
3429 rotationFlags = Transform::ROT_270;
3430 break;
3431 default:
3432 rotationFlags = Transform::ROT_0;
3433 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3434 break;
3435 }
3436
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003437 class MessageCaptureScreen : public MessageBase {
3438 SurfaceFlinger* flinger;
3439 sp<IBinder> display;
3440 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003441 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003442 uint32_t reqWidth, reqHeight;
3443 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003444 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003445 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003446 status_t result;
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003447 bool isLocalScreenshot;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003448 public:
3449 MessageCaptureScreen(SurfaceFlinger* flinger,
3450 const sp<IBinder>& display,
3451 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003452 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003453 uint32_t minLayerZ, uint32_t maxLayerZ,
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003454 bool useIdentityTransform,
3455 Transform::orientation_flags rotation,
3456 bool isLocalScreenshot)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003457 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003458 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003459 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003460 useIdentityTransform(useIdentityTransform),
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003461 rotation(rotation), result(PERMISSION_DENIED),
3462 isLocalScreenshot(isLocalScreenshot)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003463 {
3464 }
3465 status_t getResult() const {
3466 return result;
3467 }
3468 virtual bool handler() {
3469 Mutex::Autolock _l(flinger->mStateLock);
3470 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003471 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003472 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003473 useIdentityTransform, rotation, isLocalScreenshot);
Marco Nelissen097ca272014-11-14 08:01:01 -08003474 static_cast<GraphicProducerWrapper*>(IInterface::asBinder(producer).get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003475 return true;
3476 }
3477 };
3478
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003479 // this creates a "fake" BBinder which will serve as a "fake" remote
3480 // binder to receive the marshaled calls and forward them to the
3481 // real remote (a BpGraphicBufferProducer)
3482 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3483
3484 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3485 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003486 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003487 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003488 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003489 useIdentityTransform, rotationFlags, isLocalScreenshot);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003490
3491 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003492 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003493 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003494 }
3495 return res;
3496}
3497
Mathias Agopian180f10d2013-04-10 22:55:41 -07003498
3499void SurfaceFlinger::renderScreenImplLocked(
3500 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003501 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003502 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003503 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003504{
3505 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003506 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003507
3508 // get screen geometry
Andreas Gampe89fd4f72014-11-13 14:18:56 -08003509 const int32_t hw_w = hw->getWidth();
3510 const int32_t hw_h = hw->getHeight();
3511 const bool filtering = static_cast<int32_t>(reqWidth) != hw_w ||
Christopher Ferris0e749792015-03-23 14:32:15 -07003512 static_cast<int32_t>(reqHeight) != hw_h;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003513
Dan Stozac1879002014-05-22 15:59:05 -07003514 // if a default or invalid sourceCrop is passed in, set reasonable values
3515 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3516 !sourceCrop.isValid()) {
3517 sourceCrop.setLeftTop(Point(0, 0));
3518 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3519 }
3520
3521 // ensure that sourceCrop is inside screen
3522 if (sourceCrop.left < 0) {
3523 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3524 }
Dan Stozabe31f442014-06-11 11:20:54 -07003525 if (sourceCrop.right > hw_w) {
3526 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003527 }
3528 if (sourceCrop.top < 0) {
3529 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3530 }
Dan Stozabe31f442014-06-11 11:20:54 -07003531 if (sourceCrop.bottom > hw_h) {
3532 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003533 }
3534
Mathias Agopian180f10d2013-04-10 22:55:41 -07003535 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003536 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003537
3538 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003539 engine.setViewportAndProjection(
3540 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003541 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003542
3543 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003544 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003545
3546 const LayerVector& layers( mDrawingState.layersSortedByZ );
3547 const size_t count = layers.size();
3548 for (size_t i=0 ; i<count ; ++i) {
3549 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003550 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003551 if (state.layerStack == hw->getLayerStack()) {
3552 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3553 if (layer->isVisible()) {
3554 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08003555 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003556 if (filtering) layer->setFiltering(false);
3557 }
3558 }
3559 }
3560 }
3561
Mathias Agopian931bda12013-08-28 18:11:46 -07003562 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003563}
3564
3565
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003566status_t SurfaceFlinger::captureScreenImplLocked(
3567 const sp<const DisplayDevice>& hw,
3568 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003569 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003570 uint32_t minLayerZ, uint32_t maxLayerZ,
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003571 bool useIdentityTransform, Transform::orientation_flags rotation,
3572 bool isLocalScreenshot)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003573{
3574 ATRACE_CALL();
3575
Mathias Agopian180f10d2013-04-10 22:55:41 -07003576 // get screen geometry
Dan Stoza35024162015-06-09 16:44:40 -07003577 uint32_t hw_w = hw->getWidth();
3578 uint32_t hw_h = hw->getHeight();
3579
3580 if (rotation & Transform::ROT_90) {
3581 std::swap(hw_w, hw_h);
3582 }
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003583
Mathias Agopian180f10d2013-04-10 22:55:41 -07003584 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3585 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3586 reqWidth, reqHeight, hw_w, hw_h);
3587 return BAD_VALUE;
3588 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003589
Mathias Agopian180f10d2013-04-10 22:55:41 -07003590 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3591 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3592
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003593 bool secureLayerIsVisible = false;
3594 const LayerVector& layers(mDrawingState.layersSortedByZ);
3595 const size_t count = layers.size();
3596 for (size_t i = 0 ; i < count ; ++i) {
3597 const sp<Layer>& layer(layers[i]);
3598 const Layer::State& state(layer->getDrawingState());
3599 if (state.layerStack == hw->getLayerStack() && state.z >= minLayerZ &&
3600 state.z <= maxLayerZ && layer->isVisible() &&
3601 layer->isSecure()) {
3602 secureLayerIsVisible = true;
3603 }
3604 }
3605
3606 if (!isLocalScreenshot && secureLayerIsVisible) {
3607 ALOGW("FB is protected: PERMISSION_DENIED");
3608 return PERMISSION_DENIED;
3609 }
3610
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003611 // create a surface (because we're a producer, and we need to
3612 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003613 sp<Surface> sur = new Surface(producer, false);
Pablo Ceballos605d15a2016-07-21 13:44:51 -07003614
3615 // Put the screenshot Surface into async mode so that
3616 // Layer::headFenceHasSignaled will always return true and we'll latch the
3617 // first buffer regardless of whether or not its acquire fence has
3618 // signaled. This is needed to avoid a race condition in the rotation
3619 // animation. See b/30209608
3620 sur->setAsyncMode(true);
3621
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003622 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003623
Michael Lentine5a16a622015-05-21 13:48:24 -07003624 status_t result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
3625 if (result == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003626 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3627 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003628
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003629 int err = 0;
3630 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003631 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003632 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3633 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003634
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003635 if (err == NO_ERROR) {
3636 ANativeWindowBuffer* buffer;
3637 /* TODO: Once we have the sync framework everywhere this can use
3638 * server-side waits on the fence that dequeueBuffer returns.
3639 */
3640 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3641 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003642 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003643 // create an EGLImage from the buffer so we can later
3644 // turn it into a texture
3645 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3646 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3647 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003648 // this binds the given EGLImage as a framebuffer for the
3649 // duration of this scope.
3650 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3651 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003652 // this will in fact render into our dequeued buffer
3653 // via an FBO, which means we didn't have to create
3654 // an EGLSurface and therefore we're not
3655 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003656 renderScreenImplLocked(
3657 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3658 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003659
Riley Andrews86639902014-08-15 12:27:24 -07003660 // Attempt to create a sync khr object that can produce a sync point. If that
3661 // isn't available, create a non-dupable sync object in the fallback path and
3662 // wait on it directly.
3663 EGLSyncKHR sync;
3664 if (!DEBUG_SCREENSHOTS) {
3665 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Riley Andrews9707f4d2014-10-23 16:17:04 -07003666 // native fence fd will not be populated until flush() is done.
3667 getRenderEngine().flush();
Andy McFadden2d8d1202013-10-09 16:38:02 -07003668 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003669 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003670 }
Riley Andrews86639902014-08-15 12:27:24 -07003671 if (sync != EGL_NO_SYNC_KHR) {
3672 // get the sync fd
3673 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3674 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3675 ALOGW("captureScreen: failed to dup sync khr object");
3676 syncFd = -1;
3677 }
3678 eglDestroySyncKHR(mEGLDisplay, sync);
3679 } else {
3680 // fallback path
3681 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3682 if (sync != EGL_NO_SYNC_KHR) {
3683 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3684 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3685 EGLint eglErr = eglGetError();
3686 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3687 ALOGW("captureScreen: fence wait timed out");
3688 } else {
3689 ALOGW_IF(eglErr != EGL_SUCCESS,
3690 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3691 }
3692 eglDestroySyncKHR(mEGLDisplay, sync);
3693 } else {
3694 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3695 }
3696 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003697 if (DEBUG_SCREENSHOTS) {
3698 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3699 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3700 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3701 hw, minLayerZ, maxLayerZ);
3702 delete [] pixels;
3703 }
3704
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003705 } else {
3706 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3707 result = INVALID_OPERATION;
Prathmesh Prabhuf3209b02016-03-09 16:54:45 -08003708 window->cancelBuffer(window, buffer, syncFd);
3709 buffer = NULL;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003710 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003711 // destroy our image
3712 eglDestroyImageKHR(mEGLDisplay, image);
3713 } else {
3714 result = BAD_VALUE;
3715 }
Prathmesh Prabhuf3209b02016-03-09 16:54:45 -08003716 if (buffer) {
3717 // queueBuffer takes ownership of syncFd
3718 result = window->queueBuffer(window, buffer, syncFd);
3719 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003720 }
3721 } else {
3722 result = BAD_VALUE;
3723 }
3724 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3725 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003726
Mathias Agopian74c40c02010-09-29 13:02:36 -07003727 return result;
3728}
3729
Mathias Agopiand5556842013-09-19 17:08:37 -07003730void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3731 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003732 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003733 for (size_t y=0 ; y<h ; y++) {
3734 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3735 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003736 if (p[x] != 0xFF000000) return;
3737 }
3738 }
3739 ALOGE("*** we just took a black screenshot ***\n"
3740 "requested minz=%d, maxz=%d, layerStack=%d",
3741 minLayerZ, maxLayerZ, hw->getLayerStack());
3742 const LayerVector& layers( mDrawingState.layersSortedByZ );
3743 const size_t count = layers.size();
3744 for (size_t i=0 ; i<count ; ++i) {
3745 const sp<Layer>& layer(layers[i]);
3746 const Layer::State& state(layer->getDrawingState());
3747 const bool visible = (state.layerStack == hw->getLayerStack())
3748 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3749 && (layer->isVisible());
Dan Stoza9e56aa02015-11-02 13:00:03 -08003750 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%.3f",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003751 visible ? '+' : '-',
3752 i, layer->getName().string(), state.layerStack, state.z,
3753 layer->isVisible(), state.flags, state.alpha);
3754 }
3755 }
3756}
3757
Pablo Ceballosce796e72016-02-04 19:10:51 -08003758bool SurfaceFlinger::getFrameTimestamps(const Layer& layer,
3759 uint64_t frameNumber, FrameTimestamps* outTimestamps) {
3760 return mFenceTracker.getFrameTimestamps(layer, frameNumber, outTimestamps);
3761}
3762
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003763// ---------------------------------------------------------------------------
3764
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003765SurfaceFlinger::LayerVector::LayerVector() {
3766}
3767
3768SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003769 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003770}
3771
3772int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3773 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003774{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003775 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003776 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3777 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003778
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003779 uint32_t ls = l->getCurrentState().layerStack;
3780 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003781 if (ls != rs)
3782 return ls - rs;
3783
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003784 uint32_t lz = l->getCurrentState().z;
3785 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003786 if (lz != rz)
3787 return lz - rz;
3788
3789 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003790}
3791
3792// ---------------------------------------------------------------------------
3793
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003794SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Pablo Ceballos53390e12015-08-04 11:25:59 -07003795 : type(DisplayDevice::DISPLAY_ID_INVALID),
3796 layerStack(DisplayDevice::NO_LAYER_STACK),
3797 orientation(0),
3798 width(0),
3799 height(0),
3800 isSecure(false) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003801}
3802
Pablo Ceballos53390e12015-08-04 11:25:59 -07003803SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(
3804 DisplayDevice::DisplayType type, bool isSecure)
3805 : type(type),
3806 layerStack(DisplayDevice::NO_LAYER_STACK),
3807 orientation(0),
3808 width(0),
3809 height(0),
3810 isSecure(isSecure) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003811 viewport.makeInvalid();
3812 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003813}
3814
3815// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003816
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003817}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003818
3819
3820#if defined(__gl_h_)
3821#error "don't include gl/gl.h in this file"
3822#endif
3823
3824#if defined(__gl2_h_)
3825#error "don't include gl2/gl2.h in this file"
3826#endif