blob: e21b9368dc4571b7bcb2a62d1b208edd01783c81 [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),
158 mHasColorMatrix(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159{
Steve Blocka19954a2012-01-04 20:05:49 +0000160 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161
162 // debugging stuff...
163 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700164
Mathias Agopianb4b17302013-03-20 18:36:41 -0700165 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700166 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700167
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 property_get("debug.sf.showupdates", value, "0");
169 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700170
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700171 property_get("debug.sf.ddms", value, "0");
172 mDebugDDMS = atoi(value);
173 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700174 if (!startDdmConnection()) {
175 // start failed, and DDMS debugging not enabled
176 mDebugDDMS = 0;
177 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700178 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700179 ALOGI_IF(mDebugRegion, "showupdates enabled");
180 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181}
182
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800183void SurfaceFlinger::onFirstRef()
184{
185 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800186}
187
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188SurfaceFlinger::~SurfaceFlinger()
189{
Mathias Agopiana4912602012-07-12 14:25:33 -0700190 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
191 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
192 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193}
194
Dan Stozac7014012014-02-14 15:03:43 -0800195void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800196{
197 // the window manager died on us. prepare its eulogy.
198
Andy McFadden13a082e2012-08-24 10:16:42 -0700199 // restore initial conditions (default device unblank, etc)
200 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800201
202 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700203 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800204}
205
Mathias Agopian7e27f052010-05-28 14:22:23 -0700206sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207{
Mathias Agopian96f08192010-06-02 23:28:45 -0700208 sp<ISurfaceComposerClient> bclient;
209 sp<Client> client(new Client(this));
210 status_t err = client->initCheck();
211 if (err == NO_ERROR) {
212 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214 return bclient;
215}
216
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700217sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
218 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700219{
220 class DisplayToken : public BBinder {
221 sp<SurfaceFlinger> flinger;
222 virtual ~DisplayToken() {
223 // no more references, this display must be terminated
224 Mutex::Autolock _l(flinger->mStateLock);
225 flinger->mCurrentState.displays.removeItem(this);
226 flinger->setTransactionFlags(eDisplayTransactionNeeded);
227 }
228 public:
229 DisplayToken(const sp<SurfaceFlinger>& flinger)
230 : flinger(flinger) {
231 }
232 };
233
234 sp<BBinder> token = new DisplayToken(this);
235
236 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700237 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700238 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700239 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700240 mCurrentState.displays.add(token, info);
241
242 return token;
243}
244
Jesse Hall6c913be2013-08-08 12:15:49 -0700245void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
246 Mutex::Autolock _l(mStateLock);
247
248 ssize_t idx = mCurrentState.displays.indexOfKey(display);
249 if (idx < 0) {
250 ALOGW("destroyDisplay: invalid display token");
251 return;
252 }
253
254 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
255 if (!info.isVirtualDisplay()) {
256 ALOGE("destroyDisplay called for non-virtual display");
257 return;
258 }
259
260 mCurrentState.displays.removeItemsAt(idx);
261 setTransactionFlags(eDisplayTransactionNeeded);
262}
263
Jesse Hall692c7232012-11-08 15:41:56 -0800264void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
265 ALOGW_IF(mBuiltinDisplays[type],
266 "Overwriting display token for display type %d", type);
267 mBuiltinDisplays[type] = new BBinder();
268 DisplayDeviceState info(type);
269 // All non-virtual displays are currently considered secure.
270 info.isSecure = true;
271 mCurrentState.displays.add(mBuiltinDisplays[type], info);
272}
273
Mathias Agopiane57f2922012-08-09 16:29:12 -0700274sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700275 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700276 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
277 return NULL;
278 }
Jesse Hall692c7232012-11-08 15:41:56 -0800279 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700280}
281
Jamie Gennis9a78c902011-01-12 18:30:40 -0800282sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
283{
284 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
285 return gba;
286}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700287
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288void SurfaceFlinger::bootFinished()
289{
290 const nsecs_t now = systemTime();
291 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000292 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700293 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700294
295 // wait patiently for the window manager death
296 const String16 name("window");
297 sp<IBinder> window(defaultServiceManager()->getService(name));
298 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700299 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700300 }
301
302 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700303 // formerly we would just kill the process, but we now ask it to exit so it
304 // can choose where to stop the animation.
305 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306}
307
Mathias Agopian3f844832013-08-07 21:24:32 -0700308void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700309 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700310 RenderEngine& engine;
311 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700312 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700313 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
314 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700315 }
316 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700317 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700318 return true;
319 }
320 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700321 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700322}
323
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700324class DispSyncSource : public VSyncSource, private DispSync::Callback {
325public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700326 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
327 const char* label) :
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700328 mValue(0),
329 mPhaseOffset(phaseOffset),
330 mTraceVsync(traceVsync),
Andy McFadden5167ec62014-05-22 13:08:43 -0700331 mVsyncOnLabel(String8::format("VsyncOn-%s", label)),
332 mVsyncEventLabel(String8::format("VSYNC-%s", label)),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700333 mDispSync(dispSync) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700334
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700335 virtual ~DispSyncSource() {}
336
337 virtual void setVSyncEnabled(bool enable) {
338 // Do NOT lock the mutex here so as to avoid any mutex ordering issues
339 // with locking it in the onDispSyncEvent callback.
340 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700341 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700342 static_cast<DispSync::Callback*>(this));
343 if (err != NO_ERROR) {
344 ALOGE("error registering vsync callback: %s (%d)",
345 strerror(-err), err);
346 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700347 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700348 } else {
349 status_t err = mDispSync->removeEventListener(
350 static_cast<DispSync::Callback*>(this));
351 if (err != NO_ERROR) {
352 ALOGE("error unregistering vsync callback: %s (%d)",
353 strerror(-err), err);
354 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700355 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700356 }
357 }
358
359 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
360 Mutex::Autolock lock(mMutex);
361 mCallback = callback;
362 }
363
364private:
365 virtual void onDispSyncEvent(nsecs_t when) {
366 sp<VSyncSource::Callback> callback;
367 {
368 Mutex::Autolock lock(mMutex);
369 callback = mCallback;
370
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700371 if (mTraceVsync) {
372 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700373 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700374 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700375 }
376
377 if (callback != NULL) {
378 callback->onVSyncEvent(when);
379 }
380 }
381
382 int mValue;
383
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700384 const nsecs_t mPhaseOffset;
385 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700386 const String8 mVsyncOnLabel;
387 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700388
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700389 DispSync* mDispSync;
390 sp<VSyncSource::Callback> mCallback;
391 Mutex mMutex;
392};
393
394void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700395 ALOGI( "SurfaceFlinger's main thread ready to run. "
396 "Initializing graphics H/W...");
397
Jesse Hallb65f32e2013-09-27 22:10:47 -0700398 status_t err;
Jesse Hall692c7232012-11-08 15:41:56 -0800399 Mutex::Autolock _l(mStateLock);
400
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700401 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700402 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
403 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700404
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700405 // Initialize the H/W composer object. There may or may not be an
406 // actual hardware composer underneath.
407 mHwc = new HWComposer(this,
408 *static_cast<HWComposer::EventHandler *>(this));
409
Mathias Agopian875d8e12013-06-07 15:35:48 -0700410 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800411 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700412
413 // retrieve the EGL context that was selected/created
414 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700415
Mathias Agopianda27af92012-09-13 18:17:13 -0700416 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
417 "couldn't create EGLContext");
418
Mathias Agopiancde87a32012-09-13 14:09:01 -0700419 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700420 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700421 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700422 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700423 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700424 // All non-virtual displays are currently considered secure.
425 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800426 createBuiltinDisplayLocked(type);
427 wp<IBinder> token = mBuiltinDisplays[i];
428
Dan Stozab9b08832014-03-13 11:55:57 -0700429 sp<IGraphicBufferProducer> producer;
430 sp<IGraphicBufferConsumer> consumer;
431 BufferQueue::createBufferQueue(&producer, &consumer,
432 new GraphicBufferAlloc());
433
434 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
435 consumer);
Jesse Hall19e87292013-12-23 21:02:15 -0800436 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700437 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800438 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Dan Stozab9b08832014-03-13 11:55:57 -0700439 fbs, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -0800440 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700441 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700442 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700443 // for displays other than the main display, so we always
444 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800445 ALOGD("marking display %zu as acquired/unblanked", i);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700446 hw->setPowerMode(HWC_POWER_MODE_NORMAL);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700447 }
448 mDisplays.add(token, hw);
449 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700450 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700451
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700452 // make the GLContext current so that we can create textures when creating Layers
453 // (which may happens before we render something)
454 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
455
Mathias Agopian028508c2012-07-25 21:12:12 -0700456 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700457 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700458 vsyncPhaseOffsetNs, true, "app");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700459 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700460 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700461 sfVsyncPhaseOffsetNs, true, "sf");
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700462 mSFEventThread = new EventThread(sfVsyncSrc);
463 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700464
Jamie Gennisd1700752013-10-14 12:22:52 -0700465 mEventControlThread = new EventControlThread(this);
466 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
467
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700468 // set a fake vsync period if there is no HWComposer
469 if (mHwc->initCheck() != NO_ERROR) {
470 mPrimaryDispSync.setPeriod(16666667);
471 }
472
Mathias Agopian92a979a2012-08-02 18:32:23 -0700473 // initialize our drawing state
474 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700475
Andy McFadden13a082e2012-08-24 10:16:42 -0700476 // set initial conditions (e.g. unblank default device)
477 initializeDisplays();
478
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700479 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700480 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800481}
482
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700483int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700484 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700485 type : mHwc->allocateDisplayId();
486}
487
Mathias Agopiana67e4182012-06-19 17:26:12 -0700488void SurfaceFlinger::startBootAnim() {
489 // start boot animation
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -0700490 mBootFinished = false;
Mathias Agopiana67e4182012-06-19 17:26:12 -0700491 property_set("service.bootanim.exit", "0");
492 property_set("ctl.start", "bootanim");
493}
494
Mathias Agopian875d8e12013-06-07 15:35:48 -0700495size_t SurfaceFlinger::getMaxTextureSize() const {
496 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700497}
498
Mathias Agopian875d8e12013-06-07 15:35:48 -0700499size_t SurfaceFlinger::getMaxViewportDims() const {
500 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700501}
502
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800503// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800504
Jamie Gennis582270d2011-08-17 18:19:00 -0700505bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800506 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800507 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800508 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700509 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800510}
511
Dan Stoza7f7da322014-05-02 15:26:25 -0700512status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
513 Vector<DisplayInfo>* configs) {
514 if (configs == NULL) {
515 return BAD_VALUE;
516 }
517
Jesse Hall692c7232012-11-08 15:41:56 -0800518 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700519 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800520 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700521 type = i;
522 break;
523 }
524 }
525
526 if (type < 0) {
527 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700528 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700529
Mathias Agopian8b736f12012-08-13 17:54:26 -0700530 // TODO: Not sure if display density should handled by SF any longer
531 class Density {
532 static int getDensityFromProperty(char const* propName) {
533 char property[PROPERTY_VALUE_MAX];
534 int density = 0;
535 if (property_get(propName, property, NULL) > 0) {
536 density = atoi(property);
537 }
538 return density;
539 }
540 public:
541 static int getEmuDensity() {
542 return getDensityFromProperty("qemu.sf.lcd_density"); }
543 static int getBuildDensity() {
544 return getDensityFromProperty("ro.sf.lcd_density"); }
545 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700546
Dan Stoza7f7da322014-05-02 15:26:25 -0700547 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700548
Dan Stoza7f7da322014-05-02 15:26:25 -0700549 const Vector<HWComposer::DisplayConfig>& hwConfigs =
550 getHwComposer().getConfigs(type);
551 for (size_t c = 0; c < hwConfigs.size(); ++c) {
552 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
553 DisplayInfo info = DisplayInfo();
554
555 float xdpi = hwConfig.xdpi;
556 float ydpi = hwConfig.ydpi;
557
558 if (type == DisplayDevice::DISPLAY_PRIMARY) {
559 // The density of the device is provided by a build property
560 float density = Density::getBuildDensity() / 160.0f;
561 if (density == 0) {
562 // the build doesn't provide a density -- this is wrong!
563 // use xdpi instead
564 ALOGE("ro.sf.lcd_density must be defined as a build property");
565 density = xdpi / 160.0f;
566 }
567 if (Density::getEmuDensity()) {
568 // if "qemu.sf.lcd_density" is specified, it overrides everything
569 xdpi = ydpi = density = Density::getEmuDensity();
570 density /= 160.0f;
571 }
572 info.density = density;
573
574 // TODO: this needs to go away (currently needed only by webkit)
575 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
576 info.orientation = hw->getOrientation();
577 } else {
578 // TODO: where should this value come from?
579 static const int TV_DENSITY = 213;
580 info.density = TV_DENSITY / 160.0f;
581 info.orientation = 0;
582 }
583
584 info.w = hwConfig.width;
585 info.h = hwConfig.height;
586 info.xdpi = xdpi;
587 info.ydpi = ydpi;
588 info.fps = float(1e9 / hwConfig.refresh);
Andy McFadden91b2ca82014-06-13 14:04:23 -0700589 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
590
591 // This is how far in advance a buffer must be queued for
592 // presentation at a given time. If you want a buffer to appear
593 // on the screen at time N, you must submit the buffer before
594 // (N - presentationDeadline).
595 //
596 // Normally it's one full refresh period (to give SF a chance to
597 // latch the buffer), but this can be reduced by configuring a
598 // DispSync offset. Any additional delays introduced by the hardware
599 // composer or panel must be accounted for here.
600 //
601 // We add an additional 1ms to allow for processing time and
602 // differences between the ideal and actual refresh rate.
603 info.presentationDeadline =
604 hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700605
606 // All non-virtual displays are currently considered secure.
607 info.secure = true;
608
609 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700610 }
611
Dan Stoza7f7da322014-05-02 15:26:25 -0700612 return NO_ERROR;
613}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700614
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700615status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& display,
616 DisplayStatInfo* stats) {
617 if (stats == NULL) {
618 return BAD_VALUE;
619 }
620
621 // FIXME for now we always return stats for the primary display
622 memset(stats, 0, sizeof(*stats));
623 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
624 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
625 return NO_ERROR;
626}
627
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700628int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
629 return getDisplayDevice(display)->getActiveConfig();
Dan Stoza7f7da322014-05-02 15:26:25 -0700630}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700631
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700632void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
633 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
634 this);
635 int32_t type = hw->getDisplayType();
636 int currentMode = hw->getActiveConfig();
637
638 if (mode == currentMode) {
639 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
640 return;
641 }
642
643 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
644 ALOGW("Trying to set config for virtual display");
645 return;
646 }
647
648 hw->setActiveConfig(mode);
649 getHwComposer().setActiveConfig(type, mode);
650}
651
652status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
653 class MessageSetActiveConfig: public MessageBase {
654 SurfaceFlinger& mFlinger;
655 sp<IBinder> mDisplay;
656 int mMode;
657 public:
658 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
659 int mode) :
660 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
661 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700662 Vector<DisplayInfo> configs;
663 mFlinger.getDisplayConfigs(mDisplay, &configs);
664 if(mMode < 0 || mMode >= configs.size()) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700665 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700666 mMode, configs.size());
667 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700668 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
669 if (hw == NULL) {
670 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700671 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700672 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
673 ALOGW("Attempt to set active config = %d for virtual display",
674 mMode);
675 } else {
676 mFlinger.setActiveConfigInternal(hw, mMode);
677 }
678 return true;
679 }
680 };
681 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
682 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700683 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700684}
685
Svetoslavd85084b2014-03-20 10:28:31 -0700686status_t SurfaceFlinger::clearAnimationFrameStats() {
687 Mutex::Autolock _l(mStateLock);
688 mAnimFrameTracker.clearStats();
689 return NO_ERROR;
690}
691
692status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
693 Mutex::Autolock _l(mStateLock);
694 mAnimFrameTracker.getStats(outStats);
695 return NO_ERROR;
696}
697
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800698// ----------------------------------------------------------------------------
699
700sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800701 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700702}
703
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800704// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800705
706void SurfaceFlinger::waitForEvent() {
707 mEventQueue.waitMessage();
708}
709
710void SurfaceFlinger::signalTransaction() {
711 mEventQueue.invalidate();
712}
713
714void SurfaceFlinger::signalLayerUpdate() {
715 mEventQueue.invalidate();
716}
717
718void SurfaceFlinger::signalRefresh() {
719 mEventQueue.refresh();
720}
721
722status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800723 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800724 return mEventQueue.postMessage(msg, reltime);
725}
726
727status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800728 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800729 status_t res = mEventQueue.postMessage(msg, reltime);
730 if (res == NO_ERROR) {
731 msg->wait();
732 }
733 return res;
734}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800735
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700736void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700737 do {
738 waitForEvent();
739 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800740}
741
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700742void SurfaceFlinger::enableHardwareVsync() {
743 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700744 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700745 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700746 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
747 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700748 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700749 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700750}
751
Jesse Hall948fe0c2013-10-14 12:56:09 -0700752void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700753 Mutex::Autolock _l(mHWVsyncLock);
754
Jesse Hall948fe0c2013-10-14 12:56:09 -0700755 if (makeAvailable) {
756 mHWVsyncAvailable = true;
757 } else if (!mHWVsyncAvailable) {
758 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
759 return;
760 }
761
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700762 const nsecs_t period =
763 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
764
765 mPrimaryDispSync.reset();
766 mPrimaryDispSync.setPeriod(period);
767
768 if (!mPrimaryHWVsyncEnabled) {
769 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700770 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
771 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700772 mPrimaryHWVsyncEnabled = true;
773 }
774}
775
Jesse Hall948fe0c2013-10-14 12:56:09 -0700776void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700777 Mutex::Autolock _l(mHWVsyncLock);
778 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700779 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
780 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700781 mPrimaryDispSync.endResync();
782 mPrimaryHWVsyncEnabled = false;
783 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700784 if (makeUnavailable) {
785 mHWVsyncAvailable = false;
786 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700787}
788
789void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700790 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700791
Jamie Gennisd1700752013-10-14 12:22:52 -0700792 { // Scope for the lock
793 Mutex::Autolock _l(mHWVsyncLock);
794 if (type == 0 && mPrimaryHWVsyncEnabled) {
795 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700796 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700797 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700798
799 if (needsHwVsync) {
800 enableHardwareVsync();
801 } else {
802 disableHardwareVsync(false);
803 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700804}
805
806void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
807 if (mEventThread == NULL) {
808 // This is a temporary workaround for b/7145521. A non-null pointer
809 // does not mean EventThread has finished initializing, so this
810 // is not a correct fix.
811 ALOGW("WARNING: EventThread not started, ignoring hotplug");
812 return;
813 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700814
Jesse Hall9e663de2013-08-16 14:28:37 -0700815 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700816 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800817 if (connected) {
818 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700819 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800820 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
821 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700822 }
823 setTransactionFlags(eDisplayTransactionNeeded);
824
Andy McFadden9e9689c2012-10-10 18:17:51 -0700825 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700826 }
Mathias Agopian86303202012-07-24 22:46:10 -0700827}
828
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700829void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700830 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700831 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700832}
833
Mathias Agopian4fec8732012-06-29 14:12:52 -0700834void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800835 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800836 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700837 case MessageQueue::TRANSACTION:
838 handleMessageTransaction();
839 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700840 case MessageQueue::INVALIDATE:
841 handleMessageTransaction();
842 handleMessageInvalidate();
843 signalRefresh();
844 break;
845 case MessageQueue::REFRESH:
846 handleMessageRefresh();
847 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800848 }
849}
850
Mathias Agopian4fec8732012-06-29 14:12:52 -0700851void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700852 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700853 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700854 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700855 }
856}
857
858void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700859 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700860 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700861}
862
863void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700864 ATRACE_CALL();
865 preComposition();
866 rebuildLayerStacks();
867 setUpHWComposer();
868 doDebugFlashRegions();
869 doComposition();
870 postComposition();
871}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700872
Mathias Agopiancd60f992012-08-16 16:28:27 -0700873void SurfaceFlinger::doDebugFlashRegions()
874{
875 // is debugging enabled
876 if (CC_LIKELY(!mDebugRegion))
877 return;
878
879 const bool repaintEverything = mRepaintEverything;
880 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
881 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700882 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700883 // transform the dirty region into this screen's coordinate space
884 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
885 if (!dirtyRegion.isEmpty()) {
886 // redraw the whole screen
887 doComposeSurfaces(hw, Region(hw->bounds()));
888
889 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700890 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700891 RenderEngine& engine(getRenderEngine());
892 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
893
Mathias Agopiancd60f992012-08-16 16:28:27 -0700894 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700895 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700896 }
897 }
898 }
899
900 postFramebuffer();
901
902 if (mDebugRegion > 1) {
903 usleep(mDebugRegion * 1000);
904 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700905
906 HWComposer& hwc(getHwComposer());
907 if (hwc.initCheck() == NO_ERROR) {
908 status_t err = hwc.prepare();
909 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
910 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700911}
912
913void SurfaceFlinger::preComposition()
914{
915 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700916 const LayerVector& layers(mDrawingState.layersSortedByZ);
917 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700918 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700919 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700920 needExtraInvalidate = true;
921 }
922 }
923 if (needExtraInvalidate) {
924 signalLayerUpdate();
925 }
926}
927
928void SurfaceFlinger::postComposition()
929{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700930 const LayerVector& layers(mDrawingState.layersSortedByZ);
931 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700932 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700933 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700934 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800935
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700936 const HWComposer& hwc = getHwComposer();
937 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
938
939 if (presentFence->isValid()) {
940 if (mPrimaryDispSync.addPresentFence(presentFence)) {
941 enableHardwareVsync();
942 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700943 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700944 }
945 }
946
Andy McFadden5167ec62014-05-22 13:08:43 -0700947 if (kIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700948 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700949 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700950 enableHardwareVsync();
951 }
952 }
953
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800954 if (mAnimCompositionPending) {
955 mAnimCompositionPending = false;
956
Jesse Halla9a1b002013-02-27 16:39:25 -0800957 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800958 mAnimFrameTracker.setActualPresentFence(presentFence);
959 } else {
960 // The HWC doesn't support present fences, so use the refresh
961 // timestamp instead.
962 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
963 mAnimFrameTracker.setActualPresentTime(presentTime);
964 }
965 mAnimFrameTracker.advanceFrame();
966 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700967}
968
969void SurfaceFlinger::rebuildLayerStacks() {
970 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700971 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700972 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700973 mVisibleRegionsDirty = false;
974 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700975
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700976 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700977 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700978 Region opaqueRegion;
979 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800980 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700981 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700982 const Transform& tr(hw->getTransform());
983 const Rect bounds(hw->getBounds());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700984 if (hw->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700985 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700986 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700987
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700988 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700989 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700990 const sp<Layer>& layer(layers[i]);
991 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700992 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700993 Region drawRegion(tr.transform(
994 layer->visibleNonTransparentRegion));
995 drawRegion.andSelf(bounds);
996 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700997 layersSortedByZ.add(layer);
998 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700999 }
1000 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001001 }
Mathias Agopian42977342012-08-05 00:40:46 -07001002 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001003 hw->undefinedRegion.set(bounds);
1004 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
1005 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001006 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001007 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001008}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001009
Mathias Agopiancd60f992012-08-16 16:28:27 -07001010void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001011 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001012 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1013 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1014 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1015
1016 // If nothing has changed (!dirty), don't recompose.
1017 // If something changed, but we don't currently have any visible layers,
1018 // and didn't when we last did a composition, then skip it this time.
1019 // The second rule does two things:
1020 // - When all layers are removed from a display, we'll emit one black
1021 // frame, then nothing more until we get new layers.
1022 // - When a display is created with a private layer stack, we won't
1023 // emit any black frames until a layer is added to the layer stack.
1024 bool mustRecompose = dirty && !(empty && wasEmpty);
1025
1026 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1027 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1028 mustRecompose ? "doing" : "skipping",
1029 dirty ? "+" : "-",
1030 empty ? "+" : "-",
1031 wasEmpty ? "+" : "-");
1032
Dan Stoza71433162014-02-04 16:22:36 -08001033 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001034
1035 if (mustRecompose) {
1036 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1037 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001038 }
1039
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001040 HWComposer& hwc(getHwComposer());
1041 if (hwc.initCheck() == NO_ERROR) {
1042 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001043 if (CC_UNLIKELY(mHwWorkListDirty)) {
1044 mHwWorkListDirty = false;
1045 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1046 sp<const DisplayDevice> hw(mDisplays[dpy]);
1047 const int32_t id = hw->getHwcDisplayId();
1048 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001049 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001050 hw->getVisibleLayersSortedByZ());
1051 const size_t count = currentLayers.size();
1052 if (hwc.createWorkList(id, count) == NO_ERROR) {
1053 HWComposer::LayerListIterator cur = hwc.begin(id);
1054 const HWComposer::LayerListIterator end = hwc.end(id);
1055 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001056 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001057 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001058 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001059 cur->setSkip(true);
1060 }
1061 }
1062 }
1063 }
1064 }
1065 }
1066
1067 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001068 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001069 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001070 const int32_t id = hw->getHwcDisplayId();
1071 if (id >= 0) {
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001072 // Get the layers in the current drawying state
1073 const LayerVector& layers(mDrawingState.layersSortedByZ);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001074#ifdef QCOM_BSP
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001075 bool freezeSurfacePresent = false;
1076 const size_t layerCount = layers.size();
1077 char value[PROPERTY_VALUE_MAX];
1078 property_get("sys.disable_ext_animation", value, "0");
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001079 if(atoi(value) && (id != HWC_DISPLAY_PRIMARY)) {
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001080 for (size_t i = 0 ; i < layerCount ; ++i) {
1081 static int screenShotLen = strlen("ScreenshotSurface");
1082 const sp<Layer>& layer(layers[i]);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001083 const Layer::State& s(layer->getDrawingState());
1084 // check the layers associated with external display
1085 if(s.layerStack == hw->getLayerStack()) {
1086 if(!strncmp(layer->getName(), "ScreenshotSurface",
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001087 screenShotLen)) {
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001088 // Screenshot layer is present, and animation in
1089 // progress
1090 freezeSurfacePresent = true;
1091 break;
1092 }
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001093 }
1094 }
1095 }
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001096#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001097 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001098 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001099 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001100 HWComposer::LayerListIterator cur = hwc.begin(id);
1101 const HWComposer::LayerListIterator end = hwc.end(id);
1102 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1103 /*
1104 * update the per-frame h/w composer data for each layer
1105 * and build the transparent region of the FB
1106 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001107 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001108 layer->setPerFrameData(hw, *cur);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001109#ifdef QCOM_BSP
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001110 if(freezeSurfacePresent) {
1111 // if freezeSurfacePresent, set ANIMATING flag
1112 cur->setAnimating(true);
1113 } else {
1114 const KeyedVector<wp<IBinder>, DisplayDeviceState>&
1115 draw(mDrawingState.displays);
1116 size_t dc = draw.size();
1117 for (size_t i=0 ; i<dc ; i++) {
1118 if (draw[i].isMainDisplay()) {
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001119 // Pass the current orientation to HWC
1120 hwc.eventControl(HWC_DISPLAY_PRIMARY,
1121 SurfaceFlinger::EVENT_ORIENTATION,
1122 uint32_t(draw[i].orientation));
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001123 }
1124 }
1125 }
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001126#endif
Mathias Agopian1e260872012-08-08 18:35:12 -07001127 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001128 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001129 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001130
Riley Andrews03414a12014-07-01 14:22:59 -07001131 // If possible, attempt to use the cursor overlay on each display.
1132 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1133 sp<const DisplayDevice> hw(mDisplays[dpy]);
1134 const int32_t id = hw->getHwcDisplayId();
1135 if (id >= 0) {
1136 const Vector< sp<Layer> >& currentLayers(
1137 hw->getVisibleLayersSortedByZ());
1138 const size_t count = currentLayers.size();
1139 HWComposer::LayerListIterator cur = hwc.begin(id);
1140 const HWComposer::LayerListIterator end = hwc.end(id);
1141 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1142 const sp<Layer>& layer(currentLayers[i]);
1143 if (layer->isPotentialCursor()) {
1144 cur->setIsCursorLayerHint();
1145 break;
1146 }
1147 }
1148 }
1149 }
1150
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001151 status_t err = hwc.prepare();
1152 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001153
1154 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1155 sp<const DisplayDevice> hw(mDisplays[dpy]);
1156 hw->prepareFrame(hwc);
1157 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001158 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001159}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001160
Mathias Agopiancd60f992012-08-16 16:28:27 -07001161void SurfaceFlinger::doComposition() {
1162 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001163 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001164 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001165 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001166 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001167 // transform the dirty region into this screen's coordinate space
1168 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001169
1170 // repaint the framebuffer (if needed)
1171 doDisplayComposition(hw, dirtyRegion);
1172
Mathias Agopiancd60f992012-08-16 16:28:27 -07001173 hw->dirtyRegion.clear();
1174 hw->flip(hw->swapRegion);
1175 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001176 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001177 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001178 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001179 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001180 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001181}
1182
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183void SurfaceFlinger::postFramebuffer()
1184{
Mathias Agopian841cde52012-03-01 15:44:37 -08001185 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001186
Mathias Agopiana44b0412011-10-16 18:46:35 -07001187 const nsecs_t now = systemTime();
1188 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001189
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001190 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001191 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001192 if (!hwc.supportsFramebufferTarget()) {
1193 // EGL spec says:
1194 // "surface must be bound to the calling thread's current context,
1195 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001196 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001197 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001198 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001199 }
1200
Mathias Agopian6da15f42013-09-25 20:40:07 -07001201 // make the default display current because the VirtualDisplayDevice code cannot
1202 // deal with dequeueBuffer() being called outside of the composition loop; however
1203 // the code below can call glFlush() which is allowed (and does in some case) call
1204 // dequeueBuffer().
1205 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1206
Mathias Agopian92a979a2012-08-02 18:32:23 -07001207 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001208 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001209 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001210 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001211 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001212 int32_t id = hw->getHwcDisplayId();
1213 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001214 HWComposer::LayerListIterator cur = hwc.begin(id);
1215 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001216 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001217 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001218 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001219 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001220 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001221 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001222 }
Jesse Hallef194142012-06-14 14:45:17 -07001223 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001224 }
1225
Mathias Agopiana44b0412011-10-16 18:46:35 -07001226 mLastSwapBufferTime = systemTime() - now;
1227 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001228
1229 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1230 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1231 logFrameStats();
1232 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233}
1234
Mathias Agopian87baae12012-07-31 12:38:26 -07001235void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001236{
Mathias Agopian841cde52012-03-01 15:44:37 -08001237 ATRACE_CALL();
1238
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001239 // here we keep a copy of the drawing state (that is the state that's
1240 // going to be overwritten by handleTransactionLocked()) outside of
1241 // mStateLock so that the side-effects of the State assignment
1242 // don't happen with mStateLock held (which can cause deadlocks).
1243 State drawingState(mDrawingState);
1244
Mathias Agopianca4d3602011-05-19 15:38:14 -07001245 Mutex::Autolock _l(mStateLock);
1246 const nsecs_t now = systemTime();
1247 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001248
Mathias Agopianca4d3602011-05-19 15:38:14 -07001249 // Here we're guaranteed that some transaction flags are set
1250 // so we can call handleTransactionLocked() unconditionally.
1251 // We call getTransactionFlags(), which will also clear the flags,
1252 // with mStateLock held to guarantee that mCurrentState won't change
1253 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001254
Mathias Agopiane57f2922012-08-09 16:29:12 -07001255 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001256 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001257
Mathias Agopianca4d3602011-05-19 15:38:14 -07001258 mLastTransactionTime = systemTime() - now;
1259 mDebugInTransaction = 0;
1260 invalidateHwcGeometry();
1261 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001262}
1263
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301264void SurfaceFlinger::setVirtualDisplayData(
1265 int32_t hwcDisplayId,
1266 const sp<IGraphicBufferProducer>& sink)
1267{
1268 sp<ANativeWindow> mNativeWindow = new Surface(sink);
1269 ANativeWindow* const window = mNativeWindow.get();
1270
1271 int format;
1272 window->query(window, NATIVE_WINDOW_FORMAT, &format);
1273
1274 EGLSurface surface;
1275 EGLint w, h;
1276 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1277 surface = eglCreateWindowSurface(display, mRenderEngine->getEGLConfig(), window, NULL);
1278 eglQuerySurface(display, surface, EGL_WIDTH, &w);
1279 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
1280
1281 mHwc->setVirtualDisplayProperties(hwcDisplayId, w, h, format);
1282}
1283
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001284void SurfaceFlinger::configureVirtualDisplay(int32_t &hwcDisplayId,
1285 sp<DisplaySurface> &dispSurface,
1286 sp<IGraphicBufferProducer> &producer,
1287 const DisplayDeviceState state,
1288 sp<IGraphicBufferProducer> bqProducer,
1289 sp<IGraphicBufferConsumer> bqConsumer)
1290{
1291 bool vdsEnabled = mHwc->isVDSEnabled();
1292
1293 //for V4L2 based virtual display implementation
1294 if(!vdsEnabled) {
1295 // persist.sys.wfd.virtual will be set if WFD is launched via
1296 // settings app. This is currently being done in
1297 // ExtendedRemoteDisplay-WFD stack.
1298 // This flag will be reset at the time of disconnection of virtual WFD
1299 // display.
1300 // This flag is set to zero if WFD is launched via QCOM WFD
1301 // proprietary APIs which use HDMI piggyback approach.
1302 char value[PROPERTY_VALUE_MAX];
1303 property_get("persist.sys.wfd.virtual", value, "0");
1304 int wfdVirtual = atoi(value);
1305 if(!wfdVirtual) {
1306 // This is for non-wfd virtual display scenarios(e.g. SSD/SR/CTS)
1307 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001308 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1309 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001310 dispSurface = vds;
1311 // There won't be any interaction with HWC for this virtual display.
1312 // so the GLES driver can pass buffers directly to the sink.
1313 producer = state.surface;
1314 } else {
Raj Kamal366d9b42014-06-11 13:49:32 +05301315 int sinkUsage = -1;
1316 state.surface->query(NATIVE_WINDOW_CONSUMER_USAGE_BITS, &sinkUsage);
1317#ifdef QCOM_BSP
1318 if(sinkUsage & GRALLOC_USAGE_PRIVATE_WFD)
1319#endif
1320 hwcDisplayId = allocateHwcDisplayId(state.type);
1321
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001322 if (hwcDisplayId >= 0) {
1323 // This is for WFD virtual display scenario.
1324 // Read virtual display properties and create a
1325 // rendering surface for it inorder to be handled by hwc.
1326 setVirtualDisplayData(hwcDisplayId, state.surface);
1327 dispSurface = new FramebufferSurface(*mHwc, state.type,
1328 bqConsumer);
1329 producer = bqProducer;
1330 } else {
1331 // in case of WFD Virtual + SSD/SR concurrency scenario,
1332 // WFD virtual display instance gets valid hwcDisplayId and
1333 // SSD/SR will get invalid hwcDisplayId
1334 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001335 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1336 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001337 dispSurface = vds;
1338 // There won't be any interaction with HWC for this virtual
1339 // display, so the GLES driver can pass buffers directly to the
1340 // sink.
1341 producer = state.surface;
1342 }
1343 }
1344 } else {
1345 // VDS solution is enabled
1346 // HWC is allocated for first virtual display.
1347 // Subsequent virtual display sessions will be composed by GLES driver.
1348 // ToDo: Modify VDS component to allocate hwcDisplayId based on
1349 // mForceHwcCopy (which is based on Usage Flags)
1350
1351 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001352 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1353 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001354 dispSurface = vds;
1355 if (hwcDisplayId >= 0) {
1356 producer = vds;
1357 } else {
1358 // There won't be any interaction with HWC for this virtual display,
1359 // so the GLES driver can pass buffers directly to the sink.
1360 producer = state.surface;
1361 }
1362 }
1363}
1364
Mathias Agopian87baae12012-07-31 12:38:26 -07001365void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001366{
1367 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001368 const size_t count = currentLayers.size();
1369
1370 /*
1371 * Traversal of the children
1372 * (perform the transaction for each of them if needed)
1373 */
1374
Mathias Agopian3559b072012-08-15 13:46:03 -07001375 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001376 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001377 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1379 if (!trFlags) continue;
1380
1381 const uint32_t flags = layer->doTransaction(0);
1382 if (flags & Layer::eVisibleRegion)
1383 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001384 }
1385 }
1386
1387 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001388 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001389 */
1390
Mathias Agopiane57f2922012-08-09 16:29:12 -07001391 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001392 // here we take advantage of Vector's copy-on-write semantics to
1393 // improve performance by skipping the transaction entirely when
1394 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001395 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1396 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001397 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001399 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001400 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001401
1402 // find the displays that were removed
1403 // (ie: in drawing state but not in current state)
1404 // also handle displays that changed
1405 // (ie: displays that are in both lists)
1406 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001407 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1408 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001409 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001410 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001411 // Call makeCurrent() on the primary display so we can
1412 // be sure that nothing associated with this display
1413 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001414 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001415 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001416 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1417 if (hw != NULL)
1418 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001419 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001420 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001421 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001422 } else {
1423 ALOGW("trying to remove the main display");
1424 }
1425 } else {
1426 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001427 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001428 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001429 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001430 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001431 // recreating the DisplayDevice, so we just remove it
1432 // from the drawing state, so that it get re-added
1433 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001434 sp<DisplayDevice> hw(getDisplayDevice(display));
1435 if (hw != NULL)
1436 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001437 mDisplays.removeItem(display);
1438 mDrawingState.displays.removeItemsAt(i);
1439 dc--; i--;
1440 // at this point we must loop to the next item
1441 continue;
1442 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001443
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001444 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001445 if (disp != NULL) {
1446 if (state.layerStack != draw[i].layerStack) {
1447 disp->setLayerStack(state.layerStack);
1448 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001449 if ((state.orientation != draw[i].orientation)
1450 || (state.viewport != draw[i].viewport)
1451 || (state.frame != draw[i].frame))
1452 {
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001453#ifdef QCOM_BSP
1454 int orient = state.orientation;
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001455 // Honor the orientation change after boot
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301456 // animation completes and make sure boot
1457 // animation is shown in panel orientation always.
1458 if(mBootFinished){
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001459 disp->setProjection(state.orientation,
1460 state.viewport, state.frame);
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001461 orient = state.orientation;
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001462 }
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301463 else{
1464 char property[PROPERTY_VALUE_MAX];
1465 int panelOrientation =
1466 DisplayState::eOrientationDefault;
1467 if(property_get("persist.panel.orientation",
1468 property, "0") > 0){
1469 panelOrientation = atoi(property) / 90;
1470 }
1471 disp->setProjection(panelOrientation,
1472 state.viewport, state.frame);
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001473 orient = panelOrientation;
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301474 }
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001475 // Set the view frame of each display only of its
1476 // default orientation.
Saurabh Shah027b3852014-08-21 16:09:32 -07001477 if(orient == DisplayState::eOrientationDefault and
1478 state.frame.isValid()) {
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001479 qdutils::setViewFrame(disp->getHwcDisplayId(),
1480 state.frame.left, state.frame.top,
1481 state.frame.right, state.frame.bottom);
1482 }
1483#else
1484 disp->setProjection(state.orientation,
1485 state.viewport, state.frame);
1486#endif
Mathias Agopian93997a82012-08-29 17:30:36 -07001487 }
Michael Lentine47e45402014-07-18 15:34:25 -07001488 if (state.width != draw[i].width || state.height != draw[i].height) {
1489 disp->setDisplaySize(state.width, state.height);
1490 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001491 }
1492 }
1493 }
1494
1495 // find displays that were added
1496 // (ie: in current state but not in drawing state)
1497 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001498 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001499 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001500
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001501 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001502 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001503 sp<IGraphicBufferProducer> bqProducer;
1504 sp<IGraphicBufferConsumer> bqConsumer;
1505 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1506 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001507
Jesse Hall02d86562013-03-25 14:43:23 -07001508 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001509 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001510 // Virtual displays without a surface are dormant:
1511 // they have external state (layer stack, projection,
1512 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001513 if (state.surface != NULL) {
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001514 configureVirtualDisplay(hwcDisplayId,
1515 dispSurface, producer, state, bqProducer,
1516 bqConsumer);
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001517 }
1518 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001519 ALOGE_IF(state.surface!=NULL,
1520 "adding a supported display, but rendering "
1521 "surface is provided (%p), ignoring it",
1522 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001523 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001524 // for supported (by hwc) displays we provide our
1525 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001526 dispSurface = new FramebufferSurface(*mHwc, state.type,
1527 bqConsumer);
1528 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001529 }
1530
1531 const wp<IBinder>& display(curr.keyAt(i));
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301532 if (dispSurface != NULL && producer != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001533 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001534 state.type, hwcDisplayId,
1535 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001536 display, dispSurface, producer,
1537 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001538 hw->setLayerStack(state.layerStack);
1539 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001540 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001541 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001542 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001543 if (state.isVirtualDisplay()) {
1544 if (hwcDisplayId >= 0) {
1545 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1546 hw->getWidth(), hw->getHeight(),
1547 hw->getFormat());
1548 }
1549 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001550 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001551 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001552 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001553 }
1554 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001555 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001556 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001557
Mathias Agopian84300952012-11-21 16:02:13 -08001558 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1559 // The transform hint might have changed for some layers
1560 // (either because a display has changed, or because a layer
1561 // as changed).
1562 //
1563 // Walk through all the layers in currentLayers,
1564 // and update their transform hint.
1565 //
1566 // If a layer is visible only on a single display, then that
1567 // display is used to calculate the hint, otherwise we use the
1568 // default display.
1569 //
1570 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1571 // the hint is set before we acquire a buffer from the surface texture.
1572 //
1573 // NOTE: layer transactions have taken place already, so we use their
1574 // drawing state. However, SurfaceFlinger's own transaction has not
1575 // happened yet, so we must use the current state layer list
1576 // (soon to become the drawing state list).
1577 //
1578 sp<const DisplayDevice> disp;
1579 uint32_t currentlayerStack = 0;
1580 for (size_t i=0; i<count; i++) {
1581 // NOTE: we rely on the fact that layers are sorted by
1582 // layerStack first (so we don't have to traverse the list
1583 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001584 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001585 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001586 if (i==0 || currentlayerStack != layerStack) {
1587 currentlayerStack = layerStack;
1588 // figure out if this layerstack is mirrored
1589 // (more than one display) if so, pick the default display,
1590 // if not, pick the only display it's on.
1591 disp.clear();
1592 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1593 sp<const DisplayDevice> hw(mDisplays[dpy]);
1594 if (hw->getLayerStack() == currentlayerStack) {
1595 if (disp == NULL) {
1596 disp = hw;
1597 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001598 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001599 break;
1600 }
1601 }
1602 }
1603 }
Chet Haase91d25932013-04-11 15:24:55 -07001604 if (disp == NULL) {
1605 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1606 // redraw after transform hint changes. See bug 8508397.
1607
1608 // could be null when this layer is using a layerStack
1609 // that is not visible on any display. Also can occur at
1610 // screen off/on times.
1611 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001612 }
Chet Haase91d25932013-04-11 15:24:55 -07001613 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001614 }
1615 }
1616
1617
Mathias Agopian3559b072012-08-15 13:46:03 -07001618 /*
1619 * Perform our own transaction if needed
1620 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001621
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001622 const LayerVector& layers(mDrawingState.layersSortedByZ);
1623 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001624 // layers have been added
1625 mVisibleRegionsDirty = true;
1626 }
1627
1628 // some layers might have been removed, so
1629 // we need to update the regions they're exposing.
1630 if (mLayersRemoved) {
1631 mLayersRemoved = false;
1632 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001633 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001634 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001635 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001636 if (currentLayers.indexOf(layer) < 0) {
1637 // this layer is not visible anymore
1638 // TODO: we could traverse the tree from front to back and
1639 // compute the actual visible region
1640 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001641 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001642 Region visibleReg = s.transform.transform(
1643 Region(Rect(s.active.w, s.active.h)));
1644 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001645 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001646 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001647 }
1648
1649 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001650
1651 updateCursorAsync();
1652}
1653
1654void SurfaceFlinger::updateCursorAsync()
1655{
1656 HWComposer& hwc(getHwComposer());
1657 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1658 sp<const DisplayDevice> hw(mDisplays[dpy]);
1659 const int32_t id = hw->getHwcDisplayId();
1660 if (id < 0) {
1661 continue;
1662 }
1663 const Vector< sp<Layer> >& currentLayers(
1664 hw->getVisibleLayersSortedByZ());
1665 const size_t count = currentLayers.size();
1666 HWComposer::LayerListIterator cur = hwc.begin(id);
1667 const HWComposer::LayerListIterator end = hwc.end(id);
1668 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1669 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1670 continue;
1671 }
1672 const sp<Layer>& layer(currentLayers[i]);
1673 Rect cursorPos = layer->getPosition(hw);
1674 hwc.setCursorPositionAsync(id, cursorPos);
1675 break;
1676 }
1677 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001678}
1679
1680void SurfaceFlinger::commitTransaction()
1681{
1682 if (!mLayersPendingRemoval.isEmpty()) {
1683 // Notify removed layers now that they can't be drawn from
1684 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1685 mLayersPendingRemoval[i]->onRemoved();
1686 }
1687 mLayersPendingRemoval.clear();
1688 }
1689
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001690 // If this transaction is part of a window animation then the next frame
1691 // we composite should be considered an animation as well.
1692 mAnimCompositionPending = mAnimTransactionPending;
1693
Mathias Agopian4fec8732012-06-29 14:12:52 -07001694 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001695 mTransactionPending = false;
1696 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001697 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001698}
1699
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001700void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001701 const LayerVector& currentLayers, uint32_t layerStack,
1702 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001703{
Mathias Agopian841cde52012-03-01 15:44:37 -08001704 ATRACE_CALL();
1705
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001706 Region aboveOpaqueLayers;
1707 Region aboveCoveredLayers;
1708 Region dirty;
1709
Mathias Agopian87baae12012-07-31 12:38:26 -07001710 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001711
1712 size_t i = currentLayers.size();
1713 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001714 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001715
1716 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001717 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001718
Jesse Hall01e29052013-02-19 16:13:35 -08001719 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001720 if (s.layerStack != layerStack)
1721 continue;
1722
Mathias Agopianab028732010-03-16 16:41:46 -07001723 /*
1724 * opaqueRegion: area of a surface that is fully opaque.
1725 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001726 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001727
1728 /*
1729 * visibleRegion: area of a surface that is visible on screen
1730 * and not fully transparent. This is essentially the layer's
1731 * footprint minus the opaque regions above it.
1732 * Areas covered by a translucent surface are considered visible.
1733 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001734 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001735
1736 /*
1737 * coveredRegion: area of a surface that is covered by all
1738 * visible regions above it (which includes the translucent areas).
1739 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001740 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001741
Jesse Halla8026d22012-09-25 13:25:04 -07001742 /*
1743 * transparentRegion: area of a surface that is hinted to be completely
1744 * transparent. This is only used to tell when the layer has no visible
1745 * non-transparent regions and can be removed from the layer list. It
1746 * does not affect the visibleRegion of this layer or any layers
1747 * beneath it. The hint may not be correct if apps don't respect the
1748 * SurfaceView restrictions (which, sadly, some don't).
1749 */
1750 Region transparentRegion;
1751
Mathias Agopianab028732010-03-16 16:41:46 -07001752
1753 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001754 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001755 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001756 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001757 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001758 if (!visibleRegion.isEmpty()) {
1759 // Remove the transparent area from the visible region
1760 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001761 const Transform tr(s.transform);
1762 if (tr.transformed()) {
1763 if (tr.preserveRects()) {
1764 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001765 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001766 } else {
1767 // transformation too complex, can't do the
1768 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001769 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001770 }
1771 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001772 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001773 }
Mathias Agopianab028732010-03-16 16:41:46 -07001774 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001775
Mathias Agopianab028732010-03-16 16:41:46 -07001776 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001777 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001778 if (s.alpha==255 && !translucent &&
1779 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1780 // the opaque region is the layer's footprint
1781 opaqueRegion = visibleRegion;
1782 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001783 }
1784 }
1785
Mathias Agopianab028732010-03-16 16:41:46 -07001786 // Clip the covered region to the visible region
1787 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1788
1789 // Update aboveCoveredLayers for next (lower) layer
1790 aboveCoveredLayers.orSelf(visibleRegion);
1791
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001792 // subtract the opaque region covered by the layers above us
1793 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001794
1795 // compute this layer's dirty region
1796 if (layer->contentDirty) {
1797 // we need to invalidate the whole region
1798 dirty = visibleRegion;
1799 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001800 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001801 layer->contentDirty = false;
1802 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001803 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001804 * the exposed region consists of two components:
1805 * 1) what's VISIBLE now and was COVERED before
1806 * 2) what's EXPOSED now less what was EXPOSED before
1807 *
1808 * note that (1) is conservative, we start with the whole
1809 * visible region but only keep what used to be covered by
1810 * something -- which mean it may have been exposed.
1811 *
1812 * (2) handles areas that were not covered by anything but got
1813 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001814 */
Mathias Agopianab028732010-03-16 16:41:46 -07001815 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001816 const Region oldVisibleRegion = layer->visibleRegion;
1817 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001818 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1819 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001820 }
1821 dirty.subtractSelf(aboveOpaqueLayers);
1822
1823 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001824 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001825
Mathias Agopianab028732010-03-16 16:41:46 -07001826 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001827 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001828
Jesse Halla8026d22012-09-25 13:25:04 -07001829 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001830 layer->setVisibleRegion(visibleRegion);
1831 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001832 layer->setVisibleNonTransparentRegion(
1833 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001834 }
1835
Mathias Agopian87baae12012-07-31 12:38:26 -07001836 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001837}
1838
Mathias Agopian87baae12012-07-31 12:38:26 -07001839void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1840 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001841 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001842 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1843 if (hw->getLayerStack() == layerStack) {
1844 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001845 }
1846 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001847}
1848
1849void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001850{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001851 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001852
Mathias Agopian4fec8732012-06-29 14:12:52 -07001853 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001854 const LayerVector& layers(mDrawingState.layersSortedByZ);
Eric Penner51c59cd2014-07-28 19:51:58 -07001855
1856 // Store the set of layers that need updates. This set must not change as
1857 // buffers are being latched, as this could result in a deadlock.
1858 // Example: Two producers share the same command stream and:
1859 // 1.) Layer 0 is latched
1860 // 2.) Layer 0 gets a new frame
1861 // 2.) Layer 1 gets a new frame
1862 // 3.) Layer 1 is latched.
1863 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1864 // second frame. But layer 0's second frame could be waiting on display.
1865 Vector<Layer*> layersWithQueuedFrames;
1866 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001867 const sp<Layer>& layer(layers[i]);
Eric Penner51c59cd2014-07-28 19:51:58 -07001868 if (layer->hasQueuedFrame())
1869 layersWithQueuedFrames.push_back(layer.get());
1870 }
1871 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
1872 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07001873 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001874 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001875 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001876 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001877
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001878 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001879}
1880
Mathias Agopianad456f92011-01-13 17:53:01 -08001881void SurfaceFlinger::invalidateHwcGeometry()
1882{
1883 mHwWorkListDirty = true;
1884}
1885
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001886
Mathias Agopiancd60f992012-08-16 16:28:27 -07001887void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001888 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001889{
Dan Stoza71433162014-02-04 16:22:36 -08001890 // We only need to actually compose the display if:
1891 // 1) It is being handled by hardware composer, which may need this to
1892 // keep its virtual display state machine in sync, or
1893 // 2) There is work to be done (the dirty region isn't empty)
1894 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1895 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1896 return;
1897 }
1898
Mathias Agopian87baae12012-07-31 12:38:26 -07001899 Region dirtyRegion(inDirtyRegion);
1900
Mathias Agopianb8a55602009-06-26 19:06:36 -07001901 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001902 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001903
Mathias Agopian42977342012-08-05 00:40:46 -07001904 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001905 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001906 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1907 // takes a rectangle, we must make sure to update that whole
1908 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001909 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001910 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001911 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001912 // We need to redraw the rectangle that will be updated
1913 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001914 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001915 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001916 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001917 } else {
1918 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001919 dirtyRegion.set(hw->bounds());
1920 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001921 }
1922 }
1923
Alan Viverette9c5a3332013-09-12 20:04:35 -07001924 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Michael Lentine3f121fc2014-10-01 11:17:28 -07001925 if (!doComposeSurfaces(hw, dirtyRegion)) return;
Mathias Agopianff2ed702013-09-01 21:36:12 -07001926 } else {
1927 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001928 mat4 colorMatrix = mColorMatrix;
1929 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001930 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001931 }
1932 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001933 doComposeSurfaces(hw, dirtyRegion);
1934 engine.endGroup();
1935 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001936
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001937 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001938 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001939
1940 // swap buffers (presentation)
1941 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001942}
1943
Michael Lentine3f121fc2014-10-01 11:17:28 -07001944bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001945{
Mathias Agopian3f844832013-08-07 21:24:32 -07001946 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001947 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001948 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001949 HWComposer::LayerListIterator cur = hwc.begin(id);
1950 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001951
Jesse Halld05a17f2013-09-30 16:49:30 -07001952 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001953 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001954 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001955 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1956 hw->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07001957 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1958 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
1959 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
1960 }
1961 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08001962 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001963
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001964 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001965 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001966 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001967 // when using overlays, we assume a fully transparent framebuffer
1968 // NOTE: we could reduce how much we need to clear, for instance
1969 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001970 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001971 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001972 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001973 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001974 // we start with the whole screen area
1975 const Region bounds(hw->getBounds());
1976
1977 // we remove the scissor part
1978 // we're left with the letterbox region
1979 // (common case is that letterbox ends-up being empty)
1980 const Region letterbox(bounds.subtract(hw->getScissor()));
1981
1982 // compute the area to clear
1983 Region region(hw->undefinedRegion.merge(letterbox));
1984
1985 // but limit it to the dirty region
1986 region.andSelf(dirty);
1987
Mathias Agopianb9494d52012-04-18 02:28:45 -07001988 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001989 if (!region.isEmpty()) {
Radhika Ranjan Soni35c9ff22013-12-17 19:55:17 +05301990 if (cur != end) {
1991 if (cur->getCompositionType() != HWC_BLIT)
1992 // can happen with SurfaceView
1993 drawWormhole(hw, region);
1994 } else
1995 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001996 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001997 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001998
Mathias Agopian766dc492012-10-30 18:08:06 -07001999 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
2000 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07002001 // scissor on the main display. It should never be needed
2002 // anyways (though in theory it could since the API allows it).
2003 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07002004 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002005 if (scissor != bounds) {
2006 // scissor doesn't match the screen's dimensions, so we
2007 // need to clear everything outside of it and enable
2008 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07002009
Mathias Agopianf45c5102012-10-24 16:29:17 -07002010 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002011 const uint32_t height = hw->getHeight();
2012 engine.setScissor(scissor.left, height - scissor.bottom,
2013 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002014 }
2015 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002016 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002017
Mathias Agopian85d751c2012-08-29 16:59:24 -07002018 /*
2019 * and then, render the layers targeted at the framebuffer
2020 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002021
Mathias Agopian13127d82013-03-05 17:47:11 -08002022 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002023 const size_t count = layers.size();
2024 const Transform& tr = hw->getTransform();
2025 if (cur != end) {
2026 // we're using h/w composer
2027 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002028 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07002029 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07002030 if (!clip.isEmpty()) {
2031 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07002032 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07002033 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07002034 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002035 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
2036 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08002037 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07002038 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07002039 // never clear the very first layer since we're
2040 // guaranteed the FB is already cleared
2041 layer->clearWithOpenGL(hw, clip);
2042 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002043 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07002044 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002045 case HWC_FRAMEBUFFER: {
2046 layer->draw(hw, clip);
2047 break;
2048 }
Arun Kumar K.R5111ff02012-12-19 18:10:46 -08002049 case HWC_BLIT:
2050 //Do nothing
2051 break;
Mathias Agopianda27af92012-09-13 18:17:13 -07002052 case HWC_FRAMEBUFFER_TARGET: {
2053 // this should not happen as the iterator shouldn't
2054 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08002055 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07002056 break;
2057 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07002058 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002059 }
2060 layer->setAcquireFence(hw, *cur);
2061 }
2062 } else {
2063 // we're not using h/w composer
2064 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002065 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002066 const Region clip(dirty.intersect(
2067 tr.transform(layer->visibleRegion)));
2068 if (!clip.isEmpty()) {
2069 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07002070 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002071 }
2072 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002073
2074 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002075 engine.disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07002076 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002077}
2078
Mathias Agopian3f844832013-08-07 21:24:32 -07002079void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07002080 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07002081 RenderEngine& engine(getRenderEngine());
2082 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002083}
2084
Mathias Agopianac9fa422013-02-11 16:40:36 -08002085void SurfaceFlinger::addClientLayer(const sp<Client>& client,
2086 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07002087 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08002088 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07002089{
Mathias Agopian96f08192010-06-02 23:28:45 -07002090 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08002091 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07002092
Mathias Agopian96f08192010-06-02 23:28:45 -07002093 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002094 Mutex::Autolock _l(mStateLock);
2095 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07002096 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07002097}
2098
Mathias Agopian3f844832013-08-07 21:24:32 -07002099status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002100 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08002101 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002102 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07002103 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002104 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07002105 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002106 return NO_ERROR;
2107 }
Mathias Agopian3d579642009-06-04 18:46:21 -07002108 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002109}
2110
Dan Stozac7014012014-02-14 15:03:43 -08002111uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07002112 return android_atomic_release_load(&mTransactionFlags);
2113}
2114
Mathias Agopian3f844832013-08-07 21:24:32 -07002115uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002116 return android_atomic_and(~flags, &mTransactionFlags) & flags;
2117}
2118
Mathias Agopian3f844832013-08-07 21:24:32 -07002119uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002120 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
2121 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002122 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002123 }
2124 return old;
2125}
2126
Mathias Agopian8b33f032012-07-24 20:43:54 -07002127void SurfaceFlinger::setTransactionState(
2128 const Vector<ComposerState>& state,
2129 const Vector<DisplayState>& displays,
2130 uint32_t flags)
2131{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002132 ATRACE_CALL();
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08002133 size_t count = displays.size();
2134#ifdef QCOM_BSP
2135 for (size_t i=0 ; i<count ; i++) {
2136 const DisplayState& s(displays[i]);
2137 if(s.token != mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]) {
2138 const uint32_t what = s.what;
2139 // Invalidate and Delay the binder thread by 50 ms on
2140 // eDisplayProjectionChanged to trigger a draw cycle so that
2141 // it can fix one incorrect frame on the External, when we disable
2142 // external animation
2143 if (what & DisplayState::eDisplayProjectionChanged) {
2144 invalidateHwcGeometry();
2145 repaintEverything();
2146 usleep(50000);
2147 }
2148 }
2149 }
2150#endif
Mathias Agopian698c0872011-06-28 19:09:31 -07002151 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07002152 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002153
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002154 if (flags & eAnimation) {
2155 // For window updates that are part of an animation we must wait for
2156 // previous animation "frames" to be handled.
2157 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002158 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002159 if (CC_UNLIKELY(err != NO_ERROR)) {
2160 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002161 // caller after a few seconds.
2162 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
2163 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002164 mAnimTransactionPending = false;
2165 break;
2166 }
2167 }
2168 }
2169
Mathias Agopiane57f2922012-08-09 16:29:12 -07002170 for (size_t i=0 ; i<count ; i++) {
2171 const DisplayState& s(displays[i]);
2172 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002173 }
2174
Mathias Agopiane57f2922012-08-09 16:29:12 -07002175 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002176 for (size_t i=0 ; i<count ; i++) {
2177 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002178 // Here we need to check that the interface we're given is indeed
2179 // one of our own. A malicious client could give us a NULL
2180 // IInterface, or one of its own or even one of our own but a
2181 // different type. All these situations would cause us to crash.
2182 //
2183 // NOTE: it would be better to use RTTI as we could directly check
2184 // that we have a Client*. however, RTTI is disabled in Android.
2185 if (s.client != NULL) {
2186 sp<IBinder> binder = s.client->asBinder();
2187 if (binder != NULL) {
2188 String16 desc(binder->getInterfaceDescriptor());
2189 if (desc == ISurfaceComposerClient::descriptor) {
2190 sp<Client> client( static_cast<Client *>(s.client.get()) );
2191 transactionFlags |= setClientStateLocked(client, s.state);
2192 }
2193 }
2194 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002195 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002196
Mathias Agopian386aa982011-11-07 21:58:03 -08002197 if (transactionFlags) {
2198 // this triggers the transaction
2199 setTransactionFlags(transactionFlags);
2200
2201 // if this is a synchronous transaction, wait for it to take effect
2202 // before returning.
2203 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002204 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002205 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002206 if (flags & eAnimation) {
2207 mAnimTransactionPending = true;
2208 }
2209 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002210 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2211 if (CC_UNLIKELY(err != NO_ERROR)) {
2212 // just in case something goes wrong in SF, return to the
2213 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002214 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2215 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002216 break;
2217 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002218 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002219 }
2220}
2221
Mathias Agopiane57f2922012-08-09 16:29:12 -07002222uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2223{
Jesse Hall9a143922012-10-04 16:29:19 -07002224 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2225 if (dpyIdx < 0)
2226 return 0;
2227
Mathias Agopiane57f2922012-08-09 16:29:12 -07002228 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002229 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002230 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002231 const uint32_t what = s.what;
2232 if (what & DisplayState::eSurfaceChanged) {
2233 if (disp.surface->asBinder() != s.surface->asBinder()) {
2234 disp.surface = s.surface;
2235 flags |= eDisplayTransactionNeeded;
2236 }
2237 }
2238 if (what & DisplayState::eLayerStackChanged) {
2239 if (disp.layerStack != s.layerStack) {
2240 disp.layerStack = s.layerStack;
2241 flags |= eDisplayTransactionNeeded;
2242 }
2243 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002244 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002245 if (disp.orientation != s.orientation) {
2246 disp.orientation = s.orientation;
2247 flags |= eDisplayTransactionNeeded;
2248 }
2249 if (disp.frame != s.frame) {
2250 disp.frame = s.frame;
2251 flags |= eDisplayTransactionNeeded;
2252 }
2253 if (disp.viewport != s.viewport) {
2254 disp.viewport = s.viewport;
2255 flags |= eDisplayTransactionNeeded;
2256 }
2257 }
Michael Lentine47e45402014-07-18 15:34:25 -07002258 if (what & DisplayState::eDisplaySizeChanged) {
2259 if (disp.width != s.width) {
2260 disp.width = s.width;
2261 flags |= eDisplayTransactionNeeded;
2262 }
2263 if (disp.height != s.height) {
2264 disp.height = s.height;
2265 flags |= eDisplayTransactionNeeded;
2266 }
2267 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002268 }
2269 return flags;
2270}
2271
2272uint32_t SurfaceFlinger::setClientStateLocked(
2273 const sp<Client>& client,
2274 const layer_state_t& s)
2275{
2276 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002277 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002278 if (layer != 0) {
2279 const uint32_t what = s.what;
2280 if (what & layer_state_t::ePositionChanged) {
2281 if (layer->setPosition(s.x, s.y))
2282 flags |= eTraversalNeeded;
2283 }
2284 if (what & layer_state_t::eLayerChanged) {
2285 // NOTE: index needs to be calculated before we update the state
2286 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2287 if (layer->setLayer(s.z)) {
2288 mCurrentState.layersSortedByZ.removeAt(idx);
2289 mCurrentState.layersSortedByZ.add(layer);
2290 // we need traversal (state changed)
2291 // AND transaction (list changed)
2292 flags |= eTransactionNeeded|eTraversalNeeded;
2293 }
2294 }
2295 if (what & layer_state_t::eSizeChanged) {
2296 if (layer->setSize(s.w, s.h)) {
2297 flags |= eTraversalNeeded;
2298 }
2299 }
2300 if (what & layer_state_t::eAlphaChanged) {
2301 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2302 flags |= eTraversalNeeded;
2303 }
2304 if (what & layer_state_t::eMatrixChanged) {
2305 if (layer->setMatrix(s.matrix))
2306 flags |= eTraversalNeeded;
2307 }
2308 if (what & layer_state_t::eTransparentRegionChanged) {
2309 if (layer->setTransparentRegionHint(s.transparentRegion))
2310 flags |= eTraversalNeeded;
2311 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08002312 if ((what & layer_state_t::eVisibilityChanged) ||
2313 (what & layer_state_t::eOpacityChanged)) {
2314 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07002315 if (layer->setFlags(s.flags, s.mask))
2316 flags |= eTraversalNeeded;
2317 }
2318 if (what & layer_state_t::eCropChanged) {
2319 if (layer->setCrop(s.crop))
2320 flags |= eTraversalNeeded;
2321 }
2322 if (what & layer_state_t::eLayerStackChanged) {
2323 // NOTE: index needs to be calculated before we update the state
2324 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2325 if (layer->setLayerStack(s.layerStack)) {
2326 mCurrentState.layersSortedByZ.removeAt(idx);
2327 mCurrentState.layersSortedByZ.add(layer);
2328 // we need traversal (state changed)
2329 // AND transaction (list changed)
2330 flags |= eTransactionNeeded|eTraversalNeeded;
2331 }
2332 }
2333 }
2334 return flags;
2335}
2336
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002337status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002338 const String8& name,
2339 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002340 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2341 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002342{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002343 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002344 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002345 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002346 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002347 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002348 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002349
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002350 status_t result = NO_ERROR;
2351
2352 sp<Layer> layer;
2353
Mathias Agopian3165cc22012-08-08 19:42:09 -07002354 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2355 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002356 result = createNormalLayer(client,
2357 name, w, h, flags, format,
2358 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002359 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002360 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002361 result = createDimLayer(client,
2362 name, w, h, flags,
2363 handle, gbp, &layer);
2364 break;
2365 default:
2366 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002367 break;
2368 }
2369
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002370 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002371 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002372 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002373 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002374 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002375}
2376
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002377status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2378 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2379 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002380{
2381 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002382 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002383 case PIXEL_FORMAT_TRANSPARENT:
2384 case PIXEL_FORMAT_TRANSLUCENT:
2385 format = PIXEL_FORMAT_RGBA_8888;
2386 break;
2387 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002388 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002389 break;
2390 }
2391
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002392 *outLayer = new Layer(this, client, name, w, h, flags);
2393 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2394 if (err == NO_ERROR) {
2395 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002396 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002397 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002398
2399 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2400 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002401}
2402
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002403status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2404 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2405 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002406{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002407 *outLayer = new LayerDim(this, client, name, w, h, flags);
2408 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002409 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002410 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002411}
2412
Mathias Agopianac9fa422013-02-11 16:40:36 -08002413status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002414{
Mathias Agopian67106042013-03-14 19:18:13 -07002415 // called by the window manager when it wants to remove a Layer
2416 status_t err = NO_ERROR;
2417 sp<Layer> l(client->getLayerUser(handle));
2418 if (l != NULL) {
2419 err = removeLayer(l);
2420 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2421 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002422 }
2423 return err;
2424}
2425
Mathias Agopian13127d82013-03-05 17:47:11 -08002426status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002427{
Mathias Agopian67106042013-03-14 19:18:13 -07002428 // called by ~LayerCleaner() when all references to the IBinder (handle)
2429 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002430 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002431 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002432 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002433 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002434 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002435 "error removing layer=%p (%s)", l.get(), strerror(-err));
2436 }
2437 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002438}
2439
Mathias Agopianb60314a2012-04-10 22:09:54 -07002440// ---------------------------------------------------------------------------
2441
Andy McFadden13a082e2012-08-24 10:16:42 -07002442void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002443 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002444 Vector<ComposerState> state;
2445 Vector<DisplayState> displays;
2446 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002447 d.what = DisplayState::eDisplayProjectionChanged |
2448 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002449 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002450 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002451 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002452 d.frame.makeInvalid();
2453 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002454 d.width = 0;
2455 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002456 displays.add(d);
2457 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002458 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002459
2460 const nsecs_t period =
2461 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2462 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002463}
2464
2465void SurfaceFlinger::initializeDisplays() {
2466 class MessageScreenInitialized : public MessageBase {
2467 SurfaceFlinger* flinger;
2468 public:
2469 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2470 virtual bool handler() {
2471 flinger->onInitializeDisplays();
2472 return true;
2473 }
2474 };
2475 sp<MessageBase> msg = new MessageScreenInitialized(this);
2476 postMessageAsync(msg); // we may be called from main thread, use async message
2477}
2478
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002479void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2480 int mode) {
2481 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2482 this);
2483 int32_t type = hw->getDisplayType();
2484 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002485
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002486 if (mode == currentMode) {
2487 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002488 return;
2489 }
2490
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002491 hw->setPowerMode(mode);
2492 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2493 ALOGW("Trying to set power mode for virtual display");
2494 return;
2495 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002496
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002497 if (currentMode == HWC_POWER_MODE_OFF) {
2498 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002499 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2500 // FIXME: eventthread only knows about the main display right now
2501 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002502 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002503 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002504
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002505 mVisibleRegionsDirty = true;
2506 repaintEverything();
2507 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002508 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002509 disableHardwareVsync(true); // also cancels any in-progress resync
2510
Mathias Agopiancde87a32012-09-13 14:09:01 -07002511 // FIXME: eventthread only knows about the main display right now
2512 mEventThread->onScreenReleased();
2513 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002514
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002515 getHwComposer().setPowerMode(type, mode);
2516 mVisibleRegionsDirty = true;
2517 // from this point on, SF will stop drawing on this display
2518 } else {
2519 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002520 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002521}
2522
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002523void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2524 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002525 SurfaceFlinger& mFlinger;
2526 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002527 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002528 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002529 MessageSetPowerMode(SurfaceFlinger& flinger,
2530 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2531 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002532 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002533 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002534 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002535 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002536 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002537 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002538 ALOGW("Attempt to set power mode = %d for virtual display",
2539 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002540 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002541 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002542 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002543 return true;
2544 }
2545 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002546 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002547 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002548}
2549
2550// ---------------------------------------------------------------------------
2551
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002552status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2553{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002554 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002555
Mathias Agopianbd115332013-04-18 16:41:04 -07002556 IPCThreadState* ipc = IPCThreadState::self();
2557 const int pid = ipc->getCallingPid();
2558 const int uid = ipc->getCallingUid();
2559 if ((uid != AID_SHELL) &&
2560 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002561 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002562 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002563 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002564 // Try to get the main lock, but don't insist if we can't
2565 // (this would indicate SF is stuck, but we want to be able to
2566 // print something in dumpsys).
2567 int retry = 3;
2568 while (mStateLock.tryLock()<0 && --retry>=0) {
2569 usleep(1000000);
2570 }
2571 const bool locked(retry >= 0);
2572 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002573 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002574 "SurfaceFlinger appears to be unresponsive, "
2575 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002576 }
2577
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002578 bool dumpAll = true;
2579 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002580 size_t numArgs = args.size();
2581 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002582 if ((index < numArgs) &&
2583 (args[index] == String16("--list"))) {
2584 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002585 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002586 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002587 }
2588
2589 if ((index < numArgs) &&
2590 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002591 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002592 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002593 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002594 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002595
2596 if ((index < numArgs) &&
2597 (args[index] == String16("--latency-clear"))) {
2598 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002599 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002600 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002601 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002602
2603 if ((index < numArgs) &&
2604 (args[index] == String16("--dispsync"))) {
2605 index++;
2606 mPrimaryDispSync.dump(result);
2607 dumpAll = false;
2608 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002609 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002610
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002611 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002612 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002613 }
2614
Mathias Agopian9795c422009-08-26 16:36:26 -07002615 if (locked) {
2616 mStateLock.unlock();
2617 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002618 }
2619 write(fd, result.string(), result.size());
2620 return NO_ERROR;
2621}
2622
Dan Stozac7014012014-02-14 15:03:43 -08002623void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2624 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002625{
2626 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2627 const size_t count = currentLayers.size();
2628 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002629 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002630 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002631 }
2632}
2633
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002634void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002635 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002636{
2637 String8 name;
2638 if (index < args.size()) {
2639 name = String8(args[index]);
2640 index++;
2641 }
2642
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002643 const nsecs_t period =
2644 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002645 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002646
2647 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002648 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002649 } else {
2650 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2651 const size_t count = currentLayers.size();
2652 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002653 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002654 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002655 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002656 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002657 }
2658 }
2659}
2660
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002661void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002662 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002663{
2664 String8 name;
2665 if (index < args.size()) {
2666 name = String8(args[index]);
2667 index++;
2668 }
2669
2670 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2671 const size_t count = currentLayers.size();
2672 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002673 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002674 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002675 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002676 }
2677 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002678
Svetoslavd85084b2014-03-20 10:28:31 -07002679 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002680}
2681
Jamie Gennis6547ff42013-07-16 20:12:42 -07002682// This should only be called from the main thread. Otherwise it would need
2683// the lock and should use mCurrentState rather than mDrawingState.
2684void SurfaceFlinger::logFrameStats() {
2685 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2686 const size_t count = drawingLayers.size();
2687 for (size_t i=0 ; i<count ; i++) {
2688 const sp<Layer>& layer(drawingLayers[i]);
2689 layer->logFrameStats();
2690 }
2691
2692 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2693}
2694
Andy McFadden4803b742012-09-24 19:07:20 -07002695/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2696{
2697 static const char* config =
2698 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002699#ifdef HAS_CONTEXT_PRIORITY
2700 " HAS_CONTEXT_PRIORITY"
2701#endif
2702#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2703 " NEVER_DEFAULT_TO_ASYNC_MODE"
2704#endif
2705#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2706 " TARGET_DISABLE_TRIPLE_BUFFERING"
2707#endif
2708 "]";
2709 result.append(config);
2710}
2711
Mathias Agopian74d211a2013-04-22 16:55:35 +02002712void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2713 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002714{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002715 bool colorize = false;
2716 if (index < args.size()
2717 && (args[index] == String16("--color"))) {
2718 colorize = true;
2719 index++;
2720 }
2721
2722 Colorizer colorizer(colorize);
2723
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002724 // figure out if we're stuck somewhere
2725 const nsecs_t now = systemTime();
2726 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2727 const nsecs_t inTransaction(mDebugInTransaction);
2728 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2729 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2730
2731 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002732 * Dump library configuration.
2733 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002734
2735 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002736 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002737 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002738 appendSfConfigString(result);
2739 appendUiConfigString(result);
2740 appendGuiConfigString(result);
2741 result.append("\n");
2742
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002743 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002744 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002745 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002746 result.append(SyncFeatures::getInstance().toString());
2747 result.append("\n");
2748
Andy McFadden41d67d72014-04-25 16:58:34 -07002749 colorizer.bold(result);
2750 result.append("DispSync configuration: ");
2751 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002752 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2753 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002754 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2755 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2756 result.append("\n");
2757
Andy McFadden4803b742012-09-24 19:07:20 -07002758 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002759 * Dump the visible layer list
2760 */
2761 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2762 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002763 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002764 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002765 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002766 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002767 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002768 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002769 }
2770
2771 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002772 * Dump Display state
2773 */
2774
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002775 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002776 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002777 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002778 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2779 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002780 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002781 }
2782
2783 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002784 * Dump SurfaceFlinger global state
2785 */
2786
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002787 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002788 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002789 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002790
Mathias Agopian888c8222012-08-04 21:10:38 -07002791 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002792 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002793
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002794 colorizer.bold(result);
2795 result.appendFormat("EGL implementation : %s\n",
2796 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2797 colorizer.reset(result);
2798 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002799 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002800
Mathias Agopian875d8e12013-06-07 15:35:48 -07002801 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002802
Mathias Agopian42977342012-08-05 00:40:46 -07002803 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002804 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2805 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002806 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002807 " last eglSwapBuffers() time: %f us\n"
2808 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002809 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002810 " refresh-rate : %f fps\n"
2811 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002812 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002813 " gpu_to_cpu_unsupported : %d\n"
2814 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002815 mLastSwapBufferTime/1000.0,
2816 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002817 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002818 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2819 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002820 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002821 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002822
Mathias Agopian74d211a2013-04-22 16:55:35 +02002823 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002824 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002825
Mathias Agopian74d211a2013-04-22 16:55:35 +02002826 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002827 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002828
2829 /*
2830 * VSYNC state
2831 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002832 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002833
2834 /*
2835 * Dump HWComposer state
2836 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002837 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002838 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002839 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002840 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002841 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002842 (mDebugDisableHWC || mDebugRegion || mDaltonize
2843 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002844 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002845
2846 /*
2847 * Dump gralloc state
2848 */
2849 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2850 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002851}
2852
Mathias Agopian13127d82013-03-05 17:47:11 -08002853const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002854SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002855 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002856 wp<IBinder> dpy;
2857 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2858 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2859 dpy = mDisplays.keyAt(i);
2860 break;
2861 }
2862 }
2863 if (dpy == NULL) {
2864 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2865 // Just use the primary display so we have something to return
2866 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2867 }
2868 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002869}
2870
Keun young Park63f165f2012-08-31 10:53:36 -07002871bool SurfaceFlinger::startDdmConnection()
2872{
2873 void* libddmconnection_dso =
2874 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2875 if (!libddmconnection_dso) {
2876 return false;
2877 }
2878 void (*DdmConnection_start)(const char* name);
2879 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07002880 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07002881 if (!DdmConnection_start) {
2882 dlclose(libddmconnection_dso);
2883 return false;
2884 }
2885 (*DdmConnection_start)(getServiceName());
2886 return true;
2887}
2888
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002889status_t SurfaceFlinger::onTransact(
2890 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2891{
2892 switch (code) {
2893 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002894 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002895 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002896 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002897 case CLEAR_ANIMATION_FRAME_STATS:
2898 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002899 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002900 {
2901 // codes that require permission check
2902 IPCThreadState* ipc = IPCThreadState::self();
2903 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002904 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002905 if ((uid != AID_GRAPHICS) &&
2906 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002907 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002908 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2909 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002910 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002911 break;
2912 }
2913 case CAPTURE_SCREEN:
2914 {
2915 // codes that require permission check
2916 IPCThreadState* ipc = IPCThreadState::self();
2917 const int pid = ipc->getCallingPid();
2918 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002919 if ((uid != AID_GRAPHICS) &&
2920 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002921 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002922 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2923 return PERMISSION_DENIED;
2924 }
2925 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002926 }
2927 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002928
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002929 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2930 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002931 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002932 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002933 IPCThreadState* ipc = IPCThreadState::self();
2934 const int pid = ipc->getCallingPid();
2935 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002936 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002937 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002938 return PERMISSION_DENIED;
2939 }
2940 int n;
2941 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002942 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002943 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002944 return NO_ERROR;
2945 case 1002: // SHOW_UPDATES
2946 n = data.readInt32();
2947 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002948 invalidateHwcGeometry();
2949 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002950 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002951 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002952 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002953 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002954 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002955 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002956 setTransactionFlags(
2957 eTransactionNeeded|
2958 eDisplayTransactionNeeded|
2959 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002960 return NO_ERROR;
2961 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002962 case 1006:{ // send empty update
2963 signalRefresh();
2964 return NO_ERROR;
2965 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002966 case 1008: // toggle use of hw composer
2967 n = data.readInt32();
2968 mDebugDisableHWC = n ? 1 : 0;
2969 invalidateHwcGeometry();
2970 repaintEverything();
2971 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002972 case 1009: // toggle use of transform hint
2973 n = data.readInt32();
2974 mDebugDisableTransformHint = n ? 1 : 0;
2975 invalidateHwcGeometry();
2976 repaintEverything();
2977 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002978 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002979 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002980 reply->writeInt32(0);
2981 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002982 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002983 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002984 return NO_ERROR;
2985 case 1013: {
2986 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002987 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2988 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002989 return NO_ERROR;
2990 }
2991 case 1014: {
2992 // daltonize
2993 n = data.readInt32();
2994 switch (n % 10) {
2995 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2996 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2997 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2998 }
2999 if (n >= 10) {
3000 mDaltonizer.setMode(Daltonizer::correction);
3001 } else {
3002 mDaltonizer.setMode(Daltonizer::simulation);
3003 }
3004 mDaltonize = n > 0;
3005 invalidateHwcGeometry();
3006 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07003007 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003008 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003009 case 1015: {
3010 // apply a color matrix
3011 n = data.readInt32();
3012 mHasColorMatrix = n ? 1 : 0;
3013 if (n) {
3014 // color matrix is sent as mat3 matrix followed by vec3
3015 // offset, then packed into a mat4 where the last row is
3016 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07003017 for (size_t i = 0 ; i < 4; i++) {
3018 for (size_t j = 0; j < 4; j++) {
3019 mColorMatrix[i][j] = data.readFloat();
3020 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003021 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003022 } else {
3023 mColorMatrix = mat4();
3024 }
3025 invalidateHwcGeometry();
3026 repaintEverything();
3027 return NO_ERROR;
3028 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003029 // This is an experimental interface
3030 // Needs to be shifted to proper binder interface when we productize
3031 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07003032 n = data.readInt32();
3033 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003034 return NO_ERROR;
3035 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003036 }
3037 }
3038 return err;
3039}
3040
Mathias Agopian53331da2011-08-22 21:44:41 -07003041void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07003042 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08003043 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07003044}
3045
Mathias Agopian59119e62010-10-11 12:37:43 -07003046// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003047// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07003048// ---------------------------------------------------------------------------
3049
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003050/* The code below is here to handle b/8734824
3051 *
3052 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07003053 * from the surfaceflinger thread to the calling binder thread, where they
3054 * are executed. This allows the calling thread in the calling process to be
3055 * reused and not depend on having "enough" binder threads to handle the
3056 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003057 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003058class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07003059 /* Parts of GraphicProducerWrapper are run on two different threads,
3060 * communicating by sending messages via Looper but also by shared member
3061 * data. Coherence maintenance is subtle and in places implicit (ugh).
3062 *
3063 * Don't rely on Looper's sendMessage/handleMessage providing
3064 * release/acquire semantics for any data not actually in the Message.
3065 * Data going from surfaceflinger to binder threads needs to be
3066 * synchronized explicitly.
3067 *
3068 * Barrier open/wait do provide release/acquire semantics. This provides
3069 * implicit synchronization for data coming back from binder to
3070 * surfaceflinger threads.
3071 */
3072
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003073 sp<IGraphicBufferProducer> impl;
3074 sp<Looper> looper;
3075 status_t result;
3076 bool exitPending;
3077 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07003078 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003079 uint32_t code;
3080 Parcel const* data;
3081 Parcel* reply;
3082
3083 enum {
3084 MSG_API_CALL,
3085 MSG_EXIT
3086 };
3087
3088 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003089 * Called on surfaceflinger thread. This is called by our "fake"
3090 * BpGraphicBufferProducer. We package the data and reply Parcel and
3091 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003092 */
3093 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08003094 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003095 this->code = code;
3096 this->data = &data;
3097 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003098 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07003099 // if we've exited, we run the message synchronously right here.
3100 // note (JH): as far as I can tell from looking at the code, this
3101 // never actually happens. if it does, i'm not sure if it happens
3102 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003103 handleMessage(Message(MSG_API_CALL));
3104 } else {
3105 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07003106 // Prevent stores to this->{code, data, reply} from being
3107 // reordered later than the construction of Message.
3108 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003109 looper->sendMessage(this, Message(MSG_API_CALL));
3110 barrier.wait();
3111 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08003112 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003113 }
3114
3115 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003116 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003117 * call the real BpGraphicBufferProducer.
3118 */
3119 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07003120 int what = message.what;
3121 // Prevent reads below from happening before the read from Message
3122 atomic_thread_fence(memory_order_acquire);
3123 if (what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08003124 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003125 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07003126 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003127 exitRequested = true;
3128 }
3129 }
3130
3131public:
Jesse Hallb154c422014-07-13 12:47:02 -07003132 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
3133 : impl(impl),
3134 looper(new Looper(true)),
3135 exitPending(false),
3136 exitRequested(false)
3137 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003138
Jesse Hallb154c422014-07-13 12:47:02 -07003139 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003140 status_t waitForResponse() {
3141 do {
3142 looper->pollOnce(-1);
3143 } while (!exitRequested);
3144 return result;
3145 }
3146
Jesse Hallb154c422014-07-13 12:47:02 -07003147 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003148 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07003149 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003150 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07003151 // Ensure this->result is visible to the binder thread before it
3152 // handles the message.
3153 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003154 looper->sendMessage(this, Message(MSG_EXIT));
3155 }
3156};
3157
3158
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003159status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
3160 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003161 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003162 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003163 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003164
3165 if (CC_UNLIKELY(display == 0))
3166 return BAD_VALUE;
3167
3168 if (CC_UNLIKELY(producer == 0))
3169 return BAD_VALUE;
3170
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003171 // if we have secure windows on this display, never allow the screen capture
3172 // unless the producer interface is local (i.e.: we can take a screenshot for
3173 // ourselves).
3174 if (!producer->asBinder()->localBinder()) {
3175 Mutex::Autolock _l(mStateLock);
3176 sp<const DisplayDevice> hw(getDisplayDevice(display));
3177 if (hw->getSecureLayerVisible()) {
3178 ALOGW("FB is protected: PERMISSION_DENIED");
3179 return PERMISSION_DENIED;
3180 }
3181 }
3182
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003183 // Convert to surfaceflinger's internal rotation type.
3184 Transform::orientation_flags rotationFlags;
3185 switch (rotation) {
3186 case ISurfaceComposer::eRotateNone:
3187 rotationFlags = Transform::ROT_0;
3188 break;
3189 case ISurfaceComposer::eRotate90:
3190 rotationFlags = Transform::ROT_90;
3191 break;
3192 case ISurfaceComposer::eRotate180:
3193 rotationFlags = Transform::ROT_180;
3194 break;
3195 case ISurfaceComposer::eRotate270:
3196 rotationFlags = Transform::ROT_270;
3197 break;
3198 default:
3199 rotationFlags = Transform::ROT_0;
3200 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3201 break;
3202 }
3203
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003204 class MessageCaptureScreen : public MessageBase {
3205 SurfaceFlinger* flinger;
3206 sp<IBinder> display;
3207 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003208 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003209 uint32_t reqWidth, reqHeight;
3210 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003211 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003212 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003213 status_t result;
3214 public:
3215 MessageCaptureScreen(SurfaceFlinger* flinger,
3216 const sp<IBinder>& display,
3217 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003218 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003219 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003220 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003221 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003222 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003223 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003224 useIdentityTransform(useIdentityTransform),
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003225 rotation(rotation),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003226 result(PERMISSION_DENIED)
3227 {
3228 }
3229 status_t getResult() const {
3230 return result;
3231 }
3232 virtual bool handler() {
3233 Mutex::Autolock _l(flinger->mStateLock);
3234 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003235 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003236 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003237 useIdentityTransform, rotation);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003238 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003239 return true;
3240 }
3241 };
3242
Mathias Agopian9eb1f052013-04-10 16:27:17 -07003243 // make sure to process transactions before screenshots -- a transaction
3244 // might already be pending but scheduled for VSYNC; this guarantees we
3245 // will handle it before the screenshot. When VSYNC finally arrives
3246 // the scheduled transaction will be a no-op. If no transactions are
3247 // scheduled at this time, this will end-up being a no-op as well.
3248 mEventQueue.invalidateTransactionNow();
3249
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003250 // this creates a "fake" BBinder which will serve as a "fake" remote
3251 // binder to receive the marshaled calls and forward them to the
3252 // real remote (a BpGraphicBufferProducer)
3253 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3254
3255 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3256 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003257 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003258 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003259 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003260 useIdentityTransform, rotationFlags);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003261
3262 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003263 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003264 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003265 }
3266 return res;
3267}
3268
Mathias Agopian180f10d2013-04-10 22:55:41 -07003269
3270void SurfaceFlinger::renderScreenImplLocked(
3271 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003272 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003273 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003274 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003275{
3276 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003277 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003278
3279 // get screen geometry
3280 const uint32_t hw_w = hw->getWidth();
3281 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003282 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
3283
Dan Stozac1879002014-05-22 15:59:05 -07003284 // if a default or invalid sourceCrop is passed in, set reasonable values
3285 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3286 !sourceCrop.isValid()) {
3287 sourceCrop.setLeftTop(Point(0, 0));
3288 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3289 }
3290
3291 // ensure that sourceCrop is inside screen
3292 if (sourceCrop.left < 0) {
3293 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3294 }
Dan Stozabe31f442014-06-11 11:20:54 -07003295 if (sourceCrop.right > hw_w) {
3296 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003297 }
3298 if (sourceCrop.top < 0) {
3299 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3300 }
Dan Stozabe31f442014-06-11 11:20:54 -07003301 if (sourceCrop.bottom > hw_h) {
3302 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003303 }
3304
Mathias Agopian180f10d2013-04-10 22:55:41 -07003305 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003306 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003307
3308 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003309 engine.setViewportAndProjection(
3310 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003311 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003312
3313 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003314 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003315
3316 const LayerVector& layers( mDrawingState.layersSortedByZ );
3317 const size_t count = layers.size();
3318 for (size_t i=0 ; i<count ; ++i) {
3319 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003320 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003321 if (state.layerStack == hw->getLayerStack()) {
3322 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3323 if (layer->isVisible()) {
3324 if (filtering) layer->setFiltering(true);
Ramakant Singhf9af0e32014-01-07 14:17:52 +05303325 if(!layer->isProtected())
3326 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003327 if (filtering) layer->setFiltering(false);
3328 }
3329 }
3330 }
3331 }
3332
3333 // compositionComplete is needed for older driver
3334 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003335 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003336}
3337
3338
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003339status_t SurfaceFlinger::captureScreenImplLocked(
3340 const sp<const DisplayDevice>& hw,
3341 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003342 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003343 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003344 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003345{
3346 ATRACE_CALL();
3347
Mathias Agopian180f10d2013-04-10 22:55:41 -07003348 // get screen geometry
3349 const uint32_t hw_w = hw->getWidth();
3350 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003351
Mathias Agopian180f10d2013-04-10 22:55:41 -07003352 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3353 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3354 reqWidth, reqHeight, hw_w, hw_h);
3355 return BAD_VALUE;
3356 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003357
Mathias Agopian180f10d2013-04-10 22:55:41 -07003358 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3359 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3360
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003361 // create a surface (because we're a producer, and we need to
3362 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003363 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003364 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003365
3366 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003367 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003368 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3369 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003370
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003371 int err = 0;
3372 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003373 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003374 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3375 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003376
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003377 if (err == NO_ERROR) {
3378 ANativeWindowBuffer* buffer;
3379 /* TODO: Once we have the sync framework everywhere this can use
3380 * server-side waits on the fence that dequeueBuffer returns.
3381 */
3382 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3383 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003384 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003385 // create an EGLImage from the buffer so we can later
3386 // turn it into a texture
3387 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3388 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3389 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003390 // this binds the given EGLImage as a framebuffer for the
3391 // duration of this scope.
3392 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3393 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003394 // this will in fact render into our dequeued buffer
3395 // via an FBO, which means we didn't have to create
3396 // an EGLSurface and therefore we're not
3397 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003398 renderScreenImplLocked(
3399 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3400 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003401
Riley Andrews86639902014-08-15 12:27:24 -07003402 // Attempt to create a sync khr object that can produce a sync point. If that
3403 // isn't available, create a non-dupable sync object in the fallback path and
3404 // wait on it directly.
3405 EGLSyncKHR sync;
3406 if (!DEBUG_SCREENSHOTS) {
3407 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Andy McFadden2d8d1202013-10-09 16:38:02 -07003408 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003409 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003410 }
Riley Andrews86639902014-08-15 12:27:24 -07003411 if (sync != EGL_NO_SYNC_KHR) {
3412 // get the sync fd
3413 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3414 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3415 ALOGW("captureScreen: failed to dup sync khr object");
3416 syncFd = -1;
3417 }
3418 eglDestroySyncKHR(mEGLDisplay, sync);
3419 } else {
3420 // fallback path
3421 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3422 if (sync != EGL_NO_SYNC_KHR) {
3423 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3424 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3425 EGLint eglErr = eglGetError();
3426 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3427 ALOGW("captureScreen: fence wait timed out");
3428 } else {
3429 ALOGW_IF(eglErr != EGL_SUCCESS,
3430 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3431 }
3432 eglDestroySyncKHR(mEGLDisplay, sync);
3433 } else {
3434 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3435 }
3436 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003437 if (DEBUG_SCREENSHOTS) {
3438 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3439 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3440 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3441 hw, minLayerZ, maxLayerZ);
3442 delete [] pixels;
3443 }
3444
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003445 } else {
3446 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3447 result = INVALID_OPERATION;
3448 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003449 // destroy our image
3450 eglDestroyImageKHR(mEGLDisplay, image);
3451 } else {
3452 result = BAD_VALUE;
3453 }
Jesse Hallafe2b1f2014-10-21 11:09:17 -07003454 // queueBuffer takes ownership of syncFd
Riley Andrews86639902014-08-15 12:27:24 -07003455 window->queueBuffer(window, buffer, syncFd);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003456 }
3457 } else {
3458 result = BAD_VALUE;
3459 }
3460 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3461 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003462
Mathias Agopian74c40c02010-09-29 13:02:36 -07003463 return result;
3464}
3465
Mathias Agopiand5556842013-09-19 17:08:37 -07003466void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3467 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003468 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003469 for (size_t y=0 ; y<h ; y++) {
3470 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3471 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003472 if (p[x] != 0xFF000000) return;
3473 }
3474 }
3475 ALOGE("*** we just took a black screenshot ***\n"
3476 "requested minz=%d, maxz=%d, layerStack=%d",
3477 minLayerZ, maxLayerZ, hw->getLayerStack());
3478 const LayerVector& layers( mDrawingState.layersSortedByZ );
3479 const size_t count = layers.size();
3480 for (size_t i=0 ; i<count ; ++i) {
3481 const sp<Layer>& layer(layers[i]);
3482 const Layer::State& state(layer->getDrawingState());
3483 const bool visible = (state.layerStack == hw->getLayerStack())
3484 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3485 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003486 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003487 visible ? '+' : '-',
3488 i, layer->getName().string(), state.layerStack, state.z,
3489 layer->isVisible(), state.flags, state.alpha);
3490 }
3491 }
3492}
3493
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003494// ---------------------------------------------------------------------------
3495
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003496SurfaceFlinger::LayerVector::LayerVector() {
3497}
3498
3499SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003500 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003501}
3502
3503int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3504 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003505{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003506 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003507 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3508 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003509
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003510 uint32_t ls = l->getCurrentState().layerStack;
3511 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003512 if (ls != rs)
3513 return ls - rs;
3514
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003515 uint32_t lz = l->getCurrentState().z;
3516 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003517 if (lz != rz)
3518 return lz - rz;
3519
3520 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003521}
3522
3523// ---------------------------------------------------------------------------
3524
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003525SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Michael Lentine47e45402014-07-18 15:34:25 -07003526 : type(DisplayDevice::DISPLAY_ID_INVALID), width(0), height(0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003527}
3528
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003529SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Michael Lentine47e45402014-07-18 15:34:25 -07003530 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0), width(0), height(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003531 viewport.makeInvalid();
3532 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003533}
3534
3535// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003536
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003537}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003538
3539
3540#if defined(__gl_h_)
3541#error "don't include gl/gl.h in this file"
3542#endif
3543
3544#if defined(__gl2_h_)
3545#error "don't include gl2/gl2.h in this file"
3546#endif