blob: e767be2a5adca4f219d9b6d9d92e12b50742d68d [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Greg Hackmann86efcc02014-03-07 12:44:02 -080024#include <inttypes.h>
Jesse Hallb154c422014-07-13 12:47:02 -070025#include <stdatomic.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070026
27#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036
Mathias Agopianc666cae2012-07-25 18:56:13 -070037#include <ui/DisplayInfo.h>
Lajos Molnar67d8bd62014-09-11 14:58:45 -070038#include <ui/DisplayStatInfo.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070039
Mathias Agopian921e6ac2012-07-23 23:11:29 -070040#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070041#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070042#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070043#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080044#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080045#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070046
47#include <ui/GraphicBufferAllocator.h>
48#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070049#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080050
Mathias Agopiancde87a32012-09-13 14:09:01 -070051#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include <utils/String8.h>
53#include <utils/String16.h>
54#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080055#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopian921e6ac2012-07-23 23:11:29 -070057#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070058#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
Mathias Agopian3e25fd82013-04-22 17:52:16 +020060#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020062#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080063#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070064#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070065#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070066#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080067#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071
Mathias Agopiana4912602012-07-12 14:25:33 -070072#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070073#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070074#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075
Mathias Agopianff2ed702013-09-01 21:36:12 -070076#include "Effects/Daltonizer.h"
77
Mathias Agopian875d8e12013-06-07 15:35:48 -070078#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070079#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070080
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081#define DISPLAY_COUNT 1
82
Mathias Agopianfee2b462013-07-03 12:34:01 -070083/*
84 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
85 * black pixels.
86 */
87#define DEBUG_SCREENSHOTS false
88
Mathias Agopianca088332013-03-28 17:44:13 -070089EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
90
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070092
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070093// This is the phase offset in nanoseconds of the software vsync event
94// relative to the vsync event reported by HWComposer. The software vsync
95// event is when SurfaceFlinger and Choreographer-based applications run each
96// frame.
97//
98// This phase offset allows adjustment of the minimum latency from application
99// wake-up (by Choregographer) time to the time at which the resulting window
100// image is displayed. This value may be either positive (after the HW vsync)
101// or negative (before the HW vsync). Setting it to 0 will result in a
102// minimum latency of two vsync periods because the app and SurfaceFlinger
103// will run just after the HW vsync. Setting it to a positive number will
104// result in the minimum latency being:
105//
106// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
107//
108// Note that reducing this latency makes it more likely for the applications
109// to not have their window content image ready in time. When this happens
110// the latency will end up being an additional vsync period, and animations
111// will hiccup. Therefore, this latency should be tuned somewhat
112// conservatively (or at least with awareness of the trade-off being made).
113static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
114
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700115// This is the phase offset at which SurfaceFlinger's composition runs.
116static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
117
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118// ---------------------------------------------------------------------------
119
Mathias Agopian99b49842011-06-27 16:05:52 -0700120const String16 sHardwareTest("android.permission.HARDWARE_TEST");
121const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
122const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
123const String16 sDump("android.permission.DUMP");
124
125// ---------------------------------------------------------------------------
126
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700128 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700130 mTransactionPending(false),
131 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700132 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700133 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700134 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700137 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800138 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700140 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700141 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700142 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700143 mDebugInSwapBuffers(0),
144 mLastSwapBufferTime(0),
145 mDebugInTransaction(0),
146 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700147 mBootFinished(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700148 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700149 mHWVsyncAvailable(false),
Alan Viverette9c5a3332013-09-12 20:04:35 -0700150 mDaltonize(false),
151 mHasColorMatrix(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152{
Steve Blocka19954a2012-01-04 20:05:49 +0000153 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154
155 // debugging stuff...
156 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700157
Mathias Agopianb4b17302013-03-20 18:36:41 -0700158 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700159 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700160
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 property_get("debug.sf.showupdates", value, "0");
162 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700163
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700164 property_get("debug.sf.ddms", value, "0");
165 mDebugDDMS = atoi(value);
166 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700167 if (!startDdmConnection()) {
168 // start failed, and DDMS debugging not enabled
169 mDebugDDMS = 0;
170 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700171 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700172 ALOGI_IF(mDebugRegion, "showupdates enabled");
173 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174}
175
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800176void SurfaceFlinger::onFirstRef()
177{
178 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800179}
180
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181SurfaceFlinger::~SurfaceFlinger()
182{
Mathias Agopiana4912602012-07-12 14:25:33 -0700183 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
184 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
185 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186}
187
Dan Stozac7014012014-02-14 15:03:43 -0800188void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800189{
190 // the window manager died on us. prepare its eulogy.
191
Andy McFadden13a082e2012-08-24 10:16:42 -0700192 // restore initial conditions (default device unblank, etc)
193 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800194
195 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700196 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800197}
198
Mathias Agopian7e27f052010-05-28 14:22:23 -0700199sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200{
Mathias Agopian96f08192010-06-02 23:28:45 -0700201 sp<ISurfaceComposerClient> bclient;
202 sp<Client> client(new Client(this));
203 status_t err = client->initCheck();
204 if (err == NO_ERROR) {
205 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 return bclient;
208}
209
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700210sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
211 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700212{
213 class DisplayToken : public BBinder {
214 sp<SurfaceFlinger> flinger;
215 virtual ~DisplayToken() {
216 // no more references, this display must be terminated
217 Mutex::Autolock _l(flinger->mStateLock);
218 flinger->mCurrentState.displays.removeItem(this);
219 flinger->setTransactionFlags(eDisplayTransactionNeeded);
220 }
221 public:
222 DisplayToken(const sp<SurfaceFlinger>& flinger)
223 : flinger(flinger) {
224 }
225 };
226
227 sp<BBinder> token = new DisplayToken(this);
228
229 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700230 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700231 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700232 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700233 mCurrentState.displays.add(token, info);
234
235 return token;
236}
237
Jesse Hall6c913be2013-08-08 12:15:49 -0700238void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
239 Mutex::Autolock _l(mStateLock);
240
241 ssize_t idx = mCurrentState.displays.indexOfKey(display);
242 if (idx < 0) {
243 ALOGW("destroyDisplay: invalid display token");
244 return;
245 }
246
247 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
248 if (!info.isVirtualDisplay()) {
249 ALOGE("destroyDisplay called for non-virtual display");
250 return;
251 }
252
253 mCurrentState.displays.removeItemsAt(idx);
254 setTransactionFlags(eDisplayTransactionNeeded);
255}
256
Jesse Hall692c7232012-11-08 15:41:56 -0800257void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
258 ALOGW_IF(mBuiltinDisplays[type],
259 "Overwriting display token for display type %d", type);
260 mBuiltinDisplays[type] = new BBinder();
261 DisplayDeviceState info(type);
262 // All non-virtual displays are currently considered secure.
263 info.isSecure = true;
264 mCurrentState.displays.add(mBuiltinDisplays[type], info);
265}
266
Mathias Agopiane57f2922012-08-09 16:29:12 -0700267sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700268 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700269 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
270 return NULL;
271 }
Jesse Hall692c7232012-11-08 15:41:56 -0800272 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700273}
274
Jamie Gennis9a78c902011-01-12 18:30:40 -0800275sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
276{
277 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
278 return gba;
279}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700280
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281void SurfaceFlinger::bootFinished()
282{
283 const nsecs_t now = systemTime();
284 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000285 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700286 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700287
288 // wait patiently for the window manager death
289 const String16 name("window");
290 sp<IBinder> window(defaultServiceManager()->getService(name));
291 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700292 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700293 }
294
295 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700296 // formerly we would just kill the process, but we now ask it to exit so it
297 // can choose where to stop the animation.
298 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299}
300
Mathias Agopian3f844832013-08-07 21:24:32 -0700301void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700302 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700303 RenderEngine& engine;
304 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700305 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700306 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
307 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700308 }
309 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700310 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700311 return true;
312 }
313 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700314 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700315}
316
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700317class DispSyncSource : public VSyncSource, private DispSync::Callback {
318public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700319 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
320 const char* label) :
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700321 mValue(0),
322 mPhaseOffset(phaseOffset),
323 mTraceVsync(traceVsync),
Andy McFadden5167ec62014-05-22 13:08:43 -0700324 mVsyncOnLabel(String8::format("VsyncOn-%s", label)),
325 mVsyncEventLabel(String8::format("VSYNC-%s", label)),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700326 mDispSync(dispSync) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700327
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700328 virtual ~DispSyncSource() {}
329
330 virtual void setVSyncEnabled(bool enable) {
331 // Do NOT lock the mutex here so as to avoid any mutex ordering issues
332 // with locking it in the onDispSyncEvent callback.
333 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700334 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700335 static_cast<DispSync::Callback*>(this));
336 if (err != NO_ERROR) {
337 ALOGE("error registering vsync callback: %s (%d)",
338 strerror(-err), err);
339 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700340 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700341 } else {
342 status_t err = mDispSync->removeEventListener(
343 static_cast<DispSync::Callback*>(this));
344 if (err != NO_ERROR) {
345 ALOGE("error unregistering vsync callback: %s (%d)",
346 strerror(-err), err);
347 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700348 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700349 }
350 }
351
352 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
353 Mutex::Autolock lock(mMutex);
354 mCallback = callback;
355 }
356
357private:
358 virtual void onDispSyncEvent(nsecs_t when) {
359 sp<VSyncSource::Callback> callback;
360 {
361 Mutex::Autolock lock(mMutex);
362 callback = mCallback;
363
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700364 if (mTraceVsync) {
365 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700366 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700367 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700368 }
369
370 if (callback != NULL) {
371 callback->onVSyncEvent(when);
372 }
373 }
374
375 int mValue;
376
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700377 const nsecs_t mPhaseOffset;
378 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700379 const String8 mVsyncOnLabel;
380 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700381
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700382 DispSync* mDispSync;
383 sp<VSyncSource::Callback> mCallback;
384 Mutex mMutex;
385};
386
387void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700388 ALOGI( "SurfaceFlinger's main thread ready to run. "
389 "Initializing graphics H/W...");
390
Jesse Hall692c7232012-11-08 15:41:56 -0800391 Mutex::Autolock _l(mStateLock);
392
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700393 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700394 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
395 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700396
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700397 // Initialize the H/W composer object. There may or may not be an
398 // actual hardware composer underneath.
399 mHwc = new HWComposer(this,
400 *static_cast<HWComposer::EventHandler *>(this));
401
Mathias Agopian875d8e12013-06-07 15:35:48 -0700402 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800403 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700404
405 // retrieve the EGL context that was selected/created
406 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700407
Mathias Agopianda27af92012-09-13 18:17:13 -0700408 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
409 "couldn't create EGLContext");
410
Mathias Agopiancde87a32012-09-13 14:09:01 -0700411 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700412 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700413 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700414 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700415 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700416 // All non-virtual displays are currently considered secure.
417 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800418 createBuiltinDisplayLocked(type);
419 wp<IBinder> token = mBuiltinDisplays[i];
420
Dan Stozab9b08832014-03-13 11:55:57 -0700421 sp<IGraphicBufferProducer> producer;
422 sp<IGraphicBufferConsumer> consumer;
423 BufferQueue::createBufferQueue(&producer, &consumer,
424 new GraphicBufferAlloc());
425
426 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
427 consumer);
Jesse Hall19e87292013-12-23 21:02:15 -0800428 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700429 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800430 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Dan Stozab9b08832014-03-13 11:55:57 -0700431 fbs, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -0800432 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700433 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700434 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700435 // for displays other than the main display, so we always
436 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800437 ALOGD("marking display %zu as acquired/unblanked", i);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700438 hw->setPowerMode(HWC_POWER_MODE_NORMAL);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700439 }
440 mDisplays.add(token, hw);
441 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700442 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700443
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700444 // make the GLContext current so that we can create textures when creating Layers
445 // (which may happens before we render something)
446 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
447
Mathias Agopian028508c2012-07-25 21:12:12 -0700448 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700449 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700450 vsyncPhaseOffsetNs, true, "app");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700451 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700452 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700453 sfVsyncPhaseOffsetNs, true, "sf");
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700454 mSFEventThread = new EventThread(sfVsyncSrc);
455 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700456
Jamie Gennisd1700752013-10-14 12:22:52 -0700457 mEventControlThread = new EventControlThread(this);
458 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
459
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700460 // set a fake vsync period if there is no HWComposer
461 if (mHwc->initCheck() != NO_ERROR) {
462 mPrimaryDispSync.setPeriod(16666667);
463 }
464
Mathias Agopian92a979a2012-08-02 18:32:23 -0700465 // initialize our drawing state
466 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700467
Andy McFadden13a082e2012-08-24 10:16:42 -0700468 // set initial conditions (e.g. unblank default device)
469 initializeDisplays();
470
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700471 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700472 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800473}
474
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700475int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700476 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700477 type : mHwc->allocateDisplayId();
478}
479
Mathias Agopiana67e4182012-06-19 17:26:12 -0700480void SurfaceFlinger::startBootAnim() {
481 // start boot animation
482 property_set("service.bootanim.exit", "0");
483 property_set("ctl.start", "bootanim");
484}
485
Mathias Agopian875d8e12013-06-07 15:35:48 -0700486size_t SurfaceFlinger::getMaxTextureSize() const {
487 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700488}
489
Mathias Agopian875d8e12013-06-07 15:35:48 -0700490size_t SurfaceFlinger::getMaxViewportDims() const {
491 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700492}
493
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800494// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800495
Jamie Gennis582270d2011-08-17 18:19:00 -0700496bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800497 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800498 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800499 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700500 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800501}
502
Dan Stoza7f7da322014-05-02 15:26:25 -0700503status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
504 Vector<DisplayInfo>* configs) {
505 if (configs == NULL) {
506 return BAD_VALUE;
507 }
508
Jesse Hall692c7232012-11-08 15:41:56 -0800509 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700510 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800511 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700512 type = i;
513 break;
514 }
515 }
516
517 if (type < 0) {
518 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700519 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700520
Mathias Agopian8b736f12012-08-13 17:54:26 -0700521 // TODO: Not sure if display density should handled by SF any longer
522 class Density {
523 static int getDensityFromProperty(char const* propName) {
524 char property[PROPERTY_VALUE_MAX];
525 int density = 0;
526 if (property_get(propName, property, NULL) > 0) {
527 density = atoi(property);
528 }
529 return density;
530 }
531 public:
532 static int getEmuDensity() {
533 return getDensityFromProperty("qemu.sf.lcd_density"); }
534 static int getBuildDensity() {
535 return getDensityFromProperty("ro.sf.lcd_density"); }
536 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700537
Dan Stoza7f7da322014-05-02 15:26:25 -0700538 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700539
Dan Stoza7f7da322014-05-02 15:26:25 -0700540 const Vector<HWComposer::DisplayConfig>& hwConfigs =
541 getHwComposer().getConfigs(type);
542 for (size_t c = 0; c < hwConfigs.size(); ++c) {
543 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
544 DisplayInfo info = DisplayInfo();
545
546 float xdpi = hwConfig.xdpi;
547 float ydpi = hwConfig.ydpi;
548
549 if (type == DisplayDevice::DISPLAY_PRIMARY) {
550 // The density of the device is provided by a build property
551 float density = Density::getBuildDensity() / 160.0f;
552 if (density == 0) {
553 // the build doesn't provide a density -- this is wrong!
554 // use xdpi instead
555 ALOGE("ro.sf.lcd_density must be defined as a build property");
556 density = xdpi / 160.0f;
557 }
558 if (Density::getEmuDensity()) {
559 // if "qemu.sf.lcd_density" is specified, it overrides everything
560 xdpi = ydpi = density = Density::getEmuDensity();
561 density /= 160.0f;
562 }
563 info.density = density;
564
565 // TODO: this needs to go away (currently needed only by webkit)
566 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
567 info.orientation = hw->getOrientation();
568 } else {
569 // TODO: where should this value come from?
570 static const int TV_DENSITY = 213;
571 info.density = TV_DENSITY / 160.0f;
572 info.orientation = 0;
573 }
574
575 info.w = hwConfig.width;
576 info.h = hwConfig.height;
577 info.xdpi = xdpi;
578 info.ydpi = ydpi;
579 info.fps = float(1e9 / hwConfig.refresh);
Andy McFadden91b2ca82014-06-13 14:04:23 -0700580 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
581
582 // This is how far in advance a buffer must be queued for
583 // presentation at a given time. If you want a buffer to appear
584 // on the screen at time N, you must submit the buffer before
585 // (N - presentationDeadline).
586 //
587 // Normally it's one full refresh period (to give SF a chance to
588 // latch the buffer), but this can be reduced by configuring a
589 // DispSync offset. Any additional delays introduced by the hardware
590 // composer or panel must be accounted for here.
591 //
592 // We add an additional 1ms to allow for processing time and
593 // differences between the ideal and actual refresh rate.
594 info.presentationDeadline =
595 hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700596
597 // All non-virtual displays are currently considered secure.
598 info.secure = true;
599
600 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700601 }
602
Dan Stoza7f7da322014-05-02 15:26:25 -0700603 return NO_ERROR;
604}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700605
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800606status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& /* display */,
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700607 DisplayStatInfo* stats) {
608 if (stats == NULL) {
609 return BAD_VALUE;
610 }
611
612 // FIXME for now we always return stats for the primary display
613 memset(stats, 0, sizeof(*stats));
614 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
615 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
616 return NO_ERROR;
617}
618
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700619int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
620 return getDisplayDevice(display)->getActiveConfig();
Dan Stoza7f7da322014-05-02 15:26:25 -0700621}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700622
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700623void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
624 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
625 this);
626 int32_t type = hw->getDisplayType();
627 int currentMode = hw->getActiveConfig();
628
629 if (mode == currentMode) {
630 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
631 return;
632 }
633
634 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
635 ALOGW("Trying to set config for virtual display");
636 return;
637 }
638
639 hw->setActiveConfig(mode);
640 getHwComposer().setActiveConfig(type, mode);
641}
642
643status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
644 class MessageSetActiveConfig: public MessageBase {
645 SurfaceFlinger& mFlinger;
646 sp<IBinder> mDisplay;
647 int mMode;
648 public:
649 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
650 int mode) :
651 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
652 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700653 Vector<DisplayInfo> configs;
654 mFlinger.getDisplayConfigs(mDisplay, &configs);
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800655 if(mMode < 0 || static_cast<size_t>(mMode) >= configs.size()) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700656 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700657 mMode, configs.size());
658 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700659 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
660 if (hw == NULL) {
661 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700662 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700663 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
664 ALOGW("Attempt to set active config = %d for virtual display",
665 mMode);
666 } else {
667 mFlinger.setActiveConfigInternal(hw, mMode);
668 }
669 return true;
670 }
671 };
672 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
673 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700674 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700675}
676
Svetoslavd85084b2014-03-20 10:28:31 -0700677status_t SurfaceFlinger::clearAnimationFrameStats() {
678 Mutex::Autolock _l(mStateLock);
679 mAnimFrameTracker.clearStats();
680 return NO_ERROR;
681}
682
683status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
684 Mutex::Autolock _l(mStateLock);
685 mAnimFrameTracker.getStats(outStats);
686 return NO_ERROR;
687}
688
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800689// ----------------------------------------------------------------------------
690
691sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800692 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700693}
694
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800695// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800696
697void SurfaceFlinger::waitForEvent() {
698 mEventQueue.waitMessage();
699}
700
701void SurfaceFlinger::signalTransaction() {
702 mEventQueue.invalidate();
703}
704
705void SurfaceFlinger::signalLayerUpdate() {
706 mEventQueue.invalidate();
707}
708
709void SurfaceFlinger::signalRefresh() {
710 mEventQueue.refresh();
711}
712
713status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800714 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800715 return mEventQueue.postMessage(msg, reltime);
716}
717
718status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800719 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800720 status_t res = mEventQueue.postMessage(msg, reltime);
721 if (res == NO_ERROR) {
722 msg->wait();
723 }
724 return res;
725}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800726
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700727void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700728 do {
729 waitForEvent();
730 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800731}
732
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700733void SurfaceFlinger::enableHardwareVsync() {
734 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700735 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700736 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700737 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
738 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700739 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700740 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700741}
742
Jesse Hall948fe0c2013-10-14 12:56:09 -0700743void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700744 Mutex::Autolock _l(mHWVsyncLock);
745
Jesse Hall948fe0c2013-10-14 12:56:09 -0700746 if (makeAvailable) {
747 mHWVsyncAvailable = true;
748 } else if (!mHWVsyncAvailable) {
749 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
750 return;
751 }
752
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700753 const nsecs_t period =
754 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
755
756 mPrimaryDispSync.reset();
757 mPrimaryDispSync.setPeriod(period);
758
759 if (!mPrimaryHWVsyncEnabled) {
760 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700761 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
762 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700763 mPrimaryHWVsyncEnabled = true;
764 }
765}
766
Jesse Hall948fe0c2013-10-14 12:56:09 -0700767void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700768 Mutex::Autolock _l(mHWVsyncLock);
769 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700770 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
771 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700772 mPrimaryDispSync.endResync();
773 mPrimaryHWVsyncEnabled = false;
774 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700775 if (makeUnavailable) {
776 mHWVsyncAvailable = false;
777 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700778}
779
780void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700781 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700782
Jamie Gennisd1700752013-10-14 12:22:52 -0700783 { // Scope for the lock
784 Mutex::Autolock _l(mHWVsyncLock);
785 if (type == 0 && mPrimaryHWVsyncEnabled) {
786 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700787 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700788 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700789
790 if (needsHwVsync) {
791 enableHardwareVsync();
792 } else {
793 disableHardwareVsync(false);
794 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700795}
796
797void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
798 if (mEventThread == NULL) {
799 // This is a temporary workaround for b/7145521. A non-null pointer
800 // does not mean EventThread has finished initializing, so this
801 // is not a correct fix.
802 ALOGW("WARNING: EventThread not started, ignoring hotplug");
803 return;
804 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700805
Jesse Hall9e663de2013-08-16 14:28:37 -0700806 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700807 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800808 if (connected) {
809 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700810 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800811 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
812 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700813 }
814 setTransactionFlags(eDisplayTransactionNeeded);
815
Andy McFadden9e9689c2012-10-10 18:17:51 -0700816 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700817 }
Mathias Agopian86303202012-07-24 22:46:10 -0700818}
819
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700820void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700821 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700822 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700823}
824
Mathias Agopian4fec8732012-06-29 14:12:52 -0700825void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800826 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800827 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700828 case MessageQueue::TRANSACTION:
829 handleMessageTransaction();
830 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700831 case MessageQueue::INVALIDATE:
832 handleMessageTransaction();
833 handleMessageInvalidate();
834 signalRefresh();
835 break;
836 case MessageQueue::REFRESH:
837 handleMessageRefresh();
838 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800839 }
840}
841
Mathias Agopian4fec8732012-06-29 14:12:52 -0700842void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700843 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700844 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700845 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700846 }
847}
848
849void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700850 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700851 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700852}
853
854void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700855 ATRACE_CALL();
856 preComposition();
857 rebuildLayerStacks();
858 setUpHWComposer();
859 doDebugFlashRegions();
860 doComposition();
861 postComposition();
862}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700863
Mathias Agopiancd60f992012-08-16 16:28:27 -0700864void SurfaceFlinger::doDebugFlashRegions()
865{
866 // is debugging enabled
867 if (CC_LIKELY(!mDebugRegion))
868 return;
869
870 const bool repaintEverything = mRepaintEverything;
871 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
872 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700873 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700874 // transform the dirty region into this screen's coordinate space
875 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
876 if (!dirtyRegion.isEmpty()) {
877 // redraw the whole screen
878 doComposeSurfaces(hw, Region(hw->bounds()));
879
880 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700881 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700882 RenderEngine& engine(getRenderEngine());
883 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
884
Mathias Agopiancd60f992012-08-16 16:28:27 -0700885 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700886 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700887 }
888 }
889 }
890
891 postFramebuffer();
892
893 if (mDebugRegion > 1) {
894 usleep(mDebugRegion * 1000);
895 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700896
897 HWComposer& hwc(getHwComposer());
898 if (hwc.initCheck() == NO_ERROR) {
899 status_t err = hwc.prepare();
900 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
901 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700902}
903
904void SurfaceFlinger::preComposition()
905{
906 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700907 const LayerVector& layers(mDrawingState.layersSortedByZ);
908 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700909 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700910 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700911 needExtraInvalidate = true;
912 }
913 }
914 if (needExtraInvalidate) {
915 signalLayerUpdate();
916 }
917}
918
919void SurfaceFlinger::postComposition()
920{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700921 const LayerVector& layers(mDrawingState.layersSortedByZ);
922 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700923 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700924 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700925 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800926
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700927 const HWComposer& hwc = getHwComposer();
928 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
929
930 if (presentFence->isValid()) {
931 if (mPrimaryDispSync.addPresentFence(presentFence)) {
932 enableHardwareVsync();
933 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700934 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700935 }
936 }
937
Andy McFadden5167ec62014-05-22 13:08:43 -0700938 if (kIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700939 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700940 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700941 enableHardwareVsync();
942 }
943 }
944
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800945 if (mAnimCompositionPending) {
946 mAnimCompositionPending = false;
947
Jesse Halla9a1b002013-02-27 16:39:25 -0800948 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800949 mAnimFrameTracker.setActualPresentFence(presentFence);
950 } else {
951 // The HWC doesn't support present fences, so use the refresh
952 // timestamp instead.
953 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
954 mAnimFrameTracker.setActualPresentTime(presentTime);
955 }
956 mAnimFrameTracker.advanceFrame();
957 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700958}
959
960void SurfaceFlinger::rebuildLayerStacks() {
961 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700962 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700963 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700964 mVisibleRegionsDirty = false;
965 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700966
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700967 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700968 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700969 Region opaqueRegion;
970 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800971 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700972 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700973 const Transform& tr(hw->getTransform());
974 const Rect bounds(hw->getBounds());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700975 if (hw->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700976 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700977 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700978
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700979 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700980 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700981 const sp<Layer>& layer(layers[i]);
982 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700983 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700984 Region drawRegion(tr.transform(
985 layer->visibleNonTransparentRegion));
986 drawRegion.andSelf(bounds);
987 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700988 layersSortedByZ.add(layer);
989 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700990 }
991 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700992 }
Mathias Agopian42977342012-08-05 00:40:46 -0700993 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700994 hw->undefinedRegion.set(bounds);
995 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
996 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700997 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700998 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700999}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001000
Mathias Agopiancd60f992012-08-16 16:28:27 -07001001void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001002 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001003 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1004 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1005 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1006
1007 // If nothing has changed (!dirty), don't recompose.
1008 // If something changed, but we don't currently have any visible layers,
1009 // and didn't when we last did a composition, then skip it this time.
1010 // The second rule does two things:
1011 // - When all layers are removed from a display, we'll emit one black
1012 // frame, then nothing more until we get new layers.
1013 // - When a display is created with a private layer stack, we won't
1014 // emit any black frames until a layer is added to the layer stack.
1015 bool mustRecompose = dirty && !(empty && wasEmpty);
1016
1017 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1018 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1019 mustRecompose ? "doing" : "skipping",
1020 dirty ? "+" : "-",
1021 empty ? "+" : "-",
1022 wasEmpty ? "+" : "-");
1023
Dan Stoza71433162014-02-04 16:22:36 -08001024 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001025
1026 if (mustRecompose) {
1027 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1028 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001029 }
1030
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001031 HWComposer& hwc(getHwComposer());
1032 if (hwc.initCheck() == NO_ERROR) {
1033 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001034 if (CC_UNLIKELY(mHwWorkListDirty)) {
1035 mHwWorkListDirty = false;
1036 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1037 sp<const DisplayDevice> hw(mDisplays[dpy]);
1038 const int32_t id = hw->getHwcDisplayId();
1039 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001040 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001041 hw->getVisibleLayersSortedByZ());
1042 const size_t count = currentLayers.size();
1043 if (hwc.createWorkList(id, count) == NO_ERROR) {
1044 HWComposer::LayerListIterator cur = hwc.begin(id);
1045 const HWComposer::LayerListIterator end = hwc.end(id);
1046 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001047 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001048 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001049 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001050 cur->setSkip(true);
1051 }
1052 }
1053 }
1054 }
1055 }
1056 }
1057
1058 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001059 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001060 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001061 const int32_t id = hw->getHwcDisplayId();
1062 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001063 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001064 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001065 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001066 HWComposer::LayerListIterator cur = hwc.begin(id);
1067 const HWComposer::LayerListIterator end = hwc.end(id);
1068 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1069 /*
1070 * update the per-frame h/w composer data for each layer
1071 * and build the transparent region of the FB
1072 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001073 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001074 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -07001075 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001076 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001077 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001078
Riley Andrews03414a12014-07-01 14:22:59 -07001079 // If possible, attempt to use the cursor overlay on each display.
1080 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1081 sp<const DisplayDevice> hw(mDisplays[dpy]);
1082 const int32_t id = hw->getHwcDisplayId();
1083 if (id >= 0) {
1084 const Vector< sp<Layer> >& currentLayers(
1085 hw->getVisibleLayersSortedByZ());
1086 const size_t count = currentLayers.size();
1087 HWComposer::LayerListIterator cur = hwc.begin(id);
1088 const HWComposer::LayerListIterator end = hwc.end(id);
1089 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1090 const sp<Layer>& layer(currentLayers[i]);
1091 if (layer->isPotentialCursor()) {
1092 cur->setIsCursorLayerHint();
1093 break;
1094 }
1095 }
1096 }
1097 }
1098
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001099 status_t err = hwc.prepare();
1100 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001101
1102 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1103 sp<const DisplayDevice> hw(mDisplays[dpy]);
1104 hw->prepareFrame(hwc);
1105 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001106 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001107}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001108
Mathias Agopiancd60f992012-08-16 16:28:27 -07001109void SurfaceFlinger::doComposition() {
1110 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001111 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001112 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001113 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001114 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001115 // transform the dirty region into this screen's coordinate space
1116 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001117
1118 // repaint the framebuffer (if needed)
1119 doDisplayComposition(hw, dirtyRegion);
1120
Mathias Agopiancd60f992012-08-16 16:28:27 -07001121 hw->dirtyRegion.clear();
1122 hw->flip(hw->swapRegion);
1123 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001124 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001125 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001126 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001127 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001128 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001129}
1130
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001131void SurfaceFlinger::postFramebuffer()
1132{
Mathias Agopian841cde52012-03-01 15:44:37 -08001133 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001134
Mathias Agopiana44b0412011-10-16 18:46:35 -07001135 const nsecs_t now = systemTime();
1136 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001137
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001138 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001139 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001140 if (!hwc.supportsFramebufferTarget()) {
1141 // EGL spec says:
1142 // "surface must be bound to the calling thread's current context,
1143 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001144 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001145 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001146 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001147 }
1148
Mathias Agopian6da15f42013-09-25 20:40:07 -07001149 // make the default display current because the VirtualDisplayDevice code cannot
1150 // deal with dequeueBuffer() being called outside of the composition loop; however
1151 // the code below can call glFlush() which is allowed (and does in some case) call
1152 // dequeueBuffer().
1153 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1154
Mathias Agopian92a979a2012-08-02 18:32:23 -07001155 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001156 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001157 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001158 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001159 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001160 int32_t id = hw->getHwcDisplayId();
1161 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001162 HWComposer::LayerListIterator cur = hwc.begin(id);
1163 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001164 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001165 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001166 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001167 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001168 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001169 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001170 }
Jesse Hallef194142012-06-14 14:45:17 -07001171 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001172 }
1173
Mathias Agopiana44b0412011-10-16 18:46:35 -07001174 mLastSwapBufferTime = systemTime() - now;
1175 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001176
1177 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1178 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1179 logFrameStats();
1180 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181}
1182
Mathias Agopian87baae12012-07-31 12:38:26 -07001183void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184{
Mathias Agopian841cde52012-03-01 15:44:37 -08001185 ATRACE_CALL();
1186
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001187 // here we keep a copy of the drawing state (that is the state that's
1188 // going to be overwritten by handleTransactionLocked()) outside of
1189 // mStateLock so that the side-effects of the State assignment
1190 // don't happen with mStateLock held (which can cause deadlocks).
1191 State drawingState(mDrawingState);
1192
Mathias Agopianca4d3602011-05-19 15:38:14 -07001193 Mutex::Autolock _l(mStateLock);
1194 const nsecs_t now = systemTime();
1195 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001196
Mathias Agopianca4d3602011-05-19 15:38:14 -07001197 // Here we're guaranteed that some transaction flags are set
1198 // so we can call handleTransactionLocked() unconditionally.
1199 // We call getTransactionFlags(), which will also clear the flags,
1200 // with mStateLock held to guarantee that mCurrentState won't change
1201 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001202
Mathias Agopiane57f2922012-08-09 16:29:12 -07001203 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001204 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001205
Mathias Agopianca4d3602011-05-19 15:38:14 -07001206 mLastTransactionTime = systemTime() - now;
1207 mDebugInTransaction = 0;
1208 invalidateHwcGeometry();
1209 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001210}
1211
Mathias Agopian87baae12012-07-31 12:38:26 -07001212void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001213{
1214 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215 const size_t count = currentLayers.size();
1216
1217 /*
1218 * Traversal of the children
1219 * (perform the transaction for each of them if needed)
1220 */
1221
Mathias Agopian3559b072012-08-15 13:46:03 -07001222 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001223 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001224 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001225 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1226 if (!trFlags) continue;
1227
1228 const uint32_t flags = layer->doTransaction(0);
1229 if (flags & Layer::eVisibleRegion)
1230 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001231 }
1232 }
1233
1234 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001235 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001236 */
1237
Mathias Agopiane57f2922012-08-09 16:29:12 -07001238 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001239 // here we take advantage of Vector's copy-on-write semantics to
1240 // improve performance by skipping the transaction entirely when
1241 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001242 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1243 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001244 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001245 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001246 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001247 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001248
1249 // find the displays that were removed
1250 // (ie: in drawing state but not in current state)
1251 // also handle displays that changed
1252 // (ie: displays that are in both lists)
1253 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001254 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1255 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001256 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001257 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001258 // Call makeCurrent() on the primary display so we can
1259 // be sure that nothing associated with this display
1260 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001261 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001262 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001263 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1264 if (hw != NULL)
1265 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001266 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001267 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001268 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001269 } else {
1270 ALOGW("trying to remove the main display");
1271 }
1272 } else {
1273 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001274 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001275 const wp<IBinder>& display(curr.keyAt(j));
Dan Albert1474f882014-09-08 18:59:09 -07001276 const sp<IBinder> state_binder =
1277 state.surface != NULL ? state.surface->asBinder() : NULL;
1278 const sp<IBinder> draw_binder =
1279 draw[i].surface != NULL ? draw[i].surface->asBinder() : NULL;
1280 if (state_binder != draw_binder) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001281 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001282 // recreating the DisplayDevice, so we just remove it
1283 // from the drawing state, so that it get re-added
1284 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001285 sp<DisplayDevice> hw(getDisplayDevice(display));
1286 if (hw != NULL)
1287 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001288 mDisplays.removeItem(display);
1289 mDrawingState.displays.removeItemsAt(i);
1290 dc--; i--;
1291 // at this point we must loop to the next item
1292 continue;
1293 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001294
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001295 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001296 if (disp != NULL) {
1297 if (state.layerStack != draw[i].layerStack) {
1298 disp->setLayerStack(state.layerStack);
1299 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001300 if ((state.orientation != draw[i].orientation)
1301 || (state.viewport != draw[i].viewport)
1302 || (state.frame != draw[i].frame))
1303 {
1304 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001305 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001306 }
Michael Lentine47e45402014-07-18 15:34:25 -07001307 if (state.width != draw[i].width || state.height != draw[i].height) {
1308 disp->setDisplaySize(state.width, state.height);
1309 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001310 }
1311 }
1312 }
1313
1314 // find displays that were added
1315 // (ie: in current state but not in drawing state)
1316 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001317 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001318 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001319
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001320 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001321 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001322 sp<IGraphicBufferProducer> bqProducer;
1323 sp<IGraphicBufferConsumer> bqConsumer;
1324 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1325 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001326
Jesse Hall02d86562013-03-25 14:43:23 -07001327 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001328 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001329 // Virtual displays without a surface are dormant:
1330 // they have external state (layer stack, projection,
1331 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001332 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001333
Jesse Hall02d86562013-03-25 14:43:23 -07001334 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001335 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
Dan Stozab9b08832014-03-13 11:55:57 -07001336 *mHwc, hwcDisplayId, state.surface,
1337 bqProducer, bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001338
1339 dispSurface = vds;
Michael Lentine47e45402014-07-18 15:34:25 -07001340 producer = vds;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001341 }
1342 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001343 ALOGE_IF(state.surface!=NULL,
1344 "adding a supported display, but rendering "
1345 "surface is provided (%p), ignoring it",
1346 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001347 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001348 // for supported (by hwc) displays we provide our
1349 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001350 dispSurface = new FramebufferSurface(*mHwc, state.type,
1351 bqConsumer);
1352 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001353 }
1354
1355 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001356 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001357 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001358 state.type, hwcDisplayId,
1359 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001360 display, dispSurface, producer,
1361 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001362 hw->setLayerStack(state.layerStack);
1363 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001364 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001365 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001366 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001367 if (state.isVirtualDisplay()) {
1368 if (hwcDisplayId >= 0) {
1369 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1370 hw->getWidth(), hw->getHeight(),
1371 hw->getFormat());
1372 }
1373 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001374 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001375 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001376 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001377 }
1378 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001379 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001380 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381
Mathias Agopian84300952012-11-21 16:02:13 -08001382 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1383 // The transform hint might have changed for some layers
1384 // (either because a display has changed, or because a layer
1385 // as changed).
1386 //
1387 // Walk through all the layers in currentLayers,
1388 // and update their transform hint.
1389 //
1390 // If a layer is visible only on a single display, then that
1391 // display is used to calculate the hint, otherwise we use the
1392 // default display.
1393 //
1394 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1395 // the hint is set before we acquire a buffer from the surface texture.
1396 //
1397 // NOTE: layer transactions have taken place already, so we use their
1398 // drawing state. However, SurfaceFlinger's own transaction has not
1399 // happened yet, so we must use the current state layer list
1400 // (soon to become the drawing state list).
1401 //
1402 sp<const DisplayDevice> disp;
1403 uint32_t currentlayerStack = 0;
1404 for (size_t i=0; i<count; i++) {
1405 // NOTE: we rely on the fact that layers are sorted by
1406 // layerStack first (so we don't have to traverse the list
1407 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001408 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001409 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001410 if (i==0 || currentlayerStack != layerStack) {
1411 currentlayerStack = layerStack;
1412 // figure out if this layerstack is mirrored
1413 // (more than one display) if so, pick the default display,
1414 // if not, pick the only display it's on.
1415 disp.clear();
1416 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1417 sp<const DisplayDevice> hw(mDisplays[dpy]);
1418 if (hw->getLayerStack() == currentlayerStack) {
1419 if (disp == NULL) {
1420 disp = hw;
1421 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001422 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001423 break;
1424 }
1425 }
1426 }
1427 }
Chet Haase91d25932013-04-11 15:24:55 -07001428 if (disp == NULL) {
1429 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1430 // redraw after transform hint changes. See bug 8508397.
1431
1432 // could be null when this layer is using a layerStack
1433 // that is not visible on any display. Also can occur at
1434 // screen off/on times.
1435 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001436 }
Chet Haase91d25932013-04-11 15:24:55 -07001437 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001438 }
1439 }
1440
1441
Mathias Agopian3559b072012-08-15 13:46:03 -07001442 /*
1443 * Perform our own transaction if needed
1444 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001445
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001446 const LayerVector& layers(mDrawingState.layersSortedByZ);
1447 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001448 // layers have been added
1449 mVisibleRegionsDirty = true;
1450 }
1451
1452 // some layers might have been removed, so
1453 // we need to update the regions they're exposing.
1454 if (mLayersRemoved) {
1455 mLayersRemoved = false;
1456 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001457 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001458 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001459 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001460 if (currentLayers.indexOf(layer) < 0) {
1461 // this layer is not visible anymore
1462 // TODO: we could traverse the tree from front to back and
1463 // compute the actual visible region
1464 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001465 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001466 Region visibleReg = s.transform.transform(
1467 Region(Rect(s.active.w, s.active.h)));
1468 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001469 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001471 }
1472
1473 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001474
1475 updateCursorAsync();
1476}
1477
1478void SurfaceFlinger::updateCursorAsync()
1479{
1480 HWComposer& hwc(getHwComposer());
1481 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1482 sp<const DisplayDevice> hw(mDisplays[dpy]);
1483 const int32_t id = hw->getHwcDisplayId();
1484 if (id < 0) {
1485 continue;
1486 }
1487 const Vector< sp<Layer> >& currentLayers(
1488 hw->getVisibleLayersSortedByZ());
1489 const size_t count = currentLayers.size();
1490 HWComposer::LayerListIterator cur = hwc.begin(id);
1491 const HWComposer::LayerListIterator end = hwc.end(id);
1492 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1493 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1494 continue;
1495 }
1496 const sp<Layer>& layer(currentLayers[i]);
1497 Rect cursorPos = layer->getPosition(hw);
1498 hwc.setCursorPositionAsync(id, cursorPos);
1499 break;
1500 }
1501 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001502}
1503
1504void SurfaceFlinger::commitTransaction()
1505{
1506 if (!mLayersPendingRemoval.isEmpty()) {
1507 // Notify removed layers now that they can't be drawn from
1508 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1509 mLayersPendingRemoval[i]->onRemoved();
1510 }
1511 mLayersPendingRemoval.clear();
1512 }
1513
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001514 // If this transaction is part of a window animation then the next frame
1515 // we composite should be considered an animation as well.
1516 mAnimCompositionPending = mAnimTransactionPending;
1517
Mathias Agopian4fec8732012-06-29 14:12:52 -07001518 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001519 mTransactionPending = false;
1520 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001521 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001522}
1523
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001524void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001525 const LayerVector& currentLayers, uint32_t layerStack,
1526 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001527{
Mathias Agopian841cde52012-03-01 15:44:37 -08001528 ATRACE_CALL();
1529
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001530 Region aboveOpaqueLayers;
1531 Region aboveCoveredLayers;
1532 Region dirty;
1533
Mathias Agopian87baae12012-07-31 12:38:26 -07001534 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001535
1536 size_t i = currentLayers.size();
1537 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001538 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001539
1540 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001541 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001542
Jesse Hall01e29052013-02-19 16:13:35 -08001543 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001544 if (s.layerStack != layerStack)
1545 continue;
1546
Mathias Agopianab028732010-03-16 16:41:46 -07001547 /*
1548 * opaqueRegion: area of a surface that is fully opaque.
1549 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001550 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001551
1552 /*
1553 * visibleRegion: area of a surface that is visible on screen
1554 * and not fully transparent. This is essentially the layer's
1555 * footprint minus the opaque regions above it.
1556 * Areas covered by a translucent surface are considered visible.
1557 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001558 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001559
1560 /*
1561 * coveredRegion: area of a surface that is covered by all
1562 * visible regions above it (which includes the translucent areas).
1563 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001564 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001565
Jesse Halla8026d22012-09-25 13:25:04 -07001566 /*
1567 * transparentRegion: area of a surface that is hinted to be completely
1568 * transparent. This is only used to tell when the layer has no visible
1569 * non-transparent regions and can be removed from the layer list. It
1570 * does not affect the visibleRegion of this layer or any layers
1571 * beneath it. The hint may not be correct if apps don't respect the
1572 * SurfaceView restrictions (which, sadly, some don't).
1573 */
1574 Region transparentRegion;
1575
Mathias Agopianab028732010-03-16 16:41:46 -07001576
1577 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001578 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001579 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001580 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001581 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001582 if (!visibleRegion.isEmpty()) {
1583 // Remove the transparent area from the visible region
1584 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001585 const Transform tr(s.transform);
1586 if (tr.transformed()) {
1587 if (tr.preserveRects()) {
1588 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001589 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001590 } else {
1591 // transformation too complex, can't do the
1592 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001593 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001594 }
1595 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001596 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001597 }
Mathias Agopianab028732010-03-16 16:41:46 -07001598 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001599
Mathias Agopianab028732010-03-16 16:41:46 -07001600 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001601 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001602 if (s.alpha==255 && !translucent &&
1603 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1604 // the opaque region is the layer's footprint
1605 opaqueRegion = visibleRegion;
1606 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001607 }
1608 }
1609
Mathias Agopianab028732010-03-16 16:41:46 -07001610 // Clip the covered region to the visible region
1611 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1612
1613 // Update aboveCoveredLayers for next (lower) layer
1614 aboveCoveredLayers.orSelf(visibleRegion);
1615
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001616 // subtract the opaque region covered by the layers above us
1617 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001618
1619 // compute this layer's dirty region
1620 if (layer->contentDirty) {
1621 // we need to invalidate the whole region
1622 dirty = visibleRegion;
1623 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001624 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001625 layer->contentDirty = false;
1626 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001627 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001628 * the exposed region consists of two components:
1629 * 1) what's VISIBLE now and was COVERED before
1630 * 2) what's EXPOSED now less what was EXPOSED before
1631 *
1632 * note that (1) is conservative, we start with the whole
1633 * visible region but only keep what used to be covered by
1634 * something -- which mean it may have been exposed.
1635 *
1636 * (2) handles areas that were not covered by anything but got
1637 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001638 */
Mathias Agopianab028732010-03-16 16:41:46 -07001639 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001640 const Region oldVisibleRegion = layer->visibleRegion;
1641 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001642 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1643 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001644 }
1645 dirty.subtractSelf(aboveOpaqueLayers);
1646
1647 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001648 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001649
Mathias Agopianab028732010-03-16 16:41:46 -07001650 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001651 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001652
Jesse Halla8026d22012-09-25 13:25:04 -07001653 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654 layer->setVisibleRegion(visibleRegion);
1655 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001656 layer->setVisibleNonTransparentRegion(
1657 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001658 }
1659
Mathias Agopian87baae12012-07-31 12:38:26 -07001660 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001661}
1662
Mathias Agopian87baae12012-07-31 12:38:26 -07001663void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1664 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001665 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001666 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1667 if (hw->getLayerStack() == layerStack) {
1668 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001669 }
1670 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001671}
1672
1673void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001674{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001675 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001676
Mathias Agopian4fec8732012-06-29 14:12:52 -07001677 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001678 const LayerVector& layers(mDrawingState.layersSortedByZ);
Eric Penner51c59cd2014-07-28 19:51:58 -07001679
1680 // Store the set of layers that need updates. This set must not change as
1681 // buffers are being latched, as this could result in a deadlock.
1682 // Example: Two producers share the same command stream and:
1683 // 1.) Layer 0 is latched
1684 // 2.) Layer 0 gets a new frame
1685 // 2.) Layer 1 gets a new frame
1686 // 3.) Layer 1 is latched.
1687 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1688 // second frame. But layer 0's second frame could be waiting on display.
1689 Vector<Layer*> layersWithQueuedFrames;
1690 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001691 const sp<Layer>& layer(layers[i]);
Eric Penner51c59cd2014-07-28 19:51:58 -07001692 if (layer->hasQueuedFrame())
1693 layersWithQueuedFrames.push_back(layer.get());
1694 }
1695 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
1696 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07001697 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001698 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001699 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001700 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001701
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001702 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001703}
1704
Mathias Agopianad456f92011-01-13 17:53:01 -08001705void SurfaceFlinger::invalidateHwcGeometry()
1706{
1707 mHwWorkListDirty = true;
1708}
1709
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001710
Mathias Agopiancd60f992012-08-16 16:28:27 -07001711void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001712 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001713{
Dan Stoza71433162014-02-04 16:22:36 -08001714 // We only need to actually compose the display if:
1715 // 1) It is being handled by hardware composer, which may need this to
1716 // keep its virtual display state machine in sync, or
1717 // 2) There is work to be done (the dirty region isn't empty)
1718 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1719 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1720 return;
1721 }
1722
Mathias Agopian87baae12012-07-31 12:38:26 -07001723 Region dirtyRegion(inDirtyRegion);
1724
Mathias Agopianb8a55602009-06-26 19:06:36 -07001725 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001726 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001727
Mathias Agopian42977342012-08-05 00:40:46 -07001728 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001729 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001730 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1731 // takes a rectangle, we must make sure to update that whole
1732 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001733 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001734 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001735 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001736 // We need to redraw the rectangle that will be updated
1737 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001738 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001739 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001740 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001741 } else {
1742 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001743 dirtyRegion.set(hw->bounds());
1744 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001745 }
1746 }
1747
Alan Viverette9c5a3332013-09-12 20:04:35 -07001748 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Michael Lentine3f121fc2014-10-01 11:17:28 -07001749 if (!doComposeSurfaces(hw, dirtyRegion)) return;
Mathias Agopianff2ed702013-09-01 21:36:12 -07001750 } else {
1751 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001752 mat4 colorMatrix = mColorMatrix;
1753 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001754 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001755 }
1756 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001757 doComposeSurfaces(hw, dirtyRegion);
1758 engine.endGroup();
1759 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001760
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001761 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001762 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001763
1764 // swap buffers (presentation)
1765 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001766}
1767
Michael Lentine3f121fc2014-10-01 11:17:28 -07001768bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001769{
Mathias Agopian3f844832013-08-07 21:24:32 -07001770 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001771 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001772 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001773 HWComposer::LayerListIterator cur = hwc.begin(id);
1774 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001775
Jesse Halld05a17f2013-09-30 16:49:30 -07001776 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001777 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001778 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001779 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1780 hw->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07001781 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1782 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
1783 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
1784 }
1785 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08001786 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001787
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001788 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001789 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001790 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001791 // when using overlays, we assume a fully transparent framebuffer
1792 // NOTE: we could reduce how much we need to clear, for instance
1793 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001794 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001795 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001796 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001797 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001798 // we start with the whole screen area
1799 const Region bounds(hw->getBounds());
1800
1801 // we remove the scissor part
1802 // we're left with the letterbox region
1803 // (common case is that letterbox ends-up being empty)
1804 const Region letterbox(bounds.subtract(hw->getScissor()));
1805
1806 // compute the area to clear
1807 Region region(hw->undefinedRegion.merge(letterbox));
1808
1809 // but limit it to the dirty region
1810 region.andSelf(dirty);
1811
Mathias Agopianb9494d52012-04-18 02:28:45 -07001812 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001813 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001814 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001815 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001816 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001817 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001818
Mathias Agopian766dc492012-10-30 18:08:06 -07001819 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1820 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001821 // scissor on the main display. It should never be needed
1822 // anyways (though in theory it could since the API allows it).
1823 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001824 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001825 if (scissor != bounds) {
1826 // scissor doesn't match the screen's dimensions, so we
1827 // need to clear everything outside of it and enable
1828 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001829
Mathias Agopianf45c5102012-10-24 16:29:17 -07001830 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001831 const uint32_t height = hw->getHeight();
1832 engine.setScissor(scissor.left, height - scissor.bottom,
1833 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001834 }
1835 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001836 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001837
Mathias Agopian85d751c2012-08-29 16:59:24 -07001838 /*
1839 * and then, render the layers targeted at the framebuffer
1840 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001841
Mathias Agopian13127d82013-03-05 17:47:11 -08001842 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001843 const size_t count = layers.size();
1844 const Transform& tr = hw->getTransform();
1845 if (cur != end) {
1846 // we're using h/w composer
1847 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001848 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001849 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001850 if (!clip.isEmpty()) {
1851 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07001852 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07001853 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001854 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001855 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1856 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001857 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001858 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001859 // never clear the very first layer since we're
1860 // guaranteed the FB is already cleared
1861 layer->clearWithOpenGL(hw, clip);
1862 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001863 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001864 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001865 case HWC_FRAMEBUFFER: {
1866 layer->draw(hw, clip);
1867 break;
1868 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001869 case HWC_FRAMEBUFFER_TARGET: {
1870 // this should not happen as the iterator shouldn't
1871 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001872 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001873 break;
1874 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001875 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001876 }
1877 layer->setAcquireFence(hw, *cur);
1878 }
1879 } else {
1880 // we're not using h/w composer
1881 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001882 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001883 const Region clip(dirty.intersect(
1884 tr.transform(layer->visibleRegion)));
1885 if (!clip.isEmpty()) {
1886 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001887 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001888 }
1889 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001890
1891 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001892 engine.disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07001893 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001894}
1895
Mathias Agopian3f844832013-08-07 21:24:32 -07001896void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001897 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001898 RenderEngine& engine(getRenderEngine());
1899 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001900}
1901
Mathias Agopianac9fa422013-02-11 16:40:36 -08001902void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1903 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001904 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001905 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001906{
Mathias Agopian96f08192010-06-02 23:28:45 -07001907 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001908 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001909
Mathias Agopian96f08192010-06-02 23:28:45 -07001910 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001911 Mutex::Autolock _l(mStateLock);
1912 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001913 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001914}
1915
Mathias Agopian3f844832013-08-07 21:24:32 -07001916status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001917 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001918 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001919 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001920 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001921 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001922 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001923 return NO_ERROR;
1924 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001925 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001926}
1927
Dan Stozac7014012014-02-14 15:03:43 -08001928uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001929 return android_atomic_release_load(&mTransactionFlags);
1930}
1931
Mathias Agopian3f844832013-08-07 21:24:32 -07001932uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001933 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1934}
1935
Mathias Agopian3f844832013-08-07 21:24:32 -07001936uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001937 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1938 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001939 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001940 }
1941 return old;
1942}
1943
Mathias Agopian8b33f032012-07-24 20:43:54 -07001944void SurfaceFlinger::setTransactionState(
1945 const Vector<ComposerState>& state,
1946 const Vector<DisplayState>& displays,
1947 uint32_t flags)
1948{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001949 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001950 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001951 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001952
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001953 if (flags & eAnimation) {
1954 // For window updates that are part of an animation we must wait for
1955 // previous animation "frames" to be handled.
1956 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001957 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001958 if (CC_UNLIKELY(err != NO_ERROR)) {
1959 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001960 // caller after a few seconds.
1961 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1962 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001963 mAnimTransactionPending = false;
1964 break;
1965 }
1966 }
1967 }
1968
Mathias Agopiane57f2922012-08-09 16:29:12 -07001969 size_t count = displays.size();
1970 for (size_t i=0 ; i<count ; i++) {
1971 const DisplayState& s(displays[i]);
1972 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001973 }
1974
Mathias Agopiane57f2922012-08-09 16:29:12 -07001975 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001976 for (size_t i=0 ; i<count ; i++) {
1977 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001978 // Here we need to check that the interface we're given is indeed
1979 // one of our own. A malicious client could give us a NULL
1980 // IInterface, or one of its own or even one of our own but a
1981 // different type. All these situations would cause us to crash.
1982 //
1983 // NOTE: it would be better to use RTTI as we could directly check
1984 // that we have a Client*. however, RTTI is disabled in Android.
1985 if (s.client != NULL) {
1986 sp<IBinder> binder = s.client->asBinder();
1987 if (binder != NULL) {
1988 String16 desc(binder->getInterfaceDescriptor());
1989 if (desc == ISurfaceComposerClient::descriptor) {
1990 sp<Client> client( static_cast<Client *>(s.client.get()) );
1991 transactionFlags |= setClientStateLocked(client, s.state);
1992 }
1993 }
1994 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001995 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001996
Mathias Agopian386aa982011-11-07 21:58:03 -08001997 if (transactionFlags) {
1998 // this triggers the transaction
1999 setTransactionFlags(transactionFlags);
2000
2001 // if this is a synchronous transaction, wait for it to take effect
2002 // before returning.
2003 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002004 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002005 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002006 if (flags & eAnimation) {
2007 mAnimTransactionPending = true;
2008 }
2009 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002010 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2011 if (CC_UNLIKELY(err != NO_ERROR)) {
2012 // just in case something goes wrong in SF, return to the
2013 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002014 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2015 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002016 break;
2017 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002018 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002019 }
2020}
2021
Mathias Agopiane57f2922012-08-09 16:29:12 -07002022uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2023{
Jesse Hall9a143922012-10-04 16:29:19 -07002024 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2025 if (dpyIdx < 0)
2026 return 0;
2027
Mathias Agopiane57f2922012-08-09 16:29:12 -07002028 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002029 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002030 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002031 const uint32_t what = s.what;
2032 if (what & DisplayState::eSurfaceChanged) {
2033 if (disp.surface->asBinder() != s.surface->asBinder()) {
2034 disp.surface = s.surface;
2035 flags |= eDisplayTransactionNeeded;
2036 }
2037 }
2038 if (what & DisplayState::eLayerStackChanged) {
2039 if (disp.layerStack != s.layerStack) {
2040 disp.layerStack = s.layerStack;
2041 flags |= eDisplayTransactionNeeded;
2042 }
2043 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002044 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002045 if (disp.orientation != s.orientation) {
2046 disp.orientation = s.orientation;
2047 flags |= eDisplayTransactionNeeded;
2048 }
2049 if (disp.frame != s.frame) {
2050 disp.frame = s.frame;
2051 flags |= eDisplayTransactionNeeded;
2052 }
2053 if (disp.viewport != s.viewport) {
2054 disp.viewport = s.viewport;
2055 flags |= eDisplayTransactionNeeded;
2056 }
2057 }
Michael Lentine47e45402014-07-18 15:34:25 -07002058 if (what & DisplayState::eDisplaySizeChanged) {
2059 if (disp.width != s.width) {
2060 disp.width = s.width;
2061 flags |= eDisplayTransactionNeeded;
2062 }
2063 if (disp.height != s.height) {
2064 disp.height = s.height;
2065 flags |= eDisplayTransactionNeeded;
2066 }
2067 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002068 }
2069 return flags;
2070}
2071
2072uint32_t SurfaceFlinger::setClientStateLocked(
2073 const sp<Client>& client,
2074 const layer_state_t& s)
2075{
2076 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002077 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002078 if (layer != 0) {
2079 const uint32_t what = s.what;
2080 if (what & layer_state_t::ePositionChanged) {
2081 if (layer->setPosition(s.x, s.y))
2082 flags |= eTraversalNeeded;
2083 }
2084 if (what & layer_state_t::eLayerChanged) {
2085 // NOTE: index needs to be calculated before we update the state
2086 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2087 if (layer->setLayer(s.z)) {
2088 mCurrentState.layersSortedByZ.removeAt(idx);
2089 mCurrentState.layersSortedByZ.add(layer);
2090 // we need traversal (state changed)
2091 // AND transaction (list changed)
2092 flags |= eTransactionNeeded|eTraversalNeeded;
2093 }
2094 }
2095 if (what & layer_state_t::eSizeChanged) {
2096 if (layer->setSize(s.w, s.h)) {
2097 flags |= eTraversalNeeded;
2098 }
2099 }
2100 if (what & layer_state_t::eAlphaChanged) {
2101 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2102 flags |= eTraversalNeeded;
2103 }
2104 if (what & layer_state_t::eMatrixChanged) {
2105 if (layer->setMatrix(s.matrix))
2106 flags |= eTraversalNeeded;
2107 }
2108 if (what & layer_state_t::eTransparentRegionChanged) {
2109 if (layer->setTransparentRegionHint(s.transparentRegion))
2110 flags |= eTraversalNeeded;
2111 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08002112 if ((what & layer_state_t::eVisibilityChanged) ||
2113 (what & layer_state_t::eOpacityChanged)) {
2114 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07002115 if (layer->setFlags(s.flags, s.mask))
2116 flags |= eTraversalNeeded;
2117 }
2118 if (what & layer_state_t::eCropChanged) {
2119 if (layer->setCrop(s.crop))
2120 flags |= eTraversalNeeded;
2121 }
2122 if (what & layer_state_t::eLayerStackChanged) {
2123 // NOTE: index needs to be calculated before we update the state
2124 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2125 if (layer->setLayerStack(s.layerStack)) {
2126 mCurrentState.layersSortedByZ.removeAt(idx);
2127 mCurrentState.layersSortedByZ.add(layer);
2128 // we need traversal (state changed)
2129 // AND transaction (list changed)
2130 flags |= eTransactionNeeded|eTraversalNeeded;
2131 }
2132 }
2133 }
2134 return flags;
2135}
2136
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002137status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002138 const String8& name,
2139 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002140 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2141 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002142{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002143 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002144 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002145 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002146 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002147 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002148 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002149
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002150 status_t result = NO_ERROR;
2151
2152 sp<Layer> layer;
2153
Mathias Agopian3165cc22012-08-08 19:42:09 -07002154 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2155 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002156 result = createNormalLayer(client,
2157 name, w, h, flags, format,
2158 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002159 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002160 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002161 result = createDimLayer(client,
2162 name, w, h, flags,
2163 handle, gbp, &layer);
2164 break;
2165 default:
2166 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002167 break;
2168 }
2169
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002170 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002171 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002172 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002173 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002174 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002175}
2176
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002177status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2178 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2179 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002180{
2181 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002182 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002183 case PIXEL_FORMAT_TRANSPARENT:
2184 case PIXEL_FORMAT_TRANSLUCENT:
2185 format = PIXEL_FORMAT_RGBA_8888;
2186 break;
2187 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002188 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002189 break;
2190 }
2191
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002192 *outLayer = new Layer(this, client, name, w, h, flags);
2193 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2194 if (err == NO_ERROR) {
2195 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002196 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002197 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002198
2199 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2200 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002201}
2202
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002203status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2204 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2205 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002206{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002207 *outLayer = new LayerDim(this, client, name, w, h, flags);
2208 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002209 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002210 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002211}
2212
Mathias Agopianac9fa422013-02-11 16:40:36 -08002213status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002214{
Mathias Agopian67106042013-03-14 19:18:13 -07002215 // called by the window manager when it wants to remove a Layer
2216 status_t err = NO_ERROR;
2217 sp<Layer> l(client->getLayerUser(handle));
2218 if (l != NULL) {
2219 err = removeLayer(l);
2220 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2221 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002222 }
2223 return err;
2224}
2225
Mathias Agopian13127d82013-03-05 17:47:11 -08002226status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002227{
Mathias Agopian67106042013-03-14 19:18:13 -07002228 // called by ~LayerCleaner() when all references to the IBinder (handle)
2229 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002230 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002231 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002232 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002233 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002234 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002235 "error removing layer=%p (%s)", l.get(), strerror(-err));
2236 }
2237 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002238}
2239
Mathias Agopianb60314a2012-04-10 22:09:54 -07002240// ---------------------------------------------------------------------------
2241
Andy McFadden13a082e2012-08-24 10:16:42 -07002242void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002243 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002244 Vector<ComposerState> state;
2245 Vector<DisplayState> displays;
2246 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002247 d.what = DisplayState::eDisplayProjectionChanged |
2248 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002249 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002250 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002251 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002252 d.frame.makeInvalid();
2253 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002254 d.width = 0;
2255 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002256 displays.add(d);
2257 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002258 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002259
2260 const nsecs_t period =
2261 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2262 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002263}
2264
2265void SurfaceFlinger::initializeDisplays() {
2266 class MessageScreenInitialized : public MessageBase {
2267 SurfaceFlinger* flinger;
2268 public:
2269 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2270 virtual bool handler() {
2271 flinger->onInitializeDisplays();
2272 return true;
2273 }
2274 };
2275 sp<MessageBase> msg = new MessageScreenInitialized(this);
2276 postMessageAsync(msg); // we may be called from main thread, use async message
2277}
2278
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002279void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2280 int mode) {
2281 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2282 this);
2283 int32_t type = hw->getDisplayType();
2284 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002285
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002286 if (mode == currentMode) {
2287 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002288 return;
2289 }
2290
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002291 hw->setPowerMode(mode);
2292 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2293 ALOGW("Trying to set power mode for virtual display");
2294 return;
2295 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002296
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002297 if (currentMode == HWC_POWER_MODE_OFF) {
2298 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002299 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2300 // FIXME: eventthread only knows about the main display right now
2301 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002302 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002303 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002304
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002305 mVisibleRegionsDirty = true;
2306 repaintEverything();
2307 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002308 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002309 disableHardwareVsync(true); // also cancels any in-progress resync
2310
Mathias Agopiancde87a32012-09-13 14:09:01 -07002311 // FIXME: eventthread only knows about the main display right now
2312 mEventThread->onScreenReleased();
2313 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002314
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002315 getHwComposer().setPowerMode(type, mode);
2316 mVisibleRegionsDirty = true;
2317 // from this point on, SF will stop drawing on this display
2318 } else {
2319 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002320 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002321}
2322
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002323void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2324 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002325 SurfaceFlinger& mFlinger;
2326 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002327 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002328 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002329 MessageSetPowerMode(SurfaceFlinger& flinger,
2330 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2331 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002332 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002333 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002334 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002335 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002336 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002337 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002338 ALOGW("Attempt to set power mode = %d for virtual display",
2339 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002340 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002341 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002342 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002343 return true;
2344 }
2345 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002346 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002347 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002348}
2349
2350// ---------------------------------------------------------------------------
2351
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002352status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2353{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002354 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002355
Mathias Agopianbd115332013-04-18 16:41:04 -07002356 IPCThreadState* ipc = IPCThreadState::self();
2357 const int pid = ipc->getCallingPid();
2358 const int uid = ipc->getCallingUid();
2359 if ((uid != AID_SHELL) &&
2360 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002361 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002362 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002363 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002364 // Try to get the main lock, but don't insist if we can't
2365 // (this would indicate SF is stuck, but we want to be able to
2366 // print something in dumpsys).
2367 int retry = 3;
2368 while (mStateLock.tryLock()<0 && --retry>=0) {
2369 usleep(1000000);
2370 }
2371 const bool locked(retry >= 0);
2372 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002373 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002374 "SurfaceFlinger appears to be unresponsive, "
2375 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002376 }
2377
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002378 bool dumpAll = true;
2379 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002380 size_t numArgs = args.size();
2381 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002382 if ((index < numArgs) &&
2383 (args[index] == String16("--list"))) {
2384 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002385 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002386 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002387 }
2388
2389 if ((index < numArgs) &&
2390 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002391 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002392 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002393 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002394 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002395
2396 if ((index < numArgs) &&
2397 (args[index] == String16("--latency-clear"))) {
2398 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002399 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002400 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002401 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002402
2403 if ((index < numArgs) &&
2404 (args[index] == String16("--dispsync"))) {
2405 index++;
2406 mPrimaryDispSync.dump(result);
2407 dumpAll = false;
2408 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002409 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002410
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002411 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002412 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002413 }
2414
Mathias Agopian9795c422009-08-26 16:36:26 -07002415 if (locked) {
2416 mStateLock.unlock();
2417 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002418 }
2419 write(fd, result.string(), result.size());
2420 return NO_ERROR;
2421}
2422
Dan Stozac7014012014-02-14 15:03:43 -08002423void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2424 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002425{
2426 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2427 const size_t count = currentLayers.size();
2428 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002429 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002430 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002431 }
2432}
2433
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002434void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002435 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002436{
2437 String8 name;
2438 if (index < args.size()) {
2439 name = String8(args[index]);
2440 index++;
2441 }
2442
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002443 const nsecs_t period =
2444 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002445 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002446
2447 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002448 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002449 } else {
2450 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2451 const size_t count = currentLayers.size();
2452 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002453 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002454 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002455 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002456 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002457 }
2458 }
2459}
2460
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002461void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002462 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002463{
2464 String8 name;
2465 if (index < args.size()) {
2466 name = String8(args[index]);
2467 index++;
2468 }
2469
2470 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2471 const size_t count = currentLayers.size();
2472 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002473 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002474 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002475 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002476 }
2477 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002478
Svetoslavd85084b2014-03-20 10:28:31 -07002479 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002480}
2481
Jamie Gennis6547ff42013-07-16 20:12:42 -07002482// This should only be called from the main thread. Otherwise it would need
2483// the lock and should use mCurrentState rather than mDrawingState.
2484void SurfaceFlinger::logFrameStats() {
2485 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2486 const size_t count = drawingLayers.size();
2487 for (size_t i=0 ; i<count ; i++) {
2488 const sp<Layer>& layer(drawingLayers[i]);
2489 layer->logFrameStats();
2490 }
2491
2492 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2493}
2494
Andy McFadden4803b742012-09-24 19:07:20 -07002495/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2496{
2497 static const char* config =
2498 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002499#ifdef HAS_CONTEXT_PRIORITY
2500 " HAS_CONTEXT_PRIORITY"
2501#endif
2502#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2503 " NEVER_DEFAULT_TO_ASYNC_MODE"
2504#endif
2505#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2506 " TARGET_DISABLE_TRIPLE_BUFFERING"
2507#endif
2508 "]";
2509 result.append(config);
2510}
2511
Mathias Agopian74d211a2013-04-22 16:55:35 +02002512void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2513 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002514{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002515 bool colorize = false;
2516 if (index < args.size()
2517 && (args[index] == String16("--color"))) {
2518 colorize = true;
2519 index++;
2520 }
2521
2522 Colorizer colorizer(colorize);
2523
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002524 // figure out if we're stuck somewhere
2525 const nsecs_t now = systemTime();
2526 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2527 const nsecs_t inTransaction(mDebugInTransaction);
2528 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2529 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2530
2531 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002532 * Dump library configuration.
2533 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002534
2535 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002536 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002537 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002538 appendSfConfigString(result);
2539 appendUiConfigString(result);
2540 appendGuiConfigString(result);
2541 result.append("\n");
2542
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002543 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002544 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002545 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002546 result.append(SyncFeatures::getInstance().toString());
2547 result.append("\n");
2548
Andy McFadden41d67d72014-04-25 16:58:34 -07002549 colorizer.bold(result);
2550 result.append("DispSync configuration: ");
2551 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002552 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2553 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002554 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2555 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2556 result.append("\n");
2557
Andy McFadden4803b742012-09-24 19:07:20 -07002558 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002559 * Dump the visible layer list
2560 */
2561 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2562 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002563 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002564 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002565 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002566 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002567 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002568 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002569 }
2570
2571 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002572 * Dump Display state
2573 */
2574
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002575 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002576 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002577 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002578 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2579 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002580 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002581 }
2582
2583 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002584 * Dump SurfaceFlinger global state
2585 */
2586
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002587 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002588 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002589 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002590
Mathias Agopian888c8222012-08-04 21:10:38 -07002591 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002592 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002593
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002594 colorizer.bold(result);
2595 result.appendFormat("EGL implementation : %s\n",
2596 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2597 colorizer.reset(result);
2598 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002599 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002600
Mathias Agopian875d8e12013-06-07 15:35:48 -07002601 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002602
Mathias Agopian42977342012-08-05 00:40:46 -07002603 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002604 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2605 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002606 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002607 " last eglSwapBuffers() time: %f us\n"
2608 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002609 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002610 " refresh-rate : %f fps\n"
2611 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002612 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002613 " gpu_to_cpu_unsupported : %d\n"
2614 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002615 mLastSwapBufferTime/1000.0,
2616 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002617 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002618 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2619 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002620 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002621 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002622
Mathias Agopian74d211a2013-04-22 16:55:35 +02002623 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002624 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002625
Mathias Agopian74d211a2013-04-22 16:55:35 +02002626 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002627 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002628
2629 /*
2630 * VSYNC state
2631 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002632 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002633
2634 /*
2635 * Dump HWComposer state
2636 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002637 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002638 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002639 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002640 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002641 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002642 (mDebugDisableHWC || mDebugRegion || mDaltonize
2643 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002644 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002645
2646 /*
2647 * Dump gralloc state
2648 */
2649 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2650 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002651}
2652
Mathias Agopian13127d82013-03-05 17:47:11 -08002653const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002654SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002655 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002656 wp<IBinder> dpy;
2657 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2658 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2659 dpy = mDisplays.keyAt(i);
2660 break;
2661 }
2662 }
2663 if (dpy == NULL) {
2664 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2665 // Just use the primary display so we have something to return
2666 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2667 }
2668 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002669}
2670
Keun young Park63f165f2012-08-31 10:53:36 -07002671bool SurfaceFlinger::startDdmConnection()
2672{
2673 void* libddmconnection_dso =
2674 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2675 if (!libddmconnection_dso) {
2676 return false;
2677 }
2678 void (*DdmConnection_start)(const char* name);
2679 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07002680 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07002681 if (!DdmConnection_start) {
2682 dlclose(libddmconnection_dso);
2683 return false;
2684 }
2685 (*DdmConnection_start)(getServiceName());
2686 return true;
2687}
2688
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002689status_t SurfaceFlinger::onTransact(
2690 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2691{
2692 switch (code) {
2693 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002694 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002695 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002696 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002697 case CLEAR_ANIMATION_FRAME_STATS:
2698 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002699 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002700 {
2701 // codes that require permission check
2702 IPCThreadState* ipc = IPCThreadState::self();
2703 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002704 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002705 if ((uid != AID_GRAPHICS) &&
2706 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002707 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002708 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2709 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002710 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002711 break;
2712 }
2713 case CAPTURE_SCREEN:
2714 {
2715 // codes that require permission check
2716 IPCThreadState* ipc = IPCThreadState::self();
2717 const int pid = ipc->getCallingPid();
2718 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002719 if ((uid != AID_GRAPHICS) &&
2720 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002721 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002722 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2723 return PERMISSION_DENIED;
2724 }
2725 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002726 }
2727 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002728
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002729 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2730 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002731 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002732 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002733 IPCThreadState* ipc = IPCThreadState::self();
2734 const int pid = ipc->getCallingPid();
2735 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002736 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002737 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002738 return PERMISSION_DENIED;
2739 }
2740 int n;
2741 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002742 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002743 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002744 return NO_ERROR;
2745 case 1002: // SHOW_UPDATES
2746 n = data.readInt32();
2747 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002748 invalidateHwcGeometry();
2749 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002750 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002751 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002752 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002753 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002754 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002755 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002756 setTransactionFlags(
2757 eTransactionNeeded|
2758 eDisplayTransactionNeeded|
2759 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002760 return NO_ERROR;
2761 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002762 case 1006:{ // send empty update
2763 signalRefresh();
2764 return NO_ERROR;
2765 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002766 case 1008: // toggle use of hw composer
2767 n = data.readInt32();
2768 mDebugDisableHWC = n ? 1 : 0;
2769 invalidateHwcGeometry();
2770 repaintEverything();
2771 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002772 case 1009: // toggle use of transform hint
2773 n = data.readInt32();
2774 mDebugDisableTransformHint = n ? 1 : 0;
2775 invalidateHwcGeometry();
2776 repaintEverything();
2777 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002778 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002779 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002780 reply->writeInt32(0);
2781 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002782 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002783 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002784 return NO_ERROR;
2785 case 1013: {
2786 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002787 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2788 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002789 return NO_ERROR;
2790 }
2791 case 1014: {
2792 // daltonize
2793 n = data.readInt32();
2794 switch (n % 10) {
2795 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2796 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2797 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2798 }
2799 if (n >= 10) {
2800 mDaltonizer.setMode(Daltonizer::correction);
2801 } else {
2802 mDaltonizer.setMode(Daltonizer::simulation);
2803 }
2804 mDaltonize = n > 0;
2805 invalidateHwcGeometry();
2806 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002807 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002808 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002809 case 1015: {
2810 // apply a color matrix
2811 n = data.readInt32();
2812 mHasColorMatrix = n ? 1 : 0;
2813 if (n) {
2814 // color matrix is sent as mat3 matrix followed by vec3
2815 // offset, then packed into a mat4 where the last row is
2816 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002817 for (size_t i = 0 ; i < 4; i++) {
2818 for (size_t j = 0; j < 4; j++) {
2819 mColorMatrix[i][j] = data.readFloat();
2820 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002821 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002822 } else {
2823 mColorMatrix = mat4();
2824 }
2825 invalidateHwcGeometry();
2826 repaintEverything();
2827 return NO_ERROR;
2828 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002829 // This is an experimental interface
2830 // Needs to be shifted to proper binder interface when we productize
2831 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07002832 n = data.readInt32();
2833 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002834 return NO_ERROR;
2835 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002836 }
2837 }
2838 return err;
2839}
2840
Mathias Agopian53331da2011-08-22 21:44:41 -07002841void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002842 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002843 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002844}
2845
Mathias Agopian59119e62010-10-11 12:37:43 -07002846// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002847// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002848// ---------------------------------------------------------------------------
2849
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002850/* The code below is here to handle b/8734824
2851 *
2852 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07002853 * from the surfaceflinger thread to the calling binder thread, where they
2854 * are executed. This allows the calling thread in the calling process to be
2855 * reused and not depend on having "enough" binder threads to handle the
2856 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002857 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002858class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07002859 /* Parts of GraphicProducerWrapper are run on two different threads,
2860 * communicating by sending messages via Looper but also by shared member
2861 * data. Coherence maintenance is subtle and in places implicit (ugh).
2862 *
2863 * Don't rely on Looper's sendMessage/handleMessage providing
2864 * release/acquire semantics for any data not actually in the Message.
2865 * Data going from surfaceflinger to binder threads needs to be
2866 * synchronized explicitly.
2867 *
2868 * Barrier open/wait do provide release/acquire semantics. This provides
2869 * implicit synchronization for data coming back from binder to
2870 * surfaceflinger threads.
2871 */
2872
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002873 sp<IGraphicBufferProducer> impl;
2874 sp<Looper> looper;
2875 status_t result;
2876 bool exitPending;
2877 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07002878 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002879 uint32_t code;
2880 Parcel const* data;
2881 Parcel* reply;
2882
2883 enum {
2884 MSG_API_CALL,
2885 MSG_EXIT
2886 };
2887
2888 /*
Jesse Hallb154c422014-07-13 12:47:02 -07002889 * Called on surfaceflinger thread. This is called by our "fake"
2890 * BpGraphicBufferProducer. We package the data and reply Parcel and
2891 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002892 */
2893 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08002894 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002895 this->code = code;
2896 this->data = &data;
2897 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002898 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07002899 // if we've exited, we run the message synchronously right here.
2900 // note (JH): as far as I can tell from looking at the code, this
2901 // never actually happens. if it does, i'm not sure if it happens
2902 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002903 handleMessage(Message(MSG_API_CALL));
2904 } else {
2905 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07002906 // Prevent stores to this->{code, data, reply} from being
2907 // reordered later than the construction of Message.
2908 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002909 looper->sendMessage(this, Message(MSG_API_CALL));
2910 barrier.wait();
2911 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08002912 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002913 }
2914
2915 /*
Jesse Hallb154c422014-07-13 12:47:02 -07002916 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002917 * call the real BpGraphicBufferProducer.
2918 */
2919 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07002920 int what = message.what;
2921 // Prevent reads below from happening before the read from Message
2922 atomic_thread_fence(memory_order_acquire);
2923 if (what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08002924 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002925 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07002926 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002927 exitRequested = true;
2928 }
2929 }
2930
2931public:
Jesse Hallb154c422014-07-13 12:47:02 -07002932 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
2933 : impl(impl),
2934 looper(new Looper(true)),
2935 exitPending(false),
2936 exitRequested(false)
2937 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002938
Jesse Hallb154c422014-07-13 12:47:02 -07002939 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002940 status_t waitForResponse() {
2941 do {
2942 looper->pollOnce(-1);
2943 } while (!exitRequested);
2944 return result;
2945 }
2946
Jesse Hallb154c422014-07-13 12:47:02 -07002947 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002948 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002949 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002950 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07002951 // Ensure this->result is visible to the binder thread before it
2952 // handles the message.
2953 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002954 looper->sendMessage(this, Message(MSG_EXIT));
2955 }
2956};
2957
2958
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002959status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2960 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002961 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002962 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07002963 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002964
2965 if (CC_UNLIKELY(display == 0))
2966 return BAD_VALUE;
2967
2968 if (CC_UNLIKELY(producer == 0))
2969 return BAD_VALUE;
2970
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002971 // if we have secure windows on this display, never allow the screen capture
2972 // unless the producer interface is local (i.e.: we can take a screenshot for
2973 // ourselves).
2974 if (!producer->asBinder()->localBinder()) {
2975 Mutex::Autolock _l(mStateLock);
2976 sp<const DisplayDevice> hw(getDisplayDevice(display));
2977 if (hw->getSecureLayerVisible()) {
2978 ALOGW("FB is protected: PERMISSION_DENIED");
2979 return PERMISSION_DENIED;
2980 }
2981 }
2982
Riley Andrewsc3ebe662014-09-04 16:20:31 -07002983 // Convert to surfaceflinger's internal rotation type.
2984 Transform::orientation_flags rotationFlags;
2985 switch (rotation) {
2986 case ISurfaceComposer::eRotateNone:
2987 rotationFlags = Transform::ROT_0;
2988 break;
2989 case ISurfaceComposer::eRotate90:
2990 rotationFlags = Transform::ROT_90;
2991 break;
2992 case ISurfaceComposer::eRotate180:
2993 rotationFlags = Transform::ROT_180;
2994 break;
2995 case ISurfaceComposer::eRotate270:
2996 rotationFlags = Transform::ROT_270;
2997 break;
2998 default:
2999 rotationFlags = Transform::ROT_0;
3000 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3001 break;
3002 }
3003
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003004 class MessageCaptureScreen : public MessageBase {
3005 SurfaceFlinger* flinger;
3006 sp<IBinder> display;
3007 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003008 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003009 uint32_t reqWidth, reqHeight;
3010 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003011 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003012 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003013 status_t result;
3014 public:
3015 MessageCaptureScreen(SurfaceFlinger* flinger,
3016 const sp<IBinder>& display,
3017 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003018 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003019 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003020 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003021 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003022 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003023 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003024 useIdentityTransform(useIdentityTransform),
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003025 rotation(rotation),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003026 result(PERMISSION_DENIED)
3027 {
3028 }
3029 status_t getResult() const {
3030 return result;
3031 }
3032 virtual bool handler() {
3033 Mutex::Autolock _l(flinger->mStateLock);
3034 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003035 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003036 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003037 useIdentityTransform, rotation);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003038 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003039 return true;
3040 }
3041 };
3042
Mathias Agopian9eb1f052013-04-10 16:27:17 -07003043 // make sure to process transactions before screenshots -- a transaction
3044 // might already be pending but scheduled for VSYNC; this guarantees we
3045 // will handle it before the screenshot. When VSYNC finally arrives
3046 // the scheduled transaction will be a no-op. If no transactions are
3047 // scheduled at this time, this will end-up being a no-op as well.
3048 mEventQueue.invalidateTransactionNow();
3049
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003050 // this creates a "fake" BBinder which will serve as a "fake" remote
3051 // binder to receive the marshaled calls and forward them to the
3052 // real remote (a BpGraphicBufferProducer)
3053 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3054
3055 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3056 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003057 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003058 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003059 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003060 useIdentityTransform, rotationFlags);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003061
3062 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003063 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003064 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003065 }
3066 return res;
3067}
3068
Mathias Agopian180f10d2013-04-10 22:55:41 -07003069
3070void SurfaceFlinger::renderScreenImplLocked(
3071 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003072 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003073 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003074 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003075{
3076 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003077 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003078
3079 // get screen geometry
Andreas Gampe89fd4f72014-11-13 14:18:56 -08003080 const int32_t hw_w = hw->getWidth();
3081 const int32_t hw_h = hw->getHeight();
3082 const bool filtering = static_cast<int32_t>(reqWidth) != hw_w ||
3083 static_cast<int32_t>(reqWidth) != hw_h;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003084
Dan Stozac1879002014-05-22 15:59:05 -07003085 // if a default or invalid sourceCrop is passed in, set reasonable values
3086 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3087 !sourceCrop.isValid()) {
3088 sourceCrop.setLeftTop(Point(0, 0));
3089 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3090 }
3091
3092 // ensure that sourceCrop is inside screen
3093 if (sourceCrop.left < 0) {
3094 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3095 }
Dan Stozabe31f442014-06-11 11:20:54 -07003096 if (sourceCrop.right > hw_w) {
3097 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003098 }
3099 if (sourceCrop.top < 0) {
3100 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3101 }
Dan Stozabe31f442014-06-11 11:20:54 -07003102 if (sourceCrop.bottom > hw_h) {
3103 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003104 }
3105
Mathias Agopian180f10d2013-04-10 22:55:41 -07003106 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003107 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003108
3109 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003110 engine.setViewportAndProjection(
3111 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003112 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003113
3114 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003115 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003116
3117 const LayerVector& layers( mDrawingState.layersSortedByZ );
3118 const size_t count = layers.size();
3119 for (size_t i=0 ; i<count ; ++i) {
3120 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003121 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003122 if (state.layerStack == hw->getLayerStack()) {
3123 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3124 if (layer->isVisible()) {
3125 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08003126 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003127 if (filtering) layer->setFiltering(false);
3128 }
3129 }
3130 }
3131 }
3132
3133 // compositionComplete is needed for older driver
3134 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003135 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003136}
3137
3138
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003139status_t SurfaceFlinger::captureScreenImplLocked(
3140 const sp<const DisplayDevice>& hw,
3141 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003142 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003143 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003144 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003145{
3146 ATRACE_CALL();
3147
Mathias Agopian180f10d2013-04-10 22:55:41 -07003148 // get screen geometry
3149 const uint32_t hw_w = hw->getWidth();
3150 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003151
Mathias Agopian180f10d2013-04-10 22:55:41 -07003152 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3153 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3154 reqWidth, reqHeight, hw_w, hw_h);
3155 return BAD_VALUE;
3156 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003157
Mathias Agopian180f10d2013-04-10 22:55:41 -07003158 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3159 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3160
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003161 // create a surface (because we're a producer, and we need to
3162 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003163 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003164 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003165
3166 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003167 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003168 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3169 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003170
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003171 int err = 0;
3172 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003173 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003174 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3175 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003176
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003177 if (err == NO_ERROR) {
3178 ANativeWindowBuffer* buffer;
3179 /* TODO: Once we have the sync framework everywhere this can use
3180 * server-side waits on the fence that dequeueBuffer returns.
3181 */
3182 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3183 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003184 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003185 // create an EGLImage from the buffer so we can later
3186 // turn it into a texture
3187 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3188 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3189 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003190 // this binds the given EGLImage as a framebuffer for the
3191 // duration of this scope.
3192 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3193 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003194 // this will in fact render into our dequeued buffer
3195 // via an FBO, which means we didn't have to create
3196 // an EGLSurface and therefore we're not
3197 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003198 renderScreenImplLocked(
3199 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3200 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003201
Riley Andrews86639902014-08-15 12:27:24 -07003202 // Attempt to create a sync khr object that can produce a sync point. If that
3203 // isn't available, create a non-dupable sync object in the fallback path and
3204 // wait on it directly.
3205 EGLSyncKHR sync;
3206 if (!DEBUG_SCREENSHOTS) {
3207 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Andy McFadden2d8d1202013-10-09 16:38:02 -07003208 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003209 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003210 }
Riley Andrews86639902014-08-15 12:27:24 -07003211 if (sync != EGL_NO_SYNC_KHR) {
3212 // get the sync fd
3213 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3214 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3215 ALOGW("captureScreen: failed to dup sync khr object");
3216 syncFd = -1;
3217 }
3218 eglDestroySyncKHR(mEGLDisplay, sync);
3219 } else {
3220 // fallback path
3221 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3222 if (sync != EGL_NO_SYNC_KHR) {
3223 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3224 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3225 EGLint eglErr = eglGetError();
3226 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3227 ALOGW("captureScreen: fence wait timed out");
3228 } else {
3229 ALOGW_IF(eglErr != EGL_SUCCESS,
3230 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3231 }
3232 eglDestroySyncKHR(mEGLDisplay, sync);
3233 } else {
3234 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3235 }
3236 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003237 if (DEBUG_SCREENSHOTS) {
3238 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3239 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3240 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3241 hw, minLayerZ, maxLayerZ);
3242 delete [] pixels;
3243 }
3244
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003245 } else {
3246 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3247 result = INVALID_OPERATION;
3248 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003249 // destroy our image
3250 eglDestroyImageKHR(mEGLDisplay, image);
3251 } else {
3252 result = BAD_VALUE;
3253 }
Riley Andrews86639902014-08-15 12:27:24 -07003254 window->queueBuffer(window, buffer, syncFd);
3255 if (syncFd != -1) {
3256 close(syncFd);
3257 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003258 }
3259 } else {
3260 result = BAD_VALUE;
3261 }
3262 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3263 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003264
Mathias Agopian74c40c02010-09-29 13:02:36 -07003265 return result;
3266}
3267
Mathias Agopiand5556842013-09-19 17:08:37 -07003268void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3269 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003270 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003271 for (size_t y=0 ; y<h ; y++) {
3272 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3273 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003274 if (p[x] != 0xFF000000) return;
3275 }
3276 }
3277 ALOGE("*** we just took a black screenshot ***\n"
3278 "requested minz=%d, maxz=%d, layerStack=%d",
3279 minLayerZ, maxLayerZ, hw->getLayerStack());
3280 const LayerVector& layers( mDrawingState.layersSortedByZ );
3281 const size_t count = layers.size();
3282 for (size_t i=0 ; i<count ; ++i) {
3283 const sp<Layer>& layer(layers[i]);
3284 const Layer::State& state(layer->getDrawingState());
3285 const bool visible = (state.layerStack == hw->getLayerStack())
3286 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3287 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003288 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003289 visible ? '+' : '-',
3290 i, layer->getName().string(), state.layerStack, state.z,
3291 layer->isVisible(), state.flags, state.alpha);
3292 }
3293 }
3294}
3295
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003296// ---------------------------------------------------------------------------
3297
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003298SurfaceFlinger::LayerVector::LayerVector() {
3299}
3300
3301SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003302 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003303}
3304
3305int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3306 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003307{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003308 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003309 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3310 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003311
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003312 uint32_t ls = l->getCurrentState().layerStack;
3313 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003314 if (ls != rs)
3315 return ls - rs;
3316
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003317 uint32_t lz = l->getCurrentState().z;
3318 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003319 if (lz != rz)
3320 return lz - rz;
3321
3322 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003323}
3324
3325// ---------------------------------------------------------------------------
3326
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003327SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Michael Lentine47e45402014-07-18 15:34:25 -07003328 : type(DisplayDevice::DISPLAY_ID_INVALID), width(0), height(0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003329}
3330
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003331SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Michael Lentine47e45402014-07-18 15:34:25 -07003332 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0), width(0), height(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003333 viewport.makeInvalid();
3334 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003335}
3336
3337// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003338
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003339}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003340
3341
3342#if defined(__gl_h_)
3343#error "don't include gl/gl.h in this file"
3344#endif
3345
3346#if defined(__gl2_h_)
3347#error "don't include gl2/gl2.h in this file"
3348#endif