blob: eb09ea1a7c3188cba582d8396cfad83d6aedbcca [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) {
Tatenda Chipeperekwae9d5fa82014-10-29 16:47:19 -0700531 if ((configs == NULL) || (display.get() == NULL)) {
Dan Stoza7f7da322014-05-02 15:26:25 -0700532 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
Tatenda Chipeperekwab801c8a2014-09-22 16:57:24 -0700665 status_t status = getHwComposer().setActiveConfig(type, mode);
666 if (status == NO_ERROR) {
667 hw->setActiveConfig(mode);
668 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700669}
670
671status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
672 class MessageSetActiveConfig: public MessageBase {
673 SurfaceFlinger& mFlinger;
674 sp<IBinder> mDisplay;
675 int mMode;
676 public:
677 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
678 int mode) :
679 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
680 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700681 Vector<DisplayInfo> configs;
682 mFlinger.getDisplayConfigs(mDisplay, &configs);
683 if(mMode < 0 || mMode >= configs.size()) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700684 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700685 mMode, configs.size());
686 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700687 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
688 if (hw == NULL) {
689 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700690 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700691 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
692 ALOGW("Attempt to set active config = %d for virtual display",
693 mMode);
694 } else {
695 mFlinger.setActiveConfigInternal(hw, mMode);
696 }
697 return true;
698 }
699 };
700 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
701 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700702 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700703}
704
Svetoslavd85084b2014-03-20 10:28:31 -0700705status_t SurfaceFlinger::clearAnimationFrameStats() {
706 Mutex::Autolock _l(mStateLock);
707 mAnimFrameTracker.clearStats();
708 return NO_ERROR;
709}
710
711status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
712 Mutex::Autolock _l(mStateLock);
713 mAnimFrameTracker.getStats(outStats);
714 return NO_ERROR;
715}
716
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800717// ----------------------------------------------------------------------------
718
719sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800720 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700721}
722
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800723// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800724
725void SurfaceFlinger::waitForEvent() {
726 mEventQueue.waitMessage();
727}
728
729void SurfaceFlinger::signalTransaction() {
730 mEventQueue.invalidate();
731}
732
733void SurfaceFlinger::signalLayerUpdate() {
734 mEventQueue.invalidate();
735}
736
737void SurfaceFlinger::signalRefresh() {
738 mEventQueue.refresh();
739}
740
741status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800742 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800743 return mEventQueue.postMessage(msg, reltime);
744}
745
746status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800747 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800748 status_t res = mEventQueue.postMessage(msg, reltime);
749 if (res == NO_ERROR) {
750 msg->wait();
751 }
752 return res;
753}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800754
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700755void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700756 do {
757 waitForEvent();
758 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800759}
760
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700761void SurfaceFlinger::enableHardwareVsync() {
762 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700763 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700764 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700765 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
766 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700767 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700768 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700769}
770
Jesse Hall948fe0c2013-10-14 12:56:09 -0700771void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700772 Mutex::Autolock _l(mHWVsyncLock);
773
Jesse Hall948fe0c2013-10-14 12:56:09 -0700774 if (makeAvailable) {
775 mHWVsyncAvailable = true;
776 } else if (!mHWVsyncAvailable) {
777 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
778 return;
779 }
780
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700781 const nsecs_t period =
782 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
783
784 mPrimaryDispSync.reset();
785 mPrimaryDispSync.setPeriod(period);
786
787 if (!mPrimaryHWVsyncEnabled) {
788 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700789 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
790 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700791 mPrimaryHWVsyncEnabled = true;
792 }
793}
794
Jesse Hall948fe0c2013-10-14 12:56:09 -0700795void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700796 Mutex::Autolock _l(mHWVsyncLock);
797 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700798 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
799 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700800 mPrimaryDispSync.endResync();
801 mPrimaryHWVsyncEnabled = false;
802 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700803 if (makeUnavailable) {
804 mHWVsyncAvailable = false;
805 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700806}
807
808void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700809 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700810
Jamie Gennisd1700752013-10-14 12:22:52 -0700811 { // Scope for the lock
812 Mutex::Autolock _l(mHWVsyncLock);
813 if (type == 0 && mPrimaryHWVsyncEnabled) {
814 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700815 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700816 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700817
818 if (needsHwVsync) {
819 enableHardwareVsync();
820 } else {
821 disableHardwareVsync(false);
822 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700823}
824
825void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
826 if (mEventThread == NULL) {
827 // This is a temporary workaround for b/7145521. A non-null pointer
828 // does not mean EventThread has finished initializing, so this
829 // is not a correct fix.
830 ALOGW("WARNING: EventThread not started, ignoring hotplug");
831 return;
832 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700833
Jesse Hall9e663de2013-08-16 14:28:37 -0700834 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700835 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800836 if (connected) {
837 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700838 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800839 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
840 mBuiltinDisplays[type].clear();
Arun Kumar K.R766d5f62013-06-21 19:36:02 -0700841#ifdef QCOM_BSP
842 // if extended_mode is set, and set mVisibleRegionsDirty
843 // as we need to rebuildLayerStack
844 if(isExtendedMode()) {
845 mVisibleRegionsDirty = true;
846 }
847#endif
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700848 }
849 setTransactionFlags(eDisplayTransactionNeeded);
850
Andy McFadden9e9689c2012-10-10 18:17:51 -0700851 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700852 }
Mathias Agopian86303202012-07-24 22:46:10 -0700853}
854
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700855void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700856 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700857 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700858}
859
Mathias Agopian4fec8732012-06-29 14:12:52 -0700860void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800861 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800862 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700863 case MessageQueue::TRANSACTION:
864 handleMessageTransaction();
865 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700866 case MessageQueue::INVALIDATE:
867 handleMessageTransaction();
868 handleMessageInvalidate();
869 signalRefresh();
870 break;
871 case MessageQueue::REFRESH:
872 handleMessageRefresh();
873 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800874 }
875}
876
Mathias Agopian4fec8732012-06-29 14:12:52 -0700877void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700878 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700879 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700880 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700881 }
882}
883
884void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700885 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700886 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700887}
888
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530889#ifdef QCOM_BSP
890/* Compute DirtyRegion, if DR optimization for GPU comp optimization
891 * is ON & and no external device is connected.*/
892void SurfaceFlinger::setUpTiledDr() {
893 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
894 const sp<DisplayDevice>& hw(mDisplays[HWC_DISPLAY_PRIMARY]);
895 mCanUseGpuTileRender = computeTiledDr(hw);
896 }
897}
898#endif
Mathias Agopian4fec8732012-06-29 14:12:52 -0700899void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700900 ATRACE_CALL();
901 preComposition();
902 rebuildLayerStacks();
903 setUpHWComposer();
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530904#ifdef QCOM_BSP
905 setUpTiledDr();
906#endif
Mathias Agopiancd60f992012-08-16 16:28:27 -0700907 doDebugFlashRegions();
908 doComposition();
909 postComposition();
910}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700911
Mathias Agopiancd60f992012-08-16 16:28:27 -0700912void SurfaceFlinger::doDebugFlashRegions()
913{
914 // is debugging enabled
915 if (CC_LIKELY(!mDebugRegion))
916 return;
917
918 const bool repaintEverything = mRepaintEverything;
919 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
920 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530921
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700922 if (hw->isDisplayOn()) {
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530923 const int32_t height = hw->getHeight();
924 RenderEngine& engine(getRenderEngine());
925#ifdef QCOM_BSP
926 // Use Union DR, if it is valid & GPU Tiled DR optimization is ON
927 if(mCanUseGpuTileRender && !mUnionDirtyRect.isEmpty()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700928 // redraw the whole screen
929 doComposeSurfaces(hw, Region(hw->bounds()));
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530930 Region dirtyRegion(mUnionDirtyRect);
931 Rect dr = mUnionDirtyRect;
932 hw->eglSwapPreserved(true);
933 engine.startTileComposition(dr.left, (height-dr.bottom),
934 (dr.right-dr.left),
935 (dr.bottom-dr.top), 1);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700936 // and draw the dirty region
Mathias Agopian3f844832013-08-07 21:24:32 -0700937 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530938 engine.endTileComposition(GL_PRESERVE);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700939 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700940 hw->swapBuffers(getHwComposer());
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530941 } else
942#endif
943 {
944 // transform the dirty region into this screen's coordinate
945 // space
946 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
947 if (!dirtyRegion.isEmpty()) {
948 // redraw the whole screen
949 doComposeSurfaces(hw, Region(hw->bounds()));
950 // and draw the dirty region
951#ifdef QCOM_BSP
952 if(mGpuTileRenderEnable)
953 hw->eglSwapPreserved(false);
954#endif
955
956 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
957 hw->compositionComplete();
958 hw->swapBuffers(getHwComposer());
959 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700960 }
961 }
962 }
963
964 postFramebuffer();
965
966 if (mDebugRegion > 1) {
967 usleep(mDebugRegion * 1000);
968 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700969
970 HWComposer& hwc(getHwComposer());
971 if (hwc.initCheck() == NO_ERROR) {
972 status_t err = hwc.prepare();
973 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
974 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700975}
976
977void SurfaceFlinger::preComposition()
978{
979 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700980 const LayerVector& layers(mDrawingState.layersSortedByZ);
981 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700982 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700983 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700984 needExtraInvalidate = true;
985 }
986 }
987 if (needExtraInvalidate) {
988 signalLayerUpdate();
989 }
990}
991
992void SurfaceFlinger::postComposition()
993{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700994 const LayerVector& layers(mDrawingState.layersSortedByZ);
995 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700996 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700997 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700998 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800999
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001000 const HWComposer& hwc = getHwComposer();
1001 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
1002
1003 if (presentFence->isValid()) {
1004 if (mPrimaryDispSync.addPresentFence(presentFence)) {
1005 enableHardwareVsync();
1006 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -07001007 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001008 }
1009 }
1010
Andy McFadden5167ec62014-05-22 13:08:43 -07001011 if (kIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001012 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001013 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001014 enableHardwareVsync();
1015 }
1016 }
1017
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001018 if (mAnimCompositionPending) {
1019 mAnimCompositionPending = false;
1020
Jesse Halla9a1b002013-02-27 16:39:25 -08001021 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001022 mAnimFrameTracker.setActualPresentFence(presentFence);
1023 } else {
1024 // The HWC doesn't support present fences, so use the refresh
1025 // timestamp instead.
1026 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1027 mAnimFrameTracker.setActualPresentTime(presentTime);
1028 }
1029 mAnimFrameTracker.advanceFrame();
1030 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001031}
1032
1033void SurfaceFlinger::rebuildLayerStacks() {
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07001034#ifdef QCOM_BSP
1035 char prop[PROPERTY_VALUE_MAX];
1036 property_get("sys.extended_mode", prop, "0");
1037 sExtendedMode = atoi(prop) ? true : false;
1038#endif
Mathias Agopiancd60f992012-08-16 16:28:27 -07001039 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001040 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001041 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -07001042 mVisibleRegionsDirty = false;
1043 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001044
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001045 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001046 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001047 Region opaqueRegion;
1048 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -08001049 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -07001050 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001051 const Transform& tr(hw->getTransform());
1052 const Rect bounds(hw->getBounds());
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001053 int dpyId = hw->getHwcDisplayId();
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001054 if (hw->isDisplayOn()) {
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001055 SurfaceFlinger::computeVisibleRegions(dpyId, layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001056 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001057
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001058 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001059 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001060 const sp<Layer>& layer(layers[i]);
1061 const Layer::State& s(layer->getDrawingState());
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001062 Region drawRegion(tr.transform(
1063 layer->visibleNonTransparentRegion));
1064 drawRegion.andSelf(bounds);
1065 if (!drawRegion.isEmpty()) {
1066 layersSortedByZ.add(layer);
Mathias Agopian87baae12012-07-31 12:38:26 -07001067 }
1068 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001069 }
Mathias Agopian42977342012-08-05 00:40:46 -07001070 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001071 hw->undefinedRegion.set(bounds);
1072 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
1073 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001074 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001075 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001076}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001077
Mathias Agopiancd60f992012-08-16 16:28:27 -07001078void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001079 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001080 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1081 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1082 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1083
1084 // If nothing has changed (!dirty), don't recompose.
1085 // If something changed, but we don't currently have any visible layers,
1086 // and didn't when we last did a composition, then skip it this time.
1087 // The second rule does two things:
1088 // - When all layers are removed from a display, we'll emit one black
1089 // frame, then nothing more until we get new layers.
1090 // - When a display is created with a private layer stack, we won't
1091 // emit any black frames until a layer is added to the layer stack.
1092 bool mustRecompose = dirty && !(empty && wasEmpty);
1093
1094 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1095 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1096 mustRecompose ? "doing" : "skipping",
1097 dirty ? "+" : "-",
1098 empty ? "+" : "-",
1099 wasEmpty ? "+" : "-");
1100
Dan Stoza71433162014-02-04 16:22:36 -08001101 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001102
1103 if (mustRecompose) {
1104 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1105 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001106 }
1107
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001108 HWComposer& hwc(getHwComposer());
1109 if (hwc.initCheck() == NO_ERROR) {
1110 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001111 if (CC_UNLIKELY(mHwWorkListDirty)) {
1112 mHwWorkListDirty = false;
1113 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1114 sp<const DisplayDevice> hw(mDisplays[dpy]);
1115 const int32_t id = hw->getHwcDisplayId();
1116 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001117 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001118 hw->getVisibleLayersSortedByZ());
1119 const size_t count = currentLayers.size();
1120 if (hwc.createWorkList(id, count) == NO_ERROR) {
1121 HWComposer::LayerListIterator cur = hwc.begin(id);
1122 const HWComposer::LayerListIterator end = hwc.end(id);
1123 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001124 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001125 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001126 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001127 cur->setSkip(true);
1128 }
1129 }
1130 }
1131 }
1132 }
1133 }
1134
1135 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001136 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001137 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001138 const int32_t id = hw->getHwcDisplayId();
1139 if (id >= 0) {
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001140 // Get the layers in the current drawying state
1141 const LayerVector& layers(mDrawingState.layersSortedByZ);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001142#ifdef QCOM_BSP
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001143 bool freezeSurfacePresent = false;
1144 const size_t layerCount = layers.size();
Ramkumar Radhakrishnanced0b062014-08-11 15:31:07 -07001145 // Look for ScreenShotSurface in external layer list, only when
1146 // disable external rotation animation feature is enabled
1147 if(mDisableExtAnimation && (id != HWC_DISPLAY_PRIMARY)) {
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001148 for (size_t i = 0 ; i < layerCount ; ++i) {
1149 static int screenShotLen = strlen("ScreenshotSurface");
1150 const sp<Layer>& layer(layers[i]);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001151 const Layer::State& s(layer->getDrawingState());
1152 // check the layers associated with external display
1153 if(s.layerStack == hw->getLayerStack()) {
1154 if(!strncmp(layer->getName(), "ScreenshotSurface",
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001155 screenShotLen)) {
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001156 // Screenshot layer is present, and animation in
1157 // progress
1158 freezeSurfacePresent = true;
1159 break;
1160 }
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001161 }
1162 }
1163 }
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001164#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001165 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001166 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001167 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001168 HWComposer::LayerListIterator cur = hwc.begin(id);
1169 const HWComposer::LayerListIterator end = hwc.end(id);
1170 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1171 /*
1172 * update the per-frame h/w composer data for each layer
1173 * and build the transparent region of the FB
1174 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001175 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001176 layer->setPerFrameData(hw, *cur);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001177#ifdef QCOM_BSP
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001178 if(freezeSurfacePresent) {
1179 // if freezeSurfacePresent, set ANIMATING flag
1180 cur->setAnimating(true);
1181 } else {
1182 const KeyedVector<wp<IBinder>, DisplayDeviceState>&
1183 draw(mDrawingState.displays);
1184 size_t dc = draw.size();
1185 for (size_t i=0 ; i<dc ; i++) {
1186 if (draw[i].isMainDisplay()) {
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001187 // Pass the current orientation to HWC
1188 hwc.eventControl(HWC_DISPLAY_PRIMARY,
1189 SurfaceFlinger::EVENT_ORIENTATION,
1190 uint32_t(draw[i].orientation));
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001191 }
1192 }
1193 }
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001194#endif
Mathias Agopian1e260872012-08-08 18:35:12 -07001195 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001196 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001197 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001198
Riley Andrews03414a12014-07-01 14:22:59 -07001199 // If possible, attempt to use the cursor overlay on each display.
1200 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1201 sp<const DisplayDevice> hw(mDisplays[dpy]);
1202 const int32_t id = hw->getHwcDisplayId();
1203 if (id >= 0) {
1204 const Vector< sp<Layer> >& currentLayers(
1205 hw->getVisibleLayersSortedByZ());
1206 const size_t count = currentLayers.size();
1207 HWComposer::LayerListIterator cur = hwc.begin(id);
1208 const HWComposer::LayerListIterator end = hwc.end(id);
1209 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1210 const sp<Layer>& layer(currentLayers[i]);
1211 if (layer->isPotentialCursor()) {
1212 cur->setIsCursorLayerHint();
1213 break;
1214 }
1215 }
1216 }
1217 }
1218
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001219 status_t err = hwc.prepare();
1220 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001221
1222 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1223 sp<const DisplayDevice> hw(mDisplays[dpy]);
1224 hw->prepareFrame(hwc);
1225 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001226 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001227}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001228
Mathias Agopiancd60f992012-08-16 16:28:27 -07001229void SurfaceFlinger::doComposition() {
1230 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001231 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001232 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001233 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001234 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001235 // transform the dirty region into this screen's coordinate space
1236 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001237
1238 // repaint the framebuffer (if needed)
1239 doDisplayComposition(hw, dirtyRegion);
1240
Mathias Agopiancd60f992012-08-16 16:28:27 -07001241 hw->dirtyRegion.clear();
1242 hw->flip(hw->swapRegion);
1243 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001244 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001245 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001246 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001247 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001248 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001249}
1250
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001251void SurfaceFlinger::postFramebuffer()
1252{
Mathias Agopian841cde52012-03-01 15:44:37 -08001253 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001254
Mathias Agopiana44b0412011-10-16 18:46:35 -07001255 const nsecs_t now = systemTime();
1256 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001257
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001258 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001259 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001260 if (!hwc.supportsFramebufferTarget()) {
1261 // EGL spec says:
1262 // "surface must be bound to the calling thread's current context,
1263 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001264 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001265 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001266 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001267 }
1268
Mathias Agopian6da15f42013-09-25 20:40:07 -07001269 // make the default display current because the VirtualDisplayDevice code cannot
1270 // deal with dequeueBuffer() being called outside of the composition loop; however
1271 // the code below can call glFlush() which is allowed (and does in some case) call
1272 // dequeueBuffer().
1273 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1274
Mathias Agopian92a979a2012-08-02 18:32:23 -07001275 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001276 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001277 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001278 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001279 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001280 int32_t id = hw->getHwcDisplayId();
1281 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001282 HWComposer::LayerListIterator cur = hwc.begin(id);
1283 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001284 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001285 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001286 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001287 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001288 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001289 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001290 }
Jesse Hallef194142012-06-14 14:45:17 -07001291 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001292 }
1293
Mathias Agopiana44b0412011-10-16 18:46:35 -07001294 mLastSwapBufferTime = systemTime() - now;
1295 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001296
1297 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1298 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1299 logFrameStats();
1300 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301}
1302
Mathias Agopian87baae12012-07-31 12:38:26 -07001303void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304{
Mathias Agopian841cde52012-03-01 15:44:37 -08001305 ATRACE_CALL();
1306
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001307 // here we keep a copy of the drawing state (that is the state that's
1308 // going to be overwritten by handleTransactionLocked()) outside of
1309 // mStateLock so that the side-effects of the State assignment
1310 // don't happen with mStateLock held (which can cause deadlocks).
1311 State drawingState(mDrawingState);
1312
Mathias Agopianca4d3602011-05-19 15:38:14 -07001313 Mutex::Autolock _l(mStateLock);
1314 const nsecs_t now = systemTime();
1315 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001316
Mathias Agopianca4d3602011-05-19 15:38:14 -07001317 // Here we're guaranteed that some transaction flags are set
1318 // so we can call handleTransactionLocked() unconditionally.
1319 // We call getTransactionFlags(), which will also clear the flags,
1320 // with mStateLock held to guarantee that mCurrentState won't change
1321 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001322
Mathias Agopiane57f2922012-08-09 16:29:12 -07001323 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001324 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001325
Mathias Agopianca4d3602011-05-19 15:38:14 -07001326 mLastTransactionTime = systemTime() - now;
1327 mDebugInTransaction = 0;
1328 invalidateHwcGeometry();
1329 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001330}
1331
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301332void SurfaceFlinger::setVirtualDisplayData(
1333 int32_t hwcDisplayId,
1334 const sp<IGraphicBufferProducer>& sink)
1335{
1336 sp<ANativeWindow> mNativeWindow = new Surface(sink);
1337 ANativeWindow* const window = mNativeWindow.get();
1338
1339 int format;
1340 window->query(window, NATIVE_WINDOW_FORMAT, &format);
1341
1342 EGLSurface surface;
1343 EGLint w, h;
1344 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1345 surface = eglCreateWindowSurface(display, mRenderEngine->getEGLConfig(), window, NULL);
1346 eglQuerySurface(display, surface, EGL_WIDTH, &w);
1347 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
1348
1349 mHwc->setVirtualDisplayProperties(hwcDisplayId, w, h, format);
1350}
1351
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001352void SurfaceFlinger::configureVirtualDisplay(int32_t &hwcDisplayId,
1353 sp<DisplaySurface> &dispSurface,
1354 sp<IGraphicBufferProducer> &producer,
1355 const DisplayDeviceState state,
1356 sp<IGraphicBufferProducer> bqProducer,
1357 sp<IGraphicBufferConsumer> bqConsumer)
1358{
1359 bool vdsEnabled = mHwc->isVDSEnabled();
1360
1361 //for V4L2 based virtual display implementation
1362 if(!vdsEnabled) {
1363 // persist.sys.wfd.virtual will be set if WFD is launched via
1364 // settings app. This is currently being done in
1365 // ExtendedRemoteDisplay-WFD stack.
1366 // This flag will be reset at the time of disconnection of virtual WFD
1367 // display.
1368 // This flag is set to zero if WFD is launched via QCOM WFD
1369 // proprietary APIs which use HDMI piggyback approach.
1370 char value[PROPERTY_VALUE_MAX];
1371 property_get("persist.sys.wfd.virtual", value, "0");
1372 int wfdVirtual = atoi(value);
1373 if(!wfdVirtual) {
1374 // This is for non-wfd virtual display scenarios(e.g. SSD/SR/CTS)
1375 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001376 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1377 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001378 dispSurface = vds;
1379 // There won't be any interaction with HWC for this virtual display.
1380 // so the GLES driver can pass buffers directly to the sink.
1381 producer = state.surface;
1382 } else {
Raj Kamal366d9b42014-06-11 13:49:32 +05301383 int sinkUsage = -1;
1384 state.surface->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
1385#ifdef QCOM_BSP
1386 if(sinkUsage & GRALLOC_USAGE_PRIVATE_WFD)
1387#endif
1388 hwcDisplayId = allocateHwcDisplayId(state.type);
1389
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001390 if (hwcDisplayId >= 0) {
1391 // This is for WFD virtual display scenario.
1392 // Read virtual display properties and create a
1393 // rendering surface for it inorder to be handled by hwc.
1394 setVirtualDisplayData(hwcDisplayId, state.surface);
1395 dispSurface = new FramebufferSurface(*mHwc, state.type,
1396 bqConsumer);
1397 producer = bqProducer;
1398 } else {
1399 // in case of WFD Virtual + SSD/SR concurrency scenario,
1400 // WFD virtual display instance gets valid hwcDisplayId and
1401 // SSD/SR will get invalid hwcDisplayId
1402 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001403 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1404 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001405 dispSurface = vds;
1406 // There won't be any interaction with HWC for this virtual
1407 // display, so the GLES driver can pass buffers directly to the
1408 // sink.
1409 producer = state.surface;
1410 }
1411 }
1412 } else {
1413 // VDS solution is enabled
1414 // HWC is allocated for first virtual display.
1415 // Subsequent virtual display sessions will be composed by GLES driver.
1416 // ToDo: Modify VDS component to allocate hwcDisplayId based on
1417 // mForceHwcCopy (which is based on Usage Flags)
1418
1419 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001420 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1421 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001422 dispSurface = vds;
1423 if (hwcDisplayId >= 0) {
1424 producer = vds;
1425 } else {
1426 // There won't be any interaction with HWC for this virtual display,
1427 // so the GLES driver can pass buffers directly to the sink.
1428 producer = state.surface;
1429 }
1430 }
1431}
1432
Mathias Agopian87baae12012-07-31 12:38:26 -07001433void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001434{
1435 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001436 const size_t count = currentLayers.size();
1437
1438 /*
1439 * Traversal of the children
1440 * (perform the transaction for each of them if needed)
1441 */
1442
Mathias Agopian3559b072012-08-15 13:46:03 -07001443 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001444 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001445 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001446 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1447 if (!trFlags) continue;
1448
1449 const uint32_t flags = layer->doTransaction(0);
1450 if (flags & Layer::eVisibleRegion)
1451 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001452 }
1453 }
1454
1455 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001456 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001457 */
1458
Mathias Agopiane57f2922012-08-09 16:29:12 -07001459 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001460 // here we take advantage of Vector's copy-on-write semantics to
1461 // improve performance by skipping the transaction entirely when
1462 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001463 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1464 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001465 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001466 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001467 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001468 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001469
1470 // find the displays that were removed
1471 // (ie: in drawing state but not in current state)
1472 // also handle displays that changed
1473 // (ie: displays that are in both lists)
1474 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001475 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1476 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001477 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001478 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001479 // Call makeCurrent() on the primary display so we can
1480 // be sure that nothing associated with this display
1481 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001482 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001483 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001484 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1485 if (hw != NULL)
1486 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001487 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001488 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001489 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001490 } else {
1491 ALOGW("trying to remove the main display");
1492 }
1493 } else {
1494 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001495 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001496 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001497 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001498 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001499 // recreating the DisplayDevice, so we just remove it
1500 // from the drawing state, so that it get re-added
1501 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001502 sp<DisplayDevice> hw(getDisplayDevice(display));
1503 if (hw != NULL)
1504 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001505 mDisplays.removeItem(display);
1506 mDrawingState.displays.removeItemsAt(i);
1507 dc--; i--;
1508 // at this point we must loop to the next item
1509 continue;
1510 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001511
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001512 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001513 if (disp != NULL) {
1514 if (state.layerStack != draw[i].layerStack) {
1515 disp->setLayerStack(state.layerStack);
1516 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001517 if ((state.orientation != draw[i].orientation)
1518 || (state.viewport != draw[i].viewport)
1519 || (state.frame != draw[i].frame))
1520 {
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001521#ifdef QCOM_BSP
1522 int orient = state.orientation;
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001523 // Honor the orientation change after boot
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301524 // animation completes and make sure boot
1525 // animation is shown in panel orientation always.
1526 if(mBootFinished){
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001527 disp->setProjection(state.orientation,
1528 state.viewport, state.frame);
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001529 orient = state.orientation;
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001530 }
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301531 else{
1532 char property[PROPERTY_VALUE_MAX];
1533 int panelOrientation =
1534 DisplayState::eOrientationDefault;
1535 if(property_get("persist.panel.orientation",
1536 property, "0") > 0){
1537 panelOrientation = atoi(property) / 90;
1538 }
1539 disp->setProjection(panelOrientation,
1540 state.viewport, state.frame);
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001541 orient = panelOrientation;
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301542 }
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001543 // Set the view frame of each display only of its
1544 // default orientation.
Saurabh Shah027b3852014-08-21 16:09:32 -07001545 if(orient == DisplayState::eOrientationDefault and
1546 state.frame.isValid()) {
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001547 qdutils::setViewFrame(disp->getHwcDisplayId(),
1548 state.frame.left, state.frame.top,
1549 state.frame.right, state.frame.bottom);
1550 }
1551#else
1552 disp->setProjection(state.orientation,
1553 state.viewport, state.frame);
1554#endif
Mathias Agopian93997a82012-08-29 17:30:36 -07001555 }
Michael Lentine47e45402014-07-18 15:34:25 -07001556 if (state.width != draw[i].width || state.height != draw[i].height) {
1557 disp->setDisplaySize(state.width, state.height);
1558 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001559 }
1560 }
1561 }
1562
1563 // find displays that were added
1564 // (ie: in current state but not in drawing state)
1565 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001566 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001567 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001568
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001569 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001570 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001571 sp<IGraphicBufferProducer> bqProducer;
1572 sp<IGraphicBufferConsumer> bqConsumer;
1573 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1574 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001575
Jesse Hall02d86562013-03-25 14:43:23 -07001576 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001577 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001578 // Virtual displays without a surface are dormant:
1579 // they have external state (layer stack, projection,
1580 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001581 if (state.surface != NULL) {
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001582 configureVirtualDisplay(hwcDisplayId,
1583 dispSurface, producer, state, bqProducer,
1584 bqConsumer);
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001585 }
1586 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001587 ALOGE_IF(state.surface!=NULL,
1588 "adding a supported display, but rendering "
1589 "surface is provided (%p), ignoring it",
1590 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001591 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001592 // for supported (by hwc) displays we provide our
1593 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001594 dispSurface = new FramebufferSurface(*mHwc, state.type,
1595 bqConsumer);
1596 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001597 }
1598
1599 const wp<IBinder>& display(curr.keyAt(i));
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301600 if (dispSurface != NULL && producer != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001601 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001602 state.type, hwcDisplayId,
1603 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001604 display, dispSurface, producer,
1605 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001606 hw->setLayerStack(state.layerStack);
1607 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001608 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001609 hw->setDisplayName(state.displayName);
Tatenda Chipeperekwab801c8a2014-09-22 16:57:24 -07001610 // When a new display device is added update the active
1611 // config by querying HWC otherwise the default config
1612 // (config 0) will be used.
1613 int activeConfig = mHwc->getActiveConfig(hwcDisplayId);
1614 if (activeConfig >= 0) {
1615 hw->setActiveConfig(activeConfig);
1616 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07001617 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001618 if (state.isVirtualDisplay()) {
1619 if (hwcDisplayId >= 0) {
1620 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1621 hw->getWidth(), hw->getHeight(),
1622 hw->getFormat());
1623 }
1624 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001625 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001626 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001627 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001628 }
1629 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001630 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001631 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001632
Mathias Agopian84300952012-11-21 16:02:13 -08001633 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1634 // The transform hint might have changed for some layers
1635 // (either because a display has changed, or because a layer
1636 // as changed).
1637 //
1638 // Walk through all the layers in currentLayers,
1639 // and update their transform hint.
1640 //
1641 // If a layer is visible only on a single display, then that
1642 // display is used to calculate the hint, otherwise we use the
1643 // default display.
1644 //
1645 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1646 // the hint is set before we acquire a buffer from the surface texture.
1647 //
1648 // NOTE: layer transactions have taken place already, so we use their
1649 // drawing state. However, SurfaceFlinger's own transaction has not
1650 // happened yet, so we must use the current state layer list
1651 // (soon to become the drawing state list).
1652 //
1653 sp<const DisplayDevice> disp;
1654 uint32_t currentlayerStack = 0;
1655 for (size_t i=0; i<count; i++) {
1656 // NOTE: we rely on the fact that layers are sorted by
1657 // layerStack first (so we don't have to traverse the list
1658 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001659 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001660 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001661 if (i==0 || currentlayerStack != layerStack) {
1662 currentlayerStack = layerStack;
1663 // figure out if this layerstack is mirrored
1664 // (more than one display) if so, pick the default display,
1665 // if not, pick the only display it's on.
1666 disp.clear();
1667 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1668 sp<const DisplayDevice> hw(mDisplays[dpy]);
1669 if (hw->getLayerStack() == currentlayerStack) {
1670 if (disp == NULL) {
1671 disp = hw;
1672 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001673 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001674 break;
1675 }
1676 }
1677 }
1678 }
Chet Haase91d25932013-04-11 15:24:55 -07001679 if (disp == NULL) {
1680 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1681 // redraw after transform hint changes. See bug 8508397.
1682
1683 // could be null when this layer is using a layerStack
1684 // that is not visible on any display. Also can occur at
1685 // screen off/on times.
1686 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001687 }
Chet Haase91d25932013-04-11 15:24:55 -07001688 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001689 }
1690 }
1691
1692
Mathias Agopian3559b072012-08-15 13:46:03 -07001693 /*
1694 * Perform our own transaction if needed
1695 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001696
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001697 const LayerVector& layers(mDrawingState.layersSortedByZ);
1698 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001699 // layers have been added
1700 mVisibleRegionsDirty = true;
1701 }
1702
1703 // some layers might have been removed, so
1704 // we need to update the regions they're exposing.
1705 if (mLayersRemoved) {
1706 mLayersRemoved = false;
1707 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001708 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001709 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001710 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001711 if (currentLayers.indexOf(layer) < 0) {
1712 // this layer is not visible anymore
1713 // TODO: we could traverse the tree from front to back and
1714 // compute the actual visible region
1715 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001716 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001717 Region visibleReg = s.transform.transform(
1718 Region(Rect(s.active.w, s.active.h)));
1719 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001720 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001721 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001722 }
1723
1724 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001725
1726 updateCursorAsync();
1727}
1728
1729void SurfaceFlinger::updateCursorAsync()
1730{
1731 HWComposer& hwc(getHwComposer());
1732 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1733 sp<const DisplayDevice> hw(mDisplays[dpy]);
1734 const int32_t id = hw->getHwcDisplayId();
1735 if (id < 0) {
1736 continue;
1737 }
1738 const Vector< sp<Layer> >& currentLayers(
1739 hw->getVisibleLayersSortedByZ());
1740 const size_t count = currentLayers.size();
1741 HWComposer::LayerListIterator cur = hwc.begin(id);
1742 const HWComposer::LayerListIterator end = hwc.end(id);
1743 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1744 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1745 continue;
1746 }
1747 const sp<Layer>& layer(currentLayers[i]);
1748 Rect cursorPos = layer->getPosition(hw);
1749 hwc.setCursorPositionAsync(id, cursorPos);
1750 break;
1751 }
1752 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001753}
1754
1755void SurfaceFlinger::commitTransaction()
1756{
1757 if (!mLayersPendingRemoval.isEmpty()) {
1758 // Notify removed layers now that they can't be drawn from
1759 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1760 mLayersPendingRemoval[i]->onRemoved();
1761 }
1762 mLayersPendingRemoval.clear();
1763 }
1764
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001765 // If this transaction is part of a window animation then the next frame
1766 // we composite should be considered an animation as well.
1767 mAnimCompositionPending = mAnimTransactionPending;
1768
Mathias Agopian4fec8732012-06-29 14:12:52 -07001769 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001770 mTransactionPending = false;
1771 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001772 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001773}
1774
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001775void SurfaceFlinger::computeVisibleRegions(size_t dpy,
Mathias Agopian87baae12012-07-31 12:38:26 -07001776 const LayerVector& currentLayers, uint32_t layerStack,
1777 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001778{
Mathias Agopian841cde52012-03-01 15:44:37 -08001779 ATRACE_CALL();
1780
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001781 Region aboveOpaqueLayers;
1782 Region aboveCoveredLayers;
1783 Region dirty;
1784
Mathias Agopian87baae12012-07-31 12:38:26 -07001785 outDirtyRegion.clear();
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001786 bool bIgnoreLayers = false;
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001787 int indexLOI = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001788 size_t i = currentLayers.size();
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001789#ifdef QCOM_BSP
1790 while (i--) {
1791 const sp<Layer>& layer = currentLayers[i];
1792 // iterate through the layer list to find ext_only layers and store
1793 // the index
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001794 if (layer->isSecureDisplay()) {
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001795 bIgnoreLayers = true;
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001796 indexLOI = -1;
1797 if(!dpy)
1798 indexLOI = i;
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001799 break;
1800 }
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07001801 // iterate through the layer list to find ext_only layers or yuv
1802 // layer(extended_mode) and store the index
1803 if ((dpy && (layer->isExtOnly() ||
1804 (isExtendedMode() && layer->isYuvLayer())))) {
1805 bIgnoreLayers= true;
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001806 indexLOI = i;
1807 }
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001808 }
1809 i = currentLayers.size();
1810#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001811 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001812 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001813
1814 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001815 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001816
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001817#ifdef QCOM_BSP
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07001818 // Only add the layer marked as "external_only" or yuvLayer
1819 // (extended_mode) to external list and
1820 // only remove the layer marked as "external_only" or yuvLayer in
1821 // extended_mode from primary list
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001822 // and do not add the layer marked as "internal_only" to external list
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001823 // Add secure UI layers to primary and remove other layers from internal
1824 //and external list
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07001825 if(((bIgnoreLayers && indexLOI != (int)i) ||
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001826 (!dpy && layer->isExtOnly()) ||
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07001827 (!dpy && isExtendedMode() && layer->isYuvLayer()))||
1828 (dpy && layer->isIntOnly())) {
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001829 // Ignore all other layers except the layers marked as ext_only
1830 // by setting visible non transparent region empty.
1831 Region visibleNonTransRegion;
1832 visibleNonTransRegion.set(Rect(0,0));
1833 layer->setVisibleNonTransparentRegion(visibleNonTransRegion);
1834 continue;
1835 }
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001836#endif
1837 // only consider the layers on the given later stack
1838 // Override layers created using presentation class by the layers having
1839 // ext_only flag enabled
1840 if(s.layerStack != layerStack && !bIgnoreLayers) {
1841 // set the visible region as empty since we have removed the
1842 // layerstack check in rebuildLayerStack() function.
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001843 Region visibleNonTransRegion;
1844 visibleNonTransRegion.set(Rect(0,0));
1845 layer->setVisibleNonTransparentRegion(visibleNonTransRegion);
1846 continue;
1847 }
Mathias Agopianab028732010-03-16 16:41:46 -07001848 /*
1849 * opaqueRegion: area of a surface that is fully opaque.
1850 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001851 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001852
1853 /*
1854 * visibleRegion: area of a surface that is visible on screen
1855 * and not fully transparent. This is essentially the layer's
1856 * footprint minus the opaque regions above it.
1857 * Areas covered by a translucent surface are considered visible.
1858 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001859 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001860
1861 /*
1862 * coveredRegion: area of a surface that is covered by all
1863 * visible regions above it (which includes the translucent areas).
1864 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001865 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001866
Jesse Halla8026d22012-09-25 13:25:04 -07001867 /*
1868 * transparentRegion: area of a surface that is hinted to be completely
1869 * transparent. This is only used to tell when the layer has no visible
1870 * non-transparent regions and can be removed from the layer list. It
1871 * does not affect the visibleRegion of this layer or any layers
1872 * beneath it. The hint may not be correct if apps don't respect the
1873 * SurfaceView restrictions (which, sadly, some don't).
1874 */
1875 Region transparentRegion;
1876
Mathias Agopianab028732010-03-16 16:41:46 -07001877
1878 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001879 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001880 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001881 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001882 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001883 if (!visibleRegion.isEmpty()) {
1884 // Remove the transparent area from the visible region
1885 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001886 const Transform tr(s.transform);
1887 if (tr.transformed()) {
1888 if (tr.preserveRects()) {
1889 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001890 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001891 } else {
1892 // transformation too complex, can't do the
1893 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001894 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001895 }
1896 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001897 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001898 }
Mathias Agopianab028732010-03-16 16:41:46 -07001899 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001900
Mathias Agopianab028732010-03-16 16:41:46 -07001901 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001902 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001903 if (s.alpha==255 && !translucent &&
1904 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1905 // the opaque region is the layer's footprint
1906 opaqueRegion = visibleRegion;
1907 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001908 }
1909 }
1910
Mathias Agopianab028732010-03-16 16:41:46 -07001911 // Clip the covered region to the visible region
1912 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1913
1914 // Update aboveCoveredLayers for next (lower) layer
1915 aboveCoveredLayers.orSelf(visibleRegion);
1916
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001917 // subtract the opaque region covered by the layers above us
1918 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001919
1920 // compute this layer's dirty region
1921 if (layer->contentDirty) {
1922 // we need to invalidate the whole region
1923 dirty = visibleRegion;
1924 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001925 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001926 layer->contentDirty = false;
1927 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001928 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001929 * the exposed region consists of two components:
1930 * 1) what's VISIBLE now and was COVERED before
1931 * 2) what's EXPOSED now less what was EXPOSED before
1932 *
1933 * note that (1) is conservative, we start with the whole
1934 * visible region but only keep what used to be covered by
1935 * something -- which mean it may have been exposed.
1936 *
1937 * (2) handles areas that were not covered by anything but got
1938 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001939 */
Mathias Agopianab028732010-03-16 16:41:46 -07001940 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001941 const Region oldVisibleRegion = layer->visibleRegion;
1942 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001943 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1944 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001945 }
1946 dirty.subtractSelf(aboveOpaqueLayers);
1947
1948 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001949 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001950
Mathias Agopianab028732010-03-16 16:41:46 -07001951 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001952 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001953
Jesse Halla8026d22012-09-25 13:25:04 -07001954 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001955 layer->setVisibleRegion(visibleRegion);
1956 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001957 layer->setVisibleNonTransparentRegion(
1958 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001959 }
1960
Mathias Agopian87baae12012-07-31 12:38:26 -07001961 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001962}
1963
Mathias Agopian87baae12012-07-31 12:38:26 -07001964void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1965 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001966 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001967 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1968 if (hw->getLayerStack() == layerStack) {
1969 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001970 }
1971 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001972}
1973
1974void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001975{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001976 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001977
Mathias Agopian4fec8732012-06-29 14:12:52 -07001978 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001979 const LayerVector& layers(mDrawingState.layersSortedByZ);
Eric Penner51c59cd2014-07-28 19:51:58 -07001980
1981 // Store the set of layers that need updates. This set must not change as
1982 // buffers are being latched, as this could result in a deadlock.
1983 // Example: Two producers share the same command stream and:
1984 // 1.) Layer 0 is latched
1985 // 2.) Layer 0 gets a new frame
1986 // 2.) Layer 1 gets a new frame
1987 // 3.) Layer 1 is latched.
1988 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1989 // second frame. But layer 0's second frame could be waiting on display.
1990 Vector<Layer*> layersWithQueuedFrames;
1991 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001992 const sp<Layer>& layer(layers[i]);
Eric Penner51c59cd2014-07-28 19:51:58 -07001993 if (layer->hasQueuedFrame())
1994 layersWithQueuedFrames.push_back(layer.get());
1995 }
1996 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
1997 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07001998 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001999 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07002000 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07002001 }
Mathias Agopian4da75192010-08-10 17:19:56 -07002002
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002003 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002004}
2005
Mathias Agopianad456f92011-01-13 17:53:01 -08002006void SurfaceFlinger::invalidateHwcGeometry()
2007{
2008 mHwWorkListDirty = true;
2009}
2010
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002011
Mathias Agopiancd60f992012-08-16 16:28:27 -07002012void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07002013 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002014{
Dan Stoza71433162014-02-04 16:22:36 -08002015 // We only need to actually compose the display if:
2016 // 1) It is being handled by hardware composer, which may need this to
2017 // keep its virtual display state machine in sync, or
2018 // 2) There is work to be done (the dirty region isn't empty)
2019 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
2020 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
2021 return;
2022 }
2023
Mathias Agopian87baae12012-07-31 12:38:26 -07002024 Region dirtyRegion(inDirtyRegion);
2025
Mathias Agopianb8a55602009-06-26 19:06:36 -07002026 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07002027 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002028
Mathias Agopian42977342012-08-05 00:40:46 -07002029 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002030 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002031 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
2032 // takes a rectangle, we must make sure to update that whole
2033 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07002034 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002035 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002036 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002037 // We need to redraw the rectangle that will be updated
2038 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07002039 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002040 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07002041 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002042 } else {
2043 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07002044 dirtyRegion.set(hw->bounds());
2045 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002046 }
2047 }
2048
Alan Viverette9c5a3332013-09-12 20:04:35 -07002049 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Michael Lentine3f121fc2014-10-01 11:17:28 -07002050 if (!doComposeSurfaces(hw, dirtyRegion)) return;
Mathias Agopianff2ed702013-09-01 21:36:12 -07002051 } else {
2052 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07002053 mat4 colorMatrix = mColorMatrix;
2054 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07002055 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002056 }
2057 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07002058 doComposeSurfaces(hw, dirtyRegion);
2059 engine.endGroup();
2060 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002061
Mathias Agopian9c6e2972011-09-20 17:21:56 -07002062 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07002063 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07002064
2065 // swap buffers (presentation)
2066 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002067}
2068
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302069#ifdef QCOM_BSP
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302070bool SurfaceFlinger::computeTiledDr(const sp<const DisplayDevice>& hw) {
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302071 int fbWidth= hw->getWidth();
2072 int fbHeight= hw->getHeight();
2073 Rect fullScreenRect = Rect(0,0,fbWidth, fbHeight);
2074 const int32_t id = hw->getHwcDisplayId();
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302075 mUnionDirtyRect.clear();
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302076 HWComposer& hwc(getHwComposer());
2077
2078 /* Compute and return the Union of Dirty Rects.
2079 * Return false if the unionDR is fullscreen, as there is no benefit from
2080 * preserving full screen.*/
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302081 return (hwc.canUseTiledDR(id, mUnionDirtyRect) &&
2082 (mUnionDirtyRect != fullScreenRect));
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302083
2084}
2085#endif
2086
Michael Lentine3f121fc2014-10-01 11:17:28 -07002087bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07002088{
Mathias Agopian3f844832013-08-07 21:24:32 -07002089 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002090 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07002091 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07002092 HWComposer::LayerListIterator cur = hwc.begin(id);
2093 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07002094
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302095 Region clearRegion;
Jesse Halld05a17f2013-09-30 16:49:30 -07002096 bool hasGlesComposition = hwc.hasGlesComposition(id);
Sravan Kumar D.V.N74180cb2014-07-01 16:35:05 +05302097 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002098 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07002099 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08002100 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
2101 hw->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07002102 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
2103 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
2104 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
2105 }
2106 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08002107 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07002108
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07002109 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopiane60b0682012-08-21 23:34:09 -07002110 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07002111 // when using overlays, we assume a fully transparent framebuffer
2112 // NOTE: we could reduce how much we need to clear, for instance
2113 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07002114 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07002115 // We'll revisit later if needed.
Sravan Kumar D.V.N74180cb2014-07-01 16:35:05 +05302116 if(!(mGpuTileRenderEnable && (mDisplays.size()==1)))
2117 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002118 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07002119 // we start with the whole screen area
2120 const Region bounds(hw->getBounds());
2121
2122 // we remove the scissor part
2123 // we're left with the letterbox region
2124 // (common case is that letterbox ends-up being empty)
2125 const Region letterbox(bounds.subtract(hw->getScissor()));
2126
2127 // compute the area to clear
2128 Region region(hw->undefinedRegion.merge(letterbox));
2129
2130 // but limit it to the dirty region
2131 region.andSelf(dirty);
2132
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302133
Mathias Agopianb9494d52012-04-18 02:28:45 -07002134 // screen is already cleared here
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302135#ifdef QCOM_BSP
2136 clearRegion.clear();
2137 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
2138 clearRegion = region;
2139 if (cur == end) {
Radhika Ranjan Soni35c9ff22013-12-17 19:55:17 +05302140 drawWormhole(hw, region);
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302141 } else if(mCanUseGpuTileRender) {
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302142 /* If GPUTileRect DR optimization on clear only the UnionDR
2143 * (computed by computeTiledDr) which is the actual region
2144 * that will be drawn on FB in this cycle.. */
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302145 clearRegion = clearRegion.andSelf(Region(mUnionDirtyRect));
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302146 }
2147 } else
2148#endif
2149 {
2150 if (!region.isEmpty()) {
2151 if (cur != end) {
2152 if (cur->getCompositionType() != HWC_BLIT)
2153 // can happen with SurfaceView
2154 drawWormhole(hw, region);
2155 } else
2156 drawWormhole(hw, region);
2157 }
Mathias Agopianb9494d52012-04-18 02:28:45 -07002158 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002159 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002160
Mathias Agopian766dc492012-10-30 18:08:06 -07002161 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
2162 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07002163 // scissor on the main display. It should never be needed
2164 // anyways (though in theory it could since the API allows it).
2165 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07002166 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002167 if (scissor != bounds) {
2168 // scissor doesn't match the screen's dimensions, so we
2169 // need to clear everything outside of it and enable
2170 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07002171
Mathias Agopianf45c5102012-10-24 16:29:17 -07002172 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002173 const uint32_t height = hw->getHeight();
2174 engine.setScissor(scissor.left, height - scissor.bottom,
2175 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002176 }
2177 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002178 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002179
Mathias Agopian85d751c2012-08-29 16:59:24 -07002180 /*
2181 * and then, render the layers targeted at the framebuffer
2182 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002183
Mathias Agopian13127d82013-03-05 17:47:11 -08002184 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002185 const size_t count = layers.size();
2186 const Transform& tr = hw->getTransform();
2187 if (cur != end) {
2188 // we're using h/w composer
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302189#ifdef QCOM_BSP
2190 int fbWidth= hw->getWidth();
2191 int fbHeight= hw->getHeight();
2192 /* if GPUTileRender optimization property is on & can be used
2193 * i) Enable EGL_SWAP_PRESERVED flag
2194 * ii) do startTile with union DirtyRect
2195 * else , Disable EGL_SWAP_PRESERVED */
2196 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302197 if(mCanUseGpuTileRender && !mUnionDirtyRect.isEmpty()) {
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302198 hw->eglSwapPreserved(true);
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302199 Rect dr = mUnionDirtyRect;
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302200 engine.startTileComposition(dr.left, (fbHeight-dr.bottom),
2201 (dr.right-dr.left),
2202 (dr.bottom-dr.top), 0);
2203 } else {
2204 // Un Set EGL_SWAP_PRESERVED flag, if no tiling required.
2205 hw->eglSwapPreserved(false);
2206 }
2207 // DrawWormHole/Any Draw has to be within startTile & EndTile
Sravan Kumar D.V.N280bcd32014-08-28 14:54:25 +05302208 if (hasGlesComposition) {
2209 if (hasHwcComposition) {
2210 if(mCanUseGpuTileRender && !mUnionDirtyRect.isEmpty()) {
2211 const Rect& scissor(mUnionDirtyRect);
2212 engine.setScissor(scissor.left,
2213 hw->getHeight()- scissor.bottom,
2214 scissor.getWidth(), scissor.getHeight());
2215 engine.clearWithColor(0, 0, 0, 0);
2216 engine.disableScissor();
2217 } else {
2218 engine.clearWithColor(0, 0, 0, 0);
2219 }
Sravan Kumar D.V.N74180cb2014-07-01 16:35:05 +05302220 } else {
Sravan Kumar D.V.N280bcd32014-08-28 14:54:25 +05302221 if (cur->getCompositionType() != HWC_BLIT &&
2222 !clearRegion.isEmpty()) {
2223 drawWormhole(hw, clearRegion);
2224 }
Sravan Kumar D.V.N74180cb2014-07-01 16:35:05 +05302225 }
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302226 }
2227 }
2228#endif
2229
Mathias Agopian85d751c2012-08-29 16:59:24 -07002230 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002231 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07002232 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07002233 if (!clip.isEmpty()) {
2234 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07002235 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07002236 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07002237 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002238 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
2239 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08002240 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07002241 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07002242 // never clear the very first layer since we're
2243 // guaranteed the FB is already cleared
2244 layer->clearWithOpenGL(hw, clip);
2245 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002246 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07002247 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002248 case HWC_FRAMEBUFFER: {
2249 layer->draw(hw, clip);
2250 break;
2251 }
Arun Kumar K.R5111ff02012-12-19 18:10:46 -08002252 case HWC_BLIT:
2253 //Do nothing
2254 break;
Mathias Agopianda27af92012-09-13 18:17:13 -07002255 case HWC_FRAMEBUFFER_TARGET: {
2256 // this should not happen as the iterator shouldn't
2257 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08002258 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07002259 break;
2260 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07002261 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002262 }
2263 layer->setAcquireFence(hw, *cur);
2264 }
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302265
2266#ifdef QCOM_BSP
2267 // call EndTile, if starTile has been called in this cycle.
2268 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302269 if(mCanUseGpuTileRender && !mUnionDirtyRect.isEmpty()) {
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302270 engine.endTileComposition(GL_PRESERVE);
2271 }
2272 }
2273#endif
Mathias Agopian85d751c2012-08-29 16:59:24 -07002274 } else {
2275 // we're not using h/w composer
2276 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002277 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002278 const Region clip(dirty.intersect(
2279 tr.transform(layer->visibleRegion)));
2280 if (!clip.isEmpty()) {
2281 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07002282 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002283 }
2284 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002285
2286 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002287 engine.disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07002288 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002289}
2290
Mathias Agopian3f844832013-08-07 21:24:32 -07002291void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07002292 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07002293 RenderEngine& engine(getRenderEngine());
2294 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002295}
2296
Mathias Agopianac9fa422013-02-11 16:40:36 -08002297void SurfaceFlinger::addClientLayer(const sp<Client>& client,
2298 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07002299 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08002300 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07002301{
Mathias Agopian96f08192010-06-02 23:28:45 -07002302 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08002303 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07002304
Mathias Agopian96f08192010-06-02 23:28:45 -07002305 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002306 Mutex::Autolock _l(mStateLock);
2307 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07002308 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07002309}
2310
Mathias Agopian3f844832013-08-07 21:24:32 -07002311status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002312 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08002313 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002314 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07002315 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002316 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07002317 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002318 return NO_ERROR;
2319 }
Mathias Agopian3d579642009-06-04 18:46:21 -07002320 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002321}
2322
Dan Stozac7014012014-02-14 15:03:43 -08002323uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07002324 return android_atomic_release_load(&mTransactionFlags);
2325}
2326
Mathias Agopian3f844832013-08-07 21:24:32 -07002327uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002328 return android_atomic_and(~flags, &mTransactionFlags) & flags;
2329}
2330
Mathias Agopian3f844832013-08-07 21:24:32 -07002331uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002332 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
2333 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002334 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002335 }
2336 return old;
2337}
2338
Mathias Agopian8b33f032012-07-24 20:43:54 -07002339void SurfaceFlinger::setTransactionState(
2340 const Vector<ComposerState>& state,
2341 const Vector<DisplayState>& displays,
2342 uint32_t flags)
2343{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002344 ATRACE_CALL();
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08002345 size_t count = displays.size();
2346#ifdef QCOM_BSP
Ramkumar Radhakrishnanced0b062014-08-11 15:31:07 -07002347 // Delay the display projection transaction by 50ms only when the disable
2348 // external rotation animation feature is enabled
2349 if(mDisableExtAnimation) {
2350 for (size_t i=0 ; i<count ; i++) {
2351 const DisplayState& s(displays[i]);
Baldev Kumar Sahub4ff4392014-09-12 10:58:11 +05302352 if((mDisplays.indexOfKey(s.token) >= 0) && (s.token !=
2353 mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY])) {
Ramkumar Radhakrishnanced0b062014-08-11 15:31:07 -07002354 const uint32_t what = s.what;
2355 // Invalidate and Delay the binder thread by 50 ms on
2356 // eDisplayProjectionChanged to trigger a draw cycle so that
2357 // it can fix one incorrect frame on the External, when we
2358 // disable external animation
2359 if (what & DisplayState::eDisplayProjectionChanged) {
2360 invalidateHwcGeometry();
2361 repaintEverything();
2362 usleep(50000);
2363 }
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08002364 }
2365 }
2366 }
2367#endif
Mathias Agopian698c0872011-06-28 19:09:31 -07002368 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07002369 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002370
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002371 if (flags & eAnimation) {
2372 // For window updates that are part of an animation we must wait for
2373 // previous animation "frames" to be handled.
2374 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002375 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002376 if (CC_UNLIKELY(err != NO_ERROR)) {
2377 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002378 // caller after a few seconds.
2379 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
2380 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002381 mAnimTransactionPending = false;
2382 break;
2383 }
2384 }
2385 }
2386
Mathias Agopiane57f2922012-08-09 16:29:12 -07002387 for (size_t i=0 ; i<count ; i++) {
2388 const DisplayState& s(displays[i]);
2389 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002390 }
2391
Mathias Agopiane57f2922012-08-09 16:29:12 -07002392 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002393 for (size_t i=0 ; i<count ; i++) {
2394 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002395 // Here we need to check that the interface we're given is indeed
2396 // one of our own. A malicious client could give us a NULL
2397 // IInterface, or one of its own or even one of our own but a
2398 // different type. All these situations would cause us to crash.
2399 //
2400 // NOTE: it would be better to use RTTI as we could directly check
2401 // that we have a Client*. however, RTTI is disabled in Android.
2402 if (s.client != NULL) {
2403 sp<IBinder> binder = s.client->asBinder();
2404 if (binder != NULL) {
2405 String16 desc(binder->getInterfaceDescriptor());
2406 if (desc == ISurfaceComposerClient::descriptor) {
2407 sp<Client> client( static_cast<Client *>(s.client.get()) );
2408 transactionFlags |= setClientStateLocked(client, s.state);
2409 }
2410 }
2411 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002412 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002413
Mathias Agopian386aa982011-11-07 21:58:03 -08002414 if (transactionFlags) {
2415 // this triggers the transaction
2416 setTransactionFlags(transactionFlags);
2417
2418 // if this is a synchronous transaction, wait for it to take effect
2419 // before returning.
2420 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002421 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002422 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002423 if (flags & eAnimation) {
2424 mAnimTransactionPending = true;
2425 }
2426 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002427 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2428 if (CC_UNLIKELY(err != NO_ERROR)) {
2429 // just in case something goes wrong in SF, return to the
2430 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002431 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2432 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002433 break;
2434 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002435 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002436 }
2437}
2438
Mathias Agopiane57f2922012-08-09 16:29:12 -07002439uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2440{
Jesse Hall9a143922012-10-04 16:29:19 -07002441 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2442 if (dpyIdx < 0)
2443 return 0;
2444
Mathias Agopiane57f2922012-08-09 16:29:12 -07002445 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002446 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002447 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002448 const uint32_t what = s.what;
2449 if (what & DisplayState::eSurfaceChanged) {
2450 if (disp.surface->asBinder() != s.surface->asBinder()) {
2451 disp.surface = s.surface;
2452 flags |= eDisplayTransactionNeeded;
2453 }
2454 }
2455 if (what & DisplayState::eLayerStackChanged) {
2456 if (disp.layerStack != s.layerStack) {
2457 disp.layerStack = s.layerStack;
2458 flags |= eDisplayTransactionNeeded;
2459 }
2460 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002461 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002462 if (disp.orientation != s.orientation) {
2463 disp.orientation = s.orientation;
2464 flags |= eDisplayTransactionNeeded;
2465 }
2466 if (disp.frame != s.frame) {
2467 disp.frame = s.frame;
2468 flags |= eDisplayTransactionNeeded;
2469 }
2470 if (disp.viewport != s.viewport) {
2471 disp.viewport = s.viewport;
2472 flags |= eDisplayTransactionNeeded;
2473 }
2474 }
Michael Lentine47e45402014-07-18 15:34:25 -07002475 if (what & DisplayState::eDisplaySizeChanged) {
2476 if (disp.width != s.width) {
2477 disp.width = s.width;
2478 flags |= eDisplayTransactionNeeded;
2479 }
2480 if (disp.height != s.height) {
2481 disp.height = s.height;
2482 flags |= eDisplayTransactionNeeded;
2483 }
2484 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002485 }
2486 return flags;
2487}
2488
2489uint32_t SurfaceFlinger::setClientStateLocked(
2490 const sp<Client>& client,
2491 const layer_state_t& s)
2492{
2493 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002494 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002495 if (layer != 0) {
2496 const uint32_t what = s.what;
2497 if (what & layer_state_t::ePositionChanged) {
2498 if (layer->setPosition(s.x, s.y))
2499 flags |= eTraversalNeeded;
2500 }
2501 if (what & layer_state_t::eLayerChanged) {
2502 // NOTE: index needs to be calculated before we update the state
2503 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2504 if (layer->setLayer(s.z)) {
2505 mCurrentState.layersSortedByZ.removeAt(idx);
2506 mCurrentState.layersSortedByZ.add(layer);
2507 // we need traversal (state changed)
2508 // AND transaction (list changed)
2509 flags |= eTransactionNeeded|eTraversalNeeded;
2510 }
2511 }
2512 if (what & layer_state_t::eSizeChanged) {
2513 if (layer->setSize(s.w, s.h)) {
2514 flags |= eTraversalNeeded;
2515 }
2516 }
2517 if (what & layer_state_t::eAlphaChanged) {
2518 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2519 flags |= eTraversalNeeded;
2520 }
2521 if (what & layer_state_t::eMatrixChanged) {
2522 if (layer->setMatrix(s.matrix))
2523 flags |= eTraversalNeeded;
2524 }
2525 if (what & layer_state_t::eTransparentRegionChanged) {
2526 if (layer->setTransparentRegionHint(s.transparentRegion))
2527 flags |= eTraversalNeeded;
2528 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08002529 if ((what & layer_state_t::eVisibilityChanged) ||
2530 (what & layer_state_t::eOpacityChanged)) {
2531 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07002532 if (layer->setFlags(s.flags, s.mask))
2533 flags |= eTraversalNeeded;
2534 }
2535 if (what & layer_state_t::eCropChanged) {
2536 if (layer->setCrop(s.crop))
2537 flags |= eTraversalNeeded;
2538 }
2539 if (what & layer_state_t::eLayerStackChanged) {
2540 // NOTE: index needs to be calculated before we update the state
2541 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2542 if (layer->setLayerStack(s.layerStack)) {
2543 mCurrentState.layersSortedByZ.removeAt(idx);
2544 mCurrentState.layersSortedByZ.add(layer);
2545 // we need traversal (state changed)
2546 // AND transaction (list changed)
2547 flags |= eTransactionNeeded|eTraversalNeeded;
2548 }
2549 }
2550 }
2551 return flags;
2552}
2553
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002554status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002555 const String8& name,
2556 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002557 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2558 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002559{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002560 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002561 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002562 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002563 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002564 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002565 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002566
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002567 status_t result = NO_ERROR;
2568
2569 sp<Layer> layer;
2570
Mathias Agopian3165cc22012-08-08 19:42:09 -07002571 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2572 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002573 result = createNormalLayer(client,
2574 name, w, h, flags, format,
2575 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002576 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002577 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002578 result = createDimLayer(client,
2579 name, w, h, flags,
2580 handle, gbp, &layer);
2581 break;
2582 default:
2583 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002584 break;
2585 }
2586
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002587 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002588 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002589 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002590 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002591 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002592}
2593
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002594status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2595 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2596 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002597{
2598 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002599 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002600 case PIXEL_FORMAT_TRANSPARENT:
2601 case PIXEL_FORMAT_TRANSLUCENT:
2602 format = PIXEL_FORMAT_RGBA_8888;
2603 break;
2604 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002605 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002606 break;
2607 }
2608
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002609 *outLayer = new Layer(this, client, name, w, h, flags);
2610 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2611 if (err == NO_ERROR) {
2612 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002613 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002614 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002615
2616 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2617 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002618}
2619
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002620status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2621 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2622 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002623{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002624 *outLayer = new LayerDim(this, client, name, w, h, flags);
2625 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002626 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002627 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002628}
2629
Mathias Agopianac9fa422013-02-11 16:40:36 -08002630status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002631{
Mathias Agopian67106042013-03-14 19:18:13 -07002632 // called by the window manager when it wants to remove a Layer
2633 status_t err = NO_ERROR;
2634 sp<Layer> l(client->getLayerUser(handle));
2635 if (l != NULL) {
2636 err = removeLayer(l);
2637 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2638 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002639 }
2640 return err;
2641}
2642
Mathias Agopian13127d82013-03-05 17:47:11 -08002643status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002644{
Mathias Agopian67106042013-03-14 19:18:13 -07002645 // called by ~LayerCleaner() when all references to the IBinder (handle)
2646 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002647 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002648 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002649 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002650 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002651 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002652 "error removing layer=%p (%s)", l.get(), strerror(-err));
2653 }
2654 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002655}
2656
Mathias Agopianb60314a2012-04-10 22:09:54 -07002657// ---------------------------------------------------------------------------
2658
Andy McFadden13a082e2012-08-24 10:16:42 -07002659void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002660 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002661 Vector<ComposerState> state;
2662 Vector<DisplayState> displays;
2663 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002664 d.what = DisplayState::eDisplayProjectionChanged |
2665 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002666 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002667 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002668 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002669 d.frame.makeInvalid();
2670 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002671 d.width = 0;
2672 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002673 displays.add(d);
2674 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002675 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002676
2677 const nsecs_t period =
2678 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2679 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002680}
2681
2682void SurfaceFlinger::initializeDisplays() {
2683 class MessageScreenInitialized : public MessageBase {
2684 SurfaceFlinger* flinger;
2685 public:
2686 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2687 virtual bool handler() {
2688 flinger->onInitializeDisplays();
2689 return true;
2690 }
2691 };
2692 sp<MessageBase> msg = new MessageScreenInitialized(this);
2693 postMessageAsync(msg); // we may be called from main thread, use async message
2694}
2695
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002696void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2697 int mode) {
2698 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2699 this);
2700 int32_t type = hw->getDisplayType();
2701 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002702
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002703 if (mode == currentMode) {
2704 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002705 return;
2706 }
2707
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002708 hw->setPowerMode(mode);
2709 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2710 ALOGW("Trying to set power mode for virtual display");
2711 return;
2712 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002713
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002714 if (currentMode == HWC_POWER_MODE_OFF) {
2715 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002716 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2717 // FIXME: eventthread only knows about the main display right now
2718 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002719 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002720 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002721
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002722 mVisibleRegionsDirty = true;
2723 repaintEverything();
2724 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002725 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002726 disableHardwareVsync(true); // also cancels any in-progress resync
2727
Mathias Agopiancde87a32012-09-13 14:09:01 -07002728 // FIXME: eventthread only knows about the main display right now
2729 mEventThread->onScreenReleased();
2730 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002731
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002732 getHwComposer().setPowerMode(type, mode);
2733 mVisibleRegionsDirty = true;
2734 // from this point on, SF will stop drawing on this display
2735 } else {
2736 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002737 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002738}
2739
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002740void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2741 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002742 SurfaceFlinger& mFlinger;
2743 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002744 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002745 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002746 MessageSetPowerMode(SurfaceFlinger& flinger,
2747 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2748 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002749 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002750 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002751 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002752 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002753 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002754 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002755 ALOGW("Attempt to set power mode = %d for virtual display",
2756 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002757 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002758 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002759 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002760 return true;
2761 }
2762 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002763 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002764 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002765}
2766
2767// ---------------------------------------------------------------------------
2768
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002769status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2770{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002771 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002772
Mathias Agopianbd115332013-04-18 16:41:04 -07002773 IPCThreadState* ipc = IPCThreadState::self();
2774 const int pid = ipc->getCallingPid();
2775 const int uid = ipc->getCallingUid();
2776 if ((uid != AID_SHELL) &&
2777 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002778 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002779 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002780 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002781 // Try to get the main lock, but don't insist if we can't
2782 // (this would indicate SF is stuck, but we want to be able to
2783 // print something in dumpsys).
2784 int retry = 3;
2785 while (mStateLock.tryLock()<0 && --retry>=0) {
2786 usleep(1000000);
2787 }
2788 const bool locked(retry >= 0);
2789 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002790 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002791 "SurfaceFlinger appears to be unresponsive, "
2792 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002793 }
2794
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002795 bool dumpAll = true;
2796 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002797 size_t numArgs = args.size();
2798 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002799 if ((index < numArgs) &&
2800 (args[index] == String16("--list"))) {
2801 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002802 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002803 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002804 }
2805
2806 if ((index < numArgs) &&
2807 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002808 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002809 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002810 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002811 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002812
2813 if ((index < numArgs) &&
2814 (args[index] == String16("--latency-clear"))) {
2815 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002816 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002817 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002818 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002819
2820 if ((index < numArgs) &&
2821 (args[index] == String16("--dispsync"))) {
2822 index++;
2823 mPrimaryDispSync.dump(result);
2824 dumpAll = false;
2825 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002826 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002827
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002828 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002829 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002830 }
2831
Mathias Agopian9795c422009-08-26 16:36:26 -07002832 if (locked) {
2833 mStateLock.unlock();
2834 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002835 }
2836 write(fd, result.string(), result.size());
2837 return NO_ERROR;
2838}
2839
Dan Stozac7014012014-02-14 15:03:43 -08002840void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2841 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002842{
2843 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2844 const size_t count = currentLayers.size();
2845 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002846 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002847 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002848 }
2849}
2850
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002851void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002852 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002853{
2854 String8 name;
2855 if (index < args.size()) {
2856 name = String8(args[index]);
2857 index++;
2858 }
2859
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002860 const nsecs_t period =
2861 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002862 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002863
2864 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002865 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002866 } else {
2867 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2868 const size_t count = currentLayers.size();
2869 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002870 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002871 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002872 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002873 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002874 }
2875 }
2876}
2877
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002878void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002879 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002880{
2881 String8 name;
2882 if (index < args.size()) {
2883 name = String8(args[index]);
2884 index++;
2885 }
2886
2887 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2888 const size_t count = currentLayers.size();
2889 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002890 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002891 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002892 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002893 }
2894 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002895
Svetoslavd85084b2014-03-20 10:28:31 -07002896 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002897}
2898
Jamie Gennis6547ff42013-07-16 20:12:42 -07002899// This should only be called from the main thread. Otherwise it would need
2900// the lock and should use mCurrentState rather than mDrawingState.
2901void SurfaceFlinger::logFrameStats() {
2902 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2903 const size_t count = drawingLayers.size();
2904 for (size_t i=0 ; i<count ; i++) {
2905 const sp<Layer>& layer(drawingLayers[i]);
2906 layer->logFrameStats();
2907 }
2908
2909 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2910}
2911
Andy McFadden4803b742012-09-24 19:07:20 -07002912/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2913{
2914 static const char* config =
2915 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002916#ifdef HAS_CONTEXT_PRIORITY
2917 " HAS_CONTEXT_PRIORITY"
2918#endif
2919#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2920 " NEVER_DEFAULT_TO_ASYNC_MODE"
2921#endif
2922#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2923 " TARGET_DISABLE_TRIPLE_BUFFERING"
2924#endif
2925 "]";
2926 result.append(config);
2927}
2928
Mathias Agopian74d211a2013-04-22 16:55:35 +02002929void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2930 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002931{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002932 bool colorize = false;
2933 if (index < args.size()
2934 && (args[index] == String16("--color"))) {
2935 colorize = true;
2936 index++;
2937 }
2938
2939 Colorizer colorizer(colorize);
2940
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002941 // figure out if we're stuck somewhere
2942 const nsecs_t now = systemTime();
2943 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2944 const nsecs_t inTransaction(mDebugInTransaction);
2945 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2946 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2947
2948 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002949 * Dump library configuration.
2950 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002951
2952 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002953 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002954 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002955 appendSfConfigString(result);
2956 appendUiConfigString(result);
2957 appendGuiConfigString(result);
2958 result.append("\n");
2959
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002960 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002961 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002962 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002963 result.append(SyncFeatures::getInstance().toString());
2964 result.append("\n");
2965
Andy McFadden41d67d72014-04-25 16:58:34 -07002966 colorizer.bold(result);
2967 result.append("DispSync configuration: ");
2968 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002969 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2970 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002971 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2972 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2973 result.append("\n");
2974
Andy McFadden4803b742012-09-24 19:07:20 -07002975 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002976 * Dump the visible layer list
2977 */
2978 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2979 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002980 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002981 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002982 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002983 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002984 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002985 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002986 }
2987
2988 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002989 * Dump Display state
2990 */
2991
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002992 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002993 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002994 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002995 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2996 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002997 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002998 }
2999
3000 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003001 * Dump SurfaceFlinger global state
3002 */
3003
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003004 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02003005 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003006 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003007
Mathias Agopian888c8222012-08-04 21:10:38 -07003008 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07003009 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07003010
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003011 colorizer.bold(result);
3012 result.appendFormat("EGL implementation : %s\n",
3013 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
3014 colorizer.reset(result);
3015 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07003016 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07003017
Mathias Agopian875d8e12013-06-07 15:35:48 -07003018 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003019
Mathias Agopian42977342012-08-05 00:40:46 -07003020 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07003021 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
3022 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02003023 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003024 " last eglSwapBuffers() time: %f us\n"
3025 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08003026 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003027 " refresh-rate : %f fps\n"
3028 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07003029 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07003030 " gpu_to_cpu_unsupported : %d\n"
3031 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003032 mLastSwapBufferTime/1000.0,
3033 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08003034 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07003035 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
3036 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07003037 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07003038 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003039
Mathias Agopian74d211a2013-04-22 16:55:35 +02003040 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003041 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003042
Mathias Agopian74d211a2013-04-22 16:55:35 +02003043 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003044 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003045
3046 /*
3047 * VSYNC state
3048 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02003049 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003050
3051 /*
3052 * Dump HWComposer state
3053 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003054 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02003055 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003056 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02003057 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003058 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07003059 (mDebugDisableHWC || mDebugRegion || mDaltonize
3060 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02003061 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003062
3063 /*
3064 * Dump gralloc state
3065 */
3066 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
3067 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003068}
3069
Mathias Agopian13127d82013-03-05 17:47:11 -08003070const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07003071SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07003072 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07003073 wp<IBinder> dpy;
3074 for (size_t i=0 ; i<mDisplays.size() ; i++) {
3075 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
3076 dpy = mDisplays.keyAt(i);
3077 break;
3078 }
3079 }
3080 if (dpy == NULL) {
3081 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
3082 // Just use the primary display so we have something to return
3083 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
3084 }
3085 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07003086}
3087
Keun young Park63f165f2012-08-31 10:53:36 -07003088bool SurfaceFlinger::startDdmConnection()
3089{
3090 void* libddmconnection_dso =
3091 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
3092 if (!libddmconnection_dso) {
3093 return false;
3094 }
3095 void (*DdmConnection_start)(const char* name);
3096 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07003097 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07003098 if (!DdmConnection_start) {
3099 dlclose(libddmconnection_dso);
3100 return false;
3101 }
3102 (*DdmConnection_start)(getServiceName());
3103 return true;
3104}
3105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003106status_t SurfaceFlinger::onTransact(
3107 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3108{
3109 switch (code) {
3110 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07003111 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07003112 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003113 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07003114 case CLEAR_ANIMATION_FRAME_STATS:
3115 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07003116 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003117 {
3118 // codes that require permission check
3119 IPCThreadState* ipc = IPCThreadState::self();
3120 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07003121 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07003122 if ((uid != AID_GRAPHICS) &&
3123 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00003124 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07003125 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
3126 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003127 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003128 break;
3129 }
3130 case CAPTURE_SCREEN:
3131 {
3132 // codes that require permission check
3133 IPCThreadState* ipc = IPCThreadState::self();
3134 const int pid = ipc->getCallingPid();
3135 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07003136 if ((uid != AID_GRAPHICS) &&
3137 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00003138 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003139 "can't read framebuffer pid=%d, uid=%d", pid, uid);
3140 return PERMISSION_DENIED;
3141 }
3142 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003143 }
3144 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003145
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003146 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
3147 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07003148 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08003149 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07003150 IPCThreadState* ipc = IPCThreadState::self();
3151 const int pid = ipc->getCallingPid();
3152 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00003153 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07003154 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003155 return PERMISSION_DENIED;
3156 }
3157 int n;
3158 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07003159 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07003160 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003161 return NO_ERROR;
3162 case 1002: // SHOW_UPDATES
3163 n = data.readInt32();
3164 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07003165 invalidateHwcGeometry();
3166 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003167 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003168 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07003169 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003170 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003171 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003172 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07003173 setTransactionFlags(
3174 eTransactionNeeded|
3175 eDisplayTransactionNeeded|
3176 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003177 return NO_ERROR;
3178 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08003179 case 1006:{ // send empty update
3180 signalRefresh();
3181 return NO_ERROR;
3182 }
Mathias Agopian53331da2011-08-22 21:44:41 -07003183 case 1008: // toggle use of hw composer
3184 n = data.readInt32();
3185 mDebugDisableHWC = n ? 1 : 0;
3186 invalidateHwcGeometry();
3187 repaintEverything();
3188 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07003189 case 1009: // toggle use of transform hint
3190 n = data.readInt32();
3191 mDebugDisableTransformHint = n ? 1 : 0;
3192 invalidateHwcGeometry();
3193 repaintEverything();
3194 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003195 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07003196 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003197 reply->writeInt32(0);
3198 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07003199 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08003200 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003201 return NO_ERROR;
3202 case 1013: {
3203 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07003204 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
3205 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07003206 return NO_ERROR;
3207 }
3208 case 1014: {
3209 // daltonize
3210 n = data.readInt32();
3211 switch (n % 10) {
3212 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
3213 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
3214 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
3215 }
3216 if (n >= 10) {
3217 mDaltonizer.setMode(Daltonizer::correction);
3218 } else {
3219 mDaltonizer.setMode(Daltonizer::simulation);
3220 }
3221 mDaltonize = n > 0;
3222 invalidateHwcGeometry();
3223 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07003224 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003225 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003226 case 1015: {
3227 // apply a color matrix
3228 n = data.readInt32();
3229 mHasColorMatrix = n ? 1 : 0;
3230 if (n) {
3231 // color matrix is sent as mat3 matrix followed by vec3
3232 // offset, then packed into a mat4 where the last row is
3233 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07003234 for (size_t i = 0 ; i < 4; i++) {
3235 for (size_t j = 0; j < 4; j++) {
3236 mColorMatrix[i][j] = data.readFloat();
3237 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003238 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003239 } else {
3240 mColorMatrix = mat4();
3241 }
3242 invalidateHwcGeometry();
3243 repaintEverything();
3244 return NO_ERROR;
3245 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003246 // This is an experimental interface
3247 // Needs to be shifted to proper binder interface when we productize
3248 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07003249 n = data.readInt32();
3250 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003251 return NO_ERROR;
3252 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003253 }
3254 }
3255 return err;
3256}
3257
Mathias Agopian53331da2011-08-22 21:44:41 -07003258void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07003259 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08003260 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07003261}
3262
Mathias Agopian59119e62010-10-11 12:37:43 -07003263// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003264// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07003265// ---------------------------------------------------------------------------
3266
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003267/* The code below is here to handle b/8734824
3268 *
3269 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07003270 * from the surfaceflinger thread to the calling binder thread, where they
3271 * are executed. This allows the calling thread in the calling process to be
3272 * reused and not depend on having "enough" binder threads to handle the
3273 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003274 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003275class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07003276 /* Parts of GraphicProducerWrapper are run on two different threads,
3277 * communicating by sending messages via Looper but also by shared member
3278 * data. Coherence maintenance is subtle and in places implicit (ugh).
3279 *
3280 * Don't rely on Looper's sendMessage/handleMessage providing
3281 * release/acquire semantics for any data not actually in the Message.
3282 * Data going from surfaceflinger to binder threads needs to be
3283 * synchronized explicitly.
3284 *
3285 * Barrier open/wait do provide release/acquire semantics. This provides
3286 * implicit synchronization for data coming back from binder to
3287 * surfaceflinger threads.
3288 */
3289
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003290 sp<IGraphicBufferProducer> impl;
3291 sp<Looper> looper;
3292 status_t result;
3293 bool exitPending;
3294 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07003295 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003296 uint32_t code;
3297 Parcel const* data;
3298 Parcel* reply;
3299
3300 enum {
3301 MSG_API_CALL,
3302 MSG_EXIT
3303 };
3304
3305 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003306 * Called on surfaceflinger thread. This is called by our "fake"
3307 * BpGraphicBufferProducer. We package the data and reply Parcel and
3308 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003309 */
3310 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08003311 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003312 this->code = code;
3313 this->data = &data;
3314 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003315 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07003316 // if we've exited, we run the message synchronously right here.
3317 // note (JH): as far as I can tell from looking at the code, this
3318 // never actually happens. if it does, i'm not sure if it happens
3319 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003320 handleMessage(Message(MSG_API_CALL));
3321 } else {
3322 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07003323 // Prevent stores to this->{code, data, reply} from being
3324 // reordered later than the construction of Message.
3325 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003326 looper->sendMessage(this, Message(MSG_API_CALL));
3327 barrier.wait();
3328 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08003329 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003330 }
3331
3332 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003333 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003334 * call the real BpGraphicBufferProducer.
3335 */
3336 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07003337 int what = message.what;
3338 // Prevent reads below from happening before the read from Message
3339 atomic_thread_fence(memory_order_acquire);
3340 if (what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08003341 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003342 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07003343 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003344 exitRequested = true;
3345 }
3346 }
3347
3348public:
Jesse Hallb154c422014-07-13 12:47:02 -07003349 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
3350 : impl(impl),
3351 looper(new Looper(true)),
3352 exitPending(false),
3353 exitRequested(false)
3354 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003355
Jesse Hallb154c422014-07-13 12:47:02 -07003356 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003357 status_t waitForResponse() {
3358 do {
3359 looper->pollOnce(-1);
3360 } while (!exitRequested);
3361 return result;
3362 }
3363
Jesse Hallb154c422014-07-13 12:47:02 -07003364 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003365 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07003366 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003367 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07003368 // Ensure this->result is visible to the binder thread before it
3369 // handles the message.
3370 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003371 looper->sendMessage(this, Message(MSG_EXIT));
3372 }
3373};
3374
3375
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003376status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
3377 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003378 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003379 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003380 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003381
3382 if (CC_UNLIKELY(display == 0))
3383 return BAD_VALUE;
3384
3385 if (CC_UNLIKELY(producer == 0))
3386 return BAD_VALUE;
3387
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003388 // if we have secure windows on this display, never allow the screen capture
3389 // unless the producer interface is local (i.e.: we can take a screenshot for
3390 // ourselves).
3391 if (!producer->asBinder()->localBinder()) {
3392 Mutex::Autolock _l(mStateLock);
3393 sp<const DisplayDevice> hw(getDisplayDevice(display));
3394 if (hw->getSecureLayerVisible()) {
3395 ALOGW("FB is protected: PERMISSION_DENIED");
3396 return PERMISSION_DENIED;
3397 }
3398 }
3399
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003400 // Convert to surfaceflinger's internal rotation type.
3401 Transform::orientation_flags rotationFlags;
3402 switch (rotation) {
3403 case ISurfaceComposer::eRotateNone:
3404 rotationFlags = Transform::ROT_0;
3405 break;
3406 case ISurfaceComposer::eRotate90:
3407 rotationFlags = Transform::ROT_90;
3408 break;
3409 case ISurfaceComposer::eRotate180:
3410 rotationFlags = Transform::ROT_180;
3411 break;
3412 case ISurfaceComposer::eRotate270:
3413 rotationFlags = Transform::ROT_270;
3414 break;
3415 default:
3416 rotationFlags = Transform::ROT_0;
3417 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3418 break;
3419 }
3420
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003421 class MessageCaptureScreen : public MessageBase {
3422 SurfaceFlinger* flinger;
3423 sp<IBinder> display;
3424 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003425 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003426 uint32_t reqWidth, reqHeight;
3427 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003428 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003429 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003430 status_t result;
3431 public:
3432 MessageCaptureScreen(SurfaceFlinger* flinger,
3433 const sp<IBinder>& display,
3434 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003435 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003436 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003437 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003438 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003439 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003440 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003441 useIdentityTransform(useIdentityTransform),
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003442 rotation(rotation),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003443 result(PERMISSION_DENIED)
3444 {
3445 }
3446 status_t getResult() const {
3447 return result;
3448 }
3449 virtual bool handler() {
3450 Mutex::Autolock _l(flinger->mStateLock);
3451 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003452 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003453 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003454 useIdentityTransform, rotation);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003455 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003456 return true;
3457 }
3458 };
3459
Mathias Agopian9eb1f052013-04-10 16:27:17 -07003460 // make sure to process transactions before screenshots -- a transaction
3461 // might already be pending but scheduled for VSYNC; this guarantees we
3462 // will handle it before the screenshot. When VSYNC finally arrives
3463 // the scheduled transaction will be a no-op. If no transactions are
3464 // scheduled at this time, this will end-up being a no-op as well.
3465 mEventQueue.invalidateTransactionNow();
3466
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003467 // this creates a "fake" BBinder which will serve as a "fake" remote
3468 // binder to receive the marshaled calls and forward them to the
3469 // real remote (a BpGraphicBufferProducer)
3470 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3471
3472 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3473 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003474 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003475 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003476 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003477 useIdentityTransform, rotationFlags);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003478
3479 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003480 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003481 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003482 }
3483 return res;
3484}
3485
Mathias Agopian180f10d2013-04-10 22:55:41 -07003486
3487void SurfaceFlinger::renderScreenImplLocked(
3488 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003489 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003490 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003491 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003492{
3493 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003494 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003495
3496 // get screen geometry
3497 const uint32_t hw_w = hw->getWidth();
3498 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003499 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
3500
Dan Stozac1879002014-05-22 15:59:05 -07003501 // if a default or invalid sourceCrop is passed in, set reasonable values
3502 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3503 !sourceCrop.isValid()) {
3504 sourceCrop.setLeftTop(Point(0, 0));
3505 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3506 }
3507
3508 // ensure that sourceCrop is inside screen
3509 if (sourceCrop.left < 0) {
3510 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3511 }
Dan Stozabe31f442014-06-11 11:20:54 -07003512 if (sourceCrop.right > hw_w) {
3513 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003514 }
3515 if (sourceCrop.top < 0) {
3516 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3517 }
Dan Stozabe31f442014-06-11 11:20:54 -07003518 if (sourceCrop.bottom > hw_h) {
3519 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003520 }
3521
Mathias Agopian180f10d2013-04-10 22:55:41 -07003522 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003523 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003524
3525 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003526 engine.setViewportAndProjection(
3527 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003528 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003529
3530 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003531 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003532
3533 const LayerVector& layers( mDrawingState.layersSortedByZ );
3534 const size_t count = layers.size();
3535 for (size_t i=0 ; i<count ; ++i) {
3536 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003537 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003538 if (state.layerStack == hw->getLayerStack()) {
3539 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07003540#ifdef QCOM_BSP
3541 // dont render the secure Display Layer
3542 if(layer->isSecureDisplay()) {
3543 continue;
3544 }
3545#endif
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07003546#ifdef QCOM_BSP
3547 int dispType = hw->getDisplayType();
3548 // Dont let ext_only and extended_mode to be captured
3549 // If not, we would see incorrect image during rotatoin
3550 // on primary
3551 if (layer->isVisible() &&
3552 not (!dispType && (layer->isExtOnly() ||
3553 (isExtendedMode() && layer->isYuvLayer())))) {
3554#else
Mathias Agopian180f10d2013-04-10 22:55:41 -07003555 if (layer->isVisible()) {
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07003556#endif
Mathias Agopian180f10d2013-04-10 22:55:41 -07003557 if (filtering) layer->setFiltering(true);
Ramakant Singhf9af0e32014-01-07 14:17:52 +05303558 if(!layer->isProtected())
3559 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003560 if (filtering) layer->setFiltering(false);
3561 }
3562 }
3563 }
3564 }
3565
3566 // compositionComplete is needed for older driver
3567 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003568 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003569}
3570
3571
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003572status_t SurfaceFlinger::captureScreenImplLocked(
3573 const sp<const DisplayDevice>& hw,
3574 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003575 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003576 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003577 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003578{
3579 ATRACE_CALL();
3580
Mathias Agopian180f10d2013-04-10 22:55:41 -07003581 // get screen geometry
3582 const uint32_t hw_w = hw->getWidth();
3583 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003584
Mathias Agopian180f10d2013-04-10 22:55:41 -07003585 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3586 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3587 reqWidth, reqHeight, hw_w, hw_h);
3588 return BAD_VALUE;
3589 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003590
Mathias Agopian180f10d2013-04-10 22:55:41 -07003591 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3592 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3593
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003594 // create a surface (because we're a producer, and we need to
3595 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003596 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003597 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003598
3599 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003600 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003601 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3602 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003603
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003604 int err = 0;
3605 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003606 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003607 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3608 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003609
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003610 if (err == NO_ERROR) {
3611 ANativeWindowBuffer* buffer;
3612 /* TODO: Once we have the sync framework everywhere this can use
3613 * server-side waits on the fence that dequeueBuffer returns.
3614 */
3615 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3616 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003617 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003618 // create an EGLImage from the buffer so we can later
3619 // turn it into a texture
3620 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3621 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3622 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003623 // this binds the given EGLImage as a framebuffer for the
3624 // duration of this scope.
3625 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3626 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003627 // this will in fact render into our dequeued buffer
3628 // via an FBO, which means we didn't have to create
3629 // an EGLSurface and therefore we're not
3630 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003631 renderScreenImplLocked(
3632 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3633 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003634
Riley Andrews86639902014-08-15 12:27:24 -07003635 // Attempt to create a sync khr object that can produce a sync point. If that
3636 // isn't available, create a non-dupable sync object in the fallback path and
3637 // wait on it directly.
3638 EGLSyncKHR sync;
3639 if (!DEBUG_SCREENSHOTS) {
3640 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Andy McFadden2d8d1202013-10-09 16:38:02 -07003641 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003642 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003643 }
Riley Andrews86639902014-08-15 12:27:24 -07003644 if (sync != EGL_NO_SYNC_KHR) {
3645 // get the sync fd
3646 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3647 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3648 ALOGW("captureScreen: failed to dup sync khr object");
3649 syncFd = -1;
3650 }
3651 eglDestroySyncKHR(mEGLDisplay, sync);
3652 } else {
3653 // fallback path
3654 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3655 if (sync != EGL_NO_SYNC_KHR) {
3656 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3657 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3658 EGLint eglErr = eglGetError();
3659 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3660 ALOGW("captureScreen: fence wait timed out");
3661 } else {
3662 ALOGW_IF(eglErr != EGL_SUCCESS,
3663 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3664 }
3665 eglDestroySyncKHR(mEGLDisplay, sync);
3666 } else {
3667 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3668 }
3669 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003670 if (DEBUG_SCREENSHOTS) {
3671 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3672 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3673 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3674 hw, minLayerZ, maxLayerZ);
3675 delete [] pixels;
3676 }
3677
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003678 } else {
3679 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3680 result = INVALID_OPERATION;
3681 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003682 // destroy our image
3683 eglDestroyImageKHR(mEGLDisplay, image);
3684 } else {
3685 result = BAD_VALUE;
3686 }
Jesse Hallafe2b1f2014-10-21 11:09:17 -07003687 // queueBuffer takes ownership of syncFd
Riley Andrews86639902014-08-15 12:27:24 -07003688 window->queueBuffer(window, buffer, syncFd);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003689 }
3690 } else {
3691 result = BAD_VALUE;
3692 }
3693 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3694 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003695
Mathias Agopian74c40c02010-09-29 13:02:36 -07003696 return result;
3697}
3698
Mathias Agopiand5556842013-09-19 17:08:37 -07003699void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3700 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003701 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003702 for (size_t y=0 ; y<h ; y++) {
3703 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3704 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003705 if (p[x] != 0xFF000000) return;
3706 }
3707 }
3708 ALOGE("*** we just took a black screenshot ***\n"
3709 "requested minz=%d, maxz=%d, layerStack=%d",
3710 minLayerZ, maxLayerZ, hw->getLayerStack());
3711 const LayerVector& layers( mDrawingState.layersSortedByZ );
3712 const size_t count = layers.size();
3713 for (size_t i=0 ; i<count ; ++i) {
3714 const sp<Layer>& layer(layers[i]);
3715 const Layer::State& state(layer->getDrawingState());
3716 const bool visible = (state.layerStack == hw->getLayerStack())
3717 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3718 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003719 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003720 visible ? '+' : '-',
3721 i, layer->getName().string(), state.layerStack, state.z,
3722 layer->isVisible(), state.flags, state.alpha);
3723 }
3724 }
3725}
3726
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003727// ---------------------------------------------------------------------------
3728
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003729SurfaceFlinger::LayerVector::LayerVector() {
3730}
3731
3732SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003733 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003734}
3735
3736int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3737 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003738{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003739 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003740 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3741 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003742
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003743 uint32_t ls = l->getCurrentState().layerStack;
3744 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003745 if (ls != rs)
3746 return ls - rs;
3747
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003748 uint32_t lz = l->getCurrentState().z;
3749 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003750 if (lz != rz)
3751 return lz - rz;
3752
3753 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003754}
3755
3756// ---------------------------------------------------------------------------
3757
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003758SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Michael Lentine47e45402014-07-18 15:34:25 -07003759 : type(DisplayDevice::DISPLAY_ID_INVALID), width(0), height(0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003760}
3761
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003762SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Michael Lentine47e45402014-07-18 15:34:25 -07003763 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0), width(0), height(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003764 viewport.makeInvalid();
3765 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003766}
3767
3768// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003769
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003770}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003771
3772
3773#if defined(__gl_h_)
3774#error "don't include gl/gl.h in this file"
3775#endif
3776
3777#if defined(__gl2_h_)
3778#error "don't include gl2/gl2.h in this file"
3779#endif