blob: 4b39f7076ecc16c4dd15d936bef59f3b622879b9 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Greg Hackmann86efcc02014-03-07 12:44:02 -080024#include <inttypes.h>
Jesse Hallb154c422014-07-13 12:47:02 -070025#include <stdatomic.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070026
27#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036
Mathias Agopianc666cae2012-07-25 18:56:13 -070037#include <ui/DisplayInfo.h>
Lajos Molnar67d8bd62014-09-11 14:58:45 -070038#include <ui/DisplayStatInfo.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070039
Mathias Agopian921e6ac2012-07-23 23:11:29 -070040#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070041#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070042#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070043#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080044#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080045#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070046
47#include <ui/GraphicBufferAllocator.h>
48#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070049#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080050
Mathias Agopiancde87a32012-09-13 14:09:01 -070051#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include <utils/String8.h>
53#include <utils/String16.h>
54#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080055#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopian921e6ac2012-07-23 23:11:29 -070057#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070058#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
Mathias Agopian3e25fd82013-04-22 17:52:16 +020060#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020062#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080063#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070064#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070065#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070066#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080067#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071
Mathias Agopiana4912602012-07-12 14:25:33 -070072#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070073#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070074#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075
Mathias Agopianff2ed702013-09-01 21:36:12 -070076#include "Effects/Daltonizer.h"
77
Mathias Agopian875d8e12013-06-07 15:35:48 -070078#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070079#include <cutils/compiler.h>
Raj Kamal366d9b42014-06-11 13:49:32 +053080#ifdef QCOM_BSP
81#include <gralloc_priv.h>
82#endif
Mathias Agopian875d8e12013-06-07 15:35:48 -070083
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -070084#ifdef QCOM_BSP
85#include <display_config.h>
86#endif
87
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088#define DISPLAY_COUNT 1
89
Mathias Agopianfee2b462013-07-03 12:34:01 -070090/*
91 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
92 * black pixels.
93 */
94#define DEBUG_SCREENSHOTS false
95
Mathias Agopianca088332013-03-28 17:44:13 -070096EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
97
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070099
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700100// This is the phase offset in nanoseconds of the software vsync event
101// relative to the vsync event reported by HWComposer. The software vsync
102// event is when SurfaceFlinger and Choreographer-based applications run each
103// frame.
104//
105// This phase offset allows adjustment of the minimum latency from application
106// wake-up (by Choregographer) time to the time at which the resulting window
107// image is displayed. This value may be either positive (after the HW vsync)
108// or negative (before the HW vsync). Setting it to 0 will result in a
109// minimum latency of two vsync periods because the app and SurfaceFlinger
110// will run just after the HW vsync. Setting it to a positive number will
111// result in the minimum latency being:
112//
113// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
114//
115// Note that reducing this latency makes it more likely for the applications
116// to not have their window content image ready in time. When this happens
117// the latency will end up being an additional vsync period, and animations
118// will hiccup. Therefore, this latency should be tuned somewhat
119// conservatively (or at least with awareness of the trade-off being made).
120static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
121
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700122// This is the phase offset at which SurfaceFlinger's composition runs.
123static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
124
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125// ---------------------------------------------------------------------------
126
Mathias Agopian99b49842011-06-27 16:05:52 -0700127const String16 sHardwareTest("android.permission.HARDWARE_TEST");
128const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
129const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
130const String16 sDump("android.permission.DUMP");
131
132// ---------------------------------------------------------------------------
133
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700135 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700137 mTransactionPending(false),
138 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700139 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700140 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700141 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700144 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800145 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700147 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700148 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700149 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700150 mDebugInSwapBuffers(0),
151 mLastSwapBufferTime(0),
152 mDebugInTransaction(0),
153 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700154 mBootFinished(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700155 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700156 mHWVsyncAvailable(false),
Alan Viverette9c5a3332013-09-12 20:04:35 -0700157 mDaltonize(false),
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +0530158 mHasColorMatrix(false),
159 mGpuTileRenderEnable(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160{
Steve Blocka19954a2012-01-04 20:05:49 +0000161 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162
163 // debugging stuff...
164 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700165
Mathias Agopianb4b17302013-03-20 18:36:41 -0700166 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700167 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700168
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 property_get("debug.sf.showupdates", value, "0");
170 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700171
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700172 property_get("debug.sf.ddms", value, "0");
173 mDebugDDMS = atoi(value);
174 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700175 if (!startDdmConnection()) {
176 // start failed, and DDMS debugging not enabled
177 mDebugDDMS = 0;
178 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700179 }
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +0530180#ifdef QCOM_BSP
181 property_get("debug.sf.gpu_comp_tiling", value, "1");
182 mGpuTileRenderEnable = atoi(value) ? true : false;
183 if(mGpuTileRenderEnable)
184 ALOGV("DirtyRect optimization enabled for FULL GPU Composition");
185#endif
186
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700187 ALOGI_IF(mDebugRegion, "showupdates enabled");
188 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189}
190
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800191void SurfaceFlinger::onFirstRef()
192{
193 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800194}
195
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196SurfaceFlinger::~SurfaceFlinger()
197{
Mathias Agopiana4912602012-07-12 14:25:33 -0700198 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
199 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
200 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201}
202
Dan Stozac7014012014-02-14 15:03:43 -0800203void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800204{
205 // the window manager died on us. prepare its eulogy.
206
Andy McFadden13a082e2012-08-24 10:16:42 -0700207 // restore initial conditions (default device unblank, etc)
208 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800209
210 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700211 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800212}
213
Mathias Agopian7e27f052010-05-28 14:22:23 -0700214sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215{
Mathias Agopian96f08192010-06-02 23:28:45 -0700216 sp<ISurfaceComposerClient> bclient;
217 sp<Client> client(new Client(this));
218 status_t err = client->initCheck();
219 if (err == NO_ERROR) {
220 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800222 return bclient;
223}
224
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700225sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
226 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700227{
228 class DisplayToken : public BBinder {
229 sp<SurfaceFlinger> flinger;
230 virtual ~DisplayToken() {
231 // no more references, this display must be terminated
232 Mutex::Autolock _l(flinger->mStateLock);
233 flinger->mCurrentState.displays.removeItem(this);
234 flinger->setTransactionFlags(eDisplayTransactionNeeded);
235 }
236 public:
237 DisplayToken(const sp<SurfaceFlinger>& flinger)
238 : flinger(flinger) {
239 }
240 };
241
242 sp<BBinder> token = new DisplayToken(this);
243
244 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700245 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700246 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700247 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700248 mCurrentState.displays.add(token, info);
249
250 return token;
251}
252
Jesse Hall6c913be2013-08-08 12:15:49 -0700253void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
254 Mutex::Autolock _l(mStateLock);
255
256 ssize_t idx = mCurrentState.displays.indexOfKey(display);
257 if (idx < 0) {
258 ALOGW("destroyDisplay: invalid display token");
259 return;
260 }
261
262 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
263 if (!info.isVirtualDisplay()) {
264 ALOGE("destroyDisplay called for non-virtual display");
265 return;
266 }
267
268 mCurrentState.displays.removeItemsAt(idx);
269 setTransactionFlags(eDisplayTransactionNeeded);
270}
271
Jesse Hall692c7232012-11-08 15:41:56 -0800272void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
273 ALOGW_IF(mBuiltinDisplays[type],
274 "Overwriting display token for display type %d", type);
275 mBuiltinDisplays[type] = new BBinder();
276 DisplayDeviceState info(type);
277 // All non-virtual displays are currently considered secure.
278 info.isSecure = true;
279 mCurrentState.displays.add(mBuiltinDisplays[type], info);
280}
281
Mathias Agopiane57f2922012-08-09 16:29:12 -0700282sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700283 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700284 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
285 return NULL;
286 }
Jesse Hall692c7232012-11-08 15:41:56 -0800287 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700288}
289
Jamie Gennis9a78c902011-01-12 18:30:40 -0800290sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
291{
292 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
293 return gba;
294}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700295
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800296void SurfaceFlinger::bootFinished()
297{
298 const nsecs_t now = systemTime();
299 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000300 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700301 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700302
303 // wait patiently for the window manager death
304 const String16 name("window");
305 sp<IBinder> window(defaultServiceManager()->getService(name));
306 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700307 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700308 }
309
310 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700311 // formerly we would just kill the process, but we now ask it to exit so it
312 // can choose where to stop the animation.
313 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800314}
315
Mathias Agopian3f844832013-08-07 21:24:32 -0700316void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700317 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700318 RenderEngine& engine;
319 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700320 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700321 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
322 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700323 }
324 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700325 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700326 return true;
327 }
328 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700329 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700330}
331
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700332class DispSyncSource : public VSyncSource, private DispSync::Callback {
333public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700334 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
335 const char* label) :
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700336 mValue(0),
337 mPhaseOffset(phaseOffset),
338 mTraceVsync(traceVsync),
Andy McFadden5167ec62014-05-22 13:08:43 -0700339 mVsyncOnLabel(String8::format("VsyncOn-%s", label)),
340 mVsyncEventLabel(String8::format("VSYNC-%s", label)),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700341 mDispSync(dispSync) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700342
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700343 virtual ~DispSyncSource() {}
344
345 virtual void setVSyncEnabled(bool enable) {
346 // Do NOT lock the mutex here so as to avoid any mutex ordering issues
347 // with locking it in the onDispSyncEvent callback.
348 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700349 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700350 static_cast<DispSync::Callback*>(this));
351 if (err != NO_ERROR) {
352 ALOGE("error registering vsync callback: %s (%d)",
353 strerror(-err), err);
354 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700355 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700356 } else {
357 status_t err = mDispSync->removeEventListener(
358 static_cast<DispSync::Callback*>(this));
359 if (err != NO_ERROR) {
360 ALOGE("error unregistering vsync callback: %s (%d)",
361 strerror(-err), err);
362 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700363 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700364 }
365 }
366
367 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
368 Mutex::Autolock lock(mMutex);
369 mCallback = callback;
370 }
371
372private:
373 virtual void onDispSyncEvent(nsecs_t when) {
374 sp<VSyncSource::Callback> callback;
375 {
376 Mutex::Autolock lock(mMutex);
377 callback = mCallback;
378
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700379 if (mTraceVsync) {
380 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700381 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700382 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700383 }
384
385 if (callback != NULL) {
386 callback->onVSyncEvent(when);
387 }
388 }
389
390 int mValue;
391
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700392 const nsecs_t mPhaseOffset;
393 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700394 const String8 mVsyncOnLabel;
395 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700396
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700397 DispSync* mDispSync;
398 sp<VSyncSource::Callback> mCallback;
399 Mutex mMutex;
400};
401
402void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700403 ALOGI( "SurfaceFlinger's main thread ready to run. "
404 "Initializing graphics H/W...");
405
Jesse Hallb65f32e2013-09-27 22:10:47 -0700406 status_t err;
Jesse Hall692c7232012-11-08 15:41:56 -0800407 Mutex::Autolock _l(mStateLock);
408
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700409 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700410 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
411 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700412
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700413 // Initialize the H/W composer object. There may or may not be an
414 // actual hardware composer underneath.
415 mHwc = new HWComposer(this,
416 *static_cast<HWComposer::EventHandler *>(this));
417
Mathias Agopian875d8e12013-06-07 15:35:48 -0700418 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800419 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700420
421 // retrieve the EGL context that was selected/created
422 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700423
Mathias Agopianda27af92012-09-13 18:17:13 -0700424 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
425 "couldn't create EGLContext");
426
Mathias Agopiancde87a32012-09-13 14:09:01 -0700427 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700428 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700429 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700430 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700431 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700432 // All non-virtual displays are currently considered secure.
433 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800434 createBuiltinDisplayLocked(type);
435 wp<IBinder> token = mBuiltinDisplays[i];
436
Dan Stozab9b08832014-03-13 11:55:57 -0700437 sp<IGraphicBufferProducer> producer;
438 sp<IGraphicBufferConsumer> consumer;
439 BufferQueue::createBufferQueue(&producer, &consumer,
440 new GraphicBufferAlloc());
441
442 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
443 consumer);
Jesse Hall19e87292013-12-23 21:02:15 -0800444 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700445 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800446 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Dan Stozab9b08832014-03-13 11:55:57 -0700447 fbs, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -0800448 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700449 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700450 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700451 // for displays other than the main display, so we always
452 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800453 ALOGD("marking display %zu as acquired/unblanked", i);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700454 hw->setPowerMode(HWC_POWER_MODE_NORMAL);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700455 }
456 mDisplays.add(token, hw);
457 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700458 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700459
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700460 // make the GLContext current so that we can create textures when creating Layers
461 // (which may happens before we render something)
462 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
463
Mathias Agopian028508c2012-07-25 21:12:12 -0700464 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700465 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700466 vsyncPhaseOffsetNs, true, "app");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700467 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700468 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700469 sfVsyncPhaseOffsetNs, true, "sf");
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700470 mSFEventThread = new EventThread(sfVsyncSrc);
471 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700472
Jamie Gennisd1700752013-10-14 12:22:52 -0700473 mEventControlThread = new EventControlThread(this);
474 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
475
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700476 // set a fake vsync period if there is no HWComposer
477 if (mHwc->initCheck() != NO_ERROR) {
478 mPrimaryDispSync.setPeriod(16666667);
479 }
480
Mathias Agopian92a979a2012-08-02 18:32:23 -0700481 // initialize our drawing state
482 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700483
Andy McFadden13a082e2012-08-24 10:16:42 -0700484 // set initial conditions (e.g. unblank default device)
485 initializeDisplays();
486
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700487 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700488 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800489}
490
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700491int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700492 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700493 type : mHwc->allocateDisplayId();
494}
495
Mathias Agopiana67e4182012-06-19 17:26:12 -0700496void SurfaceFlinger::startBootAnim() {
497 // start boot animation
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -0700498 mBootFinished = false;
Mathias Agopiana67e4182012-06-19 17:26:12 -0700499 property_set("service.bootanim.exit", "0");
500 property_set("ctl.start", "bootanim");
501}
502
Mathias Agopian875d8e12013-06-07 15:35:48 -0700503size_t SurfaceFlinger::getMaxTextureSize() const {
504 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700505}
506
Mathias Agopian875d8e12013-06-07 15:35:48 -0700507size_t SurfaceFlinger::getMaxViewportDims() const {
508 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700509}
510
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800511// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800512
Jamie Gennis582270d2011-08-17 18:19:00 -0700513bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800514 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800515 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800516 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700517 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800518}
519
Dan Stoza7f7da322014-05-02 15:26:25 -0700520status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
521 Vector<DisplayInfo>* configs) {
522 if (configs == NULL) {
523 return BAD_VALUE;
524 }
525
Jesse Hall692c7232012-11-08 15:41:56 -0800526 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700527 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800528 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700529 type = i;
530 break;
531 }
532 }
533
534 if (type < 0) {
535 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700536 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700537
Mathias Agopian8b736f12012-08-13 17:54:26 -0700538 // TODO: Not sure if display density should handled by SF any longer
539 class Density {
540 static int getDensityFromProperty(char const* propName) {
541 char property[PROPERTY_VALUE_MAX];
542 int density = 0;
543 if (property_get(propName, property, NULL) > 0) {
544 density = atoi(property);
545 }
546 return density;
547 }
548 public:
549 static int getEmuDensity() {
550 return getDensityFromProperty("qemu.sf.lcd_density"); }
551 static int getBuildDensity() {
552 return getDensityFromProperty("ro.sf.lcd_density"); }
553 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700554
Dan Stoza7f7da322014-05-02 15:26:25 -0700555 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700556
Dan Stoza7f7da322014-05-02 15:26:25 -0700557 const Vector<HWComposer::DisplayConfig>& hwConfigs =
558 getHwComposer().getConfigs(type);
559 for (size_t c = 0; c < hwConfigs.size(); ++c) {
560 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
561 DisplayInfo info = DisplayInfo();
562
563 float xdpi = hwConfig.xdpi;
564 float ydpi = hwConfig.ydpi;
565
566 if (type == DisplayDevice::DISPLAY_PRIMARY) {
567 // The density of the device is provided by a build property
568 float density = Density::getBuildDensity() / 160.0f;
569 if (density == 0) {
570 // the build doesn't provide a density -- this is wrong!
571 // use xdpi instead
572 ALOGE("ro.sf.lcd_density must be defined as a build property");
573 density = xdpi / 160.0f;
574 }
575 if (Density::getEmuDensity()) {
576 // if "qemu.sf.lcd_density" is specified, it overrides everything
577 xdpi = ydpi = density = Density::getEmuDensity();
578 density /= 160.0f;
579 }
580 info.density = density;
581
582 // TODO: this needs to go away (currently needed only by webkit)
583 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
584 info.orientation = hw->getOrientation();
585 } else {
586 // TODO: where should this value come from?
587 static const int TV_DENSITY = 213;
588 info.density = TV_DENSITY / 160.0f;
589 info.orientation = 0;
590 }
591
592 info.w = hwConfig.width;
593 info.h = hwConfig.height;
594 info.xdpi = xdpi;
595 info.ydpi = ydpi;
596 info.fps = float(1e9 / hwConfig.refresh);
Andy McFadden91b2ca82014-06-13 14:04:23 -0700597 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
598
599 // This is how far in advance a buffer must be queued for
600 // presentation at a given time. If you want a buffer to appear
601 // on the screen at time N, you must submit the buffer before
602 // (N - presentationDeadline).
603 //
604 // Normally it's one full refresh period (to give SF a chance to
605 // latch the buffer), but this can be reduced by configuring a
606 // DispSync offset. Any additional delays introduced by the hardware
607 // composer or panel must be accounted for here.
608 //
609 // We add an additional 1ms to allow for processing time and
610 // differences between the ideal and actual refresh rate.
611 info.presentationDeadline =
612 hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700613
614 // All non-virtual displays are currently considered secure.
615 info.secure = true;
616
617 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700618 }
619
Dan Stoza7f7da322014-05-02 15:26:25 -0700620 return NO_ERROR;
621}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700622
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700623status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& display,
624 DisplayStatInfo* stats) {
625 if (stats == NULL) {
626 return BAD_VALUE;
627 }
628
629 // FIXME for now we always return stats for the primary display
630 memset(stats, 0, sizeof(*stats));
631 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
632 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
633 return NO_ERROR;
634}
635
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700636int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
637 return getDisplayDevice(display)->getActiveConfig();
Dan Stoza7f7da322014-05-02 15:26:25 -0700638}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700639
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700640void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
641 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
642 this);
643 int32_t type = hw->getDisplayType();
644 int currentMode = hw->getActiveConfig();
645
646 if (mode == currentMode) {
647 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
648 return;
649 }
650
651 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
652 ALOGW("Trying to set config for virtual display");
653 return;
654 }
655
656 hw->setActiveConfig(mode);
657 getHwComposer().setActiveConfig(type, mode);
658}
659
660status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
661 class MessageSetActiveConfig: public MessageBase {
662 SurfaceFlinger& mFlinger;
663 sp<IBinder> mDisplay;
664 int mMode;
665 public:
666 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
667 int mode) :
668 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
669 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700670 Vector<DisplayInfo> configs;
671 mFlinger.getDisplayConfigs(mDisplay, &configs);
672 if(mMode < 0 || mMode >= configs.size()) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700673 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700674 mMode, configs.size());
675 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700676 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
677 if (hw == NULL) {
678 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700679 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700680 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
681 ALOGW("Attempt to set active config = %d for virtual display",
682 mMode);
683 } else {
684 mFlinger.setActiveConfigInternal(hw, mMode);
685 }
686 return true;
687 }
688 };
689 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
690 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700691 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700692}
693
Svetoslavd85084b2014-03-20 10:28:31 -0700694status_t SurfaceFlinger::clearAnimationFrameStats() {
695 Mutex::Autolock _l(mStateLock);
696 mAnimFrameTracker.clearStats();
697 return NO_ERROR;
698}
699
700status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
701 Mutex::Autolock _l(mStateLock);
702 mAnimFrameTracker.getStats(outStats);
703 return NO_ERROR;
704}
705
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800706// ----------------------------------------------------------------------------
707
708sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800709 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700710}
711
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800712// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800713
714void SurfaceFlinger::waitForEvent() {
715 mEventQueue.waitMessage();
716}
717
718void SurfaceFlinger::signalTransaction() {
719 mEventQueue.invalidate();
720}
721
722void SurfaceFlinger::signalLayerUpdate() {
723 mEventQueue.invalidate();
724}
725
726void SurfaceFlinger::signalRefresh() {
727 mEventQueue.refresh();
728}
729
730status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800731 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800732 return mEventQueue.postMessage(msg, reltime);
733}
734
735status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800736 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800737 status_t res = mEventQueue.postMessage(msg, reltime);
738 if (res == NO_ERROR) {
739 msg->wait();
740 }
741 return res;
742}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800743
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700744void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700745 do {
746 waitForEvent();
747 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800748}
749
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700750void SurfaceFlinger::enableHardwareVsync() {
751 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700752 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700753 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700754 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
755 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700756 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700757 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700758}
759
Jesse Hall948fe0c2013-10-14 12:56:09 -0700760void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700761 Mutex::Autolock _l(mHWVsyncLock);
762
Jesse Hall948fe0c2013-10-14 12:56:09 -0700763 if (makeAvailable) {
764 mHWVsyncAvailable = true;
765 } else if (!mHWVsyncAvailable) {
766 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
767 return;
768 }
769
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700770 const nsecs_t period =
771 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
772
773 mPrimaryDispSync.reset();
774 mPrimaryDispSync.setPeriod(period);
775
776 if (!mPrimaryHWVsyncEnabled) {
777 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700778 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
779 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700780 mPrimaryHWVsyncEnabled = true;
781 }
782}
783
Jesse Hall948fe0c2013-10-14 12:56:09 -0700784void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700785 Mutex::Autolock _l(mHWVsyncLock);
786 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700787 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
788 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700789 mPrimaryDispSync.endResync();
790 mPrimaryHWVsyncEnabled = false;
791 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700792 if (makeUnavailable) {
793 mHWVsyncAvailable = false;
794 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700795}
796
797void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700798 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700799
Jamie Gennisd1700752013-10-14 12:22:52 -0700800 { // Scope for the lock
801 Mutex::Autolock _l(mHWVsyncLock);
802 if (type == 0 && mPrimaryHWVsyncEnabled) {
803 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700804 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700805 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700806
807 if (needsHwVsync) {
808 enableHardwareVsync();
809 } else {
810 disableHardwareVsync(false);
811 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700812}
813
814void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
815 if (mEventThread == NULL) {
816 // This is a temporary workaround for b/7145521. A non-null pointer
817 // does not mean EventThread has finished initializing, so this
818 // is not a correct fix.
819 ALOGW("WARNING: EventThread not started, ignoring hotplug");
820 return;
821 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700822
Jesse Hall9e663de2013-08-16 14:28:37 -0700823 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700824 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800825 if (connected) {
826 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700827 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800828 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
829 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700830 }
831 setTransactionFlags(eDisplayTransactionNeeded);
832
Andy McFadden9e9689c2012-10-10 18:17:51 -0700833 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700834 }
Mathias Agopian86303202012-07-24 22:46:10 -0700835}
836
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700837void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700838 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700839 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700840}
841
Mathias Agopian4fec8732012-06-29 14:12:52 -0700842void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800843 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800844 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700845 case MessageQueue::TRANSACTION:
846 handleMessageTransaction();
847 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700848 case MessageQueue::INVALIDATE:
849 handleMessageTransaction();
850 handleMessageInvalidate();
851 signalRefresh();
852 break;
853 case MessageQueue::REFRESH:
854 handleMessageRefresh();
855 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800856 }
857}
858
Mathias Agopian4fec8732012-06-29 14:12:52 -0700859void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700860 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700861 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700862 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700863 }
864}
865
866void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700867 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700868 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700869}
870
871void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700872 ATRACE_CALL();
873 preComposition();
874 rebuildLayerStacks();
875 setUpHWComposer();
876 doDebugFlashRegions();
877 doComposition();
878 postComposition();
879}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700880
Mathias Agopiancd60f992012-08-16 16:28:27 -0700881void SurfaceFlinger::doDebugFlashRegions()
882{
883 // is debugging enabled
884 if (CC_LIKELY(!mDebugRegion))
885 return;
886
887 const bool repaintEverything = mRepaintEverything;
888 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
889 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700890 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700891 // transform the dirty region into this screen's coordinate space
892 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
893 if (!dirtyRegion.isEmpty()) {
894 // redraw the whole screen
895 doComposeSurfaces(hw, Region(hw->bounds()));
896
897 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700898 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700899 RenderEngine& engine(getRenderEngine());
900 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
901
Mathias Agopiancd60f992012-08-16 16:28:27 -0700902 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700903 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700904 }
905 }
906 }
907
908 postFramebuffer();
909
910 if (mDebugRegion > 1) {
911 usleep(mDebugRegion * 1000);
912 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700913
914 HWComposer& hwc(getHwComposer());
915 if (hwc.initCheck() == NO_ERROR) {
916 status_t err = hwc.prepare();
917 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
918 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700919}
920
921void SurfaceFlinger::preComposition()
922{
923 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700924 const LayerVector& layers(mDrawingState.layersSortedByZ);
925 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700926 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700927 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700928 needExtraInvalidate = true;
929 }
930 }
931 if (needExtraInvalidate) {
932 signalLayerUpdate();
933 }
934}
935
936void SurfaceFlinger::postComposition()
937{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700938 const LayerVector& layers(mDrawingState.layersSortedByZ);
939 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700940 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700941 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700942 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800943
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700944 const HWComposer& hwc = getHwComposer();
945 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
946
947 if (presentFence->isValid()) {
948 if (mPrimaryDispSync.addPresentFence(presentFence)) {
949 enableHardwareVsync();
950 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700951 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700952 }
953 }
954
Andy McFadden5167ec62014-05-22 13:08:43 -0700955 if (kIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700956 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700957 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700958 enableHardwareVsync();
959 }
960 }
961
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800962 if (mAnimCompositionPending) {
963 mAnimCompositionPending = false;
964
Jesse Halla9a1b002013-02-27 16:39:25 -0800965 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800966 mAnimFrameTracker.setActualPresentFence(presentFence);
967 } else {
968 // The HWC doesn't support present fences, so use the refresh
969 // timestamp instead.
970 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
971 mAnimFrameTracker.setActualPresentTime(presentTime);
972 }
973 mAnimFrameTracker.advanceFrame();
974 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700975}
976
977void SurfaceFlinger::rebuildLayerStacks() {
978 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700979 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700980 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700981 mVisibleRegionsDirty = false;
982 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700983
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700984 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700985 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700986 Region opaqueRegion;
987 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800988 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700989 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700990 const Transform& tr(hw->getTransform());
991 const Rect bounds(hw->getBounds());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700992 if (hw->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700993 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700994 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700995
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700996 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700997 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700998 const sp<Layer>& layer(layers[i]);
999 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001000 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -07001001 Region drawRegion(tr.transform(
1002 layer->visibleNonTransparentRegion));
1003 drawRegion.andSelf(bounds);
1004 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001005 layersSortedByZ.add(layer);
1006 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001007 }
1008 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001009 }
Mathias Agopian42977342012-08-05 00:40:46 -07001010 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001011 hw->undefinedRegion.set(bounds);
1012 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
1013 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001014 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001015 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001016}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001017
Mathias Agopiancd60f992012-08-16 16:28:27 -07001018void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001019 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001020 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1021 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1022 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1023
1024 // If nothing has changed (!dirty), don't recompose.
1025 // If something changed, but we don't currently have any visible layers,
1026 // and didn't when we last did a composition, then skip it this time.
1027 // The second rule does two things:
1028 // - When all layers are removed from a display, we'll emit one black
1029 // frame, then nothing more until we get new layers.
1030 // - When a display is created with a private layer stack, we won't
1031 // emit any black frames until a layer is added to the layer stack.
1032 bool mustRecompose = dirty && !(empty && wasEmpty);
1033
1034 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1035 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1036 mustRecompose ? "doing" : "skipping",
1037 dirty ? "+" : "-",
1038 empty ? "+" : "-",
1039 wasEmpty ? "+" : "-");
1040
Dan Stoza71433162014-02-04 16:22:36 -08001041 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001042
1043 if (mustRecompose) {
1044 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1045 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001046 }
1047
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001048 HWComposer& hwc(getHwComposer());
1049 if (hwc.initCheck() == NO_ERROR) {
1050 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001051 if (CC_UNLIKELY(mHwWorkListDirty)) {
1052 mHwWorkListDirty = false;
1053 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1054 sp<const DisplayDevice> hw(mDisplays[dpy]);
1055 const int32_t id = hw->getHwcDisplayId();
1056 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001057 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001058 hw->getVisibleLayersSortedByZ());
1059 const size_t count = currentLayers.size();
1060 if (hwc.createWorkList(id, count) == NO_ERROR) {
1061 HWComposer::LayerListIterator cur = hwc.begin(id);
1062 const HWComposer::LayerListIterator end = hwc.end(id);
1063 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001064 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001065 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001066 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001067 cur->setSkip(true);
1068 }
1069 }
1070 }
1071 }
1072 }
1073 }
1074
1075 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001076 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001077 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001078 const int32_t id = hw->getHwcDisplayId();
1079 if (id >= 0) {
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001080 // Get the layers in the current drawying state
1081 const LayerVector& layers(mDrawingState.layersSortedByZ);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001082#ifdef QCOM_BSP
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001083 bool freezeSurfacePresent = false;
1084 const size_t layerCount = layers.size();
1085 char value[PROPERTY_VALUE_MAX];
1086 property_get("sys.disable_ext_animation", value, "0");
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001087 if(atoi(value) && (id != HWC_DISPLAY_PRIMARY)) {
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001088 for (size_t i = 0 ; i < layerCount ; ++i) {
1089 static int screenShotLen = strlen("ScreenshotSurface");
1090 const sp<Layer>& layer(layers[i]);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001091 const Layer::State& s(layer->getDrawingState());
1092 // check the layers associated with external display
1093 if(s.layerStack == hw->getLayerStack()) {
1094 if(!strncmp(layer->getName(), "ScreenshotSurface",
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001095 screenShotLen)) {
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001096 // Screenshot layer is present, and animation in
1097 // progress
1098 freezeSurfacePresent = true;
1099 break;
1100 }
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001101 }
1102 }
1103 }
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001104#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001105 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001106 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001107 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001108 HWComposer::LayerListIterator cur = hwc.begin(id);
1109 const HWComposer::LayerListIterator end = hwc.end(id);
1110 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1111 /*
1112 * update the per-frame h/w composer data for each layer
1113 * and build the transparent region of the FB
1114 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001115 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001116 layer->setPerFrameData(hw, *cur);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001117#ifdef QCOM_BSP
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001118 if(freezeSurfacePresent) {
1119 // if freezeSurfacePresent, set ANIMATING flag
1120 cur->setAnimating(true);
1121 } else {
1122 const KeyedVector<wp<IBinder>, DisplayDeviceState>&
1123 draw(mDrawingState.displays);
1124 size_t dc = draw.size();
1125 for (size_t i=0 ; i<dc ; i++) {
1126 if (draw[i].isMainDisplay()) {
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001127 // Pass the current orientation to HWC
1128 hwc.eventControl(HWC_DISPLAY_PRIMARY,
1129 SurfaceFlinger::EVENT_ORIENTATION,
1130 uint32_t(draw[i].orientation));
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001131 }
1132 }
1133 }
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001134#endif
Mathias Agopian1e260872012-08-08 18:35:12 -07001135 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001136 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001137 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001138
Riley Andrews03414a12014-07-01 14:22:59 -07001139 // If possible, attempt to use the cursor overlay on each display.
1140 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1141 sp<const DisplayDevice> hw(mDisplays[dpy]);
1142 const int32_t id = hw->getHwcDisplayId();
1143 if (id >= 0) {
1144 const Vector< sp<Layer> >& currentLayers(
1145 hw->getVisibleLayersSortedByZ());
1146 const size_t count = currentLayers.size();
1147 HWComposer::LayerListIterator cur = hwc.begin(id);
1148 const HWComposer::LayerListIterator end = hwc.end(id);
1149 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1150 const sp<Layer>& layer(currentLayers[i]);
1151 if (layer->isPotentialCursor()) {
1152 cur->setIsCursorLayerHint();
1153 break;
1154 }
1155 }
1156 }
1157 }
1158
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001159 status_t err = hwc.prepare();
1160 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001161
1162 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1163 sp<const DisplayDevice> hw(mDisplays[dpy]);
1164 hw->prepareFrame(hwc);
1165 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001166 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001167}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001168
Mathias Agopiancd60f992012-08-16 16:28:27 -07001169void SurfaceFlinger::doComposition() {
1170 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001171 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001172 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001173 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001174 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001175 // transform the dirty region into this screen's coordinate space
1176 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001177
1178 // repaint the framebuffer (if needed)
1179 doDisplayComposition(hw, dirtyRegion);
1180
Mathias Agopiancd60f992012-08-16 16:28:27 -07001181 hw->dirtyRegion.clear();
1182 hw->flip(hw->swapRegion);
1183 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001184 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001185 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001186 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001187 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001188 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001189}
1190
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001191void SurfaceFlinger::postFramebuffer()
1192{
Mathias Agopian841cde52012-03-01 15:44:37 -08001193 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001194
Mathias Agopiana44b0412011-10-16 18:46:35 -07001195 const nsecs_t now = systemTime();
1196 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001197
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001198 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001199 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001200 if (!hwc.supportsFramebufferTarget()) {
1201 // EGL spec says:
1202 // "surface must be bound to the calling thread's current context,
1203 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001204 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001205 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001206 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001207 }
1208
Mathias Agopian6da15f42013-09-25 20:40:07 -07001209 // make the default display current because the VirtualDisplayDevice code cannot
1210 // deal with dequeueBuffer() being called outside of the composition loop; however
1211 // the code below can call glFlush() which is allowed (and does in some case) call
1212 // dequeueBuffer().
1213 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1214
Mathias Agopian92a979a2012-08-02 18:32:23 -07001215 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001216 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001217 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001218 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001219 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001220 int32_t id = hw->getHwcDisplayId();
1221 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001222 HWComposer::LayerListIterator cur = hwc.begin(id);
1223 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001224 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001225 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001226 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001227 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001228 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001229 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001230 }
Jesse Hallef194142012-06-14 14:45:17 -07001231 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001232 }
1233
Mathias Agopiana44b0412011-10-16 18:46:35 -07001234 mLastSwapBufferTime = systemTime() - now;
1235 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001236
1237 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1238 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1239 logFrameStats();
1240 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001241}
1242
Mathias Agopian87baae12012-07-31 12:38:26 -07001243void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001244{
Mathias Agopian841cde52012-03-01 15:44:37 -08001245 ATRACE_CALL();
1246
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001247 // here we keep a copy of the drawing state (that is the state that's
1248 // going to be overwritten by handleTransactionLocked()) outside of
1249 // mStateLock so that the side-effects of the State assignment
1250 // don't happen with mStateLock held (which can cause deadlocks).
1251 State drawingState(mDrawingState);
1252
Mathias Agopianca4d3602011-05-19 15:38:14 -07001253 Mutex::Autolock _l(mStateLock);
1254 const nsecs_t now = systemTime();
1255 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256
Mathias Agopianca4d3602011-05-19 15:38:14 -07001257 // Here we're guaranteed that some transaction flags are set
1258 // so we can call handleTransactionLocked() unconditionally.
1259 // We call getTransactionFlags(), which will also clear the flags,
1260 // with mStateLock held to guarantee that mCurrentState won't change
1261 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001262
Mathias Agopiane57f2922012-08-09 16:29:12 -07001263 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001264 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001265
Mathias Agopianca4d3602011-05-19 15:38:14 -07001266 mLastTransactionTime = systemTime() - now;
1267 mDebugInTransaction = 0;
1268 invalidateHwcGeometry();
1269 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001270}
1271
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301272void SurfaceFlinger::setVirtualDisplayData(
1273 int32_t hwcDisplayId,
1274 const sp<IGraphicBufferProducer>& sink)
1275{
1276 sp<ANativeWindow> mNativeWindow = new Surface(sink);
1277 ANativeWindow* const window = mNativeWindow.get();
1278
1279 int format;
1280 window->query(window, NATIVE_WINDOW_FORMAT, &format);
1281
1282 EGLSurface surface;
1283 EGLint w, h;
1284 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1285 surface = eglCreateWindowSurface(display, mRenderEngine->getEGLConfig(), window, NULL);
1286 eglQuerySurface(display, surface, EGL_WIDTH, &w);
1287 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
1288
1289 mHwc->setVirtualDisplayProperties(hwcDisplayId, w, h, format);
1290}
1291
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001292void SurfaceFlinger::configureVirtualDisplay(int32_t &hwcDisplayId,
1293 sp<DisplaySurface> &dispSurface,
1294 sp<IGraphicBufferProducer> &producer,
1295 const DisplayDeviceState state,
1296 sp<IGraphicBufferProducer> bqProducer,
1297 sp<IGraphicBufferConsumer> bqConsumer)
1298{
1299 bool vdsEnabled = mHwc->isVDSEnabled();
1300
1301 //for V4L2 based virtual display implementation
1302 if(!vdsEnabled) {
1303 // persist.sys.wfd.virtual will be set if WFD is launched via
1304 // settings app. This is currently being done in
1305 // ExtendedRemoteDisplay-WFD stack.
1306 // This flag will be reset at the time of disconnection of virtual WFD
1307 // display.
1308 // This flag is set to zero if WFD is launched via QCOM WFD
1309 // proprietary APIs which use HDMI piggyback approach.
1310 char value[PROPERTY_VALUE_MAX];
1311 property_get("persist.sys.wfd.virtual", value, "0");
1312 int wfdVirtual = atoi(value);
1313 if(!wfdVirtual) {
1314 // This is for non-wfd virtual display scenarios(e.g. SSD/SR/CTS)
1315 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001316 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1317 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001318 dispSurface = vds;
1319 // There won't be any interaction with HWC for this virtual display.
1320 // so the GLES driver can pass buffers directly to the sink.
1321 producer = state.surface;
1322 } else {
Raj Kamal366d9b42014-06-11 13:49:32 +05301323 int sinkUsage = -1;
1324 state.surface->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
1325#ifdef QCOM_BSP
1326 if(sinkUsage & GRALLOC_USAGE_PRIVATE_WFD)
1327#endif
1328 hwcDisplayId = allocateHwcDisplayId(state.type);
1329
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001330 if (hwcDisplayId >= 0) {
1331 // This is for WFD virtual display scenario.
1332 // Read virtual display properties and create a
1333 // rendering surface for it inorder to be handled by hwc.
1334 setVirtualDisplayData(hwcDisplayId, state.surface);
1335 dispSurface = new FramebufferSurface(*mHwc, state.type,
1336 bqConsumer);
1337 producer = bqProducer;
1338 } else {
1339 // in case of WFD Virtual + SSD/SR concurrency scenario,
1340 // WFD virtual display instance gets valid hwcDisplayId and
1341 // SSD/SR will get invalid hwcDisplayId
1342 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001343 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1344 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001345 dispSurface = vds;
1346 // There won't be any interaction with HWC for this virtual
1347 // display, so the GLES driver can pass buffers directly to the
1348 // sink.
1349 producer = state.surface;
1350 }
1351 }
1352 } else {
1353 // VDS solution is enabled
1354 // HWC is allocated for first virtual display.
1355 // Subsequent virtual display sessions will be composed by GLES driver.
1356 // ToDo: Modify VDS component to allocate hwcDisplayId based on
1357 // mForceHwcCopy (which is based on Usage Flags)
1358
1359 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001360 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1361 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001362 dispSurface = vds;
1363 if (hwcDisplayId >= 0) {
1364 producer = vds;
1365 } else {
1366 // There won't be any interaction with HWC for this virtual display,
1367 // so the GLES driver can pass buffers directly to the sink.
1368 producer = state.surface;
1369 }
1370 }
1371}
1372
Mathias Agopian87baae12012-07-31 12:38:26 -07001373void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001374{
1375 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001376 const size_t count = currentLayers.size();
1377
1378 /*
1379 * Traversal of the children
1380 * (perform the transaction for each of them if needed)
1381 */
1382
Mathias Agopian3559b072012-08-15 13:46:03 -07001383 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001384 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001385 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1387 if (!trFlags) continue;
1388
1389 const uint32_t flags = layer->doTransaction(0);
1390 if (flags & Layer::eVisibleRegion)
1391 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001392 }
1393 }
1394
1395 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001396 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397 */
1398
Mathias Agopiane57f2922012-08-09 16:29:12 -07001399 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001400 // here we take advantage of Vector's copy-on-write semantics to
1401 // improve performance by skipping the transaction entirely when
1402 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001403 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1404 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001405 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001407 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001408 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001409
1410 // find the displays that were removed
1411 // (ie: in drawing state but not in current state)
1412 // also handle displays that changed
1413 // (ie: displays that are in both lists)
1414 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001415 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1416 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001417 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001418 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001419 // Call makeCurrent() on the primary display so we can
1420 // be sure that nothing associated with this display
1421 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001422 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001423 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001424 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1425 if (hw != NULL)
1426 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001427 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001428 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001429 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001430 } else {
1431 ALOGW("trying to remove the main display");
1432 }
1433 } else {
1434 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001435 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001436 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001437 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001438 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001439 // recreating the DisplayDevice, so we just remove it
1440 // from the drawing state, so that it get re-added
1441 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001442 sp<DisplayDevice> hw(getDisplayDevice(display));
1443 if (hw != NULL)
1444 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001445 mDisplays.removeItem(display);
1446 mDrawingState.displays.removeItemsAt(i);
1447 dc--; i--;
1448 // at this point we must loop to the next item
1449 continue;
1450 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001451
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001452 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001453 if (disp != NULL) {
1454 if (state.layerStack != draw[i].layerStack) {
1455 disp->setLayerStack(state.layerStack);
1456 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001457 if ((state.orientation != draw[i].orientation)
1458 || (state.viewport != draw[i].viewport)
1459 || (state.frame != draw[i].frame))
1460 {
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001461#ifdef QCOM_BSP
1462 int orient = state.orientation;
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001463 // Honor the orientation change after boot
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301464 // animation completes and make sure boot
1465 // animation is shown in panel orientation always.
1466 if(mBootFinished){
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001467 disp->setProjection(state.orientation,
1468 state.viewport, state.frame);
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001469 orient = state.orientation;
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001470 }
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301471 else{
1472 char property[PROPERTY_VALUE_MAX];
1473 int panelOrientation =
1474 DisplayState::eOrientationDefault;
1475 if(property_get("persist.panel.orientation",
1476 property, "0") > 0){
1477 panelOrientation = atoi(property) / 90;
1478 }
1479 disp->setProjection(panelOrientation,
1480 state.viewport, state.frame);
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001481 orient = panelOrientation;
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301482 }
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001483 // Set the view frame of each display only of its
1484 // default orientation.
Saurabh Shah027b3852014-08-21 16:09:32 -07001485 if(orient == DisplayState::eOrientationDefault and
1486 state.frame.isValid()) {
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001487 qdutils::setViewFrame(disp->getHwcDisplayId(),
1488 state.frame.left, state.frame.top,
1489 state.frame.right, state.frame.bottom);
1490 }
1491#else
1492 disp->setProjection(state.orientation,
1493 state.viewport, state.frame);
1494#endif
Mathias Agopian93997a82012-08-29 17:30:36 -07001495 }
Michael Lentine47e45402014-07-18 15:34:25 -07001496 if (state.width != draw[i].width || state.height != draw[i].height) {
1497 disp->setDisplaySize(state.width, state.height);
1498 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001499 }
1500 }
1501 }
1502
1503 // find displays that were added
1504 // (ie: in current state but not in drawing state)
1505 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001506 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001507 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001508
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001509 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001510 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001511 sp<IGraphicBufferProducer> bqProducer;
1512 sp<IGraphicBufferConsumer> bqConsumer;
1513 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1514 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001515
Jesse Hall02d86562013-03-25 14:43:23 -07001516 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001517 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001518 // Virtual displays without a surface are dormant:
1519 // they have external state (layer stack, projection,
1520 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001521 if (state.surface != NULL) {
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001522 configureVirtualDisplay(hwcDisplayId,
1523 dispSurface, producer, state, bqProducer,
1524 bqConsumer);
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001525 }
1526 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001527 ALOGE_IF(state.surface!=NULL,
1528 "adding a supported display, but rendering "
1529 "surface is provided (%p), ignoring it",
1530 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001531 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001532 // for supported (by hwc) displays we provide our
1533 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001534 dispSurface = new FramebufferSurface(*mHwc, state.type,
1535 bqConsumer);
1536 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001537 }
1538
1539 const wp<IBinder>& display(curr.keyAt(i));
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301540 if (dispSurface != NULL && producer != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001541 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001542 state.type, hwcDisplayId,
1543 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001544 display, dispSurface, producer,
1545 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001546 hw->setLayerStack(state.layerStack);
1547 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001548 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001549 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001550 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001551 if (state.isVirtualDisplay()) {
1552 if (hwcDisplayId >= 0) {
1553 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1554 hw->getWidth(), hw->getHeight(),
1555 hw->getFormat());
1556 }
1557 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001558 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001559 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001560 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001561 }
1562 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001563 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001564 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001565
Mathias Agopian84300952012-11-21 16:02:13 -08001566 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1567 // The transform hint might have changed for some layers
1568 // (either because a display has changed, or because a layer
1569 // as changed).
1570 //
1571 // Walk through all the layers in currentLayers,
1572 // and update their transform hint.
1573 //
1574 // If a layer is visible only on a single display, then that
1575 // display is used to calculate the hint, otherwise we use the
1576 // default display.
1577 //
1578 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1579 // the hint is set before we acquire a buffer from the surface texture.
1580 //
1581 // NOTE: layer transactions have taken place already, so we use their
1582 // drawing state. However, SurfaceFlinger's own transaction has not
1583 // happened yet, so we must use the current state layer list
1584 // (soon to become the drawing state list).
1585 //
1586 sp<const DisplayDevice> disp;
1587 uint32_t currentlayerStack = 0;
1588 for (size_t i=0; i<count; i++) {
1589 // NOTE: we rely on the fact that layers are sorted by
1590 // layerStack first (so we don't have to traverse the list
1591 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001592 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001593 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001594 if (i==0 || currentlayerStack != layerStack) {
1595 currentlayerStack = layerStack;
1596 // figure out if this layerstack is mirrored
1597 // (more than one display) if so, pick the default display,
1598 // if not, pick the only display it's on.
1599 disp.clear();
1600 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1601 sp<const DisplayDevice> hw(mDisplays[dpy]);
1602 if (hw->getLayerStack() == currentlayerStack) {
1603 if (disp == NULL) {
1604 disp = hw;
1605 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001606 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001607 break;
1608 }
1609 }
1610 }
1611 }
Chet Haase91d25932013-04-11 15:24:55 -07001612 if (disp == NULL) {
1613 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1614 // redraw after transform hint changes. See bug 8508397.
1615
1616 // could be null when this layer is using a layerStack
1617 // that is not visible on any display. Also can occur at
1618 // screen off/on times.
1619 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001620 }
Chet Haase91d25932013-04-11 15:24:55 -07001621 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001622 }
1623 }
1624
1625
Mathias Agopian3559b072012-08-15 13:46:03 -07001626 /*
1627 * Perform our own transaction if needed
1628 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001629
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001630 const LayerVector& layers(mDrawingState.layersSortedByZ);
1631 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001632 // layers have been added
1633 mVisibleRegionsDirty = true;
1634 }
1635
1636 // some layers might have been removed, so
1637 // we need to update the regions they're exposing.
1638 if (mLayersRemoved) {
1639 mLayersRemoved = false;
1640 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001641 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001642 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001643 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001644 if (currentLayers.indexOf(layer) < 0) {
1645 // this layer is not visible anymore
1646 // TODO: we could traverse the tree from front to back and
1647 // compute the actual visible region
1648 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001649 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001650 Region visibleReg = s.transform.transform(
1651 Region(Rect(s.active.w, s.active.h)));
1652 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001653 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001655 }
1656
1657 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001658
1659 updateCursorAsync();
1660}
1661
1662void SurfaceFlinger::updateCursorAsync()
1663{
1664 HWComposer& hwc(getHwComposer());
1665 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1666 sp<const DisplayDevice> hw(mDisplays[dpy]);
1667 const int32_t id = hw->getHwcDisplayId();
1668 if (id < 0) {
1669 continue;
1670 }
1671 const Vector< sp<Layer> >& currentLayers(
1672 hw->getVisibleLayersSortedByZ());
1673 const size_t count = currentLayers.size();
1674 HWComposer::LayerListIterator cur = hwc.begin(id);
1675 const HWComposer::LayerListIterator end = hwc.end(id);
1676 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1677 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1678 continue;
1679 }
1680 const sp<Layer>& layer(currentLayers[i]);
1681 Rect cursorPos = layer->getPosition(hw);
1682 hwc.setCursorPositionAsync(id, cursorPos);
1683 break;
1684 }
1685 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001686}
1687
1688void SurfaceFlinger::commitTransaction()
1689{
1690 if (!mLayersPendingRemoval.isEmpty()) {
1691 // Notify removed layers now that they can't be drawn from
1692 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1693 mLayersPendingRemoval[i]->onRemoved();
1694 }
1695 mLayersPendingRemoval.clear();
1696 }
1697
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001698 // If this transaction is part of a window animation then the next frame
1699 // we composite should be considered an animation as well.
1700 mAnimCompositionPending = mAnimTransactionPending;
1701
Mathias Agopian4fec8732012-06-29 14:12:52 -07001702 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001703 mTransactionPending = false;
1704 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001705 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001706}
1707
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001708void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001709 const LayerVector& currentLayers, uint32_t layerStack,
1710 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001711{
Mathias Agopian841cde52012-03-01 15:44:37 -08001712 ATRACE_CALL();
1713
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001714 Region aboveOpaqueLayers;
1715 Region aboveCoveredLayers;
1716 Region dirty;
1717
Mathias Agopian87baae12012-07-31 12:38:26 -07001718 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001719
1720 size_t i = currentLayers.size();
1721 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001722 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001723
1724 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001725 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001726
Jesse Hall01e29052013-02-19 16:13:35 -08001727 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001728 if (s.layerStack != layerStack)
1729 continue;
1730
Mathias Agopianab028732010-03-16 16:41:46 -07001731 /*
1732 * opaqueRegion: area of a surface that is fully opaque.
1733 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001734 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001735
1736 /*
1737 * visibleRegion: area of a surface that is visible on screen
1738 * and not fully transparent. This is essentially the layer's
1739 * footprint minus the opaque regions above it.
1740 * Areas covered by a translucent surface are considered visible.
1741 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001742 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001743
1744 /*
1745 * coveredRegion: area of a surface that is covered by all
1746 * visible regions above it (which includes the translucent areas).
1747 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001748 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001749
Jesse Halla8026d22012-09-25 13:25:04 -07001750 /*
1751 * transparentRegion: area of a surface that is hinted to be completely
1752 * transparent. This is only used to tell when the layer has no visible
1753 * non-transparent regions and can be removed from the layer list. It
1754 * does not affect the visibleRegion of this layer or any layers
1755 * beneath it. The hint may not be correct if apps don't respect the
1756 * SurfaceView restrictions (which, sadly, some don't).
1757 */
1758 Region transparentRegion;
1759
Mathias Agopianab028732010-03-16 16:41:46 -07001760
1761 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001762 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001763 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001764 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001765 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001766 if (!visibleRegion.isEmpty()) {
1767 // Remove the transparent area from the visible region
1768 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001769 const Transform tr(s.transform);
1770 if (tr.transformed()) {
1771 if (tr.preserveRects()) {
1772 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001773 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001774 } else {
1775 // transformation too complex, can't do the
1776 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001777 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001778 }
1779 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001780 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001781 }
Mathias Agopianab028732010-03-16 16:41:46 -07001782 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001783
Mathias Agopianab028732010-03-16 16:41:46 -07001784 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001785 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001786 if (s.alpha==255 && !translucent &&
1787 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1788 // the opaque region is the layer's footprint
1789 opaqueRegion = visibleRegion;
1790 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001791 }
1792 }
1793
Mathias Agopianab028732010-03-16 16:41:46 -07001794 // Clip the covered region to the visible region
1795 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1796
1797 // Update aboveCoveredLayers for next (lower) layer
1798 aboveCoveredLayers.orSelf(visibleRegion);
1799
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001800 // subtract the opaque region covered by the layers above us
1801 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001802
1803 // compute this layer's dirty region
1804 if (layer->contentDirty) {
1805 // we need to invalidate the whole region
1806 dirty = visibleRegion;
1807 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001808 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001809 layer->contentDirty = false;
1810 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001811 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001812 * the exposed region consists of two components:
1813 * 1) what's VISIBLE now and was COVERED before
1814 * 2) what's EXPOSED now less what was EXPOSED before
1815 *
1816 * note that (1) is conservative, we start with the whole
1817 * visible region but only keep what used to be covered by
1818 * something -- which mean it may have been exposed.
1819 *
1820 * (2) handles areas that were not covered by anything but got
1821 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001822 */
Mathias Agopianab028732010-03-16 16:41:46 -07001823 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001824 const Region oldVisibleRegion = layer->visibleRegion;
1825 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001826 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1827 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001828 }
1829 dirty.subtractSelf(aboveOpaqueLayers);
1830
1831 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001832 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001833
Mathias Agopianab028732010-03-16 16:41:46 -07001834 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001835 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001836
Jesse Halla8026d22012-09-25 13:25:04 -07001837 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001838 layer->setVisibleRegion(visibleRegion);
1839 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001840 layer->setVisibleNonTransparentRegion(
1841 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001842 }
1843
Mathias Agopian87baae12012-07-31 12:38:26 -07001844 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001845}
1846
Mathias Agopian87baae12012-07-31 12:38:26 -07001847void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1848 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001849 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001850 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1851 if (hw->getLayerStack() == layerStack) {
1852 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001853 }
1854 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001855}
1856
1857void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001858{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001859 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001860
Mathias Agopian4fec8732012-06-29 14:12:52 -07001861 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001862 const LayerVector& layers(mDrawingState.layersSortedByZ);
Eric Penner51c59cd2014-07-28 19:51:58 -07001863
1864 // Store the set of layers that need updates. This set must not change as
1865 // buffers are being latched, as this could result in a deadlock.
1866 // Example: Two producers share the same command stream and:
1867 // 1.) Layer 0 is latched
1868 // 2.) Layer 0 gets a new frame
1869 // 2.) Layer 1 gets a new frame
1870 // 3.) Layer 1 is latched.
1871 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1872 // second frame. But layer 0's second frame could be waiting on display.
1873 Vector<Layer*> layersWithQueuedFrames;
1874 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001875 const sp<Layer>& layer(layers[i]);
Eric Penner51c59cd2014-07-28 19:51:58 -07001876 if (layer->hasQueuedFrame())
1877 layersWithQueuedFrames.push_back(layer.get());
1878 }
1879 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
1880 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07001881 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001882 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001883 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001884 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001885
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001886 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001887}
1888
Mathias Agopianad456f92011-01-13 17:53:01 -08001889void SurfaceFlinger::invalidateHwcGeometry()
1890{
1891 mHwWorkListDirty = true;
1892}
1893
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001894
Mathias Agopiancd60f992012-08-16 16:28:27 -07001895void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001896 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001897{
Dan Stoza71433162014-02-04 16:22:36 -08001898 // We only need to actually compose the display if:
1899 // 1) It is being handled by hardware composer, which may need this to
1900 // keep its virtual display state machine in sync, or
1901 // 2) There is work to be done (the dirty region isn't empty)
1902 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1903 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1904 return;
1905 }
1906
Mathias Agopian87baae12012-07-31 12:38:26 -07001907 Region dirtyRegion(inDirtyRegion);
1908
Mathias Agopianb8a55602009-06-26 19:06:36 -07001909 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001910 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001911
Mathias Agopian42977342012-08-05 00:40:46 -07001912 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001913 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001914 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1915 // takes a rectangle, we must make sure to update that whole
1916 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001917 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001918 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001919 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001920 // We need to redraw the rectangle that will be updated
1921 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001922 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001923 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001924 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001925 } else {
1926 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001927 dirtyRegion.set(hw->bounds());
1928 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001929 }
1930 }
1931
Alan Viverette9c5a3332013-09-12 20:04:35 -07001932 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Michael Lentine3f121fc2014-10-01 11:17:28 -07001933 if (!doComposeSurfaces(hw, dirtyRegion)) return;
Mathias Agopianff2ed702013-09-01 21:36:12 -07001934 } else {
1935 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001936 mat4 colorMatrix = mColorMatrix;
1937 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001938 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001939 }
1940 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001941 doComposeSurfaces(hw, dirtyRegion);
1942 engine.endGroup();
1943 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001944
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001945 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001946 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001947
1948 // swap buffers (presentation)
1949 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001950}
1951
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05301952#ifdef QCOM_BSP
1953bool SurfaceFlinger::computeTiledDr(const sp<const DisplayDevice>& hw,
1954 Rect& unionDirtyRect) {
1955 int fbWidth= hw->getWidth();
1956 int fbHeight= hw->getHeight();
1957 Rect fullScreenRect = Rect(0,0,fbWidth, fbHeight);
1958 const int32_t id = hw->getHwcDisplayId();
1959 unionDirtyRect.clear();
1960 HWComposer& hwc(getHwComposer());
1961
1962 /* Compute and return the Union of Dirty Rects.
1963 * Return false if the unionDR is fullscreen, as there is no benefit from
1964 * preserving full screen.*/
1965 return (hwc.canUseTiledDR(id, unionDirtyRect) &&
1966 (unionDirtyRect != fullScreenRect));
1967
1968}
1969#endif
1970
Michael Lentine3f121fc2014-10-01 11:17:28 -07001971bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001972{
Mathias Agopian3f844832013-08-07 21:24:32 -07001973 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001974 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001975 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001976 HWComposer::LayerListIterator cur = hwc.begin(id);
1977 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001978
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05301979 Rect unionDirtyRect;
1980 Region clearRegion;
Jesse Halld05a17f2013-09-30 16:49:30 -07001981 bool hasGlesComposition = hwc.hasGlesComposition(id);
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05301982 bool canUseGpuTileRender = false;
Mathias Agopian85d751c2012-08-29 16:59:24 -07001983 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001984 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001985 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1986 hw->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07001987 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1988 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
1989 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
1990 }
1991 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08001992 }
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05301993#ifdef QCOM_BSP
1994 /* Compute DirtyRegion , if DR optimization for GPU comp optimization
1995 * is ON & device is primary.*/
1996 if(mGpuTileRenderEnable && (mDisplays.size()==1))
1997 canUseGpuTileRender = computeTiledDr(hw, unionDirtyRect);
1998#endif
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001999
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07002000 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07002001 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07002002 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07002003 // when using overlays, we assume a fully transparent framebuffer
2004 // NOTE: we could reduce how much we need to clear, for instance
2005 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07002006 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07002007 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07002008 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002009 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07002010 // we start with the whole screen area
2011 const Region bounds(hw->getBounds());
2012
2013 // we remove the scissor part
2014 // we're left with the letterbox region
2015 // (common case is that letterbox ends-up being empty)
2016 const Region letterbox(bounds.subtract(hw->getScissor()));
2017
2018 // compute the area to clear
2019 Region region(hw->undefinedRegion.merge(letterbox));
2020
2021 // but limit it to the dirty region
2022 region.andSelf(dirty);
2023
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302024
Mathias Agopianb9494d52012-04-18 02:28:45 -07002025 // screen is already cleared here
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302026#ifdef QCOM_BSP
2027 clearRegion.clear();
2028 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
2029 clearRegion = region;
2030 if (cur == end) {
Radhika Ranjan Soni35c9ff22013-12-17 19:55:17 +05302031 drawWormhole(hw, region);
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302032 } else if(canUseGpuTileRender) {
2033 /* If GPUTileRect DR optimization on clear only the UnionDR
2034 * (computed by computeTiledDr) which is the actual region
2035 * that will be drawn on FB in this cycle.. */
2036 clearRegion = clearRegion.andSelf(Region(unionDirtyRect));
2037 }
2038 } else
2039#endif
2040 {
2041 if (!region.isEmpty()) {
2042 if (cur != end) {
2043 if (cur->getCompositionType() != HWC_BLIT)
2044 // can happen with SurfaceView
2045 drawWormhole(hw, region);
2046 } else
2047 drawWormhole(hw, region);
2048 }
Mathias Agopianb9494d52012-04-18 02:28:45 -07002049 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07002050 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002051
Mathias Agopian766dc492012-10-30 18:08:06 -07002052 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
2053 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07002054 // scissor on the main display. It should never be needed
2055 // anyways (though in theory it could since the API allows it).
2056 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07002057 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002058 if (scissor != bounds) {
2059 // scissor doesn't match the screen's dimensions, so we
2060 // need to clear everything outside of it and enable
2061 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07002062
Mathias Agopianf45c5102012-10-24 16:29:17 -07002063 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002064 const uint32_t height = hw->getHeight();
2065 engine.setScissor(scissor.left, height - scissor.bottom,
2066 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002067 }
2068 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002069 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002070
Mathias Agopian85d751c2012-08-29 16:59:24 -07002071 /*
2072 * and then, render the layers targeted at the framebuffer
2073 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002074
Mathias Agopian13127d82013-03-05 17:47:11 -08002075 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002076 const size_t count = layers.size();
2077 const Transform& tr = hw->getTransform();
2078 if (cur != end) {
2079 // we're using h/w composer
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302080#ifdef QCOM_BSP
2081 int fbWidth= hw->getWidth();
2082 int fbHeight= hw->getHeight();
2083 /* if GPUTileRender optimization property is on & can be used
2084 * i) Enable EGL_SWAP_PRESERVED flag
2085 * ii) do startTile with union DirtyRect
2086 * else , Disable EGL_SWAP_PRESERVED */
2087 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
2088 if(canUseGpuTileRender) {
2089 hw->eglSwapPreserved(true);
2090 Rect dr = unionDirtyRect;
2091 engine.startTileComposition(dr.left, (fbHeight-dr.bottom),
2092 (dr.right-dr.left),
2093 (dr.bottom-dr.top), 0);
2094 } else {
2095 // Un Set EGL_SWAP_PRESERVED flag, if no tiling required.
2096 hw->eglSwapPreserved(false);
2097 }
2098 // DrawWormHole/Any Draw has to be within startTile & EndTile
2099 if (cur->getCompositionType() != HWC_BLIT &&
2100 !clearRegion.isEmpty()){
2101 drawWormhole(hw, clearRegion);
2102 }
2103 }
2104#endif
2105
Mathias Agopian85d751c2012-08-29 16:59:24 -07002106 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002107 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07002108 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07002109 if (!clip.isEmpty()) {
2110 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07002111 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07002112 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07002113 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002114 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
2115 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08002116 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07002117 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07002118 // never clear the very first layer since we're
2119 // guaranteed the FB is already cleared
2120 layer->clearWithOpenGL(hw, clip);
2121 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002122 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07002123 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002124 case HWC_FRAMEBUFFER: {
2125 layer->draw(hw, clip);
2126 break;
2127 }
Arun Kumar K.R5111ff02012-12-19 18:10:46 -08002128 case HWC_BLIT:
2129 //Do nothing
2130 break;
Mathias Agopianda27af92012-09-13 18:17:13 -07002131 case HWC_FRAMEBUFFER_TARGET: {
2132 // this should not happen as the iterator shouldn't
2133 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08002134 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07002135 break;
2136 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07002137 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002138 }
2139 layer->setAcquireFence(hw, *cur);
2140 }
Sravan Kumar D.V.N260f6012014-05-05 22:41:16 +05302141
2142#ifdef QCOM_BSP
2143 // call EndTile, if starTile has been called in this cycle.
2144 if(mGpuTileRenderEnable && (mDisplays.size()==1)) {
2145 if(canUseGpuTileRender) {
2146 engine.endTileComposition(GL_PRESERVE);
2147 }
2148 }
2149#endif
Mathias Agopian85d751c2012-08-29 16:59:24 -07002150 } else {
2151 // we're not using h/w composer
2152 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002153 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002154 const Region clip(dirty.intersect(
2155 tr.transform(layer->visibleRegion)));
2156 if (!clip.isEmpty()) {
2157 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07002158 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002159 }
2160 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002161
2162 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002163 engine.disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07002164 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002165}
2166
Mathias Agopian3f844832013-08-07 21:24:32 -07002167void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07002168 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07002169 RenderEngine& engine(getRenderEngine());
2170 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002171}
2172
Mathias Agopianac9fa422013-02-11 16:40:36 -08002173void SurfaceFlinger::addClientLayer(const sp<Client>& client,
2174 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07002175 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08002176 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07002177{
Mathias Agopian96f08192010-06-02 23:28:45 -07002178 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08002179 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07002180
Mathias Agopian96f08192010-06-02 23:28:45 -07002181 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002182 Mutex::Autolock _l(mStateLock);
2183 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07002184 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07002185}
2186
Mathias Agopian3f844832013-08-07 21:24:32 -07002187status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002188 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08002189 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002190 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07002191 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002192 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07002193 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002194 return NO_ERROR;
2195 }
Mathias Agopian3d579642009-06-04 18:46:21 -07002196 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002197}
2198
Dan Stozac7014012014-02-14 15:03:43 -08002199uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07002200 return android_atomic_release_load(&mTransactionFlags);
2201}
2202
Mathias Agopian3f844832013-08-07 21:24:32 -07002203uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002204 return android_atomic_and(~flags, &mTransactionFlags) & flags;
2205}
2206
Mathias Agopian3f844832013-08-07 21:24:32 -07002207uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002208 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
2209 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002210 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002211 }
2212 return old;
2213}
2214
Mathias Agopian8b33f032012-07-24 20:43:54 -07002215void SurfaceFlinger::setTransactionState(
2216 const Vector<ComposerState>& state,
2217 const Vector<DisplayState>& displays,
2218 uint32_t flags)
2219{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002220 ATRACE_CALL();
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08002221 size_t count = displays.size();
2222#ifdef QCOM_BSP
2223 for (size_t i=0 ; i<count ; i++) {
2224 const DisplayState& s(displays[i]);
2225 if(s.token != mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]) {
2226 const uint32_t what = s.what;
2227 // Invalidate and Delay the binder thread by 50 ms on
2228 // eDisplayProjectionChanged to trigger a draw cycle so that
2229 // it can fix one incorrect frame on the External, when we disable
2230 // external animation
2231 if (what & DisplayState::eDisplayProjectionChanged) {
2232 invalidateHwcGeometry();
2233 repaintEverything();
2234 usleep(50000);
2235 }
2236 }
2237 }
2238#endif
Mathias Agopian698c0872011-06-28 19:09:31 -07002239 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07002240 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002241
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002242 if (flags & eAnimation) {
2243 // For window updates that are part of an animation we must wait for
2244 // previous animation "frames" to be handled.
2245 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002246 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002247 if (CC_UNLIKELY(err != NO_ERROR)) {
2248 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002249 // caller after a few seconds.
2250 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
2251 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002252 mAnimTransactionPending = false;
2253 break;
2254 }
2255 }
2256 }
2257
Mathias Agopiane57f2922012-08-09 16:29:12 -07002258 for (size_t i=0 ; i<count ; i++) {
2259 const DisplayState& s(displays[i]);
2260 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002261 }
2262
Mathias Agopiane57f2922012-08-09 16:29:12 -07002263 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002264 for (size_t i=0 ; i<count ; i++) {
2265 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002266 // Here we need to check that the interface we're given is indeed
2267 // one of our own. A malicious client could give us a NULL
2268 // IInterface, or one of its own or even one of our own but a
2269 // different type. All these situations would cause us to crash.
2270 //
2271 // NOTE: it would be better to use RTTI as we could directly check
2272 // that we have a Client*. however, RTTI is disabled in Android.
2273 if (s.client != NULL) {
2274 sp<IBinder> binder = s.client->asBinder();
2275 if (binder != NULL) {
2276 String16 desc(binder->getInterfaceDescriptor());
2277 if (desc == ISurfaceComposerClient::descriptor) {
2278 sp<Client> client( static_cast<Client *>(s.client.get()) );
2279 transactionFlags |= setClientStateLocked(client, s.state);
2280 }
2281 }
2282 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002283 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002284
Mathias Agopian386aa982011-11-07 21:58:03 -08002285 if (transactionFlags) {
2286 // this triggers the transaction
2287 setTransactionFlags(transactionFlags);
2288
2289 // if this is a synchronous transaction, wait for it to take effect
2290 // before returning.
2291 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002292 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002293 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002294 if (flags & eAnimation) {
2295 mAnimTransactionPending = true;
2296 }
2297 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002298 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2299 if (CC_UNLIKELY(err != NO_ERROR)) {
2300 // just in case something goes wrong in SF, return to the
2301 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002302 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2303 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002304 break;
2305 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002306 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002307 }
2308}
2309
Mathias Agopiane57f2922012-08-09 16:29:12 -07002310uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2311{
Jesse Hall9a143922012-10-04 16:29:19 -07002312 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2313 if (dpyIdx < 0)
2314 return 0;
2315
Mathias Agopiane57f2922012-08-09 16:29:12 -07002316 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002317 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002318 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002319 const uint32_t what = s.what;
2320 if (what & DisplayState::eSurfaceChanged) {
2321 if (disp.surface->asBinder() != s.surface->asBinder()) {
2322 disp.surface = s.surface;
2323 flags |= eDisplayTransactionNeeded;
2324 }
2325 }
2326 if (what & DisplayState::eLayerStackChanged) {
2327 if (disp.layerStack != s.layerStack) {
2328 disp.layerStack = s.layerStack;
2329 flags |= eDisplayTransactionNeeded;
2330 }
2331 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002332 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002333 if (disp.orientation != s.orientation) {
2334 disp.orientation = s.orientation;
2335 flags |= eDisplayTransactionNeeded;
2336 }
2337 if (disp.frame != s.frame) {
2338 disp.frame = s.frame;
2339 flags |= eDisplayTransactionNeeded;
2340 }
2341 if (disp.viewport != s.viewport) {
2342 disp.viewport = s.viewport;
2343 flags |= eDisplayTransactionNeeded;
2344 }
2345 }
Michael Lentine47e45402014-07-18 15:34:25 -07002346 if (what & DisplayState::eDisplaySizeChanged) {
2347 if (disp.width != s.width) {
2348 disp.width = s.width;
2349 flags |= eDisplayTransactionNeeded;
2350 }
2351 if (disp.height != s.height) {
2352 disp.height = s.height;
2353 flags |= eDisplayTransactionNeeded;
2354 }
2355 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002356 }
2357 return flags;
2358}
2359
2360uint32_t SurfaceFlinger::setClientStateLocked(
2361 const sp<Client>& client,
2362 const layer_state_t& s)
2363{
2364 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002365 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002366 if (layer != 0) {
2367 const uint32_t what = s.what;
2368 if (what & layer_state_t::ePositionChanged) {
2369 if (layer->setPosition(s.x, s.y))
2370 flags |= eTraversalNeeded;
2371 }
2372 if (what & layer_state_t::eLayerChanged) {
2373 // NOTE: index needs to be calculated before we update the state
2374 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2375 if (layer->setLayer(s.z)) {
2376 mCurrentState.layersSortedByZ.removeAt(idx);
2377 mCurrentState.layersSortedByZ.add(layer);
2378 // we need traversal (state changed)
2379 // AND transaction (list changed)
2380 flags |= eTransactionNeeded|eTraversalNeeded;
2381 }
2382 }
2383 if (what & layer_state_t::eSizeChanged) {
2384 if (layer->setSize(s.w, s.h)) {
2385 flags |= eTraversalNeeded;
2386 }
2387 }
2388 if (what & layer_state_t::eAlphaChanged) {
2389 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2390 flags |= eTraversalNeeded;
2391 }
2392 if (what & layer_state_t::eMatrixChanged) {
2393 if (layer->setMatrix(s.matrix))
2394 flags |= eTraversalNeeded;
2395 }
2396 if (what & layer_state_t::eTransparentRegionChanged) {
2397 if (layer->setTransparentRegionHint(s.transparentRegion))
2398 flags |= eTraversalNeeded;
2399 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08002400 if ((what & layer_state_t::eVisibilityChanged) ||
2401 (what & layer_state_t::eOpacityChanged)) {
2402 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07002403 if (layer->setFlags(s.flags, s.mask))
2404 flags |= eTraversalNeeded;
2405 }
2406 if (what & layer_state_t::eCropChanged) {
2407 if (layer->setCrop(s.crop))
2408 flags |= eTraversalNeeded;
2409 }
2410 if (what & layer_state_t::eLayerStackChanged) {
2411 // NOTE: index needs to be calculated before we update the state
2412 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2413 if (layer->setLayerStack(s.layerStack)) {
2414 mCurrentState.layersSortedByZ.removeAt(idx);
2415 mCurrentState.layersSortedByZ.add(layer);
2416 // we need traversal (state changed)
2417 // AND transaction (list changed)
2418 flags |= eTransactionNeeded|eTraversalNeeded;
2419 }
2420 }
2421 }
2422 return flags;
2423}
2424
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002425status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002426 const String8& name,
2427 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002428 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2429 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002430{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002431 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002432 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002433 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002434 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002435 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002436 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002437
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002438 status_t result = NO_ERROR;
2439
2440 sp<Layer> layer;
2441
Mathias Agopian3165cc22012-08-08 19:42:09 -07002442 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2443 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002444 result = createNormalLayer(client,
2445 name, w, h, flags, format,
2446 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002447 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002448 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002449 result = createDimLayer(client,
2450 name, w, h, flags,
2451 handle, gbp, &layer);
2452 break;
2453 default:
2454 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002455 break;
2456 }
2457
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002458 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002459 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002460 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002461 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002462 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002463}
2464
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002465status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2466 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2467 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002468{
2469 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002470 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002471 case PIXEL_FORMAT_TRANSPARENT:
2472 case PIXEL_FORMAT_TRANSLUCENT:
2473 format = PIXEL_FORMAT_RGBA_8888;
2474 break;
2475 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002476 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002477 break;
2478 }
2479
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002480 *outLayer = new Layer(this, client, name, w, h, flags);
2481 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2482 if (err == NO_ERROR) {
2483 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002484 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002485 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002486
2487 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2488 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002489}
2490
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002491status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2492 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2493 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002494{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002495 *outLayer = new LayerDim(this, client, name, w, h, flags);
2496 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002497 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002498 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002499}
2500
Mathias Agopianac9fa422013-02-11 16:40:36 -08002501status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002502{
Mathias Agopian67106042013-03-14 19:18:13 -07002503 // called by the window manager when it wants to remove a Layer
2504 status_t err = NO_ERROR;
2505 sp<Layer> l(client->getLayerUser(handle));
2506 if (l != NULL) {
2507 err = removeLayer(l);
2508 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2509 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002510 }
2511 return err;
2512}
2513
Mathias Agopian13127d82013-03-05 17:47:11 -08002514status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002515{
Mathias Agopian67106042013-03-14 19:18:13 -07002516 // called by ~LayerCleaner() when all references to the IBinder (handle)
2517 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002518 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002519 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002520 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002521 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002522 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002523 "error removing layer=%p (%s)", l.get(), strerror(-err));
2524 }
2525 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002526}
2527
Mathias Agopianb60314a2012-04-10 22:09:54 -07002528// ---------------------------------------------------------------------------
2529
Andy McFadden13a082e2012-08-24 10:16:42 -07002530void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002531 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002532 Vector<ComposerState> state;
2533 Vector<DisplayState> displays;
2534 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002535 d.what = DisplayState::eDisplayProjectionChanged |
2536 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002537 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002538 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002539 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002540 d.frame.makeInvalid();
2541 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002542 d.width = 0;
2543 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002544 displays.add(d);
2545 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002546 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002547
2548 const nsecs_t period =
2549 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2550 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002551}
2552
2553void SurfaceFlinger::initializeDisplays() {
2554 class MessageScreenInitialized : public MessageBase {
2555 SurfaceFlinger* flinger;
2556 public:
2557 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2558 virtual bool handler() {
2559 flinger->onInitializeDisplays();
2560 return true;
2561 }
2562 };
2563 sp<MessageBase> msg = new MessageScreenInitialized(this);
2564 postMessageAsync(msg); // we may be called from main thread, use async message
2565}
2566
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002567void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2568 int mode) {
2569 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2570 this);
2571 int32_t type = hw->getDisplayType();
2572 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002573
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002574 if (mode == currentMode) {
2575 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002576 return;
2577 }
2578
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002579 hw->setPowerMode(mode);
2580 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2581 ALOGW("Trying to set power mode for virtual display");
2582 return;
2583 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002584
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002585 if (currentMode == HWC_POWER_MODE_OFF) {
2586 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002587 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2588 // FIXME: eventthread only knows about the main display right now
2589 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002590 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002591 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002592
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002593 mVisibleRegionsDirty = true;
2594 repaintEverything();
2595 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002596 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002597 disableHardwareVsync(true); // also cancels any in-progress resync
2598
Mathias Agopiancde87a32012-09-13 14:09:01 -07002599 // FIXME: eventthread only knows about the main display right now
2600 mEventThread->onScreenReleased();
2601 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002602
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002603 getHwComposer().setPowerMode(type, mode);
2604 mVisibleRegionsDirty = true;
2605 // from this point on, SF will stop drawing on this display
2606 } else {
2607 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002608 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002609}
2610
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002611void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2612 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002613 SurfaceFlinger& mFlinger;
2614 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002615 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002616 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002617 MessageSetPowerMode(SurfaceFlinger& flinger,
2618 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2619 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002620 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002621 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002622 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002623 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002624 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002625 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002626 ALOGW("Attempt to set power mode = %d for virtual display",
2627 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002628 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002629 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002630 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002631 return true;
2632 }
2633 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002634 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002635 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002636}
2637
2638// ---------------------------------------------------------------------------
2639
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002640status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2641{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002642 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002643
Mathias Agopianbd115332013-04-18 16:41:04 -07002644 IPCThreadState* ipc = IPCThreadState::self();
2645 const int pid = ipc->getCallingPid();
2646 const int uid = ipc->getCallingUid();
2647 if ((uid != AID_SHELL) &&
2648 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002649 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002650 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002651 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002652 // Try to get the main lock, but don't insist if we can't
2653 // (this would indicate SF is stuck, but we want to be able to
2654 // print something in dumpsys).
2655 int retry = 3;
2656 while (mStateLock.tryLock()<0 && --retry>=0) {
2657 usleep(1000000);
2658 }
2659 const bool locked(retry >= 0);
2660 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002661 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002662 "SurfaceFlinger appears to be unresponsive, "
2663 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002664 }
2665
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002666 bool dumpAll = true;
2667 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002668 size_t numArgs = args.size();
2669 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002670 if ((index < numArgs) &&
2671 (args[index] == String16("--list"))) {
2672 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002673 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002674 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002675 }
2676
2677 if ((index < numArgs) &&
2678 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002679 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002680 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002681 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002682 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002683
2684 if ((index < numArgs) &&
2685 (args[index] == String16("--latency-clear"))) {
2686 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002687 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002688 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002689 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002690
2691 if ((index < numArgs) &&
2692 (args[index] == String16("--dispsync"))) {
2693 index++;
2694 mPrimaryDispSync.dump(result);
2695 dumpAll = false;
2696 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002697 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002698
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002699 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002700 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002701 }
2702
Mathias Agopian9795c422009-08-26 16:36:26 -07002703 if (locked) {
2704 mStateLock.unlock();
2705 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002706 }
2707 write(fd, result.string(), result.size());
2708 return NO_ERROR;
2709}
2710
Dan Stozac7014012014-02-14 15:03:43 -08002711void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2712 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002713{
2714 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2715 const size_t count = currentLayers.size();
2716 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002717 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002718 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002719 }
2720}
2721
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002722void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002723 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002724{
2725 String8 name;
2726 if (index < args.size()) {
2727 name = String8(args[index]);
2728 index++;
2729 }
2730
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002731 const nsecs_t period =
2732 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002733 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002734
2735 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002736 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002737 } else {
2738 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2739 const size_t count = currentLayers.size();
2740 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002741 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002742 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002743 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002744 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002745 }
2746 }
2747}
2748
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002749void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002750 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002751{
2752 String8 name;
2753 if (index < args.size()) {
2754 name = String8(args[index]);
2755 index++;
2756 }
2757
2758 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2759 const size_t count = currentLayers.size();
2760 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002761 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002762 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002763 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002764 }
2765 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002766
Svetoslavd85084b2014-03-20 10:28:31 -07002767 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002768}
2769
Jamie Gennis6547ff42013-07-16 20:12:42 -07002770// This should only be called from the main thread. Otherwise it would need
2771// the lock and should use mCurrentState rather than mDrawingState.
2772void SurfaceFlinger::logFrameStats() {
2773 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2774 const size_t count = drawingLayers.size();
2775 for (size_t i=0 ; i<count ; i++) {
2776 const sp<Layer>& layer(drawingLayers[i]);
2777 layer->logFrameStats();
2778 }
2779
2780 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2781}
2782
Andy McFadden4803b742012-09-24 19:07:20 -07002783/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2784{
2785 static const char* config =
2786 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002787#ifdef HAS_CONTEXT_PRIORITY
2788 " HAS_CONTEXT_PRIORITY"
2789#endif
2790#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2791 " NEVER_DEFAULT_TO_ASYNC_MODE"
2792#endif
2793#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2794 " TARGET_DISABLE_TRIPLE_BUFFERING"
2795#endif
2796 "]";
2797 result.append(config);
2798}
2799
Mathias Agopian74d211a2013-04-22 16:55:35 +02002800void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2801 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002802{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002803 bool colorize = false;
2804 if (index < args.size()
2805 && (args[index] == String16("--color"))) {
2806 colorize = true;
2807 index++;
2808 }
2809
2810 Colorizer colorizer(colorize);
2811
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002812 // figure out if we're stuck somewhere
2813 const nsecs_t now = systemTime();
2814 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2815 const nsecs_t inTransaction(mDebugInTransaction);
2816 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2817 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2818
2819 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002820 * Dump library configuration.
2821 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002822
2823 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002824 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002825 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002826 appendSfConfigString(result);
2827 appendUiConfigString(result);
2828 appendGuiConfigString(result);
2829 result.append("\n");
2830
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002831 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002832 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002833 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002834 result.append(SyncFeatures::getInstance().toString());
2835 result.append("\n");
2836
Andy McFadden41d67d72014-04-25 16:58:34 -07002837 colorizer.bold(result);
2838 result.append("DispSync configuration: ");
2839 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002840 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2841 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002842 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2843 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2844 result.append("\n");
2845
Andy McFadden4803b742012-09-24 19:07:20 -07002846 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002847 * Dump the visible layer list
2848 */
2849 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2850 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002851 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002852 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002853 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002854 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002855 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002856 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002857 }
2858
2859 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002860 * Dump Display state
2861 */
2862
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002863 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002864 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002865 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002866 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2867 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002868 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002869 }
2870
2871 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002872 * Dump SurfaceFlinger global state
2873 */
2874
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002875 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002876 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002877 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002878
Mathias Agopian888c8222012-08-04 21:10:38 -07002879 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002880 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002881
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002882 colorizer.bold(result);
2883 result.appendFormat("EGL implementation : %s\n",
2884 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2885 colorizer.reset(result);
2886 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002887 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002888
Mathias Agopian875d8e12013-06-07 15:35:48 -07002889 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002890
Mathias Agopian42977342012-08-05 00:40:46 -07002891 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002892 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2893 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002894 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002895 " last eglSwapBuffers() time: %f us\n"
2896 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002897 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002898 " refresh-rate : %f fps\n"
2899 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002900 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002901 " gpu_to_cpu_unsupported : %d\n"
2902 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002903 mLastSwapBufferTime/1000.0,
2904 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002905 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002906 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2907 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002908 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002909 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002910
Mathias Agopian74d211a2013-04-22 16:55:35 +02002911 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002912 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002913
Mathias Agopian74d211a2013-04-22 16:55:35 +02002914 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002915 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002916
2917 /*
2918 * VSYNC state
2919 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002920 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002921
2922 /*
2923 * Dump HWComposer state
2924 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002925 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002926 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002927 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002928 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002929 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002930 (mDebugDisableHWC || mDebugRegion || mDaltonize
2931 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002932 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002933
2934 /*
2935 * Dump gralloc state
2936 */
2937 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2938 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002939}
2940
Mathias Agopian13127d82013-03-05 17:47:11 -08002941const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002942SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002943 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002944 wp<IBinder> dpy;
2945 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2946 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2947 dpy = mDisplays.keyAt(i);
2948 break;
2949 }
2950 }
2951 if (dpy == NULL) {
2952 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2953 // Just use the primary display so we have something to return
2954 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2955 }
2956 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002957}
2958
Keun young Park63f165f2012-08-31 10:53:36 -07002959bool SurfaceFlinger::startDdmConnection()
2960{
2961 void* libddmconnection_dso =
2962 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2963 if (!libddmconnection_dso) {
2964 return false;
2965 }
2966 void (*DdmConnection_start)(const char* name);
2967 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07002968 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07002969 if (!DdmConnection_start) {
2970 dlclose(libddmconnection_dso);
2971 return false;
2972 }
2973 (*DdmConnection_start)(getServiceName());
2974 return true;
2975}
2976
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002977status_t SurfaceFlinger::onTransact(
2978 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2979{
2980 switch (code) {
2981 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002982 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002983 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002984 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002985 case CLEAR_ANIMATION_FRAME_STATS:
2986 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002987 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002988 {
2989 // codes that require permission check
2990 IPCThreadState* ipc = IPCThreadState::self();
2991 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002992 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002993 if ((uid != AID_GRAPHICS) &&
2994 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002995 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002996 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2997 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002998 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002999 break;
3000 }
3001 case CAPTURE_SCREEN:
3002 {
3003 // codes that require permission check
3004 IPCThreadState* ipc = IPCThreadState::self();
3005 const int pid = ipc->getCallingPid();
3006 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07003007 if ((uid != AID_GRAPHICS) &&
3008 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00003009 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003010 "can't read framebuffer pid=%d, uid=%d", pid, uid);
3011 return PERMISSION_DENIED;
3012 }
3013 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003014 }
3015 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003016
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003017 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
3018 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07003019 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08003020 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07003021 IPCThreadState* ipc = IPCThreadState::self();
3022 const int pid = ipc->getCallingPid();
3023 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00003024 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07003025 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003026 return PERMISSION_DENIED;
3027 }
3028 int n;
3029 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07003030 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07003031 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003032 return NO_ERROR;
3033 case 1002: // SHOW_UPDATES
3034 n = data.readInt32();
3035 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07003036 invalidateHwcGeometry();
3037 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003038 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003039 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07003040 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003041 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003042 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003043 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07003044 setTransactionFlags(
3045 eTransactionNeeded|
3046 eDisplayTransactionNeeded|
3047 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07003048 return NO_ERROR;
3049 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08003050 case 1006:{ // send empty update
3051 signalRefresh();
3052 return NO_ERROR;
3053 }
Mathias Agopian53331da2011-08-22 21:44:41 -07003054 case 1008: // toggle use of hw composer
3055 n = data.readInt32();
3056 mDebugDisableHWC = n ? 1 : 0;
3057 invalidateHwcGeometry();
3058 repaintEverything();
3059 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07003060 case 1009: // toggle use of transform hint
3061 n = data.readInt32();
3062 mDebugDisableTransformHint = n ? 1 : 0;
3063 invalidateHwcGeometry();
3064 repaintEverything();
3065 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003066 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07003067 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003068 reply->writeInt32(0);
3069 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07003070 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08003071 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003072 return NO_ERROR;
3073 case 1013: {
3074 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07003075 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
3076 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07003077 return NO_ERROR;
3078 }
3079 case 1014: {
3080 // daltonize
3081 n = data.readInt32();
3082 switch (n % 10) {
3083 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
3084 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
3085 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
3086 }
3087 if (n >= 10) {
3088 mDaltonizer.setMode(Daltonizer::correction);
3089 } else {
3090 mDaltonizer.setMode(Daltonizer::simulation);
3091 }
3092 mDaltonize = n > 0;
3093 invalidateHwcGeometry();
3094 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07003095 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003096 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003097 case 1015: {
3098 // apply a color matrix
3099 n = data.readInt32();
3100 mHasColorMatrix = n ? 1 : 0;
3101 if (n) {
3102 // color matrix is sent as mat3 matrix followed by vec3
3103 // offset, then packed into a mat4 where the last row is
3104 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07003105 for (size_t i = 0 ; i < 4; i++) {
3106 for (size_t j = 0; j < 4; j++) {
3107 mColorMatrix[i][j] = data.readFloat();
3108 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003109 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003110 } else {
3111 mColorMatrix = mat4();
3112 }
3113 invalidateHwcGeometry();
3114 repaintEverything();
3115 return NO_ERROR;
3116 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003117 // This is an experimental interface
3118 // Needs to be shifted to proper binder interface when we productize
3119 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07003120 n = data.readInt32();
3121 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003122 return NO_ERROR;
3123 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003124 }
3125 }
3126 return err;
3127}
3128
Mathias Agopian53331da2011-08-22 21:44:41 -07003129void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07003130 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08003131 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07003132}
3133
Mathias Agopian59119e62010-10-11 12:37:43 -07003134// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003135// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07003136// ---------------------------------------------------------------------------
3137
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003138/* The code below is here to handle b/8734824
3139 *
3140 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07003141 * from the surfaceflinger thread to the calling binder thread, where they
3142 * are executed. This allows the calling thread in the calling process to be
3143 * reused and not depend on having "enough" binder threads to handle the
3144 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003145 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003146class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07003147 /* Parts of GraphicProducerWrapper are run on two different threads,
3148 * communicating by sending messages via Looper but also by shared member
3149 * data. Coherence maintenance is subtle and in places implicit (ugh).
3150 *
3151 * Don't rely on Looper's sendMessage/handleMessage providing
3152 * release/acquire semantics for any data not actually in the Message.
3153 * Data going from surfaceflinger to binder threads needs to be
3154 * synchronized explicitly.
3155 *
3156 * Barrier open/wait do provide release/acquire semantics. This provides
3157 * implicit synchronization for data coming back from binder to
3158 * surfaceflinger threads.
3159 */
3160
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003161 sp<IGraphicBufferProducer> impl;
3162 sp<Looper> looper;
3163 status_t result;
3164 bool exitPending;
3165 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07003166 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003167 uint32_t code;
3168 Parcel const* data;
3169 Parcel* reply;
3170
3171 enum {
3172 MSG_API_CALL,
3173 MSG_EXIT
3174 };
3175
3176 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003177 * Called on surfaceflinger thread. This is called by our "fake"
3178 * BpGraphicBufferProducer. We package the data and reply Parcel and
3179 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003180 */
3181 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08003182 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003183 this->code = code;
3184 this->data = &data;
3185 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003186 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07003187 // if we've exited, we run the message synchronously right here.
3188 // note (JH): as far as I can tell from looking at the code, this
3189 // never actually happens. if it does, i'm not sure if it happens
3190 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003191 handleMessage(Message(MSG_API_CALL));
3192 } else {
3193 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07003194 // Prevent stores to this->{code, data, reply} from being
3195 // reordered later than the construction of Message.
3196 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003197 looper->sendMessage(this, Message(MSG_API_CALL));
3198 barrier.wait();
3199 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08003200 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003201 }
3202
3203 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003204 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003205 * call the real BpGraphicBufferProducer.
3206 */
3207 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07003208 int what = message.what;
3209 // Prevent reads below from happening before the read from Message
3210 atomic_thread_fence(memory_order_acquire);
3211 if (what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08003212 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003213 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07003214 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003215 exitRequested = true;
3216 }
3217 }
3218
3219public:
Jesse Hallb154c422014-07-13 12:47:02 -07003220 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
3221 : impl(impl),
3222 looper(new Looper(true)),
3223 exitPending(false),
3224 exitRequested(false)
3225 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003226
Jesse Hallb154c422014-07-13 12:47:02 -07003227 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003228 status_t waitForResponse() {
3229 do {
3230 looper->pollOnce(-1);
3231 } while (!exitRequested);
3232 return result;
3233 }
3234
Jesse Hallb154c422014-07-13 12:47:02 -07003235 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003236 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07003237 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003238 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07003239 // Ensure this->result is visible to the binder thread before it
3240 // handles the message.
3241 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003242 looper->sendMessage(this, Message(MSG_EXIT));
3243 }
3244};
3245
3246
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003247status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
3248 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003249 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003250 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003251 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003252
3253 if (CC_UNLIKELY(display == 0))
3254 return BAD_VALUE;
3255
3256 if (CC_UNLIKELY(producer == 0))
3257 return BAD_VALUE;
3258
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003259 // if we have secure windows on this display, never allow the screen capture
3260 // unless the producer interface is local (i.e.: we can take a screenshot for
3261 // ourselves).
3262 if (!producer->asBinder()->localBinder()) {
3263 Mutex::Autolock _l(mStateLock);
3264 sp<const DisplayDevice> hw(getDisplayDevice(display));
3265 if (hw->getSecureLayerVisible()) {
3266 ALOGW("FB is protected: PERMISSION_DENIED");
3267 return PERMISSION_DENIED;
3268 }
3269 }
3270
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003271 // Convert to surfaceflinger's internal rotation type.
3272 Transform::orientation_flags rotationFlags;
3273 switch (rotation) {
3274 case ISurfaceComposer::eRotateNone:
3275 rotationFlags = Transform::ROT_0;
3276 break;
3277 case ISurfaceComposer::eRotate90:
3278 rotationFlags = Transform::ROT_90;
3279 break;
3280 case ISurfaceComposer::eRotate180:
3281 rotationFlags = Transform::ROT_180;
3282 break;
3283 case ISurfaceComposer::eRotate270:
3284 rotationFlags = Transform::ROT_270;
3285 break;
3286 default:
3287 rotationFlags = Transform::ROT_0;
3288 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3289 break;
3290 }
3291
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003292 class MessageCaptureScreen : public MessageBase {
3293 SurfaceFlinger* flinger;
3294 sp<IBinder> display;
3295 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003296 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003297 uint32_t reqWidth, reqHeight;
3298 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003299 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003300 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003301 status_t result;
3302 public:
3303 MessageCaptureScreen(SurfaceFlinger* flinger,
3304 const sp<IBinder>& display,
3305 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003306 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003307 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003308 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003309 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003310 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003311 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003312 useIdentityTransform(useIdentityTransform),
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003313 rotation(rotation),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003314 result(PERMISSION_DENIED)
3315 {
3316 }
3317 status_t getResult() const {
3318 return result;
3319 }
3320 virtual bool handler() {
3321 Mutex::Autolock _l(flinger->mStateLock);
3322 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003323 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003324 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003325 useIdentityTransform, rotation);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003326 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003327 return true;
3328 }
3329 };
3330
Mathias Agopian9eb1f052013-04-10 16:27:17 -07003331 // make sure to process transactions before screenshots -- a transaction
3332 // might already be pending but scheduled for VSYNC; this guarantees we
3333 // will handle it before the screenshot. When VSYNC finally arrives
3334 // the scheduled transaction will be a no-op. If no transactions are
3335 // scheduled at this time, this will end-up being a no-op as well.
3336 mEventQueue.invalidateTransactionNow();
3337
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003338 // this creates a "fake" BBinder which will serve as a "fake" remote
3339 // binder to receive the marshaled calls and forward them to the
3340 // real remote (a BpGraphicBufferProducer)
3341 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3342
3343 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3344 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003345 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003346 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003347 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003348 useIdentityTransform, rotationFlags);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003349
3350 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003351 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003352 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003353 }
3354 return res;
3355}
3356
Mathias Agopian180f10d2013-04-10 22:55:41 -07003357
3358void SurfaceFlinger::renderScreenImplLocked(
3359 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003360 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003361 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003362 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003363{
3364 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003365 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003366
3367 // get screen geometry
3368 const uint32_t hw_w = hw->getWidth();
3369 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003370 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
3371
Dan Stozac1879002014-05-22 15:59:05 -07003372 // if a default or invalid sourceCrop is passed in, set reasonable values
3373 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3374 !sourceCrop.isValid()) {
3375 sourceCrop.setLeftTop(Point(0, 0));
3376 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3377 }
3378
3379 // ensure that sourceCrop is inside screen
3380 if (sourceCrop.left < 0) {
3381 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3382 }
Dan Stozabe31f442014-06-11 11:20:54 -07003383 if (sourceCrop.right > hw_w) {
3384 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003385 }
3386 if (sourceCrop.top < 0) {
3387 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3388 }
Dan Stozabe31f442014-06-11 11:20:54 -07003389 if (sourceCrop.bottom > hw_h) {
3390 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003391 }
3392
Mathias Agopian180f10d2013-04-10 22:55:41 -07003393 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003394 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003395
3396 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003397 engine.setViewportAndProjection(
3398 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003399 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003400
3401 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003402 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003403
3404 const LayerVector& layers( mDrawingState.layersSortedByZ );
3405 const size_t count = layers.size();
3406 for (size_t i=0 ; i<count ; ++i) {
3407 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003408 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003409 if (state.layerStack == hw->getLayerStack()) {
3410 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3411 if (layer->isVisible()) {
3412 if (filtering) layer->setFiltering(true);
Ramakant Singhf9af0e32014-01-07 14:17:52 +05303413 if(!layer->isProtected())
3414 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003415 if (filtering) layer->setFiltering(false);
3416 }
3417 }
3418 }
3419 }
3420
3421 // compositionComplete is needed for older driver
3422 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003423 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003424}
3425
3426
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003427status_t SurfaceFlinger::captureScreenImplLocked(
3428 const sp<const DisplayDevice>& hw,
3429 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003430 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003431 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003432 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003433{
3434 ATRACE_CALL();
3435
Mathias Agopian180f10d2013-04-10 22:55:41 -07003436 // get screen geometry
3437 const uint32_t hw_w = hw->getWidth();
3438 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003439
Mathias Agopian180f10d2013-04-10 22:55:41 -07003440 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3441 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3442 reqWidth, reqHeight, hw_w, hw_h);
3443 return BAD_VALUE;
3444 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003445
Mathias Agopian180f10d2013-04-10 22:55:41 -07003446 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3447 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3448
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003449 // create a surface (because we're a producer, and we need to
3450 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003451 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003452 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003453
3454 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003455 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003456 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3457 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003458
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003459 int err = 0;
3460 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003461 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003462 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3463 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003464
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003465 if (err == NO_ERROR) {
3466 ANativeWindowBuffer* buffer;
3467 /* TODO: Once we have the sync framework everywhere this can use
3468 * server-side waits on the fence that dequeueBuffer returns.
3469 */
3470 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3471 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003472 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003473 // create an EGLImage from the buffer so we can later
3474 // turn it into a texture
3475 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3476 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3477 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003478 // this binds the given EGLImage as a framebuffer for the
3479 // duration of this scope.
3480 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3481 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003482 // this will in fact render into our dequeued buffer
3483 // via an FBO, which means we didn't have to create
3484 // an EGLSurface and therefore we're not
3485 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003486 renderScreenImplLocked(
3487 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3488 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003489
Riley Andrews86639902014-08-15 12:27:24 -07003490 // Attempt to create a sync khr object that can produce a sync point. If that
3491 // isn't available, create a non-dupable sync object in the fallback path and
3492 // wait on it directly.
3493 EGLSyncKHR sync;
3494 if (!DEBUG_SCREENSHOTS) {
3495 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Andy McFadden2d8d1202013-10-09 16:38:02 -07003496 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003497 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003498 }
Riley Andrews86639902014-08-15 12:27:24 -07003499 if (sync != EGL_NO_SYNC_KHR) {
3500 // get the sync fd
3501 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3502 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3503 ALOGW("captureScreen: failed to dup sync khr object");
3504 syncFd = -1;
3505 }
3506 eglDestroySyncKHR(mEGLDisplay, sync);
3507 } else {
3508 // fallback path
3509 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3510 if (sync != EGL_NO_SYNC_KHR) {
3511 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3512 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3513 EGLint eglErr = eglGetError();
3514 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3515 ALOGW("captureScreen: fence wait timed out");
3516 } else {
3517 ALOGW_IF(eglErr != EGL_SUCCESS,
3518 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3519 }
3520 eglDestroySyncKHR(mEGLDisplay, sync);
3521 } else {
3522 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3523 }
3524 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003525 if (DEBUG_SCREENSHOTS) {
3526 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3527 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3528 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3529 hw, minLayerZ, maxLayerZ);
3530 delete [] pixels;
3531 }
3532
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003533 } else {
3534 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3535 result = INVALID_OPERATION;
3536 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003537 // destroy our image
3538 eglDestroyImageKHR(mEGLDisplay, image);
3539 } else {
3540 result = BAD_VALUE;
3541 }
Jesse Hallafe2b1f2014-10-21 11:09:17 -07003542 // queueBuffer takes ownership of syncFd
Riley Andrews86639902014-08-15 12:27:24 -07003543 window->queueBuffer(window, buffer, syncFd);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003544 }
3545 } else {
3546 result = BAD_VALUE;
3547 }
3548 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3549 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003550
Mathias Agopian74c40c02010-09-29 13:02:36 -07003551 return result;
3552}
3553
Mathias Agopiand5556842013-09-19 17:08:37 -07003554void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3555 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003556 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003557 for (size_t y=0 ; y<h ; y++) {
3558 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3559 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003560 if (p[x] != 0xFF000000) return;
3561 }
3562 }
3563 ALOGE("*** we just took a black screenshot ***\n"
3564 "requested minz=%d, maxz=%d, layerStack=%d",
3565 minLayerZ, maxLayerZ, hw->getLayerStack());
3566 const LayerVector& layers( mDrawingState.layersSortedByZ );
3567 const size_t count = layers.size();
3568 for (size_t i=0 ; i<count ; ++i) {
3569 const sp<Layer>& layer(layers[i]);
3570 const Layer::State& state(layer->getDrawingState());
3571 const bool visible = (state.layerStack == hw->getLayerStack())
3572 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3573 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003574 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003575 visible ? '+' : '-',
3576 i, layer->getName().string(), state.layerStack, state.z,
3577 layer->isVisible(), state.flags, state.alpha);
3578 }
3579 }
3580}
3581
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003582// ---------------------------------------------------------------------------
3583
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003584SurfaceFlinger::LayerVector::LayerVector() {
3585}
3586
3587SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003588 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003589}
3590
3591int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3592 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003593{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003594 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003595 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3596 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003597
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003598 uint32_t ls = l->getCurrentState().layerStack;
3599 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003600 if (ls != rs)
3601 return ls - rs;
3602
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003603 uint32_t lz = l->getCurrentState().z;
3604 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003605 if (lz != rz)
3606 return lz - rz;
3607
3608 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003609}
3610
3611// ---------------------------------------------------------------------------
3612
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003613SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Michael Lentine47e45402014-07-18 15:34:25 -07003614 : type(DisplayDevice::DISPLAY_ID_INVALID), width(0), height(0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003615}
3616
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003617SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Michael Lentine47e45402014-07-18 15:34:25 -07003618 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0), width(0), height(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003619 viewport.makeInvalid();
3620 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003621}
3622
3623// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003624
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003625}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003626
3627
3628#if defined(__gl_h_)
3629#error "don't include gl/gl.h in this file"
3630#endif
3631
3632#if defined(__gl2_h_)
3633#error "don't include gl2/gl2.h in this file"
3634#endif