blob: 5fdb2555b9cde6a40d0b9687e2080311b5c2423b [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>
Yusuke Sato0a688f52015-07-30 23:05:39 -070055#include <utils/Timers.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080056#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
Mathias Agopian921e6ac2012-07-23 23:11:29 -070058#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070059#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
Mathias Agopian3e25fd82013-04-22 17:52:16 +020061#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020063#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080064#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070065#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070066#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070067#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080068#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072
Mathias Agopiana4912602012-07-12 14:25:33 -070073#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070074#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070075#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076
Mathias Agopianff2ed702013-09-01 21:36:12 -070077#include "Effects/Daltonizer.h"
78
Mathias Agopian875d8e12013-06-07 15:35:48 -070079#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070080#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070081
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082#define DISPLAY_COUNT 1
83
Mathias Agopianfee2b462013-07-03 12:34:01 -070084/*
85 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
86 * black pixels.
87 */
88#define DEBUG_SCREENSHOTS false
89
Mathias Agopianca088332013-03-28 17:44:13 -070090EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
91
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070093
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070094// This is the phase offset in nanoseconds of the software vsync event
95// relative to the vsync event reported by HWComposer. The software vsync
96// event is when SurfaceFlinger and Choreographer-based applications run each
97// frame.
98//
99// This phase offset allows adjustment of the minimum latency from application
100// wake-up (by Choregographer) time to the time at which the resulting window
101// image is displayed. This value may be either positive (after the HW vsync)
102// or negative (before the HW vsync). Setting it to 0 will result in a
103// minimum latency of two vsync periods because the app and SurfaceFlinger
104// will run just after the HW vsync. Setting it to a positive number will
105// result in the minimum latency being:
106//
107// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
108//
109// Note that reducing this latency makes it more likely for the applications
110// to not have their window content image ready in time. When this happens
111// the latency will end up being an additional vsync period, and animations
112// will hiccup. Therefore, this latency should be tuned somewhat
113// conservatively (or at least with awareness of the trade-off being made).
114static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
115
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700116// This is the phase offset at which SurfaceFlinger's composition runs.
117static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
118
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119// ---------------------------------------------------------------------------
120
Mathias Agopian99b49842011-06-27 16:05:52 -0700121const String16 sHardwareTest("android.permission.HARDWARE_TEST");
122const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
123const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
124const String16 sDump("android.permission.DUMP");
125
126// ---------------------------------------------------------------------------
127
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700129 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700131 mTransactionPending(false),
132 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700133 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700134 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700135 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700138 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800139 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700141 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700142 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700143 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700144 mDebugInSwapBuffers(0),
145 mLastSwapBufferTime(0),
146 mDebugInTransaction(0),
147 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700148 mBootFinished(false),
Dan Stozaee44edd2015-03-23 15:50:23 -0700149 mForceFullDamage(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700150 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700151 mHWVsyncAvailable(false),
Alan Viverette9c5a3332013-09-12 20:04:35 -0700152 mDaltonize(false),
Dan Stozab90cf072015-03-05 11:05:59 -0800153 mHasColorMatrix(false),
154 mHasPoweredOff(false),
155 mFrameBuckets(),
156 mTotalTime(0),
157 mLastSwapTime(0)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158{
Steve Blocka19954a2012-01-04 20:05:49 +0000159 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160
161 // debugging stuff...
162 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700163
Mathias Agopianb4b17302013-03-20 18:36:41 -0700164 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700165 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700166
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 property_get("debug.sf.showupdates", value, "0");
168 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700169
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700170 property_get("debug.sf.ddms", value, "0");
171 mDebugDDMS = atoi(value);
172 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700173 if (!startDdmConnection()) {
174 // start failed, and DDMS debugging not enabled
175 mDebugDDMS = 0;
176 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700177 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700178 ALOGI_IF(mDebugRegion, "showupdates enabled");
179 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180}
181
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800182void SurfaceFlinger::onFirstRef()
183{
184 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800185}
186
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187SurfaceFlinger::~SurfaceFlinger()
188{
Mathias Agopiana4912602012-07-12 14:25:33 -0700189 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
190 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
191 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192}
193
Dan Stozac7014012014-02-14 15:03:43 -0800194void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800195{
196 // the window manager died on us. prepare its eulogy.
197
Andy McFadden13a082e2012-08-24 10:16:42 -0700198 // restore initial conditions (default device unblank, etc)
199 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800200
201 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700202 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800203}
204
Mathias Agopian7e27f052010-05-28 14:22:23 -0700205sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206{
Mathias Agopian96f08192010-06-02 23:28:45 -0700207 sp<ISurfaceComposerClient> bclient;
208 sp<Client> client(new Client(this));
209 status_t err = client->initCheck();
210 if (err == NO_ERROR) {
211 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 return bclient;
214}
215
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700216sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
217 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700218{
219 class DisplayToken : public BBinder {
220 sp<SurfaceFlinger> flinger;
221 virtual ~DisplayToken() {
222 // no more references, this display must be terminated
223 Mutex::Autolock _l(flinger->mStateLock);
224 flinger->mCurrentState.displays.removeItem(this);
225 flinger->setTransactionFlags(eDisplayTransactionNeeded);
226 }
227 public:
228 DisplayToken(const sp<SurfaceFlinger>& flinger)
229 : flinger(flinger) {
230 }
231 };
232
233 sp<BBinder> token = new DisplayToken(this);
234
235 Mutex::Autolock _l(mStateLock);
Pablo Ceballos53390e12015-08-04 11:25:59 -0700236 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL, secure);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700237 info.displayName = displayName;
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();
Jesse Hall692c7232012-11-08 15:41:56 -0800266 // All non-virtual displays are currently considered secure.
Pablo Ceballos53390e12015-08-04 11:25:59 -0700267 DisplayDeviceState info(type, true);
Jesse Hall692c7232012-11-08 15:41:56 -0800268 mCurrentState.displays.add(mBuiltinDisplays[type], info);
269}
270
Mathias Agopiane57f2922012-08-09 16:29:12 -0700271sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700272 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700273 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
274 return NULL;
275 }
Jesse Hall692c7232012-11-08 15:41:56 -0800276 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700277}
278
Jamie Gennis9a78c902011-01-12 18:30:40 -0800279sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
280{
281 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
282 return gba;
283}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700284
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285void SurfaceFlinger::bootFinished()
286{
287 const nsecs_t now = systemTime();
288 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000289 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700290 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700291
292 // wait patiently for the window manager death
293 const String16 name("window");
294 sp<IBinder> window(defaultServiceManager()->getService(name));
295 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700296 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700297 }
298
299 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700300 // formerly we would just kill the process, but we now ask it to exit so it
301 // can choose where to stop the animation.
302 property_set("service.bootanim.exit", "1");
Yusuke Sato0a688f52015-07-30 23:05:39 -0700303
304 const int LOGTAG_SF_STOP_BOOTANIM = 60110;
305 LOG_EVENT_LONG(LOGTAG_SF_STOP_BOOTANIM,
306 ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800307}
308
Mathias Agopian3f844832013-08-07 21:24:32 -0700309void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700310 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700311 RenderEngine& engine;
312 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700313 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700314 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
315 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700316 }
317 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700318 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700319 return true;
320 }
321 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700322 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700323}
324
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700325class DispSyncSource : public VSyncSource, private DispSync::Callback {
326public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700327 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
328 const char* label) :
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700329 mValue(0),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700330 mTraceVsync(traceVsync),
Andy McFadden5167ec62014-05-22 13:08:43 -0700331 mVsyncOnLabel(String8::format("VsyncOn-%s", label)),
332 mVsyncEventLabel(String8::format("VSYNC-%s", label)),
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700333 mDispSync(dispSync),
334 mCallbackMutex(),
335 mCallback(),
336 mVsyncMutex(),
337 mPhaseOffset(phaseOffset),
338 mEnabled(false) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700339
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700340 virtual ~DispSyncSource() {}
341
342 virtual void setVSyncEnabled(bool enable) {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700343 Mutex::Autolock lock(mVsyncMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700344 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700345 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700346 static_cast<DispSync::Callback*>(this));
347 if (err != NO_ERROR) {
348 ALOGE("error registering vsync callback: %s (%d)",
349 strerror(-err), err);
350 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700351 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700352 } else {
353 status_t err = mDispSync->removeEventListener(
354 static_cast<DispSync::Callback*>(this));
355 if (err != NO_ERROR) {
356 ALOGE("error unregistering vsync callback: %s (%d)",
357 strerror(-err), err);
358 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700359 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700360 }
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700361 mEnabled = enable;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700362 }
363
364 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700365 Mutex::Autolock lock(mCallbackMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700366 mCallback = callback;
367 }
368
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700369 virtual void setPhaseOffset(nsecs_t phaseOffset) {
370 Mutex::Autolock lock(mVsyncMutex);
371
372 // Normalize phaseOffset to [0, period)
373 auto period = mDispSync->getPeriod();
374 phaseOffset %= period;
375 if (phaseOffset < 0) {
376 // If we're here, then phaseOffset is in (-period, 0). After this
377 // operation, it will be in (0, period)
378 phaseOffset += period;
379 }
380 mPhaseOffset = phaseOffset;
381
382 // If we're not enabled, we don't need to mess with the listeners
383 if (!mEnabled) {
384 return;
385 }
386
387 // Remove the listener with the old offset
388 status_t err = mDispSync->removeEventListener(
389 static_cast<DispSync::Callback*>(this));
390 if (err != NO_ERROR) {
391 ALOGE("error unregistering vsync callback: %s (%d)",
392 strerror(-err), err);
393 }
394
395 // Add a listener with the new offset
396 err = mDispSync->addEventListener(mPhaseOffset,
397 static_cast<DispSync::Callback*>(this));
398 if (err != NO_ERROR) {
399 ALOGE("error registering vsync callback: %s (%d)",
400 strerror(-err), err);
401 }
402 }
403
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700404private:
405 virtual void onDispSyncEvent(nsecs_t when) {
406 sp<VSyncSource::Callback> callback;
407 {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700408 Mutex::Autolock lock(mCallbackMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700409 callback = mCallback;
410
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700411 if (mTraceVsync) {
412 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700413 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700414 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700415 }
416
417 if (callback != NULL) {
418 callback->onVSyncEvent(when);
419 }
420 }
421
422 int mValue;
423
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700424 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700425 const String8 mVsyncOnLabel;
426 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700427
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700428 DispSync* mDispSync;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700429
430 Mutex mCallbackMutex; // Protects the following
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700431 sp<VSyncSource::Callback> mCallback;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700432
433 Mutex mVsyncMutex; // Protects the following
434 nsecs_t mPhaseOffset;
435 bool mEnabled;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700436};
437
438void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700439 ALOGI( "SurfaceFlinger's main thread ready to run. "
440 "Initializing graphics H/W...");
441
Jesse Hall692c7232012-11-08 15:41:56 -0800442 Mutex::Autolock _l(mStateLock);
443
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700444 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700445 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
446 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700447
Saurabh Shahf9481052015-06-15 17:55:51 -0700448 // start the EventThread
449 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
450 vsyncPhaseOffsetNs, true, "app");
451 mEventThread = new EventThread(vsyncSrc);
452 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
453 sfVsyncPhaseOffsetNs, true, "sf");
454 mSFEventThread = new EventThread(sfVsyncSrc);
455 mEventQueue.setEventThread(mSFEventThread);
456
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700457 // Initialize the H/W composer object. There may or may not be an
458 // actual hardware composer underneath.
459 mHwc = new HWComposer(this,
460 *static_cast<HWComposer::EventHandler *>(this));
461
Mathias Agopian875d8e12013-06-07 15:35:48 -0700462 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800463 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700464
465 // retrieve the EGL context that was selected/created
466 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700467
Mathias Agopianda27af92012-09-13 18:17:13 -0700468 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
469 "couldn't create EGLContext");
470
Mathias Agopiancde87a32012-09-13 14:09:01 -0700471 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700472 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700473 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700474 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700475 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700476 // All non-virtual displays are currently considered secure.
477 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800478 createBuiltinDisplayLocked(type);
479 wp<IBinder> token = mBuiltinDisplays[i];
480
Dan Stozab9b08832014-03-13 11:55:57 -0700481 sp<IGraphicBufferProducer> producer;
482 sp<IGraphicBufferConsumer> consumer;
483 BufferQueue::createBufferQueue(&producer, &consumer,
484 new GraphicBufferAlloc());
485
486 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
487 consumer);
Jesse Hall19e87292013-12-23 21:02:15 -0800488 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700489 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800490 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Dan Stozab9b08832014-03-13 11:55:57 -0700491 fbs, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -0800492 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700493 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700494 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700495 // for displays other than the main display, so we always
496 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800497 ALOGD("marking display %zu as acquired/unblanked", i);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700498 hw->setPowerMode(HWC_POWER_MODE_NORMAL);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700499 }
500 mDisplays.add(token, hw);
501 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700502 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700503
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700504 // make the GLContext current so that we can create textures when creating Layers
505 // (which may happens before we render something)
506 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
507
Jamie Gennisd1700752013-10-14 12:22:52 -0700508 mEventControlThread = new EventControlThread(this);
509 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
510
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700511 // set a fake vsync period if there is no HWComposer
512 if (mHwc->initCheck() != NO_ERROR) {
513 mPrimaryDispSync.setPeriod(16666667);
514 }
515
Mathias Agopian92a979a2012-08-02 18:32:23 -0700516 // initialize our drawing state
517 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700518
Andy McFadden13a082e2012-08-24 10:16:42 -0700519 // set initial conditions (e.g. unblank default device)
520 initializeDisplays();
521
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700522 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700523 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800524}
525
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700526int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700527 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700528 type : mHwc->allocateDisplayId();
529}
530
Mathias Agopiana67e4182012-06-19 17:26:12 -0700531void SurfaceFlinger::startBootAnim() {
532 // start boot animation
533 property_set("service.bootanim.exit", "0");
534 property_set("ctl.start", "bootanim");
535}
536
Mathias Agopian875d8e12013-06-07 15:35:48 -0700537size_t SurfaceFlinger::getMaxTextureSize() const {
538 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700539}
540
Mathias Agopian875d8e12013-06-07 15:35:48 -0700541size_t SurfaceFlinger::getMaxViewportDims() const {
542 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700543}
544
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800545// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800546
Jamie Gennis582270d2011-08-17 18:19:00 -0700547bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800548 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800549 Mutex::Autolock _l(mStateLock);
Marco Nelissen097ca272014-11-14 08:01:01 -0800550 sp<IBinder> surfaceTextureBinder(IInterface::asBinder(bufferProducer));
Mathias Agopian67106042013-03-14 19:18:13 -0700551 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800552}
553
Dan Stoza7f7da322014-05-02 15:26:25 -0700554status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
555 Vector<DisplayInfo>* configs) {
Tatenda Chipeperekwa23e16bb2014-10-29 16:47:19 -0700556 if ((configs == NULL) || (display.get() == NULL)) {
Dan Stoza7f7da322014-05-02 15:26:25 -0700557 return BAD_VALUE;
558 }
559
Naseer Ahmed7aa0c472014-11-03 14:49:23 -0500560 if (!display.get())
561 return NAME_NOT_FOUND;
562
Jesse Hall692c7232012-11-08 15:41:56 -0800563 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700564 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800565 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700566 type = i;
567 break;
568 }
569 }
570
571 if (type < 0) {
572 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700573 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700574
Mathias Agopian8b736f12012-08-13 17:54:26 -0700575 // TODO: Not sure if display density should handled by SF any longer
576 class Density {
577 static int getDensityFromProperty(char const* propName) {
578 char property[PROPERTY_VALUE_MAX];
579 int density = 0;
580 if (property_get(propName, property, NULL) > 0) {
581 density = atoi(property);
582 }
583 return density;
584 }
585 public:
586 static int getEmuDensity() {
587 return getDensityFromProperty("qemu.sf.lcd_density"); }
588 static int getBuildDensity() {
589 return getDensityFromProperty("ro.sf.lcd_density"); }
590 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700591
Dan Stoza7f7da322014-05-02 15:26:25 -0700592 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700593
Dan Stoza7f7da322014-05-02 15:26:25 -0700594 const Vector<HWComposer::DisplayConfig>& hwConfigs =
595 getHwComposer().getConfigs(type);
596 for (size_t c = 0; c < hwConfigs.size(); ++c) {
597 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
598 DisplayInfo info = DisplayInfo();
599
600 float xdpi = hwConfig.xdpi;
601 float ydpi = hwConfig.ydpi;
602
603 if (type == DisplayDevice::DISPLAY_PRIMARY) {
604 // The density of the device is provided by a build property
605 float density = Density::getBuildDensity() / 160.0f;
606 if (density == 0) {
607 // the build doesn't provide a density -- this is wrong!
608 // use xdpi instead
609 ALOGE("ro.sf.lcd_density must be defined as a build property");
610 density = xdpi / 160.0f;
611 }
612 if (Density::getEmuDensity()) {
613 // if "qemu.sf.lcd_density" is specified, it overrides everything
614 xdpi = ydpi = density = Density::getEmuDensity();
615 density /= 160.0f;
616 }
617 info.density = density;
618
619 // TODO: this needs to go away (currently needed only by webkit)
620 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
621 info.orientation = hw->getOrientation();
622 } else {
623 // TODO: where should this value come from?
624 static const int TV_DENSITY = 213;
625 info.density = TV_DENSITY / 160.0f;
626 info.orientation = 0;
627 }
628
629 info.w = hwConfig.width;
630 info.h = hwConfig.height;
631 info.xdpi = xdpi;
632 info.ydpi = ydpi;
633 info.fps = float(1e9 / hwConfig.refresh);
Andy McFadden91b2ca82014-06-13 14:04:23 -0700634 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
635
636 // This is how far in advance a buffer must be queued for
637 // presentation at a given time. If you want a buffer to appear
638 // on the screen at time N, you must submit the buffer before
639 // (N - presentationDeadline).
640 //
641 // Normally it's one full refresh period (to give SF a chance to
642 // latch the buffer), but this can be reduced by configuring a
643 // DispSync offset. Any additional delays introduced by the hardware
644 // composer or panel must be accounted for here.
645 //
646 // We add an additional 1ms to allow for processing time and
647 // differences between the ideal and actual refresh rate.
648 info.presentationDeadline =
649 hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700650
651 // All non-virtual displays are currently considered secure.
652 info.secure = true;
653
654 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700655 }
656
Dan Stoza7f7da322014-05-02 15:26:25 -0700657 return NO_ERROR;
658}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700659
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800660status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& /* display */,
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700661 DisplayStatInfo* stats) {
662 if (stats == NULL) {
663 return BAD_VALUE;
664 }
665
666 // FIXME for now we always return stats for the primary display
667 memset(stats, 0, sizeof(*stats));
668 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
669 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
670 return NO_ERROR;
671}
672
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700673int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
Dan Stoza24a42e92015-03-09 10:04:11 -0700674 sp<DisplayDevice> device(getDisplayDevice(display));
675 if (device != NULL) {
676 return device->getActiveConfig();
677 }
678 return BAD_VALUE;
Dan Stoza7f7da322014-05-02 15:26:25 -0700679}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700680
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700681void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
682 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
683 this);
684 int32_t type = hw->getDisplayType();
685 int currentMode = hw->getActiveConfig();
686
687 if (mode == currentMode) {
688 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
689 return;
690 }
691
692 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
693 ALOGW("Trying to set config for virtual display");
694 return;
695 }
696
697 hw->setActiveConfig(mode);
698 getHwComposer().setActiveConfig(type, mode);
699}
700
701status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
702 class MessageSetActiveConfig: public MessageBase {
703 SurfaceFlinger& mFlinger;
704 sp<IBinder> mDisplay;
705 int mMode;
706 public:
707 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
708 int mode) :
709 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
710 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700711 Vector<DisplayInfo> configs;
712 mFlinger.getDisplayConfigs(mDisplay, &configs);
Jesse Hall78442112014-08-07 22:43:06 -0700713 if (mMode < 0 || mMode >= static_cast<int>(configs.size())) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700714 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700715 mMode, configs.size());
716 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700717 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
718 if (hw == NULL) {
719 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700720 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700721 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
722 ALOGW("Attempt to set active config = %d for virtual display",
723 mMode);
724 } else {
725 mFlinger.setActiveConfigInternal(hw, mMode);
726 }
727 return true;
728 }
729 };
730 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
731 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700732 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700733}
734
Svetoslavd85084b2014-03-20 10:28:31 -0700735status_t SurfaceFlinger::clearAnimationFrameStats() {
736 Mutex::Autolock _l(mStateLock);
737 mAnimFrameTracker.clearStats();
738 return NO_ERROR;
739}
740
741status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
742 Mutex::Autolock _l(mStateLock);
743 mAnimFrameTracker.getStats(outStats);
744 return NO_ERROR;
745}
746
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800747// ----------------------------------------------------------------------------
748
749sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800750 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700751}
752
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800753// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800754
755void SurfaceFlinger::waitForEvent() {
756 mEventQueue.waitMessage();
757}
758
759void SurfaceFlinger::signalTransaction() {
760 mEventQueue.invalidate();
761}
762
763void SurfaceFlinger::signalLayerUpdate() {
764 mEventQueue.invalidate();
765}
766
767void SurfaceFlinger::signalRefresh() {
768 mEventQueue.refresh();
769}
770
771status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800772 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800773 return mEventQueue.postMessage(msg, reltime);
774}
775
776status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800777 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800778 status_t res = mEventQueue.postMessage(msg, reltime);
779 if (res == NO_ERROR) {
780 msg->wait();
781 }
782 return res;
783}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800784
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700785void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700786 do {
787 waitForEvent();
788 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800789}
790
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700791void SurfaceFlinger::enableHardwareVsync() {
792 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700793 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700794 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700795 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
796 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700797 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700798 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700799}
800
Jesse Hall948fe0c2013-10-14 12:56:09 -0700801void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700802 Mutex::Autolock _l(mHWVsyncLock);
803
Jesse Hall948fe0c2013-10-14 12:56:09 -0700804 if (makeAvailable) {
805 mHWVsyncAvailable = true;
806 } else if (!mHWVsyncAvailable) {
807 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
808 return;
809 }
810
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700811 const nsecs_t period =
812 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
813
814 mPrimaryDispSync.reset();
815 mPrimaryDispSync.setPeriod(period);
816
817 if (!mPrimaryHWVsyncEnabled) {
818 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700819 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
820 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700821 mPrimaryHWVsyncEnabled = true;
822 }
823}
824
Jesse Hall948fe0c2013-10-14 12:56:09 -0700825void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700826 Mutex::Autolock _l(mHWVsyncLock);
827 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700828 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
829 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700830 mPrimaryDispSync.endResync();
831 mPrimaryHWVsyncEnabled = false;
832 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700833 if (makeUnavailable) {
834 mHWVsyncAvailable = false;
835 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700836}
837
838void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700839 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700840
Jamie Gennisd1700752013-10-14 12:22:52 -0700841 { // Scope for the lock
842 Mutex::Autolock _l(mHWVsyncLock);
843 if (type == 0 && mPrimaryHWVsyncEnabled) {
844 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700845 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700846 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700847
848 if (needsHwVsync) {
849 enableHardwareVsync();
850 } else {
851 disableHardwareVsync(false);
852 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700853}
854
855void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
856 if (mEventThread == NULL) {
857 // This is a temporary workaround for b/7145521. A non-null pointer
858 // does not mean EventThread has finished initializing, so this
859 // is not a correct fix.
860 ALOGW("WARNING: EventThread not started, ignoring hotplug");
861 return;
862 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700863
Jesse Hall9e663de2013-08-16 14:28:37 -0700864 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700865 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800866 if (connected) {
867 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700868 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800869 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
870 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700871 }
872 setTransactionFlags(eDisplayTransactionNeeded);
873
Andy McFadden9e9689c2012-10-10 18:17:51 -0700874 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700875 }
Mathias Agopian86303202012-07-24 22:46:10 -0700876}
877
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700878void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700879 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700880 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700881}
882
Mathias Agopian4fec8732012-06-29 14:12:52 -0700883void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800884 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800885 switch (what) {
Dan Stoza6b9454d2014-11-07 16:00:59 -0800886 case MessageQueue::TRANSACTION: {
887 handleMessageTransaction();
888 break;
889 }
890 case MessageQueue::INVALIDATE: {
891 bool refreshNeeded = handleMessageTransaction();
892 refreshNeeded |= handleMessageInvalidate();
Dan Stoza58784442014-12-02 16:58:17 -0800893 refreshNeeded |= mRepaintEverything;
Dan Stoza6b9454d2014-11-07 16:00:59 -0800894 if (refreshNeeded) {
Dan Stoza58784442014-12-02 16:58:17 -0800895 // Signal a refresh if a transaction modified the window state,
896 // a new buffer was latched, or if HWC has requested a full
897 // repaint
Dan Stoza6b9454d2014-11-07 16:00:59 -0800898 signalRefresh();
899 }
900 break;
901 }
902 case MessageQueue::REFRESH: {
903 handleMessageRefresh();
904 break;
905 }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800906 }
907}
908
Dan Stoza6b9454d2014-11-07 16:00:59 -0800909bool SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700910 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700911 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700912 handleTransaction(transactionFlags);
Dan Stoza6b9454d2014-11-07 16:00:59 -0800913 return true;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700914 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800915 return false;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700916}
917
Dan Stoza6b9454d2014-11-07 16:00:59 -0800918bool SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700919 ATRACE_CALL();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800920 return handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700921}
922
923void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700924 ATRACE_CALL();
925 preComposition();
926 rebuildLayerStacks();
927 setUpHWComposer();
928 doDebugFlashRegions();
929 doComposition();
930 postComposition();
931}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700932
Mathias Agopiancd60f992012-08-16 16:28:27 -0700933void SurfaceFlinger::doDebugFlashRegions()
934{
935 // is debugging enabled
936 if (CC_LIKELY(!mDebugRegion))
937 return;
938
939 const bool repaintEverything = mRepaintEverything;
940 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
941 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700942 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700943 // transform the dirty region into this screen's coordinate space
944 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
945 if (!dirtyRegion.isEmpty()) {
946 // redraw the whole screen
947 doComposeSurfaces(hw, Region(hw->bounds()));
948
949 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700950 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700951 RenderEngine& engine(getRenderEngine());
952 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
953
Mathias Agopiancd60f992012-08-16 16:28:27 -0700954 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700955 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700956 }
957 }
958 }
959
960 postFramebuffer();
961
962 if (mDebugRegion > 1) {
963 usleep(mDebugRegion * 1000);
964 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700965
966 HWComposer& hwc(getHwComposer());
967 if (hwc.initCheck() == NO_ERROR) {
968 status_t err = hwc.prepare();
969 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
970 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700971}
972
973void SurfaceFlinger::preComposition()
974{
975 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700976 const LayerVector& layers(mDrawingState.layersSortedByZ);
977 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700978 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700979 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700980 needExtraInvalidate = true;
981 }
982 }
983 if (needExtraInvalidate) {
984 signalLayerUpdate();
985 }
986}
987
988void SurfaceFlinger::postComposition()
989{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700990 const LayerVector& layers(mDrawingState.layersSortedByZ);
991 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700992 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700993 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700994 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800995
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700996 const HWComposer& hwc = getHwComposer();
997 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
998
999 if (presentFence->isValid()) {
1000 if (mPrimaryDispSync.addPresentFence(presentFence)) {
1001 enableHardwareVsync();
1002 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -07001003 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001004 }
1005 }
1006
Dan Stozab90cf072015-03-05 11:05:59 -08001007 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Andy McFadden5167ec62014-05-22 13:08:43 -07001008 if (kIgnorePresentFences) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001009 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001010 enableHardwareVsync();
1011 }
1012 }
1013
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001014 if (mAnimCompositionPending) {
1015 mAnimCompositionPending = false;
1016
Jesse Halla9a1b002013-02-27 16:39:25 -08001017 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001018 mAnimFrameTracker.setActualPresentFence(presentFence);
1019 } else {
1020 // The HWC doesn't support present fences, so use the refresh
1021 // timestamp instead.
1022 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1023 mAnimFrameTracker.setActualPresentTime(presentTime);
1024 }
1025 mAnimFrameTracker.advanceFrame();
1026 }
Dan Stozab90cf072015-03-05 11:05:59 -08001027
1028 if (hw->getPowerMode() == HWC_POWER_MODE_OFF) {
1029 return;
1030 }
1031
1032 nsecs_t currentTime = systemTime();
1033 if (mHasPoweredOff) {
1034 mHasPoweredOff = false;
1035 } else {
1036 nsecs_t period = mPrimaryDispSync.getPeriod();
1037 nsecs_t elapsedTime = currentTime - mLastSwapTime;
1038 size_t numPeriods = static_cast<size_t>(elapsedTime / period);
1039 if (numPeriods < NUM_BUCKETS - 1) {
1040 mFrameBuckets[numPeriods] += elapsedTime;
1041 } else {
1042 mFrameBuckets[NUM_BUCKETS - 1] += elapsedTime;
1043 }
1044 mTotalTime += elapsedTime;
1045 }
1046 mLastSwapTime = currentTime;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001047}
1048
1049void SurfaceFlinger::rebuildLayerStacks() {
1050 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001051 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001052 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -07001053 mVisibleRegionsDirty = false;
1054 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001055
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001056 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001057 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001058 Region opaqueRegion;
1059 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -08001060 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -07001061 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001062 const Transform& tr(hw->getTransform());
1063 const Rect bounds(hw->getBounds());
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001064 if (hw->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001065 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001066 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001067
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001068 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001069 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001070 const sp<Layer>& layer(layers[i]);
1071 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001072 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -07001073 Region drawRegion(tr.transform(
1074 layer->visibleNonTransparentRegion));
1075 drawRegion.andSelf(bounds);
1076 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001077 layersSortedByZ.add(layer);
1078 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001079 }
1080 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001081 }
Mathias Agopian42977342012-08-05 00:40:46 -07001082 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001083 hw->undefinedRegion.set(bounds);
1084 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
1085 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001086 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001087 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001088}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001089
Mathias Agopiancd60f992012-08-16 16:28:27 -07001090void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001091 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001092 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1093 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1094 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1095
1096 // If nothing has changed (!dirty), don't recompose.
1097 // If something changed, but we don't currently have any visible layers,
1098 // and didn't when we last did a composition, then skip it this time.
1099 // The second rule does two things:
1100 // - When all layers are removed from a display, we'll emit one black
1101 // frame, then nothing more until we get new layers.
1102 // - When a display is created with a private layer stack, we won't
1103 // emit any black frames until a layer is added to the layer stack.
1104 bool mustRecompose = dirty && !(empty && wasEmpty);
1105
1106 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1107 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1108 mustRecompose ? "doing" : "skipping",
1109 dirty ? "+" : "-",
1110 empty ? "+" : "-",
1111 wasEmpty ? "+" : "-");
1112
Dan Stoza71433162014-02-04 16:22:36 -08001113 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001114
1115 if (mustRecompose) {
1116 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1117 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001118 }
1119
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001120 HWComposer& hwc(getHwComposer());
1121 if (hwc.initCheck() == NO_ERROR) {
1122 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001123 if (CC_UNLIKELY(mHwWorkListDirty)) {
1124 mHwWorkListDirty = false;
1125 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1126 sp<const DisplayDevice> hw(mDisplays[dpy]);
1127 const int32_t id = hw->getHwcDisplayId();
1128 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001129 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001130 hw->getVisibleLayersSortedByZ());
1131 const size_t count = currentLayers.size();
1132 if (hwc.createWorkList(id, count) == NO_ERROR) {
1133 HWComposer::LayerListIterator cur = hwc.begin(id);
1134 const HWComposer::LayerListIterator end = hwc.end(id);
1135 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001136 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001137 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001138 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001139 cur->setSkip(true);
1140 }
1141 }
1142 }
1143 }
1144 }
1145 }
1146
1147 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001148 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001149 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001150 const int32_t id = hw->getHwcDisplayId();
1151 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001152 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001153 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001154 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001155 HWComposer::LayerListIterator cur = hwc.begin(id);
1156 const HWComposer::LayerListIterator end = hwc.end(id);
1157 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1158 /*
1159 * update the per-frame h/w composer data for each layer
1160 * and build the transparent region of the FB
1161 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001162 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001163 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -07001164 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001165 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001166 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001167
Riley Andrews03414a12014-07-01 14:22:59 -07001168 // If possible, attempt to use the cursor overlay on each display.
1169 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1170 sp<const DisplayDevice> hw(mDisplays[dpy]);
1171 const int32_t id = hw->getHwcDisplayId();
1172 if (id >= 0) {
1173 const Vector< sp<Layer> >& currentLayers(
1174 hw->getVisibleLayersSortedByZ());
1175 const size_t count = currentLayers.size();
1176 HWComposer::LayerListIterator cur = hwc.begin(id);
1177 const HWComposer::LayerListIterator end = hwc.end(id);
1178 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1179 const sp<Layer>& layer(currentLayers[i]);
1180 if (layer->isPotentialCursor()) {
1181 cur->setIsCursorLayerHint();
1182 break;
1183 }
1184 }
1185 }
1186 }
1187
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001188 status_t err = hwc.prepare();
1189 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001190
1191 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1192 sp<const DisplayDevice> hw(mDisplays[dpy]);
1193 hw->prepareFrame(hwc);
1194 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001195 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001196}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001197
Mathias Agopiancd60f992012-08-16 16:28:27 -07001198void SurfaceFlinger::doComposition() {
1199 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001200 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001201 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001202 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001203 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001204 // transform the dirty region into this screen's coordinate space
1205 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001206
1207 // repaint the framebuffer (if needed)
1208 doDisplayComposition(hw, dirtyRegion);
1209
Mathias Agopiancd60f992012-08-16 16:28:27 -07001210 hw->dirtyRegion.clear();
1211 hw->flip(hw->swapRegion);
1212 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001213 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001214 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001215 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001216 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001217 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001218}
1219
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001220void SurfaceFlinger::postFramebuffer()
1221{
Mathias Agopian841cde52012-03-01 15:44:37 -08001222 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001223
Mathias Agopiana44b0412011-10-16 18:46:35 -07001224 const nsecs_t now = systemTime();
1225 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001226
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001227 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001228 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001229 if (!hwc.supportsFramebufferTarget()) {
1230 // EGL spec says:
1231 // "surface must be bound to the calling thread's current context,
1232 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001233 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001234 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001235 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001236 }
1237
Mathias Agopian6da15f42013-09-25 20:40:07 -07001238 // make the default display current because the VirtualDisplayDevice code cannot
1239 // deal with dequeueBuffer() being called outside of the composition loop; however
1240 // the code below can call glFlush() which is allowed (and does in some case) call
1241 // dequeueBuffer().
1242 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1243
Mathias Agopian92a979a2012-08-02 18:32:23 -07001244 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001245 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001246 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001247 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001248 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001249 int32_t id = hw->getHwcDisplayId();
1250 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001251 HWComposer::LayerListIterator cur = hwc.begin(id);
1252 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001253 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001254 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001255 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001256 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001257 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001258 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001259 }
Jesse Hallef194142012-06-14 14:45:17 -07001260 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001261 }
1262
Mathias Agopiana44b0412011-10-16 18:46:35 -07001263 mLastSwapBufferTime = systemTime() - now;
1264 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001265
1266 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1267 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1268 logFrameStats();
1269 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001270}
1271
Mathias Agopian87baae12012-07-31 12:38:26 -07001272void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001273{
Mathias Agopian841cde52012-03-01 15:44:37 -08001274 ATRACE_CALL();
1275
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001276 // here we keep a copy of the drawing state (that is the state that's
1277 // going to be overwritten by handleTransactionLocked()) outside of
1278 // mStateLock so that the side-effects of the State assignment
1279 // don't happen with mStateLock held (which can cause deadlocks).
1280 State drawingState(mDrawingState);
1281
Mathias Agopianca4d3602011-05-19 15:38:14 -07001282 Mutex::Autolock _l(mStateLock);
1283 const nsecs_t now = systemTime();
1284 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285
Mathias Agopianca4d3602011-05-19 15:38:14 -07001286 // Here we're guaranteed that some transaction flags are set
1287 // so we can call handleTransactionLocked() unconditionally.
1288 // We call getTransactionFlags(), which will also clear the flags,
1289 // with mStateLock held to guarantee that mCurrentState won't change
1290 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001291
Mathias Agopiane57f2922012-08-09 16:29:12 -07001292 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001293 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001294
Mathias Agopianca4d3602011-05-19 15:38:14 -07001295 mLastTransactionTime = systemTime() - now;
1296 mDebugInTransaction = 0;
1297 invalidateHwcGeometry();
1298 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001299}
1300
Mathias Agopian87baae12012-07-31 12:38:26 -07001301void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001302{
1303 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304 const size_t count = currentLayers.size();
1305
1306 /*
1307 * Traversal of the children
1308 * (perform the transaction for each of them if needed)
1309 */
1310
Mathias Agopian3559b072012-08-15 13:46:03 -07001311 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001313 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1315 if (!trFlags) continue;
1316
1317 const uint32_t flags = layer->doTransaction(0);
1318 if (flags & Layer::eVisibleRegion)
1319 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001320 }
1321 }
1322
1323 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001324 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325 */
1326
Mathias Agopiane57f2922012-08-09 16:29:12 -07001327 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001328 // here we take advantage of Vector's copy-on-write semantics to
1329 // improve performance by skipping the transaction entirely when
1330 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001331 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1332 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001333 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001334 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001335 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001336 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001337
1338 // find the displays that were removed
1339 // (ie: in drawing state but not in current state)
1340 // also handle displays that changed
1341 // (ie: displays that are in both lists)
1342 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001343 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1344 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001345 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001346 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001347 // Call makeCurrent() on the primary display so we can
1348 // be sure that nothing associated with this display
1349 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001350 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001351 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001352 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1353 if (hw != NULL)
1354 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001355 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001356 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001357 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001358 } else {
1359 ALOGW("trying to remove the main display");
1360 }
1361 } else {
1362 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001363 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001364 const wp<IBinder>& display(curr.keyAt(j));
Marco Nelissen097ca272014-11-14 08:01:01 -08001365 const sp<IBinder> state_binder = IInterface::asBinder(state.surface);
1366 const sp<IBinder> draw_binder = IInterface::asBinder(draw[i].surface);
Dan Albert1474f882014-09-08 18:59:09 -07001367 if (state_binder != draw_binder) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001368 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001369 // recreating the DisplayDevice, so we just remove it
1370 // from the drawing state, so that it get re-added
1371 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001372 sp<DisplayDevice> hw(getDisplayDevice(display));
1373 if (hw != NULL)
1374 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001375 mDisplays.removeItem(display);
1376 mDrawingState.displays.removeItemsAt(i);
1377 dc--; i--;
1378 // at this point we must loop to the next item
1379 continue;
1380 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001381
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001382 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001383 if (disp != NULL) {
1384 if (state.layerStack != draw[i].layerStack) {
1385 disp->setLayerStack(state.layerStack);
1386 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001387 if ((state.orientation != draw[i].orientation)
1388 || (state.viewport != draw[i].viewport)
1389 || (state.frame != draw[i].frame))
1390 {
1391 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001392 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001393 }
Michael Lentine47e45402014-07-18 15:34:25 -07001394 if (state.width != draw[i].width || state.height != draw[i].height) {
1395 disp->setDisplaySize(state.width, state.height);
1396 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001397 }
1398 }
1399 }
1400
1401 // find displays that were added
1402 // (ie: in current state but not in drawing state)
1403 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001404 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001405 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001406
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001407 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001408 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001409 sp<IGraphicBufferProducer> bqProducer;
1410 sp<IGraphicBufferConsumer> bqConsumer;
1411 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1412 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001413
Jesse Hall02d86562013-03-25 14:43:23 -07001414 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001415 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001416 // Virtual displays without a surface are dormant:
1417 // they have external state (layer stack, projection,
1418 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001419 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001420
Dan Stoza1f3efb12014-10-15 16:34:55 -07001421 int width = 0;
1422 int status = state.surface->query(
1423 NATIVE_WINDOW_WIDTH, &width);
1424 ALOGE_IF(status != NO_ERROR,
1425 "Unable to query width (%d)", status);
1426 int height = 0;
1427 status = state.surface->query(
1428 NATIVE_WINDOW_HEIGHT, &height);
1429 ALOGE_IF(status != NO_ERROR,
1430 "Unable to query height (%d)", status);
1431 if (MAX_VIRTUAL_DISPLAY_DIMENSION == 0 ||
1432 (width <= MAX_VIRTUAL_DISPLAY_DIMENSION &&
1433 height <= MAX_VIRTUAL_DISPLAY_DIMENSION)) {
1434 hwcDisplayId = allocateHwcDisplayId(state.type);
1435 }
1436
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001437 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
Dan Stozab9b08832014-03-13 11:55:57 -07001438 *mHwc, hwcDisplayId, state.surface,
1439 bqProducer, bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001440
1441 dispSurface = vds;
Michael Lentine47e45402014-07-18 15:34:25 -07001442 producer = vds;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001443 }
1444 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001445 ALOGE_IF(state.surface!=NULL,
1446 "adding a supported display, but rendering "
1447 "surface is provided (%p), ignoring it",
1448 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001449 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001450 // for supported (by hwc) displays we provide our
1451 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001452 dispSurface = new FramebufferSurface(*mHwc, state.type,
1453 bqConsumer);
1454 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001455 }
1456
1457 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001458 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001459 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001460 state.type, hwcDisplayId,
1461 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001462 display, dispSurface, producer,
1463 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001464 hw->setLayerStack(state.layerStack);
1465 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001466 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001467 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001468 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001469 if (state.isVirtualDisplay()) {
1470 if (hwcDisplayId >= 0) {
1471 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1472 hw->getWidth(), hw->getHeight(),
1473 hw->getFormat());
1474 }
1475 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001476 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001477 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001478 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001479 }
1480 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001482 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483
Mathias Agopian84300952012-11-21 16:02:13 -08001484 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1485 // The transform hint might have changed for some layers
1486 // (either because a display has changed, or because a layer
1487 // as changed).
1488 //
1489 // Walk through all the layers in currentLayers,
1490 // and update their transform hint.
1491 //
1492 // If a layer is visible only on a single display, then that
1493 // display is used to calculate the hint, otherwise we use the
1494 // default display.
1495 //
1496 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1497 // the hint is set before we acquire a buffer from the surface texture.
1498 //
1499 // NOTE: layer transactions have taken place already, so we use their
1500 // drawing state. However, SurfaceFlinger's own transaction has not
1501 // happened yet, so we must use the current state layer list
1502 // (soon to become the drawing state list).
1503 //
1504 sp<const DisplayDevice> disp;
1505 uint32_t currentlayerStack = 0;
1506 for (size_t i=0; i<count; i++) {
1507 // NOTE: we rely on the fact that layers are sorted by
1508 // layerStack first (so we don't have to traverse the list
1509 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001510 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001511 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001512 if (i==0 || currentlayerStack != layerStack) {
1513 currentlayerStack = layerStack;
1514 // figure out if this layerstack is mirrored
1515 // (more than one display) if so, pick the default display,
1516 // if not, pick the only display it's on.
1517 disp.clear();
1518 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1519 sp<const DisplayDevice> hw(mDisplays[dpy]);
1520 if (hw->getLayerStack() == currentlayerStack) {
1521 if (disp == NULL) {
1522 disp = hw;
1523 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001524 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001525 break;
1526 }
1527 }
1528 }
1529 }
Chet Haase91d25932013-04-11 15:24:55 -07001530 if (disp == NULL) {
1531 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1532 // redraw after transform hint changes. See bug 8508397.
1533
1534 // could be null when this layer is using a layerStack
1535 // that is not visible on any display. Also can occur at
1536 // screen off/on times.
1537 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001538 }
Chet Haase91d25932013-04-11 15:24:55 -07001539 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001540 }
1541 }
1542
1543
Mathias Agopian3559b072012-08-15 13:46:03 -07001544 /*
1545 * Perform our own transaction if needed
1546 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001547
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001548 const LayerVector& layers(mDrawingState.layersSortedByZ);
1549 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001550 // layers have been added
1551 mVisibleRegionsDirty = true;
1552 }
1553
1554 // some layers might have been removed, so
1555 // we need to update the regions they're exposing.
1556 if (mLayersRemoved) {
1557 mLayersRemoved = false;
1558 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001559 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001560 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001561 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001562 if (currentLayers.indexOf(layer) < 0) {
1563 // this layer is not visible anymore
1564 // TODO: we could traverse the tree from front to back and
1565 // compute the actual visible region
1566 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001567 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001568 Region visibleReg = s.transform.transform(
1569 Region(Rect(s.active.w, s.active.h)));
1570 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001571 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001572 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573 }
1574
1575 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001576
1577 updateCursorAsync();
1578}
1579
1580void SurfaceFlinger::updateCursorAsync()
1581{
1582 HWComposer& hwc(getHwComposer());
1583 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1584 sp<const DisplayDevice> hw(mDisplays[dpy]);
1585 const int32_t id = hw->getHwcDisplayId();
1586 if (id < 0) {
1587 continue;
1588 }
1589 const Vector< sp<Layer> >& currentLayers(
1590 hw->getVisibleLayersSortedByZ());
1591 const size_t count = currentLayers.size();
1592 HWComposer::LayerListIterator cur = hwc.begin(id);
1593 const HWComposer::LayerListIterator end = hwc.end(id);
1594 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1595 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1596 continue;
1597 }
1598 const sp<Layer>& layer(currentLayers[i]);
1599 Rect cursorPos = layer->getPosition(hw);
1600 hwc.setCursorPositionAsync(id, cursorPos);
1601 break;
1602 }
1603 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001604}
1605
1606void SurfaceFlinger::commitTransaction()
1607{
1608 if (!mLayersPendingRemoval.isEmpty()) {
1609 // Notify removed layers now that they can't be drawn from
1610 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1611 mLayersPendingRemoval[i]->onRemoved();
1612 }
1613 mLayersPendingRemoval.clear();
1614 }
1615
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001616 // If this transaction is part of a window animation then the next frame
1617 // we composite should be considered an animation as well.
1618 mAnimCompositionPending = mAnimTransactionPending;
1619
Mathias Agopian4fec8732012-06-29 14:12:52 -07001620 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001621 mTransactionPending = false;
1622 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001623 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001624}
1625
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001626void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001627 const LayerVector& currentLayers, uint32_t layerStack,
1628 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001629{
Mathias Agopian841cde52012-03-01 15:44:37 -08001630 ATRACE_CALL();
1631
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001632 Region aboveOpaqueLayers;
1633 Region aboveCoveredLayers;
1634 Region dirty;
1635
Mathias Agopian87baae12012-07-31 12:38:26 -07001636 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001637
1638 size_t i = currentLayers.size();
1639 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001640 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001641
1642 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001643 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001644
Jesse Hall01e29052013-02-19 16:13:35 -08001645 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001646 if (s.layerStack != layerStack)
1647 continue;
1648
Mathias Agopianab028732010-03-16 16:41:46 -07001649 /*
1650 * opaqueRegion: area of a surface that is fully opaque.
1651 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001653
1654 /*
1655 * visibleRegion: area of a surface that is visible on screen
1656 * and not fully transparent. This is essentially the layer's
1657 * footprint minus the opaque regions above it.
1658 * Areas covered by a translucent surface are considered visible.
1659 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001660 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001661
1662 /*
1663 * coveredRegion: area of a surface that is covered by all
1664 * visible regions above it (which includes the translucent areas).
1665 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001666 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001667
Jesse Halla8026d22012-09-25 13:25:04 -07001668 /*
1669 * transparentRegion: area of a surface that is hinted to be completely
1670 * transparent. This is only used to tell when the layer has no visible
1671 * non-transparent regions and can be removed from the layer list. It
1672 * does not affect the visibleRegion of this layer or any layers
1673 * beneath it. The hint may not be correct if apps don't respect the
1674 * SurfaceView restrictions (which, sadly, some don't).
1675 */
1676 Region transparentRegion;
1677
Mathias Agopianab028732010-03-16 16:41:46 -07001678
1679 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001680 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001681 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001682 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001684 if (!visibleRegion.isEmpty()) {
1685 // Remove the transparent area from the visible region
1686 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001687 const Transform tr(s.transform);
1688 if (tr.transformed()) {
1689 if (tr.preserveRects()) {
1690 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001691 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001692 } else {
1693 // transformation too complex, can't do the
1694 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001695 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001696 }
1697 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001698 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001699 }
Mathias Agopianab028732010-03-16 16:41:46 -07001700 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001701
Mathias Agopianab028732010-03-16 16:41:46 -07001702 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001703 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001704 if (s.alpha==255 && !translucent &&
1705 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1706 // the opaque region is the layer's footprint
1707 opaqueRegion = visibleRegion;
1708 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001709 }
1710 }
1711
Mathias Agopianab028732010-03-16 16:41:46 -07001712 // Clip the covered region to the visible region
1713 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1714
1715 // Update aboveCoveredLayers for next (lower) layer
1716 aboveCoveredLayers.orSelf(visibleRegion);
1717
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001718 // subtract the opaque region covered by the layers above us
1719 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001720
1721 // compute this layer's dirty region
1722 if (layer->contentDirty) {
1723 // we need to invalidate the whole region
1724 dirty = visibleRegion;
1725 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001726 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001727 layer->contentDirty = false;
1728 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001729 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001730 * the exposed region consists of two components:
1731 * 1) what's VISIBLE now and was COVERED before
1732 * 2) what's EXPOSED now less what was EXPOSED before
1733 *
1734 * note that (1) is conservative, we start with the whole
1735 * visible region but only keep what used to be covered by
1736 * something -- which mean it may have been exposed.
1737 *
1738 * (2) handles areas that were not covered by anything but got
1739 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001740 */
Mathias Agopianab028732010-03-16 16:41:46 -07001741 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001742 const Region oldVisibleRegion = layer->visibleRegion;
1743 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001744 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1745 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001746 }
1747 dirty.subtractSelf(aboveOpaqueLayers);
1748
1749 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001750 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001751
Mathias Agopianab028732010-03-16 16:41:46 -07001752 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001753 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001754
Jesse Halla8026d22012-09-25 13:25:04 -07001755 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001756 layer->setVisibleRegion(visibleRegion);
1757 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001758 layer->setVisibleNonTransparentRegion(
1759 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001760 }
1761
Mathias Agopian87baae12012-07-31 12:38:26 -07001762 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001763}
1764
Mathias Agopian87baae12012-07-31 12:38:26 -07001765void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1766 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001767 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001768 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1769 if (hw->getLayerStack() == layerStack) {
1770 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001771 }
1772 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001773}
1774
Dan Stoza6b9454d2014-11-07 16:00:59 -08001775bool SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001776{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001777 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001778
Mathias Agopian4fec8732012-06-29 14:12:52 -07001779 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001780 const LayerVector& layers(mDrawingState.layersSortedByZ);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001781 bool frameQueued = false;
Eric Penner51c59cd2014-07-28 19:51:58 -07001782
1783 // Store the set of layers that need updates. This set must not change as
1784 // buffers are being latched, as this could result in a deadlock.
1785 // Example: Two producers share the same command stream and:
1786 // 1.) Layer 0 is latched
1787 // 2.) Layer 0 gets a new frame
1788 // 2.) Layer 1 gets a new frame
1789 // 3.) Layer 1 is latched.
1790 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1791 // second frame. But layer 0's second frame could be waiting on display.
1792 Vector<Layer*> layersWithQueuedFrames;
1793 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001794 const sp<Layer>& layer(layers[i]);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001795 if (layer->hasQueuedFrame()) {
1796 frameQueued = true;
1797 if (layer->shouldPresentNow(mPrimaryDispSync)) {
1798 layersWithQueuedFrames.push_back(layer.get());
Dan Stozaee44edd2015-03-23 15:50:23 -07001799 } else {
1800 layer->useEmptyDamage();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001801 }
Dan Stozaee44edd2015-03-23 15:50:23 -07001802 } else {
1803 layer->useEmptyDamage();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001804 }
Eric Penner51c59cd2014-07-28 19:51:58 -07001805 }
1806 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
1807 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07001808 const Region dirty(layer->latchBuffer(visibleRegions));
Dan Stozaee44edd2015-03-23 15:50:23 -07001809 layer->useSurfaceDamage();
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001810 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001811 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001812 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001813
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001814 mVisibleRegionsDirty |= visibleRegions;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001815
1816 // If we will need to wake up at some time in the future to deal with a
1817 // queued frame that shouldn't be displayed during this vsync period, wake
1818 // up during the next vsync period to check again.
1819 if (frameQueued && layersWithQueuedFrames.empty()) {
1820 signalLayerUpdate();
1821 }
1822
1823 // Only continue with the refresh if there is actually new work to do
1824 return !layersWithQueuedFrames.empty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001825}
1826
Mathias Agopianad456f92011-01-13 17:53:01 -08001827void SurfaceFlinger::invalidateHwcGeometry()
1828{
1829 mHwWorkListDirty = true;
1830}
1831
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001832
Mathias Agopiancd60f992012-08-16 16:28:27 -07001833void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001834 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001835{
Dan Stoza71433162014-02-04 16:22:36 -08001836 // We only need to actually compose the display if:
1837 // 1) It is being handled by hardware composer, which may need this to
1838 // keep its virtual display state machine in sync, or
1839 // 2) There is work to be done (the dirty region isn't empty)
1840 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1841 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1842 return;
1843 }
1844
Mathias Agopian87baae12012-07-31 12:38:26 -07001845 Region dirtyRegion(inDirtyRegion);
1846
Mathias Agopianb8a55602009-06-26 19:06:36 -07001847 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001848 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001849
Mathias Agopian42977342012-08-05 00:40:46 -07001850 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001851 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001852 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1853 // takes a rectangle, we must make sure to update that whole
1854 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001855 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001856 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001857 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001858 // We need to redraw the rectangle that will be updated
1859 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001860 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001861 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001862 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001863 } else {
1864 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001865 dirtyRegion.set(hw->bounds());
1866 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001867 }
1868 }
1869
Alan Viverette9c5a3332013-09-12 20:04:35 -07001870 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Michael Lentine3f121fc2014-10-01 11:17:28 -07001871 if (!doComposeSurfaces(hw, dirtyRegion)) return;
Mathias Agopianff2ed702013-09-01 21:36:12 -07001872 } else {
1873 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001874 mat4 colorMatrix = mColorMatrix;
1875 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001876 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001877 }
Dan Stozaf0087992014-10-20 15:46:09 -07001878 mat4 oldMatrix = engine.setupColorTransform(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001879 doComposeSurfaces(hw, dirtyRegion);
Dan Stozaf0087992014-10-20 15:46:09 -07001880 engine.setupColorTransform(oldMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001881 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001882
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001883 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001884 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001885
1886 // swap buffers (presentation)
1887 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001888}
1889
Michael Lentine3f121fc2014-10-01 11:17:28 -07001890bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001891{
Mathias Agopian3f844832013-08-07 21:24:32 -07001892 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001893 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001894 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001895 HWComposer::LayerListIterator cur = hwc.begin(id);
1896 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001897
Jesse Halld05a17f2013-09-30 16:49:30 -07001898 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001899 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001900 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001901 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1902 hw->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07001903 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1904 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
1905 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
1906 }
1907 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08001908 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001909
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001910 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001911 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001912 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001913 // when using overlays, we assume a fully transparent framebuffer
1914 // NOTE: we could reduce how much we need to clear, for instance
1915 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001916 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001917 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001918 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001919 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001920 // we start with the whole screen area
1921 const Region bounds(hw->getBounds());
1922
1923 // we remove the scissor part
1924 // we're left with the letterbox region
1925 // (common case is that letterbox ends-up being empty)
1926 const Region letterbox(bounds.subtract(hw->getScissor()));
1927
1928 // compute the area to clear
1929 Region region(hw->undefinedRegion.merge(letterbox));
1930
1931 // but limit it to the dirty region
1932 region.andSelf(dirty);
1933
Mathias Agopianb9494d52012-04-18 02:28:45 -07001934 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001935 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001936 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001937 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001938 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001939 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001940
Mathias Agopian766dc492012-10-30 18:08:06 -07001941 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1942 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001943 // scissor on the main display. It should never be needed
1944 // anyways (though in theory it could since the API allows it).
1945 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001946 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001947 if (scissor != bounds) {
1948 // scissor doesn't match the screen's dimensions, so we
1949 // need to clear everything outside of it and enable
1950 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001951
Mathias Agopianf45c5102012-10-24 16:29:17 -07001952 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001953 const uint32_t height = hw->getHeight();
1954 engine.setScissor(scissor.left, height - scissor.bottom,
1955 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001956 }
1957 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001958 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001959
Mathias Agopian85d751c2012-08-29 16:59:24 -07001960 /*
1961 * and then, render the layers targeted at the framebuffer
1962 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001963
Mathias Agopian13127d82013-03-05 17:47:11 -08001964 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001965 const size_t count = layers.size();
1966 const Transform& tr = hw->getTransform();
1967 if (cur != end) {
1968 // we're using h/w composer
1969 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001970 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001971 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001972 if (!clip.isEmpty()) {
1973 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07001974 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07001975 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001976 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001977 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1978 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001979 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001980 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001981 // never clear the very first layer since we're
1982 // guaranteed the FB is already cleared
1983 layer->clearWithOpenGL(hw, clip);
1984 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001985 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001986 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001987 case HWC_FRAMEBUFFER: {
1988 layer->draw(hw, clip);
1989 break;
1990 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001991 case HWC_FRAMEBUFFER_TARGET: {
1992 // this should not happen as the iterator shouldn't
1993 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001994 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001995 break;
1996 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001997 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001998 }
1999 layer->setAcquireFence(hw, *cur);
2000 }
2001 } else {
2002 // we're not using h/w composer
2003 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002004 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002005 const Region clip(dirty.intersect(
2006 tr.transform(layer->visibleRegion)));
2007 if (!clip.isEmpty()) {
2008 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07002009 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002010 }
2011 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002012
2013 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002014 engine.disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07002015 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002016}
2017
Mathias Agopian3f844832013-08-07 21:24:32 -07002018void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07002019 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07002020 RenderEngine& engine(getRenderEngine());
2021 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002022}
2023
Dan Stoza7d89d062015-04-30 13:29:25 -07002024status_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -08002025 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07002026 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08002027 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07002028{
Dan Stoza7d89d062015-04-30 13:29:25 -07002029 // add this layer to the current state list
2030 {
2031 Mutex::Autolock _l(mStateLock);
2032 if (mCurrentState.layersSortedByZ.size() >= MAX_LAYERS) {
2033 return NO_MEMORY;
2034 }
2035 mCurrentState.layersSortedByZ.add(lbc);
2036 mGraphicBufferProducerList.add(IInterface::asBinder(gbc));
2037 }
2038
Mathias Agopian96f08192010-06-02 23:28:45 -07002039 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08002040 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07002041
Dan Stoza7d89d062015-04-30 13:29:25 -07002042 return NO_ERROR;
Mathias Agopian96f08192010-06-02 23:28:45 -07002043}
2044
Mathias Agopian3f844832013-08-07 21:24:32 -07002045status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002046 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08002047 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002048 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07002049 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002050 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07002051 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002052 return NO_ERROR;
2053 }
Mathias Agopian3d579642009-06-04 18:46:21 -07002054 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002055}
2056
Dan Stozac7014012014-02-14 15:03:43 -08002057uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07002058 return android_atomic_release_load(&mTransactionFlags);
2059}
2060
Mathias Agopian3f844832013-08-07 21:24:32 -07002061uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002062 return android_atomic_and(~flags, &mTransactionFlags) & flags;
2063}
2064
Mathias Agopian3f844832013-08-07 21:24:32 -07002065uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002066 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
2067 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002068 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002069 }
2070 return old;
2071}
2072
Mathias Agopian8b33f032012-07-24 20:43:54 -07002073void SurfaceFlinger::setTransactionState(
2074 const Vector<ComposerState>& state,
2075 const Vector<DisplayState>& displays,
2076 uint32_t flags)
2077{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002078 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07002079 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07002080 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002081
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002082 if (flags & eAnimation) {
2083 // For window updates that are part of an animation we must wait for
2084 // previous animation "frames" to be handled.
2085 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002086 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002087 if (CC_UNLIKELY(err != NO_ERROR)) {
2088 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002089 // caller after a few seconds.
2090 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
2091 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002092 mAnimTransactionPending = false;
2093 break;
2094 }
2095 }
2096 }
2097
Mathias Agopiane57f2922012-08-09 16:29:12 -07002098 size_t count = displays.size();
2099 for (size_t i=0 ; i<count ; i++) {
2100 const DisplayState& s(displays[i]);
2101 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002102 }
2103
Mathias Agopiane57f2922012-08-09 16:29:12 -07002104 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002105 for (size_t i=0 ; i<count ; i++) {
2106 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002107 // Here we need to check that the interface we're given is indeed
2108 // one of our own. A malicious client could give us a NULL
2109 // IInterface, or one of its own or even one of our own but a
2110 // different type. All these situations would cause us to crash.
2111 //
2112 // NOTE: it would be better to use RTTI as we could directly check
2113 // that we have a Client*. however, RTTI is disabled in Android.
2114 if (s.client != NULL) {
Marco Nelissen097ca272014-11-14 08:01:01 -08002115 sp<IBinder> binder = IInterface::asBinder(s.client);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002116 if (binder != NULL) {
2117 String16 desc(binder->getInterfaceDescriptor());
2118 if (desc == ISurfaceComposerClient::descriptor) {
2119 sp<Client> client( static_cast<Client *>(s.client.get()) );
2120 transactionFlags |= setClientStateLocked(client, s.state);
2121 }
2122 }
2123 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002124 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002125
Mathias Agopian386aa982011-11-07 21:58:03 -08002126 if (transactionFlags) {
2127 // this triggers the transaction
2128 setTransactionFlags(transactionFlags);
2129
2130 // if this is a synchronous transaction, wait for it to take effect
2131 // before returning.
2132 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002133 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002134 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002135 if (flags & eAnimation) {
2136 mAnimTransactionPending = true;
2137 }
2138 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002139 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2140 if (CC_UNLIKELY(err != NO_ERROR)) {
2141 // just in case something goes wrong in SF, return to the
2142 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002143 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2144 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002145 break;
2146 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002147 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002148 }
2149}
2150
Mathias Agopiane57f2922012-08-09 16:29:12 -07002151uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2152{
Jesse Hall9a143922012-10-04 16:29:19 -07002153 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2154 if (dpyIdx < 0)
2155 return 0;
2156
Mathias Agopiane57f2922012-08-09 16:29:12 -07002157 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002158 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002159 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002160 const uint32_t what = s.what;
2161 if (what & DisplayState::eSurfaceChanged) {
Marco Nelissen097ca272014-11-14 08:01:01 -08002162 if (IInterface::asBinder(disp.surface) != IInterface::asBinder(s.surface)) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002163 disp.surface = s.surface;
2164 flags |= eDisplayTransactionNeeded;
2165 }
2166 }
2167 if (what & DisplayState::eLayerStackChanged) {
2168 if (disp.layerStack != s.layerStack) {
2169 disp.layerStack = s.layerStack;
2170 flags |= eDisplayTransactionNeeded;
2171 }
2172 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002173 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002174 if (disp.orientation != s.orientation) {
2175 disp.orientation = s.orientation;
2176 flags |= eDisplayTransactionNeeded;
2177 }
2178 if (disp.frame != s.frame) {
2179 disp.frame = s.frame;
2180 flags |= eDisplayTransactionNeeded;
2181 }
2182 if (disp.viewport != s.viewport) {
2183 disp.viewport = s.viewport;
2184 flags |= eDisplayTransactionNeeded;
2185 }
2186 }
Michael Lentine47e45402014-07-18 15:34:25 -07002187 if (what & DisplayState::eDisplaySizeChanged) {
2188 if (disp.width != s.width) {
2189 disp.width = s.width;
2190 flags |= eDisplayTransactionNeeded;
2191 }
2192 if (disp.height != s.height) {
2193 disp.height = s.height;
2194 flags |= eDisplayTransactionNeeded;
2195 }
2196 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002197 }
2198 return flags;
2199}
2200
2201uint32_t SurfaceFlinger::setClientStateLocked(
2202 const sp<Client>& client,
2203 const layer_state_t& s)
2204{
2205 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002206 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002207 if (layer != 0) {
2208 const uint32_t what = s.what;
2209 if (what & layer_state_t::ePositionChanged) {
2210 if (layer->setPosition(s.x, s.y))
2211 flags |= eTraversalNeeded;
2212 }
2213 if (what & layer_state_t::eLayerChanged) {
2214 // NOTE: index needs to be calculated before we update the state
2215 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2216 if (layer->setLayer(s.z)) {
2217 mCurrentState.layersSortedByZ.removeAt(idx);
2218 mCurrentState.layersSortedByZ.add(layer);
2219 // we need traversal (state changed)
2220 // AND transaction (list changed)
2221 flags |= eTransactionNeeded|eTraversalNeeded;
2222 }
2223 }
2224 if (what & layer_state_t::eSizeChanged) {
2225 if (layer->setSize(s.w, s.h)) {
2226 flags |= eTraversalNeeded;
2227 }
2228 }
2229 if (what & layer_state_t::eAlphaChanged) {
2230 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2231 flags |= eTraversalNeeded;
2232 }
2233 if (what & layer_state_t::eMatrixChanged) {
2234 if (layer->setMatrix(s.matrix))
2235 flags |= eTraversalNeeded;
2236 }
2237 if (what & layer_state_t::eTransparentRegionChanged) {
2238 if (layer->setTransparentRegionHint(s.transparentRegion))
2239 flags |= eTraversalNeeded;
2240 }
Dan Stoza23116082015-06-18 14:58:39 -07002241 if (what & layer_state_t::eFlagsChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002242 if (layer->setFlags(s.flags, s.mask))
2243 flags |= eTraversalNeeded;
2244 }
2245 if (what & layer_state_t::eCropChanged) {
2246 if (layer->setCrop(s.crop))
2247 flags |= eTraversalNeeded;
2248 }
2249 if (what & layer_state_t::eLayerStackChanged) {
2250 // NOTE: index needs to be calculated before we update the state
2251 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2252 if (layer->setLayerStack(s.layerStack)) {
2253 mCurrentState.layersSortedByZ.removeAt(idx);
2254 mCurrentState.layersSortedByZ.add(layer);
2255 // we need traversal (state changed)
2256 // AND transaction (list changed)
2257 flags |= eTransactionNeeded|eTraversalNeeded;
2258 }
2259 }
2260 }
2261 return flags;
2262}
2263
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002264status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002265 const String8& name,
2266 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002267 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2268 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002269{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002270 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002271 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002272 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002273 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002274 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002275 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002276
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002277 status_t result = NO_ERROR;
2278
2279 sp<Layer> layer;
2280
Mathias Agopian3165cc22012-08-08 19:42:09 -07002281 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2282 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002283 result = createNormalLayer(client,
2284 name, w, h, flags, format,
2285 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002286 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002287 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002288 result = createDimLayer(client,
2289 name, w, h, flags,
2290 handle, gbp, &layer);
2291 break;
2292 default:
2293 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002294 break;
2295 }
2296
Dan Stoza7d89d062015-04-30 13:29:25 -07002297 if (result != NO_ERROR) {
2298 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002299 }
Dan Stoza7d89d062015-04-30 13:29:25 -07002300
2301 result = addClientLayer(client, *handle, *gbp, layer);
2302 if (result != NO_ERROR) {
2303 return result;
2304 }
2305
2306 setTransactionFlags(eTransactionNeeded);
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002307 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002308}
2309
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002310status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2311 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2312 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002313{
2314 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002315 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002316 case PIXEL_FORMAT_TRANSPARENT:
2317 case PIXEL_FORMAT_TRANSLUCENT:
2318 format = PIXEL_FORMAT_RGBA_8888;
2319 break;
2320 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002321 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002322 break;
2323 }
2324
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002325 *outLayer = new Layer(this, client, name, w, h, flags);
2326 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2327 if (err == NO_ERROR) {
2328 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002329 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002330 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002331
2332 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2333 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002334}
2335
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002336status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2337 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2338 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002339{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002340 *outLayer = new LayerDim(this, client, name, w, h, flags);
2341 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002342 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002343 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002344}
2345
Mathias Agopianac9fa422013-02-11 16:40:36 -08002346status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002347{
Mathias Agopian67106042013-03-14 19:18:13 -07002348 // called by the window manager when it wants to remove a Layer
2349 status_t err = NO_ERROR;
2350 sp<Layer> l(client->getLayerUser(handle));
2351 if (l != NULL) {
2352 err = removeLayer(l);
2353 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2354 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002355 }
2356 return err;
2357}
2358
Mathias Agopian13127d82013-03-05 17:47:11 -08002359status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002360{
Mathias Agopian67106042013-03-14 19:18:13 -07002361 // called by ~LayerCleaner() when all references to the IBinder (handle)
2362 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002363 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002364 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002365 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002366 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002367 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002368 "error removing layer=%p (%s)", l.get(), strerror(-err));
2369 }
2370 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002371}
2372
Mathias Agopianb60314a2012-04-10 22:09:54 -07002373// ---------------------------------------------------------------------------
2374
Andy McFadden13a082e2012-08-24 10:16:42 -07002375void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002376 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002377 Vector<ComposerState> state;
2378 Vector<DisplayState> displays;
2379 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002380 d.what = DisplayState::eDisplayProjectionChanged |
2381 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002382 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002383 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002384 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002385 d.frame.makeInvalid();
2386 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002387 d.width = 0;
2388 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002389 displays.add(d);
2390 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002391 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002392
2393 const nsecs_t period =
2394 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2395 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002396}
2397
2398void SurfaceFlinger::initializeDisplays() {
2399 class MessageScreenInitialized : public MessageBase {
2400 SurfaceFlinger* flinger;
2401 public:
2402 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2403 virtual bool handler() {
2404 flinger->onInitializeDisplays();
2405 return true;
2406 }
2407 };
2408 sp<MessageBase> msg = new MessageScreenInitialized(this);
2409 postMessageAsync(msg); // we may be called from main thread, use async message
2410}
2411
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002412void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2413 int mode) {
2414 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2415 this);
2416 int32_t type = hw->getDisplayType();
2417 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002418
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002419 if (mode == currentMode) {
2420 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002421 return;
2422 }
2423
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002424 hw->setPowerMode(mode);
2425 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2426 ALOGW("Trying to set power mode for virtual display");
2427 return;
2428 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002429
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002430 if (currentMode == HWC_POWER_MODE_OFF) {
2431 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002432 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2433 // FIXME: eventthread only knows about the main display right now
2434 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002435 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002436 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002437
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002438 mVisibleRegionsDirty = true;
Dan Stozab90cf072015-03-05 11:05:59 -08002439 mHasPoweredOff = true;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002440 repaintEverything();
2441 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002442 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002443 disableHardwareVsync(true); // also cancels any in-progress resync
2444
Mathias Agopiancde87a32012-09-13 14:09:01 -07002445 // FIXME: eventthread only knows about the main display right now
2446 mEventThread->onScreenReleased();
2447 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002448
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002449 getHwComposer().setPowerMode(type, mode);
2450 mVisibleRegionsDirty = true;
2451 // from this point on, SF will stop drawing on this display
2452 } else {
2453 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002454 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002455}
2456
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002457void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2458 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002459 SurfaceFlinger& mFlinger;
2460 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002461 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002462 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002463 MessageSetPowerMode(SurfaceFlinger& flinger,
2464 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2465 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002466 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002467 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002468 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002469 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002470 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002471 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002472 ALOGW("Attempt to set power mode = %d for virtual display",
2473 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002474 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002475 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002476 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002477 return true;
2478 }
2479 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002480 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002481 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002482}
2483
2484// ---------------------------------------------------------------------------
2485
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002486status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2487{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002488 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002489
Mathias Agopianbd115332013-04-18 16:41:04 -07002490 IPCThreadState* ipc = IPCThreadState::self();
2491 const int pid = ipc->getCallingPid();
2492 const int uid = ipc->getCallingUid();
2493 if ((uid != AID_SHELL) &&
2494 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002495 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002496 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002497 } else {
Jesse Hallfcd15b42014-12-23 13:57:23 -08002498 // Try to get the main lock, but give up after one second
Mathias Agopian9795c422009-08-26 16:36:26 -07002499 // (this would indicate SF is stuck, but we want to be able to
2500 // print something in dumpsys).
Jesse Hallfcd15b42014-12-23 13:57:23 -08002501 status_t err = mStateLock.timedLock(s2ns(1));
2502 bool locked = (err == NO_ERROR);
Mathias Agopian9795c422009-08-26 16:36:26 -07002503 if (!locked) {
Jesse Hallfcd15b42014-12-23 13:57:23 -08002504 result.appendFormat(
2505 "SurfaceFlinger appears to be unresponsive (%s [%d]), "
2506 "dumping anyways (no locks held)\n", strerror(-err), err);
Mathias Agopian9795c422009-08-26 16:36:26 -07002507 }
2508
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002509 bool dumpAll = true;
2510 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002511 size_t numArgs = args.size();
2512 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002513 if ((index < numArgs) &&
2514 (args[index] == String16("--list"))) {
2515 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002516 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002517 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002518 }
2519
2520 if ((index < numArgs) &&
2521 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002522 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002523 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002524 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002525 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002526
2527 if ((index < numArgs) &&
2528 (args[index] == String16("--latency-clear"))) {
2529 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002530 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002531 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002532 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002533
2534 if ((index < numArgs) &&
2535 (args[index] == String16("--dispsync"))) {
2536 index++;
2537 mPrimaryDispSync.dump(result);
2538 dumpAll = false;
2539 }
Dan Stozab90cf072015-03-05 11:05:59 -08002540
2541 if ((index < numArgs) &&
2542 (args[index] == String16("--static-screen"))) {
2543 index++;
2544 dumpStaticScreenStats(result);
2545 dumpAll = false;
2546 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002547 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002548
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002549 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002550 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002551 }
2552
Mathias Agopian9795c422009-08-26 16:36:26 -07002553 if (locked) {
2554 mStateLock.unlock();
2555 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002556 }
2557 write(fd, result.string(), result.size());
2558 return NO_ERROR;
2559}
2560
Dan Stozac7014012014-02-14 15:03:43 -08002561void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2562 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002563{
2564 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2565 const size_t count = currentLayers.size();
2566 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002567 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002568 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002569 }
2570}
2571
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002572void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002573 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002574{
2575 String8 name;
2576 if (index < args.size()) {
2577 name = String8(args[index]);
2578 index++;
2579 }
2580
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002581 const nsecs_t period =
2582 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002583 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002584
2585 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002586 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002587 } else {
2588 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2589 const size_t count = currentLayers.size();
2590 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002591 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002592 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002593 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002594 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002595 }
2596 }
2597}
2598
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002599void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002600 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002601{
2602 String8 name;
2603 if (index < args.size()) {
2604 name = String8(args[index]);
2605 index++;
2606 }
2607
2608 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2609 const size_t count = currentLayers.size();
2610 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002611 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002612 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002613 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002614 }
2615 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002616
Svetoslavd85084b2014-03-20 10:28:31 -07002617 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002618}
2619
Jamie Gennis6547ff42013-07-16 20:12:42 -07002620// This should only be called from the main thread. Otherwise it would need
2621// the lock and should use mCurrentState rather than mDrawingState.
2622void SurfaceFlinger::logFrameStats() {
2623 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2624 const size_t count = drawingLayers.size();
2625 for (size_t i=0 ; i<count ; i++) {
2626 const sp<Layer>& layer(drawingLayers[i]);
2627 layer->logFrameStats();
2628 }
2629
2630 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2631}
2632
Andy McFadden4803b742012-09-24 19:07:20 -07002633/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2634{
2635 static const char* config =
2636 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002637#ifdef HAS_CONTEXT_PRIORITY
2638 " HAS_CONTEXT_PRIORITY"
2639#endif
2640#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2641 " NEVER_DEFAULT_TO_ASYNC_MODE"
2642#endif
2643#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2644 " TARGET_DISABLE_TRIPLE_BUFFERING"
2645#endif
2646 "]";
2647 result.append(config);
2648}
2649
Dan Stozab90cf072015-03-05 11:05:59 -08002650void SurfaceFlinger::dumpStaticScreenStats(String8& result) const
2651{
2652 result.appendFormat("Static screen stats:\n");
2653 for (size_t b = 0; b < NUM_BUCKETS - 1; ++b) {
2654 float bucketTimeSec = mFrameBuckets[b] / 1e9;
2655 float percent = 100.0f *
2656 static_cast<float>(mFrameBuckets[b]) / mTotalTime;
2657 result.appendFormat(" < %zd frames: %.3f s (%.1f%%)\n",
2658 b + 1, bucketTimeSec, percent);
2659 }
2660 float bucketTimeSec = mFrameBuckets[NUM_BUCKETS - 1] / 1e9;
2661 float percent = 100.0f *
2662 static_cast<float>(mFrameBuckets[NUM_BUCKETS - 1]) / mTotalTime;
2663 result.appendFormat(" %zd+ frames: %.3f s (%.1f%%)\n",
2664 NUM_BUCKETS - 1, bucketTimeSec, percent);
2665}
2666
Mathias Agopian74d211a2013-04-22 16:55:35 +02002667void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2668 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002669{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002670 bool colorize = false;
2671 if (index < args.size()
2672 && (args[index] == String16("--color"))) {
2673 colorize = true;
2674 index++;
2675 }
2676
2677 Colorizer colorizer(colorize);
2678
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002679 // figure out if we're stuck somewhere
2680 const nsecs_t now = systemTime();
2681 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2682 const nsecs_t inTransaction(mDebugInTransaction);
2683 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2684 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2685
2686 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002687 * Dump library configuration.
2688 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002689
2690 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002691 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002692 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002693 appendSfConfigString(result);
2694 appendUiConfigString(result);
2695 appendGuiConfigString(result);
2696 result.append("\n");
2697
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002698 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002699 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002700 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002701 result.append(SyncFeatures::getInstance().toString());
2702 result.append("\n");
2703
Andy McFadden41d67d72014-04-25 16:58:34 -07002704 colorizer.bold(result);
2705 result.append("DispSync configuration: ");
2706 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002707 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2708 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002709 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2710 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2711 result.append("\n");
2712
Dan Stozab90cf072015-03-05 11:05:59 -08002713 // Dump static screen stats
2714 result.append("\n");
2715 dumpStaticScreenStats(result);
2716 result.append("\n");
2717
Andy McFadden4803b742012-09-24 19:07:20 -07002718 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002719 * Dump the visible layer list
2720 */
2721 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2722 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002723 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002724 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002725 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002726 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002727 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002728 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002729 }
2730
2731 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002732 * Dump Display state
2733 */
2734
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002735 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002736 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002737 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002738 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2739 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002740 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002741 }
2742
2743 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002744 * Dump SurfaceFlinger global state
2745 */
2746
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002747 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002748 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002749 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002750
Mathias Agopian888c8222012-08-04 21:10:38 -07002751 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002752 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002753
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002754 colorizer.bold(result);
2755 result.appendFormat("EGL implementation : %s\n",
2756 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2757 colorizer.reset(result);
2758 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002759 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002760
Mathias Agopian875d8e12013-06-07 15:35:48 -07002761 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002762
Mathias Agopian42977342012-08-05 00:40:46 -07002763 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002764 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2765 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002766 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002767 " last eglSwapBuffers() time: %f us\n"
2768 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002769 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002770 " refresh-rate : %f fps\n"
2771 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002772 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002773 " gpu_to_cpu_unsupported : %d\n"
2774 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002775 mLastSwapBufferTime/1000.0,
2776 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002777 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002778 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2779 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002780 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002781 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002782
Mathias Agopian74d211a2013-04-22 16:55:35 +02002783 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002784 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002785
Mathias Agopian74d211a2013-04-22 16:55:35 +02002786 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002787 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002788
2789 /*
2790 * VSYNC state
2791 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002792 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002793
2794 /*
2795 * Dump HWComposer state
2796 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002797 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002798 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002799 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002800 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002801 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002802 (mDebugDisableHWC || mDebugRegion || mDaltonize
2803 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002804 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002805
2806 /*
2807 * Dump gralloc state
2808 */
2809 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2810 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002811}
2812
Mathias Agopian13127d82013-03-05 17:47:11 -08002813const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002814SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002815 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002816 wp<IBinder> dpy;
2817 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2818 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2819 dpy = mDisplays.keyAt(i);
2820 break;
2821 }
2822 }
2823 if (dpy == NULL) {
2824 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2825 // Just use the primary display so we have something to return
2826 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2827 }
2828 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002829}
2830
Keun young Park63f165f2012-08-31 10:53:36 -07002831bool SurfaceFlinger::startDdmConnection()
2832{
2833 void* libddmconnection_dso =
2834 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2835 if (!libddmconnection_dso) {
2836 return false;
2837 }
2838 void (*DdmConnection_start)(const char* name);
2839 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07002840 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07002841 if (!DdmConnection_start) {
2842 dlclose(libddmconnection_dso);
2843 return false;
2844 }
2845 (*DdmConnection_start)(getServiceName());
2846 return true;
2847}
2848
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002849status_t SurfaceFlinger::onTransact(
2850 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2851{
2852 switch (code) {
2853 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002854 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002855 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002856 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002857 case CLEAR_ANIMATION_FRAME_STATS:
2858 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002859 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002860 {
2861 // codes that require permission check
2862 IPCThreadState* ipc = IPCThreadState::self();
2863 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002864 const int uid = ipc->getCallingUid();
Jeff Brown3bfe51d2015-04-10 20:20:13 -07002865 if ((uid != AID_GRAPHICS && uid != AID_SYSTEM) &&
Mathias Agopian99b49842011-06-27 16:05:52 -07002866 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002867 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002868 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2869 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002870 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002871 break;
2872 }
2873 case CAPTURE_SCREEN:
2874 {
2875 // codes that require permission check
2876 IPCThreadState* ipc = IPCThreadState::self();
2877 const int pid = ipc->getCallingPid();
2878 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002879 if ((uid != AID_GRAPHICS) &&
2880 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002881 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002882 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2883 return PERMISSION_DENIED;
2884 }
2885 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002886 }
2887 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002888
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002889 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2890 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002891 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002892 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002893 IPCThreadState* ipc = IPCThreadState::self();
2894 const int pid = ipc->getCallingPid();
2895 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002896 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002897 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002898 return PERMISSION_DENIED;
2899 }
2900 int n;
2901 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002902 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002903 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002904 return NO_ERROR;
2905 case 1002: // SHOW_UPDATES
2906 n = data.readInt32();
2907 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002908 invalidateHwcGeometry();
2909 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002910 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002911 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002912 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002913 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002914 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002915 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002916 setTransactionFlags(
2917 eTransactionNeeded|
2918 eDisplayTransactionNeeded|
2919 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002920 return NO_ERROR;
2921 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002922 case 1006:{ // send empty update
2923 signalRefresh();
2924 return NO_ERROR;
2925 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002926 case 1008: // toggle use of hw composer
2927 n = data.readInt32();
2928 mDebugDisableHWC = n ? 1 : 0;
2929 invalidateHwcGeometry();
2930 repaintEverything();
2931 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002932 case 1009: // toggle use of transform hint
2933 n = data.readInt32();
2934 mDebugDisableTransformHint = n ? 1 : 0;
2935 invalidateHwcGeometry();
2936 repaintEverything();
2937 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002938 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002939 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002940 reply->writeInt32(0);
2941 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002942 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002943 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002944 return NO_ERROR;
2945 case 1013: {
2946 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002947 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2948 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002949 return NO_ERROR;
2950 }
2951 case 1014: {
2952 // daltonize
2953 n = data.readInt32();
2954 switch (n % 10) {
2955 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2956 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2957 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2958 }
2959 if (n >= 10) {
2960 mDaltonizer.setMode(Daltonizer::correction);
2961 } else {
2962 mDaltonizer.setMode(Daltonizer::simulation);
2963 }
2964 mDaltonize = n > 0;
2965 invalidateHwcGeometry();
2966 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002967 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002968 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002969 case 1015: {
2970 // apply a color matrix
2971 n = data.readInt32();
2972 mHasColorMatrix = n ? 1 : 0;
2973 if (n) {
2974 // color matrix is sent as mat3 matrix followed by vec3
2975 // offset, then packed into a mat4 where the last row is
2976 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002977 for (size_t i = 0 ; i < 4; i++) {
2978 for (size_t j = 0; j < 4; j++) {
2979 mColorMatrix[i][j] = data.readFloat();
2980 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002981 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002982 } else {
2983 mColorMatrix = mat4();
2984 }
2985 invalidateHwcGeometry();
2986 repaintEverything();
2987 return NO_ERROR;
2988 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002989 // This is an experimental interface
2990 // Needs to be shifted to proper binder interface when we productize
2991 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07002992 n = data.readInt32();
2993 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002994 return NO_ERROR;
2995 }
Dan Stozaee44edd2015-03-23 15:50:23 -07002996 case 1017: {
2997 n = data.readInt32();
2998 mForceFullDamage = static_cast<bool>(n);
2999 return NO_ERROR;
3000 }
Dan Stozadb4ac3c2015-04-14 11:34:01 -07003001 case 1018: { // Modify Choreographer's phase offset
3002 n = data.readInt32();
3003 mEventThread->setPhaseOffset(static_cast<nsecs_t>(n));
3004 return NO_ERROR;
3005 }
3006 case 1019: { // Modify SurfaceFlinger's phase offset
3007 n = data.readInt32();
3008 mSFEventThread->setPhaseOffset(static_cast<nsecs_t>(n));
3009 return NO_ERROR;
3010 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003011 }
3012 }
3013 return err;
3014}
3015
Mathias Agopian53331da2011-08-22 21:44:41 -07003016void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07003017 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08003018 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07003019}
3020
Mathias Agopian59119e62010-10-11 12:37:43 -07003021// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003022// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07003023// ---------------------------------------------------------------------------
3024
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003025/* The code below is here to handle b/8734824
3026 *
3027 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07003028 * from the surfaceflinger thread to the calling binder thread, where they
3029 * are executed. This allows the calling thread in the calling process to be
3030 * reused and not depend on having "enough" binder threads to handle the
3031 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003032 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003033class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07003034 /* Parts of GraphicProducerWrapper are run on two different threads,
3035 * communicating by sending messages via Looper but also by shared member
3036 * data. Coherence maintenance is subtle and in places implicit (ugh).
3037 *
3038 * Don't rely on Looper's sendMessage/handleMessage providing
3039 * release/acquire semantics for any data not actually in the Message.
3040 * Data going from surfaceflinger to binder threads needs to be
3041 * synchronized explicitly.
3042 *
3043 * Barrier open/wait do provide release/acquire semantics. This provides
3044 * implicit synchronization for data coming back from binder to
3045 * surfaceflinger threads.
3046 */
3047
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003048 sp<IGraphicBufferProducer> impl;
3049 sp<Looper> looper;
3050 status_t result;
3051 bool exitPending;
3052 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07003053 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003054 uint32_t code;
3055 Parcel const* data;
3056 Parcel* reply;
3057
3058 enum {
3059 MSG_API_CALL,
3060 MSG_EXIT
3061 };
3062
3063 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003064 * Called on surfaceflinger thread. This is called by our "fake"
3065 * BpGraphicBufferProducer. We package the data and reply Parcel and
3066 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003067 */
3068 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08003069 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003070 this->code = code;
3071 this->data = &data;
3072 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003073 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07003074 // if we've exited, we run the message synchronously right here.
3075 // note (JH): as far as I can tell from looking at the code, this
3076 // never actually happens. if it does, i'm not sure if it happens
3077 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003078 handleMessage(Message(MSG_API_CALL));
3079 } else {
3080 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07003081 // Prevent stores to this->{code, data, reply} from being
3082 // reordered later than the construction of Message.
3083 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003084 looper->sendMessage(this, Message(MSG_API_CALL));
3085 barrier.wait();
3086 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08003087 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003088 }
3089
3090 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003091 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003092 * call the real BpGraphicBufferProducer.
3093 */
3094 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07003095 int what = message.what;
3096 // Prevent reads below from happening before the read from Message
3097 atomic_thread_fence(memory_order_acquire);
3098 if (what == MSG_API_CALL) {
Marco Nelissen097ca272014-11-14 08:01:01 -08003099 result = IInterface::asBinder(impl)->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003100 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07003101 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003102 exitRequested = true;
3103 }
3104 }
3105
3106public:
Jesse Hallb154c422014-07-13 12:47:02 -07003107 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
3108 : impl(impl),
3109 looper(new Looper(true)),
Pablo Ceballos53390e12015-08-04 11:25:59 -07003110 result(NO_ERROR),
Jesse Hallb154c422014-07-13 12:47:02 -07003111 exitPending(false),
Pablo Ceballos53390e12015-08-04 11:25:59 -07003112 exitRequested(false),
3113 code(0),
3114 data(NULL),
3115 reply(NULL)
Jesse Hallb154c422014-07-13 12:47:02 -07003116 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003117
Jesse Hallb154c422014-07-13 12:47:02 -07003118 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003119 status_t waitForResponse() {
3120 do {
3121 looper->pollOnce(-1);
3122 } while (!exitRequested);
3123 return result;
3124 }
3125
Jesse Hallb154c422014-07-13 12:47:02 -07003126 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003127 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07003128 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003129 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07003130 // Ensure this->result is visible to the binder thread before it
3131 // handles the message.
3132 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003133 looper->sendMessage(this, Message(MSG_EXIT));
3134 }
3135};
3136
3137
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003138status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
3139 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003140 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003141 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003142 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003143
3144 if (CC_UNLIKELY(display == 0))
3145 return BAD_VALUE;
3146
3147 if (CC_UNLIKELY(producer == 0))
3148 return BAD_VALUE;
3149
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003150 // if we have secure windows on this display, never allow the screen capture
3151 // unless the producer interface is local (i.e.: we can take a screenshot for
3152 // ourselves).
Marco Nelissen097ca272014-11-14 08:01:01 -08003153 if (!IInterface::asBinder(producer)->localBinder()) {
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003154 Mutex::Autolock _l(mStateLock);
3155 sp<const DisplayDevice> hw(getDisplayDevice(display));
3156 if (hw->getSecureLayerVisible()) {
3157 ALOGW("FB is protected: PERMISSION_DENIED");
3158 return PERMISSION_DENIED;
3159 }
3160 }
3161
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003162 // Convert to surfaceflinger's internal rotation type.
3163 Transform::orientation_flags rotationFlags;
3164 switch (rotation) {
3165 case ISurfaceComposer::eRotateNone:
3166 rotationFlags = Transform::ROT_0;
3167 break;
3168 case ISurfaceComposer::eRotate90:
3169 rotationFlags = Transform::ROT_90;
3170 break;
3171 case ISurfaceComposer::eRotate180:
3172 rotationFlags = Transform::ROT_180;
3173 break;
3174 case ISurfaceComposer::eRotate270:
3175 rotationFlags = Transform::ROT_270;
3176 break;
3177 default:
3178 rotationFlags = Transform::ROT_0;
3179 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3180 break;
3181 }
3182
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003183 class MessageCaptureScreen : public MessageBase {
3184 SurfaceFlinger* flinger;
3185 sp<IBinder> display;
3186 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003187 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003188 uint32_t reqWidth, reqHeight;
3189 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003190 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003191 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003192 status_t result;
3193 public:
3194 MessageCaptureScreen(SurfaceFlinger* flinger,
3195 const sp<IBinder>& display,
3196 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003197 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003198 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003199 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003200 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003201 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003202 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003203 useIdentityTransform(useIdentityTransform),
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003204 rotation(rotation),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003205 result(PERMISSION_DENIED)
3206 {
3207 }
3208 status_t getResult() const {
3209 return result;
3210 }
3211 virtual bool handler() {
3212 Mutex::Autolock _l(flinger->mStateLock);
3213 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003214 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003215 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003216 useIdentityTransform, rotation);
Marco Nelissen097ca272014-11-14 08:01:01 -08003217 static_cast<GraphicProducerWrapper*>(IInterface::asBinder(producer).get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003218 return true;
3219 }
3220 };
3221
Mathias Agopian9eb1f052013-04-10 16:27:17 -07003222 // make sure to process transactions before screenshots -- a transaction
3223 // might already be pending but scheduled for VSYNC; this guarantees we
3224 // will handle it before the screenshot. When VSYNC finally arrives
3225 // the scheduled transaction will be a no-op. If no transactions are
3226 // scheduled at this time, this will end-up being a no-op as well.
3227 mEventQueue.invalidateTransactionNow();
3228
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003229 // this creates a "fake" BBinder which will serve as a "fake" remote
3230 // binder to receive the marshaled calls and forward them to the
3231 // real remote (a BpGraphicBufferProducer)
3232 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3233
3234 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3235 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003236 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003237 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003238 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003239 useIdentityTransform, rotationFlags);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003240
3241 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003242 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003243 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003244 }
3245 return res;
3246}
3247
Mathias Agopian180f10d2013-04-10 22:55:41 -07003248
3249void SurfaceFlinger::renderScreenImplLocked(
3250 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003251 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003252 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003253 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003254{
3255 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003256 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003257
3258 // get screen geometry
Andreas Gampe89fd4f72014-11-13 14:18:56 -08003259 const int32_t hw_w = hw->getWidth();
3260 const int32_t hw_h = hw->getHeight();
3261 const bool filtering = static_cast<int32_t>(reqWidth) != hw_w ||
Christopher Ferris0e749792015-03-23 14:32:15 -07003262 static_cast<int32_t>(reqHeight) != hw_h;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003263
Dan Stozac1879002014-05-22 15:59:05 -07003264 // if a default or invalid sourceCrop is passed in, set reasonable values
3265 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3266 !sourceCrop.isValid()) {
3267 sourceCrop.setLeftTop(Point(0, 0));
3268 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3269 }
3270
3271 // ensure that sourceCrop is inside screen
3272 if (sourceCrop.left < 0) {
3273 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3274 }
Dan Stozabe31f442014-06-11 11:20:54 -07003275 if (sourceCrop.right > hw_w) {
3276 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003277 }
3278 if (sourceCrop.top < 0) {
3279 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3280 }
Dan Stozabe31f442014-06-11 11:20:54 -07003281 if (sourceCrop.bottom > hw_h) {
3282 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003283 }
3284
Mathias Agopian180f10d2013-04-10 22:55:41 -07003285 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003286 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003287
3288 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003289 engine.setViewportAndProjection(
3290 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003291 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003292
3293 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003294 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003295
3296 const LayerVector& layers( mDrawingState.layersSortedByZ );
3297 const size_t count = layers.size();
3298 for (size_t i=0 ; i<count ; ++i) {
3299 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003300 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003301 if (state.layerStack == hw->getLayerStack()) {
3302 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3303 if (layer->isVisible()) {
3304 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08003305 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003306 if (filtering) layer->setFiltering(false);
3307 }
3308 }
3309 }
3310 }
3311
3312 // compositionComplete is needed for older driver
3313 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003314 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003315}
3316
3317
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003318status_t SurfaceFlinger::captureScreenImplLocked(
3319 const sp<const DisplayDevice>& hw,
3320 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003321 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003322 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003323 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003324{
3325 ATRACE_CALL();
3326
Mathias Agopian180f10d2013-04-10 22:55:41 -07003327 // get screen geometry
Dan Stoza35024162015-06-09 16:44:40 -07003328 uint32_t hw_w = hw->getWidth();
3329 uint32_t hw_h = hw->getHeight();
3330
3331 if (rotation & Transform::ROT_90) {
3332 std::swap(hw_w, hw_h);
3333 }
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003334
Mathias Agopian180f10d2013-04-10 22:55:41 -07003335 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3336 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3337 reqWidth, reqHeight, hw_w, hw_h);
3338 return BAD_VALUE;
3339 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003340
Mathias Agopian180f10d2013-04-10 22:55:41 -07003341 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3342 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3343
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003344 // create a surface (because we're a producer, and we need to
3345 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003346 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003347 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003348
Michael Lentine5a16a622015-05-21 13:48:24 -07003349 status_t result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
3350 if (result == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003351 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3352 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003353
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003354 int err = 0;
3355 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003356 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003357 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3358 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003359
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003360 if (err == NO_ERROR) {
3361 ANativeWindowBuffer* buffer;
3362 /* TODO: Once we have the sync framework everywhere this can use
3363 * server-side waits on the fence that dequeueBuffer returns.
3364 */
3365 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3366 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003367 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003368 // create an EGLImage from the buffer so we can later
3369 // turn it into a texture
3370 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3371 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3372 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003373 // this binds the given EGLImage as a framebuffer for the
3374 // duration of this scope.
3375 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3376 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003377 // this will in fact render into our dequeued buffer
3378 // via an FBO, which means we didn't have to create
3379 // an EGLSurface and therefore we're not
3380 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003381 renderScreenImplLocked(
3382 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3383 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003384
Riley Andrews86639902014-08-15 12:27:24 -07003385 // Attempt to create a sync khr object that can produce a sync point. If that
3386 // isn't available, create a non-dupable sync object in the fallback path and
3387 // wait on it directly.
3388 EGLSyncKHR sync;
3389 if (!DEBUG_SCREENSHOTS) {
3390 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Riley Andrews9707f4d2014-10-23 16:17:04 -07003391 // native fence fd will not be populated until flush() is done.
3392 getRenderEngine().flush();
Andy McFadden2d8d1202013-10-09 16:38:02 -07003393 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003394 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003395 }
Riley Andrews86639902014-08-15 12:27:24 -07003396 if (sync != EGL_NO_SYNC_KHR) {
3397 // get the sync fd
3398 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3399 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3400 ALOGW("captureScreen: failed to dup sync khr object");
3401 syncFd = -1;
3402 }
3403 eglDestroySyncKHR(mEGLDisplay, sync);
3404 } else {
3405 // fallback path
3406 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3407 if (sync != EGL_NO_SYNC_KHR) {
3408 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3409 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3410 EGLint eglErr = eglGetError();
3411 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3412 ALOGW("captureScreen: fence wait timed out");
3413 } else {
3414 ALOGW_IF(eglErr != EGL_SUCCESS,
3415 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3416 }
3417 eglDestroySyncKHR(mEGLDisplay, sync);
3418 } else {
3419 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3420 }
3421 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003422 if (DEBUG_SCREENSHOTS) {
3423 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3424 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3425 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3426 hw, minLayerZ, maxLayerZ);
3427 delete [] pixels;
3428 }
3429
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003430 } else {
3431 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3432 result = INVALID_OPERATION;
3433 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003434 // destroy our image
3435 eglDestroyImageKHR(mEGLDisplay, image);
3436 } else {
3437 result = BAD_VALUE;
3438 }
Jesse Hallafe2b1f2014-10-21 11:09:17 -07003439 // queueBuffer takes ownership of syncFd
Michael Lentine5a16a622015-05-21 13:48:24 -07003440 result = window->queueBuffer(window, buffer, syncFd);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003441 }
3442 } else {
3443 result = BAD_VALUE;
3444 }
3445 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3446 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003447
Mathias Agopian74c40c02010-09-29 13:02:36 -07003448 return result;
3449}
3450
Mathias Agopiand5556842013-09-19 17:08:37 -07003451void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3452 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003453 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003454 for (size_t y=0 ; y<h ; y++) {
3455 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3456 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003457 if (p[x] != 0xFF000000) return;
3458 }
3459 }
3460 ALOGE("*** we just took a black screenshot ***\n"
3461 "requested minz=%d, maxz=%d, layerStack=%d",
3462 minLayerZ, maxLayerZ, hw->getLayerStack());
3463 const LayerVector& layers( mDrawingState.layersSortedByZ );
3464 const size_t count = layers.size();
3465 for (size_t i=0 ; i<count ; ++i) {
3466 const sp<Layer>& layer(layers[i]);
3467 const Layer::State& state(layer->getDrawingState());
3468 const bool visible = (state.layerStack == hw->getLayerStack())
3469 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3470 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003471 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003472 visible ? '+' : '-',
3473 i, layer->getName().string(), state.layerStack, state.z,
3474 layer->isVisible(), state.flags, state.alpha);
3475 }
3476 }
3477}
3478
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003479// ---------------------------------------------------------------------------
3480
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003481SurfaceFlinger::LayerVector::LayerVector() {
3482}
3483
3484SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003485 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003486}
3487
3488int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3489 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003490{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003491 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003492 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3493 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003494
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003495 uint32_t ls = l->getCurrentState().layerStack;
3496 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003497 if (ls != rs)
3498 return ls - rs;
3499
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003500 uint32_t lz = l->getCurrentState().z;
3501 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003502 if (lz != rz)
3503 return lz - rz;
3504
3505 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003506}
3507
3508// ---------------------------------------------------------------------------
3509
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003510SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Pablo Ceballos53390e12015-08-04 11:25:59 -07003511 : type(DisplayDevice::DISPLAY_ID_INVALID),
3512 layerStack(DisplayDevice::NO_LAYER_STACK),
3513 orientation(0),
3514 width(0),
3515 height(0),
3516 isSecure(false) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003517}
3518
Pablo Ceballos53390e12015-08-04 11:25:59 -07003519SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(
3520 DisplayDevice::DisplayType type, bool isSecure)
3521 : type(type),
3522 layerStack(DisplayDevice::NO_LAYER_STACK),
3523 orientation(0),
3524 width(0),
3525 height(0),
3526 isSecure(isSecure) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003527 viewport.makeInvalid();
3528 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003529}
3530
3531// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003532
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003533}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003534
3535
3536#if defined(__gl_h_)
3537#error "don't include gl/gl.h in this file"
3538#endif
3539
3540#if defined(__gl2_h_)
3541#error "don't include gl2/gl2.h in this file"
3542#endif