blob: ac4a0c24ba382e59cd1724894525727f5773b217 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Greg Hackmann86efcc02014-03-07 12:44:02 -080024#include <inttypes.h>
Jesse Hallb154c422014-07-13 12:47:02 -070025#include <stdatomic.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070026
27#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036
Mathias Agopianc666cae2012-07-25 18:56:13 -070037#include <ui/DisplayInfo.h>
Lajos Molnar67d8bd62014-09-11 14:58:45 -070038#include <ui/DisplayStatInfo.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070039
Mathias Agopian921e6ac2012-07-23 23:11:29 -070040#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070041#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070042#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070043#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080044#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080045#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070046
47#include <ui/GraphicBufferAllocator.h>
48#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070049#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080050
Mathias Agopiancde87a32012-09-13 14:09:01 -070051#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include <utils/String8.h>
53#include <utils/String16.h>
54#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080055#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopian921e6ac2012-07-23 23:11:29 -070057#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070058#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
Mathias Agopian3e25fd82013-04-22 17:52:16 +020060#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020062#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080063#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070064#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070065#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070066#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080067#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071
Mathias Agopiana4912602012-07-12 14:25:33 -070072#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070073#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070074#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075
Mathias Agopianff2ed702013-09-01 21:36:12 -070076#include "Effects/Daltonizer.h"
77
Mathias Agopian875d8e12013-06-07 15:35:48 -070078#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070079#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070080
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081#define DISPLAY_COUNT 1
82
Mathias Agopianfee2b462013-07-03 12:34:01 -070083/*
84 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
85 * black pixels.
86 */
87#define DEBUG_SCREENSHOTS false
88
Mathias Agopianca088332013-03-28 17:44:13 -070089EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
90
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070092
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070093// This is the phase offset in nanoseconds of the software vsync event
94// relative to the vsync event reported by HWComposer. The software vsync
95// event is when SurfaceFlinger and Choreographer-based applications run each
96// frame.
97//
98// This phase offset allows adjustment of the minimum latency from application
99// wake-up (by Choregographer) time to the time at which the resulting window
100// image is displayed. This value may be either positive (after the HW vsync)
101// or negative (before the HW vsync). Setting it to 0 will result in a
102// minimum latency of two vsync periods because the app and SurfaceFlinger
103// will run just after the HW vsync. Setting it to a positive number will
104// result in the minimum latency being:
105//
106// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
107//
108// Note that reducing this latency makes it more likely for the applications
109// to not have their window content image ready in time. When this happens
110// the latency will end up being an additional vsync period, and animations
111// will hiccup. Therefore, this latency should be tuned somewhat
112// conservatively (or at least with awareness of the trade-off being made).
113static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
114
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700115// This is the phase offset at which SurfaceFlinger's composition runs.
116static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
117
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118// ---------------------------------------------------------------------------
119
Mathias Agopian99b49842011-06-27 16:05:52 -0700120const String16 sHardwareTest("android.permission.HARDWARE_TEST");
121const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
122const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
123const String16 sDump("android.permission.DUMP");
124
125// ---------------------------------------------------------------------------
126
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700128 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700130 mTransactionPending(false),
131 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700132 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700133 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700134 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700137 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800138 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700140 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700141 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700142 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700143 mDebugInSwapBuffers(0),
144 mLastSwapBufferTime(0),
145 mDebugInTransaction(0),
146 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700147 mBootFinished(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700148 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700149 mHWVsyncAvailable(false),
Alan Viverette9c5a3332013-09-12 20:04:35 -0700150 mDaltonize(false),
151 mHasColorMatrix(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152{
Steve Blocka19954a2012-01-04 20:05:49 +0000153 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154
155 // debugging stuff...
156 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700157
Mathias Agopianb4b17302013-03-20 18:36:41 -0700158 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700159 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700160
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 property_get("debug.sf.showupdates", value, "0");
162 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700163
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700164 property_get("debug.sf.ddms", value, "0");
165 mDebugDDMS = atoi(value);
166 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700167 if (!startDdmConnection()) {
168 // start failed, and DDMS debugging not enabled
169 mDebugDDMS = 0;
170 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700171 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700172 ALOGI_IF(mDebugRegion, "showupdates enabled");
173 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174}
175
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800176void SurfaceFlinger::onFirstRef()
177{
178 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800179}
180
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181SurfaceFlinger::~SurfaceFlinger()
182{
Mathias Agopiana4912602012-07-12 14:25:33 -0700183 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
184 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
185 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186}
187
Dan Stozac7014012014-02-14 15:03:43 -0800188void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800189{
190 // the window manager died on us. prepare its eulogy.
191
Andy McFadden13a082e2012-08-24 10:16:42 -0700192 // restore initial conditions (default device unblank, etc)
193 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800194
195 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700196 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800197}
198
Mathias Agopian7e27f052010-05-28 14:22:23 -0700199sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200{
Mathias Agopian96f08192010-06-02 23:28:45 -0700201 sp<ISurfaceComposerClient> bclient;
202 sp<Client> client(new Client(this));
203 status_t err = client->initCheck();
204 if (err == NO_ERROR) {
205 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 return bclient;
208}
209
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700210sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
211 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700212{
213 class DisplayToken : public BBinder {
214 sp<SurfaceFlinger> flinger;
215 virtual ~DisplayToken() {
216 // no more references, this display must be terminated
217 Mutex::Autolock _l(flinger->mStateLock);
218 flinger->mCurrentState.displays.removeItem(this);
219 flinger->setTransactionFlags(eDisplayTransactionNeeded);
220 }
221 public:
222 DisplayToken(const sp<SurfaceFlinger>& flinger)
223 : flinger(flinger) {
224 }
225 };
226
227 sp<BBinder> token = new DisplayToken(this);
228
229 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700230 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700231 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700232 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700233 mCurrentState.displays.add(token, info);
234
235 return token;
236}
237
Jesse Hall6c913be2013-08-08 12:15:49 -0700238void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
239 Mutex::Autolock _l(mStateLock);
240
241 ssize_t idx = mCurrentState.displays.indexOfKey(display);
242 if (idx < 0) {
243 ALOGW("destroyDisplay: invalid display token");
244 return;
245 }
246
247 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
248 if (!info.isVirtualDisplay()) {
249 ALOGE("destroyDisplay called for non-virtual display");
250 return;
251 }
252
253 mCurrentState.displays.removeItemsAt(idx);
254 setTransactionFlags(eDisplayTransactionNeeded);
255}
256
Jesse Hall692c7232012-11-08 15:41:56 -0800257void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
258 ALOGW_IF(mBuiltinDisplays[type],
259 "Overwriting display token for display type %d", type);
260 mBuiltinDisplays[type] = new BBinder();
261 DisplayDeviceState info(type);
262 // All non-virtual displays are currently considered secure.
263 info.isSecure = true;
264 mCurrentState.displays.add(mBuiltinDisplays[type], info);
265}
266
Mathias Agopiane57f2922012-08-09 16:29:12 -0700267sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700268 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700269 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
270 return NULL;
271 }
Jesse Hall692c7232012-11-08 15:41:56 -0800272 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700273}
274
Jamie Gennis9a78c902011-01-12 18:30:40 -0800275sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
276{
277 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
278 return gba;
279}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700280
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281void SurfaceFlinger::bootFinished()
282{
283 const nsecs_t now = systemTime();
284 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000285 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700286 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700287
288 // wait patiently for the window manager death
289 const String16 name("window");
290 sp<IBinder> window(defaultServiceManager()->getService(name));
291 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700292 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700293 }
294
295 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700296 // formerly we would just kill the process, but we now ask it to exit so it
297 // can choose where to stop the animation.
298 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299}
300
Mathias Agopian3f844832013-08-07 21:24:32 -0700301void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700302 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700303 RenderEngine& engine;
304 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700305 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700306 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
307 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700308 }
309 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700310 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700311 return true;
312 }
313 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700314 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700315}
316
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700317class DispSyncSource : public VSyncSource, private DispSync::Callback {
318public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700319 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
320 const char* label) :
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700321 mValue(0),
322 mPhaseOffset(phaseOffset),
323 mTraceVsync(traceVsync),
Andy McFadden5167ec62014-05-22 13:08:43 -0700324 mVsyncOnLabel(String8::format("VsyncOn-%s", label)),
325 mVsyncEventLabel(String8::format("VSYNC-%s", label)),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700326 mDispSync(dispSync) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700327
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700328 virtual ~DispSyncSource() {}
329
330 virtual void setVSyncEnabled(bool enable) {
331 // Do NOT lock the mutex here so as to avoid any mutex ordering issues
332 // with locking it in the onDispSyncEvent callback.
333 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700334 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700335 static_cast<DispSync::Callback*>(this));
336 if (err != NO_ERROR) {
337 ALOGE("error registering vsync callback: %s (%d)",
338 strerror(-err), err);
339 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700340 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700341 } else {
342 status_t err = mDispSync->removeEventListener(
343 static_cast<DispSync::Callback*>(this));
344 if (err != NO_ERROR) {
345 ALOGE("error unregistering vsync callback: %s (%d)",
346 strerror(-err), err);
347 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700348 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700349 }
350 }
351
352 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
353 Mutex::Autolock lock(mMutex);
354 mCallback = callback;
355 }
356
357private:
358 virtual void onDispSyncEvent(nsecs_t when) {
359 sp<VSyncSource::Callback> callback;
360 {
361 Mutex::Autolock lock(mMutex);
362 callback = mCallback;
363
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700364 if (mTraceVsync) {
365 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700366 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700367 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700368 }
369
370 if (callback != NULL) {
371 callback->onVSyncEvent(when);
372 }
373 }
374
375 int mValue;
376
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700377 const nsecs_t mPhaseOffset;
378 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700379 const String8 mVsyncOnLabel;
380 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700381
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700382 DispSync* mDispSync;
383 sp<VSyncSource::Callback> mCallback;
384 Mutex mMutex;
385};
386
387void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700388 ALOGI( "SurfaceFlinger's main thread ready to run. "
389 "Initializing graphics H/W...");
390
Jesse Hallb65f32e2013-09-27 22:10:47 -0700391 status_t err;
Jesse Hall692c7232012-11-08 15:41:56 -0800392 Mutex::Autolock _l(mStateLock);
393
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700394 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700395 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
396 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700397
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700398 // Initialize the H/W composer object. There may or may not be an
399 // actual hardware composer underneath.
400 mHwc = new HWComposer(this,
401 *static_cast<HWComposer::EventHandler *>(this));
402
Mathias Agopian875d8e12013-06-07 15:35:48 -0700403 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800404 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700405
406 // retrieve the EGL context that was selected/created
407 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700408
Mathias Agopianda27af92012-09-13 18:17:13 -0700409 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
410 "couldn't create EGLContext");
411
Mathias Agopiancde87a32012-09-13 14:09:01 -0700412 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700413 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700414 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700415 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700416 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700417 // All non-virtual displays are currently considered secure.
418 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800419 createBuiltinDisplayLocked(type);
420 wp<IBinder> token = mBuiltinDisplays[i];
421
Dan Stozab9b08832014-03-13 11:55:57 -0700422 sp<IGraphicBufferProducer> producer;
423 sp<IGraphicBufferConsumer> consumer;
424 BufferQueue::createBufferQueue(&producer, &consumer,
425 new GraphicBufferAlloc());
426
427 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
428 consumer);
Jesse Hall19e87292013-12-23 21:02:15 -0800429 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700430 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800431 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Dan Stozab9b08832014-03-13 11:55:57 -0700432 fbs, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -0800433 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700434 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700435 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700436 // for displays other than the main display, so we always
437 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800438 ALOGD("marking display %zu as acquired/unblanked", i);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700439 hw->setPowerMode(HWC_POWER_MODE_NORMAL);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700440 }
441 mDisplays.add(token, hw);
442 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700443 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700444
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700445 // make the GLContext current so that we can create textures when creating Layers
446 // (which may happens before we render something)
447 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
448
Mathias Agopian028508c2012-07-25 21:12:12 -0700449 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700450 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700451 vsyncPhaseOffsetNs, true, "app");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700452 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700453 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700454 sfVsyncPhaseOffsetNs, true, "sf");
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700455 mSFEventThread = new EventThread(sfVsyncSrc);
456 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700457
Jamie Gennisd1700752013-10-14 12:22:52 -0700458 mEventControlThread = new EventControlThread(this);
459 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
460
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700461 // set a fake vsync period if there is no HWComposer
462 if (mHwc->initCheck() != NO_ERROR) {
463 mPrimaryDispSync.setPeriod(16666667);
464 }
465
Mathias Agopian92a979a2012-08-02 18:32:23 -0700466 // initialize our drawing state
467 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700468
Andy McFadden13a082e2012-08-24 10:16:42 -0700469 // set initial conditions (e.g. unblank default device)
470 initializeDisplays();
471
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700472 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700473 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800474}
475
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700476int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700477 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700478 type : mHwc->allocateDisplayId();
479}
480
Mathias Agopiana67e4182012-06-19 17:26:12 -0700481void SurfaceFlinger::startBootAnim() {
482 // start boot animation
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -0700483 mBootFinished = false;
Mathias Agopiana67e4182012-06-19 17:26:12 -0700484 property_set("service.bootanim.exit", "0");
485 property_set("ctl.start", "bootanim");
486}
487
Mathias Agopian875d8e12013-06-07 15:35:48 -0700488size_t SurfaceFlinger::getMaxTextureSize() const {
489 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700490}
491
Mathias Agopian875d8e12013-06-07 15:35:48 -0700492size_t SurfaceFlinger::getMaxViewportDims() const {
493 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700494}
495
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800496// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800497
Jamie Gennis582270d2011-08-17 18:19:00 -0700498bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800499 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800500 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800501 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700502 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800503}
504
Dan Stoza7f7da322014-05-02 15:26:25 -0700505status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
506 Vector<DisplayInfo>* configs) {
507 if (configs == NULL) {
508 return BAD_VALUE;
509 }
510
Jesse Hall692c7232012-11-08 15:41:56 -0800511 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700512 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800513 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700514 type = i;
515 break;
516 }
517 }
518
519 if (type < 0) {
520 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700521 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700522
Mathias Agopian8b736f12012-08-13 17:54:26 -0700523 // TODO: Not sure if display density should handled by SF any longer
524 class Density {
525 static int getDensityFromProperty(char const* propName) {
526 char property[PROPERTY_VALUE_MAX];
527 int density = 0;
528 if (property_get(propName, property, NULL) > 0) {
529 density = atoi(property);
530 }
531 return density;
532 }
533 public:
534 static int getEmuDensity() {
535 return getDensityFromProperty("qemu.sf.lcd_density"); }
536 static int getBuildDensity() {
537 return getDensityFromProperty("ro.sf.lcd_density"); }
538 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700539
Dan Stoza7f7da322014-05-02 15:26:25 -0700540 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700541
Dan Stoza7f7da322014-05-02 15:26:25 -0700542 const Vector<HWComposer::DisplayConfig>& hwConfigs =
543 getHwComposer().getConfigs(type);
544 for (size_t c = 0; c < hwConfigs.size(); ++c) {
545 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
546 DisplayInfo info = DisplayInfo();
547
548 float xdpi = hwConfig.xdpi;
549 float ydpi = hwConfig.ydpi;
550
551 if (type == DisplayDevice::DISPLAY_PRIMARY) {
552 // The density of the device is provided by a build property
553 float density = Density::getBuildDensity() / 160.0f;
554 if (density == 0) {
555 // the build doesn't provide a density -- this is wrong!
556 // use xdpi instead
557 ALOGE("ro.sf.lcd_density must be defined as a build property");
558 density = xdpi / 160.0f;
559 }
560 if (Density::getEmuDensity()) {
561 // if "qemu.sf.lcd_density" is specified, it overrides everything
562 xdpi = ydpi = density = Density::getEmuDensity();
563 density /= 160.0f;
564 }
565 info.density = density;
566
567 // TODO: this needs to go away (currently needed only by webkit)
568 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
569 info.orientation = hw->getOrientation();
570 } else {
571 // TODO: where should this value come from?
572 static const int TV_DENSITY = 213;
573 info.density = TV_DENSITY / 160.0f;
574 info.orientation = 0;
575 }
576
577 info.w = hwConfig.width;
578 info.h = hwConfig.height;
579 info.xdpi = xdpi;
580 info.ydpi = ydpi;
581 info.fps = float(1e9 / hwConfig.refresh);
Andy McFadden91b2ca82014-06-13 14:04:23 -0700582 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
583
584 // This is how far in advance a buffer must be queued for
585 // presentation at a given time. If you want a buffer to appear
586 // on the screen at time N, you must submit the buffer before
587 // (N - presentationDeadline).
588 //
589 // Normally it's one full refresh period (to give SF a chance to
590 // latch the buffer), but this can be reduced by configuring a
591 // DispSync offset. Any additional delays introduced by the hardware
592 // composer or panel must be accounted for here.
593 //
594 // We add an additional 1ms to allow for processing time and
595 // differences between the ideal and actual refresh rate.
596 info.presentationDeadline =
597 hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700598
599 // All non-virtual displays are currently considered secure.
600 info.secure = true;
601
602 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700603 }
604
Dan Stoza7f7da322014-05-02 15:26:25 -0700605 return NO_ERROR;
606}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700607
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700608status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& display,
609 DisplayStatInfo* stats) {
610 if (stats == NULL) {
611 return BAD_VALUE;
612 }
613
614 // FIXME for now we always return stats for the primary display
615 memset(stats, 0, sizeof(*stats));
616 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
617 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
618 return NO_ERROR;
619}
620
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700621int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
622 return getDisplayDevice(display)->getActiveConfig();
Dan Stoza7f7da322014-05-02 15:26:25 -0700623}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700624
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700625void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
626 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
627 this);
628 int32_t type = hw->getDisplayType();
629 int currentMode = hw->getActiveConfig();
630
631 if (mode == currentMode) {
632 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
633 return;
634 }
635
636 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
637 ALOGW("Trying to set config for virtual display");
638 return;
639 }
640
641 hw->setActiveConfig(mode);
642 getHwComposer().setActiveConfig(type, mode);
643}
644
645status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
646 class MessageSetActiveConfig: public MessageBase {
647 SurfaceFlinger& mFlinger;
648 sp<IBinder> mDisplay;
649 int mMode;
650 public:
651 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
652 int mode) :
653 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
654 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700655 Vector<DisplayInfo> configs;
656 mFlinger.getDisplayConfigs(mDisplay, &configs);
657 if(mMode < 0 || mMode >= configs.size()) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700658 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700659 mMode, configs.size());
660 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700661 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
662 if (hw == NULL) {
663 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700664 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700665 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
666 ALOGW("Attempt to set active config = %d for virtual display",
667 mMode);
668 } else {
669 mFlinger.setActiveConfigInternal(hw, mMode);
670 }
671 return true;
672 }
673 };
674 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
675 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700676 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700677}
678
Svetoslavd85084b2014-03-20 10:28:31 -0700679status_t SurfaceFlinger::clearAnimationFrameStats() {
680 Mutex::Autolock _l(mStateLock);
681 mAnimFrameTracker.clearStats();
682 return NO_ERROR;
683}
684
685status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
686 Mutex::Autolock _l(mStateLock);
687 mAnimFrameTracker.getStats(outStats);
688 return NO_ERROR;
689}
690
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800691// ----------------------------------------------------------------------------
692
693sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800694 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700695}
696
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800697// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800698
699void SurfaceFlinger::waitForEvent() {
700 mEventQueue.waitMessage();
701}
702
703void SurfaceFlinger::signalTransaction() {
704 mEventQueue.invalidate();
705}
706
707void SurfaceFlinger::signalLayerUpdate() {
708 mEventQueue.invalidate();
709}
710
711void SurfaceFlinger::signalRefresh() {
712 mEventQueue.refresh();
713}
714
715status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800716 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800717 return mEventQueue.postMessage(msg, reltime);
718}
719
720status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800721 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800722 status_t res = mEventQueue.postMessage(msg, reltime);
723 if (res == NO_ERROR) {
724 msg->wait();
725 }
726 return res;
727}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800728
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700729void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700730 do {
731 waitForEvent();
732 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800733}
734
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700735void SurfaceFlinger::enableHardwareVsync() {
736 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700737 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700738 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700739 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
740 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700741 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700742 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700743}
744
Jesse Hall948fe0c2013-10-14 12:56:09 -0700745void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700746 Mutex::Autolock _l(mHWVsyncLock);
747
Jesse Hall948fe0c2013-10-14 12:56:09 -0700748 if (makeAvailable) {
749 mHWVsyncAvailable = true;
750 } else if (!mHWVsyncAvailable) {
751 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
752 return;
753 }
754
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700755 const nsecs_t period =
756 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
757
758 mPrimaryDispSync.reset();
759 mPrimaryDispSync.setPeriod(period);
760
761 if (!mPrimaryHWVsyncEnabled) {
762 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700763 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
764 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700765 mPrimaryHWVsyncEnabled = true;
766 }
767}
768
Jesse Hall948fe0c2013-10-14 12:56:09 -0700769void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700770 Mutex::Autolock _l(mHWVsyncLock);
771 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700772 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
773 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700774 mPrimaryDispSync.endResync();
775 mPrimaryHWVsyncEnabled = false;
776 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700777 if (makeUnavailable) {
778 mHWVsyncAvailable = false;
779 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700780}
781
782void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700783 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700784
Jamie Gennisd1700752013-10-14 12:22:52 -0700785 { // Scope for the lock
786 Mutex::Autolock _l(mHWVsyncLock);
787 if (type == 0 && mPrimaryHWVsyncEnabled) {
788 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700789 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700790 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700791
792 if (needsHwVsync) {
793 enableHardwareVsync();
794 } else {
795 disableHardwareVsync(false);
796 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700797}
798
799void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
800 if (mEventThread == NULL) {
801 // This is a temporary workaround for b/7145521. A non-null pointer
802 // does not mean EventThread has finished initializing, so this
803 // is not a correct fix.
804 ALOGW("WARNING: EventThread not started, ignoring hotplug");
805 return;
806 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700807
Jesse Hall9e663de2013-08-16 14:28:37 -0700808 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700809 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800810 if (connected) {
811 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700812 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800813 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
814 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700815 }
816 setTransactionFlags(eDisplayTransactionNeeded);
817
Andy McFadden9e9689c2012-10-10 18:17:51 -0700818 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700819 }
Mathias Agopian86303202012-07-24 22:46:10 -0700820}
821
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700822void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700823 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700824 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700825}
826
Mathias Agopian4fec8732012-06-29 14:12:52 -0700827void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800828 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800829 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700830 case MessageQueue::TRANSACTION:
831 handleMessageTransaction();
832 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700833 case MessageQueue::INVALIDATE:
834 handleMessageTransaction();
835 handleMessageInvalidate();
836 signalRefresh();
837 break;
838 case MessageQueue::REFRESH:
839 handleMessageRefresh();
840 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800841 }
842}
843
Mathias Agopian4fec8732012-06-29 14:12:52 -0700844void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700845 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700846 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700847 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700848 }
849}
850
851void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700852 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700853 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700854}
855
856void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700857 ATRACE_CALL();
858 preComposition();
859 rebuildLayerStacks();
860 setUpHWComposer();
861 doDebugFlashRegions();
862 doComposition();
863 postComposition();
864}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700865
Mathias Agopiancd60f992012-08-16 16:28:27 -0700866void SurfaceFlinger::doDebugFlashRegions()
867{
868 // is debugging enabled
869 if (CC_LIKELY(!mDebugRegion))
870 return;
871
872 const bool repaintEverything = mRepaintEverything;
873 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
874 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700875 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700876 // transform the dirty region into this screen's coordinate space
877 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
878 if (!dirtyRegion.isEmpty()) {
879 // redraw the whole screen
880 doComposeSurfaces(hw, Region(hw->bounds()));
881
882 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700883 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700884 RenderEngine& engine(getRenderEngine());
885 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
886
Mathias Agopiancd60f992012-08-16 16:28:27 -0700887 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700888 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700889 }
890 }
891 }
892
893 postFramebuffer();
894
895 if (mDebugRegion > 1) {
896 usleep(mDebugRegion * 1000);
897 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700898
899 HWComposer& hwc(getHwComposer());
900 if (hwc.initCheck() == NO_ERROR) {
901 status_t err = hwc.prepare();
902 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
903 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700904}
905
906void SurfaceFlinger::preComposition()
907{
908 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700909 const LayerVector& layers(mDrawingState.layersSortedByZ);
910 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700911 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700912 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700913 needExtraInvalidate = true;
914 }
915 }
916 if (needExtraInvalidate) {
917 signalLayerUpdate();
918 }
919}
920
921void SurfaceFlinger::postComposition()
922{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700923 const LayerVector& layers(mDrawingState.layersSortedByZ);
924 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700925 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700926 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700927 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800928
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700929 const HWComposer& hwc = getHwComposer();
930 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
931
932 if (presentFence->isValid()) {
933 if (mPrimaryDispSync.addPresentFence(presentFence)) {
934 enableHardwareVsync();
935 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700936 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700937 }
938 }
939
Andy McFadden5167ec62014-05-22 13:08:43 -0700940 if (kIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700941 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700942 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700943 enableHardwareVsync();
944 }
945 }
946
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800947 if (mAnimCompositionPending) {
948 mAnimCompositionPending = false;
949
Jesse Halla9a1b002013-02-27 16:39:25 -0800950 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800951 mAnimFrameTracker.setActualPresentFence(presentFence);
952 } else {
953 // The HWC doesn't support present fences, so use the refresh
954 // timestamp instead.
955 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
956 mAnimFrameTracker.setActualPresentTime(presentTime);
957 }
958 mAnimFrameTracker.advanceFrame();
959 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700960}
961
962void SurfaceFlinger::rebuildLayerStacks() {
963 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700964 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700965 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700966 mVisibleRegionsDirty = false;
967 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700968
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700969 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700970 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700971 Region opaqueRegion;
972 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800973 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700974 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700975 const Transform& tr(hw->getTransform());
976 const Rect bounds(hw->getBounds());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700977 if (hw->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700978 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700979 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700980
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700981 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700982 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700983 const sp<Layer>& layer(layers[i]);
984 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700985 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700986 Region drawRegion(tr.transform(
987 layer->visibleNonTransparentRegion));
988 drawRegion.andSelf(bounds);
989 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700990 layersSortedByZ.add(layer);
991 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700992 }
993 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700994 }
Mathias Agopian42977342012-08-05 00:40:46 -0700995 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700996 hw->undefinedRegion.set(bounds);
997 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
998 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700999 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001000 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001001}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001002
Mathias Agopiancd60f992012-08-16 16:28:27 -07001003void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001004 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001005 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1006 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1007 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1008
1009 // If nothing has changed (!dirty), don't recompose.
1010 // If something changed, but we don't currently have any visible layers,
1011 // and didn't when we last did a composition, then skip it this time.
1012 // The second rule does two things:
1013 // - When all layers are removed from a display, we'll emit one black
1014 // frame, then nothing more until we get new layers.
1015 // - When a display is created with a private layer stack, we won't
1016 // emit any black frames until a layer is added to the layer stack.
1017 bool mustRecompose = dirty && !(empty && wasEmpty);
1018
1019 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1020 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1021 mustRecompose ? "doing" : "skipping",
1022 dirty ? "+" : "-",
1023 empty ? "+" : "-",
1024 wasEmpty ? "+" : "-");
1025
Dan Stoza71433162014-02-04 16:22:36 -08001026 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001027
1028 if (mustRecompose) {
1029 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1030 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001031 }
1032
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001033 HWComposer& hwc(getHwComposer());
1034 if (hwc.initCheck() == NO_ERROR) {
1035 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001036 if (CC_UNLIKELY(mHwWorkListDirty)) {
1037 mHwWorkListDirty = false;
1038 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1039 sp<const DisplayDevice> hw(mDisplays[dpy]);
1040 const int32_t id = hw->getHwcDisplayId();
1041 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001042 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001043 hw->getVisibleLayersSortedByZ());
1044 const size_t count = currentLayers.size();
1045 if (hwc.createWorkList(id, count) == NO_ERROR) {
1046 HWComposer::LayerListIterator cur = hwc.begin(id);
1047 const HWComposer::LayerListIterator end = hwc.end(id);
1048 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001049 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001050 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001051 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001052 cur->setSkip(true);
1053 }
1054 }
1055 }
1056 }
1057 }
1058 }
1059
1060 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001061 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001062 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001063 const int32_t id = hw->getHwcDisplayId();
1064 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001065 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001066 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001067 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001068 HWComposer::LayerListIterator cur = hwc.begin(id);
1069 const HWComposer::LayerListIterator end = hwc.end(id);
1070 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1071 /*
1072 * update the per-frame h/w composer data for each layer
1073 * and build the transparent region of the FB
1074 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001075 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001076 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -07001077 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001078 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001079 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001080
Riley Andrews03414a12014-07-01 14:22:59 -07001081 // If possible, attempt to use the cursor overlay on each display.
1082 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1083 sp<const DisplayDevice> hw(mDisplays[dpy]);
1084 const int32_t id = hw->getHwcDisplayId();
1085 if (id >= 0) {
1086 const Vector< sp<Layer> >& currentLayers(
1087 hw->getVisibleLayersSortedByZ());
1088 const size_t count = currentLayers.size();
1089 HWComposer::LayerListIterator cur = hwc.begin(id);
1090 const HWComposer::LayerListIterator end = hwc.end(id);
1091 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1092 const sp<Layer>& layer(currentLayers[i]);
1093 if (layer->isPotentialCursor()) {
1094 cur->setIsCursorLayerHint();
1095 break;
1096 }
1097 }
1098 }
1099 }
1100
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001101 status_t err = hwc.prepare();
1102 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001103
1104 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1105 sp<const DisplayDevice> hw(mDisplays[dpy]);
1106 hw->prepareFrame(hwc);
1107 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001108 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001109}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001110
Mathias Agopiancd60f992012-08-16 16:28:27 -07001111void SurfaceFlinger::doComposition() {
1112 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001113 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001114 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001115 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001116 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001117 // transform the dirty region into this screen's coordinate space
1118 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001119
1120 // repaint the framebuffer (if needed)
1121 doDisplayComposition(hw, dirtyRegion);
1122
Mathias Agopiancd60f992012-08-16 16:28:27 -07001123 hw->dirtyRegion.clear();
1124 hw->flip(hw->swapRegion);
1125 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001126 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001127 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001128 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001129 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001130 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001131}
1132
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001133void SurfaceFlinger::postFramebuffer()
1134{
Mathias Agopian841cde52012-03-01 15:44:37 -08001135 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001136
Mathias Agopiana44b0412011-10-16 18:46:35 -07001137 const nsecs_t now = systemTime();
1138 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001139
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001140 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001141 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001142 if (!hwc.supportsFramebufferTarget()) {
1143 // EGL spec says:
1144 // "surface must be bound to the calling thread's current context,
1145 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001146 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001147 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001148 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001149 }
1150
Mathias Agopian6da15f42013-09-25 20:40:07 -07001151 // make the default display current because the VirtualDisplayDevice code cannot
1152 // deal with dequeueBuffer() being called outside of the composition loop; however
1153 // the code below can call glFlush() which is allowed (and does in some case) call
1154 // dequeueBuffer().
1155 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1156
Mathias Agopian92a979a2012-08-02 18:32:23 -07001157 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001158 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001159 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001160 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001161 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001162 int32_t id = hw->getHwcDisplayId();
1163 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001164 HWComposer::LayerListIterator cur = hwc.begin(id);
1165 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001166 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001167 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001168 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001169 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001170 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001171 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001172 }
Jesse Hallef194142012-06-14 14:45:17 -07001173 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001174 }
1175
Mathias Agopiana44b0412011-10-16 18:46:35 -07001176 mLastSwapBufferTime = systemTime() - now;
1177 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001178
1179 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1180 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1181 logFrameStats();
1182 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183}
1184
Mathias Agopian87baae12012-07-31 12:38:26 -07001185void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001186{
Mathias Agopian841cde52012-03-01 15:44:37 -08001187 ATRACE_CALL();
1188
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001189 // here we keep a copy of the drawing state (that is the state that's
1190 // going to be overwritten by handleTransactionLocked()) outside of
1191 // mStateLock so that the side-effects of the State assignment
1192 // don't happen with mStateLock held (which can cause deadlocks).
1193 State drawingState(mDrawingState);
1194
Mathias Agopianca4d3602011-05-19 15:38:14 -07001195 Mutex::Autolock _l(mStateLock);
1196 const nsecs_t now = systemTime();
1197 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001198
Mathias Agopianca4d3602011-05-19 15:38:14 -07001199 // Here we're guaranteed that some transaction flags are set
1200 // so we can call handleTransactionLocked() unconditionally.
1201 // We call getTransactionFlags(), which will also clear the flags,
1202 // with mStateLock held to guarantee that mCurrentState won't change
1203 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001204
Mathias Agopiane57f2922012-08-09 16:29:12 -07001205 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001206 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001207
Mathias Agopianca4d3602011-05-19 15:38:14 -07001208 mLastTransactionTime = systemTime() - now;
1209 mDebugInTransaction = 0;
1210 invalidateHwcGeometry();
1211 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001212}
1213
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301214void SurfaceFlinger::setVirtualDisplayData(
1215 int32_t hwcDisplayId,
1216 const sp<IGraphicBufferProducer>& sink)
1217{
1218 sp<ANativeWindow> mNativeWindow = new Surface(sink);
1219 ANativeWindow* const window = mNativeWindow.get();
1220
1221 int format;
1222 window->query(window, NATIVE_WINDOW_FORMAT, &format);
1223
1224 EGLSurface surface;
1225 EGLint w, h;
1226 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1227 surface = eglCreateWindowSurface(display, mRenderEngine->getEGLConfig(), window, NULL);
1228 eglQuerySurface(display, surface, EGL_WIDTH, &w);
1229 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
1230
1231 mHwc->setVirtualDisplayProperties(hwcDisplayId, w, h, format);
1232}
1233
Mathias Agopian87baae12012-07-31 12:38:26 -07001234void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001235{
1236 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001237 const size_t count = currentLayers.size();
1238
1239 /*
1240 * Traversal of the children
1241 * (perform the transaction for each of them if needed)
1242 */
1243
Mathias Agopian3559b072012-08-15 13:46:03 -07001244 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001245 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001246 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001247 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1248 if (!trFlags) continue;
1249
1250 const uint32_t flags = layer->doTransaction(0);
1251 if (flags & Layer::eVisibleRegion)
1252 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001253 }
1254 }
1255
1256 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001257 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258 */
1259
Mathias Agopiane57f2922012-08-09 16:29:12 -07001260 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001261 // here we take advantage of Vector's copy-on-write semantics to
1262 // improve performance by skipping the transaction entirely when
1263 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001264 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1265 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001266 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001267 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001268 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001269 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001270
1271 // find the displays that were removed
1272 // (ie: in drawing state but not in current state)
1273 // also handle displays that changed
1274 // (ie: displays that are in both lists)
1275 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001276 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1277 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001278 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001279 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001280 // Call makeCurrent() on the primary display so we can
1281 // be sure that nothing associated with this display
1282 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001283 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001284 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001285 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1286 if (hw != NULL)
1287 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001288 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001289 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001290 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001291 } else {
1292 ALOGW("trying to remove the main display");
1293 }
1294 } else {
1295 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001296 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001297 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001298 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001299 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001300 // recreating the DisplayDevice, so we just remove it
1301 // from the drawing state, so that it get re-added
1302 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001303 sp<DisplayDevice> hw(getDisplayDevice(display));
1304 if (hw != NULL)
1305 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001306 mDisplays.removeItem(display);
1307 mDrawingState.displays.removeItemsAt(i);
1308 dc--; i--;
1309 // at this point we must loop to the next item
1310 continue;
1311 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001312
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001313 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001314 if (disp != NULL) {
1315 if (state.layerStack != draw[i].layerStack) {
1316 disp->setLayerStack(state.layerStack);
1317 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001318 if ((state.orientation != draw[i].orientation)
1319 || (state.viewport != draw[i].viewport)
1320 || (state.frame != draw[i].frame))
1321 {
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001322 // Honor the orientation change after boot
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301323 // animation completes and make sure boot
1324 // animation is shown in panel orientation always.
1325 if(mBootFinished){
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001326 disp->setProjection(state.orientation,
1327 state.viewport, state.frame);
1328 }
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301329 else{
1330 char property[PROPERTY_VALUE_MAX];
1331 int panelOrientation =
1332 DisplayState::eOrientationDefault;
1333 if(property_get("persist.panel.orientation",
1334 property, "0") > 0){
1335 panelOrientation = atoi(property) / 90;
1336 }
1337 disp->setProjection(panelOrientation,
1338 state.viewport, state.frame);
1339
1340 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001341 }
Michael Lentine47e45402014-07-18 15:34:25 -07001342 if (state.width != draw[i].width || state.height != draw[i].height) {
1343 disp->setDisplaySize(state.width, state.height);
1344 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001345 }
1346 }
1347 }
1348
1349 // find displays that were added
1350 // (ie: in current state but not in drawing state)
1351 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001352 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001353 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001354
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001355 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001356 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001357 sp<IGraphicBufferProducer> bqProducer;
1358 sp<IGraphicBufferConsumer> bqConsumer;
1359 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1360 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001361
Jesse Hall02d86562013-03-25 14:43:23 -07001362 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001363 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001364 // Virtual displays without a surface are dormant:
1365 // they have external state (layer stack, projection,
1366 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001367 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001368
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301369 char value[PROPERTY_VALUE_MAX];
Jesse Hall02d86562013-03-25 14:43:23 -07001370 hwcDisplayId = allocateHwcDisplayId(state.type);
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301371 property_get("persist.sys.wfd.virtual", value, "0");
1372 int wfdVirtual = atoi(value);
1373 if(!wfdVirtual) {
1374 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1375 *mHwc, hwcDisplayId, state.surface,
1376 bqProducer, bqConsumer, state.displayName);
1377 if (hwcDisplayId >= 0) {
1378 producer = vds;
1379 } else {
1380 // There won't be any interaction with HWC for this virtual display,
1381 // so the GLES driver can pass buffers directly to the sink.
1382 producer = state.surface;
1383 }
1384 } else {
1385 //Read virtual display properties and create a
1386 //rendering surface for it inorder to be handled
1387 //by hwc.
1388 setVirtualDisplayData(hwcDisplayId,
1389 state.surface);
1390 dispSurface = new FramebufferSurface(*mHwc,
1391 state.type,
1392 bqConsumer);
1393 producer = bqProducer;
1394 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001395 }
1396 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001397 ALOGE_IF(state.surface!=NULL,
1398 "adding a supported display, but rendering "
1399 "surface is provided (%p), ignoring it",
1400 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001401 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001402 // for supported (by hwc) displays we provide our
1403 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001404 dispSurface = new FramebufferSurface(*mHwc, state.type,
1405 bqConsumer);
1406 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001407 }
1408
1409 const wp<IBinder>& display(curr.keyAt(i));
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301410 if (dispSurface != NULL && producer != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001411 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001412 state.type, hwcDisplayId,
1413 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001414 display, dispSurface, producer,
1415 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001416 hw->setLayerStack(state.layerStack);
1417 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001418 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001419 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001420 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001421 if (state.isVirtualDisplay()) {
1422 if (hwcDisplayId >= 0) {
1423 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1424 hw->getWidth(), hw->getHeight(),
1425 hw->getFormat());
1426 }
1427 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001428 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001429 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001430 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001431 }
1432 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001433 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001434 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435
Mathias Agopian84300952012-11-21 16:02:13 -08001436 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1437 // The transform hint might have changed for some layers
1438 // (either because a display has changed, or because a layer
1439 // as changed).
1440 //
1441 // Walk through all the layers in currentLayers,
1442 // and update their transform hint.
1443 //
1444 // If a layer is visible only on a single display, then that
1445 // display is used to calculate the hint, otherwise we use the
1446 // default display.
1447 //
1448 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1449 // the hint is set before we acquire a buffer from the surface texture.
1450 //
1451 // NOTE: layer transactions have taken place already, so we use their
1452 // drawing state. However, SurfaceFlinger's own transaction has not
1453 // happened yet, so we must use the current state layer list
1454 // (soon to become the drawing state list).
1455 //
1456 sp<const DisplayDevice> disp;
1457 uint32_t currentlayerStack = 0;
1458 for (size_t i=0; i<count; i++) {
1459 // NOTE: we rely on the fact that layers are sorted by
1460 // layerStack first (so we don't have to traverse the list
1461 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001462 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001463 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001464 if (i==0 || currentlayerStack != layerStack) {
1465 currentlayerStack = layerStack;
1466 // figure out if this layerstack is mirrored
1467 // (more than one display) if so, pick the default display,
1468 // if not, pick the only display it's on.
1469 disp.clear();
1470 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1471 sp<const DisplayDevice> hw(mDisplays[dpy]);
1472 if (hw->getLayerStack() == currentlayerStack) {
1473 if (disp == NULL) {
1474 disp = hw;
1475 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001476 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001477 break;
1478 }
1479 }
1480 }
1481 }
Chet Haase91d25932013-04-11 15:24:55 -07001482 if (disp == NULL) {
1483 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1484 // redraw after transform hint changes. See bug 8508397.
1485
1486 // could be null when this layer is using a layerStack
1487 // that is not visible on any display. Also can occur at
1488 // screen off/on times.
1489 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001490 }
Chet Haase91d25932013-04-11 15:24:55 -07001491 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001492 }
1493 }
1494
1495
Mathias Agopian3559b072012-08-15 13:46:03 -07001496 /*
1497 * Perform our own transaction if needed
1498 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001499
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001500 const LayerVector& layers(mDrawingState.layersSortedByZ);
1501 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001502 // layers have been added
1503 mVisibleRegionsDirty = true;
1504 }
1505
1506 // some layers might have been removed, so
1507 // we need to update the regions they're exposing.
1508 if (mLayersRemoved) {
1509 mLayersRemoved = false;
1510 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001511 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001512 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001513 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001514 if (currentLayers.indexOf(layer) < 0) {
1515 // this layer is not visible anymore
1516 // TODO: we could traverse the tree from front to back and
1517 // compute the actual visible region
1518 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001519 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001520 Region visibleReg = s.transform.transform(
1521 Region(Rect(s.active.w, s.active.h)));
1522 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001523 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001524 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001525 }
1526
1527 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001528
1529 updateCursorAsync();
1530}
1531
1532void SurfaceFlinger::updateCursorAsync()
1533{
1534 HWComposer& hwc(getHwComposer());
1535 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1536 sp<const DisplayDevice> hw(mDisplays[dpy]);
1537 const int32_t id = hw->getHwcDisplayId();
1538 if (id < 0) {
1539 continue;
1540 }
1541 const Vector< sp<Layer> >& currentLayers(
1542 hw->getVisibleLayersSortedByZ());
1543 const size_t count = currentLayers.size();
1544 HWComposer::LayerListIterator cur = hwc.begin(id);
1545 const HWComposer::LayerListIterator end = hwc.end(id);
1546 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1547 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1548 continue;
1549 }
1550 const sp<Layer>& layer(currentLayers[i]);
1551 Rect cursorPos = layer->getPosition(hw);
1552 hwc.setCursorPositionAsync(id, cursorPos);
1553 break;
1554 }
1555 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001556}
1557
1558void SurfaceFlinger::commitTransaction()
1559{
1560 if (!mLayersPendingRemoval.isEmpty()) {
1561 // Notify removed layers now that they can't be drawn from
1562 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1563 mLayersPendingRemoval[i]->onRemoved();
1564 }
1565 mLayersPendingRemoval.clear();
1566 }
1567
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001568 // If this transaction is part of a window animation then the next frame
1569 // we composite should be considered an animation as well.
1570 mAnimCompositionPending = mAnimTransactionPending;
1571
Mathias Agopian4fec8732012-06-29 14:12:52 -07001572 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001573 mTransactionPending = false;
1574 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001575 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001576}
1577
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001578void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001579 const LayerVector& currentLayers, uint32_t layerStack,
1580 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001581{
Mathias Agopian841cde52012-03-01 15:44:37 -08001582 ATRACE_CALL();
1583
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001584 Region aboveOpaqueLayers;
1585 Region aboveCoveredLayers;
1586 Region dirty;
1587
Mathias Agopian87baae12012-07-31 12:38:26 -07001588 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001589
1590 size_t i = currentLayers.size();
1591 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001592 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001593
1594 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001595 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001596
Jesse Hall01e29052013-02-19 16:13:35 -08001597 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001598 if (s.layerStack != layerStack)
1599 continue;
1600
Mathias Agopianab028732010-03-16 16:41:46 -07001601 /*
1602 * opaqueRegion: area of a surface that is fully opaque.
1603 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001604 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001605
1606 /*
1607 * visibleRegion: area of a surface that is visible on screen
1608 * and not fully transparent. This is essentially the layer's
1609 * footprint minus the opaque regions above it.
1610 * Areas covered by a translucent surface are considered visible.
1611 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001612 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001613
1614 /*
1615 * coveredRegion: area of a surface that is covered by all
1616 * visible regions above it (which includes the translucent areas).
1617 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001618 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001619
Jesse Halla8026d22012-09-25 13:25:04 -07001620 /*
1621 * transparentRegion: area of a surface that is hinted to be completely
1622 * transparent. This is only used to tell when the layer has no visible
1623 * non-transparent regions and can be removed from the layer list. It
1624 * does not affect the visibleRegion of this layer or any layers
1625 * beneath it. The hint may not be correct if apps don't respect the
1626 * SurfaceView restrictions (which, sadly, some don't).
1627 */
1628 Region transparentRegion;
1629
Mathias Agopianab028732010-03-16 16:41:46 -07001630
1631 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001632 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001633 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001634 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001635 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001636 if (!visibleRegion.isEmpty()) {
1637 // Remove the transparent area from the visible region
1638 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001639 const Transform tr(s.transform);
1640 if (tr.transformed()) {
1641 if (tr.preserveRects()) {
1642 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001643 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001644 } else {
1645 // transformation too complex, can't do the
1646 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001647 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001648 }
1649 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001650 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001651 }
Mathias Agopianab028732010-03-16 16:41:46 -07001652 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001653
Mathias Agopianab028732010-03-16 16:41:46 -07001654 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001655 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001656 if (s.alpha==255 && !translucent &&
1657 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1658 // the opaque region is the layer's footprint
1659 opaqueRegion = visibleRegion;
1660 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001661 }
1662 }
1663
Mathias Agopianab028732010-03-16 16:41:46 -07001664 // Clip the covered region to the visible region
1665 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1666
1667 // Update aboveCoveredLayers for next (lower) layer
1668 aboveCoveredLayers.orSelf(visibleRegion);
1669
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001670 // subtract the opaque region covered by the layers above us
1671 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001672
1673 // compute this layer's dirty region
1674 if (layer->contentDirty) {
1675 // we need to invalidate the whole region
1676 dirty = visibleRegion;
1677 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001678 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001679 layer->contentDirty = false;
1680 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001681 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001682 * the exposed region consists of two components:
1683 * 1) what's VISIBLE now and was COVERED before
1684 * 2) what's EXPOSED now less what was EXPOSED before
1685 *
1686 * note that (1) is conservative, we start with the whole
1687 * visible region but only keep what used to be covered by
1688 * something -- which mean it may have been exposed.
1689 *
1690 * (2) handles areas that were not covered by anything but got
1691 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001692 */
Mathias Agopianab028732010-03-16 16:41:46 -07001693 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001694 const Region oldVisibleRegion = layer->visibleRegion;
1695 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001696 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1697 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001698 }
1699 dirty.subtractSelf(aboveOpaqueLayers);
1700
1701 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001702 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001703
Mathias Agopianab028732010-03-16 16:41:46 -07001704 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001706
Jesse Halla8026d22012-09-25 13:25:04 -07001707 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001708 layer->setVisibleRegion(visibleRegion);
1709 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001710 layer->setVisibleNonTransparentRegion(
1711 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001712 }
1713
Mathias Agopian87baae12012-07-31 12:38:26 -07001714 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001715}
1716
Mathias Agopian87baae12012-07-31 12:38:26 -07001717void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1718 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001719 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001720 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1721 if (hw->getLayerStack() == layerStack) {
1722 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001723 }
1724 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001725}
1726
1727void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001728{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001729 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001730
Mathias Agopian4fec8732012-06-29 14:12:52 -07001731 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001732 const LayerVector& layers(mDrawingState.layersSortedByZ);
Eric Penner51c59cd2014-07-28 19:51:58 -07001733
1734 // Store the set of layers that need updates. This set must not change as
1735 // buffers are being latched, as this could result in a deadlock.
1736 // Example: Two producers share the same command stream and:
1737 // 1.) Layer 0 is latched
1738 // 2.) Layer 0 gets a new frame
1739 // 2.) Layer 1 gets a new frame
1740 // 3.) Layer 1 is latched.
1741 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1742 // second frame. But layer 0's second frame could be waiting on display.
1743 Vector<Layer*> layersWithQueuedFrames;
1744 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001745 const sp<Layer>& layer(layers[i]);
Eric Penner51c59cd2014-07-28 19:51:58 -07001746 if (layer->hasQueuedFrame())
1747 layersWithQueuedFrames.push_back(layer.get());
1748 }
1749 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
1750 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07001751 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001752 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001753 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001754 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001755
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001756 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001757}
1758
Mathias Agopianad456f92011-01-13 17:53:01 -08001759void SurfaceFlinger::invalidateHwcGeometry()
1760{
1761 mHwWorkListDirty = true;
1762}
1763
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001764
Mathias Agopiancd60f992012-08-16 16:28:27 -07001765void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001766 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001767{
Dan Stoza71433162014-02-04 16:22:36 -08001768 // We only need to actually compose the display if:
1769 // 1) It is being handled by hardware composer, which may need this to
1770 // keep its virtual display state machine in sync, or
1771 // 2) There is work to be done (the dirty region isn't empty)
1772 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1773 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1774 return;
1775 }
1776
Mathias Agopian87baae12012-07-31 12:38:26 -07001777 Region dirtyRegion(inDirtyRegion);
1778
Mathias Agopianb8a55602009-06-26 19:06:36 -07001779 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001780 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001781
Mathias Agopian42977342012-08-05 00:40:46 -07001782 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001783 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001784 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1785 // takes a rectangle, we must make sure to update that whole
1786 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001787 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001788 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001789 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001790 // We need to redraw the rectangle that will be updated
1791 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001792 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001793 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001794 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001795 } else {
1796 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001797 dirtyRegion.set(hw->bounds());
1798 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001799 }
1800 }
1801
Alan Viverette9c5a3332013-09-12 20:04:35 -07001802 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Michael Lentine3f121fc2014-10-01 11:17:28 -07001803 if (!doComposeSurfaces(hw, dirtyRegion)) return;
Mathias Agopianff2ed702013-09-01 21:36:12 -07001804 } else {
1805 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001806 mat4 colorMatrix = mColorMatrix;
1807 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001808 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001809 }
1810 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001811 doComposeSurfaces(hw, dirtyRegion);
1812 engine.endGroup();
1813 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001814
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001815 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001816 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001817
1818 // swap buffers (presentation)
1819 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001820}
1821
Michael Lentine3f121fc2014-10-01 11:17:28 -07001822bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001823{
Mathias Agopian3f844832013-08-07 21:24:32 -07001824 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001825 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001826 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001827 HWComposer::LayerListIterator cur = hwc.begin(id);
1828 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001829
Jesse Halld05a17f2013-09-30 16:49:30 -07001830 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001831 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001832 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001833 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1834 hw->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07001835 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1836 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
1837 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
1838 }
1839 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08001840 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001841
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001842 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001843 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001844 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001845 // when using overlays, we assume a fully transparent framebuffer
1846 // NOTE: we could reduce how much we need to clear, for instance
1847 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001848 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001849 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001850 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001851 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001852 // we start with the whole screen area
1853 const Region bounds(hw->getBounds());
1854
1855 // we remove the scissor part
1856 // we're left with the letterbox region
1857 // (common case is that letterbox ends-up being empty)
1858 const Region letterbox(bounds.subtract(hw->getScissor()));
1859
1860 // compute the area to clear
1861 Region region(hw->undefinedRegion.merge(letterbox));
1862
1863 // but limit it to the dirty region
1864 region.andSelf(dirty);
1865
Mathias Agopianb9494d52012-04-18 02:28:45 -07001866 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001867 if (!region.isEmpty()) {
Radhika Ranjan Soni35c9ff22013-12-17 19:55:17 +05301868 if (cur != end) {
1869 if (cur->getCompositionType() != HWC_BLIT)
1870 // can happen with SurfaceView
1871 drawWormhole(hw, region);
1872 } else
1873 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001874 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001875 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001876
Mathias Agopian766dc492012-10-30 18:08:06 -07001877 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1878 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001879 // scissor on the main display. It should never be needed
1880 // anyways (though in theory it could since the API allows it).
1881 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001882 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001883 if (scissor != bounds) {
1884 // scissor doesn't match the screen's dimensions, so we
1885 // need to clear everything outside of it and enable
1886 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001887
Mathias Agopianf45c5102012-10-24 16:29:17 -07001888 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001889 const uint32_t height = hw->getHeight();
1890 engine.setScissor(scissor.left, height - scissor.bottom,
1891 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001892 }
1893 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001894 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001895
Mathias Agopian85d751c2012-08-29 16:59:24 -07001896 /*
1897 * and then, render the layers targeted at the framebuffer
1898 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001899
Mathias Agopian13127d82013-03-05 17:47:11 -08001900 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001901 const size_t count = layers.size();
1902 const Transform& tr = hw->getTransform();
1903 if (cur != end) {
1904 // we're using h/w composer
1905 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001906 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001907 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001908 if (!clip.isEmpty()) {
1909 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07001910 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07001911 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001912 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001913 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1914 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001915 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001916 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001917 // never clear the very first layer since we're
1918 // guaranteed the FB is already cleared
1919 layer->clearWithOpenGL(hw, clip);
1920 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001921 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001922 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001923 case HWC_FRAMEBUFFER: {
1924 layer->draw(hw, clip);
1925 break;
1926 }
Arun Kumar K.R5111ff02012-12-19 18:10:46 -08001927 case HWC_BLIT:
1928 //Do nothing
1929 break;
Mathias Agopianda27af92012-09-13 18:17:13 -07001930 case HWC_FRAMEBUFFER_TARGET: {
1931 // this should not happen as the iterator shouldn't
1932 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001933 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001934 break;
1935 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001936 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001937 }
1938 layer->setAcquireFence(hw, *cur);
1939 }
1940 } else {
1941 // we're not using h/w composer
1942 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001943 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001944 const Region clip(dirty.intersect(
1945 tr.transform(layer->visibleRegion)));
1946 if (!clip.isEmpty()) {
1947 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001948 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001949 }
1950 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001951
1952 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001953 engine.disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07001954 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001955}
1956
Mathias Agopian3f844832013-08-07 21:24:32 -07001957void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001958 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001959 RenderEngine& engine(getRenderEngine());
1960 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001961}
1962
Mathias Agopianac9fa422013-02-11 16:40:36 -08001963void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1964 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001965 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001966 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001967{
Mathias Agopian96f08192010-06-02 23:28:45 -07001968 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001969 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001970
Mathias Agopian96f08192010-06-02 23:28:45 -07001971 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001972 Mutex::Autolock _l(mStateLock);
1973 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001974 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001975}
1976
Mathias Agopian3f844832013-08-07 21:24:32 -07001977status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001978 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001979 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001980 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001981 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001982 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001983 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001984 return NO_ERROR;
1985 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001986 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001987}
1988
Dan Stozac7014012014-02-14 15:03:43 -08001989uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001990 return android_atomic_release_load(&mTransactionFlags);
1991}
1992
Mathias Agopian3f844832013-08-07 21:24:32 -07001993uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001994 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1995}
1996
Mathias Agopian3f844832013-08-07 21:24:32 -07001997uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001998 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1999 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002000 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002001 }
2002 return old;
2003}
2004
Mathias Agopian8b33f032012-07-24 20:43:54 -07002005void SurfaceFlinger::setTransactionState(
2006 const Vector<ComposerState>& state,
2007 const Vector<DisplayState>& displays,
2008 uint32_t flags)
2009{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002010 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07002011 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07002012 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002013
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002014 if (flags & eAnimation) {
2015 // For window updates that are part of an animation we must wait for
2016 // previous animation "frames" to be handled.
2017 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002018 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002019 if (CC_UNLIKELY(err != NO_ERROR)) {
2020 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002021 // caller after a few seconds.
2022 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
2023 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002024 mAnimTransactionPending = false;
2025 break;
2026 }
2027 }
2028 }
2029
Mathias Agopiane57f2922012-08-09 16:29:12 -07002030 size_t count = displays.size();
2031 for (size_t i=0 ; i<count ; i++) {
2032 const DisplayState& s(displays[i]);
2033 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002034 }
2035
Mathias Agopiane57f2922012-08-09 16:29:12 -07002036 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002037 for (size_t i=0 ; i<count ; i++) {
2038 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002039 // Here we need to check that the interface we're given is indeed
2040 // one of our own. A malicious client could give us a NULL
2041 // IInterface, or one of its own or even one of our own but a
2042 // different type. All these situations would cause us to crash.
2043 //
2044 // NOTE: it would be better to use RTTI as we could directly check
2045 // that we have a Client*. however, RTTI is disabled in Android.
2046 if (s.client != NULL) {
2047 sp<IBinder> binder = s.client->asBinder();
2048 if (binder != NULL) {
2049 String16 desc(binder->getInterfaceDescriptor());
2050 if (desc == ISurfaceComposerClient::descriptor) {
2051 sp<Client> client( static_cast<Client *>(s.client.get()) );
2052 transactionFlags |= setClientStateLocked(client, s.state);
2053 }
2054 }
2055 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002056 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002057
Mathias Agopian386aa982011-11-07 21:58:03 -08002058 if (transactionFlags) {
2059 // this triggers the transaction
2060 setTransactionFlags(transactionFlags);
2061
2062 // if this is a synchronous transaction, wait for it to take effect
2063 // before returning.
2064 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002065 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002066 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002067 if (flags & eAnimation) {
2068 mAnimTransactionPending = true;
2069 }
2070 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002071 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2072 if (CC_UNLIKELY(err != NO_ERROR)) {
2073 // just in case something goes wrong in SF, return to the
2074 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002075 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2076 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002077 break;
2078 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002079 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002080 }
2081}
2082
Mathias Agopiane57f2922012-08-09 16:29:12 -07002083uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2084{
Jesse Hall9a143922012-10-04 16:29:19 -07002085 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2086 if (dpyIdx < 0)
2087 return 0;
2088
Mathias Agopiane57f2922012-08-09 16:29:12 -07002089 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002090 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002091 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002092 const uint32_t what = s.what;
2093 if (what & DisplayState::eSurfaceChanged) {
2094 if (disp.surface->asBinder() != s.surface->asBinder()) {
2095 disp.surface = s.surface;
2096 flags |= eDisplayTransactionNeeded;
2097 }
2098 }
2099 if (what & DisplayState::eLayerStackChanged) {
2100 if (disp.layerStack != s.layerStack) {
2101 disp.layerStack = s.layerStack;
2102 flags |= eDisplayTransactionNeeded;
2103 }
2104 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002105 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002106 if (disp.orientation != s.orientation) {
2107 disp.orientation = s.orientation;
2108 flags |= eDisplayTransactionNeeded;
2109 }
2110 if (disp.frame != s.frame) {
2111 disp.frame = s.frame;
2112 flags |= eDisplayTransactionNeeded;
2113 }
2114 if (disp.viewport != s.viewport) {
2115 disp.viewport = s.viewport;
2116 flags |= eDisplayTransactionNeeded;
2117 }
2118 }
Michael Lentine47e45402014-07-18 15:34:25 -07002119 if (what & DisplayState::eDisplaySizeChanged) {
2120 if (disp.width != s.width) {
2121 disp.width = s.width;
2122 flags |= eDisplayTransactionNeeded;
2123 }
2124 if (disp.height != s.height) {
2125 disp.height = s.height;
2126 flags |= eDisplayTransactionNeeded;
2127 }
2128 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002129 }
2130 return flags;
2131}
2132
2133uint32_t SurfaceFlinger::setClientStateLocked(
2134 const sp<Client>& client,
2135 const layer_state_t& s)
2136{
2137 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002138 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002139 if (layer != 0) {
2140 const uint32_t what = s.what;
2141 if (what & layer_state_t::ePositionChanged) {
2142 if (layer->setPosition(s.x, s.y))
2143 flags |= eTraversalNeeded;
2144 }
2145 if (what & layer_state_t::eLayerChanged) {
2146 // NOTE: index needs to be calculated before we update the state
2147 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2148 if (layer->setLayer(s.z)) {
2149 mCurrentState.layersSortedByZ.removeAt(idx);
2150 mCurrentState.layersSortedByZ.add(layer);
2151 // we need traversal (state changed)
2152 // AND transaction (list changed)
2153 flags |= eTransactionNeeded|eTraversalNeeded;
2154 }
2155 }
2156 if (what & layer_state_t::eSizeChanged) {
2157 if (layer->setSize(s.w, s.h)) {
2158 flags |= eTraversalNeeded;
2159 }
2160 }
2161 if (what & layer_state_t::eAlphaChanged) {
2162 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2163 flags |= eTraversalNeeded;
2164 }
2165 if (what & layer_state_t::eMatrixChanged) {
2166 if (layer->setMatrix(s.matrix))
2167 flags |= eTraversalNeeded;
2168 }
2169 if (what & layer_state_t::eTransparentRegionChanged) {
2170 if (layer->setTransparentRegionHint(s.transparentRegion))
2171 flags |= eTraversalNeeded;
2172 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08002173 if ((what & layer_state_t::eVisibilityChanged) ||
2174 (what & layer_state_t::eOpacityChanged)) {
2175 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07002176 if (layer->setFlags(s.flags, s.mask))
2177 flags |= eTraversalNeeded;
2178 }
2179 if (what & layer_state_t::eCropChanged) {
2180 if (layer->setCrop(s.crop))
2181 flags |= eTraversalNeeded;
2182 }
2183 if (what & layer_state_t::eLayerStackChanged) {
2184 // NOTE: index needs to be calculated before we update the state
2185 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2186 if (layer->setLayerStack(s.layerStack)) {
2187 mCurrentState.layersSortedByZ.removeAt(idx);
2188 mCurrentState.layersSortedByZ.add(layer);
2189 // we need traversal (state changed)
2190 // AND transaction (list changed)
2191 flags |= eTransactionNeeded|eTraversalNeeded;
2192 }
2193 }
2194 }
2195 return flags;
2196}
2197
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002198status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002199 const String8& name,
2200 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002201 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2202 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002203{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002204 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002205 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002206 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002207 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002208 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002209 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002210
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002211 status_t result = NO_ERROR;
2212
2213 sp<Layer> layer;
2214
Mathias Agopian3165cc22012-08-08 19:42:09 -07002215 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2216 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002217 result = createNormalLayer(client,
2218 name, w, h, flags, format,
2219 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002220 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002221 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002222 result = createDimLayer(client,
2223 name, w, h, flags,
2224 handle, gbp, &layer);
2225 break;
2226 default:
2227 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002228 break;
2229 }
2230
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002231 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002232 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002233 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002234 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002235 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002236}
2237
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002238status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2239 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2240 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002241{
2242 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002243 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002244 case PIXEL_FORMAT_TRANSPARENT:
2245 case PIXEL_FORMAT_TRANSLUCENT:
2246 format = PIXEL_FORMAT_RGBA_8888;
2247 break;
2248 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002249 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002250 break;
2251 }
2252
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002253 *outLayer = new Layer(this, client, name, w, h, flags);
2254 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2255 if (err == NO_ERROR) {
2256 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002257 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002258 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002259
2260 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2261 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002262}
2263
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002264status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2265 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2266 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002267{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002268 *outLayer = new LayerDim(this, client, name, w, h, flags);
2269 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002270 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002271 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002272}
2273
Mathias Agopianac9fa422013-02-11 16:40:36 -08002274status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002275{
Mathias Agopian67106042013-03-14 19:18:13 -07002276 // called by the window manager when it wants to remove a Layer
2277 status_t err = NO_ERROR;
2278 sp<Layer> l(client->getLayerUser(handle));
2279 if (l != NULL) {
2280 err = removeLayer(l);
2281 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2282 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002283 }
2284 return err;
2285}
2286
Mathias Agopian13127d82013-03-05 17:47:11 -08002287status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002288{
Mathias Agopian67106042013-03-14 19:18:13 -07002289 // called by ~LayerCleaner() when all references to the IBinder (handle)
2290 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002291 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002292 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002293 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002294 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002295 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002296 "error removing layer=%p (%s)", l.get(), strerror(-err));
2297 }
2298 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002299}
2300
Mathias Agopianb60314a2012-04-10 22:09:54 -07002301// ---------------------------------------------------------------------------
2302
Andy McFadden13a082e2012-08-24 10:16:42 -07002303void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002304 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002305 Vector<ComposerState> state;
2306 Vector<DisplayState> displays;
2307 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002308 d.what = DisplayState::eDisplayProjectionChanged |
2309 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002310 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002311 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002312 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002313 d.frame.makeInvalid();
2314 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002315 d.width = 0;
2316 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002317 displays.add(d);
2318 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002319 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002320
2321 const nsecs_t period =
2322 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2323 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002324}
2325
2326void SurfaceFlinger::initializeDisplays() {
2327 class MessageScreenInitialized : public MessageBase {
2328 SurfaceFlinger* flinger;
2329 public:
2330 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2331 virtual bool handler() {
2332 flinger->onInitializeDisplays();
2333 return true;
2334 }
2335 };
2336 sp<MessageBase> msg = new MessageScreenInitialized(this);
2337 postMessageAsync(msg); // we may be called from main thread, use async message
2338}
2339
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002340void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2341 int mode) {
2342 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2343 this);
2344 int32_t type = hw->getDisplayType();
2345 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002346
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002347 if (mode == currentMode) {
2348 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002349 return;
2350 }
2351
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002352 hw->setPowerMode(mode);
2353 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2354 ALOGW("Trying to set power mode for virtual display");
2355 return;
2356 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002357
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002358 if (currentMode == HWC_POWER_MODE_OFF) {
2359 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002360 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2361 // FIXME: eventthread only knows about the main display right now
2362 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002363 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002364 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002365
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002366 mVisibleRegionsDirty = true;
2367 repaintEverything();
2368 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002369 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002370 disableHardwareVsync(true); // also cancels any in-progress resync
2371
Mathias Agopiancde87a32012-09-13 14:09:01 -07002372 // FIXME: eventthread only knows about the main display right now
2373 mEventThread->onScreenReleased();
2374 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002375
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002376 getHwComposer().setPowerMode(type, mode);
2377 mVisibleRegionsDirty = true;
2378 // from this point on, SF will stop drawing on this display
2379 } else {
2380 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002381 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002382}
2383
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002384void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2385 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002386 SurfaceFlinger& mFlinger;
2387 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002388 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002389 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002390 MessageSetPowerMode(SurfaceFlinger& flinger,
2391 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2392 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002393 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002394 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002395 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002396 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002397 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002398 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002399 ALOGW("Attempt to set power mode = %d for virtual display",
2400 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002401 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002402 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002403 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002404 return true;
2405 }
2406 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002407 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002408 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002409}
2410
2411// ---------------------------------------------------------------------------
2412
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002413status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2414{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002415 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002416
Mathias Agopianbd115332013-04-18 16:41:04 -07002417 IPCThreadState* ipc = IPCThreadState::self();
2418 const int pid = ipc->getCallingPid();
2419 const int uid = ipc->getCallingUid();
2420 if ((uid != AID_SHELL) &&
2421 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002422 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002423 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002424 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002425 // Try to get the main lock, but don't insist if we can't
2426 // (this would indicate SF is stuck, but we want to be able to
2427 // print something in dumpsys).
2428 int retry = 3;
2429 while (mStateLock.tryLock()<0 && --retry>=0) {
2430 usleep(1000000);
2431 }
2432 const bool locked(retry >= 0);
2433 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002434 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002435 "SurfaceFlinger appears to be unresponsive, "
2436 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002437 }
2438
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002439 bool dumpAll = true;
2440 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002441 size_t numArgs = args.size();
2442 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002443 if ((index < numArgs) &&
2444 (args[index] == String16("--list"))) {
2445 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002446 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002447 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002448 }
2449
2450 if ((index < numArgs) &&
2451 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002452 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002453 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002454 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002455 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002456
2457 if ((index < numArgs) &&
2458 (args[index] == String16("--latency-clear"))) {
2459 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002460 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002461 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002462 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002463
2464 if ((index < numArgs) &&
2465 (args[index] == String16("--dispsync"))) {
2466 index++;
2467 mPrimaryDispSync.dump(result);
2468 dumpAll = false;
2469 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002470 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002471
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002472 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002473 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002474 }
2475
Mathias Agopian9795c422009-08-26 16:36:26 -07002476 if (locked) {
2477 mStateLock.unlock();
2478 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002479 }
2480 write(fd, result.string(), result.size());
2481 return NO_ERROR;
2482}
2483
Dan Stozac7014012014-02-14 15:03:43 -08002484void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2485 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002486{
2487 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2488 const size_t count = currentLayers.size();
2489 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002490 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002491 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002492 }
2493}
2494
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002495void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002496 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002497{
2498 String8 name;
2499 if (index < args.size()) {
2500 name = String8(args[index]);
2501 index++;
2502 }
2503
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002504 const nsecs_t period =
2505 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002506 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002507
2508 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002509 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002510 } else {
2511 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2512 const size_t count = currentLayers.size();
2513 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002514 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002515 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002516 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002517 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002518 }
2519 }
2520}
2521
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002522void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002523 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002524{
2525 String8 name;
2526 if (index < args.size()) {
2527 name = String8(args[index]);
2528 index++;
2529 }
2530
2531 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2532 const size_t count = currentLayers.size();
2533 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002534 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002535 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002536 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002537 }
2538 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002539
Svetoslavd85084b2014-03-20 10:28:31 -07002540 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002541}
2542
Jamie Gennis6547ff42013-07-16 20:12:42 -07002543// This should only be called from the main thread. Otherwise it would need
2544// the lock and should use mCurrentState rather than mDrawingState.
2545void SurfaceFlinger::logFrameStats() {
2546 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2547 const size_t count = drawingLayers.size();
2548 for (size_t i=0 ; i<count ; i++) {
2549 const sp<Layer>& layer(drawingLayers[i]);
2550 layer->logFrameStats();
2551 }
2552
2553 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2554}
2555
Andy McFadden4803b742012-09-24 19:07:20 -07002556/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2557{
2558 static const char* config =
2559 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002560#ifdef HAS_CONTEXT_PRIORITY
2561 " HAS_CONTEXT_PRIORITY"
2562#endif
2563#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2564 " NEVER_DEFAULT_TO_ASYNC_MODE"
2565#endif
2566#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2567 " TARGET_DISABLE_TRIPLE_BUFFERING"
2568#endif
2569 "]";
2570 result.append(config);
2571}
2572
Mathias Agopian74d211a2013-04-22 16:55:35 +02002573void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2574 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002575{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002576 bool colorize = false;
2577 if (index < args.size()
2578 && (args[index] == String16("--color"))) {
2579 colorize = true;
2580 index++;
2581 }
2582
2583 Colorizer colorizer(colorize);
2584
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002585 // figure out if we're stuck somewhere
2586 const nsecs_t now = systemTime();
2587 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2588 const nsecs_t inTransaction(mDebugInTransaction);
2589 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2590 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2591
2592 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002593 * Dump library configuration.
2594 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002595
2596 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002597 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002598 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002599 appendSfConfigString(result);
2600 appendUiConfigString(result);
2601 appendGuiConfigString(result);
2602 result.append("\n");
2603
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002604 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002605 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002606 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002607 result.append(SyncFeatures::getInstance().toString());
2608 result.append("\n");
2609
Andy McFadden41d67d72014-04-25 16:58:34 -07002610 colorizer.bold(result);
2611 result.append("DispSync configuration: ");
2612 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002613 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2614 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002615 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2616 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2617 result.append("\n");
2618
Andy McFadden4803b742012-09-24 19:07:20 -07002619 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002620 * Dump the visible layer list
2621 */
2622 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2623 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002624 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002625 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002626 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002627 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002628 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002629 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002630 }
2631
2632 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002633 * Dump Display state
2634 */
2635
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002636 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002637 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002638 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002639 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2640 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002641 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002642 }
2643
2644 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002645 * Dump SurfaceFlinger global state
2646 */
2647
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002648 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002649 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002650 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002651
Mathias Agopian888c8222012-08-04 21:10:38 -07002652 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002653 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002654
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002655 colorizer.bold(result);
2656 result.appendFormat("EGL implementation : %s\n",
2657 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2658 colorizer.reset(result);
2659 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002660 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002661
Mathias Agopian875d8e12013-06-07 15:35:48 -07002662 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002663
Mathias Agopian42977342012-08-05 00:40:46 -07002664 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002665 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2666 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002667 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002668 " last eglSwapBuffers() time: %f us\n"
2669 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002670 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002671 " refresh-rate : %f fps\n"
2672 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002673 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002674 " gpu_to_cpu_unsupported : %d\n"
2675 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002676 mLastSwapBufferTime/1000.0,
2677 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002678 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002679 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2680 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002681 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002682 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002683
Mathias Agopian74d211a2013-04-22 16:55:35 +02002684 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002685 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002686
Mathias Agopian74d211a2013-04-22 16:55:35 +02002687 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002688 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002689
2690 /*
2691 * VSYNC state
2692 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002693 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002694
2695 /*
2696 * Dump HWComposer state
2697 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002698 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002699 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002700 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002701 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002702 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002703 (mDebugDisableHWC || mDebugRegion || mDaltonize
2704 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002705 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002706
2707 /*
2708 * Dump gralloc state
2709 */
2710 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2711 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002712}
2713
Mathias Agopian13127d82013-03-05 17:47:11 -08002714const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002715SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002716 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002717 wp<IBinder> dpy;
2718 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2719 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2720 dpy = mDisplays.keyAt(i);
2721 break;
2722 }
2723 }
2724 if (dpy == NULL) {
2725 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2726 // Just use the primary display so we have something to return
2727 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2728 }
2729 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002730}
2731
Keun young Park63f165f2012-08-31 10:53:36 -07002732bool SurfaceFlinger::startDdmConnection()
2733{
2734 void* libddmconnection_dso =
2735 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2736 if (!libddmconnection_dso) {
2737 return false;
2738 }
2739 void (*DdmConnection_start)(const char* name);
2740 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07002741 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07002742 if (!DdmConnection_start) {
2743 dlclose(libddmconnection_dso);
2744 return false;
2745 }
2746 (*DdmConnection_start)(getServiceName());
2747 return true;
2748}
2749
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002750status_t SurfaceFlinger::onTransact(
2751 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2752{
2753 switch (code) {
2754 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002755 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002756 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002757 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002758 case CLEAR_ANIMATION_FRAME_STATS:
2759 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002760 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002761 {
2762 // codes that require permission check
2763 IPCThreadState* ipc = IPCThreadState::self();
2764 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002765 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002766 if ((uid != AID_GRAPHICS) &&
2767 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002768 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002769 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2770 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002771 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002772 break;
2773 }
2774 case CAPTURE_SCREEN:
2775 {
2776 // codes that require permission check
2777 IPCThreadState* ipc = IPCThreadState::self();
2778 const int pid = ipc->getCallingPid();
2779 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002780 if ((uid != AID_GRAPHICS) &&
2781 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002782 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002783 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2784 return PERMISSION_DENIED;
2785 }
2786 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002787 }
2788 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002789
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002790 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2791 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002792 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002793 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002794 IPCThreadState* ipc = IPCThreadState::self();
2795 const int pid = ipc->getCallingPid();
2796 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002797 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002798 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002799 return PERMISSION_DENIED;
2800 }
2801 int n;
2802 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002803 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002804 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002805 return NO_ERROR;
2806 case 1002: // SHOW_UPDATES
2807 n = data.readInt32();
2808 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002809 invalidateHwcGeometry();
2810 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002811 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002812 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002813 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002814 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002815 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002816 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002817 setTransactionFlags(
2818 eTransactionNeeded|
2819 eDisplayTransactionNeeded|
2820 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002821 return NO_ERROR;
2822 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002823 case 1006:{ // send empty update
2824 signalRefresh();
2825 return NO_ERROR;
2826 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002827 case 1008: // toggle use of hw composer
2828 n = data.readInt32();
2829 mDebugDisableHWC = n ? 1 : 0;
2830 invalidateHwcGeometry();
2831 repaintEverything();
2832 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002833 case 1009: // toggle use of transform hint
2834 n = data.readInt32();
2835 mDebugDisableTransformHint = n ? 1 : 0;
2836 invalidateHwcGeometry();
2837 repaintEverything();
2838 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002839 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002840 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002841 reply->writeInt32(0);
2842 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002843 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002844 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002845 return NO_ERROR;
2846 case 1013: {
2847 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002848 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2849 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002850 return NO_ERROR;
2851 }
2852 case 1014: {
2853 // daltonize
2854 n = data.readInt32();
2855 switch (n % 10) {
2856 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2857 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2858 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2859 }
2860 if (n >= 10) {
2861 mDaltonizer.setMode(Daltonizer::correction);
2862 } else {
2863 mDaltonizer.setMode(Daltonizer::simulation);
2864 }
2865 mDaltonize = n > 0;
2866 invalidateHwcGeometry();
2867 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002868 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002869 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002870 case 1015: {
2871 // apply a color matrix
2872 n = data.readInt32();
2873 mHasColorMatrix = n ? 1 : 0;
2874 if (n) {
2875 // color matrix is sent as mat3 matrix followed by vec3
2876 // offset, then packed into a mat4 where the last row is
2877 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002878 for (size_t i = 0 ; i < 4; i++) {
2879 for (size_t j = 0; j < 4; j++) {
2880 mColorMatrix[i][j] = data.readFloat();
2881 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002882 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002883 } else {
2884 mColorMatrix = mat4();
2885 }
2886 invalidateHwcGeometry();
2887 repaintEverything();
2888 return NO_ERROR;
2889 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002890 // This is an experimental interface
2891 // Needs to be shifted to proper binder interface when we productize
2892 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07002893 n = data.readInt32();
2894 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002895 return NO_ERROR;
2896 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002897 }
2898 }
2899 return err;
2900}
2901
Mathias Agopian53331da2011-08-22 21:44:41 -07002902void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002903 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002904 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002905}
2906
Mathias Agopian59119e62010-10-11 12:37:43 -07002907// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002908// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002909// ---------------------------------------------------------------------------
2910
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002911/* The code below is here to handle b/8734824
2912 *
2913 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07002914 * from the surfaceflinger thread to the calling binder thread, where they
2915 * are executed. This allows the calling thread in the calling process to be
2916 * reused and not depend on having "enough" binder threads to handle the
2917 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002918 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002919class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07002920 /* Parts of GraphicProducerWrapper are run on two different threads,
2921 * communicating by sending messages via Looper but also by shared member
2922 * data. Coherence maintenance is subtle and in places implicit (ugh).
2923 *
2924 * Don't rely on Looper's sendMessage/handleMessage providing
2925 * release/acquire semantics for any data not actually in the Message.
2926 * Data going from surfaceflinger to binder threads needs to be
2927 * synchronized explicitly.
2928 *
2929 * Barrier open/wait do provide release/acquire semantics. This provides
2930 * implicit synchronization for data coming back from binder to
2931 * surfaceflinger threads.
2932 */
2933
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002934 sp<IGraphicBufferProducer> impl;
2935 sp<Looper> looper;
2936 status_t result;
2937 bool exitPending;
2938 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07002939 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002940 uint32_t code;
2941 Parcel const* data;
2942 Parcel* reply;
2943
2944 enum {
2945 MSG_API_CALL,
2946 MSG_EXIT
2947 };
2948
2949 /*
Jesse Hallb154c422014-07-13 12:47:02 -07002950 * Called on surfaceflinger thread. This is called by our "fake"
2951 * BpGraphicBufferProducer. We package the data and reply Parcel and
2952 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002953 */
2954 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08002955 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002956 this->code = code;
2957 this->data = &data;
2958 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002959 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07002960 // if we've exited, we run the message synchronously right here.
2961 // note (JH): as far as I can tell from looking at the code, this
2962 // never actually happens. if it does, i'm not sure if it happens
2963 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002964 handleMessage(Message(MSG_API_CALL));
2965 } else {
2966 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07002967 // Prevent stores to this->{code, data, reply} from being
2968 // reordered later than the construction of Message.
2969 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002970 looper->sendMessage(this, Message(MSG_API_CALL));
2971 barrier.wait();
2972 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08002973 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002974 }
2975
2976 /*
Jesse Hallb154c422014-07-13 12:47:02 -07002977 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002978 * call the real BpGraphicBufferProducer.
2979 */
2980 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07002981 int what = message.what;
2982 // Prevent reads below from happening before the read from Message
2983 atomic_thread_fence(memory_order_acquire);
2984 if (what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08002985 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002986 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07002987 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002988 exitRequested = true;
2989 }
2990 }
2991
2992public:
Jesse Hallb154c422014-07-13 12:47:02 -07002993 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
2994 : impl(impl),
2995 looper(new Looper(true)),
2996 exitPending(false),
2997 exitRequested(false)
2998 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002999
Jesse Hallb154c422014-07-13 12:47:02 -07003000 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003001 status_t waitForResponse() {
3002 do {
3003 looper->pollOnce(-1);
3004 } while (!exitRequested);
3005 return result;
3006 }
3007
Jesse Hallb154c422014-07-13 12:47:02 -07003008 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003009 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07003010 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003011 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07003012 // Ensure this->result is visible to the binder thread before it
3013 // handles the message.
3014 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003015 looper->sendMessage(this, Message(MSG_EXIT));
3016 }
3017};
3018
3019
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003020status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
3021 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003022 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003023 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003024 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003025
3026 if (CC_UNLIKELY(display == 0))
3027 return BAD_VALUE;
3028
3029 if (CC_UNLIKELY(producer == 0))
3030 return BAD_VALUE;
3031
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003032 // if we have secure windows on this display, never allow the screen capture
3033 // unless the producer interface is local (i.e.: we can take a screenshot for
3034 // ourselves).
3035 if (!producer->asBinder()->localBinder()) {
3036 Mutex::Autolock _l(mStateLock);
3037 sp<const DisplayDevice> hw(getDisplayDevice(display));
3038 if (hw->getSecureLayerVisible()) {
3039 ALOGW("FB is protected: PERMISSION_DENIED");
3040 return PERMISSION_DENIED;
3041 }
3042 }
3043
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003044 // Convert to surfaceflinger's internal rotation type.
3045 Transform::orientation_flags rotationFlags;
3046 switch (rotation) {
3047 case ISurfaceComposer::eRotateNone:
3048 rotationFlags = Transform::ROT_0;
3049 break;
3050 case ISurfaceComposer::eRotate90:
3051 rotationFlags = Transform::ROT_90;
3052 break;
3053 case ISurfaceComposer::eRotate180:
3054 rotationFlags = Transform::ROT_180;
3055 break;
3056 case ISurfaceComposer::eRotate270:
3057 rotationFlags = Transform::ROT_270;
3058 break;
3059 default:
3060 rotationFlags = Transform::ROT_0;
3061 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3062 break;
3063 }
3064
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003065 class MessageCaptureScreen : public MessageBase {
3066 SurfaceFlinger* flinger;
3067 sp<IBinder> display;
3068 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003069 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003070 uint32_t reqWidth, reqHeight;
3071 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003072 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003073 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003074 status_t result;
3075 public:
3076 MessageCaptureScreen(SurfaceFlinger* flinger,
3077 const sp<IBinder>& display,
3078 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003079 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003080 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003081 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003082 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003083 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003084 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003085 useIdentityTransform(useIdentityTransform),
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003086 rotation(rotation),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003087 result(PERMISSION_DENIED)
3088 {
3089 }
3090 status_t getResult() const {
3091 return result;
3092 }
3093 virtual bool handler() {
3094 Mutex::Autolock _l(flinger->mStateLock);
3095 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003096 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003097 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003098 useIdentityTransform, rotation);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003099 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003100 return true;
3101 }
3102 };
3103
Mathias Agopian9eb1f052013-04-10 16:27:17 -07003104 // make sure to process transactions before screenshots -- a transaction
3105 // might already be pending but scheduled for VSYNC; this guarantees we
3106 // will handle it before the screenshot. When VSYNC finally arrives
3107 // the scheduled transaction will be a no-op. If no transactions are
3108 // scheduled at this time, this will end-up being a no-op as well.
3109 mEventQueue.invalidateTransactionNow();
3110
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003111 // this creates a "fake" BBinder which will serve as a "fake" remote
3112 // binder to receive the marshaled calls and forward them to the
3113 // real remote (a BpGraphicBufferProducer)
3114 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3115
3116 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3117 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003118 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003119 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003120 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003121 useIdentityTransform, rotationFlags);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003122
3123 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003124 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003125 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003126 }
3127 return res;
3128}
3129
Mathias Agopian180f10d2013-04-10 22:55:41 -07003130
3131void SurfaceFlinger::renderScreenImplLocked(
3132 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003133 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003134 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003135 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003136{
3137 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003138 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003139
3140 // get screen geometry
3141 const uint32_t hw_w = hw->getWidth();
3142 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003143 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
3144
Dan Stozac1879002014-05-22 15:59:05 -07003145 // if a default or invalid sourceCrop is passed in, set reasonable values
3146 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3147 !sourceCrop.isValid()) {
3148 sourceCrop.setLeftTop(Point(0, 0));
3149 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3150 }
3151
3152 // ensure that sourceCrop is inside screen
3153 if (sourceCrop.left < 0) {
3154 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3155 }
Dan Stozabe31f442014-06-11 11:20:54 -07003156 if (sourceCrop.right > hw_w) {
3157 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003158 }
3159 if (sourceCrop.top < 0) {
3160 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3161 }
Dan Stozabe31f442014-06-11 11:20:54 -07003162 if (sourceCrop.bottom > hw_h) {
3163 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003164 }
3165
Mathias Agopian180f10d2013-04-10 22:55:41 -07003166 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003167 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003168
3169 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003170 engine.setViewportAndProjection(
3171 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003172 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003173
3174 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003175 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003176
3177 const LayerVector& layers( mDrawingState.layersSortedByZ );
3178 const size_t count = layers.size();
3179 for (size_t i=0 ; i<count ; ++i) {
3180 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003181 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003182 if (state.layerStack == hw->getLayerStack()) {
3183 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3184 if (layer->isVisible()) {
3185 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08003186 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003187 if (filtering) layer->setFiltering(false);
3188 }
3189 }
3190 }
3191 }
3192
3193 // compositionComplete is needed for older driver
3194 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003195 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003196}
3197
3198
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003199status_t SurfaceFlinger::captureScreenImplLocked(
3200 const sp<const DisplayDevice>& hw,
3201 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003202 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003203 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003204 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003205{
3206 ATRACE_CALL();
3207
Mathias Agopian180f10d2013-04-10 22:55:41 -07003208 // get screen geometry
3209 const uint32_t hw_w = hw->getWidth();
3210 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003211
Mathias Agopian180f10d2013-04-10 22:55:41 -07003212 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3213 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3214 reqWidth, reqHeight, hw_w, hw_h);
3215 return BAD_VALUE;
3216 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003217
Mathias Agopian180f10d2013-04-10 22:55:41 -07003218 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3219 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3220
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003221 // create a surface (because we're a producer, and we need to
3222 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003223 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003224 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003225
3226 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003227 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003228 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3229 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003230
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003231 int err = 0;
3232 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003233 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003234 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3235 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003236
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003237 if (err == NO_ERROR) {
3238 ANativeWindowBuffer* buffer;
3239 /* TODO: Once we have the sync framework everywhere this can use
3240 * server-side waits on the fence that dequeueBuffer returns.
3241 */
3242 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3243 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003244 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003245 // create an EGLImage from the buffer so we can later
3246 // turn it into a texture
3247 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3248 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3249 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003250 // this binds the given EGLImage as a framebuffer for the
3251 // duration of this scope.
3252 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3253 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003254 // this will in fact render into our dequeued buffer
3255 // via an FBO, which means we didn't have to create
3256 // an EGLSurface and therefore we're not
3257 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003258 renderScreenImplLocked(
3259 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3260 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003261
Riley Andrews86639902014-08-15 12:27:24 -07003262 // Attempt to create a sync khr object that can produce a sync point. If that
3263 // isn't available, create a non-dupable sync object in the fallback path and
3264 // wait on it directly.
3265 EGLSyncKHR sync;
3266 if (!DEBUG_SCREENSHOTS) {
3267 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Andy McFadden2d8d1202013-10-09 16:38:02 -07003268 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003269 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003270 }
Riley Andrews86639902014-08-15 12:27:24 -07003271 if (sync != EGL_NO_SYNC_KHR) {
3272 // get the sync fd
3273 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3274 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3275 ALOGW("captureScreen: failed to dup sync khr object");
3276 syncFd = -1;
3277 }
3278 eglDestroySyncKHR(mEGLDisplay, sync);
3279 } else {
3280 // fallback path
3281 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3282 if (sync != EGL_NO_SYNC_KHR) {
3283 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3284 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3285 EGLint eglErr = eglGetError();
3286 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3287 ALOGW("captureScreen: fence wait timed out");
3288 } else {
3289 ALOGW_IF(eglErr != EGL_SUCCESS,
3290 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3291 }
3292 eglDestroySyncKHR(mEGLDisplay, sync);
3293 } else {
3294 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3295 }
3296 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003297 if (DEBUG_SCREENSHOTS) {
3298 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3299 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3300 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3301 hw, minLayerZ, maxLayerZ);
3302 delete [] pixels;
3303 }
3304
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003305 } else {
3306 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3307 result = INVALID_OPERATION;
3308 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003309 // destroy our image
3310 eglDestroyImageKHR(mEGLDisplay, image);
3311 } else {
3312 result = BAD_VALUE;
3313 }
Jesse Hallafe2b1f2014-10-21 11:09:17 -07003314 // queueBuffer takes ownership of syncFd
Riley Andrews86639902014-08-15 12:27:24 -07003315 window->queueBuffer(window, buffer, syncFd);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003316 }
3317 } else {
3318 result = BAD_VALUE;
3319 }
3320 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3321 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003322
Mathias Agopian74c40c02010-09-29 13:02:36 -07003323 return result;
3324}
3325
Mathias Agopiand5556842013-09-19 17:08:37 -07003326void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3327 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003328 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003329 for (size_t y=0 ; y<h ; y++) {
3330 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3331 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003332 if (p[x] != 0xFF000000) return;
3333 }
3334 }
3335 ALOGE("*** we just took a black screenshot ***\n"
3336 "requested minz=%d, maxz=%d, layerStack=%d",
3337 minLayerZ, maxLayerZ, hw->getLayerStack());
3338 const LayerVector& layers( mDrawingState.layersSortedByZ );
3339 const size_t count = layers.size();
3340 for (size_t i=0 ; i<count ; ++i) {
3341 const sp<Layer>& layer(layers[i]);
3342 const Layer::State& state(layer->getDrawingState());
3343 const bool visible = (state.layerStack == hw->getLayerStack())
3344 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3345 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003346 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003347 visible ? '+' : '-',
3348 i, layer->getName().string(), state.layerStack, state.z,
3349 layer->isVisible(), state.flags, state.alpha);
3350 }
3351 }
3352}
3353
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003354// ---------------------------------------------------------------------------
3355
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003356SurfaceFlinger::LayerVector::LayerVector() {
3357}
3358
3359SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003360 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003361}
3362
3363int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3364 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003365{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003366 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003367 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3368 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003369
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003370 uint32_t ls = l->getCurrentState().layerStack;
3371 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003372 if (ls != rs)
3373 return ls - rs;
3374
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003375 uint32_t lz = l->getCurrentState().z;
3376 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003377 if (lz != rz)
3378 return lz - rz;
3379
3380 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003381}
3382
3383// ---------------------------------------------------------------------------
3384
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003385SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Michael Lentine47e45402014-07-18 15:34:25 -07003386 : type(DisplayDevice::DISPLAY_ID_INVALID), width(0), height(0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003387}
3388
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003389SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Michael Lentine47e45402014-07-18 15:34:25 -07003390 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0), width(0), height(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003391 viewport.makeInvalid();
3392 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003393}
3394
3395// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003396
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003397}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003398
3399
3400#if defined(__gl_h_)
3401#error "don't include gl/gl.h in this file"
3402#endif
3403
3404#if defined(__gl2_h_)
3405#error "don't include gl2/gl2.h in this file"
3406#endif