blob: 31d3f6fdeb6a58f2ad4662117bf5d6fd82895728 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Greg Hackmann86efcc02014-03-07 12:44:02 -080024#include <inttypes.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070025
26#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
28#include <cutils/log.h>
29#include <cutils/properties.h>
30
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070031#include <binder/IPCThreadState.h>
32#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070033#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070034#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070035
Mathias Agopianc666cae2012-07-25 18:56:13 -070036#include <ui/DisplayInfo.h>
37
Mathias Agopian921e6ac2012-07-23 23:11:29 -070038#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070039#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070040#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070041#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080042#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080043#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070044
45#include <ui/GraphicBufferAllocator.h>
46#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070047#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080048
Mathias Agopiancde87a32012-09-13 14:09:01 -070049#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050#include <utils/String8.h>
51#include <utils/String16.h>
52#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080053#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
Mathias Agopian921e6ac2012-07-23 23:11:29 -070055#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070056#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
Mathias Agopian3e25fd82013-04-22 17:52:16 +020058#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020060#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080061#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070062#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070063#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070064#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080065#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#include "LayerDim.h"
Wonsik Kimf837c932014-02-24 14:20:35 +090068#include "LayerVideoPlane.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070
Mathias Agopiana4912602012-07-12 14:25:33 -070071#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070072#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070073#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074
Mathias Agopianff2ed702013-09-01 21:36:12 -070075#include "Effects/Daltonizer.h"
76
Mathias Agopian875d8e12013-06-07 15:35:48 -070077#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070078#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070079
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080#define DISPLAY_COUNT 1
81
Mathias Agopianfee2b462013-07-03 12:34:01 -070082/*
83 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
84 * black pixels.
85 */
86#define DEBUG_SCREENSHOTS false
87
Mathias Agopianca088332013-03-28 17:44:13 -070088EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
89
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070091
92// This works around the lack of support for the sync framework on some
93// devices.
94#ifdef RUNNING_WITHOUT_SYNC_FRAMEWORK
95static const bool runningWithoutSyncFramework = true;
96#else
97static const bool runningWithoutSyncFramework = false;
98#endif
99
100// This is the phase offset in nanoseconds of the software vsync event
101// relative to the vsync event reported by HWComposer. The software vsync
102// event is when SurfaceFlinger and Choreographer-based applications run each
103// frame.
104//
105// This phase offset allows adjustment of the minimum latency from application
106// wake-up (by Choregographer) time to the time at which the resulting window
107// image is displayed. This value may be either positive (after the HW vsync)
108// or negative (before the HW vsync). Setting it to 0 will result in a
109// minimum latency of two vsync periods because the app and SurfaceFlinger
110// will run just after the HW vsync. Setting it to a positive number will
111// result in the minimum latency being:
112//
113// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
114//
115// Note that reducing this latency makes it more likely for the applications
116// to not have their window content image ready in time. When this happens
117// the latency will end up being an additional vsync period, and animations
118// will hiccup. Therefore, this latency should be tuned somewhat
119// conservatively (or at least with awareness of the trade-off being made).
120static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
121
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700122// This is the phase offset at which SurfaceFlinger's composition runs.
123static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
124
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125// ---------------------------------------------------------------------------
126
Mathias Agopian99b49842011-06-27 16:05:52 -0700127const String16 sHardwareTest("android.permission.HARDWARE_TEST");
128const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
129const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
130const String16 sDump("android.permission.DUMP");
131
132// ---------------------------------------------------------------------------
133
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700135 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700137 mTransactionPending(false),
138 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700139 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700140 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700141 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700144 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800145 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700147 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700148 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700149 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700150 mDebugInSwapBuffers(0),
151 mLastSwapBufferTime(0),
152 mDebugInTransaction(0),
153 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700154 mBootFinished(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700155 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700156 mHWVsyncAvailable(false),
Alan Viverette9c5a3332013-09-12 20:04:35 -0700157 mDaltonize(false),
158 mHasColorMatrix(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159{
Steve Blocka19954a2012-01-04 20:05:49 +0000160 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161
162 // debugging stuff...
163 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700164
Mathias Agopianb4b17302013-03-20 18:36:41 -0700165 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700166 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700167
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 property_get("debug.sf.showupdates", value, "0");
169 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700170
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700171 property_get("debug.sf.ddms", value, "0");
172 mDebugDDMS = atoi(value);
173 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700174 if (!startDdmConnection()) {
175 // start failed, and DDMS debugging not enabled
176 mDebugDDMS = 0;
177 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700178 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700179 ALOGI_IF(mDebugRegion, "showupdates enabled");
180 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181}
182
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800183void SurfaceFlinger::onFirstRef()
184{
185 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800186}
187
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188SurfaceFlinger::~SurfaceFlinger()
189{
Mathias Agopiana4912602012-07-12 14:25:33 -0700190 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
191 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
192 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193}
194
Dan Stozac7014012014-02-14 15:03:43 -0800195void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800196{
197 // the window manager died on us. prepare its eulogy.
198
Andy McFadden13a082e2012-08-24 10:16:42 -0700199 // restore initial conditions (default device unblank, etc)
200 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800201
202 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700203 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800204}
205
Mathias Agopian7e27f052010-05-28 14:22:23 -0700206sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207{
Mathias Agopian96f08192010-06-02 23:28:45 -0700208 sp<ISurfaceComposerClient> bclient;
209 sp<Client> client(new Client(this));
210 status_t err = client->initCheck();
211 if (err == NO_ERROR) {
212 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800214 return bclient;
215}
216
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700217sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
218 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700219{
220 class DisplayToken : public BBinder {
221 sp<SurfaceFlinger> flinger;
222 virtual ~DisplayToken() {
223 // no more references, this display must be terminated
224 Mutex::Autolock _l(flinger->mStateLock);
225 flinger->mCurrentState.displays.removeItem(this);
226 flinger->setTransactionFlags(eDisplayTransactionNeeded);
227 }
228 public:
229 DisplayToken(const sp<SurfaceFlinger>& flinger)
230 : flinger(flinger) {
231 }
232 };
233
234 sp<BBinder> token = new DisplayToken(this);
235
236 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700237 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700238 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700239 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700240 mCurrentState.displays.add(token, info);
241
242 return token;
243}
244
Jesse Hall6c913be2013-08-08 12:15:49 -0700245void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
246 Mutex::Autolock _l(mStateLock);
247
248 ssize_t idx = mCurrentState.displays.indexOfKey(display);
249 if (idx < 0) {
250 ALOGW("destroyDisplay: invalid display token");
251 return;
252 }
253
254 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
255 if (!info.isVirtualDisplay()) {
256 ALOGE("destroyDisplay called for non-virtual display");
257 return;
258 }
259
260 mCurrentState.displays.removeItemsAt(idx);
261 setTransactionFlags(eDisplayTransactionNeeded);
262}
263
Jesse Hall692c7232012-11-08 15:41:56 -0800264void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
265 ALOGW_IF(mBuiltinDisplays[type],
266 "Overwriting display token for display type %d", type);
267 mBuiltinDisplays[type] = new BBinder();
268 DisplayDeviceState info(type);
269 // All non-virtual displays are currently considered secure.
270 info.isSecure = true;
271 mCurrentState.displays.add(mBuiltinDisplays[type], info);
272}
273
Mathias Agopiane57f2922012-08-09 16:29:12 -0700274sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700275 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700276 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
277 return NULL;
278 }
Jesse Hall692c7232012-11-08 15:41:56 -0800279 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700280}
281
Jamie Gennis9a78c902011-01-12 18:30:40 -0800282sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
283{
284 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
285 return gba;
286}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700287
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800288void SurfaceFlinger::bootFinished()
289{
290 const nsecs_t now = systemTime();
291 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000292 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700293 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700294
295 // wait patiently for the window manager death
296 const String16 name("window");
297 sp<IBinder> window(defaultServiceManager()->getService(name));
298 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700299 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700300 }
301
302 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700303 // formerly we would just kill the process, but we now ask it to exit so it
304 // can choose where to stop the animation.
305 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800306}
307
Mathias Agopian3f844832013-08-07 21:24:32 -0700308void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700309 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700310 RenderEngine& engine;
311 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700312 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700313 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
314 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700315 }
316 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700317 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700318 return true;
319 }
320 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700321 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700322}
323
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700324class DispSyncSource : public VSyncSource, private DispSync::Callback {
325public:
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700326 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync) :
327 mValue(0),
328 mPhaseOffset(phaseOffset),
329 mTraceVsync(traceVsync),
330 mDispSync(dispSync) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700331
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700332 virtual ~DispSyncSource() {}
333
334 virtual void setVSyncEnabled(bool enable) {
335 // Do NOT lock the mutex here so as to avoid any mutex ordering issues
336 // with locking it in the onDispSyncEvent callback.
337 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700338 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700339 static_cast<DispSync::Callback*>(this));
340 if (err != NO_ERROR) {
341 ALOGE("error registering vsync callback: %s (%d)",
342 strerror(-err), err);
343 }
344 ATRACE_INT("VsyncOn", 1);
345 } else {
346 status_t err = mDispSync->removeEventListener(
347 static_cast<DispSync::Callback*>(this));
348 if (err != NO_ERROR) {
349 ALOGE("error unregistering vsync callback: %s (%d)",
350 strerror(-err), err);
351 }
352 ATRACE_INT("VsyncOn", 0);
353 }
354 }
355
356 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
357 Mutex::Autolock lock(mMutex);
358 mCallback = callback;
359 }
360
361private:
362 virtual void onDispSyncEvent(nsecs_t when) {
363 sp<VSyncSource::Callback> callback;
364 {
365 Mutex::Autolock lock(mMutex);
366 callback = mCallback;
367
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700368 if (mTraceVsync) {
369 mValue = (mValue + 1) % 2;
370 ATRACE_INT("VSYNC", mValue);
371 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700372 }
373
374 if (callback != NULL) {
375 callback->onVSyncEvent(when);
376 }
377 }
378
379 int mValue;
380
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700381 const nsecs_t mPhaseOffset;
382 const bool mTraceVsync;
383
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700384 DispSync* mDispSync;
385 sp<VSyncSource::Callback> mCallback;
386 Mutex mMutex;
387};
388
389void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700390 ALOGI( "SurfaceFlinger's main thread ready to run. "
391 "Initializing graphics H/W...");
392
Jesse Hallb65f32e2013-09-27 22:10:47 -0700393 status_t err;
Jesse Hall692c7232012-11-08 15:41:56 -0800394 Mutex::Autolock _l(mStateLock);
395
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700396 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700397 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
398 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700399
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700400 // Initialize the H/W composer object. There may or may not be an
401 // actual hardware composer underneath.
402 mHwc = new HWComposer(this,
403 *static_cast<HWComposer::EventHandler *>(this));
404
Mathias Agopian875d8e12013-06-07 15:35:48 -0700405 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800406 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700407
408 // retrieve the EGL context that was selected/created
409 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700410
Mathias Agopianda27af92012-09-13 18:17:13 -0700411 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
412 "couldn't create EGLContext");
413
Mathias Agopiancde87a32012-09-13 14:09:01 -0700414 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700415 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700416 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700417 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700418 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700419 // All non-virtual displays are currently considered secure.
420 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800421 createBuiltinDisplayLocked(type);
422 wp<IBinder> token = mBuiltinDisplays[i];
423
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700424 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
425 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i, bq);
Jesse Hall19e87292013-12-23 21:02:15 -0800426 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700427 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800428 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700429 fbs, bq,
Jesse Hall05f8c702013-12-23 20:44:38 -0800430 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700431 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700432 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700433 // for displays other than the main display, so we always
434 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800435 ALOGD("marking display %zu as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700436 hw->acquireScreen();
437 }
438 mDisplays.add(token, hw);
439 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700440 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700441
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700442 // make the GLContext current so that we can create textures when creating Layers
443 // (which may happens before we render something)
444 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
445
Mathias Agopian028508c2012-07-25 21:12:12 -0700446 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700447 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
448 vsyncPhaseOffsetNs, true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700449 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700450 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
451 sfVsyncPhaseOffsetNs, false);
452 mSFEventThread = new EventThread(sfVsyncSrc);
453 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700454
Jamie Gennisd1700752013-10-14 12:22:52 -0700455 mEventControlThread = new EventControlThread(this);
456 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
457
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700458 // set a fake vsync period if there is no HWComposer
459 if (mHwc->initCheck() != NO_ERROR) {
460 mPrimaryDispSync.setPeriod(16666667);
461 }
462
Mathias Agopian92a979a2012-08-02 18:32:23 -0700463 // initialize our drawing state
464 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700465
Andy McFadden13a082e2012-08-24 10:16:42 -0700466 // set initial conditions (e.g. unblank default device)
467 initializeDisplays();
468
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700469 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700470 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800471}
472
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700473int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700474 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700475 type : mHwc->allocateDisplayId();
476}
477
Mathias Agopiana67e4182012-06-19 17:26:12 -0700478void SurfaceFlinger::startBootAnim() {
479 // start boot animation
480 property_set("service.bootanim.exit", "0");
481 property_set("ctl.start", "bootanim");
482}
483
Mathias Agopian875d8e12013-06-07 15:35:48 -0700484size_t SurfaceFlinger::getMaxTextureSize() const {
485 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700486}
487
Mathias Agopian875d8e12013-06-07 15:35:48 -0700488size_t SurfaceFlinger::getMaxViewportDims() const {
489 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700490}
491
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800492// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800493
Jamie Gennis582270d2011-08-17 18:19:00 -0700494bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800495 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800496 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800497 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700498 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800499}
500
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700501status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800502 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700503 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800504 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700505 type = i;
506 break;
507 }
508 }
509
510 if (type < 0) {
511 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700512 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700513
514 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700515 float xdpi = hwc.getDpiX(type);
516 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700517
518 // TODO: Not sure if display density should handled by SF any longer
519 class Density {
520 static int getDensityFromProperty(char const* propName) {
521 char property[PROPERTY_VALUE_MAX];
522 int density = 0;
523 if (property_get(propName, property, NULL) > 0) {
524 density = atoi(property);
525 }
526 return density;
527 }
528 public:
529 static int getEmuDensity() {
530 return getDensityFromProperty("qemu.sf.lcd_density"); }
531 static int getBuildDensity() {
532 return getDensityFromProperty("ro.sf.lcd_density"); }
533 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700534
535 if (type == DisplayDevice::DISPLAY_PRIMARY) {
536 // The density of the device is provided by a build property
537 float density = Density::getBuildDensity() / 160.0f;
538 if (density == 0) {
539 // the build doesn't provide a density -- this is wrong!
540 // use xdpi instead
541 ALOGE("ro.sf.lcd_density must be defined as a build property");
542 density = xdpi / 160.0f;
543 }
544 if (Density::getEmuDensity()) {
545 // if "qemu.sf.lcd_density" is specified, it overrides everything
546 xdpi = ydpi = density = Density::getEmuDensity();
547 density /= 160.0f;
548 }
549 info->density = density;
550
551 // TODO: this needs to go away (currently needed only by webkit)
552 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
553 info->orientation = hw->getOrientation();
Mathias Agopian1604f772012-09-18 21:54:42 -0700554 } else {
555 // TODO: where should this value come from?
556 static const int TV_DENSITY = 213;
557 info->density = TV_DENSITY / 160.0f;
558 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700559 }
560
Mathias Agopian1604f772012-09-18 21:54:42 -0700561 info->w = hwc.getWidth(type);
562 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700563 info->xdpi = xdpi;
564 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700565 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700566
567 // All non-virtual displays are currently considered secure.
568 info->secure = true;
569
Mathias Agopian888c8222012-08-04 21:10:38 -0700570 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700571}
572
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800573// ----------------------------------------------------------------------------
574
575sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800576 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700577}
578
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800579// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800580
581void SurfaceFlinger::waitForEvent() {
582 mEventQueue.waitMessage();
583}
584
585void SurfaceFlinger::signalTransaction() {
586 mEventQueue.invalidate();
587}
588
589void SurfaceFlinger::signalLayerUpdate() {
590 mEventQueue.invalidate();
591}
592
593void SurfaceFlinger::signalRefresh() {
594 mEventQueue.refresh();
595}
596
597status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800598 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800599 return mEventQueue.postMessage(msg, reltime);
600}
601
602status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800603 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800604 status_t res = mEventQueue.postMessage(msg, reltime);
605 if (res == NO_ERROR) {
606 msg->wait();
607 }
608 return res;
609}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800610
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700611void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700612 do {
613 waitForEvent();
614 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615}
616
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700617void SurfaceFlinger::enableHardwareVsync() {
618 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700619 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700620 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700621 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
622 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700623 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700624 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700625}
626
Jesse Hall948fe0c2013-10-14 12:56:09 -0700627void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700628 Mutex::Autolock _l(mHWVsyncLock);
629
Jesse Hall948fe0c2013-10-14 12:56:09 -0700630 if (makeAvailable) {
631 mHWVsyncAvailable = true;
632 } else if (!mHWVsyncAvailable) {
633 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
634 return;
635 }
636
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700637 const nsecs_t period =
638 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
639
640 mPrimaryDispSync.reset();
641 mPrimaryDispSync.setPeriod(period);
642
643 if (!mPrimaryHWVsyncEnabled) {
644 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700645 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
646 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700647 mPrimaryHWVsyncEnabled = true;
648 }
649}
650
Jesse Hall948fe0c2013-10-14 12:56:09 -0700651void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700652 Mutex::Autolock _l(mHWVsyncLock);
653 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700654 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
655 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700656 mPrimaryDispSync.endResync();
657 mPrimaryHWVsyncEnabled = false;
658 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700659 if (makeUnavailable) {
660 mHWVsyncAvailable = false;
661 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700662}
663
664void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700665 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700666
Jamie Gennisd1700752013-10-14 12:22:52 -0700667 { // Scope for the lock
668 Mutex::Autolock _l(mHWVsyncLock);
669 if (type == 0 && mPrimaryHWVsyncEnabled) {
670 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700671 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700672 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700673
674 if (needsHwVsync) {
675 enableHardwareVsync();
676 } else {
677 disableHardwareVsync(false);
678 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700679}
680
681void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
682 if (mEventThread == NULL) {
683 // This is a temporary workaround for b/7145521. A non-null pointer
684 // does not mean EventThread has finished initializing, so this
685 // is not a correct fix.
686 ALOGW("WARNING: EventThread not started, ignoring hotplug");
687 return;
688 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700689
Jesse Hall9e663de2013-08-16 14:28:37 -0700690 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700691 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800692 if (connected) {
693 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700694 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800695 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
696 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700697 }
698 setTransactionFlags(eDisplayTransactionNeeded);
699
Andy McFadden9e9689c2012-10-10 18:17:51 -0700700 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700701 }
Mathias Agopian86303202012-07-24 22:46:10 -0700702}
703
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700704void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700705 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700706 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700707}
708
Mathias Agopian4fec8732012-06-29 14:12:52 -0700709void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800710 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800711 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700712 case MessageQueue::TRANSACTION:
713 handleMessageTransaction();
714 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700715 case MessageQueue::INVALIDATE:
716 handleMessageTransaction();
717 handleMessageInvalidate();
718 signalRefresh();
719 break;
720 case MessageQueue::REFRESH:
721 handleMessageRefresh();
722 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800723 }
724}
725
Mathias Agopian4fec8732012-06-29 14:12:52 -0700726void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700727 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700728 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700729 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700730 }
731}
732
733void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700734 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700735 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700736}
737
738void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700739 ATRACE_CALL();
740 preComposition();
741 rebuildLayerStacks();
742 setUpHWComposer();
743 doDebugFlashRegions();
744 doComposition();
745 postComposition();
746}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700747
Mathias Agopiancd60f992012-08-16 16:28:27 -0700748void SurfaceFlinger::doDebugFlashRegions()
749{
750 // is debugging enabled
751 if (CC_LIKELY(!mDebugRegion))
752 return;
753
754 const bool repaintEverything = mRepaintEverything;
755 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
756 const sp<DisplayDevice>& hw(mDisplays[dpy]);
757 if (hw->canDraw()) {
758 // transform the dirty region into this screen's coordinate space
759 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
760 if (!dirtyRegion.isEmpty()) {
761 // redraw the whole screen
762 doComposeSurfaces(hw, Region(hw->bounds()));
763
764 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700765 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700766 RenderEngine& engine(getRenderEngine());
767 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
768
Mathias Agopiancd60f992012-08-16 16:28:27 -0700769 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700770 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700771 }
772 }
773 }
774
775 postFramebuffer();
776
777 if (mDebugRegion > 1) {
778 usleep(mDebugRegion * 1000);
779 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700780
781 HWComposer& hwc(getHwComposer());
782 if (hwc.initCheck() == NO_ERROR) {
783 status_t err = hwc.prepare();
784 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
785 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700786}
787
788void SurfaceFlinger::preComposition()
789{
790 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700791 const LayerVector& layers(mDrawingState.layersSortedByZ);
792 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700793 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700794 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700795 needExtraInvalidate = true;
796 }
797 }
798 if (needExtraInvalidate) {
799 signalLayerUpdate();
800 }
801}
802
803void SurfaceFlinger::postComposition()
804{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700805 const LayerVector& layers(mDrawingState.layersSortedByZ);
806 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700807 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700808 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700809 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800810
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700811 const HWComposer& hwc = getHwComposer();
812 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
813
814 if (presentFence->isValid()) {
815 if (mPrimaryDispSync.addPresentFence(presentFence)) {
816 enableHardwareVsync();
817 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700818 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700819 }
820 }
821
822 if (runningWithoutSyncFramework) {
823 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
824 if (hw->isScreenAcquired()) {
825 enableHardwareVsync();
826 }
827 }
828
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800829 if (mAnimCompositionPending) {
830 mAnimCompositionPending = false;
831
Jesse Halla9a1b002013-02-27 16:39:25 -0800832 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800833 mAnimFrameTracker.setActualPresentFence(presentFence);
834 } else {
835 // The HWC doesn't support present fences, so use the refresh
836 // timestamp instead.
837 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
838 mAnimFrameTracker.setActualPresentTime(presentTime);
839 }
840 mAnimFrameTracker.advanceFrame();
841 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700842}
843
844void SurfaceFlinger::rebuildLayerStacks() {
845 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700846 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700847 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700848 mVisibleRegionsDirty = false;
849 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700850
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700851 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700852 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700853 Region opaqueRegion;
854 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800855 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700856 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700857 const Transform& tr(hw->getTransform());
858 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700859 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700860 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700861 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700862
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700863 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700864 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700865 const sp<Layer>& layer(layers[i]);
866 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700867 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700868 Region drawRegion(tr.transform(
869 layer->visibleNonTransparentRegion));
870 drawRegion.andSelf(bounds);
871 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700872 layersSortedByZ.add(layer);
873 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700874 }
875 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700876 }
Mathias Agopian42977342012-08-05 00:40:46 -0700877 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700878 hw->undefinedRegion.set(bounds);
879 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
880 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700881 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700882 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700883}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700884
Mathias Agopiancd60f992012-08-16 16:28:27 -0700885void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700886 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Dan Stoza71433162014-02-04 16:22:36 -0800887 bool mustRecompose =
888 !(mDisplays[dpy]->getDirtyRegion(false).isEmpty());
889 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700890 }
891
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700892 HWComposer& hwc(getHwComposer());
893 if (hwc.initCheck() == NO_ERROR) {
894 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700895 if (CC_UNLIKELY(mHwWorkListDirty)) {
896 mHwWorkListDirty = false;
897 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
898 sp<const DisplayDevice> hw(mDisplays[dpy]);
899 const int32_t id = hw->getHwcDisplayId();
900 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800901 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700902 hw->getVisibleLayersSortedByZ());
903 const size_t count = currentLayers.size();
904 if (hwc.createWorkList(id, count) == NO_ERROR) {
905 HWComposer::LayerListIterator cur = hwc.begin(id);
906 const HWComposer::LayerListIterator end = hwc.end(id);
907 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800908 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700909 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -0700910 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -0700911 cur->setSkip(true);
912 }
913 }
914 }
915 }
916 }
917 }
918
919 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700920 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700921 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700922 const int32_t id = hw->getHwcDisplayId();
923 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800924 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700925 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700926 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700927 HWComposer::LayerListIterator cur = hwc.begin(id);
928 const HWComposer::LayerListIterator end = hwc.end(id);
929 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
930 /*
931 * update the per-frame h/w composer data for each layer
932 * and build the transparent region of the FB
933 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800934 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700935 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700936 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700937 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700938 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700939
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700940 status_t err = hwc.prepare();
941 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700942
943 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
944 sp<const DisplayDevice> hw(mDisplays[dpy]);
945 hw->prepareFrame(hwc);
946 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700947 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700948}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700949
Mathias Agopiancd60f992012-08-16 16:28:27 -0700950void SurfaceFlinger::doComposition() {
951 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700952 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700953 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700954 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700955 if (hw->canDraw()) {
956 // transform the dirty region into this screen's coordinate space
957 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800958
959 // repaint the framebuffer (if needed)
960 doDisplayComposition(hw, dirtyRegion);
961
Mathias Agopiancd60f992012-08-16 16:28:27 -0700962 hw->dirtyRegion.clear();
963 hw->flip(hw->swapRegion);
964 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700965 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700966 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700967 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700968 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700969 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700970}
971
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800972void SurfaceFlinger::postFramebuffer()
973{
Mathias Agopian841cde52012-03-01 15:44:37 -0800974 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800975
Mathias Agopiana44b0412011-10-16 18:46:35 -0700976 const nsecs_t now = systemTime();
977 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700978
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700979 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700980 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700981 if (!hwc.supportsFramebufferTarget()) {
982 // EGL spec says:
983 // "surface must be bound to the calling thread's current context,
984 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700985 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700986 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700987 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700988 }
989
Mathias Agopian6da15f42013-09-25 20:40:07 -0700990 // make the default display current because the VirtualDisplayDevice code cannot
991 // deal with dequeueBuffer() being called outside of the composition loop; however
992 // the code below can call glFlush() which is allowed (and does in some case) call
993 // dequeueBuffer().
994 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
995
Mathias Agopian92a979a2012-08-02 18:32:23 -0700996 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700997 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800998 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700999 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001000 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001001 int32_t id = hw->getHwcDisplayId();
1002 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001003 HWComposer::LayerListIterator cur = hwc.begin(id);
1004 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001005 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001006 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001007 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001008 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001009 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001010 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001011 }
Jesse Hallef194142012-06-14 14:45:17 -07001012 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001013 }
1014
Mathias Agopiana44b0412011-10-16 18:46:35 -07001015 mLastSwapBufferTime = systemTime() - now;
1016 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001017
1018 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1019 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1020 logFrameStats();
1021 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022}
1023
Mathias Agopian87baae12012-07-31 12:38:26 -07001024void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001025{
Mathias Agopian841cde52012-03-01 15:44:37 -08001026 ATRACE_CALL();
1027
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001028 // here we keep a copy of the drawing state (that is the state that's
1029 // going to be overwritten by handleTransactionLocked()) outside of
1030 // mStateLock so that the side-effects of the State assignment
1031 // don't happen with mStateLock held (which can cause deadlocks).
1032 State drawingState(mDrawingState);
1033
Mathias Agopianca4d3602011-05-19 15:38:14 -07001034 Mutex::Autolock _l(mStateLock);
1035 const nsecs_t now = systemTime();
1036 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037
Mathias Agopianca4d3602011-05-19 15:38:14 -07001038 // Here we're guaranteed that some transaction flags are set
1039 // so we can call handleTransactionLocked() unconditionally.
1040 // We call getTransactionFlags(), which will also clear the flags,
1041 // with mStateLock held to guarantee that mCurrentState won't change
1042 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001043
Mathias Agopiane57f2922012-08-09 16:29:12 -07001044 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001045 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001046
Mathias Agopianca4d3602011-05-19 15:38:14 -07001047 mLastTransactionTime = systemTime() - now;
1048 mDebugInTransaction = 0;
1049 invalidateHwcGeometry();
1050 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001051}
1052
Mathias Agopian87baae12012-07-31 12:38:26 -07001053void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001054{
1055 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001056 const size_t count = currentLayers.size();
1057
1058 /*
1059 * Traversal of the children
1060 * (perform the transaction for each of them if needed)
1061 */
1062
Mathias Agopian3559b072012-08-15 13:46:03 -07001063 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001065 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001066 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1067 if (!trFlags) continue;
1068
1069 const uint32_t flags = layer->doTransaction(0);
1070 if (flags & Layer::eVisibleRegion)
1071 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072 }
1073 }
1074
1075 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001076 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077 */
1078
Mathias Agopiane57f2922012-08-09 16:29:12 -07001079 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001080 // here we take advantage of Vector's copy-on-write semantics to
1081 // improve performance by skipping the transaction entirely when
1082 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001083 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1084 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001085 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001086 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001087 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001088 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001089
1090 // find the displays that were removed
1091 // (ie: in drawing state but not in current state)
1092 // also handle displays that changed
1093 // (ie: displays that are in both lists)
1094 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001095 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1096 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001097 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001098 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001099 // Call makeCurrent() on the primary display so we can
1100 // be sure that nothing associated with this display
1101 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001102 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001103 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001104 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1105 if (hw != NULL)
1106 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001107 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001108 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001109 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001110 } else {
1111 ALOGW("trying to remove the main display");
1112 }
1113 } else {
1114 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001115 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001116 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001117 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001118 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001119 // recreating the DisplayDevice, so we just remove it
1120 // from the drawing state, so that it get re-added
1121 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001122 sp<DisplayDevice> hw(getDisplayDevice(display));
1123 if (hw != NULL)
1124 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001125 mDisplays.removeItem(display);
1126 mDrawingState.displays.removeItemsAt(i);
1127 dc--; i--;
1128 // at this point we must loop to the next item
1129 continue;
1130 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001131
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001132 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001133 if (disp != NULL) {
1134 if (state.layerStack != draw[i].layerStack) {
1135 disp->setLayerStack(state.layerStack);
1136 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001137 if ((state.orientation != draw[i].orientation)
1138 || (state.viewport != draw[i].viewport)
1139 || (state.frame != draw[i].frame))
1140 {
1141 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001142 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001143 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001144 }
1145 }
1146 }
1147
1148 // find displays that were added
1149 // (ie: in current state but not in drawing state)
1150 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001151 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001152 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001153
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001154 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001155 sp<IGraphicBufferProducer> producer;
1156 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
1157
Jesse Hall02d86562013-03-25 14:43:23 -07001158 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001159 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001160 // Virtual displays without a surface are dormant:
1161 // they have external state (layer stack, projection,
1162 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001163 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001164
Jesse Hall02d86562013-03-25 14:43:23 -07001165 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001166 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1167 *mHwc, hwcDisplayId, state.surface, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001168 state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001169
1170 dispSurface = vds;
1171 if (hwcDisplayId >= 0) {
1172 producer = vds;
1173 } else {
1174 // There won't be any interaction with HWC for this virtual display,
1175 // so the GLES driver can pass buffers directly to the sink.
1176 producer = state.surface;
1177 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001178 }
1179 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001180 ALOGE_IF(state.surface!=NULL,
1181 "adding a supported display, but rendering "
1182 "surface is provided (%p), ignoring it",
1183 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001184 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001185 // for supported (by hwc) displays we provide our
1186 // own rendering surface
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001187 dispSurface = new FramebufferSurface(*mHwc, state.type, bq);
1188 producer = bq;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001189 }
1190
1191 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001192 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001193 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001194 state.type, hwcDisplayId,
1195 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001196 display, dispSurface, producer,
1197 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001198 hw->setLayerStack(state.layerStack);
1199 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001200 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001201 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001202 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001203 if (state.isVirtualDisplay()) {
1204 if (hwcDisplayId >= 0) {
1205 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1206 hw->getWidth(), hw->getHeight(),
1207 hw->getFormat());
1208 }
1209 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001210 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001211 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001212 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001213 }
1214 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001215 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001216 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001217
Mathias Agopian84300952012-11-21 16:02:13 -08001218 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1219 // The transform hint might have changed for some layers
1220 // (either because a display has changed, or because a layer
1221 // as changed).
1222 //
1223 // Walk through all the layers in currentLayers,
1224 // and update their transform hint.
1225 //
1226 // If a layer is visible only on a single display, then that
1227 // display is used to calculate the hint, otherwise we use the
1228 // default display.
1229 //
1230 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1231 // the hint is set before we acquire a buffer from the surface texture.
1232 //
1233 // NOTE: layer transactions have taken place already, so we use their
1234 // drawing state. However, SurfaceFlinger's own transaction has not
1235 // happened yet, so we must use the current state layer list
1236 // (soon to become the drawing state list).
1237 //
1238 sp<const DisplayDevice> disp;
1239 uint32_t currentlayerStack = 0;
1240 for (size_t i=0; i<count; i++) {
1241 // NOTE: we rely on the fact that layers are sorted by
1242 // layerStack first (so we don't have to traverse the list
1243 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001244 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001245 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001246 if (i==0 || currentlayerStack != layerStack) {
1247 currentlayerStack = layerStack;
1248 // figure out if this layerstack is mirrored
1249 // (more than one display) if so, pick the default display,
1250 // if not, pick the only display it's on.
1251 disp.clear();
1252 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1253 sp<const DisplayDevice> hw(mDisplays[dpy]);
1254 if (hw->getLayerStack() == currentlayerStack) {
1255 if (disp == NULL) {
1256 disp = hw;
1257 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001258 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001259 break;
1260 }
1261 }
1262 }
1263 }
Chet Haase91d25932013-04-11 15:24:55 -07001264 if (disp == NULL) {
1265 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1266 // redraw after transform hint changes. See bug 8508397.
1267
1268 // could be null when this layer is using a layerStack
1269 // that is not visible on any display. Also can occur at
1270 // screen off/on times.
1271 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001272 }
Chet Haase91d25932013-04-11 15:24:55 -07001273 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001274 }
1275 }
1276
1277
Mathias Agopian3559b072012-08-15 13:46:03 -07001278 /*
1279 * Perform our own transaction if needed
1280 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001281
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001282 const LayerVector& layers(mDrawingState.layersSortedByZ);
1283 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001284 // layers have been added
1285 mVisibleRegionsDirty = true;
1286 }
1287
1288 // some layers might have been removed, so
1289 // we need to update the regions they're exposing.
1290 if (mLayersRemoved) {
1291 mLayersRemoved = false;
1292 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001293 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001294 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001295 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001296 if (currentLayers.indexOf(layer) < 0) {
1297 // this layer is not visible anymore
1298 // TODO: we could traverse the tree from front to back and
1299 // compute the actual visible region
1300 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001301 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001302 Region visibleReg = s.transform.transform(
1303 Region(Rect(s.active.w, s.active.h)));
1304 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001305 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307 }
1308
1309 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001310}
1311
1312void SurfaceFlinger::commitTransaction()
1313{
1314 if (!mLayersPendingRemoval.isEmpty()) {
1315 // Notify removed layers now that they can't be drawn from
1316 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1317 mLayersPendingRemoval[i]->onRemoved();
1318 }
1319 mLayersPendingRemoval.clear();
1320 }
1321
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001322 // If this transaction is part of a window animation then the next frame
1323 // we composite should be considered an animation as well.
1324 mAnimCompositionPending = mAnimTransactionPending;
1325
Mathias Agopian4fec8732012-06-29 14:12:52 -07001326 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001327 mTransactionPending = false;
1328 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001329 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001330}
1331
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001332void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001333 const LayerVector& currentLayers, uint32_t layerStack,
1334 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001335{
Mathias Agopian841cde52012-03-01 15:44:37 -08001336 ATRACE_CALL();
1337
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001338 Region aboveOpaqueLayers;
1339 Region aboveCoveredLayers;
1340 Region dirty;
1341
Mathias Agopian87baae12012-07-31 12:38:26 -07001342 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001343
1344 size_t i = currentLayers.size();
1345 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001346 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001347
1348 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001349 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350
Jesse Hall01e29052013-02-19 16:13:35 -08001351 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001352 if (s.layerStack != layerStack)
1353 continue;
1354
Mathias Agopianab028732010-03-16 16:41:46 -07001355 /*
1356 * opaqueRegion: area of a surface that is fully opaque.
1357 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001358 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001359
1360 /*
1361 * visibleRegion: area of a surface that is visible on screen
1362 * and not fully transparent. This is essentially the layer's
1363 * footprint minus the opaque regions above it.
1364 * Areas covered by a translucent surface are considered visible.
1365 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001366 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001367
1368 /*
1369 * coveredRegion: area of a surface that is covered by all
1370 * visible regions above it (which includes the translucent areas).
1371 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001372 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001373
Jesse Halla8026d22012-09-25 13:25:04 -07001374 /*
1375 * transparentRegion: area of a surface that is hinted to be completely
1376 * transparent. This is only used to tell when the layer has no visible
1377 * non-transparent regions and can be removed from the layer list. It
1378 * does not affect the visibleRegion of this layer or any layers
1379 * beneath it. The hint may not be correct if apps don't respect the
1380 * SurfaceView restrictions (which, sadly, some don't).
1381 */
1382 Region transparentRegion;
1383
Mathias Agopianab028732010-03-16 16:41:46 -07001384
1385 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001386 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001387 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001388 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001389 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001390 if (!visibleRegion.isEmpty()) {
1391 // Remove the transparent area from the visible region
1392 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001393 const Transform tr(s.transform);
1394 if (tr.transformed()) {
1395 if (tr.preserveRects()) {
1396 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001397 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001398 } else {
1399 // transformation too complex, can't do the
1400 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001401 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001402 }
1403 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001404 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001405 }
Mathias Agopianab028732010-03-16 16:41:46 -07001406 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001407
Mathias Agopianab028732010-03-16 16:41:46 -07001408 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001409 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001410 if (s.alpha==255 && !translucent &&
1411 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1412 // the opaque region is the layer's footprint
1413 opaqueRegion = visibleRegion;
1414 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415 }
1416 }
1417
Mathias Agopianab028732010-03-16 16:41:46 -07001418 // Clip the covered region to the visible region
1419 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1420
1421 // Update aboveCoveredLayers for next (lower) layer
1422 aboveCoveredLayers.orSelf(visibleRegion);
1423
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424 // subtract the opaque region covered by the layers above us
1425 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426
1427 // compute this layer's dirty region
1428 if (layer->contentDirty) {
1429 // we need to invalidate the whole region
1430 dirty = visibleRegion;
1431 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001432 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001433 layer->contentDirty = false;
1434 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001435 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001436 * the exposed region consists of two components:
1437 * 1) what's VISIBLE now and was COVERED before
1438 * 2) what's EXPOSED now less what was EXPOSED before
1439 *
1440 * note that (1) is conservative, we start with the whole
1441 * visible region but only keep what used to be covered by
1442 * something -- which mean it may have been exposed.
1443 *
1444 * (2) handles areas that were not covered by anything but got
1445 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001446 */
Mathias Agopianab028732010-03-16 16:41:46 -07001447 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001448 const Region oldVisibleRegion = layer->visibleRegion;
1449 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001450 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1451 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001452 }
1453 dirty.subtractSelf(aboveOpaqueLayers);
1454
1455 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001456 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001457
Mathias Agopianab028732010-03-16 16:41:46 -07001458 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001459 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001460
Jesse Halla8026d22012-09-25 13:25:04 -07001461 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001462 layer->setVisibleRegion(visibleRegion);
1463 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001464 layer->setVisibleNonTransparentRegion(
1465 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001466 }
1467
Mathias Agopian87baae12012-07-31 12:38:26 -07001468 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001469}
1470
Mathias Agopian87baae12012-07-31 12:38:26 -07001471void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1472 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001473 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001474 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1475 if (hw->getLayerStack() == layerStack) {
1476 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001477 }
1478 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001479}
1480
1481void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001482{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001483 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001484
Mathias Agopian4fec8732012-06-29 14:12:52 -07001485 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001486 const LayerVector& layers(mDrawingState.layersSortedByZ);
1487 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001488 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001489 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001490 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001491 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001492 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001493 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001494
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001495 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001496}
1497
Mathias Agopianad456f92011-01-13 17:53:01 -08001498void SurfaceFlinger::invalidateHwcGeometry()
1499{
1500 mHwWorkListDirty = true;
1501}
1502
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001503
Mathias Agopiancd60f992012-08-16 16:28:27 -07001504void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001505 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001506{
Dan Stoza71433162014-02-04 16:22:36 -08001507 // We only need to actually compose the display if:
1508 // 1) It is being handled by hardware composer, which may need this to
1509 // keep its virtual display state machine in sync, or
1510 // 2) There is work to be done (the dirty region isn't empty)
1511 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1512 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1513 return;
1514 }
1515
Mathias Agopian87baae12012-07-31 12:38:26 -07001516 Region dirtyRegion(inDirtyRegion);
1517
Mathias Agopianb8a55602009-06-26 19:06:36 -07001518 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001519 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001520
Mathias Agopian42977342012-08-05 00:40:46 -07001521 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001522 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001523 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1524 // takes a rectangle, we must make sure to update that whole
1525 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001526 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001527 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001528 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001529 // We need to redraw the rectangle that will be updated
1530 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001531 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001532 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001533 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001534 } else {
1535 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001536 dirtyRegion.set(hw->bounds());
1537 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001538 }
1539 }
1540
Alan Viverette9c5a3332013-09-12 20:04:35 -07001541 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Mathias Agopianff2ed702013-09-01 21:36:12 -07001542 doComposeSurfaces(hw, dirtyRegion);
1543 } else {
1544 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001545 mat4 colorMatrix = mColorMatrix;
1546 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001547 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001548 }
1549 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001550 doComposeSurfaces(hw, dirtyRegion);
1551 engine.endGroup();
1552 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001553
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001554 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001555 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001556
1557 // swap buffers (presentation)
1558 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001559}
1560
Mathias Agopiancd60f992012-08-16 16:28:27 -07001561void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001562{
Mathias Agopian3f844832013-08-07 21:24:32 -07001563 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001564 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001565 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001566 HWComposer::LayerListIterator cur = hwc.begin(id);
1567 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001568
Jesse Halld05a17f2013-09-30 16:49:30 -07001569 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001570 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001571 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001572 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1573 hw->getDisplayName().string());
1574 return;
1575 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001576
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001577 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001578 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001579 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001580 // when using overlays, we assume a fully transparent framebuffer
1581 // NOTE: we could reduce how much we need to clear, for instance
1582 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001583 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001584 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001585 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001586 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001587 // we start with the whole screen area
1588 const Region bounds(hw->getBounds());
1589
1590 // we remove the scissor part
1591 // we're left with the letterbox region
1592 // (common case is that letterbox ends-up being empty)
1593 const Region letterbox(bounds.subtract(hw->getScissor()));
1594
1595 // compute the area to clear
1596 Region region(hw->undefinedRegion.merge(letterbox));
1597
1598 // but limit it to the dirty region
1599 region.andSelf(dirty);
1600
Mathias Agopianb9494d52012-04-18 02:28:45 -07001601 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001602 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001603 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001604 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001605 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001606 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001607
Mathias Agopian766dc492012-10-30 18:08:06 -07001608 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1609 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001610 // scissor on the main display. It should never be needed
1611 // anyways (though in theory it could since the API allows it).
1612 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001613 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001614 if (scissor != bounds) {
1615 // scissor doesn't match the screen's dimensions, so we
1616 // need to clear everything outside of it and enable
1617 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001618
Mathias Agopianf45c5102012-10-24 16:29:17 -07001619 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001620 const uint32_t height = hw->getHeight();
1621 engine.setScissor(scissor.left, height - scissor.bottom,
1622 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001623 }
1624 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001625 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001626
Mathias Agopian85d751c2012-08-29 16:59:24 -07001627 /*
1628 * and then, render the layers targeted at the framebuffer
1629 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001630
Mathias Agopian13127d82013-03-05 17:47:11 -08001631 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001632 const size_t count = layers.size();
1633 const Transform& tr = hw->getTransform();
1634 if (cur != end) {
1635 // we're using h/w composer
1636 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001637 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001638 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001639 if (!clip.isEmpty()) {
1640 switch (cur->getCompositionType()) {
1641 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001642 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001643 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1644 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001645 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001646 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001647 // never clear the very first layer since we're
1648 // guaranteed the FB is already cleared
1649 layer->clearWithOpenGL(hw, clip);
1650 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001651 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001652 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001653 case HWC_FRAMEBUFFER: {
1654 layer->draw(hw, clip);
1655 break;
1656 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001657 case HWC_FRAMEBUFFER_TARGET: {
1658 // this should not happen as the iterator shouldn't
1659 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001660 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001661 break;
1662 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001663 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001664 }
1665 layer->setAcquireFence(hw, *cur);
1666 }
1667 } else {
1668 // we're not using h/w composer
1669 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001670 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001671 const Region clip(dirty.intersect(
1672 tr.transform(layer->visibleRegion)));
1673 if (!clip.isEmpty()) {
1674 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001675 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001676 }
1677 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001678
1679 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001680 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001681}
1682
Mathias Agopian3f844832013-08-07 21:24:32 -07001683void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001684 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001685 RenderEngine& engine(getRenderEngine());
1686 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687}
1688
Mathias Agopianac9fa422013-02-11 16:40:36 -08001689void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1690 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001691 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001692 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001693{
Mathias Agopian96f08192010-06-02 23:28:45 -07001694 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001695 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001696
Mathias Agopian96f08192010-06-02 23:28:45 -07001697 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001698 Mutex::Autolock _l(mStateLock);
1699 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001700 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001701}
1702
Mathias Agopian3f844832013-08-07 21:24:32 -07001703status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001704 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001705 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001706 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001707 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001708 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001709 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001710 return NO_ERROR;
1711 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001712 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001713}
1714
Dan Stozac7014012014-02-14 15:03:43 -08001715uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001716 return android_atomic_release_load(&mTransactionFlags);
1717}
1718
Mathias Agopian3f844832013-08-07 21:24:32 -07001719uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001720 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1721}
1722
Mathias Agopian3f844832013-08-07 21:24:32 -07001723uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001724 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1725 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001726 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001727 }
1728 return old;
1729}
1730
Mathias Agopian8b33f032012-07-24 20:43:54 -07001731void SurfaceFlinger::setTransactionState(
1732 const Vector<ComposerState>& state,
1733 const Vector<DisplayState>& displays,
1734 uint32_t flags)
1735{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001736 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001737 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001738 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001739
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001740 if (flags & eAnimation) {
1741 // For window updates that are part of an animation we must wait for
1742 // previous animation "frames" to be handled.
1743 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001744 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001745 if (CC_UNLIKELY(err != NO_ERROR)) {
1746 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001747 // caller after a few seconds.
1748 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1749 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001750 mAnimTransactionPending = false;
1751 break;
1752 }
1753 }
1754 }
1755
Mathias Agopiane57f2922012-08-09 16:29:12 -07001756 size_t count = displays.size();
1757 for (size_t i=0 ; i<count ; i++) {
1758 const DisplayState& s(displays[i]);
1759 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001760 }
1761
Mathias Agopiane57f2922012-08-09 16:29:12 -07001762 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001763 for (size_t i=0 ; i<count ; i++) {
1764 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001765 // Here we need to check that the interface we're given is indeed
1766 // one of our own. A malicious client could give us a NULL
1767 // IInterface, or one of its own or even one of our own but a
1768 // different type. All these situations would cause us to crash.
1769 //
1770 // NOTE: it would be better to use RTTI as we could directly check
1771 // that we have a Client*. however, RTTI is disabled in Android.
1772 if (s.client != NULL) {
1773 sp<IBinder> binder = s.client->asBinder();
1774 if (binder != NULL) {
1775 String16 desc(binder->getInterfaceDescriptor());
1776 if (desc == ISurfaceComposerClient::descriptor) {
1777 sp<Client> client( static_cast<Client *>(s.client.get()) );
1778 transactionFlags |= setClientStateLocked(client, s.state);
1779 }
1780 }
1781 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001782 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001783
Mathias Agopian386aa982011-11-07 21:58:03 -08001784 if (transactionFlags) {
1785 // this triggers the transaction
1786 setTransactionFlags(transactionFlags);
1787
1788 // if this is a synchronous transaction, wait for it to take effect
1789 // before returning.
1790 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001791 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001792 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001793 if (flags & eAnimation) {
1794 mAnimTransactionPending = true;
1795 }
1796 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001797 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1798 if (CC_UNLIKELY(err != NO_ERROR)) {
1799 // just in case something goes wrong in SF, return to the
1800 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001801 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1802 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001803 break;
1804 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001805 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001806 }
1807}
1808
Mathias Agopiane57f2922012-08-09 16:29:12 -07001809uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1810{
Jesse Hall9a143922012-10-04 16:29:19 -07001811 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1812 if (dpyIdx < 0)
1813 return 0;
1814
Mathias Agopiane57f2922012-08-09 16:29:12 -07001815 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001816 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001817 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001818 const uint32_t what = s.what;
1819 if (what & DisplayState::eSurfaceChanged) {
1820 if (disp.surface->asBinder() != s.surface->asBinder()) {
1821 disp.surface = s.surface;
1822 flags |= eDisplayTransactionNeeded;
1823 }
1824 }
1825 if (what & DisplayState::eLayerStackChanged) {
1826 if (disp.layerStack != s.layerStack) {
1827 disp.layerStack = s.layerStack;
1828 flags |= eDisplayTransactionNeeded;
1829 }
1830 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001831 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001832 if (disp.orientation != s.orientation) {
1833 disp.orientation = s.orientation;
1834 flags |= eDisplayTransactionNeeded;
1835 }
1836 if (disp.frame != s.frame) {
1837 disp.frame = s.frame;
1838 flags |= eDisplayTransactionNeeded;
1839 }
1840 if (disp.viewport != s.viewport) {
1841 disp.viewport = s.viewport;
1842 flags |= eDisplayTransactionNeeded;
1843 }
1844 }
1845 }
1846 return flags;
1847}
1848
1849uint32_t SurfaceFlinger::setClientStateLocked(
1850 const sp<Client>& client,
1851 const layer_state_t& s)
1852{
1853 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001854 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001855 if (layer != 0) {
1856 const uint32_t what = s.what;
1857 if (what & layer_state_t::ePositionChanged) {
1858 if (layer->setPosition(s.x, s.y))
1859 flags |= eTraversalNeeded;
1860 }
1861 if (what & layer_state_t::eLayerChanged) {
1862 // NOTE: index needs to be calculated before we update the state
1863 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1864 if (layer->setLayer(s.z)) {
1865 mCurrentState.layersSortedByZ.removeAt(idx);
1866 mCurrentState.layersSortedByZ.add(layer);
1867 // we need traversal (state changed)
1868 // AND transaction (list changed)
1869 flags |= eTransactionNeeded|eTraversalNeeded;
1870 }
1871 }
1872 if (what & layer_state_t::eSizeChanged) {
1873 if (layer->setSize(s.w, s.h)) {
1874 flags |= eTraversalNeeded;
1875 }
1876 }
1877 if (what & layer_state_t::eAlphaChanged) {
1878 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1879 flags |= eTraversalNeeded;
1880 }
1881 if (what & layer_state_t::eMatrixChanged) {
1882 if (layer->setMatrix(s.matrix))
1883 flags |= eTraversalNeeded;
1884 }
1885 if (what & layer_state_t::eTransparentRegionChanged) {
1886 if (layer->setTransparentRegionHint(s.transparentRegion))
1887 flags |= eTraversalNeeded;
1888 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08001889 if ((what & layer_state_t::eVisibilityChanged) ||
1890 (what & layer_state_t::eOpacityChanged)) {
1891 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07001892 if (layer->setFlags(s.flags, s.mask))
1893 flags |= eTraversalNeeded;
1894 }
1895 if (what & layer_state_t::eCropChanged) {
1896 if (layer->setCrop(s.crop))
1897 flags |= eTraversalNeeded;
1898 }
1899 if (what & layer_state_t::eLayerStackChanged) {
1900 // NOTE: index needs to be calculated before we update the state
1901 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1902 if (layer->setLayerStack(s.layerStack)) {
1903 mCurrentState.layersSortedByZ.removeAt(idx);
1904 mCurrentState.layersSortedByZ.add(layer);
1905 // we need traversal (state changed)
1906 // AND transaction (list changed)
1907 flags |= eTransactionNeeded|eTraversalNeeded;
1908 }
1909 }
1910 }
1911 return flags;
1912}
1913
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001914status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001915 const String8& name,
1916 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001917 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1918 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001919{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001920 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001921 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001922 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001923 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001924 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001925 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001926
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001927 status_t result = NO_ERROR;
1928
1929 sp<Layer> layer;
1930
Mathias Agopian3165cc22012-08-08 19:42:09 -07001931 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1932 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001933 result = createNormalLayer(client,
1934 name, w, h, flags, format,
1935 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001936 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001937 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001938 result = createDimLayer(client,
1939 name, w, h, flags,
1940 handle, gbp, &layer);
1941 break;
Wonsik Kimf837c932014-02-24 14:20:35 +09001942 case ISurfaceComposerClient::eFXSurfaceVideoPlane:
1943 result = createVideoPlaneLayer(client,
1944 name, w, h, flags,
1945 handle, gbp, &layer);
1946 break;
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001947 default:
1948 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001949 break;
1950 }
1951
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001952 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001953 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001954 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001955 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001956 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001957}
1958
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001959status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1960 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1961 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001962{
1963 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001964 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001965 case PIXEL_FORMAT_TRANSPARENT:
1966 case PIXEL_FORMAT_TRANSLUCENT:
1967 format = PIXEL_FORMAT_RGBA_8888;
1968 break;
1969 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001970#ifdef NO_RGBX_8888
1971 format = PIXEL_FORMAT_RGB_565;
1972#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001973 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001974#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001975 break;
1976 }
1977
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001978#ifdef NO_RGBX_8888
1979 if (format == PIXEL_FORMAT_RGBX_8888)
1980 format = PIXEL_FORMAT_RGBA_8888;
1981#endif
1982
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001983 *outLayer = new Layer(this, client, name, w, h, flags);
1984 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1985 if (err == NO_ERROR) {
1986 *handle = (*outLayer)->getHandle();
1987 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001988 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001989
1990 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1991 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001992}
1993
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001994status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1995 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1996 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001997{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001998 *outLayer = new LayerDim(this, client, name, w, h, flags);
1999 *handle = (*outLayer)->getHandle();
2000 *gbp = (*outLayer)->getBufferQueue();
2001 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002002}
2003
Wonsik Kimf837c932014-02-24 14:20:35 +09002004status_t SurfaceFlinger::createVideoPlaneLayer(const sp<Client>& client,
2005 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2006 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
2007{
2008 *outLayer = new LayerVideoPlane(this, client, name, w, h, flags);
2009 *handle = (*outLayer)->getHandle();
2010 *gbp = (*outLayer)->getBufferQueue();
2011 return NO_ERROR;
2012}
2013
Mathias Agopianac9fa422013-02-11 16:40:36 -08002014status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002015{
Mathias Agopian67106042013-03-14 19:18:13 -07002016 // called by the window manager when it wants to remove a Layer
2017 status_t err = NO_ERROR;
2018 sp<Layer> l(client->getLayerUser(handle));
2019 if (l != NULL) {
2020 err = removeLayer(l);
2021 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2022 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002023 }
2024 return err;
2025}
2026
Mathias Agopian13127d82013-03-05 17:47:11 -08002027status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002028{
Mathias Agopian67106042013-03-14 19:18:13 -07002029 // called by ~LayerCleaner() when all references to the IBinder (handle)
2030 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002031 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002032 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002033 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002034 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002035 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002036 "error removing layer=%p (%s)", l.get(), strerror(-err));
2037 }
2038 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002039}
2040
Mathias Agopianb60314a2012-04-10 22:09:54 -07002041// ---------------------------------------------------------------------------
2042
Andy McFadden13a082e2012-08-24 10:16:42 -07002043void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002044 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002045 Vector<ComposerState> state;
2046 Vector<DisplayState> displays;
2047 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002048 d.what = DisplayState::eDisplayProjectionChanged |
2049 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002050 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002051 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002052 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002053 d.frame.makeInvalid();
2054 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002055 displays.add(d);
2056 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002057 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07002058
2059 const nsecs_t period =
2060 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2061 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002062}
2063
2064void SurfaceFlinger::initializeDisplays() {
2065 class MessageScreenInitialized : public MessageBase {
2066 SurfaceFlinger* flinger;
2067 public:
2068 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2069 virtual bool handler() {
2070 flinger->onInitializeDisplays();
2071 return true;
2072 }
2073 };
2074 sp<MessageBase> msg = new MessageScreenInitialized(this);
2075 postMessageAsync(msg); // we may be called from main thread, use async message
2076}
2077
2078
Mathias Agopiancde87a32012-09-13 14:09:01 -07002079void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002080 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2081 if (hw->isScreenAcquired()) {
2082 // this is expected, e.g. when power manager wakes up during boot
2083 ALOGD(" screen was previously acquired");
2084 return;
2085 }
2086
Mathias Agopian42977342012-08-05 00:40:46 -07002087 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002088 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002089 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002090 // built-in display, tell the HWC
2091 getHwComposer().acquire(type);
2092
2093 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2094 // FIXME: eventthread only knows about the main display right now
2095 mEventThread->onScreenAcquired();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07002096
Jesse Hall948fe0c2013-10-14 12:56:09 -07002097 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002098 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002099 }
Mathias Agopian20128302012-08-13 18:32:13 -07002100 mVisibleRegionsDirty = true;
2101 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002102}
2103
Mathias Agopiancde87a32012-09-13 14:09:01 -07002104void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002105 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2106 if (!hw->isScreenAcquired()) {
2107 ALOGD(" screen was previously released");
2108 return;
2109 }
2110
2111 hw->releaseScreen();
2112 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002113 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002114 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002115 disableHardwareVsync(true); // also cancels any in-progress resync
2116
Mathias Agopiancde87a32012-09-13 14:09:01 -07002117 // FIXME: eventthread only knows about the main display right now
2118 mEventThread->onScreenReleased();
2119 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002120
2121 // built-in display, tell the HWC
2122 getHwComposer().release(type);
2123 }
2124 mVisibleRegionsDirty = true;
2125 // from this point on, SF will stop drawing on this display
2126}
2127
2128void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2129 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002130 SurfaceFlinger& mFlinger;
2131 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002132 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002133 MessageScreenAcquired(SurfaceFlinger& flinger,
2134 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002135 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002136 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2137 if (hw == NULL) {
2138 ALOGE("Attempt to unblank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002139 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002140 ALOGW("Attempt to unblank virtual display");
2141 } else {
2142 mFlinger.onScreenAcquired(hw);
2143 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002144 return true;
2145 }
2146 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002147 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2148 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002149}
2150
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002151void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002152 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002153 SurfaceFlinger& mFlinger;
2154 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002155 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002156 MessageScreenReleased(SurfaceFlinger& flinger,
2157 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002158 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002159 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2160 if (hw == NULL) {
2161 ALOGE("Attempt to blank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002162 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002163 ALOGW("Attempt to blank virtual display");
2164 } else {
2165 mFlinger.onScreenReleased(hw);
2166 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002167 return true;
2168 }
2169 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002170 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2171 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002172}
2173
2174// ---------------------------------------------------------------------------
2175
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002176status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2177{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002178 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002179
Mathias Agopianbd115332013-04-18 16:41:04 -07002180 IPCThreadState* ipc = IPCThreadState::self();
2181 const int pid = ipc->getCallingPid();
2182 const int uid = ipc->getCallingUid();
2183 if ((uid != AID_SHELL) &&
2184 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002185 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002186 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002187 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002188 // Try to get the main lock, but don't insist if we can't
2189 // (this would indicate SF is stuck, but we want to be able to
2190 // print something in dumpsys).
2191 int retry = 3;
2192 while (mStateLock.tryLock()<0 && --retry>=0) {
2193 usleep(1000000);
2194 }
2195 const bool locked(retry >= 0);
2196 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002197 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002198 "SurfaceFlinger appears to be unresponsive, "
2199 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002200 }
2201
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002202 bool dumpAll = true;
2203 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002204 size_t numArgs = args.size();
2205 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002206 if ((index < numArgs) &&
2207 (args[index] == String16("--list"))) {
2208 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002209 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002210 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002211 }
2212
2213 if ((index < numArgs) &&
2214 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002215 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002216 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002217 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002218 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002219
2220 if ((index < numArgs) &&
2221 (args[index] == String16("--latency-clear"))) {
2222 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002223 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002224 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002225 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002226 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002227
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002228 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002229 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002230 }
2231
Mathias Agopian9795c422009-08-26 16:36:26 -07002232 if (locked) {
2233 mStateLock.unlock();
2234 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002235 }
2236 write(fd, result.string(), result.size());
2237 return NO_ERROR;
2238}
2239
Dan Stozac7014012014-02-14 15:03:43 -08002240void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2241 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002242{
2243 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2244 const size_t count = currentLayers.size();
2245 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002246 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002247 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002248 }
2249}
2250
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002251void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002252 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002253{
2254 String8 name;
2255 if (index < args.size()) {
2256 name = String8(args[index]);
2257 index++;
2258 }
2259
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002260 const nsecs_t period =
2261 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002262 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002263
2264 if (name.isEmpty()) {
2265 mAnimFrameTracker.dump(result);
2266 } else {
2267 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2268 const size_t count = currentLayers.size();
2269 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002270 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002271 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002272 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002273 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002274 }
2275 }
2276}
2277
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002278void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002279 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002280{
2281 String8 name;
2282 if (index < args.size()) {
2283 name = String8(args[index]);
2284 index++;
2285 }
2286
2287 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2288 const size_t count = currentLayers.size();
2289 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002290 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002291 if (name.isEmpty() || (name == layer->getName())) {
2292 layer->clearStats();
2293 }
2294 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002295
2296 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002297}
2298
Jamie Gennis6547ff42013-07-16 20:12:42 -07002299// This should only be called from the main thread. Otherwise it would need
2300// the lock and should use mCurrentState rather than mDrawingState.
2301void SurfaceFlinger::logFrameStats() {
2302 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2303 const size_t count = drawingLayers.size();
2304 for (size_t i=0 ; i<count ; i++) {
2305 const sp<Layer>& layer(drawingLayers[i]);
2306 layer->logFrameStats();
2307 }
2308
2309 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2310}
2311
Andy McFadden4803b742012-09-24 19:07:20 -07002312/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2313{
2314 static const char* config =
2315 " [sf"
2316#ifdef NO_RGBX_8888
2317 " NO_RGBX_8888"
2318#endif
2319#ifdef HAS_CONTEXT_PRIORITY
2320 " HAS_CONTEXT_PRIORITY"
2321#endif
2322#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2323 " NEVER_DEFAULT_TO_ASYNC_MODE"
2324#endif
2325#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2326 " TARGET_DISABLE_TRIPLE_BUFFERING"
2327#endif
2328 "]";
2329 result.append(config);
2330}
2331
Mathias Agopian74d211a2013-04-22 16:55:35 +02002332void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2333 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002334{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002335 bool colorize = false;
2336 if (index < args.size()
2337 && (args[index] == String16("--color"))) {
2338 colorize = true;
2339 index++;
2340 }
2341
2342 Colorizer colorizer(colorize);
2343
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002344 // figure out if we're stuck somewhere
2345 const nsecs_t now = systemTime();
2346 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2347 const nsecs_t inTransaction(mDebugInTransaction);
2348 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2349 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2350
2351 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002352 * Dump library configuration.
2353 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002354
2355 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002356 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002357 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002358 appendSfConfigString(result);
2359 appendUiConfigString(result);
2360 appendGuiConfigString(result);
2361 result.append("\n");
2362
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002363 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002364 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002365 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002366 result.append(SyncFeatures::getInstance().toString());
2367 result.append("\n");
2368
Andy McFadden4803b742012-09-24 19:07:20 -07002369 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002370 * Dump the visible layer list
2371 */
2372 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2373 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002374 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002375 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002376 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002377 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002378 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002379 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002380 }
2381
2382 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002383 * Dump Display state
2384 */
2385
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002386 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002387 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002388 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002389 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2390 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002391 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002392 }
2393
2394 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002395 * Dump SurfaceFlinger global state
2396 */
2397
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002398 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002399 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002400 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002401
Mathias Agopian888c8222012-08-04 21:10:38 -07002402 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002403 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002404
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002405 colorizer.bold(result);
2406 result.appendFormat("EGL implementation : %s\n",
2407 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2408 colorizer.reset(result);
2409 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002410 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002411
Mathias Agopian875d8e12013-06-07 15:35:48 -07002412 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002413
Mathias Agopian42977342012-08-05 00:40:46 -07002414 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002415 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002416 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002417 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002418 " last eglSwapBuffers() time: %f us\n"
2419 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002420 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002421 " refresh-rate : %f fps\n"
2422 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002423 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002424 " gpu_to_cpu_unsupported : %d\n"
2425 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002426 mLastSwapBufferTime/1000.0,
2427 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002428 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002429 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2430 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002431 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002432 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002433
Mathias Agopian74d211a2013-04-22 16:55:35 +02002434 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002435 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002436
Mathias Agopian74d211a2013-04-22 16:55:35 +02002437 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002438 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002439
2440 /*
2441 * VSYNC state
2442 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002443 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002444
2445 /*
2446 * Dump HWComposer state
2447 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002448 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002449 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002450 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002451 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002452 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002453 (mDebugDisableHWC || mDebugRegion || mDaltonize
2454 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002455 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002456
2457 /*
2458 * Dump gralloc state
2459 */
2460 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2461 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002462}
2463
Mathias Agopian13127d82013-03-05 17:47:11 -08002464const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002465SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002466 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002467 wp<IBinder> dpy;
2468 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2469 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2470 dpy = mDisplays.keyAt(i);
2471 break;
2472 }
2473 }
2474 if (dpy == NULL) {
2475 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2476 // Just use the primary display so we have something to return
2477 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2478 }
2479 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002480}
2481
Keun young Park63f165f2012-08-31 10:53:36 -07002482bool SurfaceFlinger::startDdmConnection()
2483{
2484 void* libddmconnection_dso =
2485 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2486 if (!libddmconnection_dso) {
2487 return false;
2488 }
2489 void (*DdmConnection_start)(const char* name);
2490 DdmConnection_start =
2491 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2492 if (!DdmConnection_start) {
2493 dlclose(libddmconnection_dso);
2494 return false;
2495 }
2496 (*DdmConnection_start)(getServiceName());
2497 return true;
2498}
2499
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002500status_t SurfaceFlinger::onTransact(
2501 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2502{
2503 switch (code) {
2504 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002505 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002506 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002507 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002508 case BLANK:
2509 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002510 {
2511 // codes that require permission check
2512 IPCThreadState* ipc = IPCThreadState::self();
2513 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002514 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002515 if ((uid != AID_GRAPHICS) &&
2516 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002517 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002518 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2519 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002520 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002521 break;
2522 }
2523 case CAPTURE_SCREEN:
2524 {
2525 // codes that require permission check
2526 IPCThreadState* ipc = IPCThreadState::self();
2527 const int pid = ipc->getCallingPid();
2528 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002529 if ((uid != AID_GRAPHICS) &&
2530 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002531 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002532 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2533 return PERMISSION_DENIED;
2534 }
2535 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002536 }
2537 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002538
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002539 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2540 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002541 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002542 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002543 IPCThreadState* ipc = IPCThreadState::self();
2544 const int pid = ipc->getCallingPid();
2545 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002546 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002547 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002548 return PERMISSION_DENIED;
2549 }
2550 int n;
2551 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002552 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002553 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002554 return NO_ERROR;
2555 case 1002: // SHOW_UPDATES
2556 n = data.readInt32();
2557 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002558 invalidateHwcGeometry();
2559 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002560 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002561 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002562 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002563 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002564 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002565 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002566 setTransactionFlags(
2567 eTransactionNeeded|
2568 eDisplayTransactionNeeded|
2569 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002570 return NO_ERROR;
2571 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002572 case 1006:{ // send empty update
2573 signalRefresh();
2574 return NO_ERROR;
2575 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002576 case 1008: // toggle use of hw composer
2577 n = data.readInt32();
2578 mDebugDisableHWC = n ? 1 : 0;
2579 invalidateHwcGeometry();
2580 repaintEverything();
2581 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002582 case 1009: // toggle use of transform hint
2583 n = data.readInt32();
2584 mDebugDisableTransformHint = n ? 1 : 0;
2585 invalidateHwcGeometry();
2586 repaintEverything();
2587 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002588 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002589 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002590 reply->writeInt32(0);
2591 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002592 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002593 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002594 return NO_ERROR;
2595 case 1013: {
2596 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002597 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2598 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002599 return NO_ERROR;
2600 }
2601 case 1014: {
2602 // daltonize
2603 n = data.readInt32();
2604 switch (n % 10) {
2605 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2606 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2607 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2608 }
2609 if (n >= 10) {
2610 mDaltonizer.setMode(Daltonizer::correction);
2611 } else {
2612 mDaltonizer.setMode(Daltonizer::simulation);
2613 }
2614 mDaltonize = n > 0;
2615 invalidateHwcGeometry();
2616 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002617 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002618 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002619 case 1015: {
2620 // apply a color matrix
2621 n = data.readInt32();
2622 mHasColorMatrix = n ? 1 : 0;
2623 if (n) {
2624 // color matrix is sent as mat3 matrix followed by vec3
2625 // offset, then packed into a mat4 where the last row is
2626 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002627 for (size_t i = 0 ; i < 4; i++) {
2628 for (size_t j = 0; j < 4; j++) {
2629 mColorMatrix[i][j] = data.readFloat();
2630 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002631 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002632 } else {
2633 mColorMatrix = mat4();
2634 }
2635 invalidateHwcGeometry();
2636 repaintEverything();
2637 return NO_ERROR;
2638 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002639 }
2640 }
2641 return err;
2642}
2643
Mathias Agopian53331da2011-08-22 21:44:41 -07002644void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002645 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002646 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002647}
2648
Mathias Agopian59119e62010-10-11 12:37:43 -07002649// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002650// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002651// ---------------------------------------------------------------------------
2652
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002653/* The code below is here to handle b/8734824
2654 *
2655 * We create a IGraphicBufferProducer wrapper that forwards all calls
2656 * to the calling binder thread, where they are executed. This allows
2657 * the calling thread to be reused (on the other side) and not
2658 * depend on having "enough" binder threads to handle the requests.
2659 *
2660 */
2661
2662class GraphicProducerWrapper : public BBinder, public MessageHandler {
2663 sp<IGraphicBufferProducer> impl;
2664 sp<Looper> looper;
2665 status_t result;
2666 bool exitPending;
2667 bool exitRequested;
2668 mutable Barrier barrier;
2669 volatile int32_t memoryBarrier;
2670 uint32_t code;
2671 Parcel const* data;
2672 Parcel* reply;
2673
2674 enum {
2675 MSG_API_CALL,
2676 MSG_EXIT
2677 };
2678
2679 /*
2680 * this is called by our "fake" BpGraphicBufferProducer. We package the
2681 * data and reply Parcel and forward them to the calling thread.
2682 */
2683 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08002684 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002685 this->code = code;
2686 this->data = &data;
2687 this->reply = reply;
2688 android_atomic_acquire_store(0, &memoryBarrier);
2689 if (exitPending) {
2690 // if we've exited, we run the message synchronously right here
2691 handleMessage(Message(MSG_API_CALL));
2692 } else {
2693 barrier.close();
2694 looper->sendMessage(this, Message(MSG_API_CALL));
2695 barrier.wait();
2696 }
2697 return NO_ERROR;
2698 }
2699
2700 /*
2701 * here we run on the binder calling thread. All we've got to do is
2702 * call the real BpGraphicBufferProducer.
2703 */
2704 virtual void handleMessage(const Message& message) {
2705 android_atomic_release_load(&memoryBarrier);
2706 if (message.what == MSG_API_CALL) {
2707 impl->asBinder()->transact(code, data[0], reply);
2708 barrier.open();
2709 } else if (message.what == MSG_EXIT) {
2710 exitRequested = true;
2711 }
2712 }
2713
2714public:
2715 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2716 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2717 exitPending(false), exitRequested(false) {
2718 }
2719
2720 status_t waitForResponse() {
2721 do {
2722 looper->pollOnce(-1);
2723 } while (!exitRequested);
2724 return result;
2725 }
2726
2727 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002728 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002729 exitPending = true;
2730 looper->sendMessage(this, Message(MSG_EXIT));
2731 }
2732};
2733
2734
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002735status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2736 const sp<IGraphicBufferProducer>& producer,
2737 uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002738 uint32_t minLayerZ, uint32_t maxLayerZ,
2739 bool useIdentityTransform) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002740
2741 if (CC_UNLIKELY(display == 0))
2742 return BAD_VALUE;
2743
2744 if (CC_UNLIKELY(producer == 0))
2745 return BAD_VALUE;
2746
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002747 // if we have secure windows on this display, never allow the screen capture
2748 // unless the producer interface is local (i.e.: we can take a screenshot for
2749 // ourselves).
2750 if (!producer->asBinder()->localBinder()) {
2751 Mutex::Autolock _l(mStateLock);
2752 sp<const DisplayDevice> hw(getDisplayDevice(display));
2753 if (hw->getSecureLayerVisible()) {
2754 ALOGW("FB is protected: PERMISSION_DENIED");
2755 return PERMISSION_DENIED;
2756 }
2757 }
2758
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002759 class MessageCaptureScreen : public MessageBase {
2760 SurfaceFlinger* flinger;
2761 sp<IBinder> display;
2762 sp<IGraphicBufferProducer> producer;
2763 uint32_t reqWidth, reqHeight;
2764 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08002765 bool useIdentityTransform;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002766 status_t result;
2767 public:
2768 MessageCaptureScreen(SurfaceFlinger* flinger,
2769 const sp<IBinder>& display,
2770 const sp<IGraphicBufferProducer>& producer,
2771 uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002772 uint32_t minLayerZ, uint32_t maxLayerZ,
2773 bool useIdentityTransform)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002774 : flinger(flinger), display(display), producer(producer),
2775 reqWidth(reqWidth), reqHeight(reqHeight),
2776 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08002777 useIdentityTransform(useIdentityTransform),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002778 result(PERMISSION_DENIED)
2779 {
2780 }
2781 status_t getResult() const {
2782 return result;
2783 }
2784 virtual bool handler() {
2785 Mutex::Autolock _l(flinger->mStateLock);
2786 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08002787 result = flinger->captureScreenImplLocked(hw, producer,
2788 reqWidth, reqHeight, minLayerZ, maxLayerZ,
2789 useIdentityTransform);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002790 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002791 return true;
2792 }
2793 };
2794
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002795 // make sure to process transactions before screenshots -- a transaction
2796 // might already be pending but scheduled for VSYNC; this guarantees we
2797 // will handle it before the screenshot. When VSYNC finally arrives
2798 // the scheduled transaction will be a no-op. If no transactions are
2799 // scheduled at this time, this will end-up being a no-op as well.
2800 mEventQueue.invalidateTransactionNow();
2801
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002802 // this creates a "fake" BBinder which will serve as a "fake" remote
2803 // binder to receive the marshaled calls and forward them to the
2804 // real remote (a BpGraphicBufferProducer)
2805 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2806
2807 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2808 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002809 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002810 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac7014012014-02-14 15:03:43 -08002811 reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002812
2813 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002814 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002815 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002816 }
2817 return res;
2818}
2819
Mathias Agopian180f10d2013-04-10 22:55:41 -07002820
2821void SurfaceFlinger::renderScreenImplLocked(
2822 const sp<const DisplayDevice>& hw,
2823 uint32_t reqWidth, uint32_t reqHeight,
2824 uint32_t minLayerZ, uint32_t maxLayerZ,
Dan Stozac7014012014-02-14 15:03:43 -08002825 bool yswap, bool useIdentityTransform)
Mathias Agopian180f10d2013-04-10 22:55:41 -07002826{
2827 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002828 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002829
2830 // get screen geometry
2831 const uint32_t hw_w = hw->getWidth();
2832 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002833 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2834
2835 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002836 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002837
2838 // set-up our viewport
Mathias Agopian3f844832013-08-07 21:24:32 -07002839 engine.setViewportAndProjection(reqWidth, reqHeight, hw_w, hw_h, yswap);
2840 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002841
2842 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002843 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002844
2845 const LayerVector& layers( mDrawingState.layersSortedByZ );
2846 const size_t count = layers.size();
2847 for (size_t i=0 ; i<count ; ++i) {
2848 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002849 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002850 if (state.layerStack == hw->getLayerStack()) {
2851 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2852 if (layer->isVisible()) {
2853 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08002854 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002855 if (filtering) layer->setFiltering(false);
2856 }
2857 }
2858 }
2859 }
2860
2861 // compositionComplete is needed for older driver
2862 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002863 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002864}
2865
2866
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002867status_t SurfaceFlinger::captureScreenImplLocked(
2868 const sp<const DisplayDevice>& hw,
2869 const sp<IGraphicBufferProducer>& producer,
2870 uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002871 uint32_t minLayerZ, uint32_t maxLayerZ,
2872 bool useIdentityTransform)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002873{
2874 ATRACE_CALL();
2875
Mathias Agopian180f10d2013-04-10 22:55:41 -07002876 // get screen geometry
2877 const uint32_t hw_w = hw->getWidth();
2878 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002879
Mathias Agopian180f10d2013-04-10 22:55:41 -07002880 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2881 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2882 reqWidth, reqHeight, hw_w, hw_h);
2883 return BAD_VALUE;
2884 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002885
Mathias Agopian180f10d2013-04-10 22:55:41 -07002886 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2887 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2888
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002889 // create a surface (because we're a producer, and we need to
2890 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07002891 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002892 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002893
2894 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002895 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002896 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2897 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002898
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002899 int err = 0;
2900 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07002901 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002902 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2903 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002904
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002905 if (err == NO_ERROR) {
2906 ANativeWindowBuffer* buffer;
2907 /* TODO: Once we have the sync framework everywhere this can use
2908 * server-side waits on the fence that dequeueBuffer returns.
2909 */
2910 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2911 if (result == NO_ERROR) {
2912 // create an EGLImage from the buffer so we can later
2913 // turn it into a texture
2914 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2915 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2916 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002917 // this binds the given EGLImage as a framebuffer for the
2918 // duration of this scope.
2919 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2920 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002921 // this will in fact render into our dequeued buffer
2922 // via an FBO, which means we didn't have to create
2923 // an EGLSurface and therefore we're not
2924 // dependent on the context's EGLConfig.
2925 renderScreenImplLocked(hw, reqWidth, reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002926 minLayerZ, maxLayerZ, true, useIdentityTransform);
Mathias Agopiand5556842013-09-19 17:08:37 -07002927
Andy McFadden2d8d1202013-10-09 16:38:02 -07002928 // Create a sync point and wait on it, so we know the buffer is
2929 // ready before we pass it along. We can't trivially call glFlush(),
2930 // so we use a wait flag instead.
2931 // TODO: pass a sync fd to queueBuffer() and let the consumer wait.
2932 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
2933 if (sync != EGL_NO_SYNC_KHR) {
2934 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
2935 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
2936 EGLint eglErr = eglGetError();
2937 eglDestroySyncKHR(mEGLDisplay, sync);
2938 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
2939 ALOGW("captureScreen: fence wait timed out");
2940 } else {
2941 ALOGW_IF(eglErr != EGL_SUCCESS,
2942 "captureScreen: error waiting on EGL fence: %#x", eglErr);
2943 }
2944 } else {
2945 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
2946 // not fatal
2947 }
2948
Mathias Agopiand5556842013-09-19 17:08:37 -07002949 if (DEBUG_SCREENSHOTS) {
2950 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
2951 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
2952 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
2953 hw, minLayerZ, maxLayerZ);
2954 delete [] pixels;
2955 }
2956
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002957 } else {
2958 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2959 result = INVALID_OPERATION;
2960 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002961 // destroy our image
2962 eglDestroyImageKHR(mEGLDisplay, image);
2963 } else {
2964 result = BAD_VALUE;
2965 }
2966 window->queueBuffer(window, buffer, -1);
2967 }
2968 } else {
2969 result = BAD_VALUE;
2970 }
2971 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2972 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002973
Mathias Agopian74c40c02010-09-29 13:02:36 -07002974 return result;
2975}
2976
Mathias Agopiand5556842013-09-19 17:08:37 -07002977void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
2978 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07002979 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07002980 for (size_t y=0 ; y<h ; y++) {
2981 uint32_t const * p = (uint32_t const *)vaddr + y*s;
2982 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07002983 if (p[x] != 0xFF000000) return;
2984 }
2985 }
2986 ALOGE("*** we just took a black screenshot ***\n"
2987 "requested minz=%d, maxz=%d, layerStack=%d",
2988 minLayerZ, maxLayerZ, hw->getLayerStack());
2989 const LayerVector& layers( mDrawingState.layersSortedByZ );
2990 const size_t count = layers.size();
2991 for (size_t i=0 ; i<count ; ++i) {
2992 const sp<Layer>& layer(layers[i]);
2993 const Layer::State& state(layer->getDrawingState());
2994 const bool visible = (state.layerStack == hw->getLayerStack())
2995 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2996 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08002997 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07002998 visible ? '+' : '-',
2999 i, layer->getName().string(), state.layerStack, state.z,
3000 layer->isVisible(), state.flags, state.alpha);
3001 }
3002 }
3003}
3004
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003005// ---------------------------------------------------------------------------
3006
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003007SurfaceFlinger::LayerVector::LayerVector() {
3008}
3009
3010SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003011 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003012}
3013
3014int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3015 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003016{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003017 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003018 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3019 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003020
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003021 uint32_t ls = l->getCurrentState().layerStack;
3022 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003023 if (ls != rs)
3024 return ls - rs;
3025
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003026 uint32_t lz = l->getCurrentState().z;
3027 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003028 if (lz != rz)
3029 return lz - rz;
3030
3031 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003032}
3033
3034// ---------------------------------------------------------------------------
3035
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003036SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
3037 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003038}
3039
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003040SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08003041 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003042 viewport.makeInvalid();
3043 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003044}
3045
3046// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003047
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003048}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003049
3050
3051#if defined(__gl_h_)
3052#error "don't include gl/gl.h in this file"
3053#endif
3054
3055#if defined(__gl2_h_)
3056#error "don't include gl2/gl2.h in this file"
3057#endif