blob: 0ff26be27a4bfa0e60cb716c03b799c92ea0f608 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Greg Hackmann86efcc02014-03-07 12:44:02 -080024#include <inttypes.h>
Jesse Hallb154c422014-07-13 12:47:02 -070025#include <stdatomic.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070026
27#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036
Mathias Agopianc666cae2012-07-25 18:56:13 -070037#include <ui/DisplayInfo.h>
Lajos Molnar67d8bd62014-09-11 14:58:45 -070038#include <ui/DisplayStatInfo.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070039
Mathias Agopian921e6ac2012-07-23 23:11:29 -070040#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070041#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070042#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070043#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080044#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080045#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070046
47#include <ui/GraphicBufferAllocator.h>
48#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070049#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080050
Mathias Agopiancde87a32012-09-13 14:09:01 -070051#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include <utils/String8.h>
53#include <utils/String16.h>
54#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080055#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopian921e6ac2012-07-23 23:11:29 -070057#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070058#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
Mathias Agopian3e25fd82013-04-22 17:52:16 +020060#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020062#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080063#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070064#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070065#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070066#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080067#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071
Mathias Agopiana4912602012-07-12 14:25:33 -070072#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070073#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070074#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075
Mathias Agopianff2ed702013-09-01 21:36:12 -070076#include "Effects/Daltonizer.h"
77
Mathias Agopian875d8e12013-06-07 15:35:48 -070078#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070079#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070080
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081#define DISPLAY_COUNT 1
82
Mathias Agopianfee2b462013-07-03 12:34:01 -070083/*
84 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
85 * black pixels.
86 */
87#define DEBUG_SCREENSHOTS false
88
Mathias Agopianca088332013-03-28 17:44:13 -070089EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
90
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070092
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070093// This is the phase offset in nanoseconds of the software vsync event
94// relative to the vsync event reported by HWComposer. The software vsync
95// event is when SurfaceFlinger and Choreographer-based applications run each
96// frame.
97//
98// This phase offset allows adjustment of the minimum latency from application
99// wake-up (by Choregographer) time to the time at which the resulting window
100// image is displayed. This value may be either positive (after the HW vsync)
101// or negative (before the HW vsync). Setting it to 0 will result in a
102// minimum latency of two vsync periods because the app and SurfaceFlinger
103// will run just after the HW vsync. Setting it to a positive number will
104// result in the minimum latency being:
105//
106// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
107//
108// Note that reducing this latency makes it more likely for the applications
109// to not have their window content image ready in time. When this happens
110// the latency will end up being an additional vsync period, and animations
111// will hiccup. Therefore, this latency should be tuned somewhat
112// conservatively (or at least with awareness of the trade-off being made).
113static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
114
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700115// This is the phase offset at which SurfaceFlinger's composition runs.
116static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
117
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118// ---------------------------------------------------------------------------
119
Mathias Agopian99b49842011-06-27 16:05:52 -0700120const String16 sHardwareTest("android.permission.HARDWARE_TEST");
121const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
122const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
123const String16 sDump("android.permission.DUMP");
124
125// ---------------------------------------------------------------------------
126
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700128 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700130 mTransactionPending(false),
131 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700132 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700133 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700134 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700137 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800138 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700140 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700141 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700142 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700143 mDebugInSwapBuffers(0),
144 mLastSwapBufferTime(0),
145 mDebugInTransaction(0),
146 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700147 mBootFinished(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700148 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700149 mHWVsyncAvailable(false),
Alan Viverette9c5a3332013-09-12 20:04:35 -0700150 mDaltonize(false),
151 mHasColorMatrix(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152{
Steve Blocka19954a2012-01-04 20:05:49 +0000153 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154
155 // debugging stuff...
156 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700157
Mathias Agopianb4b17302013-03-20 18:36:41 -0700158 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700159 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700160
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 property_get("debug.sf.showupdates", value, "0");
162 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700163
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700164 property_get("debug.sf.ddms", value, "0");
165 mDebugDDMS = atoi(value);
166 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700167 if (!startDdmConnection()) {
168 // start failed, and DDMS debugging not enabled
169 mDebugDDMS = 0;
170 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700171 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700172 ALOGI_IF(mDebugRegion, "showupdates enabled");
173 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174}
175
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800176void SurfaceFlinger::onFirstRef()
177{
178 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800179}
180
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181SurfaceFlinger::~SurfaceFlinger()
182{
Mathias Agopiana4912602012-07-12 14:25:33 -0700183 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
184 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
185 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186}
187
Dan Stozac7014012014-02-14 15:03:43 -0800188void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800189{
190 // the window manager died on us. prepare its eulogy.
191
Andy McFadden13a082e2012-08-24 10:16:42 -0700192 // restore initial conditions (default device unblank, etc)
193 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800194
195 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700196 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800197}
198
Mathias Agopian7e27f052010-05-28 14:22:23 -0700199sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200{
Mathias Agopian96f08192010-06-02 23:28:45 -0700201 sp<ISurfaceComposerClient> bclient;
202 sp<Client> client(new Client(this));
203 status_t err = client->initCheck();
204 if (err == NO_ERROR) {
205 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 return bclient;
208}
209
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700210sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
211 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700212{
213 class DisplayToken : public BBinder {
214 sp<SurfaceFlinger> flinger;
215 virtual ~DisplayToken() {
216 // no more references, this display must be terminated
217 Mutex::Autolock _l(flinger->mStateLock);
218 flinger->mCurrentState.displays.removeItem(this);
219 flinger->setTransactionFlags(eDisplayTransactionNeeded);
220 }
221 public:
222 DisplayToken(const sp<SurfaceFlinger>& flinger)
223 : flinger(flinger) {
224 }
225 };
226
227 sp<BBinder> token = new DisplayToken(this);
228
229 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700230 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700231 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700232 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700233 mCurrentState.displays.add(token, info);
234
235 return token;
236}
237
Jesse Hall6c913be2013-08-08 12:15:49 -0700238void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
239 Mutex::Autolock _l(mStateLock);
240
241 ssize_t idx = mCurrentState.displays.indexOfKey(display);
242 if (idx < 0) {
243 ALOGW("destroyDisplay: invalid display token");
244 return;
245 }
246
247 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
248 if (!info.isVirtualDisplay()) {
249 ALOGE("destroyDisplay called for non-virtual display");
250 return;
251 }
252
253 mCurrentState.displays.removeItemsAt(idx);
254 setTransactionFlags(eDisplayTransactionNeeded);
255}
256
Jesse Hall692c7232012-11-08 15:41:56 -0800257void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
258 ALOGW_IF(mBuiltinDisplays[type],
259 "Overwriting display token for display type %d", type);
260 mBuiltinDisplays[type] = new BBinder();
261 DisplayDeviceState info(type);
262 // All non-virtual displays are currently considered secure.
263 info.isSecure = true;
264 mCurrentState.displays.add(mBuiltinDisplays[type], info);
265}
266
Mathias Agopiane57f2922012-08-09 16:29:12 -0700267sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700268 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700269 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
270 return NULL;
271 }
Jesse Hall692c7232012-11-08 15:41:56 -0800272 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700273}
274
Jamie Gennis9a78c902011-01-12 18:30:40 -0800275sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
276{
277 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
278 return gba;
279}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700280
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281void SurfaceFlinger::bootFinished()
282{
283 const nsecs_t now = systemTime();
284 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000285 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700286 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700287
288 // wait patiently for the window manager death
289 const String16 name("window");
290 sp<IBinder> window(defaultServiceManager()->getService(name));
291 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700292 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700293 }
294
295 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700296 // formerly we would just kill the process, but we now ask it to exit so it
297 // can choose where to stop the animation.
298 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299}
300
Mathias Agopian3f844832013-08-07 21:24:32 -0700301void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700302 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700303 RenderEngine& engine;
304 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700305 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700306 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
307 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700308 }
309 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700310 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700311 return true;
312 }
313 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700314 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700315}
316
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700317class DispSyncSource : public VSyncSource, private DispSync::Callback {
318public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700319 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
320 const char* label) :
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700321 mValue(0),
322 mPhaseOffset(phaseOffset),
323 mTraceVsync(traceVsync),
Andy McFadden5167ec62014-05-22 13:08:43 -0700324 mVsyncOnLabel(String8::format("VsyncOn-%s", label)),
325 mVsyncEventLabel(String8::format("VSYNC-%s", label)),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700326 mDispSync(dispSync) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700327
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700328 virtual ~DispSyncSource() {}
329
330 virtual void setVSyncEnabled(bool enable) {
331 // Do NOT lock the mutex here so as to avoid any mutex ordering issues
332 // with locking it in the onDispSyncEvent callback.
333 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700334 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700335 static_cast<DispSync::Callback*>(this));
336 if (err != NO_ERROR) {
337 ALOGE("error registering vsync callback: %s (%d)",
338 strerror(-err), err);
339 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700340 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700341 } else {
342 status_t err = mDispSync->removeEventListener(
343 static_cast<DispSync::Callback*>(this));
344 if (err != NO_ERROR) {
345 ALOGE("error unregistering vsync callback: %s (%d)",
346 strerror(-err), err);
347 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700348 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700349 }
350 }
351
352 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
353 Mutex::Autolock lock(mMutex);
354 mCallback = callback;
355 }
356
357private:
358 virtual void onDispSyncEvent(nsecs_t when) {
359 sp<VSyncSource::Callback> callback;
360 {
361 Mutex::Autolock lock(mMutex);
362 callback = mCallback;
363
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700364 if (mTraceVsync) {
365 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700366 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700367 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700368 }
369
370 if (callback != NULL) {
371 callback->onVSyncEvent(when);
372 }
373 }
374
375 int mValue;
376
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700377 const nsecs_t mPhaseOffset;
378 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700379 const String8 mVsyncOnLabel;
380 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700381
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700382 DispSync* mDispSync;
383 sp<VSyncSource::Callback> mCallback;
384 Mutex mMutex;
385};
386
387void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700388 ALOGI( "SurfaceFlinger's main thread ready to run. "
389 "Initializing graphics H/W...");
390
Jesse Hallb65f32e2013-09-27 22:10:47 -0700391 status_t err;
Jesse Hall692c7232012-11-08 15:41:56 -0800392 Mutex::Autolock _l(mStateLock);
393
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700394 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700395 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
396 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700397
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700398 // Initialize the H/W composer object. There may or may not be an
399 // actual hardware composer underneath.
400 mHwc = new HWComposer(this,
401 *static_cast<HWComposer::EventHandler *>(this));
402
Mathias Agopian875d8e12013-06-07 15:35:48 -0700403 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800404 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700405
406 // retrieve the EGL context that was selected/created
407 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700408
Mathias Agopianda27af92012-09-13 18:17:13 -0700409 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
410 "couldn't create EGLContext");
411
Mathias Agopiancde87a32012-09-13 14:09:01 -0700412 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700413 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700414 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700415 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700416 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700417 // All non-virtual displays are currently considered secure.
418 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800419 createBuiltinDisplayLocked(type);
420 wp<IBinder> token = mBuiltinDisplays[i];
421
Dan Stozab9b08832014-03-13 11:55:57 -0700422 sp<IGraphicBufferProducer> producer;
423 sp<IGraphicBufferConsumer> consumer;
424 BufferQueue::createBufferQueue(&producer, &consumer,
425 new GraphicBufferAlloc());
426
427 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
428 consumer);
Jesse Hall19e87292013-12-23 21:02:15 -0800429 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700430 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800431 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Dan Stozab9b08832014-03-13 11:55:57 -0700432 fbs, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -0800433 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700434 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700435 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700436 // for displays other than the main display, so we always
437 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800438 ALOGD("marking display %zu as acquired/unblanked", i);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700439 hw->setPowerMode(HWC_POWER_MODE_NORMAL);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700440 }
441 mDisplays.add(token, hw);
442 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700443 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700444
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700445 // make the GLContext current so that we can create textures when creating Layers
446 // (which may happens before we render something)
447 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
448
Mathias Agopian028508c2012-07-25 21:12:12 -0700449 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700450 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700451 vsyncPhaseOffsetNs, true, "app");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700452 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700453 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700454 sfVsyncPhaseOffsetNs, true, "sf");
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700455 mSFEventThread = new EventThread(sfVsyncSrc);
456 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700457
Jamie Gennisd1700752013-10-14 12:22:52 -0700458 mEventControlThread = new EventControlThread(this);
459 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
460
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700461 // set a fake vsync period if there is no HWComposer
462 if (mHwc->initCheck() != NO_ERROR) {
463 mPrimaryDispSync.setPeriod(16666667);
464 }
465
Mathias Agopian92a979a2012-08-02 18:32:23 -0700466 // initialize our drawing state
467 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700468
Andy McFadden13a082e2012-08-24 10:16:42 -0700469 // set initial conditions (e.g. unblank default device)
470 initializeDisplays();
471
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700472 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700473 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800474}
475
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700476int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700477 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700478 type : mHwc->allocateDisplayId();
479}
480
Mathias Agopiana67e4182012-06-19 17:26:12 -0700481void SurfaceFlinger::startBootAnim() {
482 // start boot animation
483 property_set("service.bootanim.exit", "0");
484 property_set("ctl.start", "bootanim");
485}
486
Mathias Agopian875d8e12013-06-07 15:35:48 -0700487size_t SurfaceFlinger::getMaxTextureSize() const {
488 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700489}
490
Mathias Agopian875d8e12013-06-07 15:35:48 -0700491size_t SurfaceFlinger::getMaxViewportDims() const {
492 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700493}
494
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800495// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800496
Jamie Gennis582270d2011-08-17 18:19:00 -0700497bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800498 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800499 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800500 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700501 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800502}
503
Dan Stoza7f7da322014-05-02 15:26:25 -0700504status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
505 Vector<DisplayInfo>* configs) {
506 if (configs == NULL) {
507 return BAD_VALUE;
508 }
509
Jesse Hall692c7232012-11-08 15:41:56 -0800510 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700511 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800512 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700513 type = i;
514 break;
515 }
516 }
517
518 if (type < 0) {
519 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700520 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700521
Mathias Agopian8b736f12012-08-13 17:54:26 -0700522 // TODO: Not sure if display density should handled by SF any longer
523 class Density {
524 static int getDensityFromProperty(char const* propName) {
525 char property[PROPERTY_VALUE_MAX];
526 int density = 0;
527 if (property_get(propName, property, NULL) > 0) {
528 density = atoi(property);
529 }
530 return density;
531 }
532 public:
533 static int getEmuDensity() {
534 return getDensityFromProperty("qemu.sf.lcd_density"); }
535 static int getBuildDensity() {
536 return getDensityFromProperty("ro.sf.lcd_density"); }
537 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700538
Dan Stoza7f7da322014-05-02 15:26:25 -0700539 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700540
Dan Stoza7f7da322014-05-02 15:26:25 -0700541 const Vector<HWComposer::DisplayConfig>& hwConfigs =
542 getHwComposer().getConfigs(type);
543 for (size_t c = 0; c < hwConfigs.size(); ++c) {
544 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
545 DisplayInfo info = DisplayInfo();
546
547 float xdpi = hwConfig.xdpi;
548 float ydpi = hwConfig.ydpi;
549
550 if (type == DisplayDevice::DISPLAY_PRIMARY) {
551 // The density of the device is provided by a build property
552 float density = Density::getBuildDensity() / 160.0f;
553 if (density == 0) {
554 // the build doesn't provide a density -- this is wrong!
555 // use xdpi instead
556 ALOGE("ro.sf.lcd_density must be defined as a build property");
557 density = xdpi / 160.0f;
558 }
559 if (Density::getEmuDensity()) {
560 // if "qemu.sf.lcd_density" is specified, it overrides everything
561 xdpi = ydpi = density = Density::getEmuDensity();
562 density /= 160.0f;
563 }
564 info.density = density;
565
566 // TODO: this needs to go away (currently needed only by webkit)
567 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
568 info.orientation = hw->getOrientation();
569 } else {
570 // TODO: where should this value come from?
571 static const int TV_DENSITY = 213;
572 info.density = TV_DENSITY / 160.0f;
573 info.orientation = 0;
574 }
575
576 info.w = hwConfig.width;
577 info.h = hwConfig.height;
578 info.xdpi = xdpi;
579 info.ydpi = ydpi;
580 info.fps = float(1e9 / hwConfig.refresh);
Andy McFadden91b2ca82014-06-13 14:04:23 -0700581 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
582
583 // This is how far in advance a buffer must be queued for
584 // presentation at a given time. If you want a buffer to appear
585 // on the screen at time N, you must submit the buffer before
586 // (N - presentationDeadline).
587 //
588 // Normally it's one full refresh period (to give SF a chance to
589 // latch the buffer), but this can be reduced by configuring a
590 // DispSync offset. Any additional delays introduced by the hardware
591 // composer or panel must be accounted for here.
592 //
593 // We add an additional 1ms to allow for processing time and
594 // differences between the ideal and actual refresh rate.
595 info.presentationDeadline =
596 hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700597
598 // All non-virtual displays are currently considered secure.
599 info.secure = true;
600
601 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700602 }
603
Dan Stoza7f7da322014-05-02 15:26:25 -0700604 return NO_ERROR;
605}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700606
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700607status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& display,
608 DisplayStatInfo* stats) {
609 if (stats == NULL) {
610 return BAD_VALUE;
611 }
612
613 // FIXME for now we always return stats for the primary display
614 memset(stats, 0, sizeof(*stats));
615 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
616 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
617 return NO_ERROR;
618}
619
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700620int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
621 return getDisplayDevice(display)->getActiveConfig();
Dan Stoza7f7da322014-05-02 15:26:25 -0700622}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700623
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700624void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
625 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
626 this);
627 int32_t type = hw->getDisplayType();
628 int currentMode = hw->getActiveConfig();
629
630 if (mode == currentMode) {
631 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
632 return;
633 }
634
635 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
636 ALOGW("Trying to set config for virtual display");
637 return;
638 }
639
640 hw->setActiveConfig(mode);
641 getHwComposer().setActiveConfig(type, mode);
642}
643
644status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
645 class MessageSetActiveConfig: public MessageBase {
646 SurfaceFlinger& mFlinger;
647 sp<IBinder> mDisplay;
648 int mMode;
649 public:
650 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
651 int mode) :
652 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
653 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700654 Vector<DisplayInfo> configs;
655 mFlinger.getDisplayConfigs(mDisplay, &configs);
656 if(mMode < 0 || mMode >= configs.size()) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700657 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700658 mMode, configs.size());
659 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700660 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
661 if (hw == NULL) {
662 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700663 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700664 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
665 ALOGW("Attempt to set active config = %d for virtual display",
666 mMode);
667 } else {
668 mFlinger.setActiveConfigInternal(hw, mMode);
669 }
670 return true;
671 }
672 };
673 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
674 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700675 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700676}
677
Svetoslavd85084b2014-03-20 10:28:31 -0700678status_t SurfaceFlinger::clearAnimationFrameStats() {
679 Mutex::Autolock _l(mStateLock);
680 mAnimFrameTracker.clearStats();
681 return NO_ERROR;
682}
683
684status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
685 Mutex::Autolock _l(mStateLock);
686 mAnimFrameTracker.getStats(outStats);
687 return NO_ERROR;
688}
689
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800690// ----------------------------------------------------------------------------
691
692sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800693 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700694}
695
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800696// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800697
698void SurfaceFlinger::waitForEvent() {
699 mEventQueue.waitMessage();
700}
701
702void SurfaceFlinger::signalTransaction() {
703 mEventQueue.invalidate();
704}
705
706void SurfaceFlinger::signalLayerUpdate() {
707 mEventQueue.invalidate();
708}
709
710void SurfaceFlinger::signalRefresh() {
711 mEventQueue.refresh();
712}
713
714status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800715 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800716 return mEventQueue.postMessage(msg, reltime);
717}
718
719status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800720 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800721 status_t res = mEventQueue.postMessage(msg, reltime);
722 if (res == NO_ERROR) {
723 msg->wait();
724 }
725 return res;
726}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800727
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700728void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700729 do {
730 waitForEvent();
731 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800732}
733
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700734void SurfaceFlinger::enableHardwareVsync() {
735 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700736 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700737 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700738 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
739 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700740 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700741 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700742}
743
Jesse Hall948fe0c2013-10-14 12:56:09 -0700744void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700745 Mutex::Autolock _l(mHWVsyncLock);
746
Jesse Hall948fe0c2013-10-14 12:56:09 -0700747 if (makeAvailable) {
748 mHWVsyncAvailable = true;
749 } else if (!mHWVsyncAvailable) {
750 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
751 return;
752 }
753
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700754 const nsecs_t period =
755 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
756
757 mPrimaryDispSync.reset();
758 mPrimaryDispSync.setPeriod(period);
759
760 if (!mPrimaryHWVsyncEnabled) {
761 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700762 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
763 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700764 mPrimaryHWVsyncEnabled = true;
765 }
766}
767
Jesse Hall948fe0c2013-10-14 12:56:09 -0700768void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700769 Mutex::Autolock _l(mHWVsyncLock);
770 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700771 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
772 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700773 mPrimaryDispSync.endResync();
774 mPrimaryHWVsyncEnabled = false;
775 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700776 if (makeUnavailable) {
777 mHWVsyncAvailable = false;
778 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700779}
780
781void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700782 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700783
Jamie Gennisd1700752013-10-14 12:22:52 -0700784 { // Scope for the lock
785 Mutex::Autolock _l(mHWVsyncLock);
786 if (type == 0 && mPrimaryHWVsyncEnabled) {
787 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700788 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700789 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700790
791 if (needsHwVsync) {
792 enableHardwareVsync();
793 } else {
794 disableHardwareVsync(false);
795 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700796}
797
798void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
799 if (mEventThread == NULL) {
800 // This is a temporary workaround for b/7145521. A non-null pointer
801 // does not mean EventThread has finished initializing, so this
802 // is not a correct fix.
803 ALOGW("WARNING: EventThread not started, ignoring hotplug");
804 return;
805 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700806
Jesse Hall9e663de2013-08-16 14:28:37 -0700807 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700808 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800809 if (connected) {
810 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700811 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800812 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
813 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700814 }
815 setTransactionFlags(eDisplayTransactionNeeded);
816
Andy McFadden9e9689c2012-10-10 18:17:51 -0700817 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700818 }
Mathias Agopian86303202012-07-24 22:46:10 -0700819}
820
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700821void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700822 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700823 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700824}
825
Mathias Agopian4fec8732012-06-29 14:12:52 -0700826void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800827 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800828 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700829 case MessageQueue::TRANSACTION:
830 handleMessageTransaction();
831 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700832 case MessageQueue::INVALIDATE:
833 handleMessageTransaction();
834 handleMessageInvalidate();
835 signalRefresh();
836 break;
837 case MessageQueue::REFRESH:
838 handleMessageRefresh();
839 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800840 }
841}
842
Mathias Agopian4fec8732012-06-29 14:12:52 -0700843void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700844 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700845 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700846 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700847 }
848}
849
850void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700851 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700852 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700853}
854
855void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700856 ATRACE_CALL();
857 preComposition();
858 rebuildLayerStacks();
859 setUpHWComposer();
860 doDebugFlashRegions();
861 doComposition();
862 postComposition();
863}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700864
Mathias Agopiancd60f992012-08-16 16:28:27 -0700865void SurfaceFlinger::doDebugFlashRegions()
866{
867 // is debugging enabled
868 if (CC_LIKELY(!mDebugRegion))
869 return;
870
871 const bool repaintEverything = mRepaintEverything;
872 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
873 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700874 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700875 // transform the dirty region into this screen's coordinate space
876 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
877 if (!dirtyRegion.isEmpty()) {
878 // redraw the whole screen
879 doComposeSurfaces(hw, Region(hw->bounds()));
880
881 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700882 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700883 RenderEngine& engine(getRenderEngine());
884 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
885
Mathias Agopiancd60f992012-08-16 16:28:27 -0700886 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700887 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700888 }
889 }
890 }
891
892 postFramebuffer();
893
894 if (mDebugRegion > 1) {
895 usleep(mDebugRegion * 1000);
896 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700897
898 HWComposer& hwc(getHwComposer());
899 if (hwc.initCheck() == NO_ERROR) {
900 status_t err = hwc.prepare();
901 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
902 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700903}
904
905void SurfaceFlinger::preComposition()
906{
907 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700908 const LayerVector& layers(mDrawingState.layersSortedByZ);
909 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700910 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700911 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700912 needExtraInvalidate = true;
913 }
914 }
915 if (needExtraInvalidate) {
916 signalLayerUpdate();
917 }
918}
919
920void SurfaceFlinger::postComposition()
921{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700922 const LayerVector& layers(mDrawingState.layersSortedByZ);
923 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700924 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700925 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700926 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800927
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700928 const HWComposer& hwc = getHwComposer();
929 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
930
931 if (presentFence->isValid()) {
932 if (mPrimaryDispSync.addPresentFence(presentFence)) {
933 enableHardwareVsync();
934 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700935 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700936 }
937 }
938
Andy McFadden5167ec62014-05-22 13:08:43 -0700939 if (kIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700940 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700941 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700942 enableHardwareVsync();
943 }
944 }
945
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800946 if (mAnimCompositionPending) {
947 mAnimCompositionPending = false;
948
Jesse Halla9a1b002013-02-27 16:39:25 -0800949 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800950 mAnimFrameTracker.setActualPresentFence(presentFence);
951 } else {
952 // The HWC doesn't support present fences, so use the refresh
953 // timestamp instead.
954 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
955 mAnimFrameTracker.setActualPresentTime(presentTime);
956 }
957 mAnimFrameTracker.advanceFrame();
958 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700959}
960
961void SurfaceFlinger::rebuildLayerStacks() {
962 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700963 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700964 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700965 mVisibleRegionsDirty = false;
966 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700967
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700968 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700969 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700970 Region opaqueRegion;
971 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800972 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700973 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700974 const Transform& tr(hw->getTransform());
975 const Rect bounds(hw->getBounds());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700976 if (hw->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700977 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700978 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700979
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700980 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700981 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700982 const sp<Layer>& layer(layers[i]);
983 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700984 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700985 Region drawRegion(tr.transform(
986 layer->visibleNonTransparentRegion));
987 drawRegion.andSelf(bounds);
988 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700989 layersSortedByZ.add(layer);
990 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700991 }
992 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700993 }
Mathias Agopian42977342012-08-05 00:40:46 -0700994 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700995 hw->undefinedRegion.set(bounds);
996 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
997 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700998 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700999 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001000}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001001
Mathias Agopiancd60f992012-08-16 16:28:27 -07001002void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001003 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001004 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1005 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1006 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1007
1008 // If nothing has changed (!dirty), don't recompose.
1009 // If something changed, but we don't currently have any visible layers,
1010 // and didn't when we last did a composition, then skip it this time.
1011 // The second rule does two things:
1012 // - When all layers are removed from a display, we'll emit one black
1013 // frame, then nothing more until we get new layers.
1014 // - When a display is created with a private layer stack, we won't
1015 // emit any black frames until a layer is added to the layer stack.
1016 bool mustRecompose = dirty && !(empty && wasEmpty);
1017
1018 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1019 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1020 mustRecompose ? "doing" : "skipping",
1021 dirty ? "+" : "-",
1022 empty ? "+" : "-",
1023 wasEmpty ? "+" : "-");
1024
Dan Stoza71433162014-02-04 16:22:36 -08001025 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001026
1027 if (mustRecompose) {
1028 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1029 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001030 }
1031
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001032 HWComposer& hwc(getHwComposer());
1033 if (hwc.initCheck() == NO_ERROR) {
1034 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001035 if (CC_UNLIKELY(mHwWorkListDirty)) {
1036 mHwWorkListDirty = false;
1037 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1038 sp<const DisplayDevice> hw(mDisplays[dpy]);
1039 const int32_t id = hw->getHwcDisplayId();
1040 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001041 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001042 hw->getVisibleLayersSortedByZ());
1043 const size_t count = currentLayers.size();
1044 if (hwc.createWorkList(id, count) == NO_ERROR) {
1045 HWComposer::LayerListIterator cur = hwc.begin(id);
1046 const HWComposer::LayerListIterator end = hwc.end(id);
1047 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001048 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001049 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001050 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001051 cur->setSkip(true);
1052 }
1053 }
1054 }
1055 }
1056 }
1057 }
1058
1059 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001060 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001061 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001062 const int32_t id = hw->getHwcDisplayId();
1063 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001064 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001065 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001066 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001067 HWComposer::LayerListIterator cur = hwc.begin(id);
1068 const HWComposer::LayerListIterator end = hwc.end(id);
1069 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1070 /*
1071 * update the per-frame h/w composer data for each layer
1072 * and build the transparent region of the FB
1073 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001074 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001075 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -07001076 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001077 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001078 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001079
Riley Andrews03414a12014-07-01 14:22:59 -07001080 // If possible, attempt to use the cursor overlay on each display.
1081 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1082 sp<const DisplayDevice> hw(mDisplays[dpy]);
1083 const int32_t id = hw->getHwcDisplayId();
1084 if (id >= 0) {
1085 const Vector< sp<Layer> >& currentLayers(
1086 hw->getVisibleLayersSortedByZ());
1087 const size_t count = currentLayers.size();
1088 HWComposer::LayerListIterator cur = hwc.begin(id);
1089 const HWComposer::LayerListIterator end = hwc.end(id);
1090 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1091 const sp<Layer>& layer(currentLayers[i]);
1092 if (layer->isPotentialCursor()) {
1093 cur->setIsCursorLayerHint();
1094 break;
1095 }
1096 }
1097 }
1098 }
1099
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001100 status_t err = hwc.prepare();
1101 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001102
1103 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1104 sp<const DisplayDevice> hw(mDisplays[dpy]);
1105 hw->prepareFrame(hwc);
1106 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001107 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001108}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001109
Mathias Agopiancd60f992012-08-16 16:28:27 -07001110void SurfaceFlinger::doComposition() {
1111 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001112 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001113 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001114 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001115 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001116 // transform the dirty region into this screen's coordinate space
1117 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001118
1119 // repaint the framebuffer (if needed)
1120 doDisplayComposition(hw, dirtyRegion);
1121
Mathias Agopiancd60f992012-08-16 16:28:27 -07001122 hw->dirtyRegion.clear();
1123 hw->flip(hw->swapRegion);
1124 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001125 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001126 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001127 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001128 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001129 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001130}
1131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001132void SurfaceFlinger::postFramebuffer()
1133{
Mathias Agopian841cde52012-03-01 15:44:37 -08001134 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001135
Mathias Agopiana44b0412011-10-16 18:46:35 -07001136 const nsecs_t now = systemTime();
1137 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001138
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001139 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001140 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001141 if (!hwc.supportsFramebufferTarget()) {
1142 // EGL spec says:
1143 // "surface must be bound to the calling thread's current context,
1144 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001145 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001146 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001147 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001148 }
1149
Mathias Agopian6da15f42013-09-25 20:40:07 -07001150 // make the default display current because the VirtualDisplayDevice code cannot
1151 // deal with dequeueBuffer() being called outside of the composition loop; however
1152 // the code below can call glFlush() which is allowed (and does in some case) call
1153 // dequeueBuffer().
1154 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1155
Mathias Agopian92a979a2012-08-02 18:32:23 -07001156 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001157 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001158 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001159 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001160 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001161 int32_t id = hw->getHwcDisplayId();
1162 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001163 HWComposer::LayerListIterator cur = hwc.begin(id);
1164 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001165 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001166 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001167 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001168 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001169 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001170 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001171 }
Jesse Hallef194142012-06-14 14:45:17 -07001172 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001173 }
1174
Mathias Agopiana44b0412011-10-16 18:46:35 -07001175 mLastSwapBufferTime = systemTime() - now;
1176 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001177
1178 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1179 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1180 logFrameStats();
1181 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001182}
1183
Mathias Agopian87baae12012-07-31 12:38:26 -07001184void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001185{
Mathias Agopian841cde52012-03-01 15:44:37 -08001186 ATRACE_CALL();
1187
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001188 // here we keep a copy of the drawing state (that is the state that's
1189 // going to be overwritten by handleTransactionLocked()) outside of
1190 // mStateLock so that the side-effects of the State assignment
1191 // don't happen with mStateLock held (which can cause deadlocks).
1192 State drawingState(mDrawingState);
1193
Mathias Agopianca4d3602011-05-19 15:38:14 -07001194 Mutex::Autolock _l(mStateLock);
1195 const nsecs_t now = systemTime();
1196 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001197
Mathias Agopianca4d3602011-05-19 15:38:14 -07001198 // Here we're guaranteed that some transaction flags are set
1199 // so we can call handleTransactionLocked() unconditionally.
1200 // We call getTransactionFlags(), which will also clear the flags,
1201 // with mStateLock held to guarantee that mCurrentState won't change
1202 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001203
Mathias Agopiane57f2922012-08-09 16:29:12 -07001204 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001205 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001206
Mathias Agopianca4d3602011-05-19 15:38:14 -07001207 mLastTransactionTime = systemTime() - now;
1208 mDebugInTransaction = 0;
1209 invalidateHwcGeometry();
1210 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001211}
1212
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301213void SurfaceFlinger::setVirtualDisplayData(
1214 int32_t hwcDisplayId,
1215 const sp<IGraphicBufferProducer>& sink)
1216{
1217 sp<ANativeWindow> mNativeWindow = new Surface(sink);
1218 ANativeWindow* const window = mNativeWindow.get();
1219
1220 int format;
1221 window->query(window, NATIVE_WINDOW_FORMAT, &format);
1222
1223 EGLSurface surface;
1224 EGLint w, h;
1225 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1226 surface = eglCreateWindowSurface(display, mRenderEngine->getEGLConfig(), window, NULL);
1227 eglQuerySurface(display, surface, EGL_WIDTH, &w);
1228 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
1229
1230 mHwc->setVirtualDisplayProperties(hwcDisplayId, w, h, format);
1231}
1232
Mathias Agopian87baae12012-07-31 12:38:26 -07001233void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001234{
1235 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001236 const size_t count = currentLayers.size();
1237
1238 /*
1239 * Traversal of the children
1240 * (perform the transaction for each of them if needed)
1241 */
1242
Mathias Agopian3559b072012-08-15 13:46:03 -07001243 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001244 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001245 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001246 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1247 if (!trFlags) continue;
1248
1249 const uint32_t flags = layer->doTransaction(0);
1250 if (flags & Layer::eVisibleRegion)
1251 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001252 }
1253 }
1254
1255 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001256 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001257 */
1258
Mathias Agopiane57f2922012-08-09 16:29:12 -07001259 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001260 // here we take advantage of Vector's copy-on-write semantics to
1261 // improve performance by skipping the transaction entirely when
1262 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001263 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1264 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001265 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001266 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001267 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001268 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001269
1270 // find the displays that were removed
1271 // (ie: in drawing state but not in current state)
1272 // also handle displays that changed
1273 // (ie: displays that are in both lists)
1274 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001275 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1276 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001277 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001278 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001279 // Call makeCurrent() on the primary display so we can
1280 // be sure that nothing associated with this display
1281 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001282 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001283 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001284 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1285 if (hw != NULL)
1286 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001287 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001288 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001289 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001290 } else {
1291 ALOGW("trying to remove the main display");
1292 }
1293 } else {
1294 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001295 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001296 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001297 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001298 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001299 // recreating the DisplayDevice, so we just remove it
1300 // from the drawing state, so that it get re-added
1301 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001302 sp<DisplayDevice> hw(getDisplayDevice(display));
1303 if (hw != NULL)
1304 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001305 mDisplays.removeItem(display);
1306 mDrawingState.displays.removeItemsAt(i);
1307 dc--; i--;
1308 // at this point we must loop to the next item
1309 continue;
1310 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001311
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001312 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001313 if (disp != NULL) {
1314 if (state.layerStack != draw[i].layerStack) {
1315 disp->setLayerStack(state.layerStack);
1316 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001317 if ((state.orientation != draw[i].orientation)
1318 || (state.viewport != draw[i].viewport)
1319 || (state.frame != draw[i].frame))
1320 {
1321 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001322 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001323 }
Michael Lentine47e45402014-07-18 15:34:25 -07001324 if (state.width != draw[i].width || state.height != draw[i].height) {
1325 disp->setDisplaySize(state.width, state.height);
1326 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001327 }
1328 }
1329 }
1330
1331 // find displays that were added
1332 // (ie: in current state but not in drawing state)
1333 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001334 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001335 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001336
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001337 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001338 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001339 sp<IGraphicBufferProducer> bqProducer;
1340 sp<IGraphicBufferConsumer> bqConsumer;
1341 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1342 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001343
Jesse Hall02d86562013-03-25 14:43:23 -07001344 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001345 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001346 // Virtual displays without a surface are dormant:
1347 // they have external state (layer stack, projection,
1348 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001349 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001350
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301351 char value[PROPERTY_VALUE_MAX];
Jesse Hall02d86562013-03-25 14:43:23 -07001352 hwcDisplayId = allocateHwcDisplayId(state.type);
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301353 property_get("persist.sys.wfd.virtual", value, "0");
1354 int wfdVirtual = atoi(value);
1355 if(!wfdVirtual) {
1356 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1357 *mHwc, hwcDisplayId, state.surface,
1358 bqProducer, bqConsumer, state.displayName);
1359 if (hwcDisplayId >= 0) {
1360 producer = vds;
1361 } else {
1362 // There won't be any interaction with HWC for this virtual display,
1363 // so the GLES driver can pass buffers directly to the sink.
1364 producer = state.surface;
1365 }
1366 } else {
1367 //Read virtual display properties and create a
1368 //rendering surface for it inorder to be handled
1369 //by hwc.
1370 setVirtualDisplayData(hwcDisplayId,
1371 state.surface);
1372 dispSurface = new FramebufferSurface(*mHwc,
1373 state.type,
1374 bqConsumer);
1375 producer = bqProducer;
1376 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001377 }
1378 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001379 ALOGE_IF(state.surface!=NULL,
1380 "adding a supported display, but rendering "
1381 "surface is provided (%p), ignoring it",
1382 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001383 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001384 // for supported (by hwc) displays we provide our
1385 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001386 dispSurface = new FramebufferSurface(*mHwc, state.type,
1387 bqConsumer);
1388 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001389 }
1390
1391 const wp<IBinder>& display(curr.keyAt(i));
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301392 if (dispSurface != NULL && producer != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001393 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001394 state.type, hwcDisplayId,
1395 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001396 display, dispSurface, producer,
1397 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001398 hw->setLayerStack(state.layerStack);
1399 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001400 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001401 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001402 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001403 if (state.isVirtualDisplay()) {
1404 if (hwcDisplayId >= 0) {
1405 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1406 hw->getWidth(), hw->getHeight(),
1407 hw->getFormat());
1408 }
1409 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001410 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001411 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001412 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001413 }
1414 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001416 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001417
Mathias Agopian84300952012-11-21 16:02:13 -08001418 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1419 // The transform hint might have changed for some layers
1420 // (either because a display has changed, or because a layer
1421 // as changed).
1422 //
1423 // Walk through all the layers in currentLayers,
1424 // and update their transform hint.
1425 //
1426 // If a layer is visible only on a single display, then that
1427 // display is used to calculate the hint, otherwise we use the
1428 // default display.
1429 //
1430 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1431 // the hint is set before we acquire a buffer from the surface texture.
1432 //
1433 // NOTE: layer transactions have taken place already, so we use their
1434 // drawing state. However, SurfaceFlinger's own transaction has not
1435 // happened yet, so we must use the current state layer list
1436 // (soon to become the drawing state list).
1437 //
1438 sp<const DisplayDevice> disp;
1439 uint32_t currentlayerStack = 0;
1440 for (size_t i=0; i<count; i++) {
1441 // NOTE: we rely on the fact that layers are sorted by
1442 // layerStack first (so we don't have to traverse the list
1443 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001444 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001445 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001446 if (i==0 || currentlayerStack != layerStack) {
1447 currentlayerStack = layerStack;
1448 // figure out if this layerstack is mirrored
1449 // (more than one display) if so, pick the default display,
1450 // if not, pick the only display it's on.
1451 disp.clear();
1452 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1453 sp<const DisplayDevice> hw(mDisplays[dpy]);
1454 if (hw->getLayerStack() == currentlayerStack) {
1455 if (disp == NULL) {
1456 disp = hw;
1457 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001458 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001459 break;
1460 }
1461 }
1462 }
1463 }
Chet Haase91d25932013-04-11 15:24:55 -07001464 if (disp == NULL) {
1465 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1466 // redraw after transform hint changes. See bug 8508397.
1467
1468 // could be null when this layer is using a layerStack
1469 // that is not visible on any display. Also can occur at
1470 // screen off/on times.
1471 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001472 }
Chet Haase91d25932013-04-11 15:24:55 -07001473 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001474 }
1475 }
1476
1477
Mathias Agopian3559b072012-08-15 13:46:03 -07001478 /*
1479 * Perform our own transaction if needed
1480 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001481
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001482 const LayerVector& layers(mDrawingState.layersSortedByZ);
1483 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001484 // layers have been added
1485 mVisibleRegionsDirty = true;
1486 }
1487
1488 // some layers might have been removed, so
1489 // we need to update the regions they're exposing.
1490 if (mLayersRemoved) {
1491 mLayersRemoved = false;
1492 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001493 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001494 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001495 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001496 if (currentLayers.indexOf(layer) < 0) {
1497 // this layer is not visible anymore
1498 // TODO: we could traverse the tree from front to back and
1499 // compute the actual visible region
1500 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001501 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001502 Region visibleReg = s.transform.transform(
1503 Region(Rect(s.active.w, s.active.h)));
1504 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001505 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001506 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001507 }
1508
1509 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001510
1511 updateCursorAsync();
1512}
1513
1514void SurfaceFlinger::updateCursorAsync()
1515{
1516 HWComposer& hwc(getHwComposer());
1517 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1518 sp<const DisplayDevice> hw(mDisplays[dpy]);
1519 const int32_t id = hw->getHwcDisplayId();
1520 if (id < 0) {
1521 continue;
1522 }
1523 const Vector< sp<Layer> >& currentLayers(
1524 hw->getVisibleLayersSortedByZ());
1525 const size_t count = currentLayers.size();
1526 HWComposer::LayerListIterator cur = hwc.begin(id);
1527 const HWComposer::LayerListIterator end = hwc.end(id);
1528 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1529 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1530 continue;
1531 }
1532 const sp<Layer>& layer(currentLayers[i]);
1533 Rect cursorPos = layer->getPosition(hw);
1534 hwc.setCursorPositionAsync(id, cursorPos);
1535 break;
1536 }
1537 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001538}
1539
1540void SurfaceFlinger::commitTransaction()
1541{
1542 if (!mLayersPendingRemoval.isEmpty()) {
1543 // Notify removed layers now that they can't be drawn from
1544 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1545 mLayersPendingRemoval[i]->onRemoved();
1546 }
1547 mLayersPendingRemoval.clear();
1548 }
1549
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001550 // If this transaction is part of a window animation then the next frame
1551 // we composite should be considered an animation as well.
1552 mAnimCompositionPending = mAnimTransactionPending;
1553
Mathias Agopian4fec8732012-06-29 14:12:52 -07001554 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001555 mTransactionPending = false;
1556 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001557 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001558}
1559
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001560void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001561 const LayerVector& currentLayers, uint32_t layerStack,
1562 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001563{
Mathias Agopian841cde52012-03-01 15:44:37 -08001564 ATRACE_CALL();
1565
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001566 Region aboveOpaqueLayers;
1567 Region aboveCoveredLayers;
1568 Region dirty;
1569
Mathias Agopian87baae12012-07-31 12:38:26 -07001570 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001571
1572 size_t i = currentLayers.size();
1573 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001574 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001575
1576 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001577 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001578
Jesse Hall01e29052013-02-19 16:13:35 -08001579 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001580 if (s.layerStack != layerStack)
1581 continue;
1582
Mathias Agopianab028732010-03-16 16:41:46 -07001583 /*
1584 * opaqueRegion: area of a surface that is fully opaque.
1585 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001586 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001587
1588 /*
1589 * visibleRegion: area of a surface that is visible on screen
1590 * and not fully transparent. This is essentially the layer's
1591 * footprint minus the opaque regions above it.
1592 * Areas covered by a translucent surface are considered visible.
1593 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001594 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001595
1596 /*
1597 * coveredRegion: area of a surface that is covered by all
1598 * visible regions above it (which includes the translucent areas).
1599 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001600 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001601
Jesse Halla8026d22012-09-25 13:25:04 -07001602 /*
1603 * transparentRegion: area of a surface that is hinted to be completely
1604 * transparent. This is only used to tell when the layer has no visible
1605 * non-transparent regions and can be removed from the layer list. It
1606 * does not affect the visibleRegion of this layer or any layers
1607 * beneath it. The hint may not be correct if apps don't respect the
1608 * SurfaceView restrictions (which, sadly, some don't).
1609 */
1610 Region transparentRegion;
1611
Mathias Agopianab028732010-03-16 16:41:46 -07001612
1613 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001614 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001615 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001616 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001617 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001618 if (!visibleRegion.isEmpty()) {
1619 // Remove the transparent area from the visible region
1620 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001621 const Transform tr(s.transform);
1622 if (tr.transformed()) {
1623 if (tr.preserveRects()) {
1624 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001625 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001626 } else {
1627 // transformation too complex, can't do the
1628 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001629 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001630 }
1631 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001632 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001633 }
Mathias Agopianab028732010-03-16 16:41:46 -07001634 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001635
Mathias Agopianab028732010-03-16 16:41:46 -07001636 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001637 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001638 if (s.alpha==255 && !translucent &&
1639 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1640 // the opaque region is the layer's footprint
1641 opaqueRegion = visibleRegion;
1642 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001643 }
1644 }
1645
Mathias Agopianab028732010-03-16 16:41:46 -07001646 // Clip the covered region to the visible region
1647 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1648
1649 // Update aboveCoveredLayers for next (lower) layer
1650 aboveCoveredLayers.orSelf(visibleRegion);
1651
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652 // subtract the opaque region covered by the layers above us
1653 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654
1655 // compute this layer's dirty region
1656 if (layer->contentDirty) {
1657 // we need to invalidate the whole region
1658 dirty = visibleRegion;
1659 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001660 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001661 layer->contentDirty = false;
1662 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001663 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001664 * the exposed region consists of two components:
1665 * 1) what's VISIBLE now and was COVERED before
1666 * 2) what's EXPOSED now less what was EXPOSED before
1667 *
1668 * note that (1) is conservative, we start with the whole
1669 * visible region but only keep what used to be covered by
1670 * something -- which mean it may have been exposed.
1671 *
1672 * (2) handles areas that were not covered by anything but got
1673 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001674 */
Mathias Agopianab028732010-03-16 16:41:46 -07001675 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001676 const Region oldVisibleRegion = layer->visibleRegion;
1677 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001678 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1679 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001680 }
1681 dirty.subtractSelf(aboveOpaqueLayers);
1682
1683 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001684 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001685
Mathias Agopianab028732010-03-16 16:41:46 -07001686 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001688
Jesse Halla8026d22012-09-25 13:25:04 -07001689 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001690 layer->setVisibleRegion(visibleRegion);
1691 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001692 layer->setVisibleNonTransparentRegion(
1693 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001694 }
1695
Mathias Agopian87baae12012-07-31 12:38:26 -07001696 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001697}
1698
Mathias Agopian87baae12012-07-31 12:38:26 -07001699void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1700 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001701 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001702 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1703 if (hw->getLayerStack() == layerStack) {
1704 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001705 }
1706 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001707}
1708
1709void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001710{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001711 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001712
Mathias Agopian4fec8732012-06-29 14:12:52 -07001713 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001714 const LayerVector& layers(mDrawingState.layersSortedByZ);
Eric Penner51c59cd2014-07-28 19:51:58 -07001715
1716 // Store the set of layers that need updates. This set must not change as
1717 // buffers are being latched, as this could result in a deadlock.
1718 // Example: Two producers share the same command stream and:
1719 // 1.) Layer 0 is latched
1720 // 2.) Layer 0 gets a new frame
1721 // 2.) Layer 1 gets a new frame
1722 // 3.) Layer 1 is latched.
1723 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1724 // second frame. But layer 0's second frame could be waiting on display.
1725 Vector<Layer*> layersWithQueuedFrames;
1726 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001727 const sp<Layer>& layer(layers[i]);
Eric Penner51c59cd2014-07-28 19:51:58 -07001728 if (layer->hasQueuedFrame())
1729 layersWithQueuedFrames.push_back(layer.get());
1730 }
1731 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
1732 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07001733 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001734 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001735 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001736 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001737
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001738 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001739}
1740
Mathias Agopianad456f92011-01-13 17:53:01 -08001741void SurfaceFlinger::invalidateHwcGeometry()
1742{
1743 mHwWorkListDirty = true;
1744}
1745
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001746
Mathias Agopiancd60f992012-08-16 16:28:27 -07001747void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001748 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001749{
Dan Stoza71433162014-02-04 16:22:36 -08001750 // We only need to actually compose the display if:
1751 // 1) It is being handled by hardware composer, which may need this to
1752 // keep its virtual display state machine in sync, or
1753 // 2) There is work to be done (the dirty region isn't empty)
1754 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1755 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1756 return;
1757 }
1758
Mathias Agopian87baae12012-07-31 12:38:26 -07001759 Region dirtyRegion(inDirtyRegion);
1760
Mathias Agopianb8a55602009-06-26 19:06:36 -07001761 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001762 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001763
Mathias Agopian42977342012-08-05 00:40:46 -07001764 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001765 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001766 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1767 // takes a rectangle, we must make sure to update that whole
1768 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001769 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001770 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001771 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001772 // We need to redraw the rectangle that will be updated
1773 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001774 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001775 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001776 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001777 } else {
1778 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001779 dirtyRegion.set(hw->bounds());
1780 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001781 }
1782 }
1783
Alan Viverette9c5a3332013-09-12 20:04:35 -07001784 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Michael Lentine3f121fc2014-10-01 11:17:28 -07001785 if (!doComposeSurfaces(hw, dirtyRegion)) return;
Mathias Agopianff2ed702013-09-01 21:36:12 -07001786 } else {
1787 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001788 mat4 colorMatrix = mColorMatrix;
1789 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001790 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001791 }
1792 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001793 doComposeSurfaces(hw, dirtyRegion);
1794 engine.endGroup();
1795 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001796
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001797 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001798 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001799
1800 // swap buffers (presentation)
1801 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001802}
1803
Michael Lentine3f121fc2014-10-01 11:17:28 -07001804bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001805{
Mathias Agopian3f844832013-08-07 21:24:32 -07001806 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001807 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001808 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001809 HWComposer::LayerListIterator cur = hwc.begin(id);
1810 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001811
Jesse Halld05a17f2013-09-30 16:49:30 -07001812 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001813 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001814 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001815 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1816 hw->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07001817 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1818 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
1819 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
1820 }
1821 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08001822 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001823
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001824 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001825 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001826 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001827 // when using overlays, we assume a fully transparent framebuffer
1828 // NOTE: we could reduce how much we need to clear, for instance
1829 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001830 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001831 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001832 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001833 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001834 // we start with the whole screen area
1835 const Region bounds(hw->getBounds());
1836
1837 // we remove the scissor part
1838 // we're left with the letterbox region
1839 // (common case is that letterbox ends-up being empty)
1840 const Region letterbox(bounds.subtract(hw->getScissor()));
1841
1842 // compute the area to clear
1843 Region region(hw->undefinedRegion.merge(letterbox));
1844
1845 // but limit it to the dirty region
1846 region.andSelf(dirty);
1847
Mathias Agopianb9494d52012-04-18 02:28:45 -07001848 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001849 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001850 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001851 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001852 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001853 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001854
Mathias Agopian766dc492012-10-30 18:08:06 -07001855 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1856 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001857 // scissor on the main display. It should never be needed
1858 // anyways (though in theory it could since the API allows it).
1859 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001860 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001861 if (scissor != bounds) {
1862 // scissor doesn't match the screen's dimensions, so we
1863 // need to clear everything outside of it and enable
1864 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001865
Mathias Agopianf45c5102012-10-24 16:29:17 -07001866 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001867 const uint32_t height = hw->getHeight();
1868 engine.setScissor(scissor.left, height - scissor.bottom,
1869 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001870 }
1871 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001872 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001873
Mathias Agopian85d751c2012-08-29 16:59:24 -07001874 /*
1875 * and then, render the layers targeted at the framebuffer
1876 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001877
Mathias Agopian13127d82013-03-05 17:47:11 -08001878 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001879 const size_t count = layers.size();
1880 const Transform& tr = hw->getTransform();
1881 if (cur != end) {
1882 // we're using h/w composer
1883 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001884 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001885 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001886 if (!clip.isEmpty()) {
1887 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07001888 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07001889 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001890 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001891 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1892 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001893 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001894 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001895 // never clear the very first layer since we're
1896 // guaranteed the FB is already cleared
1897 layer->clearWithOpenGL(hw, clip);
1898 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001899 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001900 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001901 case HWC_FRAMEBUFFER: {
1902 layer->draw(hw, clip);
1903 break;
1904 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001905 case HWC_FRAMEBUFFER_TARGET: {
1906 // this should not happen as the iterator shouldn't
1907 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001908 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001909 break;
1910 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001911 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001912 }
1913 layer->setAcquireFence(hw, *cur);
1914 }
1915 } else {
1916 // we're not using h/w composer
1917 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001918 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001919 const Region clip(dirty.intersect(
1920 tr.transform(layer->visibleRegion)));
1921 if (!clip.isEmpty()) {
1922 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001923 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001924 }
1925 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001926
1927 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001928 engine.disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07001929 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001930}
1931
Mathias Agopian3f844832013-08-07 21:24:32 -07001932void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001933 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001934 RenderEngine& engine(getRenderEngine());
1935 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001936}
1937
Mathias Agopianac9fa422013-02-11 16:40:36 -08001938void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1939 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001940 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001941 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001942{
Mathias Agopian96f08192010-06-02 23:28:45 -07001943 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001944 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001945
Mathias Agopian96f08192010-06-02 23:28:45 -07001946 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001947 Mutex::Autolock _l(mStateLock);
1948 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001949 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001950}
1951
Mathias Agopian3f844832013-08-07 21:24:32 -07001952status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001953 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001954 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001955 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001956 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001957 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001958 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001959 return NO_ERROR;
1960 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001961 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001962}
1963
Dan Stozac7014012014-02-14 15:03:43 -08001964uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001965 return android_atomic_release_load(&mTransactionFlags);
1966}
1967
Mathias Agopian3f844832013-08-07 21:24:32 -07001968uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001969 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1970}
1971
Mathias Agopian3f844832013-08-07 21:24:32 -07001972uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001973 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1974 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001975 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001976 }
1977 return old;
1978}
1979
Mathias Agopian8b33f032012-07-24 20:43:54 -07001980void SurfaceFlinger::setTransactionState(
1981 const Vector<ComposerState>& state,
1982 const Vector<DisplayState>& displays,
1983 uint32_t flags)
1984{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001985 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001986 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001987 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001988
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001989 if (flags & eAnimation) {
1990 // For window updates that are part of an animation we must wait for
1991 // previous animation "frames" to be handled.
1992 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001993 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001994 if (CC_UNLIKELY(err != NO_ERROR)) {
1995 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001996 // caller after a few seconds.
1997 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1998 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001999 mAnimTransactionPending = false;
2000 break;
2001 }
2002 }
2003 }
2004
Mathias Agopiane57f2922012-08-09 16:29:12 -07002005 size_t count = displays.size();
2006 for (size_t i=0 ; i<count ; i++) {
2007 const DisplayState& s(displays[i]);
2008 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002009 }
2010
Mathias Agopiane57f2922012-08-09 16:29:12 -07002011 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002012 for (size_t i=0 ; i<count ; i++) {
2013 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002014 // Here we need to check that the interface we're given is indeed
2015 // one of our own. A malicious client could give us a NULL
2016 // IInterface, or one of its own or even one of our own but a
2017 // different type. All these situations would cause us to crash.
2018 //
2019 // NOTE: it would be better to use RTTI as we could directly check
2020 // that we have a Client*. however, RTTI is disabled in Android.
2021 if (s.client != NULL) {
2022 sp<IBinder> binder = s.client->asBinder();
2023 if (binder != NULL) {
2024 String16 desc(binder->getInterfaceDescriptor());
2025 if (desc == ISurfaceComposerClient::descriptor) {
2026 sp<Client> client( static_cast<Client *>(s.client.get()) );
2027 transactionFlags |= setClientStateLocked(client, s.state);
2028 }
2029 }
2030 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002031 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002032
Mathias Agopian386aa982011-11-07 21:58:03 -08002033 if (transactionFlags) {
2034 // this triggers the transaction
2035 setTransactionFlags(transactionFlags);
2036
2037 // if this is a synchronous transaction, wait for it to take effect
2038 // before returning.
2039 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002040 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002041 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002042 if (flags & eAnimation) {
2043 mAnimTransactionPending = true;
2044 }
2045 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002046 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2047 if (CC_UNLIKELY(err != NO_ERROR)) {
2048 // just in case something goes wrong in SF, return to the
2049 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002050 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2051 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002052 break;
2053 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002054 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002055 }
2056}
2057
Mathias Agopiane57f2922012-08-09 16:29:12 -07002058uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2059{
Jesse Hall9a143922012-10-04 16:29:19 -07002060 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2061 if (dpyIdx < 0)
2062 return 0;
2063
Mathias Agopiane57f2922012-08-09 16:29:12 -07002064 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002065 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002066 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002067 const uint32_t what = s.what;
2068 if (what & DisplayState::eSurfaceChanged) {
2069 if (disp.surface->asBinder() != s.surface->asBinder()) {
2070 disp.surface = s.surface;
2071 flags |= eDisplayTransactionNeeded;
2072 }
2073 }
2074 if (what & DisplayState::eLayerStackChanged) {
2075 if (disp.layerStack != s.layerStack) {
2076 disp.layerStack = s.layerStack;
2077 flags |= eDisplayTransactionNeeded;
2078 }
2079 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002080 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002081 if (disp.orientation != s.orientation) {
2082 disp.orientation = s.orientation;
2083 flags |= eDisplayTransactionNeeded;
2084 }
2085 if (disp.frame != s.frame) {
2086 disp.frame = s.frame;
2087 flags |= eDisplayTransactionNeeded;
2088 }
2089 if (disp.viewport != s.viewport) {
2090 disp.viewport = s.viewport;
2091 flags |= eDisplayTransactionNeeded;
2092 }
2093 }
Michael Lentine47e45402014-07-18 15:34:25 -07002094 if (what & DisplayState::eDisplaySizeChanged) {
2095 if (disp.width != s.width) {
2096 disp.width = s.width;
2097 flags |= eDisplayTransactionNeeded;
2098 }
2099 if (disp.height != s.height) {
2100 disp.height = s.height;
2101 flags |= eDisplayTransactionNeeded;
2102 }
2103 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002104 }
2105 return flags;
2106}
2107
2108uint32_t SurfaceFlinger::setClientStateLocked(
2109 const sp<Client>& client,
2110 const layer_state_t& s)
2111{
2112 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002113 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002114 if (layer != 0) {
2115 const uint32_t what = s.what;
2116 if (what & layer_state_t::ePositionChanged) {
2117 if (layer->setPosition(s.x, s.y))
2118 flags |= eTraversalNeeded;
2119 }
2120 if (what & layer_state_t::eLayerChanged) {
2121 // NOTE: index needs to be calculated before we update the state
2122 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2123 if (layer->setLayer(s.z)) {
2124 mCurrentState.layersSortedByZ.removeAt(idx);
2125 mCurrentState.layersSortedByZ.add(layer);
2126 // we need traversal (state changed)
2127 // AND transaction (list changed)
2128 flags |= eTransactionNeeded|eTraversalNeeded;
2129 }
2130 }
2131 if (what & layer_state_t::eSizeChanged) {
2132 if (layer->setSize(s.w, s.h)) {
2133 flags |= eTraversalNeeded;
2134 }
2135 }
2136 if (what & layer_state_t::eAlphaChanged) {
2137 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2138 flags |= eTraversalNeeded;
2139 }
2140 if (what & layer_state_t::eMatrixChanged) {
2141 if (layer->setMatrix(s.matrix))
2142 flags |= eTraversalNeeded;
2143 }
2144 if (what & layer_state_t::eTransparentRegionChanged) {
2145 if (layer->setTransparentRegionHint(s.transparentRegion))
2146 flags |= eTraversalNeeded;
2147 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08002148 if ((what & layer_state_t::eVisibilityChanged) ||
2149 (what & layer_state_t::eOpacityChanged)) {
2150 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07002151 if (layer->setFlags(s.flags, s.mask))
2152 flags |= eTraversalNeeded;
2153 }
2154 if (what & layer_state_t::eCropChanged) {
2155 if (layer->setCrop(s.crop))
2156 flags |= eTraversalNeeded;
2157 }
2158 if (what & layer_state_t::eLayerStackChanged) {
2159 // NOTE: index needs to be calculated before we update the state
2160 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2161 if (layer->setLayerStack(s.layerStack)) {
2162 mCurrentState.layersSortedByZ.removeAt(idx);
2163 mCurrentState.layersSortedByZ.add(layer);
2164 // we need traversal (state changed)
2165 // AND transaction (list changed)
2166 flags |= eTransactionNeeded|eTraversalNeeded;
2167 }
2168 }
2169 }
2170 return flags;
2171}
2172
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002173status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002174 const String8& name,
2175 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002176 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2177 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002178{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002179 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002180 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002181 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002182 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002183 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002184 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002185
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002186 status_t result = NO_ERROR;
2187
2188 sp<Layer> layer;
2189
Mathias Agopian3165cc22012-08-08 19:42:09 -07002190 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2191 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002192 result = createNormalLayer(client,
2193 name, w, h, flags, format,
2194 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002195 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002196 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002197 result = createDimLayer(client,
2198 name, w, h, flags,
2199 handle, gbp, &layer);
2200 break;
2201 default:
2202 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002203 break;
2204 }
2205
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002206 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002207 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002208 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002209 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002210 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002211}
2212
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002213status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2214 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2215 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002216{
2217 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002218 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002219 case PIXEL_FORMAT_TRANSPARENT:
2220 case PIXEL_FORMAT_TRANSLUCENT:
2221 format = PIXEL_FORMAT_RGBA_8888;
2222 break;
2223 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002224 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002225 break;
2226 }
2227
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002228 *outLayer = new Layer(this, client, name, w, h, flags);
2229 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2230 if (err == NO_ERROR) {
2231 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002232 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002233 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002234
2235 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2236 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002237}
2238
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002239status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2240 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2241 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002242{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002243 *outLayer = new LayerDim(this, client, name, w, h, flags);
2244 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002245 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002246 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002247}
2248
Mathias Agopianac9fa422013-02-11 16:40:36 -08002249status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002250{
Mathias Agopian67106042013-03-14 19:18:13 -07002251 // called by the window manager when it wants to remove a Layer
2252 status_t err = NO_ERROR;
2253 sp<Layer> l(client->getLayerUser(handle));
2254 if (l != NULL) {
2255 err = removeLayer(l);
2256 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2257 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002258 }
2259 return err;
2260}
2261
Mathias Agopian13127d82013-03-05 17:47:11 -08002262status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002263{
Mathias Agopian67106042013-03-14 19:18:13 -07002264 // called by ~LayerCleaner() when all references to the IBinder (handle)
2265 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002266 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002267 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002268 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002269 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002270 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002271 "error removing layer=%p (%s)", l.get(), strerror(-err));
2272 }
2273 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002274}
2275
Mathias Agopianb60314a2012-04-10 22:09:54 -07002276// ---------------------------------------------------------------------------
2277
Andy McFadden13a082e2012-08-24 10:16:42 -07002278void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002279 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002280 Vector<ComposerState> state;
2281 Vector<DisplayState> displays;
2282 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002283 d.what = DisplayState::eDisplayProjectionChanged |
2284 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002285 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002286 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002287 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002288 d.frame.makeInvalid();
2289 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002290 d.width = 0;
2291 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002292 displays.add(d);
2293 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002294 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002295
2296 const nsecs_t period =
2297 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2298 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002299}
2300
2301void SurfaceFlinger::initializeDisplays() {
2302 class MessageScreenInitialized : public MessageBase {
2303 SurfaceFlinger* flinger;
2304 public:
2305 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2306 virtual bool handler() {
2307 flinger->onInitializeDisplays();
2308 return true;
2309 }
2310 };
2311 sp<MessageBase> msg = new MessageScreenInitialized(this);
2312 postMessageAsync(msg); // we may be called from main thread, use async message
2313}
2314
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002315void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2316 int mode) {
2317 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2318 this);
2319 int32_t type = hw->getDisplayType();
2320 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002321
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002322 if (mode == currentMode) {
2323 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002324 return;
2325 }
2326
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002327 hw->setPowerMode(mode);
2328 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2329 ALOGW("Trying to set power mode for virtual display");
2330 return;
2331 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002332
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002333 if (currentMode == HWC_POWER_MODE_OFF) {
2334 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002335 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2336 // FIXME: eventthread only knows about the main display right now
2337 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002338 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002339 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002340
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002341 mVisibleRegionsDirty = true;
2342 repaintEverything();
2343 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002344 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002345 disableHardwareVsync(true); // also cancels any in-progress resync
2346
Mathias Agopiancde87a32012-09-13 14:09:01 -07002347 // FIXME: eventthread only knows about the main display right now
2348 mEventThread->onScreenReleased();
2349 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002350
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002351 getHwComposer().setPowerMode(type, mode);
2352 mVisibleRegionsDirty = true;
2353 // from this point on, SF will stop drawing on this display
2354 } else {
2355 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002356 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002357}
2358
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002359void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2360 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002361 SurfaceFlinger& mFlinger;
2362 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002363 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002364 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002365 MessageSetPowerMode(SurfaceFlinger& flinger,
2366 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2367 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002368 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002369 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002370 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002371 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002372 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002373 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002374 ALOGW("Attempt to set power mode = %d for virtual display",
2375 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002376 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002377 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002378 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002379 return true;
2380 }
2381 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002382 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002383 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002384}
2385
2386// ---------------------------------------------------------------------------
2387
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002388status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2389{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002390 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002391
Mathias Agopianbd115332013-04-18 16:41:04 -07002392 IPCThreadState* ipc = IPCThreadState::self();
2393 const int pid = ipc->getCallingPid();
2394 const int uid = ipc->getCallingUid();
2395 if ((uid != AID_SHELL) &&
2396 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002397 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002398 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002399 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002400 // Try to get the main lock, but don't insist if we can't
2401 // (this would indicate SF is stuck, but we want to be able to
2402 // print something in dumpsys).
2403 int retry = 3;
2404 while (mStateLock.tryLock()<0 && --retry>=0) {
2405 usleep(1000000);
2406 }
2407 const bool locked(retry >= 0);
2408 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002409 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002410 "SurfaceFlinger appears to be unresponsive, "
2411 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002412 }
2413
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002414 bool dumpAll = true;
2415 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002416 size_t numArgs = args.size();
2417 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002418 if ((index < numArgs) &&
2419 (args[index] == String16("--list"))) {
2420 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002421 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002422 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002423 }
2424
2425 if ((index < numArgs) &&
2426 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002427 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002428 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002429 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002430 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002431
2432 if ((index < numArgs) &&
2433 (args[index] == String16("--latency-clear"))) {
2434 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002435 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002436 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002437 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002438
2439 if ((index < numArgs) &&
2440 (args[index] == String16("--dispsync"))) {
2441 index++;
2442 mPrimaryDispSync.dump(result);
2443 dumpAll = false;
2444 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002445 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002446
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002447 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002448 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002449 }
2450
Mathias Agopian9795c422009-08-26 16:36:26 -07002451 if (locked) {
2452 mStateLock.unlock();
2453 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002454 }
2455 write(fd, result.string(), result.size());
2456 return NO_ERROR;
2457}
2458
Dan Stozac7014012014-02-14 15:03:43 -08002459void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2460 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002461{
2462 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2463 const size_t count = currentLayers.size();
2464 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002465 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002466 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002467 }
2468}
2469
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002470void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002471 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002472{
2473 String8 name;
2474 if (index < args.size()) {
2475 name = String8(args[index]);
2476 index++;
2477 }
2478
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002479 const nsecs_t period =
2480 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002481 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002482
2483 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002484 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002485 } else {
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]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002490 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002491 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002492 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002493 }
2494 }
2495}
2496
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002497void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002498 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002499{
2500 String8 name;
2501 if (index < args.size()) {
2502 name = String8(args[index]);
2503 index++;
2504 }
2505
2506 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2507 const size_t count = currentLayers.size();
2508 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002509 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002510 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002511 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002512 }
2513 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002514
Svetoslavd85084b2014-03-20 10:28:31 -07002515 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002516}
2517
Jamie Gennis6547ff42013-07-16 20:12:42 -07002518// This should only be called from the main thread. Otherwise it would need
2519// the lock and should use mCurrentState rather than mDrawingState.
2520void SurfaceFlinger::logFrameStats() {
2521 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2522 const size_t count = drawingLayers.size();
2523 for (size_t i=0 ; i<count ; i++) {
2524 const sp<Layer>& layer(drawingLayers[i]);
2525 layer->logFrameStats();
2526 }
2527
2528 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2529}
2530
Andy McFadden4803b742012-09-24 19:07:20 -07002531/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2532{
2533 static const char* config =
2534 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002535#ifdef HAS_CONTEXT_PRIORITY
2536 " HAS_CONTEXT_PRIORITY"
2537#endif
2538#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2539 " NEVER_DEFAULT_TO_ASYNC_MODE"
2540#endif
2541#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2542 " TARGET_DISABLE_TRIPLE_BUFFERING"
2543#endif
2544 "]";
2545 result.append(config);
2546}
2547
Mathias Agopian74d211a2013-04-22 16:55:35 +02002548void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2549 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002550{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002551 bool colorize = false;
2552 if (index < args.size()
2553 && (args[index] == String16("--color"))) {
2554 colorize = true;
2555 index++;
2556 }
2557
2558 Colorizer colorizer(colorize);
2559
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002560 // figure out if we're stuck somewhere
2561 const nsecs_t now = systemTime();
2562 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2563 const nsecs_t inTransaction(mDebugInTransaction);
2564 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2565 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2566
2567 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002568 * Dump library configuration.
2569 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002570
2571 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002572 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002573 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002574 appendSfConfigString(result);
2575 appendUiConfigString(result);
2576 appendGuiConfigString(result);
2577 result.append("\n");
2578
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002579 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002580 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002581 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002582 result.append(SyncFeatures::getInstance().toString());
2583 result.append("\n");
2584
Andy McFadden41d67d72014-04-25 16:58:34 -07002585 colorizer.bold(result);
2586 result.append("DispSync configuration: ");
2587 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002588 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2589 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002590 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2591 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2592 result.append("\n");
2593
Andy McFadden4803b742012-09-24 19:07:20 -07002594 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002595 * Dump the visible layer list
2596 */
2597 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2598 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002599 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002600 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002601 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002602 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002603 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002604 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002605 }
2606
2607 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002608 * Dump Display state
2609 */
2610
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002611 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002612 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002613 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002614 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2615 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002616 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002617 }
2618
2619 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002620 * Dump SurfaceFlinger global state
2621 */
2622
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002623 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002624 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002625 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002626
Mathias Agopian888c8222012-08-04 21:10:38 -07002627 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002628 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002629
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002630 colorizer.bold(result);
2631 result.appendFormat("EGL implementation : %s\n",
2632 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2633 colorizer.reset(result);
2634 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002635 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002636
Mathias Agopian875d8e12013-06-07 15:35:48 -07002637 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002638
Mathias Agopian42977342012-08-05 00:40:46 -07002639 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002640 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2641 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002642 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002643 " last eglSwapBuffers() time: %f us\n"
2644 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002645 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002646 " refresh-rate : %f fps\n"
2647 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002648 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002649 " gpu_to_cpu_unsupported : %d\n"
2650 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002651 mLastSwapBufferTime/1000.0,
2652 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002653 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002654 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2655 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002656 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002657 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002658
Mathias Agopian74d211a2013-04-22 16:55:35 +02002659 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002660 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002661
Mathias Agopian74d211a2013-04-22 16:55:35 +02002662 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002663 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002664
2665 /*
2666 * VSYNC state
2667 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002668 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002669
2670 /*
2671 * Dump HWComposer state
2672 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002673 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002674 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002675 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002676 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002677 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002678 (mDebugDisableHWC || mDebugRegion || mDaltonize
2679 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002680 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002681
2682 /*
2683 * Dump gralloc state
2684 */
2685 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2686 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002687}
2688
Mathias Agopian13127d82013-03-05 17:47:11 -08002689const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002690SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002691 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002692 wp<IBinder> dpy;
2693 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2694 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2695 dpy = mDisplays.keyAt(i);
2696 break;
2697 }
2698 }
2699 if (dpy == NULL) {
2700 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2701 // Just use the primary display so we have something to return
2702 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2703 }
2704 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002705}
2706
Keun young Park63f165f2012-08-31 10:53:36 -07002707bool SurfaceFlinger::startDdmConnection()
2708{
2709 void* libddmconnection_dso =
2710 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2711 if (!libddmconnection_dso) {
2712 return false;
2713 }
2714 void (*DdmConnection_start)(const char* name);
2715 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07002716 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07002717 if (!DdmConnection_start) {
2718 dlclose(libddmconnection_dso);
2719 return false;
2720 }
2721 (*DdmConnection_start)(getServiceName());
2722 return true;
2723}
2724
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002725status_t SurfaceFlinger::onTransact(
2726 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2727{
2728 switch (code) {
2729 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002730 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002731 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002732 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002733 case CLEAR_ANIMATION_FRAME_STATS:
2734 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002735 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002736 {
2737 // codes that require permission check
2738 IPCThreadState* ipc = IPCThreadState::self();
2739 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002740 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002741 if ((uid != AID_GRAPHICS) &&
2742 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002743 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002744 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2745 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002746 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002747 break;
2748 }
2749 case CAPTURE_SCREEN:
2750 {
2751 // codes that require permission check
2752 IPCThreadState* ipc = IPCThreadState::self();
2753 const int pid = ipc->getCallingPid();
2754 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002755 if ((uid != AID_GRAPHICS) &&
2756 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002757 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002758 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2759 return PERMISSION_DENIED;
2760 }
2761 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002762 }
2763 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002764
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002765 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2766 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002767 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002768 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002769 IPCThreadState* ipc = IPCThreadState::self();
2770 const int pid = ipc->getCallingPid();
2771 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002772 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002773 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002774 return PERMISSION_DENIED;
2775 }
2776 int n;
2777 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002778 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002779 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002780 return NO_ERROR;
2781 case 1002: // SHOW_UPDATES
2782 n = data.readInt32();
2783 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002784 invalidateHwcGeometry();
2785 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002786 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002787 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002788 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002789 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002790 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002791 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002792 setTransactionFlags(
2793 eTransactionNeeded|
2794 eDisplayTransactionNeeded|
2795 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002796 return NO_ERROR;
2797 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002798 case 1006:{ // send empty update
2799 signalRefresh();
2800 return NO_ERROR;
2801 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002802 case 1008: // toggle use of hw composer
2803 n = data.readInt32();
2804 mDebugDisableHWC = n ? 1 : 0;
2805 invalidateHwcGeometry();
2806 repaintEverything();
2807 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002808 case 1009: // toggle use of transform hint
2809 n = data.readInt32();
2810 mDebugDisableTransformHint = n ? 1 : 0;
2811 invalidateHwcGeometry();
2812 repaintEverything();
2813 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002814 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002815 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002816 reply->writeInt32(0);
2817 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002818 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002819 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002820 return NO_ERROR;
2821 case 1013: {
2822 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002823 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2824 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002825 return NO_ERROR;
2826 }
2827 case 1014: {
2828 // daltonize
2829 n = data.readInt32();
2830 switch (n % 10) {
2831 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2832 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2833 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2834 }
2835 if (n >= 10) {
2836 mDaltonizer.setMode(Daltonizer::correction);
2837 } else {
2838 mDaltonizer.setMode(Daltonizer::simulation);
2839 }
2840 mDaltonize = n > 0;
2841 invalidateHwcGeometry();
2842 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002843 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002844 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002845 case 1015: {
2846 // apply a color matrix
2847 n = data.readInt32();
2848 mHasColorMatrix = n ? 1 : 0;
2849 if (n) {
2850 // color matrix is sent as mat3 matrix followed by vec3
2851 // offset, then packed into a mat4 where the last row is
2852 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002853 for (size_t i = 0 ; i < 4; i++) {
2854 for (size_t j = 0; j < 4; j++) {
2855 mColorMatrix[i][j] = data.readFloat();
2856 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002857 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002858 } else {
2859 mColorMatrix = mat4();
2860 }
2861 invalidateHwcGeometry();
2862 repaintEverything();
2863 return NO_ERROR;
2864 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002865 // This is an experimental interface
2866 // Needs to be shifted to proper binder interface when we productize
2867 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07002868 n = data.readInt32();
2869 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002870 return NO_ERROR;
2871 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002872 }
2873 }
2874 return err;
2875}
2876
Mathias Agopian53331da2011-08-22 21:44:41 -07002877void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002878 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002879 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002880}
2881
Mathias Agopian59119e62010-10-11 12:37:43 -07002882// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002883// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002884// ---------------------------------------------------------------------------
2885
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002886/* The code below is here to handle b/8734824
2887 *
2888 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07002889 * from the surfaceflinger thread to the calling binder thread, where they
2890 * are executed. This allows the calling thread in the calling process to be
2891 * reused and not depend on having "enough" binder threads to handle the
2892 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002893 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002894class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07002895 /* Parts of GraphicProducerWrapper are run on two different threads,
2896 * communicating by sending messages via Looper but also by shared member
2897 * data. Coherence maintenance is subtle and in places implicit (ugh).
2898 *
2899 * Don't rely on Looper's sendMessage/handleMessage providing
2900 * release/acquire semantics for any data not actually in the Message.
2901 * Data going from surfaceflinger to binder threads needs to be
2902 * synchronized explicitly.
2903 *
2904 * Barrier open/wait do provide release/acquire semantics. This provides
2905 * implicit synchronization for data coming back from binder to
2906 * surfaceflinger threads.
2907 */
2908
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002909 sp<IGraphicBufferProducer> impl;
2910 sp<Looper> looper;
2911 status_t result;
2912 bool exitPending;
2913 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07002914 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002915 uint32_t code;
2916 Parcel const* data;
2917 Parcel* reply;
2918
2919 enum {
2920 MSG_API_CALL,
2921 MSG_EXIT
2922 };
2923
2924 /*
Jesse Hallb154c422014-07-13 12:47:02 -07002925 * Called on surfaceflinger thread. This is called by our "fake"
2926 * BpGraphicBufferProducer. We package the data and reply Parcel and
2927 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002928 */
2929 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08002930 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002931 this->code = code;
2932 this->data = &data;
2933 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002934 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07002935 // if we've exited, we run the message synchronously right here.
2936 // note (JH): as far as I can tell from looking at the code, this
2937 // never actually happens. if it does, i'm not sure if it happens
2938 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002939 handleMessage(Message(MSG_API_CALL));
2940 } else {
2941 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07002942 // Prevent stores to this->{code, data, reply} from being
2943 // reordered later than the construction of Message.
2944 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002945 looper->sendMessage(this, Message(MSG_API_CALL));
2946 barrier.wait();
2947 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08002948 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002949 }
2950
2951 /*
Jesse Hallb154c422014-07-13 12:47:02 -07002952 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002953 * call the real BpGraphicBufferProducer.
2954 */
2955 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07002956 int what = message.what;
2957 // Prevent reads below from happening before the read from Message
2958 atomic_thread_fence(memory_order_acquire);
2959 if (what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08002960 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002961 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07002962 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002963 exitRequested = true;
2964 }
2965 }
2966
2967public:
Jesse Hallb154c422014-07-13 12:47:02 -07002968 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
2969 : impl(impl),
2970 looper(new Looper(true)),
2971 exitPending(false),
2972 exitRequested(false)
2973 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002974
Jesse Hallb154c422014-07-13 12:47:02 -07002975 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002976 status_t waitForResponse() {
2977 do {
2978 looper->pollOnce(-1);
2979 } while (!exitRequested);
2980 return result;
2981 }
2982
Jesse Hallb154c422014-07-13 12:47:02 -07002983 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002984 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002985 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002986 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07002987 // Ensure this->result is visible to the binder thread before it
2988 // handles the message.
2989 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002990 looper->sendMessage(this, Message(MSG_EXIT));
2991 }
2992};
2993
2994
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002995status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2996 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002997 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002998 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07002999 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003000
3001 if (CC_UNLIKELY(display == 0))
3002 return BAD_VALUE;
3003
3004 if (CC_UNLIKELY(producer == 0))
3005 return BAD_VALUE;
3006
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003007 // if we have secure windows on this display, never allow the screen capture
3008 // unless the producer interface is local (i.e.: we can take a screenshot for
3009 // ourselves).
3010 if (!producer->asBinder()->localBinder()) {
3011 Mutex::Autolock _l(mStateLock);
3012 sp<const DisplayDevice> hw(getDisplayDevice(display));
3013 if (hw->getSecureLayerVisible()) {
3014 ALOGW("FB is protected: PERMISSION_DENIED");
3015 return PERMISSION_DENIED;
3016 }
3017 }
3018
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003019 // Convert to surfaceflinger's internal rotation type.
3020 Transform::orientation_flags rotationFlags;
3021 switch (rotation) {
3022 case ISurfaceComposer::eRotateNone:
3023 rotationFlags = Transform::ROT_0;
3024 break;
3025 case ISurfaceComposer::eRotate90:
3026 rotationFlags = Transform::ROT_90;
3027 break;
3028 case ISurfaceComposer::eRotate180:
3029 rotationFlags = Transform::ROT_180;
3030 break;
3031 case ISurfaceComposer::eRotate270:
3032 rotationFlags = Transform::ROT_270;
3033 break;
3034 default:
3035 rotationFlags = Transform::ROT_0;
3036 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3037 break;
3038 }
3039
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003040 class MessageCaptureScreen : public MessageBase {
3041 SurfaceFlinger* flinger;
3042 sp<IBinder> display;
3043 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003044 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003045 uint32_t reqWidth, reqHeight;
3046 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003047 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003048 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003049 status_t result;
3050 public:
3051 MessageCaptureScreen(SurfaceFlinger* flinger,
3052 const sp<IBinder>& display,
3053 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003054 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003055 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003056 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003057 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003058 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003059 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003060 useIdentityTransform(useIdentityTransform),
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003061 rotation(rotation),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003062 result(PERMISSION_DENIED)
3063 {
3064 }
3065 status_t getResult() const {
3066 return result;
3067 }
3068 virtual bool handler() {
3069 Mutex::Autolock _l(flinger->mStateLock);
3070 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003071 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003072 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003073 useIdentityTransform, rotation);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003074 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003075 return true;
3076 }
3077 };
3078
Mathias Agopian9eb1f052013-04-10 16:27:17 -07003079 // make sure to process transactions before screenshots -- a transaction
3080 // might already be pending but scheduled for VSYNC; this guarantees we
3081 // will handle it before the screenshot. When VSYNC finally arrives
3082 // the scheduled transaction will be a no-op. If no transactions are
3083 // scheduled at this time, this will end-up being a no-op as well.
3084 mEventQueue.invalidateTransactionNow();
3085
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003086 // this creates a "fake" BBinder which will serve as a "fake" remote
3087 // binder to receive the marshaled calls and forward them to the
3088 // real remote (a BpGraphicBufferProducer)
3089 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3090
3091 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3092 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003093 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003094 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003095 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003096 useIdentityTransform, rotationFlags);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003097
3098 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003099 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003100 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003101 }
3102 return res;
3103}
3104
Mathias Agopian180f10d2013-04-10 22:55:41 -07003105
3106void SurfaceFlinger::renderScreenImplLocked(
3107 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003108 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003109 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003110 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003111{
3112 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003113 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003114
3115 // get screen geometry
3116 const uint32_t hw_w = hw->getWidth();
3117 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003118 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
3119
Dan Stozac1879002014-05-22 15:59:05 -07003120 // if a default or invalid sourceCrop is passed in, set reasonable values
3121 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3122 !sourceCrop.isValid()) {
3123 sourceCrop.setLeftTop(Point(0, 0));
3124 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3125 }
3126
3127 // ensure that sourceCrop is inside screen
3128 if (sourceCrop.left < 0) {
3129 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3130 }
Dan Stozabe31f442014-06-11 11:20:54 -07003131 if (sourceCrop.right > hw_w) {
3132 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003133 }
3134 if (sourceCrop.top < 0) {
3135 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3136 }
Dan Stozabe31f442014-06-11 11:20:54 -07003137 if (sourceCrop.bottom > hw_h) {
3138 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003139 }
3140
Mathias Agopian180f10d2013-04-10 22:55:41 -07003141 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003142 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003143
3144 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003145 engine.setViewportAndProjection(
3146 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003147 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003148
3149 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003150 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003151
3152 const LayerVector& layers( mDrawingState.layersSortedByZ );
3153 const size_t count = layers.size();
3154 for (size_t i=0 ; i<count ; ++i) {
3155 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003156 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003157 if (state.layerStack == hw->getLayerStack()) {
3158 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3159 if (layer->isVisible()) {
3160 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08003161 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003162 if (filtering) layer->setFiltering(false);
3163 }
3164 }
3165 }
3166 }
3167
3168 // compositionComplete is needed for older driver
3169 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003170 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003171}
3172
3173
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003174status_t SurfaceFlinger::captureScreenImplLocked(
3175 const sp<const DisplayDevice>& hw,
3176 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003177 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003178 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003179 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003180{
3181 ATRACE_CALL();
3182
Mathias Agopian180f10d2013-04-10 22:55:41 -07003183 // get screen geometry
3184 const uint32_t hw_w = hw->getWidth();
3185 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003186
Mathias Agopian180f10d2013-04-10 22:55:41 -07003187 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3188 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3189 reqWidth, reqHeight, hw_w, hw_h);
3190 return BAD_VALUE;
3191 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003192
Mathias Agopian180f10d2013-04-10 22:55:41 -07003193 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3194 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3195
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003196 // create a surface (because we're a producer, and we need to
3197 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003198 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003199 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003200
3201 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003202 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003203 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3204 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003205
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003206 int err = 0;
3207 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003208 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003209 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3210 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003211
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003212 if (err == NO_ERROR) {
3213 ANativeWindowBuffer* buffer;
3214 /* TODO: Once we have the sync framework everywhere this can use
3215 * server-side waits on the fence that dequeueBuffer returns.
3216 */
3217 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3218 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003219 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003220 // create an EGLImage from the buffer so we can later
3221 // turn it into a texture
3222 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3223 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3224 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003225 // this binds the given EGLImage as a framebuffer for the
3226 // duration of this scope.
3227 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3228 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003229 // this will in fact render into our dequeued buffer
3230 // via an FBO, which means we didn't have to create
3231 // an EGLSurface and therefore we're not
3232 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003233 renderScreenImplLocked(
3234 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3235 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003236
Riley Andrews86639902014-08-15 12:27:24 -07003237 // Attempt to create a sync khr object that can produce a sync point. If that
3238 // isn't available, create a non-dupable sync object in the fallback path and
3239 // wait on it directly.
3240 EGLSyncKHR sync;
3241 if (!DEBUG_SCREENSHOTS) {
3242 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Andy McFadden2d8d1202013-10-09 16:38:02 -07003243 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003244 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003245 }
Riley Andrews86639902014-08-15 12:27:24 -07003246 if (sync != EGL_NO_SYNC_KHR) {
3247 // get the sync fd
3248 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3249 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3250 ALOGW("captureScreen: failed to dup sync khr object");
3251 syncFd = -1;
3252 }
3253 eglDestroySyncKHR(mEGLDisplay, sync);
3254 } else {
3255 // fallback path
3256 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3257 if (sync != EGL_NO_SYNC_KHR) {
3258 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3259 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3260 EGLint eglErr = eglGetError();
3261 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3262 ALOGW("captureScreen: fence wait timed out");
3263 } else {
3264 ALOGW_IF(eglErr != EGL_SUCCESS,
3265 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3266 }
3267 eglDestroySyncKHR(mEGLDisplay, sync);
3268 } else {
3269 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3270 }
3271 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003272 if (DEBUG_SCREENSHOTS) {
3273 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3274 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3275 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3276 hw, minLayerZ, maxLayerZ);
3277 delete [] pixels;
3278 }
3279
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003280 } else {
3281 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3282 result = INVALID_OPERATION;
3283 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003284 // destroy our image
3285 eglDestroyImageKHR(mEGLDisplay, image);
3286 } else {
3287 result = BAD_VALUE;
3288 }
Jesse Hallafe2b1f2014-10-21 11:09:17 -07003289 // queueBuffer takes ownership of syncFd
Riley Andrews86639902014-08-15 12:27:24 -07003290 window->queueBuffer(window, buffer, syncFd);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003291 }
3292 } else {
3293 result = BAD_VALUE;
3294 }
3295 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3296 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003297
Mathias Agopian74c40c02010-09-29 13:02:36 -07003298 return result;
3299}
3300
Mathias Agopiand5556842013-09-19 17:08:37 -07003301void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3302 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003303 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003304 for (size_t y=0 ; y<h ; y++) {
3305 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3306 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003307 if (p[x] != 0xFF000000) return;
3308 }
3309 }
3310 ALOGE("*** we just took a black screenshot ***\n"
3311 "requested minz=%d, maxz=%d, layerStack=%d",
3312 minLayerZ, maxLayerZ, hw->getLayerStack());
3313 const LayerVector& layers( mDrawingState.layersSortedByZ );
3314 const size_t count = layers.size();
3315 for (size_t i=0 ; i<count ; ++i) {
3316 const sp<Layer>& layer(layers[i]);
3317 const Layer::State& state(layer->getDrawingState());
3318 const bool visible = (state.layerStack == hw->getLayerStack())
3319 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3320 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003321 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003322 visible ? '+' : '-',
3323 i, layer->getName().string(), state.layerStack, state.z,
3324 layer->isVisible(), state.flags, state.alpha);
3325 }
3326 }
3327}
3328
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003329// ---------------------------------------------------------------------------
3330
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003331SurfaceFlinger::LayerVector::LayerVector() {
3332}
3333
3334SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003335 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003336}
3337
3338int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3339 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003340{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003341 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003342 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3343 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003344
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003345 uint32_t ls = l->getCurrentState().layerStack;
3346 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003347 if (ls != rs)
3348 return ls - rs;
3349
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003350 uint32_t lz = l->getCurrentState().z;
3351 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003352 if (lz != rz)
3353 return lz - rz;
3354
3355 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003356}
3357
3358// ---------------------------------------------------------------------------
3359
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003360SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Michael Lentine47e45402014-07-18 15:34:25 -07003361 : type(DisplayDevice::DISPLAY_ID_INVALID), width(0), height(0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003362}
3363
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003364SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Michael Lentine47e45402014-07-18 15:34:25 -07003365 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0), width(0), height(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003366 viewport.makeInvalid();
3367 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003368}
3369
3370// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003371
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003372}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003373
3374
3375#if defined(__gl_h_)
3376#error "don't include gl/gl.h in this file"
3377#endif
3378
3379#if defined(__gl2_h_)
3380#error "don't include gl2/gl2.h in this file"
3381#endif