blob: a0b6b77f3507ff51cfe61e7a33ef0287b23dcdd0 [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
Evgenii Stepanov22819272016-07-14 14:17:24 -070093// Workaround for b/30067360: /proc/self/environ inaccessible in SurfaceFlinger
94// => ASan fails to read ASAN_OPTIONS => alloc-dealloc-mismatch bug is not
95// suppressed and prevents the device from booting.
96#ifndef __has_feature
97#define __has_feature(x) 0
98#endif
99#if __has_feature(address_sanitizer)
100__attribute__((visibility("default")))
101extern "C" const char* __asan_default_options() {
102 return "alloc_dealloc_mismatch=0";
103}
104#endif
105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700107
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700108// This is the phase offset in nanoseconds of the software vsync event
109// relative to the vsync event reported by HWComposer. The software vsync
110// event is when SurfaceFlinger and Choreographer-based applications run each
111// frame.
112//
113// This phase offset allows adjustment of the minimum latency from application
114// wake-up (by Choregographer) time to the time at which the resulting window
115// image is displayed. This value may be either positive (after the HW vsync)
116// or negative (before the HW vsync). Setting it to 0 will result in a
117// minimum latency of two vsync periods because the app and SurfaceFlinger
118// will run just after the HW vsync. Setting it to a positive number will
119// result in the minimum latency being:
120//
121// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
122//
123// Note that reducing this latency makes it more likely for the applications
124// to not have their window content image ready in time. When this happens
125// the latency will end up being an additional vsync period, and animations
126// will hiccup. Therefore, this latency should be tuned somewhat
127// conservatively (or at least with awareness of the trade-off being made).
128static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
129
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700130// This is the phase offset at which SurfaceFlinger's composition runs.
131static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
132
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133// ---------------------------------------------------------------------------
134
Mathias Agopian99b49842011-06-27 16:05:52 -0700135const String16 sHardwareTest("android.permission.HARDWARE_TEST");
136const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
137const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
138const String16 sDump("android.permission.DUMP");
139
140// ---------------------------------------------------------------------------
141
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700143 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700145 mTransactionPending(false),
146 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700147 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700148 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700149 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 mBootTime(systemTime()),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800151 mBuiltinDisplays(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152 mVisibleRegionsDirty(false),
Dan Stoza9e56aa02015-11-02 13:00:03 -0800153 mGeometryInvalid(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800154 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700156 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700157 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700158 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700159 mDebugInSwapBuffers(0),
160 mLastSwapBufferTime(0),
161 mDebugInTransaction(0),
162 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700163 mBootFinished(false),
Dan Stozaee44edd2015-03-23 15:50:23 -0700164 mForceFullDamage(false),
Tim Murray4a4e4a22016-04-19 16:29:23 +0000165 mPrimaryDispSync("PrimaryDispSync"),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700166 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700167 mHWVsyncAvailable(false),
Dan Stozab90cf072015-03-05 11:05:59 -0800168 mHasColorMatrix(false),
169 mHasPoweredOff(false),
170 mFrameBuckets(),
171 mTotalTime(0),
172 mLastSwapTime(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173{
Steve Blocka19954a2012-01-04 20:05:49 +0000174 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175
176 // debugging stuff...
177 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700178
Mathias Agopianb4b17302013-03-20 18:36:41 -0700179 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700180 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700181
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 property_get("debug.sf.showupdates", value, "0");
183 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700184
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700185 property_get("debug.sf.ddms", value, "0");
186 mDebugDDMS = atoi(value);
187 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700188 if (!startDdmConnection()) {
189 // start failed, and DDMS debugging not enabled
190 mDebugDDMS = 0;
191 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700192 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700193 ALOGI_IF(mDebugRegion, "showupdates enabled");
194 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
Dan Stozac5da2712016-07-20 15:38:12 -0700195
196 property_get("debug.sf.disable_backpressure", value, "0");
197 mPropagateBackpressure = !atoi(value);
198 ALOGI_IF(!mPropagateBackpressure, "Disabling backpressure propagation");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199}
200
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800201void SurfaceFlinger::onFirstRef()
202{
203 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800204}
205
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206SurfaceFlinger::~SurfaceFlinger()
207{
Mathias Agopiana4912602012-07-12 14:25:33 -0700208 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
209 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
210 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211}
212
Dan Stozac7014012014-02-14 15:03:43 -0800213void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800214{
215 // the window manager died on us. prepare its eulogy.
216
Andy McFadden13a082e2012-08-24 10:16:42 -0700217 // restore initial conditions (default device unblank, etc)
218 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800219
220 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700221 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800222}
223
Mathias Agopian7e27f052010-05-28 14:22:23 -0700224sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800225{
Mathias Agopian96f08192010-06-02 23:28:45 -0700226 sp<ISurfaceComposerClient> bclient;
227 sp<Client> client(new Client(this));
228 status_t err = client->initCheck();
229 if (err == NO_ERROR) {
230 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800232 return bclient;
233}
234
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700235sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
236 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700237{
238 class DisplayToken : public BBinder {
239 sp<SurfaceFlinger> flinger;
240 virtual ~DisplayToken() {
241 // no more references, this display must be terminated
242 Mutex::Autolock _l(flinger->mStateLock);
243 flinger->mCurrentState.displays.removeItem(this);
244 flinger->setTransactionFlags(eDisplayTransactionNeeded);
245 }
246 public:
247 DisplayToken(const sp<SurfaceFlinger>& flinger)
248 : flinger(flinger) {
249 }
250 };
251
252 sp<BBinder> token = new DisplayToken(this);
253
254 Mutex::Autolock _l(mStateLock);
Pablo Ceballos53390e12015-08-04 11:25:59 -0700255 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL, secure);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700256 info.displayName = displayName;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700257 mCurrentState.displays.add(token, info);
258
259 return token;
260}
261
Jesse Hall6c913be2013-08-08 12:15:49 -0700262void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
263 Mutex::Autolock _l(mStateLock);
264
265 ssize_t idx = mCurrentState.displays.indexOfKey(display);
266 if (idx < 0) {
267 ALOGW("destroyDisplay: invalid display token");
268 return;
269 }
270
271 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
272 if (!info.isVirtualDisplay()) {
273 ALOGE("destroyDisplay called for non-virtual display");
274 return;
275 }
276
277 mCurrentState.displays.removeItemsAt(idx);
278 setTransactionFlags(eDisplayTransactionNeeded);
279}
280
Jesse Hall692c7232012-11-08 15:41:56 -0800281void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
Dan Stoza9e56aa02015-11-02 13:00:03 -0800282 ALOGV("createBuiltinDisplayLocked(%d)", type);
Jesse Hall692c7232012-11-08 15:41:56 -0800283 ALOGW_IF(mBuiltinDisplays[type],
284 "Overwriting display token for display type %d", type);
285 mBuiltinDisplays[type] = new BBinder();
Jesse Hall692c7232012-11-08 15:41:56 -0800286 // All non-virtual displays are currently considered secure.
Pablo Ceballos53390e12015-08-04 11:25:59 -0700287 DisplayDeviceState info(type, true);
Jesse Hall692c7232012-11-08 15:41:56 -0800288 mCurrentState.displays.add(mBuiltinDisplays[type], info);
289}
290
Mathias Agopiane57f2922012-08-09 16:29:12 -0700291sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700292 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700293 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
294 return NULL;
295 }
Jesse Hall692c7232012-11-08 15:41:56 -0800296 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700297}
298
Jamie Gennis9a78c902011-01-12 18:30:40 -0800299sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
300{
301 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
302 return gba;
303}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700304
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305void SurfaceFlinger::bootFinished()
306{
307 const nsecs_t now = systemTime();
308 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000309 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700310 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700311
312 // wait patiently for the window manager death
313 const String16 name("window");
314 sp<IBinder> window(defaultServiceManager()->getService(name));
315 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700316 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700317 }
318
319 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700320 // formerly we would just kill the process, but we now ask it to exit so it
321 // can choose where to stop the animation.
322 property_set("service.bootanim.exit", "1");
Yusuke Sato0a688f52015-07-30 23:05:39 -0700323
324 const int LOGTAG_SF_STOP_BOOTANIM = 60110;
325 LOG_EVENT_LONG(LOGTAG_SF_STOP_BOOTANIM,
326 ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800327}
328
Mathias Agopian3f844832013-08-07 21:24:32 -0700329void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700330 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700331 RenderEngine& engine;
332 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700333 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700334 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
335 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700336 }
337 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700338 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700339 return true;
340 }
341 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700342 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700343}
344
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700345class DispSyncSource : public VSyncSource, private DispSync::Callback {
346public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700347 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
Tim Murray4a4e4a22016-04-19 16:29:23 +0000348 const char* name) :
349 mName(name),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700350 mValue(0),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700351 mTraceVsync(traceVsync),
Tim Murray4a4e4a22016-04-19 16:29:23 +0000352 mVsyncOnLabel(String8::format("VsyncOn-%s", name)),
353 mVsyncEventLabel(String8::format("VSYNC-%s", name)),
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700354 mDispSync(dispSync),
355 mCallbackMutex(),
356 mCallback(),
357 mVsyncMutex(),
358 mPhaseOffset(phaseOffset),
359 mEnabled(false) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700360
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700361 virtual ~DispSyncSource() {}
362
363 virtual void setVSyncEnabled(bool enable) {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700364 Mutex::Autolock lock(mVsyncMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700365 if (enable) {
Tim Murray4a4e4a22016-04-19 16:29:23 +0000366 status_t err = mDispSync->addEventListener(mName, mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700367 static_cast<DispSync::Callback*>(this));
368 if (err != NO_ERROR) {
369 ALOGE("error registering vsync callback: %s (%d)",
370 strerror(-err), err);
371 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700372 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700373 } else {
374 status_t err = mDispSync->removeEventListener(
375 static_cast<DispSync::Callback*>(this));
376 if (err != NO_ERROR) {
377 ALOGE("error unregistering vsync callback: %s (%d)",
378 strerror(-err), err);
379 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700380 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700381 }
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700382 mEnabled = enable;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700383 }
384
385 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700386 Mutex::Autolock lock(mCallbackMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700387 mCallback = callback;
388 }
389
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700390 virtual void setPhaseOffset(nsecs_t phaseOffset) {
391 Mutex::Autolock lock(mVsyncMutex);
392
393 // Normalize phaseOffset to [0, period)
394 auto period = mDispSync->getPeriod();
395 phaseOffset %= period;
396 if (phaseOffset < 0) {
397 // If we're here, then phaseOffset is in (-period, 0). After this
398 // operation, it will be in (0, period)
399 phaseOffset += period;
400 }
401 mPhaseOffset = phaseOffset;
402
403 // If we're not enabled, we don't need to mess with the listeners
404 if (!mEnabled) {
405 return;
406 }
407
408 // Remove the listener with the old offset
409 status_t err = mDispSync->removeEventListener(
410 static_cast<DispSync::Callback*>(this));
411 if (err != NO_ERROR) {
412 ALOGE("error unregistering vsync callback: %s (%d)",
413 strerror(-err), err);
414 }
415
416 // Add a listener with the new offset
Tim Murray4a4e4a22016-04-19 16:29:23 +0000417 err = mDispSync->addEventListener(mName, mPhaseOffset,
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700418 static_cast<DispSync::Callback*>(this));
419 if (err != NO_ERROR) {
420 ALOGE("error registering vsync callback: %s (%d)",
421 strerror(-err), err);
422 }
423 }
424
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700425private:
426 virtual void onDispSyncEvent(nsecs_t when) {
427 sp<VSyncSource::Callback> callback;
428 {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700429 Mutex::Autolock lock(mCallbackMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700430 callback = mCallback;
431
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700432 if (mTraceVsync) {
433 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700434 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700435 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700436 }
437
438 if (callback != NULL) {
439 callback->onVSyncEvent(when);
440 }
441 }
442
Tim Murray4a4e4a22016-04-19 16:29:23 +0000443 const char* const mName;
444
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700445 int mValue;
446
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700447 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700448 const String8 mVsyncOnLabel;
449 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700450
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700451 DispSync* mDispSync;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700452
453 Mutex mCallbackMutex; // Protects the following
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700454 sp<VSyncSource::Callback> mCallback;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700455
456 Mutex mVsyncMutex; // Protects the following
457 nsecs_t mPhaseOffset;
458 bool mEnabled;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700459};
460
461void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700462 ALOGI( "SurfaceFlinger's main thread ready to run. "
463 "Initializing graphics H/W...");
464
Dan Stoza9e56aa02015-11-02 13:00:03 -0800465 { // Autolock scope
466 Mutex::Autolock _l(mStateLock);
467
468 // initialize EGL for the default display
469 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
470 eglInitialize(mEGLDisplay, NULL, NULL);
471
472 // start the EventThread
473 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
474 vsyncPhaseOffsetNs, true, "app");
Tim Murray4a4e4a22016-04-19 16:29:23 +0000475 mEventThread = new EventThread(vsyncSrc, *this);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800476 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
477 sfVsyncPhaseOffsetNs, true, "sf");
Tim Murray4a4e4a22016-04-19 16:29:23 +0000478 mSFEventThread = new EventThread(sfVsyncSrc, *this);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800479 mEventQueue.setEventThread(mSFEventThread);
480
Tim Murray41a38532016-06-22 12:42:10 -0700481 // set EventThread and SFEventThread to SCHED_FIFO for minimum jitter
482 struct sched_param param = {0};
483 param.sched_priority = 1;
484 if (sched_setscheduler(mEventThread->getTid(), SCHED_FIFO, &param) != 0) {
485 ALOGE("Couldn't set SCHED_FIFO for EventThread");
486 }
487
488 if (sched_setscheduler(mSFEventThread->getTid(), SCHED_FIFO, &param) != 0) {
489 ALOGE("Couldn't set SCHED_FIFO for SFEventThread");
490 }
491
492
Dan Stoza9e56aa02015-11-02 13:00:03 -0800493 // Get a RenderEngine for the given display / config (can't fail)
494 mRenderEngine = RenderEngine::create(mEGLDisplay,
495 HAL_PIXEL_FORMAT_RGBA_8888);
496 }
497
498 // Drop the state lock while we initialize the hardware composer. We drop
499 // the lock because on creation, it will call back into SurfaceFlinger to
500 // initialize the primary display.
501 mHwc = new HWComposer(this);
502 mHwc->setEventHandler(static_cast<HWComposer::EventHandler*>(this));
503
Jesse Hall692c7232012-11-08 15:41:56 -0800504 Mutex::Autolock _l(mStateLock);
505
Mathias Agopian875d8e12013-06-07 15:35:48 -0700506 // retrieve the EGL context that was selected/created
507 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700508
Mathias Agopianda27af92012-09-13 18:17:13 -0700509 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
510 "couldn't create EGLContext");
511
Dan Stoza9e56aa02015-11-02 13:00:03 -0800512 // make the GLContext current so that we can create textures when creating
513 // Layers (which may happens before we render something)
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700514 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
515
Jamie Gennisd1700752013-10-14 12:22:52 -0700516 mEventControlThread = new EventControlThread(this);
517 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
518
Mathias Agopian92a979a2012-08-02 18:32:23 -0700519 // initialize our drawing state
520 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700521
Andy McFadden13a082e2012-08-24 10:16:42 -0700522 // set initial conditions (e.g. unblank default device)
523 initializeDisplays();
524
Dan Stoza4e637772016-07-28 13:31:51 -0700525 mRenderEngine->primeCache();
526
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700527 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700528 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800529
Dan Stoza9e56aa02015-11-02 13:00:03 -0800530 ALOGV("Done initializing");
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700531}
532
Mathias Agopiana67e4182012-06-19 17:26:12 -0700533void SurfaceFlinger::startBootAnim() {
534 // start boot animation
535 property_set("service.bootanim.exit", "0");
536 property_set("ctl.start", "bootanim");
537}
538
Mathias Agopian875d8e12013-06-07 15:35:48 -0700539size_t SurfaceFlinger::getMaxTextureSize() const {
540 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700541}
542
Mathias Agopian875d8e12013-06-07 15:35:48 -0700543size_t SurfaceFlinger::getMaxViewportDims() const {
544 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700545}
546
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800547// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800548
Jamie Gennis582270d2011-08-17 18:19:00 -0700549bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800550 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800551 Mutex::Autolock _l(mStateLock);
Marco Nelissen097ca272014-11-14 08:01:01 -0800552 sp<IBinder> surfaceTextureBinder(IInterface::asBinder(bufferProducer));
Mathias Agopian67106042013-03-14 19:18:13 -0700553 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800554}
555
Dan Stoza7f7da322014-05-02 15:26:25 -0700556status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
557 Vector<DisplayInfo>* configs) {
Tatenda Chipeperekwa23e16bb2014-10-29 16:47:19 -0700558 if ((configs == NULL) || (display.get() == NULL)) {
Dan Stoza7f7da322014-05-02 15:26:25 -0700559 return BAD_VALUE;
560 }
561
Naseer Ahmed7aa0c472014-11-03 14:49:23 -0500562 if (!display.get())
563 return NAME_NOT_FOUND;
564
Jesse Hall692c7232012-11-08 15:41:56 -0800565 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700566 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800567 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700568 type = i;
569 break;
570 }
571 }
572
573 if (type < 0) {
574 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700575 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700576
Mathias Agopian8b736f12012-08-13 17:54:26 -0700577 // TODO: Not sure if display density should handled by SF any longer
578 class Density {
579 static int getDensityFromProperty(char const* propName) {
580 char property[PROPERTY_VALUE_MAX];
581 int density = 0;
582 if (property_get(propName, property, NULL) > 0) {
583 density = atoi(property);
584 }
585 return density;
586 }
587 public:
588 static int getEmuDensity() {
589 return getDensityFromProperty("qemu.sf.lcd_density"); }
590 static int getBuildDensity() {
591 return getDensityFromProperty("ro.sf.lcd_density"); }
592 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700593
Dan Stoza7f7da322014-05-02 15:26:25 -0700594 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700595
Dan Stoza9e56aa02015-11-02 13:00:03 -0800596 for (const auto& hwConfig : getHwComposer().getConfigs(type)) {
Dan Stoza7f7da322014-05-02 15:26:25 -0700597 DisplayInfo info = DisplayInfo();
598
Dan Stoza9e56aa02015-11-02 13:00:03 -0800599 float xdpi = hwConfig->getDpiX();
600 float ydpi = hwConfig->getDpiY();
Dan Stoza7f7da322014-05-02 15:26:25 -0700601
602 if (type == DisplayDevice::DISPLAY_PRIMARY) {
603 // The density of the device is provided by a build property
604 float density = Density::getBuildDensity() / 160.0f;
605 if (density == 0) {
606 // the build doesn't provide a density -- this is wrong!
607 // use xdpi instead
608 ALOGE("ro.sf.lcd_density must be defined as a build property");
609 density = xdpi / 160.0f;
610 }
611 if (Density::getEmuDensity()) {
612 // if "qemu.sf.lcd_density" is specified, it overrides everything
613 xdpi = ydpi = density = Density::getEmuDensity();
614 density /= 160.0f;
615 }
616 info.density = density;
617
618 // TODO: this needs to go away (currently needed only by webkit)
619 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
620 info.orientation = hw->getOrientation();
621 } else {
622 // TODO: where should this value come from?
623 static const int TV_DENSITY = 213;
624 info.density = TV_DENSITY / 160.0f;
625 info.orientation = 0;
626 }
627
Dan Stoza9e56aa02015-11-02 13:00:03 -0800628 info.w = hwConfig->getWidth();
629 info.h = hwConfig->getHeight();
Dan Stoza7f7da322014-05-02 15:26:25 -0700630 info.xdpi = xdpi;
631 info.ydpi = ydpi;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800632 info.fps = 1e9 / hwConfig->getVsyncPeriod();
Andy McFadden91b2ca82014-06-13 14:04:23 -0700633 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800634
Andy McFadden91b2ca82014-06-13 14:04:23 -0700635 // This is how far in advance a buffer must be queued for
636 // presentation at a given time. If you want a buffer to appear
637 // on the screen at time N, you must submit the buffer before
638 // (N - presentationDeadline).
639 //
640 // Normally it's one full refresh period (to give SF a chance to
641 // latch the buffer), but this can be reduced by configuring a
642 // DispSync offset. Any additional delays introduced by the hardware
643 // composer or panel must be accounted for here.
644 //
645 // We add an additional 1ms to allow for processing time and
646 // differences between the ideal and actual refresh rate.
Dan Stoza9e56aa02015-11-02 13:00:03 -0800647 info.presentationDeadline = hwConfig->getVsyncPeriod() -
648 SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700649
650 // All non-virtual displays are currently considered secure.
651 info.secure = true;
652
Michael Wright28f24d02016-07-12 13:30:53 -0700653 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700654 }
655
Dan Stoza7f7da322014-05-02 15:26:25 -0700656 return NO_ERROR;
657}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700658
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800659status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& /* display */,
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700660 DisplayStatInfo* stats) {
661 if (stats == NULL) {
662 return BAD_VALUE;
663 }
664
665 // FIXME for now we always return stats for the primary display
666 memset(stats, 0, sizeof(*stats));
667 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
668 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
669 return NO_ERROR;
670}
671
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700672int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
Dan Stoza24a42e92015-03-09 10:04:11 -0700673 sp<DisplayDevice> device(getDisplayDevice(display));
674 if (device != NULL) {
675 return device->getActiveConfig();
676 }
677 return BAD_VALUE;
Dan Stoza7f7da322014-05-02 15:26:25 -0700678}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700679
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700680void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
681 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
682 this);
683 int32_t type = hw->getDisplayType();
684 int currentMode = hw->getActiveConfig();
685
686 if (mode == currentMode) {
687 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
688 return;
689 }
690
691 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
692 ALOGW("Trying to set config for virtual display");
693 return;
694 }
695
696 hw->setActiveConfig(mode);
697 getHwComposer().setActiveConfig(type, mode);
698}
699
700status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
701 class MessageSetActiveConfig: public MessageBase {
702 SurfaceFlinger& mFlinger;
703 sp<IBinder> mDisplay;
704 int mMode;
705 public:
706 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
707 int mode) :
708 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
709 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700710 Vector<DisplayInfo> configs;
711 mFlinger.getDisplayConfigs(mDisplay, &configs);
Jesse Hall78442112014-08-07 22:43:06 -0700712 if (mMode < 0 || mMode >= static_cast<int>(configs.size())) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700713 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700714 mMode, configs.size());
Michael Wright28f24d02016-07-12 13:30:53 -0700715 return true;
Michael Lentine7306c672014-07-30 13:00:37 -0700716 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700717 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
718 if (hw == NULL) {
719 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700720 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700721 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
722 ALOGW("Attempt to set active config = %d for virtual display",
723 mMode);
724 } else {
725 mFlinger.setActiveConfigInternal(hw, mMode);
726 }
727 return true;
728 }
729 };
730 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
731 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700732 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700733}
Michael Wright28f24d02016-07-12 13:30:53 -0700734status_t SurfaceFlinger::getDisplayColorModes(const sp<IBinder>& display,
735 Vector<android_color_mode_t>* outColorModes) {
736 if ((outColorModes == nullptr) || (display.get() == nullptr)) {
737 return BAD_VALUE;
738 }
739
740 if (!display.get()) {
741 return NAME_NOT_FOUND;
742 }
743
744 int32_t type = NAME_NOT_FOUND;
745 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
746 if (display == mBuiltinDisplays[i]) {
747 type = i;
748 break;
749 }
750 }
751
752 if (type < 0) {
753 return type;
754 }
755
756 std::vector<android_color_mode_t> modes = getHwComposer().getColorModes(type);
757 outColorModes->clear();
758 std::copy(modes.cbegin(), modes.cend(), std::back_inserter(*outColorModes));
759
760 return NO_ERROR;
761}
762
763android_color_mode_t SurfaceFlinger::getActiveColorMode(const sp<IBinder>& display) {
764 sp<DisplayDevice> device(getDisplayDevice(display));
765 if (device != nullptr) {
766 return device->getActiveColorMode();
767 }
768 return static_cast<android_color_mode_t>(BAD_VALUE);
769}
770
771void SurfaceFlinger::setActiveColorModeInternal(const sp<DisplayDevice>& hw,
772 android_color_mode_t mode) {
773 ALOGD("Set active color mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
774 this);
775 int32_t type = hw->getDisplayType();
776 android_color_mode_t currentMode = hw->getActiveColorMode();
777
778 if (mode == currentMode) {
779 ALOGD("Screen type=%d is already in color mode=%d", hw->getDisplayType(), mode);
780 return;
781 }
782
783 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
784 ALOGW("Trying to set config for virtual display");
785 return;
786 }
787
788 hw->setActiveColorMode(mode);
789 getHwComposer().setActiveColorMode(type, mode);
790}
791
792
793status_t SurfaceFlinger::setActiveColorMode(const sp<IBinder>& display,
794 android_color_mode_t colorMode) {
795 class MessageSetActiveColorMode: public MessageBase {
796 SurfaceFlinger& mFlinger;
797 sp<IBinder> mDisplay;
798 android_color_mode_t mMode;
799 public:
800 MessageSetActiveColorMode(SurfaceFlinger& flinger, const sp<IBinder>& disp,
801 android_color_mode_t mode) :
802 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
803 virtual bool handler() {
804 Vector<android_color_mode_t> modes;
805 mFlinger.getDisplayColorModes(mDisplay, &modes);
806 bool exists = std::find(std::begin(modes), std::end(modes), mMode) != std::end(modes);
807 if (mMode < 0 || !exists) {
808 ALOGE("Attempt to set invalid active color mode = %d for display %p", mMode,
809 mDisplay.get());
810 return true;
811 }
812 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
813 if (hw == nullptr) {
814 ALOGE("Attempt to set active color mode = %d for null display %p",
815 mMode, mDisplay.get());
816 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
817 ALOGW("Attempt to set active color mode= %d for virtual display",
818 mMode);
819 } else {
820 mFlinger.setActiveColorModeInternal(hw, mMode);
821 }
822 return true;
823 }
824 };
825 sp<MessageBase> msg = new MessageSetActiveColorMode(*this, display, colorMode);
826 postMessageSync(msg);
827 return NO_ERROR;
828}
Mathias Agopianc666cae2012-07-25 18:56:13 -0700829
Svetoslavd85084b2014-03-20 10:28:31 -0700830status_t SurfaceFlinger::clearAnimationFrameStats() {
831 Mutex::Autolock _l(mStateLock);
832 mAnimFrameTracker.clearStats();
833 return NO_ERROR;
834}
835
836status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
837 Mutex::Autolock _l(mStateLock);
838 mAnimFrameTracker.getStats(outStats);
839 return NO_ERROR;
840}
841
Dan Stozac4f471e2016-03-24 09:31:08 -0700842status_t SurfaceFlinger::getHdrCapabilities(const sp<IBinder>& display,
843 HdrCapabilities* outCapabilities) const {
844 Mutex::Autolock _l(mStateLock);
845
846 sp<const DisplayDevice> displayDevice(getDisplayDevice(display));
847 if (displayDevice == nullptr) {
848 ALOGE("getHdrCapabilities: Invalid display %p", displayDevice.get());
849 return BAD_VALUE;
850 }
851
852 std::unique_ptr<HdrCapabilities> capabilities =
853 mHwc->getHdrCapabilities(displayDevice->getHwcDisplayId());
854 if (capabilities) {
855 std::swap(*outCapabilities, *capabilities);
856 } else {
857 return BAD_VALUE;
858 }
859
860 return NO_ERROR;
861}
862
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800863// ----------------------------------------------------------------------------
864
865sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800866 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700867}
868
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800869// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800870
871void SurfaceFlinger::waitForEvent() {
872 mEventQueue.waitMessage();
873}
874
875void SurfaceFlinger::signalTransaction() {
876 mEventQueue.invalidate();
877}
878
879void SurfaceFlinger::signalLayerUpdate() {
880 mEventQueue.invalidate();
881}
882
883void SurfaceFlinger::signalRefresh() {
884 mEventQueue.refresh();
885}
886
887status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800888 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800889 return mEventQueue.postMessage(msg, reltime);
890}
891
892status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800893 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800894 status_t res = mEventQueue.postMessage(msg, reltime);
895 if (res == NO_ERROR) {
896 msg->wait();
897 }
898 return res;
899}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800900
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700901void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700902 do {
903 waitForEvent();
904 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800905}
906
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700907void SurfaceFlinger::enableHardwareVsync() {
908 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700909 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700910 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700911 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
912 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700913 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700914 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700915}
916
Jesse Hall948fe0c2013-10-14 12:56:09 -0700917void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700918 Mutex::Autolock _l(mHWVsyncLock);
919
Jesse Hall948fe0c2013-10-14 12:56:09 -0700920 if (makeAvailable) {
921 mHWVsyncAvailable = true;
922 } else if (!mHWVsyncAvailable) {
Dan Stoza0a3c4d62016-04-19 11:56:20 -0700923 // Hardware vsync is not currently available, so abort the resync
924 // attempt for now
Jesse Hall948fe0c2013-10-14 12:56:09 -0700925 return;
926 }
927
Dan Stoza9e56aa02015-11-02 13:00:03 -0800928 const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
929 const nsecs_t period = activeConfig->getVsyncPeriod();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700930
931 mPrimaryDispSync.reset();
932 mPrimaryDispSync.setPeriod(period);
933
934 if (!mPrimaryHWVsyncEnabled) {
935 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700936 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
937 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700938 mPrimaryHWVsyncEnabled = true;
939 }
940}
941
Jesse Hall948fe0c2013-10-14 12:56:09 -0700942void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700943 Mutex::Autolock _l(mHWVsyncLock);
944 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700945 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
946 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700947 mPrimaryDispSync.endResync();
948 mPrimaryHWVsyncEnabled = false;
949 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700950 if (makeUnavailable) {
951 mHWVsyncAvailable = false;
952 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700953}
954
Tim Murray4a4e4a22016-04-19 16:29:23 +0000955void SurfaceFlinger::resyncWithRateLimit() {
956 static constexpr nsecs_t kIgnoreDelay = ms2ns(500);
957 if (systemTime() - mLastSwapTime > kIgnoreDelay) {
Dan Stoza0a3c4d62016-04-19 11:56:20 -0700958 resyncToHardwareVsync(false);
Tim Murray4a4e4a22016-04-19 16:29:23 +0000959 }
960}
961
Dan Stoza9e56aa02015-11-02 13:00:03 -0800962void SurfaceFlinger::onVSyncReceived(int32_t type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700963 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700964
Jamie Gennisd1700752013-10-14 12:22:52 -0700965 { // Scope for the lock
966 Mutex::Autolock _l(mHWVsyncLock);
967 if (type == 0 && mPrimaryHWVsyncEnabled) {
968 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700969 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700970 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700971
972 if (needsHwVsync) {
973 enableHardwareVsync();
974 } else {
975 disableHardwareVsync(false);
976 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700977}
978
Dan Stoza9e56aa02015-11-02 13:00:03 -0800979void SurfaceFlinger::onHotplugReceived(int32_t disp, bool connected) {
980 ALOGV("onHotplugReceived(%d, %s)", disp, connected ? "true" : "false");
981 if (disp == DisplayDevice::DISPLAY_PRIMARY) {
982 Mutex::Autolock lock(mStateLock);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700983
Dan Stoza9e56aa02015-11-02 13:00:03 -0800984 // All non-virtual displays are currently considered secure.
985 bool isSecure = true;
986
987 int32_t type = DisplayDevice::DISPLAY_PRIMARY;
988 createBuiltinDisplayLocked(DisplayDevice::DISPLAY_PRIMARY);
989 wp<IBinder> token = mBuiltinDisplays[type];
990
991 sp<IGraphicBufferProducer> producer;
992 sp<IGraphicBufferConsumer> consumer;
993 BufferQueue::createBufferQueue(&producer, &consumer,
994 new GraphicBufferAlloc());
995
996 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc,
997 DisplayDevice::DISPLAY_PRIMARY, consumer);
998 sp<DisplayDevice> hw = new DisplayDevice(this,
999 DisplayDevice::DISPLAY_PRIMARY, disp, isSecure, token, fbs,
1000 producer, mRenderEngine->getEGLConfig());
1001 mDisplays.add(token, hw);
1002 } else {
1003 auto type = DisplayDevice::DISPLAY_EXTERNAL;
Mathias Agopian9e2463e2012-09-21 18:26:16 -07001004 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -08001005 if (connected) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001006 createBuiltinDisplayLocked(type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -07001007 } else {
Jesse Hall692c7232012-11-08 15:41:56 -08001008 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
1009 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -07001010 }
1011 setTransactionFlags(eDisplayTransactionNeeded);
1012
Andy McFadden9e9689c2012-10-10 18:17:51 -07001013 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001014 }
Mathias Agopian86303202012-07-24 22:46:10 -07001015}
1016
Dan Stoza9e56aa02015-11-02 13:00:03 -08001017void SurfaceFlinger::setVsyncEnabled(int disp, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001018 ATRACE_CALL();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001019 getHwComposer().setVsyncEnabled(disp,
1020 enabled ? HWC2::Vsync::Enable : HWC2::Vsync::Disable);
Mathias Agopian86303202012-07-24 22:46:10 -07001021}
1022
Mathias Agopian4fec8732012-06-29 14:12:52 -07001023void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -08001024 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001025 switch (what) {
Dan Stoza6b9454d2014-11-07 16:00:59 -08001026 case MessageQueue::INVALIDATE: {
Dan Stoza50182882016-07-08 12:02:20 -07001027 bool frameMissed = !mHadClientComposition &&
1028 mPreviousPresentFence != Fence::NO_FENCE &&
1029 mPreviousPresentFence->getSignalTime() == INT64_MAX;
1030 ATRACE_INT("FrameMissed", static_cast<int>(frameMissed));
Dan Stozac5da2712016-07-20 15:38:12 -07001031 if (mPropagateBackpressure && frameMissed) {
Dan Stoza50182882016-07-08 12:02:20 -07001032 signalLayerUpdate();
1033 break;
1034 }
1035
Dan Stoza6b9454d2014-11-07 16:00:59 -08001036 bool refreshNeeded = handleMessageTransaction();
1037 refreshNeeded |= handleMessageInvalidate();
Dan Stoza58784442014-12-02 16:58:17 -08001038 refreshNeeded |= mRepaintEverything;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001039 if (refreshNeeded) {
Dan Stoza58784442014-12-02 16:58:17 -08001040 // Signal a refresh if a transaction modified the window state,
1041 // a new buffer was latched, or if HWC has requested a full
1042 // repaint
Dan Stoza6b9454d2014-11-07 16:00:59 -08001043 signalRefresh();
1044 }
1045 break;
1046 }
1047 case MessageQueue::REFRESH: {
1048 handleMessageRefresh();
1049 break;
1050 }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001051 }
1052}
1053
Dan Stoza6b9454d2014-11-07 16:00:59 -08001054bool SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001055 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001056 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -07001057 handleTransaction(transactionFlags);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001058 return true;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001059 }
Dan Stoza6b9454d2014-11-07 16:00:59 -08001060 return false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001061}
1062
Dan Stoza6b9454d2014-11-07 16:00:59 -08001063bool SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001064 ATRACE_CALL();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001065 return handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001066}
1067
1068void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001069 ATRACE_CALL();
Dan Stoza14cd37c2015-07-09 12:43:33 -07001070
Pablo Ceballos40845df2016-01-25 17:41:15 -08001071 nsecs_t refreshStartTime = systemTime(SYSTEM_TIME_MONOTONIC);
Dan Stoza14cd37c2015-07-09 12:43:33 -07001072
Dan Stoza05dacfb2016-07-01 13:33:38 -07001073 preComposition();
1074 rebuildLayerStacks();
1075 setUpHWComposer();
1076 doDebugFlashRegions();
1077 doComposition();
1078 postComposition(refreshStartTime);
1079
1080 mPreviousPresentFence = mHwc->getRetireFence(HWC_DISPLAY_PRIMARY);
Dan Stozabfbffeb2016-07-21 14:49:33 -07001081
1082 mHadClientComposition = false;
1083 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
1084 const sp<DisplayDevice>& displayDevice = mDisplays[displayId];
1085 mHadClientComposition = mHadClientComposition ||
1086 mHwc->hasClientComposition(displayDevice->getHwcDisplayId());
1087 }
Dan Stoza14cd37c2015-07-09 12:43:33 -07001088
Dan Stoza9e56aa02015-11-02 13:00:03 -08001089 // Release any buffers which were replaced this frame
1090 for (auto& layer : mLayersWithQueuedFrames) {
1091 layer->releasePendingBuffer();
1092 }
1093 mLayersWithQueuedFrames.clear();
Mathias Agopiancd60f992012-08-16 16:28:27 -07001094}
Mathias Agopian4fec8732012-06-29 14:12:52 -07001095
Mathias Agopiancd60f992012-08-16 16:28:27 -07001096void SurfaceFlinger::doDebugFlashRegions()
1097{
1098 // is debugging enabled
1099 if (CC_LIKELY(!mDebugRegion))
1100 return;
1101
1102 const bool repaintEverything = mRepaintEverything;
1103 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1104 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001105 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001106 // transform the dirty region into this screen's coordinate space
1107 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
1108 if (!dirtyRegion.isEmpty()) {
1109 // redraw the whole screen
1110 doComposeSurfaces(hw, Region(hw->bounds()));
1111
1112 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -07001113 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001114 RenderEngine& engine(getRenderEngine());
1115 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
1116
Mathias Agopianda27af92012-09-13 18:17:13 -07001117 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -07001118 }
1119 }
1120 }
1121
1122 postFramebuffer();
1123
1124 if (mDebugRegion > 1) {
1125 usleep(mDebugRegion * 1000);
1126 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -07001127
Dan Stoza9e56aa02015-11-02 13:00:03 -08001128 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
Dan Stoza7bdf55a2016-06-17 11:29:01 -07001129 auto& displayDevice = mDisplays[displayId];
1130 if (!displayDevice->isDisplayOn()) {
1131 continue;
1132 }
1133
1134 status_t result = displayDevice->prepareFrame(*mHwc);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001135 ALOGE_IF(result != NO_ERROR, "prepareFrame for display %zd failed:"
1136 " %d (%s)", displayId, result, strerror(-result));
Mathias Agopianbb53b0e2012-09-24 21:27:29 -07001137 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001138}
1139
1140void SurfaceFlinger::preComposition()
1141{
Dan Stoza9e56aa02015-11-02 13:00:03 -08001142 ATRACE_CALL();
1143 ALOGV("preComposition");
1144
Mathias Agopiancd60f992012-08-16 16:28:27 -07001145 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001146 const LayerVector& layers(mDrawingState.layersSortedByZ);
1147 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -07001148 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001149 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001150 needExtraInvalidate = true;
1151 }
1152 }
1153 if (needExtraInvalidate) {
1154 signalLayerUpdate();
1155 }
1156}
1157
Pablo Ceballos40845df2016-01-25 17:41:15 -08001158void SurfaceFlinger::postComposition(nsecs_t refreshStartTime)
Mathias Agopiancd60f992012-08-16 16:28:27 -07001159{
Dan Stoza9e56aa02015-11-02 13:00:03 -08001160 ATRACE_CALL();
1161 ALOGV("postComposition");
1162
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001163 const LayerVector& layers(mDrawingState.layersSortedByZ);
1164 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -07001165 for (size_t i=0 ; i<count ; i++) {
Dan Stozae77c7662016-05-13 11:37:28 -07001166 bool frameLatched = layers[i]->onPostComposition();
1167 if (frameLatched) {
1168 recordBufferingStats(layers[i]->getName().string(),
1169 layers[i]->getOccupancyHistory(false));
1170 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001171 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001172
Dan Stoza9e56aa02015-11-02 13:00:03 -08001173 sp<Fence> presentFence = mHwc->getRetireFence(HWC_DISPLAY_PRIMARY);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001174
1175 if (presentFence->isValid()) {
1176 if (mPrimaryDispSync.addPresentFence(presentFence)) {
1177 enableHardwareVsync();
1178 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -07001179 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001180 }
1181 }
1182
Dan Stozab90cf072015-03-05 11:05:59 -08001183 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Andy McFadden5167ec62014-05-22 13:08:43 -07001184 if (kIgnorePresentFences) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001185 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001186 enableHardwareVsync();
1187 }
1188 }
1189
Pablo Ceballos40845df2016-01-25 17:41:15 -08001190 mFenceTracker.addFrame(refreshStartTime, presentFence,
1191 hw->getVisibleLayersSortedByZ(), hw->getClientTargetAcquireFence());
1192
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001193 if (mAnimCompositionPending) {
1194 mAnimCompositionPending = false;
1195
Jesse Halla9a1b002013-02-27 16:39:25 -08001196 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001197 mAnimFrameTracker.setActualPresentFence(presentFence);
1198 } else {
1199 // The HWC doesn't support present fences, so use the refresh
1200 // timestamp instead.
Dan Stoza9e56aa02015-11-02 13:00:03 -08001201 nsecs_t presentTime =
1202 mHwc->getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001203 mAnimFrameTracker.setActualPresentTime(presentTime);
1204 }
1205 mAnimFrameTracker.advanceFrame();
1206 }
Dan Stozab90cf072015-03-05 11:05:59 -08001207
1208 if (hw->getPowerMode() == HWC_POWER_MODE_OFF) {
1209 return;
1210 }
1211
1212 nsecs_t currentTime = systemTime();
1213 if (mHasPoweredOff) {
1214 mHasPoweredOff = false;
1215 } else {
1216 nsecs_t period = mPrimaryDispSync.getPeriod();
1217 nsecs_t elapsedTime = currentTime - mLastSwapTime;
1218 size_t numPeriods = static_cast<size_t>(elapsedTime / period);
1219 if (numPeriods < NUM_BUCKETS - 1) {
1220 mFrameBuckets[numPeriods] += elapsedTime;
1221 } else {
1222 mFrameBuckets[NUM_BUCKETS - 1] += elapsedTime;
1223 }
1224 mTotalTime += elapsedTime;
1225 }
1226 mLastSwapTime = currentTime;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001227}
1228
1229void SurfaceFlinger::rebuildLayerStacks() {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001230 ATRACE_CALL();
1231 ALOGV("rebuildLayerStacks");
1232
Mathias Agopiancd60f992012-08-16 16:28:27 -07001233 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001234 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001235 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -07001236 mVisibleRegionsDirty = false;
1237 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001238
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001239 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001240 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001241 Region opaqueRegion;
1242 Region dirtyRegion;
Dan Stoza9e56aa02015-11-02 13:00:03 -08001243 Vector<sp<Layer>> layersSortedByZ;
1244 const sp<DisplayDevice>& displayDevice(mDisplays[dpy]);
1245 const Transform& tr(displayDevice->getTransform());
1246 const Rect bounds(displayDevice->getBounds());
1247 if (displayDevice->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001248 SurfaceFlinger::computeVisibleRegions(layers,
Dan Stoza9e56aa02015-11-02 13:00:03 -08001249 displayDevice->getLayerStack(), dirtyRegion,
1250 opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001251
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001252 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001253 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001254 const sp<Layer>& layer(layers[i]);
1255 const Layer::State& s(layer->getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -08001256 if (s.layerStack == displayDevice->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -07001257 Region drawRegion(tr.transform(
1258 layer->visibleNonTransparentRegion));
1259 drawRegion.andSelf(bounds);
1260 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001261 layersSortedByZ.add(layer);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001262 } else {
1263 // Clear out the HWC layer if this layer was
1264 // previously visible, but no longer is
1265 layer->setHwcLayer(displayDevice->getHwcDisplayId(),
1266 nullptr);
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001267 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001268 }
1269 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001270 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001271 displayDevice->setVisibleLayersSortedByZ(layersSortedByZ);
1272 displayDevice->undefinedRegion.set(bounds);
1273 displayDevice->undefinedRegion.subtractSelf(
1274 tr.transform(opaqueRegion));
1275 displayDevice->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001276 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001277 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001278}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001279
Mathias Agopiancd60f992012-08-16 16:28:27 -07001280void SurfaceFlinger::setUpHWComposer() {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001281 ATRACE_CALL();
1282 ALOGV("setUpHWComposer");
1283
Jesse Hall028dc8f2013-08-20 16:35:32 -07001284 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001285 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1286 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1287 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1288
1289 // If nothing has changed (!dirty), don't recompose.
1290 // If something changed, but we don't currently have any visible layers,
1291 // and didn't when we last did a composition, then skip it this time.
1292 // The second rule does two things:
1293 // - When all layers are removed from a display, we'll emit one black
1294 // frame, then nothing more until we get new layers.
1295 // - When a display is created with a private layer stack, we won't
1296 // emit any black frames until a layer is added to the layer stack.
1297 bool mustRecompose = dirty && !(empty && wasEmpty);
1298
1299 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1300 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1301 mustRecompose ? "doing" : "skipping",
1302 dirty ? "+" : "-",
1303 empty ? "+" : "-",
1304 wasEmpty ? "+" : "-");
1305
Dan Stoza71433162014-02-04 16:22:36 -08001306 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001307
1308 if (mustRecompose) {
1309 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1310 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001311 }
1312
Dan Stoza9e56aa02015-11-02 13:00:03 -08001313 // build the h/w work list
1314 if (CC_UNLIKELY(mGeometryInvalid)) {
1315 mGeometryInvalid = false;
1316 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1317 sp<const DisplayDevice> displayDevice(mDisplays[dpy]);
1318 const auto hwcId = displayDevice->getHwcDisplayId();
1319 if (hwcId >= 0) {
1320 const Vector<sp<Layer>>& currentLayers(
1321 displayDevice->getVisibleLayersSortedByZ());
1322 bool foundLayerWithoutHwc = false;
1323 for (auto& layer : currentLayers) {
1324 if (!layer->hasHwcLayer(hwcId)) {
1325 auto hwcLayer = mHwc->createLayer(hwcId);
1326 if (hwcLayer) {
1327 layer->setHwcLayer(hwcId, std::move(hwcLayer));
1328 } else {
1329 layer->forceClientComposition(hwcId);
1330 foundLayerWithoutHwc = true;
1331 continue;
Jamie Gennisa4310c82012-09-25 20:26:00 -07001332 }
1333 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001334
Dan Stoza9e56aa02015-11-02 13:00:03 -08001335 layer->setGeometry(displayDevice);
Dan Stoza9f26a9c2016-06-22 14:51:09 -07001336 if (mDebugDisableHWC || mDebugRegion) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001337 layer->forceClientComposition(hwcId);
Riley Andrews03414a12014-07-01 14:22:59 -07001338 }
1339 }
1340 }
1341 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001342 }
Riley Andrews03414a12014-07-01 14:22:59 -07001343
Dan Stoza9f26a9c2016-06-22 14:51:09 -07001344
1345 mat4 colorMatrix = mColorMatrix * mDaltonizer();
1346
Dan Stoza9e56aa02015-11-02 13:00:03 -08001347 // Set the per-frame data
1348 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
1349 auto& displayDevice = mDisplays[displayId];
1350 const auto hwcId = displayDevice->getHwcDisplayId();
1351 if (hwcId < 0) {
1352 continue;
Jesse Hall38efe862013-04-06 23:12:29 -07001353 }
Dan Stoza9f26a9c2016-06-22 14:51:09 -07001354 if (colorMatrix != mPreviousColorMatrix) {
1355 status_t result = mHwc->setColorTransform(hwcId, colorMatrix);
1356 ALOGE_IF(result != NO_ERROR, "Failed to set color transform on "
1357 "display %zd: %d", displayId, result);
1358 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001359 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
1360 layer->setPerFrameData(displayDevice);
1361 }
1362 }
1363
Dan Stoza9f26a9c2016-06-22 14:51:09 -07001364 mPreviousColorMatrix = colorMatrix;
1365
Dan Stoza9e56aa02015-11-02 13:00:03 -08001366 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
Dan Stoza7bdf55a2016-06-17 11:29:01 -07001367 auto& displayDevice = mDisplays[displayId];
1368 if (!displayDevice->isDisplayOn()) {
1369 continue;
1370 }
1371
1372 status_t result = displayDevice->prepareFrame(*mHwc);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001373 ALOGE_IF(result != NO_ERROR, "prepareFrame for display %zd failed:"
1374 " %d (%s)", displayId, result, strerror(-result));
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001375 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001376}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001377
Mathias Agopiancd60f992012-08-16 16:28:27 -07001378void SurfaceFlinger::doComposition() {
1379 ATRACE_CALL();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001380 ALOGV("doComposition");
1381
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001382 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001383 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001384 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001385 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001386 // transform the dirty region into this screen's coordinate space
1387 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001388
1389 // repaint the framebuffer (if needed)
1390 doDisplayComposition(hw, dirtyRegion);
1391
Mathias Agopiancd60f992012-08-16 16:28:27 -07001392 hw->dirtyRegion.clear();
1393 hw->flip(hw->swapRegion);
1394 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001395 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001396 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001397 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001398}
1399
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001400void SurfaceFlinger::postFramebuffer()
1401{
Mathias Agopian841cde52012-03-01 15:44:37 -08001402 ATRACE_CALL();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001403 ALOGV("postFramebuffer");
Mathias Agopianb048cef2012-02-04 15:44:04 -08001404
Mathias Agopiana44b0412011-10-16 18:46:35 -07001405 const nsecs_t now = systemTime();
1406 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001407
Dan Stoza9e56aa02015-11-02 13:00:03 -08001408 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
1409 auto& displayDevice = mDisplays[displayId];
Dan Stoza7bdf55a2016-06-17 11:29:01 -07001410 if (!displayDevice->isDisplayOn()) {
1411 continue;
1412 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001413 const auto hwcId = displayDevice->getHwcDisplayId();
1414 if (hwcId >= 0) {
1415 mHwc->commit(hwcId);
Mathias Agopian2a231842012-09-24 18:12:35 -07001416 }
Dan Stoza2dc3be82016-04-06 14:05:37 -07001417 displayDevice->onSwapBuffersCompleted();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001418 if (displayId == 0) {
1419 // Make the default display current because the VirtualDisplayDevice
1420 // code cannot deal with dequeueBuffer() being called outside of the
1421 // composition loop; however the code below can call glFlush() which
1422 // is allowed to (and does in some case) call dequeueBuffer().
1423 displayDevice->makeCurrent(mEGLDisplay, mEGLContext);
1424 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001425 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
1426 sp<Fence> releaseFence = Fence::NO_FENCE;
1427 if (layer->getCompositionType(hwcId) == HWC2::Composition::Client) {
1428 releaseFence = displayDevice->getClientTargetAcquireFence();
1429 } else {
1430 auto hwcLayer = layer->getHwcLayer(hwcId);
1431 releaseFence = mHwc->getLayerReleaseFence(hwcId, hwcLayer);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001432 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001433 layer->onLayerDisplayed(releaseFence);
1434 }
1435 if (hwcId >= 0) {
1436 mHwc->clearReleaseFences(hwcId);
Jesse Hallef194142012-06-14 14:45:17 -07001437 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001438 }
1439
Mathias Agopiana44b0412011-10-16 18:46:35 -07001440 mLastSwapBufferTime = systemTime() - now;
1441 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001442
1443 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1444 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1445 logFrameStats();
1446 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001447}
1448
Mathias Agopian87baae12012-07-31 12:38:26 -07001449void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001450{
Mathias Agopian841cde52012-03-01 15:44:37 -08001451 ATRACE_CALL();
1452
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001453 // here we keep a copy of the drawing state (that is the state that's
1454 // going to be overwritten by handleTransactionLocked()) outside of
1455 // mStateLock so that the side-effects of the State assignment
1456 // don't happen with mStateLock held (which can cause deadlocks).
1457 State drawingState(mDrawingState);
1458
Mathias Agopianca4d3602011-05-19 15:38:14 -07001459 Mutex::Autolock _l(mStateLock);
1460 const nsecs_t now = systemTime();
1461 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462
Mathias Agopianca4d3602011-05-19 15:38:14 -07001463 // Here we're guaranteed that some transaction flags are set
1464 // so we can call handleTransactionLocked() unconditionally.
1465 // We call getTransactionFlags(), which will also clear the flags,
1466 // with mStateLock held to guarantee that mCurrentState won't change
1467 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001468
Mathias Agopiane57f2922012-08-09 16:29:12 -07001469 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001470 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001471
Mathias Agopianca4d3602011-05-19 15:38:14 -07001472 mLastTransactionTime = systemTime() - now;
1473 mDebugInTransaction = 0;
1474 invalidateHwcGeometry();
1475 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001476}
1477
Mathias Agopian87baae12012-07-31 12:38:26 -07001478void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001479{
1480 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481 const size_t count = currentLayers.size();
1482
Dan Stoza7dde5992015-05-22 09:51:44 -07001483 // Notify all layers of available frames
1484 for (size_t i = 0; i < count; ++i) {
1485 currentLayers[i]->notifyAvailableFrames();
1486 }
1487
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001488 /*
1489 * Traversal of the children
1490 * (perform the transaction for each of them if needed)
1491 */
1492
Mathias Agopian3559b072012-08-15 13:46:03 -07001493 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001494 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001495 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001496 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1497 if (!trFlags) continue;
1498
1499 const uint32_t flags = layer->doTransaction(0);
1500 if (flags & Layer::eVisibleRegion)
1501 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001502 }
1503 }
1504
1505 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001506 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001507 */
1508
Mathias Agopiane57f2922012-08-09 16:29:12 -07001509 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001510 // here we take advantage of Vector's copy-on-write semantics to
1511 // improve performance by skipping the transaction entirely when
1512 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001513 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1514 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001515 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001516 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001517 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001518 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001519
1520 // find the displays that were removed
1521 // (ie: in drawing state but not in current state)
1522 // also handle displays that changed
1523 // (ie: displays that are in both lists)
1524 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001525 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1526 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001527 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001528 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001529 // Call makeCurrent() on the primary display so we can
1530 // be sure that nothing associated with this display
1531 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001532 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001533 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001534 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1535 if (hw != NULL)
1536 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001537 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001538 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001539 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001540 } else {
1541 ALOGW("trying to remove the main display");
1542 }
1543 } else {
1544 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001545 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001546 const wp<IBinder>& display(curr.keyAt(j));
Marco Nelissen097ca272014-11-14 08:01:01 -08001547 const sp<IBinder> state_binder = IInterface::asBinder(state.surface);
1548 const sp<IBinder> draw_binder = IInterface::asBinder(draw[i].surface);
Dan Albert1474f882014-09-08 18:59:09 -07001549 if (state_binder != draw_binder) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001550 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001551 // recreating the DisplayDevice, so we just remove it
1552 // from the drawing state, so that it get re-added
1553 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001554 sp<DisplayDevice> hw(getDisplayDevice(display));
1555 if (hw != NULL)
1556 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001557 mDisplays.removeItem(display);
1558 mDrawingState.displays.removeItemsAt(i);
1559 dc--; i--;
1560 // at this point we must loop to the next item
1561 continue;
1562 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001563
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001564 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001565 if (disp != NULL) {
1566 if (state.layerStack != draw[i].layerStack) {
1567 disp->setLayerStack(state.layerStack);
1568 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001569 if ((state.orientation != draw[i].orientation)
1570 || (state.viewport != draw[i].viewport)
1571 || (state.frame != draw[i].frame))
1572 {
1573 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001574 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001575 }
Michael Lentine47e45402014-07-18 15:34:25 -07001576 if (state.width != draw[i].width || state.height != draw[i].height) {
1577 disp->setDisplaySize(state.width, state.height);
1578 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001579 }
1580 }
1581 }
1582
1583 // find displays that were added
1584 // (ie: in current state but not in drawing state)
1585 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001586 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001587 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001588
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001589 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001590 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001591 sp<IGraphicBufferProducer> bqProducer;
1592 sp<IGraphicBufferConsumer> bqConsumer;
Dan Stoza70982a52016-01-11 23:40:44 +00001593 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1594 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001595
Dan Stoza9e56aa02015-11-02 13:00:03 -08001596 int32_t hwcId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001597 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001598 // Virtual displays without a surface are dormant:
1599 // they have external state (layer stack, projection,
1600 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001601 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001602
Dan Stoza1f3efb12014-10-15 16:34:55 -07001603 int width = 0;
1604 int status = state.surface->query(
1605 NATIVE_WINDOW_WIDTH, &width);
1606 ALOGE_IF(status != NO_ERROR,
1607 "Unable to query width (%d)", status);
1608 int height = 0;
1609 status = state.surface->query(
1610 NATIVE_WINDOW_HEIGHT, &height);
1611 ALOGE_IF(status != NO_ERROR,
1612 "Unable to query height (%d)", status);
Dan Stoza5cf424b2016-05-20 14:02:39 -07001613 int intFormat = 0;
1614 status = state.surface->query(
1615 NATIVE_WINDOW_FORMAT, &intFormat);
1616 ALOGE_IF(status != NO_ERROR,
1617 "Unable to query format (%d)", status);
1618 auto format = static_cast<android_pixel_format_t>(
1619 intFormat);
Dan Stoza1f3efb12014-10-15 16:34:55 -07001620
Dan Stoza5cf424b2016-05-20 14:02:39 -07001621 mHwc->allocateVirtualDisplay(width, height, &format,
Dan Stoza9e56aa02015-11-02 13:00:03 -08001622 &hwcId);
1623
Dan Stoza5cf424b2016-05-20 14:02:39 -07001624 // TODO: Plumb requested format back up to consumer
1625
Dan Stoza9e56aa02015-11-02 13:00:03 -08001626 sp<VirtualDisplaySurface> vds =
1627 new VirtualDisplaySurface(*mHwc,
1628 hwcId, state.surface, bqProducer,
1629 bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001630
1631 dispSurface = vds;
Michael Lentine47e45402014-07-18 15:34:25 -07001632 producer = vds;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001633 }
1634 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001635 ALOGE_IF(state.surface!=NULL,
1636 "adding a supported display, but rendering "
1637 "surface is provided (%p), ignoring it",
1638 state.surface.get());
Dan Stoza9e56aa02015-11-02 13:00:03 -08001639 if (state.type == DisplayDevice::DISPLAY_EXTERNAL) {
1640 hwcId = DisplayDevice::DISPLAY_EXTERNAL;
1641 dispSurface = new FramebufferSurface(*mHwc,
1642 DisplayDevice::DISPLAY_EXTERNAL,
1643 bqConsumer);
1644 producer = bqProducer;
1645 } else {
1646 ALOGE("Attempted to add non-external non-virtual"
1647 " display");
1648 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07001649 }
1650
1651 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001652 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001653 sp<DisplayDevice> hw = new DisplayDevice(this,
Dan Stoza9e56aa02015-11-02 13:00:03 -08001654 state.type, hwcId, state.isSecure, display,
1655 dispSurface, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -08001656 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001657 hw->setLayerStack(state.layerStack);
1658 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001659 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001660 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001661 mDisplays.add(display, hw);
Dan Stoza9e56aa02015-11-02 13:00:03 -08001662 if (!state.isVirtualDisplay()) {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001663 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001664 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001665 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001666 }
1667 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001668 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001669 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001670
Mathias Agopian84300952012-11-21 16:02:13 -08001671 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1672 // The transform hint might have changed for some layers
1673 // (either because a display has changed, or because a layer
1674 // as changed).
1675 //
1676 // Walk through all the layers in currentLayers,
1677 // and update their transform hint.
1678 //
1679 // If a layer is visible only on a single display, then that
1680 // display is used to calculate the hint, otherwise we use the
1681 // default display.
1682 //
1683 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1684 // the hint is set before we acquire a buffer from the surface texture.
1685 //
1686 // NOTE: layer transactions have taken place already, so we use their
1687 // drawing state. However, SurfaceFlinger's own transaction has not
1688 // happened yet, so we must use the current state layer list
1689 // (soon to become the drawing state list).
1690 //
1691 sp<const DisplayDevice> disp;
1692 uint32_t currentlayerStack = 0;
1693 for (size_t i=0; i<count; i++) {
1694 // NOTE: we rely on the fact that layers are sorted by
1695 // layerStack first (so we don't have to traverse the list
1696 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001697 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001698 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001699 if (i==0 || currentlayerStack != layerStack) {
1700 currentlayerStack = layerStack;
1701 // figure out if this layerstack is mirrored
1702 // (more than one display) if so, pick the default display,
1703 // if not, pick the only display it's on.
1704 disp.clear();
1705 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1706 sp<const DisplayDevice> hw(mDisplays[dpy]);
1707 if (hw->getLayerStack() == currentlayerStack) {
1708 if (disp == NULL) {
1709 disp = hw;
1710 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001711 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001712 break;
1713 }
1714 }
1715 }
1716 }
Chet Haase91d25932013-04-11 15:24:55 -07001717 if (disp == NULL) {
1718 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1719 // redraw after transform hint changes. See bug 8508397.
1720
1721 // could be null when this layer is using a layerStack
1722 // that is not visible on any display. Also can occur at
1723 // screen off/on times.
1724 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001725 }
Chet Haase91d25932013-04-11 15:24:55 -07001726 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001727 }
1728 }
1729
1730
Mathias Agopian3559b072012-08-15 13:46:03 -07001731 /*
1732 * Perform our own transaction if needed
1733 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001734
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001735 const LayerVector& layers(mDrawingState.layersSortedByZ);
1736 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001737 // layers have been added
1738 mVisibleRegionsDirty = true;
1739 }
1740
1741 // some layers might have been removed, so
1742 // we need to update the regions they're exposing.
1743 if (mLayersRemoved) {
1744 mLayersRemoved = false;
1745 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001746 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001747 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001748 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001749 if (currentLayers.indexOf(layer) < 0) {
1750 // this layer is not visible anymore
1751 // TODO: we could traverse the tree from front to back and
1752 // compute the actual visible region
1753 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001754 const Layer::State& s(layer->getDrawingState());
Robert Carr3dcabfa2016-03-01 18:36:58 -08001755 Region visibleReg = s.active.transform.transform(
Mathias Agopian1501d542012-09-04 21:04:09 -07001756 Region(Rect(s.active.w, s.active.h)));
1757 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001758 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001759 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001760 }
1761
1762 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001763
1764 updateCursorAsync();
1765}
1766
1767void SurfaceFlinger::updateCursorAsync()
1768{
Dan Stoza9e56aa02015-11-02 13:00:03 -08001769 for (size_t displayId = 0; displayId < mDisplays.size(); ++displayId) {
1770 auto& displayDevice = mDisplays[displayId];
1771 if (displayDevice->getHwcDisplayId() < 0) {
Riley Andrews03414a12014-07-01 14:22:59 -07001772 continue;
1773 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001774
1775 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
1776 layer->updateCursorPosition(displayDevice);
Riley Andrews03414a12014-07-01 14:22:59 -07001777 }
1778 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001779}
1780
1781void SurfaceFlinger::commitTransaction()
1782{
1783 if (!mLayersPendingRemoval.isEmpty()) {
1784 // Notify removed layers now that they can't be drawn from
1785 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
Dan Stozae77c7662016-05-13 11:37:28 -07001786 recordBufferingStats(mLayersPendingRemoval[i]->getName().string(),
1787 mLayersPendingRemoval[i]->getOccupancyHistory(true));
Mathias Agopian4fec8732012-06-29 14:12:52 -07001788 mLayersPendingRemoval[i]->onRemoved();
1789 }
1790 mLayersPendingRemoval.clear();
1791 }
1792
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001793 // If this transaction is part of a window animation then the next frame
1794 // we composite should be considered an animation as well.
1795 mAnimCompositionPending = mAnimTransactionPending;
1796
Mathias Agopian4fec8732012-06-29 14:12:52 -07001797 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001798 mTransactionPending = false;
1799 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001800 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001801}
1802
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001803void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001804 const LayerVector& currentLayers, uint32_t layerStack,
1805 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001806{
Mathias Agopian841cde52012-03-01 15:44:37 -08001807 ATRACE_CALL();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001808 ALOGV("computeVisibleRegions");
Mathias Agopian841cde52012-03-01 15:44:37 -08001809
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001810 Region aboveOpaqueLayers;
1811 Region aboveCoveredLayers;
1812 Region dirty;
1813
Mathias Agopian87baae12012-07-31 12:38:26 -07001814 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001815
1816 size_t i = currentLayers.size();
1817 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001818 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001819
1820 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001821 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001822
Jesse Hall01e29052013-02-19 16:13:35 -08001823 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001824 if (s.layerStack != layerStack)
1825 continue;
1826
Mathias Agopianab028732010-03-16 16:41:46 -07001827 /*
1828 * opaqueRegion: area of a surface that is fully opaque.
1829 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001830 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001831
1832 /*
1833 * visibleRegion: area of a surface that is visible on screen
1834 * and not fully transparent. This is essentially the layer's
1835 * footprint minus the opaque regions above it.
1836 * Areas covered by a translucent surface are considered visible.
1837 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001838 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001839
1840 /*
1841 * coveredRegion: area of a surface that is covered by all
1842 * visible regions above it (which includes the translucent areas).
1843 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001844 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001845
Jesse Halla8026d22012-09-25 13:25:04 -07001846 /*
1847 * transparentRegion: area of a surface that is hinted to be completely
1848 * transparent. This is only used to tell when the layer has no visible
1849 * non-transparent regions and can be removed from the layer list. It
1850 * does not affect the visibleRegion of this layer or any layers
1851 * beneath it. The hint may not be correct if apps don't respect the
1852 * SurfaceView restrictions (which, sadly, some don't).
1853 */
1854 Region transparentRegion;
1855
Mathias Agopianab028732010-03-16 16:41:46 -07001856
1857 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001858 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001859 const bool translucent = !layer->isOpaque(s);
Robert Carr3dcabfa2016-03-01 18:36:58 -08001860 Rect bounds(s.active.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001861 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001862 if (!visibleRegion.isEmpty()) {
1863 // Remove the transparent area from the visible region
1864 if (translucent) {
Robert Carr3dcabfa2016-03-01 18:36:58 -08001865 const Transform tr(s.active.transform);
Dan Stoza22f7fc42016-05-10 16:19:53 -07001866 if (tr.preserveRects()) {
1867 // transform the transparent region
1868 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001869 } else {
Dan Stoza22f7fc42016-05-10 16:19:53 -07001870 // transformation too complex, can't do the
1871 // transparent region optimization.
1872 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001873 }
Mathias Agopianab028732010-03-16 16:41:46 -07001874 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001875
Mathias Agopianab028732010-03-16 16:41:46 -07001876 // compute the opaque region
Robert Carr3dcabfa2016-03-01 18:36:58 -08001877 const int32_t layerOrientation = s.active.transform.getOrientation();
Dan Stoza9e56aa02015-11-02 13:00:03 -08001878 if (s.alpha == 1.0f && !translucent &&
Mathias Agopianab028732010-03-16 16:41:46 -07001879 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1880 // the opaque region is the layer's footprint
1881 opaqueRegion = visibleRegion;
1882 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001883 }
1884 }
1885
Mathias Agopianab028732010-03-16 16:41:46 -07001886 // Clip the covered region to the visible region
1887 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1888
1889 // Update aboveCoveredLayers for next (lower) layer
1890 aboveCoveredLayers.orSelf(visibleRegion);
1891
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001892 // subtract the opaque region covered by the layers above us
1893 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001894
1895 // compute this layer's dirty region
1896 if (layer->contentDirty) {
1897 // we need to invalidate the whole region
1898 dirty = visibleRegion;
1899 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001900 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001901 layer->contentDirty = false;
1902 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001903 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001904 * the exposed region consists of two components:
1905 * 1) what's VISIBLE now and was COVERED before
1906 * 2) what's EXPOSED now less what was EXPOSED before
1907 *
1908 * note that (1) is conservative, we start with the whole
1909 * visible region but only keep what used to be covered by
1910 * something -- which mean it may have been exposed.
1911 *
1912 * (2) handles areas that were not covered by anything but got
1913 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001914 */
Mathias Agopianab028732010-03-16 16:41:46 -07001915 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001916 const Region oldVisibleRegion = layer->visibleRegion;
1917 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001918 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1919 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001920 }
1921 dirty.subtractSelf(aboveOpaqueLayers);
1922
1923 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001924 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001925
Mathias Agopianab028732010-03-16 16:41:46 -07001926 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001927 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001928
Jesse Halla8026d22012-09-25 13:25:04 -07001929 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001930 layer->setVisibleRegion(visibleRegion);
1931 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001932 layer->setVisibleNonTransparentRegion(
1933 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001934 }
1935
Mathias Agopian87baae12012-07-31 12:38:26 -07001936 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001937}
1938
Mathias Agopian87baae12012-07-31 12:38:26 -07001939void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1940 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001941 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001942 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1943 if (hw->getLayerStack() == layerStack) {
1944 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001945 }
1946 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001947}
1948
Dan Stoza6b9454d2014-11-07 16:00:59 -08001949bool SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001950{
Dan Stoza9e56aa02015-11-02 13:00:03 -08001951 ALOGV("handlePageFlip");
1952
Mathias Agopian4fec8732012-06-29 14:12:52 -07001953 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001954
Mathias Agopian4fec8732012-06-29 14:12:52 -07001955 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001956 const LayerVector& layers(mDrawingState.layersSortedByZ);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001957 bool frameQueued = false;
Eric Penner51c59cd2014-07-28 19:51:58 -07001958
1959 // Store the set of layers that need updates. This set must not change as
1960 // buffers are being latched, as this could result in a deadlock.
1961 // Example: Two producers share the same command stream and:
1962 // 1.) Layer 0 is latched
1963 // 2.) Layer 0 gets a new frame
1964 // 2.) Layer 1 gets a new frame
1965 // 3.) Layer 1 is latched.
1966 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1967 // second frame. But layer 0's second frame could be waiting on display.
Eric Penner51c59cd2014-07-28 19:51:58 -07001968 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001969 const sp<Layer>& layer(layers[i]);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001970 if (layer->hasQueuedFrame()) {
1971 frameQueued = true;
1972 if (layer->shouldPresentNow(mPrimaryDispSync)) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08001973 mLayersWithQueuedFrames.push_back(layer.get());
Dan Stozaee44edd2015-03-23 15:50:23 -07001974 } else {
1975 layer->useEmptyDamage();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001976 }
Dan Stozaee44edd2015-03-23 15:50:23 -07001977 } else {
1978 layer->useEmptyDamage();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001979 }
Eric Penner51c59cd2014-07-28 19:51:58 -07001980 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08001981 for (auto& layer : mLayersWithQueuedFrames) {
Mathias Agopian87baae12012-07-31 12:38:26 -07001982 const Region dirty(layer->latchBuffer(visibleRegions));
Dan Stozaee44edd2015-03-23 15:50:23 -07001983 layer->useSurfaceDamage();
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001984 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001985 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001986 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001987
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001988 mVisibleRegionsDirty |= visibleRegions;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001989
1990 // If we will need to wake up at some time in the future to deal with a
1991 // queued frame that shouldn't be displayed during this vsync period, wake
1992 // up during the next vsync period to check again.
Dan Stoza9e56aa02015-11-02 13:00:03 -08001993 if (frameQueued && mLayersWithQueuedFrames.empty()) {
Dan Stoza6b9454d2014-11-07 16:00:59 -08001994 signalLayerUpdate();
1995 }
1996
1997 // Only continue with the refresh if there is actually new work to do
Dan Stoza9e56aa02015-11-02 13:00:03 -08001998 return !mLayersWithQueuedFrames.empty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001999}
2000
Mathias Agopianad456f92011-01-13 17:53:01 -08002001void SurfaceFlinger::invalidateHwcGeometry()
2002{
Dan Stoza9e56aa02015-11-02 13:00:03 -08002003 mGeometryInvalid = true;
Mathias Agopianad456f92011-01-13 17:53:01 -08002004}
2005
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002006
Mathias Agopiancd60f992012-08-16 16:28:27 -07002007void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07002008 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002009{
Dan Stoza71433162014-02-04 16:22:36 -08002010 // We only need to actually compose the display if:
2011 // 1) It is being handled by hardware composer, which may need this to
2012 // keep its virtual display state machine in sync, or
2013 // 2) There is work to be done (the dirty region isn't empty)
2014 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
2015 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08002016 ALOGV("Skipping display composition");
Dan Stoza71433162014-02-04 16:22:36 -08002017 return;
2018 }
2019
Dan Stoza9e56aa02015-11-02 13:00:03 -08002020 ALOGV("doDisplayComposition");
2021
Mathias Agopian87baae12012-07-31 12:38:26 -07002022 Region dirtyRegion(inDirtyRegion);
2023
Mathias Agopianb8a55602009-06-26 19:06:36 -07002024 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07002025 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002026
Mathias Agopian42977342012-08-05 00:40:46 -07002027 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002028 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002029 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
2030 // takes a rectangle, we must make sure to update that whole
2031 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07002032 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002033 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002034 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002035 // We need to redraw the rectangle that will be updated
2036 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07002037 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002038 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07002039 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002040 } else {
2041 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07002042 dirtyRegion.set(hw->bounds());
2043 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002044 }
2045 }
2046
Dan Stoza9f26a9c2016-06-22 14:51:09 -07002047 if (!doComposeSurfaces(hw, dirtyRegion)) return;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002048
Mathias Agopian9c6e2972011-09-20 17:21:56 -07002049 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07002050 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07002051
2052 // swap buffers (presentation)
2053 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002054}
2055
Dan Stoza9e56aa02015-11-02 13:00:03 -08002056bool SurfaceFlinger::doComposeSurfaces(
2057 const sp<const DisplayDevice>& displayDevice, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07002058{
Dan Stoza9e56aa02015-11-02 13:00:03 -08002059 ALOGV("doComposeSurfaces");
Mathias Agopiancd20eb02011-09-22 20:57:04 -07002060
Dan Stoza9e56aa02015-11-02 13:00:03 -08002061 const auto hwcId = displayDevice->getHwcDisplayId();
Dan Stoza9f26a9c2016-06-22 14:51:09 -07002062
2063 mat4 oldColorMatrix;
2064 const bool applyColorMatrix = !mHwc->hasDeviceComposition(hwcId) &&
2065 !mHwc->hasCapability(HWC2::Capability::SkipClientColorTransform);
2066 if (applyColorMatrix) {
2067 mat4 colorMatrix = mColorMatrix * mDaltonizer();
2068 oldColorMatrix = getRenderEngine().setupColorTransform(colorMatrix);
2069 }
2070
Dan Stoza9e56aa02015-11-02 13:00:03 -08002071 bool hasClientComposition = mHwc->hasClientComposition(hwcId);
2072 if (hasClientComposition) {
2073 ALOGV("hasClientComposition");
2074
2075 if (!displayDevice->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08002076 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
Dan Stoza9e56aa02015-11-02 13:00:03 -08002077 displayDevice->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07002078 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
2079 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
2080 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
2081 }
2082 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08002083 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07002084
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07002085 // Never touch the framebuffer if we don't have any framebuffer layers
Dan Stoza9e56aa02015-11-02 13:00:03 -08002086 const bool hasDeviceComposition = mHwc->hasDeviceComposition(hwcId);
2087 if (hasDeviceComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07002088 // when using overlays, we assume a fully transparent framebuffer
2089 // NOTE: we could reduce how much we need to clear, for instance
2090 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07002091 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07002092 // We'll revisit later if needed.
Dan Stoza9e56aa02015-11-02 13:00:03 -08002093 mRenderEngine->clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002094 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07002095 // we start with the whole screen area
Dan Stoza9e56aa02015-11-02 13:00:03 -08002096 const Region bounds(displayDevice->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07002097
2098 // we remove the scissor part
2099 // we're left with the letterbox region
2100 // (common case is that letterbox ends-up being empty)
Dan Stoza9e56aa02015-11-02 13:00:03 -08002101 const Region letterbox(bounds.subtract(displayDevice->getScissor()));
Mathias Agopian766dc492012-10-30 18:08:06 -07002102
2103 // compute the area to clear
Dan Stoza9e56aa02015-11-02 13:00:03 -08002104 Region region(displayDevice->undefinedRegion.merge(letterbox));
Mathias Agopian766dc492012-10-30 18:08:06 -07002105
2106 // but limit it to the dirty region
2107 region.andSelf(dirty);
2108
Mathias Agopianb9494d52012-04-18 02:28:45 -07002109 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07002110 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07002111 // can happen with SurfaceView
Dan Stoza9e56aa02015-11-02 13:00:03 -08002112 drawWormhole(displayDevice, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002113 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002114 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002115
Dan Stoza9e56aa02015-11-02 13:00:03 -08002116 if (displayDevice->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopian766dc492012-10-30 18:08:06 -07002117 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07002118 // scissor on the main display. It should never be needed
2119 // anyways (though in theory it could since the API allows it).
Dan Stoza9e56aa02015-11-02 13:00:03 -08002120 const Rect& bounds(displayDevice->getBounds());
2121 const Rect& scissor(displayDevice->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002122 if (scissor != bounds) {
2123 // scissor doesn't match the screen's dimensions, so we
2124 // need to clear everything outside of it and enable
2125 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07002126
Mathias Agopianf45c5102012-10-24 16:29:17 -07002127 // enable scissor for this frame
Dan Stoza9e56aa02015-11-02 13:00:03 -08002128 const uint32_t height = displayDevice->getHeight();
2129 mRenderEngine->setScissor(scissor.left, height - scissor.bottom,
Mathias Agopian3f844832013-08-07 21:24:32 -07002130 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002131 }
2132 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002133 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002134
Mathias Agopian85d751c2012-08-29 16:59:24 -07002135 /*
2136 * and then, render the layers targeted at the framebuffer
2137 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002138
Dan Stoza9e56aa02015-11-02 13:00:03 -08002139 ALOGV("Rendering client layers");
2140 const Transform& displayTransform = displayDevice->getTransform();
2141 if (hwcId >= 0) {
Mathias Agopian85d751c2012-08-29 16:59:24 -07002142 // we're using h/w composer
Dan Stoza9e56aa02015-11-02 13:00:03 -08002143 bool firstLayer = true;
2144 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
2145 const Region clip(dirty.intersect(
2146 displayTransform.transform(layer->visibleRegion)));
2147 ALOGV("Layer: %s", layer->getName().string());
2148 ALOGV(" Composition type: %s",
2149 to_string(layer->getCompositionType(hwcId)).c_str());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002150 if (!clip.isEmpty()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08002151 switch (layer->getCompositionType(hwcId)) {
2152 case HWC2::Composition::Cursor:
2153 case HWC2::Composition::Device:
2154 case HWC2::Composition::SolidColor: {
Mathias Agopianac683022013-10-01 15:36:52 -07002155 const Layer::State& state(layer->getDrawingState());
Dan Stoza9e56aa02015-11-02 13:00:03 -08002156 if (layer->getClearClientTarget(hwcId) && !firstLayer &&
2157 layer->isOpaque(state) && (state.alpha == 1.0f)
2158 && hasClientComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07002159 // never clear the very first layer since we're
2160 // guaranteed the FB is already cleared
Dan Stoza9e56aa02015-11-02 13:00:03 -08002161 layer->clearWithOpenGL(displayDevice, clip);
Mathias Agopiancd60f992012-08-16 16:28:27 -07002162 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002163 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07002164 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08002165 case HWC2::Composition::Client: {
2166 layer->draw(displayDevice, clip);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002167 break;
2168 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08002169 default:
Mathias Agopianda27af92012-09-13 18:17:13 -07002170 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07002171 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08002172 } else {
2173 ALOGV(" Skipping for empty clip");
Mathias Agopian85d751c2012-08-29 16:59:24 -07002174 }
Dan Stoza9e56aa02015-11-02 13:00:03 -08002175 firstLayer = false;
Mathias Agopian85d751c2012-08-29 16:59:24 -07002176 }
2177 } else {
2178 // we're not using h/w composer
Dan Stoza9e56aa02015-11-02 13:00:03 -08002179 for (auto& layer : displayDevice->getVisibleLayersSortedByZ()) {
Mathias Agopian85d751c2012-08-29 16:59:24 -07002180 const Region clip(dirty.intersect(
Dan Stoza9e56aa02015-11-02 13:00:03 -08002181 displayTransform.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07002182 if (!clip.isEmpty()) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08002183 layer->draw(displayDevice, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07002184 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002185 }
2186 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002187
Dan Stoza9f26a9c2016-06-22 14:51:09 -07002188 if (applyColorMatrix) {
2189 getRenderEngine().setupColorTransform(oldColorMatrix);
2190 }
2191
Mathias Agopianf45c5102012-10-24 16:29:17 -07002192 // disable scissor at the end of the frame
Dan Stoza9e56aa02015-11-02 13:00:03 -08002193 mRenderEngine->disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07002194 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002195}
2196
Mathias Agopian3f844832013-08-07 21:24:32 -07002197void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07002198 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07002199 RenderEngine& engine(getRenderEngine());
2200 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002201}
2202
Dan Stoza7d89d062015-04-30 13:29:25 -07002203status_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -08002204 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07002205 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08002206 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07002207{
Dan Stoza7d89d062015-04-30 13:29:25 -07002208 // add this layer to the current state list
2209 {
2210 Mutex::Autolock _l(mStateLock);
2211 if (mCurrentState.layersSortedByZ.size() >= MAX_LAYERS) {
2212 return NO_MEMORY;
2213 }
2214 mCurrentState.layersSortedByZ.add(lbc);
2215 mGraphicBufferProducerList.add(IInterface::asBinder(gbc));
2216 }
2217
Mathias Agopian96f08192010-06-02 23:28:45 -07002218 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08002219 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07002220
Dan Stoza7d89d062015-04-30 13:29:25 -07002221 return NO_ERROR;
Mathias Agopian96f08192010-06-02 23:28:45 -07002222}
2223
Mathias Agopian3f844832013-08-07 21:24:32 -07002224status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002225 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08002226 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002227 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07002228 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002229 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07002230 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002231 return NO_ERROR;
2232 }
Mathias Agopian3d579642009-06-04 18:46:21 -07002233 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002234}
2235
Dan Stozac7014012014-02-14 15:03:43 -08002236uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07002237 return android_atomic_release_load(&mTransactionFlags);
2238}
2239
Mathias Agopian3f844832013-08-07 21:24:32 -07002240uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002241 return android_atomic_and(~flags, &mTransactionFlags) & flags;
2242}
2243
Mathias Agopian3f844832013-08-07 21:24:32 -07002244uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002245 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
2246 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002247 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002248 }
2249 return old;
2250}
2251
Mathias Agopian8b33f032012-07-24 20:43:54 -07002252void SurfaceFlinger::setTransactionState(
2253 const Vector<ComposerState>& state,
2254 const Vector<DisplayState>& displays,
2255 uint32_t flags)
2256{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002257 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07002258 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07002259 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002260
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002261 if (flags & eAnimation) {
2262 // For window updates that are part of an animation we must wait for
2263 // previous animation "frames" to be handled.
2264 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002265 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002266 if (CC_UNLIKELY(err != NO_ERROR)) {
2267 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002268 // caller after a few seconds.
2269 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
2270 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002271 mAnimTransactionPending = false;
2272 break;
2273 }
2274 }
2275 }
2276
Mathias Agopiane57f2922012-08-09 16:29:12 -07002277 size_t count = displays.size();
2278 for (size_t i=0 ; i<count ; i++) {
2279 const DisplayState& s(displays[i]);
2280 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002281 }
2282
Mathias Agopiane57f2922012-08-09 16:29:12 -07002283 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002284 for (size_t i=0 ; i<count ; i++) {
2285 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002286 // Here we need to check that the interface we're given is indeed
2287 // one of our own. A malicious client could give us a NULL
2288 // IInterface, or one of its own or even one of our own but a
2289 // different type. All these situations would cause us to crash.
2290 //
2291 // NOTE: it would be better to use RTTI as we could directly check
2292 // that we have a Client*. however, RTTI is disabled in Android.
2293 if (s.client != NULL) {
Marco Nelissen097ca272014-11-14 08:01:01 -08002294 sp<IBinder> binder = IInterface::asBinder(s.client);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002295 if (binder != NULL) {
2296 String16 desc(binder->getInterfaceDescriptor());
2297 if (desc == ISurfaceComposerClient::descriptor) {
2298 sp<Client> client( static_cast<Client *>(s.client.get()) );
2299 transactionFlags |= setClientStateLocked(client, s.state);
2300 }
2301 }
2302 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002303 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002304
Robert Carr2a7dbb42016-05-24 11:41:28 -07002305 // If a synchronous transaction is explicitly requested without any changes,
2306 // force a transaction anyway. This can be used as a flush mechanism for
2307 // previous async transactions.
2308 if (transactionFlags == 0 && (flags & eSynchronous)) {
2309 transactionFlags = eTransactionNeeded;
2310 }
2311
Mathias Agopian386aa982011-11-07 21:58:03 -08002312 if (transactionFlags) {
2313 // this triggers the transaction
2314 setTransactionFlags(transactionFlags);
2315
2316 // if this is a synchronous transaction, wait for it to take effect
2317 // before returning.
2318 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002319 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002320 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002321 if (flags & eAnimation) {
2322 mAnimTransactionPending = true;
2323 }
2324 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002325 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2326 if (CC_UNLIKELY(err != NO_ERROR)) {
2327 // just in case something goes wrong in SF, return to the
2328 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002329 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2330 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002331 break;
2332 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002333 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002334 }
2335}
2336
Mathias Agopiane57f2922012-08-09 16:29:12 -07002337uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2338{
Jesse Hall9a143922012-10-04 16:29:19 -07002339 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2340 if (dpyIdx < 0)
2341 return 0;
2342
Mathias Agopiane57f2922012-08-09 16:29:12 -07002343 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002344 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002345 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002346 const uint32_t what = s.what;
2347 if (what & DisplayState::eSurfaceChanged) {
Marco Nelissen097ca272014-11-14 08:01:01 -08002348 if (IInterface::asBinder(disp.surface) != IInterface::asBinder(s.surface)) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002349 disp.surface = s.surface;
2350 flags |= eDisplayTransactionNeeded;
2351 }
2352 }
2353 if (what & DisplayState::eLayerStackChanged) {
2354 if (disp.layerStack != s.layerStack) {
2355 disp.layerStack = s.layerStack;
2356 flags |= eDisplayTransactionNeeded;
2357 }
2358 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002359 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002360 if (disp.orientation != s.orientation) {
2361 disp.orientation = s.orientation;
2362 flags |= eDisplayTransactionNeeded;
2363 }
2364 if (disp.frame != s.frame) {
2365 disp.frame = s.frame;
2366 flags |= eDisplayTransactionNeeded;
2367 }
2368 if (disp.viewport != s.viewport) {
2369 disp.viewport = s.viewport;
2370 flags |= eDisplayTransactionNeeded;
2371 }
2372 }
Michael Lentine47e45402014-07-18 15:34:25 -07002373 if (what & DisplayState::eDisplaySizeChanged) {
2374 if (disp.width != s.width) {
2375 disp.width = s.width;
2376 flags |= eDisplayTransactionNeeded;
2377 }
2378 if (disp.height != s.height) {
2379 disp.height = s.height;
2380 flags |= eDisplayTransactionNeeded;
2381 }
2382 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002383 }
2384 return flags;
2385}
2386
2387uint32_t SurfaceFlinger::setClientStateLocked(
2388 const sp<Client>& client,
2389 const layer_state_t& s)
2390{
2391 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002392 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002393 if (layer != 0) {
2394 const uint32_t what = s.what;
Robert Carr99e27f02016-06-16 15:18:02 -07002395 bool geometryAppliesWithResize =
2396 what & layer_state_t::eGeometryAppliesWithResize;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002397 if (what & layer_state_t::ePositionChanged) {
Robert Carr99e27f02016-06-16 15:18:02 -07002398 if (layer->setPosition(s.x, s.y, !geometryAppliesWithResize)) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002399 flags |= eTraversalNeeded;
Robert Carr82364e32016-05-15 11:27:47 -07002400 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002401 }
2402 if (what & layer_state_t::eLayerChanged) {
2403 // NOTE: index needs to be calculated before we update the state
2404 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
Pablo Ceballos47db6eb2015-12-08 13:53:59 -08002405 if (layer->setLayer(s.z) && idx >= 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002406 mCurrentState.layersSortedByZ.removeAt(idx);
2407 mCurrentState.layersSortedByZ.add(layer);
2408 // we need traversal (state changed)
2409 // AND transaction (list changed)
2410 flags |= eTransactionNeeded|eTraversalNeeded;
2411 }
2412 }
2413 if (what & layer_state_t::eSizeChanged) {
2414 if (layer->setSize(s.w, s.h)) {
2415 flags |= eTraversalNeeded;
2416 }
2417 }
2418 if (what & layer_state_t::eAlphaChanged) {
Dan Stoza9e56aa02015-11-02 13:00:03 -08002419 if (layer->setAlpha(s.alpha))
Mathias Agopiane57f2922012-08-09 16:29:12 -07002420 flags |= eTraversalNeeded;
2421 }
2422 if (what & layer_state_t::eMatrixChanged) {
2423 if (layer->setMatrix(s.matrix))
2424 flags |= eTraversalNeeded;
2425 }
2426 if (what & layer_state_t::eTransparentRegionChanged) {
2427 if (layer->setTransparentRegionHint(s.transparentRegion))
2428 flags |= eTraversalNeeded;
2429 }
Dan Stoza23116082015-06-18 14:58:39 -07002430 if (what & layer_state_t::eFlagsChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002431 if (layer->setFlags(s.flags, s.mask))
2432 flags |= eTraversalNeeded;
2433 }
2434 if (what & layer_state_t::eCropChanged) {
Robert Carr99e27f02016-06-16 15:18:02 -07002435 if (layer->setCrop(s.crop, !geometryAppliesWithResize))
Mathias Agopiane57f2922012-08-09 16:29:12 -07002436 flags |= eTraversalNeeded;
2437 }
Pablo Ceballosacbe6782016-03-04 17:54:21 +00002438 if (what & layer_state_t::eFinalCropChanged) {
2439 if (layer->setFinalCrop(s.finalCrop))
2440 flags |= eTraversalNeeded;
2441 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002442 if (what & layer_state_t::eLayerStackChanged) {
2443 // NOTE: index needs to be calculated before we update the state
2444 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
Pablo Ceballos47db6eb2015-12-08 13:53:59 -08002445 if (layer->setLayerStack(s.layerStack) && idx >= 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002446 mCurrentState.layersSortedByZ.removeAt(idx);
2447 mCurrentState.layersSortedByZ.add(layer);
2448 // we need traversal (state changed)
2449 // AND transaction (list changed)
2450 flags |= eTransactionNeeded|eTraversalNeeded;
2451 }
2452 }
Dan Stoza7dde5992015-05-22 09:51:44 -07002453 if (what & layer_state_t::eDeferTransaction) {
2454 layer->deferTransactionUntil(s.handle, s.frameNumber);
2455 // We don't trigger a traversal here because if no other state is
2456 // changed, we don't want this to cause any more work
2457 }
Robert Carrc3574f72016-03-24 12:19:32 -07002458 if (what & layer_state_t::eOverrideScalingModeChanged) {
2459 layer->setOverrideScalingMode(s.overrideScalingMode);
2460 // We don't trigger a traversal here because if no other state is
2461 // changed, we don't want this to cause any more work
2462 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002463 }
2464 return flags;
2465}
2466
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002467status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002468 const String8& name,
2469 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002470 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2471 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002472{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002473 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002474 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002475 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002476 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002477 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002478 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002479
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002480 status_t result = NO_ERROR;
2481
2482 sp<Layer> layer;
2483
Mathias Agopian3165cc22012-08-08 19:42:09 -07002484 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2485 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002486 result = createNormalLayer(client,
2487 name, w, h, flags, format,
2488 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002489 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002490 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002491 result = createDimLayer(client,
2492 name, w, h, flags,
2493 handle, gbp, &layer);
2494 break;
2495 default:
2496 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002497 break;
2498 }
2499
Dan Stoza7d89d062015-04-30 13:29:25 -07002500 if (result != NO_ERROR) {
2501 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002502 }
Dan Stoza7d89d062015-04-30 13:29:25 -07002503
2504 result = addClientLayer(client, *handle, *gbp, layer);
2505 if (result != NO_ERROR) {
2506 return result;
2507 }
2508
2509 setTransactionFlags(eTransactionNeeded);
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002510 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002511}
2512
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002513status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2514 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2515 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002516{
2517 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002518 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002519 case PIXEL_FORMAT_TRANSPARENT:
2520 case PIXEL_FORMAT_TRANSLUCENT:
2521 format = PIXEL_FORMAT_RGBA_8888;
2522 break;
2523 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002524 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002525 break;
2526 }
2527
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002528 *outLayer = new Layer(this, client, name, w, h, flags);
2529 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2530 if (err == NO_ERROR) {
2531 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002532 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002533 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002534
2535 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2536 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002537}
2538
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002539status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2540 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2541 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002542{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002543 *outLayer = new LayerDim(this, client, name, w, h, flags);
2544 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002545 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002546 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002547}
2548
Mathias Agopianac9fa422013-02-11 16:40:36 -08002549status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002550{
Mathias Agopian67106042013-03-14 19:18:13 -07002551 // called by the window manager when it wants to remove a Layer
2552 status_t err = NO_ERROR;
2553 sp<Layer> l(client->getLayerUser(handle));
2554 if (l != NULL) {
2555 err = removeLayer(l);
2556 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2557 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002558 }
2559 return err;
2560}
2561
Mathias Agopian13127d82013-03-05 17:47:11 -08002562status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002563{
Mathias Agopian67106042013-03-14 19:18:13 -07002564 // called by ~LayerCleaner() when all references to the IBinder (handle)
2565 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002566 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002567 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002568 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002569 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002570 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002571 "error removing layer=%p (%s)", l.get(), strerror(-err));
2572 }
2573 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002574}
2575
Mathias Agopianb60314a2012-04-10 22:09:54 -07002576// ---------------------------------------------------------------------------
2577
Andy McFadden13a082e2012-08-24 10:16:42 -07002578void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002579 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002580 Vector<ComposerState> state;
2581 Vector<DisplayState> displays;
2582 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002583 d.what = DisplayState::eDisplayProjectionChanged |
2584 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002585 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002586 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002587 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002588 d.frame.makeInvalid();
2589 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002590 d.width = 0;
2591 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002592 displays.add(d);
2593 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002594 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002595
Dan Stoza9e56aa02015-11-02 13:00:03 -08002596 const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
2597 const nsecs_t period = activeConfig->getVsyncPeriod();
Jamie Gennis6547ff42013-07-16 20:12:42 -07002598 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002599}
2600
2601void SurfaceFlinger::initializeDisplays() {
2602 class MessageScreenInitialized : public MessageBase {
2603 SurfaceFlinger* flinger;
2604 public:
2605 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2606 virtual bool handler() {
2607 flinger->onInitializeDisplays();
2608 return true;
2609 }
2610 };
2611 sp<MessageBase> msg = new MessageScreenInitialized(this);
2612 postMessageAsync(msg); // we may be called from main thread, use async message
2613}
2614
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002615void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2616 int mode) {
2617 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2618 this);
2619 int32_t type = hw->getDisplayType();
2620 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002621
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002622 if (mode == currentMode) {
2623 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002624 return;
2625 }
2626
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002627 hw->setPowerMode(mode);
2628 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2629 ALOGW("Trying to set power mode for virtual display");
2630 return;
2631 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002632
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002633 if (currentMode == HWC_POWER_MODE_OFF) {
2634 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002635 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2636 // FIXME: eventthread only knows about the main display right now
2637 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002638 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002639 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002640
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002641 mVisibleRegionsDirty = true;
Dan Stozab90cf072015-03-05 11:05:59 -08002642 mHasPoweredOff = true;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002643 repaintEverything();
2644 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002645 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002646 disableHardwareVsync(true); // also cancels any in-progress resync
2647
Mathias Agopiancde87a32012-09-13 14:09:01 -07002648 // FIXME: eventthread only knows about the main display right now
2649 mEventThread->onScreenReleased();
2650 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002651
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002652 getHwComposer().setPowerMode(type, mode);
2653 mVisibleRegionsDirty = true;
2654 // from this point on, SF will stop drawing on this display
2655 } else {
2656 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002657 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002658}
2659
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002660void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2661 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002662 SurfaceFlinger& mFlinger;
2663 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002664 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002665 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002666 MessageSetPowerMode(SurfaceFlinger& flinger,
2667 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2668 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002669 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002670 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002671 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002672 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002673 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002674 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002675 ALOGW("Attempt to set power mode = %d for virtual display",
2676 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002677 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002678 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002679 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002680 return true;
2681 }
2682 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002683 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002684 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002685}
2686
2687// ---------------------------------------------------------------------------
2688
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002689status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2690{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002691 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002692
Mathias Agopianbd115332013-04-18 16:41:04 -07002693 IPCThreadState* ipc = IPCThreadState::self();
2694 const int pid = ipc->getCallingPid();
2695 const int uid = ipc->getCallingUid();
2696 if ((uid != AID_SHELL) &&
2697 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002698 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002699 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002700 } else {
Jesse Hallfcd15b42014-12-23 13:57:23 -08002701 // Try to get the main lock, but give up after one second
Mathias Agopian9795c422009-08-26 16:36:26 -07002702 // (this would indicate SF is stuck, but we want to be able to
2703 // print something in dumpsys).
Jesse Hallfcd15b42014-12-23 13:57:23 -08002704 status_t err = mStateLock.timedLock(s2ns(1));
2705 bool locked = (err == NO_ERROR);
Mathias Agopian9795c422009-08-26 16:36:26 -07002706 if (!locked) {
Jesse Hallfcd15b42014-12-23 13:57:23 -08002707 result.appendFormat(
2708 "SurfaceFlinger appears to be unresponsive (%s [%d]), "
2709 "dumping anyways (no locks held)\n", strerror(-err), err);
Mathias Agopian9795c422009-08-26 16:36:26 -07002710 }
2711
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002712 bool dumpAll = true;
2713 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002714 size_t numArgs = args.size();
2715 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002716 if ((index < numArgs) &&
2717 (args[index] == String16("--list"))) {
2718 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002719 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002720 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002721 }
2722
2723 if ((index < numArgs) &&
2724 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002725 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002726 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002727 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002728 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002729
2730 if ((index < numArgs) &&
2731 (args[index] == String16("--latency-clear"))) {
2732 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002733 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002734 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002735 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002736
2737 if ((index < numArgs) &&
2738 (args[index] == String16("--dispsync"))) {
2739 index++;
2740 mPrimaryDispSync.dump(result);
2741 dumpAll = false;
2742 }
Dan Stozab90cf072015-03-05 11:05:59 -08002743
2744 if ((index < numArgs) &&
2745 (args[index] == String16("--static-screen"))) {
2746 index++;
2747 dumpStaticScreenStats(result);
2748 dumpAll = false;
2749 }
Pablo Ceballos40845df2016-01-25 17:41:15 -08002750
2751 if ((index < numArgs) &&
2752 (args[index] == String16("--fences"))) {
2753 index++;
2754 mFenceTracker.dump(&result);
2755 dumpAll = false;
2756 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002757 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002758
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002759 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002760 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002761 }
2762
Mathias Agopian9795c422009-08-26 16:36:26 -07002763 if (locked) {
2764 mStateLock.unlock();
2765 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002766 }
2767 write(fd, result.string(), result.size());
2768 return NO_ERROR;
2769}
2770
Dan Stozac7014012014-02-14 15:03:43 -08002771void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2772 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002773{
2774 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2775 const size_t count = currentLayers.size();
2776 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002777 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002778 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002779 }
2780}
2781
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002782void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002783 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002784{
2785 String8 name;
2786 if (index < args.size()) {
2787 name = String8(args[index]);
2788 index++;
2789 }
2790
Dan Stoza9e56aa02015-11-02 13:00:03 -08002791 const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
2792 const nsecs_t period = activeConfig->getVsyncPeriod();
Greg Hackmann86efcc02014-03-07 12:44:02 -08002793 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002794
2795 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002796 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002797 } else {
2798 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2799 const size_t count = currentLayers.size();
2800 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002801 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002802 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002803 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002804 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002805 }
2806 }
2807}
2808
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002809void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002810 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002811{
2812 String8 name;
2813 if (index < args.size()) {
2814 name = String8(args[index]);
2815 index++;
2816 }
2817
2818 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2819 const size_t count = currentLayers.size();
2820 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002821 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002822 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002823 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002824 }
2825 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002826
Svetoslavd85084b2014-03-20 10:28:31 -07002827 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002828}
2829
Jamie Gennis6547ff42013-07-16 20:12:42 -07002830// This should only be called from the main thread. Otherwise it would need
2831// the lock and should use mCurrentState rather than mDrawingState.
2832void SurfaceFlinger::logFrameStats() {
2833 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2834 const size_t count = drawingLayers.size();
2835 for (size_t i=0 ; i<count ; i++) {
2836 const sp<Layer>& layer(drawingLayers[i]);
2837 layer->logFrameStats();
2838 }
2839
2840 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2841}
2842
Andy McFadden4803b742012-09-24 19:07:20 -07002843/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2844{
2845 static const char* config =
2846 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002847#ifdef HAS_CONTEXT_PRIORITY
2848 " HAS_CONTEXT_PRIORITY"
2849#endif
2850#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2851 " NEVER_DEFAULT_TO_ASYNC_MODE"
2852#endif
2853#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2854 " TARGET_DISABLE_TRIPLE_BUFFERING"
2855#endif
2856 "]";
2857 result.append(config);
2858}
2859
Dan Stozab90cf072015-03-05 11:05:59 -08002860void SurfaceFlinger::dumpStaticScreenStats(String8& result) const
2861{
2862 result.appendFormat("Static screen stats:\n");
2863 for (size_t b = 0; b < NUM_BUCKETS - 1; ++b) {
2864 float bucketTimeSec = mFrameBuckets[b] / 1e9;
2865 float percent = 100.0f *
2866 static_cast<float>(mFrameBuckets[b]) / mTotalTime;
2867 result.appendFormat(" < %zd frames: %.3f s (%.1f%%)\n",
2868 b + 1, bucketTimeSec, percent);
2869 }
2870 float bucketTimeSec = mFrameBuckets[NUM_BUCKETS - 1] / 1e9;
2871 float percent = 100.0f *
2872 static_cast<float>(mFrameBuckets[NUM_BUCKETS - 1]) / mTotalTime;
2873 result.appendFormat(" %zd+ frames: %.3f s (%.1f%%)\n",
2874 NUM_BUCKETS - 1, bucketTimeSec, percent);
2875}
2876
Dan Stozae77c7662016-05-13 11:37:28 -07002877void SurfaceFlinger::recordBufferingStats(const char* layerName,
2878 std::vector<OccupancyTracker::Segment>&& history) {
2879 Mutex::Autolock lock(mBufferingStatsMutex);
2880 auto& stats = mBufferingStats[layerName];
2881 for (const auto& segment : history) {
2882 if (!segment.usedThirdBuffer) {
2883 stats.twoBufferTime += segment.totalTime;
2884 }
2885 if (segment.occupancyAverage < 1.0f) {
2886 stats.doubleBufferedTime += segment.totalTime;
2887 } else if (segment.occupancyAverage < 2.0f) {
2888 stats.tripleBufferedTime += segment.totalTime;
2889 }
2890 ++stats.numSegments;
2891 stats.totalTime += segment.totalTime;
2892 }
2893}
2894
2895void SurfaceFlinger::dumpBufferingStats(String8& result) const {
2896 result.append("Buffering stats:\n");
2897 result.append(" [Layer name] <Active time> <Two buffer> "
2898 "<Double buffered> <Triple buffered>\n");
2899 Mutex::Autolock lock(mBufferingStatsMutex);
2900 typedef std::tuple<std::string, float, float, float> BufferTuple;
2901 std::map<float, BufferTuple, std::greater<float>> sorted;
2902 for (const auto& statsPair : mBufferingStats) {
2903 const char* name = statsPair.first.c_str();
2904 const BufferingStats& stats = statsPair.second;
2905 if (stats.numSegments == 0) {
2906 continue;
2907 }
2908 float activeTime = ns2ms(stats.totalTime) / 1000.0f;
2909 float twoBufferRatio = static_cast<float>(stats.twoBufferTime) /
2910 stats.totalTime;
2911 float doubleBufferRatio = static_cast<float>(
2912 stats.doubleBufferedTime) / stats.totalTime;
2913 float tripleBufferRatio = static_cast<float>(
2914 stats.tripleBufferedTime) / stats.totalTime;
2915 sorted.insert({activeTime, {name, twoBufferRatio,
2916 doubleBufferRatio, tripleBufferRatio}});
2917 }
2918 for (const auto& sortedPair : sorted) {
2919 float activeTime = sortedPair.first;
2920 const BufferTuple& values = sortedPair.second;
2921 result.appendFormat(" [%s] %.2f %.3f %.3f %.3f\n",
2922 std::get<0>(values).c_str(), activeTime,
2923 std::get<1>(values), std::get<2>(values),
2924 std::get<3>(values));
2925 }
2926 result.append("\n");
2927}
2928
2929
Mathias Agopian74d211a2013-04-22 16:55:35 +02002930void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2931 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002932{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002933 bool colorize = false;
2934 if (index < args.size()
2935 && (args[index] == String16("--color"))) {
2936 colorize = true;
2937 index++;
2938 }
2939
2940 Colorizer colorizer(colorize);
2941
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002942 // figure out if we're stuck somewhere
2943 const nsecs_t now = systemTime();
2944 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2945 const nsecs_t inTransaction(mDebugInTransaction);
2946 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2947 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2948
2949 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002950 * Dump library configuration.
2951 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002952
2953 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002954 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002955 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002956 appendSfConfigString(result);
2957 appendUiConfigString(result);
2958 appendGuiConfigString(result);
2959 result.append("\n");
2960
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002961 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002962 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002963 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002964 result.append(SyncFeatures::getInstance().toString());
2965 result.append("\n");
2966
Dan Stoza9e56aa02015-11-02 13:00:03 -08002967 const auto& activeConfig = mHwc->getActiveConfig(HWC_DISPLAY_PRIMARY);
2968
Andy McFadden41d67d72014-04-25 16:58:34 -07002969 colorizer.bold(result);
2970 result.append("DispSync configuration: ");
2971 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002972 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2973 "present offset %d ns (refresh %" PRId64 " ns)",
Dan Stoza9e56aa02015-11-02 13:00:03 -08002974 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs,
2975 PRESENT_TIME_OFFSET_FROM_VSYNC_NS, activeConfig->getVsyncPeriod());
Andy McFadden41d67d72014-04-25 16:58:34 -07002976 result.append("\n");
2977
Dan Stozab90cf072015-03-05 11:05:59 -08002978 // Dump static screen stats
2979 result.append("\n");
2980 dumpStaticScreenStats(result);
2981 result.append("\n");
2982
Dan Stozae77c7662016-05-13 11:37:28 -07002983 dumpBufferingStats(result);
2984
Andy McFadden4803b742012-09-24 19:07:20 -07002985 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002986 * Dump the visible layer list
2987 */
2988 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2989 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002990 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002991 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002992 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002993 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002994 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002995 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002996 }
2997
2998 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002999 * Dump Display state
3000 */
3001
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003002 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08003003 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003004 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07003005 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
3006 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02003007 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07003008 }
3009
3010 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003011 * Dump SurfaceFlinger global state
3012 */
3013
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003014 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02003015 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003016 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003017
Mathias Agopian888c8222012-08-04 21:10:38 -07003018 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07003019 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07003020
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003021 colorizer.bold(result);
3022 result.appendFormat("EGL implementation : %s\n",
3023 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
3024 colorizer.reset(result);
3025 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07003026 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07003027
Mathias Agopian875d8e12013-06-07 15:35:48 -07003028 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003029
Mathias Agopian42977342012-08-05 00:40:46 -07003030 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07003031 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
3032 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02003033 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003034 " last eglSwapBuffers() time: %f us\n"
3035 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08003036 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003037 " refresh-rate : %f fps\n"
3038 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07003039 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07003040 " gpu_to_cpu_unsupported : %d\n"
3041 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003042 mLastSwapBufferTime/1000.0,
3043 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08003044 mTransactionFlags,
Dan Stoza9e56aa02015-11-02 13:00:03 -08003045 1e9 / activeConfig->getVsyncPeriod(),
3046 activeConfig->getDpiX(),
3047 activeConfig->getDpiY(),
Mathias Agopianed985572013-03-22 00:24:39 -07003048 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003049
Mathias Agopian74d211a2013-04-22 16:55:35 +02003050 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003051 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003052
Mathias Agopian74d211a2013-04-22 16:55:35 +02003053 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003054 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003055
3056 /*
3057 * VSYNC state
3058 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02003059 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003060
3061 /*
3062 * Dump HWComposer state
3063 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003064 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02003065 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003066 colorizer.reset(result);
Dan Stoza9f26a9c2016-06-22 14:51:09 -07003067 bool hwcDisabled = mDebugDisableHWC || mDebugRegion;
Dan Stoza9e56aa02015-11-02 13:00:03 -08003068 result.appendFormat(" h/w composer %s\n",
3069 hwcDisabled ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02003070 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003071
3072 /*
3073 * Dump gralloc state
3074 */
3075 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
3076 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003077}
3078
Mathias Agopian13127d82013-03-05 17:47:11 -08003079const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07003080SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07003081 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07003082 wp<IBinder> dpy;
3083 for (size_t i=0 ; i<mDisplays.size() ; i++) {
3084 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
3085 dpy = mDisplays.keyAt(i);
3086 break;
3087 }
3088 }
3089 if (dpy == NULL) {
3090 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
3091 // Just use the primary display so we have something to return
3092 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
3093 }
3094 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07003095}
3096
Keun young Park63f165f2012-08-31 10:53:36 -07003097bool SurfaceFlinger::startDdmConnection()
3098{
3099 void* libddmconnection_dso =
3100 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
3101 if (!libddmconnection_dso) {
3102 return false;
3103 }
3104 void (*DdmConnection_start)(const char* name);
3105 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07003106 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07003107 if (!DdmConnection_start) {
3108 dlclose(libddmconnection_dso);
3109 return false;
3110 }
3111 (*DdmConnection_start)(getServiceName());
3112 return true;
3113}
3114
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003115status_t SurfaceFlinger::onTransact(
3116 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3117{
3118 switch (code) {
3119 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07003120 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07003121 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003122 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07003123 case CLEAR_ANIMATION_FRAME_STATS:
3124 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07003125 case SET_POWER_MODE:
Dan Stozac4f471e2016-03-24 09:31:08 -07003126 case GET_HDR_CAPABILITIES:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003127 {
3128 // codes that require permission check
3129 IPCThreadState* ipc = IPCThreadState::self();
3130 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07003131 const int uid = ipc->getCallingUid();
Jeff Brown3bfe51d2015-04-10 20:20:13 -07003132 if ((uid != AID_GRAPHICS && uid != AID_SYSTEM) &&
Mathias Agopian99b49842011-06-27 16:05:52 -07003133 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00003134 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07003135 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
3136 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003137 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003138 break;
3139 }
3140 case CAPTURE_SCREEN:
3141 {
3142 // codes that require permission check
3143 IPCThreadState* ipc = IPCThreadState::self();
3144 const int pid = ipc->getCallingPid();
3145 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07003146 if ((uid != AID_GRAPHICS) &&
3147 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00003148 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003149 "can't read framebuffer pid=%d, uid=%d", pid, uid);
3150 return PERMISSION_DENIED;
3151 }
3152 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003153 }
3154 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003155
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003156 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
3157 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07003158 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08003159 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07003160 IPCThreadState* ipc = IPCThreadState::self();
3161 const int pid = ipc->getCallingPid();
3162 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00003163 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07003164 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003165 return PERMISSION_DENIED;
3166 }
3167 int n;
3168 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07003169 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07003170 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003171 return NO_ERROR;
3172 case 1002: // SHOW_UPDATES
3173 n = data.readInt32();
3174 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07003175 invalidateHwcGeometry();
3176 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003177 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003178 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07003179 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003180 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003181 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003182 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07003183 setTransactionFlags(
3184 eTransactionNeeded|
3185 eDisplayTransactionNeeded|
3186 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003187 return NO_ERROR;
3188 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08003189 case 1006:{ // send empty update
3190 signalRefresh();
3191 return NO_ERROR;
3192 }
Mathias Agopian53331da2011-08-22 21:44:41 -07003193 case 1008: // toggle use of hw composer
3194 n = data.readInt32();
3195 mDebugDisableHWC = n ? 1 : 0;
3196 invalidateHwcGeometry();
3197 repaintEverything();
3198 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07003199 case 1009: // toggle use of transform hint
3200 n = data.readInt32();
3201 mDebugDisableTransformHint = n ? 1 : 0;
3202 invalidateHwcGeometry();
3203 repaintEverything();
3204 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003205 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07003206 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003207 reply->writeInt32(0);
3208 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07003209 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08003210 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003211 return NO_ERROR;
3212 case 1013: {
3213 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07003214 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
3215 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07003216 return NO_ERROR;
3217 }
3218 case 1014: {
3219 // daltonize
3220 n = data.readInt32();
3221 switch (n % 10) {
Dan Stoza9f26a9c2016-06-22 14:51:09 -07003222 case 1:
3223 mDaltonizer.setType(ColorBlindnessType::Protanomaly);
3224 break;
3225 case 2:
3226 mDaltonizer.setType(ColorBlindnessType::Deuteranomaly);
3227 break;
3228 case 3:
3229 mDaltonizer.setType(ColorBlindnessType::Tritanomaly);
3230 break;
3231 default:
3232 mDaltonizer.setType(ColorBlindnessType::None);
3233 break;
Mathias Agopianff2ed702013-09-01 21:36:12 -07003234 }
3235 if (n >= 10) {
Dan Stoza9f26a9c2016-06-22 14:51:09 -07003236 mDaltonizer.setMode(ColorBlindnessMode::Correction);
Mathias Agopianff2ed702013-09-01 21:36:12 -07003237 } else {
Dan Stoza9f26a9c2016-06-22 14:51:09 -07003238 mDaltonizer.setMode(ColorBlindnessMode::Simulation);
Mathias Agopianff2ed702013-09-01 21:36:12 -07003239 }
Mathias Agopianff2ed702013-09-01 21:36:12 -07003240 invalidateHwcGeometry();
3241 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07003242 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003243 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003244 case 1015: {
3245 // apply a color matrix
3246 n = data.readInt32();
Alan Viverette9c5a3332013-09-12 20:04:35 -07003247 if (n) {
3248 // color matrix is sent as mat3 matrix followed by vec3
3249 // offset, then packed into a mat4 where the last row is
3250 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07003251 for (size_t i = 0 ; i < 4; i++) {
Dan Stoza9f26a9c2016-06-22 14:51:09 -07003252 for (size_t j = 0; j < 4; j++) {
3253 mColorMatrix[i][j] = data.readFloat();
3254 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003255 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003256 } else {
3257 mColorMatrix = mat4();
3258 }
3259 invalidateHwcGeometry();
3260 repaintEverything();
3261 return NO_ERROR;
3262 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003263 // This is an experimental interface
3264 // Needs to be shifted to proper binder interface when we productize
3265 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07003266 n = data.readInt32();
3267 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003268 return NO_ERROR;
3269 }
Dan Stozaee44edd2015-03-23 15:50:23 -07003270 case 1017: {
3271 n = data.readInt32();
3272 mForceFullDamage = static_cast<bool>(n);
3273 return NO_ERROR;
3274 }
Dan Stozadb4ac3c2015-04-14 11:34:01 -07003275 case 1018: { // Modify Choreographer's phase offset
3276 n = data.readInt32();
3277 mEventThread->setPhaseOffset(static_cast<nsecs_t>(n));
3278 return NO_ERROR;
3279 }
3280 case 1019: { // Modify SurfaceFlinger's phase offset
3281 n = data.readInt32();
3282 mSFEventThread->setPhaseOffset(static_cast<nsecs_t>(n));
3283 return NO_ERROR;
3284 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003285 }
3286 }
3287 return err;
3288}
3289
Mathias Agopian53331da2011-08-22 21:44:41 -07003290void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07003291 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08003292 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07003293}
3294
Mathias Agopian59119e62010-10-11 12:37:43 -07003295// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003296// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07003297// ---------------------------------------------------------------------------
3298
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003299/* The code below is here to handle b/8734824
3300 *
3301 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07003302 * from the surfaceflinger thread to the calling binder thread, where they
3303 * are executed. This allows the calling thread in the calling process to be
3304 * reused and not depend on having "enough" binder threads to handle the
3305 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003306 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003307class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07003308 /* Parts of GraphicProducerWrapper are run on two different threads,
3309 * communicating by sending messages via Looper but also by shared member
3310 * data. Coherence maintenance is subtle and in places implicit (ugh).
3311 *
3312 * Don't rely on Looper's sendMessage/handleMessage providing
3313 * release/acquire semantics for any data not actually in the Message.
3314 * Data going from surfaceflinger to binder threads needs to be
3315 * synchronized explicitly.
3316 *
3317 * Barrier open/wait do provide release/acquire semantics. This provides
3318 * implicit synchronization for data coming back from binder to
3319 * surfaceflinger threads.
3320 */
3321
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003322 sp<IGraphicBufferProducer> impl;
3323 sp<Looper> looper;
3324 status_t result;
3325 bool exitPending;
3326 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07003327 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003328 uint32_t code;
3329 Parcel const* data;
3330 Parcel* reply;
3331
3332 enum {
3333 MSG_API_CALL,
3334 MSG_EXIT
3335 };
3336
3337 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003338 * Called on surfaceflinger thread. This is called by our "fake"
3339 * BpGraphicBufferProducer. We package the data and reply Parcel and
3340 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003341 */
3342 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08003343 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003344 this->code = code;
3345 this->data = &data;
3346 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003347 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07003348 // if we've exited, we run the message synchronously right here.
3349 // note (JH): as far as I can tell from looking at the code, this
3350 // never actually happens. if it does, i'm not sure if it happens
3351 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003352 handleMessage(Message(MSG_API_CALL));
3353 } else {
3354 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07003355 // Prevent stores to this->{code, data, reply} from being
3356 // reordered later than the construction of Message.
3357 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003358 looper->sendMessage(this, Message(MSG_API_CALL));
3359 barrier.wait();
3360 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08003361 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003362 }
3363
3364 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003365 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003366 * call the real BpGraphicBufferProducer.
3367 */
3368 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07003369 int what = message.what;
3370 // Prevent reads below from happening before the read from Message
3371 atomic_thread_fence(memory_order_acquire);
3372 if (what == MSG_API_CALL) {
Marco Nelissen097ca272014-11-14 08:01:01 -08003373 result = IInterface::asBinder(impl)->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003374 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07003375 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003376 exitRequested = true;
3377 }
3378 }
3379
3380public:
Jesse Hallb154c422014-07-13 12:47:02 -07003381 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
3382 : impl(impl),
3383 looper(new Looper(true)),
Pablo Ceballos53390e12015-08-04 11:25:59 -07003384 result(NO_ERROR),
Jesse Hallb154c422014-07-13 12:47:02 -07003385 exitPending(false),
Pablo Ceballos53390e12015-08-04 11:25:59 -07003386 exitRequested(false),
3387 code(0),
3388 data(NULL),
3389 reply(NULL)
Jesse Hallb154c422014-07-13 12:47:02 -07003390 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003391
Jesse Hallb154c422014-07-13 12:47:02 -07003392 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003393 status_t waitForResponse() {
3394 do {
3395 looper->pollOnce(-1);
3396 } while (!exitRequested);
3397 return result;
3398 }
3399
Jesse Hallb154c422014-07-13 12:47:02 -07003400 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003401 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07003402 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003403 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07003404 // Ensure this->result is visible to the binder thread before it
3405 // handles the message.
3406 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003407 looper->sendMessage(this, Message(MSG_EXIT));
3408 }
3409};
3410
3411
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003412status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
3413 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003414 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003415 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003416 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003417
3418 if (CC_UNLIKELY(display == 0))
3419 return BAD_VALUE;
3420
3421 if (CC_UNLIKELY(producer == 0))
3422 return BAD_VALUE;
3423
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003424 // if we have secure windows on this display, never allow the screen capture
3425 // unless the producer interface is local (i.e.: we can take a screenshot for
3426 // ourselves).
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003427 bool isLocalScreenshot = IInterface::asBinder(producer)->localBinder();
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003428
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003429 // Convert to surfaceflinger's internal rotation type.
3430 Transform::orientation_flags rotationFlags;
3431 switch (rotation) {
3432 case ISurfaceComposer::eRotateNone:
3433 rotationFlags = Transform::ROT_0;
3434 break;
3435 case ISurfaceComposer::eRotate90:
3436 rotationFlags = Transform::ROT_90;
3437 break;
3438 case ISurfaceComposer::eRotate180:
3439 rotationFlags = Transform::ROT_180;
3440 break;
3441 case ISurfaceComposer::eRotate270:
3442 rotationFlags = Transform::ROT_270;
3443 break;
3444 default:
3445 rotationFlags = Transform::ROT_0;
3446 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3447 break;
3448 }
3449
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003450 class MessageCaptureScreen : public MessageBase {
3451 SurfaceFlinger* flinger;
3452 sp<IBinder> display;
3453 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003454 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003455 uint32_t reqWidth, reqHeight;
3456 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003457 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003458 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003459 status_t result;
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003460 bool isLocalScreenshot;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003461 public:
3462 MessageCaptureScreen(SurfaceFlinger* flinger,
3463 const sp<IBinder>& display,
3464 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003465 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003466 uint32_t minLayerZ, uint32_t maxLayerZ,
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003467 bool useIdentityTransform,
3468 Transform::orientation_flags rotation,
3469 bool isLocalScreenshot)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003470 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003471 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003472 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003473 useIdentityTransform(useIdentityTransform),
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003474 rotation(rotation), result(PERMISSION_DENIED),
3475 isLocalScreenshot(isLocalScreenshot)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003476 {
3477 }
3478 status_t getResult() const {
3479 return result;
3480 }
3481 virtual bool handler() {
3482 Mutex::Autolock _l(flinger->mStateLock);
3483 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003484 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003485 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003486 useIdentityTransform, rotation, isLocalScreenshot);
Marco Nelissen097ca272014-11-14 08:01:01 -08003487 static_cast<GraphicProducerWrapper*>(IInterface::asBinder(producer).get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003488 return true;
3489 }
3490 };
3491
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003492 // this creates a "fake" BBinder which will serve as a "fake" remote
3493 // binder to receive the marshaled calls and forward them to the
3494 // real remote (a BpGraphicBufferProducer)
3495 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3496
3497 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3498 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003499 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003500 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003501 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003502 useIdentityTransform, rotationFlags, isLocalScreenshot);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003503
3504 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003505 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003506 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003507 }
3508 return res;
3509}
3510
Mathias Agopian180f10d2013-04-10 22:55:41 -07003511
3512void SurfaceFlinger::renderScreenImplLocked(
3513 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003514 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003515 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003516 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003517{
3518 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003519 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003520
3521 // get screen geometry
Andreas Gampe89fd4f72014-11-13 14:18:56 -08003522 const int32_t hw_w = hw->getWidth();
3523 const int32_t hw_h = hw->getHeight();
3524 const bool filtering = static_cast<int32_t>(reqWidth) != hw_w ||
Christopher Ferris0e749792015-03-23 14:32:15 -07003525 static_cast<int32_t>(reqHeight) != hw_h;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003526
Dan Stozac1879002014-05-22 15:59:05 -07003527 // if a default or invalid sourceCrop is passed in, set reasonable values
3528 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3529 !sourceCrop.isValid()) {
3530 sourceCrop.setLeftTop(Point(0, 0));
3531 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3532 }
3533
3534 // ensure that sourceCrop is inside screen
3535 if (sourceCrop.left < 0) {
3536 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3537 }
Dan Stozabe31f442014-06-11 11:20:54 -07003538 if (sourceCrop.right > hw_w) {
3539 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003540 }
3541 if (sourceCrop.top < 0) {
3542 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3543 }
Dan Stozabe31f442014-06-11 11:20:54 -07003544 if (sourceCrop.bottom > hw_h) {
3545 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003546 }
3547
Mathias Agopian180f10d2013-04-10 22:55:41 -07003548 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003549 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003550
3551 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003552 engine.setViewportAndProjection(
3553 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003554 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003555
3556 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003557 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003558
3559 const LayerVector& layers( mDrawingState.layersSortedByZ );
3560 const size_t count = layers.size();
3561 for (size_t i=0 ; i<count ; ++i) {
3562 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003563 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003564 if (state.layerStack == hw->getLayerStack()) {
3565 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3566 if (layer->isVisible()) {
3567 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08003568 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003569 if (filtering) layer->setFiltering(false);
3570 }
3571 }
3572 }
3573 }
3574
Mathias Agopian931bda12013-08-28 18:11:46 -07003575 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003576}
3577
3578
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003579status_t SurfaceFlinger::captureScreenImplLocked(
3580 const sp<const DisplayDevice>& hw,
3581 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003582 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003583 uint32_t minLayerZ, uint32_t maxLayerZ,
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003584 bool useIdentityTransform, Transform::orientation_flags rotation,
3585 bool isLocalScreenshot)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003586{
3587 ATRACE_CALL();
3588
Mathias Agopian180f10d2013-04-10 22:55:41 -07003589 // get screen geometry
Dan Stoza35024162015-06-09 16:44:40 -07003590 uint32_t hw_w = hw->getWidth();
3591 uint32_t hw_h = hw->getHeight();
3592
3593 if (rotation & Transform::ROT_90) {
3594 std::swap(hw_w, hw_h);
3595 }
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003596
Mathias Agopian180f10d2013-04-10 22:55:41 -07003597 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3598 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3599 reqWidth, reqHeight, hw_w, hw_h);
3600 return BAD_VALUE;
3601 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003602
Mathias Agopian180f10d2013-04-10 22:55:41 -07003603 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3604 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3605
Pablo Ceballosb5b35632016-02-23 11:18:51 -08003606 bool secureLayerIsVisible = false;
3607 const LayerVector& layers(mDrawingState.layersSortedByZ);
3608 const size_t count = layers.size();
3609 for (size_t i = 0 ; i < count ; ++i) {
3610 const sp<Layer>& layer(layers[i]);
3611 const Layer::State& state(layer->getDrawingState());
3612 if (state.layerStack == hw->getLayerStack() && state.z >= minLayerZ &&
3613 state.z <= maxLayerZ && layer->isVisible() &&
3614 layer->isSecure()) {
3615 secureLayerIsVisible = true;
3616 }
3617 }
3618
3619 if (!isLocalScreenshot && secureLayerIsVisible) {
3620 ALOGW("FB is protected: PERMISSION_DENIED");
3621 return PERMISSION_DENIED;
3622 }
3623
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003624 // create a surface (because we're a producer, and we need to
3625 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003626 sp<Surface> sur = new Surface(producer, false);
Pablo Ceballos605d15a2016-07-21 13:44:51 -07003627
3628 // Put the screenshot Surface into async mode so that
3629 // Layer::headFenceHasSignaled will always return true and we'll latch the
3630 // first buffer regardless of whether or not its acquire fence has
3631 // signaled. This is needed to avoid a race condition in the rotation
3632 // animation. See b/30209608
3633 sur->setAsyncMode(true);
3634
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003635 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003636
Michael Lentine5a16a622015-05-21 13:48:24 -07003637 status_t result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
3638 if (result == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003639 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3640 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003641
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003642 int err = 0;
3643 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003644 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003645 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3646 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003647
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003648 if (err == NO_ERROR) {
3649 ANativeWindowBuffer* buffer;
3650 /* TODO: Once we have the sync framework everywhere this can use
3651 * server-side waits on the fence that dequeueBuffer returns.
3652 */
3653 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3654 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003655 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003656 // create an EGLImage from the buffer so we can later
3657 // turn it into a texture
3658 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3659 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3660 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003661 // this binds the given EGLImage as a framebuffer for the
3662 // duration of this scope.
3663 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3664 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003665 // this will in fact render into our dequeued buffer
3666 // via an FBO, which means we didn't have to create
3667 // an EGLSurface and therefore we're not
3668 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003669 renderScreenImplLocked(
3670 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3671 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003672
Riley Andrews86639902014-08-15 12:27:24 -07003673 // Attempt to create a sync khr object that can produce a sync point. If that
3674 // isn't available, create a non-dupable sync object in the fallback path and
3675 // wait on it directly.
3676 EGLSyncKHR sync;
3677 if (!DEBUG_SCREENSHOTS) {
3678 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Riley Andrews9707f4d2014-10-23 16:17:04 -07003679 // native fence fd will not be populated until flush() is done.
3680 getRenderEngine().flush();
Andy McFadden2d8d1202013-10-09 16:38:02 -07003681 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003682 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003683 }
Riley Andrews86639902014-08-15 12:27:24 -07003684 if (sync != EGL_NO_SYNC_KHR) {
3685 // get the sync fd
3686 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3687 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3688 ALOGW("captureScreen: failed to dup sync khr object");
3689 syncFd = -1;
3690 }
3691 eglDestroySyncKHR(mEGLDisplay, sync);
3692 } else {
3693 // fallback path
3694 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3695 if (sync != EGL_NO_SYNC_KHR) {
3696 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3697 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3698 EGLint eglErr = eglGetError();
3699 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3700 ALOGW("captureScreen: fence wait timed out");
3701 } else {
3702 ALOGW_IF(eglErr != EGL_SUCCESS,
3703 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3704 }
3705 eglDestroySyncKHR(mEGLDisplay, sync);
3706 } else {
3707 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3708 }
3709 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003710 if (DEBUG_SCREENSHOTS) {
3711 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3712 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3713 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3714 hw, minLayerZ, maxLayerZ);
3715 delete [] pixels;
3716 }
3717
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003718 } else {
3719 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3720 result = INVALID_OPERATION;
Prathmesh Prabhuf3209b02016-03-09 16:54:45 -08003721 window->cancelBuffer(window, buffer, syncFd);
3722 buffer = NULL;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003723 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003724 // destroy our image
3725 eglDestroyImageKHR(mEGLDisplay, image);
3726 } else {
3727 result = BAD_VALUE;
3728 }
Prathmesh Prabhuf3209b02016-03-09 16:54:45 -08003729 if (buffer) {
3730 // queueBuffer takes ownership of syncFd
3731 result = window->queueBuffer(window, buffer, syncFd);
3732 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003733 }
3734 } else {
3735 result = BAD_VALUE;
3736 }
3737 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3738 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003739
Mathias Agopian74c40c02010-09-29 13:02:36 -07003740 return result;
3741}
3742
Mathias Agopiand5556842013-09-19 17:08:37 -07003743void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3744 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003745 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003746 for (size_t y=0 ; y<h ; y++) {
3747 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3748 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003749 if (p[x] != 0xFF000000) return;
3750 }
3751 }
3752 ALOGE("*** we just took a black screenshot ***\n"
3753 "requested minz=%d, maxz=%d, layerStack=%d",
3754 minLayerZ, maxLayerZ, hw->getLayerStack());
3755 const LayerVector& layers( mDrawingState.layersSortedByZ );
3756 const size_t count = layers.size();
3757 for (size_t i=0 ; i<count ; ++i) {
3758 const sp<Layer>& layer(layers[i]);
3759 const Layer::State& state(layer->getDrawingState());
3760 const bool visible = (state.layerStack == hw->getLayerStack())
3761 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3762 && (layer->isVisible());
Dan Stoza9e56aa02015-11-02 13:00:03 -08003763 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%.3f",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003764 visible ? '+' : '-',
3765 i, layer->getName().string(), state.layerStack, state.z,
3766 layer->isVisible(), state.flags, state.alpha);
3767 }
3768 }
3769}
3770
Pablo Ceballosce796e72016-02-04 19:10:51 -08003771bool SurfaceFlinger::getFrameTimestamps(const Layer& layer,
3772 uint64_t frameNumber, FrameTimestamps* outTimestamps) {
3773 return mFenceTracker.getFrameTimestamps(layer, frameNumber, outTimestamps);
3774}
3775
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003776// ---------------------------------------------------------------------------
3777
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003778SurfaceFlinger::LayerVector::LayerVector() {
3779}
3780
3781SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003782 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003783}
3784
3785int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3786 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003787{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003788 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003789 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3790 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003791
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003792 uint32_t ls = l->getCurrentState().layerStack;
3793 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003794 if (ls != rs)
3795 return ls - rs;
3796
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003797 uint32_t lz = l->getCurrentState().z;
3798 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003799 if (lz != rz)
3800 return lz - rz;
3801
3802 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003803}
3804
3805// ---------------------------------------------------------------------------
3806
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003807SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Pablo Ceballos53390e12015-08-04 11:25:59 -07003808 : type(DisplayDevice::DISPLAY_ID_INVALID),
3809 layerStack(DisplayDevice::NO_LAYER_STACK),
3810 orientation(0),
3811 width(0),
3812 height(0),
3813 isSecure(false) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003814}
3815
Pablo Ceballos53390e12015-08-04 11:25:59 -07003816SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(
3817 DisplayDevice::DisplayType type, bool isSecure)
3818 : type(type),
3819 layerStack(DisplayDevice::NO_LAYER_STACK),
3820 orientation(0),
3821 width(0),
3822 height(0),
3823 isSecure(isSecure) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003824 viewport.makeInvalid();
3825 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003826}
3827
3828// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003829
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003830}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003831
3832
3833#if defined(__gl_h_)
3834#error "don't include gl/gl.h in this file"
3835#endif
3836
3837#if defined(__gl2_h_)
3838#error "don't include gl2/gl2.h in this file"
3839#endif