blob: 2e6d2b6cfbd43ec8472e8c4048495e5e95b10abb [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>
Mathias Agopian875d8e12013-06-07 15:35:48 -070080
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081#define DISPLAY_COUNT 1
82
Mathias Agopianfee2b462013-07-03 12:34:01 -070083/*
84 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
85 * black pixels.
86 */
87#define DEBUG_SCREENSHOTS false
88
Mathias Agopianca088332013-03-28 17:44:13 -070089EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
90
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070092
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070093// This is the phase offset in nanoseconds of the software vsync event
94// relative to the vsync event reported by HWComposer. The software vsync
95// event is when SurfaceFlinger and Choreographer-based applications run each
96// frame.
97//
98// This phase offset allows adjustment of the minimum latency from application
99// wake-up (by Choregographer) time to the time at which the resulting window
100// image is displayed. This value may be either positive (after the HW vsync)
101// or negative (before the HW vsync). Setting it to 0 will result in a
102// minimum latency of two vsync periods because the app and SurfaceFlinger
103// will run just after the HW vsync. Setting it to a positive number will
104// result in the minimum latency being:
105//
106// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
107//
108// Note that reducing this latency makes it more likely for the applications
109// to not have their window content image ready in time. When this happens
110// the latency will end up being an additional vsync period, and animations
111// will hiccup. Therefore, this latency should be tuned somewhat
112// conservatively (or at least with awareness of the trade-off being made).
113static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
114
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700115// This is the phase offset at which SurfaceFlinger's composition runs.
116static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
117
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118// ---------------------------------------------------------------------------
119
Mathias Agopian99b49842011-06-27 16:05:52 -0700120const String16 sHardwareTest("android.permission.HARDWARE_TEST");
121const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
122const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
123const String16 sDump("android.permission.DUMP");
124
125// ---------------------------------------------------------------------------
126
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700128 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700130 mTransactionPending(false),
131 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700132 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700133 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700134 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700137 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800138 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700140 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700141 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700142 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700143 mDebugInSwapBuffers(0),
144 mLastSwapBufferTime(0),
145 mDebugInTransaction(0),
146 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700147 mBootFinished(false),
Dan Stozaee44edd2015-03-23 15:50:23 -0700148 mForceFullDamage(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700149 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700150 mHWVsyncAvailable(false),
Alan Viverette9c5a3332013-09-12 20:04:35 -0700151 mDaltonize(false),
152 mHasColorMatrix(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153{
Steve Blocka19954a2012-01-04 20:05:49 +0000154 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155
156 // debugging stuff...
157 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700158
Mathias Agopianb4b17302013-03-20 18:36:41 -0700159 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700160 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700161
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 property_get("debug.sf.showupdates", value, "0");
163 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700164
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700165 property_get("debug.sf.ddms", value, "0");
166 mDebugDDMS = atoi(value);
167 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700168 if (!startDdmConnection()) {
169 // start failed, and DDMS debugging not enabled
170 mDebugDDMS = 0;
171 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700172 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700173 ALOGI_IF(mDebugRegion, "showupdates enabled");
174 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175}
176
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800177void SurfaceFlinger::onFirstRef()
178{
179 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800180}
181
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182SurfaceFlinger::~SurfaceFlinger()
183{
Mathias Agopiana4912602012-07-12 14:25:33 -0700184 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
185 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
186 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187}
188
Dan Stozac7014012014-02-14 15:03:43 -0800189void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800190{
191 // the window manager died on us. prepare its eulogy.
192
Andy McFadden13a082e2012-08-24 10:16:42 -0700193 // restore initial conditions (default device unblank, etc)
194 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800195
196 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700197 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800198}
199
Mathias Agopian7e27f052010-05-28 14:22:23 -0700200sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201{
Mathias Agopian96f08192010-06-02 23:28:45 -0700202 sp<ISurfaceComposerClient> bclient;
203 sp<Client> client(new Client(this));
204 status_t err = client->initCheck();
205 if (err == NO_ERROR) {
206 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 return bclient;
209}
210
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700211sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
212 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700213{
214 class DisplayToken : public BBinder {
215 sp<SurfaceFlinger> flinger;
216 virtual ~DisplayToken() {
217 // no more references, this display must be terminated
218 Mutex::Autolock _l(flinger->mStateLock);
219 flinger->mCurrentState.displays.removeItem(this);
220 flinger->setTransactionFlags(eDisplayTransactionNeeded);
221 }
222 public:
223 DisplayToken(const sp<SurfaceFlinger>& flinger)
224 : flinger(flinger) {
225 }
226 };
227
228 sp<BBinder> token = new DisplayToken(this);
229
230 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700231 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700232 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700233 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700234 mCurrentState.displays.add(token, info);
235
236 return token;
237}
238
Jesse Hall6c913be2013-08-08 12:15:49 -0700239void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
240 Mutex::Autolock _l(mStateLock);
241
242 ssize_t idx = mCurrentState.displays.indexOfKey(display);
243 if (idx < 0) {
244 ALOGW("destroyDisplay: invalid display token");
245 return;
246 }
247
248 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
249 if (!info.isVirtualDisplay()) {
250 ALOGE("destroyDisplay called for non-virtual display");
251 return;
252 }
253
254 mCurrentState.displays.removeItemsAt(idx);
255 setTransactionFlags(eDisplayTransactionNeeded);
256}
257
Jesse Hall692c7232012-11-08 15:41:56 -0800258void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
259 ALOGW_IF(mBuiltinDisplays[type],
260 "Overwriting display token for display type %d", type);
261 mBuiltinDisplays[type] = new BBinder();
262 DisplayDeviceState info(type);
263 // All non-virtual displays are currently considered secure.
264 info.isSecure = true;
265 mCurrentState.displays.add(mBuiltinDisplays[type], info);
266}
267
Mathias Agopiane57f2922012-08-09 16:29:12 -0700268sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700269 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700270 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
271 return NULL;
272 }
Jesse Hall692c7232012-11-08 15:41:56 -0800273 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700274}
275
Jamie Gennis9a78c902011-01-12 18:30:40 -0800276sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
277{
278 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
279 return gba;
280}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700281
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282void SurfaceFlinger::bootFinished()
283{
284 const nsecs_t now = systemTime();
285 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000286 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700287 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700288
289 // wait patiently for the window manager death
290 const String16 name("window");
291 sp<IBinder> window(defaultServiceManager()->getService(name));
292 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700293 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700294 }
295
296 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700297 // formerly we would just kill the process, but we now ask it to exit so it
298 // can choose where to stop the animation.
299 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300}
301
Mathias Agopian3f844832013-08-07 21:24:32 -0700302void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700303 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700304 RenderEngine& engine;
305 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700306 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700307 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
308 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700309 }
310 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700311 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700312 return true;
313 }
314 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700315 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700316}
317
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700318class DispSyncSource : public VSyncSource, private DispSync::Callback {
319public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700320 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
321 const char* label) :
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700322 mValue(0),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700323 mTraceVsync(traceVsync),
Andy McFadden5167ec62014-05-22 13:08:43 -0700324 mVsyncOnLabel(String8::format("VsyncOn-%s", label)),
325 mVsyncEventLabel(String8::format("VSYNC-%s", label)),
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700326 mDispSync(dispSync),
327 mCallbackMutex(),
328 mCallback(),
329 mVsyncMutex(),
330 mPhaseOffset(phaseOffset),
331 mEnabled(false) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700332
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700333 virtual ~DispSyncSource() {}
334
335 virtual void setVSyncEnabled(bool enable) {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700336 Mutex::Autolock lock(mVsyncMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700337 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700338 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700339 static_cast<DispSync::Callback*>(this));
340 if (err != NO_ERROR) {
341 ALOGE("error registering vsync callback: %s (%d)",
342 strerror(-err), err);
343 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700344 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700345 } else {
346 status_t err = mDispSync->removeEventListener(
347 static_cast<DispSync::Callback*>(this));
348 if (err != NO_ERROR) {
349 ALOGE("error unregistering vsync callback: %s (%d)",
350 strerror(-err), err);
351 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700352 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700353 }
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700354 mEnabled = enable;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700355 }
356
357 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700358 Mutex::Autolock lock(mCallbackMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700359 mCallback = callback;
360 }
361
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700362 virtual void setPhaseOffset(nsecs_t phaseOffset) {
363 Mutex::Autolock lock(mVsyncMutex);
364
365 // Normalize phaseOffset to [0, period)
366 auto period = mDispSync->getPeriod();
367 phaseOffset %= period;
368 if (phaseOffset < 0) {
369 // If we're here, then phaseOffset is in (-period, 0). After this
370 // operation, it will be in (0, period)
371 phaseOffset += period;
372 }
373 mPhaseOffset = phaseOffset;
374
375 // If we're not enabled, we don't need to mess with the listeners
376 if (!mEnabled) {
377 return;
378 }
379
380 // Remove the listener with the old offset
381 status_t err = mDispSync->removeEventListener(
382 static_cast<DispSync::Callback*>(this));
383 if (err != NO_ERROR) {
384 ALOGE("error unregistering vsync callback: %s (%d)",
385 strerror(-err), err);
386 }
387
388 // Add a listener with the new offset
389 err = mDispSync->addEventListener(mPhaseOffset,
390 static_cast<DispSync::Callback*>(this));
391 if (err != NO_ERROR) {
392 ALOGE("error registering vsync callback: %s (%d)",
393 strerror(-err), err);
394 }
395 }
396
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700397private:
398 virtual void onDispSyncEvent(nsecs_t when) {
399 sp<VSyncSource::Callback> callback;
400 {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700401 Mutex::Autolock lock(mCallbackMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700402 callback = mCallback;
403
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700404 if (mTraceVsync) {
405 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700406 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700407 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700408 }
409
410 if (callback != NULL) {
411 callback->onVSyncEvent(when);
412 }
413 }
414
415 int mValue;
416
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700417 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700418 const String8 mVsyncOnLabel;
419 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700420
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700421 DispSync* mDispSync;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700422
423 Mutex mCallbackMutex; // Protects the following
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700424 sp<VSyncSource::Callback> mCallback;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700425
426 Mutex mVsyncMutex; // Protects the following
427 nsecs_t mPhaseOffset;
428 bool mEnabled;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700429};
430
431void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700432 ALOGI( "SurfaceFlinger's main thread ready to run. "
433 "Initializing graphics H/W...");
434
Jesse Hall692c7232012-11-08 15:41:56 -0800435 Mutex::Autolock _l(mStateLock);
436
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700437 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700438 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
439 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700440
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700441 // Initialize the H/W composer object. There may or may not be an
442 // actual hardware composer underneath.
443 mHwc = new HWComposer(this,
444 *static_cast<HWComposer::EventHandler *>(this));
445
Mathias Agopian875d8e12013-06-07 15:35:48 -0700446 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800447 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700448
449 // retrieve the EGL context that was selected/created
450 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700451
Mathias Agopianda27af92012-09-13 18:17:13 -0700452 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
453 "couldn't create EGLContext");
454
Mathias Agopiancde87a32012-09-13 14:09:01 -0700455 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700456 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700457 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700458 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700459 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700460 // All non-virtual displays are currently considered secure.
461 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800462 createBuiltinDisplayLocked(type);
463 wp<IBinder> token = mBuiltinDisplays[i];
464
Dan Stozab9b08832014-03-13 11:55:57 -0700465 sp<IGraphicBufferProducer> producer;
466 sp<IGraphicBufferConsumer> consumer;
467 BufferQueue::createBufferQueue(&producer, &consumer,
468 new GraphicBufferAlloc());
469
470 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
471 consumer);
Jesse Hall19e87292013-12-23 21:02:15 -0800472 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700473 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800474 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Dan Stozab9b08832014-03-13 11:55:57 -0700475 fbs, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -0800476 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700477 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700478 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700479 // for displays other than the main display, so we always
480 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800481 ALOGD("marking display %zu as acquired/unblanked", i);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700482 hw->setPowerMode(HWC_POWER_MODE_NORMAL);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700483 }
484 mDisplays.add(token, hw);
485 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700486 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700487
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700488 // make the GLContext current so that we can create textures when creating Layers
489 // (which may happens before we render something)
490 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
491
Mathias Agopian028508c2012-07-25 21:12:12 -0700492 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700493 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700494 vsyncPhaseOffsetNs, true, "app");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700495 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700496 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700497 sfVsyncPhaseOffsetNs, true, "sf");
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700498 mSFEventThread = new EventThread(sfVsyncSrc);
499 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700500
Jamie Gennisd1700752013-10-14 12:22:52 -0700501 mEventControlThread = new EventControlThread(this);
502 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
503
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700504 // set a fake vsync period if there is no HWComposer
505 if (mHwc->initCheck() != NO_ERROR) {
506 mPrimaryDispSync.setPeriod(16666667);
507 }
508
Mathias Agopian92a979a2012-08-02 18:32:23 -0700509 // initialize our drawing state
510 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700511
Andy McFadden13a082e2012-08-24 10:16:42 -0700512 // set initial conditions (e.g. unblank default device)
513 initializeDisplays();
514
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700515 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700516 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800517}
518
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700519int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700520 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700521 type : mHwc->allocateDisplayId();
522}
523
Mathias Agopiana67e4182012-06-19 17:26:12 -0700524void SurfaceFlinger::startBootAnim() {
525 // start boot animation
526 property_set("service.bootanim.exit", "0");
527 property_set("ctl.start", "bootanim");
528}
529
Mathias Agopian875d8e12013-06-07 15:35:48 -0700530size_t SurfaceFlinger::getMaxTextureSize() const {
531 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700532}
533
Mathias Agopian875d8e12013-06-07 15:35:48 -0700534size_t SurfaceFlinger::getMaxViewportDims() const {
535 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700536}
537
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800538// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800539
Jamie Gennis582270d2011-08-17 18:19:00 -0700540bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800541 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800542 Mutex::Autolock _l(mStateLock);
Marco Nelissen097ca272014-11-14 08:01:01 -0800543 sp<IBinder> surfaceTextureBinder(IInterface::asBinder(bufferProducer));
Mathias Agopian67106042013-03-14 19:18:13 -0700544 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800545}
546
Dan Stoza7f7da322014-05-02 15:26:25 -0700547status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
548 Vector<DisplayInfo>* configs) {
549 if (configs == NULL) {
550 return BAD_VALUE;
551 }
552
Naseer Ahmed7aa0c472014-11-03 14:49:23 -0500553 if (!display.get())
554 return NAME_NOT_FOUND;
555
Jesse Hall692c7232012-11-08 15:41:56 -0800556 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700557 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800558 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700559 type = i;
560 break;
561 }
562 }
563
564 if (type < 0) {
565 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700566 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700567
Mathias Agopian8b736f12012-08-13 17:54:26 -0700568 // TODO: Not sure if display density should handled by SF any longer
569 class Density {
570 static int getDensityFromProperty(char const* propName) {
571 char property[PROPERTY_VALUE_MAX];
572 int density = 0;
573 if (property_get(propName, property, NULL) > 0) {
574 density = atoi(property);
575 }
576 return density;
577 }
578 public:
579 static int getEmuDensity() {
580 return getDensityFromProperty("qemu.sf.lcd_density"); }
581 static int getBuildDensity() {
582 return getDensityFromProperty("ro.sf.lcd_density"); }
583 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700584
Dan Stoza7f7da322014-05-02 15:26:25 -0700585 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700586
Dan Stoza7f7da322014-05-02 15:26:25 -0700587 const Vector<HWComposer::DisplayConfig>& hwConfigs =
588 getHwComposer().getConfigs(type);
589 for (size_t c = 0; c < hwConfigs.size(); ++c) {
590 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
591 DisplayInfo info = DisplayInfo();
592
593 float xdpi = hwConfig.xdpi;
594 float ydpi = hwConfig.ydpi;
595
596 if (type == DisplayDevice::DISPLAY_PRIMARY) {
597 // The density of the device is provided by a build property
598 float density = Density::getBuildDensity() / 160.0f;
599 if (density == 0) {
600 // the build doesn't provide a density -- this is wrong!
601 // use xdpi instead
602 ALOGE("ro.sf.lcd_density must be defined as a build property");
603 density = xdpi / 160.0f;
604 }
605 if (Density::getEmuDensity()) {
606 // if "qemu.sf.lcd_density" is specified, it overrides everything
607 xdpi = ydpi = density = Density::getEmuDensity();
608 density /= 160.0f;
609 }
610 info.density = density;
611
612 // TODO: this needs to go away (currently needed only by webkit)
613 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
614 info.orientation = hw->getOrientation();
615 } else {
616 // TODO: where should this value come from?
617 static const int TV_DENSITY = 213;
618 info.density = TV_DENSITY / 160.0f;
619 info.orientation = 0;
620 }
621
622 info.w = hwConfig.width;
623 info.h = hwConfig.height;
624 info.xdpi = xdpi;
625 info.ydpi = ydpi;
626 info.fps = float(1e9 / hwConfig.refresh);
Andy McFadden91b2ca82014-06-13 14:04:23 -0700627 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
628
629 // This is how far in advance a buffer must be queued for
630 // presentation at a given time. If you want a buffer to appear
631 // on the screen at time N, you must submit the buffer before
632 // (N - presentationDeadline).
633 //
634 // Normally it's one full refresh period (to give SF a chance to
635 // latch the buffer), but this can be reduced by configuring a
636 // DispSync offset. Any additional delays introduced by the hardware
637 // composer or panel must be accounted for here.
638 //
639 // We add an additional 1ms to allow for processing time and
640 // differences between the ideal and actual refresh rate.
641 info.presentationDeadline =
642 hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700643
644 // All non-virtual displays are currently considered secure.
645 info.secure = true;
646
647 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700648 }
649
Dan Stoza7f7da322014-05-02 15:26:25 -0700650 return NO_ERROR;
651}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700652
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800653status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& /* display */,
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700654 DisplayStatInfo* stats) {
655 if (stats == NULL) {
656 return BAD_VALUE;
657 }
658
659 // FIXME for now we always return stats for the primary display
660 memset(stats, 0, sizeof(*stats));
661 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
662 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
663 return NO_ERROR;
664}
665
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700666int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
Dan Stoza24a42e92015-03-09 10:04:11 -0700667 sp<DisplayDevice> device(getDisplayDevice(display));
668 if (device != NULL) {
669 return device->getActiveConfig();
670 }
671 return BAD_VALUE;
Dan Stoza7f7da322014-05-02 15:26:25 -0700672}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700673
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700674void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
675 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
676 this);
677 int32_t type = hw->getDisplayType();
678 int currentMode = hw->getActiveConfig();
679
680 if (mode == currentMode) {
681 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
682 return;
683 }
684
685 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
686 ALOGW("Trying to set config for virtual display");
687 return;
688 }
689
690 hw->setActiveConfig(mode);
691 getHwComposer().setActiveConfig(type, mode);
692}
693
694status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
695 class MessageSetActiveConfig: public MessageBase {
696 SurfaceFlinger& mFlinger;
697 sp<IBinder> mDisplay;
698 int mMode;
699 public:
700 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
701 int mode) :
702 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
703 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700704 Vector<DisplayInfo> configs;
705 mFlinger.getDisplayConfigs(mDisplay, &configs);
Jesse Hall78442112014-08-07 22:43:06 -0700706 if (mMode < 0 || mMode >= static_cast<int>(configs.size())) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700707 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700708 mMode, configs.size());
709 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700710 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
711 if (hw == NULL) {
712 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700713 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700714 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
715 ALOGW("Attempt to set active config = %d for virtual display",
716 mMode);
717 } else {
718 mFlinger.setActiveConfigInternal(hw, mMode);
719 }
720 return true;
721 }
722 };
723 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
724 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700725 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700726}
727
Svetoslavd85084b2014-03-20 10:28:31 -0700728status_t SurfaceFlinger::clearAnimationFrameStats() {
729 Mutex::Autolock _l(mStateLock);
730 mAnimFrameTracker.clearStats();
731 return NO_ERROR;
732}
733
734status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
735 Mutex::Autolock _l(mStateLock);
736 mAnimFrameTracker.getStats(outStats);
737 return NO_ERROR;
738}
739
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800740// ----------------------------------------------------------------------------
741
742sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800743 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700744}
745
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800746// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800747
748void SurfaceFlinger::waitForEvent() {
749 mEventQueue.waitMessage();
750}
751
752void SurfaceFlinger::signalTransaction() {
753 mEventQueue.invalidate();
754}
755
756void SurfaceFlinger::signalLayerUpdate() {
757 mEventQueue.invalidate();
758}
759
760void SurfaceFlinger::signalRefresh() {
761 mEventQueue.refresh();
762}
763
764status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800765 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800766 return mEventQueue.postMessage(msg, reltime);
767}
768
769status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800770 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800771 status_t res = mEventQueue.postMessage(msg, reltime);
772 if (res == NO_ERROR) {
773 msg->wait();
774 }
775 return res;
776}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800777
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700778void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700779 do {
780 waitForEvent();
781 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800782}
783
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700784void SurfaceFlinger::enableHardwareVsync() {
785 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700786 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700787 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700788 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
789 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700790 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700791 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700792}
793
Jesse Hall948fe0c2013-10-14 12:56:09 -0700794void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700795 Mutex::Autolock _l(mHWVsyncLock);
796
Jesse Hall948fe0c2013-10-14 12:56:09 -0700797 if (makeAvailable) {
798 mHWVsyncAvailable = true;
799 } else if (!mHWVsyncAvailable) {
800 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
801 return;
802 }
803
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700804 const nsecs_t period =
805 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
806
807 mPrimaryDispSync.reset();
808 mPrimaryDispSync.setPeriod(period);
809
810 if (!mPrimaryHWVsyncEnabled) {
811 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700812 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
813 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700814 mPrimaryHWVsyncEnabled = true;
815 }
816}
817
Jesse Hall948fe0c2013-10-14 12:56:09 -0700818void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700819 Mutex::Autolock _l(mHWVsyncLock);
820 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700821 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
822 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700823 mPrimaryDispSync.endResync();
824 mPrimaryHWVsyncEnabled = false;
825 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700826 if (makeUnavailable) {
827 mHWVsyncAvailable = false;
828 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700829}
830
831void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700832 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700833
Jamie Gennisd1700752013-10-14 12:22:52 -0700834 { // Scope for the lock
835 Mutex::Autolock _l(mHWVsyncLock);
836 if (type == 0 && mPrimaryHWVsyncEnabled) {
837 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700838 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700839 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700840
841 if (needsHwVsync) {
842 enableHardwareVsync();
843 } else {
844 disableHardwareVsync(false);
845 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700846}
847
848void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
849 if (mEventThread == NULL) {
850 // This is a temporary workaround for b/7145521. A non-null pointer
851 // does not mean EventThread has finished initializing, so this
852 // is not a correct fix.
853 ALOGW("WARNING: EventThread not started, ignoring hotplug");
854 return;
855 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700856
Jesse Hall9e663de2013-08-16 14:28:37 -0700857 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700858 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800859 if (connected) {
860 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700861 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800862 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
863 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700864 }
865 setTransactionFlags(eDisplayTransactionNeeded);
866
Andy McFadden9e9689c2012-10-10 18:17:51 -0700867 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700868 }
Mathias Agopian86303202012-07-24 22:46:10 -0700869}
870
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700871void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700872 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700873 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700874}
875
Mathias Agopian4fec8732012-06-29 14:12:52 -0700876void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800877 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800878 switch (what) {
Dan Stoza6b9454d2014-11-07 16:00:59 -0800879 case MessageQueue::TRANSACTION: {
880 handleMessageTransaction();
881 break;
882 }
883 case MessageQueue::INVALIDATE: {
884 bool refreshNeeded = handleMessageTransaction();
885 refreshNeeded |= handleMessageInvalidate();
Dan Stoza58784442014-12-02 16:58:17 -0800886 refreshNeeded |= mRepaintEverything;
Dan Stoza6b9454d2014-11-07 16:00:59 -0800887 if (refreshNeeded) {
Dan Stoza58784442014-12-02 16:58:17 -0800888 // Signal a refresh if a transaction modified the window state,
889 // a new buffer was latched, or if HWC has requested a full
890 // repaint
Dan Stoza6b9454d2014-11-07 16:00:59 -0800891 signalRefresh();
892 }
893 break;
894 }
895 case MessageQueue::REFRESH: {
896 handleMessageRefresh();
897 break;
898 }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800899 }
900}
901
Dan Stoza6b9454d2014-11-07 16:00:59 -0800902bool SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700903 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700904 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700905 handleTransaction(transactionFlags);
Dan Stoza6b9454d2014-11-07 16:00:59 -0800906 return true;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700907 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800908 return false;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700909}
910
Dan Stoza6b9454d2014-11-07 16:00:59 -0800911bool SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700912 ATRACE_CALL();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800913 return handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700914}
915
916void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700917 ATRACE_CALL();
918 preComposition();
919 rebuildLayerStacks();
920 setUpHWComposer();
921 doDebugFlashRegions();
922 doComposition();
923 postComposition();
924}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700925
Mathias Agopiancd60f992012-08-16 16:28:27 -0700926void SurfaceFlinger::doDebugFlashRegions()
927{
928 // is debugging enabled
929 if (CC_LIKELY(!mDebugRegion))
930 return;
931
932 const bool repaintEverything = mRepaintEverything;
933 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
934 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700935 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700936 // transform the dirty region into this screen's coordinate space
937 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
938 if (!dirtyRegion.isEmpty()) {
939 // redraw the whole screen
940 doComposeSurfaces(hw, Region(hw->bounds()));
941
942 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700943 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700944 RenderEngine& engine(getRenderEngine());
945 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
946
Mathias Agopiancd60f992012-08-16 16:28:27 -0700947 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700948 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700949 }
950 }
951 }
952
953 postFramebuffer();
954
955 if (mDebugRegion > 1) {
956 usleep(mDebugRegion * 1000);
957 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700958
959 HWComposer& hwc(getHwComposer());
960 if (hwc.initCheck() == NO_ERROR) {
961 status_t err = hwc.prepare();
962 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
963 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700964}
965
966void SurfaceFlinger::preComposition()
967{
968 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700969 const LayerVector& layers(mDrawingState.layersSortedByZ);
970 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700971 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700972 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700973 needExtraInvalidate = true;
974 }
975 }
976 if (needExtraInvalidate) {
977 signalLayerUpdate();
978 }
979}
980
981void SurfaceFlinger::postComposition()
982{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700983 const LayerVector& layers(mDrawingState.layersSortedByZ);
984 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700985 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700986 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700987 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800988
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700989 const HWComposer& hwc = getHwComposer();
990 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
991
992 if (presentFence->isValid()) {
993 if (mPrimaryDispSync.addPresentFence(presentFence)) {
994 enableHardwareVsync();
995 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700996 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700997 }
998 }
999
Andy McFadden5167ec62014-05-22 13:08:43 -07001000 if (kIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001001 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001002 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001003 enableHardwareVsync();
1004 }
1005 }
1006
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001007 if (mAnimCompositionPending) {
1008 mAnimCompositionPending = false;
1009
Jesse Halla9a1b002013-02-27 16:39:25 -08001010 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001011 mAnimFrameTracker.setActualPresentFence(presentFence);
1012 } else {
1013 // The HWC doesn't support present fences, so use the refresh
1014 // timestamp instead.
1015 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1016 mAnimFrameTracker.setActualPresentTime(presentTime);
1017 }
1018 mAnimFrameTracker.advanceFrame();
1019 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001020}
1021
1022void SurfaceFlinger::rebuildLayerStacks() {
1023 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001024 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001025 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -07001026 mVisibleRegionsDirty = false;
1027 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001028
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001029 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001030 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001031 Region opaqueRegion;
1032 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -08001033 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -07001034 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001035 const Transform& tr(hw->getTransform());
1036 const Rect bounds(hw->getBounds());
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001037 if (hw->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001038 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001039 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001040
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001041 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001042 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001043 const sp<Layer>& layer(layers[i]);
1044 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001045 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -07001046 Region drawRegion(tr.transform(
1047 layer->visibleNonTransparentRegion));
1048 drawRegion.andSelf(bounds);
1049 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001050 layersSortedByZ.add(layer);
1051 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001052 }
1053 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001054 }
Mathias Agopian42977342012-08-05 00:40:46 -07001055 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001056 hw->undefinedRegion.set(bounds);
1057 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
1058 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001059 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001060 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001061}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001062
Mathias Agopiancd60f992012-08-16 16:28:27 -07001063void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001064 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001065 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1066 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1067 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1068
1069 // If nothing has changed (!dirty), don't recompose.
1070 // If something changed, but we don't currently have any visible layers,
1071 // and didn't when we last did a composition, then skip it this time.
1072 // The second rule does two things:
1073 // - When all layers are removed from a display, we'll emit one black
1074 // frame, then nothing more until we get new layers.
1075 // - When a display is created with a private layer stack, we won't
1076 // emit any black frames until a layer is added to the layer stack.
1077 bool mustRecompose = dirty && !(empty && wasEmpty);
1078
1079 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1080 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1081 mustRecompose ? "doing" : "skipping",
1082 dirty ? "+" : "-",
1083 empty ? "+" : "-",
1084 wasEmpty ? "+" : "-");
1085
Dan Stoza71433162014-02-04 16:22:36 -08001086 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001087
1088 if (mustRecompose) {
1089 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1090 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001091 }
1092
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001093 HWComposer& hwc(getHwComposer());
1094 if (hwc.initCheck() == NO_ERROR) {
1095 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001096 if (CC_UNLIKELY(mHwWorkListDirty)) {
1097 mHwWorkListDirty = false;
1098 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1099 sp<const DisplayDevice> hw(mDisplays[dpy]);
1100 const int32_t id = hw->getHwcDisplayId();
1101 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001102 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001103 hw->getVisibleLayersSortedByZ());
1104 const size_t count = currentLayers.size();
1105 if (hwc.createWorkList(id, count) == NO_ERROR) {
1106 HWComposer::LayerListIterator cur = hwc.begin(id);
1107 const HWComposer::LayerListIterator end = hwc.end(id);
1108 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001109 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001110 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001111 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001112 cur->setSkip(true);
1113 }
1114 }
1115 }
1116 }
1117 }
1118 }
1119
1120 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001121 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001122 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001123 const int32_t id = hw->getHwcDisplayId();
1124 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001125 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001126 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001127 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001128 HWComposer::LayerListIterator cur = hwc.begin(id);
1129 const HWComposer::LayerListIterator end = hwc.end(id);
1130 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1131 /*
1132 * update the per-frame h/w composer data for each layer
1133 * and build the transparent region of the FB
1134 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001135 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001136 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -07001137 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001138 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001139 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001140
Riley Andrews03414a12014-07-01 14:22:59 -07001141 // If possible, attempt to use the cursor overlay on each display.
1142 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1143 sp<const DisplayDevice> hw(mDisplays[dpy]);
1144 const int32_t id = hw->getHwcDisplayId();
1145 if (id >= 0) {
1146 const Vector< sp<Layer> >& currentLayers(
1147 hw->getVisibleLayersSortedByZ());
1148 const size_t count = currentLayers.size();
1149 HWComposer::LayerListIterator cur = hwc.begin(id);
1150 const HWComposer::LayerListIterator end = hwc.end(id);
1151 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1152 const sp<Layer>& layer(currentLayers[i]);
1153 if (layer->isPotentialCursor()) {
1154 cur->setIsCursorLayerHint();
1155 break;
1156 }
1157 }
1158 }
1159 }
1160
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001161 status_t err = hwc.prepare();
1162 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001163
1164 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1165 sp<const DisplayDevice> hw(mDisplays[dpy]);
1166 hw->prepareFrame(hwc);
1167 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001168 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001169}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001170
Mathias Agopiancd60f992012-08-16 16:28:27 -07001171void SurfaceFlinger::doComposition() {
1172 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001173 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001174 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001175 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001176 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001177 // transform the dirty region into this screen's coordinate space
1178 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001179
1180 // repaint the framebuffer (if needed)
1181 doDisplayComposition(hw, dirtyRegion);
1182
Mathias Agopiancd60f992012-08-16 16:28:27 -07001183 hw->dirtyRegion.clear();
1184 hw->flip(hw->swapRegion);
1185 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001186 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001187 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001188 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001189 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001190 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001191}
1192
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193void SurfaceFlinger::postFramebuffer()
1194{
Mathias Agopian841cde52012-03-01 15:44:37 -08001195 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001196
Mathias Agopiana44b0412011-10-16 18:46:35 -07001197 const nsecs_t now = systemTime();
1198 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001199
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001200 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001201 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001202 if (!hwc.supportsFramebufferTarget()) {
1203 // EGL spec says:
1204 // "surface must be bound to the calling thread's current context,
1205 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001206 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001207 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001208 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001209 }
1210
Mathias Agopian6da15f42013-09-25 20:40:07 -07001211 // make the default display current because the VirtualDisplayDevice code cannot
1212 // deal with dequeueBuffer() being called outside of the composition loop; however
1213 // the code below can call glFlush() which is allowed (and does in some case) call
1214 // dequeueBuffer().
1215 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1216
Mathias Agopian92a979a2012-08-02 18:32:23 -07001217 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001218 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001219 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001220 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001221 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001222 int32_t id = hw->getHwcDisplayId();
1223 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001224 HWComposer::LayerListIterator cur = hwc.begin(id);
1225 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001226 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001227 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001228 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001229 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001230 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001231 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001232 }
Jesse Hallef194142012-06-14 14:45:17 -07001233 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001234 }
1235
Mathias Agopiana44b0412011-10-16 18:46:35 -07001236 mLastSwapBufferTime = systemTime() - now;
1237 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001238
1239 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1240 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1241 logFrameStats();
1242 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243}
1244
Mathias Agopian87baae12012-07-31 12:38:26 -07001245void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246{
Mathias Agopian841cde52012-03-01 15:44:37 -08001247 ATRACE_CALL();
1248
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001249 // here we keep a copy of the drawing state (that is the state that's
1250 // going to be overwritten by handleTransactionLocked()) outside of
1251 // mStateLock so that the side-effects of the State assignment
1252 // don't happen with mStateLock held (which can cause deadlocks).
1253 State drawingState(mDrawingState);
1254
Mathias Agopianca4d3602011-05-19 15:38:14 -07001255 Mutex::Autolock _l(mStateLock);
1256 const nsecs_t now = systemTime();
1257 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258
Mathias Agopianca4d3602011-05-19 15:38:14 -07001259 // Here we're guaranteed that some transaction flags are set
1260 // so we can call handleTransactionLocked() unconditionally.
1261 // We call getTransactionFlags(), which will also clear the flags,
1262 // with mStateLock held to guarantee that mCurrentState won't change
1263 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001264
Mathias Agopiane57f2922012-08-09 16:29:12 -07001265 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001266 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001267
Mathias Agopianca4d3602011-05-19 15:38:14 -07001268 mLastTransactionTime = systemTime() - now;
1269 mDebugInTransaction = 0;
1270 invalidateHwcGeometry();
1271 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001272}
1273
Mathias Agopian87baae12012-07-31 12:38:26 -07001274void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001275{
1276 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277 const size_t count = currentLayers.size();
1278
1279 /*
1280 * Traversal of the children
1281 * (perform the transaction for each of them if needed)
1282 */
1283
Mathias Agopian3559b072012-08-15 13:46:03 -07001284 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001286 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1288 if (!trFlags) continue;
1289
1290 const uint32_t flags = layer->doTransaction(0);
1291 if (flags & Layer::eVisibleRegion)
1292 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001293 }
1294 }
1295
1296 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001297 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001298 */
1299
Mathias Agopiane57f2922012-08-09 16:29:12 -07001300 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001301 // here we take advantage of Vector's copy-on-write semantics to
1302 // improve performance by skipping the transaction entirely when
1303 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001304 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1305 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001306 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001308 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001309 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001310
1311 // find the displays that were removed
1312 // (ie: in drawing state but not in current state)
1313 // also handle displays that changed
1314 // (ie: displays that are in both lists)
1315 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001316 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1317 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001318 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001319 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001320 // Call makeCurrent() on the primary display so we can
1321 // be sure that nothing associated with this display
1322 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001323 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001324 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001325 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1326 if (hw != NULL)
1327 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001328 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001329 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001330 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001331 } else {
1332 ALOGW("trying to remove the main display");
1333 }
1334 } else {
1335 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001336 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001337 const wp<IBinder>& display(curr.keyAt(j));
Marco Nelissen097ca272014-11-14 08:01:01 -08001338 const sp<IBinder> state_binder = IInterface::asBinder(state.surface);
1339 const sp<IBinder> draw_binder = IInterface::asBinder(draw[i].surface);
Dan Albert1474f882014-09-08 18:59:09 -07001340 if (state_binder != draw_binder) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001341 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001342 // recreating the DisplayDevice, so we just remove it
1343 // from the drawing state, so that it get re-added
1344 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001345 sp<DisplayDevice> hw(getDisplayDevice(display));
1346 if (hw != NULL)
1347 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001348 mDisplays.removeItem(display);
1349 mDrawingState.displays.removeItemsAt(i);
1350 dc--; i--;
1351 // at this point we must loop to the next item
1352 continue;
1353 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001354
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001355 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001356 if (disp != NULL) {
1357 if (state.layerStack != draw[i].layerStack) {
1358 disp->setLayerStack(state.layerStack);
1359 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001360 if ((state.orientation != draw[i].orientation)
1361 || (state.viewport != draw[i].viewport)
1362 || (state.frame != draw[i].frame))
1363 {
1364 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001365 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001366 }
Michael Lentine47e45402014-07-18 15:34:25 -07001367 if (state.width != draw[i].width || state.height != draw[i].height) {
1368 disp->setDisplaySize(state.width, state.height);
1369 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001370 }
1371 }
1372 }
1373
1374 // find displays that were added
1375 // (ie: in current state but not in drawing state)
1376 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001377 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001378 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001379
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001380 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001381 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001382 sp<IGraphicBufferProducer> bqProducer;
1383 sp<IGraphicBufferConsumer> bqConsumer;
1384 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1385 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001386
Jesse Hall02d86562013-03-25 14:43:23 -07001387 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001388 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001389 // Virtual displays without a surface are dormant:
1390 // they have external state (layer stack, projection,
1391 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001392 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001393
Dan Stoza1f3efb12014-10-15 16:34:55 -07001394 int width = 0;
1395 int status = state.surface->query(
1396 NATIVE_WINDOW_WIDTH, &width);
1397 ALOGE_IF(status != NO_ERROR,
1398 "Unable to query width (%d)", status);
1399 int height = 0;
1400 status = state.surface->query(
1401 NATIVE_WINDOW_HEIGHT, &height);
1402 ALOGE_IF(status != NO_ERROR,
1403 "Unable to query height (%d)", status);
1404 if (MAX_VIRTUAL_DISPLAY_DIMENSION == 0 ||
1405 (width <= MAX_VIRTUAL_DISPLAY_DIMENSION &&
1406 height <= MAX_VIRTUAL_DISPLAY_DIMENSION)) {
1407 hwcDisplayId = allocateHwcDisplayId(state.type);
1408 }
1409
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001410 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
Dan Stozab9b08832014-03-13 11:55:57 -07001411 *mHwc, hwcDisplayId, state.surface,
1412 bqProducer, bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001413
1414 dispSurface = vds;
Michael Lentine47e45402014-07-18 15:34:25 -07001415 producer = vds;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001416 }
1417 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001418 ALOGE_IF(state.surface!=NULL,
1419 "adding a supported display, but rendering "
1420 "surface is provided (%p), ignoring it",
1421 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001422 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001423 // for supported (by hwc) displays we provide our
1424 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001425 dispSurface = new FramebufferSurface(*mHwc, state.type,
1426 bqConsumer);
1427 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001428 }
1429
1430 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001431 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001432 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001433 state.type, hwcDisplayId,
1434 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001435 display, dispSurface, producer,
1436 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001437 hw->setLayerStack(state.layerStack);
1438 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001439 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001440 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001441 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001442 if (state.isVirtualDisplay()) {
1443 if (hwcDisplayId >= 0) {
1444 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1445 hw->getWidth(), hw->getHeight(),
1446 hw->getFormat());
1447 }
1448 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001449 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001450 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001451 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001452 }
1453 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001454 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001455 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001456
Mathias Agopian84300952012-11-21 16:02:13 -08001457 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1458 // The transform hint might have changed for some layers
1459 // (either because a display has changed, or because a layer
1460 // as changed).
1461 //
1462 // Walk through all the layers in currentLayers,
1463 // and update their transform hint.
1464 //
1465 // If a layer is visible only on a single display, then that
1466 // display is used to calculate the hint, otherwise we use the
1467 // default display.
1468 //
1469 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1470 // the hint is set before we acquire a buffer from the surface texture.
1471 //
1472 // NOTE: layer transactions have taken place already, so we use their
1473 // drawing state. However, SurfaceFlinger's own transaction has not
1474 // happened yet, so we must use the current state layer list
1475 // (soon to become the drawing state list).
1476 //
1477 sp<const DisplayDevice> disp;
1478 uint32_t currentlayerStack = 0;
1479 for (size_t i=0; i<count; i++) {
1480 // NOTE: we rely on the fact that layers are sorted by
1481 // layerStack first (so we don't have to traverse the list
1482 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001483 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001484 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001485 if (i==0 || currentlayerStack != layerStack) {
1486 currentlayerStack = layerStack;
1487 // figure out if this layerstack is mirrored
1488 // (more than one display) if so, pick the default display,
1489 // if not, pick the only display it's on.
1490 disp.clear();
1491 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1492 sp<const DisplayDevice> hw(mDisplays[dpy]);
1493 if (hw->getLayerStack() == currentlayerStack) {
1494 if (disp == NULL) {
1495 disp = hw;
1496 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001497 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001498 break;
1499 }
1500 }
1501 }
1502 }
Chet Haase91d25932013-04-11 15:24:55 -07001503 if (disp == NULL) {
1504 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1505 // redraw after transform hint changes. See bug 8508397.
1506
1507 // could be null when this layer is using a layerStack
1508 // that is not visible on any display. Also can occur at
1509 // screen off/on times.
1510 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001511 }
Chet Haase91d25932013-04-11 15:24:55 -07001512 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001513 }
1514 }
1515
1516
Mathias Agopian3559b072012-08-15 13:46:03 -07001517 /*
1518 * Perform our own transaction if needed
1519 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001520
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001521 const LayerVector& layers(mDrawingState.layersSortedByZ);
1522 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001523 // layers have been added
1524 mVisibleRegionsDirty = true;
1525 }
1526
1527 // some layers might have been removed, so
1528 // we need to update the regions they're exposing.
1529 if (mLayersRemoved) {
1530 mLayersRemoved = false;
1531 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001532 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001533 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001534 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001535 if (currentLayers.indexOf(layer) < 0) {
1536 // this layer is not visible anymore
1537 // TODO: we could traverse the tree from front to back and
1538 // compute the actual visible region
1539 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001540 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001541 Region visibleReg = s.transform.transform(
1542 Region(Rect(s.active.w, s.active.h)));
1543 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001544 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001545 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001546 }
1547
1548 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001549
1550 updateCursorAsync();
1551}
1552
1553void SurfaceFlinger::updateCursorAsync()
1554{
1555 HWComposer& hwc(getHwComposer());
1556 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1557 sp<const DisplayDevice> hw(mDisplays[dpy]);
1558 const int32_t id = hw->getHwcDisplayId();
1559 if (id < 0) {
1560 continue;
1561 }
1562 const Vector< sp<Layer> >& currentLayers(
1563 hw->getVisibleLayersSortedByZ());
1564 const size_t count = currentLayers.size();
1565 HWComposer::LayerListIterator cur = hwc.begin(id);
1566 const HWComposer::LayerListIterator end = hwc.end(id);
1567 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1568 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1569 continue;
1570 }
1571 const sp<Layer>& layer(currentLayers[i]);
1572 Rect cursorPos = layer->getPosition(hw);
1573 hwc.setCursorPositionAsync(id, cursorPos);
1574 break;
1575 }
1576 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001577}
1578
1579void SurfaceFlinger::commitTransaction()
1580{
1581 if (!mLayersPendingRemoval.isEmpty()) {
1582 // Notify removed layers now that they can't be drawn from
1583 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1584 mLayersPendingRemoval[i]->onRemoved();
1585 }
1586 mLayersPendingRemoval.clear();
1587 }
1588
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001589 // If this transaction is part of a window animation then the next frame
1590 // we composite should be considered an animation as well.
1591 mAnimCompositionPending = mAnimTransactionPending;
1592
Mathias Agopian4fec8732012-06-29 14:12:52 -07001593 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001594 mTransactionPending = false;
1595 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001596 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001597}
1598
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001599void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001600 const LayerVector& currentLayers, uint32_t layerStack,
1601 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001602{
Mathias Agopian841cde52012-03-01 15:44:37 -08001603 ATRACE_CALL();
1604
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001605 Region aboveOpaqueLayers;
1606 Region aboveCoveredLayers;
1607 Region dirty;
1608
Mathias Agopian87baae12012-07-31 12:38:26 -07001609 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001610
1611 size_t i = currentLayers.size();
1612 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001613 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001614
1615 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001616 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001617
Jesse Hall01e29052013-02-19 16:13:35 -08001618 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001619 if (s.layerStack != layerStack)
1620 continue;
1621
Mathias Agopianab028732010-03-16 16:41:46 -07001622 /*
1623 * opaqueRegion: area of a surface that is fully opaque.
1624 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001625 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001626
1627 /*
1628 * visibleRegion: area of a surface that is visible on screen
1629 * and not fully transparent. This is essentially the layer's
1630 * footprint minus the opaque regions above it.
1631 * Areas covered by a translucent surface are considered visible.
1632 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001633 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001634
1635 /*
1636 * coveredRegion: area of a surface that is covered by all
1637 * visible regions above it (which includes the translucent areas).
1638 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001639 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001640
Jesse Halla8026d22012-09-25 13:25:04 -07001641 /*
1642 * transparentRegion: area of a surface that is hinted to be completely
1643 * transparent. This is only used to tell when the layer has no visible
1644 * non-transparent regions and can be removed from the layer list. It
1645 * does not affect the visibleRegion of this layer or any layers
1646 * beneath it. The hint may not be correct if apps don't respect the
1647 * SurfaceView restrictions (which, sadly, some don't).
1648 */
1649 Region transparentRegion;
1650
Mathias Agopianab028732010-03-16 16:41:46 -07001651
1652 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001653 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001654 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001655 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001656 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001657 if (!visibleRegion.isEmpty()) {
1658 // Remove the transparent area from the visible region
1659 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001660 const Transform tr(s.transform);
1661 if (tr.transformed()) {
1662 if (tr.preserveRects()) {
1663 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001664 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001665 } else {
1666 // transformation too complex, can't do the
1667 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001668 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001669 }
1670 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001671 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001672 }
Mathias Agopianab028732010-03-16 16:41:46 -07001673 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001674
Mathias Agopianab028732010-03-16 16:41:46 -07001675 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001676 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001677 if (s.alpha==255 && !translucent &&
1678 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1679 // the opaque region is the layer's footprint
1680 opaqueRegion = visibleRegion;
1681 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001682 }
1683 }
1684
Mathias Agopianab028732010-03-16 16:41:46 -07001685 // Clip the covered region to the visible region
1686 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1687
1688 // Update aboveCoveredLayers for next (lower) layer
1689 aboveCoveredLayers.orSelf(visibleRegion);
1690
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001691 // subtract the opaque region covered by the layers above us
1692 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001693
1694 // compute this layer's dirty region
1695 if (layer->contentDirty) {
1696 // we need to invalidate the whole region
1697 dirty = visibleRegion;
1698 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001699 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001700 layer->contentDirty = false;
1701 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001702 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001703 * the exposed region consists of two components:
1704 * 1) what's VISIBLE now and was COVERED before
1705 * 2) what's EXPOSED now less what was EXPOSED before
1706 *
1707 * note that (1) is conservative, we start with the whole
1708 * visible region but only keep what used to be covered by
1709 * something -- which mean it may have been exposed.
1710 *
1711 * (2) handles areas that were not covered by anything but got
1712 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001713 */
Mathias Agopianab028732010-03-16 16:41:46 -07001714 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001715 const Region oldVisibleRegion = layer->visibleRegion;
1716 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001717 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1718 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001719 }
1720 dirty.subtractSelf(aboveOpaqueLayers);
1721
1722 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001723 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001724
Mathias Agopianab028732010-03-16 16:41:46 -07001725 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001726 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001727
Jesse Halla8026d22012-09-25 13:25:04 -07001728 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001729 layer->setVisibleRegion(visibleRegion);
1730 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001731 layer->setVisibleNonTransparentRegion(
1732 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001733 }
1734
Mathias Agopian87baae12012-07-31 12:38:26 -07001735 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001736}
1737
Mathias Agopian87baae12012-07-31 12:38:26 -07001738void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1739 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001740 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001741 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1742 if (hw->getLayerStack() == layerStack) {
1743 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001744 }
1745 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001746}
1747
Dan Stoza6b9454d2014-11-07 16:00:59 -08001748bool SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001749{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001750 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001751
Mathias Agopian4fec8732012-06-29 14:12:52 -07001752 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001753 const LayerVector& layers(mDrawingState.layersSortedByZ);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001754 bool frameQueued = false;
Eric Penner51c59cd2014-07-28 19:51:58 -07001755
1756 // Store the set of layers that need updates. This set must not change as
1757 // buffers are being latched, as this could result in a deadlock.
1758 // Example: Two producers share the same command stream and:
1759 // 1.) Layer 0 is latched
1760 // 2.) Layer 0 gets a new frame
1761 // 2.) Layer 1 gets a new frame
1762 // 3.) Layer 1 is latched.
1763 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1764 // second frame. But layer 0's second frame could be waiting on display.
1765 Vector<Layer*> layersWithQueuedFrames;
1766 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001767 const sp<Layer>& layer(layers[i]);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001768 if (layer->hasQueuedFrame()) {
1769 frameQueued = true;
1770 if (layer->shouldPresentNow(mPrimaryDispSync)) {
1771 layersWithQueuedFrames.push_back(layer.get());
Dan Stozaee44edd2015-03-23 15:50:23 -07001772 } else {
1773 layer->useEmptyDamage();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001774 }
Dan Stozaee44edd2015-03-23 15:50:23 -07001775 } else {
1776 layer->useEmptyDamage();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001777 }
Eric Penner51c59cd2014-07-28 19:51:58 -07001778 }
1779 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
1780 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07001781 const Region dirty(layer->latchBuffer(visibleRegions));
Dan Stozaee44edd2015-03-23 15:50:23 -07001782 layer->useSurfaceDamage();
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001783 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001784 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001785 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001786
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001787 mVisibleRegionsDirty |= visibleRegions;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001788
1789 // If we will need to wake up at some time in the future to deal with a
1790 // queued frame that shouldn't be displayed during this vsync period, wake
1791 // up during the next vsync period to check again.
1792 if (frameQueued && layersWithQueuedFrames.empty()) {
1793 signalLayerUpdate();
1794 }
1795
1796 // Only continue with the refresh if there is actually new work to do
1797 return !layersWithQueuedFrames.empty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001798}
1799
Mathias Agopianad456f92011-01-13 17:53:01 -08001800void SurfaceFlinger::invalidateHwcGeometry()
1801{
1802 mHwWorkListDirty = true;
1803}
1804
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001805
Mathias Agopiancd60f992012-08-16 16:28:27 -07001806void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001807 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001808{
Dan Stoza71433162014-02-04 16:22:36 -08001809 // We only need to actually compose the display if:
1810 // 1) It is being handled by hardware composer, which may need this to
1811 // keep its virtual display state machine in sync, or
1812 // 2) There is work to be done (the dirty region isn't empty)
1813 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1814 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1815 return;
1816 }
1817
Mathias Agopian87baae12012-07-31 12:38:26 -07001818 Region dirtyRegion(inDirtyRegion);
1819
Mathias Agopianb8a55602009-06-26 19:06:36 -07001820 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001821 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001822
Mathias Agopian42977342012-08-05 00:40:46 -07001823 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001824 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001825 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1826 // takes a rectangle, we must make sure to update that whole
1827 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001828 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001829 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001830 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001831 // We need to redraw the rectangle that will be updated
1832 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001833 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001834 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001835 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001836 } else {
1837 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001838 dirtyRegion.set(hw->bounds());
1839 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001840 }
1841 }
1842
Alan Viverette9c5a3332013-09-12 20:04:35 -07001843 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Michael Lentine3f121fc2014-10-01 11:17:28 -07001844 if (!doComposeSurfaces(hw, dirtyRegion)) return;
Mathias Agopianff2ed702013-09-01 21:36:12 -07001845 } else {
1846 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001847 mat4 colorMatrix = mColorMatrix;
1848 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001849 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001850 }
Dan Stoza8e8eba52014-10-20 15:46:09 -07001851 mat4 oldMatrix = engine.setupColorTransform(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001852 doComposeSurfaces(hw, dirtyRegion);
Dan Stoza8e8eba52014-10-20 15:46:09 -07001853 engine.setupColorTransform(oldMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001854 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001855
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001856 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001857 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001858
1859 // swap buffers (presentation)
1860 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001861}
1862
Michael Lentine3f121fc2014-10-01 11:17:28 -07001863bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001864{
Mathias Agopian3f844832013-08-07 21:24:32 -07001865 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001866 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001867 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001868 HWComposer::LayerListIterator cur = hwc.begin(id);
1869 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001870
Jesse Halld05a17f2013-09-30 16:49:30 -07001871 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001872 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001873 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001874 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1875 hw->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07001876 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1877 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
1878 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
1879 }
1880 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08001881 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001882
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001883 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001884 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001885 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001886 // when using overlays, we assume a fully transparent framebuffer
1887 // NOTE: we could reduce how much we need to clear, for instance
1888 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001889 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001890 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001891 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001892 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001893 // we start with the whole screen area
1894 const Region bounds(hw->getBounds());
1895
1896 // we remove the scissor part
1897 // we're left with the letterbox region
1898 // (common case is that letterbox ends-up being empty)
1899 const Region letterbox(bounds.subtract(hw->getScissor()));
1900
1901 // compute the area to clear
1902 Region region(hw->undefinedRegion.merge(letterbox));
1903
1904 // but limit it to the dirty region
1905 region.andSelf(dirty);
1906
Mathias Agopianb9494d52012-04-18 02:28:45 -07001907 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001908 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001909 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001910 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001911 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001912 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001913
Mathias Agopian766dc492012-10-30 18:08:06 -07001914 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1915 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001916 // scissor on the main display. It should never be needed
1917 // anyways (though in theory it could since the API allows it).
1918 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001919 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001920 if (scissor != bounds) {
1921 // scissor doesn't match the screen's dimensions, so we
1922 // need to clear everything outside of it and enable
1923 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001924
Mathias Agopianf45c5102012-10-24 16:29:17 -07001925 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001926 const uint32_t height = hw->getHeight();
1927 engine.setScissor(scissor.left, height - scissor.bottom,
1928 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001929 }
1930 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001931 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001932
Mathias Agopian85d751c2012-08-29 16:59:24 -07001933 /*
1934 * and then, render the layers targeted at the framebuffer
1935 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001936
Mathias Agopian13127d82013-03-05 17:47:11 -08001937 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001938 const size_t count = layers.size();
1939 const Transform& tr = hw->getTransform();
1940 if (cur != end) {
1941 // we're using h/w composer
1942 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001943 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001944 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001945 if (!clip.isEmpty()) {
1946 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07001947 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07001948 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001949 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001950 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1951 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001952 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001953 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001954 // never clear the very first layer since we're
1955 // guaranteed the FB is already cleared
1956 layer->clearWithOpenGL(hw, clip);
1957 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001958 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001959 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001960 case HWC_FRAMEBUFFER: {
1961 layer->draw(hw, clip);
1962 break;
1963 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001964 case HWC_FRAMEBUFFER_TARGET: {
1965 // this should not happen as the iterator shouldn't
1966 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001967 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001968 break;
1969 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001970 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001971 }
1972 layer->setAcquireFence(hw, *cur);
1973 }
1974 } else {
1975 // we're not using h/w composer
1976 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001977 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001978 const Region clip(dirty.intersect(
1979 tr.transform(layer->visibleRegion)));
1980 if (!clip.isEmpty()) {
1981 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001982 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001983 }
1984 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001985
1986 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001987 engine.disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07001988 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001989}
1990
Mathias Agopian3f844832013-08-07 21:24:32 -07001991void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001992 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001993 RenderEngine& engine(getRenderEngine());
1994 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001995}
1996
Dan Stozae7f8dde2015-04-30 13:29:25 -07001997status_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -08001998 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001999 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08002000 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07002001{
Dan Stozae7f8dde2015-04-30 13:29:25 -07002002 // add this layer to the current state list
2003 {
2004 Mutex::Autolock _l(mStateLock);
2005 if (mCurrentState.layersSortedByZ.size() >= MAX_LAYERS) {
2006 return NO_MEMORY;
2007 }
2008 mCurrentState.layersSortedByZ.add(lbc);
2009 mGraphicBufferProducerList.add(IInterface::asBinder(gbc));
2010 }
2011
Mathias Agopian96f08192010-06-02 23:28:45 -07002012 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08002013 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07002014
Dan Stozae7f8dde2015-04-30 13:29:25 -07002015 return NO_ERROR;
Mathias Agopian96f08192010-06-02 23:28:45 -07002016}
2017
Mathias Agopian3f844832013-08-07 21:24:32 -07002018status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002019 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08002020 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002021 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07002022 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002023 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07002024 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002025 return NO_ERROR;
2026 }
Mathias Agopian3d579642009-06-04 18:46:21 -07002027 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002028}
2029
Dan Stozac7014012014-02-14 15:03:43 -08002030uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07002031 return android_atomic_release_load(&mTransactionFlags);
2032}
2033
Mathias Agopian3f844832013-08-07 21:24:32 -07002034uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002035 return android_atomic_and(~flags, &mTransactionFlags) & flags;
2036}
2037
Mathias Agopian3f844832013-08-07 21:24:32 -07002038uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002039 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
2040 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002041 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002042 }
2043 return old;
2044}
2045
Mathias Agopian8b33f032012-07-24 20:43:54 -07002046void SurfaceFlinger::setTransactionState(
2047 const Vector<ComposerState>& state,
2048 const Vector<DisplayState>& displays,
2049 uint32_t flags)
2050{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002051 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07002052 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07002053 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002054
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002055 if (flags & eAnimation) {
2056 // For window updates that are part of an animation we must wait for
2057 // previous animation "frames" to be handled.
2058 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002059 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002060 if (CC_UNLIKELY(err != NO_ERROR)) {
2061 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002062 // caller after a few seconds.
2063 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
2064 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002065 mAnimTransactionPending = false;
2066 break;
2067 }
2068 }
2069 }
2070
Mathias Agopiane57f2922012-08-09 16:29:12 -07002071 size_t count = displays.size();
2072 for (size_t i=0 ; i<count ; i++) {
2073 const DisplayState& s(displays[i]);
2074 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002075 }
2076
Mathias Agopiane57f2922012-08-09 16:29:12 -07002077 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002078 for (size_t i=0 ; i<count ; i++) {
2079 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002080 // Here we need to check that the interface we're given is indeed
2081 // one of our own. A malicious client could give us a NULL
2082 // IInterface, or one of its own or even one of our own but a
2083 // different type. All these situations would cause us to crash.
2084 //
2085 // NOTE: it would be better to use RTTI as we could directly check
2086 // that we have a Client*. however, RTTI is disabled in Android.
2087 if (s.client != NULL) {
Marco Nelissen097ca272014-11-14 08:01:01 -08002088 sp<IBinder> binder = IInterface::asBinder(s.client);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002089 if (binder != NULL) {
2090 String16 desc(binder->getInterfaceDescriptor());
2091 if (desc == ISurfaceComposerClient::descriptor) {
2092 sp<Client> client( static_cast<Client *>(s.client.get()) );
2093 transactionFlags |= setClientStateLocked(client, s.state);
2094 }
2095 }
2096 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002097 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002098
Mathias Agopian386aa982011-11-07 21:58:03 -08002099 if (transactionFlags) {
2100 // this triggers the transaction
2101 setTransactionFlags(transactionFlags);
2102
2103 // if this is a synchronous transaction, wait for it to take effect
2104 // before returning.
2105 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002106 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002107 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002108 if (flags & eAnimation) {
2109 mAnimTransactionPending = true;
2110 }
2111 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002112 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2113 if (CC_UNLIKELY(err != NO_ERROR)) {
2114 // just in case something goes wrong in SF, return to the
2115 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002116 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2117 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002118 break;
2119 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002120 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002121 }
2122}
2123
Mathias Agopiane57f2922012-08-09 16:29:12 -07002124uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2125{
Jesse Hall9a143922012-10-04 16:29:19 -07002126 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2127 if (dpyIdx < 0)
2128 return 0;
2129
Mathias Agopiane57f2922012-08-09 16:29:12 -07002130 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002131 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002132 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002133 const uint32_t what = s.what;
2134 if (what & DisplayState::eSurfaceChanged) {
Marco Nelissen097ca272014-11-14 08:01:01 -08002135 if (IInterface::asBinder(disp.surface) != IInterface::asBinder(s.surface)) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002136 disp.surface = s.surface;
2137 flags |= eDisplayTransactionNeeded;
2138 }
2139 }
2140 if (what & DisplayState::eLayerStackChanged) {
2141 if (disp.layerStack != s.layerStack) {
2142 disp.layerStack = s.layerStack;
2143 flags |= eDisplayTransactionNeeded;
2144 }
2145 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002146 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002147 if (disp.orientation != s.orientation) {
2148 disp.orientation = s.orientation;
2149 flags |= eDisplayTransactionNeeded;
2150 }
2151 if (disp.frame != s.frame) {
2152 disp.frame = s.frame;
2153 flags |= eDisplayTransactionNeeded;
2154 }
2155 if (disp.viewport != s.viewport) {
2156 disp.viewport = s.viewport;
2157 flags |= eDisplayTransactionNeeded;
2158 }
2159 }
Michael Lentine47e45402014-07-18 15:34:25 -07002160 if (what & DisplayState::eDisplaySizeChanged) {
2161 if (disp.width != s.width) {
2162 disp.width = s.width;
2163 flags |= eDisplayTransactionNeeded;
2164 }
2165 if (disp.height != s.height) {
2166 disp.height = s.height;
2167 flags |= eDisplayTransactionNeeded;
2168 }
2169 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002170 }
2171 return flags;
2172}
2173
2174uint32_t SurfaceFlinger::setClientStateLocked(
2175 const sp<Client>& client,
2176 const layer_state_t& s)
2177{
2178 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002179 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002180 if (layer != 0) {
2181 const uint32_t what = s.what;
2182 if (what & layer_state_t::ePositionChanged) {
2183 if (layer->setPosition(s.x, s.y))
2184 flags |= eTraversalNeeded;
2185 }
2186 if (what & layer_state_t::eLayerChanged) {
2187 // NOTE: index needs to be calculated before we update the state
2188 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2189 if (layer->setLayer(s.z)) {
2190 mCurrentState.layersSortedByZ.removeAt(idx);
2191 mCurrentState.layersSortedByZ.add(layer);
2192 // we need traversal (state changed)
2193 // AND transaction (list changed)
2194 flags |= eTransactionNeeded|eTraversalNeeded;
2195 }
2196 }
2197 if (what & layer_state_t::eSizeChanged) {
2198 if (layer->setSize(s.w, s.h)) {
2199 flags |= eTraversalNeeded;
2200 }
2201 }
2202 if (what & layer_state_t::eAlphaChanged) {
2203 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2204 flags |= eTraversalNeeded;
2205 }
2206 if (what & layer_state_t::eMatrixChanged) {
2207 if (layer->setMatrix(s.matrix))
2208 flags |= eTraversalNeeded;
2209 }
2210 if (what & layer_state_t::eTransparentRegionChanged) {
2211 if (layer->setTransparentRegionHint(s.transparentRegion))
2212 flags |= eTraversalNeeded;
2213 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08002214 if ((what & layer_state_t::eVisibilityChanged) ||
2215 (what & layer_state_t::eOpacityChanged)) {
2216 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07002217 if (layer->setFlags(s.flags, s.mask))
2218 flags |= eTraversalNeeded;
2219 }
2220 if (what & layer_state_t::eCropChanged) {
2221 if (layer->setCrop(s.crop))
2222 flags |= eTraversalNeeded;
2223 }
2224 if (what & layer_state_t::eLayerStackChanged) {
2225 // NOTE: index needs to be calculated before we update the state
2226 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2227 if (layer->setLayerStack(s.layerStack)) {
2228 mCurrentState.layersSortedByZ.removeAt(idx);
2229 mCurrentState.layersSortedByZ.add(layer);
2230 // we need traversal (state changed)
2231 // AND transaction (list changed)
2232 flags |= eTransactionNeeded|eTraversalNeeded;
2233 }
2234 }
2235 }
2236 return flags;
2237}
2238
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002239status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002240 const String8& name,
2241 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002242 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2243 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002244{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002245 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002246 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002247 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002248 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002249 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002250 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002251
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002252 status_t result = NO_ERROR;
2253
2254 sp<Layer> layer;
2255
Mathias Agopian3165cc22012-08-08 19:42:09 -07002256 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2257 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002258 result = createNormalLayer(client,
2259 name, w, h, flags, format,
2260 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002261 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002262 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002263 result = createDimLayer(client,
2264 name, w, h, flags,
2265 handle, gbp, &layer);
2266 break;
2267 default:
2268 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002269 break;
2270 }
2271
Dan Stozae7f8dde2015-04-30 13:29:25 -07002272 if (result != NO_ERROR) {
2273 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002274 }
Dan Stozae7f8dde2015-04-30 13:29:25 -07002275
2276 result = addClientLayer(client, *handle, *gbp, layer);
2277 if (result != NO_ERROR) {
2278 return result;
2279 }
2280
2281 setTransactionFlags(eTransactionNeeded);
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002282 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002283}
2284
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002285status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2286 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2287 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002288{
2289 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002290 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002291 case PIXEL_FORMAT_TRANSPARENT:
2292 case PIXEL_FORMAT_TRANSLUCENT:
2293 format = PIXEL_FORMAT_RGBA_8888;
2294 break;
2295 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002296 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002297 break;
2298 }
2299
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002300 *outLayer = new Layer(this, client, name, w, h, flags);
2301 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2302 if (err == NO_ERROR) {
2303 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002304 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002305 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002306
2307 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2308 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002309}
2310
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002311status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2312 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2313 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002314{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002315 *outLayer = new LayerDim(this, client, name, w, h, flags);
2316 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002317 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002318 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002319}
2320
Mathias Agopianac9fa422013-02-11 16:40:36 -08002321status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002322{
Mathias Agopian67106042013-03-14 19:18:13 -07002323 // called by the window manager when it wants to remove a Layer
2324 status_t err = NO_ERROR;
2325 sp<Layer> l(client->getLayerUser(handle));
2326 if (l != NULL) {
2327 err = removeLayer(l);
2328 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2329 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002330 }
2331 return err;
2332}
2333
Mathias Agopian13127d82013-03-05 17:47:11 -08002334status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002335{
Mathias Agopian67106042013-03-14 19:18:13 -07002336 // called by ~LayerCleaner() when all references to the IBinder (handle)
2337 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002338 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002339 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002340 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002341 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002342 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002343 "error removing layer=%p (%s)", l.get(), strerror(-err));
2344 }
2345 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002346}
2347
Mathias Agopianb60314a2012-04-10 22:09:54 -07002348// ---------------------------------------------------------------------------
2349
Andy McFadden13a082e2012-08-24 10:16:42 -07002350void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002351 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002352 Vector<ComposerState> state;
2353 Vector<DisplayState> displays;
2354 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002355 d.what = DisplayState::eDisplayProjectionChanged |
2356 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002357 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002358 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002359 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002360 d.frame.makeInvalid();
2361 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002362 d.width = 0;
2363 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002364 displays.add(d);
2365 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002366 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002367
2368 const nsecs_t period =
2369 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2370 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002371}
2372
2373void SurfaceFlinger::initializeDisplays() {
2374 class MessageScreenInitialized : public MessageBase {
2375 SurfaceFlinger* flinger;
2376 public:
2377 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2378 virtual bool handler() {
2379 flinger->onInitializeDisplays();
2380 return true;
2381 }
2382 };
2383 sp<MessageBase> msg = new MessageScreenInitialized(this);
2384 postMessageAsync(msg); // we may be called from main thread, use async message
2385}
2386
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002387void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2388 int mode) {
2389 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2390 this);
2391 int32_t type = hw->getDisplayType();
2392 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002393
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002394 if (mode == currentMode) {
2395 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002396 return;
2397 }
2398
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002399 hw->setPowerMode(mode);
2400 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2401 ALOGW("Trying to set power mode for virtual display");
2402 return;
2403 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002404
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002405 if (currentMode == HWC_POWER_MODE_OFF) {
2406 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002407 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2408 // FIXME: eventthread only knows about the main display right now
2409 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002410 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002411 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002412
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002413 mVisibleRegionsDirty = true;
2414 repaintEverything();
2415 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002416 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002417 disableHardwareVsync(true); // also cancels any in-progress resync
2418
Mathias Agopiancde87a32012-09-13 14:09:01 -07002419 // FIXME: eventthread only knows about the main display right now
2420 mEventThread->onScreenReleased();
2421 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002422
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002423 getHwComposer().setPowerMode(type, mode);
2424 mVisibleRegionsDirty = true;
2425 // from this point on, SF will stop drawing on this display
2426 } else {
2427 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002428 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002429}
2430
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002431void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2432 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002433 SurfaceFlinger& mFlinger;
2434 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002435 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002436 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002437 MessageSetPowerMode(SurfaceFlinger& flinger,
2438 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2439 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002440 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002441 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002442 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002443 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002444 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002445 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002446 ALOGW("Attempt to set power mode = %d for virtual display",
2447 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002448 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002449 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002450 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002451 return true;
2452 }
2453 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002454 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002455 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002456}
2457
2458// ---------------------------------------------------------------------------
2459
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002460status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2461{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002462 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002463
Mathias Agopianbd115332013-04-18 16:41:04 -07002464 IPCThreadState* ipc = IPCThreadState::self();
2465 const int pid = ipc->getCallingPid();
2466 const int uid = ipc->getCallingUid();
2467 if ((uid != AID_SHELL) &&
2468 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002469 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002470 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002471 } else {
Jesse Hallfcd15b42014-12-23 13:57:23 -08002472 // Try to get the main lock, but give up after one second
Mathias Agopian9795c422009-08-26 16:36:26 -07002473 // (this would indicate SF is stuck, but we want to be able to
2474 // print something in dumpsys).
Jesse Hallfcd15b42014-12-23 13:57:23 -08002475 status_t err = mStateLock.timedLock(s2ns(1));
2476 bool locked = (err == NO_ERROR);
Mathias Agopian9795c422009-08-26 16:36:26 -07002477 if (!locked) {
Jesse Hallfcd15b42014-12-23 13:57:23 -08002478 result.appendFormat(
2479 "SurfaceFlinger appears to be unresponsive (%s [%d]), "
2480 "dumping anyways (no locks held)\n", strerror(-err), err);
Mathias Agopian9795c422009-08-26 16:36:26 -07002481 }
2482
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002483 bool dumpAll = true;
2484 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002485 size_t numArgs = args.size();
2486 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002487 if ((index < numArgs) &&
2488 (args[index] == String16("--list"))) {
2489 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002490 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002491 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002492 }
2493
2494 if ((index < numArgs) &&
2495 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002496 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002497 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002498 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002499 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002500
2501 if ((index < numArgs) &&
2502 (args[index] == String16("--latency-clear"))) {
2503 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002504 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002505 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002506 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002507
2508 if ((index < numArgs) &&
2509 (args[index] == String16("--dispsync"))) {
2510 index++;
2511 mPrimaryDispSync.dump(result);
2512 dumpAll = false;
2513 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002514 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002515
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002516 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002517 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002518 }
2519
Mathias Agopian9795c422009-08-26 16:36:26 -07002520 if (locked) {
2521 mStateLock.unlock();
2522 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002523 }
2524 write(fd, result.string(), result.size());
2525 return NO_ERROR;
2526}
2527
Dan Stozac7014012014-02-14 15:03:43 -08002528void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2529 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002530{
2531 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2532 const size_t count = currentLayers.size();
2533 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002534 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002535 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002536 }
2537}
2538
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002539void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002540 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002541{
2542 String8 name;
2543 if (index < args.size()) {
2544 name = String8(args[index]);
2545 index++;
2546 }
2547
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002548 const nsecs_t period =
2549 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002550 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002551
2552 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002553 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002554 } else {
2555 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2556 const size_t count = currentLayers.size();
2557 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002558 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002559 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002560 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002561 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002562 }
2563 }
2564}
2565
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002566void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002567 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002568{
2569 String8 name;
2570 if (index < args.size()) {
2571 name = String8(args[index]);
2572 index++;
2573 }
2574
2575 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2576 const size_t count = currentLayers.size();
2577 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002578 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002579 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002580 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002581 }
2582 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002583
Svetoslavd85084b2014-03-20 10:28:31 -07002584 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002585}
2586
Jamie Gennis6547ff42013-07-16 20:12:42 -07002587// This should only be called from the main thread. Otherwise it would need
2588// the lock and should use mCurrentState rather than mDrawingState.
2589void SurfaceFlinger::logFrameStats() {
2590 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2591 const size_t count = drawingLayers.size();
2592 for (size_t i=0 ; i<count ; i++) {
2593 const sp<Layer>& layer(drawingLayers[i]);
2594 layer->logFrameStats();
2595 }
2596
2597 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2598}
2599
Andy McFadden4803b742012-09-24 19:07:20 -07002600/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2601{
2602 static const char* config =
2603 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002604#ifdef HAS_CONTEXT_PRIORITY
2605 " HAS_CONTEXT_PRIORITY"
2606#endif
2607#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2608 " NEVER_DEFAULT_TO_ASYNC_MODE"
2609#endif
2610#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2611 " TARGET_DISABLE_TRIPLE_BUFFERING"
2612#endif
2613 "]";
2614 result.append(config);
2615}
2616
Mathias Agopian74d211a2013-04-22 16:55:35 +02002617void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2618 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002619{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002620 bool colorize = false;
2621 if (index < args.size()
2622 && (args[index] == String16("--color"))) {
2623 colorize = true;
2624 index++;
2625 }
2626
2627 Colorizer colorizer(colorize);
2628
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002629 // figure out if we're stuck somewhere
2630 const nsecs_t now = systemTime();
2631 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2632 const nsecs_t inTransaction(mDebugInTransaction);
2633 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2634 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2635
2636 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002637 * Dump library configuration.
2638 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002639
2640 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002641 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002642 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002643 appendSfConfigString(result);
2644 appendUiConfigString(result);
2645 appendGuiConfigString(result);
2646 result.append("\n");
2647
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002648 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002649 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002650 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002651 result.append(SyncFeatures::getInstance().toString());
2652 result.append("\n");
2653
Andy McFadden41d67d72014-04-25 16:58:34 -07002654 colorizer.bold(result);
2655 result.append("DispSync configuration: ");
2656 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002657 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2658 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002659 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2660 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2661 result.append("\n");
2662
Andy McFadden4803b742012-09-24 19:07:20 -07002663 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002664 * Dump the visible layer list
2665 */
2666 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2667 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002668 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002669 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002670 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002671 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002672 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002673 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002674 }
2675
2676 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002677 * Dump Display state
2678 */
2679
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002680 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002681 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002682 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002683 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2684 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002685 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002686 }
2687
2688 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002689 * Dump SurfaceFlinger global state
2690 */
2691
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002692 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002693 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002694 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002695
Mathias Agopian888c8222012-08-04 21:10:38 -07002696 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002697 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002698
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002699 colorizer.bold(result);
2700 result.appendFormat("EGL implementation : %s\n",
2701 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2702 colorizer.reset(result);
2703 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002704 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002705
Mathias Agopian875d8e12013-06-07 15:35:48 -07002706 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002707
Mathias Agopian42977342012-08-05 00:40:46 -07002708 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002709 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2710 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002711 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002712 " last eglSwapBuffers() time: %f us\n"
2713 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002714 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002715 " refresh-rate : %f fps\n"
2716 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002717 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002718 " gpu_to_cpu_unsupported : %d\n"
2719 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002720 mLastSwapBufferTime/1000.0,
2721 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002722 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002723 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2724 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002725 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002726 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002727
Mathias Agopian74d211a2013-04-22 16:55:35 +02002728 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002729 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002730
Mathias Agopian74d211a2013-04-22 16:55:35 +02002731 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002732 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002733
2734 /*
2735 * VSYNC state
2736 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002737 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002738
2739 /*
2740 * Dump HWComposer state
2741 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002742 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002743 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002744 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002745 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002746 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002747 (mDebugDisableHWC || mDebugRegion || mDaltonize
2748 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002749 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002750
2751 /*
2752 * Dump gralloc state
2753 */
2754 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2755 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002756}
2757
Mathias Agopian13127d82013-03-05 17:47:11 -08002758const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002759SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002760 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002761 wp<IBinder> dpy;
2762 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2763 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2764 dpy = mDisplays.keyAt(i);
2765 break;
2766 }
2767 }
2768 if (dpy == NULL) {
2769 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2770 // Just use the primary display so we have something to return
2771 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2772 }
2773 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002774}
2775
Keun young Park63f165f2012-08-31 10:53:36 -07002776bool SurfaceFlinger::startDdmConnection()
2777{
2778 void* libddmconnection_dso =
2779 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2780 if (!libddmconnection_dso) {
2781 return false;
2782 }
2783 void (*DdmConnection_start)(const char* name);
2784 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07002785 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07002786 if (!DdmConnection_start) {
2787 dlclose(libddmconnection_dso);
2788 return false;
2789 }
2790 (*DdmConnection_start)(getServiceName());
2791 return true;
2792}
2793
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002794status_t SurfaceFlinger::onTransact(
2795 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2796{
2797 switch (code) {
2798 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002799 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002800 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002801 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002802 case CLEAR_ANIMATION_FRAME_STATS:
2803 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002804 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002805 {
2806 // codes that require permission check
2807 IPCThreadState* ipc = IPCThreadState::self();
2808 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002809 const int uid = ipc->getCallingUid();
Jeff Brown8b047252015-04-10 20:20:13 -07002810 if ((uid != AID_GRAPHICS && uid != AID_SYSTEM) &&
Mathias Agopian99b49842011-06-27 16:05:52 -07002811 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002812 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002813 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2814 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002815 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002816 break;
2817 }
2818 case CAPTURE_SCREEN:
2819 {
2820 // codes that require permission check
2821 IPCThreadState* ipc = IPCThreadState::self();
2822 const int pid = ipc->getCallingPid();
2823 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002824 if ((uid != AID_GRAPHICS) &&
2825 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002826 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002827 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2828 return PERMISSION_DENIED;
2829 }
2830 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002831 }
2832 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002833
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002834 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2835 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002836 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002837 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002838 IPCThreadState* ipc = IPCThreadState::self();
2839 const int pid = ipc->getCallingPid();
2840 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002841 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002842 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002843 return PERMISSION_DENIED;
2844 }
2845 int n;
2846 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002847 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002848 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002849 return NO_ERROR;
2850 case 1002: // SHOW_UPDATES
2851 n = data.readInt32();
2852 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002853 invalidateHwcGeometry();
2854 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002855 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002856 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002857 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002858 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002859 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002860 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002861 setTransactionFlags(
2862 eTransactionNeeded|
2863 eDisplayTransactionNeeded|
2864 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002865 return NO_ERROR;
2866 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002867 case 1006:{ // send empty update
2868 signalRefresh();
2869 return NO_ERROR;
2870 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002871 case 1008: // toggle use of hw composer
2872 n = data.readInt32();
2873 mDebugDisableHWC = n ? 1 : 0;
2874 invalidateHwcGeometry();
2875 repaintEverything();
2876 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002877 case 1009: // toggle use of transform hint
2878 n = data.readInt32();
2879 mDebugDisableTransformHint = n ? 1 : 0;
2880 invalidateHwcGeometry();
2881 repaintEverything();
2882 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002883 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002884 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002885 reply->writeInt32(0);
2886 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002887 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002888 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002889 return NO_ERROR;
2890 case 1013: {
2891 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002892 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2893 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002894 return NO_ERROR;
2895 }
2896 case 1014: {
2897 // daltonize
2898 n = data.readInt32();
2899 switch (n % 10) {
2900 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2901 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2902 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2903 }
2904 if (n >= 10) {
2905 mDaltonizer.setMode(Daltonizer::correction);
2906 } else {
2907 mDaltonizer.setMode(Daltonizer::simulation);
2908 }
2909 mDaltonize = n > 0;
2910 invalidateHwcGeometry();
2911 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002912 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002913 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002914 case 1015: {
2915 // apply a color matrix
2916 n = data.readInt32();
2917 mHasColorMatrix = n ? 1 : 0;
2918 if (n) {
2919 // color matrix is sent as mat3 matrix followed by vec3
2920 // offset, then packed into a mat4 where the last row is
2921 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002922 for (size_t i = 0 ; i < 4; i++) {
2923 for (size_t j = 0; j < 4; j++) {
2924 mColorMatrix[i][j] = data.readFloat();
2925 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002926 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002927 } else {
2928 mColorMatrix = mat4();
2929 }
2930 invalidateHwcGeometry();
2931 repaintEverything();
2932 return NO_ERROR;
2933 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002934 // This is an experimental interface
2935 // Needs to be shifted to proper binder interface when we productize
2936 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07002937 n = data.readInt32();
2938 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002939 return NO_ERROR;
2940 }
Dan Stozaee44edd2015-03-23 15:50:23 -07002941 case 1017: {
2942 n = data.readInt32();
2943 mForceFullDamage = static_cast<bool>(n);
2944 return NO_ERROR;
2945 }
Dan Stozadb4ac3c2015-04-14 11:34:01 -07002946 case 1018: { // Modify Choreographer's phase offset
2947 n = data.readInt32();
2948 mEventThread->setPhaseOffset(static_cast<nsecs_t>(n));
2949 return NO_ERROR;
2950 }
2951 case 1019: { // Modify SurfaceFlinger's phase offset
2952 n = data.readInt32();
2953 mSFEventThread->setPhaseOffset(static_cast<nsecs_t>(n));
2954 return NO_ERROR;
2955 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002956 }
2957 }
2958 return err;
2959}
2960
Mathias Agopian53331da2011-08-22 21:44:41 -07002961void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002962 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002963 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002964}
2965
Mathias Agopian59119e62010-10-11 12:37:43 -07002966// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002967// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002968// ---------------------------------------------------------------------------
2969
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002970/* The code below is here to handle b/8734824
2971 *
2972 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07002973 * from the surfaceflinger thread to the calling binder thread, where they
2974 * are executed. This allows the calling thread in the calling process to be
2975 * reused and not depend on having "enough" binder threads to handle the
2976 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002977 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002978class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07002979 /* Parts of GraphicProducerWrapper are run on two different threads,
2980 * communicating by sending messages via Looper but also by shared member
2981 * data. Coherence maintenance is subtle and in places implicit (ugh).
2982 *
2983 * Don't rely on Looper's sendMessage/handleMessage providing
2984 * release/acquire semantics for any data not actually in the Message.
2985 * Data going from surfaceflinger to binder threads needs to be
2986 * synchronized explicitly.
2987 *
2988 * Barrier open/wait do provide release/acquire semantics. This provides
2989 * implicit synchronization for data coming back from binder to
2990 * surfaceflinger threads.
2991 */
2992
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002993 sp<IGraphicBufferProducer> impl;
2994 sp<Looper> looper;
2995 status_t result;
2996 bool exitPending;
2997 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07002998 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002999 uint32_t code;
3000 Parcel const* data;
3001 Parcel* reply;
3002
3003 enum {
3004 MSG_API_CALL,
3005 MSG_EXIT
3006 };
3007
3008 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003009 * Called on surfaceflinger thread. This is called by our "fake"
3010 * BpGraphicBufferProducer. We package the data and reply Parcel and
3011 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003012 */
3013 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08003014 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003015 this->code = code;
3016 this->data = &data;
3017 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003018 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07003019 // if we've exited, we run the message synchronously right here.
3020 // note (JH): as far as I can tell from looking at the code, this
3021 // never actually happens. if it does, i'm not sure if it happens
3022 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003023 handleMessage(Message(MSG_API_CALL));
3024 } else {
3025 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07003026 // Prevent stores to this->{code, data, reply} from being
3027 // reordered later than the construction of Message.
3028 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003029 looper->sendMessage(this, Message(MSG_API_CALL));
3030 barrier.wait();
3031 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08003032 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003033 }
3034
3035 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003036 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003037 * call the real BpGraphicBufferProducer.
3038 */
3039 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07003040 int what = message.what;
3041 // Prevent reads below from happening before the read from Message
3042 atomic_thread_fence(memory_order_acquire);
3043 if (what == MSG_API_CALL) {
Marco Nelissen097ca272014-11-14 08:01:01 -08003044 result = IInterface::asBinder(impl)->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003045 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07003046 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003047 exitRequested = true;
3048 }
3049 }
3050
3051public:
Jesse Hallb154c422014-07-13 12:47:02 -07003052 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
3053 : impl(impl),
3054 looper(new Looper(true)),
3055 exitPending(false),
3056 exitRequested(false)
3057 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003058
Jesse Hallb154c422014-07-13 12:47:02 -07003059 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003060 status_t waitForResponse() {
3061 do {
3062 looper->pollOnce(-1);
3063 } while (!exitRequested);
3064 return result;
3065 }
3066
Jesse Hallb154c422014-07-13 12:47:02 -07003067 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003068 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07003069 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003070 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07003071 // Ensure this->result is visible to the binder thread before it
3072 // handles the message.
3073 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003074 looper->sendMessage(this, Message(MSG_EXIT));
3075 }
3076};
3077
3078
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003079status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
3080 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003081 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003082 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003083 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003084
3085 if (CC_UNLIKELY(display == 0))
3086 return BAD_VALUE;
3087
3088 if (CC_UNLIKELY(producer == 0))
3089 return BAD_VALUE;
3090
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003091 // if we have secure windows on this display, never allow the screen capture
3092 // unless the producer interface is local (i.e.: we can take a screenshot for
3093 // ourselves).
Marco Nelissen097ca272014-11-14 08:01:01 -08003094 if (!IInterface::asBinder(producer)->localBinder()) {
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003095 Mutex::Autolock _l(mStateLock);
3096 sp<const DisplayDevice> hw(getDisplayDevice(display));
3097 if (hw->getSecureLayerVisible()) {
3098 ALOGW("FB is protected: PERMISSION_DENIED");
3099 return PERMISSION_DENIED;
3100 }
3101 }
3102
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003103 // Convert to surfaceflinger's internal rotation type.
3104 Transform::orientation_flags rotationFlags;
3105 switch (rotation) {
3106 case ISurfaceComposer::eRotateNone:
3107 rotationFlags = Transform::ROT_0;
3108 break;
3109 case ISurfaceComposer::eRotate90:
3110 rotationFlags = Transform::ROT_90;
3111 break;
3112 case ISurfaceComposer::eRotate180:
3113 rotationFlags = Transform::ROT_180;
3114 break;
3115 case ISurfaceComposer::eRotate270:
3116 rotationFlags = Transform::ROT_270;
3117 break;
3118 default:
3119 rotationFlags = Transform::ROT_0;
3120 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3121 break;
3122 }
3123
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003124 class MessageCaptureScreen : public MessageBase {
3125 SurfaceFlinger* flinger;
3126 sp<IBinder> display;
3127 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003128 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003129 uint32_t reqWidth, reqHeight;
3130 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003131 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003132 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003133 status_t result;
3134 public:
3135 MessageCaptureScreen(SurfaceFlinger* flinger,
3136 const sp<IBinder>& display,
3137 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003138 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003139 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003140 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003141 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003142 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003143 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003144 useIdentityTransform(useIdentityTransform),
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003145 rotation(rotation),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003146 result(PERMISSION_DENIED)
3147 {
3148 }
3149 status_t getResult() const {
3150 return result;
3151 }
3152 virtual bool handler() {
3153 Mutex::Autolock _l(flinger->mStateLock);
3154 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003155 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003156 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003157 useIdentityTransform, rotation);
Marco Nelissen097ca272014-11-14 08:01:01 -08003158 static_cast<GraphicProducerWrapper*>(IInterface::asBinder(producer).get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003159 return true;
3160 }
3161 };
3162
Mathias Agopian9eb1f052013-04-10 16:27:17 -07003163 // make sure to process transactions before screenshots -- a transaction
3164 // might already be pending but scheduled for VSYNC; this guarantees we
3165 // will handle it before the screenshot. When VSYNC finally arrives
3166 // the scheduled transaction will be a no-op. If no transactions are
3167 // scheduled at this time, this will end-up being a no-op as well.
3168 mEventQueue.invalidateTransactionNow();
3169
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003170 // this creates a "fake" BBinder which will serve as a "fake" remote
3171 // binder to receive the marshaled calls and forward them to the
3172 // real remote (a BpGraphicBufferProducer)
3173 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3174
3175 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3176 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003177 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003178 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003179 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003180 useIdentityTransform, rotationFlags);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003181
3182 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003183 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003184 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003185 }
3186 return res;
3187}
3188
Mathias Agopian180f10d2013-04-10 22:55:41 -07003189
3190void SurfaceFlinger::renderScreenImplLocked(
3191 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003192 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003193 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003194 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003195{
3196 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003197 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003198
3199 // get screen geometry
Andreas Gampe89fd4f72014-11-13 14:18:56 -08003200 const int32_t hw_w = hw->getWidth();
3201 const int32_t hw_h = hw->getHeight();
3202 const bool filtering = static_cast<int32_t>(reqWidth) != hw_w ||
Christopher Ferris0e749792015-03-23 14:32:15 -07003203 static_cast<int32_t>(reqHeight) != hw_h;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003204
Dan Stozac1879002014-05-22 15:59:05 -07003205 // if a default or invalid sourceCrop is passed in, set reasonable values
3206 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3207 !sourceCrop.isValid()) {
3208 sourceCrop.setLeftTop(Point(0, 0));
3209 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3210 }
3211
3212 // ensure that sourceCrop is inside screen
3213 if (sourceCrop.left < 0) {
3214 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3215 }
Dan Stozabe31f442014-06-11 11:20:54 -07003216 if (sourceCrop.right > hw_w) {
3217 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003218 }
3219 if (sourceCrop.top < 0) {
3220 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3221 }
Dan Stozabe31f442014-06-11 11:20:54 -07003222 if (sourceCrop.bottom > hw_h) {
3223 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003224 }
3225
Mathias Agopian180f10d2013-04-10 22:55:41 -07003226 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003227 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003228
3229 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003230 engine.setViewportAndProjection(
3231 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003232 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003233
3234 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003235 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003236
3237 const LayerVector& layers( mDrawingState.layersSortedByZ );
3238 const size_t count = layers.size();
3239 for (size_t i=0 ; i<count ; ++i) {
3240 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003241 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003242 if (state.layerStack == hw->getLayerStack()) {
3243 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3244 if (layer->isVisible()) {
3245 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08003246 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003247 if (filtering) layer->setFiltering(false);
3248 }
3249 }
3250 }
3251 }
3252
3253 // compositionComplete is needed for older driver
3254 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003255 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003256}
3257
3258
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003259status_t SurfaceFlinger::captureScreenImplLocked(
3260 const sp<const DisplayDevice>& hw,
3261 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003262 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003263 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003264 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003265{
3266 ATRACE_CALL();
3267
Mathias Agopian180f10d2013-04-10 22:55:41 -07003268 // get screen geometry
Dan Stoza65dc7ee2015-06-09 16:44:40 -07003269 uint32_t hw_w = hw->getWidth();
3270 uint32_t hw_h = hw->getHeight();
3271
3272 if (rotation & Transform::ROT_90) {
3273 std::swap(hw_w, hw_h);
3274 }
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003275
Mathias Agopian180f10d2013-04-10 22:55:41 -07003276 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3277 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3278 reqWidth, reqHeight, hw_w, hw_h);
3279 return BAD_VALUE;
3280 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003281
Mathias Agopian180f10d2013-04-10 22:55:41 -07003282 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3283 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3284
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003285 // create a surface (because we're a producer, and we need to
3286 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003287 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003288 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003289
Michael Lentineb64d8752015-05-21 13:48:24 -07003290 status_t result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
3291 if (result == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003292 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3293 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003294
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003295 int err = 0;
3296 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003297 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003298 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3299 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003300
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003301 if (err == NO_ERROR) {
3302 ANativeWindowBuffer* buffer;
3303 /* TODO: Once we have the sync framework everywhere this can use
3304 * server-side waits on the fence that dequeueBuffer returns.
3305 */
3306 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3307 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003308 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003309 // create an EGLImage from the buffer so we can later
3310 // turn it into a texture
3311 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3312 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3313 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003314 // this binds the given EGLImage as a framebuffer for the
3315 // duration of this scope.
3316 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3317 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003318 // this will in fact render into our dequeued buffer
3319 // via an FBO, which means we didn't have to create
3320 // an EGLSurface and therefore we're not
3321 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003322 renderScreenImplLocked(
3323 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3324 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003325
Riley Andrews86639902014-08-15 12:27:24 -07003326 // Attempt to create a sync khr object that can produce a sync point. If that
3327 // isn't available, create a non-dupable sync object in the fallback path and
3328 // wait on it directly.
3329 EGLSyncKHR sync;
3330 if (!DEBUG_SCREENSHOTS) {
3331 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Riley Andrews9707f4d2014-10-23 16:17:04 -07003332 // native fence fd will not be populated until flush() is done.
3333 getRenderEngine().flush();
Andy McFadden2d8d1202013-10-09 16:38:02 -07003334 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003335 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003336 }
Riley Andrews86639902014-08-15 12:27:24 -07003337 if (sync != EGL_NO_SYNC_KHR) {
3338 // get the sync fd
3339 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3340 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3341 ALOGW("captureScreen: failed to dup sync khr object");
3342 syncFd = -1;
3343 }
3344 eglDestroySyncKHR(mEGLDisplay, sync);
3345 } else {
3346 // fallback path
3347 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3348 if (sync != EGL_NO_SYNC_KHR) {
3349 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3350 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3351 EGLint eglErr = eglGetError();
3352 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3353 ALOGW("captureScreen: fence wait timed out");
3354 } else {
3355 ALOGW_IF(eglErr != EGL_SUCCESS,
3356 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3357 }
3358 eglDestroySyncKHR(mEGLDisplay, sync);
3359 } else {
3360 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3361 }
3362 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003363 if (DEBUG_SCREENSHOTS) {
3364 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3365 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3366 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3367 hw, minLayerZ, maxLayerZ);
3368 delete [] pixels;
3369 }
3370
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003371 } else {
3372 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3373 result = INVALID_OPERATION;
3374 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003375 // destroy our image
3376 eglDestroyImageKHR(mEGLDisplay, image);
3377 } else {
3378 result = BAD_VALUE;
3379 }
Jesse Hallafe2b1f2014-10-21 11:09:17 -07003380 // queueBuffer takes ownership of syncFd
Michael Lentineb64d8752015-05-21 13:48:24 -07003381 result = window->queueBuffer(window, buffer, syncFd);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003382 }
3383 } else {
3384 result = BAD_VALUE;
3385 }
3386 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3387 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003388
Mathias Agopian74c40c02010-09-29 13:02:36 -07003389 return result;
3390}
3391
Mathias Agopiand5556842013-09-19 17:08:37 -07003392void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3393 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003394 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003395 for (size_t y=0 ; y<h ; y++) {
3396 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3397 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003398 if (p[x] != 0xFF000000) return;
3399 }
3400 }
3401 ALOGE("*** we just took a black screenshot ***\n"
3402 "requested minz=%d, maxz=%d, layerStack=%d",
3403 minLayerZ, maxLayerZ, hw->getLayerStack());
3404 const LayerVector& layers( mDrawingState.layersSortedByZ );
3405 const size_t count = layers.size();
3406 for (size_t i=0 ; i<count ; ++i) {
3407 const sp<Layer>& layer(layers[i]);
3408 const Layer::State& state(layer->getDrawingState());
3409 const bool visible = (state.layerStack == hw->getLayerStack())
3410 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3411 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003412 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003413 visible ? '+' : '-',
3414 i, layer->getName().string(), state.layerStack, state.z,
3415 layer->isVisible(), state.flags, state.alpha);
3416 }
3417 }
3418}
3419
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003420// ---------------------------------------------------------------------------
3421
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003422SurfaceFlinger::LayerVector::LayerVector() {
3423}
3424
3425SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003426 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003427}
3428
3429int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3430 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003431{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003432 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003433 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3434 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003435
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003436 uint32_t ls = l->getCurrentState().layerStack;
3437 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003438 if (ls != rs)
3439 return ls - rs;
3440
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003441 uint32_t lz = l->getCurrentState().z;
3442 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003443 if (lz != rz)
3444 return lz - rz;
3445
3446 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003447}
3448
3449// ---------------------------------------------------------------------------
3450
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003451SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Michael Lentine47e45402014-07-18 15:34:25 -07003452 : type(DisplayDevice::DISPLAY_ID_INVALID), width(0), height(0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003453}
3454
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003455SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Michael Lentine47e45402014-07-18 15:34:25 -07003456 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0), width(0), height(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003457 viewport.makeInvalid();
3458 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003459}
3460
3461// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003462
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003463}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003464
3465
3466#if defined(__gl_h_)
3467#error "don't include gl/gl.h in this file"
3468#endif
3469
3470#if defined(__gl2_h_)
3471#error "don't include gl2/gl2.h in this file"
3472#endif