blob: 6a034ecdcd0624d558d62ba7ec7f6d927e12a2d3 [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() {
640 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
641 if (hw == NULL) {
642 ALOGE("Attempt to set active config = %d for null display %p",
643 mDisplay.get(), mMode);
644 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
645 ALOGW("Attempt to set active config = %d for virtual display",
646 mMode);
647 } else {
648 mFlinger.setActiveConfigInternal(hw, mMode);
649 }
650 return true;
651 }
652 };
653 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
654 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700655 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700656}
657
Svetoslavd85084b2014-03-20 10:28:31 -0700658status_t SurfaceFlinger::clearAnimationFrameStats() {
659 Mutex::Autolock _l(mStateLock);
660 mAnimFrameTracker.clearStats();
661 return NO_ERROR;
662}
663
664status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
665 Mutex::Autolock _l(mStateLock);
666 mAnimFrameTracker.getStats(outStats);
667 return NO_ERROR;
668}
669
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800670// ----------------------------------------------------------------------------
671
672sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800673 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700674}
675
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800676// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800677
678void SurfaceFlinger::waitForEvent() {
679 mEventQueue.waitMessage();
680}
681
682void SurfaceFlinger::signalTransaction() {
683 mEventQueue.invalidate();
684}
685
686void SurfaceFlinger::signalLayerUpdate() {
687 mEventQueue.invalidate();
688}
689
690void SurfaceFlinger::signalRefresh() {
691 mEventQueue.refresh();
692}
693
694status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800695 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800696 return mEventQueue.postMessage(msg, reltime);
697}
698
699status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800700 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800701 status_t res = mEventQueue.postMessage(msg, reltime);
702 if (res == NO_ERROR) {
703 msg->wait();
704 }
705 return res;
706}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800707
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700708void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700709 do {
710 waitForEvent();
711 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800712}
713
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700714void SurfaceFlinger::enableHardwareVsync() {
715 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700716 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700717 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700718 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
719 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700720 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700721 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700722}
723
Jesse Hall948fe0c2013-10-14 12:56:09 -0700724void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700725 Mutex::Autolock _l(mHWVsyncLock);
726
Jesse Hall948fe0c2013-10-14 12:56:09 -0700727 if (makeAvailable) {
728 mHWVsyncAvailable = true;
729 } else if (!mHWVsyncAvailable) {
730 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
731 return;
732 }
733
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700734 const nsecs_t period =
735 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
736
737 mPrimaryDispSync.reset();
738 mPrimaryDispSync.setPeriod(period);
739
740 if (!mPrimaryHWVsyncEnabled) {
741 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700742 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
743 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700744 mPrimaryHWVsyncEnabled = true;
745 }
746}
747
Jesse Hall948fe0c2013-10-14 12:56:09 -0700748void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700749 Mutex::Autolock _l(mHWVsyncLock);
750 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700751 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
752 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700753 mPrimaryDispSync.endResync();
754 mPrimaryHWVsyncEnabled = false;
755 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700756 if (makeUnavailable) {
757 mHWVsyncAvailable = false;
758 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700759}
760
761void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700762 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700763
Jamie Gennisd1700752013-10-14 12:22:52 -0700764 { // Scope for the lock
765 Mutex::Autolock _l(mHWVsyncLock);
766 if (type == 0 && mPrimaryHWVsyncEnabled) {
767 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700768 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700769 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700770
771 if (needsHwVsync) {
772 enableHardwareVsync();
773 } else {
774 disableHardwareVsync(false);
775 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700776}
777
778void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
779 if (mEventThread == NULL) {
780 // This is a temporary workaround for b/7145521. A non-null pointer
781 // does not mean EventThread has finished initializing, so this
782 // is not a correct fix.
783 ALOGW("WARNING: EventThread not started, ignoring hotplug");
784 return;
785 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700786
Jesse Hall9e663de2013-08-16 14:28:37 -0700787 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700788 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800789 if (connected) {
790 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700791 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800792 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
793 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700794 }
795 setTransactionFlags(eDisplayTransactionNeeded);
796
Andy McFadden9e9689c2012-10-10 18:17:51 -0700797 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700798 }
Mathias Agopian86303202012-07-24 22:46:10 -0700799}
800
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700801void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700802 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700803 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700804}
805
Mathias Agopian4fec8732012-06-29 14:12:52 -0700806void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800807 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800808 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700809 case MessageQueue::TRANSACTION:
810 handleMessageTransaction();
811 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700812 case MessageQueue::INVALIDATE:
813 handleMessageTransaction();
814 handleMessageInvalidate();
815 signalRefresh();
816 break;
817 case MessageQueue::REFRESH:
818 handleMessageRefresh();
819 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800820 }
821}
822
Mathias Agopian4fec8732012-06-29 14:12:52 -0700823void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700824 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700825 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700826 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700827 }
828}
829
830void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700831 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700832 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700833}
834
835void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700836 ATRACE_CALL();
837 preComposition();
838 rebuildLayerStacks();
839 setUpHWComposer();
840 doDebugFlashRegions();
841 doComposition();
842 postComposition();
843}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700844
Mathias Agopiancd60f992012-08-16 16:28:27 -0700845void SurfaceFlinger::doDebugFlashRegions()
846{
847 // is debugging enabled
848 if (CC_LIKELY(!mDebugRegion))
849 return;
850
851 const bool repaintEverything = mRepaintEverything;
852 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
853 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700854 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700855 // transform the dirty region into this screen's coordinate space
856 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
857 if (!dirtyRegion.isEmpty()) {
858 // redraw the whole screen
859 doComposeSurfaces(hw, Region(hw->bounds()));
860
861 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700862 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700863 RenderEngine& engine(getRenderEngine());
864 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
865
Mathias Agopiancd60f992012-08-16 16:28:27 -0700866 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700867 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700868 }
869 }
870 }
871
872 postFramebuffer();
873
874 if (mDebugRegion > 1) {
875 usleep(mDebugRegion * 1000);
876 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700877
878 HWComposer& hwc(getHwComposer());
879 if (hwc.initCheck() == NO_ERROR) {
880 status_t err = hwc.prepare();
881 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
882 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700883}
884
885void SurfaceFlinger::preComposition()
886{
887 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700888 const LayerVector& layers(mDrawingState.layersSortedByZ);
889 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700890 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700891 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700892 needExtraInvalidate = true;
893 }
894 }
895 if (needExtraInvalidate) {
896 signalLayerUpdate();
897 }
898}
899
900void SurfaceFlinger::postComposition()
901{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700902 const LayerVector& layers(mDrawingState.layersSortedByZ);
903 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700904 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700905 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700906 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800907
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700908 const HWComposer& hwc = getHwComposer();
909 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
910
911 if (presentFence->isValid()) {
912 if (mPrimaryDispSync.addPresentFence(presentFence)) {
913 enableHardwareVsync();
914 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700915 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700916 }
917 }
918
Andy McFadden5167ec62014-05-22 13:08:43 -0700919 if (kIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700920 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700921 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700922 enableHardwareVsync();
923 }
924 }
925
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800926 if (mAnimCompositionPending) {
927 mAnimCompositionPending = false;
928
Jesse Halla9a1b002013-02-27 16:39:25 -0800929 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800930 mAnimFrameTracker.setActualPresentFence(presentFence);
931 } else {
932 // The HWC doesn't support present fences, so use the refresh
933 // timestamp instead.
934 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
935 mAnimFrameTracker.setActualPresentTime(presentTime);
936 }
937 mAnimFrameTracker.advanceFrame();
938 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700939}
940
941void SurfaceFlinger::rebuildLayerStacks() {
942 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700943 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700944 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700945 mVisibleRegionsDirty = false;
946 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700947
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700948 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700949 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700950 Region opaqueRegion;
951 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800952 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700953 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700954 const Transform& tr(hw->getTransform());
955 const Rect bounds(hw->getBounds());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700956 if (hw->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700957 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700958 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700959
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700960 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700961 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700962 const sp<Layer>& layer(layers[i]);
963 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700964 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700965 Region drawRegion(tr.transform(
966 layer->visibleNonTransparentRegion));
967 drawRegion.andSelf(bounds);
968 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700969 layersSortedByZ.add(layer);
970 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700971 }
972 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700973 }
Mathias Agopian42977342012-08-05 00:40:46 -0700974 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700975 hw->undefinedRegion.set(bounds);
976 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
977 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700978 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700979 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700980}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700981
Mathias Agopiancd60f992012-08-16 16:28:27 -0700982void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700983 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Dan Stoza71433162014-02-04 16:22:36 -0800984 bool mustRecompose =
985 !(mDisplays[dpy]->getDirtyRegion(false).isEmpty());
986 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700987 }
988
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700989 HWComposer& hwc(getHwComposer());
990 if (hwc.initCheck() == NO_ERROR) {
991 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700992 if (CC_UNLIKELY(mHwWorkListDirty)) {
993 mHwWorkListDirty = false;
994 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
995 sp<const DisplayDevice> hw(mDisplays[dpy]);
996 const int32_t id = hw->getHwcDisplayId();
997 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800998 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700999 hw->getVisibleLayersSortedByZ());
1000 const size_t count = currentLayers.size();
1001 if (hwc.createWorkList(id, count) == NO_ERROR) {
1002 HWComposer::LayerListIterator cur = hwc.begin(id);
1003 const HWComposer::LayerListIterator end = hwc.end(id);
1004 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001005 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001006 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001007 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001008 cur->setSkip(true);
1009 }
1010 }
1011 }
1012 }
1013 }
1014 }
1015
1016 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001017 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001018 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001019 const int32_t id = hw->getHwcDisplayId();
1020 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001021 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001022 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001023 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001024 HWComposer::LayerListIterator cur = hwc.begin(id);
1025 const HWComposer::LayerListIterator end = hwc.end(id);
1026 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1027 /*
1028 * update the per-frame h/w composer data for each layer
1029 * and build the transparent region of the FB
1030 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001031 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001032 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -07001033 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001034 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001035 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001036
Riley Andrews03414a12014-07-01 14:22:59 -07001037 // If possible, attempt to use the cursor overlay on each display.
1038 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1039 sp<const DisplayDevice> hw(mDisplays[dpy]);
1040 const int32_t id = hw->getHwcDisplayId();
1041 if (id >= 0) {
1042 const Vector< sp<Layer> >& currentLayers(
1043 hw->getVisibleLayersSortedByZ());
1044 const size_t count = currentLayers.size();
1045 HWComposer::LayerListIterator cur = hwc.begin(id);
1046 const HWComposer::LayerListIterator end = hwc.end(id);
1047 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1048 const sp<Layer>& layer(currentLayers[i]);
1049 if (layer->isPotentialCursor()) {
1050 cur->setIsCursorLayerHint();
1051 break;
1052 }
1053 }
1054 }
1055 }
1056
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001057 status_t err = hwc.prepare();
1058 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001059
1060 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1061 sp<const DisplayDevice> hw(mDisplays[dpy]);
1062 hw->prepareFrame(hwc);
1063 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001064 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001065}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001066
Mathias Agopiancd60f992012-08-16 16:28:27 -07001067void SurfaceFlinger::doComposition() {
1068 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001069 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001070 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001071 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001072 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001073 // transform the dirty region into this screen's coordinate space
1074 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001075
1076 // repaint the framebuffer (if needed)
1077 doDisplayComposition(hw, dirtyRegion);
1078
Mathias Agopiancd60f992012-08-16 16:28:27 -07001079 hw->dirtyRegion.clear();
1080 hw->flip(hw->swapRegion);
1081 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001082 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001083 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001084 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001085 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001086 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001087}
1088
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089void SurfaceFlinger::postFramebuffer()
1090{
Mathias Agopian841cde52012-03-01 15:44:37 -08001091 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001092
Mathias Agopiana44b0412011-10-16 18:46:35 -07001093 const nsecs_t now = systemTime();
1094 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001095
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001096 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001097 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001098 if (!hwc.supportsFramebufferTarget()) {
1099 // EGL spec says:
1100 // "surface must be bound to the calling thread's current context,
1101 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001102 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001103 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001104 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001105 }
1106
Mathias Agopian6da15f42013-09-25 20:40:07 -07001107 // make the default display current because the VirtualDisplayDevice code cannot
1108 // deal with dequeueBuffer() being called outside of the composition loop; however
1109 // the code below can call glFlush() which is allowed (and does in some case) call
1110 // dequeueBuffer().
1111 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1112
Mathias Agopian92a979a2012-08-02 18:32:23 -07001113 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001114 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001115 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001116 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001117 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001118 int32_t id = hw->getHwcDisplayId();
1119 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001120 HWComposer::LayerListIterator cur = hwc.begin(id);
1121 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001122 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001123 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001124 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001125 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001126 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001127 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001128 }
Jesse Hallef194142012-06-14 14:45:17 -07001129 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001130 }
1131
Mathias Agopiana44b0412011-10-16 18:46:35 -07001132 mLastSwapBufferTime = systemTime() - now;
1133 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001134
1135 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1136 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1137 logFrameStats();
1138 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001139}
1140
Mathias Agopian87baae12012-07-31 12:38:26 -07001141void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001142{
Mathias Agopian841cde52012-03-01 15:44:37 -08001143 ATRACE_CALL();
1144
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001145 // here we keep a copy of the drawing state (that is the state that's
1146 // going to be overwritten by handleTransactionLocked()) outside of
1147 // mStateLock so that the side-effects of the State assignment
1148 // don't happen with mStateLock held (which can cause deadlocks).
1149 State drawingState(mDrawingState);
1150
Mathias Agopianca4d3602011-05-19 15:38:14 -07001151 Mutex::Autolock _l(mStateLock);
1152 const nsecs_t now = systemTime();
1153 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001154
Mathias Agopianca4d3602011-05-19 15:38:14 -07001155 // Here we're guaranteed that some transaction flags are set
1156 // so we can call handleTransactionLocked() unconditionally.
1157 // We call getTransactionFlags(), which will also clear the flags,
1158 // with mStateLock held to guarantee that mCurrentState won't change
1159 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001160
Mathias Agopiane57f2922012-08-09 16:29:12 -07001161 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001162 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001163
Mathias Agopianca4d3602011-05-19 15:38:14 -07001164 mLastTransactionTime = systemTime() - now;
1165 mDebugInTransaction = 0;
1166 invalidateHwcGeometry();
1167 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001168}
1169
Mathias Agopian87baae12012-07-31 12:38:26 -07001170void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001171{
1172 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001173 const size_t count = currentLayers.size();
1174
1175 /*
1176 * Traversal of the children
1177 * (perform the transaction for each of them if needed)
1178 */
1179
Mathias Agopian3559b072012-08-15 13:46:03 -07001180 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001182 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1184 if (!trFlags) continue;
1185
1186 const uint32_t flags = layer->doTransaction(0);
1187 if (flags & Layer::eVisibleRegion)
1188 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 }
1190 }
1191
1192 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001193 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001194 */
1195
Mathias Agopiane57f2922012-08-09 16:29:12 -07001196 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001197 // here we take advantage of Vector's copy-on-write semantics to
1198 // improve performance by skipping the transaction entirely when
1199 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001200 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1201 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001202 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001204 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001205 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001206
1207 // find the displays that were removed
1208 // (ie: in drawing state but not in current state)
1209 // also handle displays that changed
1210 // (ie: displays that are in both lists)
1211 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001212 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1213 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001214 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001215 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001216 // Call makeCurrent() on the primary display so we can
1217 // be sure that nothing associated with this display
1218 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001219 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001220 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001221 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1222 if (hw != NULL)
1223 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001224 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001225 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001226 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001227 } else {
1228 ALOGW("trying to remove the main display");
1229 }
1230 } else {
1231 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001232 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001233 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001234 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001235 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001236 // recreating the DisplayDevice, so we just remove it
1237 // from the drawing state, so that it get re-added
1238 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001239 sp<DisplayDevice> hw(getDisplayDevice(display));
1240 if (hw != NULL)
1241 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001242 mDisplays.removeItem(display);
1243 mDrawingState.displays.removeItemsAt(i);
1244 dc--; i--;
1245 // at this point we must loop to the next item
1246 continue;
1247 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001248
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001249 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001250 if (disp != NULL) {
1251 if (state.layerStack != draw[i].layerStack) {
1252 disp->setLayerStack(state.layerStack);
1253 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001254 if ((state.orientation != draw[i].orientation)
1255 || (state.viewport != draw[i].viewport)
1256 || (state.frame != draw[i].frame))
1257 {
1258 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001259 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001260 }
Michael Lentine47e45402014-07-18 15:34:25 -07001261 if (state.width != draw[i].width || state.height != draw[i].height) {
1262 disp->setDisplaySize(state.width, state.height);
1263 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001264 }
1265 }
1266 }
1267
1268 // find displays that were added
1269 // (ie: in current state but not in drawing state)
1270 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001271 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001272 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001273
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001274 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001275 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001276 sp<IGraphicBufferProducer> bqProducer;
1277 sp<IGraphicBufferConsumer> bqConsumer;
1278 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1279 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001280
Jesse Hall02d86562013-03-25 14:43:23 -07001281 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001282 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001283 // Virtual displays without a surface are dormant:
1284 // they have external state (layer stack, projection,
1285 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001286 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001287
Jesse Hall02d86562013-03-25 14:43:23 -07001288 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001289 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
Dan Stozab9b08832014-03-13 11:55:57 -07001290 *mHwc, hwcDisplayId, state.surface,
1291 bqProducer, bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001292
1293 dispSurface = vds;
Michael Lentine47e45402014-07-18 15:34:25 -07001294 producer = vds;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001295 }
1296 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001297 ALOGE_IF(state.surface!=NULL,
1298 "adding a supported display, but rendering "
1299 "surface is provided (%p), ignoring it",
1300 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001301 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001302 // for supported (by hwc) displays we provide our
1303 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001304 dispSurface = new FramebufferSurface(*mHwc, state.type,
1305 bqConsumer);
1306 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001307 }
1308
1309 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001310 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001311 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001312 state.type, hwcDisplayId,
1313 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001314 display, dispSurface, producer,
1315 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001316 hw->setLayerStack(state.layerStack);
1317 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001318 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001319 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001320 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001321 if (state.isVirtualDisplay()) {
1322 if (hwcDisplayId >= 0) {
1323 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1324 hw->getWidth(), hw->getHeight(),
1325 hw->getFormat());
1326 }
1327 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001328 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001329 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001330 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001331 }
1332 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001333 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001334 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001335
Mathias Agopian84300952012-11-21 16:02:13 -08001336 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1337 // The transform hint might have changed for some layers
1338 // (either because a display has changed, or because a layer
1339 // as changed).
1340 //
1341 // Walk through all the layers in currentLayers,
1342 // and update their transform hint.
1343 //
1344 // If a layer is visible only on a single display, then that
1345 // display is used to calculate the hint, otherwise we use the
1346 // default display.
1347 //
1348 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1349 // the hint is set before we acquire a buffer from the surface texture.
1350 //
1351 // NOTE: layer transactions have taken place already, so we use their
1352 // drawing state. However, SurfaceFlinger's own transaction has not
1353 // happened yet, so we must use the current state layer list
1354 // (soon to become the drawing state list).
1355 //
1356 sp<const DisplayDevice> disp;
1357 uint32_t currentlayerStack = 0;
1358 for (size_t i=0; i<count; i++) {
1359 // NOTE: we rely on the fact that layers are sorted by
1360 // layerStack first (so we don't have to traverse the list
1361 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001362 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001363 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001364 if (i==0 || currentlayerStack != layerStack) {
1365 currentlayerStack = layerStack;
1366 // figure out if this layerstack is mirrored
1367 // (more than one display) if so, pick the default display,
1368 // if not, pick the only display it's on.
1369 disp.clear();
1370 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1371 sp<const DisplayDevice> hw(mDisplays[dpy]);
1372 if (hw->getLayerStack() == currentlayerStack) {
1373 if (disp == NULL) {
1374 disp = hw;
1375 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001376 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001377 break;
1378 }
1379 }
1380 }
1381 }
Chet Haase91d25932013-04-11 15:24:55 -07001382 if (disp == NULL) {
1383 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1384 // redraw after transform hint changes. See bug 8508397.
1385
1386 // could be null when this layer is using a layerStack
1387 // that is not visible on any display. Also can occur at
1388 // screen off/on times.
1389 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001390 }
Chet Haase91d25932013-04-11 15:24:55 -07001391 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001392 }
1393 }
1394
1395
Mathias Agopian3559b072012-08-15 13:46:03 -07001396 /*
1397 * Perform our own transaction if needed
1398 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001399
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001400 const LayerVector& layers(mDrawingState.layersSortedByZ);
1401 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001402 // layers have been added
1403 mVisibleRegionsDirty = true;
1404 }
1405
1406 // some layers might have been removed, so
1407 // we need to update the regions they're exposing.
1408 if (mLayersRemoved) {
1409 mLayersRemoved = false;
1410 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001411 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001412 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001413 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001414 if (currentLayers.indexOf(layer) < 0) {
1415 // this layer is not visible anymore
1416 // TODO: we could traverse the tree from front to back and
1417 // compute the actual visible region
1418 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001419 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001420 Region visibleReg = s.transform.transform(
1421 Region(Rect(s.active.w, s.active.h)));
1422 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001423 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425 }
1426
1427 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001428
1429 updateCursorAsync();
1430}
1431
1432void SurfaceFlinger::updateCursorAsync()
1433{
1434 HWComposer& hwc(getHwComposer());
1435 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1436 sp<const DisplayDevice> hw(mDisplays[dpy]);
1437 const int32_t id = hw->getHwcDisplayId();
1438 if (id < 0) {
1439 continue;
1440 }
1441 const Vector< sp<Layer> >& currentLayers(
1442 hw->getVisibleLayersSortedByZ());
1443 const size_t count = currentLayers.size();
1444 HWComposer::LayerListIterator cur = hwc.begin(id);
1445 const HWComposer::LayerListIterator end = hwc.end(id);
1446 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1447 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1448 continue;
1449 }
1450 const sp<Layer>& layer(currentLayers[i]);
1451 Rect cursorPos = layer->getPosition(hw);
1452 hwc.setCursorPositionAsync(id, cursorPos);
1453 break;
1454 }
1455 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001456}
1457
1458void SurfaceFlinger::commitTransaction()
1459{
1460 if (!mLayersPendingRemoval.isEmpty()) {
1461 // Notify removed layers now that they can't be drawn from
1462 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1463 mLayersPendingRemoval[i]->onRemoved();
1464 }
1465 mLayersPendingRemoval.clear();
1466 }
1467
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001468 // If this transaction is part of a window animation then the next frame
1469 // we composite should be considered an animation as well.
1470 mAnimCompositionPending = mAnimTransactionPending;
1471
Mathias Agopian4fec8732012-06-29 14:12:52 -07001472 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001473 mTransactionPending = false;
1474 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001475 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001476}
1477
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001478void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001479 const LayerVector& currentLayers, uint32_t layerStack,
1480 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481{
Mathias Agopian841cde52012-03-01 15:44:37 -08001482 ATRACE_CALL();
1483
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001484 Region aboveOpaqueLayers;
1485 Region aboveCoveredLayers;
1486 Region dirty;
1487
Mathias Agopian87baae12012-07-31 12:38:26 -07001488 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489
1490 size_t i = currentLayers.size();
1491 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001492 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001493
1494 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001495 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001496
Jesse Hall01e29052013-02-19 16:13:35 -08001497 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001498 if (s.layerStack != layerStack)
1499 continue;
1500
Mathias Agopianab028732010-03-16 16:41:46 -07001501 /*
1502 * opaqueRegion: area of a surface that is fully opaque.
1503 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001504 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001505
1506 /*
1507 * visibleRegion: area of a surface that is visible on screen
1508 * and not fully transparent. This is essentially the layer's
1509 * footprint minus the opaque regions above it.
1510 * Areas covered by a translucent surface are considered visible.
1511 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001512 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001513
1514 /*
1515 * coveredRegion: area of a surface that is covered by all
1516 * visible regions above it (which includes the translucent areas).
1517 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001519
Jesse Halla8026d22012-09-25 13:25:04 -07001520 /*
1521 * transparentRegion: area of a surface that is hinted to be completely
1522 * transparent. This is only used to tell when the layer has no visible
1523 * non-transparent regions and can be removed from the layer list. It
1524 * does not affect the visibleRegion of this layer or any layers
1525 * beneath it. The hint may not be correct if apps don't respect the
1526 * SurfaceView restrictions (which, sadly, some don't).
1527 */
1528 Region transparentRegion;
1529
Mathias Agopianab028732010-03-16 16:41:46 -07001530
1531 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001532 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001533 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001534 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001535 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001536 if (!visibleRegion.isEmpty()) {
1537 // Remove the transparent area from the visible region
1538 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001539 const Transform tr(s.transform);
1540 if (tr.transformed()) {
1541 if (tr.preserveRects()) {
1542 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001543 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001544 } else {
1545 // transformation too complex, can't do the
1546 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001547 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001548 }
1549 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001550 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001551 }
Mathias Agopianab028732010-03-16 16:41:46 -07001552 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001553
Mathias Agopianab028732010-03-16 16:41:46 -07001554 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001555 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001556 if (s.alpha==255 && !translucent &&
1557 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1558 // the opaque region is the layer's footprint
1559 opaqueRegion = visibleRegion;
1560 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001561 }
1562 }
1563
Mathias Agopianab028732010-03-16 16:41:46 -07001564 // Clip the covered region to the visible region
1565 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1566
1567 // Update aboveCoveredLayers for next (lower) layer
1568 aboveCoveredLayers.orSelf(visibleRegion);
1569
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001570 // subtract the opaque region covered by the layers above us
1571 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001572
1573 // compute this layer's dirty region
1574 if (layer->contentDirty) {
1575 // we need to invalidate the whole region
1576 dirty = visibleRegion;
1577 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001578 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001579 layer->contentDirty = false;
1580 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001581 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001582 * the exposed region consists of two components:
1583 * 1) what's VISIBLE now and was COVERED before
1584 * 2) what's EXPOSED now less what was EXPOSED before
1585 *
1586 * note that (1) is conservative, we start with the whole
1587 * visible region but only keep what used to be covered by
1588 * something -- which mean it may have been exposed.
1589 *
1590 * (2) handles areas that were not covered by anything but got
1591 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001592 */
Mathias Agopianab028732010-03-16 16:41:46 -07001593 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001594 const Region oldVisibleRegion = layer->visibleRegion;
1595 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001596 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1597 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001598 }
1599 dirty.subtractSelf(aboveOpaqueLayers);
1600
1601 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001602 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001603
Mathias Agopianab028732010-03-16 16:41:46 -07001604 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001605 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001606
Jesse Halla8026d22012-09-25 13:25:04 -07001607 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001608 layer->setVisibleRegion(visibleRegion);
1609 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001610 layer->setVisibleNonTransparentRegion(
1611 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001612 }
1613
Mathias Agopian87baae12012-07-31 12:38:26 -07001614 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001615}
1616
Mathias Agopian87baae12012-07-31 12:38:26 -07001617void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1618 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001619 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001620 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1621 if (hw->getLayerStack() == layerStack) {
1622 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001623 }
1624 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001625}
1626
1627void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001628{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001629 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001630
Mathias Agopian4fec8732012-06-29 14:12:52 -07001631 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001632 const LayerVector& layers(mDrawingState.layersSortedByZ);
1633 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001634 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001635 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001636 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001637 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001638 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001639 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001640
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001641 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001642}
1643
Mathias Agopianad456f92011-01-13 17:53:01 -08001644void SurfaceFlinger::invalidateHwcGeometry()
1645{
1646 mHwWorkListDirty = true;
1647}
1648
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001649
Mathias Agopiancd60f992012-08-16 16:28:27 -07001650void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001651 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652{
Dan Stoza71433162014-02-04 16:22:36 -08001653 // We only need to actually compose the display if:
1654 // 1) It is being handled by hardware composer, which may need this to
1655 // keep its virtual display state machine in sync, or
1656 // 2) There is work to be done (the dirty region isn't empty)
1657 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1658 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1659 return;
1660 }
1661
Mathias Agopian87baae12012-07-31 12:38:26 -07001662 Region dirtyRegion(inDirtyRegion);
1663
Mathias Agopianb8a55602009-06-26 19:06:36 -07001664 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001665 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001666
Mathias Agopian42977342012-08-05 00:40:46 -07001667 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001668 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001669 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1670 // takes a rectangle, we must make sure to update that whole
1671 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001672 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001673 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001674 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001675 // We need to redraw the rectangle that will be updated
1676 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001677 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001678 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001679 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001680 } else {
1681 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001682 dirtyRegion.set(hw->bounds());
1683 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001684 }
1685 }
1686
Alan Viverette9c5a3332013-09-12 20:04:35 -07001687 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Mathias Agopianff2ed702013-09-01 21:36:12 -07001688 doComposeSurfaces(hw, dirtyRegion);
1689 } else {
1690 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001691 mat4 colorMatrix = mColorMatrix;
1692 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001693 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001694 }
1695 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001696 doComposeSurfaces(hw, dirtyRegion);
1697 engine.endGroup();
1698 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001699
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001700 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001701 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001702
1703 // swap buffers (presentation)
1704 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705}
1706
Mathias Agopiancd60f992012-08-16 16:28:27 -07001707void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001708{
Mathias Agopian3f844832013-08-07 21:24:32 -07001709 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001710 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001711 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001712 HWComposer::LayerListIterator cur = hwc.begin(id);
1713 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001714
Jesse Halld05a17f2013-09-30 16:49:30 -07001715 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001716 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001717 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001718 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1719 hw->getDisplayName().string());
1720 return;
1721 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001722
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001723 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001724 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001725 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001726 // when using overlays, we assume a fully transparent framebuffer
1727 // NOTE: we could reduce how much we need to clear, for instance
1728 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001729 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001730 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001731 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001732 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001733 // we start with the whole screen area
1734 const Region bounds(hw->getBounds());
1735
1736 // we remove the scissor part
1737 // we're left with the letterbox region
1738 // (common case is that letterbox ends-up being empty)
1739 const Region letterbox(bounds.subtract(hw->getScissor()));
1740
1741 // compute the area to clear
1742 Region region(hw->undefinedRegion.merge(letterbox));
1743
1744 // but limit it to the dirty region
1745 region.andSelf(dirty);
1746
Mathias Agopianb9494d52012-04-18 02:28:45 -07001747 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001748 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001749 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001750 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001751 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001752 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001753
Mathias Agopian766dc492012-10-30 18:08:06 -07001754 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1755 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001756 // scissor on the main display. It should never be needed
1757 // anyways (though in theory it could since the API allows it).
1758 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001759 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001760 if (scissor != bounds) {
1761 // scissor doesn't match the screen's dimensions, so we
1762 // need to clear everything outside of it and enable
1763 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001764
Mathias Agopianf45c5102012-10-24 16:29:17 -07001765 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001766 const uint32_t height = hw->getHeight();
1767 engine.setScissor(scissor.left, height - scissor.bottom,
1768 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001769 }
1770 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001771 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001772
Mathias Agopian85d751c2012-08-29 16:59:24 -07001773 /*
1774 * and then, render the layers targeted at the framebuffer
1775 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001776
Mathias Agopian13127d82013-03-05 17:47:11 -08001777 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001778 const size_t count = layers.size();
1779 const Transform& tr = hw->getTransform();
1780 if (cur != end) {
1781 // we're using h/w composer
1782 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001783 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001784 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001785 if (!clip.isEmpty()) {
1786 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07001787 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07001788 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001789 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001790 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1791 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001792 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001793 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001794 // never clear the very first layer since we're
1795 // guaranteed the FB is already cleared
1796 layer->clearWithOpenGL(hw, clip);
1797 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001798 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001799 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001800 case HWC_FRAMEBUFFER: {
1801 layer->draw(hw, clip);
1802 break;
1803 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001804 case HWC_FRAMEBUFFER_TARGET: {
1805 // this should not happen as the iterator shouldn't
1806 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001807 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001808 break;
1809 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001810 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001811 }
1812 layer->setAcquireFence(hw, *cur);
1813 }
1814 } else {
1815 // we're not using h/w composer
1816 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001817 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001818 const Region clip(dirty.intersect(
1819 tr.transform(layer->visibleRegion)));
1820 if (!clip.isEmpty()) {
1821 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001822 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001823 }
1824 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001825
1826 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001827 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001828}
1829
Mathias Agopian3f844832013-08-07 21:24:32 -07001830void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001831 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001832 RenderEngine& engine(getRenderEngine());
1833 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001834}
1835
Mathias Agopianac9fa422013-02-11 16:40:36 -08001836void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1837 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001838 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001839 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001840{
Mathias Agopian96f08192010-06-02 23:28:45 -07001841 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001842 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001843
Mathias Agopian96f08192010-06-02 23:28:45 -07001844 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001845 Mutex::Autolock _l(mStateLock);
1846 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001847 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001848}
1849
Mathias Agopian3f844832013-08-07 21:24:32 -07001850status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001851 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001852 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001853 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001854 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001855 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001856 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001857 return NO_ERROR;
1858 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001859 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001860}
1861
Dan Stozac7014012014-02-14 15:03:43 -08001862uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001863 return android_atomic_release_load(&mTransactionFlags);
1864}
1865
Mathias Agopian3f844832013-08-07 21:24:32 -07001866uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001867 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1868}
1869
Mathias Agopian3f844832013-08-07 21:24:32 -07001870uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001871 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1872 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001873 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001874 }
1875 return old;
1876}
1877
Mathias Agopian8b33f032012-07-24 20:43:54 -07001878void SurfaceFlinger::setTransactionState(
1879 const Vector<ComposerState>& state,
1880 const Vector<DisplayState>& displays,
1881 uint32_t flags)
1882{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001883 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001884 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001885 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001886
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001887 if (flags & eAnimation) {
1888 // For window updates that are part of an animation we must wait for
1889 // previous animation "frames" to be handled.
1890 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001891 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001892 if (CC_UNLIKELY(err != NO_ERROR)) {
1893 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001894 // caller after a few seconds.
1895 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1896 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001897 mAnimTransactionPending = false;
1898 break;
1899 }
1900 }
1901 }
1902
Mathias Agopiane57f2922012-08-09 16:29:12 -07001903 size_t count = displays.size();
1904 for (size_t i=0 ; i<count ; i++) {
1905 const DisplayState& s(displays[i]);
1906 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001907 }
1908
Mathias Agopiane57f2922012-08-09 16:29:12 -07001909 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001910 for (size_t i=0 ; i<count ; i++) {
1911 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001912 // Here we need to check that the interface we're given is indeed
1913 // one of our own. A malicious client could give us a NULL
1914 // IInterface, or one of its own or even one of our own but a
1915 // different type. All these situations would cause us to crash.
1916 //
1917 // NOTE: it would be better to use RTTI as we could directly check
1918 // that we have a Client*. however, RTTI is disabled in Android.
1919 if (s.client != NULL) {
1920 sp<IBinder> binder = s.client->asBinder();
1921 if (binder != NULL) {
1922 String16 desc(binder->getInterfaceDescriptor());
1923 if (desc == ISurfaceComposerClient::descriptor) {
1924 sp<Client> client( static_cast<Client *>(s.client.get()) );
1925 transactionFlags |= setClientStateLocked(client, s.state);
1926 }
1927 }
1928 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001929 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001930
Mathias Agopian386aa982011-11-07 21:58:03 -08001931 if (transactionFlags) {
1932 // this triggers the transaction
1933 setTransactionFlags(transactionFlags);
1934
1935 // if this is a synchronous transaction, wait for it to take effect
1936 // before returning.
1937 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001938 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001939 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001940 if (flags & eAnimation) {
1941 mAnimTransactionPending = true;
1942 }
1943 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001944 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1945 if (CC_UNLIKELY(err != NO_ERROR)) {
1946 // just in case something goes wrong in SF, return to the
1947 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001948 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1949 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001950 break;
1951 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001952 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001953 }
1954}
1955
Mathias Agopiane57f2922012-08-09 16:29:12 -07001956uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1957{
Jesse Hall9a143922012-10-04 16:29:19 -07001958 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1959 if (dpyIdx < 0)
1960 return 0;
1961
Mathias Agopiane57f2922012-08-09 16:29:12 -07001962 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001963 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001964 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001965 const uint32_t what = s.what;
1966 if (what & DisplayState::eSurfaceChanged) {
1967 if (disp.surface->asBinder() != s.surface->asBinder()) {
1968 disp.surface = s.surface;
1969 flags |= eDisplayTransactionNeeded;
1970 }
1971 }
1972 if (what & DisplayState::eLayerStackChanged) {
1973 if (disp.layerStack != s.layerStack) {
1974 disp.layerStack = s.layerStack;
1975 flags |= eDisplayTransactionNeeded;
1976 }
1977 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001978 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001979 if (disp.orientation != s.orientation) {
1980 disp.orientation = s.orientation;
1981 flags |= eDisplayTransactionNeeded;
1982 }
1983 if (disp.frame != s.frame) {
1984 disp.frame = s.frame;
1985 flags |= eDisplayTransactionNeeded;
1986 }
1987 if (disp.viewport != s.viewport) {
1988 disp.viewport = s.viewport;
1989 flags |= eDisplayTransactionNeeded;
1990 }
1991 }
Michael Lentine47e45402014-07-18 15:34:25 -07001992 if (what & DisplayState::eDisplaySizeChanged) {
1993 if (disp.width != s.width) {
1994 disp.width = s.width;
1995 flags |= eDisplayTransactionNeeded;
1996 }
1997 if (disp.height != s.height) {
1998 disp.height = s.height;
1999 flags |= eDisplayTransactionNeeded;
2000 }
2001 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002002 }
2003 return flags;
2004}
2005
2006uint32_t SurfaceFlinger::setClientStateLocked(
2007 const sp<Client>& client,
2008 const layer_state_t& s)
2009{
2010 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002011 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002012 if (layer != 0) {
2013 const uint32_t what = s.what;
2014 if (what & layer_state_t::ePositionChanged) {
2015 if (layer->setPosition(s.x, s.y))
2016 flags |= eTraversalNeeded;
2017 }
2018 if (what & layer_state_t::eLayerChanged) {
2019 // NOTE: index needs to be calculated before we update the state
2020 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2021 if (layer->setLayer(s.z)) {
2022 mCurrentState.layersSortedByZ.removeAt(idx);
2023 mCurrentState.layersSortedByZ.add(layer);
2024 // we need traversal (state changed)
2025 // AND transaction (list changed)
2026 flags |= eTransactionNeeded|eTraversalNeeded;
2027 }
2028 }
2029 if (what & layer_state_t::eSizeChanged) {
2030 if (layer->setSize(s.w, s.h)) {
2031 flags |= eTraversalNeeded;
2032 }
2033 }
2034 if (what & layer_state_t::eAlphaChanged) {
2035 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2036 flags |= eTraversalNeeded;
2037 }
2038 if (what & layer_state_t::eMatrixChanged) {
2039 if (layer->setMatrix(s.matrix))
2040 flags |= eTraversalNeeded;
2041 }
2042 if (what & layer_state_t::eTransparentRegionChanged) {
2043 if (layer->setTransparentRegionHint(s.transparentRegion))
2044 flags |= eTraversalNeeded;
2045 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08002046 if ((what & layer_state_t::eVisibilityChanged) ||
2047 (what & layer_state_t::eOpacityChanged)) {
2048 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07002049 if (layer->setFlags(s.flags, s.mask))
2050 flags |= eTraversalNeeded;
2051 }
2052 if (what & layer_state_t::eCropChanged) {
2053 if (layer->setCrop(s.crop))
2054 flags |= eTraversalNeeded;
2055 }
2056 if (what & layer_state_t::eLayerStackChanged) {
2057 // NOTE: index needs to be calculated before we update the state
2058 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2059 if (layer->setLayerStack(s.layerStack)) {
2060 mCurrentState.layersSortedByZ.removeAt(idx);
2061 mCurrentState.layersSortedByZ.add(layer);
2062 // we need traversal (state changed)
2063 // AND transaction (list changed)
2064 flags |= eTransactionNeeded|eTraversalNeeded;
2065 }
2066 }
2067 }
2068 return flags;
2069}
2070
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002071status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002072 const String8& name,
2073 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002074 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2075 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002076{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002077 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002078 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002079 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002080 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002081 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002082 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002083
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002084 status_t result = NO_ERROR;
2085
2086 sp<Layer> layer;
2087
Mathias Agopian3165cc22012-08-08 19:42:09 -07002088 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2089 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002090 result = createNormalLayer(client,
2091 name, w, h, flags, format,
2092 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002093 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002094 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002095 result = createDimLayer(client,
2096 name, w, h, flags,
2097 handle, gbp, &layer);
2098 break;
2099 default:
2100 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002101 break;
2102 }
2103
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002104 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002105 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002106 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002107 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002108 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002109}
2110
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002111status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2112 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2113 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002114{
2115 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002116 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002117 case PIXEL_FORMAT_TRANSPARENT:
2118 case PIXEL_FORMAT_TRANSLUCENT:
2119 format = PIXEL_FORMAT_RGBA_8888;
2120 break;
2121 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002122 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002123 break;
2124 }
2125
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002126 *outLayer = new Layer(this, client, name, w, h, flags);
2127 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2128 if (err == NO_ERROR) {
2129 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002130 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002131 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002132
2133 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2134 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002135}
2136
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002137status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2138 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2139 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002140{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002141 *outLayer = new LayerDim(this, client, name, w, h, flags);
2142 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002143 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002144 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002145}
2146
Mathias Agopianac9fa422013-02-11 16:40:36 -08002147status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002148{
Mathias Agopian67106042013-03-14 19:18:13 -07002149 // called by the window manager when it wants to remove a Layer
2150 status_t err = NO_ERROR;
2151 sp<Layer> l(client->getLayerUser(handle));
2152 if (l != NULL) {
2153 err = removeLayer(l);
2154 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2155 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002156 }
2157 return err;
2158}
2159
Mathias Agopian13127d82013-03-05 17:47:11 -08002160status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002161{
Mathias Agopian67106042013-03-14 19:18:13 -07002162 // called by ~LayerCleaner() when all references to the IBinder (handle)
2163 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002164 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002165 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002166 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002167 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002168 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002169 "error removing layer=%p (%s)", l.get(), strerror(-err));
2170 }
2171 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002172}
2173
Mathias Agopianb60314a2012-04-10 22:09:54 -07002174// ---------------------------------------------------------------------------
2175
Andy McFadden13a082e2012-08-24 10:16:42 -07002176void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002177 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002178 Vector<ComposerState> state;
2179 Vector<DisplayState> displays;
2180 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002181 d.what = DisplayState::eDisplayProjectionChanged |
2182 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002183 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002184 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002185 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002186 d.frame.makeInvalid();
2187 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002188 d.width = 0;
2189 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002190 displays.add(d);
2191 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002192 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002193
2194 const nsecs_t period =
2195 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2196 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002197}
2198
2199void SurfaceFlinger::initializeDisplays() {
2200 class MessageScreenInitialized : public MessageBase {
2201 SurfaceFlinger* flinger;
2202 public:
2203 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2204 virtual bool handler() {
2205 flinger->onInitializeDisplays();
2206 return true;
2207 }
2208 };
2209 sp<MessageBase> msg = new MessageScreenInitialized(this);
2210 postMessageAsync(msg); // we may be called from main thread, use async message
2211}
2212
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002213void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2214 int mode) {
2215 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2216 this);
2217 int32_t type = hw->getDisplayType();
2218 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002219
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002220 if (mode == currentMode) {
2221 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002222 return;
2223 }
2224
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002225 hw->setPowerMode(mode);
2226 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2227 ALOGW("Trying to set power mode for virtual display");
2228 return;
2229 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002230
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002231 if (currentMode == HWC_POWER_MODE_OFF) {
2232 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002233 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2234 // FIXME: eventthread only knows about the main display right now
2235 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002236 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002237 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002238
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002239 mVisibleRegionsDirty = true;
2240 repaintEverything();
2241 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002242 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002243 disableHardwareVsync(true); // also cancels any in-progress resync
2244
Mathias Agopiancde87a32012-09-13 14:09:01 -07002245 // FIXME: eventthread only knows about the main display right now
2246 mEventThread->onScreenReleased();
2247 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002248
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002249 getHwComposer().setPowerMode(type, mode);
2250 mVisibleRegionsDirty = true;
2251 // from this point on, SF will stop drawing on this display
2252 } else {
2253 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002254 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002255}
2256
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002257void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2258 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002259 SurfaceFlinger& mFlinger;
2260 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002261 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002262 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002263 MessageSetPowerMode(SurfaceFlinger& flinger,
2264 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2265 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002266 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002267 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002268 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002269 ALOGE("Attempt to set power mode = %d for null display %p",
2270 mDisplay.get(), mMode);
Jesse Hall9e663de2013-08-16 14:28:37 -07002271 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002272 ALOGW("Attempt to set power mode = %d for virtual display",
2273 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002274 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002275 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002276 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002277 return true;
2278 }
2279 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002280 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002281 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002282}
2283
2284// ---------------------------------------------------------------------------
2285
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002286status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2287{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002288 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002289
Mathias Agopianbd115332013-04-18 16:41:04 -07002290 IPCThreadState* ipc = IPCThreadState::self();
2291 const int pid = ipc->getCallingPid();
2292 const int uid = ipc->getCallingUid();
2293 if ((uid != AID_SHELL) &&
2294 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002295 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002296 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002297 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002298 // Try to get the main lock, but don't insist if we can't
2299 // (this would indicate SF is stuck, but we want to be able to
2300 // print something in dumpsys).
2301 int retry = 3;
2302 while (mStateLock.tryLock()<0 && --retry>=0) {
2303 usleep(1000000);
2304 }
2305 const bool locked(retry >= 0);
2306 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002307 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002308 "SurfaceFlinger appears to be unresponsive, "
2309 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002310 }
2311
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002312 bool dumpAll = true;
2313 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002314 size_t numArgs = args.size();
2315 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002316 if ((index < numArgs) &&
2317 (args[index] == String16("--list"))) {
2318 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002319 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002320 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002321 }
2322
2323 if ((index < numArgs) &&
2324 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002325 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002326 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002327 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002328 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002329
2330 if ((index < numArgs) &&
2331 (args[index] == String16("--latency-clear"))) {
2332 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002333 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002334 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002335 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002336
2337 if ((index < numArgs) &&
2338 (args[index] == String16("--dispsync"))) {
2339 index++;
2340 mPrimaryDispSync.dump(result);
2341 dumpAll = false;
2342 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002343 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002344
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002345 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002346 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002347 }
2348
Mathias Agopian9795c422009-08-26 16:36:26 -07002349 if (locked) {
2350 mStateLock.unlock();
2351 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002352 }
2353 write(fd, result.string(), result.size());
2354 return NO_ERROR;
2355}
2356
Dan Stozac7014012014-02-14 15:03:43 -08002357void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2358 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002359{
2360 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2361 const size_t count = currentLayers.size();
2362 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002363 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002364 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002365 }
2366}
2367
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002368void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002369 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002370{
2371 String8 name;
2372 if (index < args.size()) {
2373 name = String8(args[index]);
2374 index++;
2375 }
2376
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002377 const nsecs_t period =
2378 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002379 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002380
2381 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002382 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002383 } else {
2384 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2385 const size_t count = currentLayers.size();
2386 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002387 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002388 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002389 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002390 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002391 }
2392 }
2393}
2394
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002395void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002396 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002397{
2398 String8 name;
2399 if (index < args.size()) {
2400 name = String8(args[index]);
2401 index++;
2402 }
2403
2404 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2405 const size_t count = currentLayers.size();
2406 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002407 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002408 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002409 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002410 }
2411 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002412
Svetoslavd85084b2014-03-20 10:28:31 -07002413 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002414}
2415
Jamie Gennis6547ff42013-07-16 20:12:42 -07002416// This should only be called from the main thread. Otherwise it would need
2417// the lock and should use mCurrentState rather than mDrawingState.
2418void SurfaceFlinger::logFrameStats() {
2419 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2420 const size_t count = drawingLayers.size();
2421 for (size_t i=0 ; i<count ; i++) {
2422 const sp<Layer>& layer(drawingLayers[i]);
2423 layer->logFrameStats();
2424 }
2425
2426 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2427}
2428
Andy McFadden4803b742012-09-24 19:07:20 -07002429/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2430{
2431 static const char* config =
2432 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002433#ifdef HAS_CONTEXT_PRIORITY
2434 " HAS_CONTEXT_PRIORITY"
2435#endif
2436#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2437 " NEVER_DEFAULT_TO_ASYNC_MODE"
2438#endif
2439#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2440 " TARGET_DISABLE_TRIPLE_BUFFERING"
2441#endif
2442 "]";
2443 result.append(config);
2444}
2445
Mathias Agopian74d211a2013-04-22 16:55:35 +02002446void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2447 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002448{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002449 bool colorize = false;
2450 if (index < args.size()
2451 && (args[index] == String16("--color"))) {
2452 colorize = true;
2453 index++;
2454 }
2455
2456 Colorizer colorizer(colorize);
2457
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002458 // figure out if we're stuck somewhere
2459 const nsecs_t now = systemTime();
2460 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2461 const nsecs_t inTransaction(mDebugInTransaction);
2462 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2463 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2464
2465 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002466 * Dump library configuration.
2467 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002468
2469 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002470 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002471 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002472 appendSfConfigString(result);
2473 appendUiConfigString(result);
2474 appendGuiConfigString(result);
2475 result.append("\n");
2476
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002477 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002478 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002479 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002480 result.append(SyncFeatures::getInstance().toString());
2481 result.append("\n");
2482
Andy McFadden41d67d72014-04-25 16:58:34 -07002483 colorizer.bold(result);
2484 result.append("DispSync configuration: ");
2485 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002486 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2487 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002488 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2489 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2490 result.append("\n");
2491
Andy McFadden4803b742012-09-24 19:07:20 -07002492 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002493 * Dump the visible layer list
2494 */
2495 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2496 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002497 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002498 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002499 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002500 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002501 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002502 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002503 }
2504
2505 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002506 * Dump Display state
2507 */
2508
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002509 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002510 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002511 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002512 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2513 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002514 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002515 }
2516
2517 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002518 * Dump SurfaceFlinger global state
2519 */
2520
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002521 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002522 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002523 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002524
Mathias Agopian888c8222012-08-04 21:10:38 -07002525 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002526 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002527
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002528 colorizer.bold(result);
2529 result.appendFormat("EGL implementation : %s\n",
2530 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2531 colorizer.reset(result);
2532 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002533 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002534
Mathias Agopian875d8e12013-06-07 15:35:48 -07002535 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002536
Mathias Agopian42977342012-08-05 00:40:46 -07002537 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002538 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2539 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002540 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002541 " last eglSwapBuffers() time: %f us\n"
2542 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002543 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002544 " refresh-rate : %f fps\n"
2545 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002546 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002547 " gpu_to_cpu_unsupported : %d\n"
2548 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002549 mLastSwapBufferTime/1000.0,
2550 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002551 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002552 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2553 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002554 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002555 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002556
Mathias Agopian74d211a2013-04-22 16:55:35 +02002557 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002558 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002559
Mathias Agopian74d211a2013-04-22 16:55:35 +02002560 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002561 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002562
2563 /*
2564 * VSYNC state
2565 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002566 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002567
2568 /*
2569 * Dump HWComposer state
2570 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002571 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002572 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002573 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002574 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002575 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002576 (mDebugDisableHWC || mDebugRegion || mDaltonize
2577 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002578 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002579
2580 /*
2581 * Dump gralloc state
2582 */
2583 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2584 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002585}
2586
Mathias Agopian13127d82013-03-05 17:47:11 -08002587const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002588SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002589 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002590 wp<IBinder> dpy;
2591 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2592 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2593 dpy = mDisplays.keyAt(i);
2594 break;
2595 }
2596 }
2597 if (dpy == NULL) {
2598 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2599 // Just use the primary display so we have something to return
2600 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2601 }
2602 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002603}
2604
Keun young Park63f165f2012-08-31 10:53:36 -07002605bool SurfaceFlinger::startDdmConnection()
2606{
2607 void* libddmconnection_dso =
2608 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2609 if (!libddmconnection_dso) {
2610 return false;
2611 }
2612 void (*DdmConnection_start)(const char* name);
2613 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07002614 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07002615 if (!DdmConnection_start) {
2616 dlclose(libddmconnection_dso);
2617 return false;
2618 }
2619 (*DdmConnection_start)(getServiceName());
2620 return true;
2621}
2622
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002623status_t SurfaceFlinger::onTransact(
2624 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2625{
2626 switch (code) {
2627 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002628 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002629 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002630 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002631 case CLEAR_ANIMATION_FRAME_STATS:
2632 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002633 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002634 {
2635 // codes that require permission check
2636 IPCThreadState* ipc = IPCThreadState::self();
2637 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002638 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002639 if ((uid != AID_GRAPHICS) &&
2640 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002641 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002642 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2643 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002644 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002645 break;
2646 }
2647 case CAPTURE_SCREEN:
2648 {
2649 // codes that require permission check
2650 IPCThreadState* ipc = IPCThreadState::self();
2651 const int pid = ipc->getCallingPid();
2652 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002653 if ((uid != AID_GRAPHICS) &&
2654 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002655 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002656 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2657 return PERMISSION_DENIED;
2658 }
2659 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002660 }
2661 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002662
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002663 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2664 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002665 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002666 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002667 IPCThreadState* ipc = IPCThreadState::self();
2668 const int pid = ipc->getCallingPid();
2669 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002670 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002671 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002672 return PERMISSION_DENIED;
2673 }
2674 int n;
2675 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002676 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002677 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002678 return NO_ERROR;
2679 case 1002: // SHOW_UPDATES
2680 n = data.readInt32();
2681 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002682 invalidateHwcGeometry();
2683 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002684 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002685 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002686 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002687 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002688 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002689 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002690 setTransactionFlags(
2691 eTransactionNeeded|
2692 eDisplayTransactionNeeded|
2693 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002694 return NO_ERROR;
2695 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002696 case 1006:{ // send empty update
2697 signalRefresh();
2698 return NO_ERROR;
2699 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002700 case 1008: // toggle use of hw composer
2701 n = data.readInt32();
2702 mDebugDisableHWC = n ? 1 : 0;
2703 invalidateHwcGeometry();
2704 repaintEverything();
2705 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002706 case 1009: // toggle use of transform hint
2707 n = data.readInt32();
2708 mDebugDisableTransformHint = n ? 1 : 0;
2709 invalidateHwcGeometry();
2710 repaintEverything();
2711 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002712 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002713 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002714 reply->writeInt32(0);
2715 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002716 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002717 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002718 return NO_ERROR;
2719 case 1013: {
2720 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002721 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2722 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002723 return NO_ERROR;
2724 }
2725 case 1014: {
2726 // daltonize
2727 n = data.readInt32();
2728 switch (n % 10) {
2729 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2730 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2731 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2732 }
2733 if (n >= 10) {
2734 mDaltonizer.setMode(Daltonizer::correction);
2735 } else {
2736 mDaltonizer.setMode(Daltonizer::simulation);
2737 }
2738 mDaltonize = n > 0;
2739 invalidateHwcGeometry();
2740 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002741 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002742 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002743 case 1015: {
2744 // apply a color matrix
2745 n = data.readInt32();
2746 mHasColorMatrix = n ? 1 : 0;
2747 if (n) {
2748 // color matrix is sent as mat3 matrix followed by vec3
2749 // offset, then packed into a mat4 where the last row is
2750 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002751 for (size_t i = 0 ; i < 4; i++) {
2752 for (size_t j = 0; j < 4; j++) {
2753 mColorMatrix[i][j] = data.readFloat();
2754 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002755 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002756 } else {
2757 mColorMatrix = mat4();
2758 }
2759 invalidateHwcGeometry();
2760 repaintEverything();
2761 return NO_ERROR;
2762 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002763 // This is an experimental interface
2764 // Needs to be shifted to proper binder interface when we productize
2765 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07002766 n = data.readInt32();
2767 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002768 return NO_ERROR;
2769 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002770 }
2771 }
2772 return err;
2773}
2774
Mathias Agopian53331da2011-08-22 21:44:41 -07002775void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002776 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002777 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002778}
2779
Mathias Agopian59119e62010-10-11 12:37:43 -07002780// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002781// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002782// ---------------------------------------------------------------------------
2783
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002784/* The code below is here to handle b/8734824
2785 *
2786 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07002787 * from the surfaceflinger thread to the calling binder thread, where they
2788 * are executed. This allows the calling thread in the calling process to be
2789 * reused and not depend on having "enough" binder threads to handle the
2790 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002791 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002792class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07002793 /* Parts of GraphicProducerWrapper are run on two different threads,
2794 * communicating by sending messages via Looper but also by shared member
2795 * data. Coherence maintenance is subtle and in places implicit (ugh).
2796 *
2797 * Don't rely on Looper's sendMessage/handleMessage providing
2798 * release/acquire semantics for any data not actually in the Message.
2799 * Data going from surfaceflinger to binder threads needs to be
2800 * synchronized explicitly.
2801 *
2802 * Barrier open/wait do provide release/acquire semantics. This provides
2803 * implicit synchronization for data coming back from binder to
2804 * surfaceflinger threads.
2805 */
2806
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002807 sp<IGraphicBufferProducer> impl;
2808 sp<Looper> looper;
2809 status_t result;
2810 bool exitPending;
2811 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07002812 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002813 uint32_t code;
2814 Parcel const* data;
2815 Parcel* reply;
2816
2817 enum {
2818 MSG_API_CALL,
2819 MSG_EXIT
2820 };
2821
2822 /*
Jesse Hallb154c422014-07-13 12:47:02 -07002823 * Called on surfaceflinger thread. This is called by our "fake"
2824 * BpGraphicBufferProducer. We package the data and reply Parcel and
2825 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002826 */
2827 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08002828 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002829 this->code = code;
2830 this->data = &data;
2831 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002832 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07002833 // if we've exited, we run the message synchronously right here.
2834 // note (JH): as far as I can tell from looking at the code, this
2835 // never actually happens. if it does, i'm not sure if it happens
2836 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002837 handleMessage(Message(MSG_API_CALL));
2838 } else {
2839 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07002840 // Prevent stores to this->{code, data, reply} from being
2841 // reordered later than the construction of Message.
2842 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002843 looper->sendMessage(this, Message(MSG_API_CALL));
2844 barrier.wait();
2845 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08002846 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002847 }
2848
2849 /*
Jesse Hallb154c422014-07-13 12:47:02 -07002850 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002851 * call the real BpGraphicBufferProducer.
2852 */
2853 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07002854 int what = message.what;
2855 // Prevent reads below from happening before the read from Message
2856 atomic_thread_fence(memory_order_acquire);
2857 if (what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08002858 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002859 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07002860 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002861 exitRequested = true;
2862 }
2863 }
2864
2865public:
Jesse Hallb154c422014-07-13 12:47:02 -07002866 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
2867 : impl(impl),
2868 looper(new Looper(true)),
2869 exitPending(false),
2870 exitRequested(false)
2871 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002872
Jesse Hallb154c422014-07-13 12:47:02 -07002873 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002874 status_t waitForResponse() {
2875 do {
2876 looper->pollOnce(-1);
2877 } while (!exitRequested);
2878 return result;
2879 }
2880
Jesse Hallb154c422014-07-13 12:47:02 -07002881 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002882 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002883 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002884 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07002885 // Ensure this->result is visible to the binder thread before it
2886 // handles the message.
2887 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002888 looper->sendMessage(this, Message(MSG_EXIT));
2889 }
2890};
2891
2892
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002893status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2894 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002895 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002896 uint32_t minLayerZ, uint32_t maxLayerZ,
2897 bool useIdentityTransform) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002898
2899 if (CC_UNLIKELY(display == 0))
2900 return BAD_VALUE;
2901
2902 if (CC_UNLIKELY(producer == 0))
2903 return BAD_VALUE;
2904
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002905 // if we have secure windows on this display, never allow the screen capture
2906 // unless the producer interface is local (i.e.: we can take a screenshot for
2907 // ourselves).
2908 if (!producer->asBinder()->localBinder()) {
2909 Mutex::Autolock _l(mStateLock);
2910 sp<const DisplayDevice> hw(getDisplayDevice(display));
2911 if (hw->getSecureLayerVisible()) {
2912 ALOGW("FB is protected: PERMISSION_DENIED");
2913 return PERMISSION_DENIED;
2914 }
2915 }
2916
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002917 class MessageCaptureScreen : public MessageBase {
2918 SurfaceFlinger* flinger;
2919 sp<IBinder> display;
2920 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07002921 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002922 uint32_t reqWidth, reqHeight;
2923 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08002924 bool useIdentityTransform;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002925 status_t result;
2926 public:
2927 MessageCaptureScreen(SurfaceFlinger* flinger,
2928 const sp<IBinder>& display,
2929 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002930 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002931 uint32_t minLayerZ, uint32_t maxLayerZ,
2932 bool useIdentityTransform)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002933 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07002934 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002935 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08002936 useIdentityTransform(useIdentityTransform),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002937 result(PERMISSION_DENIED)
2938 {
2939 }
2940 status_t getResult() const {
2941 return result;
2942 }
2943 virtual bool handler() {
2944 Mutex::Autolock _l(flinger->mStateLock);
2945 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08002946 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07002947 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Dan Stozac7014012014-02-14 15:03:43 -08002948 useIdentityTransform);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002949 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002950 return true;
2951 }
2952 };
2953
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002954 // make sure to process transactions before screenshots -- a transaction
2955 // might already be pending but scheduled for VSYNC; this guarantees we
2956 // will handle it before the screenshot. When VSYNC finally arrives
2957 // the scheduled transaction will be a no-op. If no transactions are
2958 // scheduled at this time, this will end-up being a no-op as well.
2959 mEventQueue.invalidateTransactionNow();
2960
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002961 // this creates a "fake" BBinder which will serve as a "fake" remote
2962 // binder to receive the marshaled calls and forward them to the
2963 // real remote (a BpGraphicBufferProducer)
2964 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2965
2966 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2967 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002968 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002969 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07002970 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
2971 useIdentityTransform);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002972
2973 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002974 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002975 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002976 }
2977 return res;
2978}
2979
Mathias Agopian180f10d2013-04-10 22:55:41 -07002980
2981void SurfaceFlinger::renderScreenImplLocked(
2982 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07002983 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07002984 uint32_t minLayerZ, uint32_t maxLayerZ,
Dan Stozac7014012014-02-14 15:03:43 -08002985 bool yswap, bool useIdentityTransform)
Mathias Agopian180f10d2013-04-10 22:55:41 -07002986{
2987 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002988 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002989
2990 // get screen geometry
2991 const uint32_t hw_w = hw->getWidth();
2992 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002993 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2994
Dan Stozac1879002014-05-22 15:59:05 -07002995 // if a default or invalid sourceCrop is passed in, set reasonable values
2996 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
2997 !sourceCrop.isValid()) {
2998 sourceCrop.setLeftTop(Point(0, 0));
2999 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3000 }
3001
3002 // ensure that sourceCrop is inside screen
3003 if (sourceCrop.left < 0) {
3004 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3005 }
Dan Stozabe31f442014-06-11 11:20:54 -07003006 if (sourceCrop.right > hw_w) {
3007 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003008 }
3009 if (sourceCrop.top < 0) {
3010 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3011 }
Dan Stozabe31f442014-06-11 11:20:54 -07003012 if (sourceCrop.bottom > hw_h) {
3013 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003014 }
3015
Mathias Agopian180f10d2013-04-10 22:55:41 -07003016 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003017 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003018
3019 // set-up our viewport
Dan Stozac1879002014-05-22 15:59:05 -07003020 engine.setViewportAndProjection(reqWidth, reqHeight, sourceCrop, hw_h, yswap);
Mathias Agopian3f844832013-08-07 21:24:32 -07003021 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003022
3023 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003024 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003025
3026 const LayerVector& layers( mDrawingState.layersSortedByZ );
3027 const size_t count = layers.size();
3028 for (size_t i=0 ; i<count ; ++i) {
3029 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003030 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003031 if (state.layerStack == hw->getLayerStack()) {
3032 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3033 if (layer->isVisible()) {
3034 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08003035 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003036 if (filtering) layer->setFiltering(false);
3037 }
3038 }
3039 }
3040 }
3041
3042 // compositionComplete is needed for older driver
3043 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003044 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003045}
3046
3047
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003048status_t SurfaceFlinger::captureScreenImplLocked(
3049 const sp<const DisplayDevice>& hw,
3050 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003051 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003052 uint32_t minLayerZ, uint32_t maxLayerZ,
3053 bool useIdentityTransform)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003054{
3055 ATRACE_CALL();
3056
Mathias Agopian180f10d2013-04-10 22:55:41 -07003057 // get screen geometry
3058 const uint32_t hw_w = hw->getWidth();
3059 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003060
Mathias Agopian180f10d2013-04-10 22:55:41 -07003061 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3062 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3063 reqWidth, reqHeight, hw_w, hw_h);
3064 return BAD_VALUE;
3065 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003066
Mathias Agopian180f10d2013-04-10 22:55:41 -07003067 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3068 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3069
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003070 // create a surface (because we're a producer, and we need to
3071 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003072 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003073 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003074
3075 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003076 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003077 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3078 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003079
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003080 int err = 0;
3081 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003082 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003083 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3084 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003085
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003086 if (err == NO_ERROR) {
3087 ANativeWindowBuffer* buffer;
3088 /* TODO: Once we have the sync framework everywhere this can use
3089 * server-side waits on the fence that dequeueBuffer returns.
3090 */
3091 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3092 if (result == NO_ERROR) {
3093 // create an EGLImage from the buffer so we can later
3094 // turn it into a texture
3095 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3096 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3097 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003098 // this binds the given EGLImage as a framebuffer for the
3099 // duration of this scope.
3100 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3101 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003102 // this will in fact render into our dequeued buffer
3103 // via an FBO, which means we didn't have to create
3104 // an EGLSurface and therefore we're not
3105 // dependent on the context's EGLConfig.
Dan Stozac1879002014-05-22 15:59:05 -07003106 renderScreenImplLocked(hw, sourceCrop, reqWidth, reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003107 minLayerZ, maxLayerZ, true, useIdentityTransform);
Mathias Agopiand5556842013-09-19 17:08:37 -07003108
Andy McFadden2d8d1202013-10-09 16:38:02 -07003109 // Create a sync point and wait on it, so we know the buffer is
3110 // ready before we pass it along. We can't trivially call glFlush(),
3111 // so we use a wait flag instead.
3112 // TODO: pass a sync fd to queueBuffer() and let the consumer wait.
3113 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3114 if (sync != EGL_NO_SYNC_KHR) {
3115 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3116 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3117 EGLint eglErr = eglGetError();
3118 eglDestroySyncKHR(mEGLDisplay, sync);
3119 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3120 ALOGW("captureScreen: fence wait timed out");
3121 } else {
3122 ALOGW_IF(eglErr != EGL_SUCCESS,
3123 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3124 }
3125 } else {
3126 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3127 // not fatal
3128 }
3129
Mathias Agopiand5556842013-09-19 17:08:37 -07003130 if (DEBUG_SCREENSHOTS) {
3131 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3132 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3133 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3134 hw, minLayerZ, maxLayerZ);
3135 delete [] pixels;
3136 }
3137
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003138 } else {
3139 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3140 result = INVALID_OPERATION;
3141 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003142 // destroy our image
3143 eglDestroyImageKHR(mEGLDisplay, image);
3144 } else {
3145 result = BAD_VALUE;
3146 }
3147 window->queueBuffer(window, buffer, -1);
3148 }
3149 } else {
3150 result = BAD_VALUE;
3151 }
3152 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3153 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003154
Mathias Agopian74c40c02010-09-29 13:02:36 -07003155 return result;
3156}
3157
Mathias Agopiand5556842013-09-19 17:08:37 -07003158void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3159 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003160 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003161 for (size_t y=0 ; y<h ; y++) {
3162 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3163 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003164 if (p[x] != 0xFF000000) return;
3165 }
3166 }
3167 ALOGE("*** we just took a black screenshot ***\n"
3168 "requested minz=%d, maxz=%d, layerStack=%d",
3169 minLayerZ, maxLayerZ, hw->getLayerStack());
3170 const LayerVector& layers( mDrawingState.layersSortedByZ );
3171 const size_t count = layers.size();
3172 for (size_t i=0 ; i<count ; ++i) {
3173 const sp<Layer>& layer(layers[i]);
3174 const Layer::State& state(layer->getDrawingState());
3175 const bool visible = (state.layerStack == hw->getLayerStack())
3176 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3177 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003178 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003179 visible ? '+' : '-',
3180 i, layer->getName().string(), state.layerStack, state.z,
3181 layer->isVisible(), state.flags, state.alpha);
3182 }
3183 }
3184}
3185
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003186// ---------------------------------------------------------------------------
3187
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003188SurfaceFlinger::LayerVector::LayerVector() {
3189}
3190
3191SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003192 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003193}
3194
3195int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3196 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003197{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003198 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003199 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3200 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003201
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003202 uint32_t ls = l->getCurrentState().layerStack;
3203 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003204 if (ls != rs)
3205 return ls - rs;
3206
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003207 uint32_t lz = l->getCurrentState().z;
3208 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003209 if (lz != rz)
3210 return lz - rz;
3211
3212 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003213}
3214
3215// ---------------------------------------------------------------------------
3216
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003217SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Michael Lentine47e45402014-07-18 15:34:25 -07003218 : type(DisplayDevice::DISPLAY_ID_INVALID), width(0), height(0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003219}
3220
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003221SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Michael Lentine47e45402014-07-18 15:34:25 -07003222 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0), width(0), height(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003223 viewport.makeInvalid();
3224 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003225}
3226
3227// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003228
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003229}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003230
3231
3232#if defined(__gl_h_)
3233#error "don't include gl/gl.h in this file"
3234#endif
3235
3236#if defined(__gl2_h_)
3237#error "don't include gl2/gl2.h in this file"
3238#endif