blob: 093ca12fd617f4da955b8e345cbd21ffd0fe1057 [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
Dan Stoza14cd37c2015-07-09 12:43:33 -0700167 property_get("debug.sf.drop_missed_frames", value, "0");
168 mDropMissedFrames = atoi(value);
169
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170 property_get("debug.sf.showupdates", value, "0");
171 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700172
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700173 property_get("debug.sf.ddms", value, "0");
174 mDebugDDMS = atoi(value);
175 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700176 if (!startDdmConnection()) {
177 // start failed, and DDMS debugging not enabled
178 mDebugDDMS = 0;
179 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700180 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700181 ALOGI_IF(mDebugRegion, "showupdates enabled");
182 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183}
184
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800185void SurfaceFlinger::onFirstRef()
186{
187 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800188}
189
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190SurfaceFlinger::~SurfaceFlinger()
191{
Mathias Agopiana4912602012-07-12 14:25:33 -0700192 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
193 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
194 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195}
196
Dan Stozac7014012014-02-14 15:03:43 -0800197void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800198{
199 // the window manager died on us. prepare its eulogy.
200
Andy McFadden13a082e2012-08-24 10:16:42 -0700201 // restore initial conditions (default device unblank, etc)
202 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800203
204 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700205 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800206}
207
Mathias Agopian7e27f052010-05-28 14:22:23 -0700208sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800209{
Mathias Agopian96f08192010-06-02 23:28:45 -0700210 sp<ISurfaceComposerClient> bclient;
211 sp<Client> client(new Client(this));
212 status_t err = client->initCheck();
213 if (err == NO_ERROR) {
214 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800215 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800216 return bclient;
217}
218
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700219sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
220 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700221{
222 class DisplayToken : public BBinder {
223 sp<SurfaceFlinger> flinger;
224 virtual ~DisplayToken() {
225 // no more references, this display must be terminated
226 Mutex::Autolock _l(flinger->mStateLock);
227 flinger->mCurrentState.displays.removeItem(this);
228 flinger->setTransactionFlags(eDisplayTransactionNeeded);
229 }
230 public:
231 DisplayToken(const sp<SurfaceFlinger>& flinger)
232 : flinger(flinger) {
233 }
234 };
235
236 sp<BBinder> token = new DisplayToken(this);
237
238 Mutex::Autolock _l(mStateLock);
Pablo Ceballos53390e12015-08-04 11:25:59 -0700239 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL, secure);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700240 info.displayName = displayName;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700241 mCurrentState.displays.add(token, info);
242
243 return token;
244}
245
Jesse Hall6c913be2013-08-08 12:15:49 -0700246void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
247 Mutex::Autolock _l(mStateLock);
248
249 ssize_t idx = mCurrentState.displays.indexOfKey(display);
250 if (idx < 0) {
251 ALOGW("destroyDisplay: invalid display token");
252 return;
253 }
254
255 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
256 if (!info.isVirtualDisplay()) {
257 ALOGE("destroyDisplay called for non-virtual display");
258 return;
259 }
260
261 mCurrentState.displays.removeItemsAt(idx);
262 setTransactionFlags(eDisplayTransactionNeeded);
263}
264
Jesse Hall692c7232012-11-08 15:41:56 -0800265void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
266 ALOGW_IF(mBuiltinDisplays[type],
267 "Overwriting display token for display type %d", type);
268 mBuiltinDisplays[type] = new BBinder();
Jesse Hall692c7232012-11-08 15:41:56 -0800269 // All non-virtual displays are currently considered secure.
Pablo Ceballos53390e12015-08-04 11:25:59 -0700270 DisplayDeviceState info(type, true);
Jesse Hall692c7232012-11-08 15:41:56 -0800271 mCurrentState.displays.add(mBuiltinDisplays[type], info);
272}
273
Mathias Agopiane57f2922012-08-09 16:29:12 -0700274sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700275 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700276 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
277 return NULL;
278 }
Jesse Hall692c7232012-11-08 15:41:56 -0800279 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700280}
281
Jamie Gennis9a78c902011-01-12 18:30:40 -0800282sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
283{
284 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
285 return gba;
286}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700287
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288void SurfaceFlinger::bootFinished()
289{
290 const nsecs_t now = systemTime();
291 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000292 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700293 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700294
295 // wait patiently for the window manager death
296 const String16 name("window");
297 sp<IBinder> window(defaultServiceManager()->getService(name));
298 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700299 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700300 }
301
302 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700303 // formerly we would just kill the process, but we now ask it to exit so it
304 // can choose where to stop the animation.
305 property_set("service.bootanim.exit", "1");
Yusuke Sato0a688f52015-07-30 23:05:39 -0700306
307 const int LOGTAG_SF_STOP_BOOTANIM = 60110;
308 LOG_EVENT_LONG(LOGTAG_SF_STOP_BOOTANIM,
309 ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800310}
311
Mathias Agopian3f844832013-08-07 21:24:32 -0700312void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700313 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700314 RenderEngine& engine;
315 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700316 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700317 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
318 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700319 }
320 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700321 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700322 return true;
323 }
324 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700325 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700326}
327
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700328class DispSyncSource : public VSyncSource, private DispSync::Callback {
329public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700330 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
331 const char* label) :
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700332 mValue(0),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700333 mTraceVsync(traceVsync),
Andy McFadden5167ec62014-05-22 13:08:43 -0700334 mVsyncOnLabel(String8::format("VsyncOn-%s", label)),
335 mVsyncEventLabel(String8::format("VSYNC-%s", label)),
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700336 mDispSync(dispSync),
337 mCallbackMutex(),
338 mCallback(),
339 mVsyncMutex(),
340 mPhaseOffset(phaseOffset),
341 mEnabled(false) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700342
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700343 virtual ~DispSyncSource() {}
344
345 virtual void setVSyncEnabled(bool enable) {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700346 Mutex::Autolock lock(mVsyncMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700347 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700348 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700349 static_cast<DispSync::Callback*>(this));
350 if (err != NO_ERROR) {
351 ALOGE("error registering vsync callback: %s (%d)",
352 strerror(-err), err);
353 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700354 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700355 } else {
356 status_t err = mDispSync->removeEventListener(
357 static_cast<DispSync::Callback*>(this));
358 if (err != NO_ERROR) {
359 ALOGE("error unregistering vsync callback: %s (%d)",
360 strerror(-err), err);
361 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700362 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700363 }
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700364 mEnabled = enable;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700365 }
366
367 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700368 Mutex::Autolock lock(mCallbackMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700369 mCallback = callback;
370 }
371
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700372 virtual void setPhaseOffset(nsecs_t phaseOffset) {
373 Mutex::Autolock lock(mVsyncMutex);
374
375 // Normalize phaseOffset to [0, period)
376 auto period = mDispSync->getPeriod();
377 phaseOffset %= period;
378 if (phaseOffset < 0) {
379 // If we're here, then phaseOffset is in (-period, 0). After this
380 // operation, it will be in (0, period)
381 phaseOffset += period;
382 }
383 mPhaseOffset = phaseOffset;
384
385 // If we're not enabled, we don't need to mess with the listeners
386 if (!mEnabled) {
387 return;
388 }
389
390 // Remove the listener with the old offset
391 status_t err = mDispSync->removeEventListener(
392 static_cast<DispSync::Callback*>(this));
393 if (err != NO_ERROR) {
394 ALOGE("error unregistering vsync callback: %s (%d)",
395 strerror(-err), err);
396 }
397
398 // Add a listener with the new offset
399 err = mDispSync->addEventListener(mPhaseOffset,
400 static_cast<DispSync::Callback*>(this));
401 if (err != NO_ERROR) {
402 ALOGE("error registering vsync callback: %s (%d)",
403 strerror(-err), err);
404 }
405 }
406
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700407private:
408 virtual void onDispSyncEvent(nsecs_t when) {
409 sp<VSyncSource::Callback> callback;
410 {
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700411 Mutex::Autolock lock(mCallbackMutex);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700412 callback = mCallback;
413
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700414 if (mTraceVsync) {
415 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700416 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700417 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700418 }
419
420 if (callback != NULL) {
421 callback->onVSyncEvent(when);
422 }
423 }
424
425 int mValue;
426
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700427 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700428 const String8 mVsyncOnLabel;
429 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700430
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700431 DispSync* mDispSync;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700432
433 Mutex mCallbackMutex; // Protects the following
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700434 sp<VSyncSource::Callback> mCallback;
Dan Stozadb4ac3c2015-04-14 11:34:01 -0700435
436 Mutex mVsyncMutex; // Protects the following
437 nsecs_t mPhaseOffset;
438 bool mEnabled;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700439};
440
441void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700442 ALOGI( "SurfaceFlinger's main thread ready to run. "
443 "Initializing graphics H/W...");
444
Jesse Hall692c7232012-11-08 15:41:56 -0800445 Mutex::Autolock _l(mStateLock);
446
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700447 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700448 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
449 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700450
Saurabh Shahf9481052015-06-15 17:55:51 -0700451 // start the EventThread
452 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
453 vsyncPhaseOffsetNs, true, "app");
454 mEventThread = new EventThread(vsyncSrc);
455 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
456 sfVsyncPhaseOffsetNs, true, "sf");
457 mSFEventThread = new EventThread(sfVsyncSrc);
458 mEventQueue.setEventThread(mSFEventThread);
459
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700460 // Initialize the H/W composer object. There may or may not be an
461 // actual hardware composer underneath.
462 mHwc = new HWComposer(this,
463 *static_cast<HWComposer::EventHandler *>(this));
464
Mathias Agopian875d8e12013-06-07 15:35:48 -0700465 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800466 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700467
468 // retrieve the EGL context that was selected/created
469 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700470
Mathias Agopianda27af92012-09-13 18:17:13 -0700471 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
472 "couldn't create EGLContext");
473
Mathias Agopiancde87a32012-09-13 14:09:01 -0700474 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700475 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700476 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700477 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700478 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700479 // All non-virtual displays are currently considered secure.
480 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800481 createBuiltinDisplayLocked(type);
482 wp<IBinder> token = mBuiltinDisplays[i];
483
Dan Stozab9b08832014-03-13 11:55:57 -0700484 sp<IGraphicBufferProducer> producer;
485 sp<IGraphicBufferConsumer> consumer;
486 BufferQueue::createBufferQueue(&producer, &consumer,
487 new GraphicBufferAlloc());
488
489 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
490 consumer);
Jesse Hall19e87292013-12-23 21:02:15 -0800491 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700492 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800493 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Dan Stozab9b08832014-03-13 11:55:57 -0700494 fbs, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -0800495 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700496 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700497 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700498 // for displays other than the main display, so we always
499 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800500 ALOGD("marking display %zu as acquired/unblanked", i);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700501 hw->setPowerMode(HWC_POWER_MODE_NORMAL);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700502 }
503 mDisplays.add(token, hw);
504 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700505 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700506
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700507 // make the GLContext current so that we can create textures when creating Layers
508 // (which may happens before we render something)
509 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
510
Jamie Gennisd1700752013-10-14 12:22:52 -0700511 mEventControlThread = new EventControlThread(this);
512 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
513
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700514 // set a fake vsync period if there is no HWComposer
515 if (mHwc->initCheck() != NO_ERROR) {
516 mPrimaryDispSync.setPeriod(16666667);
517 }
518
Mathias Agopian92a979a2012-08-02 18:32:23 -0700519 // initialize our drawing state
520 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700521
Andy McFadden13a082e2012-08-24 10:16:42 -0700522 // set initial conditions (e.g. unblank default device)
523 initializeDisplays();
524
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700525 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700526 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800527}
528
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700529int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700530 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700531 type : mHwc->allocateDisplayId();
532}
533
Mathias Agopiana67e4182012-06-19 17:26:12 -0700534void SurfaceFlinger::startBootAnim() {
535 // start boot animation
536 property_set("service.bootanim.exit", "0");
537 property_set("ctl.start", "bootanim");
538}
539
Mathias Agopian875d8e12013-06-07 15:35:48 -0700540size_t SurfaceFlinger::getMaxTextureSize() const {
541 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700542}
543
Mathias Agopian875d8e12013-06-07 15:35:48 -0700544size_t SurfaceFlinger::getMaxViewportDims() const {
545 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700546}
547
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800548// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800549
Jamie Gennis582270d2011-08-17 18:19:00 -0700550bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800551 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800552 Mutex::Autolock _l(mStateLock);
Marco Nelissen097ca272014-11-14 08:01:01 -0800553 sp<IBinder> surfaceTextureBinder(IInterface::asBinder(bufferProducer));
Mathias Agopian67106042013-03-14 19:18:13 -0700554 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800555}
556
Dan Stoza7f7da322014-05-02 15:26:25 -0700557status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
558 Vector<DisplayInfo>* configs) {
Tatenda Chipeperekwa23e16bb2014-10-29 16:47:19 -0700559 if ((configs == NULL) || (display.get() == NULL)) {
Dan Stoza7f7da322014-05-02 15:26:25 -0700560 return BAD_VALUE;
561 }
562
Naseer Ahmed7aa0c472014-11-03 14:49:23 -0500563 if (!display.get())
564 return NAME_NOT_FOUND;
565
Jesse Hall692c7232012-11-08 15:41:56 -0800566 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700567 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800568 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700569 type = i;
570 break;
571 }
572 }
573
574 if (type < 0) {
575 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700576 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700577
Mathias Agopian8b736f12012-08-13 17:54:26 -0700578 // TODO: Not sure if display density should handled by SF any longer
579 class Density {
580 static int getDensityFromProperty(char const* propName) {
581 char property[PROPERTY_VALUE_MAX];
582 int density = 0;
583 if (property_get(propName, property, NULL) > 0) {
584 density = atoi(property);
585 }
586 return density;
587 }
588 public:
589 static int getEmuDensity() {
590 return getDensityFromProperty("qemu.sf.lcd_density"); }
591 static int getBuildDensity() {
592 return getDensityFromProperty("ro.sf.lcd_density"); }
593 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700594
Dan Stoza7f7da322014-05-02 15:26:25 -0700595 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700596
Dan Stoza7f7da322014-05-02 15:26:25 -0700597 const Vector<HWComposer::DisplayConfig>& hwConfigs =
598 getHwComposer().getConfigs(type);
599 for (size_t c = 0; c < hwConfigs.size(); ++c) {
600 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
601 DisplayInfo info = DisplayInfo();
602
603 float xdpi = hwConfig.xdpi;
604 float ydpi = hwConfig.ydpi;
605
606 if (type == DisplayDevice::DISPLAY_PRIMARY) {
607 // The density of the device is provided by a build property
608 float density = Density::getBuildDensity() / 160.0f;
609 if (density == 0) {
610 // the build doesn't provide a density -- this is wrong!
611 // use xdpi instead
612 ALOGE("ro.sf.lcd_density must be defined as a build property");
613 density = xdpi / 160.0f;
614 }
615 if (Density::getEmuDensity()) {
616 // if "qemu.sf.lcd_density" is specified, it overrides everything
617 xdpi = ydpi = density = Density::getEmuDensity();
618 density /= 160.0f;
619 }
620 info.density = density;
621
622 // TODO: this needs to go away (currently needed only by webkit)
623 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
624 info.orientation = hw->getOrientation();
625 } else {
626 // TODO: where should this value come from?
627 static const int TV_DENSITY = 213;
628 info.density = TV_DENSITY / 160.0f;
629 info.orientation = 0;
630 }
631
632 info.w = hwConfig.width;
633 info.h = hwConfig.height;
634 info.xdpi = xdpi;
635 info.ydpi = ydpi;
636 info.fps = float(1e9 / hwConfig.refresh);
Andy McFadden91b2ca82014-06-13 14:04:23 -0700637 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
Dan Stozaf2699fc2015-08-31 12:06:48 -0700638 info.colorTransform = hwConfig.colorTransform;
Andy McFadden91b2ca82014-06-13 14:04:23 -0700639
640 // This is how far in advance a buffer must be queued for
641 // presentation at a given time. If you want a buffer to appear
642 // on the screen at time N, you must submit the buffer before
643 // (N - presentationDeadline).
644 //
645 // Normally it's one full refresh period (to give SF a chance to
646 // latch the buffer), but this can be reduced by configuring a
647 // DispSync offset. Any additional delays introduced by the hardware
648 // composer or panel must be accounted for here.
649 //
650 // We add an additional 1ms to allow for processing time and
651 // differences between the ideal and actual refresh rate.
652 info.presentationDeadline =
653 hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700654
655 // All non-virtual displays are currently considered secure.
656 info.secure = true;
657
658 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700659 }
660
Dan Stoza7f7da322014-05-02 15:26:25 -0700661 return NO_ERROR;
662}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700663
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800664status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& /* display */,
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700665 DisplayStatInfo* stats) {
666 if (stats == NULL) {
667 return BAD_VALUE;
668 }
669
670 // FIXME for now we always return stats for the primary display
671 memset(stats, 0, sizeof(*stats));
672 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
673 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
674 return NO_ERROR;
675}
676
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700677int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
Dan Stoza24a42e92015-03-09 10:04:11 -0700678 sp<DisplayDevice> device(getDisplayDevice(display));
679 if (device != NULL) {
680 return device->getActiveConfig();
681 }
682 return BAD_VALUE;
Dan Stoza7f7da322014-05-02 15:26:25 -0700683}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700684
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700685void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
686 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
687 this);
688 int32_t type = hw->getDisplayType();
689 int currentMode = hw->getActiveConfig();
690
691 if (mode == currentMode) {
692 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
693 return;
694 }
695
696 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
697 ALOGW("Trying to set config for virtual display");
698 return;
699 }
700
701 hw->setActiveConfig(mode);
702 getHwComposer().setActiveConfig(type, mode);
703}
704
705status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
706 class MessageSetActiveConfig: public MessageBase {
707 SurfaceFlinger& mFlinger;
708 sp<IBinder> mDisplay;
709 int mMode;
710 public:
711 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
712 int mode) :
713 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
714 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700715 Vector<DisplayInfo> configs;
716 mFlinger.getDisplayConfigs(mDisplay, &configs);
Jesse Hall78442112014-08-07 22:43:06 -0700717 if (mMode < 0 || mMode >= static_cast<int>(configs.size())) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700718 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700719 mMode, configs.size());
720 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700721 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
722 if (hw == NULL) {
723 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700724 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700725 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
726 ALOGW("Attempt to set active config = %d for virtual display",
727 mMode);
728 } else {
729 mFlinger.setActiveConfigInternal(hw, mMode);
730 }
731 return true;
732 }
733 };
734 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
735 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700736 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700737}
738
Svetoslavd85084b2014-03-20 10:28:31 -0700739status_t SurfaceFlinger::clearAnimationFrameStats() {
740 Mutex::Autolock _l(mStateLock);
741 mAnimFrameTracker.clearStats();
742 return NO_ERROR;
743}
744
745status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
746 Mutex::Autolock _l(mStateLock);
747 mAnimFrameTracker.getStats(outStats);
748 return NO_ERROR;
749}
750
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800751// ----------------------------------------------------------------------------
752
753sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800754 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700755}
756
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800757// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800758
759void SurfaceFlinger::waitForEvent() {
760 mEventQueue.waitMessage();
761}
762
763void SurfaceFlinger::signalTransaction() {
764 mEventQueue.invalidate();
765}
766
767void SurfaceFlinger::signalLayerUpdate() {
768 mEventQueue.invalidate();
769}
770
771void SurfaceFlinger::signalRefresh() {
772 mEventQueue.refresh();
773}
774
775status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800776 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800777 return mEventQueue.postMessage(msg, reltime);
778}
779
780status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800781 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800782 status_t res = mEventQueue.postMessage(msg, reltime);
783 if (res == NO_ERROR) {
784 msg->wait();
785 }
786 return res;
787}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800788
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700789void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700790 do {
791 waitForEvent();
792 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800793}
794
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700795void SurfaceFlinger::enableHardwareVsync() {
796 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700797 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700798 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700799 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
800 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700801 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700802 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700803}
804
Jesse Hall948fe0c2013-10-14 12:56:09 -0700805void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700806 Mutex::Autolock _l(mHWVsyncLock);
807
Jesse Hall948fe0c2013-10-14 12:56:09 -0700808 if (makeAvailable) {
809 mHWVsyncAvailable = true;
810 } else if (!mHWVsyncAvailable) {
811 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
812 return;
813 }
814
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700815 const nsecs_t period =
816 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
817
818 mPrimaryDispSync.reset();
819 mPrimaryDispSync.setPeriod(period);
820
821 if (!mPrimaryHWVsyncEnabled) {
822 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700823 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
824 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700825 mPrimaryHWVsyncEnabled = true;
826 }
827}
828
Jesse Hall948fe0c2013-10-14 12:56:09 -0700829void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700830 Mutex::Autolock _l(mHWVsyncLock);
831 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700832 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
833 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700834 mPrimaryDispSync.endResync();
835 mPrimaryHWVsyncEnabled = false;
836 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700837 if (makeUnavailable) {
838 mHWVsyncAvailable = false;
839 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700840}
841
842void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700843 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700844
Jamie Gennisd1700752013-10-14 12:22:52 -0700845 { // Scope for the lock
846 Mutex::Autolock _l(mHWVsyncLock);
847 if (type == 0 && mPrimaryHWVsyncEnabled) {
848 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700849 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700850 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700851
852 if (needsHwVsync) {
853 enableHardwareVsync();
854 } else {
855 disableHardwareVsync(false);
856 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700857}
858
859void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
860 if (mEventThread == NULL) {
861 // This is a temporary workaround for b/7145521. A non-null pointer
862 // does not mean EventThread has finished initializing, so this
863 // is not a correct fix.
864 ALOGW("WARNING: EventThread not started, ignoring hotplug");
865 return;
866 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700867
Jesse Hall9e663de2013-08-16 14:28:37 -0700868 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700869 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800870 if (connected) {
871 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700872 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800873 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
874 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700875 }
876 setTransactionFlags(eDisplayTransactionNeeded);
877
Andy McFadden9e9689c2012-10-10 18:17:51 -0700878 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700879 }
Mathias Agopian86303202012-07-24 22:46:10 -0700880}
881
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700882void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700883 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700884 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700885}
886
Mathias Agopian4fec8732012-06-29 14:12:52 -0700887void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800888 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800889 switch (what) {
Dan Stoza6b9454d2014-11-07 16:00:59 -0800890 case MessageQueue::TRANSACTION: {
891 handleMessageTransaction();
892 break;
893 }
894 case MessageQueue::INVALIDATE: {
895 bool refreshNeeded = handleMessageTransaction();
896 refreshNeeded |= handleMessageInvalidate();
Dan Stoza58784442014-12-02 16:58:17 -0800897 refreshNeeded |= mRepaintEverything;
Dan Stoza6b9454d2014-11-07 16:00:59 -0800898 if (refreshNeeded) {
Dan Stoza58784442014-12-02 16:58:17 -0800899 // Signal a refresh if a transaction modified the window state,
900 // a new buffer was latched, or if HWC has requested a full
901 // repaint
Dan Stoza6b9454d2014-11-07 16:00:59 -0800902 signalRefresh();
903 }
904 break;
905 }
906 case MessageQueue::REFRESH: {
907 handleMessageRefresh();
908 break;
909 }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800910 }
911}
912
Dan Stoza6b9454d2014-11-07 16:00:59 -0800913bool SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700914 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700915 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700916 handleTransaction(transactionFlags);
Dan Stoza6b9454d2014-11-07 16:00:59 -0800917 return true;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700918 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800919 return false;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700920}
921
Dan Stoza6b9454d2014-11-07 16:00:59 -0800922bool SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700923 ATRACE_CALL();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800924 return handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700925}
926
927void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700928 ATRACE_CALL();
Dan Stoza14cd37c2015-07-09 12:43:33 -0700929
930 static nsecs_t previousExpectedPresent = 0;
931 nsecs_t expectedPresent = mPrimaryDispSync.computeNextRefresh(0);
932 static bool previousFrameMissed = false;
933 bool frameMissed = (expectedPresent == previousExpectedPresent);
934 if (frameMissed != previousFrameMissed) {
935 ATRACE_INT("FrameMissed", static_cast<int>(frameMissed));
936 }
937 previousFrameMissed = frameMissed;
938
939 if (CC_UNLIKELY(mDropMissedFrames && frameMissed)) {
940 // Latch buffers, but don't send anything to HWC, then signal another
941 // wakeup for the next vsync
942 preComposition();
943 repaintEverything();
944 } else {
945 preComposition();
946 rebuildLayerStacks();
947 setUpHWComposer();
948 doDebugFlashRegions();
949 doComposition();
950 postComposition();
951 }
952
953 previousExpectedPresent = mPrimaryDispSync.computeNextRefresh(0);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700954}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700955
Mathias Agopiancd60f992012-08-16 16:28:27 -0700956void SurfaceFlinger::doDebugFlashRegions()
957{
958 // is debugging enabled
959 if (CC_LIKELY(!mDebugRegion))
960 return;
961
962 const bool repaintEverything = mRepaintEverything;
963 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
964 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700965 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700966 // transform the dirty region into this screen's coordinate space
967 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
968 if (!dirtyRegion.isEmpty()) {
969 // redraw the whole screen
970 doComposeSurfaces(hw, Region(hw->bounds()));
971
972 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700973 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700974 RenderEngine& engine(getRenderEngine());
975 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
976
Mathias Agopiancd60f992012-08-16 16:28:27 -0700977 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700978 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700979 }
980 }
981 }
982
983 postFramebuffer();
984
985 if (mDebugRegion > 1) {
986 usleep(mDebugRegion * 1000);
987 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700988
989 HWComposer& hwc(getHwComposer());
990 if (hwc.initCheck() == NO_ERROR) {
991 status_t err = hwc.prepare();
992 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
993 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700994}
995
996void SurfaceFlinger::preComposition()
997{
998 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700999 const LayerVector& layers(mDrawingState.layersSortedByZ);
1000 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -07001001 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001002 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001003 needExtraInvalidate = true;
1004 }
1005 }
1006 if (needExtraInvalidate) {
1007 signalLayerUpdate();
1008 }
1009}
1010
1011void SurfaceFlinger::postComposition()
1012{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001013 const LayerVector& layers(mDrawingState.layersSortedByZ);
1014 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -07001015 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001016 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -07001017 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001018
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001019 const HWComposer& hwc = getHwComposer();
1020 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
1021
1022 if (presentFence->isValid()) {
1023 if (mPrimaryDispSync.addPresentFence(presentFence)) {
1024 enableHardwareVsync();
1025 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -07001026 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001027 }
1028 }
1029
Dan Stozab90cf072015-03-05 11:05:59 -08001030 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Andy McFadden5167ec62014-05-22 13:08:43 -07001031 if (kIgnorePresentFences) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001032 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07001033 enableHardwareVsync();
1034 }
1035 }
1036
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001037 if (mAnimCompositionPending) {
1038 mAnimCompositionPending = false;
1039
Jesse Halla9a1b002013-02-27 16:39:25 -08001040 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001041 mAnimFrameTracker.setActualPresentFence(presentFence);
1042 } else {
1043 // The HWC doesn't support present fences, so use the refresh
1044 // timestamp instead.
1045 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
1046 mAnimFrameTracker.setActualPresentTime(presentTime);
1047 }
1048 mAnimFrameTracker.advanceFrame();
1049 }
Dan Stozab90cf072015-03-05 11:05:59 -08001050
1051 if (hw->getPowerMode() == HWC_POWER_MODE_OFF) {
1052 return;
1053 }
1054
1055 nsecs_t currentTime = systemTime();
1056 if (mHasPoweredOff) {
1057 mHasPoweredOff = false;
1058 } else {
1059 nsecs_t period = mPrimaryDispSync.getPeriod();
1060 nsecs_t elapsedTime = currentTime - mLastSwapTime;
1061 size_t numPeriods = static_cast<size_t>(elapsedTime / period);
1062 if (numPeriods < NUM_BUCKETS - 1) {
1063 mFrameBuckets[numPeriods] += elapsedTime;
1064 } else {
1065 mFrameBuckets[NUM_BUCKETS - 1] += elapsedTime;
1066 }
1067 mTotalTime += elapsedTime;
1068 }
1069 mLastSwapTime = currentTime;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001070}
1071
1072void SurfaceFlinger::rebuildLayerStacks() {
1073 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001074 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001075 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -07001076 mVisibleRegionsDirty = false;
1077 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001078
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001079 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001080 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001081 Region opaqueRegion;
1082 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -08001083 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -07001084 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001085 const Transform& tr(hw->getTransform());
1086 const Rect bounds(hw->getBounds());
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001087 if (hw->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001088 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001089 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001090
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001091 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001092 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001093 const sp<Layer>& layer(layers[i]);
1094 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001095 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -07001096 Region drawRegion(tr.transform(
1097 layer->visibleNonTransparentRegion));
1098 drawRegion.andSelf(bounds);
1099 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001100 layersSortedByZ.add(layer);
1101 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001102 }
1103 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001104 }
Mathias Agopian42977342012-08-05 00:40:46 -07001105 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001106 hw->undefinedRegion.set(bounds);
1107 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
1108 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001109 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001110 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001111}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001112
Mathias Agopiancd60f992012-08-16 16:28:27 -07001113void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001114 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001115 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1116 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1117 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1118
1119 // If nothing has changed (!dirty), don't recompose.
1120 // If something changed, but we don't currently have any visible layers,
1121 // and didn't when we last did a composition, then skip it this time.
1122 // The second rule does two things:
1123 // - When all layers are removed from a display, we'll emit one black
1124 // frame, then nothing more until we get new layers.
1125 // - When a display is created with a private layer stack, we won't
1126 // emit any black frames until a layer is added to the layer stack.
1127 bool mustRecompose = dirty && !(empty && wasEmpty);
1128
1129 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1130 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1131 mustRecompose ? "doing" : "skipping",
1132 dirty ? "+" : "-",
1133 empty ? "+" : "-",
1134 wasEmpty ? "+" : "-");
1135
Dan Stoza71433162014-02-04 16:22:36 -08001136 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001137
1138 if (mustRecompose) {
1139 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1140 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001141 }
1142
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001143 HWComposer& hwc(getHwComposer());
1144 if (hwc.initCheck() == NO_ERROR) {
1145 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001146 if (CC_UNLIKELY(mHwWorkListDirty)) {
1147 mHwWorkListDirty = false;
1148 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1149 sp<const DisplayDevice> hw(mDisplays[dpy]);
1150 const int32_t id = hw->getHwcDisplayId();
1151 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001152 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001153 hw->getVisibleLayersSortedByZ());
1154 const size_t count = currentLayers.size();
1155 if (hwc.createWorkList(id, count) == NO_ERROR) {
1156 HWComposer::LayerListIterator cur = hwc.begin(id);
1157 const HWComposer::LayerListIterator end = hwc.end(id);
1158 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001159 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001160 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001161 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001162 cur->setSkip(true);
1163 }
1164 }
1165 }
1166 }
1167 }
1168 }
1169
1170 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001171 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001172 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001173 const int32_t id = hw->getHwcDisplayId();
1174 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001175 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001176 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001177 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001178 HWComposer::LayerListIterator cur = hwc.begin(id);
1179 const HWComposer::LayerListIterator end = hwc.end(id);
1180 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1181 /*
1182 * update the per-frame h/w composer data for each layer
1183 * and build the transparent region of the FB
1184 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001185 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001186 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -07001187 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001188 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001189 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001190
Riley Andrews03414a12014-07-01 14:22:59 -07001191 // If possible, attempt to use the cursor overlay on each display.
1192 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1193 sp<const DisplayDevice> hw(mDisplays[dpy]);
1194 const int32_t id = hw->getHwcDisplayId();
1195 if (id >= 0) {
1196 const Vector< sp<Layer> >& currentLayers(
1197 hw->getVisibleLayersSortedByZ());
1198 const size_t count = currentLayers.size();
1199 HWComposer::LayerListIterator cur = hwc.begin(id);
1200 const HWComposer::LayerListIterator end = hwc.end(id);
1201 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1202 const sp<Layer>& layer(currentLayers[i]);
1203 if (layer->isPotentialCursor()) {
1204 cur->setIsCursorLayerHint();
1205 break;
1206 }
1207 }
1208 }
1209 }
1210
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001211 status_t err = hwc.prepare();
1212 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001213
1214 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1215 sp<const DisplayDevice> hw(mDisplays[dpy]);
1216 hw->prepareFrame(hwc);
1217 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001218 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001219}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001220
Mathias Agopiancd60f992012-08-16 16:28:27 -07001221void SurfaceFlinger::doComposition() {
1222 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001223 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001224 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001225 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001226 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001227 // transform the dirty region into this screen's coordinate space
1228 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001229
1230 // repaint the framebuffer (if needed)
1231 doDisplayComposition(hw, dirtyRegion);
1232
Mathias Agopiancd60f992012-08-16 16:28:27 -07001233 hw->dirtyRegion.clear();
1234 hw->flip(hw->swapRegion);
1235 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001236 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001237 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001238 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001239 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001240 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001241}
1242
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001243void SurfaceFlinger::postFramebuffer()
1244{
Mathias Agopian841cde52012-03-01 15:44:37 -08001245 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001246
Mathias Agopiana44b0412011-10-16 18:46:35 -07001247 const nsecs_t now = systemTime();
1248 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001249
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001250 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001251 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001252 if (!hwc.supportsFramebufferTarget()) {
1253 // EGL spec says:
1254 // "surface must be bound to the calling thread's current context,
1255 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001256 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001257 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001258 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001259 }
1260
Mathias Agopian6da15f42013-09-25 20:40:07 -07001261 // make the default display current because the VirtualDisplayDevice code cannot
1262 // deal with dequeueBuffer() being called outside of the composition loop; however
1263 // the code below can call glFlush() which is allowed (and does in some case) call
1264 // dequeueBuffer().
1265 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1266
Mathias Agopian92a979a2012-08-02 18:32:23 -07001267 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001268 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001269 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001270 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001271 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001272 int32_t id = hw->getHwcDisplayId();
1273 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001274 HWComposer::LayerListIterator cur = hwc.begin(id);
1275 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001276 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001277 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001278 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001279 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001280 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001281 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001282 }
Jesse Hallef194142012-06-14 14:45:17 -07001283 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001284 }
1285
Mathias Agopiana44b0412011-10-16 18:46:35 -07001286 mLastSwapBufferTime = systemTime() - now;
1287 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001288
1289 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1290 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1291 logFrameStats();
1292 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001293}
1294
Mathias Agopian87baae12012-07-31 12:38:26 -07001295void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001296{
Mathias Agopian841cde52012-03-01 15:44:37 -08001297 ATRACE_CALL();
1298
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001299 // here we keep a copy of the drawing state (that is the state that's
1300 // going to be overwritten by handleTransactionLocked()) outside of
1301 // mStateLock so that the side-effects of the State assignment
1302 // don't happen with mStateLock held (which can cause deadlocks).
1303 State drawingState(mDrawingState);
1304
Mathias Agopianca4d3602011-05-19 15:38:14 -07001305 Mutex::Autolock _l(mStateLock);
1306 const nsecs_t now = systemTime();
1307 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308
Mathias Agopianca4d3602011-05-19 15:38:14 -07001309 // Here we're guaranteed that some transaction flags are set
1310 // so we can call handleTransactionLocked() unconditionally.
1311 // We call getTransactionFlags(), which will also clear the flags,
1312 // with mStateLock held to guarantee that mCurrentState won't change
1313 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001314
Mathias Agopiane57f2922012-08-09 16:29:12 -07001315 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001316 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001317
Mathias Agopianca4d3602011-05-19 15:38:14 -07001318 mLastTransactionTime = systemTime() - now;
1319 mDebugInTransaction = 0;
1320 invalidateHwcGeometry();
1321 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001322}
1323
Mathias Agopian87baae12012-07-31 12:38:26 -07001324void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001325{
1326 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001327 const size_t count = currentLayers.size();
1328
Dan Stoza7dde5992015-05-22 09:51:44 -07001329 // Notify all layers of available frames
1330 for (size_t i = 0; i < count; ++i) {
1331 currentLayers[i]->notifyAvailableFrames();
1332 }
1333
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001334 /*
1335 * Traversal of the children
1336 * (perform the transaction for each of them if needed)
1337 */
1338
Mathias Agopian3559b072012-08-15 13:46:03 -07001339 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001340 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001341 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001342 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1343 if (!trFlags) continue;
1344
1345 const uint32_t flags = layer->doTransaction(0);
1346 if (flags & Layer::eVisibleRegion)
1347 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348 }
1349 }
1350
1351 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001352 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001353 */
1354
Mathias Agopiane57f2922012-08-09 16:29:12 -07001355 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001356 // here we take advantage of Vector's copy-on-write semantics to
1357 // improve performance by skipping the transaction entirely when
1358 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001359 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1360 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001361 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001363 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001364 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001365
1366 // find the displays that were removed
1367 // (ie: in drawing state but not in current state)
1368 // also handle displays that changed
1369 // (ie: displays that are in both lists)
1370 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001371 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1372 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001373 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001374 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001375 // Call makeCurrent() on the primary display so we can
1376 // be sure that nothing associated with this display
1377 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001378 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001379 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001380 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1381 if (hw != NULL)
1382 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001383 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001384 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001385 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001386 } else {
1387 ALOGW("trying to remove the main display");
1388 }
1389 } else {
1390 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001391 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001392 const wp<IBinder>& display(curr.keyAt(j));
Marco Nelissen097ca272014-11-14 08:01:01 -08001393 const sp<IBinder> state_binder = IInterface::asBinder(state.surface);
1394 const sp<IBinder> draw_binder = IInterface::asBinder(draw[i].surface);
Dan Albert1474f882014-09-08 18:59:09 -07001395 if (state_binder != draw_binder) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001396 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001397 // recreating the DisplayDevice, so we just remove it
1398 // from the drawing state, so that it get re-added
1399 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001400 sp<DisplayDevice> hw(getDisplayDevice(display));
1401 if (hw != NULL)
1402 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001403 mDisplays.removeItem(display);
1404 mDrawingState.displays.removeItemsAt(i);
1405 dc--; i--;
1406 // at this point we must loop to the next item
1407 continue;
1408 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001409
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001410 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001411 if (disp != NULL) {
1412 if (state.layerStack != draw[i].layerStack) {
1413 disp->setLayerStack(state.layerStack);
1414 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001415 if ((state.orientation != draw[i].orientation)
1416 || (state.viewport != draw[i].viewport)
1417 || (state.frame != draw[i].frame))
1418 {
1419 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001420 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001421 }
Michael Lentine47e45402014-07-18 15:34:25 -07001422 if (state.width != draw[i].width || state.height != draw[i].height) {
1423 disp->setDisplaySize(state.width, state.height);
1424 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001425 }
1426 }
1427 }
1428
1429 // find displays that were added
1430 // (ie: in current state but not in drawing state)
1431 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001432 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001433 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001434
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001435 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001436 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001437 sp<IGraphicBufferProducer> bqProducer;
1438 sp<IGraphicBufferConsumer> bqConsumer;
1439 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1440 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001441
Jesse Hall02d86562013-03-25 14:43:23 -07001442 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001443 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001444 // Virtual displays without a surface are dormant:
1445 // they have external state (layer stack, projection,
1446 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001447 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001448
Dan Stoza1f3efb12014-10-15 16:34:55 -07001449 int width = 0;
1450 int status = state.surface->query(
1451 NATIVE_WINDOW_WIDTH, &width);
1452 ALOGE_IF(status != NO_ERROR,
1453 "Unable to query width (%d)", status);
1454 int height = 0;
1455 status = state.surface->query(
1456 NATIVE_WINDOW_HEIGHT, &height);
1457 ALOGE_IF(status != NO_ERROR,
1458 "Unable to query height (%d)", status);
1459 if (MAX_VIRTUAL_DISPLAY_DIMENSION == 0 ||
1460 (width <= MAX_VIRTUAL_DISPLAY_DIMENSION &&
1461 height <= MAX_VIRTUAL_DISPLAY_DIMENSION)) {
1462 hwcDisplayId = allocateHwcDisplayId(state.type);
1463 }
1464
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001465 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
Dan Stozab9b08832014-03-13 11:55:57 -07001466 *mHwc, hwcDisplayId, state.surface,
1467 bqProducer, bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001468
1469 dispSurface = vds;
Michael Lentine47e45402014-07-18 15:34:25 -07001470 producer = vds;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001471 }
1472 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001473 ALOGE_IF(state.surface!=NULL,
1474 "adding a supported display, but rendering "
1475 "surface is provided (%p), ignoring it",
1476 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001477 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001478 // for supported (by hwc) displays we provide our
1479 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001480 dispSurface = new FramebufferSurface(*mHwc, state.type,
1481 bqConsumer);
1482 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001483 }
1484
1485 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001486 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001487 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001488 state.type, hwcDisplayId,
1489 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001490 display, dispSurface, producer,
1491 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001492 hw->setLayerStack(state.layerStack);
1493 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001494 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001495 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001496 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001497 if (state.isVirtualDisplay()) {
1498 if (hwcDisplayId >= 0) {
1499 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1500 hw->getWidth(), hw->getHeight(),
1501 hw->getFormat());
1502 }
1503 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001504 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001505 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001506 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001507 }
1508 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001509 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001510 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001511
Mathias Agopian84300952012-11-21 16:02:13 -08001512 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1513 // The transform hint might have changed for some layers
1514 // (either because a display has changed, or because a layer
1515 // as changed).
1516 //
1517 // Walk through all the layers in currentLayers,
1518 // and update their transform hint.
1519 //
1520 // If a layer is visible only on a single display, then that
1521 // display is used to calculate the hint, otherwise we use the
1522 // default display.
1523 //
1524 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1525 // the hint is set before we acquire a buffer from the surface texture.
1526 //
1527 // NOTE: layer transactions have taken place already, so we use their
1528 // drawing state. However, SurfaceFlinger's own transaction has not
1529 // happened yet, so we must use the current state layer list
1530 // (soon to become the drawing state list).
1531 //
1532 sp<const DisplayDevice> disp;
1533 uint32_t currentlayerStack = 0;
1534 for (size_t i=0; i<count; i++) {
1535 // NOTE: we rely on the fact that layers are sorted by
1536 // layerStack first (so we don't have to traverse the list
1537 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001538 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001539 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001540 if (i==0 || currentlayerStack != layerStack) {
1541 currentlayerStack = layerStack;
1542 // figure out if this layerstack is mirrored
1543 // (more than one display) if so, pick the default display,
1544 // if not, pick the only display it's on.
1545 disp.clear();
1546 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1547 sp<const DisplayDevice> hw(mDisplays[dpy]);
1548 if (hw->getLayerStack() == currentlayerStack) {
1549 if (disp == NULL) {
1550 disp = hw;
1551 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001552 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001553 break;
1554 }
1555 }
1556 }
1557 }
Chet Haase91d25932013-04-11 15:24:55 -07001558 if (disp == NULL) {
1559 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1560 // redraw after transform hint changes. See bug 8508397.
1561
1562 // could be null when this layer is using a layerStack
1563 // that is not visible on any display. Also can occur at
1564 // screen off/on times.
1565 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001566 }
Chet Haase91d25932013-04-11 15:24:55 -07001567 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001568 }
1569 }
1570
1571
Mathias Agopian3559b072012-08-15 13:46:03 -07001572 /*
1573 * Perform our own transaction if needed
1574 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001575
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001576 const LayerVector& layers(mDrawingState.layersSortedByZ);
1577 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001578 // layers have been added
1579 mVisibleRegionsDirty = true;
1580 }
1581
1582 // some layers might have been removed, so
1583 // we need to update the regions they're exposing.
1584 if (mLayersRemoved) {
1585 mLayersRemoved = false;
1586 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001587 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001588 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001589 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001590 if (currentLayers.indexOf(layer) < 0) {
1591 // this layer is not visible anymore
1592 // TODO: we could traverse the tree from front to back and
1593 // compute the actual visible region
1594 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001595 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001596 Region visibleReg = s.transform.transform(
1597 Region(Rect(s.active.w, s.active.h)));
1598 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001599 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001600 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001601 }
1602
1603 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001604
1605 updateCursorAsync();
1606}
1607
1608void SurfaceFlinger::updateCursorAsync()
1609{
1610 HWComposer& hwc(getHwComposer());
1611 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1612 sp<const DisplayDevice> hw(mDisplays[dpy]);
1613 const int32_t id = hw->getHwcDisplayId();
1614 if (id < 0) {
1615 continue;
1616 }
1617 const Vector< sp<Layer> >& currentLayers(
1618 hw->getVisibleLayersSortedByZ());
1619 const size_t count = currentLayers.size();
1620 HWComposer::LayerListIterator cur = hwc.begin(id);
1621 const HWComposer::LayerListIterator end = hwc.end(id);
1622 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1623 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1624 continue;
1625 }
1626 const sp<Layer>& layer(currentLayers[i]);
1627 Rect cursorPos = layer->getPosition(hw);
1628 hwc.setCursorPositionAsync(id, cursorPos);
1629 break;
1630 }
1631 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001632}
1633
1634void SurfaceFlinger::commitTransaction()
1635{
1636 if (!mLayersPendingRemoval.isEmpty()) {
1637 // Notify removed layers now that they can't be drawn from
1638 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1639 mLayersPendingRemoval[i]->onRemoved();
1640 }
1641 mLayersPendingRemoval.clear();
1642 }
1643
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001644 // If this transaction is part of a window animation then the next frame
1645 // we composite should be considered an animation as well.
1646 mAnimCompositionPending = mAnimTransactionPending;
1647
Mathias Agopian4fec8732012-06-29 14:12:52 -07001648 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001649 mTransactionPending = false;
1650 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001651 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652}
1653
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001655 const LayerVector& currentLayers, uint32_t layerStack,
1656 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001657{
Mathias Agopian841cde52012-03-01 15:44:37 -08001658 ATRACE_CALL();
1659
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001660 Region aboveOpaqueLayers;
1661 Region aboveCoveredLayers;
1662 Region dirty;
1663
Mathias Agopian87baae12012-07-31 12:38:26 -07001664 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001665
1666 size_t i = currentLayers.size();
1667 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001668 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001669
1670 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001671 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001672
Jesse Hall01e29052013-02-19 16:13:35 -08001673 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001674 if (s.layerStack != layerStack)
1675 continue;
1676
Mathias Agopianab028732010-03-16 16:41:46 -07001677 /*
1678 * opaqueRegion: area of a surface that is fully opaque.
1679 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001680 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001681
1682 /*
1683 * visibleRegion: area of a surface that is visible on screen
1684 * and not fully transparent. This is essentially the layer's
1685 * footprint minus the opaque regions above it.
1686 * Areas covered by a translucent surface are considered visible.
1687 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001688 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001689
1690 /*
1691 * coveredRegion: area of a surface that is covered by all
1692 * visible regions above it (which includes the translucent areas).
1693 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001694 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001695
Jesse Halla8026d22012-09-25 13:25:04 -07001696 /*
1697 * transparentRegion: area of a surface that is hinted to be completely
1698 * transparent. This is only used to tell when the layer has no visible
1699 * non-transparent regions and can be removed from the layer list. It
1700 * does not affect the visibleRegion of this layer or any layers
1701 * beneath it. The hint may not be correct if apps don't respect the
1702 * SurfaceView restrictions (which, sadly, some don't).
1703 */
1704 Region transparentRegion;
1705
Mathias Agopianab028732010-03-16 16:41:46 -07001706
1707 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001708 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001709 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001710 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001711 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001712 if (!visibleRegion.isEmpty()) {
1713 // Remove the transparent area from the visible region
1714 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001715 const Transform tr(s.transform);
1716 if (tr.transformed()) {
1717 if (tr.preserveRects()) {
1718 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001719 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001720 } else {
1721 // transformation too complex, can't do the
1722 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001723 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001724 }
1725 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001726 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001727 }
Mathias Agopianab028732010-03-16 16:41:46 -07001728 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001729
Mathias Agopianab028732010-03-16 16:41:46 -07001730 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001731 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001732 if (s.alpha==255 && !translucent &&
1733 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1734 // the opaque region is the layer's footprint
1735 opaqueRegion = visibleRegion;
1736 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001737 }
1738 }
1739
Mathias Agopianab028732010-03-16 16:41:46 -07001740 // Clip the covered region to the visible region
1741 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1742
1743 // Update aboveCoveredLayers for next (lower) layer
1744 aboveCoveredLayers.orSelf(visibleRegion);
1745
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001746 // subtract the opaque region covered by the layers above us
1747 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001748
1749 // compute this layer's dirty region
1750 if (layer->contentDirty) {
1751 // we need to invalidate the whole region
1752 dirty = visibleRegion;
1753 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001754 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001755 layer->contentDirty = false;
1756 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001757 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001758 * the exposed region consists of two components:
1759 * 1) what's VISIBLE now and was COVERED before
1760 * 2) what's EXPOSED now less what was EXPOSED before
1761 *
1762 * note that (1) is conservative, we start with the whole
1763 * visible region but only keep what used to be covered by
1764 * something -- which mean it may have been exposed.
1765 *
1766 * (2) handles areas that were not covered by anything but got
1767 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001768 */
Mathias Agopianab028732010-03-16 16:41:46 -07001769 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001770 const Region oldVisibleRegion = layer->visibleRegion;
1771 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001772 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1773 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001774 }
1775 dirty.subtractSelf(aboveOpaqueLayers);
1776
1777 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001778 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001779
Mathias Agopianab028732010-03-16 16:41:46 -07001780 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001781 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001782
Jesse Halla8026d22012-09-25 13:25:04 -07001783 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001784 layer->setVisibleRegion(visibleRegion);
1785 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001786 layer->setVisibleNonTransparentRegion(
1787 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001788 }
1789
Mathias Agopian87baae12012-07-31 12:38:26 -07001790 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001791}
1792
Mathias Agopian87baae12012-07-31 12:38:26 -07001793void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1794 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001795 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001796 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1797 if (hw->getLayerStack() == layerStack) {
1798 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001799 }
1800 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001801}
1802
Dan Stoza6b9454d2014-11-07 16:00:59 -08001803bool SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001804{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001805 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001806
Mathias Agopian4fec8732012-06-29 14:12:52 -07001807 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001808 const LayerVector& layers(mDrawingState.layersSortedByZ);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001809 bool frameQueued = false;
Eric Penner51c59cd2014-07-28 19:51:58 -07001810
1811 // Store the set of layers that need updates. This set must not change as
1812 // buffers are being latched, as this could result in a deadlock.
1813 // Example: Two producers share the same command stream and:
1814 // 1.) Layer 0 is latched
1815 // 2.) Layer 0 gets a new frame
1816 // 2.) Layer 1 gets a new frame
1817 // 3.) Layer 1 is latched.
1818 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1819 // second frame. But layer 0's second frame could be waiting on display.
1820 Vector<Layer*> layersWithQueuedFrames;
1821 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001822 const sp<Layer>& layer(layers[i]);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001823 if (layer->hasQueuedFrame()) {
1824 frameQueued = true;
1825 if (layer->shouldPresentNow(mPrimaryDispSync)) {
1826 layersWithQueuedFrames.push_back(layer.get());
Dan Stozaee44edd2015-03-23 15:50:23 -07001827 } else {
1828 layer->useEmptyDamage();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001829 }
Dan Stozaee44edd2015-03-23 15:50:23 -07001830 } else {
1831 layer->useEmptyDamage();
Dan Stoza6b9454d2014-11-07 16:00:59 -08001832 }
Eric Penner51c59cd2014-07-28 19:51:58 -07001833 }
1834 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
1835 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07001836 const Region dirty(layer->latchBuffer(visibleRegions));
Dan Stozaee44edd2015-03-23 15:50:23 -07001837 layer->useSurfaceDamage();
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001838 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001839 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001840 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001841
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001842 mVisibleRegionsDirty |= visibleRegions;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001843
1844 // If we will need to wake up at some time in the future to deal with a
1845 // queued frame that shouldn't be displayed during this vsync period, wake
1846 // up during the next vsync period to check again.
1847 if (frameQueued && layersWithQueuedFrames.empty()) {
1848 signalLayerUpdate();
1849 }
1850
1851 // Only continue with the refresh if there is actually new work to do
1852 return !layersWithQueuedFrames.empty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001853}
1854
Mathias Agopianad456f92011-01-13 17:53:01 -08001855void SurfaceFlinger::invalidateHwcGeometry()
1856{
1857 mHwWorkListDirty = true;
1858}
1859
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001860
Mathias Agopiancd60f992012-08-16 16:28:27 -07001861void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001862 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001863{
Dan Stoza71433162014-02-04 16:22:36 -08001864 // We only need to actually compose the display if:
1865 // 1) It is being handled by hardware composer, which may need this to
1866 // keep its virtual display state machine in sync, or
1867 // 2) There is work to be done (the dirty region isn't empty)
1868 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1869 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1870 return;
1871 }
1872
Mathias Agopian87baae12012-07-31 12:38:26 -07001873 Region dirtyRegion(inDirtyRegion);
1874
Mathias Agopianb8a55602009-06-26 19:06:36 -07001875 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001876 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001877
Mathias Agopian42977342012-08-05 00:40:46 -07001878 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001879 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001880 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1881 // takes a rectangle, we must make sure to update that whole
1882 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001883 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001884 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001885 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001886 // We need to redraw the rectangle that will be updated
1887 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001888 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001889 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001890 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001891 } else {
1892 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001893 dirtyRegion.set(hw->bounds());
1894 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001895 }
1896 }
1897
Alan Viverette9c5a3332013-09-12 20:04:35 -07001898 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Michael Lentine3f121fc2014-10-01 11:17:28 -07001899 if (!doComposeSurfaces(hw, dirtyRegion)) return;
Mathias Agopianff2ed702013-09-01 21:36:12 -07001900 } else {
1901 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001902 mat4 colorMatrix = mColorMatrix;
1903 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001904 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001905 }
Dan Stozaf0087992014-10-20 15:46:09 -07001906 mat4 oldMatrix = engine.setupColorTransform(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001907 doComposeSurfaces(hw, dirtyRegion);
Dan Stozaf0087992014-10-20 15:46:09 -07001908 engine.setupColorTransform(oldMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001909 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001910
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001911 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001912 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001913
1914 // swap buffers (presentation)
1915 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001916}
1917
Michael Lentine3f121fc2014-10-01 11:17:28 -07001918bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001919{
Mathias Agopian3f844832013-08-07 21:24:32 -07001920 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001921 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001922 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001923 HWComposer::LayerListIterator cur = hwc.begin(id);
1924 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001925
Jesse Halld05a17f2013-09-30 16:49:30 -07001926 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001927 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001928 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001929 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1930 hw->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07001931 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1932 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
1933 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
1934 }
1935 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08001936 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001937
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001938 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001939 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001940 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001941 // when using overlays, we assume a fully transparent framebuffer
1942 // NOTE: we could reduce how much we need to clear, for instance
1943 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001944 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001945 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001946 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001947 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001948 // we start with the whole screen area
1949 const Region bounds(hw->getBounds());
1950
1951 // we remove the scissor part
1952 // we're left with the letterbox region
1953 // (common case is that letterbox ends-up being empty)
1954 const Region letterbox(bounds.subtract(hw->getScissor()));
1955
1956 // compute the area to clear
1957 Region region(hw->undefinedRegion.merge(letterbox));
1958
1959 // but limit it to the dirty region
1960 region.andSelf(dirty);
1961
Mathias Agopianb9494d52012-04-18 02:28:45 -07001962 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001963 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001964 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001965 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001966 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001967 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001968
Mathias Agopian766dc492012-10-30 18:08:06 -07001969 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1970 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001971 // scissor on the main display. It should never be needed
1972 // anyways (though in theory it could since the API allows it).
1973 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001974 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001975 if (scissor != bounds) {
1976 // scissor doesn't match the screen's dimensions, so we
1977 // need to clear everything outside of it and enable
1978 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001979
Mathias Agopianf45c5102012-10-24 16:29:17 -07001980 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001981 const uint32_t height = hw->getHeight();
1982 engine.setScissor(scissor.left, height - scissor.bottom,
1983 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001984 }
1985 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001986 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001987
Mathias Agopian85d751c2012-08-29 16:59:24 -07001988 /*
1989 * and then, render the layers targeted at the framebuffer
1990 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001991
Mathias Agopian13127d82013-03-05 17:47:11 -08001992 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001993 const size_t count = layers.size();
1994 const Transform& tr = hw->getTransform();
1995 if (cur != end) {
1996 // we're using h/w composer
1997 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001998 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001999 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07002000 if (!clip.isEmpty()) {
2001 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07002002 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07002003 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07002004 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002005 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
2006 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08002007 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07002008 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07002009 // never clear the very first layer since we're
2010 // guaranteed the FB is already cleared
2011 layer->clearWithOpenGL(hw, clip);
2012 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002013 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07002014 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002015 case HWC_FRAMEBUFFER: {
2016 layer->draw(hw, clip);
2017 break;
2018 }
Mathias Agopianda27af92012-09-13 18:17:13 -07002019 case HWC_FRAMEBUFFER_TARGET: {
2020 // this should not happen as the iterator shouldn't
2021 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08002022 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07002023 break;
2024 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07002025 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002026 }
2027 layer->setAcquireFence(hw, *cur);
2028 }
2029 } else {
2030 // we're not using h/w composer
2031 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002032 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002033 const Region clip(dirty.intersect(
2034 tr.transform(layer->visibleRegion)));
2035 if (!clip.isEmpty()) {
2036 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07002037 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002038 }
2039 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002040
2041 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002042 engine.disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07002043 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002044}
2045
Mathias Agopian3f844832013-08-07 21:24:32 -07002046void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07002047 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07002048 RenderEngine& engine(getRenderEngine());
2049 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002050}
2051
Dan Stoza7d89d062015-04-30 13:29:25 -07002052status_t SurfaceFlinger::addClientLayer(const sp<Client>& client,
Mathias Agopianac9fa422013-02-11 16:40:36 -08002053 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07002054 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08002055 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07002056{
Dan Stoza7d89d062015-04-30 13:29:25 -07002057 // add this layer to the current state list
2058 {
2059 Mutex::Autolock _l(mStateLock);
2060 if (mCurrentState.layersSortedByZ.size() >= MAX_LAYERS) {
2061 return NO_MEMORY;
2062 }
2063 mCurrentState.layersSortedByZ.add(lbc);
2064 mGraphicBufferProducerList.add(IInterface::asBinder(gbc));
2065 }
2066
Mathias Agopian96f08192010-06-02 23:28:45 -07002067 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08002068 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07002069
Dan Stoza7d89d062015-04-30 13:29:25 -07002070 return NO_ERROR;
Mathias Agopian96f08192010-06-02 23:28:45 -07002071}
2072
Mathias Agopian3f844832013-08-07 21:24:32 -07002073status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002074 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08002075 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002076 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07002077 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002078 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07002079 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002080 return NO_ERROR;
2081 }
Mathias Agopian3d579642009-06-04 18:46:21 -07002082 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002083}
2084
Dan Stozac7014012014-02-14 15:03:43 -08002085uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07002086 return android_atomic_release_load(&mTransactionFlags);
2087}
2088
Mathias Agopian3f844832013-08-07 21:24:32 -07002089uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002090 return android_atomic_and(~flags, &mTransactionFlags) & flags;
2091}
2092
Mathias Agopian3f844832013-08-07 21:24:32 -07002093uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002094 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
2095 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002096 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002097 }
2098 return old;
2099}
2100
Mathias Agopian8b33f032012-07-24 20:43:54 -07002101void SurfaceFlinger::setTransactionState(
2102 const Vector<ComposerState>& state,
2103 const Vector<DisplayState>& displays,
2104 uint32_t flags)
2105{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002106 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07002107 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07002108 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002109
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002110 if (flags & eAnimation) {
2111 // For window updates that are part of an animation we must wait for
2112 // previous animation "frames" to be handled.
2113 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002114 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002115 if (CC_UNLIKELY(err != NO_ERROR)) {
2116 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002117 // caller after a few seconds.
2118 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
2119 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002120 mAnimTransactionPending = false;
2121 break;
2122 }
2123 }
2124 }
2125
Mathias Agopiane57f2922012-08-09 16:29:12 -07002126 size_t count = displays.size();
2127 for (size_t i=0 ; i<count ; i++) {
2128 const DisplayState& s(displays[i]);
2129 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002130 }
2131
Mathias Agopiane57f2922012-08-09 16:29:12 -07002132 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002133 for (size_t i=0 ; i<count ; i++) {
2134 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002135 // Here we need to check that the interface we're given is indeed
2136 // one of our own. A malicious client could give us a NULL
2137 // IInterface, or one of its own or even one of our own but a
2138 // different type. All these situations would cause us to crash.
2139 //
2140 // NOTE: it would be better to use RTTI as we could directly check
2141 // that we have a Client*. however, RTTI is disabled in Android.
2142 if (s.client != NULL) {
Marco Nelissen097ca272014-11-14 08:01:01 -08002143 sp<IBinder> binder = IInterface::asBinder(s.client);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002144 if (binder != NULL) {
2145 String16 desc(binder->getInterfaceDescriptor());
2146 if (desc == ISurfaceComposerClient::descriptor) {
2147 sp<Client> client( static_cast<Client *>(s.client.get()) );
2148 transactionFlags |= setClientStateLocked(client, s.state);
2149 }
2150 }
2151 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002152 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002153
Mathias Agopian386aa982011-11-07 21:58:03 -08002154 if (transactionFlags) {
2155 // this triggers the transaction
2156 setTransactionFlags(transactionFlags);
2157
2158 // if this is a synchronous transaction, wait for it to take effect
2159 // before returning.
2160 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002161 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002162 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002163 if (flags & eAnimation) {
2164 mAnimTransactionPending = true;
2165 }
2166 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002167 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2168 if (CC_UNLIKELY(err != NO_ERROR)) {
2169 // just in case something goes wrong in SF, return to the
2170 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002171 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2172 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002173 break;
2174 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002175 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002176 }
2177}
2178
Mathias Agopiane57f2922012-08-09 16:29:12 -07002179uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2180{
Jesse Hall9a143922012-10-04 16:29:19 -07002181 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2182 if (dpyIdx < 0)
2183 return 0;
2184
Mathias Agopiane57f2922012-08-09 16:29:12 -07002185 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002186 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002187 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002188 const uint32_t what = s.what;
2189 if (what & DisplayState::eSurfaceChanged) {
Marco Nelissen097ca272014-11-14 08:01:01 -08002190 if (IInterface::asBinder(disp.surface) != IInterface::asBinder(s.surface)) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002191 disp.surface = s.surface;
2192 flags |= eDisplayTransactionNeeded;
2193 }
2194 }
2195 if (what & DisplayState::eLayerStackChanged) {
2196 if (disp.layerStack != s.layerStack) {
2197 disp.layerStack = s.layerStack;
2198 flags |= eDisplayTransactionNeeded;
2199 }
2200 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002201 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002202 if (disp.orientation != s.orientation) {
2203 disp.orientation = s.orientation;
2204 flags |= eDisplayTransactionNeeded;
2205 }
2206 if (disp.frame != s.frame) {
2207 disp.frame = s.frame;
2208 flags |= eDisplayTransactionNeeded;
2209 }
2210 if (disp.viewport != s.viewport) {
2211 disp.viewport = s.viewport;
2212 flags |= eDisplayTransactionNeeded;
2213 }
2214 }
Michael Lentine47e45402014-07-18 15:34:25 -07002215 if (what & DisplayState::eDisplaySizeChanged) {
2216 if (disp.width != s.width) {
2217 disp.width = s.width;
2218 flags |= eDisplayTransactionNeeded;
2219 }
2220 if (disp.height != s.height) {
2221 disp.height = s.height;
2222 flags |= eDisplayTransactionNeeded;
2223 }
2224 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002225 }
2226 return flags;
2227}
2228
2229uint32_t SurfaceFlinger::setClientStateLocked(
2230 const sp<Client>& client,
2231 const layer_state_t& s)
2232{
2233 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002234 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002235 if (layer != 0) {
2236 const uint32_t what = s.what;
2237 if (what & layer_state_t::ePositionChanged) {
2238 if (layer->setPosition(s.x, s.y))
2239 flags |= eTraversalNeeded;
2240 }
2241 if (what & layer_state_t::eLayerChanged) {
2242 // NOTE: index needs to be calculated before we update the state
2243 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2244 if (layer->setLayer(s.z)) {
2245 mCurrentState.layersSortedByZ.removeAt(idx);
2246 mCurrentState.layersSortedByZ.add(layer);
2247 // we need traversal (state changed)
2248 // AND transaction (list changed)
2249 flags |= eTransactionNeeded|eTraversalNeeded;
2250 }
2251 }
2252 if (what & layer_state_t::eSizeChanged) {
2253 if (layer->setSize(s.w, s.h)) {
2254 flags |= eTraversalNeeded;
2255 }
2256 }
2257 if (what & layer_state_t::eAlphaChanged) {
2258 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2259 flags |= eTraversalNeeded;
2260 }
2261 if (what & layer_state_t::eMatrixChanged) {
2262 if (layer->setMatrix(s.matrix))
2263 flags |= eTraversalNeeded;
2264 }
2265 if (what & layer_state_t::eTransparentRegionChanged) {
2266 if (layer->setTransparentRegionHint(s.transparentRegion))
2267 flags |= eTraversalNeeded;
2268 }
Dan Stoza23116082015-06-18 14:58:39 -07002269 if (what & layer_state_t::eFlagsChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002270 if (layer->setFlags(s.flags, s.mask))
2271 flags |= eTraversalNeeded;
2272 }
2273 if (what & layer_state_t::eCropChanged) {
2274 if (layer->setCrop(s.crop))
2275 flags |= eTraversalNeeded;
2276 }
2277 if (what & layer_state_t::eLayerStackChanged) {
2278 // NOTE: index needs to be calculated before we update the state
2279 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2280 if (layer->setLayerStack(s.layerStack)) {
2281 mCurrentState.layersSortedByZ.removeAt(idx);
2282 mCurrentState.layersSortedByZ.add(layer);
2283 // we need traversal (state changed)
2284 // AND transaction (list changed)
2285 flags |= eTransactionNeeded|eTraversalNeeded;
2286 }
2287 }
Dan Stoza7dde5992015-05-22 09:51:44 -07002288 if (what & layer_state_t::eDeferTransaction) {
2289 layer->deferTransactionUntil(s.handle, s.frameNumber);
2290 // We don't trigger a traversal here because if no other state is
2291 // changed, we don't want this to cause any more work
2292 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002293 }
2294 return flags;
2295}
2296
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002297status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002298 const String8& name,
2299 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002300 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2301 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002302{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002303 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002304 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002305 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002306 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002307 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002308 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002309
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002310 status_t result = NO_ERROR;
2311
2312 sp<Layer> layer;
2313
Mathias Agopian3165cc22012-08-08 19:42:09 -07002314 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2315 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002316 result = createNormalLayer(client,
2317 name, w, h, flags, format,
2318 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002319 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002320 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002321 result = createDimLayer(client,
2322 name, w, h, flags,
2323 handle, gbp, &layer);
2324 break;
2325 default:
2326 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002327 break;
2328 }
2329
Dan Stoza7d89d062015-04-30 13:29:25 -07002330 if (result != NO_ERROR) {
2331 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002332 }
Dan Stoza7d89d062015-04-30 13:29:25 -07002333
2334 result = addClientLayer(client, *handle, *gbp, layer);
2335 if (result != NO_ERROR) {
2336 return result;
2337 }
2338
2339 setTransactionFlags(eTransactionNeeded);
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002340 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002341}
2342
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002343status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2344 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2345 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002346{
2347 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002348 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002349 case PIXEL_FORMAT_TRANSPARENT:
2350 case PIXEL_FORMAT_TRANSLUCENT:
2351 format = PIXEL_FORMAT_RGBA_8888;
2352 break;
2353 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002354 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002355 break;
2356 }
2357
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002358 *outLayer = new Layer(this, client, name, w, h, flags);
2359 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2360 if (err == NO_ERROR) {
2361 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002362 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002363 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002364
2365 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2366 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002367}
2368
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002369status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2370 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2371 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002372{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002373 *outLayer = new LayerDim(this, client, name, w, h, flags);
2374 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002375 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002376 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002377}
2378
Mathias Agopianac9fa422013-02-11 16:40:36 -08002379status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002380{
Mathias Agopian67106042013-03-14 19:18:13 -07002381 // called by the window manager when it wants to remove a Layer
2382 status_t err = NO_ERROR;
2383 sp<Layer> l(client->getLayerUser(handle));
2384 if (l != NULL) {
2385 err = removeLayer(l);
2386 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2387 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002388 }
2389 return err;
2390}
2391
Mathias Agopian13127d82013-03-05 17:47:11 -08002392status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002393{
Mathias Agopian67106042013-03-14 19:18:13 -07002394 // called by ~LayerCleaner() when all references to the IBinder (handle)
2395 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002396 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002397 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002398 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002399 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002400 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002401 "error removing layer=%p (%s)", l.get(), strerror(-err));
2402 }
2403 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002404}
2405
Mathias Agopianb60314a2012-04-10 22:09:54 -07002406// ---------------------------------------------------------------------------
2407
Andy McFadden13a082e2012-08-24 10:16:42 -07002408void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002409 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002410 Vector<ComposerState> state;
2411 Vector<DisplayState> displays;
2412 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002413 d.what = DisplayState::eDisplayProjectionChanged |
2414 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002415 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002416 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002417 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002418 d.frame.makeInvalid();
2419 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002420 d.width = 0;
2421 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002422 displays.add(d);
2423 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002424 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002425
2426 const nsecs_t period =
2427 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2428 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002429}
2430
2431void SurfaceFlinger::initializeDisplays() {
2432 class MessageScreenInitialized : public MessageBase {
2433 SurfaceFlinger* flinger;
2434 public:
2435 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2436 virtual bool handler() {
2437 flinger->onInitializeDisplays();
2438 return true;
2439 }
2440 };
2441 sp<MessageBase> msg = new MessageScreenInitialized(this);
2442 postMessageAsync(msg); // we may be called from main thread, use async message
2443}
2444
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002445void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2446 int mode) {
2447 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2448 this);
2449 int32_t type = hw->getDisplayType();
2450 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002451
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002452 if (mode == currentMode) {
2453 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002454 return;
2455 }
2456
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002457 hw->setPowerMode(mode);
2458 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2459 ALOGW("Trying to set power mode for virtual display");
2460 return;
2461 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002462
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002463 if (currentMode == HWC_POWER_MODE_OFF) {
2464 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002465 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2466 // FIXME: eventthread only knows about the main display right now
2467 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002468 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002469 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002470
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002471 mVisibleRegionsDirty = true;
Dan Stozab90cf072015-03-05 11:05:59 -08002472 mHasPoweredOff = true;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002473 repaintEverything();
2474 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002475 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002476 disableHardwareVsync(true); // also cancels any in-progress resync
2477
Mathias Agopiancde87a32012-09-13 14:09:01 -07002478 // FIXME: eventthread only knows about the main display right now
2479 mEventThread->onScreenReleased();
2480 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002481
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002482 getHwComposer().setPowerMode(type, mode);
2483 mVisibleRegionsDirty = true;
2484 // from this point on, SF will stop drawing on this display
2485 } else {
2486 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002487 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002488}
2489
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002490void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2491 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002492 SurfaceFlinger& mFlinger;
2493 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002494 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002495 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002496 MessageSetPowerMode(SurfaceFlinger& flinger,
2497 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2498 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002499 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002500 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002501 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002502 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002503 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002504 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002505 ALOGW("Attempt to set power mode = %d for virtual display",
2506 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002507 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002508 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002509 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002510 return true;
2511 }
2512 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002513 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002514 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002515}
2516
2517// ---------------------------------------------------------------------------
2518
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002519status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2520{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002521 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002522
Mathias Agopianbd115332013-04-18 16:41:04 -07002523 IPCThreadState* ipc = IPCThreadState::self();
2524 const int pid = ipc->getCallingPid();
2525 const int uid = ipc->getCallingUid();
2526 if ((uid != AID_SHELL) &&
2527 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002528 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002529 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002530 } else {
Jesse Hallfcd15b42014-12-23 13:57:23 -08002531 // Try to get the main lock, but give up after one second
Mathias Agopian9795c422009-08-26 16:36:26 -07002532 // (this would indicate SF is stuck, but we want to be able to
2533 // print something in dumpsys).
Jesse Hallfcd15b42014-12-23 13:57:23 -08002534 status_t err = mStateLock.timedLock(s2ns(1));
2535 bool locked = (err == NO_ERROR);
Mathias Agopian9795c422009-08-26 16:36:26 -07002536 if (!locked) {
Jesse Hallfcd15b42014-12-23 13:57:23 -08002537 result.appendFormat(
2538 "SurfaceFlinger appears to be unresponsive (%s [%d]), "
2539 "dumping anyways (no locks held)\n", strerror(-err), err);
Mathias Agopian9795c422009-08-26 16:36:26 -07002540 }
2541
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002542 bool dumpAll = true;
2543 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002544 size_t numArgs = args.size();
2545 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002546 if ((index < numArgs) &&
2547 (args[index] == String16("--list"))) {
2548 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002549 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002550 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002551 }
2552
2553 if ((index < numArgs) &&
2554 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002555 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002556 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002557 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002558 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002559
2560 if ((index < numArgs) &&
2561 (args[index] == String16("--latency-clear"))) {
2562 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002563 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002564 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002565 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002566
2567 if ((index < numArgs) &&
2568 (args[index] == String16("--dispsync"))) {
2569 index++;
2570 mPrimaryDispSync.dump(result);
2571 dumpAll = false;
2572 }
Dan Stozab90cf072015-03-05 11:05:59 -08002573
2574 if ((index < numArgs) &&
2575 (args[index] == String16("--static-screen"))) {
2576 index++;
2577 dumpStaticScreenStats(result);
2578 dumpAll = false;
2579 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002580 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002581
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002582 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002583 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002584 }
2585
Mathias Agopian9795c422009-08-26 16:36:26 -07002586 if (locked) {
2587 mStateLock.unlock();
2588 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002589 }
2590 write(fd, result.string(), result.size());
2591 return NO_ERROR;
2592}
2593
Dan Stozac7014012014-02-14 15:03:43 -08002594void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2595 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002596{
2597 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2598 const size_t count = currentLayers.size();
2599 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002600 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002601 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002602 }
2603}
2604
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002605void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002606 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002607{
2608 String8 name;
2609 if (index < args.size()) {
2610 name = String8(args[index]);
2611 index++;
2612 }
2613
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002614 const nsecs_t period =
2615 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002616 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002617
2618 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002619 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002620 } else {
2621 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2622 const size_t count = currentLayers.size();
2623 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002624 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002625 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002626 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002627 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002628 }
2629 }
2630}
2631
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002632void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002633 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002634{
2635 String8 name;
2636 if (index < args.size()) {
2637 name = String8(args[index]);
2638 index++;
2639 }
2640
2641 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2642 const size_t count = currentLayers.size();
2643 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002644 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002645 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002646 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002647 }
2648 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002649
Svetoslavd85084b2014-03-20 10:28:31 -07002650 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002651}
2652
Jamie Gennis6547ff42013-07-16 20:12:42 -07002653// This should only be called from the main thread. Otherwise it would need
2654// the lock and should use mCurrentState rather than mDrawingState.
2655void SurfaceFlinger::logFrameStats() {
2656 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2657 const size_t count = drawingLayers.size();
2658 for (size_t i=0 ; i<count ; i++) {
2659 const sp<Layer>& layer(drawingLayers[i]);
2660 layer->logFrameStats();
2661 }
2662
2663 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2664}
2665
Andy McFadden4803b742012-09-24 19:07:20 -07002666/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2667{
2668 static const char* config =
2669 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002670#ifdef HAS_CONTEXT_PRIORITY
2671 " HAS_CONTEXT_PRIORITY"
2672#endif
2673#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2674 " NEVER_DEFAULT_TO_ASYNC_MODE"
2675#endif
2676#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2677 " TARGET_DISABLE_TRIPLE_BUFFERING"
2678#endif
2679 "]";
2680 result.append(config);
2681}
2682
Dan Stozab90cf072015-03-05 11:05:59 -08002683void SurfaceFlinger::dumpStaticScreenStats(String8& result) const
2684{
2685 result.appendFormat("Static screen stats:\n");
2686 for (size_t b = 0; b < NUM_BUCKETS - 1; ++b) {
2687 float bucketTimeSec = mFrameBuckets[b] / 1e9;
2688 float percent = 100.0f *
2689 static_cast<float>(mFrameBuckets[b]) / mTotalTime;
2690 result.appendFormat(" < %zd frames: %.3f s (%.1f%%)\n",
2691 b + 1, bucketTimeSec, percent);
2692 }
2693 float bucketTimeSec = mFrameBuckets[NUM_BUCKETS - 1] / 1e9;
2694 float percent = 100.0f *
2695 static_cast<float>(mFrameBuckets[NUM_BUCKETS - 1]) / mTotalTime;
2696 result.appendFormat(" %zd+ frames: %.3f s (%.1f%%)\n",
2697 NUM_BUCKETS - 1, bucketTimeSec, percent);
2698}
2699
Mathias Agopian74d211a2013-04-22 16:55:35 +02002700void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2701 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002702{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002703 bool colorize = false;
2704 if (index < args.size()
2705 && (args[index] == String16("--color"))) {
2706 colorize = true;
2707 index++;
2708 }
2709
2710 Colorizer colorizer(colorize);
2711
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002712 // figure out if we're stuck somewhere
2713 const nsecs_t now = systemTime();
2714 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2715 const nsecs_t inTransaction(mDebugInTransaction);
2716 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2717 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2718
2719 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002720 * Dump library configuration.
2721 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002722
2723 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002724 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002725 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002726 appendSfConfigString(result);
2727 appendUiConfigString(result);
2728 appendGuiConfigString(result);
2729 result.append("\n");
2730
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002731 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002732 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002733 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002734 result.append(SyncFeatures::getInstance().toString());
2735 result.append("\n");
2736
Andy McFadden41d67d72014-04-25 16:58:34 -07002737 colorizer.bold(result);
2738 result.append("DispSync configuration: ");
2739 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002740 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2741 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002742 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2743 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2744 result.append("\n");
2745
Dan Stozab90cf072015-03-05 11:05:59 -08002746 // Dump static screen stats
2747 result.append("\n");
2748 dumpStaticScreenStats(result);
2749 result.append("\n");
2750
Andy McFadden4803b742012-09-24 19:07:20 -07002751 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002752 * Dump the visible layer list
2753 */
2754 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2755 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002756 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002757 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002758 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002759 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002760 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002761 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002762 }
2763
2764 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002765 * Dump Display state
2766 */
2767
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002768 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002769 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002770 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002771 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2772 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002773 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002774 }
2775
2776 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002777 * Dump SurfaceFlinger global state
2778 */
2779
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002780 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002781 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002782 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002783
Mathias Agopian888c8222012-08-04 21:10:38 -07002784 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002785 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002786
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002787 colorizer.bold(result);
2788 result.appendFormat("EGL implementation : %s\n",
2789 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2790 colorizer.reset(result);
2791 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002792 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002793
Mathias Agopian875d8e12013-06-07 15:35:48 -07002794 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002795
Mathias Agopian42977342012-08-05 00:40:46 -07002796 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002797 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2798 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002799 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002800 " last eglSwapBuffers() time: %f us\n"
2801 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002802 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002803 " refresh-rate : %f fps\n"
2804 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002805 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002806 " gpu_to_cpu_unsupported : %d\n"
2807 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002808 mLastSwapBufferTime/1000.0,
2809 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002810 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002811 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2812 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002813 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002814 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002815
Mathias Agopian74d211a2013-04-22 16:55:35 +02002816 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002817 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002818
Mathias Agopian74d211a2013-04-22 16:55:35 +02002819 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002820 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002821
2822 /*
2823 * VSYNC state
2824 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002825 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002826
2827 /*
2828 * Dump HWComposer state
2829 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002830 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002831 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002832 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002833 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002834 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002835 (mDebugDisableHWC || mDebugRegion || mDaltonize
2836 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002837 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002838
2839 /*
2840 * Dump gralloc state
2841 */
2842 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2843 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002844}
2845
Mathias Agopian13127d82013-03-05 17:47:11 -08002846const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002847SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002848 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002849 wp<IBinder> dpy;
2850 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2851 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2852 dpy = mDisplays.keyAt(i);
2853 break;
2854 }
2855 }
2856 if (dpy == NULL) {
2857 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2858 // Just use the primary display so we have something to return
2859 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2860 }
2861 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002862}
2863
Keun young Park63f165f2012-08-31 10:53:36 -07002864bool SurfaceFlinger::startDdmConnection()
2865{
2866 void* libddmconnection_dso =
2867 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2868 if (!libddmconnection_dso) {
2869 return false;
2870 }
2871 void (*DdmConnection_start)(const char* name);
2872 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07002873 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07002874 if (!DdmConnection_start) {
2875 dlclose(libddmconnection_dso);
2876 return false;
2877 }
2878 (*DdmConnection_start)(getServiceName());
2879 return true;
2880}
2881
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002882status_t SurfaceFlinger::onTransact(
2883 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2884{
2885 switch (code) {
2886 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002887 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002888 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002889 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002890 case CLEAR_ANIMATION_FRAME_STATS:
2891 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002892 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002893 {
2894 // codes that require permission check
2895 IPCThreadState* ipc = IPCThreadState::self();
2896 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002897 const int uid = ipc->getCallingUid();
Jeff Brown3bfe51d2015-04-10 20:20:13 -07002898 if ((uid != AID_GRAPHICS && uid != AID_SYSTEM) &&
Mathias Agopian99b49842011-06-27 16:05:52 -07002899 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002900 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002901 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2902 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002903 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002904 break;
2905 }
2906 case CAPTURE_SCREEN:
2907 {
2908 // codes that require permission check
2909 IPCThreadState* ipc = IPCThreadState::self();
2910 const int pid = ipc->getCallingPid();
2911 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002912 if ((uid != AID_GRAPHICS) &&
2913 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002914 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002915 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2916 return PERMISSION_DENIED;
2917 }
2918 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002919 }
2920 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002921
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002922 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2923 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002924 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002925 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002926 IPCThreadState* ipc = IPCThreadState::self();
2927 const int pid = ipc->getCallingPid();
2928 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002929 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002930 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002931 return PERMISSION_DENIED;
2932 }
2933 int n;
2934 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002935 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002936 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002937 return NO_ERROR;
2938 case 1002: // SHOW_UPDATES
2939 n = data.readInt32();
2940 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002941 invalidateHwcGeometry();
2942 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002943 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002944 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002945 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002946 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002947 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002948 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002949 setTransactionFlags(
2950 eTransactionNeeded|
2951 eDisplayTransactionNeeded|
2952 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002953 return NO_ERROR;
2954 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002955 case 1006:{ // send empty update
2956 signalRefresh();
2957 return NO_ERROR;
2958 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002959 case 1008: // toggle use of hw composer
2960 n = data.readInt32();
2961 mDebugDisableHWC = n ? 1 : 0;
2962 invalidateHwcGeometry();
2963 repaintEverything();
2964 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002965 case 1009: // toggle use of transform hint
2966 n = data.readInt32();
2967 mDebugDisableTransformHint = n ? 1 : 0;
2968 invalidateHwcGeometry();
2969 repaintEverything();
2970 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002971 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002972 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002973 reply->writeInt32(0);
2974 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002975 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002976 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002977 return NO_ERROR;
2978 case 1013: {
2979 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002980 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2981 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002982 return NO_ERROR;
2983 }
2984 case 1014: {
2985 // daltonize
2986 n = data.readInt32();
2987 switch (n % 10) {
2988 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2989 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2990 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2991 }
2992 if (n >= 10) {
2993 mDaltonizer.setMode(Daltonizer::correction);
2994 } else {
2995 mDaltonizer.setMode(Daltonizer::simulation);
2996 }
2997 mDaltonize = n > 0;
2998 invalidateHwcGeometry();
2999 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07003000 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003001 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003002 case 1015: {
3003 // apply a color matrix
3004 n = data.readInt32();
3005 mHasColorMatrix = n ? 1 : 0;
3006 if (n) {
3007 // color matrix is sent as mat3 matrix followed by vec3
3008 // offset, then packed into a mat4 where the last row is
3009 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07003010 for (size_t i = 0 ; i < 4; i++) {
3011 for (size_t j = 0; j < 4; j++) {
3012 mColorMatrix[i][j] = data.readFloat();
3013 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003014 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003015 } else {
3016 mColorMatrix = mat4();
3017 }
3018 invalidateHwcGeometry();
3019 repaintEverything();
3020 return NO_ERROR;
3021 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003022 // This is an experimental interface
3023 // Needs to be shifted to proper binder interface when we productize
3024 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07003025 n = data.readInt32();
3026 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003027 return NO_ERROR;
3028 }
Dan Stozaee44edd2015-03-23 15:50:23 -07003029 case 1017: {
3030 n = data.readInt32();
3031 mForceFullDamage = static_cast<bool>(n);
3032 return NO_ERROR;
3033 }
Dan Stozadb4ac3c2015-04-14 11:34:01 -07003034 case 1018: { // Modify Choreographer's phase offset
3035 n = data.readInt32();
3036 mEventThread->setPhaseOffset(static_cast<nsecs_t>(n));
3037 return NO_ERROR;
3038 }
3039 case 1019: { // Modify SurfaceFlinger's phase offset
3040 n = data.readInt32();
3041 mSFEventThread->setPhaseOffset(static_cast<nsecs_t>(n));
3042 return NO_ERROR;
3043 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003044 }
3045 }
3046 return err;
3047}
3048
Mathias Agopian53331da2011-08-22 21:44:41 -07003049void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07003050 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08003051 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07003052}
3053
Mathias Agopian59119e62010-10-11 12:37:43 -07003054// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003055// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07003056// ---------------------------------------------------------------------------
3057
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003058/* The code below is here to handle b/8734824
3059 *
3060 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07003061 * from the surfaceflinger thread to the calling binder thread, where they
3062 * are executed. This allows the calling thread in the calling process to be
3063 * reused and not depend on having "enough" binder threads to handle the
3064 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003065 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003066class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07003067 /* Parts of GraphicProducerWrapper are run on two different threads,
3068 * communicating by sending messages via Looper but also by shared member
3069 * data. Coherence maintenance is subtle and in places implicit (ugh).
3070 *
3071 * Don't rely on Looper's sendMessage/handleMessage providing
3072 * release/acquire semantics for any data not actually in the Message.
3073 * Data going from surfaceflinger to binder threads needs to be
3074 * synchronized explicitly.
3075 *
3076 * Barrier open/wait do provide release/acquire semantics. This provides
3077 * implicit synchronization for data coming back from binder to
3078 * surfaceflinger threads.
3079 */
3080
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003081 sp<IGraphicBufferProducer> impl;
3082 sp<Looper> looper;
3083 status_t result;
3084 bool exitPending;
3085 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07003086 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003087 uint32_t code;
3088 Parcel const* data;
3089 Parcel* reply;
3090
3091 enum {
3092 MSG_API_CALL,
3093 MSG_EXIT
3094 };
3095
3096 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003097 * Called on surfaceflinger thread. This is called by our "fake"
3098 * BpGraphicBufferProducer. We package the data and reply Parcel and
3099 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003100 */
3101 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08003102 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003103 this->code = code;
3104 this->data = &data;
3105 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003106 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07003107 // if we've exited, we run the message synchronously right here.
3108 // note (JH): as far as I can tell from looking at the code, this
3109 // never actually happens. if it does, i'm not sure if it happens
3110 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003111 handleMessage(Message(MSG_API_CALL));
3112 } else {
3113 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07003114 // Prevent stores to this->{code, data, reply} from being
3115 // reordered later than the construction of Message.
3116 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003117 looper->sendMessage(this, Message(MSG_API_CALL));
3118 barrier.wait();
3119 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08003120 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003121 }
3122
3123 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003124 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003125 * call the real BpGraphicBufferProducer.
3126 */
3127 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07003128 int what = message.what;
3129 // Prevent reads below from happening before the read from Message
3130 atomic_thread_fence(memory_order_acquire);
3131 if (what == MSG_API_CALL) {
Marco Nelissen097ca272014-11-14 08:01:01 -08003132 result = IInterface::asBinder(impl)->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003133 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07003134 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003135 exitRequested = true;
3136 }
3137 }
3138
3139public:
Jesse Hallb154c422014-07-13 12:47:02 -07003140 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
3141 : impl(impl),
3142 looper(new Looper(true)),
Pablo Ceballos53390e12015-08-04 11:25:59 -07003143 result(NO_ERROR),
Jesse Hallb154c422014-07-13 12:47:02 -07003144 exitPending(false),
Pablo Ceballos53390e12015-08-04 11:25:59 -07003145 exitRequested(false),
3146 code(0),
3147 data(NULL),
3148 reply(NULL)
Jesse Hallb154c422014-07-13 12:47:02 -07003149 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003150
Jesse Hallb154c422014-07-13 12:47:02 -07003151 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003152 status_t waitForResponse() {
3153 do {
3154 looper->pollOnce(-1);
3155 } while (!exitRequested);
3156 return result;
3157 }
3158
Jesse Hallb154c422014-07-13 12:47:02 -07003159 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003160 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07003161 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003162 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07003163 // Ensure this->result is visible to the binder thread before it
3164 // handles the message.
3165 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003166 looper->sendMessage(this, Message(MSG_EXIT));
3167 }
3168};
3169
3170
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003171status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
3172 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003173 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003174 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003175 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003176
3177 if (CC_UNLIKELY(display == 0))
3178 return BAD_VALUE;
3179
3180 if (CC_UNLIKELY(producer == 0))
3181 return BAD_VALUE;
3182
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003183 // if we have secure windows on this display, never allow the screen capture
3184 // unless the producer interface is local (i.e.: we can take a screenshot for
3185 // ourselves).
Marco Nelissen097ca272014-11-14 08:01:01 -08003186 if (!IInterface::asBinder(producer)->localBinder()) {
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003187 Mutex::Autolock _l(mStateLock);
3188 sp<const DisplayDevice> hw(getDisplayDevice(display));
3189 if (hw->getSecureLayerVisible()) {
3190 ALOGW("FB is protected: PERMISSION_DENIED");
3191 return PERMISSION_DENIED;
3192 }
3193 }
3194
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003195 // Convert to surfaceflinger's internal rotation type.
3196 Transform::orientation_flags rotationFlags;
3197 switch (rotation) {
3198 case ISurfaceComposer::eRotateNone:
3199 rotationFlags = Transform::ROT_0;
3200 break;
3201 case ISurfaceComposer::eRotate90:
3202 rotationFlags = Transform::ROT_90;
3203 break;
3204 case ISurfaceComposer::eRotate180:
3205 rotationFlags = Transform::ROT_180;
3206 break;
3207 case ISurfaceComposer::eRotate270:
3208 rotationFlags = Transform::ROT_270;
3209 break;
3210 default:
3211 rotationFlags = Transform::ROT_0;
3212 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3213 break;
3214 }
3215
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003216 class MessageCaptureScreen : public MessageBase {
3217 SurfaceFlinger* flinger;
3218 sp<IBinder> display;
3219 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003220 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003221 uint32_t reqWidth, reqHeight;
3222 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003223 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003224 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003225 status_t result;
3226 public:
3227 MessageCaptureScreen(SurfaceFlinger* flinger,
3228 const sp<IBinder>& display,
3229 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003230 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003231 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003232 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003233 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003234 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003235 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003236 useIdentityTransform(useIdentityTransform),
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003237 rotation(rotation),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003238 result(PERMISSION_DENIED)
3239 {
3240 }
3241 status_t getResult() const {
3242 return result;
3243 }
3244 virtual bool handler() {
3245 Mutex::Autolock _l(flinger->mStateLock);
3246 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003247 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003248 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003249 useIdentityTransform, rotation);
Marco Nelissen097ca272014-11-14 08:01:01 -08003250 static_cast<GraphicProducerWrapper*>(IInterface::asBinder(producer).get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003251 return true;
3252 }
3253 };
3254
Mathias Agopian9eb1f052013-04-10 16:27:17 -07003255 // make sure to process transactions before screenshots -- a transaction
3256 // might already be pending but scheduled for VSYNC; this guarantees we
3257 // will handle it before the screenshot. When VSYNC finally arrives
3258 // the scheduled transaction will be a no-op. If no transactions are
3259 // scheduled at this time, this will end-up being a no-op as well.
3260 mEventQueue.invalidateTransactionNow();
3261
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003262 // this creates a "fake" BBinder which will serve as a "fake" remote
3263 // binder to receive the marshaled calls and forward them to the
3264 // real remote (a BpGraphicBufferProducer)
3265 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3266
3267 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3268 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003269 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003270 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003271 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003272 useIdentityTransform, rotationFlags);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003273
3274 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003275 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003276 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003277 }
3278 return res;
3279}
3280
Mathias Agopian180f10d2013-04-10 22:55:41 -07003281
3282void SurfaceFlinger::renderScreenImplLocked(
3283 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003284 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003285 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003286 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003287{
3288 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003289 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003290
3291 // get screen geometry
Andreas Gampe89fd4f72014-11-13 14:18:56 -08003292 const int32_t hw_w = hw->getWidth();
3293 const int32_t hw_h = hw->getHeight();
3294 const bool filtering = static_cast<int32_t>(reqWidth) != hw_w ||
Christopher Ferris0e749792015-03-23 14:32:15 -07003295 static_cast<int32_t>(reqHeight) != hw_h;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003296
Dan Stozac1879002014-05-22 15:59:05 -07003297 // if a default or invalid sourceCrop is passed in, set reasonable values
3298 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3299 !sourceCrop.isValid()) {
3300 sourceCrop.setLeftTop(Point(0, 0));
3301 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3302 }
3303
3304 // ensure that sourceCrop is inside screen
3305 if (sourceCrop.left < 0) {
3306 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3307 }
Dan Stozabe31f442014-06-11 11:20:54 -07003308 if (sourceCrop.right > hw_w) {
3309 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003310 }
3311 if (sourceCrop.top < 0) {
3312 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3313 }
Dan Stozabe31f442014-06-11 11:20:54 -07003314 if (sourceCrop.bottom > hw_h) {
3315 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003316 }
3317
Mathias Agopian180f10d2013-04-10 22:55:41 -07003318 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003319 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003320
3321 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003322 engine.setViewportAndProjection(
3323 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003324 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003325
3326 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003327 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003328
3329 const LayerVector& layers( mDrawingState.layersSortedByZ );
3330 const size_t count = layers.size();
3331 for (size_t i=0 ; i<count ; ++i) {
3332 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003333 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003334 if (state.layerStack == hw->getLayerStack()) {
3335 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3336 if (layer->isVisible()) {
3337 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08003338 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003339 if (filtering) layer->setFiltering(false);
3340 }
3341 }
3342 }
3343 }
3344
3345 // compositionComplete is needed for older driver
3346 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003347 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003348}
3349
3350
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003351status_t SurfaceFlinger::captureScreenImplLocked(
3352 const sp<const DisplayDevice>& hw,
3353 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003354 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003355 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003356 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003357{
3358 ATRACE_CALL();
3359
Mathias Agopian180f10d2013-04-10 22:55:41 -07003360 // get screen geometry
Dan Stoza35024162015-06-09 16:44:40 -07003361 uint32_t hw_w = hw->getWidth();
3362 uint32_t hw_h = hw->getHeight();
3363
3364 if (rotation & Transform::ROT_90) {
3365 std::swap(hw_w, hw_h);
3366 }
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003367
Mathias Agopian180f10d2013-04-10 22:55:41 -07003368 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3369 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3370 reqWidth, reqHeight, hw_w, hw_h);
3371 return BAD_VALUE;
3372 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003373
Mathias Agopian180f10d2013-04-10 22:55:41 -07003374 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3375 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3376
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003377 // create a surface (because we're a producer, and we need to
3378 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003379 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003380 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003381
Michael Lentine5a16a622015-05-21 13:48:24 -07003382 status_t result = native_window_api_connect(window, NATIVE_WINDOW_API_EGL);
3383 if (result == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003384 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3385 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003386
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003387 int err = 0;
3388 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003389 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003390 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3391 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003392
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003393 if (err == NO_ERROR) {
3394 ANativeWindowBuffer* buffer;
3395 /* TODO: Once we have the sync framework everywhere this can use
3396 * server-side waits on the fence that dequeueBuffer returns.
3397 */
3398 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3399 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003400 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003401 // create an EGLImage from the buffer so we can later
3402 // turn it into a texture
3403 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3404 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3405 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003406 // this binds the given EGLImage as a framebuffer for the
3407 // duration of this scope.
3408 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3409 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003410 // this will in fact render into our dequeued buffer
3411 // via an FBO, which means we didn't have to create
3412 // an EGLSurface and therefore we're not
3413 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003414 renderScreenImplLocked(
3415 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3416 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003417
Riley Andrews86639902014-08-15 12:27:24 -07003418 // Attempt to create a sync khr object that can produce a sync point. If that
3419 // isn't available, create a non-dupable sync object in the fallback path and
3420 // wait on it directly.
3421 EGLSyncKHR sync;
3422 if (!DEBUG_SCREENSHOTS) {
3423 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Riley Andrews9707f4d2014-10-23 16:17:04 -07003424 // native fence fd will not be populated until flush() is done.
3425 getRenderEngine().flush();
Andy McFadden2d8d1202013-10-09 16:38:02 -07003426 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003427 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003428 }
Riley Andrews86639902014-08-15 12:27:24 -07003429 if (sync != EGL_NO_SYNC_KHR) {
3430 // get the sync fd
3431 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3432 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3433 ALOGW("captureScreen: failed to dup sync khr object");
3434 syncFd = -1;
3435 }
3436 eglDestroySyncKHR(mEGLDisplay, sync);
3437 } else {
3438 // fallback path
3439 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3440 if (sync != EGL_NO_SYNC_KHR) {
3441 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3442 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3443 EGLint eglErr = eglGetError();
3444 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3445 ALOGW("captureScreen: fence wait timed out");
3446 } else {
3447 ALOGW_IF(eglErr != EGL_SUCCESS,
3448 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3449 }
3450 eglDestroySyncKHR(mEGLDisplay, sync);
3451 } else {
3452 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3453 }
3454 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003455 if (DEBUG_SCREENSHOTS) {
3456 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3457 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3458 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3459 hw, minLayerZ, maxLayerZ);
3460 delete [] pixels;
3461 }
3462
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003463 } else {
3464 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3465 result = INVALID_OPERATION;
3466 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003467 // destroy our image
3468 eglDestroyImageKHR(mEGLDisplay, image);
3469 } else {
3470 result = BAD_VALUE;
3471 }
Jesse Hallafe2b1f2014-10-21 11:09:17 -07003472 // queueBuffer takes ownership of syncFd
Michael Lentine5a16a622015-05-21 13:48:24 -07003473 result = window->queueBuffer(window, buffer, syncFd);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003474 }
3475 } else {
3476 result = BAD_VALUE;
3477 }
3478 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3479 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003480
Mathias Agopian74c40c02010-09-29 13:02:36 -07003481 return result;
3482}
3483
Mathias Agopiand5556842013-09-19 17:08:37 -07003484void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3485 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003486 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003487 for (size_t y=0 ; y<h ; y++) {
3488 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3489 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003490 if (p[x] != 0xFF000000) return;
3491 }
3492 }
3493 ALOGE("*** we just took a black screenshot ***\n"
3494 "requested minz=%d, maxz=%d, layerStack=%d",
3495 minLayerZ, maxLayerZ, hw->getLayerStack());
3496 const LayerVector& layers( mDrawingState.layersSortedByZ );
3497 const size_t count = layers.size();
3498 for (size_t i=0 ; i<count ; ++i) {
3499 const sp<Layer>& layer(layers[i]);
3500 const Layer::State& state(layer->getDrawingState());
3501 const bool visible = (state.layerStack == hw->getLayerStack())
3502 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3503 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003504 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003505 visible ? '+' : '-',
3506 i, layer->getName().string(), state.layerStack, state.z,
3507 layer->isVisible(), state.flags, state.alpha);
3508 }
3509 }
3510}
3511
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003512// ---------------------------------------------------------------------------
3513
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003514SurfaceFlinger::LayerVector::LayerVector() {
3515}
3516
3517SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003518 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003519}
3520
3521int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3522 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003523{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003524 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003525 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3526 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003527
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003528 uint32_t ls = l->getCurrentState().layerStack;
3529 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003530 if (ls != rs)
3531 return ls - rs;
3532
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003533 uint32_t lz = l->getCurrentState().z;
3534 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003535 if (lz != rz)
3536 return lz - rz;
3537
3538 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003539}
3540
3541// ---------------------------------------------------------------------------
3542
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003543SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Pablo Ceballos53390e12015-08-04 11:25:59 -07003544 : type(DisplayDevice::DISPLAY_ID_INVALID),
3545 layerStack(DisplayDevice::NO_LAYER_STACK),
3546 orientation(0),
3547 width(0),
3548 height(0),
3549 isSecure(false) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003550}
3551
Pablo Ceballos53390e12015-08-04 11:25:59 -07003552SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(
3553 DisplayDevice::DisplayType type, bool isSecure)
3554 : type(type),
3555 layerStack(DisplayDevice::NO_LAYER_STACK),
3556 orientation(0),
3557 width(0),
3558 height(0),
3559 isSecure(isSecure) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003560 viewport.makeInvalid();
3561 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003562}
3563
3564// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003565
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003566}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003567
3568
3569#if defined(__gl_h_)
3570#error "don't include gl/gl.h in this file"
3571#endif
3572
3573#if defined(__gl2_h_)
3574#error "don't include gl2/gl2.h in this file"
3575#endif