blob: b9c70e0557f40fd7acc7604bf574e1fc82e477bc [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>
38
Mathias Agopian921e6ac2012-07-23 23:11:29 -070039#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070040#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070041#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070042#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080043#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080044#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070045
46#include <ui/GraphicBufferAllocator.h>
47#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070048#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080049
Mathias Agopiancde87a32012-09-13 14:09:01 -070050#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include <utils/String8.h>
52#include <utils/String16.h>
53#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080054#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Mathias Agopian921e6ac2012-07-23 23:11:29 -070056#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070057#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
Mathias Agopian3e25fd82013-04-22 17:52:16 +020059#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020061#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080062#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070063#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070064#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070065#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080066#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070
Mathias Agopiana4912602012-07-12 14:25:33 -070071#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070072#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070073#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074
Mathias Agopianff2ed702013-09-01 21:36:12 -070075#include "Effects/Daltonizer.h"
76
Mathias Agopian875d8e12013-06-07 15:35:48 -070077#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070078#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070079
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080#define DISPLAY_COUNT 1
81
Mathias Agopianfee2b462013-07-03 12:34:01 -070082/*
83 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
84 * black pixels.
85 */
86#define DEBUG_SCREENSHOTS false
87
Mathias Agopianca088332013-03-28 17:44:13 -070088EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
89
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070091
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070092// This is the phase offset in nanoseconds of the software vsync event
93// relative to the vsync event reported by HWComposer. The software vsync
94// event is when SurfaceFlinger and Choreographer-based applications run each
95// frame.
96//
97// This phase offset allows adjustment of the minimum latency from application
98// wake-up (by Choregographer) time to the time at which the resulting window
99// image is displayed. This value may be either positive (after the HW vsync)
100// or negative (before the HW vsync). Setting it to 0 will result in a
101// minimum latency of two vsync periods because the app and SurfaceFlinger
102// will run just after the HW vsync. Setting it to a positive number will
103// result in the minimum latency being:
104//
105// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
106//
107// Note that reducing this latency makes it more likely for the applications
108// to not have their window content image ready in time. When this happens
109// the latency will end up being an additional vsync period, and animations
110// will hiccup. Therefore, this latency should be tuned somewhat
111// conservatively (or at least with awareness of the trade-off being made).
112static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
113
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700114// This is the phase offset at which SurfaceFlinger's composition runs.
115static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
116
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117// ---------------------------------------------------------------------------
118
Mathias Agopian99b49842011-06-27 16:05:52 -0700119const String16 sHardwareTest("android.permission.HARDWARE_TEST");
120const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
121const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
122const String16 sDump("android.permission.DUMP");
123
124// ---------------------------------------------------------------------------
125
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700127 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700129 mTransactionPending(false),
130 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700131 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700132 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700133 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700136 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800137 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700139 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700140 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700141 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700142 mDebugInSwapBuffers(0),
143 mLastSwapBufferTime(0),
144 mDebugInTransaction(0),
145 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700146 mBootFinished(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700147 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700148 mHWVsyncAvailable(false),
Alan Viverette9c5a3332013-09-12 20:04:35 -0700149 mDaltonize(false),
150 mHasColorMatrix(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151{
Steve Blocka19954a2012-01-04 20:05:49 +0000152 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153
154 // debugging stuff...
155 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700156
Mathias Agopianb4b17302013-03-20 18:36:41 -0700157 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700158 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700159
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 property_get("debug.sf.showupdates", value, "0");
161 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700162
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700163 property_get("debug.sf.ddms", value, "0");
164 mDebugDDMS = atoi(value);
165 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700166 if (!startDdmConnection()) {
167 // start failed, and DDMS debugging not enabled
168 mDebugDDMS = 0;
169 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700170 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700171 ALOGI_IF(mDebugRegion, "showupdates enabled");
172 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173}
174
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800175void SurfaceFlinger::onFirstRef()
176{
177 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800178}
179
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180SurfaceFlinger::~SurfaceFlinger()
181{
Mathias Agopiana4912602012-07-12 14:25:33 -0700182 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
183 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
184 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185}
186
Dan Stozac7014012014-02-14 15:03:43 -0800187void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800188{
189 // the window manager died on us. prepare its eulogy.
190
Andy McFadden13a082e2012-08-24 10:16:42 -0700191 // restore initial conditions (default device unblank, etc)
192 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800193
194 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700195 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800196}
197
Mathias Agopian7e27f052010-05-28 14:22:23 -0700198sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199{
Mathias Agopian96f08192010-06-02 23:28:45 -0700200 sp<ISurfaceComposerClient> bclient;
201 sp<Client> client(new Client(this));
202 status_t err = client->initCheck();
203 if (err == NO_ERROR) {
204 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 return bclient;
207}
208
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700209sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
210 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700211{
212 class DisplayToken : public BBinder {
213 sp<SurfaceFlinger> flinger;
214 virtual ~DisplayToken() {
215 // no more references, this display must be terminated
216 Mutex::Autolock _l(flinger->mStateLock);
217 flinger->mCurrentState.displays.removeItem(this);
218 flinger->setTransactionFlags(eDisplayTransactionNeeded);
219 }
220 public:
221 DisplayToken(const sp<SurfaceFlinger>& flinger)
222 : flinger(flinger) {
223 }
224 };
225
226 sp<BBinder> token = new DisplayToken(this);
227
228 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700229 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700230 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700231 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700232 mCurrentState.displays.add(token, info);
233
234 return token;
235}
236
Jesse Hall6c913be2013-08-08 12:15:49 -0700237void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
238 Mutex::Autolock _l(mStateLock);
239
240 ssize_t idx = mCurrentState.displays.indexOfKey(display);
241 if (idx < 0) {
242 ALOGW("destroyDisplay: invalid display token");
243 return;
244 }
245
246 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
247 if (!info.isVirtualDisplay()) {
248 ALOGE("destroyDisplay called for non-virtual display");
249 return;
250 }
251
252 mCurrentState.displays.removeItemsAt(idx);
253 setTransactionFlags(eDisplayTransactionNeeded);
254}
255
Jesse Hall692c7232012-11-08 15:41:56 -0800256void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
257 ALOGW_IF(mBuiltinDisplays[type],
258 "Overwriting display token for display type %d", type);
259 mBuiltinDisplays[type] = new BBinder();
260 DisplayDeviceState info(type);
261 // All non-virtual displays are currently considered secure.
262 info.isSecure = true;
263 mCurrentState.displays.add(mBuiltinDisplays[type], info);
264}
265
Mathias Agopiane57f2922012-08-09 16:29:12 -0700266sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700267 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700268 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
269 return NULL;
270 }
Jesse Hall692c7232012-11-08 15:41:56 -0800271 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700272}
273
Jamie Gennis9a78c902011-01-12 18:30:40 -0800274sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
275{
276 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
277 return gba;
278}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700279
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280void SurfaceFlinger::bootFinished()
281{
282 const nsecs_t now = systemTime();
283 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000284 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700285 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700286
287 // wait patiently for the window manager death
288 const String16 name("window");
289 sp<IBinder> window(defaultServiceManager()->getService(name));
290 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700291 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700292 }
293
294 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700295 // formerly we would just kill the process, but we now ask it to exit so it
296 // can choose where to stop the animation.
297 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298}
299
Mathias Agopian3f844832013-08-07 21:24:32 -0700300void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700301 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700302 RenderEngine& engine;
303 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700304 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700305 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
306 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700307 }
308 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700309 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700310 return true;
311 }
312 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700313 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700314}
315
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700316class DispSyncSource : public VSyncSource, private DispSync::Callback {
317public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700318 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
319 const char* label) :
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700320 mValue(0),
321 mPhaseOffset(phaseOffset),
322 mTraceVsync(traceVsync),
Andy McFadden5167ec62014-05-22 13:08:43 -0700323 mVsyncOnLabel(String8::format("VsyncOn-%s", label)),
324 mVsyncEventLabel(String8::format("VSYNC-%s", label)),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700325 mDispSync(dispSync) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700326
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700327 virtual ~DispSyncSource() {}
328
329 virtual void setVSyncEnabled(bool enable) {
330 // Do NOT lock the mutex here so as to avoid any mutex ordering issues
331 // with locking it in the onDispSyncEvent callback.
332 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700333 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700334 static_cast<DispSync::Callback*>(this));
335 if (err != NO_ERROR) {
336 ALOGE("error registering vsync callback: %s (%d)",
337 strerror(-err), err);
338 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700339 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700340 } else {
341 status_t err = mDispSync->removeEventListener(
342 static_cast<DispSync::Callback*>(this));
343 if (err != NO_ERROR) {
344 ALOGE("error unregistering vsync callback: %s (%d)",
345 strerror(-err), err);
346 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700347 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700348 }
349 }
350
351 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
352 Mutex::Autolock lock(mMutex);
353 mCallback = callback;
354 }
355
356private:
357 virtual void onDispSyncEvent(nsecs_t when) {
358 sp<VSyncSource::Callback> callback;
359 {
360 Mutex::Autolock lock(mMutex);
361 callback = mCallback;
362
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700363 if (mTraceVsync) {
364 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700365 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700366 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700367 }
368
369 if (callback != NULL) {
370 callback->onVSyncEvent(when);
371 }
372 }
373
374 int mValue;
375
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700376 const nsecs_t mPhaseOffset;
377 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700378 const String8 mVsyncOnLabel;
379 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700380
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700381 DispSync* mDispSync;
382 sp<VSyncSource::Callback> mCallback;
383 Mutex mMutex;
384};
385
386void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700387 ALOGI( "SurfaceFlinger's main thread ready to run. "
388 "Initializing graphics H/W...");
389
Jesse Hallb65f32e2013-09-27 22:10:47 -0700390 status_t err;
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
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700606int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
607 return getDisplayDevice(display)->getActiveConfig();
Dan Stoza7f7da322014-05-02 15:26:25 -0700608}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700609
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700610void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
611 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
612 this);
613 int32_t type = hw->getDisplayType();
614 int currentMode = hw->getActiveConfig();
615
616 if (mode == currentMode) {
617 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
618 return;
619 }
620
621 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
622 ALOGW("Trying to set config for virtual display");
623 return;
624 }
625
626 hw->setActiveConfig(mode);
627 getHwComposer().setActiveConfig(type, mode);
628}
629
630status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
631 class MessageSetActiveConfig: public MessageBase {
632 SurfaceFlinger& mFlinger;
633 sp<IBinder> mDisplay;
634 int mMode;
635 public:
636 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
637 int mode) :
638 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
639 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700640 Vector<DisplayInfo> configs;
641 mFlinger.getDisplayConfigs(mDisplay, &configs);
642 if(mMode < 0 || mMode >= configs.size()) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700643 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700644 mMode, configs.size());
645 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700646 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
647 if (hw == NULL) {
648 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700649 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700650 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
651 ALOGW("Attempt to set active config = %d for virtual display",
652 mMode);
653 } else {
654 mFlinger.setActiveConfigInternal(hw, mMode);
655 }
656 return true;
657 }
658 };
659 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
660 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700661 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700662}
663
Svetoslavd85084b2014-03-20 10:28:31 -0700664status_t SurfaceFlinger::clearAnimationFrameStats() {
665 Mutex::Autolock _l(mStateLock);
666 mAnimFrameTracker.clearStats();
667 return NO_ERROR;
668}
669
670status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
671 Mutex::Autolock _l(mStateLock);
672 mAnimFrameTracker.getStats(outStats);
673 return NO_ERROR;
674}
675
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800676// ----------------------------------------------------------------------------
677
678sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800679 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700680}
681
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800682// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800683
684void SurfaceFlinger::waitForEvent() {
685 mEventQueue.waitMessage();
686}
687
688void SurfaceFlinger::signalTransaction() {
689 mEventQueue.invalidate();
690}
691
692void SurfaceFlinger::signalLayerUpdate() {
693 mEventQueue.invalidate();
694}
695
696void SurfaceFlinger::signalRefresh() {
697 mEventQueue.refresh();
698}
699
700status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800701 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800702 return mEventQueue.postMessage(msg, reltime);
703}
704
705status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800706 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800707 status_t res = mEventQueue.postMessage(msg, reltime);
708 if (res == NO_ERROR) {
709 msg->wait();
710 }
711 return res;
712}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800713
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700714void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700715 do {
716 waitForEvent();
717 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800718}
719
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700720void SurfaceFlinger::enableHardwareVsync() {
721 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700722 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700723 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700724 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
725 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700726 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700727 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700728}
729
Jesse Hall948fe0c2013-10-14 12:56:09 -0700730void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700731 Mutex::Autolock _l(mHWVsyncLock);
732
Jesse Hall948fe0c2013-10-14 12:56:09 -0700733 if (makeAvailable) {
734 mHWVsyncAvailable = true;
735 } else if (!mHWVsyncAvailable) {
736 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
737 return;
738 }
739
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700740 const nsecs_t period =
741 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
742
743 mPrimaryDispSync.reset();
744 mPrimaryDispSync.setPeriod(period);
745
746 if (!mPrimaryHWVsyncEnabled) {
747 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700748 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
749 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700750 mPrimaryHWVsyncEnabled = true;
751 }
752}
753
Jesse Hall948fe0c2013-10-14 12:56:09 -0700754void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700755 Mutex::Autolock _l(mHWVsyncLock);
756 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700757 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
758 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700759 mPrimaryDispSync.endResync();
760 mPrimaryHWVsyncEnabled = false;
761 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700762 if (makeUnavailable) {
763 mHWVsyncAvailable = false;
764 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700765}
766
767void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700768 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700769
Jamie Gennisd1700752013-10-14 12:22:52 -0700770 { // Scope for the lock
771 Mutex::Autolock _l(mHWVsyncLock);
772 if (type == 0 && mPrimaryHWVsyncEnabled) {
773 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700774 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700775 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700776
777 if (needsHwVsync) {
778 enableHardwareVsync();
779 } else {
780 disableHardwareVsync(false);
781 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700782}
783
784void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
785 if (mEventThread == NULL) {
786 // This is a temporary workaround for b/7145521. A non-null pointer
787 // does not mean EventThread has finished initializing, so this
788 // is not a correct fix.
789 ALOGW("WARNING: EventThread not started, ignoring hotplug");
790 return;
791 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700792
Jesse Hall9e663de2013-08-16 14:28:37 -0700793 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700794 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800795 if (connected) {
796 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700797 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800798 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
799 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700800 }
801 setTransactionFlags(eDisplayTransactionNeeded);
802
Andy McFadden9e9689c2012-10-10 18:17:51 -0700803 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700804 }
Mathias Agopian86303202012-07-24 22:46:10 -0700805}
806
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700807void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700808 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700809 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700810}
811
Mathias Agopian4fec8732012-06-29 14:12:52 -0700812void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800813 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800814 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700815 case MessageQueue::TRANSACTION:
816 handleMessageTransaction();
817 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700818 case MessageQueue::INVALIDATE:
819 handleMessageTransaction();
820 handleMessageInvalidate();
821 signalRefresh();
822 break;
823 case MessageQueue::REFRESH:
824 handleMessageRefresh();
825 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800826 }
827}
828
Mathias Agopian4fec8732012-06-29 14:12:52 -0700829void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700830 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700831 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700832 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700833 }
834}
835
836void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700837 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700838 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700839}
840
841void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700842 ATRACE_CALL();
843 preComposition();
844 rebuildLayerStacks();
845 setUpHWComposer();
846 doDebugFlashRegions();
847 doComposition();
848 postComposition();
849}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700850
Mathias Agopiancd60f992012-08-16 16:28:27 -0700851void SurfaceFlinger::doDebugFlashRegions()
852{
853 // is debugging enabled
854 if (CC_LIKELY(!mDebugRegion))
855 return;
856
857 const bool repaintEverything = mRepaintEverything;
858 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
859 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700860 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700861 // transform the dirty region into this screen's coordinate space
862 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
863 if (!dirtyRegion.isEmpty()) {
864 // redraw the whole screen
865 doComposeSurfaces(hw, Region(hw->bounds()));
866
867 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700868 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700869 RenderEngine& engine(getRenderEngine());
870 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
871
Mathias Agopiancd60f992012-08-16 16:28:27 -0700872 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700873 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700874 }
875 }
876 }
877
878 postFramebuffer();
879
880 if (mDebugRegion > 1) {
881 usleep(mDebugRegion * 1000);
882 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700883
884 HWComposer& hwc(getHwComposer());
885 if (hwc.initCheck() == NO_ERROR) {
886 status_t err = hwc.prepare();
887 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
888 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700889}
890
891void SurfaceFlinger::preComposition()
892{
893 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700894 const LayerVector& layers(mDrawingState.layersSortedByZ);
895 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700896 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700897 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700898 needExtraInvalidate = true;
899 }
900 }
901 if (needExtraInvalidate) {
902 signalLayerUpdate();
903 }
904}
905
906void SurfaceFlinger::postComposition()
907{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700908 const LayerVector& layers(mDrawingState.layersSortedByZ);
909 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700910 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700911 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700912 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800913
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700914 const HWComposer& hwc = getHwComposer();
915 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
916
917 if (presentFence->isValid()) {
918 if (mPrimaryDispSync.addPresentFence(presentFence)) {
919 enableHardwareVsync();
920 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700921 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700922 }
923 }
924
Andy McFadden5167ec62014-05-22 13:08:43 -0700925 if (kIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700926 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700927 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700928 enableHardwareVsync();
929 }
930 }
931
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800932 if (mAnimCompositionPending) {
933 mAnimCompositionPending = false;
934
Jesse Halla9a1b002013-02-27 16:39:25 -0800935 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800936 mAnimFrameTracker.setActualPresentFence(presentFence);
937 } else {
938 // The HWC doesn't support present fences, so use the refresh
939 // timestamp instead.
940 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
941 mAnimFrameTracker.setActualPresentTime(presentTime);
942 }
943 mAnimFrameTracker.advanceFrame();
944 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700945}
946
947void SurfaceFlinger::rebuildLayerStacks() {
948 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700949 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700950 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700951 mVisibleRegionsDirty = false;
952 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700953
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700954 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700955 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700956 Region opaqueRegion;
957 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800958 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700959 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700960 const Transform& tr(hw->getTransform());
961 const Rect bounds(hw->getBounds());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700962 if (hw->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700963 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700964 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700965
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700966 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700967 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700968 const sp<Layer>& layer(layers[i]);
969 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700970 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700971 Region drawRegion(tr.transform(
972 layer->visibleNonTransparentRegion));
973 drawRegion.andSelf(bounds);
974 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700975 layersSortedByZ.add(layer);
976 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700977 }
978 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700979 }
Mathias Agopian42977342012-08-05 00:40:46 -0700980 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700981 hw->undefinedRegion.set(bounds);
982 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
983 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700984 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700985 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700986}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700987
Mathias Agopiancd60f992012-08-16 16:28:27 -0700988void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700989 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Dan Stoza71433162014-02-04 16:22:36 -0800990 bool mustRecompose =
991 !(mDisplays[dpy]->getDirtyRegion(false).isEmpty());
992 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700993 }
994
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700995 HWComposer& hwc(getHwComposer());
996 if (hwc.initCheck() == NO_ERROR) {
997 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700998 if (CC_UNLIKELY(mHwWorkListDirty)) {
999 mHwWorkListDirty = false;
1000 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1001 sp<const DisplayDevice> hw(mDisplays[dpy]);
1002 const int32_t id = hw->getHwcDisplayId();
1003 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001004 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001005 hw->getVisibleLayersSortedByZ());
1006 const size_t count = currentLayers.size();
1007 if (hwc.createWorkList(id, count) == NO_ERROR) {
1008 HWComposer::LayerListIterator cur = hwc.begin(id);
1009 const HWComposer::LayerListIterator end = hwc.end(id);
1010 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001011 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001012 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001013 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001014 cur->setSkip(true);
1015 }
1016 }
1017 }
1018 }
1019 }
1020 }
1021
1022 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001023 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001024 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001025 const int32_t id = hw->getHwcDisplayId();
1026 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001027 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001028 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001029 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001030 HWComposer::LayerListIterator cur = hwc.begin(id);
1031 const HWComposer::LayerListIterator end = hwc.end(id);
1032 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1033 /*
1034 * update the per-frame h/w composer data for each layer
1035 * and build the transparent region of the FB
1036 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001037 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001038 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -07001039 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001040 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001041 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001042
Riley Andrews03414a12014-07-01 14:22:59 -07001043 // If possible, attempt to use the cursor overlay on each display.
1044 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1045 sp<const DisplayDevice> hw(mDisplays[dpy]);
1046 const int32_t id = hw->getHwcDisplayId();
1047 if (id >= 0) {
1048 const Vector< sp<Layer> >& currentLayers(
1049 hw->getVisibleLayersSortedByZ());
1050 const size_t count = currentLayers.size();
1051 HWComposer::LayerListIterator cur = hwc.begin(id);
1052 const HWComposer::LayerListIterator end = hwc.end(id);
1053 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1054 const sp<Layer>& layer(currentLayers[i]);
1055 if (layer->isPotentialCursor()) {
1056 cur->setIsCursorLayerHint();
1057 break;
1058 }
1059 }
1060 }
1061 }
1062
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001063 status_t err = hwc.prepare();
1064 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001065
1066 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1067 sp<const DisplayDevice> hw(mDisplays[dpy]);
1068 hw->prepareFrame(hwc);
1069 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001070 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001071}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001072
Mathias Agopiancd60f992012-08-16 16:28:27 -07001073void SurfaceFlinger::doComposition() {
1074 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001075 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001076 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001077 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001078 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001079 // transform the dirty region into this screen's coordinate space
1080 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001081
1082 // repaint the framebuffer (if needed)
1083 doDisplayComposition(hw, dirtyRegion);
1084
Mathias Agopiancd60f992012-08-16 16:28:27 -07001085 hw->dirtyRegion.clear();
1086 hw->flip(hw->swapRegion);
1087 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001088 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001089 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001090 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001091 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001092 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001093}
1094
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095void SurfaceFlinger::postFramebuffer()
1096{
Mathias Agopian841cde52012-03-01 15:44:37 -08001097 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001098
Mathias Agopiana44b0412011-10-16 18:46:35 -07001099 const nsecs_t now = systemTime();
1100 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001101
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001102 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001103 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001104 if (!hwc.supportsFramebufferTarget()) {
1105 // EGL spec says:
1106 // "surface must be bound to the calling thread's current context,
1107 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001108 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001109 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001110 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001111 }
1112
Mathias Agopian6da15f42013-09-25 20:40:07 -07001113 // make the default display current because the VirtualDisplayDevice code cannot
1114 // deal with dequeueBuffer() being called outside of the composition loop; however
1115 // the code below can call glFlush() which is allowed (and does in some case) call
1116 // dequeueBuffer().
1117 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1118
Mathias Agopian92a979a2012-08-02 18:32:23 -07001119 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001120 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001121 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001122 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001123 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001124 int32_t id = hw->getHwcDisplayId();
1125 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001126 HWComposer::LayerListIterator cur = hwc.begin(id);
1127 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001128 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001129 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001130 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001131 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001132 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001133 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001134 }
Jesse Hallef194142012-06-14 14:45:17 -07001135 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001136 }
1137
Mathias Agopiana44b0412011-10-16 18:46:35 -07001138 mLastSwapBufferTime = systemTime() - now;
1139 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001140
1141 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1142 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1143 logFrameStats();
1144 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001145}
1146
Mathias Agopian87baae12012-07-31 12:38:26 -07001147void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001148{
Mathias Agopian841cde52012-03-01 15:44:37 -08001149 ATRACE_CALL();
1150
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001151 // here we keep a copy of the drawing state (that is the state that's
1152 // going to be overwritten by handleTransactionLocked()) outside of
1153 // mStateLock so that the side-effects of the State assignment
1154 // don't happen with mStateLock held (which can cause deadlocks).
1155 State drawingState(mDrawingState);
1156
Mathias Agopianca4d3602011-05-19 15:38:14 -07001157 Mutex::Autolock _l(mStateLock);
1158 const nsecs_t now = systemTime();
1159 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001160
Mathias Agopianca4d3602011-05-19 15:38:14 -07001161 // Here we're guaranteed that some transaction flags are set
1162 // so we can call handleTransactionLocked() unconditionally.
1163 // We call getTransactionFlags(), which will also clear the flags,
1164 // with mStateLock held to guarantee that mCurrentState won't change
1165 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001166
Mathias Agopiane57f2922012-08-09 16:29:12 -07001167 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001168 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001169
Mathias Agopianca4d3602011-05-19 15:38:14 -07001170 mLastTransactionTime = systemTime() - now;
1171 mDebugInTransaction = 0;
1172 invalidateHwcGeometry();
1173 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001174}
1175
Mathias Agopian87baae12012-07-31 12:38:26 -07001176void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001177{
1178 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001179 const size_t count = currentLayers.size();
1180
1181 /*
1182 * Traversal of the children
1183 * (perform the transaction for each of them if needed)
1184 */
1185
Mathias Agopian3559b072012-08-15 13:46:03 -07001186 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001188 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1190 if (!trFlags) continue;
1191
1192 const uint32_t flags = layer->doTransaction(0);
1193 if (flags & Layer::eVisibleRegion)
1194 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195 }
1196 }
1197
1198 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001199 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200 */
1201
Mathias Agopiane57f2922012-08-09 16:29:12 -07001202 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001203 // here we take advantage of Vector's copy-on-write semantics to
1204 // improve performance by skipping the transaction entirely when
1205 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001206 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1207 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001208 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001209 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001210 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001211 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001212
1213 // find the displays that were removed
1214 // (ie: in drawing state but not in current state)
1215 // also handle displays that changed
1216 // (ie: displays that are in both lists)
1217 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001218 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1219 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001220 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001221 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001222 // Call makeCurrent() on the primary display so we can
1223 // be sure that nothing associated with this display
1224 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001225 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001226 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001227 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1228 if (hw != NULL)
1229 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001230 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001231 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001232 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001233 } else {
1234 ALOGW("trying to remove the main display");
1235 }
1236 } else {
1237 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001238 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001239 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001240 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001241 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001242 // recreating the DisplayDevice, so we just remove it
1243 // from the drawing state, so that it get re-added
1244 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001245 sp<DisplayDevice> hw(getDisplayDevice(display));
1246 if (hw != NULL)
1247 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001248 mDisplays.removeItem(display);
1249 mDrawingState.displays.removeItemsAt(i);
1250 dc--; i--;
1251 // at this point we must loop to the next item
1252 continue;
1253 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001254
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001255 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001256 if (disp != NULL) {
1257 if (state.layerStack != draw[i].layerStack) {
1258 disp->setLayerStack(state.layerStack);
1259 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001260 if ((state.orientation != draw[i].orientation)
1261 || (state.viewport != draw[i].viewport)
1262 || (state.frame != draw[i].frame))
1263 {
1264 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001265 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001266 }
Michael Lentine47e45402014-07-18 15:34:25 -07001267 if (state.width != draw[i].width || state.height != draw[i].height) {
1268 disp->setDisplaySize(state.width, state.height);
1269 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001270 }
1271 }
1272 }
1273
1274 // find displays that were added
1275 // (ie: in current state but not in drawing state)
1276 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001277 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001278 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001279
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001280 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001281 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001282 sp<IGraphicBufferProducer> bqProducer;
1283 sp<IGraphicBufferConsumer> bqConsumer;
1284 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1285 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001286
Jesse Hall02d86562013-03-25 14:43:23 -07001287 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001288 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001289 // Virtual displays without a surface are dormant:
1290 // they have external state (layer stack, projection,
1291 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001292 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001293
Jesse Hall02d86562013-03-25 14:43:23 -07001294 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001295 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
Dan Stozab9b08832014-03-13 11:55:57 -07001296 *mHwc, hwcDisplayId, state.surface,
1297 bqProducer, bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001298
1299 dispSurface = vds;
Michael Lentine47e45402014-07-18 15:34:25 -07001300 producer = vds;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001301 }
1302 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001303 ALOGE_IF(state.surface!=NULL,
1304 "adding a supported display, but rendering "
1305 "surface is provided (%p), ignoring it",
1306 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001307 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001308 // for supported (by hwc) displays we provide our
1309 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001310 dispSurface = new FramebufferSurface(*mHwc, state.type,
1311 bqConsumer);
1312 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001313 }
1314
1315 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001316 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001317 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001318 state.type, hwcDisplayId,
1319 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001320 display, dispSurface, producer,
1321 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001322 hw->setLayerStack(state.layerStack);
1323 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001324 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001325 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001326 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001327 if (state.isVirtualDisplay()) {
1328 if (hwcDisplayId >= 0) {
1329 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1330 hw->getWidth(), hw->getHeight(),
1331 hw->getFormat());
1332 }
1333 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001334 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001335 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001336 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001337 }
1338 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001339 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001340 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001341
Mathias Agopian84300952012-11-21 16:02:13 -08001342 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1343 // The transform hint might have changed for some layers
1344 // (either because a display has changed, or because a layer
1345 // as changed).
1346 //
1347 // Walk through all the layers in currentLayers,
1348 // and update their transform hint.
1349 //
1350 // If a layer is visible only on a single display, then that
1351 // display is used to calculate the hint, otherwise we use the
1352 // default display.
1353 //
1354 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1355 // the hint is set before we acquire a buffer from the surface texture.
1356 //
1357 // NOTE: layer transactions have taken place already, so we use their
1358 // drawing state. However, SurfaceFlinger's own transaction has not
1359 // happened yet, so we must use the current state layer list
1360 // (soon to become the drawing state list).
1361 //
1362 sp<const DisplayDevice> disp;
1363 uint32_t currentlayerStack = 0;
1364 for (size_t i=0; i<count; i++) {
1365 // NOTE: we rely on the fact that layers are sorted by
1366 // layerStack first (so we don't have to traverse the list
1367 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001368 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001369 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001370 if (i==0 || currentlayerStack != layerStack) {
1371 currentlayerStack = layerStack;
1372 // figure out if this layerstack is mirrored
1373 // (more than one display) if so, pick the default display,
1374 // if not, pick the only display it's on.
1375 disp.clear();
1376 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1377 sp<const DisplayDevice> hw(mDisplays[dpy]);
1378 if (hw->getLayerStack() == currentlayerStack) {
1379 if (disp == NULL) {
1380 disp = hw;
1381 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001382 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001383 break;
1384 }
1385 }
1386 }
1387 }
Chet Haase91d25932013-04-11 15:24:55 -07001388 if (disp == NULL) {
1389 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1390 // redraw after transform hint changes. See bug 8508397.
1391
1392 // could be null when this layer is using a layerStack
1393 // that is not visible on any display. Also can occur at
1394 // screen off/on times.
1395 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001396 }
Chet Haase91d25932013-04-11 15:24:55 -07001397 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001398 }
1399 }
1400
1401
Mathias Agopian3559b072012-08-15 13:46:03 -07001402 /*
1403 * Perform our own transaction if needed
1404 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001405
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001406 const LayerVector& layers(mDrawingState.layersSortedByZ);
1407 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001408 // layers have been added
1409 mVisibleRegionsDirty = true;
1410 }
1411
1412 // some layers might have been removed, so
1413 // we need to update the regions they're exposing.
1414 if (mLayersRemoved) {
1415 mLayersRemoved = false;
1416 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001417 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001418 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001419 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001420 if (currentLayers.indexOf(layer) < 0) {
1421 // this layer is not visible anymore
1422 // TODO: we could traverse the tree from front to back and
1423 // compute the actual visible region
1424 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001425 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001426 Region visibleReg = s.transform.transform(
1427 Region(Rect(s.active.w, s.active.h)));
1428 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001429 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001430 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431 }
1432
1433 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001434
1435 updateCursorAsync();
1436}
1437
1438void SurfaceFlinger::updateCursorAsync()
1439{
1440 HWComposer& hwc(getHwComposer());
1441 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1442 sp<const DisplayDevice> hw(mDisplays[dpy]);
1443 const int32_t id = hw->getHwcDisplayId();
1444 if (id < 0) {
1445 continue;
1446 }
1447 const Vector< sp<Layer> >& currentLayers(
1448 hw->getVisibleLayersSortedByZ());
1449 const size_t count = currentLayers.size();
1450 HWComposer::LayerListIterator cur = hwc.begin(id);
1451 const HWComposer::LayerListIterator end = hwc.end(id);
1452 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1453 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1454 continue;
1455 }
1456 const sp<Layer>& layer(currentLayers[i]);
1457 Rect cursorPos = layer->getPosition(hw);
1458 hwc.setCursorPositionAsync(id, cursorPos);
1459 break;
1460 }
1461 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001462}
1463
1464void SurfaceFlinger::commitTransaction()
1465{
1466 if (!mLayersPendingRemoval.isEmpty()) {
1467 // Notify removed layers now that they can't be drawn from
1468 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1469 mLayersPendingRemoval[i]->onRemoved();
1470 }
1471 mLayersPendingRemoval.clear();
1472 }
1473
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001474 // If this transaction is part of a window animation then the next frame
1475 // we composite should be considered an animation as well.
1476 mAnimCompositionPending = mAnimTransactionPending;
1477
Mathias Agopian4fec8732012-06-29 14:12:52 -07001478 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001479 mTransactionPending = false;
1480 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001481 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001482}
1483
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001484void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001485 const LayerVector& currentLayers, uint32_t layerStack,
1486 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001487{
Mathias Agopian841cde52012-03-01 15:44:37 -08001488 ATRACE_CALL();
1489
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001490 Region aboveOpaqueLayers;
1491 Region aboveCoveredLayers;
1492 Region dirty;
1493
Mathias Agopian87baae12012-07-31 12:38:26 -07001494 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495
1496 size_t i = currentLayers.size();
1497 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001498 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001499
1500 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001501 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001502
Jesse Hall01e29052013-02-19 16:13:35 -08001503 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001504 if (s.layerStack != layerStack)
1505 continue;
1506
Mathias Agopianab028732010-03-16 16:41:46 -07001507 /*
1508 * opaqueRegion: area of a surface that is fully opaque.
1509 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001510 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001511
1512 /*
1513 * visibleRegion: area of a surface that is visible on screen
1514 * and not fully transparent. This is essentially the layer's
1515 * footprint minus the opaque regions above it.
1516 * Areas covered by a translucent surface are considered visible.
1517 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001519
1520 /*
1521 * coveredRegion: area of a surface that is covered by all
1522 * visible regions above it (which includes the translucent areas).
1523 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001524 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001525
Jesse Halla8026d22012-09-25 13:25:04 -07001526 /*
1527 * transparentRegion: area of a surface that is hinted to be completely
1528 * transparent. This is only used to tell when the layer has no visible
1529 * non-transparent regions and can be removed from the layer list. It
1530 * does not affect the visibleRegion of this layer or any layers
1531 * beneath it. The hint may not be correct if apps don't respect the
1532 * SurfaceView restrictions (which, sadly, some don't).
1533 */
1534 Region transparentRegion;
1535
Mathias Agopianab028732010-03-16 16:41:46 -07001536
1537 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001538 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001539 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001540 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001541 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001542 if (!visibleRegion.isEmpty()) {
1543 // Remove the transparent area from the visible region
1544 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001545 const Transform tr(s.transform);
1546 if (tr.transformed()) {
1547 if (tr.preserveRects()) {
1548 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001549 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001550 } else {
1551 // transformation too complex, can't do the
1552 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001553 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001554 }
1555 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001556 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001557 }
Mathias Agopianab028732010-03-16 16:41:46 -07001558 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001559
Mathias Agopianab028732010-03-16 16:41:46 -07001560 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001561 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001562 if (s.alpha==255 && !translucent &&
1563 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1564 // the opaque region is the layer's footprint
1565 opaqueRegion = visibleRegion;
1566 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001567 }
1568 }
1569
Mathias Agopianab028732010-03-16 16:41:46 -07001570 // Clip the covered region to the visible region
1571 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1572
1573 // Update aboveCoveredLayers for next (lower) layer
1574 aboveCoveredLayers.orSelf(visibleRegion);
1575
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001576 // subtract the opaque region covered by the layers above us
1577 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001578
1579 // compute this layer's dirty region
1580 if (layer->contentDirty) {
1581 // we need to invalidate the whole region
1582 dirty = visibleRegion;
1583 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001584 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001585 layer->contentDirty = false;
1586 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001587 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001588 * the exposed region consists of two components:
1589 * 1) what's VISIBLE now and was COVERED before
1590 * 2) what's EXPOSED now less what was EXPOSED before
1591 *
1592 * note that (1) is conservative, we start with the whole
1593 * visible region but only keep what used to be covered by
1594 * something -- which mean it may have been exposed.
1595 *
1596 * (2) handles areas that were not covered by anything but got
1597 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001598 */
Mathias Agopianab028732010-03-16 16:41:46 -07001599 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001600 const Region oldVisibleRegion = layer->visibleRegion;
1601 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001602 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1603 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001604 }
1605 dirty.subtractSelf(aboveOpaqueLayers);
1606
1607 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001608 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001609
Mathias Agopianab028732010-03-16 16:41:46 -07001610 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001611 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001612
Jesse Halla8026d22012-09-25 13:25:04 -07001613 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001614 layer->setVisibleRegion(visibleRegion);
1615 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001616 layer->setVisibleNonTransparentRegion(
1617 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001618 }
1619
Mathias Agopian87baae12012-07-31 12:38:26 -07001620 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001621}
1622
Mathias Agopian87baae12012-07-31 12:38:26 -07001623void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1624 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001625 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001626 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1627 if (hw->getLayerStack() == layerStack) {
1628 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001629 }
1630 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001631}
1632
1633void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001634{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001635 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001636
Mathias Agopian4fec8732012-06-29 14:12:52 -07001637 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001638 const LayerVector& layers(mDrawingState.layersSortedByZ);
Eric Penner51c59cd2014-07-28 19:51:58 -07001639
1640 // Store the set of layers that need updates. This set must not change as
1641 // buffers are being latched, as this could result in a deadlock.
1642 // Example: Two producers share the same command stream and:
1643 // 1.) Layer 0 is latched
1644 // 2.) Layer 0 gets a new frame
1645 // 2.) Layer 1 gets a new frame
1646 // 3.) Layer 1 is latched.
1647 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1648 // second frame. But layer 0's second frame could be waiting on display.
1649 Vector<Layer*> layersWithQueuedFrames;
1650 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001651 const sp<Layer>& layer(layers[i]);
Eric Penner51c59cd2014-07-28 19:51:58 -07001652 if (layer->hasQueuedFrame())
1653 layersWithQueuedFrames.push_back(layer.get());
1654 }
1655 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
1656 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07001657 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001658 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001659 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001660 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001661
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001662 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663}
1664
Mathias Agopianad456f92011-01-13 17:53:01 -08001665void SurfaceFlinger::invalidateHwcGeometry()
1666{
1667 mHwWorkListDirty = true;
1668}
1669
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001670
Mathias Agopiancd60f992012-08-16 16:28:27 -07001671void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001672 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001673{
Dan Stoza71433162014-02-04 16:22:36 -08001674 // We only need to actually compose the display if:
1675 // 1) It is being handled by hardware composer, which may need this to
1676 // keep its virtual display state machine in sync, or
1677 // 2) There is work to be done (the dirty region isn't empty)
1678 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1679 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1680 return;
1681 }
1682
Mathias Agopian87baae12012-07-31 12:38:26 -07001683 Region dirtyRegion(inDirtyRegion);
1684
Mathias Agopianb8a55602009-06-26 19:06:36 -07001685 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001686 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687
Mathias Agopian42977342012-08-05 00:40:46 -07001688 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001689 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001690 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1691 // takes a rectangle, we must make sure to update that whole
1692 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001693 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001694 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001695 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001696 // We need to redraw the rectangle that will be updated
1697 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001698 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001699 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001700 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001701 } else {
1702 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001703 dirtyRegion.set(hw->bounds());
1704 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705 }
1706 }
1707
Alan Viverette9c5a3332013-09-12 20:04:35 -07001708 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Mathias Agopianff2ed702013-09-01 21:36:12 -07001709 doComposeSurfaces(hw, dirtyRegion);
1710 } else {
1711 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001712 mat4 colorMatrix = mColorMatrix;
1713 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001714 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001715 }
1716 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001717 doComposeSurfaces(hw, dirtyRegion);
1718 engine.endGroup();
1719 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001720
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001721 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001722 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001723
1724 // swap buffers (presentation)
1725 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001726}
1727
Mathias Agopiancd60f992012-08-16 16:28:27 -07001728void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001729{
Mathias Agopian3f844832013-08-07 21:24:32 -07001730 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001731 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001732 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001733 HWComposer::LayerListIterator cur = hwc.begin(id);
1734 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001735
Jesse Halld05a17f2013-09-30 16:49:30 -07001736 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001737 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001738 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001739 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1740 hw->getDisplayName().string());
1741 return;
1742 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001743
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001744 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001745 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001746 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001747 // when using overlays, we assume a fully transparent framebuffer
1748 // NOTE: we could reduce how much we need to clear, for instance
1749 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001750 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001751 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001752 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001753 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001754 // we start with the whole screen area
1755 const Region bounds(hw->getBounds());
1756
1757 // we remove the scissor part
1758 // we're left with the letterbox region
1759 // (common case is that letterbox ends-up being empty)
1760 const Region letterbox(bounds.subtract(hw->getScissor()));
1761
1762 // compute the area to clear
1763 Region region(hw->undefinedRegion.merge(letterbox));
1764
1765 // but limit it to the dirty region
1766 region.andSelf(dirty);
1767
Mathias Agopianb9494d52012-04-18 02:28:45 -07001768 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001769 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001770 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001771 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001772 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001773 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001774
Mathias Agopian766dc492012-10-30 18:08:06 -07001775 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1776 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001777 // scissor on the main display. It should never be needed
1778 // anyways (though in theory it could since the API allows it).
1779 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001780 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001781 if (scissor != bounds) {
1782 // scissor doesn't match the screen's dimensions, so we
1783 // need to clear everything outside of it and enable
1784 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001785
Mathias Agopianf45c5102012-10-24 16:29:17 -07001786 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001787 const uint32_t height = hw->getHeight();
1788 engine.setScissor(scissor.left, height - scissor.bottom,
1789 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001790 }
1791 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001792 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001793
Mathias Agopian85d751c2012-08-29 16:59:24 -07001794 /*
1795 * and then, render the layers targeted at the framebuffer
1796 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001797
Mathias Agopian13127d82013-03-05 17:47:11 -08001798 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001799 const size_t count = layers.size();
1800 const Transform& tr = hw->getTransform();
1801 if (cur != end) {
1802 // we're using h/w composer
1803 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001804 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001805 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001806 if (!clip.isEmpty()) {
1807 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07001808 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07001809 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001810 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001811 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1812 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001813 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001814 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001815 // never clear the very first layer since we're
1816 // guaranteed the FB is already cleared
1817 layer->clearWithOpenGL(hw, clip);
1818 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001819 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001820 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001821 case HWC_FRAMEBUFFER: {
1822 layer->draw(hw, clip);
1823 break;
1824 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001825 case HWC_FRAMEBUFFER_TARGET: {
1826 // this should not happen as the iterator shouldn't
1827 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001828 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001829 break;
1830 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001831 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001832 }
1833 layer->setAcquireFence(hw, *cur);
1834 }
1835 } else {
1836 // we're not using h/w composer
1837 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001838 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001839 const Region clip(dirty.intersect(
1840 tr.transform(layer->visibleRegion)));
1841 if (!clip.isEmpty()) {
1842 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001843 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001844 }
1845 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001846
1847 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001848 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001849}
1850
Mathias Agopian3f844832013-08-07 21:24:32 -07001851void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001852 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001853 RenderEngine& engine(getRenderEngine());
1854 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001855}
1856
Mathias Agopianac9fa422013-02-11 16:40:36 -08001857void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1858 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001859 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001860 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001861{
Mathias Agopian96f08192010-06-02 23:28:45 -07001862 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001863 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001864
Mathias Agopian96f08192010-06-02 23:28:45 -07001865 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001866 Mutex::Autolock _l(mStateLock);
1867 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001868 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001869}
1870
Mathias Agopian3f844832013-08-07 21:24:32 -07001871status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001872 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001873 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001874 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001875 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001876 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001877 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001878 return NO_ERROR;
1879 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001880 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001881}
1882
Dan Stozac7014012014-02-14 15:03:43 -08001883uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001884 return android_atomic_release_load(&mTransactionFlags);
1885}
1886
Mathias Agopian3f844832013-08-07 21:24:32 -07001887uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001888 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1889}
1890
Mathias Agopian3f844832013-08-07 21:24:32 -07001891uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001892 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1893 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001894 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001895 }
1896 return old;
1897}
1898
Mathias Agopian8b33f032012-07-24 20:43:54 -07001899void SurfaceFlinger::setTransactionState(
1900 const Vector<ComposerState>& state,
1901 const Vector<DisplayState>& displays,
1902 uint32_t flags)
1903{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001904 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001905 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001906 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001907
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001908 if (flags & eAnimation) {
1909 // For window updates that are part of an animation we must wait for
1910 // previous animation "frames" to be handled.
1911 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001912 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001913 if (CC_UNLIKELY(err != NO_ERROR)) {
1914 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001915 // caller after a few seconds.
1916 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1917 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001918 mAnimTransactionPending = false;
1919 break;
1920 }
1921 }
1922 }
1923
Mathias Agopiane57f2922012-08-09 16:29:12 -07001924 size_t count = displays.size();
1925 for (size_t i=0 ; i<count ; i++) {
1926 const DisplayState& s(displays[i]);
1927 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001928 }
1929
Mathias Agopiane57f2922012-08-09 16:29:12 -07001930 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001931 for (size_t i=0 ; i<count ; i++) {
1932 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001933 // Here we need to check that the interface we're given is indeed
1934 // one of our own. A malicious client could give us a NULL
1935 // IInterface, or one of its own or even one of our own but a
1936 // different type. All these situations would cause us to crash.
1937 //
1938 // NOTE: it would be better to use RTTI as we could directly check
1939 // that we have a Client*. however, RTTI is disabled in Android.
1940 if (s.client != NULL) {
1941 sp<IBinder> binder = s.client->asBinder();
1942 if (binder != NULL) {
1943 String16 desc(binder->getInterfaceDescriptor());
1944 if (desc == ISurfaceComposerClient::descriptor) {
1945 sp<Client> client( static_cast<Client *>(s.client.get()) );
1946 transactionFlags |= setClientStateLocked(client, s.state);
1947 }
1948 }
1949 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001950 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001951
Mathias Agopian386aa982011-11-07 21:58:03 -08001952 if (transactionFlags) {
1953 // this triggers the transaction
1954 setTransactionFlags(transactionFlags);
1955
1956 // if this is a synchronous transaction, wait for it to take effect
1957 // before returning.
1958 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001959 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001960 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001961 if (flags & eAnimation) {
1962 mAnimTransactionPending = true;
1963 }
1964 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001965 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1966 if (CC_UNLIKELY(err != NO_ERROR)) {
1967 // just in case something goes wrong in SF, return to the
1968 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001969 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1970 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001971 break;
1972 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001973 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001974 }
1975}
1976
Mathias Agopiane57f2922012-08-09 16:29:12 -07001977uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1978{
Jesse Hall9a143922012-10-04 16:29:19 -07001979 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1980 if (dpyIdx < 0)
1981 return 0;
1982
Mathias Agopiane57f2922012-08-09 16:29:12 -07001983 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001984 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001985 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001986 const uint32_t what = s.what;
1987 if (what & DisplayState::eSurfaceChanged) {
1988 if (disp.surface->asBinder() != s.surface->asBinder()) {
1989 disp.surface = s.surface;
1990 flags |= eDisplayTransactionNeeded;
1991 }
1992 }
1993 if (what & DisplayState::eLayerStackChanged) {
1994 if (disp.layerStack != s.layerStack) {
1995 disp.layerStack = s.layerStack;
1996 flags |= eDisplayTransactionNeeded;
1997 }
1998 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001999 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002000 if (disp.orientation != s.orientation) {
2001 disp.orientation = s.orientation;
2002 flags |= eDisplayTransactionNeeded;
2003 }
2004 if (disp.frame != s.frame) {
2005 disp.frame = s.frame;
2006 flags |= eDisplayTransactionNeeded;
2007 }
2008 if (disp.viewport != s.viewport) {
2009 disp.viewport = s.viewport;
2010 flags |= eDisplayTransactionNeeded;
2011 }
2012 }
Michael Lentine47e45402014-07-18 15:34:25 -07002013 if (what & DisplayState::eDisplaySizeChanged) {
2014 if (disp.width != s.width) {
2015 disp.width = s.width;
2016 flags |= eDisplayTransactionNeeded;
2017 }
2018 if (disp.height != s.height) {
2019 disp.height = s.height;
2020 flags |= eDisplayTransactionNeeded;
2021 }
2022 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002023 }
2024 return flags;
2025}
2026
2027uint32_t SurfaceFlinger::setClientStateLocked(
2028 const sp<Client>& client,
2029 const layer_state_t& s)
2030{
2031 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002032 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002033 if (layer != 0) {
2034 const uint32_t what = s.what;
2035 if (what & layer_state_t::ePositionChanged) {
2036 if (layer->setPosition(s.x, s.y))
2037 flags |= eTraversalNeeded;
2038 }
2039 if (what & layer_state_t::eLayerChanged) {
2040 // NOTE: index needs to be calculated before we update the state
2041 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2042 if (layer->setLayer(s.z)) {
2043 mCurrentState.layersSortedByZ.removeAt(idx);
2044 mCurrentState.layersSortedByZ.add(layer);
2045 // we need traversal (state changed)
2046 // AND transaction (list changed)
2047 flags |= eTransactionNeeded|eTraversalNeeded;
2048 }
2049 }
2050 if (what & layer_state_t::eSizeChanged) {
2051 if (layer->setSize(s.w, s.h)) {
2052 flags |= eTraversalNeeded;
2053 }
2054 }
2055 if (what & layer_state_t::eAlphaChanged) {
2056 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2057 flags |= eTraversalNeeded;
2058 }
2059 if (what & layer_state_t::eMatrixChanged) {
2060 if (layer->setMatrix(s.matrix))
2061 flags |= eTraversalNeeded;
2062 }
2063 if (what & layer_state_t::eTransparentRegionChanged) {
2064 if (layer->setTransparentRegionHint(s.transparentRegion))
2065 flags |= eTraversalNeeded;
2066 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08002067 if ((what & layer_state_t::eVisibilityChanged) ||
2068 (what & layer_state_t::eOpacityChanged)) {
2069 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07002070 if (layer->setFlags(s.flags, s.mask))
2071 flags |= eTraversalNeeded;
2072 }
2073 if (what & layer_state_t::eCropChanged) {
2074 if (layer->setCrop(s.crop))
2075 flags |= eTraversalNeeded;
2076 }
2077 if (what & layer_state_t::eLayerStackChanged) {
2078 // NOTE: index needs to be calculated before we update the state
2079 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2080 if (layer->setLayerStack(s.layerStack)) {
2081 mCurrentState.layersSortedByZ.removeAt(idx);
2082 mCurrentState.layersSortedByZ.add(layer);
2083 // we need traversal (state changed)
2084 // AND transaction (list changed)
2085 flags |= eTransactionNeeded|eTraversalNeeded;
2086 }
2087 }
2088 }
2089 return flags;
2090}
2091
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002092status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002093 const String8& name,
2094 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002095 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2096 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002097{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002098 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002099 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002100 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002101 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002102 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002103 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002104
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002105 status_t result = NO_ERROR;
2106
2107 sp<Layer> layer;
2108
Mathias Agopian3165cc22012-08-08 19:42:09 -07002109 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2110 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002111 result = createNormalLayer(client,
2112 name, w, h, flags, format,
2113 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002114 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002115 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002116 result = createDimLayer(client,
2117 name, w, h, flags,
2118 handle, gbp, &layer);
2119 break;
2120 default:
2121 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002122 break;
2123 }
2124
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002125 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002126 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002127 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002128 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002129 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002130}
2131
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002132status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2133 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2134 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002135{
2136 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002137 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002138 case PIXEL_FORMAT_TRANSPARENT:
2139 case PIXEL_FORMAT_TRANSLUCENT:
2140 format = PIXEL_FORMAT_RGBA_8888;
2141 break;
2142 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002143 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002144 break;
2145 }
2146
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002147 *outLayer = new Layer(this, client, name, w, h, flags);
2148 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2149 if (err == NO_ERROR) {
2150 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002151 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002152 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002153
2154 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2155 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002156}
2157
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002158status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2159 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2160 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002161{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002162 *outLayer = new LayerDim(this, client, name, w, h, flags);
2163 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002164 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002165 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002166}
2167
Mathias Agopianac9fa422013-02-11 16:40:36 -08002168status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002169{
Mathias Agopian67106042013-03-14 19:18:13 -07002170 // called by the window manager when it wants to remove a Layer
2171 status_t err = NO_ERROR;
2172 sp<Layer> l(client->getLayerUser(handle));
2173 if (l != NULL) {
2174 err = removeLayer(l);
2175 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2176 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002177 }
2178 return err;
2179}
2180
Mathias Agopian13127d82013-03-05 17:47:11 -08002181status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002182{
Mathias Agopian67106042013-03-14 19:18:13 -07002183 // called by ~LayerCleaner() when all references to the IBinder (handle)
2184 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002185 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002186 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002187 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002188 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002189 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002190 "error removing layer=%p (%s)", l.get(), strerror(-err));
2191 }
2192 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002193}
2194
Mathias Agopianb60314a2012-04-10 22:09:54 -07002195// ---------------------------------------------------------------------------
2196
Andy McFadden13a082e2012-08-24 10:16:42 -07002197void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002198 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002199 Vector<ComposerState> state;
2200 Vector<DisplayState> displays;
2201 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002202 d.what = DisplayState::eDisplayProjectionChanged |
2203 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002204 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002205 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002206 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002207 d.frame.makeInvalid();
2208 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002209 d.width = 0;
2210 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002211 displays.add(d);
2212 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002213 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002214
2215 const nsecs_t period =
2216 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2217 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002218}
2219
2220void SurfaceFlinger::initializeDisplays() {
2221 class MessageScreenInitialized : public MessageBase {
2222 SurfaceFlinger* flinger;
2223 public:
2224 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2225 virtual bool handler() {
2226 flinger->onInitializeDisplays();
2227 return true;
2228 }
2229 };
2230 sp<MessageBase> msg = new MessageScreenInitialized(this);
2231 postMessageAsync(msg); // we may be called from main thread, use async message
2232}
2233
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002234void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2235 int mode) {
2236 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2237 this);
2238 int32_t type = hw->getDisplayType();
2239 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002240
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002241 if (mode == currentMode) {
2242 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002243 return;
2244 }
2245
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002246 hw->setPowerMode(mode);
2247 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2248 ALOGW("Trying to set power mode for virtual display");
2249 return;
2250 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002251
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002252 if (currentMode == HWC_POWER_MODE_OFF) {
2253 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002254 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2255 // FIXME: eventthread only knows about the main display right now
2256 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002257 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002258 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002259
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002260 mVisibleRegionsDirty = true;
2261 repaintEverything();
2262 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002263 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002264 disableHardwareVsync(true); // also cancels any in-progress resync
2265
Mathias Agopiancde87a32012-09-13 14:09:01 -07002266 // FIXME: eventthread only knows about the main display right now
2267 mEventThread->onScreenReleased();
2268 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002269
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002270 getHwComposer().setPowerMode(type, mode);
2271 mVisibleRegionsDirty = true;
2272 // from this point on, SF will stop drawing on this display
2273 } else {
2274 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002275 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002276}
2277
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002278void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2279 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002280 SurfaceFlinger& mFlinger;
2281 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002282 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002283 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002284 MessageSetPowerMode(SurfaceFlinger& flinger,
2285 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2286 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002287 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002288 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002289 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002290 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002291 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002292 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002293 ALOGW("Attempt to set power mode = %d for virtual display",
2294 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002295 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002296 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002297 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002298 return true;
2299 }
2300 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002301 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002302 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002303}
2304
2305// ---------------------------------------------------------------------------
2306
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002307status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2308{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002309 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002310
Mathias Agopianbd115332013-04-18 16:41:04 -07002311 IPCThreadState* ipc = IPCThreadState::self();
2312 const int pid = ipc->getCallingPid();
2313 const int uid = ipc->getCallingUid();
2314 if ((uid != AID_SHELL) &&
2315 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002316 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002317 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002318 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002319 // Try to get the main lock, but don't insist if we can't
2320 // (this would indicate SF is stuck, but we want to be able to
2321 // print something in dumpsys).
2322 int retry = 3;
2323 while (mStateLock.tryLock()<0 && --retry>=0) {
2324 usleep(1000000);
2325 }
2326 const bool locked(retry >= 0);
2327 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002328 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002329 "SurfaceFlinger appears to be unresponsive, "
2330 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002331 }
2332
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002333 bool dumpAll = true;
2334 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002335 size_t numArgs = args.size();
2336 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002337 if ((index < numArgs) &&
2338 (args[index] == String16("--list"))) {
2339 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002340 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002341 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002342 }
2343
2344 if ((index < numArgs) &&
2345 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002346 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002347 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002348 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002349 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002350
2351 if ((index < numArgs) &&
2352 (args[index] == String16("--latency-clear"))) {
2353 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002354 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002355 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002356 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002357
2358 if ((index < numArgs) &&
2359 (args[index] == String16("--dispsync"))) {
2360 index++;
2361 mPrimaryDispSync.dump(result);
2362 dumpAll = false;
2363 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002364 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002365
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002366 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002367 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002368 }
2369
Mathias Agopian9795c422009-08-26 16:36:26 -07002370 if (locked) {
2371 mStateLock.unlock();
2372 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002373 }
2374 write(fd, result.string(), result.size());
2375 return NO_ERROR;
2376}
2377
Dan Stozac7014012014-02-14 15:03:43 -08002378void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2379 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002380{
2381 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2382 const size_t count = currentLayers.size();
2383 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002384 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002385 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002386 }
2387}
2388
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002389void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002390 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002391{
2392 String8 name;
2393 if (index < args.size()) {
2394 name = String8(args[index]);
2395 index++;
2396 }
2397
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002398 const nsecs_t period =
2399 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002400 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002401
2402 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002403 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002404 } else {
2405 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2406 const size_t count = currentLayers.size();
2407 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002408 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002409 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002410 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002411 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002412 }
2413 }
2414}
2415
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002416void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002417 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002418{
2419 String8 name;
2420 if (index < args.size()) {
2421 name = String8(args[index]);
2422 index++;
2423 }
2424
2425 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2426 const size_t count = currentLayers.size();
2427 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002428 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002429 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002430 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002431 }
2432 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002433
Svetoslavd85084b2014-03-20 10:28:31 -07002434 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002435}
2436
Jamie Gennis6547ff42013-07-16 20:12:42 -07002437// This should only be called from the main thread. Otherwise it would need
2438// the lock and should use mCurrentState rather than mDrawingState.
2439void SurfaceFlinger::logFrameStats() {
2440 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2441 const size_t count = drawingLayers.size();
2442 for (size_t i=0 ; i<count ; i++) {
2443 const sp<Layer>& layer(drawingLayers[i]);
2444 layer->logFrameStats();
2445 }
2446
2447 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2448}
2449
Andy McFadden4803b742012-09-24 19:07:20 -07002450/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2451{
2452 static const char* config =
2453 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002454#ifdef HAS_CONTEXT_PRIORITY
2455 " HAS_CONTEXT_PRIORITY"
2456#endif
2457#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2458 " NEVER_DEFAULT_TO_ASYNC_MODE"
2459#endif
2460#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2461 " TARGET_DISABLE_TRIPLE_BUFFERING"
2462#endif
2463 "]";
2464 result.append(config);
2465}
2466
Mathias Agopian74d211a2013-04-22 16:55:35 +02002467void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2468 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002469{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002470 bool colorize = false;
2471 if (index < args.size()
2472 && (args[index] == String16("--color"))) {
2473 colorize = true;
2474 index++;
2475 }
2476
2477 Colorizer colorizer(colorize);
2478
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002479 // figure out if we're stuck somewhere
2480 const nsecs_t now = systemTime();
2481 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2482 const nsecs_t inTransaction(mDebugInTransaction);
2483 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2484 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2485
2486 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002487 * Dump library configuration.
2488 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002489
2490 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002491 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002492 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002493 appendSfConfigString(result);
2494 appendUiConfigString(result);
2495 appendGuiConfigString(result);
2496 result.append("\n");
2497
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002498 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002499 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002500 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002501 result.append(SyncFeatures::getInstance().toString());
2502 result.append("\n");
2503
Andy McFadden41d67d72014-04-25 16:58:34 -07002504 colorizer.bold(result);
2505 result.append("DispSync configuration: ");
2506 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002507 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2508 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002509 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2510 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2511 result.append("\n");
2512
Andy McFadden4803b742012-09-24 19:07:20 -07002513 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002514 * Dump the visible layer list
2515 */
2516 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2517 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002518 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002519 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002520 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002521 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002522 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002523 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002524 }
2525
2526 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002527 * Dump Display state
2528 */
2529
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002530 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002531 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002532 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002533 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2534 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002535 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002536 }
2537
2538 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002539 * Dump SurfaceFlinger global state
2540 */
2541
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002542 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002543 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002544 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002545
Mathias Agopian888c8222012-08-04 21:10:38 -07002546 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002547 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002548
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002549 colorizer.bold(result);
2550 result.appendFormat("EGL implementation : %s\n",
2551 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2552 colorizer.reset(result);
2553 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002554 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002555
Mathias Agopian875d8e12013-06-07 15:35:48 -07002556 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002557
Mathias Agopian42977342012-08-05 00:40:46 -07002558 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002559 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2560 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002561 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002562 " last eglSwapBuffers() time: %f us\n"
2563 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002564 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002565 " refresh-rate : %f fps\n"
2566 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002567 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002568 " gpu_to_cpu_unsupported : %d\n"
2569 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002570 mLastSwapBufferTime/1000.0,
2571 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002572 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002573 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2574 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002575 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002576 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002577
Mathias Agopian74d211a2013-04-22 16:55:35 +02002578 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002579 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002580
Mathias Agopian74d211a2013-04-22 16:55:35 +02002581 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002582 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002583
2584 /*
2585 * VSYNC state
2586 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002587 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002588
2589 /*
2590 * Dump HWComposer state
2591 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002592 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002593 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002594 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002595 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002596 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002597 (mDebugDisableHWC || mDebugRegion || mDaltonize
2598 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002599 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002600
2601 /*
2602 * Dump gralloc state
2603 */
2604 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2605 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002606}
2607
Mathias Agopian13127d82013-03-05 17:47:11 -08002608const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002609SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002610 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002611 wp<IBinder> dpy;
2612 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2613 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2614 dpy = mDisplays.keyAt(i);
2615 break;
2616 }
2617 }
2618 if (dpy == NULL) {
2619 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2620 // Just use the primary display so we have something to return
2621 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2622 }
2623 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002624}
2625
Keun young Park63f165f2012-08-31 10:53:36 -07002626bool SurfaceFlinger::startDdmConnection()
2627{
2628 void* libddmconnection_dso =
2629 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2630 if (!libddmconnection_dso) {
2631 return false;
2632 }
2633 void (*DdmConnection_start)(const char* name);
2634 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07002635 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07002636 if (!DdmConnection_start) {
2637 dlclose(libddmconnection_dso);
2638 return false;
2639 }
2640 (*DdmConnection_start)(getServiceName());
2641 return true;
2642}
2643
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002644status_t SurfaceFlinger::onTransact(
2645 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2646{
2647 switch (code) {
2648 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002649 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002650 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002651 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002652 case CLEAR_ANIMATION_FRAME_STATS:
2653 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002654 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002655 {
2656 // codes that require permission check
2657 IPCThreadState* ipc = IPCThreadState::self();
2658 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002659 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002660 if ((uid != AID_GRAPHICS) &&
2661 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002662 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002663 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2664 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002665 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002666 break;
2667 }
2668 case CAPTURE_SCREEN:
2669 {
2670 // codes that require permission check
2671 IPCThreadState* ipc = IPCThreadState::self();
2672 const int pid = ipc->getCallingPid();
2673 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002674 if ((uid != AID_GRAPHICS) &&
2675 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002676 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002677 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2678 return PERMISSION_DENIED;
2679 }
2680 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002681 }
2682 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002683
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002684 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2685 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002686 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002687 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002688 IPCThreadState* ipc = IPCThreadState::self();
2689 const int pid = ipc->getCallingPid();
2690 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002691 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002692 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002693 return PERMISSION_DENIED;
2694 }
2695 int n;
2696 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002697 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002698 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002699 return NO_ERROR;
2700 case 1002: // SHOW_UPDATES
2701 n = data.readInt32();
2702 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002703 invalidateHwcGeometry();
2704 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002705 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002706 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002707 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002708 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002709 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002710 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002711 setTransactionFlags(
2712 eTransactionNeeded|
2713 eDisplayTransactionNeeded|
2714 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002715 return NO_ERROR;
2716 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002717 case 1006:{ // send empty update
2718 signalRefresh();
2719 return NO_ERROR;
2720 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002721 case 1008: // toggle use of hw composer
2722 n = data.readInt32();
2723 mDebugDisableHWC = n ? 1 : 0;
2724 invalidateHwcGeometry();
2725 repaintEverything();
2726 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002727 case 1009: // toggle use of transform hint
2728 n = data.readInt32();
2729 mDebugDisableTransformHint = n ? 1 : 0;
2730 invalidateHwcGeometry();
2731 repaintEverything();
2732 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002733 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002734 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002735 reply->writeInt32(0);
2736 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002737 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002738 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002739 return NO_ERROR;
2740 case 1013: {
2741 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002742 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2743 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002744 return NO_ERROR;
2745 }
2746 case 1014: {
2747 // daltonize
2748 n = data.readInt32();
2749 switch (n % 10) {
2750 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2751 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2752 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2753 }
2754 if (n >= 10) {
2755 mDaltonizer.setMode(Daltonizer::correction);
2756 } else {
2757 mDaltonizer.setMode(Daltonizer::simulation);
2758 }
2759 mDaltonize = n > 0;
2760 invalidateHwcGeometry();
2761 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002762 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002763 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002764 case 1015: {
2765 // apply a color matrix
2766 n = data.readInt32();
2767 mHasColorMatrix = n ? 1 : 0;
2768 if (n) {
2769 // color matrix is sent as mat3 matrix followed by vec3
2770 // offset, then packed into a mat4 where the last row is
2771 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002772 for (size_t i = 0 ; i < 4; i++) {
2773 for (size_t j = 0; j < 4; j++) {
2774 mColorMatrix[i][j] = data.readFloat();
2775 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002776 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002777 } else {
2778 mColorMatrix = mat4();
2779 }
2780 invalidateHwcGeometry();
2781 repaintEverything();
2782 return NO_ERROR;
2783 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002784 // This is an experimental interface
2785 // Needs to be shifted to proper binder interface when we productize
2786 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07002787 n = data.readInt32();
2788 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002789 return NO_ERROR;
2790 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002791 }
2792 }
2793 return err;
2794}
2795
Mathias Agopian53331da2011-08-22 21:44:41 -07002796void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002797 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002798 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002799}
2800
Mathias Agopian59119e62010-10-11 12:37:43 -07002801// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002802// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002803// ---------------------------------------------------------------------------
2804
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002805/* The code below is here to handle b/8734824
2806 *
2807 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07002808 * from the surfaceflinger thread to the calling binder thread, where they
2809 * are executed. This allows the calling thread in the calling process to be
2810 * reused and not depend on having "enough" binder threads to handle the
2811 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002812 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002813class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07002814 /* Parts of GraphicProducerWrapper are run on two different threads,
2815 * communicating by sending messages via Looper but also by shared member
2816 * data. Coherence maintenance is subtle and in places implicit (ugh).
2817 *
2818 * Don't rely on Looper's sendMessage/handleMessage providing
2819 * release/acquire semantics for any data not actually in the Message.
2820 * Data going from surfaceflinger to binder threads needs to be
2821 * synchronized explicitly.
2822 *
2823 * Barrier open/wait do provide release/acquire semantics. This provides
2824 * implicit synchronization for data coming back from binder to
2825 * surfaceflinger threads.
2826 */
2827
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002828 sp<IGraphicBufferProducer> impl;
2829 sp<Looper> looper;
2830 status_t result;
2831 bool exitPending;
2832 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07002833 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002834 uint32_t code;
2835 Parcel const* data;
2836 Parcel* reply;
2837
2838 enum {
2839 MSG_API_CALL,
2840 MSG_EXIT
2841 };
2842
2843 /*
Jesse Hallb154c422014-07-13 12:47:02 -07002844 * Called on surfaceflinger thread. This is called by our "fake"
2845 * BpGraphicBufferProducer. We package the data and reply Parcel and
2846 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002847 */
2848 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08002849 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002850 this->code = code;
2851 this->data = &data;
2852 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002853 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07002854 // if we've exited, we run the message synchronously right here.
2855 // note (JH): as far as I can tell from looking at the code, this
2856 // never actually happens. if it does, i'm not sure if it happens
2857 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002858 handleMessage(Message(MSG_API_CALL));
2859 } else {
2860 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07002861 // Prevent stores to this->{code, data, reply} from being
2862 // reordered later than the construction of Message.
2863 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002864 looper->sendMessage(this, Message(MSG_API_CALL));
2865 barrier.wait();
2866 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08002867 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002868 }
2869
2870 /*
Jesse Hallb154c422014-07-13 12:47:02 -07002871 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002872 * call the real BpGraphicBufferProducer.
2873 */
2874 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07002875 int what = message.what;
2876 // Prevent reads below from happening before the read from Message
2877 atomic_thread_fence(memory_order_acquire);
2878 if (what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08002879 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002880 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07002881 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002882 exitRequested = true;
2883 }
2884 }
2885
2886public:
Jesse Hallb154c422014-07-13 12:47:02 -07002887 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
2888 : impl(impl),
2889 looper(new Looper(true)),
2890 exitPending(false),
2891 exitRequested(false)
2892 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002893
Jesse Hallb154c422014-07-13 12:47:02 -07002894 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002895 status_t waitForResponse() {
2896 do {
2897 looper->pollOnce(-1);
2898 } while (!exitRequested);
2899 return result;
2900 }
2901
Jesse Hallb154c422014-07-13 12:47:02 -07002902 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002903 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002904 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002905 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07002906 // Ensure this->result is visible to the binder thread before it
2907 // handles the message.
2908 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002909 looper->sendMessage(this, Message(MSG_EXIT));
2910 }
2911};
2912
2913
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002914status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2915 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002916 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002917 uint32_t minLayerZ, uint32_t maxLayerZ,
2918 bool useIdentityTransform) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002919
2920 if (CC_UNLIKELY(display == 0))
2921 return BAD_VALUE;
2922
2923 if (CC_UNLIKELY(producer == 0))
2924 return BAD_VALUE;
2925
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002926 // if we have secure windows on this display, never allow the screen capture
2927 // unless the producer interface is local (i.e.: we can take a screenshot for
2928 // ourselves).
2929 if (!producer->asBinder()->localBinder()) {
2930 Mutex::Autolock _l(mStateLock);
2931 sp<const DisplayDevice> hw(getDisplayDevice(display));
2932 if (hw->getSecureLayerVisible()) {
2933 ALOGW("FB is protected: PERMISSION_DENIED");
2934 return PERMISSION_DENIED;
2935 }
2936 }
2937
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002938 class MessageCaptureScreen : public MessageBase {
2939 SurfaceFlinger* flinger;
2940 sp<IBinder> display;
2941 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07002942 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002943 uint32_t reqWidth, reqHeight;
2944 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08002945 bool useIdentityTransform;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002946 status_t result;
2947 public:
2948 MessageCaptureScreen(SurfaceFlinger* flinger,
2949 const sp<IBinder>& display,
2950 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002951 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002952 uint32_t minLayerZ, uint32_t maxLayerZ,
2953 bool useIdentityTransform)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002954 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07002955 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002956 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08002957 useIdentityTransform(useIdentityTransform),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002958 result(PERMISSION_DENIED)
2959 {
2960 }
2961 status_t getResult() const {
2962 return result;
2963 }
2964 virtual bool handler() {
2965 Mutex::Autolock _l(flinger->mStateLock);
2966 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08002967 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07002968 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Dan Stozac7014012014-02-14 15:03:43 -08002969 useIdentityTransform);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002970 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002971 return true;
2972 }
2973 };
2974
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002975 // make sure to process transactions before screenshots -- a transaction
2976 // might already be pending but scheduled for VSYNC; this guarantees we
2977 // will handle it before the screenshot. When VSYNC finally arrives
2978 // the scheduled transaction will be a no-op. If no transactions are
2979 // scheduled at this time, this will end-up being a no-op as well.
2980 mEventQueue.invalidateTransactionNow();
2981
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002982 // this creates a "fake" BBinder which will serve as a "fake" remote
2983 // binder to receive the marshaled calls and forward them to the
2984 // real remote (a BpGraphicBufferProducer)
2985 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2986
2987 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2988 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002989 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002990 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07002991 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
2992 useIdentityTransform);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002993
2994 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002995 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002996 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002997 }
2998 return res;
2999}
3000
Mathias Agopian180f10d2013-04-10 22:55:41 -07003001
3002void SurfaceFlinger::renderScreenImplLocked(
3003 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003004 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003005 uint32_t minLayerZ, uint32_t maxLayerZ,
Dan Stozac7014012014-02-14 15:03:43 -08003006 bool yswap, bool useIdentityTransform)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003007{
3008 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003009 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003010
3011 // get screen geometry
3012 const uint32_t hw_w = hw->getWidth();
3013 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003014 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
3015
Dan Stozac1879002014-05-22 15:59:05 -07003016 // if a default or invalid sourceCrop is passed in, set reasonable values
3017 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3018 !sourceCrop.isValid()) {
3019 sourceCrop.setLeftTop(Point(0, 0));
3020 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3021 }
3022
3023 // ensure that sourceCrop is inside screen
3024 if (sourceCrop.left < 0) {
3025 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3026 }
Dan Stozabe31f442014-06-11 11:20:54 -07003027 if (sourceCrop.right > hw_w) {
3028 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003029 }
3030 if (sourceCrop.top < 0) {
3031 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3032 }
Dan Stozabe31f442014-06-11 11:20:54 -07003033 if (sourceCrop.bottom > hw_h) {
3034 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003035 }
3036
Mathias Agopian180f10d2013-04-10 22:55:41 -07003037 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003038 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003039
3040 // set-up our viewport
Dan Stozac1879002014-05-22 15:59:05 -07003041 engine.setViewportAndProjection(reqWidth, reqHeight, sourceCrop, hw_h, yswap);
Mathias Agopian3f844832013-08-07 21:24:32 -07003042 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003043
3044 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003045 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003046
3047 const LayerVector& layers( mDrawingState.layersSortedByZ );
3048 const size_t count = layers.size();
3049 for (size_t i=0 ; i<count ; ++i) {
3050 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003051 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003052 if (state.layerStack == hw->getLayerStack()) {
3053 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3054 if (layer->isVisible()) {
3055 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08003056 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003057 if (filtering) layer->setFiltering(false);
3058 }
3059 }
3060 }
3061 }
3062
3063 // compositionComplete is needed for older driver
3064 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003065 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003066}
3067
3068
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003069status_t SurfaceFlinger::captureScreenImplLocked(
3070 const sp<const DisplayDevice>& hw,
3071 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003072 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003073 uint32_t minLayerZ, uint32_t maxLayerZ,
3074 bool useIdentityTransform)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003075{
3076 ATRACE_CALL();
3077
Mathias Agopian180f10d2013-04-10 22:55:41 -07003078 // get screen geometry
3079 const uint32_t hw_w = hw->getWidth();
3080 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003081
Mathias Agopian180f10d2013-04-10 22:55:41 -07003082 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3083 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3084 reqWidth, reqHeight, hw_w, hw_h);
3085 return BAD_VALUE;
3086 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003087
Mathias Agopian180f10d2013-04-10 22:55:41 -07003088 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3089 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3090
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003091 // create a surface (because we're a producer, and we need to
3092 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003093 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003094 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003095
3096 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003097 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003098 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3099 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003100
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003101 int err = 0;
3102 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003103 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003104 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3105 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003106
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003107 if (err == NO_ERROR) {
3108 ANativeWindowBuffer* buffer;
3109 /* TODO: Once we have the sync framework everywhere this can use
3110 * server-side waits on the fence that dequeueBuffer returns.
3111 */
3112 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3113 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003114 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003115 // create an EGLImage from the buffer so we can later
3116 // turn it into a texture
3117 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3118 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3119 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003120 // this binds the given EGLImage as a framebuffer for the
3121 // duration of this scope.
3122 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3123 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003124 // this will in fact render into our dequeued buffer
3125 // via an FBO, which means we didn't have to create
3126 // an EGLSurface and therefore we're not
3127 // dependent on the context's EGLConfig.
Dan Stozac1879002014-05-22 15:59:05 -07003128 renderScreenImplLocked(hw, sourceCrop, reqWidth, reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003129 minLayerZ, maxLayerZ, true, useIdentityTransform);
Mathias Agopiand5556842013-09-19 17:08:37 -07003130
Riley Andrews86639902014-08-15 12:27:24 -07003131 // Attempt to create a sync khr object that can produce a sync point. If that
3132 // isn't available, create a non-dupable sync object in the fallback path and
3133 // wait on it directly.
3134 EGLSyncKHR sync;
3135 if (!DEBUG_SCREENSHOTS) {
3136 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Andy McFadden2d8d1202013-10-09 16:38:02 -07003137 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003138 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003139 }
Riley Andrews86639902014-08-15 12:27:24 -07003140 if (sync != EGL_NO_SYNC_KHR) {
3141 // get the sync fd
3142 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3143 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3144 ALOGW("captureScreen: failed to dup sync khr object");
3145 syncFd = -1;
3146 }
3147 eglDestroySyncKHR(mEGLDisplay, sync);
3148 } else {
3149 // fallback path
3150 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3151 if (sync != EGL_NO_SYNC_KHR) {
3152 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3153 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3154 EGLint eglErr = eglGetError();
3155 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3156 ALOGW("captureScreen: fence wait timed out");
3157 } else {
3158 ALOGW_IF(eglErr != EGL_SUCCESS,
3159 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3160 }
3161 eglDestroySyncKHR(mEGLDisplay, sync);
3162 } else {
3163 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3164 }
3165 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003166 if (DEBUG_SCREENSHOTS) {
3167 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3168 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3169 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3170 hw, minLayerZ, maxLayerZ);
3171 delete [] pixels;
3172 }
3173
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003174 } else {
3175 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3176 result = INVALID_OPERATION;
3177 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003178 // destroy our image
3179 eglDestroyImageKHR(mEGLDisplay, image);
3180 } else {
3181 result = BAD_VALUE;
3182 }
Riley Andrews86639902014-08-15 12:27:24 -07003183 window->queueBuffer(window, buffer, syncFd);
3184 if (syncFd != -1) {
3185 close(syncFd);
3186 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003187 }
3188 } else {
3189 result = BAD_VALUE;
3190 }
3191 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3192 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003193
Mathias Agopian74c40c02010-09-29 13:02:36 -07003194 return result;
3195}
3196
Mathias Agopiand5556842013-09-19 17:08:37 -07003197void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3198 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003199 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003200 for (size_t y=0 ; y<h ; y++) {
3201 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3202 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003203 if (p[x] != 0xFF000000) return;
3204 }
3205 }
3206 ALOGE("*** we just took a black screenshot ***\n"
3207 "requested minz=%d, maxz=%d, layerStack=%d",
3208 minLayerZ, maxLayerZ, hw->getLayerStack());
3209 const LayerVector& layers( mDrawingState.layersSortedByZ );
3210 const size_t count = layers.size();
3211 for (size_t i=0 ; i<count ; ++i) {
3212 const sp<Layer>& layer(layers[i]);
3213 const Layer::State& state(layer->getDrawingState());
3214 const bool visible = (state.layerStack == hw->getLayerStack())
3215 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3216 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003217 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003218 visible ? '+' : '-',
3219 i, layer->getName().string(), state.layerStack, state.z,
3220 layer->isVisible(), state.flags, state.alpha);
3221 }
3222 }
3223}
3224
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003225// ---------------------------------------------------------------------------
3226
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003227SurfaceFlinger::LayerVector::LayerVector() {
3228}
3229
3230SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003231 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003232}
3233
3234int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3235 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003236{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003237 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003238 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3239 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003240
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003241 uint32_t ls = l->getCurrentState().layerStack;
3242 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003243 if (ls != rs)
3244 return ls - rs;
3245
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003246 uint32_t lz = l->getCurrentState().z;
3247 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003248 if (lz != rz)
3249 return lz - rz;
3250
3251 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003252}
3253
3254// ---------------------------------------------------------------------------
3255
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003256SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Michael Lentine47e45402014-07-18 15:34:25 -07003257 : type(DisplayDevice::DISPLAY_ID_INVALID), width(0), height(0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003258}
3259
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003260SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Michael Lentine47e45402014-07-18 15:34:25 -07003261 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0), width(0), height(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003262 viewport.makeInvalid();
3263 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003264}
3265
3266// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003267
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003268}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003269
3270
3271#if defined(__gl_h_)
3272#error "don't include gl/gl.h in this file"
3273#endif
3274
3275#if defined(__gl2_h_)
3276#error "don't include gl2/gl2.h in this file"
3277#endif