blob: 026e22ae6660c804a1690a64920e3027c7f079a3 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Greg Hackmann86efcc02014-03-07 12:44:02 -080024#include <inttypes.h>
Jesse Hallb154c422014-07-13 12:47:02 -070025#include <stdatomic.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070026
27#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036
Mathias Agopianc666cae2012-07-25 18:56:13 -070037#include <ui/DisplayInfo.h>
Lajos Molnar67d8bd62014-09-11 14:58:45 -070038#include <ui/DisplayStatInfo.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070039
Mathias Agopian921e6ac2012-07-23 23:11:29 -070040#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070041#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070042#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070043#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080044#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080045#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070046
47#include <ui/GraphicBufferAllocator.h>
48#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070049#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080050
Mathias Agopiancde87a32012-09-13 14:09:01 -070051#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include <utils/String8.h>
53#include <utils/String16.h>
54#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080055#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopian921e6ac2012-07-23 23:11:29 -070057#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070058#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
Mathias Agopian3e25fd82013-04-22 17:52:16 +020060#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020062#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080063#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070064#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070065#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070066#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080067#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071
Mathias Agopiana4912602012-07-12 14:25:33 -070072#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070073#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070074#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075
Mathias Agopianff2ed702013-09-01 21:36:12 -070076#include "Effects/Daltonizer.h"
77
Mathias Agopian875d8e12013-06-07 15:35:48 -070078#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070079#include <cutils/compiler.h>
Raj Kamal366d9b42014-06-11 13:49:32 +053080#ifdef QCOM_BSP
81#include <gralloc_priv.h>
82#endif
Mathias Agopian875d8e12013-06-07 15:35:48 -070083
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -070084#ifdef QCOM_BSP
85#include <display_config.h>
86#endif
87
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088#define DISPLAY_COUNT 1
89
Mathias Agopianfee2b462013-07-03 12:34:01 -070090/*
91 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
92 * black pixels.
93 */
94#define DEBUG_SCREENSHOTS false
95
Mathias Agopianca088332013-03-28 17:44:13 -070096EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
97
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070099
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700100// This is the phase offset in nanoseconds of the software vsync event
101// relative to the vsync event reported by HWComposer. The software vsync
102// event is when SurfaceFlinger and Choreographer-based applications run each
103// frame.
104//
105// This phase offset allows adjustment of the minimum latency from application
106// wake-up (by Choregographer) time to the time at which the resulting window
107// image is displayed. This value may be either positive (after the HW vsync)
108// or negative (before the HW vsync). Setting it to 0 will result in a
109// minimum latency of two vsync periods because the app and SurfaceFlinger
110// will run just after the HW vsync. Setting it to a positive number will
111// result in the minimum latency being:
112//
113// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
114//
115// Note that reducing this latency makes it more likely for the applications
116// to not have their window content image ready in time. When this happens
117// the latency will end up being an additional vsync period, and animations
118// will hiccup. Therefore, this latency should be tuned somewhat
119// conservatively (or at least with awareness of the trade-off being made).
120static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
121
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700122// This is the phase offset at which SurfaceFlinger's composition runs.
123static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
124
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125// ---------------------------------------------------------------------------
126
Mathias Agopian99b49842011-06-27 16:05:52 -0700127const String16 sHardwareTest("android.permission.HARDWARE_TEST");
128const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
129const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
130const String16 sDump("android.permission.DUMP");
131
132// ---------------------------------------------------------------------------
133
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700135 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700137 mTransactionPending(false),
138 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700139 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700140 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700141 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700144 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800145 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700147 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700148 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700149 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700150 mDebugInSwapBuffers(0),
151 mLastSwapBufferTime(0),
152 mDebugInTransaction(0),
153 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700154 mBootFinished(false),
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530155 mGpuTileRenderEnable(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700156 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700157 mHWVsyncAvailable(false),
Alan Viverette9c5a3332013-09-12 20:04:35 -0700158 mDaltonize(false),
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530159 mHasColorMatrix(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160{
Steve Blocka19954a2012-01-04 20:05:49 +0000161 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162
163 // debugging stuff...
164 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700165
Mathias Agopianb4b17302013-03-20 18:36:41 -0700166 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700167 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700168
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 property_get("debug.sf.showupdates", value, "0");
170 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700171
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700172 property_get("debug.sf.ddms", value, "0");
173 mDebugDDMS = atoi(value);
174 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700175 if (!startDdmConnection()) {
176 // start failed, and DDMS debugging not enabled
177 mDebugDDMS = 0;
178 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700179 }
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +0530180#ifdef QCOM_BSP
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530181 mCanUseGpuTileRender = false;
182 property_get("debug.sf.gpu_comp_tiling", value, "0");
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +0530183 mGpuTileRenderEnable = atoi(value) ? true : false;
184 if(mGpuTileRenderEnable)
185 ALOGV("DirtyRect optimization enabled for FULL GPU Composition");
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530186 mUnionDirtyRect.clear();
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +0530187#endif
188
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700189 ALOGI_IF(mDebugRegion, "showupdates enabled");
190 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191}
192
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800193void SurfaceFlinger::onFirstRef()
194{
195 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800196}
197
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198SurfaceFlinger::~SurfaceFlinger()
199{
Mathias Agopiana4912602012-07-12 14:25:33 -0700200 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
201 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
202 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203}
204
Dan Stozac7014012014-02-14 15:03:43 -0800205void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800206{
207 // the window manager died on us. prepare its eulogy.
208
Andy McFadden13a082e2012-08-24 10:16:42 -0700209 // restore initial conditions (default device unblank, etc)
210 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800211
212 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700213 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800214}
215
Mathias Agopian7e27f052010-05-28 14:22:23 -0700216sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217{
Mathias Agopian96f08192010-06-02 23:28:45 -0700218 sp<ISurfaceComposerClient> bclient;
219 sp<Client> client(new Client(this));
220 status_t err = client->initCheck();
221 if (err == NO_ERROR) {
222 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 return bclient;
225}
226
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700227sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
228 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700229{
230 class DisplayToken : public BBinder {
231 sp<SurfaceFlinger> flinger;
232 virtual ~DisplayToken() {
233 // no more references, this display must be terminated
234 Mutex::Autolock _l(flinger->mStateLock);
235 flinger->mCurrentState.displays.removeItem(this);
236 flinger->setTransactionFlags(eDisplayTransactionNeeded);
237 }
238 public:
239 DisplayToken(const sp<SurfaceFlinger>& flinger)
240 : flinger(flinger) {
241 }
242 };
243
244 sp<BBinder> token = new DisplayToken(this);
245
246 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700247 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700248 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700249 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700250 mCurrentState.displays.add(token, info);
251
252 return token;
253}
254
Jesse Hall6c913be2013-08-08 12:15:49 -0700255void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
256 Mutex::Autolock _l(mStateLock);
257
258 ssize_t idx = mCurrentState.displays.indexOfKey(display);
259 if (idx < 0) {
260 ALOGW("destroyDisplay: invalid display token");
261 return;
262 }
263
264 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
265 if (!info.isVirtualDisplay()) {
266 ALOGE("destroyDisplay called for non-virtual display");
267 return;
268 }
269
270 mCurrentState.displays.removeItemsAt(idx);
271 setTransactionFlags(eDisplayTransactionNeeded);
272}
273
Jesse Hall692c7232012-11-08 15:41:56 -0800274void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
275 ALOGW_IF(mBuiltinDisplays[type],
276 "Overwriting display token for display type %d", type);
277 mBuiltinDisplays[type] = new BBinder();
278 DisplayDeviceState info(type);
279 // All non-virtual displays are currently considered secure.
280 info.isSecure = true;
281 mCurrentState.displays.add(mBuiltinDisplays[type], info);
282}
283
Mathias Agopiane57f2922012-08-09 16:29:12 -0700284sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700285 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700286 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
287 return NULL;
288 }
Jesse Hall692c7232012-11-08 15:41:56 -0800289 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700290}
291
Jamie Gennis9a78c902011-01-12 18:30:40 -0800292sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
293{
294 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
295 return gba;
296}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700297
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298void SurfaceFlinger::bootFinished()
299{
300 const nsecs_t now = systemTime();
301 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000302 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700303 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700304
305 // wait patiently for the window manager death
306 const String16 name("window");
307 sp<IBinder> window(defaultServiceManager()->getService(name));
308 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700309 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700310 }
311
312 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700313 // formerly we would just kill the process, but we now ask it to exit so it
314 // can choose where to stop the animation.
315 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316}
317
Mathias Agopian3f844832013-08-07 21:24:32 -0700318void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700319 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700320 RenderEngine& engine;
321 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700322 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700323 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
324 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700325 }
326 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700327 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700328 return true;
329 }
330 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700331 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700332}
333
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700334class DispSyncSource : public VSyncSource, private DispSync::Callback {
335public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700336 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
337 const char* label) :
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700338 mValue(0),
339 mPhaseOffset(phaseOffset),
340 mTraceVsync(traceVsync),
Andy McFadden5167ec62014-05-22 13:08:43 -0700341 mVsyncOnLabel(String8::format("VsyncOn-%s", label)),
342 mVsyncEventLabel(String8::format("VSYNC-%s", label)),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700343 mDispSync(dispSync) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700344
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700345 virtual ~DispSyncSource() {}
346
347 virtual void setVSyncEnabled(bool enable) {
348 // Do NOT lock the mutex here so as to avoid any mutex ordering issues
349 // with locking it in the onDispSyncEvent callback.
350 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700351 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700352 static_cast<DispSync::Callback*>(this));
353 if (err != NO_ERROR) {
354 ALOGE("error registering vsync callback: %s (%d)",
355 strerror(-err), err);
356 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700357 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700358 } else {
359 status_t err = mDispSync->removeEventListener(
360 static_cast<DispSync::Callback*>(this));
361 if (err != NO_ERROR) {
362 ALOGE("error unregistering vsync callback: %s (%d)",
363 strerror(-err), err);
364 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700365 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700366 }
367 }
368
369 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
370 Mutex::Autolock lock(mMutex);
371 mCallback = callback;
372 }
373
374private:
375 virtual void onDispSyncEvent(nsecs_t when) {
376 sp<VSyncSource::Callback> callback;
377 {
378 Mutex::Autolock lock(mMutex);
379 callback = mCallback;
380
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700381 if (mTraceVsync) {
382 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700383 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700384 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700385 }
386
387 if (callback != NULL) {
388 callback->onVSyncEvent(when);
389 }
390 }
391
392 int mValue;
393
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700394 const nsecs_t mPhaseOffset;
395 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700396 const String8 mVsyncOnLabel;
397 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700398
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700399 DispSync* mDispSync;
400 sp<VSyncSource::Callback> mCallback;
401 Mutex mMutex;
402};
403
404void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700405 ALOGI( "SurfaceFlinger's main thread ready to run. "
406 "Initializing graphics H/W...");
407
Jesse Hallb65f32e2013-09-27 22:10:47 -0700408 status_t err;
Jesse Hall692c7232012-11-08 15:41:56 -0800409 Mutex::Autolock _l(mStateLock);
410
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700411 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700412 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
413 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700414
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700415 // Initialize the H/W composer object. There may or may not be an
416 // actual hardware composer underneath.
417 mHwc = new HWComposer(this,
418 *static_cast<HWComposer::EventHandler *>(this));
419
Mathias Agopian875d8e12013-06-07 15:35:48 -0700420 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800421 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700422
423 // retrieve the EGL context that was selected/created
424 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700425
Mathias Agopianda27af92012-09-13 18:17:13 -0700426 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
427 "couldn't create EGLContext");
428
Mathias Agopiancde87a32012-09-13 14:09:01 -0700429 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700430 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700431 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700432 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700433 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700434 // All non-virtual displays are currently considered secure.
435 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800436 createBuiltinDisplayLocked(type);
437 wp<IBinder> token = mBuiltinDisplays[i];
438
Dan Stozab9b08832014-03-13 11:55:57 -0700439 sp<IGraphicBufferProducer> producer;
440 sp<IGraphicBufferConsumer> consumer;
441 BufferQueue::createBufferQueue(&producer, &consumer,
442 new GraphicBufferAlloc());
443
444 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
445 consumer);
Jesse Hall19e87292013-12-23 21:02:15 -0800446 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700447 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800448 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Dan Stozab9b08832014-03-13 11:55:57 -0700449 fbs, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -0800450 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700451 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700452 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700453 // for displays other than the main display, so we always
454 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800455 ALOGD("marking display %zu as acquired/unblanked", i);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700456 hw->setPowerMode(HWC_POWER_MODE_NORMAL);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700457 }
458 mDisplays.add(token, hw);
459 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700460 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700461
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700462 // make the GLContext current so that we can create textures when creating Layers
463 // (which may happens before we render something)
464 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
465
Mathias Agopian028508c2012-07-25 21:12:12 -0700466 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700467 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700468 vsyncPhaseOffsetNs, true, "app");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700469 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700470 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700471 sfVsyncPhaseOffsetNs, true, "sf");
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700472 mSFEventThread = new EventThread(sfVsyncSrc);
473 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700474
Jamie Gennisd1700752013-10-14 12:22:52 -0700475 mEventControlThread = new EventControlThread(this);
476 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
477
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700478 // set a fake vsync period if there is no HWComposer
479 if (mHwc->initCheck() != NO_ERROR) {
480 mPrimaryDispSync.setPeriod(16666667);
481 }
482
Mathias Agopian92a979a2012-08-02 18:32:23 -0700483 // initialize our drawing state
484 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700485
Andy McFadden13a082e2012-08-24 10:16:42 -0700486 // set initial conditions (e.g. unblank default device)
487 initializeDisplays();
488
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700489 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700490 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491}
492
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700493int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700494 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700495 type : mHwc->allocateDisplayId();
496}
497
Mathias Agopiana67e4182012-06-19 17:26:12 -0700498void SurfaceFlinger::startBootAnim() {
499 // start boot animation
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -0700500 mBootFinished = false;
Mathias Agopiana67e4182012-06-19 17:26:12 -0700501 property_set("service.bootanim.exit", "0");
502 property_set("ctl.start", "bootanim");
503}
504
Mathias Agopian875d8e12013-06-07 15:35:48 -0700505size_t SurfaceFlinger::getMaxTextureSize() const {
506 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700507}
508
Mathias Agopian875d8e12013-06-07 15:35:48 -0700509size_t SurfaceFlinger::getMaxViewportDims() const {
510 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700511}
512
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800513// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800514
Jamie Gennis582270d2011-08-17 18:19:00 -0700515bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800516 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800517 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800518 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700519 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800520}
521
Dan Stoza7f7da322014-05-02 15:26:25 -0700522status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
523 Vector<DisplayInfo>* configs) {
524 if (configs == NULL) {
525 return BAD_VALUE;
526 }
527
Jesse Hall692c7232012-11-08 15:41:56 -0800528 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700529 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800530 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700531 type = i;
532 break;
533 }
534 }
535
536 if (type < 0) {
537 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700538 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700539
Mathias Agopian8b736f12012-08-13 17:54:26 -0700540 // TODO: Not sure if display density should handled by SF any longer
541 class Density {
542 static int getDensityFromProperty(char const* propName) {
543 char property[PROPERTY_VALUE_MAX];
544 int density = 0;
545 if (property_get(propName, property, NULL) > 0) {
546 density = atoi(property);
547 }
548 return density;
549 }
550 public:
551 static int getEmuDensity() {
552 return getDensityFromProperty("qemu.sf.lcd_density"); }
553 static int getBuildDensity() {
554 return getDensityFromProperty("ro.sf.lcd_density"); }
555 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700556
Dan Stoza7f7da322014-05-02 15:26:25 -0700557 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700558
Dan Stoza7f7da322014-05-02 15:26:25 -0700559 const Vector<HWComposer::DisplayConfig>& hwConfigs =
560 getHwComposer().getConfigs(type);
561 for (size_t c = 0; c < hwConfigs.size(); ++c) {
562 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
563 DisplayInfo info = DisplayInfo();
564
565 float xdpi = hwConfig.xdpi;
566 float ydpi = hwConfig.ydpi;
567
568 if (type == DisplayDevice::DISPLAY_PRIMARY) {
569 // The density of the device is provided by a build property
570 float density = Density::getBuildDensity() / 160.0f;
571 if (density == 0) {
572 // the build doesn't provide a density -- this is wrong!
573 // use xdpi instead
574 ALOGE("ro.sf.lcd_density must be defined as a build property");
575 density = xdpi / 160.0f;
576 }
577 if (Density::getEmuDensity()) {
578 // if "qemu.sf.lcd_density" is specified, it overrides everything
579 xdpi = ydpi = density = Density::getEmuDensity();
580 density /= 160.0f;
581 }
582 info.density = density;
583
584 // TODO: this needs to go away (currently needed only by webkit)
585 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
586 info.orientation = hw->getOrientation();
587 } else {
588 // TODO: where should this value come from?
589 static const int TV_DENSITY = 213;
590 info.density = TV_DENSITY / 160.0f;
591 info.orientation = 0;
592 }
593
594 info.w = hwConfig.width;
595 info.h = hwConfig.height;
596 info.xdpi = xdpi;
597 info.ydpi = ydpi;
598 info.fps = float(1e9 / hwConfig.refresh);
Andy McFadden91b2ca82014-06-13 14:04:23 -0700599 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
600
601 // This is how far in advance a buffer must be queued for
602 // presentation at a given time. If you want a buffer to appear
603 // on the screen at time N, you must submit the buffer before
604 // (N - presentationDeadline).
605 //
606 // Normally it's one full refresh period (to give SF a chance to
607 // latch the buffer), but this can be reduced by configuring a
608 // DispSync offset. Any additional delays introduced by the hardware
609 // composer or panel must be accounted for here.
610 //
611 // We add an additional 1ms to allow for processing time and
612 // differences between the ideal and actual refresh rate.
613 info.presentationDeadline =
614 hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700615
616 // All non-virtual displays are currently considered secure.
617 info.secure = true;
618
619 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700620 }
621
Dan Stoza7f7da322014-05-02 15:26:25 -0700622 return NO_ERROR;
623}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700624
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700625status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& display,
626 DisplayStatInfo* stats) {
627 if (stats == NULL) {
628 return BAD_VALUE;
629 }
630
631 // FIXME for now we always return stats for the primary display
632 memset(stats, 0, sizeof(*stats));
633 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
634 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
635 return NO_ERROR;
636}
637
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700638int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
639 return getDisplayDevice(display)->getActiveConfig();
Dan Stoza7f7da322014-05-02 15:26:25 -0700640}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700641
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700642void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
643 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
644 this);
645 int32_t type = hw->getDisplayType();
646 int currentMode = hw->getActiveConfig();
647
648 if (mode == currentMode) {
649 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
650 return;
651 }
652
653 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
654 ALOGW("Trying to set config for virtual display");
655 return;
656 }
657
658 hw->setActiveConfig(mode);
659 getHwComposer().setActiveConfig(type, mode);
660}
661
662status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
663 class MessageSetActiveConfig: public MessageBase {
664 SurfaceFlinger& mFlinger;
665 sp<IBinder> mDisplay;
666 int mMode;
667 public:
668 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
669 int mode) :
670 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
671 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700672 Vector<DisplayInfo> configs;
673 mFlinger.getDisplayConfigs(mDisplay, &configs);
674 if(mMode < 0 || mMode >= configs.size()) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700675 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700676 mMode, configs.size());
677 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700678 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
679 if (hw == NULL) {
680 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700681 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700682 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
683 ALOGW("Attempt to set active config = %d for virtual display",
684 mMode);
685 } else {
686 mFlinger.setActiveConfigInternal(hw, mMode);
687 }
688 return true;
689 }
690 };
691 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
692 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700693 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700694}
695
Svetoslavd85084b2014-03-20 10:28:31 -0700696status_t SurfaceFlinger::clearAnimationFrameStats() {
697 Mutex::Autolock _l(mStateLock);
698 mAnimFrameTracker.clearStats();
699 return NO_ERROR;
700}
701
702status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
703 Mutex::Autolock _l(mStateLock);
704 mAnimFrameTracker.getStats(outStats);
705 return NO_ERROR;
706}
707
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800708// ----------------------------------------------------------------------------
709
710sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800711 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700712}
713
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800714// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800715
716void SurfaceFlinger::waitForEvent() {
717 mEventQueue.waitMessage();
718}
719
720void SurfaceFlinger::signalTransaction() {
721 mEventQueue.invalidate();
722}
723
724void SurfaceFlinger::signalLayerUpdate() {
725 mEventQueue.invalidate();
726}
727
728void SurfaceFlinger::signalRefresh() {
729 mEventQueue.refresh();
730}
731
732status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800733 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800734 return mEventQueue.postMessage(msg, reltime);
735}
736
737status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800738 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800739 status_t res = mEventQueue.postMessage(msg, reltime);
740 if (res == NO_ERROR) {
741 msg->wait();
742 }
743 return res;
744}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800745
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700746void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700747 do {
748 waitForEvent();
749 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800750}
751
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700752void SurfaceFlinger::enableHardwareVsync() {
753 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700754 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700755 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700756 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
757 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700758 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700759 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700760}
761
Jesse Hall948fe0c2013-10-14 12:56:09 -0700762void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700763 Mutex::Autolock _l(mHWVsyncLock);
764
Jesse Hall948fe0c2013-10-14 12:56:09 -0700765 if (makeAvailable) {
766 mHWVsyncAvailable = true;
767 } else if (!mHWVsyncAvailable) {
768 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
769 return;
770 }
771
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700772 const nsecs_t period =
773 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
774
775 mPrimaryDispSync.reset();
776 mPrimaryDispSync.setPeriod(period);
777
778 if (!mPrimaryHWVsyncEnabled) {
779 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700780 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
781 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700782 mPrimaryHWVsyncEnabled = true;
783 }
784}
785
Jesse Hall948fe0c2013-10-14 12:56:09 -0700786void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700787 Mutex::Autolock _l(mHWVsyncLock);
788 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700789 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
790 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700791 mPrimaryDispSync.endResync();
792 mPrimaryHWVsyncEnabled = false;
793 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700794 if (makeUnavailable) {
795 mHWVsyncAvailable = false;
796 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700797}
798
799void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700800 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700801
Jamie Gennisd1700752013-10-14 12:22:52 -0700802 { // Scope for the lock
803 Mutex::Autolock _l(mHWVsyncLock);
804 if (type == 0 && mPrimaryHWVsyncEnabled) {
805 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700806 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700807 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700808
809 if (needsHwVsync) {
810 enableHardwareVsync();
811 } else {
812 disableHardwareVsync(false);
813 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700814}
815
816void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
817 if (mEventThread == NULL) {
818 // This is a temporary workaround for b/7145521. A non-null pointer
819 // does not mean EventThread has finished initializing, so this
820 // is not a correct fix.
821 ALOGW("WARNING: EventThread not started, ignoring hotplug");
822 return;
823 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700824
Jesse Hall9e663de2013-08-16 14:28:37 -0700825 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700826 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800827 if (connected) {
828 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700829 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800830 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
831 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700832 }
833 setTransactionFlags(eDisplayTransactionNeeded);
834
Andy McFadden9e9689c2012-10-10 18:17:51 -0700835 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700836 }
Mathias Agopian86303202012-07-24 22:46:10 -0700837}
838
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700839void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700840 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700841 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700842}
843
Mathias Agopian4fec8732012-06-29 14:12:52 -0700844void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800845 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800846 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700847 case MessageQueue::TRANSACTION:
848 handleMessageTransaction();
849 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700850 case MessageQueue::INVALIDATE:
851 handleMessageTransaction();
852 handleMessageInvalidate();
853 signalRefresh();
854 break;
855 case MessageQueue::REFRESH:
856 handleMessageRefresh();
857 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800858 }
859}
860
Mathias Agopian4fec8732012-06-29 14:12:52 -0700861void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700862 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700863 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700864 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700865 }
866}
867
868void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700869 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700870 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700871}
872
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530873#ifdef QCOM_BSP
874/* Compute DirtyRegion, if DR optimization for GPU comp optimization
875 * is ON & and no external device is connected.*/
876void SurfaceFlinger::setUpTiledDr() {
877 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
878 const sp<DisplayDevice>& hw(mDisplays[HWC_DISPLAY_PRIMARY]);
879 mCanUseGpuTileRender = computeTiledDr(hw);
880 }
881}
882#endif
Mathias Agopian4fec8732012-06-29 14:12:52 -0700883void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700884 ATRACE_CALL();
885 preComposition();
886 rebuildLayerStacks();
887 setUpHWComposer();
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530888#ifdef QCOM_BSP
889 setUpTiledDr();
890#endif
Mathias Agopiancd60f992012-08-16 16:28:27 -0700891 doDebugFlashRegions();
892 doComposition();
893 postComposition();
894}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700895
Mathias Agopiancd60f992012-08-16 16:28:27 -0700896void SurfaceFlinger::doDebugFlashRegions()
897{
898 // is debugging enabled
899 if (CC_LIKELY(!mDebugRegion))
900 return;
901
902 const bool repaintEverything = mRepaintEverything;
903 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
904 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530905
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700906 if (hw->isDisplayOn()) {
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530907 const int32_t height = hw->getHeight();
908 RenderEngine& engine(getRenderEngine());
909#ifdef QCOM_BSP
910 // Use Union DR, if it is valid & GPU Tiled DR optimization is ON
911 if(mCanUseGpuTileRender && !mUnionDirtyRect.isEmpty()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700912 // redraw the whole screen
913 doComposeSurfaces(hw, Region(hw->bounds()));
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530914 Region dirtyRegion(mUnionDirtyRect);
915 Rect dr = mUnionDirtyRect;
916 hw->eglSwapPreserved(true);
917 engine.startTileComposition(dr.left, (height-dr.bottom),
918 (dr.right-dr.left),
919 (dr.bottom-dr.top), 1);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700920 // and draw the dirty region
Mathias Agopian3f844832013-08-07 21:24:32 -0700921 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530922 engine.endTileComposition(GL_PRESERVE);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700923 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700924 hw->swapBuffers(getHwComposer());
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530925 } else
926#endif
927 {
928 // transform the dirty region into this screen's coordinate
929 // space
930 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
931 if (!dirtyRegion.isEmpty()) {
932 // redraw the whole screen
933 doComposeSurfaces(hw, Region(hw->bounds()));
934 // and draw the dirty region
935#ifdef QCOM_BSP
936 if(mGpuTileRenderEnable)
937 hw->eglSwapPreserved(false);
938#endif
939
940 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
941 hw->compositionComplete();
942 hw->swapBuffers(getHwComposer());
943 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700944 }
945 }
946 }
947
948 postFramebuffer();
949
950 if (mDebugRegion > 1) {
951 usleep(mDebugRegion * 1000);
952 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700953
954 HWComposer& hwc(getHwComposer());
955 if (hwc.initCheck() == NO_ERROR) {
956 status_t err = hwc.prepare();
957 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
958 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700959}
960
961void SurfaceFlinger::preComposition()
962{
963 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700964 const LayerVector& layers(mDrawingState.layersSortedByZ);
965 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700966 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700967 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700968 needExtraInvalidate = true;
969 }
970 }
971 if (needExtraInvalidate) {
972 signalLayerUpdate();
973 }
974}
975
976void SurfaceFlinger::postComposition()
977{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700978 const LayerVector& layers(mDrawingState.layersSortedByZ);
979 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700980 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700981 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700982 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800983
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700984 const HWComposer& hwc = getHwComposer();
985 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
986
987 if (presentFence->isValid()) {
988 if (mPrimaryDispSync.addPresentFence(presentFence)) {
989 enableHardwareVsync();
990 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700991 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700992 }
993 }
994
Andy McFadden5167ec62014-05-22 13:08:43 -0700995 if (kIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700996 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700997 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700998 enableHardwareVsync();
999 }
1000 }
1001
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001002 if (mAnimCompositionPending) {
1003 mAnimCompositionPending = false;
1004
Jesse Halla9a1b002013-02-27 16:39:25 -08001005 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001006 mAnimFrameTracker.setActualPresentFence(presentFence);
1007 } else {
1008 // The HWC doesn't support present fences, so use the refresh
1009 // timestamp instead.
1010 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1011 mAnimFrameTracker.setActualPresentTime(presentTime);
1012 }
1013 mAnimFrameTracker.advanceFrame();
1014 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001015}
1016
1017void SurfaceFlinger::rebuildLayerStacks() {
1018 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001019 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001020 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -07001021 mVisibleRegionsDirty = false;
1022 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001023
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001024 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001025 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001026 Region opaqueRegion;
1027 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -08001028 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -07001029 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001030 const Transform& tr(hw->getTransform());
1031 const Rect bounds(hw->getBounds());
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001032 int dpyId = hw->getHwcDisplayId();
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001033 if (hw->isDisplayOn()) {
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001034 SurfaceFlinger::computeVisibleRegions(dpyId, layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001035 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001036
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001037 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001038 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001039 const sp<Layer>& layer(layers[i]);
1040 const Layer::State& s(layer->getDrawingState());
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001041 Region drawRegion(tr.transform(
1042 layer->visibleNonTransparentRegion));
1043 drawRegion.andSelf(bounds);
1044 if (!drawRegion.isEmpty()) {
1045 layersSortedByZ.add(layer);
Mathias Agopian87baae12012-07-31 12:38:26 -07001046 }
1047 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001048 }
Mathias Agopian42977342012-08-05 00:40:46 -07001049 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001050 hw->undefinedRegion.set(bounds);
1051 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
1052 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001053 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001054 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001055}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001056
Mathias Agopiancd60f992012-08-16 16:28:27 -07001057void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001058 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001059 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1060 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1061 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1062
1063 // If nothing has changed (!dirty), don't recompose.
1064 // If something changed, but we don't currently have any visible layers,
1065 // and didn't when we last did a composition, then skip it this time.
1066 // The second rule does two things:
1067 // - When all layers are removed from a display, we'll emit one black
1068 // frame, then nothing more until we get new layers.
1069 // - When a display is created with a private layer stack, we won't
1070 // emit any black frames until a layer is added to the layer stack.
1071 bool mustRecompose = dirty && !(empty && wasEmpty);
1072
1073 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1074 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1075 mustRecompose ? "doing" : "skipping",
1076 dirty ? "+" : "-",
1077 empty ? "+" : "-",
1078 wasEmpty ? "+" : "-");
1079
Dan Stoza71433162014-02-04 16:22:36 -08001080 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001081
1082 if (mustRecompose) {
1083 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1084 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001085 }
1086
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001087 HWComposer& hwc(getHwComposer());
1088 if (hwc.initCheck() == NO_ERROR) {
1089 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001090 if (CC_UNLIKELY(mHwWorkListDirty)) {
1091 mHwWorkListDirty = false;
1092 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1093 sp<const DisplayDevice> hw(mDisplays[dpy]);
1094 const int32_t id = hw->getHwcDisplayId();
1095 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001096 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001097 hw->getVisibleLayersSortedByZ());
1098 const size_t count = currentLayers.size();
1099 if (hwc.createWorkList(id, count) == NO_ERROR) {
1100 HWComposer::LayerListIterator cur = hwc.begin(id);
1101 const HWComposer::LayerListIterator end = hwc.end(id);
1102 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001103 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001104 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001105 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001106 cur->setSkip(true);
1107 }
1108 }
1109 }
1110 }
1111 }
1112 }
1113
1114 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001115 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001116 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001117 const int32_t id = hw->getHwcDisplayId();
1118 if (id >= 0) {
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001119 // Get the layers in the current drawying state
1120 const LayerVector& layers(mDrawingState.layersSortedByZ);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001121#ifdef QCOM_BSP
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001122 bool freezeSurfacePresent = false;
1123 const size_t layerCount = layers.size();
1124 char value[PROPERTY_VALUE_MAX];
1125 property_get("sys.disable_ext_animation", value, "0");
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001126 if(atoi(value) && (id != HWC_DISPLAY_PRIMARY)) {
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001127 for (size_t i = 0 ; i < layerCount ; ++i) {
1128 static int screenShotLen = strlen("ScreenshotSurface");
1129 const sp<Layer>& layer(layers[i]);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001130 const Layer::State& s(layer->getDrawingState());
1131 // check the layers associated with external display
1132 if(s.layerStack == hw->getLayerStack()) {
1133 if(!strncmp(layer->getName(), "ScreenshotSurface",
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001134 screenShotLen)) {
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001135 // Screenshot layer is present, and animation in
1136 // progress
1137 freezeSurfacePresent = true;
1138 break;
1139 }
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001140 }
1141 }
1142 }
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001143#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001144 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001145 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001146 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001147 HWComposer::LayerListIterator cur = hwc.begin(id);
1148 const HWComposer::LayerListIterator end = hwc.end(id);
1149 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1150 /*
1151 * update the per-frame h/w composer data for each layer
1152 * and build the transparent region of the FB
1153 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001154 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001155 layer->setPerFrameData(hw, *cur);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001156#ifdef QCOM_BSP
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001157 if(freezeSurfacePresent) {
1158 // if freezeSurfacePresent, set ANIMATING flag
1159 cur->setAnimating(true);
1160 } else {
1161 const KeyedVector<wp<IBinder>, DisplayDeviceState>&
1162 draw(mDrawingState.displays);
1163 size_t dc = draw.size();
1164 for (size_t i=0 ; i<dc ; i++) {
1165 if (draw[i].isMainDisplay()) {
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001166 // Pass the current orientation to HWC
1167 hwc.eventControl(HWC_DISPLAY_PRIMARY,
1168 SurfaceFlinger::EVENT_ORIENTATION,
1169 uint32_t(draw[i].orientation));
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001170 }
1171 }
1172 }
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001173#endif
Mathias Agopian1e260872012-08-08 18:35:12 -07001174 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001175 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001176 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001177
Riley Andrews03414a12014-07-01 14:22:59 -07001178 // If possible, attempt to use the cursor overlay on each display.
1179 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1180 sp<const DisplayDevice> hw(mDisplays[dpy]);
1181 const int32_t id = hw->getHwcDisplayId();
1182 if (id >= 0) {
1183 const Vector< sp<Layer> >& currentLayers(
1184 hw->getVisibleLayersSortedByZ());
1185 const size_t count = currentLayers.size();
1186 HWComposer::LayerListIterator cur = hwc.begin(id);
1187 const HWComposer::LayerListIterator end = hwc.end(id);
1188 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1189 const sp<Layer>& layer(currentLayers[i]);
1190 if (layer->isPotentialCursor()) {
1191 cur->setIsCursorLayerHint();
1192 break;
1193 }
1194 }
1195 }
1196 }
1197
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001198 status_t err = hwc.prepare();
1199 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001200
1201 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1202 sp<const DisplayDevice> hw(mDisplays[dpy]);
1203 hw->prepareFrame(hwc);
1204 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001205 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001206}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001207
Mathias Agopiancd60f992012-08-16 16:28:27 -07001208void SurfaceFlinger::doComposition() {
1209 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001210 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001211 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001212 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001213 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001214 // transform the dirty region into this screen's coordinate space
1215 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001216
1217 // repaint the framebuffer (if needed)
1218 doDisplayComposition(hw, dirtyRegion);
1219
Mathias Agopiancd60f992012-08-16 16:28:27 -07001220 hw->dirtyRegion.clear();
1221 hw->flip(hw->swapRegion);
1222 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001223 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001224 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001225 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001226 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001227 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001228}
1229
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001230void SurfaceFlinger::postFramebuffer()
1231{
Mathias Agopian841cde52012-03-01 15:44:37 -08001232 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001233
Mathias Agopiana44b0412011-10-16 18:46:35 -07001234 const nsecs_t now = systemTime();
1235 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001236
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001237 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001238 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001239 if (!hwc.supportsFramebufferTarget()) {
1240 // EGL spec says:
1241 // "surface must be bound to the calling thread's current context,
1242 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001243 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001244 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001245 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001246 }
1247
Mathias Agopian6da15f42013-09-25 20:40:07 -07001248 // make the default display current because the VirtualDisplayDevice code cannot
1249 // deal with dequeueBuffer() being called outside of the composition loop; however
1250 // the code below can call glFlush() which is allowed (and does in some case) call
1251 // dequeueBuffer().
1252 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1253
Mathias Agopian92a979a2012-08-02 18:32:23 -07001254 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001255 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001256 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001257 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001258 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001259 int32_t id = hw->getHwcDisplayId();
1260 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001261 HWComposer::LayerListIterator cur = hwc.begin(id);
1262 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001263 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001264 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001265 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001266 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001267 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001268 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001269 }
Jesse Hallef194142012-06-14 14:45:17 -07001270 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001271 }
1272
Mathias Agopiana44b0412011-10-16 18:46:35 -07001273 mLastSwapBufferTime = systemTime() - now;
1274 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001275
1276 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1277 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1278 logFrameStats();
1279 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001280}
1281
Mathias Agopian87baae12012-07-31 12:38:26 -07001282void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283{
Mathias Agopian841cde52012-03-01 15:44:37 -08001284 ATRACE_CALL();
1285
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001286 // here we keep a copy of the drawing state (that is the state that's
1287 // going to be overwritten by handleTransactionLocked()) outside of
1288 // mStateLock so that the side-effects of the State assignment
1289 // don't happen with mStateLock held (which can cause deadlocks).
1290 State drawingState(mDrawingState);
1291
Mathias Agopianca4d3602011-05-19 15:38:14 -07001292 Mutex::Autolock _l(mStateLock);
1293 const nsecs_t now = systemTime();
1294 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001295
Mathias Agopianca4d3602011-05-19 15:38:14 -07001296 // Here we're guaranteed that some transaction flags are set
1297 // so we can call handleTransactionLocked() unconditionally.
1298 // We call getTransactionFlags(), which will also clear the flags,
1299 // with mStateLock held to guarantee that mCurrentState won't change
1300 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001301
Mathias Agopiane57f2922012-08-09 16:29:12 -07001302 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001303 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001304
Mathias Agopianca4d3602011-05-19 15:38:14 -07001305 mLastTransactionTime = systemTime() - now;
1306 mDebugInTransaction = 0;
1307 invalidateHwcGeometry();
1308 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001309}
1310
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301311void SurfaceFlinger::setVirtualDisplayData(
1312 int32_t hwcDisplayId,
1313 const sp<IGraphicBufferProducer>& sink)
1314{
1315 sp<ANativeWindow> mNativeWindow = new Surface(sink);
1316 ANativeWindow* const window = mNativeWindow.get();
1317
1318 int format;
1319 window->query(window, NATIVE_WINDOW_FORMAT, &format);
1320
1321 EGLSurface surface;
1322 EGLint w, h;
1323 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1324 surface = eglCreateWindowSurface(display, mRenderEngine->getEGLConfig(), window, NULL);
1325 eglQuerySurface(display, surface, EGL_WIDTH, &w);
1326 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
1327
1328 mHwc->setVirtualDisplayProperties(hwcDisplayId, w, h, format);
1329}
1330
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001331void SurfaceFlinger::configureVirtualDisplay(int32_t &hwcDisplayId,
1332 sp<DisplaySurface> &dispSurface,
1333 sp<IGraphicBufferProducer> &producer,
1334 const DisplayDeviceState state,
1335 sp<IGraphicBufferProducer> bqProducer,
1336 sp<IGraphicBufferConsumer> bqConsumer)
1337{
1338 bool vdsEnabled = mHwc->isVDSEnabled();
1339
1340 //for V4L2 based virtual display implementation
1341 if(!vdsEnabled) {
1342 // persist.sys.wfd.virtual will be set if WFD is launched via
1343 // settings app. This is currently being done in
1344 // ExtendedRemoteDisplay-WFD stack.
1345 // This flag will be reset at the time of disconnection of virtual WFD
1346 // display.
1347 // This flag is set to zero if WFD is launched via QCOM WFD
1348 // proprietary APIs which use HDMI piggyback approach.
1349 char value[PROPERTY_VALUE_MAX];
1350 property_get("persist.sys.wfd.virtual", value, "0");
1351 int wfdVirtual = atoi(value);
1352 if(!wfdVirtual) {
1353 // This is for non-wfd virtual display scenarios(e.g. SSD/SR/CTS)
1354 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001355 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1356 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001357 dispSurface = vds;
1358 // There won't be any interaction with HWC for this virtual display.
1359 // so the GLES driver can pass buffers directly to the sink.
1360 producer = state.surface;
1361 } else {
Raj Kamal366d9b42014-06-11 13:49:32 +05301362 int sinkUsage = -1;
1363 state.surface->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
1364#ifdef QCOM_BSP
1365 if(sinkUsage & GRALLOC_USAGE_PRIVATE_WFD)
1366#endif
1367 hwcDisplayId = allocateHwcDisplayId(state.type);
1368
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001369 if (hwcDisplayId >= 0) {
1370 // This is for WFD virtual display scenario.
1371 // Read virtual display properties and create a
1372 // rendering surface for it inorder to be handled by hwc.
1373 setVirtualDisplayData(hwcDisplayId, state.surface);
1374 dispSurface = new FramebufferSurface(*mHwc, state.type,
1375 bqConsumer);
1376 producer = bqProducer;
1377 } else {
1378 // in case of WFD Virtual + SSD/SR concurrency scenario,
1379 // WFD virtual display instance gets valid hwcDisplayId and
1380 // SSD/SR will get invalid hwcDisplayId
1381 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001382 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1383 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001384 dispSurface = vds;
1385 // There won't be any interaction with HWC for this virtual
1386 // display, so the GLES driver can pass buffers directly to the
1387 // sink.
1388 producer = state.surface;
1389 }
1390 }
1391 } else {
1392 // VDS solution is enabled
1393 // HWC is allocated for first virtual display.
1394 // Subsequent virtual display sessions will be composed by GLES driver.
1395 // ToDo: Modify VDS component to allocate hwcDisplayId based on
1396 // mForceHwcCopy (which is based on Usage Flags)
1397
1398 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001399 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1400 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001401 dispSurface = vds;
1402 if (hwcDisplayId >= 0) {
1403 producer = vds;
1404 } else {
1405 // There won't be any interaction with HWC for this virtual display,
1406 // so the GLES driver can pass buffers directly to the sink.
1407 producer = state.surface;
1408 }
1409 }
1410}
1411
Mathias Agopian87baae12012-07-31 12:38:26 -07001412void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001413{
1414 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415 const size_t count = currentLayers.size();
1416
1417 /*
1418 * Traversal of the children
1419 * (perform the transaction for each of them if needed)
1420 */
1421
Mathias Agopian3559b072012-08-15 13:46:03 -07001422 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001423 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001424 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1426 if (!trFlags) continue;
1427
1428 const uint32_t flags = layer->doTransaction(0);
1429 if (flags & Layer::eVisibleRegion)
1430 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431 }
1432 }
1433
1434 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001435 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001436 */
1437
Mathias Agopiane57f2922012-08-09 16:29:12 -07001438 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001439 // here we take advantage of Vector's copy-on-write semantics to
1440 // improve performance by skipping the transaction entirely when
1441 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001442 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1443 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001444 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001445 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001446 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001447 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001448
1449 // find the displays that were removed
1450 // (ie: in drawing state but not in current state)
1451 // also handle displays that changed
1452 // (ie: displays that are in both lists)
1453 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001454 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1455 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001456 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001457 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001458 // Call makeCurrent() on the primary display so we can
1459 // be sure that nothing associated with this display
1460 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001461 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001462 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001463 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1464 if (hw != NULL)
1465 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001466 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001467 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001468 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001469 } else {
1470 ALOGW("trying to remove the main display");
1471 }
1472 } else {
1473 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001474 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001475 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001476 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001477 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001478 // recreating the DisplayDevice, so we just remove it
1479 // from the drawing state, so that it get re-added
1480 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001481 sp<DisplayDevice> hw(getDisplayDevice(display));
1482 if (hw != NULL)
1483 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001484 mDisplays.removeItem(display);
1485 mDrawingState.displays.removeItemsAt(i);
1486 dc--; i--;
1487 // at this point we must loop to the next item
1488 continue;
1489 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001490
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001491 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001492 if (disp != NULL) {
1493 if (state.layerStack != draw[i].layerStack) {
1494 disp->setLayerStack(state.layerStack);
1495 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001496 if ((state.orientation != draw[i].orientation)
1497 || (state.viewport != draw[i].viewport)
1498 || (state.frame != draw[i].frame))
1499 {
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001500#ifdef QCOM_BSP
1501 int orient = state.orientation;
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001502 // Honor the orientation change after boot
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301503 // animation completes and make sure boot
1504 // animation is shown in panel orientation always.
1505 if(mBootFinished){
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001506 disp->setProjection(state.orientation,
1507 state.viewport, state.frame);
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001508 orient = state.orientation;
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001509 }
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301510 else{
1511 char property[PROPERTY_VALUE_MAX];
1512 int panelOrientation =
1513 DisplayState::eOrientationDefault;
1514 if(property_get("persist.panel.orientation",
1515 property, "0") > 0){
1516 panelOrientation = atoi(property) / 90;
1517 }
1518 disp->setProjection(panelOrientation,
1519 state.viewport, state.frame);
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001520 orient = panelOrientation;
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301521 }
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001522 // Set the view frame of each display only of its
1523 // default orientation.
Saurabh Shah027b3852014-08-21 16:09:32 -07001524 if(orient == DisplayState::eOrientationDefault and
1525 state.frame.isValid()) {
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001526 qdutils::setViewFrame(disp->getHwcDisplayId(),
1527 state.frame.left, state.frame.top,
1528 state.frame.right, state.frame.bottom);
1529 }
1530#else
1531 disp->setProjection(state.orientation,
1532 state.viewport, state.frame);
1533#endif
Mathias Agopian93997a82012-08-29 17:30:36 -07001534 }
Michael Lentine47e45402014-07-18 15:34:25 -07001535 if (state.width != draw[i].width || state.height != draw[i].height) {
1536 disp->setDisplaySize(state.width, state.height);
1537 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001538 }
1539 }
1540 }
1541
1542 // find displays that were added
1543 // (ie: in current state but not in drawing state)
1544 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001545 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001546 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001547
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001548 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001549 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001550 sp<IGraphicBufferProducer> bqProducer;
1551 sp<IGraphicBufferConsumer> bqConsumer;
1552 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1553 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001554
Jesse Hall02d86562013-03-25 14:43:23 -07001555 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001556 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001557 // Virtual displays without a surface are dormant:
1558 // they have external state (layer stack, projection,
1559 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001560 if (state.surface != NULL) {
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001561 configureVirtualDisplay(hwcDisplayId,
1562 dispSurface, producer, state, bqProducer,
1563 bqConsumer);
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001564 }
1565 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001566 ALOGE_IF(state.surface!=NULL,
1567 "adding a supported display, but rendering "
1568 "surface is provided (%p), ignoring it",
1569 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001570 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001571 // for supported (by hwc) displays we provide our
1572 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001573 dispSurface = new FramebufferSurface(*mHwc, state.type,
1574 bqConsumer);
1575 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001576 }
1577
1578 const wp<IBinder>& display(curr.keyAt(i));
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301579 if (dispSurface != NULL && producer != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001580 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001581 state.type, hwcDisplayId,
1582 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001583 display, dispSurface, producer,
1584 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001585 hw->setLayerStack(state.layerStack);
1586 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001587 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001588 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001589 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001590 if (state.isVirtualDisplay()) {
1591 if (hwcDisplayId >= 0) {
1592 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1593 hw->getWidth(), hw->getHeight(),
1594 hw->getFormat());
1595 }
1596 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001597 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001598 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001599 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001600 }
1601 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001602 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001603 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001604
Mathias Agopian84300952012-11-21 16:02:13 -08001605 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1606 // The transform hint might have changed for some layers
1607 // (either because a display has changed, or because a layer
1608 // as changed).
1609 //
1610 // Walk through all the layers in currentLayers,
1611 // and update their transform hint.
1612 //
1613 // If a layer is visible only on a single display, then that
1614 // display is used to calculate the hint, otherwise we use the
1615 // default display.
1616 //
1617 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1618 // the hint is set before we acquire a buffer from the surface texture.
1619 //
1620 // NOTE: layer transactions have taken place already, so we use their
1621 // drawing state. However, SurfaceFlinger's own transaction has not
1622 // happened yet, so we must use the current state layer list
1623 // (soon to become the drawing state list).
1624 //
1625 sp<const DisplayDevice> disp;
1626 uint32_t currentlayerStack = 0;
1627 for (size_t i=0; i<count; i++) {
1628 // NOTE: we rely on the fact that layers are sorted by
1629 // layerStack first (so we don't have to traverse the list
1630 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001631 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001632 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001633 if (i==0 || currentlayerStack != layerStack) {
1634 currentlayerStack = layerStack;
1635 // figure out if this layerstack is mirrored
1636 // (more than one display) if so, pick the default display,
1637 // if not, pick the only display it's on.
1638 disp.clear();
1639 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1640 sp<const DisplayDevice> hw(mDisplays[dpy]);
1641 if (hw->getLayerStack() == currentlayerStack) {
1642 if (disp == NULL) {
1643 disp = hw;
1644 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001645 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001646 break;
1647 }
1648 }
1649 }
1650 }
Chet Haase91d25932013-04-11 15:24:55 -07001651 if (disp == NULL) {
1652 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1653 // redraw after transform hint changes. See bug 8508397.
1654
1655 // could be null when this layer is using a layerStack
1656 // that is not visible on any display. Also can occur at
1657 // screen off/on times.
1658 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001659 }
Chet Haase91d25932013-04-11 15:24:55 -07001660 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001661 }
1662 }
1663
1664
Mathias Agopian3559b072012-08-15 13:46:03 -07001665 /*
1666 * Perform our own transaction if needed
1667 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001668
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001669 const LayerVector& layers(mDrawingState.layersSortedByZ);
1670 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001671 // layers have been added
1672 mVisibleRegionsDirty = true;
1673 }
1674
1675 // some layers might have been removed, so
1676 // we need to update the regions they're exposing.
1677 if (mLayersRemoved) {
1678 mLayersRemoved = false;
1679 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001680 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001681 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001682 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001683 if (currentLayers.indexOf(layer) < 0) {
1684 // this layer is not visible anymore
1685 // TODO: we could traverse the tree from front to back and
1686 // compute the actual visible region
1687 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001688 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001689 Region visibleReg = s.transform.transform(
1690 Region(Rect(s.active.w, s.active.h)));
1691 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001692 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001693 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001694 }
1695
1696 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001697
1698 updateCursorAsync();
1699}
1700
1701void SurfaceFlinger::updateCursorAsync()
1702{
1703 HWComposer& hwc(getHwComposer());
1704 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1705 sp<const DisplayDevice> hw(mDisplays[dpy]);
1706 const int32_t id = hw->getHwcDisplayId();
1707 if (id < 0) {
1708 continue;
1709 }
1710 const Vector< sp<Layer> >& currentLayers(
1711 hw->getVisibleLayersSortedByZ());
1712 const size_t count = currentLayers.size();
1713 HWComposer::LayerListIterator cur = hwc.begin(id);
1714 const HWComposer::LayerListIterator end = hwc.end(id);
1715 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1716 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1717 continue;
1718 }
1719 const sp<Layer>& layer(currentLayers[i]);
1720 Rect cursorPos = layer->getPosition(hw);
1721 hwc.setCursorPositionAsync(id, cursorPos);
1722 break;
1723 }
1724 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001725}
1726
1727void SurfaceFlinger::commitTransaction()
1728{
1729 if (!mLayersPendingRemoval.isEmpty()) {
1730 // Notify removed layers now that they can't be drawn from
1731 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1732 mLayersPendingRemoval[i]->onRemoved();
1733 }
1734 mLayersPendingRemoval.clear();
1735 }
1736
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001737 // If this transaction is part of a window animation then the next frame
1738 // we composite should be considered an animation as well.
1739 mAnimCompositionPending = mAnimTransactionPending;
1740
Mathias Agopian4fec8732012-06-29 14:12:52 -07001741 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001742 mTransactionPending = false;
1743 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001744 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001745}
1746
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001747void SurfaceFlinger::computeVisibleRegions(size_t dpy,
Mathias Agopian87baae12012-07-31 12:38:26 -07001748 const LayerVector& currentLayers, uint32_t layerStack,
1749 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001750{
Mathias Agopian841cde52012-03-01 15:44:37 -08001751 ATRACE_CALL();
1752
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001753 Region aboveOpaqueLayers;
1754 Region aboveCoveredLayers;
1755 Region dirty;
1756
Mathias Agopian87baae12012-07-31 12:38:26 -07001757 outDirtyRegion.clear();
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001758 bool bIgnoreLayers = false;
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001759 int indexLOI = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001760 size_t i = currentLayers.size();
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001761#ifdef QCOM_BSP
1762 while (i--) {
1763 const sp<Layer>& layer = currentLayers[i];
1764 // iterate through the layer list to find ext_only layers and store
1765 // the index
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001766 if (layer->isSecureDisplay()) {
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001767 bIgnoreLayers = true;
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001768 indexLOI = -1;
1769 if(!dpy)
1770 indexLOI = i;
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001771 break;
1772 }
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001773
1774 if (dpy && layer->isExtOnly()) {
1775 bIgnoreLayers = true;
1776 indexLOI = i;
1777 }
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001778 }
1779 i = currentLayers.size();
1780#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001781 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001782 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001783
1784 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001785 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001786
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001787#ifdef QCOM_BSP
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001788 // Only add the layer marked as "external_only" to external list and
1789 // only remove the layer marked as "external_only" from primary list
1790 // and do not add the layer marked as "internal_only" to external list
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001791 // Add secure UI layers to primary and remove other layers from internal
1792 //and external list
1793 if((bIgnoreLayers && indexLOI != (int)i) ||
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001794 (!dpy && layer->isExtOnly()) ||
1795 (dpy && layer->isIntOnly())) {
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001796 // Ignore all other layers except the layers marked as ext_only
1797 // by setting visible non transparent region empty.
1798 Region visibleNonTransRegion;
1799 visibleNonTransRegion.set(Rect(0,0));
1800 layer->setVisibleNonTransparentRegion(visibleNonTransRegion);
1801 continue;
1802 }
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001803#endif
1804 // only consider the layers on the given later stack
1805 // Override layers created using presentation class by the layers having
1806 // ext_only flag enabled
1807 if(s.layerStack != layerStack && !bIgnoreLayers) {
1808 // set the visible region as empty since we have removed the
1809 // layerstack check in rebuildLayerStack() function.
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001810 Region visibleNonTransRegion;
1811 visibleNonTransRegion.set(Rect(0,0));
1812 layer->setVisibleNonTransparentRegion(visibleNonTransRegion);
1813 continue;
1814 }
Mathias Agopianab028732010-03-16 16:41:46 -07001815 /*
1816 * opaqueRegion: area of a surface that is fully opaque.
1817 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001818 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001819
1820 /*
1821 * visibleRegion: area of a surface that is visible on screen
1822 * and not fully transparent. This is essentially the layer's
1823 * footprint minus the opaque regions above it.
1824 * Areas covered by a translucent surface are considered visible.
1825 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001826 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001827
1828 /*
1829 * coveredRegion: area of a surface that is covered by all
1830 * visible regions above it (which includes the translucent areas).
1831 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001832 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001833
Jesse Halla8026d22012-09-25 13:25:04 -07001834 /*
1835 * transparentRegion: area of a surface that is hinted to be completely
1836 * transparent. This is only used to tell when the layer has no visible
1837 * non-transparent regions and can be removed from the layer list. It
1838 * does not affect the visibleRegion of this layer or any layers
1839 * beneath it. The hint may not be correct if apps don't respect the
1840 * SurfaceView restrictions (which, sadly, some don't).
1841 */
1842 Region transparentRegion;
1843
Mathias Agopianab028732010-03-16 16:41:46 -07001844
1845 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001846 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001847 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001848 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001849 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001850 if (!visibleRegion.isEmpty()) {
1851 // Remove the transparent area from the visible region
1852 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001853 const Transform tr(s.transform);
1854 if (tr.transformed()) {
1855 if (tr.preserveRects()) {
1856 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001857 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001858 } else {
1859 // transformation too complex, can't do the
1860 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001861 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001862 }
1863 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001864 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001865 }
Mathias Agopianab028732010-03-16 16:41:46 -07001866 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001867
Mathias Agopianab028732010-03-16 16:41:46 -07001868 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001869 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001870 if (s.alpha==255 && !translucent &&
1871 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1872 // the opaque region is the layer's footprint
1873 opaqueRegion = visibleRegion;
1874 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001875 }
1876 }
1877
Mathias Agopianab028732010-03-16 16:41:46 -07001878 // Clip the covered region to the visible region
1879 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1880
1881 // Update aboveCoveredLayers for next (lower) layer
1882 aboveCoveredLayers.orSelf(visibleRegion);
1883
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001884 // subtract the opaque region covered by the layers above us
1885 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001886
1887 // compute this layer's dirty region
1888 if (layer->contentDirty) {
1889 // we need to invalidate the whole region
1890 dirty = visibleRegion;
1891 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001892 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001893 layer->contentDirty = false;
1894 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001895 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001896 * the exposed region consists of two components:
1897 * 1) what's VISIBLE now and was COVERED before
1898 * 2) what's EXPOSED now less what was EXPOSED before
1899 *
1900 * note that (1) is conservative, we start with the whole
1901 * visible region but only keep what used to be covered by
1902 * something -- which mean it may have been exposed.
1903 *
1904 * (2) handles areas that were not covered by anything but got
1905 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001906 */
Mathias Agopianab028732010-03-16 16:41:46 -07001907 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001908 const Region oldVisibleRegion = layer->visibleRegion;
1909 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001910 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1911 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001912 }
1913 dirty.subtractSelf(aboveOpaqueLayers);
1914
1915 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001916 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001917
Mathias Agopianab028732010-03-16 16:41:46 -07001918 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001919 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001920
Jesse Halla8026d22012-09-25 13:25:04 -07001921 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001922 layer->setVisibleRegion(visibleRegion);
1923 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001924 layer->setVisibleNonTransparentRegion(
1925 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001926 }
1927
Mathias Agopian87baae12012-07-31 12:38:26 -07001928 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001929}
1930
Mathias Agopian87baae12012-07-31 12:38:26 -07001931void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1932 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001933 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001934 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1935 if (hw->getLayerStack() == layerStack) {
1936 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001937 }
1938 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001939}
1940
1941void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001942{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001943 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001944
Mathias Agopian4fec8732012-06-29 14:12:52 -07001945 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001946 const LayerVector& layers(mDrawingState.layersSortedByZ);
Eric Penner51c59cd2014-07-28 19:51:58 -07001947
1948 // Store the set of layers that need updates. This set must not change as
1949 // buffers are being latched, as this could result in a deadlock.
1950 // Example: Two producers share the same command stream and:
1951 // 1.) Layer 0 is latched
1952 // 2.) Layer 0 gets a new frame
1953 // 2.) Layer 1 gets a new frame
1954 // 3.) Layer 1 is latched.
1955 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1956 // second frame. But layer 0's second frame could be waiting on display.
1957 Vector<Layer*> layersWithQueuedFrames;
1958 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001959 const sp<Layer>& layer(layers[i]);
Eric Penner51c59cd2014-07-28 19:51:58 -07001960 if (layer->hasQueuedFrame())
1961 layersWithQueuedFrames.push_back(layer.get());
1962 }
1963 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
1964 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07001965 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001966 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001967 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001968 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001969
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001970 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001971}
1972
Mathias Agopianad456f92011-01-13 17:53:01 -08001973void SurfaceFlinger::invalidateHwcGeometry()
1974{
1975 mHwWorkListDirty = true;
1976}
1977
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001978
Mathias Agopiancd60f992012-08-16 16:28:27 -07001979void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001980 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001981{
Dan Stoza71433162014-02-04 16:22:36 -08001982 // We only need to actually compose the display if:
1983 // 1) It is being handled by hardware composer, which may need this to
1984 // keep its virtual display state machine in sync, or
1985 // 2) There is work to be done (the dirty region isn't empty)
1986 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1987 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1988 return;
1989 }
1990
Mathias Agopian87baae12012-07-31 12:38:26 -07001991 Region dirtyRegion(inDirtyRegion);
1992
Mathias Agopianb8a55602009-06-26 19:06:36 -07001993 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001994 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001995
Mathias Agopian42977342012-08-05 00:40:46 -07001996 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001997 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001998 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1999 // takes a rectangle, we must make sure to update that whole
2000 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07002001 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002002 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002003 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002004 // We need to redraw the rectangle that will be updated
2005 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07002006 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002007 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07002008 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002009 } else {
2010 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07002011 dirtyRegion.set(hw->bounds());
2012 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002013 }
2014 }
2015
Alan Viverette9c5a3332013-09-12 20:04:35 -07002016 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Michael Lentine3f121fc2014-10-01 11:17:28 -07002017 if (!doComposeSurfaces(hw, dirtyRegion)) return;
Mathias Agopianff2ed702013-09-01 21:36:12 -07002018 } else {
2019 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07002020 mat4 colorMatrix = mColorMatrix;
2021 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07002022 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002023 }
2024 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07002025 doComposeSurfaces(hw, dirtyRegion);
2026 engine.endGroup();
2027 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002028
Mathias Agopian9c6e2972011-09-20 17:21:56 -07002029 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07002030 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07002031
2032 // swap buffers (presentation)
2033 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002034}
2035
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302036#ifdef QCOM_BSP
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302037bool SurfaceFlinger::computeTiledDr(const sp<const DisplayDevice>& hw) {
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302038 int fbWidth= hw->getWidth();
2039 int fbHeight= hw->getHeight();
2040 Rect fullScreenRect = Rect(0,0,fbWidth, fbHeight);
2041 const int32_t id = hw->getHwcDisplayId();
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302042 mUnionDirtyRect.clear();
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302043 HWComposer& hwc(getHwComposer());
2044
2045 /* Compute and return the Union of Dirty Rects.
2046 * Return false if the unionDR is fullscreen, as there is no benefit from
2047 * preserving full screen.*/
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302048 return (hwc.canUseTiledDR(id, mUnionDirtyRect) &&
2049 (mUnionDirtyRect != fullScreenRect));
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302050
2051}
2052#endif
2053
Michael Lentine3f121fc2014-10-01 11:17:28 -07002054bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07002055{
Mathias Agopian3f844832013-08-07 21:24:32 -07002056 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002057 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07002058 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07002059 HWComposer::LayerListIterator cur = hwc.begin(id);
2060 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07002061
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302062 Region clearRegion;
Jesse Halld05a17f2013-09-30 16:49:30 -07002063 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002064 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07002065 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08002066 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
2067 hw->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07002068 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
2069 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
2070 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
2071 }
2072 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08002073 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07002074
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07002075 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07002076 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07002077 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07002078 // when using overlays, we assume a fully transparent framebuffer
2079 // NOTE: we could reduce how much we need to clear, for instance
2080 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07002081 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07002082 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07002083 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002084 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07002085 // we start with the whole screen area
2086 const Region bounds(hw->getBounds());
2087
2088 // we remove the scissor part
2089 // we're left with the letterbox region
2090 // (common case is that letterbox ends-up being empty)
2091 const Region letterbox(bounds.subtract(hw->getScissor()));
2092
2093 // compute the area to clear
2094 Region region(hw->undefinedRegion.merge(letterbox));
2095
2096 // but limit it to the dirty region
2097 region.andSelf(dirty);
2098
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302099
Mathias Agopianb9494d52012-04-18 02:28:45 -07002100 // screen is already cleared here
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302101#ifdef QCOM_BSP
2102 clearRegion.clear();
2103 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
2104 clearRegion = region;
2105 if (cur == end) {
Radhika Ranjan Soni35c9ff22013-12-17 19:55:17 +05302106 drawWormhole(hw, region);
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302107 } else if(mCanUseGpuTileRender) {
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302108 /* If GPUTileRect DR optimization on clear only the UnionDR
2109 * (computed by computeTiledDr) which is the actual region
2110 * that will be drawn on FB in this cycle.. */
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302111 clearRegion = clearRegion.andSelf(Region(mUnionDirtyRect));
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302112 }
2113 } else
2114#endif
2115 {
2116 if (!region.isEmpty()) {
2117 if (cur != end) {
2118 if (cur->getCompositionType() != HWC_BLIT)
2119 // can happen with SurfaceView
2120 drawWormhole(hw, region);
2121 } else
2122 drawWormhole(hw, region);
2123 }
Mathias Agopianb9494d52012-04-18 02:28:45 -07002124 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002125 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002126
Mathias Agopian766dc492012-10-30 18:08:06 -07002127 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
2128 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07002129 // scissor on the main display. It should never be needed
2130 // anyways (though in theory it could since the API allows it).
2131 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07002132 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002133 if (scissor != bounds) {
2134 // scissor doesn't match the screen's dimensions, so we
2135 // need to clear everything outside of it and enable
2136 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07002137
Mathias Agopianf45c5102012-10-24 16:29:17 -07002138 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002139 const uint32_t height = hw->getHeight();
2140 engine.setScissor(scissor.left, height - scissor.bottom,
2141 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002142 }
2143 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002144 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002145
Mathias Agopian85d751c2012-08-29 16:59:24 -07002146 /*
2147 * and then, render the layers targeted at the framebuffer
2148 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002149
Mathias Agopian13127d82013-03-05 17:47:11 -08002150 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002151 const size_t count = layers.size();
2152 const Transform& tr = hw->getTransform();
2153 if (cur != end) {
2154 // we're using h/w composer
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302155#ifdef QCOM_BSP
2156 int fbWidth= hw->getWidth();
2157 int fbHeight= hw->getHeight();
2158 /* if GPUTileRender optimization property is on & can be used
2159 * i) Enable EGL_SWAP_PRESERVED flag
2160 * ii) do startTile with union DirtyRect
2161 * else , Disable EGL_SWAP_PRESERVED */
2162 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302163 if(mCanUseGpuTileRender && !mUnionDirtyRect.isEmpty()) {
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302164 hw->eglSwapPreserved(true);
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302165 Rect dr = mUnionDirtyRect;
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302166 engine.startTileComposition(dr.left, (fbHeight-dr.bottom),
2167 (dr.right-dr.left),
2168 (dr.bottom-dr.top), 0);
2169 } else {
2170 // Un Set EGL_SWAP_PRESERVED flag, if no tiling required.
2171 hw->eglSwapPreserved(false);
2172 }
2173 // DrawWormHole/Any Draw has to be within startTile & EndTile
2174 if (cur->getCompositionType() != HWC_BLIT &&
2175 !clearRegion.isEmpty()){
2176 drawWormhole(hw, clearRegion);
2177 }
2178 }
2179#endif
2180
Mathias Agopian85d751c2012-08-29 16:59:24 -07002181 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002182 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07002183 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07002184 if (!clip.isEmpty()) {
2185 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07002186 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07002187 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07002188 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002189 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
2190 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08002191 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07002192 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07002193 // never clear the very first layer since we're
2194 // guaranteed the FB is already cleared
2195 layer->clearWithOpenGL(hw, clip);
2196 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002197 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07002198 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002199 case HWC_FRAMEBUFFER: {
2200 layer->draw(hw, clip);
2201 break;
2202 }
Arun Kumar K.R5111ff02012-12-19 18:10:46 -08002203 case HWC_BLIT:
2204 //Do nothing
2205 break;
Mathias Agopianda27af92012-09-13 18:17:13 -07002206 case HWC_FRAMEBUFFER_TARGET: {
2207 // this should not happen as the iterator shouldn't
2208 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08002209 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07002210 break;
2211 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07002212 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002213 }
2214 layer->setAcquireFence(hw, *cur);
2215 }
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302216
2217#ifdef QCOM_BSP
2218 // call EndTile, if starTile has been called in this cycle.
2219 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302220 if(mCanUseGpuTileRender && !mUnionDirtyRect.isEmpty()) {
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302221 engine.endTileComposition(GL_PRESERVE);
2222 }
2223 }
2224#endif
Mathias Agopian85d751c2012-08-29 16:59:24 -07002225 } else {
2226 // we're not using h/w composer
2227 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002228 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002229 const Region clip(dirty.intersect(
2230 tr.transform(layer->visibleRegion)));
2231 if (!clip.isEmpty()) {
2232 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07002233 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002234 }
2235 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002236
2237 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002238 engine.disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07002239 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002240}
2241
Mathias Agopian3f844832013-08-07 21:24:32 -07002242void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07002243 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07002244 RenderEngine& engine(getRenderEngine());
2245 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002246}
2247
Mathias Agopianac9fa422013-02-11 16:40:36 -08002248void SurfaceFlinger::addClientLayer(const sp<Client>& client,
2249 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07002250 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08002251 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07002252{
Mathias Agopian96f08192010-06-02 23:28:45 -07002253 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08002254 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07002255
Mathias Agopian96f08192010-06-02 23:28:45 -07002256 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002257 Mutex::Autolock _l(mStateLock);
2258 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07002259 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07002260}
2261
Mathias Agopian3f844832013-08-07 21:24:32 -07002262status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002263 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08002264 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002265 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07002266 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002267 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07002268 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002269 return NO_ERROR;
2270 }
Mathias Agopian3d579642009-06-04 18:46:21 -07002271 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002272}
2273
Dan Stozac7014012014-02-14 15:03:43 -08002274uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07002275 return android_atomic_release_load(&mTransactionFlags);
2276}
2277
Mathias Agopian3f844832013-08-07 21:24:32 -07002278uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002279 return android_atomic_and(~flags, &mTransactionFlags) & flags;
2280}
2281
Mathias Agopian3f844832013-08-07 21:24:32 -07002282uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002283 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
2284 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002285 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002286 }
2287 return old;
2288}
2289
Mathias Agopian8b33f032012-07-24 20:43:54 -07002290void SurfaceFlinger::setTransactionState(
2291 const Vector<ComposerState>& state,
2292 const Vector<DisplayState>& displays,
2293 uint32_t flags)
2294{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002295 ATRACE_CALL();
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08002296 size_t count = displays.size();
2297#ifdef QCOM_BSP
2298 for (size_t i=0 ; i<count ; i++) {
2299 const DisplayState& s(displays[i]);
2300 if(s.token != mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]) {
2301 const uint32_t what = s.what;
2302 // Invalidate and Delay the binder thread by 50 ms on
2303 // eDisplayProjectionChanged to trigger a draw cycle so that
2304 // it can fix one incorrect frame on the External, when we disable
2305 // external animation
2306 if (what & DisplayState::eDisplayProjectionChanged) {
2307 invalidateHwcGeometry();
2308 repaintEverything();
2309 usleep(50000);
2310 }
2311 }
2312 }
2313#endif
Mathias Agopian698c0872011-06-28 19:09:31 -07002314 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07002315 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002316
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002317 if (flags & eAnimation) {
2318 // For window updates that are part of an animation we must wait for
2319 // previous animation "frames" to be handled.
2320 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002321 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002322 if (CC_UNLIKELY(err != NO_ERROR)) {
2323 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002324 // caller after a few seconds.
2325 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
2326 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002327 mAnimTransactionPending = false;
2328 break;
2329 }
2330 }
2331 }
2332
Mathias Agopiane57f2922012-08-09 16:29:12 -07002333 for (size_t i=0 ; i<count ; i++) {
2334 const DisplayState& s(displays[i]);
2335 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002336 }
2337
Mathias Agopiane57f2922012-08-09 16:29:12 -07002338 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002339 for (size_t i=0 ; i<count ; i++) {
2340 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002341 // Here we need to check that the interface we're given is indeed
2342 // one of our own. A malicious client could give us a NULL
2343 // IInterface, or one of its own or even one of our own but a
2344 // different type. All these situations would cause us to crash.
2345 //
2346 // NOTE: it would be better to use RTTI as we could directly check
2347 // that we have a Client*. however, RTTI is disabled in Android.
2348 if (s.client != NULL) {
2349 sp<IBinder> binder = s.client->asBinder();
2350 if (binder != NULL) {
2351 String16 desc(binder->getInterfaceDescriptor());
2352 if (desc == ISurfaceComposerClient::descriptor) {
2353 sp<Client> client( static_cast<Client *>(s.client.get()) );
2354 transactionFlags |= setClientStateLocked(client, s.state);
2355 }
2356 }
2357 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002358 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002359
Mathias Agopian386aa982011-11-07 21:58:03 -08002360 if (transactionFlags) {
2361 // this triggers the transaction
2362 setTransactionFlags(transactionFlags);
2363
2364 // if this is a synchronous transaction, wait for it to take effect
2365 // before returning.
2366 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002367 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002368 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002369 if (flags & eAnimation) {
2370 mAnimTransactionPending = true;
2371 }
2372 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002373 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2374 if (CC_UNLIKELY(err != NO_ERROR)) {
2375 // just in case something goes wrong in SF, return to the
2376 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002377 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2378 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002379 break;
2380 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002381 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002382 }
2383}
2384
Mathias Agopiane57f2922012-08-09 16:29:12 -07002385uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2386{
Jesse Hall9a143922012-10-04 16:29:19 -07002387 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2388 if (dpyIdx < 0)
2389 return 0;
2390
Mathias Agopiane57f2922012-08-09 16:29:12 -07002391 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002392 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002393 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002394 const uint32_t what = s.what;
2395 if (what & DisplayState::eSurfaceChanged) {
2396 if (disp.surface->asBinder() != s.surface->asBinder()) {
2397 disp.surface = s.surface;
2398 flags |= eDisplayTransactionNeeded;
2399 }
2400 }
2401 if (what & DisplayState::eLayerStackChanged) {
2402 if (disp.layerStack != s.layerStack) {
2403 disp.layerStack = s.layerStack;
2404 flags |= eDisplayTransactionNeeded;
2405 }
2406 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002407 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002408 if (disp.orientation != s.orientation) {
2409 disp.orientation = s.orientation;
2410 flags |= eDisplayTransactionNeeded;
2411 }
2412 if (disp.frame != s.frame) {
2413 disp.frame = s.frame;
2414 flags |= eDisplayTransactionNeeded;
2415 }
2416 if (disp.viewport != s.viewport) {
2417 disp.viewport = s.viewport;
2418 flags |= eDisplayTransactionNeeded;
2419 }
2420 }
Michael Lentine47e45402014-07-18 15:34:25 -07002421 if (what & DisplayState::eDisplaySizeChanged) {
2422 if (disp.width != s.width) {
2423 disp.width = s.width;
2424 flags |= eDisplayTransactionNeeded;
2425 }
2426 if (disp.height != s.height) {
2427 disp.height = s.height;
2428 flags |= eDisplayTransactionNeeded;
2429 }
2430 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002431 }
2432 return flags;
2433}
2434
2435uint32_t SurfaceFlinger::setClientStateLocked(
2436 const sp<Client>& client,
2437 const layer_state_t& s)
2438{
2439 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002440 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002441 if (layer != 0) {
2442 const uint32_t what = s.what;
2443 if (what & layer_state_t::ePositionChanged) {
2444 if (layer->setPosition(s.x, s.y))
2445 flags |= eTraversalNeeded;
2446 }
2447 if (what & layer_state_t::eLayerChanged) {
2448 // NOTE: index needs to be calculated before we update the state
2449 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2450 if (layer->setLayer(s.z)) {
2451 mCurrentState.layersSortedByZ.removeAt(idx);
2452 mCurrentState.layersSortedByZ.add(layer);
2453 // we need traversal (state changed)
2454 // AND transaction (list changed)
2455 flags |= eTransactionNeeded|eTraversalNeeded;
2456 }
2457 }
2458 if (what & layer_state_t::eSizeChanged) {
2459 if (layer->setSize(s.w, s.h)) {
2460 flags |= eTraversalNeeded;
2461 }
2462 }
2463 if (what & layer_state_t::eAlphaChanged) {
2464 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2465 flags |= eTraversalNeeded;
2466 }
2467 if (what & layer_state_t::eMatrixChanged) {
2468 if (layer->setMatrix(s.matrix))
2469 flags |= eTraversalNeeded;
2470 }
2471 if (what & layer_state_t::eTransparentRegionChanged) {
2472 if (layer->setTransparentRegionHint(s.transparentRegion))
2473 flags |= eTraversalNeeded;
2474 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08002475 if ((what & layer_state_t::eVisibilityChanged) ||
2476 (what & layer_state_t::eOpacityChanged)) {
2477 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07002478 if (layer->setFlags(s.flags, s.mask))
2479 flags |= eTraversalNeeded;
2480 }
2481 if (what & layer_state_t::eCropChanged) {
2482 if (layer->setCrop(s.crop))
2483 flags |= eTraversalNeeded;
2484 }
2485 if (what & layer_state_t::eLayerStackChanged) {
2486 // NOTE: index needs to be calculated before we update the state
2487 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2488 if (layer->setLayerStack(s.layerStack)) {
2489 mCurrentState.layersSortedByZ.removeAt(idx);
2490 mCurrentState.layersSortedByZ.add(layer);
2491 // we need traversal (state changed)
2492 // AND transaction (list changed)
2493 flags |= eTransactionNeeded|eTraversalNeeded;
2494 }
2495 }
2496 }
2497 return flags;
2498}
2499
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002500status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002501 const String8& name,
2502 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002503 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2504 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002505{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002506 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002507 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002508 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002509 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002510 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002511 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002512
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002513 status_t result = NO_ERROR;
2514
2515 sp<Layer> layer;
2516
Mathias Agopian3165cc22012-08-08 19:42:09 -07002517 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2518 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002519 result = createNormalLayer(client,
2520 name, w, h, flags, format,
2521 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002522 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002523 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002524 result = createDimLayer(client,
2525 name, w, h, flags,
2526 handle, gbp, &layer);
2527 break;
2528 default:
2529 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002530 break;
2531 }
2532
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002533 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002534 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002535 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002536 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002537 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002538}
2539
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002540status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2541 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2542 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002543{
2544 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002545 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002546 case PIXEL_FORMAT_TRANSPARENT:
2547 case PIXEL_FORMAT_TRANSLUCENT:
2548 format = PIXEL_FORMAT_RGBA_8888;
2549 break;
2550 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002551 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002552 break;
2553 }
2554
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002555 *outLayer = new Layer(this, client, name, w, h, flags);
2556 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2557 if (err == NO_ERROR) {
2558 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002559 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002560 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002561
2562 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2563 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002564}
2565
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002566status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2567 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2568 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002569{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002570 *outLayer = new LayerDim(this, client, name, w, h, flags);
2571 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002572 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002573 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002574}
2575
Mathias Agopianac9fa422013-02-11 16:40:36 -08002576status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002577{
Mathias Agopian67106042013-03-14 19:18:13 -07002578 // called by the window manager when it wants to remove a Layer
2579 status_t err = NO_ERROR;
2580 sp<Layer> l(client->getLayerUser(handle));
2581 if (l != NULL) {
2582 err = removeLayer(l);
2583 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2584 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002585 }
2586 return err;
2587}
2588
Mathias Agopian13127d82013-03-05 17:47:11 -08002589status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002590{
Mathias Agopian67106042013-03-14 19:18:13 -07002591 // called by ~LayerCleaner() when all references to the IBinder (handle)
2592 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002593 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002594 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002595 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002596 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002597 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002598 "error removing layer=%p (%s)", l.get(), strerror(-err));
2599 }
2600 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002601}
2602
Mathias Agopianb60314a2012-04-10 22:09:54 -07002603// ---------------------------------------------------------------------------
2604
Andy McFadden13a082e2012-08-24 10:16:42 -07002605void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002606 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002607 Vector<ComposerState> state;
2608 Vector<DisplayState> displays;
2609 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002610 d.what = DisplayState::eDisplayProjectionChanged |
2611 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002612 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002613 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002614 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002615 d.frame.makeInvalid();
2616 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002617 d.width = 0;
2618 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002619 displays.add(d);
2620 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002621 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002622
2623 const nsecs_t period =
2624 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2625 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002626}
2627
2628void SurfaceFlinger::initializeDisplays() {
2629 class MessageScreenInitialized : public MessageBase {
2630 SurfaceFlinger* flinger;
2631 public:
2632 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2633 virtual bool handler() {
2634 flinger->onInitializeDisplays();
2635 return true;
2636 }
2637 };
2638 sp<MessageBase> msg = new MessageScreenInitialized(this);
2639 postMessageAsync(msg); // we may be called from main thread, use async message
2640}
2641
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002642void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2643 int mode) {
2644 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2645 this);
2646 int32_t type = hw->getDisplayType();
2647 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002648
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002649 if (mode == currentMode) {
2650 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002651 return;
2652 }
2653
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002654 hw->setPowerMode(mode);
2655 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2656 ALOGW("Trying to set power mode for virtual display");
2657 return;
2658 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002659
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002660 if (currentMode == HWC_POWER_MODE_OFF) {
2661 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002662 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2663 // FIXME: eventthread only knows about the main display right now
2664 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002665 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002666 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002667
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002668 mVisibleRegionsDirty = true;
2669 repaintEverything();
2670 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002671 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002672 disableHardwareVsync(true); // also cancels any in-progress resync
2673
Mathias Agopiancde87a32012-09-13 14:09:01 -07002674 // FIXME: eventthread only knows about the main display right now
2675 mEventThread->onScreenReleased();
2676 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002677
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002678 getHwComposer().setPowerMode(type, mode);
2679 mVisibleRegionsDirty = true;
2680 // from this point on, SF will stop drawing on this display
2681 } else {
2682 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002683 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002684}
2685
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002686void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2687 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002688 SurfaceFlinger& mFlinger;
2689 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002690 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002691 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002692 MessageSetPowerMode(SurfaceFlinger& flinger,
2693 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2694 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002695 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002696 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002697 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002698 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002699 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002700 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002701 ALOGW("Attempt to set power mode = %d for virtual display",
2702 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002703 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002704 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002705 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002706 return true;
2707 }
2708 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002709 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002710 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002711}
2712
2713// ---------------------------------------------------------------------------
2714
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002715status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2716{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002717 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002718
Mathias Agopianbd115332013-04-18 16:41:04 -07002719 IPCThreadState* ipc = IPCThreadState::self();
2720 const int pid = ipc->getCallingPid();
2721 const int uid = ipc->getCallingUid();
2722 if ((uid != AID_SHELL) &&
2723 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002724 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002725 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002726 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002727 // Try to get the main lock, but don't insist if we can't
2728 // (this would indicate SF is stuck, but we want to be able to
2729 // print something in dumpsys).
2730 int retry = 3;
2731 while (mStateLock.tryLock()<0 && --retry>=0) {
2732 usleep(1000000);
2733 }
2734 const bool locked(retry >= 0);
2735 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002736 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002737 "SurfaceFlinger appears to be unresponsive, "
2738 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002739 }
2740
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002741 bool dumpAll = true;
2742 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002743 size_t numArgs = args.size();
2744 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002745 if ((index < numArgs) &&
2746 (args[index] == String16("--list"))) {
2747 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002748 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002749 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002750 }
2751
2752 if ((index < numArgs) &&
2753 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002754 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002755 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002756 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002757 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002758
2759 if ((index < numArgs) &&
2760 (args[index] == String16("--latency-clear"))) {
2761 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002762 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002763 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002764 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002765
2766 if ((index < numArgs) &&
2767 (args[index] == String16("--dispsync"))) {
2768 index++;
2769 mPrimaryDispSync.dump(result);
2770 dumpAll = false;
2771 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002772 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002773
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002774 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002775 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002776 }
2777
Mathias Agopian9795c422009-08-26 16:36:26 -07002778 if (locked) {
2779 mStateLock.unlock();
2780 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002781 }
2782 write(fd, result.string(), result.size());
2783 return NO_ERROR;
2784}
2785
Dan Stozac7014012014-02-14 15:03:43 -08002786void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2787 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002788{
2789 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2790 const size_t count = currentLayers.size();
2791 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002792 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002793 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002794 }
2795}
2796
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002797void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002798 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002799{
2800 String8 name;
2801 if (index < args.size()) {
2802 name = String8(args[index]);
2803 index++;
2804 }
2805
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002806 const nsecs_t period =
2807 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002808 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002809
2810 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002811 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002812 } else {
2813 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2814 const size_t count = currentLayers.size();
2815 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002816 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002817 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002818 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002819 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002820 }
2821 }
2822}
2823
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002824void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002825 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002826{
2827 String8 name;
2828 if (index < args.size()) {
2829 name = String8(args[index]);
2830 index++;
2831 }
2832
2833 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2834 const size_t count = currentLayers.size();
2835 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002836 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002837 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002838 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002839 }
2840 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002841
Svetoslavd85084b2014-03-20 10:28:31 -07002842 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002843}
2844
Jamie Gennis6547ff42013-07-16 20:12:42 -07002845// This should only be called from the main thread. Otherwise it would need
2846// the lock and should use mCurrentState rather than mDrawingState.
2847void SurfaceFlinger::logFrameStats() {
2848 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2849 const size_t count = drawingLayers.size();
2850 for (size_t i=0 ; i<count ; i++) {
2851 const sp<Layer>& layer(drawingLayers[i]);
2852 layer->logFrameStats();
2853 }
2854
2855 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2856}
2857
Andy McFadden4803b742012-09-24 19:07:20 -07002858/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2859{
2860 static const char* config =
2861 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002862#ifdef HAS_CONTEXT_PRIORITY
2863 " HAS_CONTEXT_PRIORITY"
2864#endif
2865#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2866 " NEVER_DEFAULT_TO_ASYNC_MODE"
2867#endif
2868#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2869 " TARGET_DISABLE_TRIPLE_BUFFERING"
2870#endif
2871 "]";
2872 result.append(config);
2873}
2874
Mathias Agopian74d211a2013-04-22 16:55:35 +02002875void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2876 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002877{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002878 bool colorize = false;
2879 if (index < args.size()
2880 && (args[index] == String16("--color"))) {
2881 colorize = true;
2882 index++;
2883 }
2884
2885 Colorizer colorizer(colorize);
2886
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002887 // figure out if we're stuck somewhere
2888 const nsecs_t now = systemTime();
2889 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2890 const nsecs_t inTransaction(mDebugInTransaction);
2891 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2892 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2893
2894 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002895 * Dump library configuration.
2896 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002897
2898 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002899 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002900 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002901 appendSfConfigString(result);
2902 appendUiConfigString(result);
2903 appendGuiConfigString(result);
2904 result.append("\n");
2905
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002906 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002907 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002908 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002909 result.append(SyncFeatures::getInstance().toString());
2910 result.append("\n");
2911
Andy McFadden41d67d72014-04-25 16:58:34 -07002912 colorizer.bold(result);
2913 result.append("DispSync configuration: ");
2914 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002915 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2916 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002917 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2918 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2919 result.append("\n");
2920
Andy McFadden4803b742012-09-24 19:07:20 -07002921 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002922 * Dump the visible layer list
2923 */
2924 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2925 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002926 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002927 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002928 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002929 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002930 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002931 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002932 }
2933
2934 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002935 * Dump Display state
2936 */
2937
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002938 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002939 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002940 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002941 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2942 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002943 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002944 }
2945
2946 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002947 * Dump SurfaceFlinger global state
2948 */
2949
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002950 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002951 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002952 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002953
Mathias Agopian888c8222012-08-04 21:10:38 -07002954 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002955 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002956
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002957 colorizer.bold(result);
2958 result.appendFormat("EGL implementation : %s\n",
2959 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2960 colorizer.reset(result);
2961 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002962 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002963
Mathias Agopian875d8e12013-06-07 15:35:48 -07002964 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002965
Mathias Agopian42977342012-08-05 00:40:46 -07002966 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002967 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2968 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002969 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002970 " last eglSwapBuffers() time: %f us\n"
2971 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002972 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002973 " refresh-rate : %f fps\n"
2974 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002975 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002976 " gpu_to_cpu_unsupported : %d\n"
2977 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002978 mLastSwapBufferTime/1000.0,
2979 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002980 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002981 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2982 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002983 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002984 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002985
Mathias Agopian74d211a2013-04-22 16:55:35 +02002986 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002987 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002988
Mathias Agopian74d211a2013-04-22 16:55:35 +02002989 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002990 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002991
2992 /*
2993 * VSYNC state
2994 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002995 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002996
2997 /*
2998 * Dump HWComposer state
2999 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003000 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02003001 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003002 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02003003 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003004 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07003005 (mDebugDisableHWC || mDebugRegion || mDaltonize
3006 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02003007 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003008
3009 /*
3010 * Dump gralloc state
3011 */
3012 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
3013 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003014}
3015
Mathias Agopian13127d82013-03-05 17:47:11 -08003016const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07003017SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07003018 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07003019 wp<IBinder> dpy;
3020 for (size_t i=0 ; i<mDisplays.size() ; i++) {
3021 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
3022 dpy = mDisplays.keyAt(i);
3023 break;
3024 }
3025 }
3026 if (dpy == NULL) {
3027 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
3028 // Just use the primary display so we have something to return
3029 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
3030 }
3031 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07003032}
3033
Keun young Park63f165f2012-08-31 10:53:36 -07003034bool SurfaceFlinger::startDdmConnection()
3035{
3036 void* libddmconnection_dso =
3037 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
3038 if (!libddmconnection_dso) {
3039 return false;
3040 }
3041 void (*DdmConnection_start)(const char* name);
3042 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07003043 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07003044 if (!DdmConnection_start) {
3045 dlclose(libddmconnection_dso);
3046 return false;
3047 }
3048 (*DdmConnection_start)(getServiceName());
3049 return true;
3050}
3051
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003052status_t SurfaceFlinger::onTransact(
3053 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3054{
3055 switch (code) {
3056 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07003057 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07003058 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003059 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07003060 case CLEAR_ANIMATION_FRAME_STATS:
3061 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07003062 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003063 {
3064 // codes that require permission check
3065 IPCThreadState* ipc = IPCThreadState::self();
3066 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07003067 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07003068 if ((uid != AID_GRAPHICS) &&
3069 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00003070 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07003071 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
3072 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003073 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003074 break;
3075 }
3076 case CAPTURE_SCREEN:
3077 {
3078 // codes that require permission check
3079 IPCThreadState* ipc = IPCThreadState::self();
3080 const int pid = ipc->getCallingPid();
3081 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07003082 if ((uid != AID_GRAPHICS) &&
3083 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00003084 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003085 "can't read framebuffer pid=%d, uid=%d", pid, uid);
3086 return PERMISSION_DENIED;
3087 }
3088 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003089 }
3090 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003091
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003092 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
3093 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07003094 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08003095 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07003096 IPCThreadState* ipc = IPCThreadState::self();
3097 const int pid = ipc->getCallingPid();
3098 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00003099 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07003100 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003101 return PERMISSION_DENIED;
3102 }
3103 int n;
3104 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07003105 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07003106 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003107 return NO_ERROR;
3108 case 1002: // SHOW_UPDATES
3109 n = data.readInt32();
3110 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07003111 invalidateHwcGeometry();
3112 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003113 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003114 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07003115 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003116 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003117 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003118 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07003119 setTransactionFlags(
3120 eTransactionNeeded|
3121 eDisplayTransactionNeeded|
3122 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003123 return NO_ERROR;
3124 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08003125 case 1006:{ // send empty update
3126 signalRefresh();
3127 return NO_ERROR;
3128 }
Mathias Agopian53331da2011-08-22 21:44:41 -07003129 case 1008: // toggle use of hw composer
3130 n = data.readInt32();
3131 mDebugDisableHWC = n ? 1 : 0;
3132 invalidateHwcGeometry();
3133 repaintEverything();
3134 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07003135 case 1009: // toggle use of transform hint
3136 n = data.readInt32();
3137 mDebugDisableTransformHint = n ? 1 : 0;
3138 invalidateHwcGeometry();
3139 repaintEverything();
3140 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003141 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07003142 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003143 reply->writeInt32(0);
3144 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07003145 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08003146 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003147 return NO_ERROR;
3148 case 1013: {
3149 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07003150 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
3151 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07003152 return NO_ERROR;
3153 }
3154 case 1014: {
3155 // daltonize
3156 n = data.readInt32();
3157 switch (n % 10) {
3158 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
3159 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
3160 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
3161 }
3162 if (n >= 10) {
3163 mDaltonizer.setMode(Daltonizer::correction);
3164 } else {
3165 mDaltonizer.setMode(Daltonizer::simulation);
3166 }
3167 mDaltonize = n > 0;
3168 invalidateHwcGeometry();
3169 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07003170 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003171 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003172 case 1015: {
3173 // apply a color matrix
3174 n = data.readInt32();
3175 mHasColorMatrix = n ? 1 : 0;
3176 if (n) {
3177 // color matrix is sent as mat3 matrix followed by vec3
3178 // offset, then packed into a mat4 where the last row is
3179 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07003180 for (size_t i = 0 ; i < 4; i++) {
3181 for (size_t j = 0; j < 4; j++) {
3182 mColorMatrix[i][j] = data.readFloat();
3183 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003184 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003185 } else {
3186 mColorMatrix = mat4();
3187 }
3188 invalidateHwcGeometry();
3189 repaintEverything();
3190 return NO_ERROR;
3191 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003192 // This is an experimental interface
3193 // Needs to be shifted to proper binder interface when we productize
3194 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07003195 n = data.readInt32();
3196 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003197 return NO_ERROR;
3198 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003199 }
3200 }
3201 return err;
3202}
3203
Mathias Agopian53331da2011-08-22 21:44:41 -07003204void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07003205 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08003206 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07003207}
3208
Mathias Agopian59119e62010-10-11 12:37:43 -07003209// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003210// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07003211// ---------------------------------------------------------------------------
3212
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003213/* The code below is here to handle b/8734824
3214 *
3215 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07003216 * from the surfaceflinger thread to the calling binder thread, where they
3217 * are executed. This allows the calling thread in the calling process to be
3218 * reused and not depend on having "enough" binder threads to handle the
3219 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003220 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003221class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07003222 /* Parts of GraphicProducerWrapper are run on two different threads,
3223 * communicating by sending messages via Looper but also by shared member
3224 * data. Coherence maintenance is subtle and in places implicit (ugh).
3225 *
3226 * Don't rely on Looper's sendMessage/handleMessage providing
3227 * release/acquire semantics for any data not actually in the Message.
3228 * Data going from surfaceflinger to binder threads needs to be
3229 * synchronized explicitly.
3230 *
3231 * Barrier open/wait do provide release/acquire semantics. This provides
3232 * implicit synchronization for data coming back from binder to
3233 * surfaceflinger threads.
3234 */
3235
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003236 sp<IGraphicBufferProducer> impl;
3237 sp<Looper> looper;
3238 status_t result;
3239 bool exitPending;
3240 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07003241 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003242 uint32_t code;
3243 Parcel const* data;
3244 Parcel* reply;
3245
3246 enum {
3247 MSG_API_CALL,
3248 MSG_EXIT
3249 };
3250
3251 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003252 * Called on surfaceflinger thread. This is called by our "fake"
3253 * BpGraphicBufferProducer. We package the data and reply Parcel and
3254 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003255 */
3256 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08003257 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003258 this->code = code;
3259 this->data = &data;
3260 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003261 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07003262 // if we've exited, we run the message synchronously right here.
3263 // note (JH): as far as I can tell from looking at the code, this
3264 // never actually happens. if it does, i'm not sure if it happens
3265 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003266 handleMessage(Message(MSG_API_CALL));
3267 } else {
3268 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07003269 // Prevent stores to this->{code, data, reply} from being
3270 // reordered later than the construction of Message.
3271 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003272 looper->sendMessage(this, Message(MSG_API_CALL));
3273 barrier.wait();
3274 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08003275 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003276 }
3277
3278 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003279 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003280 * call the real BpGraphicBufferProducer.
3281 */
3282 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07003283 int what = message.what;
3284 // Prevent reads below from happening before the read from Message
3285 atomic_thread_fence(memory_order_acquire);
3286 if (what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08003287 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003288 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07003289 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003290 exitRequested = true;
3291 }
3292 }
3293
3294public:
Jesse Hallb154c422014-07-13 12:47:02 -07003295 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
3296 : impl(impl),
3297 looper(new Looper(true)),
3298 exitPending(false),
3299 exitRequested(false)
3300 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003301
Jesse Hallb154c422014-07-13 12:47:02 -07003302 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003303 status_t waitForResponse() {
3304 do {
3305 looper->pollOnce(-1);
3306 } while (!exitRequested);
3307 return result;
3308 }
3309
Jesse Hallb154c422014-07-13 12:47:02 -07003310 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003311 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07003312 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003313 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07003314 // Ensure this->result is visible to the binder thread before it
3315 // handles the message.
3316 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003317 looper->sendMessage(this, Message(MSG_EXIT));
3318 }
3319};
3320
3321
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003322status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
3323 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003324 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003325 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003326 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003327
3328 if (CC_UNLIKELY(display == 0))
3329 return BAD_VALUE;
3330
3331 if (CC_UNLIKELY(producer == 0))
3332 return BAD_VALUE;
3333
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003334 // if we have secure windows on this display, never allow the screen capture
3335 // unless the producer interface is local (i.e.: we can take a screenshot for
3336 // ourselves).
3337 if (!producer->asBinder()->localBinder()) {
3338 Mutex::Autolock _l(mStateLock);
3339 sp<const DisplayDevice> hw(getDisplayDevice(display));
3340 if (hw->getSecureLayerVisible()) {
3341 ALOGW("FB is protected: PERMISSION_DENIED");
3342 return PERMISSION_DENIED;
3343 }
3344 }
3345
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003346 // Convert to surfaceflinger's internal rotation type.
3347 Transform::orientation_flags rotationFlags;
3348 switch (rotation) {
3349 case ISurfaceComposer::eRotateNone:
3350 rotationFlags = Transform::ROT_0;
3351 break;
3352 case ISurfaceComposer::eRotate90:
3353 rotationFlags = Transform::ROT_90;
3354 break;
3355 case ISurfaceComposer::eRotate180:
3356 rotationFlags = Transform::ROT_180;
3357 break;
3358 case ISurfaceComposer::eRotate270:
3359 rotationFlags = Transform::ROT_270;
3360 break;
3361 default:
3362 rotationFlags = Transform::ROT_0;
3363 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3364 break;
3365 }
3366
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003367 class MessageCaptureScreen : public MessageBase {
3368 SurfaceFlinger* flinger;
3369 sp<IBinder> display;
3370 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003371 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003372 uint32_t reqWidth, reqHeight;
3373 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003374 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003375 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003376 status_t result;
3377 public:
3378 MessageCaptureScreen(SurfaceFlinger* flinger,
3379 const sp<IBinder>& display,
3380 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003381 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003382 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003383 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003384 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003385 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003386 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003387 useIdentityTransform(useIdentityTransform),
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003388 rotation(rotation),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003389 result(PERMISSION_DENIED)
3390 {
3391 }
3392 status_t getResult() const {
3393 return result;
3394 }
3395 virtual bool handler() {
3396 Mutex::Autolock _l(flinger->mStateLock);
3397 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003398 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003399 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003400 useIdentityTransform, rotation);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003401 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003402 return true;
3403 }
3404 };
3405
Mathias Agopian9eb1f052013-04-10 16:27:17 -07003406 // make sure to process transactions before screenshots -- a transaction
3407 // might already be pending but scheduled for VSYNC; this guarantees we
3408 // will handle it before the screenshot. When VSYNC finally arrives
3409 // the scheduled transaction will be a no-op. If no transactions are
3410 // scheduled at this time, this will end-up being a no-op as well.
3411 mEventQueue.invalidateTransactionNow();
3412
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003413 // this creates a "fake" BBinder which will serve as a "fake" remote
3414 // binder to receive the marshaled calls and forward them to the
3415 // real remote (a BpGraphicBufferProducer)
3416 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3417
3418 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3419 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003420 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003421 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003422 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003423 useIdentityTransform, rotationFlags);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003424
3425 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003426 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003427 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003428 }
3429 return res;
3430}
3431
Mathias Agopian180f10d2013-04-10 22:55:41 -07003432
3433void SurfaceFlinger::renderScreenImplLocked(
3434 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003435 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003436 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003437 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003438{
3439 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003440 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003441
3442 // get screen geometry
3443 const uint32_t hw_w = hw->getWidth();
3444 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003445 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
3446
Dan Stozac1879002014-05-22 15:59:05 -07003447 // if a default or invalid sourceCrop is passed in, set reasonable values
3448 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3449 !sourceCrop.isValid()) {
3450 sourceCrop.setLeftTop(Point(0, 0));
3451 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3452 }
3453
3454 // ensure that sourceCrop is inside screen
3455 if (sourceCrop.left < 0) {
3456 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3457 }
Dan Stozabe31f442014-06-11 11:20:54 -07003458 if (sourceCrop.right > hw_w) {
3459 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003460 }
3461 if (sourceCrop.top < 0) {
3462 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3463 }
Dan Stozabe31f442014-06-11 11:20:54 -07003464 if (sourceCrop.bottom > hw_h) {
3465 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003466 }
3467
Mathias Agopian180f10d2013-04-10 22:55:41 -07003468 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003469 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003470
3471 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003472 engine.setViewportAndProjection(
3473 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003474 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003475
3476 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003477 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003478
3479 const LayerVector& layers( mDrawingState.layersSortedByZ );
3480 const size_t count = layers.size();
3481 for (size_t i=0 ; i<count ; ++i) {
3482 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003483 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003484 if (state.layerStack == hw->getLayerStack()) {
3485 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07003486#ifdef QCOM_BSP
3487 // dont render the secure Display Layer
3488 if(layer->isSecureDisplay()) {
3489 continue;
3490 }
3491#endif
Mathias Agopian180f10d2013-04-10 22:55:41 -07003492 if (layer->isVisible()) {
3493 if (filtering) layer->setFiltering(true);
Ramakant Singhf9af0e32014-01-07 14:17:52 +05303494 if(!layer->isProtected())
3495 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003496 if (filtering) layer->setFiltering(false);
3497 }
3498 }
3499 }
3500 }
3501
3502 // compositionComplete is needed for older driver
3503 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003504 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003505}
3506
3507
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003508status_t SurfaceFlinger::captureScreenImplLocked(
3509 const sp<const DisplayDevice>& hw,
3510 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003511 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003512 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003513 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003514{
3515 ATRACE_CALL();
3516
Mathias Agopian180f10d2013-04-10 22:55:41 -07003517 // get screen geometry
3518 const uint32_t hw_w = hw->getWidth();
3519 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003520
Mathias Agopian180f10d2013-04-10 22:55:41 -07003521 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3522 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3523 reqWidth, reqHeight, hw_w, hw_h);
3524 return BAD_VALUE;
3525 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003526
Mathias Agopian180f10d2013-04-10 22:55:41 -07003527 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3528 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3529
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003530 // create a surface (because we're a producer, and we need to
3531 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003532 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003533 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003534
3535 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003536 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003537 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3538 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003539
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003540 int err = 0;
3541 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003542 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003543 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3544 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003545
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003546 if (err == NO_ERROR) {
3547 ANativeWindowBuffer* buffer;
3548 /* TODO: Once we have the sync framework everywhere this can use
3549 * server-side waits on the fence that dequeueBuffer returns.
3550 */
3551 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3552 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003553 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003554 // create an EGLImage from the buffer so we can later
3555 // turn it into a texture
3556 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3557 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3558 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003559 // this binds the given EGLImage as a framebuffer for the
3560 // duration of this scope.
3561 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3562 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003563 // this will in fact render into our dequeued buffer
3564 // via an FBO, which means we didn't have to create
3565 // an EGLSurface and therefore we're not
3566 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003567 renderScreenImplLocked(
3568 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3569 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003570
Riley Andrews86639902014-08-15 12:27:24 -07003571 // Attempt to create a sync khr object that can produce a sync point. If that
3572 // isn't available, create a non-dupable sync object in the fallback path and
3573 // wait on it directly.
3574 EGLSyncKHR sync;
3575 if (!DEBUG_SCREENSHOTS) {
3576 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Andy McFadden2d8d1202013-10-09 16:38:02 -07003577 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003578 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003579 }
Riley Andrews86639902014-08-15 12:27:24 -07003580 if (sync != EGL_NO_SYNC_KHR) {
3581 // get the sync fd
3582 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3583 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3584 ALOGW("captureScreen: failed to dup sync khr object");
3585 syncFd = -1;
3586 }
3587 eglDestroySyncKHR(mEGLDisplay, sync);
3588 } else {
3589 // fallback path
3590 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3591 if (sync != EGL_NO_SYNC_KHR) {
3592 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3593 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3594 EGLint eglErr = eglGetError();
3595 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3596 ALOGW("captureScreen: fence wait timed out");
3597 } else {
3598 ALOGW_IF(eglErr != EGL_SUCCESS,
3599 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3600 }
3601 eglDestroySyncKHR(mEGLDisplay, sync);
3602 } else {
3603 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3604 }
3605 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003606 if (DEBUG_SCREENSHOTS) {
3607 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3608 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3609 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3610 hw, minLayerZ, maxLayerZ);
3611 delete [] pixels;
3612 }
3613
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003614 } else {
3615 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3616 result = INVALID_OPERATION;
3617 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003618 // destroy our image
3619 eglDestroyImageKHR(mEGLDisplay, image);
3620 } else {
3621 result = BAD_VALUE;
3622 }
Jesse Hallafe2b1f2014-10-21 11:09:17 -07003623 // queueBuffer takes ownership of syncFd
Riley Andrews86639902014-08-15 12:27:24 -07003624 window->queueBuffer(window, buffer, syncFd);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003625 }
3626 } else {
3627 result = BAD_VALUE;
3628 }
3629 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3630 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003631
Mathias Agopian74c40c02010-09-29 13:02:36 -07003632 return result;
3633}
3634
Mathias Agopiand5556842013-09-19 17:08:37 -07003635void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3636 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003637 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003638 for (size_t y=0 ; y<h ; y++) {
3639 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3640 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003641 if (p[x] != 0xFF000000) return;
3642 }
3643 }
3644 ALOGE("*** we just took a black screenshot ***\n"
3645 "requested minz=%d, maxz=%d, layerStack=%d",
3646 minLayerZ, maxLayerZ, hw->getLayerStack());
3647 const LayerVector& layers( mDrawingState.layersSortedByZ );
3648 const size_t count = layers.size();
3649 for (size_t i=0 ; i<count ; ++i) {
3650 const sp<Layer>& layer(layers[i]);
3651 const Layer::State& state(layer->getDrawingState());
3652 const bool visible = (state.layerStack == hw->getLayerStack())
3653 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3654 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003655 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003656 visible ? '+' : '-',
3657 i, layer->getName().string(), state.layerStack, state.z,
3658 layer->isVisible(), state.flags, state.alpha);
3659 }
3660 }
3661}
3662
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003663// ---------------------------------------------------------------------------
3664
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003665SurfaceFlinger::LayerVector::LayerVector() {
3666}
3667
3668SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003669 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003670}
3671
3672int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3673 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003674{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003675 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003676 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3677 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003678
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003679 uint32_t ls = l->getCurrentState().layerStack;
3680 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003681 if (ls != rs)
3682 return ls - rs;
3683
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003684 uint32_t lz = l->getCurrentState().z;
3685 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003686 if (lz != rz)
3687 return lz - rz;
3688
3689 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003690}
3691
3692// ---------------------------------------------------------------------------
3693
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003694SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Michael Lentine47e45402014-07-18 15:34:25 -07003695 : type(DisplayDevice::DISPLAY_ID_INVALID), width(0), height(0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003696}
3697
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003698SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Michael Lentine47e45402014-07-18 15:34:25 -07003699 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0), width(0), height(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003700 viewport.makeInvalid();
3701 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003702}
3703
3704// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003705
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003706}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003707
3708
3709#if defined(__gl_h_)
3710#error "don't include gl/gl.h in this file"
3711#endif
3712
3713#if defined(__gl2_h_)
3714#error "don't include gl2/gl2.h in this file"
3715#endif