blob: 1060b096a94e102cfa4f2cabef3da2bbddec367c [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 Hall692c7232012-11-08 15:41:56 -0800391 Mutex::Autolock _l(mStateLock);
392
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700393 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700394 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
395 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700396
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700397 // Initialize the H/W composer object. There may or may not be an
398 // actual hardware composer underneath.
399 mHwc = new HWComposer(this,
400 *static_cast<HWComposer::EventHandler *>(this));
401
Mathias Agopian875d8e12013-06-07 15:35:48 -0700402 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800403 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700404
405 // retrieve the EGL context that was selected/created
406 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700407
Mathias Agopianda27af92012-09-13 18:17:13 -0700408 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
409 "couldn't create EGLContext");
410
Mathias Agopiancde87a32012-09-13 14:09:01 -0700411 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700412 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700413 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700414 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700415 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700416 // All non-virtual displays are currently considered secure.
417 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800418 createBuiltinDisplayLocked(type);
419 wp<IBinder> token = mBuiltinDisplays[i];
420
Dan Stozab9b08832014-03-13 11:55:57 -0700421 sp<IGraphicBufferProducer> producer;
422 sp<IGraphicBufferConsumer> consumer;
423 BufferQueue::createBufferQueue(&producer, &consumer,
424 new GraphicBufferAlloc());
425
426 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
427 consumer);
Jesse Hall19e87292013-12-23 21:02:15 -0800428 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700429 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800430 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Dan Stozab9b08832014-03-13 11:55:57 -0700431 fbs, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -0800432 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700433 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700434 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700435 // for displays other than the main display, so we always
436 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800437 ALOGD("marking display %zu as acquired/unblanked", i);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700438 hw->setPowerMode(HWC_POWER_MODE_NORMAL);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700439 }
440 mDisplays.add(token, hw);
441 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700442 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700443
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700444 // make the GLContext current so that we can create textures when creating Layers
445 // (which may happens before we render something)
446 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
447
Mathias Agopian028508c2012-07-25 21:12:12 -0700448 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700449 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700450 vsyncPhaseOffsetNs, true, "app");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700451 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700452 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700453 sfVsyncPhaseOffsetNs, true, "sf");
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700454 mSFEventThread = new EventThread(sfVsyncSrc);
455 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700456
Jamie Gennisd1700752013-10-14 12:22:52 -0700457 mEventControlThread = new EventControlThread(this);
458 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
459
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700460 // set a fake vsync period if there is no HWComposer
461 if (mHwc->initCheck() != NO_ERROR) {
462 mPrimaryDispSync.setPeriod(16666667);
463 }
464
Mathias Agopian92a979a2012-08-02 18:32:23 -0700465 // initialize our drawing state
466 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700467
Andy McFadden13a082e2012-08-24 10:16:42 -0700468 // set initial conditions (e.g. unblank default device)
469 initializeDisplays();
470
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700471 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700472 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800473}
474
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700475int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700476 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700477 type : mHwc->allocateDisplayId();
478}
479
Mathias Agopiana67e4182012-06-19 17:26:12 -0700480void SurfaceFlinger::startBootAnim() {
481 // start boot animation
482 property_set("service.bootanim.exit", "0");
483 property_set("ctl.start", "bootanim");
484}
485
Mathias Agopian875d8e12013-06-07 15:35:48 -0700486size_t SurfaceFlinger::getMaxTextureSize() const {
487 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700488}
489
Mathias Agopian875d8e12013-06-07 15:35:48 -0700490size_t SurfaceFlinger::getMaxViewportDims() const {
491 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700492}
493
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800494// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800495
Jamie Gennis582270d2011-08-17 18:19:00 -0700496bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800497 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800498 Mutex::Autolock _l(mStateLock);
Marco Nelissen2ea926b2014-11-14 08:01:01 -0800499 sp<IBinder> surfaceTextureBinder(IInterface::asBinder(bufferProducer));
Mathias Agopian67106042013-03-14 19:18:13 -0700500 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800501}
502
Dan Stoza7f7da322014-05-02 15:26:25 -0700503status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
504 Vector<DisplayInfo>* configs) {
505 if (configs == NULL) {
506 return BAD_VALUE;
507 }
508
Naseer Ahmed7aa0c472014-11-03 14:49:23 -0500509 if (!display.get())
510 return NAME_NOT_FOUND;
511
Jesse Hall692c7232012-11-08 15:41:56 -0800512 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700513 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800514 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700515 type = i;
516 break;
517 }
518 }
519
520 if (type < 0) {
521 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700522 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700523
Mathias Agopian8b736f12012-08-13 17:54:26 -0700524 // TODO: Not sure if display density should handled by SF any longer
525 class Density {
526 static int getDensityFromProperty(char const* propName) {
527 char property[PROPERTY_VALUE_MAX];
528 int density = 0;
529 if (property_get(propName, property, NULL) > 0) {
530 density = atoi(property);
531 }
532 return density;
533 }
534 public:
535 static int getEmuDensity() {
536 return getDensityFromProperty("qemu.sf.lcd_density"); }
537 static int getBuildDensity() {
538 return getDensityFromProperty("ro.sf.lcd_density"); }
539 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700540
Dan Stoza7f7da322014-05-02 15:26:25 -0700541 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700542
Dan Stoza7f7da322014-05-02 15:26:25 -0700543 const Vector<HWComposer::DisplayConfig>& hwConfigs =
544 getHwComposer().getConfigs(type);
545 for (size_t c = 0; c < hwConfigs.size(); ++c) {
546 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
547 DisplayInfo info = DisplayInfo();
548
549 float xdpi = hwConfig.xdpi;
550 float ydpi = hwConfig.ydpi;
551
552 if (type == DisplayDevice::DISPLAY_PRIMARY) {
553 // The density of the device is provided by a build property
554 float density = Density::getBuildDensity() / 160.0f;
555 if (density == 0) {
556 // the build doesn't provide a density -- this is wrong!
557 // use xdpi instead
558 ALOGE("ro.sf.lcd_density must be defined as a build property");
559 density = xdpi / 160.0f;
560 }
561 if (Density::getEmuDensity()) {
562 // if "qemu.sf.lcd_density" is specified, it overrides everything
563 xdpi = ydpi = density = Density::getEmuDensity();
564 density /= 160.0f;
565 }
566 info.density = density;
567
568 // TODO: this needs to go away (currently needed only by webkit)
569 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
570 info.orientation = hw->getOrientation();
571 } else {
572 // TODO: where should this value come from?
573 static const int TV_DENSITY = 213;
574 info.density = TV_DENSITY / 160.0f;
575 info.orientation = 0;
576 }
577
578 info.w = hwConfig.width;
579 info.h = hwConfig.height;
580 info.xdpi = xdpi;
581 info.ydpi = ydpi;
582 info.fps = float(1e9 / hwConfig.refresh);
Andy McFadden91b2ca82014-06-13 14:04:23 -0700583 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
584
585 // This is how far in advance a buffer must be queued for
586 // presentation at a given time. If you want a buffer to appear
587 // on the screen at time N, you must submit the buffer before
588 // (N - presentationDeadline).
589 //
590 // Normally it's one full refresh period (to give SF a chance to
591 // latch the buffer), but this can be reduced by configuring a
592 // DispSync offset. Any additional delays introduced by the hardware
593 // composer or panel must be accounted for here.
594 //
595 // We add an additional 1ms to allow for processing time and
596 // differences between the ideal and actual refresh rate.
597 info.presentationDeadline =
598 hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700599
600 // All non-virtual displays are currently considered secure.
601 info.secure = true;
602
603 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700604 }
605
Dan Stoza7f7da322014-05-02 15:26:25 -0700606 return NO_ERROR;
607}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700608
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800609status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& /* display */,
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700610 DisplayStatInfo* stats) {
611 if (stats == NULL) {
612 return BAD_VALUE;
613 }
614
615 // FIXME for now we always return stats for the primary display
616 memset(stats, 0, sizeof(*stats));
617 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
618 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
619 return NO_ERROR;
620}
621
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700622int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
623 return getDisplayDevice(display)->getActiveConfig();
Dan Stoza7f7da322014-05-02 15:26:25 -0700624}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700625
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700626void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
627 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
628 this);
629 int32_t type = hw->getDisplayType();
630 int currentMode = hw->getActiveConfig();
631
632 if (mode == currentMode) {
633 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
634 return;
635 }
636
637 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
638 ALOGW("Trying to set config for virtual display");
639 return;
640 }
641
642 hw->setActiveConfig(mode);
643 getHwComposer().setActiveConfig(type, mode);
644}
645
646status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
647 class MessageSetActiveConfig: public MessageBase {
648 SurfaceFlinger& mFlinger;
649 sp<IBinder> mDisplay;
650 int mMode;
651 public:
652 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
653 int mode) :
654 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
655 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700656 Vector<DisplayInfo> configs;
657 mFlinger.getDisplayConfigs(mDisplay, &configs);
Jesse Hall78442112014-08-07 22:43:06 -0700658 if (mMode < 0 || mMode >= static_cast<int>(configs.size())) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700659 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700660 mMode, configs.size());
661 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700662 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
663 if (hw == NULL) {
664 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700665 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700666 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
667 ALOGW("Attempt to set active config = %d for virtual display",
668 mMode);
669 } else {
670 mFlinger.setActiveConfigInternal(hw, mMode);
671 }
672 return true;
673 }
674 };
675 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
676 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700677 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700678}
679
Svetoslavd85084b2014-03-20 10:28:31 -0700680status_t SurfaceFlinger::clearAnimationFrameStats() {
681 Mutex::Autolock _l(mStateLock);
682 mAnimFrameTracker.clearStats();
683 return NO_ERROR;
684}
685
686status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
687 Mutex::Autolock _l(mStateLock);
688 mAnimFrameTracker.getStats(outStats);
689 return NO_ERROR;
690}
691
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800692// ----------------------------------------------------------------------------
693
694sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800695 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700696}
697
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800698// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800699
700void SurfaceFlinger::waitForEvent() {
701 mEventQueue.waitMessage();
702}
703
704void SurfaceFlinger::signalTransaction() {
705 mEventQueue.invalidate();
706}
707
708void SurfaceFlinger::signalLayerUpdate() {
709 mEventQueue.invalidate();
710}
711
712void SurfaceFlinger::signalRefresh() {
713 mEventQueue.refresh();
714}
715
716status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800717 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800718 return mEventQueue.postMessage(msg, reltime);
719}
720
721status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800722 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800723 status_t res = mEventQueue.postMessage(msg, reltime);
724 if (res == NO_ERROR) {
725 msg->wait();
726 }
727 return res;
728}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800729
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700730void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700731 do {
732 waitForEvent();
733 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800734}
735
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700736void SurfaceFlinger::enableHardwareVsync() {
737 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700738 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700739 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700740 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
741 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700742 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700743 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700744}
745
Jesse Hall948fe0c2013-10-14 12:56:09 -0700746void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700747 Mutex::Autolock _l(mHWVsyncLock);
748
Jesse Hall948fe0c2013-10-14 12:56:09 -0700749 if (makeAvailable) {
750 mHWVsyncAvailable = true;
751 } else if (!mHWVsyncAvailable) {
752 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
753 return;
754 }
755
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700756 const nsecs_t period =
757 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
758
759 mPrimaryDispSync.reset();
760 mPrimaryDispSync.setPeriod(period);
761
762 if (!mPrimaryHWVsyncEnabled) {
763 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700764 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
765 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700766 mPrimaryHWVsyncEnabled = true;
767 }
768}
769
Jesse Hall948fe0c2013-10-14 12:56:09 -0700770void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700771 Mutex::Autolock _l(mHWVsyncLock);
772 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700773 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
774 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700775 mPrimaryDispSync.endResync();
776 mPrimaryHWVsyncEnabled = false;
777 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700778 if (makeUnavailable) {
779 mHWVsyncAvailable = false;
780 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700781}
782
783void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700784 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700785
Jamie Gennisd1700752013-10-14 12:22:52 -0700786 { // Scope for the lock
787 Mutex::Autolock _l(mHWVsyncLock);
788 if (type == 0 && mPrimaryHWVsyncEnabled) {
789 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700790 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700791 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700792
793 if (needsHwVsync) {
794 enableHardwareVsync();
795 } else {
796 disableHardwareVsync(false);
797 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700798}
799
800void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
801 if (mEventThread == NULL) {
802 // This is a temporary workaround for b/7145521. A non-null pointer
803 // does not mean EventThread has finished initializing, so this
804 // is not a correct fix.
805 ALOGW("WARNING: EventThread not started, ignoring hotplug");
806 return;
807 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700808
Jesse Hall9e663de2013-08-16 14:28:37 -0700809 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700810 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800811 if (connected) {
812 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700813 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800814 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
815 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700816 }
817 setTransactionFlags(eDisplayTransactionNeeded);
818
Andy McFadden9e9689c2012-10-10 18:17:51 -0700819 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700820 }
Mathias Agopian86303202012-07-24 22:46:10 -0700821}
822
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700823void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700824 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700825 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700826}
827
Mathias Agopian4fec8732012-06-29 14:12:52 -0700828void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800829 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800830 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700831 case MessageQueue::TRANSACTION:
832 handleMessageTransaction();
833 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700834 case MessageQueue::INVALIDATE:
835 handleMessageTransaction();
836 handleMessageInvalidate();
837 signalRefresh();
838 break;
839 case MessageQueue::REFRESH:
840 handleMessageRefresh();
841 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800842 }
843}
844
Mathias Agopian4fec8732012-06-29 14:12:52 -0700845void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700846 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700847 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700848 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700849 }
850}
851
852void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700853 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700854 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700855}
856
857void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700858 ATRACE_CALL();
859 preComposition();
860 rebuildLayerStacks();
861 setUpHWComposer();
862 doDebugFlashRegions();
863 doComposition();
864 postComposition();
865}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700866
Mathias Agopiancd60f992012-08-16 16:28:27 -0700867void SurfaceFlinger::doDebugFlashRegions()
868{
869 // is debugging enabled
870 if (CC_LIKELY(!mDebugRegion))
871 return;
872
873 const bool repaintEverything = mRepaintEverything;
874 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
875 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700876 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700877 // transform the dirty region into this screen's coordinate space
878 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
879 if (!dirtyRegion.isEmpty()) {
880 // redraw the whole screen
881 doComposeSurfaces(hw, Region(hw->bounds()));
882
883 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700884 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700885 RenderEngine& engine(getRenderEngine());
886 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
887
Mathias Agopiancd60f992012-08-16 16:28:27 -0700888 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700889 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700890 }
891 }
892 }
893
894 postFramebuffer();
895
896 if (mDebugRegion > 1) {
897 usleep(mDebugRegion * 1000);
898 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700899
900 HWComposer& hwc(getHwComposer());
901 if (hwc.initCheck() == NO_ERROR) {
902 status_t err = hwc.prepare();
903 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
904 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700905}
906
907void SurfaceFlinger::preComposition()
908{
909 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700910 const LayerVector& layers(mDrawingState.layersSortedByZ);
911 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700912 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700913 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700914 needExtraInvalidate = true;
915 }
916 }
917 if (needExtraInvalidate) {
918 signalLayerUpdate();
919 }
920}
921
922void SurfaceFlinger::postComposition()
923{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700924 const LayerVector& layers(mDrawingState.layersSortedByZ);
925 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700926 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700927 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700928 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800929
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700930 const HWComposer& hwc = getHwComposer();
931 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
932
933 if (presentFence->isValid()) {
934 if (mPrimaryDispSync.addPresentFence(presentFence)) {
935 enableHardwareVsync();
936 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700937 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700938 }
939 }
940
Andy McFadden5167ec62014-05-22 13:08:43 -0700941 if (kIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700942 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700943 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700944 enableHardwareVsync();
945 }
946 }
947
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800948 if (mAnimCompositionPending) {
949 mAnimCompositionPending = false;
950
Jesse Halla9a1b002013-02-27 16:39:25 -0800951 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800952 mAnimFrameTracker.setActualPresentFence(presentFence);
953 } else {
954 // The HWC doesn't support present fences, so use the refresh
955 // timestamp instead.
956 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
957 mAnimFrameTracker.setActualPresentTime(presentTime);
958 }
959 mAnimFrameTracker.advanceFrame();
960 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700961}
962
963void SurfaceFlinger::rebuildLayerStacks() {
964 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700965 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700966 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700967 mVisibleRegionsDirty = false;
968 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700969
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700970 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700971 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700972 Region opaqueRegion;
973 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800974 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700975 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700976 const Transform& tr(hw->getTransform());
977 const Rect bounds(hw->getBounds());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700978 if (hw->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700979 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700980 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700981
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700982 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700983 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700984 const sp<Layer>& layer(layers[i]);
985 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700986 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700987 Region drawRegion(tr.transform(
988 layer->visibleNonTransparentRegion));
989 drawRegion.andSelf(bounds);
990 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700991 layersSortedByZ.add(layer);
992 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700993 }
994 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700995 }
Mathias Agopian42977342012-08-05 00:40:46 -0700996 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700997 hw->undefinedRegion.set(bounds);
998 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
999 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001000 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001001 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001002}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001003
Mathias Agopiancd60f992012-08-16 16:28:27 -07001004void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001005 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001006 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1007 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1008 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1009
1010 // If nothing has changed (!dirty), don't recompose.
1011 // If something changed, but we don't currently have any visible layers,
1012 // and didn't when we last did a composition, then skip it this time.
1013 // The second rule does two things:
1014 // - When all layers are removed from a display, we'll emit one black
1015 // frame, then nothing more until we get new layers.
1016 // - When a display is created with a private layer stack, we won't
1017 // emit any black frames until a layer is added to the layer stack.
1018 bool mustRecompose = dirty && !(empty && wasEmpty);
1019
1020 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1021 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1022 mustRecompose ? "doing" : "skipping",
1023 dirty ? "+" : "-",
1024 empty ? "+" : "-",
1025 wasEmpty ? "+" : "-");
1026
Dan Stoza71433162014-02-04 16:22:36 -08001027 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001028
1029 if (mustRecompose) {
1030 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1031 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001032 }
1033
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001034 HWComposer& hwc(getHwComposer());
1035 if (hwc.initCheck() == NO_ERROR) {
1036 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001037 if (CC_UNLIKELY(mHwWorkListDirty)) {
1038 mHwWorkListDirty = false;
1039 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1040 sp<const DisplayDevice> hw(mDisplays[dpy]);
1041 const int32_t id = hw->getHwcDisplayId();
1042 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001043 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001044 hw->getVisibleLayersSortedByZ());
1045 const size_t count = currentLayers.size();
1046 if (hwc.createWorkList(id, count) == NO_ERROR) {
1047 HWComposer::LayerListIterator cur = hwc.begin(id);
1048 const HWComposer::LayerListIterator end = hwc.end(id);
1049 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001050 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001051 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001052 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001053 cur->setSkip(true);
1054 }
1055 }
1056 }
1057 }
1058 }
1059 }
1060
1061 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001062 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001063 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001064 const int32_t id = hw->getHwcDisplayId();
1065 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001066 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001067 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001068 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001069 HWComposer::LayerListIterator cur = hwc.begin(id);
1070 const HWComposer::LayerListIterator end = hwc.end(id);
1071 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1072 /*
1073 * update the per-frame h/w composer data for each layer
1074 * and build the transparent region of the FB
1075 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001076 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001077 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -07001078 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001079 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001080 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001081
Riley Andrews03414a12014-07-01 14:22:59 -07001082 // If possible, attempt to use the cursor overlay on each display.
1083 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1084 sp<const DisplayDevice> hw(mDisplays[dpy]);
1085 const int32_t id = hw->getHwcDisplayId();
1086 if (id >= 0) {
1087 const Vector< sp<Layer> >& currentLayers(
1088 hw->getVisibleLayersSortedByZ());
1089 const size_t count = currentLayers.size();
1090 HWComposer::LayerListIterator cur = hwc.begin(id);
1091 const HWComposer::LayerListIterator end = hwc.end(id);
1092 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1093 const sp<Layer>& layer(currentLayers[i]);
1094 if (layer->isPotentialCursor()) {
1095 cur->setIsCursorLayerHint();
1096 break;
1097 }
1098 }
1099 }
1100 }
1101
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001102 status_t err = hwc.prepare();
1103 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001104
1105 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1106 sp<const DisplayDevice> hw(mDisplays[dpy]);
1107 hw->prepareFrame(hwc);
1108 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001109 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001110}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001111
Mathias Agopiancd60f992012-08-16 16:28:27 -07001112void SurfaceFlinger::doComposition() {
1113 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001114 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001115 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001116 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001117 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001118 // transform the dirty region into this screen's coordinate space
1119 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001120
1121 // repaint the framebuffer (if needed)
1122 doDisplayComposition(hw, dirtyRegion);
1123
Mathias Agopiancd60f992012-08-16 16:28:27 -07001124 hw->dirtyRegion.clear();
1125 hw->flip(hw->swapRegion);
1126 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001127 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001128 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001129 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001130 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001131 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001132}
1133
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001134void SurfaceFlinger::postFramebuffer()
1135{
Mathias Agopian841cde52012-03-01 15:44:37 -08001136 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001137
Mathias Agopiana44b0412011-10-16 18:46:35 -07001138 const nsecs_t now = systemTime();
1139 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001140
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001141 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001142 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001143 if (!hwc.supportsFramebufferTarget()) {
1144 // EGL spec says:
1145 // "surface must be bound to the calling thread's current context,
1146 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001147 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001148 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001149 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001150 }
1151
Mathias Agopian6da15f42013-09-25 20:40:07 -07001152 // make the default display current because the VirtualDisplayDevice code cannot
1153 // deal with dequeueBuffer() being called outside of the composition loop; however
1154 // the code below can call glFlush() which is allowed (and does in some case) call
1155 // dequeueBuffer().
1156 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1157
Mathias Agopian92a979a2012-08-02 18:32:23 -07001158 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001159 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001160 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001161 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001162 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001163 int32_t id = hw->getHwcDisplayId();
1164 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001165 HWComposer::LayerListIterator cur = hwc.begin(id);
1166 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001167 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001168 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001169 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001170 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001171 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001172 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001173 }
Jesse Hallef194142012-06-14 14:45:17 -07001174 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001175 }
1176
Mathias Agopiana44b0412011-10-16 18:46:35 -07001177 mLastSwapBufferTime = systemTime() - now;
1178 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001179
1180 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1181 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1182 logFrameStats();
1183 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001184}
1185
Mathias Agopian87baae12012-07-31 12:38:26 -07001186void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187{
Mathias Agopian841cde52012-03-01 15:44:37 -08001188 ATRACE_CALL();
1189
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001190 // here we keep a copy of the drawing state (that is the state that's
1191 // going to be overwritten by handleTransactionLocked()) outside of
1192 // mStateLock so that the side-effects of the State assignment
1193 // don't happen with mStateLock held (which can cause deadlocks).
1194 State drawingState(mDrawingState);
1195
Mathias Agopianca4d3602011-05-19 15:38:14 -07001196 Mutex::Autolock _l(mStateLock);
1197 const nsecs_t now = systemTime();
1198 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001199
Mathias Agopianca4d3602011-05-19 15:38:14 -07001200 // Here we're guaranteed that some transaction flags are set
1201 // so we can call handleTransactionLocked() unconditionally.
1202 // We call getTransactionFlags(), which will also clear the flags,
1203 // with mStateLock held to guarantee that mCurrentState won't change
1204 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001205
Mathias Agopiane57f2922012-08-09 16:29:12 -07001206 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001207 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001208
Mathias Agopianca4d3602011-05-19 15:38:14 -07001209 mLastTransactionTime = systemTime() - now;
1210 mDebugInTransaction = 0;
1211 invalidateHwcGeometry();
1212 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001213}
1214
Mathias Agopian87baae12012-07-31 12:38:26 -07001215void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001216{
1217 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001218 const size_t count = currentLayers.size();
1219
1220 /*
1221 * Traversal of the children
1222 * (perform the transaction for each of them if needed)
1223 */
1224
Mathias Agopian3559b072012-08-15 13:46:03 -07001225 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001226 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001227 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1229 if (!trFlags) continue;
1230
1231 const uint32_t flags = layer->doTransaction(0);
1232 if (flags & Layer::eVisibleRegion)
1233 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234 }
1235 }
1236
1237 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001238 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001239 */
1240
Mathias Agopiane57f2922012-08-09 16:29:12 -07001241 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001242 // here we take advantage of Vector's copy-on-write semantics to
1243 // improve performance by skipping the transaction entirely when
1244 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001245 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1246 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001247 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001248 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001249 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001250 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001251
1252 // find the displays that were removed
1253 // (ie: in drawing state but not in current state)
1254 // also handle displays that changed
1255 // (ie: displays that are in both lists)
1256 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001257 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1258 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001259 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001260 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001261 // Call makeCurrent() on the primary display so we can
1262 // be sure that nothing associated with this display
1263 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001264 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001265 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001266 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1267 if (hw != NULL)
1268 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001269 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001270 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001271 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001272 } else {
1273 ALOGW("trying to remove the main display");
1274 }
1275 } else {
1276 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001277 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001278 const wp<IBinder>& display(curr.keyAt(j));
Marco Nelissen2ea926b2014-11-14 08:01:01 -08001279 const sp<IBinder> state_binder = IInterface::asBinder(state.surface);
1280 const sp<IBinder> draw_binder = IInterface::asBinder(draw[i].surface);
Dan Albert1474f882014-09-08 18:59:09 -07001281 if (state_binder != draw_binder) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001282 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001283 // recreating the DisplayDevice, so we just remove it
1284 // from the drawing state, so that it get re-added
1285 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001286 sp<DisplayDevice> hw(getDisplayDevice(display));
1287 if (hw != NULL)
1288 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001289 mDisplays.removeItem(display);
1290 mDrawingState.displays.removeItemsAt(i);
1291 dc--; i--;
1292 // at this point we must loop to the next item
1293 continue;
1294 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001295
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001296 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001297 if (disp != NULL) {
1298 if (state.layerStack != draw[i].layerStack) {
1299 disp->setLayerStack(state.layerStack);
1300 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001301 if ((state.orientation != draw[i].orientation)
1302 || (state.viewport != draw[i].viewport)
1303 || (state.frame != draw[i].frame))
1304 {
1305 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001306 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001307 }
Michael Lentine47e45402014-07-18 15:34:25 -07001308 if (state.width != draw[i].width || state.height != draw[i].height) {
1309 disp->setDisplaySize(state.width, state.height);
1310 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001311 }
1312 }
1313 }
1314
1315 // find displays that were added
1316 // (ie: in current state but not in drawing state)
1317 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001318 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001319 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001320
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001321 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001322 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001323 sp<IGraphicBufferProducer> bqProducer;
1324 sp<IGraphicBufferConsumer> bqConsumer;
1325 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1326 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001327
Jesse Hall02d86562013-03-25 14:43:23 -07001328 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001329 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001330 // Virtual displays without a surface are dormant:
1331 // they have external state (layer stack, projection,
1332 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001333 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001334
Dan Stoza1f3efb12014-10-15 16:34:55 -07001335 int width = 0;
1336 int status = state.surface->query(
1337 NATIVE_WINDOW_WIDTH, &width);
1338 ALOGE_IF(status != NO_ERROR,
1339 "Unable to query width (%d)", status);
1340 int height = 0;
1341 status = state.surface->query(
1342 NATIVE_WINDOW_HEIGHT, &height);
1343 ALOGE_IF(status != NO_ERROR,
1344 "Unable to query height (%d)", status);
1345 if (MAX_VIRTUAL_DISPLAY_DIMENSION == 0 ||
1346 (width <= MAX_VIRTUAL_DISPLAY_DIMENSION &&
1347 height <= MAX_VIRTUAL_DISPLAY_DIMENSION)) {
1348 hwcDisplayId = allocateHwcDisplayId(state.type);
1349 }
1350
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001351 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
Dan Stozab9b08832014-03-13 11:55:57 -07001352 *mHwc, hwcDisplayId, state.surface,
1353 bqProducer, bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001354
1355 dispSurface = vds;
Michael Lentine47e45402014-07-18 15:34:25 -07001356 producer = vds;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001357 }
1358 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001359 ALOGE_IF(state.surface!=NULL,
1360 "adding a supported display, but rendering "
1361 "surface is provided (%p), ignoring it",
1362 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001363 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001364 // for supported (by hwc) displays we provide our
1365 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001366 dispSurface = new FramebufferSurface(*mHwc, state.type,
1367 bqConsumer);
1368 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001369 }
1370
1371 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001372 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001373 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001374 state.type, hwcDisplayId,
1375 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001376 display, dispSurface, producer,
1377 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001378 hw->setLayerStack(state.layerStack);
1379 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001380 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001381 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001382 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001383 if (state.isVirtualDisplay()) {
1384 if (hwcDisplayId >= 0) {
1385 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1386 hw->getWidth(), hw->getHeight(),
1387 hw->getFormat());
1388 }
1389 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001390 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001391 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001392 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001393 }
1394 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001395 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001396 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397
Mathias Agopian84300952012-11-21 16:02:13 -08001398 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1399 // The transform hint might have changed for some layers
1400 // (either because a display has changed, or because a layer
1401 // as changed).
1402 //
1403 // Walk through all the layers in currentLayers,
1404 // and update their transform hint.
1405 //
1406 // If a layer is visible only on a single display, then that
1407 // display is used to calculate the hint, otherwise we use the
1408 // default display.
1409 //
1410 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1411 // the hint is set before we acquire a buffer from the surface texture.
1412 //
1413 // NOTE: layer transactions have taken place already, so we use their
1414 // drawing state. However, SurfaceFlinger's own transaction has not
1415 // happened yet, so we must use the current state layer list
1416 // (soon to become the drawing state list).
1417 //
1418 sp<const DisplayDevice> disp;
1419 uint32_t currentlayerStack = 0;
1420 for (size_t i=0; i<count; i++) {
1421 // NOTE: we rely on the fact that layers are sorted by
1422 // layerStack first (so we don't have to traverse the list
1423 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001424 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001425 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001426 if (i==0 || currentlayerStack != layerStack) {
1427 currentlayerStack = layerStack;
1428 // figure out if this layerstack is mirrored
1429 // (more than one display) if so, pick the default display,
1430 // if not, pick the only display it's on.
1431 disp.clear();
1432 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1433 sp<const DisplayDevice> hw(mDisplays[dpy]);
1434 if (hw->getLayerStack() == currentlayerStack) {
1435 if (disp == NULL) {
1436 disp = hw;
1437 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001438 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001439 break;
1440 }
1441 }
1442 }
1443 }
Chet Haase91d25932013-04-11 15:24:55 -07001444 if (disp == NULL) {
1445 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1446 // redraw after transform hint changes. See bug 8508397.
1447
1448 // could be null when this layer is using a layerStack
1449 // that is not visible on any display. Also can occur at
1450 // screen off/on times.
1451 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001452 }
Chet Haase91d25932013-04-11 15:24:55 -07001453 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001454 }
1455 }
1456
1457
Mathias Agopian3559b072012-08-15 13:46:03 -07001458 /*
1459 * Perform our own transaction if needed
1460 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001461
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001462 const LayerVector& layers(mDrawingState.layersSortedByZ);
1463 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001464 // layers have been added
1465 mVisibleRegionsDirty = true;
1466 }
1467
1468 // some layers might have been removed, so
1469 // we need to update the regions they're exposing.
1470 if (mLayersRemoved) {
1471 mLayersRemoved = false;
1472 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001473 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001474 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001475 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001476 if (currentLayers.indexOf(layer) < 0) {
1477 // this layer is not visible anymore
1478 // TODO: we could traverse the tree from front to back and
1479 // compute the actual visible region
1480 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001481 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001482 Region visibleReg = s.transform.transform(
1483 Region(Rect(s.active.w, s.active.h)));
1484 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001485 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001487 }
1488
1489 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001490
1491 updateCursorAsync();
1492}
1493
1494void SurfaceFlinger::updateCursorAsync()
1495{
1496 HWComposer& hwc(getHwComposer());
1497 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1498 sp<const DisplayDevice> hw(mDisplays[dpy]);
1499 const int32_t id = hw->getHwcDisplayId();
1500 if (id < 0) {
1501 continue;
1502 }
1503 const Vector< sp<Layer> >& currentLayers(
1504 hw->getVisibleLayersSortedByZ());
1505 const size_t count = currentLayers.size();
1506 HWComposer::LayerListIterator cur = hwc.begin(id);
1507 const HWComposer::LayerListIterator end = hwc.end(id);
1508 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1509 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1510 continue;
1511 }
1512 const sp<Layer>& layer(currentLayers[i]);
1513 Rect cursorPos = layer->getPosition(hw);
1514 hwc.setCursorPositionAsync(id, cursorPos);
1515 break;
1516 }
1517 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001518}
1519
1520void SurfaceFlinger::commitTransaction()
1521{
1522 if (!mLayersPendingRemoval.isEmpty()) {
1523 // Notify removed layers now that they can't be drawn from
1524 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1525 mLayersPendingRemoval[i]->onRemoved();
1526 }
1527 mLayersPendingRemoval.clear();
1528 }
1529
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001530 // If this transaction is part of a window animation then the next frame
1531 // we composite should be considered an animation as well.
1532 mAnimCompositionPending = mAnimTransactionPending;
1533
Mathias Agopian4fec8732012-06-29 14:12:52 -07001534 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001535 mTransactionPending = false;
1536 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001537 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001538}
1539
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001540void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001541 const LayerVector& currentLayers, uint32_t layerStack,
1542 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001543{
Mathias Agopian841cde52012-03-01 15:44:37 -08001544 ATRACE_CALL();
1545
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001546 Region aboveOpaqueLayers;
1547 Region aboveCoveredLayers;
1548 Region dirty;
1549
Mathias Agopian87baae12012-07-31 12:38:26 -07001550 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001551
1552 size_t i = currentLayers.size();
1553 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001554 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001555
1556 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001557 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001558
Jesse Hall01e29052013-02-19 16:13:35 -08001559 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001560 if (s.layerStack != layerStack)
1561 continue;
1562
Mathias Agopianab028732010-03-16 16:41:46 -07001563 /*
1564 * opaqueRegion: area of a surface that is fully opaque.
1565 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001566 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001567
1568 /*
1569 * visibleRegion: area of a surface that is visible on screen
1570 * and not fully transparent. This is essentially the layer's
1571 * footprint minus the opaque regions above it.
1572 * Areas covered by a translucent surface are considered visible.
1573 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001574 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001575
1576 /*
1577 * coveredRegion: area of a surface that is covered by all
1578 * visible regions above it (which includes the translucent areas).
1579 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001580 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001581
Jesse Halla8026d22012-09-25 13:25:04 -07001582 /*
1583 * transparentRegion: area of a surface that is hinted to be completely
1584 * transparent. This is only used to tell when the layer has no visible
1585 * non-transparent regions and can be removed from the layer list. It
1586 * does not affect the visibleRegion of this layer or any layers
1587 * beneath it. The hint may not be correct if apps don't respect the
1588 * SurfaceView restrictions (which, sadly, some don't).
1589 */
1590 Region transparentRegion;
1591
Mathias Agopianab028732010-03-16 16:41:46 -07001592
1593 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001594 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001595 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001596 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001597 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001598 if (!visibleRegion.isEmpty()) {
1599 // Remove the transparent area from the visible region
1600 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001601 const Transform tr(s.transform);
1602 if (tr.transformed()) {
1603 if (tr.preserveRects()) {
1604 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001605 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001606 } else {
1607 // transformation too complex, can't do the
1608 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001609 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001610 }
1611 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001612 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001613 }
Mathias Agopianab028732010-03-16 16:41:46 -07001614 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001615
Mathias Agopianab028732010-03-16 16:41:46 -07001616 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001617 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001618 if (s.alpha==255 && !translucent &&
1619 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1620 // the opaque region is the layer's footprint
1621 opaqueRegion = visibleRegion;
1622 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001623 }
1624 }
1625
Mathias Agopianab028732010-03-16 16:41:46 -07001626 // Clip the covered region to the visible region
1627 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1628
1629 // Update aboveCoveredLayers for next (lower) layer
1630 aboveCoveredLayers.orSelf(visibleRegion);
1631
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001632 // subtract the opaque region covered by the layers above us
1633 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001634
1635 // compute this layer's dirty region
1636 if (layer->contentDirty) {
1637 // we need to invalidate the whole region
1638 dirty = visibleRegion;
1639 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001640 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001641 layer->contentDirty = false;
1642 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001643 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001644 * the exposed region consists of two components:
1645 * 1) what's VISIBLE now and was COVERED before
1646 * 2) what's EXPOSED now less what was EXPOSED before
1647 *
1648 * note that (1) is conservative, we start with the whole
1649 * visible region but only keep what used to be covered by
1650 * something -- which mean it may have been exposed.
1651 *
1652 * (2) handles areas that were not covered by anything but got
1653 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001654 */
Mathias Agopianab028732010-03-16 16:41:46 -07001655 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001656 const Region oldVisibleRegion = layer->visibleRegion;
1657 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001658 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1659 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001660 }
1661 dirty.subtractSelf(aboveOpaqueLayers);
1662
1663 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001664 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001665
Mathias Agopianab028732010-03-16 16:41:46 -07001666 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001668
Jesse Halla8026d22012-09-25 13:25:04 -07001669 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001670 layer->setVisibleRegion(visibleRegion);
1671 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001672 layer->setVisibleNonTransparentRegion(
1673 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001674 }
1675
Mathias Agopian87baae12012-07-31 12:38:26 -07001676 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001677}
1678
Mathias Agopian87baae12012-07-31 12:38:26 -07001679void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1680 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001681 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001682 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1683 if (hw->getLayerStack() == layerStack) {
1684 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001685 }
1686 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001687}
1688
1689void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001690{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001691 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001692
Mathias Agopian4fec8732012-06-29 14:12:52 -07001693 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001694 const LayerVector& layers(mDrawingState.layersSortedByZ);
Eric Penner51c59cd2014-07-28 19:51:58 -07001695
1696 // Store the set of layers that need updates. This set must not change as
1697 // buffers are being latched, as this could result in a deadlock.
1698 // Example: Two producers share the same command stream and:
1699 // 1.) Layer 0 is latched
1700 // 2.) Layer 0 gets a new frame
1701 // 2.) Layer 1 gets a new frame
1702 // 3.) Layer 1 is latched.
1703 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1704 // second frame. But layer 0's second frame could be waiting on display.
1705 Vector<Layer*> layersWithQueuedFrames;
1706 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001707 const sp<Layer>& layer(layers[i]);
Eric Penner51c59cd2014-07-28 19:51:58 -07001708 if (layer->hasQueuedFrame())
1709 layersWithQueuedFrames.push_back(layer.get());
1710 }
1711 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
1712 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07001713 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001714 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001715 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001716 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001717
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001718 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001719}
1720
Mathias Agopianad456f92011-01-13 17:53:01 -08001721void SurfaceFlinger::invalidateHwcGeometry()
1722{
1723 mHwWorkListDirty = true;
1724}
1725
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001726
Mathias Agopiancd60f992012-08-16 16:28:27 -07001727void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001728 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001729{
Dan Stoza71433162014-02-04 16:22:36 -08001730 // We only need to actually compose the display if:
1731 // 1) It is being handled by hardware composer, which may need this to
1732 // keep its virtual display state machine in sync, or
1733 // 2) There is work to be done (the dirty region isn't empty)
1734 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1735 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1736 return;
1737 }
1738
Mathias Agopian87baae12012-07-31 12:38:26 -07001739 Region dirtyRegion(inDirtyRegion);
1740
Mathias Agopianb8a55602009-06-26 19:06:36 -07001741 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001742 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001743
Mathias Agopian42977342012-08-05 00:40:46 -07001744 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001745 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001746 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1747 // takes a rectangle, we must make sure to update that whole
1748 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001749 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001750 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001751 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001752 // We need to redraw the rectangle that will be updated
1753 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001754 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001755 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001756 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001757 } else {
1758 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001759 dirtyRegion.set(hw->bounds());
1760 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001761 }
1762 }
1763
Alan Viverette9c5a3332013-09-12 20:04:35 -07001764 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Michael Lentine3f121fc2014-10-01 11:17:28 -07001765 if (!doComposeSurfaces(hw, dirtyRegion)) return;
Mathias Agopianff2ed702013-09-01 21:36:12 -07001766 } else {
1767 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001768 mat4 colorMatrix = mColorMatrix;
1769 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001770 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001771 }
1772 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001773 doComposeSurfaces(hw, dirtyRegion);
1774 engine.endGroup();
1775 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001776
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001777 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001778 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001779
1780 // swap buffers (presentation)
1781 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001782}
1783
Michael Lentine3f121fc2014-10-01 11:17:28 -07001784bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001785{
Mathias Agopian3f844832013-08-07 21:24:32 -07001786 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001787 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001788 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001789 HWComposer::LayerListIterator cur = hwc.begin(id);
1790 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001791
Jesse Halld05a17f2013-09-30 16:49:30 -07001792 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001793 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001794 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001795 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1796 hw->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07001797 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1798 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
1799 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
1800 }
1801 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08001802 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001803
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001804 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001805 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001806 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001807 // when using overlays, we assume a fully transparent framebuffer
1808 // NOTE: we could reduce how much we need to clear, for instance
1809 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001810 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001811 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001812 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001813 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001814 // we start with the whole screen area
1815 const Region bounds(hw->getBounds());
1816
1817 // we remove the scissor part
1818 // we're left with the letterbox region
1819 // (common case is that letterbox ends-up being empty)
1820 const Region letterbox(bounds.subtract(hw->getScissor()));
1821
1822 // compute the area to clear
1823 Region region(hw->undefinedRegion.merge(letterbox));
1824
1825 // but limit it to the dirty region
1826 region.andSelf(dirty);
1827
Mathias Agopianb9494d52012-04-18 02:28:45 -07001828 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001829 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001830 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001831 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001832 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001833 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001834
Mathias Agopian766dc492012-10-30 18:08:06 -07001835 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1836 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001837 // scissor on the main display. It should never be needed
1838 // anyways (though in theory it could since the API allows it).
1839 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001840 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001841 if (scissor != bounds) {
1842 // scissor doesn't match the screen's dimensions, so we
1843 // need to clear everything outside of it and enable
1844 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001845
Mathias Agopianf45c5102012-10-24 16:29:17 -07001846 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001847 const uint32_t height = hw->getHeight();
1848 engine.setScissor(scissor.left, height - scissor.bottom,
1849 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001850 }
1851 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001852 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001853
Mathias Agopian85d751c2012-08-29 16:59:24 -07001854 /*
1855 * and then, render the layers targeted at the framebuffer
1856 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001857
Mathias Agopian13127d82013-03-05 17:47:11 -08001858 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001859 const size_t count = layers.size();
1860 const Transform& tr = hw->getTransform();
1861 if (cur != end) {
1862 // we're using h/w composer
1863 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001864 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001865 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001866 if (!clip.isEmpty()) {
1867 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07001868 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07001869 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001870 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001871 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1872 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001873 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001874 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001875 // never clear the very first layer since we're
1876 // guaranteed the FB is already cleared
1877 layer->clearWithOpenGL(hw, clip);
1878 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001879 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001880 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001881 case HWC_FRAMEBUFFER: {
1882 layer->draw(hw, clip);
1883 break;
1884 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001885 case HWC_FRAMEBUFFER_TARGET: {
1886 // this should not happen as the iterator shouldn't
1887 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001888 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001889 break;
1890 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001891 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001892 }
1893 layer->setAcquireFence(hw, *cur);
1894 }
1895 } else {
1896 // we're not using h/w composer
1897 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001898 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001899 const Region clip(dirty.intersect(
1900 tr.transform(layer->visibleRegion)));
1901 if (!clip.isEmpty()) {
1902 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001903 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001904 }
1905 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001906
1907 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001908 engine.disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07001909 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001910}
1911
Mathias Agopian3f844832013-08-07 21:24:32 -07001912void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001913 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001914 RenderEngine& engine(getRenderEngine());
1915 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001916}
1917
Mathias Agopianac9fa422013-02-11 16:40:36 -08001918void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1919 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001920 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001921 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001922{
Mathias Agopian96f08192010-06-02 23:28:45 -07001923 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001924 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001925
Mathias Agopian96f08192010-06-02 23:28:45 -07001926 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001927 Mutex::Autolock _l(mStateLock);
1928 mCurrentState.layersSortedByZ.add(lbc);
Marco Nelissen2ea926b2014-11-14 08:01:01 -08001929 mGraphicBufferProducerList.add(IInterface::asBinder(gbc));
Mathias Agopian96f08192010-06-02 23:28:45 -07001930}
1931
Mathias Agopian3f844832013-08-07 21:24:32 -07001932status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001933 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001934 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001935 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001936 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001937 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001938 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001939 return NO_ERROR;
1940 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001941 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001942}
1943
Dan Stozac7014012014-02-14 15:03:43 -08001944uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001945 return android_atomic_release_load(&mTransactionFlags);
1946}
1947
Mathias Agopian3f844832013-08-07 21:24:32 -07001948uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001949 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1950}
1951
Mathias Agopian3f844832013-08-07 21:24:32 -07001952uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001953 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1954 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001955 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001956 }
1957 return old;
1958}
1959
Mathias Agopian8b33f032012-07-24 20:43:54 -07001960void SurfaceFlinger::setTransactionState(
1961 const Vector<ComposerState>& state,
1962 const Vector<DisplayState>& displays,
1963 uint32_t flags)
1964{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001965 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001966 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001967 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001968
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001969 if (flags & eAnimation) {
1970 // For window updates that are part of an animation we must wait for
1971 // previous animation "frames" to be handled.
1972 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001973 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001974 if (CC_UNLIKELY(err != NO_ERROR)) {
1975 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001976 // caller after a few seconds.
1977 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1978 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001979 mAnimTransactionPending = false;
1980 break;
1981 }
1982 }
1983 }
1984
Mathias Agopiane57f2922012-08-09 16:29:12 -07001985 size_t count = displays.size();
1986 for (size_t i=0 ; i<count ; i++) {
1987 const DisplayState& s(displays[i]);
1988 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001989 }
1990
Mathias Agopiane57f2922012-08-09 16:29:12 -07001991 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001992 for (size_t i=0 ; i<count ; i++) {
1993 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001994 // Here we need to check that the interface we're given is indeed
1995 // one of our own. A malicious client could give us a NULL
1996 // IInterface, or one of its own or even one of our own but a
1997 // different type. All these situations would cause us to crash.
1998 //
1999 // NOTE: it would be better to use RTTI as we could directly check
2000 // that we have a Client*. however, RTTI is disabled in Android.
2001 if (s.client != NULL) {
Marco Nelissen2ea926b2014-11-14 08:01:01 -08002002 sp<IBinder> binder = IInterface::asBinder(s.client);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002003 if (binder != NULL) {
2004 String16 desc(binder->getInterfaceDescriptor());
2005 if (desc == ISurfaceComposerClient::descriptor) {
2006 sp<Client> client( static_cast<Client *>(s.client.get()) );
2007 transactionFlags |= setClientStateLocked(client, s.state);
2008 }
2009 }
2010 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002011 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002012
Mathias Agopian386aa982011-11-07 21:58:03 -08002013 if (transactionFlags) {
2014 // this triggers the transaction
2015 setTransactionFlags(transactionFlags);
2016
2017 // if this is a synchronous transaction, wait for it to take effect
2018 // before returning.
2019 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002020 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002021 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002022 if (flags & eAnimation) {
2023 mAnimTransactionPending = true;
2024 }
2025 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002026 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2027 if (CC_UNLIKELY(err != NO_ERROR)) {
2028 // just in case something goes wrong in SF, return to the
2029 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002030 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2031 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002032 break;
2033 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002034 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002035 }
2036}
2037
Mathias Agopiane57f2922012-08-09 16:29:12 -07002038uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2039{
Jesse Hall9a143922012-10-04 16:29:19 -07002040 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2041 if (dpyIdx < 0)
2042 return 0;
2043
Mathias Agopiane57f2922012-08-09 16:29:12 -07002044 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002045 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002046 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002047 const uint32_t what = s.what;
2048 if (what & DisplayState::eSurfaceChanged) {
Marco Nelissen2ea926b2014-11-14 08:01:01 -08002049 if (IInterface::asBinder(disp.surface) != IInterface::asBinder(s.surface)) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002050 disp.surface = s.surface;
2051 flags |= eDisplayTransactionNeeded;
2052 }
2053 }
2054 if (what & DisplayState::eLayerStackChanged) {
2055 if (disp.layerStack != s.layerStack) {
2056 disp.layerStack = s.layerStack;
2057 flags |= eDisplayTransactionNeeded;
2058 }
2059 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002060 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002061 if (disp.orientation != s.orientation) {
2062 disp.orientation = s.orientation;
2063 flags |= eDisplayTransactionNeeded;
2064 }
2065 if (disp.frame != s.frame) {
2066 disp.frame = s.frame;
2067 flags |= eDisplayTransactionNeeded;
2068 }
2069 if (disp.viewport != s.viewport) {
2070 disp.viewport = s.viewport;
2071 flags |= eDisplayTransactionNeeded;
2072 }
2073 }
Michael Lentine47e45402014-07-18 15:34:25 -07002074 if (what & DisplayState::eDisplaySizeChanged) {
2075 if (disp.width != s.width) {
2076 disp.width = s.width;
2077 flags |= eDisplayTransactionNeeded;
2078 }
2079 if (disp.height != s.height) {
2080 disp.height = s.height;
2081 flags |= eDisplayTransactionNeeded;
2082 }
2083 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002084 }
2085 return flags;
2086}
2087
2088uint32_t SurfaceFlinger::setClientStateLocked(
2089 const sp<Client>& client,
2090 const layer_state_t& s)
2091{
2092 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002093 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002094 if (layer != 0) {
2095 const uint32_t what = s.what;
2096 if (what & layer_state_t::ePositionChanged) {
2097 if (layer->setPosition(s.x, s.y))
2098 flags |= eTraversalNeeded;
2099 }
2100 if (what & layer_state_t::eLayerChanged) {
2101 // NOTE: index needs to be calculated before we update the state
2102 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2103 if (layer->setLayer(s.z)) {
2104 mCurrentState.layersSortedByZ.removeAt(idx);
2105 mCurrentState.layersSortedByZ.add(layer);
2106 // we need traversal (state changed)
2107 // AND transaction (list changed)
2108 flags |= eTransactionNeeded|eTraversalNeeded;
2109 }
2110 }
2111 if (what & layer_state_t::eSizeChanged) {
2112 if (layer->setSize(s.w, s.h)) {
2113 flags |= eTraversalNeeded;
2114 }
2115 }
2116 if (what & layer_state_t::eAlphaChanged) {
2117 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2118 flags |= eTraversalNeeded;
2119 }
2120 if (what & layer_state_t::eMatrixChanged) {
2121 if (layer->setMatrix(s.matrix))
2122 flags |= eTraversalNeeded;
2123 }
2124 if (what & layer_state_t::eTransparentRegionChanged) {
2125 if (layer->setTransparentRegionHint(s.transparentRegion))
2126 flags |= eTraversalNeeded;
2127 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08002128 if ((what & layer_state_t::eVisibilityChanged) ||
2129 (what & layer_state_t::eOpacityChanged)) {
2130 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07002131 if (layer->setFlags(s.flags, s.mask))
2132 flags |= eTraversalNeeded;
2133 }
2134 if (what & layer_state_t::eCropChanged) {
2135 if (layer->setCrop(s.crop))
2136 flags |= eTraversalNeeded;
2137 }
2138 if (what & layer_state_t::eLayerStackChanged) {
2139 // NOTE: index needs to be calculated before we update the state
2140 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2141 if (layer->setLayerStack(s.layerStack)) {
2142 mCurrentState.layersSortedByZ.removeAt(idx);
2143 mCurrentState.layersSortedByZ.add(layer);
2144 // we need traversal (state changed)
2145 // AND transaction (list changed)
2146 flags |= eTransactionNeeded|eTraversalNeeded;
2147 }
2148 }
2149 }
2150 return flags;
2151}
2152
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002153status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002154 const String8& name,
2155 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002156 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2157 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002158{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002159 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002160 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002161 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002162 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002163 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002164 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002165
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002166 status_t result = NO_ERROR;
2167
2168 sp<Layer> layer;
2169
Mathias Agopian3165cc22012-08-08 19:42:09 -07002170 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2171 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002172 result = createNormalLayer(client,
2173 name, w, h, flags, format,
2174 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002175 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002176 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002177 result = createDimLayer(client,
2178 name, w, h, flags,
2179 handle, gbp, &layer);
2180 break;
2181 default:
2182 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002183 break;
2184 }
2185
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002186 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002187 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002188 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002189 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002190 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002191}
2192
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002193status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2194 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2195 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002196{
2197 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002198 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002199 case PIXEL_FORMAT_TRANSPARENT:
2200 case PIXEL_FORMAT_TRANSLUCENT:
2201 format = PIXEL_FORMAT_RGBA_8888;
2202 break;
2203 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002204 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002205 break;
2206 }
2207
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002208 *outLayer = new Layer(this, client, name, w, h, flags);
2209 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2210 if (err == NO_ERROR) {
2211 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002212 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002213 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002214
2215 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2216 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002217}
2218
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002219status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2220 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2221 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002222{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002223 *outLayer = new LayerDim(this, client, name, w, h, flags);
2224 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002225 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002226 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002227}
2228
Mathias Agopianac9fa422013-02-11 16:40:36 -08002229status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002230{
Mathias Agopian67106042013-03-14 19:18:13 -07002231 // called by the window manager when it wants to remove a Layer
2232 status_t err = NO_ERROR;
2233 sp<Layer> l(client->getLayerUser(handle));
2234 if (l != NULL) {
2235 err = removeLayer(l);
2236 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2237 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002238 }
2239 return err;
2240}
2241
Mathias Agopian13127d82013-03-05 17:47:11 -08002242status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002243{
Mathias Agopian67106042013-03-14 19:18:13 -07002244 // called by ~LayerCleaner() when all references to the IBinder (handle)
2245 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002246 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002247 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002248 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002249 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002250 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002251 "error removing layer=%p (%s)", l.get(), strerror(-err));
2252 }
2253 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002254}
2255
Mathias Agopianb60314a2012-04-10 22:09:54 -07002256// ---------------------------------------------------------------------------
2257
Andy McFadden13a082e2012-08-24 10:16:42 -07002258void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002259 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002260 Vector<ComposerState> state;
2261 Vector<DisplayState> displays;
2262 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002263 d.what = DisplayState::eDisplayProjectionChanged |
2264 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002265 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002266 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002267 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002268 d.frame.makeInvalid();
2269 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002270 d.width = 0;
2271 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002272 displays.add(d);
2273 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002274 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002275
2276 const nsecs_t period =
2277 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2278 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002279}
2280
2281void SurfaceFlinger::initializeDisplays() {
2282 class MessageScreenInitialized : public MessageBase {
2283 SurfaceFlinger* flinger;
2284 public:
2285 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2286 virtual bool handler() {
2287 flinger->onInitializeDisplays();
2288 return true;
2289 }
2290 };
2291 sp<MessageBase> msg = new MessageScreenInitialized(this);
2292 postMessageAsync(msg); // we may be called from main thread, use async message
2293}
2294
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002295void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2296 int mode) {
2297 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2298 this);
2299 int32_t type = hw->getDisplayType();
2300 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002301
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002302 if (mode == currentMode) {
2303 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002304 return;
2305 }
2306
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002307 hw->setPowerMode(mode);
2308 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2309 ALOGW("Trying to set power mode for virtual display");
2310 return;
2311 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002312
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002313 if (currentMode == HWC_POWER_MODE_OFF) {
2314 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002315 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2316 // FIXME: eventthread only knows about the main display right now
2317 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002318 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002319 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002320
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002321 mVisibleRegionsDirty = true;
2322 repaintEverything();
2323 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002324 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002325 disableHardwareVsync(true); // also cancels any in-progress resync
2326
Mathias Agopiancde87a32012-09-13 14:09:01 -07002327 // FIXME: eventthread only knows about the main display right now
2328 mEventThread->onScreenReleased();
2329 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002330
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002331 getHwComposer().setPowerMode(type, mode);
2332 mVisibleRegionsDirty = true;
2333 // from this point on, SF will stop drawing on this display
2334 } else {
2335 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002336 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002337}
2338
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002339void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2340 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002341 SurfaceFlinger& mFlinger;
2342 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002343 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002344 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002345 MessageSetPowerMode(SurfaceFlinger& flinger,
2346 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2347 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002348 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002349 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002350 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002351 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002352 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002353 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002354 ALOGW("Attempt to set power mode = %d for virtual display",
2355 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002356 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002357 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002358 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002359 return true;
2360 }
2361 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002362 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002363 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002364}
2365
2366// ---------------------------------------------------------------------------
2367
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002368status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2369{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002370 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002371
Mathias Agopianbd115332013-04-18 16:41:04 -07002372 IPCThreadState* ipc = IPCThreadState::self();
2373 const int pid = ipc->getCallingPid();
2374 const int uid = ipc->getCallingUid();
2375 if ((uid != AID_SHELL) &&
2376 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002377 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002378 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002379 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002380 // Try to get the main lock, but don't insist if we can't
2381 // (this would indicate SF is stuck, but we want to be able to
2382 // print something in dumpsys).
2383 int retry = 3;
2384 while (mStateLock.tryLock()<0 && --retry>=0) {
2385 usleep(1000000);
2386 }
2387 const bool locked(retry >= 0);
2388 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002389 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002390 "SurfaceFlinger appears to be unresponsive, "
2391 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002392 }
2393
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002394 bool dumpAll = true;
2395 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002396 size_t numArgs = args.size();
2397 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002398 if ((index < numArgs) &&
2399 (args[index] == String16("--list"))) {
2400 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002401 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002402 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002403 }
2404
2405 if ((index < numArgs) &&
2406 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002407 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002408 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002409 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002410 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002411
2412 if ((index < numArgs) &&
2413 (args[index] == String16("--latency-clear"))) {
2414 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002415 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002416 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002417 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002418
2419 if ((index < numArgs) &&
2420 (args[index] == String16("--dispsync"))) {
2421 index++;
2422 mPrimaryDispSync.dump(result);
2423 dumpAll = false;
2424 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002425 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002426
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002427 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002428 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002429 }
2430
Mathias Agopian9795c422009-08-26 16:36:26 -07002431 if (locked) {
2432 mStateLock.unlock();
2433 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002434 }
2435 write(fd, result.string(), result.size());
2436 return NO_ERROR;
2437}
2438
Dan Stozac7014012014-02-14 15:03:43 -08002439void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2440 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002441{
2442 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2443 const size_t count = currentLayers.size();
2444 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002445 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002446 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002447 }
2448}
2449
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002450void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002451 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002452{
2453 String8 name;
2454 if (index < args.size()) {
2455 name = String8(args[index]);
2456 index++;
2457 }
2458
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002459 const nsecs_t period =
2460 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002461 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002462
2463 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002464 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002465 } else {
2466 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2467 const size_t count = currentLayers.size();
2468 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002469 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002470 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002471 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002472 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002473 }
2474 }
2475}
2476
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002477void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002478 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002479{
2480 String8 name;
2481 if (index < args.size()) {
2482 name = String8(args[index]);
2483 index++;
2484 }
2485
2486 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2487 const size_t count = currentLayers.size();
2488 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002489 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002490 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002491 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002492 }
2493 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002494
Svetoslavd85084b2014-03-20 10:28:31 -07002495 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002496}
2497
Jamie Gennis6547ff42013-07-16 20:12:42 -07002498// This should only be called from the main thread. Otherwise it would need
2499// the lock and should use mCurrentState rather than mDrawingState.
2500void SurfaceFlinger::logFrameStats() {
2501 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2502 const size_t count = drawingLayers.size();
2503 for (size_t i=0 ; i<count ; i++) {
2504 const sp<Layer>& layer(drawingLayers[i]);
2505 layer->logFrameStats();
2506 }
2507
2508 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2509}
2510
Andy McFadden4803b742012-09-24 19:07:20 -07002511/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2512{
2513 static const char* config =
2514 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002515#ifdef HAS_CONTEXT_PRIORITY
2516 " HAS_CONTEXT_PRIORITY"
2517#endif
2518#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2519 " NEVER_DEFAULT_TO_ASYNC_MODE"
2520#endif
2521#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2522 " TARGET_DISABLE_TRIPLE_BUFFERING"
2523#endif
2524 "]";
2525 result.append(config);
2526}
2527
Mathias Agopian74d211a2013-04-22 16:55:35 +02002528void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2529 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002530{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002531 bool colorize = false;
2532 if (index < args.size()
2533 && (args[index] == String16("--color"))) {
2534 colorize = true;
2535 index++;
2536 }
2537
2538 Colorizer colorizer(colorize);
2539
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002540 // figure out if we're stuck somewhere
2541 const nsecs_t now = systemTime();
2542 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2543 const nsecs_t inTransaction(mDebugInTransaction);
2544 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2545 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2546
2547 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002548 * Dump library configuration.
2549 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002550
2551 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002552 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002553 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002554 appendSfConfigString(result);
2555 appendUiConfigString(result);
2556 appendGuiConfigString(result);
2557 result.append("\n");
2558
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002559 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002560 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002561 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002562 result.append(SyncFeatures::getInstance().toString());
2563 result.append("\n");
2564
Andy McFadden41d67d72014-04-25 16:58:34 -07002565 colorizer.bold(result);
2566 result.append("DispSync configuration: ");
2567 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002568 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2569 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002570 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2571 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2572 result.append("\n");
2573
Andy McFadden4803b742012-09-24 19:07:20 -07002574 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002575 * Dump the visible layer list
2576 */
2577 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2578 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002579 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002580 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002581 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002582 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002583 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002584 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002585 }
2586
2587 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002588 * Dump Display state
2589 */
2590
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002591 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002592 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002593 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002594 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2595 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002596 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002597 }
2598
2599 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002600 * Dump SurfaceFlinger global state
2601 */
2602
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002603 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002604 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002605 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002606
Mathias Agopian888c8222012-08-04 21:10:38 -07002607 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002608 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002609
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002610 colorizer.bold(result);
2611 result.appendFormat("EGL implementation : %s\n",
2612 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2613 colorizer.reset(result);
2614 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002615 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002616
Mathias Agopian875d8e12013-06-07 15:35:48 -07002617 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002618
Mathias Agopian42977342012-08-05 00:40:46 -07002619 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002620 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2621 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002622 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002623 " last eglSwapBuffers() time: %f us\n"
2624 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002625 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002626 " refresh-rate : %f fps\n"
2627 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002628 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002629 " gpu_to_cpu_unsupported : %d\n"
2630 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002631 mLastSwapBufferTime/1000.0,
2632 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002633 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002634 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2635 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002636 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002637 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002638
Mathias Agopian74d211a2013-04-22 16:55:35 +02002639 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002640 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002641
Mathias Agopian74d211a2013-04-22 16:55:35 +02002642 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002643 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002644
2645 /*
2646 * VSYNC state
2647 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002648 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002649
2650 /*
2651 * Dump HWComposer state
2652 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002653 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002654 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002655 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002656 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002657 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002658 (mDebugDisableHWC || mDebugRegion || mDaltonize
2659 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002660 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002661
2662 /*
2663 * Dump gralloc state
2664 */
2665 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2666 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002667}
2668
Mathias Agopian13127d82013-03-05 17:47:11 -08002669const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002670SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002671 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002672 wp<IBinder> dpy;
2673 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2674 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2675 dpy = mDisplays.keyAt(i);
2676 break;
2677 }
2678 }
2679 if (dpy == NULL) {
2680 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2681 // Just use the primary display so we have something to return
2682 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2683 }
2684 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002685}
2686
Keun young Park63f165f2012-08-31 10:53:36 -07002687bool SurfaceFlinger::startDdmConnection()
2688{
2689 void* libddmconnection_dso =
2690 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2691 if (!libddmconnection_dso) {
2692 return false;
2693 }
2694 void (*DdmConnection_start)(const char* name);
2695 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07002696 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07002697 if (!DdmConnection_start) {
2698 dlclose(libddmconnection_dso);
2699 return false;
2700 }
2701 (*DdmConnection_start)(getServiceName());
2702 return true;
2703}
2704
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002705status_t SurfaceFlinger::onTransact(
2706 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2707{
2708 switch (code) {
2709 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002710 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002711 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002712 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002713 case CLEAR_ANIMATION_FRAME_STATS:
2714 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002715 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002716 {
2717 // codes that require permission check
2718 IPCThreadState* ipc = IPCThreadState::self();
2719 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002720 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002721 if ((uid != AID_GRAPHICS) &&
2722 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002723 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002724 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2725 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002726 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002727 break;
2728 }
2729 case CAPTURE_SCREEN:
2730 {
2731 // codes that require permission check
2732 IPCThreadState* ipc = IPCThreadState::self();
2733 const int pid = ipc->getCallingPid();
2734 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002735 if ((uid != AID_GRAPHICS) &&
2736 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002737 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002738 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2739 return PERMISSION_DENIED;
2740 }
2741 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002742 }
2743 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002744
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002745 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2746 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002747 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002748 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002749 IPCThreadState* ipc = IPCThreadState::self();
2750 const int pid = ipc->getCallingPid();
2751 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002752 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002753 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002754 return PERMISSION_DENIED;
2755 }
2756 int n;
2757 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002758 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002759 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002760 return NO_ERROR;
2761 case 1002: // SHOW_UPDATES
2762 n = data.readInt32();
2763 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002764 invalidateHwcGeometry();
2765 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002766 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002767 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002768 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002769 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002770 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002771 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002772 setTransactionFlags(
2773 eTransactionNeeded|
2774 eDisplayTransactionNeeded|
2775 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002776 return NO_ERROR;
2777 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002778 case 1006:{ // send empty update
2779 signalRefresh();
2780 return NO_ERROR;
2781 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002782 case 1008: // toggle use of hw composer
2783 n = data.readInt32();
2784 mDebugDisableHWC = n ? 1 : 0;
2785 invalidateHwcGeometry();
2786 repaintEverything();
2787 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002788 case 1009: // toggle use of transform hint
2789 n = data.readInt32();
2790 mDebugDisableTransformHint = n ? 1 : 0;
2791 invalidateHwcGeometry();
2792 repaintEverything();
2793 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002794 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002795 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002796 reply->writeInt32(0);
2797 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002798 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002799 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002800 return NO_ERROR;
2801 case 1013: {
2802 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002803 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2804 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002805 return NO_ERROR;
2806 }
2807 case 1014: {
2808 // daltonize
2809 n = data.readInt32();
2810 switch (n % 10) {
2811 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2812 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2813 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2814 }
2815 if (n >= 10) {
2816 mDaltonizer.setMode(Daltonizer::correction);
2817 } else {
2818 mDaltonizer.setMode(Daltonizer::simulation);
2819 }
2820 mDaltonize = n > 0;
2821 invalidateHwcGeometry();
2822 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002823 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002824 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002825 case 1015: {
2826 // apply a color matrix
2827 n = data.readInt32();
2828 mHasColorMatrix = n ? 1 : 0;
2829 if (n) {
2830 // color matrix is sent as mat3 matrix followed by vec3
2831 // offset, then packed into a mat4 where the last row is
2832 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002833 for (size_t i = 0 ; i < 4; i++) {
2834 for (size_t j = 0; j < 4; j++) {
2835 mColorMatrix[i][j] = data.readFloat();
2836 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002837 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002838 } else {
2839 mColorMatrix = mat4();
2840 }
2841 invalidateHwcGeometry();
2842 repaintEverything();
2843 return NO_ERROR;
2844 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002845 // This is an experimental interface
2846 // Needs to be shifted to proper binder interface when we productize
2847 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07002848 n = data.readInt32();
2849 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002850 return NO_ERROR;
2851 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002852 }
2853 }
2854 return err;
2855}
2856
Mathias Agopian53331da2011-08-22 21:44:41 -07002857void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002858 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002859 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002860}
2861
Mathias Agopian59119e62010-10-11 12:37:43 -07002862// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002863// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002864// ---------------------------------------------------------------------------
2865
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002866/* The code below is here to handle b/8734824
2867 *
2868 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07002869 * from the surfaceflinger thread to the calling binder thread, where they
2870 * are executed. This allows the calling thread in the calling process to be
2871 * reused and not depend on having "enough" binder threads to handle the
2872 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002873 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002874class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07002875 /* Parts of GraphicProducerWrapper are run on two different threads,
2876 * communicating by sending messages via Looper but also by shared member
2877 * data. Coherence maintenance is subtle and in places implicit (ugh).
2878 *
2879 * Don't rely on Looper's sendMessage/handleMessage providing
2880 * release/acquire semantics for any data not actually in the Message.
2881 * Data going from surfaceflinger to binder threads needs to be
2882 * synchronized explicitly.
2883 *
2884 * Barrier open/wait do provide release/acquire semantics. This provides
2885 * implicit synchronization for data coming back from binder to
2886 * surfaceflinger threads.
2887 */
2888
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002889 sp<IGraphicBufferProducer> impl;
2890 sp<Looper> looper;
2891 status_t result;
2892 bool exitPending;
2893 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07002894 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002895 uint32_t code;
2896 Parcel const* data;
2897 Parcel* reply;
2898
2899 enum {
2900 MSG_API_CALL,
2901 MSG_EXIT
2902 };
2903
2904 /*
Jesse Hallb154c422014-07-13 12:47:02 -07002905 * Called on surfaceflinger thread. This is called by our "fake"
2906 * BpGraphicBufferProducer. We package the data and reply Parcel and
2907 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002908 */
2909 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08002910 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002911 this->code = code;
2912 this->data = &data;
2913 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002914 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07002915 // if we've exited, we run the message synchronously right here.
2916 // note (JH): as far as I can tell from looking at the code, this
2917 // never actually happens. if it does, i'm not sure if it happens
2918 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002919 handleMessage(Message(MSG_API_CALL));
2920 } else {
2921 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07002922 // Prevent stores to this->{code, data, reply} from being
2923 // reordered later than the construction of Message.
2924 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002925 looper->sendMessage(this, Message(MSG_API_CALL));
2926 barrier.wait();
2927 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08002928 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002929 }
2930
2931 /*
Jesse Hallb154c422014-07-13 12:47:02 -07002932 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002933 * call the real BpGraphicBufferProducer.
2934 */
2935 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07002936 int what = message.what;
2937 // Prevent reads below from happening before the read from Message
2938 atomic_thread_fence(memory_order_acquire);
2939 if (what == MSG_API_CALL) {
Marco Nelissen2ea926b2014-11-14 08:01:01 -08002940 result = IInterface::asBinder(impl)->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002941 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07002942 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002943 exitRequested = true;
2944 }
2945 }
2946
2947public:
Jesse Hallb154c422014-07-13 12:47:02 -07002948 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
2949 : impl(impl),
2950 looper(new Looper(true)),
2951 exitPending(false),
2952 exitRequested(false)
2953 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002954
Jesse Hallb154c422014-07-13 12:47:02 -07002955 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002956 status_t waitForResponse() {
2957 do {
2958 looper->pollOnce(-1);
2959 } while (!exitRequested);
2960 return result;
2961 }
2962
Jesse Hallb154c422014-07-13 12:47:02 -07002963 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002964 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002965 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002966 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07002967 // Ensure this->result is visible to the binder thread before it
2968 // handles the message.
2969 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002970 looper->sendMessage(this, Message(MSG_EXIT));
2971 }
2972};
2973
2974
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002975status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2976 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002977 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002978 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07002979 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002980
2981 if (CC_UNLIKELY(display == 0))
2982 return BAD_VALUE;
2983
2984 if (CC_UNLIKELY(producer == 0))
2985 return BAD_VALUE;
2986
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002987 // if we have secure windows on this display, never allow the screen capture
2988 // unless the producer interface is local (i.e.: we can take a screenshot for
2989 // ourselves).
Marco Nelissen2ea926b2014-11-14 08:01:01 -08002990 if (!IInterface::asBinder(producer)->localBinder()) {
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002991 Mutex::Autolock _l(mStateLock);
2992 sp<const DisplayDevice> hw(getDisplayDevice(display));
2993 if (hw->getSecureLayerVisible()) {
2994 ALOGW("FB is protected: PERMISSION_DENIED");
2995 return PERMISSION_DENIED;
2996 }
2997 }
2998
Riley Andrewsc3ebe662014-09-04 16:20:31 -07002999 // Convert to surfaceflinger's internal rotation type.
3000 Transform::orientation_flags rotationFlags;
3001 switch (rotation) {
3002 case ISurfaceComposer::eRotateNone:
3003 rotationFlags = Transform::ROT_0;
3004 break;
3005 case ISurfaceComposer::eRotate90:
3006 rotationFlags = Transform::ROT_90;
3007 break;
3008 case ISurfaceComposer::eRotate180:
3009 rotationFlags = Transform::ROT_180;
3010 break;
3011 case ISurfaceComposer::eRotate270:
3012 rotationFlags = Transform::ROT_270;
3013 break;
3014 default:
3015 rotationFlags = Transform::ROT_0;
3016 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3017 break;
3018 }
3019
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003020 class MessageCaptureScreen : public MessageBase {
3021 SurfaceFlinger* flinger;
3022 sp<IBinder> display;
3023 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003024 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003025 uint32_t reqWidth, reqHeight;
3026 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003027 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003028 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003029 status_t result;
3030 public:
3031 MessageCaptureScreen(SurfaceFlinger* flinger,
3032 const sp<IBinder>& display,
3033 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003034 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003035 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003036 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003037 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003038 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003039 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003040 useIdentityTransform(useIdentityTransform),
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003041 rotation(rotation),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003042 result(PERMISSION_DENIED)
3043 {
3044 }
3045 status_t getResult() const {
3046 return result;
3047 }
3048 virtual bool handler() {
3049 Mutex::Autolock _l(flinger->mStateLock);
3050 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003051 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003052 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003053 useIdentityTransform, rotation);
Marco Nelissen2ea926b2014-11-14 08:01:01 -08003054 static_cast<GraphicProducerWrapper*>(IInterface::asBinder(producer).get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003055 return true;
3056 }
3057 };
3058
Mathias Agopian9eb1f052013-04-10 16:27:17 -07003059 // make sure to process transactions before screenshots -- a transaction
3060 // might already be pending but scheduled for VSYNC; this guarantees we
3061 // will handle it before the screenshot. When VSYNC finally arrives
3062 // the scheduled transaction will be a no-op. If no transactions are
3063 // scheduled at this time, this will end-up being a no-op as well.
3064 mEventQueue.invalidateTransactionNow();
3065
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003066 // this creates a "fake" BBinder which will serve as a "fake" remote
3067 // binder to receive the marshaled calls and forward them to the
3068 // real remote (a BpGraphicBufferProducer)
3069 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3070
3071 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3072 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003073 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003074 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003075 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003076 useIdentityTransform, rotationFlags);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003077
3078 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003079 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003080 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003081 }
3082 return res;
3083}
3084
Mathias Agopian180f10d2013-04-10 22:55:41 -07003085
3086void SurfaceFlinger::renderScreenImplLocked(
3087 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003088 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003089 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003090 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003091{
3092 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003093 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003094
3095 // get screen geometry
Andreas Gampe89fd4f72014-11-13 14:18:56 -08003096 const int32_t hw_w = hw->getWidth();
3097 const int32_t hw_h = hw->getHeight();
3098 const bool filtering = static_cast<int32_t>(reqWidth) != hw_w ||
3099 static_cast<int32_t>(reqWidth) != hw_h;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003100
Dan Stozac1879002014-05-22 15:59:05 -07003101 // if a default or invalid sourceCrop is passed in, set reasonable values
3102 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3103 !sourceCrop.isValid()) {
3104 sourceCrop.setLeftTop(Point(0, 0));
3105 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3106 }
3107
3108 // ensure that sourceCrop is inside screen
3109 if (sourceCrop.left < 0) {
3110 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3111 }
Dan Stozabe31f442014-06-11 11:20:54 -07003112 if (sourceCrop.right > hw_w) {
3113 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003114 }
3115 if (sourceCrop.top < 0) {
3116 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3117 }
Dan Stozabe31f442014-06-11 11:20:54 -07003118 if (sourceCrop.bottom > hw_h) {
3119 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003120 }
3121
Mathias Agopian180f10d2013-04-10 22:55:41 -07003122 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003123 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003124
3125 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003126 engine.setViewportAndProjection(
3127 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003128 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003129
3130 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003131 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003132
3133 const LayerVector& layers( mDrawingState.layersSortedByZ );
3134 const size_t count = layers.size();
3135 for (size_t i=0 ; i<count ; ++i) {
3136 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003137 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003138 if (state.layerStack == hw->getLayerStack()) {
3139 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3140 if (layer->isVisible()) {
3141 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08003142 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003143 if (filtering) layer->setFiltering(false);
3144 }
3145 }
3146 }
3147 }
3148
3149 // compositionComplete is needed for older driver
3150 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003151 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003152}
3153
3154
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003155status_t SurfaceFlinger::captureScreenImplLocked(
3156 const sp<const DisplayDevice>& hw,
3157 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003158 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003159 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003160 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003161{
3162 ATRACE_CALL();
3163
Mathias Agopian180f10d2013-04-10 22:55:41 -07003164 // get screen geometry
3165 const uint32_t hw_w = hw->getWidth();
3166 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003167
Mathias Agopian180f10d2013-04-10 22:55:41 -07003168 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3169 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3170 reqWidth, reqHeight, hw_w, hw_h);
3171 return BAD_VALUE;
3172 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003173
Mathias Agopian180f10d2013-04-10 22:55:41 -07003174 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3175 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3176
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003177 // create a surface (because we're a producer, and we need to
3178 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003179 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003180 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003181
3182 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003183 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003184 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3185 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003186
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003187 int err = 0;
3188 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003189 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003190 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3191 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003192
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003193 if (err == NO_ERROR) {
3194 ANativeWindowBuffer* buffer;
3195 /* TODO: Once we have the sync framework everywhere this can use
3196 * server-side waits on the fence that dequeueBuffer returns.
3197 */
3198 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3199 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003200 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003201 // create an EGLImage from the buffer so we can later
3202 // turn it into a texture
3203 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3204 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3205 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003206 // this binds the given EGLImage as a framebuffer for the
3207 // duration of this scope.
3208 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3209 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003210 // this will in fact render into our dequeued buffer
3211 // via an FBO, which means we didn't have to create
3212 // an EGLSurface and therefore we're not
3213 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003214 renderScreenImplLocked(
3215 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3216 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003217
Riley Andrews86639902014-08-15 12:27:24 -07003218 // Attempt to create a sync khr object that can produce a sync point. If that
3219 // isn't available, create a non-dupable sync object in the fallback path and
3220 // wait on it directly.
3221 EGLSyncKHR sync;
3222 if (!DEBUG_SCREENSHOTS) {
3223 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Riley Andrews9707f4d2014-10-23 16:17:04 -07003224 // native fence fd will not be populated until flush() is done.
3225 getRenderEngine().flush();
Andy McFadden2d8d1202013-10-09 16:38:02 -07003226 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003227 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003228 }
Riley Andrews86639902014-08-15 12:27:24 -07003229 if (sync != EGL_NO_SYNC_KHR) {
3230 // get the sync fd
3231 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3232 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3233 ALOGW("captureScreen: failed to dup sync khr object");
3234 syncFd = -1;
3235 }
3236 eglDestroySyncKHR(mEGLDisplay, sync);
3237 } else {
3238 // fallback path
3239 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3240 if (sync != EGL_NO_SYNC_KHR) {
3241 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3242 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3243 EGLint eglErr = eglGetError();
3244 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3245 ALOGW("captureScreen: fence wait timed out");
3246 } else {
3247 ALOGW_IF(eglErr != EGL_SUCCESS,
3248 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3249 }
3250 eglDestroySyncKHR(mEGLDisplay, sync);
3251 } else {
3252 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3253 }
3254 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003255 if (DEBUG_SCREENSHOTS) {
3256 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3257 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3258 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3259 hw, minLayerZ, maxLayerZ);
3260 delete [] pixels;
3261 }
3262
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003263 } else {
3264 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3265 result = INVALID_OPERATION;
3266 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003267 // destroy our image
3268 eglDestroyImageKHR(mEGLDisplay, image);
3269 } else {
3270 result = BAD_VALUE;
3271 }
Jesse Hallafe2b1f2014-10-21 11:09:17 -07003272 // queueBuffer takes ownership of syncFd
Riley Andrews86639902014-08-15 12:27:24 -07003273 window->queueBuffer(window, buffer, syncFd);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003274 }
3275 } else {
3276 result = BAD_VALUE;
3277 }
3278 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3279 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003280
Mathias Agopian74c40c02010-09-29 13:02:36 -07003281 return result;
3282}
3283
Mathias Agopiand5556842013-09-19 17:08:37 -07003284void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3285 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003286 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003287 for (size_t y=0 ; y<h ; y++) {
3288 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3289 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003290 if (p[x] != 0xFF000000) return;
3291 }
3292 }
3293 ALOGE("*** we just took a black screenshot ***\n"
3294 "requested minz=%d, maxz=%d, layerStack=%d",
3295 minLayerZ, maxLayerZ, hw->getLayerStack());
3296 const LayerVector& layers( mDrawingState.layersSortedByZ );
3297 const size_t count = layers.size();
3298 for (size_t i=0 ; i<count ; ++i) {
3299 const sp<Layer>& layer(layers[i]);
3300 const Layer::State& state(layer->getDrawingState());
3301 const bool visible = (state.layerStack == hw->getLayerStack())
3302 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3303 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003304 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003305 visible ? '+' : '-',
3306 i, layer->getName().string(), state.layerStack, state.z,
3307 layer->isVisible(), state.flags, state.alpha);
3308 }
3309 }
3310}
3311
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003312// ---------------------------------------------------------------------------
3313
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003314SurfaceFlinger::LayerVector::LayerVector() {
3315}
3316
3317SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003318 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003319}
3320
3321int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3322 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003323{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003324 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003325 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3326 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003327
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003328 uint32_t ls = l->getCurrentState().layerStack;
3329 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003330 if (ls != rs)
3331 return ls - rs;
3332
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003333 uint32_t lz = l->getCurrentState().z;
3334 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003335 if (lz != rz)
3336 return lz - rz;
3337
3338 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003339}
3340
3341// ---------------------------------------------------------------------------
3342
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003343SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Michael Lentine47e45402014-07-18 15:34:25 -07003344 : type(DisplayDevice::DISPLAY_ID_INVALID), width(0), height(0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003345}
3346
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003347SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Michael Lentine47e45402014-07-18 15:34:25 -07003348 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0), width(0), height(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003349 viewport.makeInvalid();
3350 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003351}
3352
3353// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003354
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003355}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003356
3357
3358#if defined(__gl_h_)
3359#error "don't include gl/gl.h in this file"
3360#endif
3361
3362#if defined(__gl2_h_)
3363#error "don't include gl2/gl2.h in this file"
3364#endif