blob: 2da1457521e97d24a86c2837e7a0e15c1b6d331b [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
Arun Kumar K.R6c6d3482014-08-21 16:49:34 -0700281void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type,
282 bool secure) {
Jesse Hall692c7232012-11-08 15:41:56 -0800283 ALOGW_IF(mBuiltinDisplays[type],
284 "Overwriting display token for display type %d", type);
285 mBuiltinDisplays[type] = new BBinder();
286 DisplayDeviceState info(type);
Arun Kumar K.R6c6d3482014-08-21 16:49:34 -0700287 info.isSecure = secure;
Jesse Hall692c7232012-11-08 15:41:56 -0800288 mCurrentState.displays.add(mBuiltinDisplays[type], info);
289}
290
Mathias Agopiane57f2922012-08-09 16:29:12 -0700291sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700292 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700293 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
294 return NULL;
295 }
Jesse Hall692c7232012-11-08 15:41:56 -0800296 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700297}
298
Jamie Gennis9a78c902011-01-12 18:30:40 -0800299sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
300{
301 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
302 return gba;
303}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700304
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305void SurfaceFlinger::bootFinished()
306{
307 const nsecs_t now = systemTime();
308 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000309 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700310 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700311
312 // wait patiently for the window manager death
313 const String16 name("window");
314 sp<IBinder> window(defaultServiceManager()->getService(name));
315 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700316 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700317 }
318
319 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700320 // formerly we would just kill the process, but we now ask it to exit so it
321 // can choose where to stop the animation.
322 property_set("service.bootanim.exit", "1");
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
Ramkumar Radhakrishnan67301c32014-10-29 14:38:22 -0700418 /* Set the mask bit of the sigset to block the SIGPIPE signal */
419 sigset_t sigMask;
420 sigemptyset (&sigMask);
421 sigaddset(&sigMask, SIGPIPE);
422 sigprocmask(SIG_BLOCK, &sigMask, NULL);
423
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700424 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700425 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
426 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700427
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700428 // Initialize the H/W composer object. There may or may not be an
429 // actual hardware composer underneath.
430 mHwc = new HWComposer(this,
431 *static_cast<HWComposer::EventHandler *>(this));
432
Mathias Agopian875d8e12013-06-07 15:35:48 -0700433 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800434 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700435
436 // retrieve the EGL context that was selected/created
437 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700438
Mathias Agopianda27af92012-09-13 18:17:13 -0700439 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
440 "couldn't create EGLContext");
441
Mathias Agopiancde87a32012-09-13 14:09:01 -0700442 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700443 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700444 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700445 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700446 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Arun Kumar K.R6c6d3482014-08-21 16:49:34 -0700447 // query from hwc if the non-virtual display is secure.
448 bool isSecure = mHwc->isSecure(i);;
449 createBuiltinDisplayLocked(type, isSecure);
Jesse Hall692c7232012-11-08 15:41:56 -0800450 wp<IBinder> token = mBuiltinDisplays[i];
451
Dan Stozab9b08832014-03-13 11:55:57 -0700452 sp<IGraphicBufferProducer> producer;
453 sp<IGraphicBufferConsumer> consumer;
454 BufferQueue::createBufferQueue(&producer, &consumer,
455 new GraphicBufferAlloc());
456
457 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
458 consumer);
Jesse Hall19e87292013-12-23 21:02:15 -0800459 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700460 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800461 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Dan Stozab9b08832014-03-13 11:55:57 -0700462 fbs, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -0800463 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700464 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700465 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700466 // for displays other than the main display, so we always
467 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800468 ALOGD("marking display %zu as acquired/unblanked", i);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700469 hw->setPowerMode(HWC_POWER_MODE_NORMAL);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700470 }
471 mDisplays.add(token, hw);
472 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700473 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700474
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700475 // make the GLContext current so that we can create textures when creating Layers
476 // (which may happens before we render something)
477 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
478
Mathias Agopian028508c2012-07-25 21:12:12 -0700479 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700480 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700481 vsyncPhaseOffsetNs, true, "app");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700482 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700483 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700484 sfVsyncPhaseOffsetNs, true, "sf");
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700485 mSFEventThread = new EventThread(sfVsyncSrc);
486 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700487
Jamie Gennisd1700752013-10-14 12:22:52 -0700488 mEventControlThread = new EventControlThread(this);
489 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
490
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700491 // set a fake vsync period if there is no HWComposer
492 if (mHwc->initCheck() != NO_ERROR) {
493 mPrimaryDispSync.setPeriod(16666667);
494 }
495
Mathias Agopian92a979a2012-08-02 18:32:23 -0700496 // initialize our drawing state
497 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700498
Andy McFadden13a082e2012-08-24 10:16:42 -0700499 // set initial conditions (e.g. unblank default device)
500 initializeDisplays();
501
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700502 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700503 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800504}
505
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700506int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700507 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700508 type : mHwc->allocateDisplayId();
509}
510
Mathias Agopiana67e4182012-06-19 17:26:12 -0700511void SurfaceFlinger::startBootAnim() {
512 // start boot animation
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -0700513 mBootFinished = false;
Mathias Agopiana67e4182012-06-19 17:26:12 -0700514 property_set("service.bootanim.exit", "0");
515 property_set("ctl.start", "bootanim");
516}
517
Mathias Agopian875d8e12013-06-07 15:35:48 -0700518size_t SurfaceFlinger::getMaxTextureSize() const {
519 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700520}
521
Mathias Agopian875d8e12013-06-07 15:35:48 -0700522size_t SurfaceFlinger::getMaxViewportDims() const {
523 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700524}
525
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800526// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800527
Jamie Gennis582270d2011-08-17 18:19:00 -0700528bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800529 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800530 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800531 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700532 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800533}
534
Dan Stoza7f7da322014-05-02 15:26:25 -0700535status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
536 Vector<DisplayInfo>* configs) {
Tatenda Chipeperekwae9d5fa82014-10-29 16:47:19 -0700537 if ((configs == NULL) || (display.get() == NULL)) {
Dan Stoza7f7da322014-05-02 15:26:25 -0700538 return BAD_VALUE;
539 }
540
Naseer Ahmed7aa0c472014-11-03 14:49:23 -0500541 if (!display.get())
542 return NAME_NOT_FOUND;
543
Jesse Hall692c7232012-11-08 15:41:56 -0800544 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700545 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800546 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700547 type = i;
548 break;
549 }
550 }
551
552 if (type < 0) {
553 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700554 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700555
Mathias Agopian8b736f12012-08-13 17:54:26 -0700556 // TODO: Not sure if display density should handled by SF any longer
557 class Density {
558 static int getDensityFromProperty(char const* propName) {
559 char property[PROPERTY_VALUE_MAX];
560 int density = 0;
561 if (property_get(propName, property, NULL) > 0) {
562 density = atoi(property);
563 }
564 return density;
565 }
566 public:
567 static int getEmuDensity() {
568 return getDensityFromProperty("qemu.sf.lcd_density"); }
569 static int getBuildDensity() {
570 return getDensityFromProperty("ro.sf.lcd_density"); }
571 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700572
Dan Stoza7f7da322014-05-02 15:26:25 -0700573 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700574
Dan Stoza7f7da322014-05-02 15:26:25 -0700575 const Vector<HWComposer::DisplayConfig>& hwConfigs =
576 getHwComposer().getConfigs(type);
577 for (size_t c = 0; c < hwConfigs.size(); ++c) {
578 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
579 DisplayInfo info = DisplayInfo();
580
581 float xdpi = hwConfig.xdpi;
582 float ydpi = hwConfig.ydpi;
583
584 if (type == DisplayDevice::DISPLAY_PRIMARY) {
585 // The density of the device is provided by a build property
586 float density = Density::getBuildDensity() / 160.0f;
587 if (density == 0) {
588 // the build doesn't provide a density -- this is wrong!
589 // use xdpi instead
590 ALOGE("ro.sf.lcd_density must be defined as a build property");
591 density = xdpi / 160.0f;
592 }
593 if (Density::getEmuDensity()) {
594 // if "qemu.sf.lcd_density" is specified, it overrides everything
595 xdpi = ydpi = density = Density::getEmuDensity();
596 density /= 160.0f;
597 }
598 info.density = density;
599
600 // TODO: this needs to go away (currently needed only by webkit)
601 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
602 info.orientation = hw->getOrientation();
603 } else {
604 // TODO: where should this value come from?
605 static const int TV_DENSITY = 213;
606 info.density = TV_DENSITY / 160.0f;
607 info.orientation = 0;
608 }
609
610 info.w = hwConfig.width;
611 info.h = hwConfig.height;
612 info.xdpi = xdpi;
613 info.ydpi = ydpi;
614 info.fps = float(1e9 / hwConfig.refresh);
Andy McFadden91b2ca82014-06-13 14:04:23 -0700615 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
616
617 // This is how far in advance a buffer must be queued for
618 // presentation at a given time. If you want a buffer to appear
619 // on the screen at time N, you must submit the buffer before
620 // (N - presentationDeadline).
621 //
622 // Normally it's one full refresh period (to give SF a chance to
623 // latch the buffer), but this can be reduced by configuring a
624 // DispSync offset. Any additional delays introduced by the hardware
625 // composer or panel must be accounted for here.
626 //
627 // We add an additional 1ms to allow for processing time and
628 // differences between the ideal and actual refresh rate.
629 info.presentationDeadline =
630 hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700631
Arun Kumar K.R6c6d3482014-08-21 16:49:34 -0700632 // set secure info based on the hwcConfig
633 info.secure = hwConfig.secure;
Dan Stoza7f7da322014-05-02 15:26:25 -0700634
635 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700636 }
637
Dan Stoza7f7da322014-05-02 15:26:25 -0700638 return NO_ERROR;
639}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700640
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700641status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& display,
642 DisplayStatInfo* stats) {
643 if (stats == NULL) {
644 return BAD_VALUE;
645 }
646
647 // FIXME for now we always return stats for the primary display
648 memset(stats, 0, sizeof(*stats));
649 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
650 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
651 return NO_ERROR;
652}
653
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700654int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
655 return getDisplayDevice(display)->getActiveConfig();
Dan Stoza7f7da322014-05-02 15:26:25 -0700656}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700657
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700658void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
659 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
660 this);
661 int32_t type = hw->getDisplayType();
662 int currentMode = hw->getActiveConfig();
663
664 if (mode == currentMode) {
665 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
666 return;
667 }
668
669 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
670 ALOGW("Trying to set config for virtual display");
671 return;
672 }
673
Tatenda Chipeperekwab801c8a2014-09-22 16:57:24 -0700674 status_t status = getHwComposer().setActiveConfig(type, mode);
675 if (status == NO_ERROR) {
676 hw->setActiveConfig(mode);
677 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700678}
679
680status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
681 class MessageSetActiveConfig: public MessageBase {
682 SurfaceFlinger& mFlinger;
683 sp<IBinder> mDisplay;
684 int mMode;
685 public:
686 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
687 int mode) :
688 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
689 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700690 Vector<DisplayInfo> configs;
691 mFlinger.getDisplayConfigs(mDisplay, &configs);
Jesse Hall78442112014-08-07 22:43:06 -0700692 if (mMode < 0 || mMode >= static_cast<int>(configs.size())) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700693 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700694 mMode, configs.size());
695 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700696 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
697 if (hw == NULL) {
698 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700699 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700700 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
701 ALOGW("Attempt to set active config = %d for virtual display",
702 mMode);
703 } else {
704 mFlinger.setActiveConfigInternal(hw, mMode);
705 }
706 return true;
707 }
708 };
709 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
710 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700711 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700712}
713
Svetoslavd85084b2014-03-20 10:28:31 -0700714status_t SurfaceFlinger::clearAnimationFrameStats() {
715 Mutex::Autolock _l(mStateLock);
716 mAnimFrameTracker.clearStats();
717 return NO_ERROR;
718}
719
720status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
721 Mutex::Autolock _l(mStateLock);
722 mAnimFrameTracker.getStats(outStats);
723 return NO_ERROR;
724}
725
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800726// ----------------------------------------------------------------------------
727
728sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800729 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700730}
731
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800732// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800733
734void SurfaceFlinger::waitForEvent() {
735 mEventQueue.waitMessage();
736}
737
738void SurfaceFlinger::signalTransaction() {
739 mEventQueue.invalidate();
740}
741
742void SurfaceFlinger::signalLayerUpdate() {
743 mEventQueue.invalidate();
744}
745
746void SurfaceFlinger::signalRefresh() {
747 mEventQueue.refresh();
748}
749
750status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800751 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800752 return mEventQueue.postMessage(msg, reltime);
753}
754
755status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800756 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800757 status_t res = mEventQueue.postMessage(msg, reltime);
758 if (res == NO_ERROR) {
759 msg->wait();
760 }
761 return res;
762}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800763
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700764void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700765 do {
766 waitForEvent();
767 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800768}
769
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700770void SurfaceFlinger::enableHardwareVsync() {
771 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700772 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700773 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700774 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
775 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700776 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700777 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700778}
779
Jesse Hall948fe0c2013-10-14 12:56:09 -0700780void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700781 Mutex::Autolock _l(mHWVsyncLock);
782
Jesse Hall948fe0c2013-10-14 12:56:09 -0700783 if (makeAvailable) {
784 mHWVsyncAvailable = true;
785 } else if (!mHWVsyncAvailable) {
786 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
787 return;
788 }
789
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700790 const nsecs_t period =
791 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
792
793 mPrimaryDispSync.reset();
794 mPrimaryDispSync.setPeriod(period);
795
796 if (!mPrimaryHWVsyncEnabled) {
797 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700798 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
799 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700800 mPrimaryHWVsyncEnabled = true;
801 }
802}
803
Jesse Hall948fe0c2013-10-14 12:56:09 -0700804void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700805 Mutex::Autolock _l(mHWVsyncLock);
806 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700807 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
808 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700809 mPrimaryDispSync.endResync();
810 mPrimaryHWVsyncEnabled = false;
811 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700812 if (makeUnavailable) {
813 mHWVsyncAvailable = false;
814 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700815}
816
817void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700818 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700819
Jamie Gennisd1700752013-10-14 12:22:52 -0700820 { // Scope for the lock
821 Mutex::Autolock _l(mHWVsyncLock);
822 if (type == 0 && mPrimaryHWVsyncEnabled) {
823 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700824 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700825 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700826
827 if (needsHwVsync) {
828 enableHardwareVsync();
829 } else {
830 disableHardwareVsync(false);
831 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700832}
833
834void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
835 if (mEventThread == NULL) {
836 // This is a temporary workaround for b/7145521. A non-null pointer
837 // does not mean EventThread has finished initializing, so this
838 // is not a correct fix.
839 ALOGW("WARNING: EventThread not started, ignoring hotplug");
840 return;
841 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700842
Jesse Hall9e663de2013-08-16 14:28:37 -0700843 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700844 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800845 if (connected) {
Arun Kumar K.R6c6d3482014-08-21 16:49:34 -0700846 // query from hwc if the connected display is secure
847 bool secure = mHwc->isSecure(type);;
848 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type, secure);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700849 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800850 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
851 mBuiltinDisplays[type].clear();
Arun Kumar K.R766d5f62013-06-21 19:36:02 -0700852#ifdef QCOM_BSP
853 // if extended_mode is set, and set mVisibleRegionsDirty
854 // as we need to rebuildLayerStack
855 if(isExtendedMode()) {
856 mVisibleRegionsDirty = true;
857 }
858#endif
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700859 }
860 setTransactionFlags(eDisplayTransactionNeeded);
861
Andy McFadden9e9689c2012-10-10 18:17:51 -0700862 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700863 }
Mathias Agopian86303202012-07-24 22:46:10 -0700864}
865
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700866void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700867 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700868 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700869}
870
Mathias Agopian4fec8732012-06-29 14:12:52 -0700871void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800872 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800873 switch (what) {
Dan Stoza6b9454d2014-11-07 16:00:59 -0800874 case MessageQueue::TRANSACTION: {
875 handleMessageTransaction();
876 break;
877 }
878 case MessageQueue::INVALIDATE: {
879 bool refreshNeeded = handleMessageTransaction();
880 refreshNeeded |= handleMessageInvalidate();
Dan Stoza58784442014-12-02 16:58:17 -0800881 refreshNeeded |= mRepaintEverything;
Dan Stoza6b9454d2014-11-07 16:00:59 -0800882 if (refreshNeeded) {
Dan Stoza58784442014-12-02 16:58:17 -0800883 // Signal a refresh if a transaction modified the window state,
884 // a new buffer was latched, or if HWC has requested a full
885 // repaint
Dan Stoza6b9454d2014-11-07 16:00:59 -0800886 signalRefresh();
887 }
888 break;
889 }
890 case MessageQueue::REFRESH: {
891 handleMessageRefresh();
892 break;
893 }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800894 }
895}
896
Dan Stoza6b9454d2014-11-07 16:00:59 -0800897bool SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700898 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700899 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700900 handleTransaction(transactionFlags);
Dan Stoza6b9454d2014-11-07 16:00:59 -0800901 return true;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700902 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800903 return false;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700904}
905
Dan Stoza6b9454d2014-11-07 16:00:59 -0800906bool SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700907 ATRACE_CALL();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800908 return handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700909}
910
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530911#ifdef QCOM_BSP
912/* Compute DirtyRegion, if DR optimization for GPU comp optimization
913 * is ON & and no external device is connected.*/
914void SurfaceFlinger::setUpTiledDr() {
915 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
916 const sp<DisplayDevice>& hw(mDisplays[HWC_DISPLAY_PRIMARY]);
917 mCanUseGpuTileRender = computeTiledDr(hw);
918 }
919}
920#endif
Mathias Agopian4fec8732012-06-29 14:12:52 -0700921void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700922 ATRACE_CALL();
923 preComposition();
924 rebuildLayerStacks();
925 setUpHWComposer();
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530926#ifdef QCOM_BSP
927 setUpTiledDr();
928#endif
Mathias Agopiancd60f992012-08-16 16:28:27 -0700929 doDebugFlashRegions();
930 doComposition();
931 postComposition();
932}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700933
Mathias Agopiancd60f992012-08-16 16:28:27 -0700934void SurfaceFlinger::doDebugFlashRegions()
935{
936 // is debugging enabled
937 if (CC_LIKELY(!mDebugRegion))
938 return;
939
940 const bool repaintEverything = mRepaintEverything;
941 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
942 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530943
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700944 if (hw->isDisplayOn()) {
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530945 const int32_t height = hw->getHeight();
946 RenderEngine& engine(getRenderEngine());
947#ifdef QCOM_BSP
948 // Use Union DR, if it is valid & GPU Tiled DR optimization is ON
949 if(mCanUseGpuTileRender && !mUnionDirtyRect.isEmpty()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700950 // redraw the whole screen
951 doComposeSurfaces(hw, Region(hw->bounds()));
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530952 Region dirtyRegion(mUnionDirtyRect);
953 Rect dr = mUnionDirtyRect;
954 hw->eglSwapPreserved(true);
955 engine.startTileComposition(dr.left, (height-dr.bottom),
956 (dr.right-dr.left),
957 (dr.bottom-dr.top), 1);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700958 // and draw the dirty region
Mathias Agopian3f844832013-08-07 21:24:32 -0700959 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530960 engine.endTileComposition(GL_PRESERVE);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700961 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700962 hw->swapBuffers(getHwComposer());
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +0530963 } else
964#endif
965 {
966 // transform the dirty region into this screen's coordinate
967 // space
968 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
969 if (!dirtyRegion.isEmpty()) {
970 // redraw the whole screen
971 doComposeSurfaces(hw, Region(hw->bounds()));
972 // and draw the dirty region
973#ifdef QCOM_BSP
974 if(mGpuTileRenderEnable)
975 hw->eglSwapPreserved(false);
976#endif
977
978 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
979 hw->compositionComplete();
980 hw->swapBuffers(getHwComposer());
981 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700982 }
983 }
984 }
985
986 postFramebuffer();
987
988 if (mDebugRegion > 1) {
989 usleep(mDebugRegion * 1000);
990 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700991
992 HWComposer& hwc(getHwComposer());
993 if (hwc.initCheck() == NO_ERROR) {
994 status_t err = hwc.prepare();
995 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
996 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700997}
998
999void SurfaceFlinger::preComposition()
1000{
1001 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001002 const LayerVector& layers(mDrawingState.layersSortedByZ);
1003 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -07001004 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001005 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001006 needExtraInvalidate = true;
1007 }
1008 }
1009 if (needExtraInvalidate) {
1010 signalLayerUpdate();
1011 }
1012}
1013
1014void SurfaceFlinger::postComposition()
1015{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001016 const LayerVector& layers(mDrawingState.layersSortedByZ);
1017 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -07001018 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001019 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -07001020 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001021
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001022 const HWComposer& hwc = getHwComposer();
1023 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
1024
1025 if (presentFence->isValid()) {
1026 if (mPrimaryDispSync.addPresentFence(presentFence)) {
1027 enableHardwareVsync();
1028 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -07001029 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001030 }
1031 }
1032
Andy McFadden5167ec62014-05-22 13:08:43 -07001033 if (kIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001034 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001035 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001036 enableHardwareVsync();
1037 }
1038 }
1039
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001040 if (mAnimCompositionPending) {
1041 mAnimCompositionPending = false;
1042
Jesse Halla9a1b002013-02-27 16:39:25 -08001043 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001044 mAnimFrameTracker.setActualPresentFence(presentFence);
1045 } else {
1046 // The HWC doesn't support present fences, so use the refresh
1047 // timestamp instead.
1048 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1049 mAnimFrameTracker.setActualPresentTime(presentTime);
1050 }
1051 mAnimFrameTracker.advanceFrame();
1052 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001053}
1054
1055void SurfaceFlinger::rebuildLayerStacks() {
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07001056#ifdef QCOM_BSP
1057 char prop[PROPERTY_VALUE_MAX];
1058 property_get("sys.extended_mode", prop, "0");
1059 sExtendedMode = atoi(prop) ? true : false;
1060#endif
Mathias Agopiancd60f992012-08-16 16:28:27 -07001061 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001062 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001063 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -07001064 mVisibleRegionsDirty = false;
1065 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001066
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001067 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001068 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001069 Region opaqueRegion;
1070 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -08001071 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -07001072 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001073 const Transform& tr(hw->getTransform());
1074 const Rect bounds(hw->getBounds());
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001075 int dpyId = hw->getHwcDisplayId();
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001076 if (hw->isDisplayOn()) {
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001077 SurfaceFlinger::computeVisibleRegions(dpyId, layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001078 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001079
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001080 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001081 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001082 const sp<Layer>& layer(layers[i]);
1083 const Layer::State& s(layer->getDrawingState());
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001084 Region drawRegion(tr.transform(
1085 layer->visibleNonTransparentRegion));
1086 drawRegion.andSelf(bounds);
1087 if (!drawRegion.isEmpty()) {
1088 layersSortedByZ.add(layer);
Mathias Agopian87baae12012-07-31 12:38:26 -07001089 }
1090 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001091 }
Mathias Agopian42977342012-08-05 00:40:46 -07001092 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001093 hw->undefinedRegion.set(bounds);
1094 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
1095 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001096 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001097 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001098}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001099
Mathias Agopiancd60f992012-08-16 16:28:27 -07001100void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001101 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001102 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1103 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1104 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1105
1106 // If nothing has changed (!dirty), don't recompose.
1107 // If something changed, but we don't currently have any visible layers,
1108 // and didn't when we last did a composition, then skip it this time.
1109 // The second rule does two things:
1110 // - When all layers are removed from a display, we'll emit one black
1111 // frame, then nothing more until we get new layers.
1112 // - When a display is created with a private layer stack, we won't
1113 // emit any black frames until a layer is added to the layer stack.
1114 bool mustRecompose = dirty && !(empty && wasEmpty);
1115
1116 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1117 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1118 mustRecompose ? "doing" : "skipping",
1119 dirty ? "+" : "-",
1120 empty ? "+" : "-",
1121 wasEmpty ? "+" : "-");
1122
Dan Stoza71433162014-02-04 16:22:36 -08001123 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001124
1125 if (mustRecompose) {
1126 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1127 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001128 }
1129
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001130 HWComposer& hwc(getHwComposer());
1131 if (hwc.initCheck() == NO_ERROR) {
1132 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001133 if (CC_UNLIKELY(mHwWorkListDirty)) {
1134 mHwWorkListDirty = false;
1135 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1136 sp<const DisplayDevice> hw(mDisplays[dpy]);
1137 const int32_t id = hw->getHwcDisplayId();
1138 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001139 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001140 hw->getVisibleLayersSortedByZ());
1141 const size_t count = currentLayers.size();
1142 if (hwc.createWorkList(id, count) == NO_ERROR) {
1143 HWComposer::LayerListIterator cur = hwc.begin(id);
1144 const HWComposer::LayerListIterator end = hwc.end(id);
1145 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001146 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001147 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001148 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001149 cur->setSkip(true);
1150 }
1151 }
1152 }
1153 }
1154 }
1155 }
1156
1157 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001158 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001159 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001160 const int32_t id = hw->getHwcDisplayId();
1161 if (id >= 0) {
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001162 // Get the layers in the current drawying state
1163 const LayerVector& layers(mDrawingState.layersSortedByZ);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001164#ifdef QCOM_BSP
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001165 bool freezeSurfacePresent = false;
1166 const size_t layerCount = layers.size();
Ramkumar Radhakrishnanced0b062014-08-11 15:31:07 -07001167 // Look for ScreenShotSurface in external layer list, only when
1168 // disable external rotation animation feature is enabled
1169 if(mDisableExtAnimation && (id != HWC_DISPLAY_PRIMARY)) {
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001170 for (size_t i = 0 ; i < layerCount ; ++i) {
1171 static int screenShotLen = strlen("ScreenshotSurface");
1172 const sp<Layer>& layer(layers[i]);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001173 const Layer::State& s(layer->getDrawingState());
1174 // check the layers associated with external display
1175 if(s.layerStack == hw->getLayerStack()) {
1176 if(!strncmp(layer->getName(), "ScreenshotSurface",
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001177 screenShotLen)) {
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001178 // Screenshot layer is present, and animation in
1179 // progress
1180 freezeSurfacePresent = true;
1181 break;
1182 }
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001183 }
1184 }
1185 }
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001186#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001187 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001188 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001189 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001190 HWComposer::LayerListIterator cur = hwc.begin(id);
1191 const HWComposer::LayerListIterator end = hwc.end(id);
1192 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1193 /*
1194 * update the per-frame h/w composer data for each layer
1195 * and build the transparent region of the FB
1196 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001197 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001198 layer->setPerFrameData(hw, *cur);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001199#ifdef QCOM_BSP
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001200 if(freezeSurfacePresent) {
1201 // if freezeSurfacePresent, set ANIMATING flag
1202 cur->setAnimating(true);
1203 } else {
1204 const KeyedVector<wp<IBinder>, DisplayDeviceState>&
1205 draw(mDrawingState.displays);
1206 size_t dc = draw.size();
1207 for (size_t i=0 ; i<dc ; i++) {
1208 if (draw[i].isMainDisplay()) {
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001209 // Pass the current orientation to HWC
1210 hwc.eventControl(HWC_DISPLAY_PRIMARY,
1211 SurfaceFlinger::EVENT_ORIENTATION,
1212 uint32_t(draw[i].orientation));
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001213 }
1214 }
1215 }
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001216#endif
Mathias Agopian1e260872012-08-08 18:35:12 -07001217 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001218 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001219 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001220
Riley Andrews03414a12014-07-01 14:22:59 -07001221 // If possible, attempt to use the cursor overlay on each display.
1222 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1223 sp<const DisplayDevice> hw(mDisplays[dpy]);
1224 const int32_t id = hw->getHwcDisplayId();
1225 if (id >= 0) {
1226 const Vector< sp<Layer> >& currentLayers(
1227 hw->getVisibleLayersSortedByZ());
1228 const size_t count = currentLayers.size();
1229 HWComposer::LayerListIterator cur = hwc.begin(id);
1230 const HWComposer::LayerListIterator end = hwc.end(id);
1231 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1232 const sp<Layer>& layer(currentLayers[i]);
1233 if (layer->isPotentialCursor()) {
1234 cur->setIsCursorLayerHint();
1235 break;
1236 }
1237 }
1238 }
1239 }
1240
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001241 status_t err = hwc.prepare();
1242 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001243
1244 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1245 sp<const DisplayDevice> hw(mDisplays[dpy]);
1246 hw->prepareFrame(hwc);
1247 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001248 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001249}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001250
Mathias Agopiancd60f992012-08-16 16:28:27 -07001251void SurfaceFlinger::doComposition() {
1252 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001253 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001254 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001255 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001256 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001257 // transform the dirty region into this screen's coordinate space
1258 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001259
1260 // repaint the framebuffer (if needed)
1261 doDisplayComposition(hw, dirtyRegion);
1262
Mathias Agopiancd60f992012-08-16 16:28:27 -07001263 hw->dirtyRegion.clear();
1264 hw->flip(hw->swapRegion);
1265 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001266 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001267 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001268 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001269 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001270 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001271}
1272
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001273void SurfaceFlinger::postFramebuffer()
1274{
Mathias Agopian841cde52012-03-01 15:44:37 -08001275 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001276
Mathias Agopiana44b0412011-10-16 18:46:35 -07001277 const nsecs_t now = systemTime();
1278 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001279
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001280 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001281 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001282 if (!hwc.supportsFramebufferTarget()) {
1283 // EGL spec says:
1284 // "surface must be bound to the calling thread's current context,
1285 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001286 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001287 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001288 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001289 }
1290
Mathias Agopian6da15f42013-09-25 20:40:07 -07001291 // make the default display current because the VirtualDisplayDevice code cannot
1292 // deal with dequeueBuffer() being called outside of the composition loop; however
1293 // the code below can call glFlush() which is allowed (and does in some case) call
1294 // dequeueBuffer().
1295 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1296
Mathias Agopian92a979a2012-08-02 18:32:23 -07001297 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001298 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001299 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001300 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001301 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001302 int32_t id = hw->getHwcDisplayId();
1303 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001304 HWComposer::LayerListIterator cur = hwc.begin(id);
1305 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001306 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001307 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001308 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001309 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001310 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001311 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001312 }
Jesse Hallef194142012-06-14 14:45:17 -07001313 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001314 }
1315
Mathias Agopiana44b0412011-10-16 18:46:35 -07001316 mLastSwapBufferTime = systemTime() - now;
1317 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001318
1319 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1320 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1321 logFrameStats();
1322 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323}
1324
Mathias Agopian87baae12012-07-31 12:38:26 -07001325void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001326{
Mathias Agopian841cde52012-03-01 15:44:37 -08001327 ATRACE_CALL();
1328
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001329 // here we keep a copy of the drawing state (that is the state that's
1330 // going to be overwritten by handleTransactionLocked()) outside of
1331 // mStateLock so that the side-effects of the State assignment
1332 // don't happen with mStateLock held (which can cause deadlocks).
1333 State drawingState(mDrawingState);
1334
Mathias Agopianca4d3602011-05-19 15:38:14 -07001335 Mutex::Autolock _l(mStateLock);
1336 const nsecs_t now = systemTime();
1337 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001338
Mathias Agopianca4d3602011-05-19 15:38:14 -07001339 // Here we're guaranteed that some transaction flags are set
1340 // so we can call handleTransactionLocked() unconditionally.
1341 // We call getTransactionFlags(), which will also clear the flags,
1342 // with mStateLock held to guarantee that mCurrentState won't change
1343 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001344
Mathias Agopiane57f2922012-08-09 16:29:12 -07001345 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001346 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001347
Mathias Agopianca4d3602011-05-19 15:38:14 -07001348 mLastTransactionTime = systemTime() - now;
1349 mDebugInTransaction = 0;
1350 invalidateHwcGeometry();
1351 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001352}
1353
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301354void SurfaceFlinger::setVirtualDisplayData(
1355 int32_t hwcDisplayId,
1356 const sp<IGraphicBufferProducer>& sink)
1357{
1358 sp<ANativeWindow> mNativeWindow = new Surface(sink);
1359 ANativeWindow* const window = mNativeWindow.get();
1360
1361 int format;
1362 window->query(window, NATIVE_WINDOW_FORMAT, &format);
1363
1364 EGLSurface surface;
1365 EGLint w, h;
1366 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1367 surface = eglCreateWindowSurface(display, mRenderEngine->getEGLConfig(), window, NULL);
1368 eglQuerySurface(display, surface, EGL_WIDTH, &w);
1369 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
1370
1371 mHwc->setVirtualDisplayProperties(hwcDisplayId, w, h, format);
1372}
1373
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001374void SurfaceFlinger::configureVirtualDisplay(int32_t &hwcDisplayId,
1375 sp<DisplaySurface> &dispSurface,
1376 sp<IGraphicBufferProducer> &producer,
1377 const DisplayDeviceState state,
1378 sp<IGraphicBufferProducer> bqProducer,
1379 sp<IGraphicBufferConsumer> bqConsumer)
1380{
1381 bool vdsEnabled = mHwc->isVDSEnabled();
1382
1383 //for V4L2 based virtual display implementation
1384 if(!vdsEnabled) {
1385 // persist.sys.wfd.virtual will be set if WFD is launched via
1386 // settings app. This is currently being done in
1387 // ExtendedRemoteDisplay-WFD stack.
1388 // This flag will be reset at the time of disconnection of virtual WFD
1389 // display.
1390 // This flag is set to zero if WFD is launched via QCOM WFD
1391 // proprietary APIs which use HDMI piggyback approach.
1392 char value[PROPERTY_VALUE_MAX];
1393 property_get("persist.sys.wfd.virtual", value, "0");
1394 int wfdVirtual = atoi(value);
1395 if(!wfdVirtual) {
1396 // This is for non-wfd virtual display scenarios(e.g. SSD/SR/CTS)
1397 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001398 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1399 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001400 dispSurface = vds;
1401 // There won't be any interaction with HWC for this virtual display.
1402 // so the GLES driver can pass buffers directly to the sink.
1403 producer = state.surface;
1404 } else {
Raj Kamal366d9b42014-06-11 13:49:32 +05301405 int sinkUsage = -1;
1406 state.surface->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
1407#ifdef QCOM_BSP
1408 if(sinkUsage & GRALLOC_USAGE_PRIVATE_WFD)
1409#endif
1410 hwcDisplayId = allocateHwcDisplayId(state.type);
1411
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001412 if (hwcDisplayId >= 0) {
1413 // This is for WFD virtual display scenario.
1414 // Read virtual display properties and create a
1415 // rendering surface for it inorder to be handled by hwc.
1416 setVirtualDisplayData(hwcDisplayId, state.surface);
1417 dispSurface = new FramebufferSurface(*mHwc, state.type,
1418 bqConsumer);
1419 producer = bqProducer;
1420 } else {
1421 // in case of WFD Virtual + SSD/SR concurrency scenario,
1422 // WFD virtual display instance gets valid hwcDisplayId and
1423 // SSD/SR will get invalid hwcDisplayId
1424 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001425 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1426 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001427 dispSurface = vds;
1428 // There won't be any interaction with HWC for this virtual
1429 // display, so the GLES driver can pass buffers directly to the
1430 // sink.
1431 producer = state.surface;
1432 }
1433 }
1434 } else {
1435 // VDS solution is enabled
1436 // HWC is allocated for first virtual display.
1437 // Subsequent virtual display sessions will be composed by GLES driver.
1438 // ToDo: Modify VDS component to allocate hwcDisplayId based on
1439 // mForceHwcCopy (which is based on Usage Flags)
1440
1441 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001442 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1443 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001444 dispSurface = vds;
1445 if (hwcDisplayId >= 0) {
1446 producer = vds;
1447 } else {
1448 // There won't be any interaction with HWC for this virtual display,
1449 // so the GLES driver can pass buffers directly to the sink.
1450 producer = state.surface;
1451 }
1452 }
1453}
1454
Mathias Agopian87baae12012-07-31 12:38:26 -07001455void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001456{
1457 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001458 const size_t count = currentLayers.size();
1459
1460 /*
1461 * Traversal of the children
1462 * (perform the transaction for each of them if needed)
1463 */
1464
Mathias Agopian3559b072012-08-15 13:46:03 -07001465 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001466 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001467 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001468 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1469 if (!trFlags) continue;
1470
1471 const uint32_t flags = layer->doTransaction(0);
1472 if (flags & Layer::eVisibleRegion)
1473 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001474 }
1475 }
1476
1477 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001478 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001479 */
1480
Mathias Agopiane57f2922012-08-09 16:29:12 -07001481 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001482 // here we take advantage of Vector's copy-on-write semantics to
1483 // improve performance by skipping the transaction entirely when
1484 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001485 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1486 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001487 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001488 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001489 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001490 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001491
1492 // find the displays that were removed
1493 // (ie: in drawing state but not in current state)
1494 // also handle displays that changed
1495 // (ie: displays that are in both lists)
1496 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001497 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1498 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001499 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001500 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001501 // Call makeCurrent() on the primary display so we can
1502 // be sure that nothing associated with this display
1503 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001504 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001505 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001506 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1507 if (hw != NULL)
1508 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001509 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001510 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001511 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001512 } else {
1513 ALOGW("trying to remove the main display");
1514 }
1515 } else {
1516 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001517 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001518 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001519 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001520 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001521 // recreating the DisplayDevice, so we just remove it
1522 // from the drawing state, so that it get re-added
1523 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001524 sp<DisplayDevice> hw(getDisplayDevice(display));
1525 if (hw != NULL)
1526 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001527 mDisplays.removeItem(display);
1528 mDrawingState.displays.removeItemsAt(i);
1529 dc--; i--;
1530 // at this point we must loop to the next item
1531 continue;
1532 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001533
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001534 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001535 if (disp != NULL) {
1536 if (state.layerStack != draw[i].layerStack) {
1537 disp->setLayerStack(state.layerStack);
1538 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001539 if ((state.orientation != draw[i].orientation)
1540 || (state.viewport != draw[i].viewport)
1541 || (state.frame != draw[i].frame))
1542 {
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001543#ifdef QCOM_BSP
1544 int orient = state.orientation;
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001545 // Honor the orientation change after boot
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301546 // animation completes and make sure boot
1547 // animation is shown in panel orientation always.
1548 if(mBootFinished){
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001549 disp->setProjection(state.orientation,
1550 state.viewport, state.frame);
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001551 orient = state.orientation;
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001552 }
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301553 else{
1554 char property[PROPERTY_VALUE_MAX];
1555 int panelOrientation =
1556 DisplayState::eOrientationDefault;
1557 if(property_get("persist.panel.orientation",
1558 property, "0") > 0){
1559 panelOrientation = atoi(property) / 90;
1560 }
1561 disp->setProjection(panelOrientation,
1562 state.viewport, state.frame);
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001563 orient = panelOrientation;
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301564 }
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001565 // Set the view frame of each display only of its
1566 // default orientation.
Saurabh Shah027b3852014-08-21 16:09:32 -07001567 if(orient == DisplayState::eOrientationDefault and
1568 state.frame.isValid()) {
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001569 qdutils::setViewFrame(disp->getHwcDisplayId(),
1570 state.frame.left, state.frame.top,
1571 state.frame.right, state.frame.bottom);
1572 }
1573#else
1574 disp->setProjection(state.orientation,
1575 state.viewport, state.frame);
1576#endif
Mathias Agopian93997a82012-08-29 17:30:36 -07001577 }
Michael Lentine47e45402014-07-18 15:34:25 -07001578 if (state.width != draw[i].width || state.height != draw[i].height) {
1579 disp->setDisplaySize(state.width, state.height);
1580 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001581 }
1582 }
1583 }
1584
1585 // find displays that were added
1586 // (ie: in current state but not in drawing state)
1587 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001588 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001589 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001590
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001591 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001592 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001593 sp<IGraphicBufferProducer> bqProducer;
1594 sp<IGraphicBufferConsumer> bqConsumer;
1595 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1596 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001597
Jesse Hall02d86562013-03-25 14:43:23 -07001598 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001599 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001600 // Virtual displays without a surface are dormant:
1601 // they have external state (layer stack, projection,
1602 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001603 if (state.surface != NULL) {
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001604 configureVirtualDisplay(hwcDisplayId,
1605 dispSurface, producer, state, bqProducer,
1606 bqConsumer);
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001607 }
1608 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001609 ALOGE_IF(state.surface!=NULL,
1610 "adding a supported display, but rendering "
1611 "surface is provided (%p), ignoring it",
1612 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001613 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001614 // for supported (by hwc) displays we provide our
1615 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001616 dispSurface = new FramebufferSurface(*mHwc, state.type,
1617 bqConsumer);
1618 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001619 }
1620
1621 const wp<IBinder>& display(curr.keyAt(i));
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301622 if (dispSurface != NULL && producer != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001623 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001624 state.type, hwcDisplayId,
1625 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001626 display, dispSurface, producer,
1627 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001628 hw->setLayerStack(state.layerStack);
1629 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001630 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001631 hw->setDisplayName(state.displayName);
Tatenda Chipeperekwab801c8a2014-09-22 16:57:24 -07001632 // When a new display device is added update the active
1633 // config by querying HWC otherwise the default config
1634 // (config 0) will be used.
1635 int activeConfig = mHwc->getActiveConfig(hwcDisplayId);
1636 if (activeConfig >= 0) {
1637 hw->setActiveConfig(activeConfig);
1638 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07001639 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001640 if (state.isVirtualDisplay()) {
1641 if (hwcDisplayId >= 0) {
1642 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1643 hw->getWidth(), hw->getHeight(),
1644 hw->getFormat());
1645 }
1646 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001647 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001648 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001649 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001650 }
1651 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001653 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654
Mathias Agopian84300952012-11-21 16:02:13 -08001655 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1656 // The transform hint might have changed for some layers
1657 // (either because a display has changed, or because a layer
1658 // as changed).
1659 //
1660 // Walk through all the layers in currentLayers,
1661 // and update their transform hint.
1662 //
1663 // If a layer is visible only on a single display, then that
1664 // display is used to calculate the hint, otherwise we use the
1665 // default display.
1666 //
1667 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1668 // the hint is set before we acquire a buffer from the surface texture.
1669 //
1670 // NOTE: layer transactions have taken place already, so we use their
1671 // drawing state. However, SurfaceFlinger's own transaction has not
1672 // happened yet, so we must use the current state layer list
1673 // (soon to become the drawing state list).
1674 //
1675 sp<const DisplayDevice> disp;
1676 uint32_t currentlayerStack = 0;
1677 for (size_t i=0; i<count; i++) {
1678 // NOTE: we rely on the fact that layers are sorted by
1679 // layerStack first (so we don't have to traverse the list
1680 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001681 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001682 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001683 if (i==0 || currentlayerStack != layerStack) {
1684 currentlayerStack = layerStack;
1685 // figure out if this layerstack is mirrored
1686 // (more than one display) if so, pick the default display,
1687 // if not, pick the only display it's on.
1688 disp.clear();
1689 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1690 sp<const DisplayDevice> hw(mDisplays[dpy]);
1691 if (hw->getLayerStack() == currentlayerStack) {
1692 if (disp == NULL) {
1693 disp = hw;
1694 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001695 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001696 break;
1697 }
1698 }
1699 }
1700 }
Chet Haase91d25932013-04-11 15:24:55 -07001701 if (disp == NULL) {
1702 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1703 // redraw after transform hint changes. See bug 8508397.
1704
1705 // could be null when this layer is using a layerStack
1706 // that is not visible on any display. Also can occur at
1707 // screen off/on times.
1708 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001709 }
Chet Haase91d25932013-04-11 15:24:55 -07001710 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001711 }
1712 }
1713
1714
Mathias Agopian3559b072012-08-15 13:46:03 -07001715 /*
1716 * Perform our own transaction if needed
1717 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001718
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001719 const LayerVector& layers(mDrawingState.layersSortedByZ);
1720 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001721 // layers have been added
1722 mVisibleRegionsDirty = true;
1723 }
1724
1725 // some layers might have been removed, so
1726 // we need to update the regions they're exposing.
1727 if (mLayersRemoved) {
1728 mLayersRemoved = false;
1729 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001730 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001731 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001732 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001733 if (currentLayers.indexOf(layer) < 0) {
1734 // this layer is not visible anymore
1735 // TODO: we could traverse the tree from front to back and
1736 // compute the actual visible region
1737 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001738 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001739 Region visibleReg = s.transform.transform(
1740 Region(Rect(s.active.w, s.active.h)));
1741 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001742 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001743 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001744 }
1745
1746 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001747
1748 updateCursorAsync();
1749}
1750
1751void SurfaceFlinger::updateCursorAsync()
1752{
1753 HWComposer& hwc(getHwComposer());
1754 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1755 sp<const DisplayDevice> hw(mDisplays[dpy]);
1756 const int32_t id = hw->getHwcDisplayId();
1757 if (id < 0) {
1758 continue;
1759 }
1760 const Vector< sp<Layer> >& currentLayers(
1761 hw->getVisibleLayersSortedByZ());
1762 const size_t count = currentLayers.size();
1763 HWComposer::LayerListIterator cur = hwc.begin(id);
1764 const HWComposer::LayerListIterator end = hwc.end(id);
1765 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1766 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1767 continue;
1768 }
1769 const sp<Layer>& layer(currentLayers[i]);
1770 Rect cursorPos = layer->getPosition(hw);
1771 hwc.setCursorPositionAsync(id, cursorPos);
1772 break;
1773 }
1774 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001775}
1776
1777void SurfaceFlinger::commitTransaction()
1778{
1779 if (!mLayersPendingRemoval.isEmpty()) {
1780 // Notify removed layers now that they can't be drawn from
1781 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1782 mLayersPendingRemoval[i]->onRemoved();
1783 }
1784 mLayersPendingRemoval.clear();
1785 }
1786
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001787 // If this transaction is part of a window animation then the next frame
1788 // we composite should be considered an animation as well.
1789 mAnimCompositionPending = mAnimTransactionPending;
1790
Mathias Agopian4fec8732012-06-29 14:12:52 -07001791 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001792 mTransactionPending = false;
1793 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001794 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001795}
1796
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001797void SurfaceFlinger::computeVisibleRegions(size_t dpy,
Mathias Agopian87baae12012-07-31 12:38:26 -07001798 const LayerVector& currentLayers, uint32_t layerStack,
1799 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001800{
Mathias Agopian841cde52012-03-01 15:44:37 -08001801 ATRACE_CALL();
1802
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001803 Region aboveOpaqueLayers;
1804 Region aboveCoveredLayers;
1805 Region dirty;
1806
Mathias Agopian87baae12012-07-31 12:38:26 -07001807 outDirtyRegion.clear();
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001808 bool bIgnoreLayers = false;
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001809 int indexLOI = -1;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001810 size_t i = currentLayers.size();
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001811#ifdef QCOM_BSP
1812 while (i--) {
1813 const sp<Layer>& layer = currentLayers[i];
1814 // iterate through the layer list to find ext_only layers and store
1815 // the index
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001816 if (layer->isSecureDisplay()) {
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001817 bIgnoreLayers = true;
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001818 indexLOI = -1;
1819 if(!dpy)
1820 indexLOI = i;
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001821 break;
1822 }
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07001823 // iterate through the layer list to find ext_only layers or yuv
1824 // layer(extended_mode) and store the index
1825 if ((dpy && (layer->isExtOnly() ||
1826 (isExtendedMode() && layer->isYuvLayer())))) {
1827 bIgnoreLayers= true;
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001828 indexLOI = i;
1829 }
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001830 }
1831 i = currentLayers.size();
1832#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001833 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001834 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001835
1836 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001837 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001838
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001839#ifdef QCOM_BSP
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07001840 // Only add the layer marked as "external_only" or yuvLayer
1841 // (extended_mode) to external list and
1842 // only remove the layer marked as "external_only" or yuvLayer in
1843 // extended_mode from primary list
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001844 // and do not add the layer marked as "internal_only" to external list
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07001845 // Add secure UI layers to primary and remove other layers from internal
1846 //and external list
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07001847 if(((bIgnoreLayers && indexLOI != (int)i) ||
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001848 (!dpy && layer->isExtOnly()) ||
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07001849 (!dpy && isExtendedMode() && layer->isYuvLayer()))||
1850 (dpy && layer->isIntOnly())) {
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001851 // Ignore all other layers except the layers marked as ext_only
1852 // by setting visible non transparent region empty.
1853 Region visibleNonTransRegion;
1854 visibleNonTransRegion.set(Rect(0,0));
1855 layer->setVisibleNonTransparentRegion(visibleNonTransRegion);
1856 continue;
1857 }
Ramkumar Radhakrishnan679eefe2013-06-18 19:36:19 -07001858#endif
1859 // only consider the layers on the given later stack
1860 // Override layers created using presentation class by the layers having
1861 // ext_only flag enabled
1862 if(s.layerStack != layerStack && !bIgnoreLayers) {
1863 // set the visible region as empty since we have removed the
1864 // layerstack check in rebuildLayerStack() function.
Ramkumar Radhakrishnan5ee229d2013-04-09 22:33:36 -07001865 Region visibleNonTransRegion;
1866 visibleNonTransRegion.set(Rect(0,0));
1867 layer->setVisibleNonTransparentRegion(visibleNonTransRegion);
1868 continue;
1869 }
Mathias Agopianab028732010-03-16 16:41:46 -07001870 /*
1871 * opaqueRegion: area of a surface that is fully opaque.
1872 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001873 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001874
1875 /*
1876 * visibleRegion: area of a surface that is visible on screen
1877 * and not fully transparent. This is essentially the layer's
1878 * footprint minus the opaque regions above it.
1879 * Areas covered by a translucent surface are considered visible.
1880 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001881 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001882
1883 /*
1884 * coveredRegion: area of a surface that is covered by all
1885 * visible regions above it (which includes the translucent areas).
1886 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001887 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001888
Jesse Halla8026d22012-09-25 13:25:04 -07001889 /*
1890 * transparentRegion: area of a surface that is hinted to be completely
1891 * transparent. This is only used to tell when the layer has no visible
1892 * non-transparent regions and can be removed from the layer list. It
1893 * does not affect the visibleRegion of this layer or any layers
1894 * beneath it. The hint may not be correct if apps don't respect the
1895 * SurfaceView restrictions (which, sadly, some don't).
1896 */
1897 Region transparentRegion;
1898
Mathias Agopianab028732010-03-16 16:41:46 -07001899
1900 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001901 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001902 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001903 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001904 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001905 if (!visibleRegion.isEmpty()) {
1906 // Remove the transparent area from the visible region
1907 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001908 const Transform tr(s.transform);
1909 if (tr.transformed()) {
1910 if (tr.preserveRects()) {
1911 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001912 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001913 } else {
1914 // transformation too complex, can't do the
1915 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001916 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001917 }
1918 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001919 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001920 }
Mathias Agopianab028732010-03-16 16:41:46 -07001921 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001922
Mathias Agopianab028732010-03-16 16:41:46 -07001923 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001924 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001925 if (s.alpha==255 && !translucent &&
1926 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1927 // the opaque region is the layer's footprint
1928 opaqueRegion = visibleRegion;
1929 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001930 }
1931 }
1932
Mathias Agopianab028732010-03-16 16:41:46 -07001933 // Clip the covered region to the visible region
1934 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1935
1936 // Update aboveCoveredLayers for next (lower) layer
1937 aboveCoveredLayers.orSelf(visibleRegion);
1938
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001939 // subtract the opaque region covered by the layers above us
1940 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001941
1942 // compute this layer's dirty region
1943 if (layer->contentDirty) {
1944 // we need to invalidate the whole region
1945 dirty = visibleRegion;
1946 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001947 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001948 layer->contentDirty = false;
1949 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001950 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001951 * the exposed region consists of two components:
1952 * 1) what's VISIBLE now and was COVERED before
1953 * 2) what's EXPOSED now less what was EXPOSED before
1954 *
1955 * note that (1) is conservative, we start with the whole
1956 * visible region but only keep what used to be covered by
1957 * something -- which mean it may have been exposed.
1958 *
1959 * (2) handles areas that were not covered by anything but got
1960 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001961 */
Mathias Agopianab028732010-03-16 16:41:46 -07001962 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001963 const Region oldVisibleRegion = layer->visibleRegion;
1964 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001965 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1966 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001967 }
1968 dirty.subtractSelf(aboveOpaqueLayers);
1969
1970 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001971 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001972
Mathias Agopianab028732010-03-16 16:41:46 -07001973 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001974 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001975
Jesse Halla8026d22012-09-25 13:25:04 -07001976 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001977 layer->setVisibleRegion(visibleRegion);
1978 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001979 layer->setVisibleNonTransparentRegion(
1980 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001981 }
1982
Mathias Agopian87baae12012-07-31 12:38:26 -07001983 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001984}
1985
Mathias Agopian87baae12012-07-31 12:38:26 -07001986void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1987 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001988 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001989 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1990 if (hw->getLayerStack() == layerStack) {
1991 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001992 }
1993 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001994}
1995
Dan Stoza6b9454d2014-11-07 16:00:59 -08001996bool SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001997{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001998 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001999
Mathias Agopian4fec8732012-06-29 14:12:52 -07002000 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002001 const LayerVector& layers(mDrawingState.layersSortedByZ);
Dan Stoza6b9454d2014-11-07 16:00:59 -08002002 bool frameQueued = false;
Eric Penner51c59cd2014-07-28 19:51:58 -07002003
2004 // Store the set of layers that need updates. This set must not change as
2005 // buffers are being latched, as this could result in a deadlock.
2006 // Example: Two producers share the same command stream and:
2007 // 1.) Layer 0 is latched
2008 // 2.) Layer 0 gets a new frame
2009 // 2.) Layer 1 gets a new frame
2010 // 3.) Layer 1 is latched.
2011 // Display is now waiting on Layer 1's frame, which is behind layer 0's
2012 // second frame. But layer 0's second frame could be waiting on display.
2013 Vector<Layer*> layersWithQueuedFrames;
2014 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002015 const sp<Layer>& layer(layers[i]);
Dan Stoza6b9454d2014-11-07 16:00:59 -08002016 if (layer->hasQueuedFrame()) {
2017 frameQueued = true;
2018 if (layer->shouldPresentNow(mPrimaryDispSync)) {
2019 layersWithQueuedFrames.push_back(layer.get());
2020 }
2021 }
Eric Penner51c59cd2014-07-28 19:51:58 -07002022 }
2023 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
2024 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07002025 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002026 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07002027 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07002028 }
Mathias Agopian4da75192010-08-10 17:19:56 -07002029
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07002030 mVisibleRegionsDirty |= visibleRegions;
Dan Stoza6b9454d2014-11-07 16:00:59 -08002031
2032 // If we will need to wake up at some time in the future to deal with a
2033 // queued frame that shouldn't be displayed during this vsync period, wake
2034 // up during the next vsync period to check again.
2035 if (frameQueued && layersWithQueuedFrames.empty()) {
2036 signalLayerUpdate();
2037 }
2038
2039 // Only continue with the refresh if there is actually new work to do
2040 return !layersWithQueuedFrames.empty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002041}
2042
Mathias Agopianad456f92011-01-13 17:53:01 -08002043void SurfaceFlinger::invalidateHwcGeometry()
2044{
2045 mHwWorkListDirty = true;
2046}
2047
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002048
Mathias Agopiancd60f992012-08-16 16:28:27 -07002049void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07002050 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002051{
Dan Stoza71433162014-02-04 16:22:36 -08002052 // We only need to actually compose the display if:
2053 // 1) It is being handled by hardware composer, which may need this to
2054 // keep its virtual display state machine in sync, or
2055 // 2) There is work to be done (the dirty region isn't empty)
2056 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
2057 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
2058 return;
2059 }
2060
Mathias Agopian87baae12012-07-31 12:38:26 -07002061 Region dirtyRegion(inDirtyRegion);
2062
Mathias Agopianb8a55602009-06-26 19:06:36 -07002063 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07002064 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002065
Mathias Agopian42977342012-08-05 00:40:46 -07002066 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002067 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002068 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
2069 // takes a rectangle, we must make sure to update that whole
2070 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07002071 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002072 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002073 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002074 // We need to redraw the rectangle that will be updated
2075 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07002076 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07002077 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07002078 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07002079 } else {
2080 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07002081 dirtyRegion.set(hw->bounds());
2082 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002083 }
2084 }
2085
Alan Viverette9c5a3332013-09-12 20:04:35 -07002086 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Michael Lentine3f121fc2014-10-01 11:17:28 -07002087 if (!doComposeSurfaces(hw, dirtyRegion)) return;
Mathias Agopianff2ed702013-09-01 21:36:12 -07002088 } else {
2089 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07002090 mat4 colorMatrix = mColorMatrix;
2091 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07002092 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002093 }
2094 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07002095 doComposeSurfaces(hw, dirtyRegion);
2096 engine.endGroup();
2097 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002098
Mathias Agopian9c6e2972011-09-20 17:21:56 -07002099 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07002100 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07002101
2102 // swap buffers (presentation)
2103 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002104}
2105
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302106#ifdef QCOM_BSP
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302107bool SurfaceFlinger::computeTiledDr(const sp<const DisplayDevice>& hw) {
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302108 int fbWidth= hw->getWidth();
2109 int fbHeight= hw->getHeight();
2110 Rect fullScreenRect = Rect(0,0,fbWidth, fbHeight);
2111 const int32_t id = hw->getHwcDisplayId();
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302112 mUnionDirtyRect.clear();
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302113 HWComposer& hwc(getHwComposer());
2114
2115 /* Compute and return the Union of Dirty Rects.
2116 * Return false if the unionDR is fullscreen, as there is no benefit from
2117 * preserving full screen.*/
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302118 return (hwc.canUseTiledDR(id, mUnionDirtyRect) &&
2119 (mUnionDirtyRect != fullScreenRect));
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302120
2121}
2122#endif
2123
Michael Lentine3f121fc2014-10-01 11:17:28 -07002124bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07002125{
Mathias Agopian3f844832013-08-07 21:24:32 -07002126 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002127 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07002128 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07002129 HWComposer::LayerListIterator cur = hwc.begin(id);
2130 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07002131
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302132 Region clearRegion;
Jesse Halld05a17f2013-09-30 16:49:30 -07002133 bool hasGlesComposition = hwc.hasGlesComposition(id);
Sravan Kumar D.V.N74180cb2014-07-01 16:35:05 +05302134 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002135 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07002136 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08002137 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
2138 hw->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07002139 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
2140 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
2141 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
2142 }
2143 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08002144 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07002145
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07002146 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopiane60b0682012-08-21 23:34:09 -07002147 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07002148 // when using overlays, we assume a fully transparent framebuffer
2149 // NOTE: we could reduce how much we need to clear, for instance
2150 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07002151 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07002152 // We'll revisit later if needed.
Sravan Kumar D.V.N74180cb2014-07-01 16:35:05 +05302153 if(!(mGpuTileRenderEnable && (mDisplays.size()==1)))
2154 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002155 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07002156 // we start with the whole screen area
2157 const Region bounds(hw->getBounds());
2158
2159 // we remove the scissor part
2160 // we're left with the letterbox region
2161 // (common case is that letterbox ends-up being empty)
2162 const Region letterbox(bounds.subtract(hw->getScissor()));
2163
2164 // compute the area to clear
2165 Region region(hw->undefinedRegion.merge(letterbox));
2166
2167 // but limit it to the dirty region
2168 region.andSelf(dirty);
2169
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302170
Mathias Agopianb9494d52012-04-18 02:28:45 -07002171 // screen is already cleared here
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302172#ifdef QCOM_BSP
2173 clearRegion.clear();
2174 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
2175 clearRegion = region;
2176 if (cur == end) {
Radhika Ranjan Soni35c9ff22013-12-17 19:55:17 +05302177 drawWormhole(hw, region);
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302178 } else if(mCanUseGpuTileRender) {
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302179 /* If GPUTileRect DR optimization on clear only the UnionDR
2180 * (computed by computeTiledDr) which is the actual region
2181 * that will be drawn on FB in this cycle.. */
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302182 clearRegion = clearRegion.andSelf(Region(mUnionDirtyRect));
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302183 }
2184 } else
2185#endif
2186 {
2187 if (!region.isEmpty()) {
2188 if (cur != end) {
2189 if (cur->getCompositionType() != HWC_BLIT)
2190 // can happen with SurfaceView
2191 drawWormhole(hw, region);
2192 } else
2193 drawWormhole(hw, region);
2194 }
Mathias Agopianb9494d52012-04-18 02:28:45 -07002195 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002196 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002197
Mathias Agopian766dc492012-10-30 18:08:06 -07002198 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
2199 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07002200 // scissor on the main display. It should never be needed
2201 // anyways (though in theory it could since the API allows it).
2202 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07002203 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002204 if (scissor != bounds) {
2205 // scissor doesn't match the screen's dimensions, so we
2206 // need to clear everything outside of it and enable
2207 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07002208
Mathias Agopianf45c5102012-10-24 16:29:17 -07002209 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002210 const uint32_t height = hw->getHeight();
2211 engine.setScissor(scissor.left, height - scissor.bottom,
2212 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002213 }
2214 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002215 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002216
Mathias Agopian85d751c2012-08-29 16:59:24 -07002217 /*
2218 * and then, render the layers targeted at the framebuffer
2219 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002220
Mathias Agopian13127d82013-03-05 17:47:11 -08002221 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002222 const size_t count = layers.size();
2223 const Transform& tr = hw->getTransform();
2224 if (cur != end) {
2225 // we're using h/w composer
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302226#ifdef QCOM_BSP
2227 int fbWidth= hw->getWidth();
2228 int fbHeight= hw->getHeight();
2229 /* if GPUTileRender optimization property is on & can be used
2230 * i) Enable EGL_SWAP_PRESERVED flag
2231 * ii) do startTile with union DirtyRect
2232 * else , Disable EGL_SWAP_PRESERVED */
2233 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302234 if(mCanUseGpuTileRender && !mUnionDirtyRect.isEmpty()) {
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302235 hw->eglSwapPreserved(true);
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302236 Rect dr = mUnionDirtyRect;
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302237 engine.startTileComposition(dr.left, (fbHeight-dr.bottom),
2238 (dr.right-dr.left),
2239 (dr.bottom-dr.top), 0);
2240 } else {
2241 // Un Set EGL_SWAP_PRESERVED flag, if no tiling required.
2242 hw->eglSwapPreserved(false);
2243 }
2244 // DrawWormHole/Any Draw has to be within startTile & EndTile
Sravan Kumar D.V.N280bcd32014-08-28 14:54:25 +05302245 if (hasGlesComposition) {
2246 if (hasHwcComposition) {
2247 if(mCanUseGpuTileRender && !mUnionDirtyRect.isEmpty()) {
2248 const Rect& scissor(mUnionDirtyRect);
2249 engine.setScissor(scissor.left,
2250 hw->getHeight()- scissor.bottom,
2251 scissor.getWidth(), scissor.getHeight());
2252 engine.clearWithColor(0, 0, 0, 0);
2253 engine.disableScissor();
2254 } else {
2255 engine.clearWithColor(0, 0, 0, 0);
2256 }
Sravan Kumar D.V.N74180cb2014-07-01 16:35:05 +05302257 } else {
Sravan Kumar D.V.N280bcd32014-08-28 14:54:25 +05302258 if (cur->getCompositionType() != HWC_BLIT &&
2259 !clearRegion.isEmpty()) {
2260 drawWormhole(hw, clearRegion);
2261 }
Sravan Kumar D.V.N74180cb2014-07-01 16:35:05 +05302262 }
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302263 }
2264 }
2265#endif
2266
Mathias Agopian85d751c2012-08-29 16:59:24 -07002267 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002268 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07002269 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07002270 if (!clip.isEmpty()) {
2271 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07002272 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07002273 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07002274 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002275 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
2276 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08002277 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07002278 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07002279 // never clear the very first layer since we're
2280 // guaranteed the FB is already cleared
2281 layer->clearWithOpenGL(hw, clip);
2282 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002283 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07002284 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002285 case HWC_FRAMEBUFFER: {
2286 layer->draw(hw, clip);
2287 break;
2288 }
Arun Kumar K.R5111ff02012-12-19 18:10:46 -08002289 case HWC_BLIT:
2290 //Do nothing
2291 break;
Mathias Agopianda27af92012-09-13 18:17:13 -07002292 case HWC_FRAMEBUFFER_TARGET: {
2293 // this should not happen as the iterator shouldn't
2294 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08002295 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07002296 break;
2297 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07002298 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002299 }
2300 layer->setAcquireFence(hw, *cur);
2301 }
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302302
2303#ifdef QCOM_BSP
2304 // call EndTile, if starTile has been called in this cycle.
2305 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
Sravan Kumar D.V.Nf3d922a2014-06-18 16:44:22 +05302306 if(mCanUseGpuTileRender && !mUnionDirtyRect.isEmpty()) {
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302307 engine.endTileComposition(GL_PRESERVE);
2308 }
2309 }
2310#endif
Mathias Agopian85d751c2012-08-29 16:59:24 -07002311 } else {
2312 // we're not using h/w composer
2313 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002314 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002315 const Region clip(dirty.intersect(
2316 tr.transform(layer->visibleRegion)));
2317 if (!clip.isEmpty()) {
2318 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07002319 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002320 }
2321 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002322
2323 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002324 engine.disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07002325 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002326}
2327
Mathias Agopian3f844832013-08-07 21:24:32 -07002328void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07002329 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07002330 RenderEngine& engine(getRenderEngine());
2331 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002332}
2333
Mathias Agopianac9fa422013-02-11 16:40:36 -08002334void SurfaceFlinger::addClientLayer(const sp<Client>& client,
2335 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07002336 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08002337 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07002338{
Mathias Agopian96f08192010-06-02 23:28:45 -07002339 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08002340 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07002341
Mathias Agopian96f08192010-06-02 23:28:45 -07002342 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002343 Mutex::Autolock _l(mStateLock);
2344 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07002345 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07002346}
2347
Mathias Agopian3f844832013-08-07 21:24:32 -07002348status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002349 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08002350 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002351 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07002352 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002353 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07002354 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002355 return NO_ERROR;
2356 }
Mathias Agopian3d579642009-06-04 18:46:21 -07002357 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002358}
2359
Dan Stozac7014012014-02-14 15:03:43 -08002360uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07002361 return android_atomic_release_load(&mTransactionFlags);
2362}
2363
Mathias Agopian3f844832013-08-07 21:24:32 -07002364uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002365 return android_atomic_and(~flags, &mTransactionFlags) & flags;
2366}
2367
Mathias Agopian3f844832013-08-07 21:24:32 -07002368uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002369 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
2370 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002371 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002372 }
2373 return old;
2374}
2375
Mathias Agopian8b33f032012-07-24 20:43:54 -07002376void SurfaceFlinger::setTransactionState(
2377 const Vector<ComposerState>& state,
2378 const Vector<DisplayState>& displays,
2379 uint32_t flags)
2380{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002381 ATRACE_CALL();
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08002382 size_t count = displays.size();
2383#ifdef QCOM_BSP
Ramkumar Radhakrishnanced0b062014-08-11 15:31:07 -07002384 // Delay the display projection transaction by 50ms only when the disable
2385 // external rotation animation feature is enabled
2386 if(mDisableExtAnimation) {
2387 for (size_t i=0 ; i<count ; i++) {
2388 const DisplayState& s(displays[i]);
Baldev Kumar Sahub4ff4392014-09-12 10:58:11 +05302389 if((mDisplays.indexOfKey(s.token) >= 0) && (s.token !=
2390 mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY])) {
Ramkumar Radhakrishnanced0b062014-08-11 15:31:07 -07002391 const uint32_t what = s.what;
2392 // Invalidate and Delay the binder thread by 50 ms on
2393 // eDisplayProjectionChanged to trigger a draw cycle so that
2394 // it can fix one incorrect frame on the External, when we
2395 // disable external animation
2396 if (what & DisplayState::eDisplayProjectionChanged) {
2397 invalidateHwcGeometry();
2398 repaintEverything();
2399 usleep(50000);
2400 }
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08002401 }
2402 }
2403 }
2404#endif
Mathias Agopian698c0872011-06-28 19:09:31 -07002405 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07002406 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002407
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002408 if (flags & eAnimation) {
2409 // For window updates that are part of an animation we must wait for
2410 // previous animation "frames" to be handled.
2411 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002412 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002413 if (CC_UNLIKELY(err != NO_ERROR)) {
2414 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002415 // caller after a few seconds.
2416 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
2417 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002418 mAnimTransactionPending = false;
2419 break;
2420 }
2421 }
2422 }
2423
Mathias Agopiane57f2922012-08-09 16:29:12 -07002424 for (size_t i=0 ; i<count ; i++) {
2425 const DisplayState& s(displays[i]);
2426 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002427 }
2428
Mathias Agopiane57f2922012-08-09 16:29:12 -07002429 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002430 for (size_t i=0 ; i<count ; i++) {
2431 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002432 // Here we need to check that the interface we're given is indeed
2433 // one of our own. A malicious client could give us a NULL
2434 // IInterface, or one of its own or even one of our own but a
2435 // different type. All these situations would cause us to crash.
2436 //
2437 // NOTE: it would be better to use RTTI as we could directly check
2438 // that we have a Client*. however, RTTI is disabled in Android.
2439 if (s.client != NULL) {
2440 sp<IBinder> binder = s.client->asBinder();
2441 if (binder != NULL) {
Fabien Sanglard1d262342017-01-19 11:13:20 -08002442 if (binder->queryLocalInterface(ISurfaceComposerClient::descriptor) != NULL) {
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002443 sp<Client> client( static_cast<Client *>(s.client.get()) );
2444 transactionFlags |= setClientStateLocked(client, s.state);
2445 }
2446 }
2447 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002448 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002449
Mathias Agopian386aa982011-11-07 21:58:03 -08002450 if (transactionFlags) {
2451 // this triggers the transaction
2452 setTransactionFlags(transactionFlags);
2453
2454 // if this is a synchronous transaction, wait for it to take effect
2455 // before returning.
2456 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002457 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002458 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002459 if (flags & eAnimation) {
2460 mAnimTransactionPending = true;
2461 }
2462 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002463 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2464 if (CC_UNLIKELY(err != NO_ERROR)) {
2465 // just in case something goes wrong in SF, return to the
2466 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002467 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2468 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002469 break;
2470 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002471 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002472 }
2473}
2474
Mathias Agopiane57f2922012-08-09 16:29:12 -07002475uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2476{
Jesse Hall9a143922012-10-04 16:29:19 -07002477 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2478 if (dpyIdx < 0)
2479 return 0;
2480
Mathias Agopiane57f2922012-08-09 16:29:12 -07002481 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002482 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002483 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002484 const uint32_t what = s.what;
2485 if (what & DisplayState::eSurfaceChanged) {
2486 if (disp.surface->asBinder() != s.surface->asBinder()) {
2487 disp.surface = s.surface;
2488 flags |= eDisplayTransactionNeeded;
2489 }
2490 }
2491 if (what & DisplayState::eLayerStackChanged) {
2492 if (disp.layerStack != s.layerStack) {
2493 disp.layerStack = s.layerStack;
2494 flags |= eDisplayTransactionNeeded;
2495 }
2496 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002497 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002498 if (disp.orientation != s.orientation) {
2499 disp.orientation = s.orientation;
2500 flags |= eDisplayTransactionNeeded;
2501 }
2502 if (disp.frame != s.frame) {
2503 disp.frame = s.frame;
2504 flags |= eDisplayTransactionNeeded;
2505 }
2506 if (disp.viewport != s.viewport) {
2507 disp.viewport = s.viewport;
2508 flags |= eDisplayTransactionNeeded;
2509 }
2510 }
Michael Lentine47e45402014-07-18 15:34:25 -07002511 if (what & DisplayState::eDisplaySizeChanged) {
2512 if (disp.width != s.width) {
2513 disp.width = s.width;
2514 flags |= eDisplayTransactionNeeded;
2515 }
2516 if (disp.height != s.height) {
2517 disp.height = s.height;
2518 flags |= eDisplayTransactionNeeded;
2519 }
2520 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002521 }
2522 return flags;
2523}
2524
2525uint32_t SurfaceFlinger::setClientStateLocked(
2526 const sp<Client>& client,
2527 const layer_state_t& s)
2528{
2529 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002530 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002531 if (layer != 0) {
2532 const uint32_t what = s.what;
2533 if (what & layer_state_t::ePositionChanged) {
2534 if (layer->setPosition(s.x, s.y))
2535 flags |= eTraversalNeeded;
2536 }
2537 if (what & layer_state_t::eLayerChanged) {
2538 // NOTE: index needs to be calculated before we update the state
2539 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2540 if (layer->setLayer(s.z)) {
2541 mCurrentState.layersSortedByZ.removeAt(idx);
2542 mCurrentState.layersSortedByZ.add(layer);
2543 // we need traversal (state changed)
2544 // AND transaction (list changed)
2545 flags |= eTransactionNeeded|eTraversalNeeded;
2546 }
2547 }
2548 if (what & layer_state_t::eSizeChanged) {
2549 if (layer->setSize(s.w, s.h)) {
2550 flags |= eTraversalNeeded;
2551 }
2552 }
2553 if (what & layer_state_t::eAlphaChanged) {
2554 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2555 flags |= eTraversalNeeded;
2556 }
2557 if (what & layer_state_t::eMatrixChanged) {
2558 if (layer->setMatrix(s.matrix))
2559 flags |= eTraversalNeeded;
2560 }
2561 if (what & layer_state_t::eTransparentRegionChanged) {
2562 if (layer->setTransparentRegionHint(s.transparentRegion))
2563 flags |= eTraversalNeeded;
2564 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08002565 if ((what & layer_state_t::eVisibilityChanged) ||
Saurabh Shahfcd5c992014-11-24 11:08:34 -08002566 (what & layer_state_t::eOpacityChanged) ||
2567 (what & layer_state_t::eTransparencyChanged)) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08002568 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07002569 if (layer->setFlags(s.flags, s.mask))
2570 flags |= eTraversalNeeded;
2571 }
2572 if (what & layer_state_t::eCropChanged) {
2573 if (layer->setCrop(s.crop))
2574 flags |= eTraversalNeeded;
2575 }
2576 if (what & layer_state_t::eLayerStackChanged) {
2577 // NOTE: index needs to be calculated before we update the state
2578 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2579 if (layer->setLayerStack(s.layerStack)) {
2580 mCurrentState.layersSortedByZ.removeAt(idx);
2581 mCurrentState.layersSortedByZ.add(layer);
2582 // we need traversal (state changed)
2583 // AND transaction (list changed)
2584 flags |= eTransactionNeeded|eTraversalNeeded;
2585 }
2586 }
2587 }
2588 return flags;
2589}
2590
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002591status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002592 const String8& name,
2593 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002594 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2595 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002596{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002597 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002598 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002599 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002600 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002601 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002602 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002603
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002604 status_t result = NO_ERROR;
2605
2606 sp<Layer> layer;
2607
Mathias Agopian3165cc22012-08-08 19:42:09 -07002608 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2609 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002610 result = createNormalLayer(client,
2611 name, w, h, flags, format,
2612 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002613 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002614 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002615 result = createDimLayer(client,
2616 name, w, h, flags,
2617 handle, gbp, &layer);
2618 break;
2619 default:
2620 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002621 break;
2622 }
2623
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002624 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002625 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002626 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002627 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002628 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002629}
2630
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002631status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2632 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2633 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002634{
2635 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002636 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002637 case PIXEL_FORMAT_TRANSPARENT:
2638 case PIXEL_FORMAT_TRANSLUCENT:
2639 format = PIXEL_FORMAT_RGBA_8888;
2640 break;
2641 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002642 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002643 break;
2644 }
2645
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002646 *outLayer = new Layer(this, client, name, w, h, flags);
2647 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2648 if (err == NO_ERROR) {
2649 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002650 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002651 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002652
2653 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2654 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002655}
2656
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002657status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2658 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2659 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002660{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002661 *outLayer = new LayerDim(this, client, name, w, h, flags);
2662 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002663 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002664 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002665}
2666
Mathias Agopianac9fa422013-02-11 16:40:36 -08002667status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002668{
Mathias Agopian67106042013-03-14 19:18:13 -07002669 // called by the window manager when it wants to remove a Layer
2670 status_t err = NO_ERROR;
2671 sp<Layer> l(client->getLayerUser(handle));
2672 if (l != NULL) {
2673 err = removeLayer(l);
2674 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2675 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002676 }
2677 return err;
2678}
2679
Mathias Agopian13127d82013-03-05 17:47:11 -08002680status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002681{
Mathias Agopian67106042013-03-14 19:18:13 -07002682 // called by ~LayerCleaner() when all references to the IBinder (handle)
2683 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002684 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002685 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002686 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002687 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002688 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002689 "error removing layer=%p (%s)", l.get(), strerror(-err));
2690 }
2691 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002692}
2693
Mathias Agopianb60314a2012-04-10 22:09:54 -07002694// ---------------------------------------------------------------------------
2695
Andy McFadden13a082e2012-08-24 10:16:42 -07002696void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002697 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002698 Vector<ComposerState> state;
2699 Vector<DisplayState> displays;
2700 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002701 d.what = DisplayState::eDisplayProjectionChanged |
2702 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002703 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002704 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002705 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002706 d.frame.makeInvalid();
2707 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002708 d.width = 0;
2709 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002710 displays.add(d);
2711 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002712 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002713
2714 const nsecs_t period =
2715 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2716 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002717}
2718
2719void SurfaceFlinger::initializeDisplays() {
2720 class MessageScreenInitialized : public MessageBase {
2721 SurfaceFlinger* flinger;
2722 public:
2723 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2724 virtual bool handler() {
2725 flinger->onInitializeDisplays();
2726 return true;
2727 }
2728 };
2729 sp<MessageBase> msg = new MessageScreenInitialized(this);
2730 postMessageAsync(msg); // we may be called from main thread, use async message
2731}
2732
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002733void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2734 int mode) {
2735 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2736 this);
2737 int32_t type = hw->getDisplayType();
2738 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002739
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002740 if (mode == currentMode) {
2741 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002742 return;
2743 }
2744
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002745 hw->setPowerMode(mode);
2746 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2747 ALOGW("Trying to set power mode for virtual display");
2748 return;
2749 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002750
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002751 if (currentMode == HWC_POWER_MODE_OFF) {
2752 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002753 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2754 // FIXME: eventthread only knows about the main display right now
2755 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002756 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002757 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002758
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002759 mVisibleRegionsDirty = true;
2760 repaintEverything();
2761 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002762 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002763 disableHardwareVsync(true); // also cancels any in-progress resync
2764
Mathias Agopiancde87a32012-09-13 14:09:01 -07002765 // FIXME: eventthread only knows about the main display right now
2766 mEventThread->onScreenReleased();
2767 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002768
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002769 getHwComposer().setPowerMode(type, mode);
2770 mVisibleRegionsDirty = true;
2771 // from this point on, SF will stop drawing on this display
2772 } else {
2773 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002774 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002775}
2776
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002777void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2778 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002779 SurfaceFlinger& mFlinger;
2780 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002781 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002782 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002783 MessageSetPowerMode(SurfaceFlinger& flinger,
2784 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2785 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002786 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002787 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002788 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002789 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002790 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002791 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002792 ALOGW("Attempt to set power mode = %d for virtual display",
2793 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002794 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002795 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002796 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002797 return true;
2798 }
2799 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002800 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002801 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002802}
2803
2804// ---------------------------------------------------------------------------
2805
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002806status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2807{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002808 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002809
Mathias Agopianbd115332013-04-18 16:41:04 -07002810 IPCThreadState* ipc = IPCThreadState::self();
2811 const int pid = ipc->getCallingPid();
2812 const int uid = ipc->getCallingUid();
2813 if ((uid != AID_SHELL) &&
2814 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002815 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002816 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002817 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002818 // Try to get the main lock, but don't insist if we can't
2819 // (this would indicate SF is stuck, but we want to be able to
2820 // print something in dumpsys).
2821 int retry = 3;
2822 while (mStateLock.tryLock()<0 && --retry>=0) {
2823 usleep(1000000);
2824 }
2825 const bool locked(retry >= 0);
2826 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002827 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002828 "SurfaceFlinger appears to be unresponsive, "
2829 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002830 }
2831
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002832 bool dumpAll = true;
2833 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002834 size_t numArgs = args.size();
2835 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002836 if ((index < numArgs) &&
2837 (args[index] == String16("--list"))) {
2838 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002839 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002840 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002841 }
2842
2843 if ((index < numArgs) &&
2844 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002845 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002846 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002847 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002848 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002849
2850 if ((index < numArgs) &&
2851 (args[index] == String16("--latency-clear"))) {
2852 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002853 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002854 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002855 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002856
2857 if ((index < numArgs) &&
2858 (args[index] == String16("--dispsync"))) {
2859 index++;
2860 mPrimaryDispSync.dump(result);
2861 dumpAll = false;
2862 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002863 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002864
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002865 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002866 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002867 }
2868
Mathias Agopian9795c422009-08-26 16:36:26 -07002869 if (locked) {
2870 mStateLock.unlock();
2871 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002872 }
2873 write(fd, result.string(), result.size());
2874 return NO_ERROR;
2875}
2876
Dan Stozac7014012014-02-14 15:03:43 -08002877void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2878 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002879{
2880 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2881 const size_t count = currentLayers.size();
2882 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002883 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002884 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002885 }
2886}
2887
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002888void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002889 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002890{
2891 String8 name;
2892 if (index < args.size()) {
2893 name = String8(args[index]);
2894 index++;
2895 }
2896
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002897 const nsecs_t period =
2898 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002899 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002900
2901 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002902 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002903 } else {
2904 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2905 const size_t count = currentLayers.size();
2906 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002907 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002908 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002909 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002910 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002911 }
2912 }
2913}
2914
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002915void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002916 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002917{
2918 String8 name;
2919 if (index < args.size()) {
2920 name = String8(args[index]);
2921 index++;
2922 }
2923
2924 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2925 const size_t count = currentLayers.size();
2926 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002927 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002928 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002929 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002930 }
2931 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002932
Svetoslavd85084b2014-03-20 10:28:31 -07002933 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002934}
2935
Jamie Gennis6547ff42013-07-16 20:12:42 -07002936// This should only be called from the main thread. Otherwise it would need
2937// the lock and should use mCurrentState rather than mDrawingState.
2938void SurfaceFlinger::logFrameStats() {
2939 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2940 const size_t count = drawingLayers.size();
2941 for (size_t i=0 ; i<count ; i++) {
2942 const sp<Layer>& layer(drawingLayers[i]);
2943 layer->logFrameStats();
2944 }
2945
2946 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2947}
2948
Andy McFadden4803b742012-09-24 19:07:20 -07002949/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2950{
2951 static const char* config =
2952 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002953#ifdef HAS_CONTEXT_PRIORITY
2954 " HAS_CONTEXT_PRIORITY"
2955#endif
2956#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2957 " NEVER_DEFAULT_TO_ASYNC_MODE"
2958#endif
2959#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2960 " TARGET_DISABLE_TRIPLE_BUFFERING"
2961#endif
2962 "]";
2963 result.append(config);
2964}
2965
Mathias Agopian74d211a2013-04-22 16:55:35 +02002966void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2967 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002968{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002969 bool colorize = false;
2970 if (index < args.size()
2971 && (args[index] == String16("--color"))) {
2972 colorize = true;
2973 index++;
2974 }
2975
2976 Colorizer colorizer(colorize);
2977
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002978 // figure out if we're stuck somewhere
2979 const nsecs_t now = systemTime();
2980 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2981 const nsecs_t inTransaction(mDebugInTransaction);
2982 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2983 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2984
2985 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002986 * Dump library configuration.
2987 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002988
2989 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002990 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002991 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002992 appendSfConfigString(result);
2993 appendUiConfigString(result);
2994 appendGuiConfigString(result);
2995 result.append("\n");
2996
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002997 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002998 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002999 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07003000 result.append(SyncFeatures::getInstance().toString());
3001 result.append("\n");
3002
Andy McFadden41d67d72014-04-25 16:58:34 -07003003 colorizer.bold(result);
3004 result.append("DispSync configuration: ");
3005 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07003006 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
3007 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07003008 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
3009 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
3010 result.append("\n");
3011
Andy McFadden4803b742012-09-24 19:07:20 -07003012 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003013 * Dump the visible layer list
3014 */
3015 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
3016 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003017 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08003018 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003019 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003020 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08003021 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003022 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003023 }
3024
3025 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07003026 * Dump Display state
3027 */
3028
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003029 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08003030 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003031 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07003032 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
3033 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02003034 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07003035 }
3036
3037 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003038 * Dump SurfaceFlinger global state
3039 */
3040
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003041 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02003042 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003043 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003044
Mathias Agopian888c8222012-08-04 21:10:38 -07003045 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07003046 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07003047
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003048 colorizer.bold(result);
3049 result.appendFormat("EGL implementation : %s\n",
3050 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
3051 colorizer.reset(result);
3052 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07003053 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07003054
Mathias Agopian875d8e12013-06-07 15:35:48 -07003055 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003056
Mathias Agopian42977342012-08-05 00:40:46 -07003057 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07003058 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
3059 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02003060 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003061 " last eglSwapBuffers() time: %f us\n"
3062 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08003063 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003064 " refresh-rate : %f fps\n"
3065 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07003066 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07003067 " gpu_to_cpu_unsupported : %d\n"
3068 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003069 mLastSwapBufferTime/1000.0,
3070 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08003071 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07003072 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
3073 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07003074 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07003075 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003076
Mathias Agopian74d211a2013-04-22 16:55:35 +02003077 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003078 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003079
Mathias Agopian74d211a2013-04-22 16:55:35 +02003080 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003081 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003082
3083 /*
3084 * VSYNC state
3085 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02003086 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003087
3088 /*
3089 * Dump HWComposer state
3090 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003091 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02003092 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02003093 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02003094 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003095 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07003096 (mDebugDisableHWC || mDebugRegion || mDaltonize
3097 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02003098 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003099
3100 /*
3101 * Dump gralloc state
3102 */
3103 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
3104 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08003105}
3106
Mathias Agopian13127d82013-03-05 17:47:11 -08003107const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07003108SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07003109 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07003110 wp<IBinder> dpy;
3111 for (size_t i=0 ; i<mDisplays.size() ; i++) {
3112 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
3113 dpy = mDisplays.keyAt(i);
3114 break;
3115 }
3116 }
3117 if (dpy == NULL) {
3118 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
3119 // Just use the primary display so we have something to return
3120 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
3121 }
3122 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07003123}
3124
Keun young Park63f165f2012-08-31 10:53:36 -07003125bool SurfaceFlinger::startDdmConnection()
3126{
3127 void* libddmconnection_dso =
3128 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
3129 if (!libddmconnection_dso) {
3130 return false;
3131 }
3132 void (*DdmConnection_start)(const char* name);
3133 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07003134 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07003135 if (!DdmConnection_start) {
3136 dlclose(libddmconnection_dso);
3137 return false;
3138 }
3139 (*DdmConnection_start)(getServiceName());
3140 return true;
3141}
3142
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003143status_t SurfaceFlinger::onTransact(
3144 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
3145{
3146 switch (code) {
3147 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07003148 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07003149 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003150 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07003151 case CLEAR_ANIMATION_FRAME_STATS:
3152 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07003153 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003154 {
3155 // codes that require permission check
3156 IPCThreadState* ipc = IPCThreadState::self();
3157 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07003158 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07003159 if ((uid != AID_GRAPHICS) &&
3160 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00003161 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07003162 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
3163 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003164 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003165 break;
3166 }
3167 case CAPTURE_SCREEN:
3168 {
3169 // codes that require permission check
3170 IPCThreadState* ipc = IPCThreadState::self();
3171 const int pid = ipc->getCallingPid();
3172 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07003173 if ((uid != AID_GRAPHICS) &&
3174 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00003175 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003176 "can't read framebuffer pid=%d, uid=%d", pid, uid);
3177 return PERMISSION_DENIED;
3178 }
3179 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003180 }
3181 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003182
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003183 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
3184 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07003185 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08003186 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07003187 IPCThreadState* ipc = IPCThreadState::self();
3188 const int pid = ipc->getCallingPid();
3189 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00003190 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07003191 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003192 return PERMISSION_DENIED;
3193 }
3194 int n;
3195 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07003196 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07003197 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003198 return NO_ERROR;
3199 case 1002: // SHOW_UPDATES
3200 n = data.readInt32();
3201 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07003202 invalidateHwcGeometry();
3203 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003204 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003205 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07003206 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003207 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003208 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003209 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07003210 setTransactionFlags(
3211 eTransactionNeeded|
3212 eDisplayTransactionNeeded|
3213 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003214 return NO_ERROR;
3215 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08003216 case 1006:{ // send empty update
3217 signalRefresh();
3218 return NO_ERROR;
3219 }
Mathias Agopian53331da2011-08-22 21:44:41 -07003220 case 1008: // toggle use of hw composer
3221 n = data.readInt32();
3222 mDebugDisableHWC = n ? 1 : 0;
3223 invalidateHwcGeometry();
3224 repaintEverything();
3225 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07003226 case 1009: // toggle use of transform hint
3227 n = data.readInt32();
3228 mDebugDisableTransformHint = n ? 1 : 0;
3229 invalidateHwcGeometry();
3230 repaintEverything();
3231 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003232 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07003233 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003234 reply->writeInt32(0);
3235 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07003236 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08003237 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003238 return NO_ERROR;
3239 case 1013: {
3240 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07003241 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
3242 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07003243 return NO_ERROR;
3244 }
3245 case 1014: {
3246 // daltonize
3247 n = data.readInt32();
3248 switch (n % 10) {
3249 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
3250 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
3251 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
3252 }
3253 if (n >= 10) {
3254 mDaltonizer.setMode(Daltonizer::correction);
3255 } else {
3256 mDaltonizer.setMode(Daltonizer::simulation);
3257 }
3258 mDaltonize = n > 0;
3259 invalidateHwcGeometry();
3260 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07003261 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003262 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003263 case 1015: {
3264 // apply a color matrix
3265 n = data.readInt32();
3266 mHasColorMatrix = n ? 1 : 0;
3267 if (n) {
3268 // color matrix is sent as mat3 matrix followed by vec3
3269 // offset, then packed into a mat4 where the last row is
3270 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07003271 for (size_t i = 0 ; i < 4; i++) {
3272 for (size_t j = 0; j < 4; j++) {
3273 mColorMatrix[i][j] = data.readFloat();
3274 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003275 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003276 } else {
3277 mColorMatrix = mat4();
3278 }
3279 invalidateHwcGeometry();
3280 repaintEverything();
3281 return NO_ERROR;
3282 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003283 // This is an experimental interface
3284 // Needs to be shifted to proper binder interface when we productize
3285 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07003286 n = data.readInt32();
3287 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003288 return NO_ERROR;
3289 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003290 }
3291 }
3292 return err;
3293}
3294
Mathias Agopian53331da2011-08-22 21:44:41 -07003295void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07003296 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08003297 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07003298}
3299
Mathias Agopian59119e62010-10-11 12:37:43 -07003300// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003301// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07003302// ---------------------------------------------------------------------------
3303
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003304/* The code below is here to handle b/8734824
3305 *
3306 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07003307 * from the surfaceflinger thread to the calling binder thread, where they
3308 * are executed. This allows the calling thread in the calling process to be
3309 * reused and not depend on having "enough" binder threads to handle the
3310 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003311 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003312class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07003313 /* Parts of GraphicProducerWrapper are run on two different threads,
3314 * communicating by sending messages via Looper but also by shared member
3315 * data. Coherence maintenance is subtle and in places implicit (ugh).
3316 *
3317 * Don't rely on Looper's sendMessage/handleMessage providing
3318 * release/acquire semantics for any data not actually in the Message.
3319 * Data going from surfaceflinger to binder threads needs to be
3320 * synchronized explicitly.
3321 *
3322 * Barrier open/wait do provide release/acquire semantics. This provides
3323 * implicit synchronization for data coming back from binder to
3324 * surfaceflinger threads.
3325 */
3326
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003327 sp<IGraphicBufferProducer> impl;
3328 sp<Looper> looper;
3329 status_t result;
3330 bool exitPending;
3331 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07003332 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003333 uint32_t code;
3334 Parcel const* data;
3335 Parcel* reply;
3336
3337 enum {
3338 MSG_API_CALL,
3339 MSG_EXIT
3340 };
3341
3342 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003343 * Called on surfaceflinger thread. This is called by our "fake"
3344 * BpGraphicBufferProducer. We package the data and reply Parcel and
3345 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003346 */
3347 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08003348 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003349 this->code = code;
3350 this->data = &data;
3351 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003352 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07003353 // if we've exited, we run the message synchronously right here.
3354 // note (JH): as far as I can tell from looking at the code, this
3355 // never actually happens. if it does, i'm not sure if it happens
3356 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003357 handleMessage(Message(MSG_API_CALL));
3358 } else {
3359 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07003360 // Prevent stores to this->{code, data, reply} from being
3361 // reordered later than the construction of Message.
3362 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003363 looper->sendMessage(this, Message(MSG_API_CALL));
3364 barrier.wait();
3365 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08003366 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003367 }
3368
3369 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003370 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003371 * call the real BpGraphicBufferProducer.
3372 */
3373 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07003374 int what = message.what;
3375 // Prevent reads below from happening before the read from Message
3376 atomic_thread_fence(memory_order_acquire);
3377 if (what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08003378 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003379 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07003380 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003381 exitRequested = true;
3382 }
3383 }
3384
3385public:
Jesse Hallb154c422014-07-13 12:47:02 -07003386 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
3387 : impl(impl),
3388 looper(new Looper(true)),
3389 exitPending(false),
3390 exitRequested(false)
3391 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003392
Jesse Hallb154c422014-07-13 12:47:02 -07003393 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003394 status_t waitForResponse() {
3395 do {
3396 looper->pollOnce(-1);
3397 } while (!exitRequested);
3398 return result;
3399 }
3400
Jesse Hallb154c422014-07-13 12:47:02 -07003401 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003402 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07003403 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003404 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07003405 // Ensure this->result is visible to the binder thread before it
3406 // handles the message.
3407 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003408 looper->sendMessage(this, Message(MSG_EXIT));
3409 }
3410};
3411
3412
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003413status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
3414 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003415 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003416 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003417 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003418
3419 if (CC_UNLIKELY(display == 0))
3420 return BAD_VALUE;
3421
3422 if (CC_UNLIKELY(producer == 0))
3423 return BAD_VALUE;
3424
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003425 // if we have secure windows on this display, never allow the screen capture
3426 // unless the producer interface is local (i.e.: we can take a screenshot for
3427 // ourselves).
3428 if (!producer->asBinder()->localBinder()) {
3429 Mutex::Autolock _l(mStateLock);
3430 sp<const DisplayDevice> hw(getDisplayDevice(display));
3431 if (hw->getSecureLayerVisible()) {
3432 ALOGW("FB is protected: PERMISSION_DENIED");
3433 return PERMISSION_DENIED;
3434 }
3435 }
3436
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003437 // Convert to surfaceflinger's internal rotation type.
3438 Transform::orientation_flags rotationFlags;
3439 switch (rotation) {
3440 case ISurfaceComposer::eRotateNone:
3441 rotationFlags = Transform::ROT_0;
3442 break;
3443 case ISurfaceComposer::eRotate90:
3444 rotationFlags = Transform::ROT_90;
3445 break;
3446 case ISurfaceComposer::eRotate180:
3447 rotationFlags = Transform::ROT_180;
3448 break;
3449 case ISurfaceComposer::eRotate270:
3450 rotationFlags = Transform::ROT_270;
3451 break;
3452 default:
3453 rotationFlags = Transform::ROT_0;
3454 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3455 break;
3456 }
3457
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003458 class MessageCaptureScreen : public MessageBase {
3459 SurfaceFlinger* flinger;
3460 sp<IBinder> display;
3461 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003462 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003463 uint32_t reqWidth, reqHeight;
3464 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003465 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003466 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003467 status_t result;
3468 public:
3469 MessageCaptureScreen(SurfaceFlinger* flinger,
3470 const sp<IBinder>& display,
3471 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003472 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003473 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003474 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003475 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003476 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003477 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003478 useIdentityTransform(useIdentityTransform),
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003479 rotation(rotation),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003480 result(PERMISSION_DENIED)
3481 {
3482 }
3483 status_t getResult() const {
3484 return result;
3485 }
3486 virtual bool handler() {
3487 Mutex::Autolock _l(flinger->mStateLock);
3488 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003489 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003490 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003491 useIdentityTransform, rotation);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003492 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003493 return true;
3494 }
3495 };
3496
Mathias Agopian9eb1f052013-04-10 16:27:17 -07003497 // make sure to process transactions before screenshots -- a transaction
3498 // might already be pending but scheduled for VSYNC; this guarantees we
3499 // will handle it before the screenshot. When VSYNC finally arrives
3500 // the scheduled transaction will be a no-op. If no transactions are
3501 // scheduled at this time, this will end-up being a no-op as well.
3502 mEventQueue.invalidateTransactionNow();
3503
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003504 // this creates a "fake" BBinder which will serve as a "fake" remote
3505 // binder to receive the marshaled calls and forward them to the
3506 // real remote (a BpGraphicBufferProducer)
3507 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3508
3509 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3510 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003511 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003512 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003513 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003514 useIdentityTransform, rotationFlags);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003515
3516 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003517 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003518 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003519 }
3520 return res;
3521}
3522
Mathias Agopian180f10d2013-04-10 22:55:41 -07003523
3524void SurfaceFlinger::renderScreenImplLocked(
3525 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003526 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003527 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003528 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003529{
3530 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003531 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003532
3533 // get screen geometry
3534 const uint32_t hw_w = hw->getWidth();
3535 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003536 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
3537
Dan Stozac1879002014-05-22 15:59:05 -07003538 // if a default or invalid sourceCrop is passed in, set reasonable values
3539 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3540 !sourceCrop.isValid()) {
3541 sourceCrop.setLeftTop(Point(0, 0));
3542 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3543 }
3544
3545 // ensure that sourceCrop is inside screen
3546 if (sourceCrop.left < 0) {
3547 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3548 }
Jesse Hall78442112014-08-07 22:43:06 -07003549 if (static_cast<uint32_t>(sourceCrop.right) > hw_w) {
Dan Stozabe31f442014-06-11 11:20:54 -07003550 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003551 }
3552 if (sourceCrop.top < 0) {
3553 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3554 }
Jesse Hall78442112014-08-07 22:43:06 -07003555 if (static_cast<uint32_t>(sourceCrop.bottom) > hw_h) {
Dan Stozabe31f442014-06-11 11:20:54 -07003556 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003557 }
3558
Mathias Agopian180f10d2013-04-10 22:55:41 -07003559 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003560 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003561
Prabhanjan Kandula5eb682f2014-11-06 17:58:04 +05303562 if (DisplayDevice::DISPLAY_PRIMARY == hw->getDisplayType() &&
3563 hw->isPanelInverseMounted()) {
3564 rotation = (Transform::orientation_flags)
3565 (rotation ^ Transform::ROT_180);
3566 }
3567
Mathias Agopian180f10d2013-04-10 22:55:41 -07003568 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003569 engine.setViewportAndProjection(
3570 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003571 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003572
3573 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003574 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003575
3576 const LayerVector& layers( mDrawingState.layersSortedByZ );
3577 const size_t count = layers.size();
3578 for (size_t i=0 ; i<count ; ++i) {
3579 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003580 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003581 if (state.layerStack == hw->getLayerStack()) {
3582 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
Ramkumar Radhakrishnandb7ca4d2013-08-30 18:37:55 -07003583#ifdef QCOM_BSP
3584 // dont render the secure Display Layer
3585 if(layer->isSecureDisplay()) {
3586 continue;
3587 }
3588#endif
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07003589#ifdef QCOM_BSP
3590 int dispType = hw->getDisplayType();
3591 // Dont let ext_only and extended_mode to be captured
3592 // If not, we would see incorrect image during rotatoin
3593 // on primary
3594 if (layer->isVisible() &&
3595 not (!dispType && (layer->isExtOnly() ||
3596 (isExtendedMode() && layer->isYuvLayer())))) {
3597#else
Mathias Agopian180f10d2013-04-10 22:55:41 -07003598 if (layer->isVisible()) {
Arun Kumar K.R766d5f62013-06-21 19:36:02 -07003599#endif
Mathias Agopian180f10d2013-04-10 22:55:41 -07003600 if (filtering) layer->setFiltering(true);
Ramakant Singhf9af0e32014-01-07 14:17:52 +05303601 if(!layer->isProtected())
3602 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003603 if (filtering) layer->setFiltering(false);
3604 }
3605 }
3606 }
3607 }
3608
3609 // compositionComplete is needed for older driver
3610 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003611 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003612}
3613
3614
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003615status_t SurfaceFlinger::captureScreenImplLocked(
3616 const sp<const DisplayDevice>& hw,
3617 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003618 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003619 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003620 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003621{
3622 ATRACE_CALL();
3623
Mathias Agopian180f10d2013-04-10 22:55:41 -07003624 // get screen geometry
3625 const uint32_t hw_w = hw->getWidth();
3626 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003627
Mathias Agopian180f10d2013-04-10 22:55:41 -07003628 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3629 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3630 reqWidth, reqHeight, hw_w, hw_h);
3631 return BAD_VALUE;
3632 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003633
Mathias Agopian180f10d2013-04-10 22:55:41 -07003634 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3635 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3636
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003637 // create a surface (because we're a producer, and we need to
3638 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003639 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003640 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003641
3642 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003643 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003644 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3645 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003646
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003647 int err = 0;
3648 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003649 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003650 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3651 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003652
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003653 if (err == NO_ERROR) {
3654 ANativeWindowBuffer* buffer;
3655 /* TODO: Once we have the sync framework everywhere this can use
3656 * server-side waits on the fence that dequeueBuffer returns.
3657 */
3658 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3659 if (result == NO_ERROR) {
3660 // create an EGLImage from the buffer so we can later
3661 // turn it into a texture
3662 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3663 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3664 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003665 // this binds the given EGLImage as a framebuffer for the
3666 // duration of this scope.
3667 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3668 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003669 // this will in fact render into our dequeued buffer
3670 // via an FBO, which means we didn't have to create
3671 // an EGLSurface and therefore we're not
3672 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003673 renderScreenImplLocked(
3674 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3675 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003676
Riley Andrews86639902014-08-15 12:27:24 -07003677 // Attempt to create a sync khr object that can produce a sync point. If that
3678 // isn't available, create a non-dupable sync object in the fallback path and
3679 // wait on it directly.
3680 EGLSyncKHR sync;
3681 if (!DEBUG_SCREENSHOTS) {
3682 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Riley Andrews9707f4d2014-10-23 16:17:04 -07003683 // native fence fd will not be populated until flush() is done.
3684 getRenderEngine().flush();
Andy McFadden2d8d1202013-10-09 16:38:02 -07003685 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003686 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003687 }
Riley Andrews86639902014-08-15 12:27:24 -07003688 if (sync != EGL_NO_SYNC_KHR) {
Anupam Sakargayan6610a0a2014-10-27 17:08:43 +05303689 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
Riley Andrews86639902014-08-15 12:27:24 -07003690 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
Anupam Sakargayan6610a0a2014-10-27 17:08:43 +05303691 EGLint eglErr = eglGetError();
3692 eglDestroySyncKHR(mEGLDisplay, sync);
3693 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3694 ALOGW("captureScreen: fence wait timed out");
Riley Andrews86639902014-08-15 12:27:24 -07003695 } else {
Anupam Sakargayan6610a0a2014-10-27 17:08:43 +05303696 ALOGW_IF(eglErr != EGL_SUCCESS,
3697 "captureScreen: error waiting on EGL fence: %#x", eglErr);
Riley Andrews86639902014-08-15 12:27:24 -07003698 }
Anupam Sakargayan6610a0a2014-10-27 17:08:43 +05303699 } else {
3700 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3701 // not fatal
Riley Andrews86639902014-08-15 12:27:24 -07003702 }
Anupam Sakargayan6610a0a2014-10-27 17:08:43 +05303703
Mathias Agopiand5556842013-09-19 17:08:37 -07003704 if (DEBUG_SCREENSHOTS) {
3705 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3706 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3707 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3708 hw, minLayerZ, maxLayerZ);
3709 delete [] pixels;
3710 }
3711
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003712 } else {
3713 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3714 result = INVALID_OPERATION;
3715 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003716 // destroy our image
3717 eglDestroyImageKHR(mEGLDisplay, image);
3718 } else {
3719 result = BAD_VALUE;
3720 }
Anupam Sakargayan6610a0a2014-10-27 17:08:43 +05303721 window->queueBuffer(window, buffer, -1);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003722 }
3723 } else {
3724 result = BAD_VALUE;
3725 }
3726 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3727 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003728
Mathias Agopian74c40c02010-09-29 13:02:36 -07003729 return result;
3730}
3731
Mathias Agopiand5556842013-09-19 17:08:37 -07003732void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3733 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003734 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003735 for (size_t y=0 ; y<h ; y++) {
3736 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3737 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003738 if (p[x] != 0xFF000000) return;
3739 }
3740 }
3741 ALOGE("*** we just took a black screenshot ***\n"
3742 "requested minz=%d, maxz=%d, layerStack=%d",
3743 minLayerZ, maxLayerZ, hw->getLayerStack());
3744 const LayerVector& layers( mDrawingState.layersSortedByZ );
3745 const size_t count = layers.size();
3746 for (size_t i=0 ; i<count ; ++i) {
3747 const sp<Layer>& layer(layers[i]);
3748 const Layer::State& state(layer->getDrawingState());
3749 const bool visible = (state.layerStack == hw->getLayerStack())
3750 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3751 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003752 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003753 visible ? '+' : '-',
3754 i, layer->getName().string(), state.layerStack, state.z,
3755 layer->isVisible(), state.flags, state.alpha);
3756 }
3757 }
3758}
3759
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003760// ---------------------------------------------------------------------------
3761
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003762SurfaceFlinger::LayerVector::LayerVector() {
3763}
3764
3765SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003766 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003767}
3768
3769int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3770 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003771{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003772 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003773 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3774 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003775
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003776 uint32_t ls = l->getCurrentState().layerStack;
3777 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003778 if (ls != rs)
3779 return ls - rs;
3780
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003781 uint32_t lz = l->getCurrentState().z;
3782 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003783 if (lz != rz)
3784 return lz - rz;
3785
3786 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003787}
3788
3789// ---------------------------------------------------------------------------
3790
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003791SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Michael Lentine47e45402014-07-18 15:34:25 -07003792 : type(DisplayDevice::DISPLAY_ID_INVALID), width(0), height(0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003793}
3794
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003795SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Michael Lentine47e45402014-07-18 15:34:25 -07003796 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0), width(0), height(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003797 viewport.makeInvalid();
3798 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003799}
3800
3801// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003802
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003803}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003804
3805
3806#if defined(__gl_h_)
3807#error "don't include gl/gl.h in this file"
3808#endif
3809
3810#if defined(__gl2_h_)
3811#error "don't include gl2/gl2.h in this file"
3812#endif