blob: 14195577e02c6277eeaadd7e389bcb832406eb36 [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),
Dan Stozab90cf072015-03-05 11:05:59 -0800152 mHasColorMatrix(false),
153 mHasPoweredOff(false),
154 mFrameBuckets(),
155 mTotalTime(0),
156 mLastSwapTime(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157{
Steve Blocka19954a2012-01-04 20:05:49 +0000158 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159
160 // debugging stuff...
161 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700162
Mathias Agopianb4b17302013-03-20 18:36:41 -0700163 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700164 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700165
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 property_get("debug.sf.showupdates", value, "0");
167 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700168
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700169 property_get("debug.sf.ddms", value, "0");
170 mDebugDDMS = atoi(value);
171 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700172 if (!startDdmConnection()) {
173 // start failed, and DDMS debugging not enabled
174 mDebugDDMS = 0;
175 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700176 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700177 ALOGI_IF(mDebugRegion, "showupdates enabled");
178 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179}
180
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800181void SurfaceFlinger::onFirstRef()
182{
183 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800184}
185
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186SurfaceFlinger::~SurfaceFlinger()
187{
Mathias Agopiana4912602012-07-12 14:25:33 -0700188 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
189 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
190 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191}
192
Dan Stozac7014012014-02-14 15:03:43 -0800193void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800194{
195 // the window manager died on us. prepare its eulogy.
196
Andy McFadden13a082e2012-08-24 10:16:42 -0700197 // restore initial conditions (default device unblank, etc)
198 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800199
200 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700201 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800202}
203
Mathias Agopian7e27f052010-05-28 14:22:23 -0700204sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205{
Mathias Agopian96f08192010-06-02 23:28:45 -0700206 sp<ISurfaceComposerClient> bclient;
207 sp<Client> client(new Client(this));
208 status_t err = client->initCheck();
209 if (err == NO_ERROR) {
210 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 return bclient;
213}
214
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700215sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
216 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700217{
218 class DisplayToken : public BBinder {
219 sp<SurfaceFlinger> flinger;
220 virtual ~DisplayToken() {
221 // no more references, this display must be terminated
222 Mutex::Autolock _l(flinger->mStateLock);
223 flinger->mCurrentState.displays.removeItem(this);
224 flinger->setTransactionFlags(eDisplayTransactionNeeded);
225 }
226 public:
227 DisplayToken(const sp<SurfaceFlinger>& flinger)
228 : flinger(flinger) {
229 }
230 };
231
232 sp<BBinder> token = new DisplayToken(this);
233
234 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700235 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700236 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700237 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700238 mCurrentState.displays.add(token, info);
239
240 return token;
241}
242
Jesse Hall6c913be2013-08-08 12:15:49 -0700243void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
244 Mutex::Autolock _l(mStateLock);
245
246 ssize_t idx = mCurrentState.displays.indexOfKey(display);
247 if (idx < 0) {
248 ALOGW("destroyDisplay: invalid display token");
249 return;
250 }
251
252 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
253 if (!info.isVirtualDisplay()) {
254 ALOGE("destroyDisplay called for non-virtual display");
255 return;
256 }
257
258 mCurrentState.displays.removeItemsAt(idx);
259 setTransactionFlags(eDisplayTransactionNeeded);
260}
261
Jesse Hall692c7232012-11-08 15:41:56 -0800262void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
263 ALOGW_IF(mBuiltinDisplays[type],
264 "Overwriting display token for display type %d", type);
265 mBuiltinDisplays[type] = new BBinder();
266 DisplayDeviceState info(type);
267 // All non-virtual displays are currently considered secure.
268 info.isSecure = true;
269 mCurrentState.displays.add(mBuiltinDisplays[type], info);
270}
271
Mathias Agopiane57f2922012-08-09 16:29:12 -0700272sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700273 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700274 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
275 return NULL;
276 }
Jesse Hall692c7232012-11-08 15:41:56 -0800277 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700278}
279
Jamie Gennis9a78c902011-01-12 18:30:40 -0800280sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
281{
282 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
283 return gba;
284}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700285
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286void SurfaceFlinger::bootFinished()
287{
288 const nsecs_t now = systemTime();
289 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000290 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700291 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700292
293 // wait patiently for the window manager death
294 const String16 name("window");
295 sp<IBinder> window(defaultServiceManager()->getService(name));
296 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700297 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700298 }
299
300 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700301 // formerly we would just kill the process, but we now ask it to exit so it
302 // can choose where to stop the animation.
303 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304}
305
Mathias Agopian3f844832013-08-07 21:24:32 -0700306void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700307 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700308 RenderEngine& engine;
309 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700310 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700311 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
312 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700313 }
314 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700315 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700316 return true;
317 }
318 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700319 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700320}
321
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700322class DispSyncSource : public VSyncSource, private DispSync::Callback {
323public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700324 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
325 const char* label) :
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700326 mValue(0),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700327 mTraceVsync(traceVsync),
Andy McFadden5167ec62014-05-22 13:08:43 -0700328 mVsyncOnLabel(String8::format("VsyncOn-%s", label)),
329 mVsyncEventLabel(String8::format("VSYNC-%s", label)),
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700330 mDispSync(dispSync),
331 mCallbackMutex(),
332 mCallback(),
333 mVsyncMutex(),
334 mPhaseOffset(phaseOffset),
335 mEnabled(false) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700336
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700337 virtual ~DispSyncSource() {}
338
339 virtual void setVSyncEnabled(bool enable) {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700340 Mutex::Autolock lock(mVsyncMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700341 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700342 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700343 static_cast<DispSync::Callback*>(this));
344 if (err != NO_ERROR) {
345 ALOGE("error registering vsync callback: %s (%d)",
346 strerror(-err), err);
347 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700348 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700349 } else {
350 status_t err = mDispSync->removeEventListener(
351 static_cast<DispSync::Callback*>(this));
352 if (err != NO_ERROR) {
353 ALOGE("error unregistering vsync callback: %s (%d)",
354 strerror(-err), err);
355 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700356 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700357 }
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700358 mEnabled = enable;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700359 }
360
361 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700362 Mutex::Autolock lock(mCallbackMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700363 mCallback = callback;
364 }
365
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700366 virtual void setPhaseOffset(nsecs_t phaseOffset) {
367 Mutex::Autolock lock(mVsyncMutex);
368
369 // Normalize phaseOffset to [0, period)
370 auto period = mDispSync->getPeriod();
371 phaseOffset %= period;
372 if (phaseOffset < 0) {
373 // If we're here, then phaseOffset is in (-period, 0). After this
374 // operation, it will be in (0, period)
375 phaseOffset += period;
376 }
377 mPhaseOffset = phaseOffset;
378
379 // If we're not enabled, we don't need to mess with the listeners
380 if (!mEnabled) {
381 return;
382 }
383
384 // Remove the listener with the old offset
385 status_t err = mDispSync->removeEventListener(
386 static_cast<DispSync::Callback*>(this));
387 if (err != NO_ERROR) {
388 ALOGE("error unregistering vsync callback: %s (%d)",
389 strerror(-err), err);
390 }
391
392 // Add a listener with the new offset
393 err = mDispSync->addEventListener(mPhaseOffset,
394 static_cast<DispSync::Callback*>(this));
395 if (err != NO_ERROR) {
396 ALOGE("error registering vsync callback: %s (%d)",
397 strerror(-err), err);
398 }
399 }
400
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700401private:
402 virtual void onDispSyncEvent(nsecs_t when) {
403 sp<VSyncSource::Callback> callback;
404 {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700405 Mutex::Autolock lock(mCallbackMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700406 callback = mCallback;
407
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700408 if (mTraceVsync) {
409 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700410 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700411 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700412 }
413
414 if (callback != NULL) {
415 callback->onVSyncEvent(when);
416 }
417 }
418
419 int mValue;
420
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700421 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700422 const String8 mVsyncOnLabel;
423 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700424
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700425 DispSync* mDispSync;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700426
427 Mutex mCallbackMutex; // Protects the following
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700428 sp<VSyncSource::Callback> mCallback;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700429
430 Mutex mVsyncMutex; // Protects the following
431 nsecs_t mPhaseOffset;
432 bool mEnabled;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700433};
434
435void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700436 ALOGI( "SurfaceFlinger's main thread ready to run. "
437 "Initializing graphics H/W...");
438
Jesse Hall692c7232012-11-08 15:41:56 -0800439 Mutex::Autolock _l(mStateLock);
440
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700441 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700442 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
443 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700444
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700445 // Initialize the H/W composer object. There may or may not be an
446 // actual hardware composer underneath.
447 mHwc = new HWComposer(this,
448 *static_cast<HWComposer::EventHandler *>(this));
449
Mathias Agopian875d8e12013-06-07 15:35:48 -0700450 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800451 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700452
453 // retrieve the EGL context that was selected/created
454 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700455
Mathias Agopianda27af92012-09-13 18:17:13 -0700456 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
457 "couldn't create EGLContext");
458
Mathias Agopiancde87a32012-09-13 14:09:01 -0700459 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700460 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700461 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700462 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700463 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700464 // All non-virtual displays are currently considered secure.
465 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800466 createBuiltinDisplayLocked(type);
467 wp<IBinder> token = mBuiltinDisplays[i];
468
Dan Stozab9b08832014-03-13 11:55:57 -0700469 sp<IGraphicBufferProducer> producer;
470 sp<IGraphicBufferConsumer> consumer;
471 BufferQueue::createBufferQueue(&producer, &consumer,
472 new GraphicBufferAlloc());
473
474 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
475 consumer);
Jesse Hall19e87292013-12-23 21:02:15 -0800476 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700477 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800478 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Dan Stozab9b08832014-03-13 11:55:57 -0700479 fbs, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -0800480 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700481 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700482 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700483 // for displays other than the main display, so we always
484 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800485 ALOGD("marking display %zu as acquired/unblanked", i);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700486 hw->setPowerMode(HWC_POWER_MODE_NORMAL);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700487 }
488 mDisplays.add(token, hw);
489 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700490 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700491
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700492 // make the GLContext current so that we can create textures when creating Layers
493 // (which may happens before we render something)
494 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
495
Mathias Agopian028508c2012-07-25 21:12:12 -0700496 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700497 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700498 vsyncPhaseOffsetNs, true, "app");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700499 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700500 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700501 sfVsyncPhaseOffsetNs, true, "sf");
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700502 mSFEventThread = new EventThread(sfVsyncSrc);
503 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700504
Jamie Gennisd1700752013-10-14 12:22:52 -0700505 mEventControlThread = new EventControlThread(this);
506 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
507
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700508 // set a fake vsync period if there is no HWComposer
509 if (mHwc->initCheck() != NO_ERROR) {
510 mPrimaryDispSync.setPeriod(16666667);
511 }
512
Mathias Agopian92a979a2012-08-02 18:32:23 -0700513 // initialize our drawing state
514 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700515
Andy McFadden13a082e2012-08-24 10:16:42 -0700516 // set initial conditions (e.g. unblank default device)
517 initializeDisplays();
518
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700519 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700520 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800521}
522
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700523int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700524 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700525 type : mHwc->allocateDisplayId();
526}
527
Mathias Agopiana67e4182012-06-19 17:26:12 -0700528void SurfaceFlinger::startBootAnim() {
529 // start boot animation
530 property_set("service.bootanim.exit", "0");
531 property_set("ctl.start", "bootanim");
532}
533
Mathias Agopian875d8e12013-06-07 15:35:48 -0700534size_t SurfaceFlinger::getMaxTextureSize() const {
535 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700536}
537
Mathias Agopian875d8e12013-06-07 15:35:48 -0700538size_t SurfaceFlinger::getMaxViewportDims() const {
539 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700540}
541
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800542// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800543
Jamie Gennis582270d2011-08-17 18:19:00 -0700544bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800545 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800546 Mutex::Autolock _l(mStateLock);
Marco Nelissen097ca272014-11-14 08:01:01 -0800547 sp<IBinder> surfaceTextureBinder(IInterface::asBinder(bufferProducer));
Mathias Agopian67106042013-03-14 19:18:13 -0700548 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800549}
550
Dan Stoza7f7da322014-05-02 15:26:25 -0700551status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
552 Vector<DisplayInfo>* configs) {
553 if (configs == NULL) {
554 return BAD_VALUE;
555 }
556
Naseer Ahmed7aa0c472014-11-03 14:49:23 -0500557 if (!display.get())
558 return NAME_NOT_FOUND;
559
Jesse Hall692c7232012-11-08 15:41:56 -0800560 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700561 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800562 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700563 type = i;
564 break;
565 }
566 }
567
568 if (type < 0) {
569 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700570 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700571
Mathias Agopian8b736f12012-08-13 17:54:26 -0700572 // TODO: Not sure if display density should handled by SF any longer
573 class Density {
574 static int getDensityFromProperty(char const* propName) {
575 char property[PROPERTY_VALUE_MAX];
576 int density = 0;
577 if (property_get(propName, property, NULL) > 0) {
578 density = atoi(property);
579 }
580 return density;
581 }
582 public:
583 static int getEmuDensity() {
584 return getDensityFromProperty("qemu.sf.lcd_density"); }
585 static int getBuildDensity() {
586 return getDensityFromProperty("ro.sf.lcd_density"); }
587 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700588
Dan Stoza7f7da322014-05-02 15:26:25 -0700589 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700590
Dan Stoza7f7da322014-05-02 15:26:25 -0700591 const Vector<HWComposer::DisplayConfig>& hwConfigs =
592 getHwComposer().getConfigs(type);
593 for (size_t c = 0; c < hwConfigs.size(); ++c) {
594 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
595 DisplayInfo info = DisplayInfo();
596
597 float xdpi = hwConfig.xdpi;
598 float ydpi = hwConfig.ydpi;
599
600 if (type == DisplayDevice::DISPLAY_PRIMARY) {
601 // The density of the device is provided by a build property
602 float density = Density::getBuildDensity() / 160.0f;
603 if (density == 0) {
604 // the build doesn't provide a density -- this is wrong!
605 // use xdpi instead
606 ALOGE("ro.sf.lcd_density must be defined as a build property");
607 density = xdpi / 160.0f;
608 }
609 if (Density::getEmuDensity()) {
610 // if "qemu.sf.lcd_density" is specified, it overrides everything
611 xdpi = ydpi = density = Density::getEmuDensity();
612 density /= 160.0f;
613 }
614 info.density = density;
615
616 // TODO: this needs to go away (currently needed only by webkit)
617 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
618 info.orientation = hw->getOrientation();
619 } else {
620 // TODO: where should this value come from?
621 static const int TV_DENSITY = 213;
622 info.density = TV_DENSITY / 160.0f;
623 info.orientation = 0;
624 }
625
626 info.w = hwConfig.width;
627 info.h = hwConfig.height;
628 info.xdpi = xdpi;
629 info.ydpi = ydpi;
630 info.fps = float(1e9 / hwConfig.refresh);
Andy McFadden91b2ca82014-06-13 14:04:23 -0700631 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
632
633 // This is how far in advance a buffer must be queued for
634 // presentation at a given time. If you want a buffer to appear
635 // on the screen at time N, you must submit the buffer before
636 // (N - presentationDeadline).
637 //
638 // Normally it's one full refresh period (to give SF a chance to
639 // latch the buffer), but this can be reduced by configuring a
640 // DispSync offset. Any additional delays introduced by the hardware
641 // composer or panel must be accounted for here.
642 //
643 // We add an additional 1ms to allow for processing time and
644 // differences between the ideal and actual refresh rate.
645 info.presentationDeadline =
646 hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700647
648 // All non-virtual displays are currently considered secure.
649 info.secure = true;
650
651 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700652 }
653
Dan Stoza7f7da322014-05-02 15:26:25 -0700654 return NO_ERROR;
655}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700656
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800657status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& /* display */,
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700658 DisplayStatInfo* stats) {
659 if (stats == NULL) {
660 return BAD_VALUE;
661 }
662
663 // FIXME for now we always return stats for the primary display
664 memset(stats, 0, sizeof(*stats));
665 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
666 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
667 return NO_ERROR;
668}
669
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700670int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
Dan Stoza24a42e92015-03-09 10:04:11 -0700671 sp<DisplayDevice> device(getDisplayDevice(display));
672 if (device != NULL) {
673 return device->getActiveConfig();
674 }
675 return BAD_VALUE;
Dan Stoza7f7da322014-05-02 15:26:25 -0700676}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700677
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700678void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
679 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
680 this);
681 int32_t type = hw->getDisplayType();
682 int currentMode = hw->getActiveConfig();
683
684 if (mode == currentMode) {
685 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
686 return;
687 }
688
689 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
690 ALOGW("Trying to set config for virtual display");
691 return;
692 }
693
694 hw->setActiveConfig(mode);
695 getHwComposer().setActiveConfig(type, mode);
696}
697
698status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
699 class MessageSetActiveConfig: public MessageBase {
700 SurfaceFlinger& mFlinger;
701 sp<IBinder> mDisplay;
702 int mMode;
703 public:
704 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
705 int mode) :
706 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
707 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700708 Vector<DisplayInfo> configs;
709 mFlinger.getDisplayConfigs(mDisplay, &configs);
Jesse Hall78442112014-08-07 22:43:06 -0700710 if (mMode < 0 || mMode >= static_cast<int>(configs.size())) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700711 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700712 mMode, configs.size());
713 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700714 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
715 if (hw == NULL) {
716 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700717 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700718 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
719 ALOGW("Attempt to set active config = %d for virtual display",
720 mMode);
721 } else {
722 mFlinger.setActiveConfigInternal(hw, mMode);
723 }
724 return true;
725 }
726 };
727 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
728 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700729 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700730}
731
Svetoslavd85084b2014-03-20 10:28:31 -0700732status_t SurfaceFlinger::clearAnimationFrameStats() {
733 Mutex::Autolock _l(mStateLock);
734 mAnimFrameTracker.clearStats();
735 return NO_ERROR;
736}
737
738status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
739 Mutex::Autolock _l(mStateLock);
740 mAnimFrameTracker.getStats(outStats);
741 return NO_ERROR;
742}
743
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800744// ----------------------------------------------------------------------------
745
746sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800747 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700748}
749
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800750// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800751
752void SurfaceFlinger::waitForEvent() {
753 mEventQueue.waitMessage();
754}
755
756void SurfaceFlinger::signalTransaction() {
757 mEventQueue.invalidate();
758}
759
760void SurfaceFlinger::signalLayerUpdate() {
761 mEventQueue.invalidate();
762}
763
764void SurfaceFlinger::signalRefresh() {
765 mEventQueue.refresh();
766}
767
768status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800769 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800770 return mEventQueue.postMessage(msg, reltime);
771}
772
773status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800774 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800775 status_t res = mEventQueue.postMessage(msg, reltime);
776 if (res == NO_ERROR) {
777 msg->wait();
778 }
779 return res;
780}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800781
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700782void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700783 do {
784 waitForEvent();
785 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800786}
787
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700788void SurfaceFlinger::enableHardwareVsync() {
789 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700790 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700791 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700792 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
793 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700794 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700795 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700796}
797
Jesse Hall948fe0c2013-10-14 12:56:09 -0700798void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700799 Mutex::Autolock _l(mHWVsyncLock);
800
Jesse Hall948fe0c2013-10-14 12:56:09 -0700801 if (makeAvailable) {
802 mHWVsyncAvailable = true;
803 } else if (!mHWVsyncAvailable) {
804 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
805 return;
806 }
807
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700808 const nsecs_t period =
809 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
810
811 mPrimaryDispSync.reset();
812 mPrimaryDispSync.setPeriod(period);
813
814 if (!mPrimaryHWVsyncEnabled) {
815 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700816 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
817 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700818 mPrimaryHWVsyncEnabled = true;
819 }
820}
821
Jesse Hall948fe0c2013-10-14 12:56:09 -0700822void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700823 Mutex::Autolock _l(mHWVsyncLock);
824 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700825 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
826 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700827 mPrimaryDispSync.endResync();
828 mPrimaryHWVsyncEnabled = false;
829 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700830 if (makeUnavailable) {
831 mHWVsyncAvailable = false;
832 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700833}
834
835void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700836 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700837
Jamie Gennisd1700752013-10-14 12:22:52 -0700838 { // Scope for the lock
839 Mutex::Autolock _l(mHWVsyncLock);
840 if (type == 0 && mPrimaryHWVsyncEnabled) {
841 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700842 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700843 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700844
845 if (needsHwVsync) {
846 enableHardwareVsync();
847 } else {
848 disableHardwareVsync(false);
849 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700850}
851
852void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
853 if (mEventThread == NULL) {
854 // This is a temporary workaround for b/7145521. A non-null pointer
855 // does not mean EventThread has finished initializing, so this
856 // is not a correct fix.
857 ALOGW("WARNING: EventThread not started, ignoring hotplug");
858 return;
859 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700860
Jesse Hall9e663de2013-08-16 14:28:37 -0700861 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700862 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800863 if (connected) {
864 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700865 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800866 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
867 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700868 }
869 setTransactionFlags(eDisplayTransactionNeeded);
870
Andy McFadden9e9689c2012-10-10 18:17:51 -0700871 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700872 }
Mathias Agopian86303202012-07-24 22:46:10 -0700873}
874
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700875void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700876 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700877 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700878}
879
Mathias Agopian4fec8732012-06-29 14:12:52 -0700880void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800881 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800882 switch (what) {
Dan Stoza6b9454d2014-11-07 16:00:59 -0800883 case MessageQueue::TRANSACTION: {
884 handleMessageTransaction();
885 break;
886 }
887 case MessageQueue::INVALIDATE: {
888 bool refreshNeeded = handleMessageTransaction();
889 refreshNeeded |= handleMessageInvalidate();
Dan Stoza58784442014-12-02 16:58:17 -0800890 refreshNeeded |= mRepaintEverything;
Dan Stoza6b9454d2014-11-07 16:00:59 -0800891 if (refreshNeeded) {
Dan Stoza58784442014-12-02 16:58:17 -0800892 // Signal a refresh if a transaction modified the window state,
893 // a new buffer was latched, or if HWC has requested a full
894 // repaint
Dan Stoza6b9454d2014-11-07 16:00:59 -0800895 signalRefresh();
896 }
897 break;
898 }
899 case MessageQueue::REFRESH: {
900 handleMessageRefresh();
901 break;
902 }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800903 }
904}
905
Dan Stoza6b9454d2014-11-07 16:00:59 -0800906bool SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700907 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700908 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700909 handleTransaction(transactionFlags);
Dan Stoza6b9454d2014-11-07 16:00:59 -0800910 return true;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700911 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800912 return false;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700913}
914
Dan Stoza6b9454d2014-11-07 16:00:59 -0800915bool SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700916 ATRACE_CALL();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800917 return handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700918}
919
920void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700921 ATRACE_CALL();
922 preComposition();
923 rebuildLayerStacks();
924 setUpHWComposer();
925 doDebugFlashRegions();
926 doComposition();
927 postComposition();
928}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700929
Mathias Agopiancd60f992012-08-16 16:28:27 -0700930void SurfaceFlinger::doDebugFlashRegions()
931{
932 // is debugging enabled
933 if (CC_LIKELY(!mDebugRegion))
934 return;
935
936 const bool repaintEverything = mRepaintEverything;
937 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
938 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700939 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700940 // transform the dirty region into this screen's coordinate space
941 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
942 if (!dirtyRegion.isEmpty()) {
943 // redraw the whole screen
944 doComposeSurfaces(hw, Region(hw->bounds()));
945
946 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700947 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700948 RenderEngine& engine(getRenderEngine());
949 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
950
Mathias Agopiancd60f992012-08-16 16:28:27 -0700951 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700952 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700953 }
954 }
955 }
956
957 postFramebuffer();
958
959 if (mDebugRegion > 1) {
960 usleep(mDebugRegion * 1000);
961 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700962
963 HWComposer& hwc(getHwComposer());
964 if (hwc.initCheck() == NO_ERROR) {
965 status_t err = hwc.prepare();
966 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
967 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700968}
969
970void SurfaceFlinger::preComposition()
971{
972 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700973 const LayerVector& layers(mDrawingState.layersSortedByZ);
974 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700975 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700976 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700977 needExtraInvalidate = true;
978 }
979 }
980 if (needExtraInvalidate) {
981 signalLayerUpdate();
982 }
983}
984
985void SurfaceFlinger::postComposition()
986{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700987 const LayerVector& layers(mDrawingState.layersSortedByZ);
988 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700989 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700990 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700991 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800992
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700993 const HWComposer& hwc = getHwComposer();
994 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
995
996 if (presentFence->isValid()) {
997 if (mPrimaryDispSync.addPresentFence(presentFence)) {
998 enableHardwareVsync();
999 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -07001000 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001001 }
1002 }
1003
Dan Stozab90cf072015-03-05 11:05:59 -08001004 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Andy McFadden5167ec62014-05-22 13:08:43 -07001005 if (kIgnorePresentFences) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001006 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001007 enableHardwareVsync();
1008 }
1009 }
1010
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001011 if (mAnimCompositionPending) {
1012 mAnimCompositionPending = false;
1013
Jesse Halla9a1b002013-02-27 16:39:25 -08001014 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001015 mAnimFrameTracker.setActualPresentFence(presentFence);
1016 } else {
1017 // The HWC doesn't support present fences, so use the refresh
1018 // timestamp instead.
1019 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1020 mAnimFrameTracker.setActualPresentTime(presentTime);
1021 }
1022 mAnimFrameTracker.advanceFrame();
1023 }
Dan Stozab90cf072015-03-05 11:05:59 -08001024
1025 if (hw->getPowerMode() == HWC_POWER_MODE_OFF) {
1026 return;
1027 }
1028
1029 nsecs_t currentTime = systemTime();
1030 if (mHasPoweredOff) {
1031 mHasPoweredOff = false;
1032 } else {
1033 nsecs_t period = mPrimaryDispSync.getPeriod();
1034 nsecs_t elapsedTime = currentTime - mLastSwapTime;
1035 size_t numPeriods = static_cast<size_t>(elapsedTime / period);
1036 if (numPeriods < NUM_BUCKETS - 1) {
1037 mFrameBuckets[numPeriods] += elapsedTime;
1038 } else {
1039 mFrameBuckets[NUM_BUCKETS - 1] += elapsedTime;
1040 }
1041 mTotalTime += elapsedTime;
1042 }
1043 mLastSwapTime = currentTime;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001044}
1045
1046void SurfaceFlinger::rebuildLayerStacks() {
1047 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001048 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001049 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -07001050 mVisibleRegionsDirty = false;
1051 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001052
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001053 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001054 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001055 Region opaqueRegion;
1056 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -08001057 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -07001058 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001059 const Transform& tr(hw->getTransform());
1060 const Rect bounds(hw->getBounds());
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001061 if (hw->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001062 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001063 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001064
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001065 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001066 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001067 const sp<Layer>& layer(layers[i]);
1068 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001069 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -07001070 Region drawRegion(tr.transform(
1071 layer->visibleNonTransparentRegion));
1072 drawRegion.andSelf(bounds);
1073 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001074 layersSortedByZ.add(layer);
1075 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001076 }
1077 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001078 }
Mathias Agopian42977342012-08-05 00:40:46 -07001079 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001080 hw->undefinedRegion.set(bounds);
1081 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
1082 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001083 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001084 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001085}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001086
Mathias Agopiancd60f992012-08-16 16:28:27 -07001087void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001088 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001089 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1090 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1091 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1092
1093 // If nothing has changed (!dirty), don't recompose.
1094 // If something changed, but we don't currently have any visible layers,
1095 // and didn't when we last did a composition, then skip it this time.
1096 // The second rule does two things:
1097 // - When all layers are removed from a display, we'll emit one black
1098 // frame, then nothing more until we get new layers.
1099 // - When a display is created with a private layer stack, we won't
1100 // emit any black frames until a layer is added to the layer stack.
1101 bool mustRecompose = dirty && !(empty && wasEmpty);
1102
1103 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1104 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1105 mustRecompose ? "doing" : "skipping",
1106 dirty ? "+" : "-",
1107 empty ? "+" : "-",
1108 wasEmpty ? "+" : "-");
1109
Dan Stoza71433162014-02-04 16:22:36 -08001110 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001111
1112 if (mustRecompose) {
1113 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1114 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001115 }
1116
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001117 HWComposer& hwc(getHwComposer());
1118 if (hwc.initCheck() == NO_ERROR) {
1119 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001120 if (CC_UNLIKELY(mHwWorkListDirty)) {
1121 mHwWorkListDirty = false;
1122 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1123 sp<const DisplayDevice> hw(mDisplays[dpy]);
1124 const int32_t id = hw->getHwcDisplayId();
1125 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001126 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001127 hw->getVisibleLayersSortedByZ());
1128 const size_t count = currentLayers.size();
1129 if (hwc.createWorkList(id, count) == NO_ERROR) {
1130 HWComposer::LayerListIterator cur = hwc.begin(id);
1131 const HWComposer::LayerListIterator end = hwc.end(id);
1132 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001133 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001134 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001135 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001136 cur->setSkip(true);
1137 }
1138 }
1139 }
1140 }
1141 }
1142 }
1143
1144 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001145 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001146 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001147 const int32_t id = hw->getHwcDisplayId();
1148 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001149 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001150 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001151 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001152 HWComposer::LayerListIterator cur = hwc.begin(id);
1153 const HWComposer::LayerListIterator end = hwc.end(id);
1154 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1155 /*
1156 * update the per-frame h/w composer data for each layer
1157 * and build the transparent region of the FB
1158 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001159 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001160 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -07001161 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001162 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001163 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001164
Riley Andrews03414a12014-07-01 14:22:59 -07001165 // If possible, attempt to use the cursor overlay on each display.
1166 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1167 sp<const DisplayDevice> hw(mDisplays[dpy]);
1168 const int32_t id = hw->getHwcDisplayId();
1169 if (id >= 0) {
1170 const Vector< sp<Layer> >& currentLayers(
1171 hw->getVisibleLayersSortedByZ());
1172 const size_t count = currentLayers.size();
1173 HWComposer::LayerListIterator cur = hwc.begin(id);
1174 const HWComposer::LayerListIterator end = hwc.end(id);
1175 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1176 const sp<Layer>& layer(currentLayers[i]);
1177 if (layer->isPotentialCursor()) {
1178 cur->setIsCursorLayerHint();
1179 break;
1180 }
1181 }
1182 }
1183 }
1184
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001185 status_t err = hwc.prepare();
1186 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001187
1188 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1189 sp<const DisplayDevice> hw(mDisplays[dpy]);
1190 hw->prepareFrame(hwc);
1191 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001192 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001193}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001194
Mathias Agopiancd60f992012-08-16 16:28:27 -07001195void SurfaceFlinger::doComposition() {
1196 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001197 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001198 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001199 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001200 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001201 // transform the dirty region into this screen's coordinate space
1202 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001203
1204 // repaint the framebuffer (if needed)
1205 doDisplayComposition(hw, dirtyRegion);
1206
Mathias Agopiancd60f992012-08-16 16:28:27 -07001207 hw->dirtyRegion.clear();
1208 hw->flip(hw->swapRegion);
1209 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001210 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001211 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001212 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001213 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001214 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001215}
1216
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217void SurfaceFlinger::postFramebuffer()
1218{
Mathias Agopian841cde52012-03-01 15:44:37 -08001219 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001220
Mathias Agopiana44b0412011-10-16 18:46:35 -07001221 const nsecs_t now = systemTime();
1222 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001223
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001224 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001225 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001226 if (!hwc.supportsFramebufferTarget()) {
1227 // EGL spec says:
1228 // "surface must be bound to the calling thread's current context,
1229 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001230 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001231 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001232 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001233 }
1234
Mathias Agopian6da15f42013-09-25 20:40:07 -07001235 // make the default display current because the VirtualDisplayDevice code cannot
1236 // deal with dequeueBuffer() being called outside of the composition loop; however
1237 // the code below can call glFlush() which is allowed (and does in some case) call
1238 // dequeueBuffer().
1239 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1240
Mathias Agopian92a979a2012-08-02 18:32:23 -07001241 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001242 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001243 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001244 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001245 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001246 int32_t id = hw->getHwcDisplayId();
1247 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001248 HWComposer::LayerListIterator cur = hwc.begin(id);
1249 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001250 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001251 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001252 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001253 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001254 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001255 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001256 }
Jesse Hallef194142012-06-14 14:45:17 -07001257 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001258 }
1259
Mathias Agopiana44b0412011-10-16 18:46:35 -07001260 mLastSwapBufferTime = systemTime() - now;
1261 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001262
1263 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1264 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1265 logFrameStats();
1266 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267}
1268
Mathias Agopian87baae12012-07-31 12:38:26 -07001269void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270{
Mathias Agopian841cde52012-03-01 15:44:37 -08001271 ATRACE_CALL();
1272
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001273 // here we keep a copy of the drawing state (that is the state that's
1274 // going to be overwritten by handleTransactionLocked()) outside of
1275 // mStateLock so that the side-effects of the State assignment
1276 // don't happen with mStateLock held (which can cause deadlocks).
1277 State drawingState(mDrawingState);
1278
Mathias Agopianca4d3602011-05-19 15:38:14 -07001279 Mutex::Autolock _l(mStateLock);
1280 const nsecs_t now = systemTime();
1281 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282
Mathias Agopianca4d3602011-05-19 15:38:14 -07001283 // Here we're guaranteed that some transaction flags are set
1284 // so we can call handleTransactionLocked() unconditionally.
1285 // We call getTransactionFlags(), which will also clear the flags,
1286 // with mStateLock held to guarantee that mCurrentState won't change
1287 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001288
Mathias Agopiane57f2922012-08-09 16:29:12 -07001289 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001290 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001291
Mathias Agopianca4d3602011-05-19 15:38:14 -07001292 mLastTransactionTime = systemTime() - now;
1293 mDebugInTransaction = 0;
1294 invalidateHwcGeometry();
1295 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001296}
1297
Mathias Agopian87baae12012-07-31 12:38:26 -07001298void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001299{
1300 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301 const size_t count = currentLayers.size();
1302
1303 /*
1304 * Traversal of the children
1305 * (perform the transaction for each of them if needed)
1306 */
1307
Mathias Agopian3559b072012-08-15 13:46:03 -07001308 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001310 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001311 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1312 if (!trFlags) continue;
1313
1314 const uint32_t flags = layer->doTransaction(0);
1315 if (flags & Layer::eVisibleRegion)
1316 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317 }
1318 }
1319
1320 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001321 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322 */
1323
Mathias Agopiane57f2922012-08-09 16:29:12 -07001324 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001325 // here we take advantage of Vector's copy-on-write semantics to
1326 // improve performance by skipping the transaction entirely when
1327 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001328 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1329 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001330 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001332 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001333 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001334
1335 // find the displays that were removed
1336 // (ie: in drawing state but not in current state)
1337 // also handle displays that changed
1338 // (ie: displays that are in both lists)
1339 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001340 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1341 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001342 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001343 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001344 // Call makeCurrent() on the primary display so we can
1345 // be sure that nothing associated with this display
1346 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001347 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001348 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001349 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1350 if (hw != NULL)
1351 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001352 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001353 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001354 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001355 } else {
1356 ALOGW("trying to remove the main display");
1357 }
1358 } else {
1359 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001360 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001361 const wp<IBinder>& display(curr.keyAt(j));
Marco Nelissen097ca272014-11-14 08:01:01 -08001362 const sp<IBinder> state_binder = IInterface::asBinder(state.surface);
1363 const sp<IBinder> draw_binder = IInterface::asBinder(draw[i].surface);
Dan Albert1474f882014-09-08 18:59:09 -07001364 if (state_binder != draw_binder) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001365 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001366 // recreating the DisplayDevice, so we just remove it
1367 // from the drawing state, so that it get re-added
1368 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001369 sp<DisplayDevice> hw(getDisplayDevice(display));
1370 if (hw != NULL)
1371 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001372 mDisplays.removeItem(display);
1373 mDrawingState.displays.removeItemsAt(i);
1374 dc--; i--;
1375 // at this point we must loop to the next item
1376 continue;
1377 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001378
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001379 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001380 if (disp != NULL) {
1381 if (state.layerStack != draw[i].layerStack) {
1382 disp->setLayerStack(state.layerStack);
1383 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001384 if ((state.orientation != draw[i].orientation)
1385 || (state.viewport != draw[i].viewport)
1386 || (state.frame != draw[i].frame))
1387 {
1388 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001389 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001390 }
Michael Lentine47e45402014-07-18 15:34:25 -07001391 if (state.width != draw[i].width || state.height != draw[i].height) {
1392 disp->setDisplaySize(state.width, state.height);
1393 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001394 }
1395 }
1396 }
1397
1398 // find displays that were added
1399 // (ie: in current state but not in drawing state)
1400 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001401 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001402 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001403
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001404 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001405 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001406 sp<IGraphicBufferProducer> bqProducer;
1407 sp<IGraphicBufferConsumer> bqConsumer;
1408 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1409 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001410
Jesse Hall02d86562013-03-25 14:43:23 -07001411 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001412 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001413 // Virtual displays without a surface are dormant:
1414 // they have external state (layer stack, projection,
1415 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001416 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001417
Dan Stoza1f3efb12014-10-15 16:34:55 -07001418 int width = 0;
1419 int status = state.surface->query(
1420 NATIVE_WINDOW_WIDTH, &width);
1421 ALOGE_IF(status != NO_ERROR,
1422 "Unable to query width (%d)", status);
1423 int height = 0;
1424 status = state.surface->query(
1425 NATIVE_WINDOW_HEIGHT, &height);
1426 ALOGE_IF(status != NO_ERROR,
1427 "Unable to query height (%d)", status);
1428 if (MAX_VIRTUAL_DISPLAY_DIMENSION == 0 ||
1429 (width <= MAX_VIRTUAL_DISPLAY_DIMENSION &&
1430 height <= MAX_VIRTUAL_DISPLAY_DIMENSION)) {
1431 hwcDisplayId = allocateHwcDisplayId(state.type);
1432 }
1433
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001434 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
Dan Stozab9b08832014-03-13 11:55:57 -07001435 *mHwc, hwcDisplayId, state.surface,
1436 bqProducer, bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001437
1438 dispSurface = vds;
Michael Lentine47e45402014-07-18 15:34:25 -07001439 producer = vds;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001440 }
1441 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001442 ALOGE_IF(state.surface!=NULL,
1443 "adding a supported display, but rendering "
1444 "surface is provided (%p), ignoring it",
1445 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001446 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001447 // for supported (by hwc) displays we provide our
1448 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001449 dispSurface = new FramebufferSurface(*mHwc, state.type,
1450 bqConsumer);
1451 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001452 }
1453
1454 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001455 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001456 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001457 state.type, hwcDisplayId,
1458 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001459 display, dispSurface, producer,
1460 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001461 hw->setLayerStack(state.layerStack);
1462 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001463 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001464 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001465 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001466 if (state.isVirtualDisplay()) {
1467 if (hwcDisplayId >= 0) {
1468 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1469 hw->getWidth(), hw->getHeight(),
1470 hw->getFormat());
1471 }
1472 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001473 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001474 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001475 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001476 }
1477 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001478 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001479 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480
Mathias Agopian84300952012-11-21 16:02:13 -08001481 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1482 // The transform hint might have changed for some layers
1483 // (either because a display has changed, or because a layer
1484 // as changed).
1485 //
1486 // Walk through all the layers in currentLayers,
1487 // and update their transform hint.
1488 //
1489 // If a layer is visible only on a single display, then that
1490 // display is used to calculate the hint, otherwise we use the
1491 // default display.
1492 //
1493 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1494 // the hint is set before we acquire a buffer from the surface texture.
1495 //
1496 // NOTE: layer transactions have taken place already, so we use their
1497 // drawing state. However, SurfaceFlinger's own transaction has not
1498 // happened yet, so we must use the current state layer list
1499 // (soon to become the drawing state list).
1500 //
1501 sp<const DisplayDevice> disp;
1502 uint32_t currentlayerStack = 0;
1503 for (size_t i=0; i<count; i++) {
1504 // NOTE: we rely on the fact that layers are sorted by
1505 // layerStack first (so we don't have to traverse the list
1506 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001507 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001508 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001509 if (i==0 || currentlayerStack != layerStack) {
1510 currentlayerStack = layerStack;
1511 // figure out if this layerstack is mirrored
1512 // (more than one display) if so, pick the default display,
1513 // if not, pick the only display it's on.
1514 disp.clear();
1515 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1516 sp<const DisplayDevice> hw(mDisplays[dpy]);
1517 if (hw->getLayerStack() == currentlayerStack) {
1518 if (disp == NULL) {
1519 disp = hw;
1520 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001521 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001522 break;
1523 }
1524 }
1525 }
1526 }
Chet Haase91d25932013-04-11 15:24:55 -07001527 if (disp == NULL) {
1528 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1529 // redraw after transform hint changes. See bug 8508397.
1530
1531 // could be null when this layer is using a layerStack
1532 // that is not visible on any display. Also can occur at
1533 // screen off/on times.
1534 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001535 }
Chet Haase91d25932013-04-11 15:24:55 -07001536 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001537 }
1538 }
1539
1540
Mathias Agopian3559b072012-08-15 13:46:03 -07001541 /*
1542 * Perform our own transaction if needed
1543 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001544
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001545 const LayerVector& layers(mDrawingState.layersSortedByZ);
1546 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001547 // layers have been added
1548 mVisibleRegionsDirty = true;
1549 }
1550
1551 // some layers might have been removed, so
1552 // we need to update the regions they're exposing.
1553 if (mLayersRemoved) {
1554 mLayersRemoved = false;
1555 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001556 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001557 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001558 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001559 if (currentLayers.indexOf(layer) < 0) {
1560 // this layer is not visible anymore
1561 // TODO: we could traverse the tree from front to back and
1562 // compute the actual visible region
1563 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001564 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001565 Region visibleReg = s.transform.transform(
1566 Region(Rect(s.active.w, s.active.h)));
1567 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001568 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001569 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001570 }
1571
1572 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001573
1574 updateCursorAsync();
1575}
1576
1577void SurfaceFlinger::updateCursorAsync()
1578{
1579 HWComposer& hwc(getHwComposer());
1580 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1581 sp<const DisplayDevice> hw(mDisplays[dpy]);
1582 const int32_t id = hw->getHwcDisplayId();
1583 if (id < 0) {
1584 continue;
1585 }
1586 const Vector< sp<Layer> >& currentLayers(
1587 hw->getVisibleLayersSortedByZ());
1588 const size_t count = currentLayers.size();
1589 HWComposer::LayerListIterator cur = hwc.begin(id);
1590 const HWComposer::LayerListIterator end = hwc.end(id);
1591 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1592 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1593 continue;
1594 }
1595 const sp<Layer>& layer(currentLayers[i]);
1596 Rect cursorPos = layer->getPosition(hw);
1597 hwc.setCursorPositionAsync(id, cursorPos);
1598 break;
1599 }
1600 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001601}
1602
1603void SurfaceFlinger::commitTransaction()
1604{
1605 if (!mLayersPendingRemoval.isEmpty()) {
1606 // Notify removed layers now that they can't be drawn from
1607 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1608 mLayersPendingRemoval[i]->onRemoved();
1609 }
1610 mLayersPendingRemoval.clear();
1611 }
1612
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001613 // If this transaction is part of a window animation then the next frame
1614 // we composite should be considered an animation as well.
1615 mAnimCompositionPending = mAnimTransactionPending;
1616
Mathias Agopian4fec8732012-06-29 14:12:52 -07001617 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001618 mTransactionPending = false;
1619 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001620 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001621}
1622
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001623void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001624 const LayerVector& currentLayers, uint32_t layerStack,
1625 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001626{
Mathias Agopian841cde52012-03-01 15:44:37 -08001627 ATRACE_CALL();
1628
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001629 Region aboveOpaqueLayers;
1630 Region aboveCoveredLayers;
1631 Region dirty;
1632
Mathias Agopian87baae12012-07-31 12:38:26 -07001633 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001634
1635 size_t i = currentLayers.size();
1636 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001637 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001638
1639 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001640 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001641
Jesse Hall01e29052013-02-19 16:13:35 -08001642 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001643 if (s.layerStack != layerStack)
1644 continue;
1645
Mathias Agopianab028732010-03-16 16:41:46 -07001646 /*
1647 * opaqueRegion: area of a surface that is fully opaque.
1648 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001649 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001650
1651 /*
1652 * visibleRegion: area of a surface that is visible on screen
1653 * and not fully transparent. This is essentially the layer's
1654 * footprint minus the opaque regions above it.
1655 * Areas covered by a translucent surface are considered visible.
1656 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001657 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001658
1659 /*
1660 * coveredRegion: area of a surface that is covered by all
1661 * visible regions above it (which includes the translucent areas).
1662 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001664
Jesse Halla8026d22012-09-25 13:25:04 -07001665 /*
1666 * transparentRegion: area of a surface that is hinted to be completely
1667 * transparent. This is only used to tell when the layer has no visible
1668 * non-transparent regions and can be removed from the layer list. It
1669 * does not affect the visibleRegion of this layer or any layers
1670 * beneath it. The hint may not be correct if apps don't respect the
1671 * SurfaceView restrictions (which, sadly, some don't).
1672 */
1673 Region transparentRegion;
1674
Mathias Agopianab028732010-03-16 16:41:46 -07001675
1676 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001677 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001678 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001679 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001680 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001681 if (!visibleRegion.isEmpty()) {
1682 // Remove the transparent area from the visible region
1683 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001684 const Transform tr(s.transform);
1685 if (tr.transformed()) {
1686 if (tr.preserveRects()) {
1687 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001688 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001689 } else {
1690 // transformation too complex, can't do the
1691 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001692 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001693 }
1694 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001695 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001696 }
Mathias Agopianab028732010-03-16 16:41:46 -07001697 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001698
Mathias Agopianab028732010-03-16 16:41:46 -07001699 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001700 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001701 if (s.alpha==255 && !translucent &&
1702 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1703 // the opaque region is the layer's footprint
1704 opaqueRegion = visibleRegion;
1705 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001706 }
1707 }
1708
Mathias Agopianab028732010-03-16 16:41:46 -07001709 // Clip the covered region to the visible region
1710 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1711
1712 // Update aboveCoveredLayers for next (lower) layer
1713 aboveCoveredLayers.orSelf(visibleRegion);
1714
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001715 // subtract the opaque region covered by the layers above us
1716 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001717
1718 // compute this layer's dirty region
1719 if (layer->contentDirty) {
1720 // we need to invalidate the whole region
1721 dirty = visibleRegion;
1722 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001723 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001724 layer->contentDirty = false;
1725 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001726 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001727 * the exposed region consists of two components:
1728 * 1) what's VISIBLE now and was COVERED before
1729 * 2) what's EXPOSED now less what was EXPOSED before
1730 *
1731 * note that (1) is conservative, we start with the whole
1732 * visible region but only keep what used to be covered by
1733 * something -- which mean it may have been exposed.
1734 *
1735 * (2) handles areas that were not covered by anything but got
1736 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001737 */
Mathias Agopianab028732010-03-16 16:41:46 -07001738 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001739 const Region oldVisibleRegion = layer->visibleRegion;
1740 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001741 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1742 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001743 }
1744 dirty.subtractSelf(aboveOpaqueLayers);
1745
1746 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001747 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001748
Mathias Agopianab028732010-03-16 16:41:46 -07001749 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001750 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001751
Jesse Halla8026d22012-09-25 13:25:04 -07001752 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001753 layer->setVisibleRegion(visibleRegion);
1754 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001755 layer->setVisibleNonTransparentRegion(
1756 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001757 }
1758
Mathias Agopian87baae12012-07-31 12:38:26 -07001759 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001760}
1761
Mathias Agopian87baae12012-07-31 12:38:26 -07001762void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1763 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001764 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001765 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1766 if (hw->getLayerStack() == layerStack) {
1767 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001768 }
1769 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001770}
1771
Dan Stoza6b9454d2014-11-07 16:00:59 -08001772bool SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001773{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001774 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001775
Mathias Agopian4fec8732012-06-29 14:12:52 -07001776 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001777 const LayerVector& layers(mDrawingState.layersSortedByZ);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001778 bool frameQueued = false;
Eric Penner51c59cd2014-07-28 19:51:58 -07001779
1780 // Store the set of layers that need updates. This set must not change as
1781 // buffers are being latched, as this could result in a deadlock.
1782 // Example: Two producers share the same command stream and:
1783 // 1.) Layer 0 is latched
1784 // 2.) Layer 0 gets a new frame
1785 // 2.) Layer 1 gets a new frame
1786 // 3.) Layer 1 is latched.
1787 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1788 // second frame. But layer 0's second frame could be waiting on display.
1789 Vector<Layer*> layersWithQueuedFrames;
1790 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001791 const sp<Layer>& layer(layers[i]);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001792 if (layer->hasQueuedFrame()) {
1793 frameQueued = true;
1794 if (layer->shouldPresentNow(mPrimaryDispSync)) {
1795 layersWithQueuedFrames.push_back(layer.get());
Dan Stozaee44edd2015-03-23 15:50:23 -07001796 } else {
1797 layer->useEmptyDamage();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001798 }
Dan Stozaee44edd2015-03-23 15:50:23 -07001799 } else {
1800 layer->useEmptyDamage();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001801 }
Eric Penner51c59cd2014-07-28 19:51:58 -07001802 }
1803 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
1804 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07001805 const Region dirty(layer->latchBuffer(visibleRegions));
Dan Stozaee44edd2015-03-23 15:50:23 -07001806 layer->useSurfaceDamage();
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001807 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001808 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001809 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001810
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001811 mVisibleRegionsDirty |= visibleRegions;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001812
1813 // If we will need to wake up at some time in the future to deal with a
1814 // queued frame that shouldn't be displayed during this vsync period, wake
1815 // up during the next vsync period to check again.
1816 if (frameQueued && layersWithQueuedFrames.empty()) {
1817 signalLayerUpdate();
1818 }
1819
1820 // Only continue with the refresh if there is actually new work to do
1821 return !layersWithQueuedFrames.empty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001822}
1823
Mathias Agopianad456f92011-01-13 17:53:01 -08001824void SurfaceFlinger::invalidateHwcGeometry()
1825{
1826 mHwWorkListDirty = true;
1827}
1828
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001829
Mathias Agopiancd60f992012-08-16 16:28:27 -07001830void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001831 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001832{
Dan Stoza71433162014-02-04 16:22:36 -08001833 // We only need to actually compose the display if:
1834 // 1) It is being handled by hardware composer, which may need this to
1835 // keep its virtual display state machine in sync, or
1836 // 2) There is work to be done (the dirty region isn't empty)
1837 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1838 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1839 return;
1840 }
1841
Mathias Agopian87baae12012-07-31 12:38:26 -07001842 Region dirtyRegion(inDirtyRegion);
1843
Mathias Agopianb8a55602009-06-26 19:06:36 -07001844 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001845 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001846
Mathias Agopian42977342012-08-05 00:40:46 -07001847 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001848 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001849 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1850 // takes a rectangle, we must make sure to update that whole
1851 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001852 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001853 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001854 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001855 // We need to redraw the rectangle that will be updated
1856 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001857 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001858 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001859 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001860 } else {
1861 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001862 dirtyRegion.set(hw->bounds());
1863 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001864 }
1865 }
1866
Alan Viverette9c5a3332013-09-12 20:04:35 -07001867 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Michael Lentine3f121fc2014-10-01 11:17:28 -07001868 if (!doComposeSurfaces(hw, dirtyRegion)) return;
Mathias Agopianff2ed702013-09-01 21:36:12 -07001869 } else {
1870 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001871 mat4 colorMatrix = mColorMatrix;
1872 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001873 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001874 }
1875 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001876 doComposeSurfaces(hw, dirtyRegion);
1877 engine.endGroup();
1878 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001879
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001880 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001881 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001882
1883 // swap buffers (presentation)
1884 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001885}
1886
Michael Lentine3f121fc2014-10-01 11:17:28 -07001887bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001888{
Mathias Agopian3f844832013-08-07 21:24:32 -07001889 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001890 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001891 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001892 HWComposer::LayerListIterator cur = hwc.begin(id);
1893 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001894
Jesse Halld05a17f2013-09-30 16:49:30 -07001895 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001896 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001897 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001898 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1899 hw->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07001900 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1901 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
1902 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
1903 }
1904 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08001905 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001906
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001907 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001908 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001909 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001910 // when using overlays, we assume a fully transparent framebuffer
1911 // NOTE: we could reduce how much we need to clear, for instance
1912 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001913 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001914 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001915 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001916 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001917 // we start with the whole screen area
1918 const Region bounds(hw->getBounds());
1919
1920 // we remove the scissor part
1921 // we're left with the letterbox region
1922 // (common case is that letterbox ends-up being empty)
1923 const Region letterbox(bounds.subtract(hw->getScissor()));
1924
1925 // compute the area to clear
1926 Region region(hw->undefinedRegion.merge(letterbox));
1927
1928 // but limit it to the dirty region
1929 region.andSelf(dirty);
1930
Mathias Agopianb9494d52012-04-18 02:28:45 -07001931 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001932 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001933 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001934 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001935 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001936 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001937
Mathias Agopian766dc492012-10-30 18:08:06 -07001938 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1939 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001940 // scissor on the main display. It should never be needed
1941 // anyways (though in theory it could since the API allows it).
1942 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001943 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001944 if (scissor != bounds) {
1945 // scissor doesn't match the screen's dimensions, so we
1946 // need to clear everything outside of it and enable
1947 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001948
Mathias Agopianf45c5102012-10-24 16:29:17 -07001949 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001950 const uint32_t height = hw->getHeight();
1951 engine.setScissor(scissor.left, height - scissor.bottom,
1952 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001953 }
1954 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001955 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001956
Mathias Agopian85d751c2012-08-29 16:59:24 -07001957 /*
1958 * and then, render the layers targeted at the framebuffer
1959 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001960
Mathias Agopian13127d82013-03-05 17:47:11 -08001961 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001962 const size_t count = layers.size();
1963 const Transform& tr = hw->getTransform();
1964 if (cur != end) {
1965 // we're using h/w composer
1966 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001967 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001968 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001969 if (!clip.isEmpty()) {
1970 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07001971 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07001972 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001973 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001974 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1975 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001976 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001977 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001978 // never clear the very first layer since we're
1979 // guaranteed the FB is already cleared
1980 layer->clearWithOpenGL(hw, clip);
1981 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001982 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001983 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001984 case HWC_FRAMEBUFFER: {
1985 layer->draw(hw, clip);
1986 break;
1987 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001988 case HWC_FRAMEBUFFER_TARGET: {
1989 // this should not happen as the iterator shouldn't
1990 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001991 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001992 break;
1993 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001994 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001995 }
1996 layer->setAcquireFence(hw, *cur);
1997 }
1998 } else {
1999 // we're not using h/w composer
2000 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002001 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002002 const Region clip(dirty.intersect(
2003 tr.transform(layer->visibleRegion)));
2004 if (!clip.isEmpty()) {
2005 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07002006 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002007 }
2008 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002009
2010 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002011 engine.disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07002012 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002013}
2014
Mathias Agopian3f844832013-08-07 21:24:32 -07002015void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07002016 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07002017 RenderEngine& engine(getRenderEngine());
2018 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002019}
2020
Mathias Agopianac9fa422013-02-11 16:40:36 -08002021void SurfaceFlinger::addClientLayer(const sp<Client>& client,
2022 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07002023 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08002024 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07002025{
Mathias Agopian96f08192010-06-02 23:28:45 -07002026 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08002027 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07002028
Mathias Agopian96f08192010-06-02 23:28:45 -07002029 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002030 Mutex::Autolock _l(mStateLock);
2031 mCurrentState.layersSortedByZ.add(lbc);
Marco Nelissen097ca272014-11-14 08:01:01 -08002032 mGraphicBufferProducerList.add(IInterface::asBinder(gbc));
Mathias Agopian96f08192010-06-02 23:28:45 -07002033}
2034
Mathias Agopian3f844832013-08-07 21:24:32 -07002035status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002036 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08002037 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002038 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07002039 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002040 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07002041 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002042 return NO_ERROR;
2043 }
Mathias Agopian3d579642009-06-04 18:46:21 -07002044 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002045}
2046
Dan Stozac7014012014-02-14 15:03:43 -08002047uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07002048 return android_atomic_release_load(&mTransactionFlags);
2049}
2050
Mathias Agopian3f844832013-08-07 21:24:32 -07002051uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002052 return android_atomic_and(~flags, &mTransactionFlags) & flags;
2053}
2054
Mathias Agopian3f844832013-08-07 21:24:32 -07002055uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002056 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
2057 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002058 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002059 }
2060 return old;
2061}
2062
Mathias Agopian8b33f032012-07-24 20:43:54 -07002063void SurfaceFlinger::setTransactionState(
2064 const Vector<ComposerState>& state,
2065 const Vector<DisplayState>& displays,
2066 uint32_t flags)
2067{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002068 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07002069 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07002070 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002071
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002072 if (flags & eAnimation) {
2073 // For window updates that are part of an animation we must wait for
2074 // previous animation "frames" to be handled.
2075 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002076 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002077 if (CC_UNLIKELY(err != NO_ERROR)) {
2078 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002079 // caller after a few seconds.
2080 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
2081 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002082 mAnimTransactionPending = false;
2083 break;
2084 }
2085 }
2086 }
2087
Mathias Agopiane57f2922012-08-09 16:29:12 -07002088 size_t count = displays.size();
2089 for (size_t i=0 ; i<count ; i++) {
2090 const DisplayState& s(displays[i]);
2091 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002092 }
2093
Mathias Agopiane57f2922012-08-09 16:29:12 -07002094 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002095 for (size_t i=0 ; i<count ; i++) {
2096 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002097 // Here we need to check that the interface we're given is indeed
2098 // one of our own. A malicious client could give us a NULL
2099 // IInterface, or one of its own or even one of our own but a
2100 // different type. All these situations would cause us to crash.
2101 //
2102 // NOTE: it would be better to use RTTI as we could directly check
2103 // that we have a Client*. however, RTTI is disabled in Android.
2104 if (s.client != NULL) {
Marco Nelissen097ca272014-11-14 08:01:01 -08002105 sp<IBinder> binder = IInterface::asBinder(s.client);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002106 if (binder != NULL) {
2107 String16 desc(binder->getInterfaceDescriptor());
2108 if (desc == ISurfaceComposerClient::descriptor) {
2109 sp<Client> client( static_cast<Client *>(s.client.get()) );
2110 transactionFlags |= setClientStateLocked(client, s.state);
2111 }
2112 }
2113 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002114 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002115
Mathias Agopian386aa982011-11-07 21:58:03 -08002116 if (transactionFlags) {
2117 // this triggers the transaction
2118 setTransactionFlags(transactionFlags);
2119
2120 // if this is a synchronous transaction, wait for it to take effect
2121 // before returning.
2122 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002123 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002124 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002125 if (flags & eAnimation) {
2126 mAnimTransactionPending = true;
2127 }
2128 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002129 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2130 if (CC_UNLIKELY(err != NO_ERROR)) {
2131 // just in case something goes wrong in SF, return to the
2132 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002133 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2134 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002135 break;
2136 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002137 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002138 }
2139}
2140
Mathias Agopiane57f2922012-08-09 16:29:12 -07002141uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2142{
Jesse Hall9a143922012-10-04 16:29:19 -07002143 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2144 if (dpyIdx < 0)
2145 return 0;
2146
Mathias Agopiane57f2922012-08-09 16:29:12 -07002147 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002148 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002149 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002150 const uint32_t what = s.what;
2151 if (what & DisplayState::eSurfaceChanged) {
Marco Nelissen097ca272014-11-14 08:01:01 -08002152 if (IInterface::asBinder(disp.surface) != IInterface::asBinder(s.surface)) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002153 disp.surface = s.surface;
2154 flags |= eDisplayTransactionNeeded;
2155 }
2156 }
2157 if (what & DisplayState::eLayerStackChanged) {
2158 if (disp.layerStack != s.layerStack) {
2159 disp.layerStack = s.layerStack;
2160 flags |= eDisplayTransactionNeeded;
2161 }
2162 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002163 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002164 if (disp.orientation != s.orientation) {
2165 disp.orientation = s.orientation;
2166 flags |= eDisplayTransactionNeeded;
2167 }
2168 if (disp.frame != s.frame) {
2169 disp.frame = s.frame;
2170 flags |= eDisplayTransactionNeeded;
2171 }
2172 if (disp.viewport != s.viewport) {
2173 disp.viewport = s.viewport;
2174 flags |= eDisplayTransactionNeeded;
2175 }
2176 }
Michael Lentine47e45402014-07-18 15:34:25 -07002177 if (what & DisplayState::eDisplaySizeChanged) {
2178 if (disp.width != s.width) {
2179 disp.width = s.width;
2180 flags |= eDisplayTransactionNeeded;
2181 }
2182 if (disp.height != s.height) {
2183 disp.height = s.height;
2184 flags |= eDisplayTransactionNeeded;
2185 }
2186 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002187 }
2188 return flags;
2189}
2190
2191uint32_t SurfaceFlinger::setClientStateLocked(
2192 const sp<Client>& client,
2193 const layer_state_t& s)
2194{
2195 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002196 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002197 if (layer != 0) {
2198 const uint32_t what = s.what;
2199 if (what & layer_state_t::ePositionChanged) {
2200 if (layer->setPosition(s.x, s.y))
2201 flags |= eTraversalNeeded;
2202 }
2203 if (what & layer_state_t::eLayerChanged) {
2204 // NOTE: index needs to be calculated before we update the state
2205 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2206 if (layer->setLayer(s.z)) {
2207 mCurrentState.layersSortedByZ.removeAt(idx);
2208 mCurrentState.layersSortedByZ.add(layer);
2209 // we need traversal (state changed)
2210 // AND transaction (list changed)
2211 flags |= eTransactionNeeded|eTraversalNeeded;
2212 }
2213 }
2214 if (what & layer_state_t::eSizeChanged) {
2215 if (layer->setSize(s.w, s.h)) {
2216 flags |= eTraversalNeeded;
2217 }
2218 }
2219 if (what & layer_state_t::eAlphaChanged) {
2220 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2221 flags |= eTraversalNeeded;
2222 }
2223 if (what & layer_state_t::eMatrixChanged) {
2224 if (layer->setMatrix(s.matrix))
2225 flags |= eTraversalNeeded;
2226 }
2227 if (what & layer_state_t::eTransparentRegionChanged) {
2228 if (layer->setTransparentRegionHint(s.transparentRegion))
2229 flags |= eTraversalNeeded;
2230 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08002231 if ((what & layer_state_t::eVisibilityChanged) ||
2232 (what & layer_state_t::eOpacityChanged)) {
2233 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07002234 if (layer->setFlags(s.flags, s.mask))
2235 flags |= eTraversalNeeded;
2236 }
2237 if (what & layer_state_t::eCropChanged) {
2238 if (layer->setCrop(s.crop))
2239 flags |= eTraversalNeeded;
2240 }
2241 if (what & layer_state_t::eLayerStackChanged) {
2242 // NOTE: index needs to be calculated before we update the state
2243 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2244 if (layer->setLayerStack(s.layerStack)) {
2245 mCurrentState.layersSortedByZ.removeAt(idx);
2246 mCurrentState.layersSortedByZ.add(layer);
2247 // we need traversal (state changed)
2248 // AND transaction (list changed)
2249 flags |= eTransactionNeeded|eTraversalNeeded;
2250 }
2251 }
2252 }
2253 return flags;
2254}
2255
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002256status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002257 const String8& name,
2258 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002259 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2260 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002261{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002262 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002263 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002264 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002265 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002266 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002267 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002268
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002269 status_t result = NO_ERROR;
2270
2271 sp<Layer> layer;
2272
Mathias Agopian3165cc22012-08-08 19:42:09 -07002273 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2274 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002275 result = createNormalLayer(client,
2276 name, w, h, flags, format,
2277 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002278 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002279 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002280 result = createDimLayer(client,
2281 name, w, h, flags,
2282 handle, gbp, &layer);
2283 break;
2284 default:
2285 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002286 break;
2287 }
2288
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002289 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002290 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002291 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002292 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002293 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002294}
2295
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002296status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2297 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2298 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002299{
2300 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002301 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002302 case PIXEL_FORMAT_TRANSPARENT:
2303 case PIXEL_FORMAT_TRANSLUCENT:
2304 format = PIXEL_FORMAT_RGBA_8888;
2305 break;
2306 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002307 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002308 break;
2309 }
2310
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002311 *outLayer = new Layer(this, client, name, w, h, flags);
2312 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2313 if (err == NO_ERROR) {
2314 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002315 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002316 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002317
2318 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2319 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002320}
2321
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002322status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2323 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2324 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002325{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002326 *outLayer = new LayerDim(this, client, name, w, h, flags);
2327 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002328 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002329 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002330}
2331
Mathias Agopianac9fa422013-02-11 16:40:36 -08002332status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002333{
Mathias Agopian67106042013-03-14 19:18:13 -07002334 // called by the window manager when it wants to remove a Layer
2335 status_t err = NO_ERROR;
2336 sp<Layer> l(client->getLayerUser(handle));
2337 if (l != NULL) {
2338 err = removeLayer(l);
2339 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2340 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002341 }
2342 return err;
2343}
2344
Mathias Agopian13127d82013-03-05 17:47:11 -08002345status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002346{
Mathias Agopian67106042013-03-14 19:18:13 -07002347 // called by ~LayerCleaner() when all references to the IBinder (handle)
2348 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002349 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002350 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002351 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002352 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002353 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002354 "error removing layer=%p (%s)", l.get(), strerror(-err));
2355 }
2356 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002357}
2358
Mathias Agopianb60314a2012-04-10 22:09:54 -07002359// ---------------------------------------------------------------------------
2360
Andy McFadden13a082e2012-08-24 10:16:42 -07002361void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002362 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002363 Vector<ComposerState> state;
2364 Vector<DisplayState> displays;
2365 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002366 d.what = DisplayState::eDisplayProjectionChanged |
2367 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002368 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002369 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002370 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002371 d.frame.makeInvalid();
2372 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002373 d.width = 0;
2374 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002375 displays.add(d);
2376 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002377 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002378
2379 const nsecs_t period =
2380 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2381 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002382}
2383
2384void SurfaceFlinger::initializeDisplays() {
2385 class MessageScreenInitialized : public MessageBase {
2386 SurfaceFlinger* flinger;
2387 public:
2388 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2389 virtual bool handler() {
2390 flinger->onInitializeDisplays();
2391 return true;
2392 }
2393 };
2394 sp<MessageBase> msg = new MessageScreenInitialized(this);
2395 postMessageAsync(msg); // we may be called from main thread, use async message
2396}
2397
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002398void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2399 int mode) {
2400 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2401 this);
2402 int32_t type = hw->getDisplayType();
2403 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002404
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002405 if (mode == currentMode) {
2406 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002407 return;
2408 }
2409
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002410 hw->setPowerMode(mode);
2411 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2412 ALOGW("Trying to set power mode for virtual display");
2413 return;
2414 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002415
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002416 if (currentMode == HWC_POWER_MODE_OFF) {
2417 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002418 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2419 // FIXME: eventthread only knows about the main display right now
2420 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002421 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002422 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002423
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002424 mVisibleRegionsDirty = true;
Dan Stozab90cf072015-03-05 11:05:59 -08002425 mHasPoweredOff = true;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002426 repaintEverything();
2427 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002428 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002429 disableHardwareVsync(true); // also cancels any in-progress resync
2430
Mathias Agopiancde87a32012-09-13 14:09:01 -07002431 // FIXME: eventthread only knows about the main display right now
2432 mEventThread->onScreenReleased();
2433 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002434
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002435 getHwComposer().setPowerMode(type, mode);
2436 mVisibleRegionsDirty = true;
2437 // from this point on, SF will stop drawing on this display
2438 } else {
2439 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002440 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002441}
2442
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002443void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2444 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002445 SurfaceFlinger& mFlinger;
2446 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002447 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002448 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002449 MessageSetPowerMode(SurfaceFlinger& flinger,
2450 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2451 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002452 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002453 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002454 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002455 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002456 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002457 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002458 ALOGW("Attempt to set power mode = %d for virtual display",
2459 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002460 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002461 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002462 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002463 return true;
2464 }
2465 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002466 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002467 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002468}
2469
2470// ---------------------------------------------------------------------------
2471
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002472status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2473{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002474 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002475
Mathias Agopianbd115332013-04-18 16:41:04 -07002476 IPCThreadState* ipc = IPCThreadState::self();
2477 const int pid = ipc->getCallingPid();
2478 const int uid = ipc->getCallingUid();
2479 if ((uid != AID_SHELL) &&
2480 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002481 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002482 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002483 } else {
Jesse Hallfcd15b42014-12-23 13:57:23 -08002484 // Try to get the main lock, but give up after one second
Mathias Agopian9795c422009-08-26 16:36:26 -07002485 // (this would indicate SF is stuck, but we want to be able to
2486 // print something in dumpsys).
Jesse Hallfcd15b42014-12-23 13:57:23 -08002487 status_t err = mStateLock.timedLock(s2ns(1));
2488 bool locked = (err == NO_ERROR);
Mathias Agopian9795c422009-08-26 16:36:26 -07002489 if (!locked) {
Jesse Hallfcd15b42014-12-23 13:57:23 -08002490 result.appendFormat(
2491 "SurfaceFlinger appears to be unresponsive (%s [%d]), "
2492 "dumping anyways (no locks held)\n", strerror(-err), err);
Mathias Agopian9795c422009-08-26 16:36:26 -07002493 }
2494
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002495 bool dumpAll = true;
2496 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002497 size_t numArgs = args.size();
2498 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002499 if ((index < numArgs) &&
2500 (args[index] == String16("--list"))) {
2501 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002502 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002503 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002504 }
2505
2506 if ((index < numArgs) &&
2507 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002508 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002509 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002510 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002511 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002512
2513 if ((index < numArgs) &&
2514 (args[index] == String16("--latency-clear"))) {
2515 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002516 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002517 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002518 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002519
2520 if ((index < numArgs) &&
2521 (args[index] == String16("--dispsync"))) {
2522 index++;
2523 mPrimaryDispSync.dump(result);
2524 dumpAll = false;
2525 }
Dan Stozab90cf072015-03-05 11:05:59 -08002526
2527 if ((index < numArgs) &&
2528 (args[index] == String16("--static-screen"))) {
2529 index++;
2530 dumpStaticScreenStats(result);
2531 dumpAll = false;
2532 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002533 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002534
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002535 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002536 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002537 }
2538
Mathias Agopian9795c422009-08-26 16:36:26 -07002539 if (locked) {
2540 mStateLock.unlock();
2541 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002542 }
2543 write(fd, result.string(), result.size());
2544 return NO_ERROR;
2545}
2546
Dan Stozac7014012014-02-14 15:03:43 -08002547void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2548 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002549{
2550 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2551 const size_t count = currentLayers.size();
2552 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002553 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002554 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002555 }
2556}
2557
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002558void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002559 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002560{
2561 String8 name;
2562 if (index < args.size()) {
2563 name = String8(args[index]);
2564 index++;
2565 }
2566
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002567 const nsecs_t period =
2568 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002569 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002570
2571 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002572 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002573 } else {
2574 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2575 const size_t count = currentLayers.size();
2576 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002577 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002578 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002579 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002580 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002581 }
2582 }
2583}
2584
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002585void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002586 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002587{
2588 String8 name;
2589 if (index < args.size()) {
2590 name = String8(args[index]);
2591 index++;
2592 }
2593
2594 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2595 const size_t count = currentLayers.size();
2596 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002597 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002598 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002599 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002600 }
2601 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002602
Svetoslavd85084b2014-03-20 10:28:31 -07002603 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002604}
2605
Jamie Gennis6547ff42013-07-16 20:12:42 -07002606// This should only be called from the main thread. Otherwise it would need
2607// the lock and should use mCurrentState rather than mDrawingState.
2608void SurfaceFlinger::logFrameStats() {
2609 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2610 const size_t count = drawingLayers.size();
2611 for (size_t i=0 ; i<count ; i++) {
2612 const sp<Layer>& layer(drawingLayers[i]);
2613 layer->logFrameStats();
2614 }
2615
2616 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2617}
2618
Andy McFadden4803b742012-09-24 19:07:20 -07002619/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2620{
2621 static const char* config =
2622 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002623#ifdef HAS_CONTEXT_PRIORITY
2624 " HAS_CONTEXT_PRIORITY"
2625#endif
2626#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2627 " NEVER_DEFAULT_TO_ASYNC_MODE"
2628#endif
2629#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2630 " TARGET_DISABLE_TRIPLE_BUFFERING"
2631#endif
2632 "]";
2633 result.append(config);
2634}
2635
Dan Stozab90cf072015-03-05 11:05:59 -08002636void SurfaceFlinger::dumpStaticScreenStats(String8& result) const
2637{
2638 result.appendFormat("Static screen stats:\n");
2639 for (size_t b = 0; b < NUM_BUCKETS - 1; ++b) {
2640 float bucketTimeSec = mFrameBuckets[b] / 1e9;
2641 float percent = 100.0f *
2642 static_cast<float>(mFrameBuckets[b]) / mTotalTime;
2643 result.appendFormat(" < %zd frames: %.3f s (%.1f%%)\n",
2644 b + 1, bucketTimeSec, percent);
2645 }
2646 float bucketTimeSec = mFrameBuckets[NUM_BUCKETS - 1] / 1e9;
2647 float percent = 100.0f *
2648 static_cast<float>(mFrameBuckets[NUM_BUCKETS - 1]) / mTotalTime;
2649 result.appendFormat(" %zd+ frames: %.3f s (%.1f%%)\n",
2650 NUM_BUCKETS - 1, bucketTimeSec, percent);
2651}
2652
Mathias Agopian74d211a2013-04-22 16:55:35 +02002653void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2654 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002655{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002656 bool colorize = false;
2657 if (index < args.size()
2658 && (args[index] == String16("--color"))) {
2659 colorize = true;
2660 index++;
2661 }
2662
2663 Colorizer colorizer(colorize);
2664
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002665 // figure out if we're stuck somewhere
2666 const nsecs_t now = systemTime();
2667 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2668 const nsecs_t inTransaction(mDebugInTransaction);
2669 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2670 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2671
2672 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002673 * Dump library configuration.
2674 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002675
2676 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002677 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002678 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002679 appendSfConfigString(result);
2680 appendUiConfigString(result);
2681 appendGuiConfigString(result);
2682 result.append("\n");
2683
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002684 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002685 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002686 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002687 result.append(SyncFeatures::getInstance().toString());
2688 result.append("\n");
2689
Andy McFadden41d67d72014-04-25 16:58:34 -07002690 colorizer.bold(result);
2691 result.append("DispSync configuration: ");
2692 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002693 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2694 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002695 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2696 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2697 result.append("\n");
2698
Dan Stozab90cf072015-03-05 11:05:59 -08002699 // Dump static screen stats
2700 result.append("\n");
2701 dumpStaticScreenStats(result);
2702 result.append("\n");
2703
Andy McFadden4803b742012-09-24 19:07:20 -07002704 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002705 * Dump the visible layer list
2706 */
2707 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2708 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002709 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002710 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002711 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002712 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002713 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002714 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002715 }
2716
2717 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002718 * Dump Display state
2719 */
2720
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002721 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002722 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002723 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002724 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2725 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002726 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002727 }
2728
2729 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002730 * Dump SurfaceFlinger global state
2731 */
2732
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002733 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002734 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002735 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002736
Mathias Agopian888c8222012-08-04 21:10:38 -07002737 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002738 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002739
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002740 colorizer.bold(result);
2741 result.appendFormat("EGL implementation : %s\n",
2742 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2743 colorizer.reset(result);
2744 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002745 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002746
Mathias Agopian875d8e12013-06-07 15:35:48 -07002747 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002748
Mathias Agopian42977342012-08-05 00:40:46 -07002749 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002750 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2751 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002752 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002753 " last eglSwapBuffers() time: %f us\n"
2754 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002755 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002756 " refresh-rate : %f fps\n"
2757 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002758 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002759 " gpu_to_cpu_unsupported : %d\n"
2760 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002761 mLastSwapBufferTime/1000.0,
2762 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002763 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002764 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2765 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002766 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002767 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002768
Mathias Agopian74d211a2013-04-22 16:55:35 +02002769 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002770 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002771
Mathias Agopian74d211a2013-04-22 16:55:35 +02002772 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002773 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002774
2775 /*
2776 * VSYNC state
2777 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002778 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002779
2780 /*
2781 * Dump HWComposer state
2782 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002783 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002784 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002785 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002786 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002787 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002788 (mDebugDisableHWC || mDebugRegion || mDaltonize
2789 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002790 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002791
2792 /*
2793 * Dump gralloc state
2794 */
2795 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2796 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002797}
2798
Mathias Agopian13127d82013-03-05 17:47:11 -08002799const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002800SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002801 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002802 wp<IBinder> dpy;
2803 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2804 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2805 dpy = mDisplays.keyAt(i);
2806 break;
2807 }
2808 }
2809 if (dpy == NULL) {
2810 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2811 // Just use the primary display so we have something to return
2812 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2813 }
2814 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002815}
2816
Keun young Park63f165f2012-08-31 10:53:36 -07002817bool SurfaceFlinger::startDdmConnection()
2818{
2819 void* libddmconnection_dso =
2820 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2821 if (!libddmconnection_dso) {
2822 return false;
2823 }
2824 void (*DdmConnection_start)(const char* name);
2825 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07002826 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07002827 if (!DdmConnection_start) {
2828 dlclose(libddmconnection_dso);
2829 return false;
2830 }
2831 (*DdmConnection_start)(getServiceName());
2832 return true;
2833}
2834
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002835status_t SurfaceFlinger::onTransact(
2836 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2837{
2838 switch (code) {
2839 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002840 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002841 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002842 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002843 case CLEAR_ANIMATION_FRAME_STATS:
2844 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002845 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002846 {
2847 // codes that require permission check
2848 IPCThreadState* ipc = IPCThreadState::self();
2849 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002850 const int uid = ipc->getCallingUid();
Jeff Brown3bfe51d2015-04-10 20:20:13 -07002851 if ((uid != AID_GRAPHICS && uid != AID_SYSTEM) &&
Mathias Agopian99b49842011-06-27 16:05:52 -07002852 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002853 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002854 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2855 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002856 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002857 break;
2858 }
2859 case CAPTURE_SCREEN:
2860 {
2861 // codes that require permission check
2862 IPCThreadState* ipc = IPCThreadState::self();
2863 const int pid = ipc->getCallingPid();
2864 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002865 if ((uid != AID_GRAPHICS) &&
2866 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002867 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002868 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2869 return PERMISSION_DENIED;
2870 }
2871 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002872 }
2873 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002874
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002875 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2876 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002877 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002878 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002879 IPCThreadState* ipc = IPCThreadState::self();
2880 const int pid = ipc->getCallingPid();
2881 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002882 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002883 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002884 return PERMISSION_DENIED;
2885 }
2886 int n;
2887 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002888 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002889 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002890 return NO_ERROR;
2891 case 1002: // SHOW_UPDATES
2892 n = data.readInt32();
2893 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002894 invalidateHwcGeometry();
2895 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002896 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002897 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002898 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002899 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002900 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002901 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002902 setTransactionFlags(
2903 eTransactionNeeded|
2904 eDisplayTransactionNeeded|
2905 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002906 return NO_ERROR;
2907 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002908 case 1006:{ // send empty update
2909 signalRefresh();
2910 return NO_ERROR;
2911 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002912 case 1008: // toggle use of hw composer
2913 n = data.readInt32();
2914 mDebugDisableHWC = n ? 1 : 0;
2915 invalidateHwcGeometry();
2916 repaintEverything();
2917 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002918 case 1009: // toggle use of transform hint
2919 n = data.readInt32();
2920 mDebugDisableTransformHint = n ? 1 : 0;
2921 invalidateHwcGeometry();
2922 repaintEverything();
2923 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002924 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002925 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002926 reply->writeInt32(0);
2927 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002928 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002929 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002930 return NO_ERROR;
2931 case 1013: {
2932 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002933 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2934 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002935 return NO_ERROR;
2936 }
2937 case 1014: {
2938 // daltonize
2939 n = data.readInt32();
2940 switch (n % 10) {
2941 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2942 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2943 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2944 }
2945 if (n >= 10) {
2946 mDaltonizer.setMode(Daltonizer::correction);
2947 } else {
2948 mDaltonizer.setMode(Daltonizer::simulation);
2949 }
2950 mDaltonize = n > 0;
2951 invalidateHwcGeometry();
2952 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002953 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002954 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002955 case 1015: {
2956 // apply a color matrix
2957 n = data.readInt32();
2958 mHasColorMatrix = n ? 1 : 0;
2959 if (n) {
2960 // color matrix is sent as mat3 matrix followed by vec3
2961 // offset, then packed into a mat4 where the last row is
2962 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002963 for (size_t i = 0 ; i < 4; i++) {
2964 for (size_t j = 0; j < 4; j++) {
2965 mColorMatrix[i][j] = data.readFloat();
2966 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002967 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002968 } else {
2969 mColorMatrix = mat4();
2970 }
2971 invalidateHwcGeometry();
2972 repaintEverything();
2973 return NO_ERROR;
2974 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002975 // This is an experimental interface
2976 // Needs to be shifted to proper binder interface when we productize
2977 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07002978 n = data.readInt32();
2979 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002980 return NO_ERROR;
2981 }
Dan Stozaee44edd2015-03-23 15:50:23 -07002982 case 1017: {
2983 n = data.readInt32();
2984 mForceFullDamage = static_cast<bool>(n);
2985 return NO_ERROR;
2986 }
Dan Stozadb4ac3c2015-04-14 11:34:01 -07002987 case 1018: { // Modify Choreographer's phase offset
2988 n = data.readInt32();
2989 mEventThread->setPhaseOffset(static_cast<nsecs_t>(n));
2990 return NO_ERROR;
2991 }
2992 case 1019: { // Modify SurfaceFlinger's phase offset
2993 n = data.readInt32();
2994 mSFEventThread->setPhaseOffset(static_cast<nsecs_t>(n));
2995 return NO_ERROR;
2996 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002997 }
2998 }
2999 return err;
3000}
3001
Mathias Agopian53331da2011-08-22 21:44:41 -07003002void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07003003 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08003004 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07003005}
3006
Mathias Agopian59119e62010-10-11 12:37:43 -07003007// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003008// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07003009// ---------------------------------------------------------------------------
3010
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003011/* The code below is here to handle b/8734824
3012 *
3013 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07003014 * from the surfaceflinger thread to the calling binder thread, where they
3015 * are executed. This allows the calling thread in the calling process to be
3016 * reused and not depend on having "enough" binder threads to handle the
3017 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003018 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003019class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07003020 /* Parts of GraphicProducerWrapper are run on two different threads,
3021 * communicating by sending messages via Looper but also by shared member
3022 * data. Coherence maintenance is subtle and in places implicit (ugh).
3023 *
3024 * Don't rely on Looper's sendMessage/handleMessage providing
3025 * release/acquire semantics for any data not actually in the Message.
3026 * Data going from surfaceflinger to binder threads needs to be
3027 * synchronized explicitly.
3028 *
3029 * Barrier open/wait do provide release/acquire semantics. This provides
3030 * implicit synchronization for data coming back from binder to
3031 * surfaceflinger threads.
3032 */
3033
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003034 sp<IGraphicBufferProducer> impl;
3035 sp<Looper> looper;
3036 status_t result;
3037 bool exitPending;
3038 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07003039 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003040 uint32_t code;
3041 Parcel const* data;
3042 Parcel* reply;
3043
3044 enum {
3045 MSG_API_CALL,
3046 MSG_EXIT
3047 };
3048
3049 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003050 * Called on surfaceflinger thread. This is called by our "fake"
3051 * BpGraphicBufferProducer. We package the data and reply Parcel and
3052 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003053 */
3054 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08003055 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003056 this->code = code;
3057 this->data = &data;
3058 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003059 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07003060 // if we've exited, we run the message synchronously right here.
3061 // note (JH): as far as I can tell from looking at the code, this
3062 // never actually happens. if it does, i'm not sure if it happens
3063 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003064 handleMessage(Message(MSG_API_CALL));
3065 } else {
3066 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07003067 // Prevent stores to this->{code, data, reply} from being
3068 // reordered later than the construction of Message.
3069 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003070 looper->sendMessage(this, Message(MSG_API_CALL));
3071 barrier.wait();
3072 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08003073 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003074 }
3075
3076 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003077 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003078 * call the real BpGraphicBufferProducer.
3079 */
3080 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07003081 int what = message.what;
3082 // Prevent reads below from happening before the read from Message
3083 atomic_thread_fence(memory_order_acquire);
3084 if (what == MSG_API_CALL) {
Marco Nelissen097ca272014-11-14 08:01:01 -08003085 result = IInterface::asBinder(impl)->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003086 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07003087 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003088 exitRequested = true;
3089 }
3090 }
3091
3092public:
Jesse Hallb154c422014-07-13 12:47:02 -07003093 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
3094 : impl(impl),
3095 looper(new Looper(true)),
3096 exitPending(false),
3097 exitRequested(false)
3098 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003099
Jesse Hallb154c422014-07-13 12:47:02 -07003100 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003101 status_t waitForResponse() {
3102 do {
3103 looper->pollOnce(-1);
3104 } while (!exitRequested);
3105 return result;
3106 }
3107
Jesse Hallb154c422014-07-13 12:47:02 -07003108 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003109 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07003110 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003111 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07003112 // Ensure this->result is visible to the binder thread before it
3113 // handles the message.
3114 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003115 looper->sendMessage(this, Message(MSG_EXIT));
3116 }
3117};
3118
3119
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003120status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
3121 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003122 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003123 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003124 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003125
3126 if (CC_UNLIKELY(display == 0))
3127 return BAD_VALUE;
3128
3129 if (CC_UNLIKELY(producer == 0))
3130 return BAD_VALUE;
3131
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003132 // if we have secure windows on this display, never allow the screen capture
3133 // unless the producer interface is local (i.e.: we can take a screenshot for
3134 // ourselves).
Marco Nelissen097ca272014-11-14 08:01:01 -08003135 if (!IInterface::asBinder(producer)->localBinder()) {
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003136 Mutex::Autolock _l(mStateLock);
3137 sp<const DisplayDevice> hw(getDisplayDevice(display));
3138 if (hw->getSecureLayerVisible()) {
3139 ALOGW("FB is protected: PERMISSION_DENIED");
3140 return PERMISSION_DENIED;
3141 }
3142 }
3143
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003144 // Convert to surfaceflinger's internal rotation type.
3145 Transform::orientation_flags rotationFlags;
3146 switch (rotation) {
3147 case ISurfaceComposer::eRotateNone:
3148 rotationFlags = Transform::ROT_0;
3149 break;
3150 case ISurfaceComposer::eRotate90:
3151 rotationFlags = Transform::ROT_90;
3152 break;
3153 case ISurfaceComposer::eRotate180:
3154 rotationFlags = Transform::ROT_180;
3155 break;
3156 case ISurfaceComposer::eRotate270:
3157 rotationFlags = Transform::ROT_270;
3158 break;
3159 default:
3160 rotationFlags = Transform::ROT_0;
3161 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3162 break;
3163 }
3164
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003165 class MessageCaptureScreen : public MessageBase {
3166 SurfaceFlinger* flinger;
3167 sp<IBinder> display;
3168 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003169 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003170 uint32_t reqWidth, reqHeight;
3171 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003172 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003173 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003174 status_t result;
3175 public:
3176 MessageCaptureScreen(SurfaceFlinger* flinger,
3177 const sp<IBinder>& display,
3178 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003179 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003180 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003181 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003182 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003183 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003184 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003185 useIdentityTransform(useIdentityTransform),
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003186 rotation(rotation),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003187 result(PERMISSION_DENIED)
3188 {
3189 }
3190 status_t getResult() const {
3191 return result;
3192 }
3193 virtual bool handler() {
3194 Mutex::Autolock _l(flinger->mStateLock);
3195 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003196 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003197 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003198 useIdentityTransform, rotation);
Marco Nelissen097ca272014-11-14 08:01:01 -08003199 static_cast<GraphicProducerWrapper*>(IInterface::asBinder(producer).get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003200 return true;
3201 }
3202 };
3203
Mathias Agopian9eb1f052013-04-10 16:27:17 -07003204 // make sure to process transactions before screenshots -- a transaction
3205 // might already be pending but scheduled for VSYNC; this guarantees we
3206 // will handle it before the screenshot. When VSYNC finally arrives
3207 // the scheduled transaction will be a no-op. If no transactions are
3208 // scheduled at this time, this will end-up being a no-op as well.
3209 mEventQueue.invalidateTransactionNow();
3210
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003211 // this creates a "fake" BBinder which will serve as a "fake" remote
3212 // binder to receive the marshaled calls and forward them to the
3213 // real remote (a BpGraphicBufferProducer)
3214 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3215
3216 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3217 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003218 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003219 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003220 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003221 useIdentityTransform, rotationFlags);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003222
3223 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003224 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003225 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003226 }
3227 return res;
3228}
3229
Mathias Agopian180f10d2013-04-10 22:55:41 -07003230
3231void SurfaceFlinger::renderScreenImplLocked(
3232 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003233 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003234 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003235 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003236{
3237 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003238 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003239
3240 // get screen geometry
Andreas Gampe89fd4f72014-11-13 14:18:56 -08003241 const int32_t hw_w = hw->getWidth();
3242 const int32_t hw_h = hw->getHeight();
3243 const bool filtering = static_cast<int32_t>(reqWidth) != hw_w ||
Christopher Ferris0e749792015-03-23 14:32:15 -07003244 static_cast<int32_t>(reqHeight) != hw_h;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003245
Dan Stozac1879002014-05-22 15:59:05 -07003246 // if a default or invalid sourceCrop is passed in, set reasonable values
3247 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3248 !sourceCrop.isValid()) {
3249 sourceCrop.setLeftTop(Point(0, 0));
3250 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3251 }
3252
3253 // ensure that sourceCrop is inside screen
3254 if (sourceCrop.left < 0) {
3255 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3256 }
Dan Stozabe31f442014-06-11 11:20:54 -07003257 if (sourceCrop.right > hw_w) {
3258 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003259 }
3260 if (sourceCrop.top < 0) {
3261 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3262 }
Dan Stozabe31f442014-06-11 11:20:54 -07003263 if (sourceCrop.bottom > hw_h) {
3264 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003265 }
3266
Mathias Agopian180f10d2013-04-10 22:55:41 -07003267 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003268 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003269
3270 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003271 engine.setViewportAndProjection(
3272 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003273 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003274
3275 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003276 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003277
3278 const LayerVector& layers( mDrawingState.layersSortedByZ );
3279 const size_t count = layers.size();
3280 for (size_t i=0 ; i<count ; ++i) {
3281 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003282 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003283 if (state.layerStack == hw->getLayerStack()) {
3284 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3285 if (layer->isVisible()) {
3286 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08003287 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003288 if (filtering) layer->setFiltering(false);
3289 }
3290 }
3291 }
3292 }
3293
3294 // compositionComplete is needed for older driver
3295 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003296 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003297}
3298
3299
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003300status_t SurfaceFlinger::captureScreenImplLocked(
3301 const sp<const DisplayDevice>& hw,
3302 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003303 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003304 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003305 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003306{
3307 ATRACE_CALL();
3308
Mathias Agopian180f10d2013-04-10 22:55:41 -07003309 // get screen geometry
3310 const uint32_t hw_w = hw->getWidth();
3311 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003312
Mathias Agopian180f10d2013-04-10 22:55:41 -07003313 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3314 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3315 reqWidth, reqHeight, hw_w, hw_h);
3316 return BAD_VALUE;
3317 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003318
Mathias Agopian180f10d2013-04-10 22:55:41 -07003319 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3320 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3321
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003322 // create a surface (because we're a producer, and we need to
3323 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003324 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003325 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003326
3327 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003328 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003329 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3330 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003331
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003332 int err = 0;
3333 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003334 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003335 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3336 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003337
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003338 if (err == NO_ERROR) {
3339 ANativeWindowBuffer* buffer;
3340 /* TODO: Once we have the sync framework everywhere this can use
3341 * server-side waits on the fence that dequeueBuffer returns.
3342 */
3343 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3344 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003345 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003346 // create an EGLImage from the buffer so we can later
3347 // turn it into a texture
3348 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3349 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3350 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003351 // this binds the given EGLImage as a framebuffer for the
3352 // duration of this scope.
3353 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3354 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003355 // this will in fact render into our dequeued buffer
3356 // via an FBO, which means we didn't have to create
3357 // an EGLSurface and therefore we're not
3358 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003359 renderScreenImplLocked(
3360 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3361 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003362
Riley Andrews86639902014-08-15 12:27:24 -07003363 // Attempt to create a sync khr object that can produce a sync point. If that
3364 // isn't available, create a non-dupable sync object in the fallback path and
3365 // wait on it directly.
3366 EGLSyncKHR sync;
3367 if (!DEBUG_SCREENSHOTS) {
3368 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Riley Andrews9707f4d2014-10-23 16:17:04 -07003369 // native fence fd will not be populated until flush() is done.
3370 getRenderEngine().flush();
Andy McFadden2d8d1202013-10-09 16:38:02 -07003371 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003372 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003373 }
Riley Andrews86639902014-08-15 12:27:24 -07003374 if (sync != EGL_NO_SYNC_KHR) {
3375 // get the sync fd
3376 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3377 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3378 ALOGW("captureScreen: failed to dup sync khr object");
3379 syncFd = -1;
3380 }
3381 eglDestroySyncKHR(mEGLDisplay, sync);
3382 } else {
3383 // fallback path
3384 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3385 if (sync != EGL_NO_SYNC_KHR) {
3386 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3387 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3388 EGLint eglErr = eglGetError();
3389 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3390 ALOGW("captureScreen: fence wait timed out");
3391 } else {
3392 ALOGW_IF(eglErr != EGL_SUCCESS,
3393 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3394 }
3395 eglDestroySyncKHR(mEGLDisplay, sync);
3396 } else {
3397 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3398 }
3399 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003400 if (DEBUG_SCREENSHOTS) {
3401 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3402 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3403 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3404 hw, minLayerZ, maxLayerZ);
3405 delete [] pixels;
3406 }
3407
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003408 } else {
3409 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3410 result = INVALID_OPERATION;
3411 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003412 // destroy our image
3413 eglDestroyImageKHR(mEGLDisplay, image);
3414 } else {
3415 result = BAD_VALUE;
3416 }
Jesse Hallafe2b1f2014-10-21 11:09:17 -07003417 // queueBuffer takes ownership of syncFd
Riley Andrews86639902014-08-15 12:27:24 -07003418 window->queueBuffer(window, buffer, syncFd);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003419 }
3420 } else {
3421 result = BAD_VALUE;
3422 }
3423 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3424 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003425
Mathias Agopian74c40c02010-09-29 13:02:36 -07003426 return result;
3427}
3428
Mathias Agopiand5556842013-09-19 17:08:37 -07003429void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3430 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003431 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003432 for (size_t y=0 ; y<h ; y++) {
3433 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3434 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003435 if (p[x] != 0xFF000000) return;
3436 }
3437 }
3438 ALOGE("*** we just took a black screenshot ***\n"
3439 "requested minz=%d, maxz=%d, layerStack=%d",
3440 minLayerZ, maxLayerZ, hw->getLayerStack());
3441 const LayerVector& layers( mDrawingState.layersSortedByZ );
3442 const size_t count = layers.size();
3443 for (size_t i=0 ; i<count ; ++i) {
3444 const sp<Layer>& layer(layers[i]);
3445 const Layer::State& state(layer->getDrawingState());
3446 const bool visible = (state.layerStack == hw->getLayerStack())
3447 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3448 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003449 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003450 visible ? '+' : '-',
3451 i, layer->getName().string(), state.layerStack, state.z,
3452 layer->isVisible(), state.flags, state.alpha);
3453 }
3454 }
3455}
3456
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003457// ---------------------------------------------------------------------------
3458
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003459SurfaceFlinger::LayerVector::LayerVector() {
3460}
3461
3462SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003463 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003464}
3465
3466int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3467 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003468{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003469 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003470 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3471 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003472
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003473 uint32_t ls = l->getCurrentState().layerStack;
3474 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003475 if (ls != rs)
3476 return ls - rs;
3477
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003478 uint32_t lz = l->getCurrentState().z;
3479 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003480 if (lz != rz)
3481 return lz - rz;
3482
3483 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003484}
3485
3486// ---------------------------------------------------------------------------
3487
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003488SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Michael Lentine47e45402014-07-18 15:34:25 -07003489 : type(DisplayDevice::DISPLAY_ID_INVALID), width(0), height(0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003490}
3491
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003492SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Michael Lentine47e45402014-07-18 15:34:25 -07003493 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0), width(0), height(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003494 viewport.makeInvalid();
3495 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003496}
3497
3498// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003499
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003500}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003501
3502
3503#if defined(__gl_h_)
3504#error "don't include gl/gl.h in this file"
3505#endif
3506
3507#if defined(__gl2_h_)
3508#error "don't include gl2/gl2.h in this file"
3509#endif