blob: e379bf62add5f00ad512efbc4fd69ce49fc25a13 [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// ---------------------------------------------------------------------------
Arun Kumar K.R766d5f62013-06-21 19:36:02 -0700133// Initialize extendedMode to false
134#ifdef QCOM_BSP
135bool SurfaceFlinger::sExtendedMode = false;
136#endif
Mathias Agopian99b49842011-06-27 16:05:52 -0700137
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700139 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700141 mTransactionPending(false),
142 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700143 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700144 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700145 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700148 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800149 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700151 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700152 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700153 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700154 mDebugInSwapBuffers(0),
155 mLastSwapBufferTime(0),
156 mDebugInTransaction(0),
157 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700158 mBootFinished(false),
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530159 mGpuTileRenderEnable(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700160 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700161 mHWVsyncAvailable(false),
Alan Viverette9c5a3332013-09-12 20:04:35 -0700162 mDaltonize(false),
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530163 mHasColorMatrix(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164{
Steve Blocka19954a2012-01-04 20:05:49 +0000165 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166
167 // debugging stuff...
168 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700169
Mathias Agopianb4b17302013-03-20 18:36:41 -0700170 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700171 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700172
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 property_get("debug.sf.showupdates", value, "0");
174 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700175
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700176 property_get("debug.sf.ddms", value, "0");
177 mDebugDDMS = atoi(value);
178 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700179 if (!startDdmConnection()) {
180 // start failed, and DDMS debugging not enabled
181 mDebugDDMS = 0;
182 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700183 }
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +0530184#ifdef QCOM_BSP
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530185 mCanUseGpuTileRender = false;
186 property_get("debug.sf.gpu_comp_tiling", value, "0");
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +0530187 mGpuTileRenderEnable = atoi(value) ? true : false;
188 if(mGpuTileRenderEnable)
189 ALOGV("DirtyRect optimization enabled for FULL GPU Composition");
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530190 mUnionDirtyRect.clear();
Ramkumar Radhakrishnanced0b062014-08-11 15:31:07 -0700191
192 property_get("sys.disable_ext_animation", value, "0");
193 mDisableExtAnimation = atoi(value) ? true : false;
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +0530194#endif
195
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700196 ALOGI_IF(mDebugRegion, "showupdates enabled");
197 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198}
199
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800200void SurfaceFlinger::onFirstRef()
201{
202 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800203}
204
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205SurfaceFlinger::~SurfaceFlinger()
206{
Mathias Agopiana4912602012-07-12 14:25:33 -0700207 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
208 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
209 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210}
211
Dan Stozac7014012014-02-14 15:03:43 -0800212void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800213{
214 // the window manager died on us. prepare its eulogy.
215
Andy McFadden13a082e2012-08-24 10:16:42 -0700216 // restore initial conditions (default device unblank, etc)
217 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800218
219 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700220 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800221}
222
Mathias Agopian7e27f052010-05-28 14:22:23 -0700223sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224{
Mathias Agopian96f08192010-06-02 23:28:45 -0700225 sp<ISurfaceComposerClient> bclient;
226 sp<Client> client(new Client(this));
227 status_t err = client->initCheck();
228 if (err == NO_ERROR) {
229 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800230 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231 return bclient;
232}
233
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700234sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
235 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700236{
237 class DisplayToken : public BBinder {
238 sp<SurfaceFlinger> flinger;
239 virtual ~DisplayToken() {
240 // no more references, this display must be terminated
241 Mutex::Autolock _l(flinger->mStateLock);
242 flinger->mCurrentState.displays.removeItem(this);
243 flinger->setTransactionFlags(eDisplayTransactionNeeded);
244 }
245 public:
246 DisplayToken(const sp<SurfaceFlinger>& flinger)
247 : flinger(flinger) {
248 }
249 };
250
251 sp<BBinder> token = new DisplayToken(this);
252
253 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700254 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700255 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700256 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700257 mCurrentState.displays.add(token, info);
258
259 return token;
260}
261
Jesse Hall6c913be2013-08-08 12:15:49 -0700262void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
263 Mutex::Autolock _l(mStateLock);
264
265 ssize_t idx = mCurrentState.displays.indexOfKey(display);
266 if (idx < 0) {
267 ALOGW("destroyDisplay: invalid display token");
268 return;
269 }
270
271 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
272 if (!info.isVirtualDisplay()) {
273 ALOGE("destroyDisplay called for non-virtual display");
274 return;
275 }
276
277 mCurrentState.displays.removeItemsAt(idx);
278 setTransactionFlags(eDisplayTransactionNeeded);
279}
280
Jesse Hall692c7232012-11-08 15:41:56 -0800281void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
282 ALOGW_IF(mBuiltinDisplays[type],
283 "Overwriting display token for display type %d", type);
284 mBuiltinDisplays[type] = new BBinder();
285 DisplayDeviceState info(type);
286 // All non-virtual displays are currently considered secure.
287 info.isSecure = true;
288 mCurrentState.displays.add(mBuiltinDisplays[type], info);
289}
290
Mathias Agopiane57f2922012-08-09 16:29:12 -0700291sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700292 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700293 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
294 return NULL;
295 }
Jesse Hall692c7232012-11-08 15:41:56 -0800296 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700297}
298
Jamie Gennis9a78c902011-01-12 18:30:40 -0800299sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
300{
301 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
302 return gba;
303}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700304
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305void SurfaceFlinger::bootFinished()
306{
307 const nsecs_t now = systemTime();
308 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000309 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700310 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700311
312 // wait patiently for the window manager death
313 const String16 name("window");
314 sp<IBinder> window(defaultServiceManager()->getService(name));
315 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700316 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700317 }
318
319 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700320 // formerly we would just kill the process, but we now ask it to exit so it
321 // can choose where to stop the animation.
322 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800323}
324
Mathias Agopian3f844832013-08-07 21:24:32 -0700325void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700326 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700327 RenderEngine& engine;
328 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700329 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700330 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
331 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700332 }
333 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700334 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700335 return true;
336 }
337 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700338 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700339}
340
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700341class DispSyncSource : public VSyncSource, private DispSync::Callback {
342public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700343 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
344 const char* label) :
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700345 mValue(0),
346 mPhaseOffset(phaseOffset),
347 mTraceVsync(traceVsync),
Andy McFadden5167ec62014-05-22 13:08:43 -0700348 mVsyncOnLabel(String8::format("VsyncOn-%s", label)),
349 mVsyncEventLabel(String8::format("VSYNC-%s", label)),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700350 mDispSync(dispSync) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700351
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700352 virtual ~DispSyncSource() {}
353
354 virtual void setVSyncEnabled(bool enable) {
355 // Do NOT lock the mutex here so as to avoid any mutex ordering issues
356 // with locking it in the onDispSyncEvent callback.
357 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700358 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700359 static_cast<DispSync::Callback*>(this));
360 if (err != NO_ERROR) {
361 ALOGE("error registering vsync callback: %s (%d)",
362 strerror(-err), err);
363 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700364 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700365 } else {
366 status_t err = mDispSync->removeEventListener(
367 static_cast<DispSync::Callback*>(this));
368 if (err != NO_ERROR) {
369 ALOGE("error unregistering vsync callback: %s (%d)",
370 strerror(-err), err);
371 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700372 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700373 }
374 }
375
376 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
377 Mutex::Autolock lock(mMutex);
378 mCallback = callback;
379 }
380
381private:
382 virtual void onDispSyncEvent(nsecs_t when) {
383 sp<VSyncSource::Callback> callback;
384 {
385 Mutex::Autolock lock(mMutex);
386 callback = mCallback;
387
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700388 if (mTraceVsync) {
389 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700390 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700391 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700392 }
393
394 if (callback != NULL) {
395 callback->onVSyncEvent(when);
396 }
397 }
398
399 int mValue;
400
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700401 const nsecs_t mPhaseOffset;
402 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700403 const String8 mVsyncOnLabel;
404 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700405
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700406 DispSync* mDispSync;
407 sp<VSyncSource::Callback> mCallback;
408 Mutex mMutex;
409};
410
411void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700412 ALOGI( "SurfaceFlinger's main thread ready to run. "
413 "Initializing graphics H/W...");
414
Jesse Hallb65f32e2013-09-27 22:10:47 -0700415 status_t err;
Jesse Hall692c7232012-11-08 15:41:56 -0800416 Mutex::Autolock _l(mStateLock);
417
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700418 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700419 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
420 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700421
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700422 // Initialize the H/W composer object. There may or may not be an
423 // actual hardware composer underneath.
424 mHwc = new HWComposer(this,
425 *static_cast<HWComposer::EventHandler *>(this));
426
Mathias Agopian875d8e12013-06-07 15:35:48 -0700427 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800428 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700429
430 // retrieve the EGL context that was selected/created
431 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700432
Mathias Agopianda27af92012-09-13 18:17:13 -0700433 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
434 "couldn't create EGLContext");
435
Mathias Agopiancde87a32012-09-13 14:09:01 -0700436 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700437 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700438 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700439 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700440 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700441 // All non-virtual displays are currently considered secure.
442 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800443 createBuiltinDisplayLocked(type);
444 wp<IBinder> token = mBuiltinDisplays[i];
445
Dan Stozab9b08832014-03-13 11:55:57 -0700446 sp<IGraphicBufferProducer> producer;
447 sp<IGraphicBufferConsumer> consumer;
448 BufferQueue::createBufferQueue(&producer, &consumer,
449 new GraphicBufferAlloc());
450
451 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
452 consumer);
Jesse Hall19e87292013-12-23 21:02:15 -0800453 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700454 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800455 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Dan Stozab9b08832014-03-13 11:55:57 -0700456 fbs, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -0800457 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700458 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700459 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700460 // for displays other than the main display, so we always
461 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800462 ALOGD("marking display %zu as acquired/unblanked", i);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700463 hw->setPowerMode(HWC_POWER_MODE_NORMAL);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700464 }
465 mDisplays.add(token, hw);
466 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700467 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700468
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700469 // make the GLContext current so that we can create textures when creating Layers
470 // (which may happens before we render something)
471 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
472
Mathias Agopian028508c2012-07-25 21:12:12 -0700473 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700474 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700475 vsyncPhaseOffsetNs, true, "app");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700476 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700477 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700478 sfVsyncPhaseOffsetNs, true, "sf");
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700479 mSFEventThread = new EventThread(sfVsyncSrc);
480 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700481
Jamie Gennisd1700752013-10-14 12:22:52 -0700482 mEventControlThread = new EventControlThread(this);
483 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
484
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700485 // set a fake vsync period if there is no HWComposer
486 if (mHwc->initCheck() != NO_ERROR) {
487 mPrimaryDispSync.setPeriod(16666667);
488 }
489
Mathias Agopian92a979a2012-08-02 18:32:23 -0700490 // initialize our drawing state
491 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700492
Andy McFadden13a082e2012-08-24 10:16:42 -0700493 // set initial conditions (e.g. unblank default device)
494 initializeDisplays();
495
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700496 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700497 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800498}
499
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700500int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700501 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700502 type : mHwc->allocateDisplayId();
503}
504
Mathias Agopiana67e4182012-06-19 17:26:12 -0700505void SurfaceFlinger::startBootAnim() {
506 // start boot animation
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -0700507 mBootFinished = false;
Mathias Agopiana67e4182012-06-19 17:26:12 -0700508 property_set("service.bootanim.exit", "0");
509 property_set("ctl.start", "bootanim");
510}
511
Mathias Agopian875d8e12013-06-07 15:35:48 -0700512size_t SurfaceFlinger::getMaxTextureSize() const {
513 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700514}
515
Mathias Agopian875d8e12013-06-07 15:35:48 -0700516size_t SurfaceFlinger::getMaxViewportDims() const {
517 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700518}
519
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800520// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800521
Jamie Gennis582270d2011-08-17 18:19:00 -0700522bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800523 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800524 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800525 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700526 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800527}
528
Dan Stoza7f7da322014-05-02 15:26:25 -0700529status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
530 Vector<DisplayInfo>* configs) {
531 if (configs == NULL) {
532 return BAD_VALUE;
533 }
534
Jesse Hall692c7232012-11-08 15:41:56 -0800535 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700536 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800537 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700538 type = i;
539 break;
540 }
541 }
542
543 if (type < 0) {
544 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700545 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700546
Mathias Agopian8b736f12012-08-13 17:54:26 -0700547 // TODO: Not sure if display density should handled by SF any longer
548 class Density {
549 static int getDensityFromProperty(char const* propName) {
550 char property[PROPERTY_VALUE_MAX];
551 int density = 0;
552 if (property_get(propName, property, NULL) > 0) {
553 density = atoi(property);
554 }
555 return density;
556 }
557 public:
558 static int getEmuDensity() {
559 return getDensityFromProperty("qemu.sf.lcd_density"); }
560 static int getBuildDensity() {
561 return getDensityFromProperty("ro.sf.lcd_density"); }
562 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700563
Dan Stoza7f7da322014-05-02 15:26:25 -0700564 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700565
Dan Stoza7f7da322014-05-02 15:26:25 -0700566 const Vector<HWComposer::DisplayConfig>& hwConfigs =
567 getHwComposer().getConfigs(type);
568 for (size_t c = 0; c < hwConfigs.size(); ++c) {
569 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
570 DisplayInfo info = DisplayInfo();
571
572 float xdpi = hwConfig.xdpi;
573 float ydpi = hwConfig.ydpi;
574
575 if (type == DisplayDevice::DISPLAY_PRIMARY) {
576 // The density of the device is provided by a build property
577 float density = Density::getBuildDensity() / 160.0f;
578 if (density == 0) {
579 // the build doesn't provide a density -- this is wrong!
580 // use xdpi instead
581 ALOGE("ro.sf.lcd_density must be defined as a build property");
582 density = xdpi / 160.0f;
583 }
584 if (Density::getEmuDensity()) {
585 // if "qemu.sf.lcd_density" is specified, it overrides everything
586 xdpi = ydpi = density = Density::getEmuDensity();
587 density /= 160.0f;
588 }
589 info.density = density;
590
591 // TODO: this needs to go away (currently needed only by webkit)
592 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
593 info.orientation = hw->getOrientation();
594 } else {
595 // TODO: where should this value come from?
596 static const int TV_DENSITY = 213;
597 info.density = TV_DENSITY / 160.0f;
598 info.orientation = 0;
599 }
600
601 info.w = hwConfig.width;
602 info.h = hwConfig.height;
603 info.xdpi = xdpi;
604 info.ydpi = ydpi;
605 info.fps = float(1e9 / hwConfig.refresh);
Andy McFadden91b2ca82014-06-13 14:04:23 -0700606 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
607
608 // This is how far in advance a buffer must be queued for
609 // presentation at a given time. If you want a buffer to appear
610 // on the screen at time N, you must submit the buffer before
611 // (N - presentationDeadline).
612 //
613 // Normally it's one full refresh period (to give SF a chance to
614 // latch the buffer), but this can be reduced by configuring a
615 // DispSync offset. Any additional delays introduced by the hardware
616 // composer or panel must be accounted for here.
617 //
618 // We add an additional 1ms to allow for processing time and
619 // differences between the ideal and actual refresh rate.
620 info.presentationDeadline =
621 hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700622
623 // All non-virtual displays are currently considered secure.
624 info.secure = true;
625
626 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700627 }
628
Dan Stoza7f7da322014-05-02 15:26:25 -0700629 return NO_ERROR;
630}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700631
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700632status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& display,
633 DisplayStatInfo* stats) {
634 if (stats == NULL) {
635 return BAD_VALUE;
636 }
637
638 // FIXME for now we always return stats for the primary display
639 memset(stats, 0, sizeof(*stats));
640 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
641 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
642 return NO_ERROR;
643}
644
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700645int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
646 return getDisplayDevice(display)->getActiveConfig();
Dan Stoza7f7da322014-05-02 15:26:25 -0700647}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700648
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700649void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
650 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
651 this);
652 int32_t type = hw->getDisplayType();
653 int currentMode = hw->getActiveConfig();
654
655 if (mode == currentMode) {
656 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
657 return;
658 }
659
660 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
661 ALOGW("Trying to set config for virtual display");
662 return;
663 }
664
665 hw->setActiveConfig(mode);
666 getHwComposer().setActiveConfig(type, mode);
667}
668
669status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
670 class MessageSetActiveConfig: public MessageBase {
671 SurfaceFlinger& mFlinger;
672 sp<IBinder> mDisplay;
673 int mMode;
674 public:
675 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
676 int mode) :
677 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
678 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700679 Vector<DisplayInfo> configs;
680 mFlinger.getDisplayConfigs(mDisplay, &configs);
681 if(mMode < 0 || mMode >= configs.size()) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700682 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700683 mMode, configs.size());
684 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700685 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
686 if (hw == NULL) {
687 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700688 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700689 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
690 ALOGW("Attempt to set active config = %d for virtual display",
691 mMode);
692 } else {
693 mFlinger.setActiveConfigInternal(hw, mMode);
694 }
695 return true;
696 }
697 };
698 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
699 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700700 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700701}
702
Svetoslavd85084b2014-03-20 10:28:31 -0700703status_t SurfaceFlinger::clearAnimationFrameStats() {
704 Mutex::Autolock _l(mStateLock);
705 mAnimFrameTracker.clearStats();
706 return NO_ERROR;
707}
708
709status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
710 Mutex::Autolock _l(mStateLock);
711 mAnimFrameTracker.getStats(outStats);
712 return NO_ERROR;
713}
714
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800715// ----------------------------------------------------------------------------
716
717sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800718 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700719}
720
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800721// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800722
723void SurfaceFlinger::waitForEvent() {
724 mEventQueue.waitMessage();
725}
726
727void SurfaceFlinger::signalTransaction() {
728 mEventQueue.invalidate();
729}
730
731void SurfaceFlinger::signalLayerUpdate() {
732 mEventQueue.invalidate();
733}
734
735void SurfaceFlinger::signalRefresh() {
736 mEventQueue.refresh();
737}
738
739status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800740 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800741 return mEventQueue.postMessage(msg, reltime);
742}
743
744status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800745 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800746 status_t res = mEventQueue.postMessage(msg, reltime);
747 if (res == NO_ERROR) {
748 msg->wait();
749 }
750 return res;
751}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800752
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700753void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700754 do {
755 waitForEvent();
756 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757}
758
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700759void SurfaceFlinger::enableHardwareVsync() {
760 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700761 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700762 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700763 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
764 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700765 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700766 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700767}
768
Jesse Hall948fe0c2013-10-14 12:56:09 -0700769void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700770 Mutex::Autolock _l(mHWVsyncLock);
771
Jesse Hall948fe0c2013-10-14 12:56:09 -0700772 if (makeAvailable) {
773 mHWVsyncAvailable = true;
774 } else if (!mHWVsyncAvailable) {
775 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
776 return;
777 }
778
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700779 const nsecs_t period =
780 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
781
782 mPrimaryDispSync.reset();
783 mPrimaryDispSync.setPeriod(period);
784
785 if (!mPrimaryHWVsyncEnabled) {
786 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700787 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
788 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700789 mPrimaryHWVsyncEnabled = true;
790 }
791}
792
Jesse Hall948fe0c2013-10-14 12:56:09 -0700793void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700794 Mutex::Autolock _l(mHWVsyncLock);
795 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700796 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
797 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700798 mPrimaryDispSync.endResync();
799 mPrimaryHWVsyncEnabled = false;
800 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700801 if (makeUnavailable) {
802 mHWVsyncAvailable = false;
803 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700804}
805
806void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700807 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700808
Jamie Gennisd1700752013-10-14 12:22:52 -0700809 { // Scope for the lock
810 Mutex::Autolock _l(mHWVsyncLock);
811 if (type == 0 && mPrimaryHWVsyncEnabled) {
812 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700813 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700814 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700815
816 if (needsHwVsync) {
817 enableHardwareVsync();
818 } else {
819 disableHardwareVsync(false);
820 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700821}
822
823void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
824 if (mEventThread == NULL) {
825 // This is a temporary workaround for b/7145521. A non-null pointer
826 // does not mean EventThread has finished initializing, so this
827 // is not a correct fix.
828 ALOGW("WARNING: EventThread not started, ignoring hotplug");
829 return;
830 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700831
Jesse Hall9e663de2013-08-16 14:28:37 -0700832 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700833 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800834 if (connected) {
835 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700836 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800837 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
838 mBuiltinDisplays[type].clear();
Arun Kumar K.R766d5f62013-06-21 19:36:02 -0700839#ifdef QCOM_BSP
840 // if extended_mode is set, and set mVisibleRegionsDirty
841 // as we need to rebuildLayerStack
842 if(isExtendedMode()) {
843 mVisibleRegionsDirty = true;
844 }
845#endif
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700846 }
847 setTransactionFlags(eDisplayTransactionNeeded);
848
Andy McFadden9e9689c2012-10-10 18:17:51 -0700849 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700850 }
Mathias Agopian86303202012-07-24 22:46:10 -0700851}
852
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700853void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700854 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700855 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700856}
857
Mathias Agopian4fec8732012-06-29 14:12:52 -0700858void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800859 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800860 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700861 case MessageQueue::TRANSACTION:
862 handleMessageTransaction();
863 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700864 case MessageQueue::INVALIDATE:
865 handleMessageTransaction();
866 handleMessageInvalidate();
867 signalRefresh();
868 break;
869 case MessageQueue::REFRESH:
870 handleMessageRefresh();
871 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800872 }
873}
874
Mathias Agopian4fec8732012-06-29 14:12:52 -0700875void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700876 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700877 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700878 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700879 }
880}
881
882void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700883 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700884 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700885}
886
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530887#ifdef QCOM_BSP
888/* Compute DirtyRegion, if DR optimization for GPU comp optimization
889 * is ON & and no external device is connected.*/
890void SurfaceFlinger::setUpTiledDr() {
891 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
892 const sp<DisplayDevice>& hw(mDisplays[HWC_DISPLAY_PRIMARY]);
893 mCanUseGpuTileRender = computeTiledDr(hw);
894 }
895}
896#endif
Mathias Agopian4fec8732012-06-29 14:12:52 -0700897void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700898 ATRACE_CALL();
899 preComposition();
900 rebuildLayerStacks();
901 setUpHWComposer();
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530902#ifdef QCOM_BSP
903 setUpTiledDr();
904#endif
Mathias Agopiancd60f992012-08-16 16:28:27 -0700905 doDebugFlashRegions();
906 doComposition();
907 postComposition();
908}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700909
Mathias Agopiancd60f992012-08-16 16:28:27 -0700910void SurfaceFlinger::doDebugFlashRegions()
911{
912 // is debugging enabled
913 if (CC_LIKELY(!mDebugRegion))
914 return;
915
916 const bool repaintEverything = mRepaintEverything;
917 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
918 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530919
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700920 if (hw->isDisplayOn()) {
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530921 const int32_t height = hw->getHeight();
922 RenderEngine& engine(getRenderEngine());
923#ifdef QCOM_BSP
924 // Use Union DR, if it is valid & GPU Tiled DR optimization is ON
925 if(mCanUseGpuTileRender && !mUnionDirtyRect.isEmpty()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700926 // redraw the whole screen
927 doComposeSurfaces(hw, Region(hw->bounds()));
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530928 Region dirtyRegion(mUnionDirtyRect);
929 Rect dr = mUnionDirtyRect;
930 hw->eglSwapPreserved(true);
931 engine.startTileComposition(dr.left, (height-dr.bottom),
932 (dr.right-dr.left),
933 (dr.bottom-dr.top), 1);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700934 // and draw the dirty region
Mathias Agopian3f844832013-08-07 21:24:32 -0700935 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530936 engine.endTileComposition(GL_PRESERVE);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700937 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700938 hw->swapBuffers(getHwComposer());
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530939 } else
940#endif
941 {
942 // transform the dirty region into this screen's coordinate
943 // space
944 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
945 if (!dirtyRegion.isEmpty()) {
946 // redraw the whole screen
947 doComposeSurfaces(hw, Region(hw->bounds()));
948 // and draw the dirty region
949#ifdef QCOM_BSP
950 if(mGpuTileRenderEnable)
951 hw->eglSwapPreserved(false);
952#endif
953
954 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
955 hw->compositionComplete();
956 hw->swapBuffers(getHwComposer());
957 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700958 }
959 }
960 }
961
962 postFramebuffer();
963
964 if (mDebugRegion > 1) {
965 usleep(mDebugRegion * 1000);
966 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700967
968 HWComposer& hwc(getHwComposer());
969 if (hwc.initCheck() == NO_ERROR) {
970 status_t err = hwc.prepare();
971 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
972 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700973}
974
975void SurfaceFlinger::preComposition()
976{
977 bool needExtraInvalidate = false;
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 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700982 needExtraInvalidate = true;
983 }
984 }
985 if (needExtraInvalidate) {
986 signalLayerUpdate();
987 }
988}
989
990void SurfaceFlinger::postComposition()
991{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700992 const LayerVector& layers(mDrawingState.layersSortedByZ);
993 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700994 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700995 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700996 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800997
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700998 const HWComposer& hwc = getHwComposer();
999 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
1000
1001 if (presentFence->isValid()) {
1002 if (mPrimaryDispSync.addPresentFence(presentFence)) {
1003 enableHardwareVsync();
1004 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -07001005 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001006 }
1007 }
1008
Andy McFadden5167ec62014-05-22 13:08:43 -07001009 if (kIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001010 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001011 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001012 enableHardwareVsync();
1013 }
1014 }
1015
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001016 if (mAnimCompositionPending) {
1017 mAnimCompositionPending = false;
1018
Jesse Halla9a1b002013-02-27 16:39:25 -08001019 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001020 mAnimFrameTracker.setActualPresentFence(presentFence);
1021 } else {
1022 // The HWC doesn't support present fences, so use the refresh
1023 // timestamp instead.
1024 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1025 mAnimFrameTracker.setActualPresentTime(presentTime);
1026 }
1027 mAnimFrameTracker.advanceFrame();
1028 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001029}
1030
1031void SurfaceFlinger::rebuildLayerStacks() {
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07001032#ifdef QCOM_BSP
1033 char prop[PROPERTY_VALUE_MAX];
1034 property_get("sys.extended_mode", prop, "0");
1035 sExtendedMode = atoi(prop) ? true : false;
1036#endif
Mathias Agopiancd60f992012-08-16 16:28:27 -07001037 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001038 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001039 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -07001040 mVisibleRegionsDirty = false;
1041 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001042
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001043 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001044 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001045 Region opaqueRegion;
1046 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -08001047 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -07001048 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001049 const Transform& tr(hw->getTransform());
1050 const Rect bounds(hw->getBounds());
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001051 int dpyId = hw->getHwcDisplayId();
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001052 if (hw->isDisplayOn()) {
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001053 SurfaceFlinger::computeVisibleRegions(dpyId, layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001054 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001055
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001056 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001057 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001058 const sp<Layer>& layer(layers[i]);
1059 const Layer::State& s(layer->getDrawingState());
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001060 Region drawRegion(tr.transform(
1061 layer->visibleNonTransparentRegion));
1062 drawRegion.andSelf(bounds);
1063 if (!drawRegion.isEmpty()) {
1064 layersSortedByZ.add(layer);
Mathias Agopian87baae12012-07-31 12:38:26 -07001065 }
1066 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001067 }
Mathias Agopian42977342012-08-05 00:40:46 -07001068 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001069 hw->undefinedRegion.set(bounds);
1070 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
1071 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001072 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001073 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001074}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001075
Mathias Agopiancd60f992012-08-16 16:28:27 -07001076void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001077 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001078 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1079 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1080 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1081
1082 // If nothing has changed (!dirty), don't recompose.
1083 // If something changed, but we don't currently have any visible layers,
1084 // and didn't when we last did a composition, then skip it this time.
1085 // The second rule does two things:
1086 // - When all layers are removed from a display, we'll emit one black
1087 // frame, then nothing more until we get new layers.
1088 // - When a display is created with a private layer stack, we won't
1089 // emit any black frames until a layer is added to the layer stack.
1090 bool mustRecompose = dirty && !(empty && wasEmpty);
1091
1092 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1093 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1094 mustRecompose ? "doing" : "skipping",
1095 dirty ? "+" : "-",
1096 empty ? "+" : "-",
1097 wasEmpty ? "+" : "-");
1098
Dan Stoza71433162014-02-04 16:22:36 -08001099 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001100
1101 if (mustRecompose) {
1102 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1103 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001104 }
1105
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001106 HWComposer& hwc(getHwComposer());
1107 if (hwc.initCheck() == NO_ERROR) {
1108 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001109 if (CC_UNLIKELY(mHwWorkListDirty)) {
1110 mHwWorkListDirty = false;
1111 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1112 sp<const DisplayDevice> hw(mDisplays[dpy]);
1113 const int32_t id = hw->getHwcDisplayId();
1114 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001115 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001116 hw->getVisibleLayersSortedByZ());
1117 const size_t count = currentLayers.size();
1118 if (hwc.createWorkList(id, count) == NO_ERROR) {
1119 HWComposer::LayerListIterator cur = hwc.begin(id);
1120 const HWComposer::LayerListIterator end = hwc.end(id);
1121 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001122 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001123 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001124 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001125 cur->setSkip(true);
1126 }
1127 }
1128 }
1129 }
1130 }
1131 }
1132
1133 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001134 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001135 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001136 const int32_t id = hw->getHwcDisplayId();
1137 if (id >= 0) {
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001138 // Get the layers in the current drawying state
1139 const LayerVector& layers(mDrawingState.layersSortedByZ);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001140#ifdef QCOM_BSP
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001141 bool freezeSurfacePresent = false;
1142 const size_t layerCount = layers.size();
Ramkumar Radhakrishnanced0b062014-08-11 15:31:07 -07001143 // Look for ScreenShotSurface in external layer list, only when
1144 // disable external rotation animation feature is enabled
1145 if(mDisableExtAnimation && (id != HWC_DISPLAY_PRIMARY)) {
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001146 for (size_t i = 0 ; i < layerCount ; ++i) {
1147 static int screenShotLen = strlen("ScreenshotSurface");
1148 const sp<Layer>& layer(layers[i]);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001149 const Layer::State& s(layer->getDrawingState());
1150 // check the layers associated with external display
1151 if(s.layerStack == hw->getLayerStack()) {
1152 if(!strncmp(layer->getName(), "ScreenshotSurface",
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001153 screenShotLen)) {
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001154 // Screenshot layer is present, and animation in
1155 // progress
1156 freezeSurfacePresent = true;
1157 break;
1158 }
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001159 }
1160 }
1161 }
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001162#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001163 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001164 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001165 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001166 HWComposer::LayerListIterator cur = hwc.begin(id);
1167 const HWComposer::LayerListIterator end = hwc.end(id);
1168 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1169 /*
1170 * update the per-frame h/w composer data for each layer
1171 * and build the transparent region of the FB
1172 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001173 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001174 layer->setPerFrameData(hw, *cur);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001175#ifdef QCOM_BSP
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001176 if(freezeSurfacePresent) {
1177 // if freezeSurfacePresent, set ANIMATING flag
1178 cur->setAnimating(true);
1179 } else {
1180 const KeyedVector<wp<IBinder>, DisplayDeviceState>&
1181 draw(mDrawingState.displays);
1182 size_t dc = draw.size();
1183 for (size_t i=0 ; i<dc ; i++) {
1184 if (draw[i].isMainDisplay()) {
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001185 // Pass the current orientation to HWC
1186 hwc.eventControl(HWC_DISPLAY_PRIMARY,
1187 SurfaceFlinger::EVENT_ORIENTATION,
1188 uint32_t(draw[i].orientation));
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001189 }
1190 }
1191 }
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001192#endif
Mathias Agopian1e260872012-08-08 18:35:12 -07001193 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001194 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001195 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001196
Riley Andrews03414a12014-07-01 14:22:59 -07001197 // If possible, attempt to use the cursor overlay on each display.
1198 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1199 sp<const DisplayDevice> hw(mDisplays[dpy]);
1200 const int32_t id = hw->getHwcDisplayId();
1201 if (id >= 0) {
1202 const Vector< sp<Layer> >& currentLayers(
1203 hw->getVisibleLayersSortedByZ());
1204 const size_t count = currentLayers.size();
1205 HWComposer::LayerListIterator cur = hwc.begin(id);
1206 const HWComposer::LayerListIterator end = hwc.end(id);
1207 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1208 const sp<Layer>& layer(currentLayers[i]);
1209 if (layer->isPotentialCursor()) {
1210 cur->setIsCursorLayerHint();
1211 break;
1212 }
1213 }
1214 }
1215 }
1216
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001217 status_t err = hwc.prepare();
1218 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001219
1220 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1221 sp<const DisplayDevice> hw(mDisplays[dpy]);
1222 hw->prepareFrame(hwc);
1223 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001224 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001225}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001226
Mathias Agopiancd60f992012-08-16 16:28:27 -07001227void SurfaceFlinger::doComposition() {
1228 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001229 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001230 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001231 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001232 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001233 // transform the dirty region into this screen's coordinate space
1234 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001235
1236 // repaint the framebuffer (if needed)
1237 doDisplayComposition(hw, dirtyRegion);
1238
Mathias Agopiancd60f992012-08-16 16:28:27 -07001239 hw->dirtyRegion.clear();
1240 hw->flip(hw->swapRegion);
1241 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001242 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001243 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001244 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001245 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001246 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001247}
1248
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001249void SurfaceFlinger::postFramebuffer()
1250{
Mathias Agopian841cde52012-03-01 15:44:37 -08001251 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001252
Mathias Agopiana44b0412011-10-16 18:46:35 -07001253 const nsecs_t now = systemTime();
1254 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001255
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001256 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001257 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001258 if (!hwc.supportsFramebufferTarget()) {
1259 // EGL spec says:
1260 // "surface must be bound to the calling thread's current context,
1261 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001262 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001263 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001264 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001265 }
1266
Mathias Agopian6da15f42013-09-25 20:40:07 -07001267 // make the default display current because the VirtualDisplayDevice code cannot
1268 // deal with dequeueBuffer() being called outside of the composition loop; however
1269 // the code below can call glFlush() which is allowed (and does in some case) call
1270 // dequeueBuffer().
1271 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1272
Mathias Agopian92a979a2012-08-02 18:32:23 -07001273 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001274 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001275 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001276 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001277 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001278 int32_t id = hw->getHwcDisplayId();
1279 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001280 HWComposer::LayerListIterator cur = hwc.begin(id);
1281 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001282 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001283 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001284 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001285 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001286 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001287 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001288 }
Jesse Hallef194142012-06-14 14:45:17 -07001289 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001290 }
1291
Mathias Agopiana44b0412011-10-16 18:46:35 -07001292 mLastSwapBufferTime = systemTime() - now;
1293 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001294
1295 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1296 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1297 logFrameStats();
1298 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299}
1300
Mathias Agopian87baae12012-07-31 12:38:26 -07001301void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302{
Mathias Agopian841cde52012-03-01 15:44:37 -08001303 ATRACE_CALL();
1304
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001305 // here we keep a copy of the drawing state (that is the state that's
1306 // going to be overwritten by handleTransactionLocked()) outside of
1307 // mStateLock so that the side-effects of the State assignment
1308 // don't happen with mStateLock held (which can cause deadlocks).
1309 State drawingState(mDrawingState);
1310
Mathias Agopianca4d3602011-05-19 15:38:14 -07001311 Mutex::Autolock _l(mStateLock);
1312 const nsecs_t now = systemTime();
1313 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314
Mathias Agopianca4d3602011-05-19 15:38:14 -07001315 // Here we're guaranteed that some transaction flags are set
1316 // so we can call handleTransactionLocked() unconditionally.
1317 // We call getTransactionFlags(), which will also clear the flags,
1318 // with mStateLock held to guarantee that mCurrentState won't change
1319 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001320
Mathias Agopiane57f2922012-08-09 16:29:12 -07001321 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001322 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001323
Mathias Agopianca4d3602011-05-19 15:38:14 -07001324 mLastTransactionTime = systemTime() - now;
1325 mDebugInTransaction = 0;
1326 invalidateHwcGeometry();
1327 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001328}
1329
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301330void SurfaceFlinger::setVirtualDisplayData(
1331 int32_t hwcDisplayId,
1332 const sp<IGraphicBufferProducer>& sink)
1333{
1334 sp<ANativeWindow> mNativeWindow = new Surface(sink);
1335 ANativeWindow* const window = mNativeWindow.get();
1336
1337 int format;
1338 window->query(window, NATIVE_WINDOW_FORMAT, &format);
1339
1340 EGLSurface surface;
1341 EGLint w, h;
1342 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1343 surface = eglCreateWindowSurface(display, mRenderEngine->getEGLConfig(), window, NULL);
1344 eglQuerySurface(display, surface, EGL_WIDTH, &w);
1345 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
1346
1347 mHwc->setVirtualDisplayProperties(hwcDisplayId, w, h, format);
1348}
1349
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001350void SurfaceFlinger::configureVirtualDisplay(int32_t &hwcDisplayId,
1351 sp<DisplaySurface> &dispSurface,
1352 sp<IGraphicBufferProducer> &producer,
1353 const DisplayDeviceState state,
1354 sp<IGraphicBufferProducer> bqProducer,
1355 sp<IGraphicBufferConsumer> bqConsumer)
1356{
1357 bool vdsEnabled = mHwc->isVDSEnabled();
1358
1359 //for V4L2 based virtual display implementation
1360 if(!vdsEnabled) {
1361 // persist.sys.wfd.virtual will be set if WFD is launched via
1362 // settings app. This is currently being done in
1363 // ExtendedRemoteDisplay-WFD stack.
1364 // This flag will be reset at the time of disconnection of virtual WFD
1365 // display.
1366 // This flag is set to zero if WFD is launched via QCOM WFD
1367 // proprietary APIs which use HDMI piggyback approach.
1368 char value[PROPERTY_VALUE_MAX];
1369 property_get("persist.sys.wfd.virtual", value, "0");
1370 int wfdVirtual = atoi(value);
1371 if(!wfdVirtual) {
1372 // This is for non-wfd virtual display scenarios(e.g. SSD/SR/CTS)
1373 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001374 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1375 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001376 dispSurface = vds;
1377 // There won't be any interaction with HWC for this virtual display.
1378 // so the GLES driver can pass buffers directly to the sink.
1379 producer = state.surface;
1380 } else {
Raj Kamal366d9b42014-06-11 13:49:32 +05301381 int sinkUsage = -1;
1382 state.surface->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
1383#ifdef QCOM_BSP
1384 if(sinkUsage & GRALLOC_USAGE_PRIVATE_WFD)
1385#endif
1386 hwcDisplayId = allocateHwcDisplayId(state.type);
1387
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001388 if (hwcDisplayId >= 0) {
1389 // This is for WFD virtual display scenario.
1390 // Read virtual display properties and create a
1391 // rendering surface for it inorder to be handled by hwc.
1392 setVirtualDisplayData(hwcDisplayId, state.surface);
1393 dispSurface = new FramebufferSurface(*mHwc, state.type,
1394 bqConsumer);
1395 producer = bqProducer;
1396 } else {
1397 // in case of WFD Virtual + SSD/SR concurrency scenario,
1398 // WFD virtual display instance gets valid hwcDisplayId and
1399 // SSD/SR will get invalid hwcDisplayId
1400 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001401 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1402 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001403 dispSurface = vds;
1404 // There won't be any interaction with HWC for this virtual
1405 // display, so the GLES driver can pass buffers directly to the
1406 // sink.
1407 producer = state.surface;
1408 }
1409 }
1410 } else {
1411 // VDS solution is enabled
1412 // HWC is allocated for first virtual display.
1413 // Subsequent virtual display sessions will be composed by GLES driver.
1414 // ToDo: Modify VDS component to allocate hwcDisplayId based on
1415 // mForceHwcCopy (which is based on Usage Flags)
1416
1417 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001418 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1419 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001420 dispSurface = vds;
1421 if (hwcDisplayId >= 0) {
1422 producer = vds;
1423 } else {
1424 // There won't be any interaction with HWC for this virtual display,
1425 // so the GLES driver can pass buffers directly to the sink.
1426 producer = state.surface;
1427 }
1428 }
1429}
1430
Mathias Agopian87baae12012-07-31 12:38:26 -07001431void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001432{
1433 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001434 const size_t count = currentLayers.size();
1435
1436 /*
1437 * Traversal of the children
1438 * (perform the transaction for each of them if needed)
1439 */
1440
Mathias Agopian3559b072012-08-15 13:46:03 -07001441 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001442 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001443 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001444 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1445 if (!trFlags) continue;
1446
1447 const uint32_t flags = layer->doTransaction(0);
1448 if (flags & Layer::eVisibleRegion)
1449 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001450 }
1451 }
1452
1453 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001454 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455 */
1456
Mathias Agopiane57f2922012-08-09 16:29:12 -07001457 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001458 // here we take advantage of Vector's copy-on-write semantics to
1459 // improve performance by skipping the transaction entirely when
1460 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001461 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1462 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001463 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001464 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001465 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001466 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001467
1468 // find the displays that were removed
1469 // (ie: in drawing state but not in current state)
1470 // also handle displays that changed
1471 // (ie: displays that are in both lists)
1472 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001473 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1474 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001475 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001476 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001477 // Call makeCurrent() on the primary display so we can
1478 // be sure that nothing associated with this display
1479 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001480 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001481 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001482 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1483 if (hw != NULL)
1484 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001485 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001486 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001487 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001488 } else {
1489 ALOGW("trying to remove the main display");
1490 }
1491 } else {
1492 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001493 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001494 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001495 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001496 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001497 // recreating the DisplayDevice, so we just remove it
1498 // from the drawing state, so that it get re-added
1499 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001500 sp<DisplayDevice> hw(getDisplayDevice(display));
1501 if (hw != NULL)
1502 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001503 mDisplays.removeItem(display);
1504 mDrawingState.displays.removeItemsAt(i);
1505 dc--; i--;
1506 // at this point we must loop to the next item
1507 continue;
1508 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001509
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001510 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001511 if (disp != NULL) {
1512 if (state.layerStack != draw[i].layerStack) {
1513 disp->setLayerStack(state.layerStack);
1514 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001515 if ((state.orientation != draw[i].orientation)
1516 || (state.viewport != draw[i].viewport)
1517 || (state.frame != draw[i].frame))
1518 {
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001519#ifdef QCOM_BSP
1520 int orient = state.orientation;
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001521 // Honor the orientation change after boot
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301522 // animation completes and make sure boot
1523 // animation is shown in panel orientation always.
1524 if(mBootFinished){
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001525 disp->setProjection(state.orientation,
1526 state.viewport, state.frame);
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001527 orient = state.orientation;
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001528 }
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301529 else{
1530 char property[PROPERTY_VALUE_MAX];
1531 int panelOrientation =
1532 DisplayState::eOrientationDefault;
1533 if(property_get("persist.panel.orientation",
1534 property, "0") > 0){
1535 panelOrientation = atoi(property) / 90;
1536 }
1537 disp->setProjection(panelOrientation,
1538 state.viewport, state.frame);
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001539 orient = panelOrientation;
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301540 }
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001541 // Set the view frame of each display only of its
1542 // default orientation.
Saurabh Shah027b3852014-08-21 16:09:32 -07001543 if(orient == DisplayState::eOrientationDefault and
1544 state.frame.isValid()) {
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001545 qdutils::setViewFrame(disp->getHwcDisplayId(),
1546 state.frame.left, state.frame.top,
1547 state.frame.right, state.frame.bottom);
1548 }
1549#else
1550 disp->setProjection(state.orientation,
1551 state.viewport, state.frame);
1552#endif
Mathias Agopian93997a82012-08-29 17:30:36 -07001553 }
Michael Lentine47e45402014-07-18 15:34:25 -07001554 if (state.width != draw[i].width || state.height != draw[i].height) {
1555 disp->setDisplaySize(state.width, state.height);
1556 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001557 }
1558 }
1559 }
1560
1561 // find displays that were added
1562 // (ie: in current state but not in drawing state)
1563 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001564 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001565 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001566
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001567 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001568 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001569 sp<IGraphicBufferProducer> bqProducer;
1570 sp<IGraphicBufferConsumer> bqConsumer;
1571 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1572 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001573
Jesse Hall02d86562013-03-25 14:43:23 -07001574 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001575 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001576 // Virtual displays without a surface are dormant:
1577 // they have external state (layer stack, projection,
1578 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001579 if (state.surface != NULL) {
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001580 configureVirtualDisplay(hwcDisplayId,
1581 dispSurface, producer, state, bqProducer,
1582 bqConsumer);
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001583 }
1584 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001585 ALOGE_IF(state.surface!=NULL,
1586 "adding a supported display, but rendering "
1587 "surface is provided (%p), ignoring it",
1588 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001589 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001590 // for supported (by hwc) displays we provide our
1591 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001592 dispSurface = new FramebufferSurface(*mHwc, state.type,
1593 bqConsumer);
1594 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001595 }
1596
1597 const wp<IBinder>& display(curr.keyAt(i));
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301598 if (dispSurface != NULL && producer != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001599 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001600 state.type, hwcDisplayId,
1601 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001602 display, dispSurface, producer,
1603 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001604 hw->setLayerStack(state.layerStack);
1605 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001606 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001607 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001608 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001609 if (state.isVirtualDisplay()) {
1610 if (hwcDisplayId >= 0) {
1611 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1612 hw->getWidth(), hw->getHeight(),
1613 hw->getFormat());
1614 }
1615 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001616 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001617 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001618 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001619 }
1620 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001621 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001622 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001623
Mathias Agopian84300952012-11-21 16:02:13 -08001624 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1625 // The transform hint might have changed for some layers
1626 // (either because a display has changed, or because a layer
1627 // as changed).
1628 //
1629 // Walk through all the layers in currentLayers,
1630 // and update their transform hint.
1631 //
1632 // If a layer is visible only on a single display, then that
1633 // display is used to calculate the hint, otherwise we use the
1634 // default display.
1635 //
1636 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1637 // the hint is set before we acquire a buffer from the surface texture.
1638 //
1639 // NOTE: layer transactions have taken place already, so we use their
1640 // drawing state. However, SurfaceFlinger's own transaction has not
1641 // happened yet, so we must use the current state layer list
1642 // (soon to become the drawing state list).
1643 //
1644 sp<const DisplayDevice> disp;
1645 uint32_t currentlayerStack = 0;
1646 for (size_t i=0; i<count; i++) {
1647 // NOTE: we rely on the fact that layers are sorted by
1648 // layerStack first (so we don't have to traverse the list
1649 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001650 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001651 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001652 if (i==0 || currentlayerStack != layerStack) {
1653 currentlayerStack = layerStack;
1654 // figure out if this layerstack is mirrored
1655 // (more than one display) if so, pick the default display,
1656 // if not, pick the only display it's on.
1657 disp.clear();
1658 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1659 sp<const DisplayDevice> hw(mDisplays[dpy]);
1660 if (hw->getLayerStack() == currentlayerStack) {
1661 if (disp == NULL) {
1662 disp = hw;
1663 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001664 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001665 break;
1666 }
1667 }
1668 }
1669 }
Chet Haase91d25932013-04-11 15:24:55 -07001670 if (disp == NULL) {
1671 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1672 // redraw after transform hint changes. See bug 8508397.
1673
1674 // could be null when this layer is using a layerStack
1675 // that is not visible on any display. Also can occur at
1676 // screen off/on times.
1677 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001678 }
Chet Haase91d25932013-04-11 15:24:55 -07001679 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001680 }
1681 }
1682
1683
Mathias Agopian3559b072012-08-15 13:46:03 -07001684 /*
1685 * Perform our own transaction if needed
1686 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001687
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001688 const LayerVector& layers(mDrawingState.layersSortedByZ);
1689 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001690 // layers have been added
1691 mVisibleRegionsDirty = true;
1692 }
1693
1694 // some layers might have been removed, so
1695 // we need to update the regions they're exposing.
1696 if (mLayersRemoved) {
1697 mLayersRemoved = false;
1698 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001699 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001700 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001701 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001702 if (currentLayers.indexOf(layer) < 0) {
1703 // this layer is not visible anymore
1704 // TODO: we could traverse the tree from front to back and
1705 // compute the actual visible region
1706 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001707 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001708 Region visibleReg = s.transform.transform(
1709 Region(Rect(s.active.w, s.active.h)));
1710 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001711 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001712 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001713 }
1714
1715 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001716
1717 updateCursorAsync();
1718}
1719
1720void SurfaceFlinger::updateCursorAsync()
1721{
1722 HWComposer& hwc(getHwComposer());
1723 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1724 sp<const DisplayDevice> hw(mDisplays[dpy]);
1725 const int32_t id = hw->getHwcDisplayId();
1726 if (id < 0) {
1727 continue;
1728 }
1729 const Vector< sp<Layer> >& currentLayers(
1730 hw->getVisibleLayersSortedByZ());
1731 const size_t count = currentLayers.size();
1732 HWComposer::LayerListIterator cur = hwc.begin(id);
1733 const HWComposer::LayerListIterator end = hwc.end(id);
1734 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1735 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1736 continue;
1737 }
1738 const sp<Layer>& layer(currentLayers[i]);
1739 Rect cursorPos = layer->getPosition(hw);
1740 hwc.setCursorPositionAsync(id, cursorPos);
1741 break;
1742 }
1743 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001744}
1745
1746void SurfaceFlinger::commitTransaction()
1747{
1748 if (!mLayersPendingRemoval.isEmpty()) {
1749 // Notify removed layers now that they can't be drawn from
1750 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1751 mLayersPendingRemoval[i]->onRemoved();
1752 }
1753 mLayersPendingRemoval.clear();
1754 }
1755
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001756 // If this transaction is part of a window animation then the next frame
1757 // we composite should be considered an animation as well.
1758 mAnimCompositionPending = mAnimTransactionPending;
1759
Mathias Agopian4fec8732012-06-29 14:12:52 -07001760 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001761 mTransactionPending = false;
1762 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001763 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001764}
1765
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001766void SurfaceFlinger::computeVisibleRegions(size_t dpy,
Mathias Agopian87baae12012-07-31 12:38:26 -07001767 const LayerVector& currentLayers, uint32_t layerStack,
1768 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001769{
Mathias Agopian841cde52012-03-01 15:44:37 -08001770 ATRACE_CALL();
1771
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001772 Region aboveOpaqueLayers;
1773 Region aboveCoveredLayers;
1774 Region dirty;
1775
Mathias Agopian87baae12012-07-31 12:38:26 -07001776 outDirtyRegion.clear();
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001777 bool bIgnoreLayers = false;
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001778 int indexLOI = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001779 size_t i = currentLayers.size();
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001780#ifdef QCOM_BSP
1781 while (i--) {
1782 const sp<Layer>& layer = currentLayers[i];
1783 // iterate through the layer list to find ext_only layers and store
1784 // the index
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001785 if (layer->isSecureDisplay()) {
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001786 bIgnoreLayers = true;
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001787 indexLOI = -1;
1788 if(!dpy)
1789 indexLOI = i;
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001790 break;
1791 }
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07001792 // iterate through the layer list to find ext_only layers or yuv
1793 // layer(extended_mode) and store the index
1794 if ((dpy && (layer->isExtOnly() ||
1795 (isExtendedMode() && layer->isYuvLayer())))) {
1796 bIgnoreLayers= true;
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001797 indexLOI = i;
1798 }
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001799 }
1800 i = currentLayers.size();
1801#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001802 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001803 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001804
1805 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001806 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001807
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001808#ifdef QCOM_BSP
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07001809 // Only add the layer marked as "external_only" or yuvLayer
1810 // (extended_mode) to external list and
1811 // only remove the layer marked as "external_only" or yuvLayer in
1812 // extended_mode from primary list
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001813 // and do not add the layer marked as "internal_only" to external list
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001814 // Add secure UI layers to primary and remove other layers from internal
1815 //and external list
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07001816 if(((bIgnoreLayers && indexLOI != (int)i) ||
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001817 (!dpy && layer->isExtOnly()) ||
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07001818 (!dpy && isExtendedMode() && layer->isYuvLayer()))||
1819 (dpy && layer->isIntOnly())) {
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001820 // Ignore all other layers except the layers marked as ext_only
1821 // by setting visible non transparent region empty.
1822 Region visibleNonTransRegion;
1823 visibleNonTransRegion.set(Rect(0,0));
1824 layer->setVisibleNonTransparentRegion(visibleNonTransRegion);
1825 continue;
1826 }
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001827#endif
1828 // only consider the layers on the given later stack
1829 // Override layers created using presentation class by the layers having
1830 // ext_only flag enabled
1831 if(s.layerStack != layerStack && !bIgnoreLayers) {
1832 // set the visible region as empty since we have removed the
1833 // layerstack check in rebuildLayerStack() function.
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001834 Region visibleNonTransRegion;
1835 visibleNonTransRegion.set(Rect(0,0));
1836 layer->setVisibleNonTransparentRegion(visibleNonTransRegion);
1837 continue;
1838 }
Mathias Agopianab028732010-03-16 16:41:46 -07001839 /*
1840 * opaqueRegion: area of a surface that is fully opaque.
1841 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001842 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001843
1844 /*
1845 * visibleRegion: area of a surface that is visible on screen
1846 * and not fully transparent. This is essentially the layer's
1847 * footprint minus the opaque regions above it.
1848 * Areas covered by a translucent surface are considered visible.
1849 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001850 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001851
1852 /*
1853 * coveredRegion: area of a surface that is covered by all
1854 * visible regions above it (which includes the translucent areas).
1855 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001856 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001857
Jesse Halla8026d22012-09-25 13:25:04 -07001858 /*
1859 * transparentRegion: area of a surface that is hinted to be completely
1860 * transparent. This is only used to tell when the layer has no visible
1861 * non-transparent regions and can be removed from the layer list. It
1862 * does not affect the visibleRegion of this layer or any layers
1863 * beneath it. The hint may not be correct if apps don't respect the
1864 * SurfaceView restrictions (which, sadly, some don't).
1865 */
1866 Region transparentRegion;
1867
Mathias Agopianab028732010-03-16 16:41:46 -07001868
1869 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001870 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001871 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001872 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001873 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001874 if (!visibleRegion.isEmpty()) {
1875 // Remove the transparent area from the visible region
1876 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001877 const Transform tr(s.transform);
1878 if (tr.transformed()) {
1879 if (tr.preserveRects()) {
1880 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001881 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001882 } else {
1883 // transformation too complex, can't do the
1884 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001885 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001886 }
1887 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001888 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001889 }
Mathias Agopianab028732010-03-16 16:41:46 -07001890 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001891
Mathias Agopianab028732010-03-16 16:41:46 -07001892 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001893 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001894 if (s.alpha==255 && !translucent &&
1895 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1896 // the opaque region is the layer's footprint
1897 opaqueRegion = visibleRegion;
1898 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001899 }
1900 }
1901
Mathias Agopianab028732010-03-16 16:41:46 -07001902 // Clip the covered region to the visible region
1903 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1904
1905 // Update aboveCoveredLayers for next (lower) layer
1906 aboveCoveredLayers.orSelf(visibleRegion);
1907
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001908 // subtract the opaque region covered by the layers above us
1909 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001910
1911 // compute this layer's dirty region
1912 if (layer->contentDirty) {
1913 // we need to invalidate the whole region
1914 dirty = visibleRegion;
1915 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001916 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001917 layer->contentDirty = false;
1918 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001919 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001920 * the exposed region consists of two components:
1921 * 1) what's VISIBLE now and was COVERED before
1922 * 2) what's EXPOSED now less what was EXPOSED before
1923 *
1924 * note that (1) is conservative, we start with the whole
1925 * visible region but only keep what used to be covered by
1926 * something -- which mean it may have been exposed.
1927 *
1928 * (2) handles areas that were not covered by anything but got
1929 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001930 */
Mathias Agopianab028732010-03-16 16:41:46 -07001931 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001932 const Region oldVisibleRegion = layer->visibleRegion;
1933 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001934 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1935 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001936 }
1937 dirty.subtractSelf(aboveOpaqueLayers);
1938
1939 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001940 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001941
Mathias Agopianab028732010-03-16 16:41:46 -07001942 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001943 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001944
Jesse Halla8026d22012-09-25 13:25:04 -07001945 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001946 layer->setVisibleRegion(visibleRegion);
1947 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001948 layer->setVisibleNonTransparentRegion(
1949 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001950 }
1951
Mathias Agopian87baae12012-07-31 12:38:26 -07001952 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001953}
1954
Mathias Agopian87baae12012-07-31 12:38:26 -07001955void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1956 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001957 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001958 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1959 if (hw->getLayerStack() == layerStack) {
1960 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001961 }
1962 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001963}
1964
1965void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001966{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001967 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001968
Mathias Agopian4fec8732012-06-29 14:12:52 -07001969 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001970 const LayerVector& layers(mDrawingState.layersSortedByZ);
Eric Penner51c59cd2014-07-28 19:51:58 -07001971
1972 // Store the set of layers that need updates. This set must not change as
1973 // buffers are being latched, as this could result in a deadlock.
1974 // Example: Two producers share the same command stream and:
1975 // 1.) Layer 0 is latched
1976 // 2.) Layer 0 gets a new frame
1977 // 2.) Layer 1 gets a new frame
1978 // 3.) Layer 1 is latched.
1979 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1980 // second frame. But layer 0's second frame could be waiting on display.
1981 Vector<Layer*> layersWithQueuedFrames;
1982 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001983 const sp<Layer>& layer(layers[i]);
Eric Penner51c59cd2014-07-28 19:51:58 -07001984 if (layer->hasQueuedFrame())
1985 layersWithQueuedFrames.push_back(layer.get());
1986 }
1987 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
1988 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07001989 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001990 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001991 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001992 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001993
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001994 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001995}
1996
Mathias Agopianad456f92011-01-13 17:53:01 -08001997void SurfaceFlinger::invalidateHwcGeometry()
1998{
1999 mHwWorkListDirty = true;
2000}
2001
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002002
Mathias Agopiancd60f992012-08-16 16:28:27 -07002003void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07002004 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002005{
Dan Stoza71433162014-02-04 16:22:36 -08002006 // We only need to actually compose the display if:
2007 // 1) It is being handled by hardware composer, which may need this to
2008 // keep its virtual display state machine in sync, or
2009 // 2) There is work to be done (the dirty region isn't empty)
2010 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
2011 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
2012 return;
2013 }
2014
Mathias Agopian87baae12012-07-31 12:38:26 -07002015 Region dirtyRegion(inDirtyRegion);
2016
Mathias Agopianb8a55602009-06-26 19:06:36 -07002017 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07002018 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002019
Mathias Agopian42977342012-08-05 00:40:46 -07002020 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002021 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002022 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
2023 // takes a rectangle, we must make sure to update that whole
2024 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07002025 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002026 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002027 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002028 // We need to redraw the rectangle that will be updated
2029 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07002030 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002031 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07002032 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002033 } else {
2034 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07002035 dirtyRegion.set(hw->bounds());
2036 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002037 }
2038 }
2039
Alan Viverette9c5a3332013-09-12 20:04:35 -07002040 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Michael Lentine3f121fc2014-10-01 11:17:28 -07002041 if (!doComposeSurfaces(hw, dirtyRegion)) return;
Mathias Agopianff2ed702013-09-01 21:36:12 -07002042 } else {
2043 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07002044 mat4 colorMatrix = mColorMatrix;
2045 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07002046 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002047 }
2048 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07002049 doComposeSurfaces(hw, dirtyRegion);
2050 engine.endGroup();
2051 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002052
Mathias Agopian9c6e2972011-09-20 17:21:56 -07002053 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07002054 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07002055
2056 // swap buffers (presentation)
2057 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002058}
2059
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302060#ifdef QCOM_BSP
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302061bool SurfaceFlinger::computeTiledDr(const sp<const DisplayDevice>& hw) {
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302062 int fbWidth= hw->getWidth();
2063 int fbHeight= hw->getHeight();
2064 Rect fullScreenRect = Rect(0,0,fbWidth, fbHeight);
2065 const int32_t id = hw->getHwcDisplayId();
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302066 mUnionDirtyRect.clear();
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302067 HWComposer& hwc(getHwComposer());
2068
2069 /* Compute and return the Union of Dirty Rects.
2070 * Return false if the unionDR is fullscreen, as there is no benefit from
2071 * preserving full screen.*/
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302072 return (hwc.canUseTiledDR(id, mUnionDirtyRect) &&
2073 (mUnionDirtyRect != fullScreenRect));
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302074
2075}
2076#endif
2077
Michael Lentine3f121fc2014-10-01 11:17:28 -07002078bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07002079{
Mathias Agopian3f844832013-08-07 21:24:32 -07002080 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002081 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07002082 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07002083 HWComposer::LayerListIterator cur = hwc.begin(id);
2084 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07002085
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302086 Region clearRegion;
Jesse Halld05a17f2013-09-30 16:49:30 -07002087 bool hasGlesComposition = hwc.hasGlesComposition(id);
Sravan Kumar D.V.N74180cb2014-07-01 16:35:05 +05302088 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002089 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07002090 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08002091 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
2092 hw->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07002093 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
2094 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
2095 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
2096 }
2097 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08002098 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07002099
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07002100 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopiane60b0682012-08-21 23:34:09 -07002101 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07002102 // when using overlays, we assume a fully transparent framebuffer
2103 // NOTE: we could reduce how much we need to clear, for instance
2104 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07002105 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07002106 // We'll revisit later if needed.
Sravan Kumar D.V.N74180cb2014-07-01 16:35:05 +05302107 if(!(mGpuTileRenderEnable && (mDisplays.size()==1)))
2108 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002109 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07002110 // we start with the whole screen area
2111 const Region bounds(hw->getBounds());
2112
2113 // we remove the scissor part
2114 // we're left with the letterbox region
2115 // (common case is that letterbox ends-up being empty)
2116 const Region letterbox(bounds.subtract(hw->getScissor()));
2117
2118 // compute the area to clear
2119 Region region(hw->undefinedRegion.merge(letterbox));
2120
2121 // but limit it to the dirty region
2122 region.andSelf(dirty);
2123
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302124
Mathias Agopianb9494d52012-04-18 02:28:45 -07002125 // screen is already cleared here
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302126#ifdef QCOM_BSP
2127 clearRegion.clear();
2128 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
2129 clearRegion = region;
2130 if (cur == end) {
Radhika Ranjan Soni35c9ff22013-12-17 19:55:17 +05302131 drawWormhole(hw, region);
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302132 } else if(mCanUseGpuTileRender) {
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302133 /* If GPUTileRect DR optimization on clear only the UnionDR
2134 * (computed by computeTiledDr) which is the actual region
2135 * that will be drawn on FB in this cycle.. */
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302136 clearRegion = clearRegion.andSelf(Region(mUnionDirtyRect));
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302137 }
2138 } else
2139#endif
2140 {
2141 if (!region.isEmpty()) {
2142 if (cur != end) {
2143 if (cur->getCompositionType() != HWC_BLIT)
2144 // can happen with SurfaceView
2145 drawWormhole(hw, region);
2146 } else
2147 drawWormhole(hw, region);
2148 }
Mathias Agopianb9494d52012-04-18 02:28:45 -07002149 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002150 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002151
Mathias Agopian766dc492012-10-30 18:08:06 -07002152 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
2153 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07002154 // scissor on the main display. It should never be needed
2155 // anyways (though in theory it could since the API allows it).
2156 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07002157 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002158 if (scissor != bounds) {
2159 // scissor doesn't match the screen's dimensions, so we
2160 // need to clear everything outside of it and enable
2161 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07002162
Mathias Agopianf45c5102012-10-24 16:29:17 -07002163 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002164 const uint32_t height = hw->getHeight();
2165 engine.setScissor(scissor.left, height - scissor.bottom,
2166 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002167 }
2168 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002169 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002170
Mathias Agopian85d751c2012-08-29 16:59:24 -07002171 /*
2172 * and then, render the layers targeted at the framebuffer
2173 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002174
Mathias Agopian13127d82013-03-05 17:47:11 -08002175 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002176 const size_t count = layers.size();
2177 const Transform& tr = hw->getTransform();
2178 if (cur != end) {
2179 // we're using h/w composer
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302180#ifdef QCOM_BSP
2181 int fbWidth= hw->getWidth();
2182 int fbHeight= hw->getHeight();
2183 /* if GPUTileRender optimization property is on & can be used
2184 * i) Enable EGL_SWAP_PRESERVED flag
2185 * ii) do startTile with union DirtyRect
2186 * else , Disable EGL_SWAP_PRESERVED */
2187 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302188 if(mCanUseGpuTileRender && !mUnionDirtyRect.isEmpty()) {
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302189 hw->eglSwapPreserved(true);
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302190 Rect dr = mUnionDirtyRect;
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302191 engine.startTileComposition(dr.left, (fbHeight-dr.bottom),
2192 (dr.right-dr.left),
2193 (dr.bottom-dr.top), 0);
2194 } else {
2195 // Un Set EGL_SWAP_PRESERVED flag, if no tiling required.
2196 hw->eglSwapPreserved(false);
2197 }
2198 // DrawWormHole/Any Draw has to be within startTile & EndTile
Sravan Kumar D.V.N280bcd32014-08-28 14:54:25 +05302199 if (hasGlesComposition) {
2200 if (hasHwcComposition) {
2201 if(mCanUseGpuTileRender && !mUnionDirtyRect.isEmpty()) {
2202 const Rect& scissor(mUnionDirtyRect);
2203 engine.setScissor(scissor.left,
2204 hw->getHeight()- scissor.bottom,
2205 scissor.getWidth(), scissor.getHeight());
2206 engine.clearWithColor(0, 0, 0, 0);
2207 engine.disableScissor();
2208 } else {
2209 engine.clearWithColor(0, 0, 0, 0);
2210 }
Sravan Kumar D.V.N74180cb2014-07-01 16:35:05 +05302211 } else {
Sravan Kumar D.V.N280bcd32014-08-28 14:54:25 +05302212 if (cur->getCompositionType() != HWC_BLIT &&
2213 !clearRegion.isEmpty()) {
2214 drawWormhole(hw, clearRegion);
2215 }
Sravan Kumar D.V.N74180cb2014-07-01 16:35:05 +05302216 }
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302217 }
2218 }
2219#endif
2220
Mathias Agopian85d751c2012-08-29 16:59:24 -07002221 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002222 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07002223 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07002224 if (!clip.isEmpty()) {
2225 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07002226 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07002227 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07002228 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002229 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
2230 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08002231 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07002232 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07002233 // never clear the very first layer since we're
2234 // guaranteed the FB is already cleared
2235 layer->clearWithOpenGL(hw, clip);
2236 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002237 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07002238 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002239 case HWC_FRAMEBUFFER: {
2240 layer->draw(hw, clip);
2241 break;
2242 }
Arun Kumar K.R5111ff02012-12-19 18:10:46 -08002243 case HWC_BLIT:
2244 //Do nothing
2245 break;
Mathias Agopianda27af92012-09-13 18:17:13 -07002246 case HWC_FRAMEBUFFER_TARGET: {
2247 // this should not happen as the iterator shouldn't
2248 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08002249 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07002250 break;
2251 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07002252 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002253 }
2254 layer->setAcquireFence(hw, *cur);
2255 }
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302256
2257#ifdef QCOM_BSP
2258 // call EndTile, if starTile has been called in this cycle.
2259 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302260 if(mCanUseGpuTileRender && !mUnionDirtyRect.isEmpty()) {
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302261 engine.endTileComposition(GL_PRESERVE);
2262 }
2263 }
2264#endif
Mathias Agopian85d751c2012-08-29 16:59:24 -07002265 } else {
2266 // we're not using h/w composer
2267 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002268 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002269 const Region clip(dirty.intersect(
2270 tr.transform(layer->visibleRegion)));
2271 if (!clip.isEmpty()) {
2272 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07002273 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002274 }
2275 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002276
2277 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002278 engine.disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07002279 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002280}
2281
Mathias Agopian3f844832013-08-07 21:24:32 -07002282void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07002283 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07002284 RenderEngine& engine(getRenderEngine());
2285 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002286}
2287
Mathias Agopianac9fa422013-02-11 16:40:36 -08002288void SurfaceFlinger::addClientLayer(const sp<Client>& client,
2289 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07002290 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08002291 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07002292{
Mathias Agopian96f08192010-06-02 23:28:45 -07002293 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08002294 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07002295
Mathias Agopian96f08192010-06-02 23:28:45 -07002296 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002297 Mutex::Autolock _l(mStateLock);
2298 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07002299 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07002300}
2301
Mathias Agopian3f844832013-08-07 21:24:32 -07002302status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002303 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08002304 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002305 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07002306 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002307 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07002308 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002309 return NO_ERROR;
2310 }
Mathias Agopian3d579642009-06-04 18:46:21 -07002311 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002312}
2313
Dan Stozac7014012014-02-14 15:03:43 -08002314uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07002315 return android_atomic_release_load(&mTransactionFlags);
2316}
2317
Mathias Agopian3f844832013-08-07 21:24:32 -07002318uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002319 return android_atomic_and(~flags, &mTransactionFlags) & flags;
2320}
2321
Mathias Agopian3f844832013-08-07 21:24:32 -07002322uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002323 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
2324 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002325 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002326 }
2327 return old;
2328}
2329
Mathias Agopian8b33f032012-07-24 20:43:54 -07002330void SurfaceFlinger::setTransactionState(
2331 const Vector<ComposerState>& state,
2332 const Vector<DisplayState>& displays,
2333 uint32_t flags)
2334{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002335 ATRACE_CALL();
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08002336 size_t count = displays.size();
2337#ifdef QCOM_BSP
Ramkumar Radhakrishnanced0b062014-08-11 15:31:07 -07002338 // Delay the display projection transaction by 50ms only when the disable
2339 // external rotation animation feature is enabled
2340 if(mDisableExtAnimation) {
2341 for (size_t i=0 ; i<count ; i++) {
2342 const DisplayState& s(displays[i]);
Baldev Kumar Sahub4ff4392014-09-12 10:58:11 +05302343 if((mDisplays.indexOfKey(s.token) >= 0) && (s.token !=
2344 mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY])) {
Ramkumar Radhakrishnanced0b062014-08-11 15:31:07 -07002345 const uint32_t what = s.what;
2346 // Invalidate and Delay the binder thread by 50 ms on
2347 // eDisplayProjectionChanged to trigger a draw cycle so that
2348 // it can fix one incorrect frame on the External, when we
2349 // disable external animation
2350 if (what & DisplayState::eDisplayProjectionChanged) {
2351 invalidateHwcGeometry();
2352 repaintEverything();
2353 usleep(50000);
2354 }
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08002355 }
2356 }
2357 }
2358#endif
Mathias Agopian698c0872011-06-28 19:09:31 -07002359 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07002360 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002361
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002362 if (flags & eAnimation) {
2363 // For window updates that are part of an animation we must wait for
2364 // previous animation "frames" to be handled.
2365 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002366 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002367 if (CC_UNLIKELY(err != NO_ERROR)) {
2368 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002369 // caller after a few seconds.
2370 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
2371 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002372 mAnimTransactionPending = false;
2373 break;
2374 }
2375 }
2376 }
2377
Mathias Agopiane57f2922012-08-09 16:29:12 -07002378 for (size_t i=0 ; i<count ; i++) {
2379 const DisplayState& s(displays[i]);
2380 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002381 }
2382
Mathias Agopiane57f2922012-08-09 16:29:12 -07002383 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002384 for (size_t i=0 ; i<count ; i++) {
2385 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002386 // Here we need to check that the interface we're given is indeed
2387 // one of our own. A malicious client could give us a NULL
2388 // IInterface, or one of its own or even one of our own but a
2389 // different type. All these situations would cause us to crash.
2390 //
2391 // NOTE: it would be better to use RTTI as we could directly check
2392 // that we have a Client*. however, RTTI is disabled in Android.
2393 if (s.client != NULL) {
2394 sp<IBinder> binder = s.client->asBinder();
2395 if (binder != NULL) {
2396 String16 desc(binder->getInterfaceDescriptor());
2397 if (desc == ISurfaceComposerClient::descriptor) {
2398 sp<Client> client( static_cast<Client *>(s.client.get()) );
2399 transactionFlags |= setClientStateLocked(client, s.state);
2400 }
2401 }
2402 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002403 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002404
Mathias Agopian386aa982011-11-07 21:58:03 -08002405 if (transactionFlags) {
2406 // this triggers the transaction
2407 setTransactionFlags(transactionFlags);
2408
2409 // if this is a synchronous transaction, wait for it to take effect
2410 // before returning.
2411 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002412 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002413 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002414 if (flags & eAnimation) {
2415 mAnimTransactionPending = true;
2416 }
2417 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002418 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2419 if (CC_UNLIKELY(err != NO_ERROR)) {
2420 // just in case something goes wrong in SF, return to the
2421 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002422 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2423 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002424 break;
2425 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002426 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002427 }
2428}
2429
Mathias Agopiane57f2922012-08-09 16:29:12 -07002430uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2431{
Jesse Hall9a143922012-10-04 16:29:19 -07002432 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2433 if (dpyIdx < 0)
2434 return 0;
2435
Mathias Agopiane57f2922012-08-09 16:29:12 -07002436 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002437 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002438 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002439 const uint32_t what = s.what;
2440 if (what & DisplayState::eSurfaceChanged) {
2441 if (disp.surface->asBinder() != s.surface->asBinder()) {
2442 disp.surface = s.surface;
2443 flags |= eDisplayTransactionNeeded;
2444 }
2445 }
2446 if (what & DisplayState::eLayerStackChanged) {
2447 if (disp.layerStack != s.layerStack) {
2448 disp.layerStack = s.layerStack;
2449 flags |= eDisplayTransactionNeeded;
2450 }
2451 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002452 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002453 if (disp.orientation != s.orientation) {
2454 disp.orientation = s.orientation;
2455 flags |= eDisplayTransactionNeeded;
2456 }
2457 if (disp.frame != s.frame) {
2458 disp.frame = s.frame;
2459 flags |= eDisplayTransactionNeeded;
2460 }
2461 if (disp.viewport != s.viewport) {
2462 disp.viewport = s.viewport;
2463 flags |= eDisplayTransactionNeeded;
2464 }
2465 }
Michael Lentine47e45402014-07-18 15:34:25 -07002466 if (what & DisplayState::eDisplaySizeChanged) {
2467 if (disp.width != s.width) {
2468 disp.width = s.width;
2469 flags |= eDisplayTransactionNeeded;
2470 }
2471 if (disp.height != s.height) {
2472 disp.height = s.height;
2473 flags |= eDisplayTransactionNeeded;
2474 }
2475 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002476 }
2477 return flags;
2478}
2479
2480uint32_t SurfaceFlinger::setClientStateLocked(
2481 const sp<Client>& client,
2482 const layer_state_t& s)
2483{
2484 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002485 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002486 if (layer != 0) {
2487 const uint32_t what = s.what;
2488 if (what & layer_state_t::ePositionChanged) {
2489 if (layer->setPosition(s.x, s.y))
2490 flags |= eTraversalNeeded;
2491 }
2492 if (what & layer_state_t::eLayerChanged) {
2493 // NOTE: index needs to be calculated before we update the state
2494 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2495 if (layer->setLayer(s.z)) {
2496 mCurrentState.layersSortedByZ.removeAt(idx);
2497 mCurrentState.layersSortedByZ.add(layer);
2498 // we need traversal (state changed)
2499 // AND transaction (list changed)
2500 flags |= eTransactionNeeded|eTraversalNeeded;
2501 }
2502 }
2503 if (what & layer_state_t::eSizeChanged) {
2504 if (layer->setSize(s.w, s.h)) {
2505 flags |= eTraversalNeeded;
2506 }
2507 }
2508 if (what & layer_state_t::eAlphaChanged) {
2509 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2510 flags |= eTraversalNeeded;
2511 }
2512 if (what & layer_state_t::eMatrixChanged) {
2513 if (layer->setMatrix(s.matrix))
2514 flags |= eTraversalNeeded;
2515 }
2516 if (what & layer_state_t::eTransparentRegionChanged) {
2517 if (layer->setTransparentRegionHint(s.transparentRegion))
2518 flags |= eTraversalNeeded;
2519 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08002520 if ((what & layer_state_t::eVisibilityChanged) ||
2521 (what & layer_state_t::eOpacityChanged)) {
2522 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07002523 if (layer->setFlags(s.flags, s.mask))
2524 flags |= eTraversalNeeded;
2525 }
2526 if (what & layer_state_t::eCropChanged) {
2527 if (layer->setCrop(s.crop))
2528 flags |= eTraversalNeeded;
2529 }
2530 if (what & layer_state_t::eLayerStackChanged) {
2531 // NOTE: index needs to be calculated before we update the state
2532 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2533 if (layer->setLayerStack(s.layerStack)) {
2534 mCurrentState.layersSortedByZ.removeAt(idx);
2535 mCurrentState.layersSortedByZ.add(layer);
2536 // we need traversal (state changed)
2537 // AND transaction (list changed)
2538 flags |= eTransactionNeeded|eTraversalNeeded;
2539 }
2540 }
2541 }
2542 return flags;
2543}
2544
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002545status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002546 const String8& name,
2547 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002548 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2549 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002550{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002551 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002552 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002553 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002554 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002555 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002556 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002557
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002558 status_t result = NO_ERROR;
2559
2560 sp<Layer> layer;
2561
Mathias Agopian3165cc22012-08-08 19:42:09 -07002562 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2563 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002564 result = createNormalLayer(client,
2565 name, w, h, flags, format,
2566 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002567 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002568 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002569 result = createDimLayer(client,
2570 name, w, h, flags,
2571 handle, gbp, &layer);
2572 break;
2573 default:
2574 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002575 break;
2576 }
2577
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002578 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002579 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002580 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002581 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002582 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002583}
2584
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002585status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2586 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2587 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002588{
2589 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002590 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002591 case PIXEL_FORMAT_TRANSPARENT:
2592 case PIXEL_FORMAT_TRANSLUCENT:
2593 format = PIXEL_FORMAT_RGBA_8888;
2594 break;
2595 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002596 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002597 break;
2598 }
2599
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002600 *outLayer = new Layer(this, client, name, w, h, flags);
2601 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2602 if (err == NO_ERROR) {
2603 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002604 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002605 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002606
2607 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2608 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002609}
2610
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002611status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2612 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2613 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002614{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002615 *outLayer = new LayerDim(this, client, name, w, h, flags);
2616 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002617 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002618 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002619}
2620
Mathias Agopianac9fa422013-02-11 16:40:36 -08002621status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002622{
Mathias Agopian67106042013-03-14 19:18:13 -07002623 // called by the window manager when it wants to remove a Layer
2624 status_t err = NO_ERROR;
2625 sp<Layer> l(client->getLayerUser(handle));
2626 if (l != NULL) {
2627 err = removeLayer(l);
2628 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2629 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002630 }
2631 return err;
2632}
2633
Mathias Agopian13127d82013-03-05 17:47:11 -08002634status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002635{
Mathias Agopian67106042013-03-14 19:18:13 -07002636 // called by ~LayerCleaner() when all references to the IBinder (handle)
2637 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002638 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002639 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002640 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002641 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002642 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002643 "error removing layer=%p (%s)", l.get(), strerror(-err));
2644 }
2645 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002646}
2647
Mathias Agopianb60314a2012-04-10 22:09:54 -07002648// ---------------------------------------------------------------------------
2649
Andy McFadden13a082e2012-08-24 10:16:42 -07002650void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002651 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002652 Vector<ComposerState> state;
2653 Vector<DisplayState> displays;
2654 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002655 d.what = DisplayState::eDisplayProjectionChanged |
2656 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002657 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002658 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002659 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002660 d.frame.makeInvalid();
2661 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002662 d.width = 0;
2663 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002664 displays.add(d);
2665 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002666 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002667
2668 const nsecs_t period =
2669 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2670 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002671}
2672
2673void SurfaceFlinger::initializeDisplays() {
2674 class MessageScreenInitialized : public MessageBase {
2675 SurfaceFlinger* flinger;
2676 public:
2677 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2678 virtual bool handler() {
2679 flinger->onInitializeDisplays();
2680 return true;
2681 }
2682 };
2683 sp<MessageBase> msg = new MessageScreenInitialized(this);
2684 postMessageAsync(msg); // we may be called from main thread, use async message
2685}
2686
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002687void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2688 int mode) {
2689 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2690 this);
2691 int32_t type = hw->getDisplayType();
2692 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002693
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002694 if (mode == currentMode) {
2695 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002696 return;
2697 }
2698
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002699 hw->setPowerMode(mode);
2700 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2701 ALOGW("Trying to set power mode for virtual display");
2702 return;
2703 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002704
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002705 if (currentMode == HWC_POWER_MODE_OFF) {
2706 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002707 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2708 // FIXME: eventthread only knows about the main display right now
2709 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002710 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002711 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002712
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002713 mVisibleRegionsDirty = true;
2714 repaintEverything();
2715 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002716 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002717 disableHardwareVsync(true); // also cancels any in-progress resync
2718
Mathias Agopiancde87a32012-09-13 14:09:01 -07002719 // FIXME: eventthread only knows about the main display right now
2720 mEventThread->onScreenReleased();
2721 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002722
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002723 getHwComposer().setPowerMode(type, mode);
2724 mVisibleRegionsDirty = true;
2725 // from this point on, SF will stop drawing on this display
2726 } else {
2727 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002728 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002729}
2730
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002731void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2732 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002733 SurfaceFlinger& mFlinger;
2734 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002735 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002736 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002737 MessageSetPowerMode(SurfaceFlinger& flinger,
2738 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2739 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002740 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002741 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002742 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002743 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002744 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002745 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002746 ALOGW("Attempt to set power mode = %d for virtual display",
2747 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002748 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002749 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002750 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002751 return true;
2752 }
2753 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002754 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002755 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002756}
2757
2758// ---------------------------------------------------------------------------
2759
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002760status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2761{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002762 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002763
Mathias Agopianbd115332013-04-18 16:41:04 -07002764 IPCThreadState* ipc = IPCThreadState::self();
2765 const int pid = ipc->getCallingPid();
2766 const int uid = ipc->getCallingUid();
2767 if ((uid != AID_SHELL) &&
2768 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002769 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002770 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002771 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002772 // Try to get the main lock, but don't insist if we can't
2773 // (this would indicate SF is stuck, but we want to be able to
2774 // print something in dumpsys).
2775 int retry = 3;
2776 while (mStateLock.tryLock()<0 && --retry>=0) {
2777 usleep(1000000);
2778 }
2779 const bool locked(retry >= 0);
2780 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002781 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002782 "SurfaceFlinger appears to be unresponsive, "
2783 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002784 }
2785
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002786 bool dumpAll = true;
2787 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002788 size_t numArgs = args.size();
2789 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002790 if ((index < numArgs) &&
2791 (args[index] == String16("--list"))) {
2792 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002793 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002794 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002795 }
2796
2797 if ((index < numArgs) &&
2798 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002799 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002800 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002801 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002802 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002803
2804 if ((index < numArgs) &&
2805 (args[index] == String16("--latency-clear"))) {
2806 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002807 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002808 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002809 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002810
2811 if ((index < numArgs) &&
2812 (args[index] == String16("--dispsync"))) {
2813 index++;
2814 mPrimaryDispSync.dump(result);
2815 dumpAll = false;
2816 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002817 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002818
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002819 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002820 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002821 }
2822
Mathias Agopian9795c422009-08-26 16:36:26 -07002823 if (locked) {
2824 mStateLock.unlock();
2825 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002826 }
2827 write(fd, result.string(), result.size());
2828 return NO_ERROR;
2829}
2830
Dan Stozac7014012014-02-14 15:03:43 -08002831void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2832 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002833{
2834 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2835 const size_t count = currentLayers.size();
2836 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002837 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002838 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002839 }
2840}
2841
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002842void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002843 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002844{
2845 String8 name;
2846 if (index < args.size()) {
2847 name = String8(args[index]);
2848 index++;
2849 }
2850
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002851 const nsecs_t period =
2852 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002853 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002854
2855 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002856 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002857 } else {
2858 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2859 const size_t count = currentLayers.size();
2860 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002861 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002862 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002863 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002864 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002865 }
2866 }
2867}
2868
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002869void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002870 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002871{
2872 String8 name;
2873 if (index < args.size()) {
2874 name = String8(args[index]);
2875 index++;
2876 }
2877
2878 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2879 const size_t count = currentLayers.size();
2880 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002881 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002882 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002883 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002884 }
2885 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002886
Svetoslavd85084b2014-03-20 10:28:31 -07002887 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002888}
2889
Jamie Gennis6547ff42013-07-16 20:12:42 -07002890// This should only be called from the main thread. Otherwise it would need
2891// the lock and should use mCurrentState rather than mDrawingState.
2892void SurfaceFlinger::logFrameStats() {
2893 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2894 const size_t count = drawingLayers.size();
2895 for (size_t i=0 ; i<count ; i++) {
2896 const sp<Layer>& layer(drawingLayers[i]);
2897 layer->logFrameStats();
2898 }
2899
2900 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2901}
2902
Andy McFadden4803b742012-09-24 19:07:20 -07002903/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2904{
2905 static const char* config =
2906 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002907#ifdef HAS_CONTEXT_PRIORITY
2908 " HAS_CONTEXT_PRIORITY"
2909#endif
2910#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2911 " NEVER_DEFAULT_TO_ASYNC_MODE"
2912#endif
2913#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2914 " TARGET_DISABLE_TRIPLE_BUFFERING"
2915#endif
2916 "]";
2917 result.append(config);
2918}
2919
Mathias Agopian74d211a2013-04-22 16:55:35 +02002920void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2921 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002922{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002923 bool colorize = false;
2924 if (index < args.size()
2925 && (args[index] == String16("--color"))) {
2926 colorize = true;
2927 index++;
2928 }
2929
2930 Colorizer colorizer(colorize);
2931
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002932 // figure out if we're stuck somewhere
2933 const nsecs_t now = systemTime();
2934 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2935 const nsecs_t inTransaction(mDebugInTransaction);
2936 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2937 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2938
2939 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002940 * Dump library configuration.
2941 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002942
2943 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002944 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002945 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002946 appendSfConfigString(result);
2947 appendUiConfigString(result);
2948 appendGuiConfigString(result);
2949 result.append("\n");
2950
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002951 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002952 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002953 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002954 result.append(SyncFeatures::getInstance().toString());
2955 result.append("\n");
2956
Andy McFadden41d67d72014-04-25 16:58:34 -07002957 colorizer.bold(result);
2958 result.append("DispSync configuration: ");
2959 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002960 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2961 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002962 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2963 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2964 result.append("\n");
2965
Andy McFadden4803b742012-09-24 19:07:20 -07002966 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002967 * Dump the visible layer list
2968 */
2969 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2970 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002971 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002972 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002973 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002974 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002975 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002976 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002977 }
2978
2979 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002980 * Dump Display state
2981 */
2982
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002983 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002984 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002985 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002986 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2987 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002988 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002989 }
2990
2991 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002992 * Dump SurfaceFlinger global state
2993 */
2994
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002995 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002996 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002997 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002998
Mathias Agopian888c8222012-08-04 21:10:38 -07002999 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07003000 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07003001
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003002 colorizer.bold(result);
3003 result.appendFormat("EGL implementation : %s\n",
3004 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
3005 colorizer.reset(result);
3006 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07003007 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07003008
Mathias Agopian875d8e12013-06-07 15:35:48 -07003009 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003010
Mathias Agopian42977342012-08-05 00:40:46 -07003011 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07003012 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
3013 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02003014 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003015 " last eglSwapBuffers() time: %f us\n"
3016 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08003017 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003018 " refresh-rate : %f fps\n"
3019 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07003020 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07003021 " gpu_to_cpu_unsupported : %d\n"
3022 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003023 mLastSwapBufferTime/1000.0,
3024 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08003025 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07003026 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
3027 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07003028 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07003029 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003030
Mathias Agopian74d211a2013-04-22 16:55:35 +02003031 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003032 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003033
Mathias Agopian74d211a2013-04-22 16:55:35 +02003034 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003035 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003036
3037 /*
3038 * VSYNC state
3039 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02003040 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003041
3042 /*
3043 * Dump HWComposer state
3044 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003045 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02003046 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003047 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02003048 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003049 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07003050 (mDebugDisableHWC || mDebugRegion || mDaltonize
3051 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02003052 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003053
3054 /*
3055 * Dump gralloc state
3056 */
3057 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
3058 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003059}
3060
Mathias Agopian13127d82013-03-05 17:47:11 -08003061const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07003062SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07003063 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07003064 wp<IBinder> dpy;
3065 for (size_t i=0 ; i<mDisplays.size() ; i++) {
3066 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
3067 dpy = mDisplays.keyAt(i);
3068 break;
3069 }
3070 }
3071 if (dpy == NULL) {
3072 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
3073 // Just use the primary display so we have something to return
3074 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
3075 }
3076 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07003077}
3078
Keun young Park63f165f2012-08-31 10:53:36 -07003079bool SurfaceFlinger::startDdmConnection()
3080{
3081 void* libddmconnection_dso =
3082 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
3083 if (!libddmconnection_dso) {
3084 return false;
3085 }
3086 void (*DdmConnection_start)(const char* name);
3087 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07003088 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07003089 if (!DdmConnection_start) {
3090 dlclose(libddmconnection_dso);
3091 return false;
3092 }
3093 (*DdmConnection_start)(getServiceName());
3094 return true;
3095}
3096
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003097status_t SurfaceFlinger::onTransact(
3098 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3099{
3100 switch (code) {
3101 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07003102 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07003103 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003104 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07003105 case CLEAR_ANIMATION_FRAME_STATS:
3106 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07003107 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003108 {
3109 // codes that require permission check
3110 IPCThreadState* ipc = IPCThreadState::self();
3111 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07003112 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07003113 if ((uid != AID_GRAPHICS) &&
3114 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00003115 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07003116 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
3117 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003118 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003119 break;
3120 }
3121 case CAPTURE_SCREEN:
3122 {
3123 // codes that require permission check
3124 IPCThreadState* ipc = IPCThreadState::self();
3125 const int pid = ipc->getCallingPid();
3126 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07003127 if ((uid != AID_GRAPHICS) &&
3128 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00003129 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003130 "can't read framebuffer pid=%d, uid=%d", pid, uid);
3131 return PERMISSION_DENIED;
3132 }
3133 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003134 }
3135 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003136
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003137 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
3138 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07003139 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08003140 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07003141 IPCThreadState* ipc = IPCThreadState::self();
3142 const int pid = ipc->getCallingPid();
3143 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00003144 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07003145 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003146 return PERMISSION_DENIED;
3147 }
3148 int n;
3149 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07003150 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07003151 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003152 return NO_ERROR;
3153 case 1002: // SHOW_UPDATES
3154 n = data.readInt32();
3155 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07003156 invalidateHwcGeometry();
3157 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003158 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003159 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07003160 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003161 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003162 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003163 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07003164 setTransactionFlags(
3165 eTransactionNeeded|
3166 eDisplayTransactionNeeded|
3167 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003168 return NO_ERROR;
3169 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08003170 case 1006:{ // send empty update
3171 signalRefresh();
3172 return NO_ERROR;
3173 }
Mathias Agopian53331da2011-08-22 21:44:41 -07003174 case 1008: // toggle use of hw composer
3175 n = data.readInt32();
3176 mDebugDisableHWC = n ? 1 : 0;
3177 invalidateHwcGeometry();
3178 repaintEverything();
3179 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07003180 case 1009: // toggle use of transform hint
3181 n = data.readInt32();
3182 mDebugDisableTransformHint = n ? 1 : 0;
3183 invalidateHwcGeometry();
3184 repaintEverything();
3185 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003186 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07003187 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003188 reply->writeInt32(0);
3189 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07003190 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08003191 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003192 return NO_ERROR;
3193 case 1013: {
3194 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07003195 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
3196 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07003197 return NO_ERROR;
3198 }
3199 case 1014: {
3200 // daltonize
3201 n = data.readInt32();
3202 switch (n % 10) {
3203 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
3204 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
3205 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
3206 }
3207 if (n >= 10) {
3208 mDaltonizer.setMode(Daltonizer::correction);
3209 } else {
3210 mDaltonizer.setMode(Daltonizer::simulation);
3211 }
3212 mDaltonize = n > 0;
3213 invalidateHwcGeometry();
3214 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07003215 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003216 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003217 case 1015: {
3218 // apply a color matrix
3219 n = data.readInt32();
3220 mHasColorMatrix = n ? 1 : 0;
3221 if (n) {
3222 // color matrix is sent as mat3 matrix followed by vec3
3223 // offset, then packed into a mat4 where the last row is
3224 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07003225 for (size_t i = 0 ; i < 4; i++) {
3226 for (size_t j = 0; j < 4; j++) {
3227 mColorMatrix[i][j] = data.readFloat();
3228 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003229 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003230 } else {
3231 mColorMatrix = mat4();
3232 }
3233 invalidateHwcGeometry();
3234 repaintEverything();
3235 return NO_ERROR;
3236 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003237 // This is an experimental interface
3238 // Needs to be shifted to proper binder interface when we productize
3239 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07003240 n = data.readInt32();
3241 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003242 return NO_ERROR;
3243 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003244 }
3245 }
3246 return err;
3247}
3248
Mathias Agopian53331da2011-08-22 21:44:41 -07003249void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07003250 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08003251 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07003252}
3253
Mathias Agopian59119e62010-10-11 12:37:43 -07003254// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003255// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07003256// ---------------------------------------------------------------------------
3257
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003258/* The code below is here to handle b/8734824
3259 *
3260 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07003261 * from the surfaceflinger thread to the calling binder thread, where they
3262 * are executed. This allows the calling thread in the calling process to be
3263 * reused and not depend on having "enough" binder threads to handle the
3264 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003265 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003266class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07003267 /* Parts of GraphicProducerWrapper are run on two different threads,
3268 * communicating by sending messages via Looper but also by shared member
3269 * data. Coherence maintenance is subtle and in places implicit (ugh).
3270 *
3271 * Don't rely on Looper's sendMessage/handleMessage providing
3272 * release/acquire semantics for any data not actually in the Message.
3273 * Data going from surfaceflinger to binder threads needs to be
3274 * synchronized explicitly.
3275 *
3276 * Barrier open/wait do provide release/acquire semantics. This provides
3277 * implicit synchronization for data coming back from binder to
3278 * surfaceflinger threads.
3279 */
3280
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003281 sp<IGraphicBufferProducer> impl;
3282 sp<Looper> looper;
3283 status_t result;
3284 bool exitPending;
3285 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07003286 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003287 uint32_t code;
3288 Parcel const* data;
3289 Parcel* reply;
3290
3291 enum {
3292 MSG_API_CALL,
3293 MSG_EXIT
3294 };
3295
3296 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003297 * Called on surfaceflinger thread. This is called by our "fake"
3298 * BpGraphicBufferProducer. We package the data and reply Parcel and
3299 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003300 */
3301 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08003302 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003303 this->code = code;
3304 this->data = &data;
3305 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003306 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07003307 // if we've exited, we run the message synchronously right here.
3308 // note (JH): as far as I can tell from looking at the code, this
3309 // never actually happens. if it does, i'm not sure if it happens
3310 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003311 handleMessage(Message(MSG_API_CALL));
3312 } else {
3313 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07003314 // Prevent stores to this->{code, data, reply} from being
3315 // reordered later than the construction of Message.
3316 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003317 looper->sendMessage(this, Message(MSG_API_CALL));
3318 barrier.wait();
3319 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08003320 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003321 }
3322
3323 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003324 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003325 * call the real BpGraphicBufferProducer.
3326 */
3327 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07003328 int what = message.what;
3329 // Prevent reads below from happening before the read from Message
3330 atomic_thread_fence(memory_order_acquire);
3331 if (what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08003332 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003333 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07003334 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003335 exitRequested = true;
3336 }
3337 }
3338
3339public:
Jesse Hallb154c422014-07-13 12:47:02 -07003340 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
3341 : impl(impl),
3342 looper(new Looper(true)),
3343 exitPending(false),
3344 exitRequested(false)
3345 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003346
Jesse Hallb154c422014-07-13 12:47:02 -07003347 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003348 status_t waitForResponse() {
3349 do {
3350 looper->pollOnce(-1);
3351 } while (!exitRequested);
3352 return result;
3353 }
3354
Jesse Hallb154c422014-07-13 12:47:02 -07003355 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003356 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07003357 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003358 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07003359 // Ensure this->result is visible to the binder thread before it
3360 // handles the message.
3361 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003362 looper->sendMessage(this, Message(MSG_EXIT));
3363 }
3364};
3365
3366
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003367status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
3368 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003369 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003370 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003371 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003372
3373 if (CC_UNLIKELY(display == 0))
3374 return BAD_VALUE;
3375
3376 if (CC_UNLIKELY(producer == 0))
3377 return BAD_VALUE;
3378
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003379 // if we have secure windows on this display, never allow the screen capture
3380 // unless the producer interface is local (i.e.: we can take a screenshot for
3381 // ourselves).
3382 if (!producer->asBinder()->localBinder()) {
3383 Mutex::Autolock _l(mStateLock);
3384 sp<const DisplayDevice> hw(getDisplayDevice(display));
3385 if (hw->getSecureLayerVisible()) {
3386 ALOGW("FB is protected: PERMISSION_DENIED");
3387 return PERMISSION_DENIED;
3388 }
3389 }
3390
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003391 // Convert to surfaceflinger's internal rotation type.
3392 Transform::orientation_flags rotationFlags;
3393 switch (rotation) {
3394 case ISurfaceComposer::eRotateNone:
3395 rotationFlags = Transform::ROT_0;
3396 break;
3397 case ISurfaceComposer::eRotate90:
3398 rotationFlags = Transform::ROT_90;
3399 break;
3400 case ISurfaceComposer::eRotate180:
3401 rotationFlags = Transform::ROT_180;
3402 break;
3403 case ISurfaceComposer::eRotate270:
3404 rotationFlags = Transform::ROT_270;
3405 break;
3406 default:
3407 rotationFlags = Transform::ROT_0;
3408 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3409 break;
3410 }
3411
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003412 class MessageCaptureScreen : public MessageBase {
3413 SurfaceFlinger* flinger;
3414 sp<IBinder> display;
3415 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003416 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003417 uint32_t reqWidth, reqHeight;
3418 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003419 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003420 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003421 status_t result;
3422 public:
3423 MessageCaptureScreen(SurfaceFlinger* flinger,
3424 const sp<IBinder>& display,
3425 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003426 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003427 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003428 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003429 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003430 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003431 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003432 useIdentityTransform(useIdentityTransform),
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003433 rotation(rotation),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003434 result(PERMISSION_DENIED)
3435 {
3436 }
3437 status_t getResult() const {
3438 return result;
3439 }
3440 virtual bool handler() {
3441 Mutex::Autolock _l(flinger->mStateLock);
3442 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003443 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003444 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003445 useIdentityTransform, rotation);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003446 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003447 return true;
3448 }
3449 };
3450
Mathias Agopian9eb1f052013-04-10 16:27:17 -07003451 // make sure to process transactions before screenshots -- a transaction
3452 // might already be pending but scheduled for VSYNC; this guarantees we
3453 // will handle it before the screenshot. When VSYNC finally arrives
3454 // the scheduled transaction will be a no-op. If no transactions are
3455 // scheduled at this time, this will end-up being a no-op as well.
3456 mEventQueue.invalidateTransactionNow();
3457
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003458 // this creates a "fake" BBinder which will serve as a "fake" remote
3459 // binder to receive the marshaled calls and forward them to the
3460 // real remote (a BpGraphicBufferProducer)
3461 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3462
3463 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3464 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003465 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003466 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003467 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003468 useIdentityTransform, rotationFlags);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003469
3470 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003471 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003472 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003473 }
3474 return res;
3475}
3476
Mathias Agopian180f10d2013-04-10 22:55:41 -07003477
3478void SurfaceFlinger::renderScreenImplLocked(
3479 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003480 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003481 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003482 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003483{
3484 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003485 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003486
3487 // get screen geometry
3488 const uint32_t hw_w = hw->getWidth();
3489 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003490 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
3491
Dan Stozac1879002014-05-22 15:59:05 -07003492 // if a default or invalid sourceCrop is passed in, set reasonable values
3493 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3494 !sourceCrop.isValid()) {
3495 sourceCrop.setLeftTop(Point(0, 0));
3496 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3497 }
3498
3499 // ensure that sourceCrop is inside screen
3500 if (sourceCrop.left < 0) {
3501 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3502 }
Dan Stozabe31f442014-06-11 11:20:54 -07003503 if (sourceCrop.right > hw_w) {
3504 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003505 }
3506 if (sourceCrop.top < 0) {
3507 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3508 }
Dan Stozabe31f442014-06-11 11:20:54 -07003509 if (sourceCrop.bottom > hw_h) {
3510 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003511 }
3512
Mathias Agopian180f10d2013-04-10 22:55:41 -07003513 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003514 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003515
3516 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003517 engine.setViewportAndProjection(
3518 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003519 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003520
3521 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003522 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003523
3524 const LayerVector& layers( mDrawingState.layersSortedByZ );
3525 const size_t count = layers.size();
3526 for (size_t i=0 ; i<count ; ++i) {
3527 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003528 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003529 if (state.layerStack == hw->getLayerStack()) {
3530 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07003531#ifdef QCOM_BSP
3532 // dont render the secure Display Layer
3533 if(layer->isSecureDisplay()) {
3534 continue;
3535 }
3536#endif
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07003537#ifdef QCOM_BSP
3538 int dispType = hw->getDisplayType();
3539 // Dont let ext_only and extended_mode to be captured
3540 // If not, we would see incorrect image during rotatoin
3541 // on primary
3542 if (layer->isVisible() &&
3543 not (!dispType && (layer->isExtOnly() ||
3544 (isExtendedMode() && layer->isYuvLayer())))) {
3545#else
Mathias Agopian180f10d2013-04-10 22:55:41 -07003546 if (layer->isVisible()) {
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07003547#endif
Mathias Agopian180f10d2013-04-10 22:55:41 -07003548 if (filtering) layer->setFiltering(true);
Ramakant Singhf9af0e32014-01-07 14:17:52 +05303549 if(!layer->isProtected())
3550 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003551 if (filtering) layer->setFiltering(false);
3552 }
3553 }
3554 }
3555 }
3556
3557 // compositionComplete is needed for older driver
3558 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003559 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003560}
3561
3562
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003563status_t SurfaceFlinger::captureScreenImplLocked(
3564 const sp<const DisplayDevice>& hw,
3565 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003566 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003567 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003568 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003569{
3570 ATRACE_CALL();
3571
Mathias Agopian180f10d2013-04-10 22:55:41 -07003572 // get screen geometry
3573 const uint32_t hw_w = hw->getWidth();
3574 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003575
Mathias Agopian180f10d2013-04-10 22:55:41 -07003576 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3577 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3578 reqWidth, reqHeight, hw_w, hw_h);
3579 return BAD_VALUE;
3580 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003581
Mathias Agopian180f10d2013-04-10 22:55:41 -07003582 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3583 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3584
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003585 // create a surface (because we're a producer, and we need to
3586 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003587 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003588 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003589
3590 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003591 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003592 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3593 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003594
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003595 int err = 0;
3596 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003597 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003598 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3599 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003600
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003601 if (err == NO_ERROR) {
3602 ANativeWindowBuffer* buffer;
3603 /* TODO: Once we have the sync framework everywhere this can use
3604 * server-side waits on the fence that dequeueBuffer returns.
3605 */
3606 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3607 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003608 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003609 // create an EGLImage from the buffer so we can later
3610 // turn it into a texture
3611 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3612 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3613 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003614 // this binds the given EGLImage as a framebuffer for the
3615 // duration of this scope.
3616 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3617 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003618 // this will in fact render into our dequeued buffer
3619 // via an FBO, which means we didn't have to create
3620 // an EGLSurface and therefore we're not
3621 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003622 renderScreenImplLocked(
3623 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3624 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003625
Riley Andrews86639902014-08-15 12:27:24 -07003626 // Attempt to create a sync khr object that can produce a sync point. If that
3627 // isn't available, create a non-dupable sync object in the fallback path and
3628 // wait on it directly.
3629 EGLSyncKHR sync;
3630 if (!DEBUG_SCREENSHOTS) {
3631 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Andy McFadden2d8d1202013-10-09 16:38:02 -07003632 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003633 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003634 }
Riley Andrews86639902014-08-15 12:27:24 -07003635 if (sync != EGL_NO_SYNC_KHR) {
3636 // get the sync fd
3637 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3638 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3639 ALOGW("captureScreen: failed to dup sync khr object");
3640 syncFd = -1;
3641 }
3642 eglDestroySyncKHR(mEGLDisplay, sync);
3643 } else {
3644 // fallback path
3645 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3646 if (sync != EGL_NO_SYNC_KHR) {
3647 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3648 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3649 EGLint eglErr = eglGetError();
3650 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3651 ALOGW("captureScreen: fence wait timed out");
3652 } else {
3653 ALOGW_IF(eglErr != EGL_SUCCESS,
3654 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3655 }
3656 eglDestroySyncKHR(mEGLDisplay, sync);
3657 } else {
3658 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3659 }
3660 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003661 if (DEBUG_SCREENSHOTS) {
3662 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3663 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3664 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3665 hw, minLayerZ, maxLayerZ);
3666 delete [] pixels;
3667 }
3668
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003669 } else {
3670 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3671 result = INVALID_OPERATION;
3672 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003673 // destroy our image
3674 eglDestroyImageKHR(mEGLDisplay, image);
3675 } else {
3676 result = BAD_VALUE;
3677 }
Jesse Hallafe2b1f2014-10-21 11:09:17 -07003678 // queueBuffer takes ownership of syncFd
Riley Andrews86639902014-08-15 12:27:24 -07003679 window->queueBuffer(window, buffer, syncFd);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003680 }
3681 } else {
3682 result = BAD_VALUE;
3683 }
3684 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3685 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003686
Mathias Agopian74c40c02010-09-29 13:02:36 -07003687 return result;
3688}
3689
Mathias Agopiand5556842013-09-19 17:08:37 -07003690void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3691 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003692 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003693 for (size_t y=0 ; y<h ; y++) {
3694 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3695 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003696 if (p[x] != 0xFF000000) return;
3697 }
3698 }
3699 ALOGE("*** we just took a black screenshot ***\n"
3700 "requested minz=%d, maxz=%d, layerStack=%d",
3701 minLayerZ, maxLayerZ, hw->getLayerStack());
3702 const LayerVector& layers( mDrawingState.layersSortedByZ );
3703 const size_t count = layers.size();
3704 for (size_t i=0 ; i<count ; ++i) {
3705 const sp<Layer>& layer(layers[i]);
3706 const Layer::State& state(layer->getDrawingState());
3707 const bool visible = (state.layerStack == hw->getLayerStack())
3708 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3709 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003710 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003711 visible ? '+' : '-',
3712 i, layer->getName().string(), state.layerStack, state.z,
3713 layer->isVisible(), state.flags, state.alpha);
3714 }
3715 }
3716}
3717
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003718// ---------------------------------------------------------------------------
3719
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003720SurfaceFlinger::LayerVector::LayerVector() {
3721}
3722
3723SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003724 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003725}
3726
3727int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3728 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003729{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003730 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003731 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3732 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003733
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003734 uint32_t ls = l->getCurrentState().layerStack;
3735 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003736 if (ls != rs)
3737 return ls - rs;
3738
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003739 uint32_t lz = l->getCurrentState().z;
3740 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003741 if (lz != rz)
3742 return lz - rz;
3743
3744 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003745}
3746
3747// ---------------------------------------------------------------------------
3748
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003749SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Michael Lentine47e45402014-07-18 15:34:25 -07003750 : type(DisplayDevice::DISPLAY_ID_INVALID), width(0), height(0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003751}
3752
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003753SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Michael Lentine47e45402014-07-18 15:34:25 -07003754 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0), width(0), height(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003755 viewport.makeInvalid();
3756 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003757}
3758
3759// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003760
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003761}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003762
3763
3764#if defined(__gl_h_)
3765#error "don't include gl/gl.h in this file"
3766#endif
3767
3768#if defined(__gl2_h_)
3769#error "don't include gl2/gl2.h in this file"
3770#endif