blob: 3c447db036aa22cccf306813a4697ab06cd6cc52 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Greg Hackmann86efcc02014-03-07 12:44:02 -080024#include <inttypes.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070025
26#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
28#include <cutils/log.h>
29#include <cutils/properties.h>
30
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070031#include <binder/IPCThreadState.h>
32#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070033#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070034#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070035
Mathias Agopianc666cae2012-07-25 18:56:13 -070036#include <ui/DisplayInfo.h>
37
Mathias Agopian921e6ac2012-07-23 23:11:29 -070038#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070039#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070040#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070041#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080042#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080043#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070044
45#include <ui/GraphicBufferAllocator.h>
46#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070047#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080048
Mathias Agopiancde87a32012-09-13 14:09:01 -070049#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050#include <utils/String8.h>
51#include <utils/String16.h>
52#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080053#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
Mathias Agopian921e6ac2012-07-23 23:11:29 -070055#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070056#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
Mathias Agopian3e25fd82013-04-22 17:52:16 +020058#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020060#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080061#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070062#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070063#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070064#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080065#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069
Mathias Agopiana4912602012-07-12 14:25:33 -070070#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070071#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070072#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073
Mathias Agopianff2ed702013-09-01 21:36:12 -070074#include "Effects/Daltonizer.h"
75
Mathias Agopian875d8e12013-06-07 15:35:48 -070076#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070077#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070078
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079#define DISPLAY_COUNT 1
80
Mathias Agopianfee2b462013-07-03 12:34:01 -070081/*
82 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
83 * black pixels.
84 */
85#define DEBUG_SCREENSHOTS false
86
Mathias Agopianca088332013-03-28 17:44:13 -070087EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
88
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070090
91// This works around the lack of support for the sync framework on some
92// devices.
93#ifdef RUNNING_WITHOUT_SYNC_FRAMEWORK
94static const bool runningWithoutSyncFramework = true;
95#else
96static const bool runningWithoutSyncFramework = false;
97#endif
98
99// This is the phase offset in nanoseconds of the software vsync event
100// relative to the vsync event reported by HWComposer. The software vsync
101// event is when SurfaceFlinger and Choreographer-based applications run each
102// frame.
103//
104// This phase offset allows adjustment of the minimum latency from application
105// wake-up (by Choregographer) time to the time at which the resulting window
106// image is displayed. This value may be either positive (after the HW vsync)
107// or negative (before the HW vsync). Setting it to 0 will result in a
108// minimum latency of two vsync periods because the app and SurfaceFlinger
109// will run just after the HW vsync. Setting it to a positive number will
110// result in the minimum latency being:
111//
112// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
113//
114// Note that reducing this latency makes it more likely for the applications
115// to not have their window content image ready in time. When this happens
116// the latency will end up being an additional vsync period, and animations
117// will hiccup. Therefore, this latency should be tuned somewhat
118// conservatively (or at least with awareness of the trade-off being made).
119static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
120
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700121// This is the phase offset at which SurfaceFlinger's composition runs.
122static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
123
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124// ---------------------------------------------------------------------------
125
Mathias Agopian99b49842011-06-27 16:05:52 -0700126const String16 sHardwareTest("android.permission.HARDWARE_TEST");
127const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
128const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
129const String16 sDump("android.permission.DUMP");
130
131// ---------------------------------------------------------------------------
132
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700134 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700136 mTransactionPending(false),
137 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700138 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700139 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700140 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700143 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800144 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700146 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700147 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700148 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700149 mDebugInSwapBuffers(0),
150 mLastSwapBufferTime(0),
151 mDebugInTransaction(0),
152 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700153 mBootFinished(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700154 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700155 mHWVsyncAvailable(false),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700156 mDaltonize(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.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800433 ALOGD("marking display %zu 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++) {
Dan Stozaf3c07d42014-02-04 16:22:36 -0800885 bool mustRecompose =
886 !(mDisplays[dpy]->getDirtyRegion(false).isEmpty());
887 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700888 }
889
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700890 HWComposer& hwc(getHwComposer());
891 if (hwc.initCheck() == NO_ERROR) {
892 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700893 if (CC_UNLIKELY(mHwWorkListDirty)) {
894 mHwWorkListDirty = false;
895 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
896 sp<const DisplayDevice> hw(mDisplays[dpy]);
897 const int32_t id = hw->getHwcDisplayId();
898 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800899 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700900 hw->getVisibleLayersSortedByZ());
901 const size_t count = currentLayers.size();
902 if (hwc.createWorkList(id, count) == NO_ERROR) {
903 HWComposer::LayerListIterator cur = hwc.begin(id);
904 const HWComposer::LayerListIterator end = hwc.end(id);
905 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800906 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700907 layer->setGeometry(hw, *cur);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700908 if (mDebugDisableHWC || mDebugRegion || mDaltonize) {
Jamie Gennisa4310c82012-09-25 20:26:00 -0700909 cur->setSkip(true);
910 }
911 }
912 }
913 }
914 }
915 }
916
917 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700918 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700919 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700920 const int32_t id = hw->getHwcDisplayId();
921 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800922 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700923 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700924 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700925 HWComposer::LayerListIterator cur = hwc.begin(id);
926 const HWComposer::LayerListIterator end = hwc.end(id);
927 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
928 /*
929 * update the per-frame h/w composer data for each layer
930 * and build the transparent region of the FB
931 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800932 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700933 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700934 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700935 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700936 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700937
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700938 status_t err = hwc.prepare();
939 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700940
941 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
942 sp<const DisplayDevice> hw(mDisplays[dpy]);
943 hw->prepareFrame(hwc);
944 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700945 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700946}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700947
Mathias Agopiancd60f992012-08-16 16:28:27 -0700948void SurfaceFlinger::doComposition() {
949 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700950 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700951 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700952 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700953 if (hw->canDraw()) {
954 // transform the dirty region into this screen's coordinate space
955 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800956
957 // repaint the framebuffer (if needed)
958 doDisplayComposition(hw, dirtyRegion);
959
Mathias Agopiancd60f992012-08-16 16:28:27 -0700960 hw->dirtyRegion.clear();
961 hw->flip(hw->swapRegion);
962 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700963 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700964 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700965 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700966 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700967 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700968}
969
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800970void SurfaceFlinger::postFramebuffer()
971{
Mathias Agopian841cde52012-03-01 15:44:37 -0800972 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800973
Mathias Agopiana44b0412011-10-16 18:46:35 -0700974 const nsecs_t now = systemTime();
975 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700976
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700977 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700978 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700979 if (!hwc.supportsFramebufferTarget()) {
980 // EGL spec says:
981 // "surface must be bound to the calling thread's current context,
982 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700983 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700984 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700985 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700986 }
987
Mathias Agopian6da15f42013-09-25 20:40:07 -0700988 // make the default display current because the VirtualDisplayDevice code cannot
989 // deal with dequeueBuffer() being called outside of the composition loop; however
990 // the code below can call glFlush() which is allowed (and does in some case) call
991 // dequeueBuffer().
992 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
993
Mathias Agopian92a979a2012-08-02 18:32:23 -0700994 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700995 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800996 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700997 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700998 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700999 int32_t id = hw->getHwcDisplayId();
1000 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001001 HWComposer::LayerListIterator cur = hwc.begin(id);
1002 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001003 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001004 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001005 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001006 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001007 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001008 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001009 }
Jesse Hallef194142012-06-14 14:45:17 -07001010 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001011 }
1012
Mathias Agopiana44b0412011-10-16 18:46:35 -07001013 mLastSwapBufferTime = systemTime() - now;
1014 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001015
1016 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1017 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1018 logFrameStats();
1019 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001020}
1021
Mathias Agopian87baae12012-07-31 12:38:26 -07001022void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001023{
Mathias Agopian841cde52012-03-01 15:44:37 -08001024 ATRACE_CALL();
1025
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001026 // here we keep a copy of the drawing state (that is the state that's
1027 // going to be overwritten by handleTransactionLocked()) outside of
1028 // mStateLock so that the side-effects of the State assignment
1029 // don't happen with mStateLock held (which can cause deadlocks).
1030 State drawingState(mDrawingState);
1031
Mathias Agopianca4d3602011-05-19 15:38:14 -07001032 Mutex::Autolock _l(mStateLock);
1033 const nsecs_t now = systemTime();
1034 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001035
Mathias Agopianca4d3602011-05-19 15:38:14 -07001036 // Here we're guaranteed that some transaction flags are set
1037 // so we can call handleTransactionLocked() unconditionally.
1038 // We call getTransactionFlags(), which will also clear the flags,
1039 // with mStateLock held to guarantee that mCurrentState won't change
1040 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001041
Mathias Agopiane57f2922012-08-09 16:29:12 -07001042 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001043 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001044
Mathias Agopianca4d3602011-05-19 15:38:14 -07001045 mLastTransactionTime = systemTime() - now;
1046 mDebugInTransaction = 0;
1047 invalidateHwcGeometry();
1048 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001049}
1050
Mathias Agopian87baae12012-07-31 12:38:26 -07001051void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001052{
1053 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001054 const size_t count = currentLayers.size();
1055
1056 /*
1057 * Traversal of the children
1058 * (perform the transaction for each of them if needed)
1059 */
1060
Mathias Agopian3559b072012-08-15 13:46:03 -07001061 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001063 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1065 if (!trFlags) continue;
1066
1067 const uint32_t flags = layer->doTransaction(0);
1068 if (flags & Layer::eVisibleRegion)
1069 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001070 }
1071 }
1072
1073 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001074 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001075 */
1076
Mathias Agopiane57f2922012-08-09 16:29:12 -07001077 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001078 // here we take advantage of Vector's copy-on-write semantics to
1079 // improve performance by skipping the transaction entirely when
1080 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001081 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1082 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001083 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001084 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001085 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001086 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001087
1088 // find the displays that were removed
1089 // (ie: in drawing state but not in current state)
1090 // also handle displays that changed
1091 // (ie: displays that are in both lists)
1092 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001093 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1094 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001095 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001096 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001097 // Call makeCurrent() on the primary display so we can
1098 // be sure that nothing associated with this display
1099 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001100 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001101 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001102 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1103 if (hw != NULL)
1104 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001105 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001106 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001107 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001108 } else {
1109 ALOGW("trying to remove the main display");
1110 }
1111 } else {
1112 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001113 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001114 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001115 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001116 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001117 // recreating the DisplayDevice, so we just remove it
1118 // from the drawing state, so that it get re-added
1119 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001120 sp<DisplayDevice> hw(getDisplayDevice(display));
1121 if (hw != NULL)
1122 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001123 mDisplays.removeItem(display);
1124 mDrawingState.displays.removeItemsAt(i);
1125 dc--; i--;
1126 // at this point we must loop to the next item
1127 continue;
1128 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001129
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001130 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001131 if (disp != NULL) {
1132 if (state.layerStack != draw[i].layerStack) {
1133 disp->setLayerStack(state.layerStack);
1134 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001135 if ((state.orientation != draw[i].orientation)
1136 || (state.viewport != draw[i].viewport)
1137 || (state.frame != draw[i].frame))
1138 {
1139 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001140 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001141 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001142 }
1143 }
1144 }
1145
1146 // find displays that were added
1147 // (ie: in current state but not in drawing state)
1148 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001149 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001150 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001151
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001152 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001153 sp<IGraphicBufferProducer> producer;
1154 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
1155
Jesse Hall02d86562013-03-25 14:43:23 -07001156 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001157 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001158 // Virtual displays without a surface are dormant:
1159 // they have external state (layer stack, projection,
1160 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001161 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001162
Jesse Hall02d86562013-03-25 14:43:23 -07001163 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001164 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1165 *mHwc, hwcDisplayId, state.surface, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001166 state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001167
1168 dispSurface = vds;
1169 if (hwcDisplayId >= 0) {
1170 producer = vds;
1171 } else {
1172 // There won't be any interaction with HWC for this virtual display,
1173 // so the GLES driver can pass buffers directly to the sink.
1174 producer = state.surface;
1175 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001176 }
1177 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001178 ALOGE_IF(state.surface!=NULL,
1179 "adding a supported display, but rendering "
1180 "surface is provided (%p), ignoring it",
1181 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001182 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001183 // for supported (by hwc) displays we provide our
1184 // own rendering surface
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001185 dispSurface = new FramebufferSurface(*mHwc, state.type, bq);
1186 producer = bq;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001187 }
1188
1189 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001190 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001191 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001192 state.type, hwcDisplayId,
1193 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001194 display, dispSurface, producer,
1195 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001196 hw->setLayerStack(state.layerStack);
1197 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001198 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001199 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001200 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001201 if (state.isVirtualDisplay()) {
1202 if (hwcDisplayId >= 0) {
1203 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1204 hw->getWidth(), hw->getHeight(),
1205 hw->getFormat());
1206 }
1207 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001208 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001209 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001210 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001211 }
1212 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001214 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215
Mathias Agopian84300952012-11-21 16:02:13 -08001216 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1217 // The transform hint might have changed for some layers
1218 // (either because a display has changed, or because a layer
1219 // as changed).
1220 //
1221 // Walk through all the layers in currentLayers,
1222 // and update their transform hint.
1223 //
1224 // If a layer is visible only on a single display, then that
1225 // display is used to calculate the hint, otherwise we use the
1226 // default display.
1227 //
1228 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1229 // the hint is set before we acquire a buffer from the surface texture.
1230 //
1231 // NOTE: layer transactions have taken place already, so we use their
1232 // drawing state. However, SurfaceFlinger's own transaction has not
1233 // happened yet, so we must use the current state layer list
1234 // (soon to become the drawing state list).
1235 //
1236 sp<const DisplayDevice> disp;
1237 uint32_t currentlayerStack = 0;
1238 for (size_t i=0; i<count; i++) {
1239 // NOTE: we rely on the fact that layers are sorted by
1240 // layerStack first (so we don't have to traverse the list
1241 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001242 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001243 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001244 if (i==0 || currentlayerStack != layerStack) {
1245 currentlayerStack = layerStack;
1246 // figure out if this layerstack is mirrored
1247 // (more than one display) if so, pick the default display,
1248 // if not, pick the only display it's on.
1249 disp.clear();
1250 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1251 sp<const DisplayDevice> hw(mDisplays[dpy]);
1252 if (hw->getLayerStack() == currentlayerStack) {
1253 if (disp == NULL) {
1254 disp = hw;
1255 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001256 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001257 break;
1258 }
1259 }
1260 }
1261 }
Chet Haase91d25932013-04-11 15:24:55 -07001262 if (disp == NULL) {
1263 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1264 // redraw after transform hint changes. See bug 8508397.
1265
1266 // could be null when this layer is using a layerStack
1267 // that is not visible on any display. Also can occur at
1268 // screen off/on times.
1269 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001270 }
Chet Haase91d25932013-04-11 15:24:55 -07001271 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001272 }
1273 }
1274
1275
Mathias Agopian3559b072012-08-15 13:46:03 -07001276 /*
1277 * Perform our own transaction if needed
1278 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001279
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001280 const LayerVector& layers(mDrawingState.layersSortedByZ);
1281 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001282 // layers have been added
1283 mVisibleRegionsDirty = true;
1284 }
1285
1286 // some layers might have been removed, so
1287 // we need to update the regions they're exposing.
1288 if (mLayersRemoved) {
1289 mLayersRemoved = false;
1290 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001291 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001292 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001293 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001294 if (currentLayers.indexOf(layer) < 0) {
1295 // this layer is not visible anymore
1296 // TODO: we could traverse the tree from front to back and
1297 // compute the actual visible region
1298 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001299 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001300 Region visibleReg = s.transform.transform(
1301 Region(Rect(s.active.w, s.active.h)));
1302 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001303 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305 }
1306
1307 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001308}
1309
1310void SurfaceFlinger::commitTransaction()
1311{
1312 if (!mLayersPendingRemoval.isEmpty()) {
1313 // Notify removed layers now that they can't be drawn from
1314 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1315 mLayersPendingRemoval[i]->onRemoved();
1316 }
1317 mLayersPendingRemoval.clear();
1318 }
1319
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001320 // If this transaction is part of a window animation then the next frame
1321 // we composite should be considered an animation as well.
1322 mAnimCompositionPending = mAnimTransactionPending;
1323
Mathias Agopian4fec8732012-06-29 14:12:52 -07001324 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001325 mTransactionPending = false;
1326 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001327 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328}
1329
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001330void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001331 const LayerVector& currentLayers, uint32_t layerStack,
1332 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001333{
Mathias Agopian841cde52012-03-01 15:44:37 -08001334 ATRACE_CALL();
1335
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001336 Region aboveOpaqueLayers;
1337 Region aboveCoveredLayers;
1338 Region dirty;
1339
Mathias Agopian87baae12012-07-31 12:38:26 -07001340 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001341
1342 size_t i = currentLayers.size();
1343 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001344 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001345
1346 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001347 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001348
Jesse Hall01e29052013-02-19 16:13:35 -08001349 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001350 if (s.layerStack != layerStack)
1351 continue;
1352
Mathias Agopianab028732010-03-16 16:41:46 -07001353 /*
1354 * opaqueRegion: area of a surface that is fully opaque.
1355 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001356 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001357
1358 /*
1359 * visibleRegion: area of a surface that is visible on screen
1360 * and not fully transparent. This is essentially the layer's
1361 * footprint minus the opaque regions above it.
1362 * Areas covered by a translucent surface are considered visible.
1363 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001364 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001365
1366 /*
1367 * coveredRegion: area of a surface that is covered by all
1368 * visible regions above it (which includes the translucent areas).
1369 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001370 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001371
Jesse Halla8026d22012-09-25 13:25:04 -07001372 /*
1373 * transparentRegion: area of a surface that is hinted to be completely
1374 * transparent. This is only used to tell when the layer has no visible
1375 * non-transparent regions and can be removed from the layer list. It
1376 * does not affect the visibleRegion of this layer or any layers
1377 * beneath it. The hint may not be correct if apps don't respect the
1378 * SurfaceView restrictions (which, sadly, some don't).
1379 */
1380 Region transparentRegion;
1381
Mathias Agopianab028732010-03-16 16:41:46 -07001382
1383 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001384 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden44c35ec2014-01-29 17:17:11 -08001385 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001386 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001387 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001388 if (!visibleRegion.isEmpty()) {
1389 // Remove the transparent area from the visible region
1390 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001391 const Transform tr(s.transform);
1392 if (tr.transformed()) {
1393 if (tr.preserveRects()) {
1394 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001395 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001396 } else {
1397 // transformation too complex, can't do the
1398 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001399 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001400 }
1401 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001402 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001403 }
Mathias Agopianab028732010-03-16 16:41:46 -07001404 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405
Mathias Agopianab028732010-03-16 16:41:46 -07001406 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001407 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001408 if (s.alpha==255 && !translucent &&
1409 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1410 // the opaque region is the layer's footprint
1411 opaqueRegion = visibleRegion;
1412 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001413 }
1414 }
1415
Mathias Agopianab028732010-03-16 16:41:46 -07001416 // Clip the covered region to the visible region
1417 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1418
1419 // Update aboveCoveredLayers for next (lower) layer
1420 aboveCoveredLayers.orSelf(visibleRegion);
1421
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422 // subtract the opaque region covered by the layers above us
1423 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424
1425 // compute this layer's dirty region
1426 if (layer->contentDirty) {
1427 // we need to invalidate the whole region
1428 dirty = visibleRegion;
1429 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001430 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431 layer->contentDirty = false;
1432 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001433 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001434 * the exposed region consists of two components:
1435 * 1) what's VISIBLE now and was COVERED before
1436 * 2) what's EXPOSED now less what was EXPOSED before
1437 *
1438 * note that (1) is conservative, we start with the whole
1439 * visible region but only keep what used to be covered by
1440 * something -- which mean it may have been exposed.
1441 *
1442 * (2) handles areas that were not covered by anything but got
1443 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001444 */
Mathias Agopianab028732010-03-16 16:41:46 -07001445 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001446 const Region oldVisibleRegion = layer->visibleRegion;
1447 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001448 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1449 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001450 }
1451 dirty.subtractSelf(aboveOpaqueLayers);
1452
1453 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001454 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455
Mathias Agopianab028732010-03-16 16:41:46 -07001456 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001457 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001458
Jesse Halla8026d22012-09-25 13:25:04 -07001459 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001460 layer->setVisibleRegion(visibleRegion);
1461 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001462 layer->setVisibleNonTransparentRegion(
1463 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001464 }
1465
Mathias Agopian87baae12012-07-31 12:38:26 -07001466 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467}
1468
Mathias Agopian87baae12012-07-31 12:38:26 -07001469void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1470 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001471 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001472 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1473 if (hw->getLayerStack() == layerStack) {
1474 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001475 }
1476 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001477}
1478
1479void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001481 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001482
Mathias Agopian4fec8732012-06-29 14:12:52 -07001483 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001484 const LayerVector& layers(mDrawingState.layersSortedByZ);
1485 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001486 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001487 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001488 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001489 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001490 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001491 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001492
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001493 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001494}
1495
Mathias Agopianad456f92011-01-13 17:53:01 -08001496void SurfaceFlinger::invalidateHwcGeometry()
1497{
1498 mHwWorkListDirty = true;
1499}
1500
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001501
Mathias Agopiancd60f992012-08-16 16:28:27 -07001502void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001503 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001504{
Dan Stozaf3c07d42014-02-04 16:22:36 -08001505 // We only need to actually compose the display if:
1506 // 1) It is being handled by hardware composer, which may need this to
1507 // keep its virtual display state machine in sync, or
1508 // 2) There is work to be done (the dirty region isn't empty)
1509 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1510 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1511 return;
1512 }
1513
Mathias Agopian87baae12012-07-31 12:38:26 -07001514 Region dirtyRegion(inDirtyRegion);
1515
Mathias Agopianb8a55602009-06-26 19:06:36 -07001516 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001517 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518
Mathias Agopian42977342012-08-05 00:40:46 -07001519 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001520 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001521 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1522 // takes a rectangle, we must make sure to update that whole
1523 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001524 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001525 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001526 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001527 // We need to redraw the rectangle that will be updated
1528 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001529 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001530 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001531 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001532 } else {
1533 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001534 dirtyRegion.set(hw->bounds());
1535 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001536 }
1537 }
1538
Mathias Agopianff2ed702013-09-01 21:36:12 -07001539 if (CC_LIKELY(!mDaltonize)) {
1540 doComposeSurfaces(hw, dirtyRegion);
1541 } else {
1542 RenderEngine& engine(getRenderEngine());
1543 engine.beginGroup(mDaltonizer());
1544 doComposeSurfaces(hw, dirtyRegion);
1545 engine.endGroup();
1546 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001547
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001548 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001549 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001550
1551 // swap buffers (presentation)
1552 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001553}
1554
Mathias Agopiancd60f992012-08-16 16:28:27 -07001555void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001556{
Mathias Agopian3f844832013-08-07 21:24:32 -07001557 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001558 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001559 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001560 HWComposer::LayerListIterator cur = hwc.begin(id);
1561 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001562
Jesse Halld05a17f2013-09-30 16:49:30 -07001563 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001564 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001565 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001566 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1567 hw->getDisplayName().string());
1568 return;
1569 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001570
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001571 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001572 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001573 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001574 // when using overlays, we assume a fully transparent framebuffer
1575 // NOTE: we could reduce how much we need to clear, for instance
1576 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001577 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001578 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001579 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001580 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001581 // we start with the whole screen area
1582 const Region bounds(hw->getBounds());
1583
1584 // we remove the scissor part
1585 // we're left with the letterbox region
1586 // (common case is that letterbox ends-up being empty)
1587 const Region letterbox(bounds.subtract(hw->getScissor()));
1588
1589 // compute the area to clear
1590 Region region(hw->undefinedRegion.merge(letterbox));
1591
1592 // but limit it to the dirty region
1593 region.andSelf(dirty);
1594
Mathias Agopianb9494d52012-04-18 02:28:45 -07001595 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001596 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001597 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001598 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001599 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001600 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001601
Mathias Agopian766dc492012-10-30 18:08:06 -07001602 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1603 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001604 // scissor on the main display. It should never be needed
1605 // anyways (though in theory it could since the API allows it).
1606 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001607 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001608 if (scissor != bounds) {
1609 // scissor doesn't match the screen's dimensions, so we
1610 // need to clear everything outside of it and enable
1611 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001612
Mathias Agopianf45c5102012-10-24 16:29:17 -07001613 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001614 const uint32_t height = hw->getHeight();
1615 engine.setScissor(scissor.left, height - scissor.bottom,
1616 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001617 }
1618 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001619 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001620
Mathias Agopian85d751c2012-08-29 16:59:24 -07001621 /*
1622 * and then, render the layers targeted at the framebuffer
1623 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001624
Mathias Agopian13127d82013-03-05 17:47:11 -08001625 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001626 const size_t count = layers.size();
1627 const Transform& tr = hw->getTransform();
1628 if (cur != end) {
1629 // we're using h/w composer
1630 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001631 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001632 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001633 if (!clip.isEmpty()) {
1634 switch (cur->getCompositionType()) {
1635 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001636 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001637 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1638 && i
Andy McFadden44c35ec2014-01-29 17:17:11 -08001639 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001640 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001641 // never clear the very first layer since we're
1642 // guaranteed the FB is already cleared
1643 layer->clearWithOpenGL(hw, clip);
1644 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001645 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001646 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001647 case HWC_FRAMEBUFFER: {
1648 layer->draw(hw, clip);
1649 break;
1650 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001651 case HWC_FRAMEBUFFER_TARGET: {
1652 // this should not happen as the iterator shouldn't
1653 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001654 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001655 break;
1656 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001657 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001658 }
1659 layer->setAcquireFence(hw, *cur);
1660 }
1661 } else {
1662 // we're not using h/w composer
1663 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001664 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001665 const Region clip(dirty.intersect(
1666 tr.transform(layer->visibleRegion)));
1667 if (!clip.isEmpty()) {
1668 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001669 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001670 }
1671 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001672
1673 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001674 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001675}
1676
Mathias Agopian3f844832013-08-07 21:24:32 -07001677void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001678 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001679 RenderEngine& engine(getRenderEngine());
1680 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001681}
1682
Mathias Agopianac9fa422013-02-11 16:40:36 -08001683void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1684 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001685 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001686 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001687{
Mathias Agopian96f08192010-06-02 23:28:45 -07001688 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001689 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001690
Mathias Agopian96f08192010-06-02 23:28:45 -07001691 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001692 Mutex::Autolock _l(mStateLock);
1693 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001694 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001695}
1696
Mathias Agopian3f844832013-08-07 21:24:32 -07001697status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001698 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001699 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001700 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001701 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001702 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001703 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001704 return NO_ERROR;
1705 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001706 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001707}
1708
Mathias Agopian3f844832013-08-07 21:24:32 -07001709uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001710 return android_atomic_release_load(&mTransactionFlags);
1711}
1712
Mathias Agopian3f844832013-08-07 21:24:32 -07001713uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001714 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1715}
1716
Mathias Agopian3f844832013-08-07 21:24:32 -07001717uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001718 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1719 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001720 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001721 }
1722 return old;
1723}
1724
Mathias Agopian8b33f032012-07-24 20:43:54 -07001725void SurfaceFlinger::setTransactionState(
1726 const Vector<ComposerState>& state,
1727 const Vector<DisplayState>& displays,
1728 uint32_t flags)
1729{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001730 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001731 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001732 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001733
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001734 if (flags & eAnimation) {
1735 // For window updates that are part of an animation we must wait for
1736 // previous animation "frames" to be handled.
1737 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001738 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001739 if (CC_UNLIKELY(err != NO_ERROR)) {
1740 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001741 // caller after a few seconds.
1742 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1743 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001744 mAnimTransactionPending = false;
1745 break;
1746 }
1747 }
1748 }
1749
Mathias Agopiane57f2922012-08-09 16:29:12 -07001750 size_t count = displays.size();
1751 for (size_t i=0 ; i<count ; i++) {
1752 const DisplayState& s(displays[i]);
1753 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001754 }
1755
Mathias Agopiane57f2922012-08-09 16:29:12 -07001756 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001757 for (size_t i=0 ; i<count ; i++) {
1758 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001759 // Here we need to check that the interface we're given is indeed
1760 // one of our own. A malicious client could give us a NULL
1761 // IInterface, or one of its own or even one of our own but a
1762 // different type. All these situations would cause us to crash.
1763 //
1764 // NOTE: it would be better to use RTTI as we could directly check
1765 // that we have a Client*. however, RTTI is disabled in Android.
1766 if (s.client != NULL) {
1767 sp<IBinder> binder = s.client->asBinder();
1768 if (binder != NULL) {
1769 String16 desc(binder->getInterfaceDescriptor());
1770 if (desc == ISurfaceComposerClient::descriptor) {
1771 sp<Client> client( static_cast<Client *>(s.client.get()) );
1772 transactionFlags |= setClientStateLocked(client, s.state);
1773 }
1774 }
1775 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001776 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001777
Mathias Agopian386aa982011-11-07 21:58:03 -08001778 if (transactionFlags) {
1779 // this triggers the transaction
1780 setTransactionFlags(transactionFlags);
1781
1782 // if this is a synchronous transaction, wait for it to take effect
1783 // before returning.
1784 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001785 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001786 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001787 if (flags & eAnimation) {
1788 mAnimTransactionPending = true;
1789 }
1790 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001791 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1792 if (CC_UNLIKELY(err != NO_ERROR)) {
1793 // just in case something goes wrong in SF, return to the
1794 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001795 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1796 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001797 break;
1798 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001799 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001800 }
1801}
1802
Mathias Agopiane57f2922012-08-09 16:29:12 -07001803uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1804{
Jesse Hall9a143922012-10-04 16:29:19 -07001805 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1806 if (dpyIdx < 0)
1807 return 0;
1808
Mathias Agopiane57f2922012-08-09 16:29:12 -07001809 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001810 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001811 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001812 const uint32_t what = s.what;
1813 if (what & DisplayState::eSurfaceChanged) {
1814 if (disp.surface->asBinder() != s.surface->asBinder()) {
1815 disp.surface = s.surface;
1816 flags |= eDisplayTransactionNeeded;
1817 }
1818 }
1819 if (what & DisplayState::eLayerStackChanged) {
1820 if (disp.layerStack != s.layerStack) {
1821 disp.layerStack = s.layerStack;
1822 flags |= eDisplayTransactionNeeded;
1823 }
1824 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001825 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001826 if (disp.orientation != s.orientation) {
1827 disp.orientation = s.orientation;
1828 flags |= eDisplayTransactionNeeded;
1829 }
1830 if (disp.frame != s.frame) {
1831 disp.frame = s.frame;
1832 flags |= eDisplayTransactionNeeded;
1833 }
1834 if (disp.viewport != s.viewport) {
1835 disp.viewport = s.viewport;
1836 flags |= eDisplayTransactionNeeded;
1837 }
1838 }
1839 }
1840 return flags;
1841}
1842
1843uint32_t SurfaceFlinger::setClientStateLocked(
1844 const sp<Client>& client,
1845 const layer_state_t& s)
1846{
1847 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001848 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001849 if (layer != 0) {
1850 const uint32_t what = s.what;
1851 if (what & layer_state_t::ePositionChanged) {
1852 if (layer->setPosition(s.x, s.y))
1853 flags |= eTraversalNeeded;
1854 }
1855 if (what & layer_state_t::eLayerChanged) {
1856 // NOTE: index needs to be calculated before we update the state
1857 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1858 if (layer->setLayer(s.z)) {
1859 mCurrentState.layersSortedByZ.removeAt(idx);
1860 mCurrentState.layersSortedByZ.add(layer);
1861 // we need traversal (state changed)
1862 // AND transaction (list changed)
1863 flags |= eTransactionNeeded|eTraversalNeeded;
1864 }
1865 }
1866 if (what & layer_state_t::eSizeChanged) {
1867 if (layer->setSize(s.w, s.h)) {
1868 flags |= eTraversalNeeded;
1869 }
1870 }
1871 if (what & layer_state_t::eAlphaChanged) {
1872 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1873 flags |= eTraversalNeeded;
1874 }
1875 if (what & layer_state_t::eMatrixChanged) {
1876 if (layer->setMatrix(s.matrix))
1877 flags |= eTraversalNeeded;
1878 }
1879 if (what & layer_state_t::eTransparentRegionChanged) {
1880 if (layer->setTransparentRegionHint(s.transparentRegion))
1881 flags |= eTraversalNeeded;
1882 }
Andy McFadden44c35ec2014-01-29 17:17:11 -08001883 if ((what & layer_state_t::eVisibilityChanged) ||
1884 (what & layer_state_t::eOpacityChanged)) {
1885 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07001886 if (layer->setFlags(s.flags, s.mask))
1887 flags |= eTraversalNeeded;
1888 }
1889 if (what & layer_state_t::eCropChanged) {
1890 if (layer->setCrop(s.crop))
1891 flags |= eTraversalNeeded;
1892 }
1893 if (what & layer_state_t::eLayerStackChanged) {
1894 // NOTE: index needs to be calculated before we update the state
1895 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1896 if (layer->setLayerStack(s.layerStack)) {
1897 mCurrentState.layersSortedByZ.removeAt(idx);
1898 mCurrentState.layersSortedByZ.add(layer);
1899 // we need traversal (state changed)
1900 // AND transaction (list changed)
1901 flags |= eTransactionNeeded|eTraversalNeeded;
1902 }
1903 }
1904 }
1905 return flags;
1906}
1907
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001908status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001909 const String8& name,
1910 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001911 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1912 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001913{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001914 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001915 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001916 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001917 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001918 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001919 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001920
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001921 status_t result = NO_ERROR;
1922
1923 sp<Layer> layer;
1924
Mathias Agopian3165cc22012-08-08 19:42:09 -07001925 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1926 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001927 result = createNormalLayer(client,
1928 name, w, h, flags, format,
1929 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001930 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001931 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001932 result = createDimLayer(client,
1933 name, w, h, flags,
1934 handle, gbp, &layer);
1935 break;
1936 default:
1937 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001938 break;
1939 }
1940
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001941 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001942 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001943 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001944 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001945 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001946}
1947
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001948status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1949 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1950 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001951{
1952 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001953 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001954 case PIXEL_FORMAT_TRANSPARENT:
1955 case PIXEL_FORMAT_TRANSLUCENT:
1956 format = PIXEL_FORMAT_RGBA_8888;
1957 break;
1958 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07001959 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001960 break;
1961 }
1962
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001963 *outLayer = new Layer(this, client, name, w, h, flags);
1964 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1965 if (err == NO_ERROR) {
1966 *handle = (*outLayer)->getHandle();
1967 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001968 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001969
1970 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1971 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001972}
1973
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001974status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1975 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1976 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001977{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001978 *outLayer = new LayerDim(this, client, name, w, h, flags);
1979 *handle = (*outLayer)->getHandle();
1980 *gbp = (*outLayer)->getBufferQueue();
1981 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001982}
1983
Mathias Agopianac9fa422013-02-11 16:40:36 -08001984status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001985{
Mathias Agopian67106042013-03-14 19:18:13 -07001986 // called by the window manager when it wants to remove a Layer
1987 status_t err = NO_ERROR;
1988 sp<Layer> l(client->getLayerUser(handle));
1989 if (l != NULL) {
1990 err = removeLayer(l);
1991 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1992 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001993 }
1994 return err;
1995}
1996
Mathias Agopian13127d82013-03-05 17:47:11 -08001997status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001998{
Mathias Agopian67106042013-03-14 19:18:13 -07001999 // called by ~LayerCleaner() when all references to the IBinder (handle)
2000 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002001 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002002 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002003 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002004 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002005 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002006 "error removing layer=%p (%s)", l.get(), strerror(-err));
2007 }
2008 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002009}
2010
Mathias Agopianb60314a2012-04-10 22:09:54 -07002011// ---------------------------------------------------------------------------
2012
Andy McFadden13a082e2012-08-24 10:16:42 -07002013void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002014 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002015 Vector<ComposerState> state;
2016 Vector<DisplayState> displays;
2017 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002018 d.what = DisplayState::eDisplayProjectionChanged |
2019 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002020 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002021 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002022 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002023 d.frame.makeInvalid();
2024 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002025 displays.add(d);
2026 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002027 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07002028
2029 const nsecs_t period =
2030 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2031 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002032}
2033
2034void SurfaceFlinger::initializeDisplays() {
2035 class MessageScreenInitialized : public MessageBase {
2036 SurfaceFlinger* flinger;
2037 public:
2038 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2039 virtual bool handler() {
2040 flinger->onInitializeDisplays();
2041 return true;
2042 }
2043 };
2044 sp<MessageBase> msg = new MessageScreenInitialized(this);
2045 postMessageAsync(msg); // we may be called from main thread, use async message
2046}
2047
2048
Mathias Agopiancde87a32012-09-13 14:09:01 -07002049void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002050 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2051 if (hw->isScreenAcquired()) {
2052 // this is expected, e.g. when power manager wakes up during boot
2053 ALOGD(" screen was previously acquired");
2054 return;
2055 }
2056
Mathias Agopian42977342012-08-05 00:40:46 -07002057 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002058 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002059 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002060 // built-in display, tell the HWC
2061 getHwComposer().acquire(type);
2062
2063 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2064 // FIXME: eventthread only knows about the main display right now
2065 mEventThread->onScreenAcquired();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07002066
Jesse Hall948fe0c2013-10-14 12:56:09 -07002067 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002068 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002069 }
Mathias Agopian20128302012-08-13 18:32:13 -07002070 mVisibleRegionsDirty = true;
2071 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002072}
2073
Mathias Agopiancde87a32012-09-13 14:09:01 -07002074void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002075 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2076 if (!hw->isScreenAcquired()) {
2077 ALOGD(" screen was previously released");
2078 return;
2079 }
2080
2081 hw->releaseScreen();
2082 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002083 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002084 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002085 disableHardwareVsync(true); // also cancels any in-progress resync
2086
Mathias Agopiancde87a32012-09-13 14:09:01 -07002087 // FIXME: eventthread only knows about the main display right now
2088 mEventThread->onScreenReleased();
2089 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002090
2091 // built-in display, tell the HWC
2092 getHwComposer().release(type);
2093 }
2094 mVisibleRegionsDirty = true;
2095 // from this point on, SF will stop drawing on this display
2096}
2097
2098void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2099 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002100 SurfaceFlinger& mFlinger;
2101 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002102 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002103 MessageScreenAcquired(SurfaceFlinger& flinger,
2104 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002105 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002106 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2107 if (hw == NULL) {
2108 ALOGE("Attempt to unblank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002109 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002110 ALOGW("Attempt to unblank virtual display");
2111 } else {
2112 mFlinger.onScreenAcquired(hw);
2113 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002114 return true;
2115 }
2116 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002117 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2118 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002119}
2120
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002121void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002122 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002123 SurfaceFlinger& mFlinger;
2124 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002125 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002126 MessageScreenReleased(SurfaceFlinger& flinger,
2127 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002128 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002129 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2130 if (hw == NULL) {
2131 ALOGE("Attempt to blank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002132 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002133 ALOGW("Attempt to blank virtual display");
2134 } else {
2135 mFlinger.onScreenReleased(hw);
2136 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002137 return true;
2138 }
2139 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002140 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2141 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002142}
2143
2144// ---------------------------------------------------------------------------
2145
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002146status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2147{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002148 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002149
Mathias Agopianbd115332013-04-18 16:41:04 -07002150 IPCThreadState* ipc = IPCThreadState::self();
2151 const int pid = ipc->getCallingPid();
2152 const int uid = ipc->getCallingUid();
2153 if ((uid != AID_SHELL) &&
2154 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002155 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002156 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002157 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002158 // Try to get the main lock, but don't insist if we can't
2159 // (this would indicate SF is stuck, but we want to be able to
2160 // print something in dumpsys).
2161 int retry = 3;
2162 while (mStateLock.tryLock()<0 && --retry>=0) {
2163 usleep(1000000);
2164 }
2165 const bool locked(retry >= 0);
2166 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002167 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002168 "SurfaceFlinger appears to be unresponsive, "
2169 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002170 }
2171
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002172 bool dumpAll = true;
2173 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002174 size_t numArgs = args.size();
2175 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002176 if ((index < numArgs) &&
2177 (args[index] == String16("--list"))) {
2178 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002179 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002180 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002181 }
2182
2183 if ((index < numArgs) &&
2184 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002185 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002186 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002187 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002188 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002189
2190 if ((index < numArgs) &&
2191 (args[index] == String16("--latency-clear"))) {
2192 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002193 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002194 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002195 }
Andy McFadden232f5bc2014-05-08 14:53:26 -07002196
2197 if ((index < numArgs) &&
2198 (args[index] == String16("--dispsync"))) {
2199 index++;
2200 mPrimaryDispSync.dump(result);
2201 dumpAll = false;
2202 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002203 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002204
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002205 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002206 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002207 }
2208
Mathias Agopian9795c422009-08-26 16:36:26 -07002209 if (locked) {
2210 mStateLock.unlock();
2211 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002212 }
2213 write(fd, result.string(), result.size());
2214 return NO_ERROR;
2215}
2216
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002217void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002218 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002219{
2220 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2221 const size_t count = currentLayers.size();
2222 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002223 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002224 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002225 }
2226}
2227
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002228void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002229 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002230{
2231 String8 name;
2232 if (index < args.size()) {
2233 name = String8(args[index]);
2234 index++;
2235 }
2236
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002237 const nsecs_t period =
2238 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002239 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002240
2241 if (name.isEmpty()) {
2242 mAnimFrameTracker.dump(result);
2243 } else {
2244 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2245 const size_t count = currentLayers.size();
2246 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002247 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002248 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002249 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002250 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002251 }
2252 }
2253}
2254
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002255void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002256 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002257{
2258 String8 name;
2259 if (index < args.size()) {
2260 name = String8(args[index]);
2261 index++;
2262 }
2263
2264 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2265 const size_t count = currentLayers.size();
2266 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002267 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002268 if (name.isEmpty() || (name == layer->getName())) {
2269 layer->clearStats();
2270 }
2271 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002272
2273 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002274}
2275
Jamie Gennis6547ff42013-07-16 20:12:42 -07002276// This should only be called from the main thread. Otherwise it would need
2277// the lock and should use mCurrentState rather than mDrawingState.
2278void SurfaceFlinger::logFrameStats() {
2279 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2280 const size_t count = drawingLayers.size();
2281 for (size_t i=0 ; i<count ; i++) {
2282 const sp<Layer>& layer(drawingLayers[i]);
2283 layer->logFrameStats();
2284 }
2285
2286 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2287}
2288
Andy McFadden4803b742012-09-24 19:07:20 -07002289/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2290{
2291 static const char* config =
2292 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002293#ifdef HAS_CONTEXT_PRIORITY
2294 " HAS_CONTEXT_PRIORITY"
2295#endif
2296#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2297 " NEVER_DEFAULT_TO_ASYNC_MODE"
2298#endif
2299#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2300 " TARGET_DISABLE_TRIPLE_BUFFERING"
2301#endif
2302 "]";
2303 result.append(config);
2304}
2305
Mathias Agopian74d211a2013-04-22 16:55:35 +02002306void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2307 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002308{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002309 bool colorize = false;
2310 if (index < args.size()
2311 && (args[index] == String16("--color"))) {
2312 colorize = true;
2313 index++;
2314 }
2315
2316 Colorizer colorizer(colorize);
2317
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002318 // figure out if we're stuck somewhere
2319 const nsecs_t now = systemTime();
2320 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2321 const nsecs_t inTransaction(mDebugInTransaction);
2322 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2323 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2324
2325 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002326 * Dump library configuration.
2327 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002328
2329 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002330 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002331 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002332 appendSfConfigString(result);
2333 appendUiConfigString(result);
2334 appendGuiConfigString(result);
2335 result.append("\n");
2336
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002337 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002338 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002339 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002340 result.append(SyncFeatures::getInstance().toString());
2341 result.append("\n");
2342
Andy McFadden4803b742012-09-24 19:07:20 -07002343 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002344 * Dump the visible layer list
2345 */
2346 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2347 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002348 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002349 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002350 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002351 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002352 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002353 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002354 }
2355
2356 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002357 * Dump Display state
2358 */
2359
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002360 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002361 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002362 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002363 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2364 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002365 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002366 }
2367
2368 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002369 * Dump SurfaceFlinger global state
2370 */
2371
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002372 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002373 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002374 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002375
Mathias Agopian888c8222012-08-04 21:10:38 -07002376 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002377 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002378
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002379 colorizer.bold(result);
2380 result.appendFormat("EGL implementation : %s\n",
2381 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2382 colorizer.reset(result);
2383 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002384 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002385
Mathias Agopian875d8e12013-06-07 15:35:48 -07002386 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002387
Mathias Agopian42977342012-08-05 00:40:46 -07002388 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002389 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002390 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002391 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002392 " last eglSwapBuffers() time: %f us\n"
2393 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002394 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002395 " refresh-rate : %f fps\n"
2396 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002397 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002398 " gpu_to_cpu_unsupported : %d\n"
2399 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002400 mLastSwapBufferTime/1000.0,
2401 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002402 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002403 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2404 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002405 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002406 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002407
Mathias Agopian74d211a2013-04-22 16:55:35 +02002408 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002409 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002410
Mathias Agopian74d211a2013-04-22 16:55:35 +02002411 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002412 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002413
2414 /*
2415 * VSYNC state
2416 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002417 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002418
2419 /*
2420 * Dump HWComposer state
2421 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002422 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002423 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002424 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002425 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002426 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopianff2ed702013-09-01 21:36:12 -07002427 (mDebugDisableHWC || mDebugRegion || mDaltonize) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002428 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002429
2430 /*
2431 * Dump gralloc state
2432 */
2433 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2434 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002435}
2436
Mathias Agopian13127d82013-03-05 17:47:11 -08002437const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002438SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002439 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002440 wp<IBinder> dpy;
2441 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2442 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2443 dpy = mDisplays.keyAt(i);
2444 break;
2445 }
2446 }
2447 if (dpy == NULL) {
2448 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2449 // Just use the primary display so we have something to return
2450 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2451 }
2452 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002453}
2454
Keun young Park63f165f2012-08-31 10:53:36 -07002455bool SurfaceFlinger::startDdmConnection()
2456{
2457 void* libddmconnection_dso =
2458 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2459 if (!libddmconnection_dso) {
2460 return false;
2461 }
2462 void (*DdmConnection_start)(const char* name);
2463 DdmConnection_start =
2464 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2465 if (!DdmConnection_start) {
2466 dlclose(libddmconnection_dso);
2467 return false;
2468 }
2469 (*DdmConnection_start)(getServiceName());
2470 return true;
2471}
2472
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002473status_t SurfaceFlinger::onTransact(
2474 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2475{
2476 switch (code) {
2477 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002478 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002479 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002480 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002481 case BLANK:
2482 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002483 {
2484 // codes that require permission check
2485 IPCThreadState* ipc = IPCThreadState::self();
2486 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002487 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002488 if ((uid != AID_GRAPHICS) &&
2489 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002490 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002491 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2492 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002493 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002494 break;
2495 }
2496 case CAPTURE_SCREEN:
2497 {
2498 // codes that require permission check
2499 IPCThreadState* ipc = IPCThreadState::self();
2500 const int pid = ipc->getCallingPid();
2501 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002502 if ((uid != AID_GRAPHICS) &&
2503 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002504 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002505 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2506 return PERMISSION_DENIED;
2507 }
2508 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002509 }
2510 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002511
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002512 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2513 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002514 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002515 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002516 IPCThreadState* ipc = IPCThreadState::self();
2517 const int pid = ipc->getCallingPid();
2518 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002519 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002520 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002521 return PERMISSION_DENIED;
2522 }
2523 int n;
2524 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002525 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002526 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002527 return NO_ERROR;
2528 case 1002: // SHOW_UPDATES
2529 n = data.readInt32();
2530 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002531 invalidateHwcGeometry();
2532 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002533 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002534 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002535 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002536 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002537 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002538 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002539 setTransactionFlags(
2540 eTransactionNeeded|
2541 eDisplayTransactionNeeded|
2542 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002543 return NO_ERROR;
2544 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002545 case 1006:{ // send empty update
2546 signalRefresh();
2547 return NO_ERROR;
2548 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002549 case 1008: // toggle use of hw composer
2550 n = data.readInt32();
2551 mDebugDisableHWC = n ? 1 : 0;
2552 invalidateHwcGeometry();
2553 repaintEverything();
2554 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002555 case 1009: // toggle use of transform hint
2556 n = data.readInt32();
2557 mDebugDisableTransformHint = n ? 1 : 0;
2558 invalidateHwcGeometry();
2559 repaintEverything();
2560 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002561 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002562 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002563 reply->writeInt32(0);
2564 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002565 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002566 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002567 return NO_ERROR;
2568 case 1013: {
2569 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002570 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2571 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002572 return NO_ERROR;
2573 }
2574 case 1014: {
2575 // daltonize
2576 n = data.readInt32();
2577 switch (n % 10) {
2578 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2579 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2580 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2581 }
2582 if (n >= 10) {
2583 mDaltonizer.setMode(Daltonizer::correction);
2584 } else {
2585 mDaltonizer.setMode(Daltonizer::simulation);
2586 }
2587 mDaltonize = n > 0;
2588 invalidateHwcGeometry();
2589 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002590 }
2591 return NO_ERROR;
2592 }
2593 }
2594 return err;
2595}
2596
Mathias Agopian53331da2011-08-22 21:44:41 -07002597void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002598 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002599 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002600}
2601
Mathias Agopian59119e62010-10-11 12:37:43 -07002602// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002603// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002604// ---------------------------------------------------------------------------
2605
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002606/* The code below is here to handle b/8734824
2607 *
2608 * We create a IGraphicBufferProducer wrapper that forwards all calls
2609 * to the calling binder thread, where they are executed. This allows
2610 * the calling thread to be reused (on the other side) and not
2611 * depend on having "enough" binder threads to handle the requests.
2612 *
2613 */
2614
2615class GraphicProducerWrapper : public BBinder, public MessageHandler {
2616 sp<IGraphicBufferProducer> impl;
2617 sp<Looper> looper;
2618 status_t result;
2619 bool exitPending;
2620 bool exitRequested;
2621 mutable Barrier barrier;
2622 volatile int32_t memoryBarrier;
2623 uint32_t code;
2624 Parcel const* data;
2625 Parcel* reply;
2626
2627 enum {
2628 MSG_API_CALL,
2629 MSG_EXIT
2630 };
2631
2632 /*
2633 * this is called by our "fake" BpGraphicBufferProducer. We package the
2634 * data and reply Parcel and forward them to the calling thread.
2635 */
2636 virtual status_t transact(uint32_t code,
2637 const Parcel& data, Parcel* reply, uint32_t flags) {
2638 this->code = code;
2639 this->data = &data;
2640 this->reply = reply;
2641 android_atomic_acquire_store(0, &memoryBarrier);
2642 if (exitPending) {
2643 // if we've exited, we run the message synchronously right here
2644 handleMessage(Message(MSG_API_CALL));
2645 } else {
2646 barrier.close();
2647 looper->sendMessage(this, Message(MSG_API_CALL));
2648 barrier.wait();
2649 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08002650 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002651 }
2652
2653 /*
2654 * here we run on the binder calling thread. All we've got to do is
2655 * call the real BpGraphicBufferProducer.
2656 */
2657 virtual void handleMessage(const Message& message) {
2658 android_atomic_release_load(&memoryBarrier);
2659 if (message.what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08002660 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002661 barrier.open();
2662 } else if (message.what == MSG_EXIT) {
2663 exitRequested = true;
2664 }
2665 }
2666
2667public:
2668 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2669 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2670 exitPending(false), exitRequested(false) {
2671 }
2672
2673 status_t waitForResponse() {
2674 do {
2675 looper->pollOnce(-1);
2676 } while (!exitRequested);
2677 return result;
2678 }
2679
2680 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002681 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002682 exitPending = true;
2683 looper->sendMessage(this, Message(MSG_EXIT));
2684 }
2685};
2686
2687
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002688status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2689 const sp<IGraphicBufferProducer>& producer,
2690 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002691 uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002692
2693 if (CC_UNLIKELY(display == 0))
2694 return BAD_VALUE;
2695
2696 if (CC_UNLIKELY(producer == 0))
2697 return BAD_VALUE;
2698
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002699 // if we have secure windows on this display, never allow the screen capture
2700 // unless the producer interface is local (i.e.: we can take a screenshot for
2701 // ourselves).
2702 if (!producer->asBinder()->localBinder()) {
2703 Mutex::Autolock _l(mStateLock);
2704 sp<const DisplayDevice> hw(getDisplayDevice(display));
2705 if (hw->getSecureLayerVisible()) {
2706 ALOGW("FB is protected: PERMISSION_DENIED");
2707 return PERMISSION_DENIED;
2708 }
2709 }
2710
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002711 class MessageCaptureScreen : public MessageBase {
2712 SurfaceFlinger* flinger;
2713 sp<IBinder> display;
2714 sp<IGraphicBufferProducer> producer;
2715 uint32_t reqWidth, reqHeight;
2716 uint32_t minLayerZ,maxLayerZ;
2717 status_t result;
2718 public:
2719 MessageCaptureScreen(SurfaceFlinger* flinger,
2720 const sp<IBinder>& display,
2721 const sp<IGraphicBufferProducer>& producer,
2722 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002723 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002724 : flinger(flinger), display(display), producer(producer),
2725 reqWidth(reqWidth), reqHeight(reqHeight),
2726 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2727 result(PERMISSION_DENIED)
2728 {
2729 }
2730 status_t getResult() const {
2731 return result;
2732 }
2733 virtual bool handler() {
2734 Mutex::Autolock _l(flinger->mStateLock);
2735 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002736 result = flinger->captureScreenImplLocked(hw,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002737 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002738 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002739 return true;
2740 }
2741 };
2742
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002743 // make sure to process transactions before screenshots -- a transaction
2744 // might already be pending but scheduled for VSYNC; this guarantees we
2745 // will handle it before the screenshot. When VSYNC finally arrives
2746 // the scheduled transaction will be a no-op. If no transactions are
2747 // scheduled at this time, this will end-up being a no-op as well.
2748 mEventQueue.invalidateTransactionNow();
2749
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002750 // this creates a "fake" BBinder which will serve as a "fake" remote
2751 // binder to receive the marshaled calls and forward them to the
2752 // real remote (a BpGraphicBufferProducer)
2753 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2754
2755 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2756 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002757 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002758 display, IGraphicBufferProducer::asInterface( wrapper ),
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002759 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002760
2761 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002762 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002763 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002764 }
2765 return res;
2766}
2767
Mathias Agopian180f10d2013-04-10 22:55:41 -07002768
2769void SurfaceFlinger::renderScreenImplLocked(
2770 const sp<const DisplayDevice>& hw,
2771 uint32_t reqWidth, uint32_t reqHeight,
2772 uint32_t minLayerZ, uint32_t maxLayerZ,
2773 bool yswap)
2774{
2775 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002776 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002777
2778 // get screen geometry
2779 const uint32_t hw_w = hw->getWidth();
2780 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002781 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2782
2783 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002784 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002785
2786 // set-up our viewport
Mathias Agopian3f844832013-08-07 21:24:32 -07002787 engine.setViewportAndProjection(reqWidth, reqHeight, hw_w, hw_h, yswap);
2788 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002789
2790 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002791 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002792
2793 const LayerVector& layers( mDrawingState.layersSortedByZ );
2794 const size_t count = layers.size();
2795 for (size_t i=0 ; i<count ; ++i) {
2796 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002797 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002798 if (state.layerStack == hw->getLayerStack()) {
2799 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2800 if (layer->isVisible()) {
2801 if (filtering) layer->setFiltering(true);
2802 layer->draw(hw);
2803 if (filtering) layer->setFiltering(false);
2804 }
2805 }
2806 }
2807 }
2808
2809 // compositionComplete is needed for older driver
2810 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002811 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002812}
2813
2814
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002815status_t SurfaceFlinger::captureScreenImplLocked(
2816 const sp<const DisplayDevice>& hw,
2817 const sp<IGraphicBufferProducer>& producer,
2818 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002819 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002820{
2821 ATRACE_CALL();
2822
Mathias Agopian180f10d2013-04-10 22:55:41 -07002823 // get screen geometry
2824 const uint32_t hw_w = hw->getWidth();
2825 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002826
Mathias Agopian180f10d2013-04-10 22:55:41 -07002827 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2828 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2829 reqWidth, reqHeight, hw_w, hw_h);
2830 return BAD_VALUE;
2831 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002832
Mathias Agopian180f10d2013-04-10 22:55:41 -07002833 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2834 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2835
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002836 // create a surface (because we're a producer, and we need to
2837 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07002838 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002839 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002840
2841 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002842 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002843 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2844 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002845
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002846 int err = 0;
2847 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07002848 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002849 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2850 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002851
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002852 if (err == NO_ERROR) {
2853 ANativeWindowBuffer* buffer;
2854 /* TODO: Once we have the sync framework everywhere this can use
2855 * server-side waits on the fence that dequeueBuffer returns.
2856 */
2857 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2858 if (result == NO_ERROR) {
2859 // create an EGLImage from the buffer so we can later
2860 // turn it into a texture
2861 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2862 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2863 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002864 // this binds the given EGLImage as a framebuffer for the
2865 // duration of this scope.
2866 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2867 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002868 // this will in fact render into our dequeued buffer
2869 // via an FBO, which means we didn't have to create
2870 // an EGLSurface and therefore we're not
2871 // dependent on the context's EGLConfig.
2872 renderScreenImplLocked(hw, reqWidth, reqHeight,
2873 minLayerZ, maxLayerZ, true);
Mathias Agopiand5556842013-09-19 17:08:37 -07002874
Andy McFadden2d8d1202013-10-09 16:38:02 -07002875 // Create a sync point and wait on it, so we know the buffer is
2876 // ready before we pass it along. We can't trivially call glFlush(),
2877 // so we use a wait flag instead.
2878 // TODO: pass a sync fd to queueBuffer() and let the consumer wait.
2879 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
2880 if (sync != EGL_NO_SYNC_KHR) {
2881 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
2882 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
2883 EGLint eglErr = eglGetError();
2884 eglDestroySyncKHR(mEGLDisplay, sync);
2885 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
2886 ALOGW("captureScreen: fence wait timed out");
2887 } else {
2888 ALOGW_IF(eglErr != EGL_SUCCESS,
2889 "captureScreen: error waiting on EGL fence: %#x", eglErr);
2890 }
2891 } else {
2892 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
2893 // not fatal
2894 }
2895
Mathias Agopiand5556842013-09-19 17:08:37 -07002896 if (DEBUG_SCREENSHOTS) {
2897 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
2898 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
2899 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
2900 hw, minLayerZ, maxLayerZ);
2901 delete [] pixels;
2902 }
2903
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002904 } else {
2905 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2906 result = INVALID_OPERATION;
2907 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002908 // destroy our image
2909 eglDestroyImageKHR(mEGLDisplay, image);
2910 } else {
2911 result = BAD_VALUE;
2912 }
2913 window->queueBuffer(window, buffer, -1);
2914 }
2915 } else {
2916 result = BAD_VALUE;
2917 }
2918 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2919 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002920
Mathias Agopian74c40c02010-09-29 13:02:36 -07002921 return result;
2922}
2923
Mathias Agopiand5556842013-09-19 17:08:37 -07002924void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
2925 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07002926 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07002927 for (size_t y=0 ; y<h ; y++) {
2928 uint32_t const * p = (uint32_t const *)vaddr + y*s;
2929 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07002930 if (p[x] != 0xFF000000) return;
2931 }
2932 }
2933 ALOGE("*** we just took a black screenshot ***\n"
2934 "requested minz=%d, maxz=%d, layerStack=%d",
2935 minLayerZ, maxLayerZ, hw->getLayerStack());
2936 const LayerVector& layers( mDrawingState.layersSortedByZ );
2937 const size_t count = layers.size();
2938 for (size_t i=0 ; i<count ; ++i) {
2939 const sp<Layer>& layer(layers[i]);
2940 const Layer::State& state(layer->getDrawingState());
2941 const bool visible = (state.layerStack == hw->getLayerStack())
2942 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2943 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08002944 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07002945 visible ? '+' : '-',
2946 i, layer->getName().string(), state.layerStack, state.z,
2947 layer->isVisible(), state.flags, state.alpha);
2948 }
2949 }
2950}
2951
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002952// ---------------------------------------------------------------------------
2953
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002954SurfaceFlinger::LayerVector::LayerVector() {
2955}
2956
2957SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002958 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002959}
2960
2961int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2962 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002963{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002964 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002965 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2966 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002967
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002968 uint32_t ls = l->getCurrentState().layerStack;
2969 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002970 if (ls != rs)
2971 return ls - rs;
2972
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002973 uint32_t lz = l->getCurrentState().z;
2974 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002975 if (lz != rz)
2976 return lz - rz;
2977
2978 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002979}
2980
2981// ---------------------------------------------------------------------------
2982
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002983SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2984 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002985}
2986
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002987SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002988 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002989 viewport.makeInvalid();
2990 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002991}
2992
2993// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002994
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002995}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002996
2997
2998#if defined(__gl_h_)
2999#error "don't include gl/gl.h in this file"
3000#endif
3001
3002#if defined(__gl2_h_)
3003#error "don't include gl2/gl2.h in this file"
3004#endif