blob: ec643906bffadab69bc4190b4b74c3bbdc3a6964 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070024
25#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <cutils/log.h>
28#include <cutils/properties.h>
29
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070032#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070033#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034
Mathias Agopianc666cae2012-07-25 18:56:13 -070035#include <ui/DisplayInfo.h>
36
Mathias Agopian921e6ac2012-07-23 23:11:29 -070037#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070038#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070039#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070040#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080041#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080042#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070043
44#include <ui/GraphicBufferAllocator.h>
45#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070046#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080047
Mathias Agopiancde87a32012-09-13 14:09:01 -070048#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <utils/String8.h>
50#include <utils/String16.h>
51#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080052#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian921e6ac2012-07-23 23:11:29 -070054#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070055#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopian3e25fd82013-04-22 17:52:16 +020057#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020059#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080060#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070061#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070062#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070063#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080064#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
Mathias Agopiana4912602012-07-12 14:25:33 -070069#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070070#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070071#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072
Mathias Agopianff2ed702013-09-01 21:36:12 -070073#include "Effects/Daltonizer.h"
74
Mathias Agopian875d8e12013-06-07 15:35:48 -070075#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070076#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070077
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078#define DISPLAY_COUNT 1
79
Mathias Agopianfee2b462013-07-03 12:34:01 -070080/*
81 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
82 * black pixels.
83 */
84#define DEBUG_SCREENSHOTS false
85
Mathias Agopianca088332013-03-28 17:44:13 -070086EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
87
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070089
90// This works around the lack of support for the sync framework on some
91// devices.
92#ifdef RUNNING_WITHOUT_SYNC_FRAMEWORK
93static const bool runningWithoutSyncFramework = true;
94#else
95static const bool runningWithoutSyncFramework = false;
96#endif
97
98// This is the phase offset in nanoseconds of the software vsync event
99// relative to the vsync event reported by HWComposer. The software vsync
100// event is when SurfaceFlinger and Choreographer-based applications run each
101// frame.
102//
103// This phase offset allows adjustment of the minimum latency from application
104// wake-up (by Choregographer) time to the time at which the resulting window
105// image is displayed. This value may be either positive (after the HW vsync)
106// or negative (before the HW vsync). Setting it to 0 will result in a
107// minimum latency of two vsync periods because the app and SurfaceFlinger
108// will run just after the HW vsync. Setting it to a positive number will
109// result in the minimum latency being:
110//
111// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
112//
113// Note that reducing this latency makes it more likely for the applications
114// to not have their window content image ready in time. When this happens
115// the latency will end up being an additional vsync period, and animations
116// will hiccup. Therefore, this latency should be tuned somewhat
117// conservatively (or at least with awareness of the trade-off being made).
118static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
119
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700120// This is the phase offset at which SurfaceFlinger's composition runs.
121static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
122
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123// ---------------------------------------------------------------------------
124
Mathias Agopian99b49842011-06-27 16:05:52 -0700125const String16 sHardwareTest("android.permission.HARDWARE_TEST");
126const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
127const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
128const String16 sDump("android.permission.DUMP");
129
130// ---------------------------------------------------------------------------
131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700133 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700135 mTransactionPending(false),
136 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700137 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700138 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700139 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700142 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800143 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700145 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700146 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700147 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700148 mDebugInSwapBuffers(0),
149 mLastSwapBufferTime(0),
150 mDebugInTransaction(0),
151 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700152 mBootFinished(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700153 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700154 mHWVsyncAvailable(false),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700155 mDaltonize(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156{
Steve Blocka19954a2012-01-04 20:05:49 +0000157 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158
159 // debugging stuff...
160 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700161
Mathias Agopianb4b17302013-03-20 18:36:41 -0700162 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700163 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700164
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 property_get("debug.sf.showupdates", value, "0");
166 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700167
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700168 property_get("debug.sf.ddms", value, "0");
169 mDebugDDMS = atoi(value);
170 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700171 if (!startDdmConnection()) {
172 // start failed, and DDMS debugging not enabled
173 mDebugDDMS = 0;
174 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700175 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700176 ALOGI_IF(mDebugRegion, "showupdates enabled");
177 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178}
179
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800180void SurfaceFlinger::onFirstRef()
181{
182 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800183}
184
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185SurfaceFlinger::~SurfaceFlinger()
186{
Mathias Agopiana4912602012-07-12 14:25:33 -0700187 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
188 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
189 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190}
191
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800192void SurfaceFlinger::binderDied(const wp<IBinder>& who)
193{
194 // the window manager died on us. prepare its eulogy.
195
Andy McFadden13a082e2012-08-24 10:16:42 -0700196 // restore initial conditions (default device unblank, etc)
197 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800198
199 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700200 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800201}
202
Mathias Agopian7e27f052010-05-28 14:22:23 -0700203sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204{
Mathias Agopian96f08192010-06-02 23:28:45 -0700205 sp<ISurfaceComposerClient> bclient;
206 sp<Client> client(new Client(this));
207 status_t err = client->initCheck();
208 if (err == NO_ERROR) {
209 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 return bclient;
212}
213
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700214sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
215 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700216{
217 class DisplayToken : public BBinder {
218 sp<SurfaceFlinger> flinger;
219 virtual ~DisplayToken() {
220 // no more references, this display must be terminated
221 Mutex::Autolock _l(flinger->mStateLock);
222 flinger->mCurrentState.displays.removeItem(this);
223 flinger->setTransactionFlags(eDisplayTransactionNeeded);
224 }
225 public:
226 DisplayToken(const sp<SurfaceFlinger>& flinger)
227 : flinger(flinger) {
228 }
229 };
230
231 sp<BBinder> token = new DisplayToken(this);
232
233 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700234 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700235 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700236 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700237 mCurrentState.displays.add(token, info);
238
239 return token;
240}
241
Jesse Hall6c913be2013-08-08 12:15:49 -0700242void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
243 Mutex::Autolock _l(mStateLock);
244
245 ssize_t idx = mCurrentState.displays.indexOfKey(display);
246 if (idx < 0) {
247 ALOGW("destroyDisplay: invalid display token");
248 return;
249 }
250
251 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
252 if (!info.isVirtualDisplay()) {
253 ALOGE("destroyDisplay called for non-virtual display");
254 return;
255 }
256
257 mCurrentState.displays.removeItemsAt(idx);
258 setTransactionFlags(eDisplayTransactionNeeded);
259}
260
Jesse Hall692c7232012-11-08 15:41:56 -0800261void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
262 ALOGW_IF(mBuiltinDisplays[type],
263 "Overwriting display token for display type %d", type);
264 mBuiltinDisplays[type] = new BBinder();
265 DisplayDeviceState info(type);
266 // All non-virtual displays are currently considered secure.
267 info.isSecure = true;
268 mCurrentState.displays.add(mBuiltinDisplays[type], info);
269}
270
Mathias Agopiane57f2922012-08-09 16:29:12 -0700271sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700272 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700273 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
274 return NULL;
275 }
Jesse Hall692c7232012-11-08 15:41:56 -0800276 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700277}
278
Jamie Gennis9a78c902011-01-12 18:30:40 -0800279sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
280{
281 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
282 return gba;
283}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700284
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285void SurfaceFlinger::bootFinished()
286{
287 const nsecs_t now = systemTime();
288 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000289 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700290 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700291
292 // wait patiently for the window manager death
293 const String16 name("window");
294 sp<IBinder> window(defaultServiceManager()->getService(name));
295 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700296 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700297 }
298
299 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700300 // formerly we would just kill the process, but we now ask it to exit so it
301 // can choose where to stop the animation.
302 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303}
304
Mathias Agopian3f844832013-08-07 21:24:32 -0700305void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700306 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700307 RenderEngine& engine;
308 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700309 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700310 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
311 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700312 }
313 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700314 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700315 return true;
316 }
317 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700318 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700319}
320
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700321class DispSyncSource : public VSyncSource, private DispSync::Callback {
322public:
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700323 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync) :
324 mValue(0),
325 mPhaseOffset(phaseOffset),
326 mTraceVsync(traceVsync),
327 mDispSync(dispSync) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700328
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700329 virtual ~DispSyncSource() {}
330
331 virtual void setVSyncEnabled(bool enable) {
332 // Do NOT lock the mutex here so as to avoid any mutex ordering issues
333 // with locking it in the onDispSyncEvent callback.
334 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700335 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700336 static_cast<DispSync::Callback*>(this));
337 if (err != NO_ERROR) {
338 ALOGE("error registering vsync callback: %s (%d)",
339 strerror(-err), err);
340 }
341 ATRACE_INT("VsyncOn", 1);
342 } else {
343 status_t err = mDispSync->removeEventListener(
344 static_cast<DispSync::Callback*>(this));
345 if (err != NO_ERROR) {
346 ALOGE("error unregistering vsync callback: %s (%d)",
347 strerror(-err), err);
348 }
349 ATRACE_INT("VsyncOn", 0);
350 }
351 }
352
353 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
354 Mutex::Autolock lock(mMutex);
355 mCallback = callback;
356 }
357
358private:
359 virtual void onDispSyncEvent(nsecs_t when) {
360 sp<VSyncSource::Callback> callback;
361 {
362 Mutex::Autolock lock(mMutex);
363 callback = mCallback;
364
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700365 if (mTraceVsync) {
366 mValue = (mValue + 1) % 2;
367 ATRACE_INT("VSYNC", mValue);
368 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700369 }
370
371 if (callback != NULL) {
372 callback->onVSyncEvent(when);
373 }
374 }
375
376 int mValue;
377
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700378 const nsecs_t mPhaseOffset;
379 const bool mTraceVsync;
380
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700381 DispSync* mDispSync;
382 sp<VSyncSource::Callback> mCallback;
383 Mutex mMutex;
384};
385
386void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700387 ALOGI( "SurfaceFlinger's main thread ready to run. "
388 "Initializing graphics H/W...");
389
Jesse Hallb65f32e2013-09-27 22:10:47 -0700390 status_t err;
Jesse Hall692c7232012-11-08 15:41:56 -0800391 Mutex::Autolock _l(mStateLock);
392
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700393 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700394 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
395 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700396
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700397 // Initialize the H/W composer object. There may or may not be an
398 // actual hardware composer underneath.
399 mHwc = new HWComposer(this,
400 *static_cast<HWComposer::EventHandler *>(this));
401
Mathias Agopian875d8e12013-06-07 15:35:48 -0700402 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800403 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700404
405 // retrieve the EGL context that was selected/created
406 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700407
Mathias Agopianda27af92012-09-13 18:17:13 -0700408 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
409 "couldn't create EGLContext");
410
Mathias Agopiancde87a32012-09-13 14:09:01 -0700411 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700412 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700413 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700414 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700415 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700416 // All non-virtual displays are currently considered secure.
417 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800418 createBuiltinDisplayLocked(type);
419 wp<IBinder> token = mBuiltinDisplays[i];
420
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700421 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
422 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i, bq);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700423 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700424 type, allocateHwcDisplayId(type), isSecure, token,
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700425 fbs, bq,
Jesse Hall05f8c702013-12-23 20:44:38 -0800426 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700427 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700428 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700429 // for displays other than the main display, so we always
430 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700431 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700432 hw->acquireScreen();
433 }
434 mDisplays.add(token, hw);
435 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700436 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700437
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700438 // make the GLContext current so that we can create textures when creating Layers
439 // (which may happens before we render something)
440 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
441
Mathias Agopian028508c2012-07-25 21:12:12 -0700442 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700443 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
444 vsyncPhaseOffsetNs, true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700445 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700446 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
447 sfVsyncPhaseOffsetNs, false);
448 mSFEventThread = new EventThread(sfVsyncSrc);
449 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700450
Jamie Gennisd1700752013-10-14 12:22:52 -0700451 mEventControlThread = new EventControlThread(this);
452 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
453
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700454 // set a fake vsync period if there is no HWComposer
455 if (mHwc->initCheck() != NO_ERROR) {
456 mPrimaryDispSync.setPeriod(16666667);
457 }
458
Mathias Agopian92a979a2012-08-02 18:32:23 -0700459 // initialize our drawing state
460 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700461
Andy McFadden13a082e2012-08-24 10:16:42 -0700462 // set initial conditions (e.g. unblank default device)
463 initializeDisplays();
464
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700465 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700466 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800467}
468
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700469int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700470 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700471 type : mHwc->allocateDisplayId();
472}
473
Mathias Agopiana67e4182012-06-19 17:26:12 -0700474void SurfaceFlinger::startBootAnim() {
475 // start boot animation
476 property_set("service.bootanim.exit", "0");
477 property_set("ctl.start", "bootanim");
478}
479
Mathias Agopian875d8e12013-06-07 15:35:48 -0700480size_t SurfaceFlinger::getMaxTextureSize() const {
481 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700482}
483
Mathias Agopian875d8e12013-06-07 15:35:48 -0700484size_t SurfaceFlinger::getMaxViewportDims() const {
485 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700486}
487
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800488// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800489
Jamie Gennis582270d2011-08-17 18:19:00 -0700490bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800491 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800492 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800493 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700494 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800495}
496
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700497status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800498 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700499 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800500 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700501 type = i;
502 break;
503 }
504 }
505
506 if (type < 0) {
507 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700508 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700509
510 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700511 float xdpi = hwc.getDpiX(type);
512 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700513
514 // TODO: Not sure if display density should handled by SF any longer
515 class Density {
516 static int getDensityFromProperty(char const* propName) {
517 char property[PROPERTY_VALUE_MAX];
518 int density = 0;
519 if (property_get(propName, property, NULL) > 0) {
520 density = atoi(property);
521 }
522 return density;
523 }
524 public:
525 static int getEmuDensity() {
526 return getDensityFromProperty("qemu.sf.lcd_density"); }
527 static int getBuildDensity() {
528 return getDensityFromProperty("ro.sf.lcd_density"); }
529 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700530
531 if (type == DisplayDevice::DISPLAY_PRIMARY) {
532 // The density of the device is provided by a build property
533 float density = Density::getBuildDensity() / 160.0f;
534 if (density == 0) {
535 // the build doesn't provide a density -- this is wrong!
536 // use xdpi instead
537 ALOGE("ro.sf.lcd_density must be defined as a build property");
538 density = xdpi / 160.0f;
539 }
540 if (Density::getEmuDensity()) {
541 // if "qemu.sf.lcd_density" is specified, it overrides everything
542 xdpi = ydpi = density = Density::getEmuDensity();
543 density /= 160.0f;
544 }
545 info->density = density;
546
547 // TODO: this needs to go away (currently needed only by webkit)
548 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
549 info->orientation = hw->getOrientation();
Mathias Agopian1604f772012-09-18 21:54:42 -0700550 } else {
551 // TODO: where should this value come from?
552 static const int TV_DENSITY = 213;
553 info->density = TV_DENSITY / 160.0f;
554 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700555 }
556
Mathias Agopian1604f772012-09-18 21:54:42 -0700557 info->w = hwc.getWidth(type);
558 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700559 info->xdpi = xdpi;
560 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700561 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700562
563 // All non-virtual displays are currently considered secure.
564 info->secure = true;
565
Mathias Agopian888c8222012-08-04 21:10:38 -0700566 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700567}
568
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800569// ----------------------------------------------------------------------------
570
571sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800572 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700573}
574
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800575// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800576
577void SurfaceFlinger::waitForEvent() {
578 mEventQueue.waitMessage();
579}
580
581void SurfaceFlinger::signalTransaction() {
582 mEventQueue.invalidate();
583}
584
585void SurfaceFlinger::signalLayerUpdate() {
586 mEventQueue.invalidate();
587}
588
589void SurfaceFlinger::signalRefresh() {
590 mEventQueue.refresh();
591}
592
593status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
594 nsecs_t reltime, uint32_t flags) {
595 return mEventQueue.postMessage(msg, reltime);
596}
597
598status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
599 nsecs_t reltime, uint32_t flags) {
600 status_t res = mEventQueue.postMessage(msg, reltime);
601 if (res == NO_ERROR) {
602 msg->wait();
603 }
604 return res;
605}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800606
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700607void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700608 do {
609 waitForEvent();
610 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800611}
612
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700613void SurfaceFlinger::enableHardwareVsync() {
614 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700615 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700616 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700617 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
618 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700619 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700620 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700621}
622
Jesse Hall948fe0c2013-10-14 12:56:09 -0700623void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700624 Mutex::Autolock _l(mHWVsyncLock);
625
Jesse Hall948fe0c2013-10-14 12:56:09 -0700626 if (makeAvailable) {
627 mHWVsyncAvailable = true;
628 } else if (!mHWVsyncAvailable) {
629 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
630 return;
631 }
632
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700633 const nsecs_t period =
634 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
635
636 mPrimaryDispSync.reset();
637 mPrimaryDispSync.setPeriod(period);
638
639 if (!mPrimaryHWVsyncEnabled) {
640 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700641 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
642 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700643 mPrimaryHWVsyncEnabled = true;
644 }
645}
646
Jesse Hall948fe0c2013-10-14 12:56:09 -0700647void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700648 Mutex::Autolock _l(mHWVsyncLock);
649 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700650 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
651 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700652 mPrimaryDispSync.endResync();
653 mPrimaryHWVsyncEnabled = false;
654 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700655 if (makeUnavailable) {
656 mHWVsyncAvailable = false;
657 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700658}
659
660void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700661 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700662
Jamie Gennisd1700752013-10-14 12:22:52 -0700663 { // Scope for the lock
664 Mutex::Autolock _l(mHWVsyncLock);
665 if (type == 0 && mPrimaryHWVsyncEnabled) {
666 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700667 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700668 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700669
670 if (needsHwVsync) {
671 enableHardwareVsync();
672 } else {
673 disableHardwareVsync(false);
674 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700675}
676
677void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
678 if (mEventThread == NULL) {
679 // This is a temporary workaround for b/7145521. A non-null pointer
680 // does not mean EventThread has finished initializing, so this
681 // is not a correct fix.
682 ALOGW("WARNING: EventThread not started, ignoring hotplug");
683 return;
684 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700685
Jesse Hall9e663de2013-08-16 14:28:37 -0700686 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700687 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800688 if (connected) {
689 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700690 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800691 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
692 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700693 }
694 setTransactionFlags(eDisplayTransactionNeeded);
695
Andy McFadden9e9689c2012-10-10 18:17:51 -0700696 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700697 }
Mathias Agopian86303202012-07-24 22:46:10 -0700698}
699
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700700void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700701 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700702 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700703}
704
Mathias Agopian4fec8732012-06-29 14:12:52 -0700705void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800706 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800707 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700708 case MessageQueue::TRANSACTION:
709 handleMessageTransaction();
710 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700711 case MessageQueue::INVALIDATE:
712 handleMessageTransaction();
713 handleMessageInvalidate();
714 signalRefresh();
715 break;
716 case MessageQueue::REFRESH:
717 handleMessageRefresh();
718 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800719 }
720}
721
Mathias Agopian4fec8732012-06-29 14:12:52 -0700722void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700723 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700724 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700725 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700726 }
727}
728
729void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700730 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700731 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700732}
733
734void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700735 ATRACE_CALL();
736 preComposition();
737 rebuildLayerStacks();
738 setUpHWComposer();
739 doDebugFlashRegions();
740 doComposition();
741 postComposition();
742}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700743
Mathias Agopiancd60f992012-08-16 16:28:27 -0700744void SurfaceFlinger::doDebugFlashRegions()
745{
746 // is debugging enabled
747 if (CC_LIKELY(!mDebugRegion))
748 return;
749
750 const bool repaintEverything = mRepaintEverything;
751 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
752 const sp<DisplayDevice>& hw(mDisplays[dpy]);
753 if (hw->canDraw()) {
754 // transform the dirty region into this screen's coordinate space
755 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
756 if (!dirtyRegion.isEmpty()) {
757 // redraw the whole screen
758 doComposeSurfaces(hw, Region(hw->bounds()));
759
760 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700761 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700762 RenderEngine& engine(getRenderEngine());
763 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
764
Mathias Agopiancd60f992012-08-16 16:28:27 -0700765 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700766 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700767 }
768 }
769 }
770
771 postFramebuffer();
772
773 if (mDebugRegion > 1) {
774 usleep(mDebugRegion * 1000);
775 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700776
777 HWComposer& hwc(getHwComposer());
778 if (hwc.initCheck() == NO_ERROR) {
779 status_t err = hwc.prepare();
780 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
781 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700782}
783
784void SurfaceFlinger::preComposition()
785{
786 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700787 const LayerVector& layers(mDrawingState.layersSortedByZ);
788 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700789 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700790 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700791 needExtraInvalidate = true;
792 }
793 }
794 if (needExtraInvalidate) {
795 signalLayerUpdate();
796 }
797}
798
799void SurfaceFlinger::postComposition()
800{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700801 const LayerVector& layers(mDrawingState.layersSortedByZ);
802 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700803 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700804 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700805 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800806
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700807 const HWComposer& hwc = getHwComposer();
808 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
809
810 if (presentFence->isValid()) {
811 if (mPrimaryDispSync.addPresentFence(presentFence)) {
812 enableHardwareVsync();
813 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700814 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700815 }
816 }
817
818 if (runningWithoutSyncFramework) {
819 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
820 if (hw->isScreenAcquired()) {
821 enableHardwareVsync();
822 }
823 }
824
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800825 if (mAnimCompositionPending) {
826 mAnimCompositionPending = false;
827
Jesse Halla9a1b002013-02-27 16:39:25 -0800828 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800829 mAnimFrameTracker.setActualPresentFence(presentFence);
830 } else {
831 // The HWC doesn't support present fences, so use the refresh
832 // timestamp instead.
833 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
834 mAnimFrameTracker.setActualPresentTime(presentTime);
835 }
836 mAnimFrameTracker.advanceFrame();
837 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700838}
839
840void SurfaceFlinger::rebuildLayerStacks() {
841 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700842 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700843 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700844 mVisibleRegionsDirty = false;
845 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700846
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700847 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700848 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700849 Region opaqueRegion;
850 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800851 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700852 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700853 const Transform& tr(hw->getTransform());
854 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700855 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700856 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700857 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700858
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700859 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700860 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700861 const sp<Layer>& layer(layers[i]);
862 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700863 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700864 Region drawRegion(tr.transform(
865 layer->visibleNonTransparentRegion));
866 drawRegion.andSelf(bounds);
867 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700868 layersSortedByZ.add(layer);
869 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700870 }
871 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700872 }
Mathias Agopian42977342012-08-05 00:40:46 -0700873 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700874 hw->undefinedRegion.set(bounds);
875 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
876 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700877 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700878 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700879}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700880
Mathias Agopiancd60f992012-08-16 16:28:27 -0700881void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700882 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
883 mDisplays[dpy]->beginFrame();
884 }
885
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700886 HWComposer& hwc(getHwComposer());
887 if (hwc.initCheck() == NO_ERROR) {
888 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700889 if (CC_UNLIKELY(mHwWorkListDirty)) {
890 mHwWorkListDirty = false;
891 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
892 sp<const DisplayDevice> hw(mDisplays[dpy]);
893 const int32_t id = hw->getHwcDisplayId();
894 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800895 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700896 hw->getVisibleLayersSortedByZ());
897 const size_t count = currentLayers.size();
898 if (hwc.createWorkList(id, count) == NO_ERROR) {
899 HWComposer::LayerListIterator cur = hwc.begin(id);
900 const HWComposer::LayerListIterator end = hwc.end(id);
901 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800902 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700903 layer->setGeometry(hw, *cur);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700904 if (mDebugDisableHWC || mDebugRegion || mDaltonize) {
Jamie Gennisa4310c82012-09-25 20:26:00 -0700905 cur->setSkip(true);
906 }
907 }
908 }
909 }
910 }
911 }
912
913 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700914 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700915 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700916 const int32_t id = hw->getHwcDisplayId();
917 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800918 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700919 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700920 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700921 HWComposer::LayerListIterator cur = hwc.begin(id);
922 const HWComposer::LayerListIterator end = hwc.end(id);
923 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
924 /*
925 * update the per-frame h/w composer data for each layer
926 * and build the transparent region of the FB
927 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800928 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700929 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700930 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700931 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700932 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700933
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700934 status_t err = hwc.prepare();
935 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700936
937 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
938 sp<const DisplayDevice> hw(mDisplays[dpy]);
939 hw->prepareFrame(hwc);
940 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700941 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700942}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700943
Mathias Agopiancd60f992012-08-16 16:28:27 -0700944void SurfaceFlinger::doComposition() {
945 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700946 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700947 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700948 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700949 if (hw->canDraw()) {
950 // transform the dirty region into this screen's coordinate space
951 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800952
953 // repaint the framebuffer (if needed)
954 doDisplayComposition(hw, dirtyRegion);
955
Mathias Agopiancd60f992012-08-16 16:28:27 -0700956 hw->dirtyRegion.clear();
957 hw->flip(hw->swapRegion);
958 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700959 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700960 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700961 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700962 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700963 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700964}
965
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800966void SurfaceFlinger::postFramebuffer()
967{
Mathias Agopian841cde52012-03-01 15:44:37 -0800968 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800969
Mathias Agopiana44b0412011-10-16 18:46:35 -0700970 const nsecs_t now = systemTime();
971 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700972
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700973 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700974 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700975 if (!hwc.supportsFramebufferTarget()) {
976 // EGL spec says:
977 // "surface must be bound to the calling thread's current context,
978 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700979 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700980 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700981 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700982 }
983
Mathias Agopian6da15f42013-09-25 20:40:07 -0700984 // make the default display current because the VirtualDisplayDevice code cannot
985 // deal with dequeueBuffer() being called outside of the composition loop; however
986 // the code below can call glFlush() which is allowed (and does in some case) call
987 // dequeueBuffer().
988 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
989
Mathias Agopian92a979a2012-08-02 18:32:23 -0700990 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700991 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800992 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700993 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700994 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700995 int32_t id = hw->getHwcDisplayId();
996 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700997 HWComposer::LayerListIterator cur = hwc.begin(id);
998 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700999 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001000 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001001 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001002 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001003 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001004 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001005 }
Jesse Hallef194142012-06-14 14:45:17 -07001006 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001007 }
1008
Mathias Agopiana44b0412011-10-16 18:46:35 -07001009 mLastSwapBufferTime = systemTime() - now;
1010 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001011
1012 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1013 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1014 logFrameStats();
1015 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016}
1017
Mathias Agopian87baae12012-07-31 12:38:26 -07001018void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019{
Mathias Agopian841cde52012-03-01 15:44:37 -08001020 ATRACE_CALL();
1021
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001022 // here we keep a copy of the drawing state (that is the state that's
1023 // going to be overwritten by handleTransactionLocked()) outside of
1024 // mStateLock so that the side-effects of the State assignment
1025 // don't happen with mStateLock held (which can cause deadlocks).
1026 State drawingState(mDrawingState);
1027
Mathias Agopianca4d3602011-05-19 15:38:14 -07001028 Mutex::Autolock _l(mStateLock);
1029 const nsecs_t now = systemTime();
1030 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031
Mathias Agopianca4d3602011-05-19 15:38:14 -07001032 // Here we're guaranteed that some transaction flags are set
1033 // so we can call handleTransactionLocked() unconditionally.
1034 // We call getTransactionFlags(), which will also clear the flags,
1035 // with mStateLock held to guarantee that mCurrentState won't change
1036 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001037
Mathias Agopiane57f2922012-08-09 16:29:12 -07001038 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001039 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001040
Mathias Agopianca4d3602011-05-19 15:38:14 -07001041 mLastTransactionTime = systemTime() - now;
1042 mDebugInTransaction = 0;
1043 invalidateHwcGeometry();
1044 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001045}
1046
Mathias Agopian87baae12012-07-31 12:38:26 -07001047void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001048{
1049 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050 const size_t count = currentLayers.size();
1051
1052 /*
1053 * Traversal of the children
1054 * (perform the transaction for each of them if needed)
1055 */
1056
Mathias Agopian3559b072012-08-15 13:46:03 -07001057 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001059 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1061 if (!trFlags) continue;
1062
1063 const uint32_t flags = layer->doTransaction(0);
1064 if (flags & Layer::eVisibleRegion)
1065 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066 }
1067 }
1068
1069 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001070 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071 */
1072
Mathias Agopiane57f2922012-08-09 16:29:12 -07001073 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001074 // here we take advantage of Vector's copy-on-write semantics to
1075 // improve performance by skipping the transaction entirely when
1076 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001077 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1078 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001079 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001081 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001082 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001083
1084 // find the displays that were removed
1085 // (ie: in drawing state but not in current state)
1086 // also handle displays that changed
1087 // (ie: displays that are in both lists)
1088 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001089 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1090 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001091 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001092 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001093 // Call makeCurrent() on the primary display so we can
1094 // be sure that nothing associated with this display
1095 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001096 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001097 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001098 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1099 if (hw != NULL)
1100 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001101 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001102 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001103 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001104 } else {
1105 ALOGW("trying to remove the main display");
1106 }
1107 } else {
1108 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001109 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001110 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001111 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001112 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001113 // recreating the DisplayDevice, so we just remove it
1114 // from the drawing state, so that it get re-added
1115 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001116 sp<DisplayDevice> hw(getDisplayDevice(display));
1117 if (hw != NULL)
1118 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001119 mDisplays.removeItem(display);
1120 mDrawingState.displays.removeItemsAt(i);
1121 dc--; i--;
1122 // at this point we must loop to the next item
1123 continue;
1124 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001125
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001126 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001127 if (disp != NULL) {
1128 if (state.layerStack != draw[i].layerStack) {
1129 disp->setLayerStack(state.layerStack);
1130 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001131 if ((state.orientation != draw[i].orientation)
1132 || (state.viewport != draw[i].viewport)
1133 || (state.frame != draw[i].frame))
1134 {
1135 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001136 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001137 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001138 }
1139 }
1140 }
1141
1142 // find displays that were added
1143 // (ie: in current state but not in drawing state)
1144 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001145 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001146 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001147
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001148 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001149 sp<IGraphicBufferProducer> producer;
1150 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
1151
Jesse Hall02d86562013-03-25 14:43:23 -07001152 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001153 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001154 // Virtual displays without a surface are dormant:
1155 // they have external state (layer stack, projection,
1156 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001157 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001158
Jesse Hall02d86562013-03-25 14:43:23 -07001159 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001160 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1161 *mHwc, hwcDisplayId, state.surface, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001162 state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001163
1164 dispSurface = vds;
1165 if (hwcDisplayId >= 0) {
1166 producer = vds;
1167 } else {
1168 // There won't be any interaction with HWC for this virtual display,
1169 // so the GLES driver can pass buffers directly to the sink.
1170 producer = state.surface;
1171 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001172 }
1173 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001174 ALOGE_IF(state.surface!=NULL,
1175 "adding a supported display, but rendering "
1176 "surface is provided (%p), ignoring it",
1177 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001178 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001179 // for supported (by hwc) displays we provide our
1180 // own rendering surface
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001181 dispSurface = new FramebufferSurface(*mHwc, state.type, bq);
1182 producer = bq;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001183 }
1184
1185 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001186 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001187 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001188 state.type, hwcDisplayId, state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001189 display, dispSurface, producer,
1190 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001191 hw->setLayerStack(state.layerStack);
1192 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001193 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001194 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001195 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001196 if (state.isVirtualDisplay()) {
1197 if (hwcDisplayId >= 0) {
1198 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1199 hw->getWidth(), hw->getHeight(),
1200 hw->getFormat());
1201 }
1202 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001203 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001204 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001205 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001206 }
1207 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001208 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001209 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001210
Mathias Agopian84300952012-11-21 16:02:13 -08001211 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1212 // The transform hint might have changed for some layers
1213 // (either because a display has changed, or because a layer
1214 // as changed).
1215 //
1216 // Walk through all the layers in currentLayers,
1217 // and update their transform hint.
1218 //
1219 // If a layer is visible only on a single display, then that
1220 // display is used to calculate the hint, otherwise we use the
1221 // default display.
1222 //
1223 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1224 // the hint is set before we acquire a buffer from the surface texture.
1225 //
1226 // NOTE: layer transactions have taken place already, so we use their
1227 // drawing state. However, SurfaceFlinger's own transaction has not
1228 // happened yet, so we must use the current state layer list
1229 // (soon to become the drawing state list).
1230 //
1231 sp<const DisplayDevice> disp;
1232 uint32_t currentlayerStack = 0;
1233 for (size_t i=0; i<count; i++) {
1234 // NOTE: we rely on the fact that layers are sorted by
1235 // layerStack first (so we don't have to traverse the list
1236 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001237 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001238 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001239 if (i==0 || currentlayerStack != layerStack) {
1240 currentlayerStack = layerStack;
1241 // figure out if this layerstack is mirrored
1242 // (more than one display) if so, pick the default display,
1243 // if not, pick the only display it's on.
1244 disp.clear();
1245 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1246 sp<const DisplayDevice> hw(mDisplays[dpy]);
1247 if (hw->getLayerStack() == currentlayerStack) {
1248 if (disp == NULL) {
1249 disp = hw;
1250 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001251 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001252 break;
1253 }
1254 }
1255 }
1256 }
Chet Haase91d25932013-04-11 15:24:55 -07001257 if (disp == NULL) {
1258 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1259 // redraw after transform hint changes. See bug 8508397.
1260
1261 // could be null when this layer is using a layerStack
1262 // that is not visible on any display. Also can occur at
1263 // screen off/on times.
1264 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001265 }
Chet Haase91d25932013-04-11 15:24:55 -07001266 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001267 }
1268 }
1269
1270
Mathias Agopian3559b072012-08-15 13:46:03 -07001271 /*
1272 * Perform our own transaction if needed
1273 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001274
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001275 const LayerVector& layers(mDrawingState.layersSortedByZ);
1276 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001277 // layers have been added
1278 mVisibleRegionsDirty = true;
1279 }
1280
1281 // some layers might have been removed, so
1282 // we need to update the regions they're exposing.
1283 if (mLayersRemoved) {
1284 mLayersRemoved = false;
1285 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001286 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001287 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001288 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001289 if (currentLayers.indexOf(layer) < 0) {
1290 // this layer is not visible anymore
1291 // TODO: we could traverse the tree from front to back and
1292 // compute the actual visible region
1293 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001294 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001295 Region visibleReg = s.transform.transform(
1296 Region(Rect(s.active.w, s.active.h)));
1297 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001298 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001299 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300 }
1301
1302 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001303}
1304
1305void SurfaceFlinger::commitTransaction()
1306{
1307 if (!mLayersPendingRemoval.isEmpty()) {
1308 // Notify removed layers now that they can't be drawn from
1309 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1310 mLayersPendingRemoval[i]->onRemoved();
1311 }
1312 mLayersPendingRemoval.clear();
1313 }
1314
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001315 // If this transaction is part of a window animation then the next frame
1316 // we composite should be considered an animation as well.
1317 mAnimCompositionPending = mAnimTransactionPending;
1318
Mathias Agopian4fec8732012-06-29 14:12:52 -07001319 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001320 mTransactionPending = false;
1321 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001322 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323}
1324
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001326 const LayerVector& currentLayers, uint32_t layerStack,
1327 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328{
Mathias Agopian841cde52012-03-01 15:44:37 -08001329 ATRACE_CALL();
1330
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331 Region aboveOpaqueLayers;
1332 Region aboveCoveredLayers;
1333 Region dirty;
1334
Mathias Agopian87baae12012-07-31 12:38:26 -07001335 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001336
1337 size_t i = currentLayers.size();
1338 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001339 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001340
1341 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001342 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001343
Jesse Hall01e29052013-02-19 16:13:35 -08001344 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001345 if (s.layerStack != layerStack)
1346 continue;
1347
Mathias Agopianab028732010-03-16 16:41:46 -07001348 /*
1349 * opaqueRegion: area of a surface that is fully opaque.
1350 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001351 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001352
1353 /*
1354 * visibleRegion: area of a surface that is visible on screen
1355 * and not fully transparent. This is essentially the layer's
1356 * footprint minus the opaque regions above it.
1357 * Areas covered by a translucent surface are considered visible.
1358 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001359 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001360
1361 /*
1362 * coveredRegion: area of a surface that is covered by all
1363 * visible regions above it (which includes the translucent areas).
1364 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001365 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001366
Jesse Halla8026d22012-09-25 13:25:04 -07001367 /*
1368 * transparentRegion: area of a surface that is hinted to be completely
1369 * transparent. This is only used to tell when the layer has no visible
1370 * non-transparent regions and can be removed from the layer list. It
1371 * does not affect the visibleRegion of this layer or any layers
1372 * beneath it. The hint may not be correct if apps don't respect the
1373 * SurfaceView restrictions (which, sadly, some don't).
1374 */
1375 Region transparentRegion;
1376
Mathias Agopianab028732010-03-16 16:41:46 -07001377
1378 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001379 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001380 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001381 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001382 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001383 if (!visibleRegion.isEmpty()) {
1384 // Remove the transparent area from the visible region
1385 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001386 const Transform tr(s.transform);
1387 if (tr.transformed()) {
1388 if (tr.preserveRects()) {
1389 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001390 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001391 } else {
1392 // transformation too complex, can't do the
1393 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001394 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001395 }
1396 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001397 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001398 }
Mathias Agopianab028732010-03-16 16:41:46 -07001399 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001400
Mathias Agopianab028732010-03-16 16:41:46 -07001401 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001402 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001403 if (s.alpha==255 && !translucent &&
1404 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1405 // the opaque region is the layer's footprint
1406 opaqueRegion = visibleRegion;
1407 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001408 }
1409 }
1410
Mathias Agopianab028732010-03-16 16:41:46 -07001411 // Clip the covered region to the visible region
1412 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1413
1414 // Update aboveCoveredLayers for next (lower) layer
1415 aboveCoveredLayers.orSelf(visibleRegion);
1416
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001417 // subtract the opaque region covered by the layers above us
1418 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419
1420 // compute this layer's dirty region
1421 if (layer->contentDirty) {
1422 // we need to invalidate the whole region
1423 dirty = visibleRegion;
1424 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001425 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426 layer->contentDirty = false;
1427 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001428 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001429 * the exposed region consists of two components:
1430 * 1) what's VISIBLE now and was COVERED before
1431 * 2) what's EXPOSED now less what was EXPOSED before
1432 *
1433 * note that (1) is conservative, we start with the whole
1434 * visible region but only keep what used to be covered by
1435 * something -- which mean it may have been exposed.
1436 *
1437 * (2) handles areas that were not covered by anything but got
1438 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001439 */
Mathias Agopianab028732010-03-16 16:41:46 -07001440 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001441 const Region oldVisibleRegion = layer->visibleRegion;
1442 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001443 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1444 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001445 }
1446 dirty.subtractSelf(aboveOpaqueLayers);
1447
1448 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001449 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001450
Mathias Agopianab028732010-03-16 16:41:46 -07001451 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001452 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001453
Jesse Halla8026d22012-09-25 13:25:04 -07001454 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455 layer->setVisibleRegion(visibleRegion);
1456 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001457 layer->setVisibleNonTransparentRegion(
1458 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001459 }
1460
Mathias Agopian87baae12012-07-31 12:38:26 -07001461 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462}
1463
Mathias Agopian87baae12012-07-31 12:38:26 -07001464void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1465 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001466 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001467 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1468 if (hw->getLayerStack() == layerStack) {
1469 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001470 }
1471 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001472}
1473
1474void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001475{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001476 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001477
Mathias Agopian4fec8732012-06-29 14:12:52 -07001478 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001479 const LayerVector& layers(mDrawingState.layersSortedByZ);
1480 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001481 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001482 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001483 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001484 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001485 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001486 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001487
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001488 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489}
1490
Mathias Agopianad456f92011-01-13 17:53:01 -08001491void SurfaceFlinger::invalidateHwcGeometry()
1492{
1493 mHwWorkListDirty = true;
1494}
1495
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001496
Mathias Agopiancd60f992012-08-16 16:28:27 -07001497void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001498 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001499{
Mathias Agopian87baae12012-07-31 12:38:26 -07001500 Region dirtyRegion(inDirtyRegion);
1501
Mathias Agopianb8a55602009-06-26 19:06:36 -07001502 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001503 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001504
Mathias Agopian42977342012-08-05 00:40:46 -07001505 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001506 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001507 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1508 // takes a rectangle, we must make sure to update that whole
1509 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001510 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001511 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001512 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001513 // We need to redraw the rectangle that will be updated
1514 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001515 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001516 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001517 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001518 } else {
1519 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001520 dirtyRegion.set(hw->bounds());
1521 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001522 }
1523 }
1524
Mathias Agopianff2ed702013-09-01 21:36:12 -07001525 if (CC_LIKELY(!mDaltonize)) {
1526 doComposeSurfaces(hw, dirtyRegion);
1527 } else {
1528 RenderEngine& engine(getRenderEngine());
1529 engine.beginGroup(mDaltonizer());
1530 doComposeSurfaces(hw, dirtyRegion);
1531 engine.endGroup();
1532 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001533
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001534 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001535 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001536
1537 // swap buffers (presentation)
1538 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001539}
1540
Mathias Agopiancd60f992012-08-16 16:28:27 -07001541void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001542{
Mathias Agopian3f844832013-08-07 21:24:32 -07001543 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001544 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001545 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001546 HWComposer::LayerListIterator cur = hwc.begin(id);
1547 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001548
Jesse Halld05a17f2013-09-30 16:49:30 -07001549 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001550 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001551 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001552 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1553 hw->getDisplayName().string());
1554 return;
1555 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001556
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001557 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001558 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001559 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001560 // when using overlays, we assume a fully transparent framebuffer
1561 // NOTE: we could reduce how much we need to clear, for instance
1562 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001563 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001564 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001565 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001566 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001567 // we start with the whole screen area
1568 const Region bounds(hw->getBounds());
1569
1570 // we remove the scissor part
1571 // we're left with the letterbox region
1572 // (common case is that letterbox ends-up being empty)
1573 const Region letterbox(bounds.subtract(hw->getScissor()));
1574
1575 // compute the area to clear
1576 Region region(hw->undefinedRegion.merge(letterbox));
1577
1578 // but limit it to the dirty region
1579 region.andSelf(dirty);
1580
Mathias Agopianb9494d52012-04-18 02:28:45 -07001581 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001582 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001583 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001584 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001585 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001586 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001587
Mathias Agopian766dc492012-10-30 18:08:06 -07001588 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1589 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001590 // scissor on the main display. It should never be needed
1591 // anyways (though in theory it could since the API allows it).
1592 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001593 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001594 if (scissor != bounds) {
1595 // scissor doesn't match the screen's dimensions, so we
1596 // need to clear everything outside of it and enable
1597 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001598
Mathias Agopianf45c5102012-10-24 16:29:17 -07001599 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001600 const uint32_t height = hw->getHeight();
1601 engine.setScissor(scissor.left, height - scissor.bottom,
1602 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001603 }
1604 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001605 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001606
Mathias Agopian85d751c2012-08-29 16:59:24 -07001607 /*
1608 * and then, render the layers targeted at the framebuffer
1609 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001610
Mathias Agopian13127d82013-03-05 17:47:11 -08001611 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001612 const size_t count = layers.size();
1613 const Transform& tr = hw->getTransform();
1614 if (cur != end) {
1615 // we're using h/w composer
1616 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001617 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001618 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001619 if (!clip.isEmpty()) {
1620 switch (cur->getCompositionType()) {
1621 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001622 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001623 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1624 && i
Mathias Agopianac683022013-10-01 15:36:52 -07001625 && layer->isOpaque() && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001626 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001627 // never clear the very first layer since we're
1628 // guaranteed the FB is already cleared
1629 layer->clearWithOpenGL(hw, clip);
1630 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001631 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001632 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001633 case HWC_FRAMEBUFFER: {
1634 layer->draw(hw, clip);
1635 break;
1636 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001637 case HWC_FRAMEBUFFER_TARGET: {
1638 // this should not happen as the iterator shouldn't
1639 // let us get there.
1640 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1641 break;
1642 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001643 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001644 }
1645 layer->setAcquireFence(hw, *cur);
1646 }
1647 } else {
1648 // we're not using h/w composer
1649 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001650 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001651 const Region clip(dirty.intersect(
1652 tr.transform(layer->visibleRegion)));
1653 if (!clip.isEmpty()) {
1654 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001655 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001656 }
1657 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001658
1659 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001660 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001661}
1662
Mathias Agopian3f844832013-08-07 21:24:32 -07001663void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001664 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001665 RenderEngine& engine(getRenderEngine());
1666 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667}
1668
Mathias Agopianac9fa422013-02-11 16:40:36 -08001669void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1670 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001671 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001672 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001673{
Mathias Agopian96f08192010-06-02 23:28:45 -07001674 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001675 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001676
Mathias Agopian96f08192010-06-02 23:28:45 -07001677 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001678 Mutex::Autolock _l(mStateLock);
1679 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001680 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001681}
1682
Mathias Agopian3f844832013-08-07 21:24:32 -07001683status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001684 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001685 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001686 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001687 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001688 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001689 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001690 return NO_ERROR;
1691 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001692 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001693}
1694
Mathias Agopian3f844832013-08-07 21:24:32 -07001695uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001696 return android_atomic_release_load(&mTransactionFlags);
1697}
1698
Mathias Agopian3f844832013-08-07 21:24:32 -07001699uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001700 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1701}
1702
Mathias Agopian3f844832013-08-07 21:24:32 -07001703uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001704 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1705 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001706 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001707 }
1708 return old;
1709}
1710
Mathias Agopian8b33f032012-07-24 20:43:54 -07001711void SurfaceFlinger::setTransactionState(
1712 const Vector<ComposerState>& state,
1713 const Vector<DisplayState>& displays,
1714 uint32_t flags)
1715{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001716 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001717 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001718 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001719
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001720 if (flags & eAnimation) {
1721 // For window updates that are part of an animation we must wait for
1722 // previous animation "frames" to be handled.
1723 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001724 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001725 if (CC_UNLIKELY(err != NO_ERROR)) {
1726 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001727 // caller after a few seconds.
1728 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1729 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001730 mAnimTransactionPending = false;
1731 break;
1732 }
1733 }
1734 }
1735
Mathias Agopiane57f2922012-08-09 16:29:12 -07001736 size_t count = displays.size();
1737 for (size_t i=0 ; i<count ; i++) {
1738 const DisplayState& s(displays[i]);
1739 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001740 }
1741
Mathias Agopiane57f2922012-08-09 16:29:12 -07001742 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001743 for (size_t i=0 ; i<count ; i++) {
1744 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001745 // Here we need to check that the interface we're given is indeed
1746 // one of our own. A malicious client could give us a NULL
1747 // IInterface, or one of its own or even one of our own but a
1748 // different type. All these situations would cause us to crash.
1749 //
1750 // NOTE: it would be better to use RTTI as we could directly check
1751 // that we have a Client*. however, RTTI is disabled in Android.
1752 if (s.client != NULL) {
1753 sp<IBinder> binder = s.client->asBinder();
1754 if (binder != NULL) {
1755 String16 desc(binder->getInterfaceDescriptor());
1756 if (desc == ISurfaceComposerClient::descriptor) {
1757 sp<Client> client( static_cast<Client *>(s.client.get()) );
1758 transactionFlags |= setClientStateLocked(client, s.state);
1759 }
1760 }
1761 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001762 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001763
Mathias Agopian386aa982011-11-07 21:58:03 -08001764 if (transactionFlags) {
1765 // this triggers the transaction
1766 setTransactionFlags(transactionFlags);
1767
1768 // if this is a synchronous transaction, wait for it to take effect
1769 // before returning.
1770 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001771 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001772 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001773 if (flags & eAnimation) {
1774 mAnimTransactionPending = true;
1775 }
1776 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001777 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1778 if (CC_UNLIKELY(err != NO_ERROR)) {
1779 // just in case something goes wrong in SF, return to the
1780 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001781 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1782 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001783 break;
1784 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001785 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001786 }
1787}
1788
Mathias Agopiane57f2922012-08-09 16:29:12 -07001789uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1790{
Jesse Hall9a143922012-10-04 16:29:19 -07001791 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1792 if (dpyIdx < 0)
1793 return 0;
1794
Mathias Agopiane57f2922012-08-09 16:29:12 -07001795 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001796 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001797 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001798 const uint32_t what = s.what;
1799 if (what & DisplayState::eSurfaceChanged) {
1800 if (disp.surface->asBinder() != s.surface->asBinder()) {
1801 disp.surface = s.surface;
1802 flags |= eDisplayTransactionNeeded;
1803 }
1804 }
1805 if (what & DisplayState::eLayerStackChanged) {
1806 if (disp.layerStack != s.layerStack) {
1807 disp.layerStack = s.layerStack;
1808 flags |= eDisplayTransactionNeeded;
1809 }
1810 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001811 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001812 if (disp.orientation != s.orientation) {
1813 disp.orientation = s.orientation;
1814 flags |= eDisplayTransactionNeeded;
1815 }
1816 if (disp.frame != s.frame) {
1817 disp.frame = s.frame;
1818 flags |= eDisplayTransactionNeeded;
1819 }
1820 if (disp.viewport != s.viewport) {
1821 disp.viewport = s.viewport;
1822 flags |= eDisplayTransactionNeeded;
1823 }
1824 }
1825 }
1826 return flags;
1827}
1828
1829uint32_t SurfaceFlinger::setClientStateLocked(
1830 const sp<Client>& client,
1831 const layer_state_t& s)
1832{
1833 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001834 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001835 if (layer != 0) {
1836 const uint32_t what = s.what;
1837 if (what & layer_state_t::ePositionChanged) {
1838 if (layer->setPosition(s.x, s.y))
1839 flags |= eTraversalNeeded;
1840 }
1841 if (what & layer_state_t::eLayerChanged) {
1842 // NOTE: index needs to be calculated before we update the state
1843 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1844 if (layer->setLayer(s.z)) {
1845 mCurrentState.layersSortedByZ.removeAt(idx);
1846 mCurrentState.layersSortedByZ.add(layer);
1847 // we need traversal (state changed)
1848 // AND transaction (list changed)
1849 flags |= eTransactionNeeded|eTraversalNeeded;
1850 }
1851 }
1852 if (what & layer_state_t::eSizeChanged) {
1853 if (layer->setSize(s.w, s.h)) {
1854 flags |= eTraversalNeeded;
1855 }
1856 }
1857 if (what & layer_state_t::eAlphaChanged) {
1858 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1859 flags |= eTraversalNeeded;
1860 }
1861 if (what & layer_state_t::eMatrixChanged) {
1862 if (layer->setMatrix(s.matrix))
1863 flags |= eTraversalNeeded;
1864 }
1865 if (what & layer_state_t::eTransparentRegionChanged) {
1866 if (layer->setTransparentRegionHint(s.transparentRegion))
1867 flags |= eTraversalNeeded;
1868 }
1869 if (what & layer_state_t::eVisibilityChanged) {
1870 if (layer->setFlags(s.flags, s.mask))
1871 flags |= eTraversalNeeded;
1872 }
1873 if (what & layer_state_t::eCropChanged) {
1874 if (layer->setCrop(s.crop))
1875 flags |= eTraversalNeeded;
1876 }
1877 if (what & layer_state_t::eLayerStackChanged) {
1878 // NOTE: index needs to be calculated before we update the state
1879 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1880 if (layer->setLayerStack(s.layerStack)) {
1881 mCurrentState.layersSortedByZ.removeAt(idx);
1882 mCurrentState.layersSortedByZ.add(layer);
1883 // we need traversal (state changed)
1884 // AND transaction (list changed)
1885 flags |= eTransactionNeeded|eTraversalNeeded;
1886 }
1887 }
1888 }
1889 return flags;
1890}
1891
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001892status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001893 const String8& name,
1894 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001895 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1896 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001897{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001898 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001899 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001900 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001901 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001902 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001903 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001904
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001905 status_t result = NO_ERROR;
1906
1907 sp<Layer> layer;
1908
Mathias Agopian3165cc22012-08-08 19:42:09 -07001909 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1910 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001911 result = createNormalLayer(client,
1912 name, w, h, flags, format,
1913 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001914 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001915 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001916 result = createDimLayer(client,
1917 name, w, h, flags,
1918 handle, gbp, &layer);
1919 break;
1920 default:
1921 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001922 break;
1923 }
1924
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001925 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001926 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001927 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001928 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001929 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001930}
1931
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001932status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1933 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1934 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001935{
1936 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001937 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001938 case PIXEL_FORMAT_TRANSPARENT:
1939 case PIXEL_FORMAT_TRANSLUCENT:
1940 format = PIXEL_FORMAT_RGBA_8888;
1941 break;
1942 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001943#ifdef NO_RGBX_8888
1944 format = PIXEL_FORMAT_RGB_565;
1945#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001946 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001947#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001948 break;
1949 }
1950
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001951#ifdef NO_RGBX_8888
1952 if (format == PIXEL_FORMAT_RGBX_8888)
1953 format = PIXEL_FORMAT_RGBA_8888;
1954#endif
1955
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001956 *outLayer = new Layer(this, client, name, w, h, flags);
1957 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1958 if (err == NO_ERROR) {
1959 *handle = (*outLayer)->getHandle();
1960 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001961 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001962
1963 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1964 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001965}
1966
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001967status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1968 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1969 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001970{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001971 *outLayer = new LayerDim(this, client, name, w, h, flags);
1972 *handle = (*outLayer)->getHandle();
1973 *gbp = (*outLayer)->getBufferQueue();
1974 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001975}
1976
Mathias Agopianac9fa422013-02-11 16:40:36 -08001977status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001978{
Mathias Agopian67106042013-03-14 19:18:13 -07001979 // called by the window manager when it wants to remove a Layer
1980 status_t err = NO_ERROR;
1981 sp<Layer> l(client->getLayerUser(handle));
1982 if (l != NULL) {
1983 err = removeLayer(l);
1984 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1985 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001986 }
1987 return err;
1988}
1989
Mathias Agopian13127d82013-03-05 17:47:11 -08001990status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001991{
Mathias Agopian67106042013-03-14 19:18:13 -07001992 // called by ~LayerCleaner() when all references to the IBinder (handle)
1993 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001994 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001995 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001996 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07001997 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001998 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001999 "error removing layer=%p (%s)", l.get(), strerror(-err));
2000 }
2001 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002002}
2003
Mathias Agopianb60314a2012-04-10 22:09:54 -07002004// ---------------------------------------------------------------------------
2005
Andy McFadden13a082e2012-08-24 10:16:42 -07002006void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002007 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002008 Vector<ComposerState> state;
2009 Vector<DisplayState> displays;
2010 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002011 d.what = DisplayState::eDisplayProjectionChanged |
2012 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002013 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002014 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002015 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002016 d.frame.makeInvalid();
2017 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002018 displays.add(d);
2019 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002020 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07002021
2022 const nsecs_t period =
2023 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2024 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002025}
2026
2027void SurfaceFlinger::initializeDisplays() {
2028 class MessageScreenInitialized : public MessageBase {
2029 SurfaceFlinger* flinger;
2030 public:
2031 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2032 virtual bool handler() {
2033 flinger->onInitializeDisplays();
2034 return true;
2035 }
2036 };
2037 sp<MessageBase> msg = new MessageScreenInitialized(this);
2038 postMessageAsync(msg); // we may be called from main thread, use async message
2039}
2040
2041
Mathias Agopiancde87a32012-09-13 14:09:01 -07002042void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002043 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2044 if (hw->isScreenAcquired()) {
2045 // this is expected, e.g. when power manager wakes up during boot
2046 ALOGD(" screen was previously acquired");
2047 return;
2048 }
2049
Mathias Agopian42977342012-08-05 00:40:46 -07002050 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002051 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002052 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002053 // built-in display, tell the HWC
2054 getHwComposer().acquire(type);
2055
2056 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2057 // FIXME: eventthread only knows about the main display right now
2058 mEventThread->onScreenAcquired();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07002059
Jesse Hall948fe0c2013-10-14 12:56:09 -07002060 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002061 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002062 }
Mathias Agopian20128302012-08-13 18:32:13 -07002063 mVisibleRegionsDirty = true;
2064 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002065}
2066
Mathias Agopiancde87a32012-09-13 14:09:01 -07002067void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002068 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2069 if (!hw->isScreenAcquired()) {
2070 ALOGD(" screen was previously released");
2071 return;
2072 }
2073
2074 hw->releaseScreen();
2075 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002076 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002077 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002078 disableHardwareVsync(true); // also cancels any in-progress resync
2079
Mathias Agopiancde87a32012-09-13 14:09:01 -07002080 // FIXME: eventthread only knows about the main display right now
2081 mEventThread->onScreenReleased();
2082 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002083
2084 // built-in display, tell the HWC
2085 getHwComposer().release(type);
2086 }
2087 mVisibleRegionsDirty = true;
2088 // from this point on, SF will stop drawing on this display
2089}
2090
2091void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2092 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002093 SurfaceFlinger& mFlinger;
2094 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002095 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002096 MessageScreenAcquired(SurfaceFlinger& flinger,
2097 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002098 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002099 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2100 if (hw == NULL) {
2101 ALOGE("Attempt to unblank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002102 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002103 ALOGW("Attempt to unblank virtual display");
2104 } else {
2105 mFlinger.onScreenAcquired(hw);
2106 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002107 return true;
2108 }
2109 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002110 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2111 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002112}
2113
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002114void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002115 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002116 SurfaceFlinger& mFlinger;
2117 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002118 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002119 MessageScreenReleased(SurfaceFlinger& flinger,
2120 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002121 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002122 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2123 if (hw == NULL) {
2124 ALOGE("Attempt to blank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002125 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002126 ALOGW("Attempt to blank virtual display");
2127 } else {
2128 mFlinger.onScreenReleased(hw);
2129 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002130 return true;
2131 }
2132 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002133 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2134 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002135}
2136
2137// ---------------------------------------------------------------------------
2138
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002139status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2140{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002141 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002142
Mathias Agopianbd115332013-04-18 16:41:04 -07002143 IPCThreadState* ipc = IPCThreadState::self();
2144 const int pid = ipc->getCallingPid();
2145 const int uid = ipc->getCallingUid();
2146 if ((uid != AID_SHELL) &&
2147 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002148 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002149 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002150 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002151 // Try to get the main lock, but don't insist if we can't
2152 // (this would indicate SF is stuck, but we want to be able to
2153 // print something in dumpsys).
2154 int retry = 3;
2155 while (mStateLock.tryLock()<0 && --retry>=0) {
2156 usleep(1000000);
2157 }
2158 const bool locked(retry >= 0);
2159 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002160 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002161 "SurfaceFlinger appears to be unresponsive, "
2162 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002163 }
2164
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002165 bool dumpAll = true;
2166 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002167 size_t numArgs = args.size();
2168 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002169 if ((index < numArgs) &&
2170 (args[index] == String16("--list"))) {
2171 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002172 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002173 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002174 }
2175
2176 if ((index < numArgs) &&
2177 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002178 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002179 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002180 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002181 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002182
2183 if ((index < numArgs) &&
2184 (args[index] == String16("--latency-clear"))) {
2185 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002186 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002187 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002188 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002189 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002190
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002191 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002192 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002193 }
2194
Mathias Agopian9795c422009-08-26 16:36:26 -07002195 if (locked) {
2196 mStateLock.unlock();
2197 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002198 }
2199 write(fd, result.string(), result.size());
2200 return NO_ERROR;
2201}
2202
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002203void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002204 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002205{
2206 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2207 const size_t count = currentLayers.size();
2208 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002209 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002210 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002211 }
2212}
2213
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002214void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002215 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002216{
2217 String8 name;
2218 if (index < args.size()) {
2219 name = String8(args[index]);
2220 index++;
2221 }
2222
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002223 const nsecs_t period =
2224 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2225 result.appendFormat("%lld\n", period);
2226
2227 if (name.isEmpty()) {
2228 mAnimFrameTracker.dump(result);
2229 } else {
2230 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2231 const size_t count = currentLayers.size();
2232 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002233 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002234 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002235 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002236 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002237 }
2238 }
2239}
2240
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002241void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002242 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002243{
2244 String8 name;
2245 if (index < args.size()) {
2246 name = String8(args[index]);
2247 index++;
2248 }
2249
2250 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2251 const size_t count = currentLayers.size();
2252 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002253 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002254 if (name.isEmpty() || (name == layer->getName())) {
2255 layer->clearStats();
2256 }
2257 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002258
2259 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002260}
2261
Jamie Gennis6547ff42013-07-16 20:12:42 -07002262// This should only be called from the main thread. Otherwise it would need
2263// the lock and should use mCurrentState rather than mDrawingState.
2264void SurfaceFlinger::logFrameStats() {
2265 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2266 const size_t count = drawingLayers.size();
2267 for (size_t i=0 ; i<count ; i++) {
2268 const sp<Layer>& layer(drawingLayers[i]);
2269 layer->logFrameStats();
2270 }
2271
2272 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2273}
2274
Andy McFadden4803b742012-09-24 19:07:20 -07002275/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2276{
2277 static const char* config =
2278 " [sf"
2279#ifdef NO_RGBX_8888
2280 " NO_RGBX_8888"
2281#endif
2282#ifdef HAS_CONTEXT_PRIORITY
2283 " HAS_CONTEXT_PRIORITY"
2284#endif
2285#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2286 " NEVER_DEFAULT_TO_ASYNC_MODE"
2287#endif
2288#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2289 " TARGET_DISABLE_TRIPLE_BUFFERING"
2290#endif
2291 "]";
2292 result.append(config);
2293}
2294
Mathias Agopian74d211a2013-04-22 16:55:35 +02002295void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2296 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002297{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002298 bool colorize = false;
2299 if (index < args.size()
2300 && (args[index] == String16("--color"))) {
2301 colorize = true;
2302 index++;
2303 }
2304
2305 Colorizer colorizer(colorize);
2306
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002307 // figure out if we're stuck somewhere
2308 const nsecs_t now = systemTime();
2309 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2310 const nsecs_t inTransaction(mDebugInTransaction);
2311 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2312 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2313
2314 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002315 * Dump library configuration.
2316 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002317
2318 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002319 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002320 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002321 appendSfConfigString(result);
2322 appendUiConfigString(result);
2323 appendGuiConfigString(result);
2324 result.append("\n");
2325
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002326 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002327 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002328 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002329 result.append(SyncFeatures::getInstance().toString());
2330 result.append("\n");
2331
Andy McFadden4803b742012-09-24 19:07:20 -07002332 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002333 * Dump the visible layer list
2334 */
2335 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2336 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002337 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002338 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002339 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002340 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002341 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002342 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002343 }
2344
2345 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002346 * Dump Display state
2347 */
2348
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002349 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002350 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002351 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002352 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2353 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002354 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002355 }
2356
2357 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002358 * Dump SurfaceFlinger global state
2359 */
2360
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002361 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002362 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002363 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002364
Mathias Agopian888c8222012-08-04 21:10:38 -07002365 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002366 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002367
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002368 colorizer.bold(result);
2369 result.appendFormat("EGL implementation : %s\n",
2370 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2371 colorizer.reset(result);
2372 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002373 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002374
Mathias Agopian875d8e12013-06-07 15:35:48 -07002375 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002376
Mathias Agopian42977342012-08-05 00:40:46 -07002377 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002378 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002379 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002380 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002381 " last eglSwapBuffers() time: %f us\n"
2382 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002383 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002384 " refresh-rate : %f fps\n"
2385 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002386 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002387 " gpu_to_cpu_unsupported : %d\n"
2388 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002389 mLastSwapBufferTime/1000.0,
2390 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002391 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002392 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2393 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002394 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002395 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002396
Mathias Agopian74d211a2013-04-22 16:55:35 +02002397 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002398 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002399
Mathias Agopian74d211a2013-04-22 16:55:35 +02002400 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002401 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002402
2403 /*
2404 * VSYNC state
2405 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002406 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002407
2408 /*
2409 * Dump HWComposer state
2410 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002411 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002412 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002413 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002414 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002415 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopianff2ed702013-09-01 21:36:12 -07002416 (mDebugDisableHWC || mDebugRegion || mDaltonize) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002417 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002418
2419 /*
2420 * Dump gralloc state
2421 */
2422 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2423 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002424}
2425
Mathias Agopian13127d82013-03-05 17:47:11 -08002426const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002427SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002428 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002429 wp<IBinder> dpy;
2430 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2431 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2432 dpy = mDisplays.keyAt(i);
2433 break;
2434 }
2435 }
2436 if (dpy == NULL) {
2437 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2438 // Just use the primary display so we have something to return
2439 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2440 }
2441 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002442}
2443
Keun young Park63f165f2012-08-31 10:53:36 -07002444bool SurfaceFlinger::startDdmConnection()
2445{
2446 void* libddmconnection_dso =
2447 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2448 if (!libddmconnection_dso) {
2449 return false;
2450 }
2451 void (*DdmConnection_start)(const char* name);
2452 DdmConnection_start =
2453 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2454 if (!DdmConnection_start) {
2455 dlclose(libddmconnection_dso);
2456 return false;
2457 }
2458 (*DdmConnection_start)(getServiceName());
2459 return true;
2460}
2461
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002462status_t SurfaceFlinger::onTransact(
2463 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2464{
2465 switch (code) {
2466 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002467 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002468 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002469 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002470 case BLANK:
2471 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002472 {
2473 // codes that require permission check
2474 IPCThreadState* ipc = IPCThreadState::self();
2475 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002476 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002477 if ((uid != AID_GRAPHICS) &&
2478 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002479 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002480 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2481 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002482 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002483 break;
2484 }
2485 case CAPTURE_SCREEN:
2486 {
2487 // codes that require permission check
2488 IPCThreadState* ipc = IPCThreadState::self();
2489 const int pid = ipc->getCallingPid();
2490 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002491 if ((uid != AID_GRAPHICS) &&
2492 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002493 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002494 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2495 return PERMISSION_DENIED;
2496 }
2497 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002498 }
2499 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002500
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002501 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2502 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002503 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002504 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002505 IPCThreadState* ipc = IPCThreadState::self();
2506 const int pid = ipc->getCallingPid();
2507 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002508 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002509 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002510 return PERMISSION_DENIED;
2511 }
2512 int n;
2513 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002514 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002515 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002516 return NO_ERROR;
2517 case 1002: // SHOW_UPDATES
2518 n = data.readInt32();
2519 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002520 invalidateHwcGeometry();
2521 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002522 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002523 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002524 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002525 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002526 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002527 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002528 setTransactionFlags(
2529 eTransactionNeeded|
2530 eDisplayTransactionNeeded|
2531 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002532 return NO_ERROR;
2533 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002534 case 1006:{ // send empty update
2535 signalRefresh();
2536 return NO_ERROR;
2537 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002538 case 1008: // toggle use of hw composer
2539 n = data.readInt32();
2540 mDebugDisableHWC = n ? 1 : 0;
2541 invalidateHwcGeometry();
2542 repaintEverything();
2543 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002544 case 1009: // toggle use of transform hint
2545 n = data.readInt32();
2546 mDebugDisableTransformHint = n ? 1 : 0;
2547 invalidateHwcGeometry();
2548 repaintEverything();
2549 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002550 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002551 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002552 reply->writeInt32(0);
2553 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002554 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002555 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002556 return NO_ERROR;
2557 case 1013: {
2558 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002559 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2560 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002561 return NO_ERROR;
2562 }
2563 case 1014: {
2564 // daltonize
2565 n = data.readInt32();
2566 switch (n % 10) {
2567 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2568 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2569 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2570 }
2571 if (n >= 10) {
2572 mDaltonizer.setMode(Daltonizer::correction);
2573 } else {
2574 mDaltonizer.setMode(Daltonizer::simulation);
2575 }
2576 mDaltonize = n > 0;
2577 invalidateHwcGeometry();
2578 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002579 }
2580 return NO_ERROR;
2581 }
2582 }
2583 return err;
2584}
2585
Mathias Agopian53331da2011-08-22 21:44:41 -07002586void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002587 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002588 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002589}
2590
Mathias Agopian59119e62010-10-11 12:37:43 -07002591// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002592// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002593// ---------------------------------------------------------------------------
2594
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002595/* The code below is here to handle b/8734824
2596 *
2597 * We create a IGraphicBufferProducer wrapper that forwards all calls
2598 * to the calling binder thread, where they are executed. This allows
2599 * the calling thread to be reused (on the other side) and not
2600 * depend on having "enough" binder threads to handle the requests.
2601 *
2602 */
2603
2604class GraphicProducerWrapper : public BBinder, public MessageHandler {
2605 sp<IGraphicBufferProducer> impl;
2606 sp<Looper> looper;
2607 status_t result;
2608 bool exitPending;
2609 bool exitRequested;
2610 mutable Barrier barrier;
2611 volatile int32_t memoryBarrier;
2612 uint32_t code;
2613 Parcel const* data;
2614 Parcel* reply;
2615
2616 enum {
2617 MSG_API_CALL,
2618 MSG_EXIT
2619 };
2620
2621 /*
2622 * this is called by our "fake" BpGraphicBufferProducer. We package the
2623 * data and reply Parcel and forward them to the calling thread.
2624 */
2625 virtual status_t transact(uint32_t code,
2626 const Parcel& data, Parcel* reply, uint32_t flags) {
2627 this->code = code;
2628 this->data = &data;
2629 this->reply = reply;
2630 android_atomic_acquire_store(0, &memoryBarrier);
2631 if (exitPending) {
2632 // if we've exited, we run the message synchronously right here
2633 handleMessage(Message(MSG_API_CALL));
2634 } else {
2635 barrier.close();
2636 looper->sendMessage(this, Message(MSG_API_CALL));
2637 barrier.wait();
2638 }
2639 return NO_ERROR;
2640 }
2641
2642 /*
2643 * here we run on the binder calling thread. All we've got to do is
2644 * call the real BpGraphicBufferProducer.
2645 */
2646 virtual void handleMessage(const Message& message) {
2647 android_atomic_release_load(&memoryBarrier);
2648 if (message.what == MSG_API_CALL) {
2649 impl->asBinder()->transact(code, data[0], reply);
2650 barrier.open();
2651 } else if (message.what == MSG_EXIT) {
2652 exitRequested = true;
2653 }
2654 }
2655
2656public:
2657 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2658 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2659 exitPending(false), exitRequested(false) {
2660 }
2661
2662 status_t waitForResponse() {
2663 do {
2664 looper->pollOnce(-1);
2665 } while (!exitRequested);
2666 return result;
2667 }
2668
2669 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002670 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002671 exitPending = true;
2672 looper->sendMessage(this, Message(MSG_EXIT));
2673 }
2674};
2675
2676
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002677status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2678 const sp<IGraphicBufferProducer>& producer,
2679 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002680 uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002681
2682 if (CC_UNLIKELY(display == 0))
2683 return BAD_VALUE;
2684
2685 if (CC_UNLIKELY(producer == 0))
2686 return BAD_VALUE;
2687
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002688 // if we have secure windows on this display, never allow the screen capture
2689 // unless the producer interface is local (i.e.: we can take a screenshot for
2690 // ourselves).
2691 if (!producer->asBinder()->localBinder()) {
2692 Mutex::Autolock _l(mStateLock);
2693 sp<const DisplayDevice> hw(getDisplayDevice(display));
2694 if (hw->getSecureLayerVisible()) {
2695 ALOGW("FB is protected: PERMISSION_DENIED");
2696 return PERMISSION_DENIED;
2697 }
2698 }
2699
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002700 class MessageCaptureScreen : public MessageBase {
2701 SurfaceFlinger* flinger;
2702 sp<IBinder> display;
2703 sp<IGraphicBufferProducer> producer;
2704 uint32_t reqWidth, reqHeight;
2705 uint32_t minLayerZ,maxLayerZ;
2706 status_t result;
2707 public:
2708 MessageCaptureScreen(SurfaceFlinger* flinger,
2709 const sp<IBinder>& display,
2710 const sp<IGraphicBufferProducer>& producer,
2711 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002712 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002713 : flinger(flinger), display(display), producer(producer),
2714 reqWidth(reqWidth), reqHeight(reqHeight),
2715 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2716 result(PERMISSION_DENIED)
2717 {
2718 }
2719 status_t getResult() const {
2720 return result;
2721 }
2722 virtual bool handler() {
2723 Mutex::Autolock _l(flinger->mStateLock);
2724 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002725 result = flinger->captureScreenImplLocked(hw,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002726 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002727 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002728 return true;
2729 }
2730 };
2731
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002732 // make sure to process transactions before screenshots -- a transaction
2733 // might already be pending but scheduled for VSYNC; this guarantees we
2734 // will handle it before the screenshot. When VSYNC finally arrives
2735 // the scheduled transaction will be a no-op. If no transactions are
2736 // scheduled at this time, this will end-up being a no-op as well.
2737 mEventQueue.invalidateTransactionNow();
2738
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002739 // this creates a "fake" BBinder which will serve as a "fake" remote
2740 // binder to receive the marshaled calls and forward them to the
2741 // real remote (a BpGraphicBufferProducer)
2742 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2743
2744 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2745 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002746 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002747 display, IGraphicBufferProducer::asInterface( wrapper ),
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002748 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002749
2750 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002751 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002752 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002753 }
2754 return res;
2755}
2756
Mathias Agopian180f10d2013-04-10 22:55:41 -07002757
2758void SurfaceFlinger::renderScreenImplLocked(
2759 const sp<const DisplayDevice>& hw,
2760 uint32_t reqWidth, uint32_t reqHeight,
2761 uint32_t minLayerZ, uint32_t maxLayerZ,
2762 bool yswap)
2763{
2764 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002765 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002766
2767 // get screen geometry
2768 const uint32_t hw_w = hw->getWidth();
2769 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002770 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2771
2772 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002773 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002774
2775 // set-up our viewport
Mathias Agopian3f844832013-08-07 21:24:32 -07002776 engine.setViewportAndProjection(reqWidth, reqHeight, hw_w, hw_h, yswap);
2777 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002778
2779 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002780 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002781
2782 const LayerVector& layers( mDrawingState.layersSortedByZ );
2783 const size_t count = layers.size();
2784 for (size_t i=0 ; i<count ; ++i) {
2785 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002786 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002787 if (state.layerStack == hw->getLayerStack()) {
2788 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2789 if (layer->isVisible()) {
2790 if (filtering) layer->setFiltering(true);
2791 layer->draw(hw);
2792 if (filtering) layer->setFiltering(false);
2793 }
2794 }
2795 }
2796 }
2797
2798 // compositionComplete is needed for older driver
2799 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002800 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002801}
2802
2803
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002804status_t SurfaceFlinger::captureScreenImplLocked(
2805 const sp<const DisplayDevice>& hw,
2806 const sp<IGraphicBufferProducer>& producer,
2807 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002808 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002809{
2810 ATRACE_CALL();
2811
Mathias Agopian180f10d2013-04-10 22:55:41 -07002812 // get screen geometry
2813 const uint32_t hw_w = hw->getWidth();
2814 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002815
Mathias Agopian180f10d2013-04-10 22:55:41 -07002816 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2817 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2818 reqWidth, reqHeight, hw_w, hw_h);
2819 return BAD_VALUE;
2820 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002821
Mathias Agopian180f10d2013-04-10 22:55:41 -07002822 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2823 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2824
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002825 // create a surface (because we're a producer, and we need to
2826 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07002827 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002828 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002829
2830 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002831 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002832 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2833 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002834
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002835 int err = 0;
2836 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07002837 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002838 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2839 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002840
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002841 if (err == NO_ERROR) {
2842 ANativeWindowBuffer* buffer;
2843 /* TODO: Once we have the sync framework everywhere this can use
2844 * server-side waits on the fence that dequeueBuffer returns.
2845 */
2846 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2847 if (result == NO_ERROR) {
2848 // create an EGLImage from the buffer so we can later
2849 // turn it into a texture
2850 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2851 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2852 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002853 // this binds the given EGLImage as a framebuffer for the
2854 // duration of this scope.
2855 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2856 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002857 // this will in fact render into our dequeued buffer
2858 // via an FBO, which means we didn't have to create
2859 // an EGLSurface and therefore we're not
2860 // dependent on the context's EGLConfig.
2861 renderScreenImplLocked(hw, reqWidth, reqHeight,
2862 minLayerZ, maxLayerZ, true);
Mathias Agopiand5556842013-09-19 17:08:37 -07002863
Andy McFadden2d8d1202013-10-09 16:38:02 -07002864 // Create a sync point and wait on it, so we know the buffer is
2865 // ready before we pass it along. We can't trivially call glFlush(),
2866 // so we use a wait flag instead.
2867 // TODO: pass a sync fd to queueBuffer() and let the consumer wait.
2868 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
2869 if (sync != EGL_NO_SYNC_KHR) {
2870 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
2871 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
2872 EGLint eglErr = eglGetError();
2873 eglDestroySyncKHR(mEGLDisplay, sync);
2874 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
2875 ALOGW("captureScreen: fence wait timed out");
2876 } else {
2877 ALOGW_IF(eglErr != EGL_SUCCESS,
2878 "captureScreen: error waiting on EGL fence: %#x", eglErr);
2879 }
2880 } else {
2881 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
2882 // not fatal
2883 }
2884
Mathias Agopiand5556842013-09-19 17:08:37 -07002885 if (DEBUG_SCREENSHOTS) {
2886 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
2887 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
2888 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
2889 hw, minLayerZ, maxLayerZ);
2890 delete [] pixels;
2891 }
2892
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002893 } else {
2894 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2895 result = INVALID_OPERATION;
2896 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002897 // destroy our image
2898 eglDestroyImageKHR(mEGLDisplay, image);
2899 } else {
2900 result = BAD_VALUE;
2901 }
2902 window->queueBuffer(window, buffer, -1);
2903 }
2904 } else {
2905 result = BAD_VALUE;
2906 }
2907 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2908 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002909
Mathias Agopian74c40c02010-09-29 13:02:36 -07002910 return result;
2911}
2912
Mathias Agopiand5556842013-09-19 17:08:37 -07002913void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
2914 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07002915 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07002916 for (size_t y=0 ; y<h ; y++) {
2917 uint32_t const * p = (uint32_t const *)vaddr + y*s;
2918 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07002919 if (p[x] != 0xFF000000) return;
2920 }
2921 }
2922 ALOGE("*** we just took a black screenshot ***\n"
2923 "requested minz=%d, maxz=%d, layerStack=%d",
2924 minLayerZ, maxLayerZ, hw->getLayerStack());
2925 const LayerVector& layers( mDrawingState.layersSortedByZ );
2926 const size_t count = layers.size();
2927 for (size_t i=0 ; i<count ; ++i) {
2928 const sp<Layer>& layer(layers[i]);
2929 const Layer::State& state(layer->getDrawingState());
2930 const bool visible = (state.layerStack == hw->getLayerStack())
2931 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2932 && (layer->isVisible());
2933 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
2934 visible ? '+' : '-',
2935 i, layer->getName().string(), state.layerStack, state.z,
2936 layer->isVisible(), state.flags, state.alpha);
2937 }
2938 }
2939}
2940
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002941// ---------------------------------------------------------------------------
2942
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002943SurfaceFlinger::LayerVector::LayerVector() {
2944}
2945
2946SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002947 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002948}
2949
2950int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2951 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002952{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002953 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002954 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2955 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002956
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002957 uint32_t ls = l->getCurrentState().layerStack;
2958 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002959 if (ls != rs)
2960 return ls - rs;
2961
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002962 uint32_t lz = l->getCurrentState().z;
2963 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002964 if (lz != rz)
2965 return lz - rz;
2966
2967 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002968}
2969
2970// ---------------------------------------------------------------------------
2971
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002972SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2973 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002974}
2975
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002976SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002977 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002978 viewport.makeInvalid();
2979 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002980}
2981
2982// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002983
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002984}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002985
2986
2987#if defined(__gl_h_)
2988#error "don't include gl/gl.h in this file"
2989#endif
2990
2991#if defined(__gl2_h_)
2992#error "don't include gl2/gl2.h in this file"
2993#endif