blob: 40ca8ebf92a18572c04da46600a25e0248ec91a6 [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>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070024
25#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <cutils/log.h>
28#include <cutils/properties.h>
29
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070032#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070033#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034
Mathias Agopianc666cae2012-07-25 18:56:13 -070035#include <ui/DisplayInfo.h>
36
Mathias Agopian921e6ac2012-07-23 23:11:29 -070037#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070038#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070039#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070040#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080041#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080042#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070043
44#include <ui/GraphicBufferAllocator.h>
45#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070046#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080047
Mathias Agopiancde87a32012-09-13 14:09:01 -070048#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <utils/String8.h>
50#include <utils/String16.h>
51#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080052#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian921e6ac2012-07-23 23:11:29 -070054#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070055#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopian3e25fd82013-04-22 17:52:16 +020057#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020059#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080060#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070061#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070062#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070063#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080064#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
Mathias Agopiana4912602012-07-12 14:25:33 -070069#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070070#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070071#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072
Mathias Agopianff2ed702013-09-01 21:36:12 -070073#include "Effects/Daltonizer.h"
74
Mathias Agopian875d8e12013-06-07 15:35:48 -070075#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070076#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070077
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078#define DISPLAY_COUNT 1
79
Mathias Agopianfee2b462013-07-03 12:34:01 -070080/*
81 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
82 * black pixels.
83 */
84#define DEBUG_SCREENSHOTS false
85
Mathias Agopianca088332013-03-28 17:44:13 -070086EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
87
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070089
90// This works around the lack of support for the sync framework on some
91// devices.
92#ifdef RUNNING_WITHOUT_SYNC_FRAMEWORK
93static const bool runningWithoutSyncFramework = true;
94#else
95static const bool runningWithoutSyncFramework = false;
96#endif
97
98// This is the phase offset in nanoseconds of the software vsync event
99// relative to the vsync event reported by HWComposer. The software vsync
100// event is when SurfaceFlinger and Choreographer-based applications run each
101// frame.
102//
103// This phase offset allows adjustment of the minimum latency from application
104// wake-up (by Choregographer) time to the time at which the resulting window
105// image is displayed. This value may be either positive (after the HW vsync)
106// or negative (before the HW vsync). Setting it to 0 will result in a
107// minimum latency of two vsync periods because the app and SurfaceFlinger
108// will run just after the HW vsync. Setting it to a positive number will
109// result in the minimum latency being:
110//
111// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
112//
113// Note that reducing this latency makes it more likely for the applications
114// to not have their window content image ready in time. When this happens
115// the latency will end up being an additional vsync period, and animations
116// will hiccup. Therefore, this latency should be tuned somewhat
117// conservatively (or at least with awareness of the trade-off being made).
118static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
119
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700120// This is the phase offset at which SurfaceFlinger's composition runs.
121static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
122
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123// ---------------------------------------------------------------------------
124
Mathias Agopian99b49842011-06-27 16:05:52 -0700125const String16 sHardwareTest("android.permission.HARDWARE_TEST");
126const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
127const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
128const String16 sDump("android.permission.DUMP");
129
130// ---------------------------------------------------------------------------
131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700133 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700135 mTransactionPending(false),
136 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700137 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700138 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700139 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700142 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800143 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700145 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700146 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700147 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700148 mDebugInSwapBuffers(0),
149 mLastSwapBufferTime(0),
150 mDebugInTransaction(0),
151 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700152 mBootFinished(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700153 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700154 mHWVsyncAvailable(false),
Alan Viverette9c5a3332013-09-12 20:04:35 -0700155 mDaltonize(false),
156 mHasColorMatrix(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157{
Steve Blocka19954a2012-01-04 20:05:49 +0000158 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159
160 // debugging stuff...
161 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700162
Mathias Agopianb4b17302013-03-20 18:36:41 -0700163 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700164 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700165
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 property_get("debug.sf.showupdates", value, "0");
167 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700168
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700169 property_get("debug.sf.ddms", value, "0");
170 mDebugDDMS = atoi(value);
171 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700172 if (!startDdmConnection()) {
173 // start failed, and DDMS debugging not enabled
174 mDebugDDMS = 0;
175 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700176 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700177 ALOGI_IF(mDebugRegion, "showupdates enabled");
178 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179}
180
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800181void SurfaceFlinger::onFirstRef()
182{
183 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800184}
185
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186SurfaceFlinger::~SurfaceFlinger()
187{
Mathias Agopiana4912602012-07-12 14:25:33 -0700188 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
189 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
190 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191}
192
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800193void SurfaceFlinger::binderDied(const wp<IBinder>& who)
194{
195 // the window manager died on us. prepare its eulogy.
196
Andy McFadden13a082e2012-08-24 10:16:42 -0700197 // restore initial conditions (default device unblank, etc)
198 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800199
200 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700201 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800202}
203
Mathias Agopian7e27f052010-05-28 14:22:23 -0700204sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205{
Mathias Agopian96f08192010-06-02 23:28:45 -0700206 sp<ISurfaceComposerClient> bclient;
207 sp<Client> client(new Client(this));
208 status_t err = client->initCheck();
209 if (err == NO_ERROR) {
210 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 return bclient;
213}
214
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700215sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
216 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700217{
218 class DisplayToken : public BBinder {
219 sp<SurfaceFlinger> flinger;
220 virtual ~DisplayToken() {
221 // no more references, this display must be terminated
222 Mutex::Autolock _l(flinger->mStateLock);
223 flinger->mCurrentState.displays.removeItem(this);
224 flinger->setTransactionFlags(eDisplayTransactionNeeded);
225 }
226 public:
227 DisplayToken(const sp<SurfaceFlinger>& flinger)
228 : flinger(flinger) {
229 }
230 };
231
232 sp<BBinder> token = new DisplayToken(this);
233
234 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700235 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700236 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700237 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700238 mCurrentState.displays.add(token, info);
239
240 return token;
241}
242
Jesse Hall6c913be2013-08-08 12:15:49 -0700243void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
244 Mutex::Autolock _l(mStateLock);
245
246 ssize_t idx = mCurrentState.displays.indexOfKey(display);
247 if (idx < 0) {
248 ALOGW("destroyDisplay: invalid display token");
249 return;
250 }
251
252 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
253 if (!info.isVirtualDisplay()) {
254 ALOGE("destroyDisplay called for non-virtual display");
255 return;
256 }
257
258 mCurrentState.displays.removeItemsAt(idx);
259 setTransactionFlags(eDisplayTransactionNeeded);
260}
261
Jesse Hall692c7232012-11-08 15:41:56 -0800262void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
263 ALOGW_IF(mBuiltinDisplays[type],
264 "Overwriting display token for display type %d", type);
265 mBuiltinDisplays[type] = new BBinder();
266 DisplayDeviceState info(type);
267 // All non-virtual displays are currently considered secure.
268 info.isSecure = true;
269 mCurrentState.displays.add(mBuiltinDisplays[type], info);
270}
271
Mathias Agopiane57f2922012-08-09 16:29:12 -0700272sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700273 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700274 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
275 return NULL;
276 }
Jesse Hall692c7232012-11-08 15:41:56 -0800277 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700278}
279
Jamie Gennis9a78c902011-01-12 18:30:40 -0800280sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
281{
282 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
283 return gba;
284}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700285
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286void SurfaceFlinger::bootFinished()
287{
288 const nsecs_t now = systemTime();
289 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000290 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700291 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700292
293 // wait patiently for the window manager death
294 const String16 name("window");
295 sp<IBinder> window(defaultServiceManager()->getService(name));
296 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700297 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700298 }
299
300 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700301 // formerly we would just kill the process, but we now ask it to exit so it
302 // can choose where to stop the animation.
303 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304}
305
Mathias Agopian3f844832013-08-07 21:24:32 -0700306void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700307 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700308 RenderEngine& engine;
309 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700310 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700311 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
312 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700313 }
314 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700315 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700316 return true;
317 }
318 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700319 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700320}
321
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700322class DispSyncSource : public VSyncSource, private DispSync::Callback {
323public:
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700324 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync) :
325 mValue(0),
326 mPhaseOffset(phaseOffset),
327 mTraceVsync(traceVsync),
328 mDispSync(dispSync) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700329
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700330 virtual ~DispSyncSource() {}
331
332 virtual void setVSyncEnabled(bool enable) {
333 // Do NOT lock the mutex here so as to avoid any mutex ordering issues
334 // with locking it in the onDispSyncEvent callback.
335 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700336 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700337 static_cast<DispSync::Callback*>(this));
338 if (err != NO_ERROR) {
339 ALOGE("error registering vsync callback: %s (%d)",
340 strerror(-err), err);
341 }
342 ATRACE_INT("VsyncOn", 1);
343 } else {
344 status_t err = mDispSync->removeEventListener(
345 static_cast<DispSync::Callback*>(this));
346 if (err != NO_ERROR) {
347 ALOGE("error unregistering vsync callback: %s (%d)",
348 strerror(-err), err);
349 }
350 ATRACE_INT("VsyncOn", 0);
351 }
352 }
353
354 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
355 Mutex::Autolock lock(mMutex);
356 mCallback = callback;
357 }
358
359private:
360 virtual void onDispSyncEvent(nsecs_t when) {
361 sp<VSyncSource::Callback> callback;
362 {
363 Mutex::Autolock lock(mMutex);
364 callback = mCallback;
365
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700366 if (mTraceVsync) {
367 mValue = (mValue + 1) % 2;
368 ATRACE_INT("VSYNC", mValue);
369 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700370 }
371
372 if (callback != NULL) {
373 callback->onVSyncEvent(when);
374 }
375 }
376
377 int mValue;
378
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700379 const nsecs_t mPhaseOffset;
380 const bool mTraceVsync;
381
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700382 DispSync* mDispSync;
383 sp<VSyncSource::Callback> mCallback;
384 Mutex mMutex;
385};
386
387void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700388 ALOGI( "SurfaceFlinger's main thread ready to run. "
389 "Initializing graphics H/W...");
390
Jesse Hallb65f32e2013-09-27 22:10:47 -0700391 status_t err;
Jesse Hall692c7232012-11-08 15:41:56 -0800392 Mutex::Autolock _l(mStateLock);
393
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700394 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700395 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
396 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700397
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700398 // Initialize the H/W composer object. There may or may not be an
399 // actual hardware composer underneath.
400 mHwc = new HWComposer(this,
401 *static_cast<HWComposer::EventHandler *>(this));
402
Mathias Agopian875d8e12013-06-07 15:35:48 -0700403 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800404 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700405
406 // retrieve the EGL context that was selected/created
407 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700408
Mathias Agopianda27af92012-09-13 18:17:13 -0700409 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
410 "couldn't create EGLContext");
411
Mathias Agopiancde87a32012-09-13 14:09:01 -0700412 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700413 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700414 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700415 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700416 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700417 // All non-virtual displays are currently considered secure.
418 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800419 createBuiltinDisplayLocked(type);
420 wp<IBinder> token = mBuiltinDisplays[i];
421
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700422 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
423 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i, bq);
Jesse Hall19e87292013-12-23 21:02:15 -0800424 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700425 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800426 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700427 fbs, bq,
Jesse Hall05f8c702013-12-23 20:44:38 -0800428 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700429 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700430 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700431 // for displays other than the main display, so we always
432 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700433 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700434 hw->acquireScreen();
435 }
436 mDisplays.add(token, hw);
437 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700438 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700439
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700440 // make the GLContext current so that we can create textures when creating Layers
441 // (which may happens before we render something)
442 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
443
Mathias Agopian028508c2012-07-25 21:12:12 -0700444 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700445 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
446 vsyncPhaseOffsetNs, true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700447 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700448 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
449 sfVsyncPhaseOffsetNs, false);
450 mSFEventThread = new EventThread(sfVsyncSrc);
451 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700452
Jamie Gennisd1700752013-10-14 12:22:52 -0700453 mEventControlThread = new EventControlThread(this);
454 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
455
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700456 // set a fake vsync period if there is no HWComposer
457 if (mHwc->initCheck() != NO_ERROR) {
458 mPrimaryDispSync.setPeriod(16666667);
459 }
460
Mathias Agopian92a979a2012-08-02 18:32:23 -0700461 // initialize our drawing state
462 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700463
Andy McFadden13a082e2012-08-24 10:16:42 -0700464 // set initial conditions (e.g. unblank default device)
465 initializeDisplays();
466
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700467 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700468 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800469}
470
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700471int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700472 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700473 type : mHwc->allocateDisplayId();
474}
475
Mathias Agopiana67e4182012-06-19 17:26:12 -0700476void SurfaceFlinger::startBootAnim() {
477 // start boot animation
478 property_set("service.bootanim.exit", "0");
479 property_set("ctl.start", "bootanim");
480}
481
Mathias Agopian875d8e12013-06-07 15:35:48 -0700482size_t SurfaceFlinger::getMaxTextureSize() const {
483 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700484}
485
Mathias Agopian875d8e12013-06-07 15:35:48 -0700486size_t SurfaceFlinger::getMaxViewportDims() const {
487 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700488}
489
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800490// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800491
Jamie Gennis582270d2011-08-17 18:19:00 -0700492bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800493 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800494 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800495 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700496 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800497}
498
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700499status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800500 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700501 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800502 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700503 type = i;
504 break;
505 }
506 }
507
508 if (type < 0) {
509 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700510 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700511
512 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700513 float xdpi = hwc.getDpiX(type);
514 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700515
516 // TODO: Not sure if display density should handled by SF any longer
517 class Density {
518 static int getDensityFromProperty(char const* propName) {
519 char property[PROPERTY_VALUE_MAX];
520 int density = 0;
521 if (property_get(propName, property, NULL) > 0) {
522 density = atoi(property);
523 }
524 return density;
525 }
526 public:
527 static int getEmuDensity() {
528 return getDensityFromProperty("qemu.sf.lcd_density"); }
529 static int getBuildDensity() {
530 return getDensityFromProperty("ro.sf.lcd_density"); }
531 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700532
533 if (type == DisplayDevice::DISPLAY_PRIMARY) {
534 // The density of the device is provided by a build property
535 float density = Density::getBuildDensity() / 160.0f;
536 if (density == 0) {
537 // the build doesn't provide a density -- this is wrong!
538 // use xdpi instead
539 ALOGE("ro.sf.lcd_density must be defined as a build property");
540 density = xdpi / 160.0f;
541 }
542 if (Density::getEmuDensity()) {
543 // if "qemu.sf.lcd_density" is specified, it overrides everything
544 xdpi = ydpi = density = Density::getEmuDensity();
545 density /= 160.0f;
546 }
547 info->density = density;
548
549 // TODO: this needs to go away (currently needed only by webkit)
550 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
551 info->orientation = hw->getOrientation();
Mathias Agopian1604f772012-09-18 21:54:42 -0700552 } else {
553 // TODO: where should this value come from?
554 static const int TV_DENSITY = 213;
555 info->density = TV_DENSITY / 160.0f;
556 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700557 }
558
Mathias Agopian1604f772012-09-18 21:54:42 -0700559 info->w = hwc.getWidth(type);
560 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700561 info->xdpi = xdpi;
562 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700563 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700564
565 // All non-virtual displays are currently considered secure.
566 info->secure = true;
567
Mathias Agopian888c8222012-08-04 21:10:38 -0700568 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700569}
570
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800571// ----------------------------------------------------------------------------
572
573sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800574 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700575}
576
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800577// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800578
579void SurfaceFlinger::waitForEvent() {
580 mEventQueue.waitMessage();
581}
582
583void SurfaceFlinger::signalTransaction() {
584 mEventQueue.invalidate();
585}
586
587void SurfaceFlinger::signalLayerUpdate() {
588 mEventQueue.invalidate();
589}
590
591void SurfaceFlinger::signalRefresh() {
592 mEventQueue.refresh();
593}
594
595status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
596 nsecs_t reltime, uint32_t flags) {
597 return mEventQueue.postMessage(msg, reltime);
598}
599
600status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
601 nsecs_t reltime, uint32_t flags) {
602 status_t res = mEventQueue.postMessage(msg, reltime);
603 if (res == NO_ERROR) {
604 msg->wait();
605 }
606 return res;
607}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700609void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700610 do {
611 waitForEvent();
612 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800613}
614
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700615void SurfaceFlinger::enableHardwareVsync() {
616 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700617 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700618 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700619 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
620 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700621 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700622 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700623}
624
Jesse Hall948fe0c2013-10-14 12:56:09 -0700625void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700626 Mutex::Autolock _l(mHWVsyncLock);
627
Jesse Hall948fe0c2013-10-14 12:56:09 -0700628 if (makeAvailable) {
629 mHWVsyncAvailable = true;
630 } else if (!mHWVsyncAvailable) {
631 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
632 return;
633 }
634
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700635 const nsecs_t period =
636 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
637
638 mPrimaryDispSync.reset();
639 mPrimaryDispSync.setPeriod(period);
640
641 if (!mPrimaryHWVsyncEnabled) {
642 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700643 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
644 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700645 mPrimaryHWVsyncEnabled = true;
646 }
647}
648
Jesse Hall948fe0c2013-10-14 12:56:09 -0700649void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700650 Mutex::Autolock _l(mHWVsyncLock);
651 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700652 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
653 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700654 mPrimaryDispSync.endResync();
655 mPrimaryHWVsyncEnabled = false;
656 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700657 if (makeUnavailable) {
658 mHWVsyncAvailable = false;
659 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700660}
661
662void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700663 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700664
Jamie Gennisd1700752013-10-14 12:22:52 -0700665 { // Scope for the lock
666 Mutex::Autolock _l(mHWVsyncLock);
667 if (type == 0 && mPrimaryHWVsyncEnabled) {
668 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700669 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700670 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700671
672 if (needsHwVsync) {
673 enableHardwareVsync();
674 } else {
675 disableHardwareVsync(false);
676 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700677}
678
679void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
680 if (mEventThread == NULL) {
681 // This is a temporary workaround for b/7145521. A non-null pointer
682 // does not mean EventThread has finished initializing, so this
683 // is not a correct fix.
684 ALOGW("WARNING: EventThread not started, ignoring hotplug");
685 return;
686 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700687
Jesse Hall9e663de2013-08-16 14:28:37 -0700688 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700689 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800690 if (connected) {
691 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700692 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800693 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
694 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700695 }
696 setTransactionFlags(eDisplayTransactionNeeded);
697
Andy McFadden9e9689c2012-10-10 18:17:51 -0700698 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700699 }
Mathias Agopian86303202012-07-24 22:46:10 -0700700}
701
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700702void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700703 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700704 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700705}
706
Mathias Agopian4fec8732012-06-29 14:12:52 -0700707void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800708 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800709 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700710 case MessageQueue::TRANSACTION:
711 handleMessageTransaction();
712 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700713 case MessageQueue::INVALIDATE:
714 handleMessageTransaction();
715 handleMessageInvalidate();
716 signalRefresh();
717 break;
718 case MessageQueue::REFRESH:
719 handleMessageRefresh();
720 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800721 }
722}
723
Mathias Agopian4fec8732012-06-29 14:12:52 -0700724void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700725 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700726 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700727 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700728 }
729}
730
731void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700732 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700733 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700734}
735
736void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700737 ATRACE_CALL();
738 preComposition();
739 rebuildLayerStacks();
740 setUpHWComposer();
741 doDebugFlashRegions();
742 doComposition();
743 postComposition();
744}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700745
Mathias Agopiancd60f992012-08-16 16:28:27 -0700746void SurfaceFlinger::doDebugFlashRegions()
747{
748 // is debugging enabled
749 if (CC_LIKELY(!mDebugRegion))
750 return;
751
752 const bool repaintEverything = mRepaintEverything;
753 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
754 const sp<DisplayDevice>& hw(mDisplays[dpy]);
755 if (hw->canDraw()) {
756 // transform the dirty region into this screen's coordinate space
757 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
758 if (!dirtyRegion.isEmpty()) {
759 // redraw the whole screen
760 doComposeSurfaces(hw, Region(hw->bounds()));
761
762 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700763 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700764 RenderEngine& engine(getRenderEngine());
765 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
766
Mathias Agopiancd60f992012-08-16 16:28:27 -0700767 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700768 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700769 }
770 }
771 }
772
773 postFramebuffer();
774
775 if (mDebugRegion > 1) {
776 usleep(mDebugRegion * 1000);
777 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700778
779 HWComposer& hwc(getHwComposer());
780 if (hwc.initCheck() == NO_ERROR) {
781 status_t err = hwc.prepare();
782 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
783 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700784}
785
786void SurfaceFlinger::preComposition()
787{
788 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700789 const LayerVector& layers(mDrawingState.layersSortedByZ);
790 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700791 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700792 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700793 needExtraInvalidate = true;
794 }
795 }
796 if (needExtraInvalidate) {
797 signalLayerUpdate();
798 }
799}
800
801void SurfaceFlinger::postComposition()
802{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700803 const LayerVector& layers(mDrawingState.layersSortedByZ);
804 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700805 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700806 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700807 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800808
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700809 const HWComposer& hwc = getHwComposer();
810 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
811
812 if (presentFence->isValid()) {
813 if (mPrimaryDispSync.addPresentFence(presentFence)) {
814 enableHardwareVsync();
815 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700816 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700817 }
818 }
819
820 if (runningWithoutSyncFramework) {
821 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
822 if (hw->isScreenAcquired()) {
823 enableHardwareVsync();
824 }
825 }
826
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800827 if (mAnimCompositionPending) {
828 mAnimCompositionPending = false;
829
Jesse Halla9a1b002013-02-27 16:39:25 -0800830 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800831 mAnimFrameTracker.setActualPresentFence(presentFence);
832 } else {
833 // The HWC doesn't support present fences, so use the refresh
834 // timestamp instead.
835 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
836 mAnimFrameTracker.setActualPresentTime(presentTime);
837 }
838 mAnimFrameTracker.advanceFrame();
839 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700840}
841
842void SurfaceFlinger::rebuildLayerStacks() {
843 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700844 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700845 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700846 mVisibleRegionsDirty = false;
847 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700848
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700849 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700850 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700851 Region opaqueRegion;
852 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800853 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700854 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700855 const Transform& tr(hw->getTransform());
856 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700857 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700858 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700859 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700860
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700861 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700862 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700863 const sp<Layer>& layer(layers[i]);
864 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700865 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700866 Region drawRegion(tr.transform(
867 layer->visibleNonTransparentRegion));
868 drawRegion.andSelf(bounds);
869 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700870 layersSortedByZ.add(layer);
871 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700872 }
873 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700874 }
Mathias Agopian42977342012-08-05 00:40:46 -0700875 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700876 hw->undefinedRegion.set(bounds);
877 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
878 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700879 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700880 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700881}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700882
Mathias Agopiancd60f992012-08-16 16:28:27 -0700883void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700884 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
885 mDisplays[dpy]->beginFrame();
886 }
887
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700888 HWComposer& hwc(getHwComposer());
889 if (hwc.initCheck() == NO_ERROR) {
890 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700891 if (CC_UNLIKELY(mHwWorkListDirty)) {
892 mHwWorkListDirty = false;
893 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
894 sp<const DisplayDevice> hw(mDisplays[dpy]);
895 const int32_t id = hw->getHwcDisplayId();
896 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800897 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700898 hw->getVisibleLayersSortedByZ());
899 const size_t count = currentLayers.size();
900 if (hwc.createWorkList(id, count) == NO_ERROR) {
901 HWComposer::LayerListIterator cur = hwc.begin(id);
902 const HWComposer::LayerListIterator end = hwc.end(id);
903 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800904 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700905 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -0700906 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -0700907 cur->setSkip(true);
908 }
909 }
910 }
911 }
912 }
913 }
914
915 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700916 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700917 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700918 const int32_t id = hw->getHwcDisplayId();
919 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800920 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700921 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700922 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700923 HWComposer::LayerListIterator cur = hwc.begin(id);
924 const HWComposer::LayerListIterator end = hwc.end(id);
925 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
926 /*
927 * update the per-frame h/w composer data for each layer
928 * and build the transparent region of the FB
929 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800930 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700931 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700932 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700933 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700934 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700935
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700936 status_t err = hwc.prepare();
937 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700938
939 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
940 sp<const DisplayDevice> hw(mDisplays[dpy]);
941 hw->prepareFrame(hwc);
942 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700943 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700944}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700945
Mathias Agopiancd60f992012-08-16 16:28:27 -0700946void SurfaceFlinger::doComposition() {
947 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700948 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700949 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700950 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700951 if (hw->canDraw()) {
952 // transform the dirty region into this screen's coordinate space
953 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800954
955 // repaint the framebuffer (if needed)
956 doDisplayComposition(hw, dirtyRegion);
957
Mathias Agopiancd60f992012-08-16 16:28:27 -0700958 hw->dirtyRegion.clear();
959 hw->flip(hw->swapRegion);
960 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700961 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700962 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700963 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700964 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700965 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700966}
967
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800968void SurfaceFlinger::postFramebuffer()
969{
Mathias Agopian841cde52012-03-01 15:44:37 -0800970 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800971
Mathias Agopiana44b0412011-10-16 18:46:35 -0700972 const nsecs_t now = systemTime();
973 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700974
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700975 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700976 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700977 if (!hwc.supportsFramebufferTarget()) {
978 // EGL spec says:
979 // "surface must be bound to the calling thread's current context,
980 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700981 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700982 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700983 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700984 }
985
Mathias Agopian6da15f42013-09-25 20:40:07 -0700986 // make the default display current because the VirtualDisplayDevice code cannot
987 // deal with dequeueBuffer() being called outside of the composition loop; however
988 // the code below can call glFlush() which is allowed (and does in some case) call
989 // dequeueBuffer().
990 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
991
Mathias Agopian92a979a2012-08-02 18:32:23 -0700992 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700993 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800994 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700995 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700996 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700997 int32_t id = hw->getHwcDisplayId();
998 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700999 HWComposer::LayerListIterator cur = hwc.begin(id);
1000 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001001 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001002 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001003 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001004 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001005 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001006 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001007 }
Jesse Hallef194142012-06-14 14:45:17 -07001008 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001009 }
1010
Mathias Agopiana44b0412011-10-16 18:46:35 -07001011 mLastSwapBufferTime = systemTime() - now;
1012 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001013
1014 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1015 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1016 logFrameStats();
1017 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001018}
1019
Mathias Agopian87baae12012-07-31 12:38:26 -07001020void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021{
Mathias Agopian841cde52012-03-01 15:44:37 -08001022 ATRACE_CALL();
1023
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001024 // here we keep a copy of the drawing state (that is the state that's
1025 // going to be overwritten by handleTransactionLocked()) outside of
1026 // mStateLock so that the side-effects of the State assignment
1027 // don't happen with mStateLock held (which can cause deadlocks).
1028 State drawingState(mDrawingState);
1029
Mathias Agopianca4d3602011-05-19 15:38:14 -07001030 Mutex::Autolock _l(mStateLock);
1031 const nsecs_t now = systemTime();
1032 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001033
Mathias Agopianca4d3602011-05-19 15:38:14 -07001034 // Here we're guaranteed that some transaction flags are set
1035 // so we can call handleTransactionLocked() unconditionally.
1036 // We call getTransactionFlags(), which will also clear the flags,
1037 // with mStateLock held to guarantee that mCurrentState won't change
1038 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001039
Mathias Agopiane57f2922012-08-09 16:29:12 -07001040 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001041 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001042
Mathias Agopianca4d3602011-05-19 15:38:14 -07001043 mLastTransactionTime = systemTime() - now;
1044 mDebugInTransaction = 0;
1045 invalidateHwcGeometry();
1046 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001047}
1048
Mathias Agopian87baae12012-07-31 12:38:26 -07001049void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001050{
1051 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001052 const size_t count = currentLayers.size();
1053
1054 /*
1055 * Traversal of the children
1056 * (perform the transaction for each of them if needed)
1057 */
1058
Mathias Agopian3559b072012-08-15 13:46:03 -07001059 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001061 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1063 if (!trFlags) continue;
1064
1065 const uint32_t flags = layer->doTransaction(0);
1066 if (flags & Layer::eVisibleRegion)
1067 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001068 }
1069 }
1070
1071 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001072 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073 */
1074
Mathias Agopiane57f2922012-08-09 16:29:12 -07001075 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001076 // here we take advantage of Vector's copy-on-write semantics to
1077 // improve performance by skipping the transaction entirely when
1078 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001079 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1080 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001081 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001083 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001084 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001085
1086 // find the displays that were removed
1087 // (ie: in drawing state but not in current state)
1088 // also handle displays that changed
1089 // (ie: displays that are in both lists)
1090 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001091 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1092 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001093 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001094 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001095 // Call makeCurrent() on the primary display so we can
1096 // be sure that nothing associated with this display
1097 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001098 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001099 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001100 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1101 if (hw != NULL)
1102 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001103 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001104 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001105 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001106 } else {
1107 ALOGW("trying to remove the main display");
1108 }
1109 } else {
1110 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001111 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001112 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001113 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001114 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001115 // recreating the DisplayDevice, so we just remove it
1116 // from the drawing state, so that it get re-added
1117 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001118 sp<DisplayDevice> hw(getDisplayDevice(display));
1119 if (hw != NULL)
1120 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001121 mDisplays.removeItem(display);
1122 mDrawingState.displays.removeItemsAt(i);
1123 dc--; i--;
1124 // at this point we must loop to the next item
1125 continue;
1126 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001127
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001128 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001129 if (disp != NULL) {
1130 if (state.layerStack != draw[i].layerStack) {
1131 disp->setLayerStack(state.layerStack);
1132 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001133 if ((state.orientation != draw[i].orientation)
1134 || (state.viewport != draw[i].viewport)
1135 || (state.frame != draw[i].frame))
1136 {
1137 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001138 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001139 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001140 }
1141 }
1142 }
1143
1144 // find displays that were added
1145 // (ie: in current state but not in drawing state)
1146 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001147 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001148 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001149
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001150 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001151 sp<IGraphicBufferProducer> producer;
1152 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
1153
Jesse Hall02d86562013-03-25 14:43:23 -07001154 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001155 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001156 // Virtual displays without a surface are dormant:
1157 // they have external state (layer stack, projection,
1158 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001159 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001160
Jesse Hall02d86562013-03-25 14:43:23 -07001161 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001162 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1163 *mHwc, hwcDisplayId, state.surface, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001164 state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001165
1166 dispSurface = vds;
1167 if (hwcDisplayId >= 0) {
1168 producer = vds;
1169 } else {
1170 // There won't be any interaction with HWC for this virtual display,
1171 // so the GLES driver can pass buffers directly to the sink.
1172 producer = state.surface;
1173 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001174 }
1175 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001176 ALOGE_IF(state.surface!=NULL,
1177 "adding a supported display, but rendering "
1178 "surface is provided (%p), ignoring it",
1179 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001180 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001181 // for supported (by hwc) displays we provide our
1182 // own rendering surface
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001183 dispSurface = new FramebufferSurface(*mHwc, state.type, bq);
1184 producer = bq;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001185 }
1186
1187 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001188 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001189 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001190 state.type, hwcDisplayId,
1191 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001192 display, dispSurface, producer,
1193 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001194 hw->setLayerStack(state.layerStack);
1195 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001196 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001197 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001198 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001199 if (state.isVirtualDisplay()) {
1200 if (hwcDisplayId >= 0) {
1201 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1202 hw->getWidth(), hw->getHeight(),
1203 hw->getFormat());
1204 }
1205 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001206 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001207 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001208 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001209 }
1210 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001211 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001212 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213
Mathias Agopian84300952012-11-21 16:02:13 -08001214 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1215 // The transform hint might have changed for some layers
1216 // (either because a display has changed, or because a layer
1217 // as changed).
1218 //
1219 // Walk through all the layers in currentLayers,
1220 // and update their transform hint.
1221 //
1222 // If a layer is visible only on a single display, then that
1223 // display is used to calculate the hint, otherwise we use the
1224 // default display.
1225 //
1226 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1227 // the hint is set before we acquire a buffer from the surface texture.
1228 //
1229 // NOTE: layer transactions have taken place already, so we use their
1230 // drawing state. However, SurfaceFlinger's own transaction has not
1231 // happened yet, so we must use the current state layer list
1232 // (soon to become the drawing state list).
1233 //
1234 sp<const DisplayDevice> disp;
1235 uint32_t currentlayerStack = 0;
1236 for (size_t i=0; i<count; i++) {
1237 // NOTE: we rely on the fact that layers are sorted by
1238 // layerStack first (so we don't have to traverse the list
1239 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001240 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001241 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001242 if (i==0 || currentlayerStack != layerStack) {
1243 currentlayerStack = layerStack;
1244 // figure out if this layerstack is mirrored
1245 // (more than one display) if so, pick the default display,
1246 // if not, pick the only display it's on.
1247 disp.clear();
1248 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1249 sp<const DisplayDevice> hw(mDisplays[dpy]);
1250 if (hw->getLayerStack() == currentlayerStack) {
1251 if (disp == NULL) {
1252 disp = hw;
1253 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001254 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001255 break;
1256 }
1257 }
1258 }
1259 }
Chet Haase91d25932013-04-11 15:24:55 -07001260 if (disp == NULL) {
1261 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1262 // redraw after transform hint changes. See bug 8508397.
1263
1264 // could be null when this layer is using a layerStack
1265 // that is not visible on any display. Also can occur at
1266 // screen off/on times.
1267 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001268 }
Chet Haase91d25932013-04-11 15:24:55 -07001269 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001270 }
1271 }
1272
1273
Mathias Agopian3559b072012-08-15 13:46:03 -07001274 /*
1275 * Perform our own transaction if needed
1276 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001277
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001278 const LayerVector& layers(mDrawingState.layersSortedByZ);
1279 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001280 // layers have been added
1281 mVisibleRegionsDirty = true;
1282 }
1283
1284 // some layers might have been removed, so
1285 // we need to update the regions they're exposing.
1286 if (mLayersRemoved) {
1287 mLayersRemoved = false;
1288 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001289 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001290 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001291 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001292 if (currentLayers.indexOf(layer) < 0) {
1293 // this layer is not visible anymore
1294 // TODO: we could traverse the tree from front to back and
1295 // compute the actual visible region
1296 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001297 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001298 Region visibleReg = s.transform.transform(
1299 Region(Rect(s.active.w, s.active.h)));
1300 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001301 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303 }
1304
1305 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001306}
1307
1308void SurfaceFlinger::commitTransaction()
1309{
1310 if (!mLayersPendingRemoval.isEmpty()) {
1311 // Notify removed layers now that they can't be drawn from
1312 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1313 mLayersPendingRemoval[i]->onRemoved();
1314 }
1315 mLayersPendingRemoval.clear();
1316 }
1317
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001318 // If this transaction is part of a window animation then the next frame
1319 // we composite should be considered an animation as well.
1320 mAnimCompositionPending = mAnimTransactionPending;
1321
Mathias Agopian4fec8732012-06-29 14:12:52 -07001322 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001323 mTransactionPending = false;
1324 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001325 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001326}
1327
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001329 const LayerVector& currentLayers, uint32_t layerStack,
1330 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331{
Mathias Agopian841cde52012-03-01 15:44:37 -08001332 ATRACE_CALL();
1333
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001334 Region aboveOpaqueLayers;
1335 Region aboveCoveredLayers;
1336 Region dirty;
1337
Mathias Agopian87baae12012-07-31 12:38:26 -07001338 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001339
1340 size_t i = currentLayers.size();
1341 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001342 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001343
1344 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001345 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001346
Jesse Hall01e29052013-02-19 16:13:35 -08001347 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001348 if (s.layerStack != layerStack)
1349 continue;
1350
Mathias Agopianab028732010-03-16 16:41:46 -07001351 /*
1352 * opaqueRegion: area of a surface that is fully opaque.
1353 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001354 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001355
1356 /*
1357 * visibleRegion: area of a surface that is visible on screen
1358 * and not fully transparent. This is essentially the layer's
1359 * footprint minus the opaque regions above it.
1360 * Areas covered by a translucent surface are considered visible.
1361 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001363
1364 /*
1365 * coveredRegion: area of a surface that is covered by all
1366 * visible regions above it (which includes the translucent areas).
1367 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001368 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001369
Jesse Halla8026d22012-09-25 13:25:04 -07001370 /*
1371 * transparentRegion: area of a surface that is hinted to be completely
1372 * transparent. This is only used to tell when the layer has no visible
1373 * non-transparent regions and can be removed from the layer list. It
1374 * does not affect the visibleRegion of this layer or any layers
1375 * beneath it. The hint may not be correct if apps don't respect the
1376 * SurfaceView restrictions (which, sadly, some don't).
1377 */
1378 Region transparentRegion;
1379
Mathias Agopianab028732010-03-16 16:41:46 -07001380
1381 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001382 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001383 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001384 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001385 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001386 if (!visibleRegion.isEmpty()) {
1387 // Remove the transparent area from the visible region
1388 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001389 const Transform tr(s.transform);
1390 if (tr.transformed()) {
1391 if (tr.preserveRects()) {
1392 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001393 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001394 } else {
1395 // transformation too complex, can't do the
1396 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001397 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001398 }
1399 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001400 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001401 }
Mathias Agopianab028732010-03-16 16:41:46 -07001402 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001403
Mathias Agopianab028732010-03-16 16:41:46 -07001404 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001405 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001406 if (s.alpha==255 && !translucent &&
1407 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1408 // the opaque region is the layer's footprint
1409 opaqueRegion = visibleRegion;
1410 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411 }
1412 }
1413
Mathias Agopianab028732010-03-16 16:41:46 -07001414 // Clip the covered region to the visible region
1415 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1416
1417 // Update aboveCoveredLayers for next (lower) layer
1418 aboveCoveredLayers.orSelf(visibleRegion);
1419
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001420 // subtract the opaque region covered by the layers above us
1421 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422
1423 // compute this layer's dirty region
1424 if (layer->contentDirty) {
1425 // we need to invalidate the whole region
1426 dirty = visibleRegion;
1427 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001428 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001429 layer->contentDirty = false;
1430 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001431 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001432 * the exposed region consists of two components:
1433 * 1) what's VISIBLE now and was COVERED before
1434 * 2) what's EXPOSED now less what was EXPOSED before
1435 *
1436 * note that (1) is conservative, we start with the whole
1437 * visible region but only keep what used to be covered by
1438 * something -- which mean it may have been exposed.
1439 *
1440 * (2) handles areas that were not covered by anything but got
1441 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001442 */
Mathias Agopianab028732010-03-16 16:41:46 -07001443 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001444 const Region oldVisibleRegion = layer->visibleRegion;
1445 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001446 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1447 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001448 }
1449 dirty.subtractSelf(aboveOpaqueLayers);
1450
1451 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001452 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001453
Mathias Agopianab028732010-03-16 16:41:46 -07001454 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001456
Jesse Halla8026d22012-09-25 13:25:04 -07001457 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001458 layer->setVisibleRegion(visibleRegion);
1459 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001460 layer->setVisibleNonTransparentRegion(
1461 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462 }
1463
Mathias Agopian87baae12012-07-31 12:38:26 -07001464 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001465}
1466
Mathias Agopian87baae12012-07-31 12:38:26 -07001467void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1468 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001469 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001470 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1471 if (hw->getLayerStack() == layerStack) {
1472 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001473 }
1474 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001475}
1476
1477void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001478{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001479 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480
Mathias Agopian4fec8732012-06-29 14:12:52 -07001481 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001482 const LayerVector& layers(mDrawingState.layersSortedByZ);
1483 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001484 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001485 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001486 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001487 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001488 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001489 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001490
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001491 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001492}
1493
Mathias Agopianad456f92011-01-13 17:53:01 -08001494void SurfaceFlinger::invalidateHwcGeometry()
1495{
1496 mHwWorkListDirty = true;
1497}
1498
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001499
Mathias Agopiancd60f992012-08-16 16:28:27 -07001500void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001501 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001502{
Mathias Agopian87baae12012-07-31 12:38:26 -07001503 Region dirtyRegion(inDirtyRegion);
1504
Mathias Agopianb8a55602009-06-26 19:06:36 -07001505 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001506 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001507
Mathias Agopian42977342012-08-05 00:40:46 -07001508 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001509 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001510 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1511 // takes a rectangle, we must make sure to update that whole
1512 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001513 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001514 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001515 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001516 // We need to redraw the rectangle that will be updated
1517 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001518 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001519 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001520 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001521 } else {
1522 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001523 dirtyRegion.set(hw->bounds());
1524 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001525 }
1526 }
1527
Alan Viverette9c5a3332013-09-12 20:04:35 -07001528 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Mathias Agopianff2ed702013-09-01 21:36:12 -07001529 doComposeSurfaces(hw, dirtyRegion);
1530 } else {
1531 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001532 mat4 colorMatrix = mColorMatrix;
1533 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001534 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001535 }
1536 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001537 doComposeSurfaces(hw, dirtyRegion);
1538 engine.endGroup();
1539 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001540
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001541 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001542 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001543
1544 // swap buffers (presentation)
1545 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001546}
1547
Mathias Agopiancd60f992012-08-16 16:28:27 -07001548void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001549{
Mathias Agopian3f844832013-08-07 21:24:32 -07001550 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001551 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001552 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001553 HWComposer::LayerListIterator cur = hwc.begin(id);
1554 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001555
Jesse Halld05a17f2013-09-30 16:49:30 -07001556 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001557 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001558 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001559 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1560 hw->getDisplayName().string());
1561 return;
1562 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001563
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001564 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001565 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001566 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001567 // when using overlays, we assume a fully transparent framebuffer
1568 // NOTE: we could reduce how much we need to clear, for instance
1569 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001570 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001571 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001572 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001573 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001574 // we start with the whole screen area
1575 const Region bounds(hw->getBounds());
1576
1577 // we remove the scissor part
1578 // we're left with the letterbox region
1579 // (common case is that letterbox ends-up being empty)
1580 const Region letterbox(bounds.subtract(hw->getScissor()));
1581
1582 // compute the area to clear
1583 Region region(hw->undefinedRegion.merge(letterbox));
1584
1585 // but limit it to the dirty region
1586 region.andSelf(dirty);
1587
Mathias Agopianb9494d52012-04-18 02:28:45 -07001588 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001589 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001590 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001591 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001592 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001593 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001594
Mathias Agopian766dc492012-10-30 18:08:06 -07001595 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1596 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001597 // scissor on the main display. It should never be needed
1598 // anyways (though in theory it could since the API allows it).
1599 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001600 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001601 if (scissor != bounds) {
1602 // scissor doesn't match the screen's dimensions, so we
1603 // need to clear everything outside of it and enable
1604 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001605
Mathias Agopianf45c5102012-10-24 16:29:17 -07001606 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001607 const uint32_t height = hw->getHeight();
1608 engine.setScissor(scissor.left, height - scissor.bottom,
1609 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001610 }
1611 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001612 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001613
Mathias Agopian85d751c2012-08-29 16:59:24 -07001614 /*
1615 * and then, render the layers targeted at the framebuffer
1616 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001617
Mathias Agopian13127d82013-03-05 17:47:11 -08001618 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001619 const size_t count = layers.size();
1620 const Transform& tr = hw->getTransform();
1621 if (cur != end) {
1622 // we're using h/w composer
1623 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001624 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001625 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001626 if (!clip.isEmpty()) {
1627 switch (cur->getCompositionType()) {
1628 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001629 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001630 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1631 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001632 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001633 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001634 // never clear the very first layer since we're
1635 // guaranteed the FB is already cleared
1636 layer->clearWithOpenGL(hw, clip);
1637 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001638 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001639 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001640 case HWC_FRAMEBUFFER: {
1641 layer->draw(hw, clip);
1642 break;
1643 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001644 case HWC_FRAMEBUFFER_TARGET: {
1645 // this should not happen as the iterator shouldn't
1646 // let us get there.
1647 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1648 break;
1649 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001650 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001651 }
1652 layer->setAcquireFence(hw, *cur);
1653 }
1654 } else {
1655 // we're not using h/w composer
1656 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001657 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001658 const Region clip(dirty.intersect(
1659 tr.transform(layer->visibleRegion)));
1660 if (!clip.isEmpty()) {
1661 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001662 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001663 }
1664 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001665
1666 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001667 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001668}
1669
Mathias Agopian3f844832013-08-07 21:24:32 -07001670void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001671 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001672 RenderEngine& engine(getRenderEngine());
1673 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001674}
1675
Mathias Agopianac9fa422013-02-11 16:40:36 -08001676void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1677 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001678 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001679 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001680{
Mathias Agopian96f08192010-06-02 23:28:45 -07001681 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001682 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001683
Mathias Agopian96f08192010-06-02 23:28:45 -07001684 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001685 Mutex::Autolock _l(mStateLock);
1686 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001687 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001688}
1689
Mathias Agopian3f844832013-08-07 21:24:32 -07001690status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001691 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001692 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001693 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001694 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001695 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001696 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001697 return NO_ERROR;
1698 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001699 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001700}
1701
Mathias Agopian3f844832013-08-07 21:24:32 -07001702uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001703 return android_atomic_release_load(&mTransactionFlags);
1704}
1705
Mathias Agopian3f844832013-08-07 21:24:32 -07001706uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001707 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1708}
1709
Mathias Agopian3f844832013-08-07 21:24:32 -07001710uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001711 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1712 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001713 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001714 }
1715 return old;
1716}
1717
Mathias Agopian8b33f032012-07-24 20:43:54 -07001718void SurfaceFlinger::setTransactionState(
1719 const Vector<ComposerState>& state,
1720 const Vector<DisplayState>& displays,
1721 uint32_t flags)
1722{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001723 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001724 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001725 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001726
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001727 if (flags & eAnimation) {
1728 // For window updates that are part of an animation we must wait for
1729 // previous animation "frames" to be handled.
1730 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001731 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001732 if (CC_UNLIKELY(err != NO_ERROR)) {
1733 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001734 // caller after a few seconds.
1735 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1736 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001737 mAnimTransactionPending = false;
1738 break;
1739 }
1740 }
1741 }
1742
Mathias Agopiane57f2922012-08-09 16:29:12 -07001743 size_t count = displays.size();
1744 for (size_t i=0 ; i<count ; i++) {
1745 const DisplayState& s(displays[i]);
1746 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001747 }
1748
Mathias Agopiane57f2922012-08-09 16:29:12 -07001749 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001750 for (size_t i=0 ; i<count ; i++) {
1751 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001752 // Here we need to check that the interface we're given is indeed
1753 // one of our own. A malicious client could give us a NULL
1754 // IInterface, or one of its own or even one of our own but a
1755 // different type. All these situations would cause us to crash.
1756 //
1757 // NOTE: it would be better to use RTTI as we could directly check
1758 // that we have a Client*. however, RTTI is disabled in Android.
1759 if (s.client != NULL) {
1760 sp<IBinder> binder = s.client->asBinder();
1761 if (binder != NULL) {
1762 String16 desc(binder->getInterfaceDescriptor());
1763 if (desc == ISurfaceComposerClient::descriptor) {
1764 sp<Client> client( static_cast<Client *>(s.client.get()) );
1765 transactionFlags |= setClientStateLocked(client, s.state);
1766 }
1767 }
1768 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001769 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001770
Mathias Agopian386aa982011-11-07 21:58:03 -08001771 if (transactionFlags) {
1772 // this triggers the transaction
1773 setTransactionFlags(transactionFlags);
1774
1775 // if this is a synchronous transaction, wait for it to take effect
1776 // before returning.
1777 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001778 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001779 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001780 if (flags & eAnimation) {
1781 mAnimTransactionPending = true;
1782 }
1783 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001784 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1785 if (CC_UNLIKELY(err != NO_ERROR)) {
1786 // just in case something goes wrong in SF, return to the
1787 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001788 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1789 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001790 break;
1791 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001792 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001793 }
1794}
1795
Mathias Agopiane57f2922012-08-09 16:29:12 -07001796uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1797{
Jesse Hall9a143922012-10-04 16:29:19 -07001798 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1799 if (dpyIdx < 0)
1800 return 0;
1801
Mathias Agopiane57f2922012-08-09 16:29:12 -07001802 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001803 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001804 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001805 const uint32_t what = s.what;
1806 if (what & DisplayState::eSurfaceChanged) {
1807 if (disp.surface->asBinder() != s.surface->asBinder()) {
1808 disp.surface = s.surface;
1809 flags |= eDisplayTransactionNeeded;
1810 }
1811 }
1812 if (what & DisplayState::eLayerStackChanged) {
1813 if (disp.layerStack != s.layerStack) {
1814 disp.layerStack = s.layerStack;
1815 flags |= eDisplayTransactionNeeded;
1816 }
1817 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001818 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001819 if (disp.orientation != s.orientation) {
1820 disp.orientation = s.orientation;
1821 flags |= eDisplayTransactionNeeded;
1822 }
1823 if (disp.frame != s.frame) {
1824 disp.frame = s.frame;
1825 flags |= eDisplayTransactionNeeded;
1826 }
1827 if (disp.viewport != s.viewport) {
1828 disp.viewport = s.viewport;
1829 flags |= eDisplayTransactionNeeded;
1830 }
1831 }
1832 }
1833 return flags;
1834}
1835
1836uint32_t SurfaceFlinger::setClientStateLocked(
1837 const sp<Client>& client,
1838 const layer_state_t& s)
1839{
1840 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001841 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001842 if (layer != 0) {
1843 const uint32_t what = s.what;
1844 if (what & layer_state_t::ePositionChanged) {
1845 if (layer->setPosition(s.x, s.y))
1846 flags |= eTraversalNeeded;
1847 }
1848 if (what & layer_state_t::eLayerChanged) {
1849 // NOTE: index needs to be calculated before we update the state
1850 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1851 if (layer->setLayer(s.z)) {
1852 mCurrentState.layersSortedByZ.removeAt(idx);
1853 mCurrentState.layersSortedByZ.add(layer);
1854 // we need traversal (state changed)
1855 // AND transaction (list changed)
1856 flags |= eTransactionNeeded|eTraversalNeeded;
1857 }
1858 }
1859 if (what & layer_state_t::eSizeChanged) {
1860 if (layer->setSize(s.w, s.h)) {
1861 flags |= eTraversalNeeded;
1862 }
1863 }
1864 if (what & layer_state_t::eAlphaChanged) {
1865 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1866 flags |= eTraversalNeeded;
1867 }
1868 if (what & layer_state_t::eMatrixChanged) {
1869 if (layer->setMatrix(s.matrix))
1870 flags |= eTraversalNeeded;
1871 }
1872 if (what & layer_state_t::eTransparentRegionChanged) {
1873 if (layer->setTransparentRegionHint(s.transparentRegion))
1874 flags |= eTraversalNeeded;
1875 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08001876 if ((what & layer_state_t::eVisibilityChanged) ||
1877 (what & layer_state_t::eOpacityChanged)) {
1878 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07001879 if (layer->setFlags(s.flags, s.mask))
1880 flags |= eTraversalNeeded;
1881 }
1882 if (what & layer_state_t::eCropChanged) {
1883 if (layer->setCrop(s.crop))
1884 flags |= eTraversalNeeded;
1885 }
1886 if (what & layer_state_t::eLayerStackChanged) {
1887 // NOTE: index needs to be calculated before we update the state
1888 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1889 if (layer->setLayerStack(s.layerStack)) {
1890 mCurrentState.layersSortedByZ.removeAt(idx);
1891 mCurrentState.layersSortedByZ.add(layer);
1892 // we need traversal (state changed)
1893 // AND transaction (list changed)
1894 flags |= eTransactionNeeded|eTraversalNeeded;
1895 }
1896 }
1897 }
1898 return flags;
1899}
1900
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001901status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001902 const String8& name,
1903 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001904 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1905 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001906{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001907 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001908 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001909 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001910 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001911 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001912 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001913
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001914 status_t result = NO_ERROR;
1915
1916 sp<Layer> layer;
1917
Mathias Agopian3165cc22012-08-08 19:42:09 -07001918 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1919 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001920 result = createNormalLayer(client,
1921 name, w, h, flags, format,
1922 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001923 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001924 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001925 result = createDimLayer(client,
1926 name, w, h, flags,
1927 handle, gbp, &layer);
1928 break;
1929 default:
1930 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001931 break;
1932 }
1933
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001934 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001935 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001936 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001937 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001938 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001939}
1940
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001941status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1942 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1943 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001944{
1945 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001946 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001947 case PIXEL_FORMAT_TRANSPARENT:
1948 case PIXEL_FORMAT_TRANSLUCENT:
1949 format = PIXEL_FORMAT_RGBA_8888;
1950 break;
1951 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001952#ifdef NO_RGBX_8888
1953 format = PIXEL_FORMAT_RGB_565;
1954#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001955 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001956#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001957 break;
1958 }
1959
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001960#ifdef NO_RGBX_8888
1961 if (format == PIXEL_FORMAT_RGBX_8888)
1962 format = PIXEL_FORMAT_RGBA_8888;
1963#endif
1964
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001965 *outLayer = new Layer(this, client, name, w, h, flags);
1966 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1967 if (err == NO_ERROR) {
1968 *handle = (*outLayer)->getHandle();
1969 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001970 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001971
1972 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1973 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001974}
1975
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001976status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1977 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1978 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001979{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001980 *outLayer = new LayerDim(this, client, name, w, h, flags);
1981 *handle = (*outLayer)->getHandle();
1982 *gbp = (*outLayer)->getBufferQueue();
1983 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001984}
1985
Mathias Agopianac9fa422013-02-11 16:40:36 -08001986status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001987{
Mathias Agopian67106042013-03-14 19:18:13 -07001988 // called by the window manager when it wants to remove a Layer
1989 status_t err = NO_ERROR;
1990 sp<Layer> l(client->getLayerUser(handle));
1991 if (l != NULL) {
1992 err = removeLayer(l);
1993 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1994 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001995 }
1996 return err;
1997}
1998
Mathias Agopian13127d82013-03-05 17:47:11 -08001999status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002000{
Mathias Agopian67106042013-03-14 19:18:13 -07002001 // called by ~LayerCleaner() when all references to the IBinder (handle)
2002 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002003 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002004 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002005 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002006 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002007 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002008 "error removing layer=%p (%s)", l.get(), strerror(-err));
2009 }
2010 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002011}
2012
Mathias Agopianb60314a2012-04-10 22:09:54 -07002013// ---------------------------------------------------------------------------
2014
Andy McFadden13a082e2012-08-24 10:16:42 -07002015void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002016 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002017 Vector<ComposerState> state;
2018 Vector<DisplayState> displays;
2019 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002020 d.what = DisplayState::eDisplayProjectionChanged |
2021 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002022 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002023 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002024 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002025 d.frame.makeInvalid();
2026 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002027 displays.add(d);
2028 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002029 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07002030
2031 const nsecs_t period =
2032 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2033 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002034}
2035
2036void SurfaceFlinger::initializeDisplays() {
2037 class MessageScreenInitialized : public MessageBase {
2038 SurfaceFlinger* flinger;
2039 public:
2040 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2041 virtual bool handler() {
2042 flinger->onInitializeDisplays();
2043 return true;
2044 }
2045 };
2046 sp<MessageBase> msg = new MessageScreenInitialized(this);
2047 postMessageAsync(msg); // we may be called from main thread, use async message
2048}
2049
2050
Mathias Agopiancde87a32012-09-13 14:09:01 -07002051void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002052 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2053 if (hw->isScreenAcquired()) {
2054 // this is expected, e.g. when power manager wakes up during boot
2055 ALOGD(" screen was previously acquired");
2056 return;
2057 }
2058
Mathias Agopian42977342012-08-05 00:40:46 -07002059 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002060 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002061 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002062 // built-in display, tell the HWC
2063 getHwComposer().acquire(type);
2064
2065 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2066 // FIXME: eventthread only knows about the main display right now
2067 mEventThread->onScreenAcquired();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07002068
Jesse Hall948fe0c2013-10-14 12:56:09 -07002069 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002070 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002071 }
Mathias Agopian20128302012-08-13 18:32:13 -07002072 mVisibleRegionsDirty = true;
2073 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002074}
2075
Mathias Agopiancde87a32012-09-13 14:09:01 -07002076void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002077 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2078 if (!hw->isScreenAcquired()) {
2079 ALOGD(" screen was previously released");
2080 return;
2081 }
2082
2083 hw->releaseScreen();
2084 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002085 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002086 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002087 disableHardwareVsync(true); // also cancels any in-progress resync
2088
Mathias Agopiancde87a32012-09-13 14:09:01 -07002089 // FIXME: eventthread only knows about the main display right now
2090 mEventThread->onScreenReleased();
2091 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002092
2093 // built-in display, tell the HWC
2094 getHwComposer().release(type);
2095 }
2096 mVisibleRegionsDirty = true;
2097 // from this point on, SF will stop drawing on this display
2098}
2099
2100void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2101 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002102 SurfaceFlinger& mFlinger;
2103 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002104 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002105 MessageScreenAcquired(SurfaceFlinger& flinger,
2106 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002107 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002108 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2109 if (hw == NULL) {
2110 ALOGE("Attempt to unblank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002111 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002112 ALOGW("Attempt to unblank virtual display");
2113 } else {
2114 mFlinger.onScreenAcquired(hw);
2115 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002116 return true;
2117 }
2118 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002119 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2120 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002121}
2122
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002123void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002124 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002125 SurfaceFlinger& mFlinger;
2126 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002127 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002128 MessageScreenReleased(SurfaceFlinger& flinger,
2129 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002130 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002131 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2132 if (hw == NULL) {
2133 ALOGE("Attempt to blank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002134 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002135 ALOGW("Attempt to blank virtual display");
2136 } else {
2137 mFlinger.onScreenReleased(hw);
2138 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002139 return true;
2140 }
2141 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002142 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2143 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002144}
2145
2146// ---------------------------------------------------------------------------
2147
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002148status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2149{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002150 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002151
Mathias Agopianbd115332013-04-18 16:41:04 -07002152 IPCThreadState* ipc = IPCThreadState::self();
2153 const int pid = ipc->getCallingPid();
2154 const int uid = ipc->getCallingUid();
2155 if ((uid != AID_SHELL) &&
2156 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002157 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002158 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002159 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002160 // Try to get the main lock, but don't insist if we can't
2161 // (this would indicate SF is stuck, but we want to be able to
2162 // print something in dumpsys).
2163 int retry = 3;
2164 while (mStateLock.tryLock()<0 && --retry>=0) {
2165 usleep(1000000);
2166 }
2167 const bool locked(retry >= 0);
2168 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002169 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002170 "SurfaceFlinger appears to be unresponsive, "
2171 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002172 }
2173
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002174 bool dumpAll = true;
2175 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002176 size_t numArgs = args.size();
2177 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002178 if ((index < numArgs) &&
2179 (args[index] == String16("--list"))) {
2180 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002181 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002182 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002183 }
2184
2185 if ((index < numArgs) &&
2186 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002187 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002188 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002189 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002190 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002191
2192 if ((index < numArgs) &&
2193 (args[index] == String16("--latency-clear"))) {
2194 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002195 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002196 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002197 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002198 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002199
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002200 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002201 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002202 }
2203
Mathias Agopian9795c422009-08-26 16:36:26 -07002204 if (locked) {
2205 mStateLock.unlock();
2206 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002207 }
2208 write(fd, result.string(), result.size());
2209 return NO_ERROR;
2210}
2211
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002212void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002213 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002214{
2215 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2216 const size_t count = currentLayers.size();
2217 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002218 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002219 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002220 }
2221}
2222
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002223void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002224 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002225{
2226 String8 name;
2227 if (index < args.size()) {
2228 name = String8(args[index]);
2229 index++;
2230 }
2231
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002232 const nsecs_t period =
2233 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2234 result.appendFormat("%lld\n", period);
2235
2236 if (name.isEmpty()) {
2237 mAnimFrameTracker.dump(result);
2238 } else {
2239 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2240 const size_t count = currentLayers.size();
2241 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002242 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002243 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002244 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002245 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002246 }
2247 }
2248}
2249
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002250void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002251 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002252{
2253 String8 name;
2254 if (index < args.size()) {
2255 name = String8(args[index]);
2256 index++;
2257 }
2258
2259 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2260 const size_t count = currentLayers.size();
2261 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002262 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002263 if (name.isEmpty() || (name == layer->getName())) {
2264 layer->clearStats();
2265 }
2266 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002267
2268 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002269}
2270
Jamie Gennis6547ff42013-07-16 20:12:42 -07002271// This should only be called from the main thread. Otherwise it would need
2272// the lock and should use mCurrentState rather than mDrawingState.
2273void SurfaceFlinger::logFrameStats() {
2274 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2275 const size_t count = drawingLayers.size();
2276 for (size_t i=0 ; i<count ; i++) {
2277 const sp<Layer>& layer(drawingLayers[i]);
2278 layer->logFrameStats();
2279 }
2280
2281 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2282}
2283
Andy McFadden4803b742012-09-24 19:07:20 -07002284/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2285{
2286 static const char* config =
2287 " [sf"
2288#ifdef NO_RGBX_8888
2289 " NO_RGBX_8888"
2290#endif
2291#ifdef HAS_CONTEXT_PRIORITY
2292 " HAS_CONTEXT_PRIORITY"
2293#endif
2294#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2295 " NEVER_DEFAULT_TO_ASYNC_MODE"
2296#endif
2297#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2298 " TARGET_DISABLE_TRIPLE_BUFFERING"
2299#endif
2300 "]";
2301 result.append(config);
2302}
2303
Mathias Agopian74d211a2013-04-22 16:55:35 +02002304void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2305 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002306{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002307 bool colorize = false;
2308 if (index < args.size()
2309 && (args[index] == String16("--color"))) {
2310 colorize = true;
2311 index++;
2312 }
2313
2314 Colorizer colorizer(colorize);
2315
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002316 // figure out if we're stuck somewhere
2317 const nsecs_t now = systemTime();
2318 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2319 const nsecs_t inTransaction(mDebugInTransaction);
2320 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2321 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2322
2323 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002324 * Dump library configuration.
2325 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002326
2327 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002328 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002329 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002330 appendSfConfigString(result);
2331 appendUiConfigString(result);
2332 appendGuiConfigString(result);
2333 result.append("\n");
2334
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002335 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002336 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002337 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002338 result.append(SyncFeatures::getInstance().toString());
2339 result.append("\n");
2340
Andy McFadden4803b742012-09-24 19:07:20 -07002341 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002342 * Dump the visible layer list
2343 */
2344 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2345 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002346 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002347 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002348 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002349 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002350 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002351 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002352 }
2353
2354 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002355 * Dump Display state
2356 */
2357
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002358 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002359 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002360 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002361 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2362 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002363 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002364 }
2365
2366 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002367 * Dump SurfaceFlinger global state
2368 */
2369
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002370 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002371 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002372 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002373
Mathias Agopian888c8222012-08-04 21:10:38 -07002374 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002375 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002376
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002377 colorizer.bold(result);
2378 result.appendFormat("EGL implementation : %s\n",
2379 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2380 colorizer.reset(result);
2381 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002382 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002383
Mathias Agopian875d8e12013-06-07 15:35:48 -07002384 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002385
Mathias Agopian42977342012-08-05 00:40:46 -07002386 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002387 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002388 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002389 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002390 " last eglSwapBuffers() time: %f us\n"
2391 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002392 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002393 " refresh-rate : %f fps\n"
2394 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002395 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002396 " gpu_to_cpu_unsupported : %d\n"
2397 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002398 mLastSwapBufferTime/1000.0,
2399 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002400 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002401 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2402 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002403 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002404 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002405
Mathias Agopian74d211a2013-04-22 16:55:35 +02002406 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002407 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002408
Mathias Agopian74d211a2013-04-22 16:55:35 +02002409 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002410 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002411
2412 /*
2413 * VSYNC state
2414 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002415 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002416
2417 /*
2418 * Dump HWComposer state
2419 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002420 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002421 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002422 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002423 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002424 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002425 (mDebugDisableHWC || mDebugRegion || mDaltonize
2426 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002427 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002428
2429 /*
2430 * Dump gralloc state
2431 */
2432 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2433 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002434}
2435
Mathias Agopian13127d82013-03-05 17:47:11 -08002436const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002437SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002438 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002439 wp<IBinder> dpy;
2440 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2441 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2442 dpy = mDisplays.keyAt(i);
2443 break;
2444 }
2445 }
2446 if (dpy == NULL) {
2447 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2448 // Just use the primary display so we have something to return
2449 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2450 }
2451 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002452}
2453
Keun young Park63f165f2012-08-31 10:53:36 -07002454bool SurfaceFlinger::startDdmConnection()
2455{
2456 void* libddmconnection_dso =
2457 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2458 if (!libddmconnection_dso) {
2459 return false;
2460 }
2461 void (*DdmConnection_start)(const char* name);
2462 DdmConnection_start =
2463 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2464 if (!DdmConnection_start) {
2465 dlclose(libddmconnection_dso);
2466 return false;
2467 }
2468 (*DdmConnection_start)(getServiceName());
2469 return true;
2470}
2471
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002472status_t SurfaceFlinger::onTransact(
2473 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2474{
2475 switch (code) {
2476 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002477 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002478 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002479 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002480 case BLANK:
2481 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002482 {
2483 // codes that require permission check
2484 IPCThreadState* ipc = IPCThreadState::self();
2485 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002486 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002487 if ((uid != AID_GRAPHICS) &&
2488 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002489 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002490 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2491 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002492 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002493 break;
2494 }
2495 case CAPTURE_SCREEN:
2496 {
2497 // codes that require permission check
2498 IPCThreadState* ipc = IPCThreadState::self();
2499 const int pid = ipc->getCallingPid();
2500 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002501 if ((uid != AID_GRAPHICS) &&
2502 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002503 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002504 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2505 return PERMISSION_DENIED;
2506 }
2507 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002508 }
2509 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002510
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002511 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2512 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002513 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002514 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002515 IPCThreadState* ipc = IPCThreadState::self();
2516 const int pid = ipc->getCallingPid();
2517 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002518 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002519 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002520 return PERMISSION_DENIED;
2521 }
2522 int n;
2523 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002524 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002525 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002526 return NO_ERROR;
2527 case 1002: // SHOW_UPDATES
2528 n = data.readInt32();
2529 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002530 invalidateHwcGeometry();
2531 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002532 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002533 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002534 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002535 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002536 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002537 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002538 setTransactionFlags(
2539 eTransactionNeeded|
2540 eDisplayTransactionNeeded|
2541 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002542 return NO_ERROR;
2543 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002544 case 1006:{ // send empty update
2545 signalRefresh();
2546 return NO_ERROR;
2547 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002548 case 1008: // toggle use of hw composer
2549 n = data.readInt32();
2550 mDebugDisableHWC = n ? 1 : 0;
2551 invalidateHwcGeometry();
2552 repaintEverything();
2553 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002554 case 1009: // toggle use of transform hint
2555 n = data.readInt32();
2556 mDebugDisableTransformHint = n ? 1 : 0;
2557 invalidateHwcGeometry();
2558 repaintEverything();
2559 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002560 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002561 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002562 reply->writeInt32(0);
2563 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002564 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002565 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002566 return NO_ERROR;
2567 case 1013: {
2568 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002569 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2570 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002571 return NO_ERROR;
2572 }
2573 case 1014: {
2574 // daltonize
2575 n = data.readInt32();
2576 switch (n % 10) {
2577 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2578 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2579 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2580 }
2581 if (n >= 10) {
2582 mDaltonizer.setMode(Daltonizer::correction);
2583 } else {
2584 mDaltonizer.setMode(Daltonizer::simulation);
2585 }
2586 mDaltonize = n > 0;
2587 invalidateHwcGeometry();
2588 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002589 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002590 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002591 case 1015: {
2592 // apply a color matrix
2593 n = data.readInt32();
2594 mHasColorMatrix = n ? 1 : 0;
2595 if (n) {
2596 // color matrix is sent as mat3 matrix followed by vec3
2597 // offset, then packed into a mat4 where the last row is
2598 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002599 for (size_t i = 0 ; i < 4; i++) {
2600 for (size_t j = 0; j < 4; j++) {
2601 mColorMatrix[i][j] = data.readFloat();
2602 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002603 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002604 } else {
2605 mColorMatrix = mat4();
2606 }
2607 invalidateHwcGeometry();
2608 repaintEverything();
2609 return NO_ERROR;
2610 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002611 }
2612 }
2613 return err;
2614}
2615
Mathias Agopian53331da2011-08-22 21:44:41 -07002616void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002617 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002618 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002619}
2620
Mathias Agopian59119e62010-10-11 12:37:43 -07002621// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002622// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002623// ---------------------------------------------------------------------------
2624
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002625/* The code below is here to handle b/8734824
2626 *
2627 * We create a IGraphicBufferProducer wrapper that forwards all calls
2628 * to the calling binder thread, where they are executed. This allows
2629 * the calling thread to be reused (on the other side) and not
2630 * depend on having "enough" binder threads to handle the requests.
2631 *
2632 */
2633
2634class GraphicProducerWrapper : public BBinder, public MessageHandler {
2635 sp<IGraphicBufferProducer> impl;
2636 sp<Looper> looper;
2637 status_t result;
2638 bool exitPending;
2639 bool exitRequested;
2640 mutable Barrier barrier;
2641 volatile int32_t memoryBarrier;
2642 uint32_t code;
2643 Parcel const* data;
2644 Parcel* reply;
2645
2646 enum {
2647 MSG_API_CALL,
2648 MSG_EXIT
2649 };
2650
2651 /*
2652 * this is called by our "fake" BpGraphicBufferProducer. We package the
2653 * data and reply Parcel and forward them to the calling thread.
2654 */
2655 virtual status_t transact(uint32_t code,
2656 const Parcel& data, Parcel* reply, uint32_t flags) {
2657 this->code = code;
2658 this->data = &data;
2659 this->reply = reply;
2660 android_atomic_acquire_store(0, &memoryBarrier);
2661 if (exitPending) {
2662 // if we've exited, we run the message synchronously right here
2663 handleMessage(Message(MSG_API_CALL));
2664 } else {
2665 barrier.close();
2666 looper->sendMessage(this, Message(MSG_API_CALL));
2667 barrier.wait();
2668 }
2669 return NO_ERROR;
2670 }
2671
2672 /*
2673 * here we run on the binder calling thread. All we've got to do is
2674 * call the real BpGraphicBufferProducer.
2675 */
2676 virtual void handleMessage(const Message& message) {
2677 android_atomic_release_load(&memoryBarrier);
2678 if (message.what == MSG_API_CALL) {
2679 impl->asBinder()->transact(code, data[0], reply);
2680 barrier.open();
2681 } else if (message.what == MSG_EXIT) {
2682 exitRequested = true;
2683 }
2684 }
2685
2686public:
2687 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2688 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2689 exitPending(false), exitRequested(false) {
2690 }
2691
2692 status_t waitForResponse() {
2693 do {
2694 looper->pollOnce(-1);
2695 } while (!exitRequested);
2696 return result;
2697 }
2698
2699 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002700 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002701 exitPending = true;
2702 looper->sendMessage(this, Message(MSG_EXIT));
2703 }
2704};
2705
2706
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002707status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2708 const sp<IGraphicBufferProducer>& producer,
2709 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002710 uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002711
2712 if (CC_UNLIKELY(display == 0))
2713 return BAD_VALUE;
2714
2715 if (CC_UNLIKELY(producer == 0))
2716 return BAD_VALUE;
2717
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002718 // if we have secure windows on this display, never allow the screen capture
2719 // unless the producer interface is local (i.e.: we can take a screenshot for
2720 // ourselves).
2721 if (!producer->asBinder()->localBinder()) {
2722 Mutex::Autolock _l(mStateLock);
2723 sp<const DisplayDevice> hw(getDisplayDevice(display));
2724 if (hw->getSecureLayerVisible()) {
2725 ALOGW("FB is protected: PERMISSION_DENIED");
2726 return PERMISSION_DENIED;
2727 }
2728 }
2729
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002730 class MessageCaptureScreen : public MessageBase {
2731 SurfaceFlinger* flinger;
2732 sp<IBinder> display;
2733 sp<IGraphicBufferProducer> producer;
2734 uint32_t reqWidth, reqHeight;
2735 uint32_t minLayerZ,maxLayerZ;
2736 status_t result;
2737 public:
2738 MessageCaptureScreen(SurfaceFlinger* flinger,
2739 const sp<IBinder>& display,
2740 const sp<IGraphicBufferProducer>& producer,
2741 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002742 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002743 : flinger(flinger), display(display), producer(producer),
2744 reqWidth(reqWidth), reqHeight(reqHeight),
2745 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2746 result(PERMISSION_DENIED)
2747 {
2748 }
2749 status_t getResult() const {
2750 return result;
2751 }
2752 virtual bool handler() {
2753 Mutex::Autolock _l(flinger->mStateLock);
2754 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002755 result = flinger->captureScreenImplLocked(hw,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002756 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002757 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002758 return true;
2759 }
2760 };
2761
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002762 // make sure to process transactions before screenshots -- a transaction
2763 // might already be pending but scheduled for VSYNC; this guarantees we
2764 // will handle it before the screenshot. When VSYNC finally arrives
2765 // the scheduled transaction will be a no-op. If no transactions are
2766 // scheduled at this time, this will end-up being a no-op as well.
2767 mEventQueue.invalidateTransactionNow();
2768
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002769 // this creates a "fake" BBinder which will serve as a "fake" remote
2770 // binder to receive the marshaled calls and forward them to the
2771 // real remote (a BpGraphicBufferProducer)
2772 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2773
2774 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2775 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002776 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002777 display, IGraphicBufferProducer::asInterface( wrapper ),
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002778 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002779
2780 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002781 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002782 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002783 }
2784 return res;
2785}
2786
Mathias Agopian180f10d2013-04-10 22:55:41 -07002787
2788void SurfaceFlinger::renderScreenImplLocked(
2789 const sp<const DisplayDevice>& hw,
2790 uint32_t reqWidth, uint32_t reqHeight,
2791 uint32_t minLayerZ, uint32_t maxLayerZ,
2792 bool yswap)
2793{
2794 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002795 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002796
2797 // get screen geometry
2798 const uint32_t hw_w = hw->getWidth();
2799 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002800 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2801
2802 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002803 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002804
2805 // set-up our viewport
Mathias Agopian3f844832013-08-07 21:24:32 -07002806 engine.setViewportAndProjection(reqWidth, reqHeight, hw_w, hw_h, yswap);
2807 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002808
2809 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002810 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002811
2812 const LayerVector& layers( mDrawingState.layersSortedByZ );
2813 const size_t count = layers.size();
2814 for (size_t i=0 ; i<count ; ++i) {
2815 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002816 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002817 if (state.layerStack == hw->getLayerStack()) {
2818 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2819 if (layer->isVisible()) {
2820 if (filtering) layer->setFiltering(true);
2821 layer->draw(hw);
2822 if (filtering) layer->setFiltering(false);
2823 }
2824 }
2825 }
2826 }
2827
2828 // compositionComplete is needed for older driver
2829 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002830 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002831}
2832
2833
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002834status_t SurfaceFlinger::captureScreenImplLocked(
2835 const sp<const DisplayDevice>& hw,
2836 const sp<IGraphicBufferProducer>& producer,
2837 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002838 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002839{
2840 ATRACE_CALL();
2841
Mathias Agopian180f10d2013-04-10 22:55:41 -07002842 // get screen geometry
2843 const uint32_t hw_w = hw->getWidth();
2844 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002845
Mathias Agopian180f10d2013-04-10 22:55:41 -07002846 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2847 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2848 reqWidth, reqHeight, hw_w, hw_h);
2849 return BAD_VALUE;
2850 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002851
Mathias Agopian180f10d2013-04-10 22:55:41 -07002852 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2853 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2854
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002855 // create a surface (because we're a producer, and we need to
2856 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07002857 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002858 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002859
2860 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002861 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002862 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2863 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002864
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002865 int err = 0;
2866 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07002867 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002868 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2869 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002870
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002871 if (err == NO_ERROR) {
2872 ANativeWindowBuffer* buffer;
2873 /* TODO: Once we have the sync framework everywhere this can use
2874 * server-side waits on the fence that dequeueBuffer returns.
2875 */
2876 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2877 if (result == NO_ERROR) {
2878 // create an EGLImage from the buffer so we can later
2879 // turn it into a texture
2880 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2881 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2882 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002883 // this binds the given EGLImage as a framebuffer for the
2884 // duration of this scope.
2885 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2886 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002887 // this will in fact render into our dequeued buffer
2888 // via an FBO, which means we didn't have to create
2889 // an EGLSurface and therefore we're not
2890 // dependent on the context's EGLConfig.
2891 renderScreenImplLocked(hw, reqWidth, reqHeight,
2892 minLayerZ, maxLayerZ, true);
Mathias Agopiand5556842013-09-19 17:08:37 -07002893
Andy McFadden2d8d1202013-10-09 16:38:02 -07002894 // Create a sync point and wait on it, so we know the buffer is
2895 // ready before we pass it along. We can't trivially call glFlush(),
2896 // so we use a wait flag instead.
2897 // TODO: pass a sync fd to queueBuffer() and let the consumer wait.
2898 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
2899 if (sync != EGL_NO_SYNC_KHR) {
2900 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
2901 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
2902 EGLint eglErr = eglGetError();
2903 eglDestroySyncKHR(mEGLDisplay, sync);
2904 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
2905 ALOGW("captureScreen: fence wait timed out");
2906 } else {
2907 ALOGW_IF(eglErr != EGL_SUCCESS,
2908 "captureScreen: error waiting on EGL fence: %#x", eglErr);
2909 }
2910 } else {
2911 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
2912 // not fatal
2913 }
2914
Mathias Agopiand5556842013-09-19 17:08:37 -07002915 if (DEBUG_SCREENSHOTS) {
2916 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
2917 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
2918 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
2919 hw, minLayerZ, maxLayerZ);
2920 delete [] pixels;
2921 }
2922
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002923 } else {
2924 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2925 result = INVALID_OPERATION;
2926 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002927 // destroy our image
2928 eglDestroyImageKHR(mEGLDisplay, image);
2929 } else {
2930 result = BAD_VALUE;
2931 }
2932 window->queueBuffer(window, buffer, -1);
2933 }
2934 } else {
2935 result = BAD_VALUE;
2936 }
2937 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2938 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002939
Mathias Agopian74c40c02010-09-29 13:02:36 -07002940 return result;
2941}
2942
Mathias Agopiand5556842013-09-19 17:08:37 -07002943void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
2944 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07002945 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07002946 for (size_t y=0 ; y<h ; y++) {
2947 uint32_t const * p = (uint32_t const *)vaddr + y*s;
2948 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07002949 if (p[x] != 0xFF000000) return;
2950 }
2951 }
2952 ALOGE("*** we just took a black screenshot ***\n"
2953 "requested minz=%d, maxz=%d, layerStack=%d",
2954 minLayerZ, maxLayerZ, hw->getLayerStack());
2955 const LayerVector& layers( mDrawingState.layersSortedByZ );
2956 const size_t count = layers.size();
2957 for (size_t i=0 ; i<count ; ++i) {
2958 const sp<Layer>& layer(layers[i]);
2959 const Layer::State& state(layer->getDrawingState());
2960 const bool visible = (state.layerStack == hw->getLayerStack())
2961 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2962 && (layer->isVisible());
2963 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
2964 visible ? '+' : '-',
2965 i, layer->getName().string(), state.layerStack, state.z,
2966 layer->isVisible(), state.flags, state.alpha);
2967 }
2968 }
2969}
2970
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002971// ---------------------------------------------------------------------------
2972
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002973SurfaceFlinger::LayerVector::LayerVector() {
2974}
2975
2976SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002977 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002978}
2979
2980int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2981 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002982{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002983 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002984 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2985 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002986
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002987 uint32_t ls = l->getCurrentState().layerStack;
2988 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002989 if (ls != rs)
2990 return ls - rs;
2991
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002992 uint32_t lz = l->getCurrentState().z;
2993 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002994 if (lz != rz)
2995 return lz - rz;
2996
2997 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002998}
2999
3000// ---------------------------------------------------------------------------
3001
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003002SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
3003 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003004}
3005
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003006SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08003007 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003008 viewport.makeInvalid();
3009 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003010}
3011
3012// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003013
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003014}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003015
3016
3017#if defined(__gl_h_)
3018#error "don't include gl/gl.h in this file"
3019#endif
3020
3021#if defined(__gl2_h_)
3022#error "don't include gl2/gl2.h in this file"
3023#endif