blob: 43a69dfb9a7bc13f2410c1f5bf13e24697b25f6d [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
Naseer Ahmed7aa0c472014-11-03 14:49:23 -0500510 if (!display.get())
511 return NAME_NOT_FOUND;
512
Jesse Hall692c7232012-11-08 15:41:56 -0800513 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700514 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800515 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700516 type = i;
517 break;
518 }
519 }
520
521 if (type < 0) {
522 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700523 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700524
Mathias Agopian8b736f12012-08-13 17:54:26 -0700525 // TODO: Not sure if display density should handled by SF any longer
526 class Density {
527 static int getDensityFromProperty(char const* propName) {
528 char property[PROPERTY_VALUE_MAX];
529 int density = 0;
530 if (property_get(propName, property, NULL) > 0) {
531 density = atoi(property);
532 }
533 return density;
534 }
535 public:
536 static int getEmuDensity() {
537 return getDensityFromProperty("qemu.sf.lcd_density"); }
538 static int getBuildDensity() {
539 return getDensityFromProperty("ro.sf.lcd_density"); }
540 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700541
Dan Stoza7f7da322014-05-02 15:26:25 -0700542 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700543
Dan Stoza7f7da322014-05-02 15:26:25 -0700544 const Vector<HWComposer::DisplayConfig>& hwConfigs =
545 getHwComposer().getConfigs(type);
546 for (size_t c = 0; c < hwConfigs.size(); ++c) {
547 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
548 DisplayInfo info = DisplayInfo();
549
550 float xdpi = hwConfig.xdpi;
551 float ydpi = hwConfig.ydpi;
552
553 if (type == DisplayDevice::DISPLAY_PRIMARY) {
554 // The density of the device is provided by a build property
555 float density = Density::getBuildDensity() / 160.0f;
556 if (density == 0) {
557 // the build doesn't provide a density -- this is wrong!
558 // use xdpi instead
559 ALOGE("ro.sf.lcd_density must be defined as a build property");
560 density = xdpi / 160.0f;
561 }
562 if (Density::getEmuDensity()) {
563 // if "qemu.sf.lcd_density" is specified, it overrides everything
564 xdpi = ydpi = density = Density::getEmuDensity();
565 density /= 160.0f;
566 }
567 info.density = density;
568
569 // TODO: this needs to go away (currently needed only by webkit)
570 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
571 info.orientation = hw->getOrientation();
572 } else {
573 // TODO: where should this value come from?
574 static const int TV_DENSITY = 213;
575 info.density = TV_DENSITY / 160.0f;
576 info.orientation = 0;
577 }
578
579 info.w = hwConfig.width;
580 info.h = hwConfig.height;
581 info.xdpi = xdpi;
582 info.ydpi = ydpi;
583 info.fps = float(1e9 / hwConfig.refresh);
Andy McFadden91b2ca82014-06-13 14:04:23 -0700584 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
585
586 // This is how far in advance a buffer must be queued for
587 // presentation at a given time. If you want a buffer to appear
588 // on the screen at time N, you must submit the buffer before
589 // (N - presentationDeadline).
590 //
591 // Normally it's one full refresh period (to give SF a chance to
592 // latch the buffer), but this can be reduced by configuring a
593 // DispSync offset. Any additional delays introduced by the hardware
594 // composer or panel must be accounted for here.
595 //
596 // We add an additional 1ms to allow for processing time and
597 // differences between the ideal and actual refresh rate.
598 info.presentationDeadline =
599 hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700600
601 // All non-virtual displays are currently considered secure.
602 info.secure = true;
603
604 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700605 }
606
Dan Stoza7f7da322014-05-02 15:26:25 -0700607 return NO_ERROR;
608}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700609
Dan Stozaf10c46e2014-11-11 10:32:31 -0800610status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& /* display */,
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700611 DisplayStatInfo* stats) {
612 if (stats == NULL) {
613 return BAD_VALUE;
614 }
615
616 // FIXME for now we always return stats for the primary display
617 memset(stats, 0, sizeof(*stats));
618 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
619 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
620 return NO_ERROR;
621}
622
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700623int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
624 return getDisplayDevice(display)->getActiveConfig();
Dan Stoza7f7da322014-05-02 15:26:25 -0700625}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700626
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700627void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
628 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
629 this);
630 int32_t type = hw->getDisplayType();
631 int currentMode = hw->getActiveConfig();
632
633 if (mode == currentMode) {
634 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
635 return;
636 }
637
638 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
639 ALOGW("Trying to set config for virtual display");
640 return;
641 }
642
643 hw->setActiveConfig(mode);
644 getHwComposer().setActiveConfig(type, mode);
645}
646
647status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
648 class MessageSetActiveConfig: public MessageBase {
649 SurfaceFlinger& mFlinger;
650 sp<IBinder> mDisplay;
651 int mMode;
652 public:
653 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
654 int mode) :
655 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
656 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700657 Vector<DisplayInfo> configs;
658 mFlinger.getDisplayConfigs(mDisplay, &configs);
Jesse Hall78442112014-08-07 22:43:06 -0700659 if (mMode < 0 || mMode >= static_cast<int>(configs.size())) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700660 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700661 mMode, configs.size());
662 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700663 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
664 if (hw == NULL) {
665 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700666 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700667 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
668 ALOGW("Attempt to set active config = %d for virtual display",
669 mMode);
670 } else {
671 mFlinger.setActiveConfigInternal(hw, mMode);
672 }
673 return true;
674 }
675 };
676 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
677 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700678 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700679}
680
Svetoslavd85084b2014-03-20 10:28:31 -0700681status_t SurfaceFlinger::clearAnimationFrameStats() {
682 Mutex::Autolock _l(mStateLock);
683 mAnimFrameTracker.clearStats();
684 return NO_ERROR;
685}
686
687status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
688 Mutex::Autolock _l(mStateLock);
689 mAnimFrameTracker.getStats(outStats);
690 return NO_ERROR;
691}
692
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800693// ----------------------------------------------------------------------------
694
695sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800696 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700697}
698
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800699// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800700
701void SurfaceFlinger::waitForEvent() {
702 mEventQueue.waitMessage();
703}
704
705void SurfaceFlinger::signalTransaction() {
706 mEventQueue.invalidate();
707}
708
709void SurfaceFlinger::signalLayerUpdate() {
710 mEventQueue.invalidate();
711}
712
713void SurfaceFlinger::signalRefresh() {
714 mEventQueue.refresh();
715}
716
717status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800718 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800719 return mEventQueue.postMessage(msg, reltime);
720}
721
722status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800723 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800724 status_t res = mEventQueue.postMessage(msg, reltime);
725 if (res == NO_ERROR) {
726 msg->wait();
727 }
728 return res;
729}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800730
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700731void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700732 do {
733 waitForEvent();
734 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800735}
736
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700737void SurfaceFlinger::enableHardwareVsync() {
738 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700739 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700740 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700741 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
742 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700743 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700744 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700745}
746
Jesse Hall948fe0c2013-10-14 12:56:09 -0700747void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700748 Mutex::Autolock _l(mHWVsyncLock);
749
Jesse Hall948fe0c2013-10-14 12:56:09 -0700750 if (makeAvailable) {
751 mHWVsyncAvailable = true;
752 } else if (!mHWVsyncAvailable) {
753 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
754 return;
755 }
756
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700757 const nsecs_t period =
758 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
759
760 mPrimaryDispSync.reset();
761 mPrimaryDispSync.setPeriod(period);
762
763 if (!mPrimaryHWVsyncEnabled) {
764 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700765 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
766 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700767 mPrimaryHWVsyncEnabled = true;
768 }
769}
770
Jesse Hall948fe0c2013-10-14 12:56:09 -0700771void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700772 Mutex::Autolock _l(mHWVsyncLock);
773 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700774 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
775 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700776 mPrimaryDispSync.endResync();
777 mPrimaryHWVsyncEnabled = false;
778 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700779 if (makeUnavailable) {
780 mHWVsyncAvailable = false;
781 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700782}
783
784void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700785 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700786
Jamie Gennisd1700752013-10-14 12:22:52 -0700787 { // Scope for the lock
788 Mutex::Autolock _l(mHWVsyncLock);
789 if (type == 0 && mPrimaryHWVsyncEnabled) {
790 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700791 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700792 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700793
794 if (needsHwVsync) {
795 enableHardwareVsync();
796 } else {
797 disableHardwareVsync(false);
798 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700799}
800
801void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
802 if (mEventThread == NULL) {
803 // This is a temporary workaround for b/7145521. A non-null pointer
804 // does not mean EventThread has finished initializing, so this
805 // is not a correct fix.
806 ALOGW("WARNING: EventThread not started, ignoring hotplug");
807 return;
808 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700809
Jesse Hall9e663de2013-08-16 14:28:37 -0700810 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700811 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800812 if (connected) {
813 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700814 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800815 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
816 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700817 }
818 setTransactionFlags(eDisplayTransactionNeeded);
819
Andy McFadden9e9689c2012-10-10 18:17:51 -0700820 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700821 }
Mathias Agopian86303202012-07-24 22:46:10 -0700822}
823
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700824void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700825 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700826 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700827}
828
Mathias Agopian4fec8732012-06-29 14:12:52 -0700829void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800830 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800831 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700832 case MessageQueue::TRANSACTION:
833 handleMessageTransaction();
834 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700835 case MessageQueue::INVALIDATE:
836 handleMessageTransaction();
837 handleMessageInvalidate();
838 signalRefresh();
839 break;
840 case MessageQueue::REFRESH:
841 handleMessageRefresh();
842 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800843 }
844}
845
Mathias Agopian4fec8732012-06-29 14:12:52 -0700846void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700847 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700848 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700849 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700850 }
851}
852
853void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700854 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700855 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700856}
857
858void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700859 ATRACE_CALL();
860 preComposition();
861 rebuildLayerStacks();
862 setUpHWComposer();
863 doDebugFlashRegions();
864 doComposition();
865 postComposition();
866}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700867
Mathias Agopiancd60f992012-08-16 16:28:27 -0700868void SurfaceFlinger::doDebugFlashRegions()
869{
870 // is debugging enabled
871 if (CC_LIKELY(!mDebugRegion))
872 return;
873
874 const bool repaintEverything = mRepaintEverything;
875 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
876 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700877 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700878 // transform the dirty region into this screen's coordinate space
879 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
880 if (!dirtyRegion.isEmpty()) {
881 // redraw the whole screen
882 doComposeSurfaces(hw, Region(hw->bounds()));
883
884 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700885 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700886 RenderEngine& engine(getRenderEngine());
887 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
888
Mathias Agopiancd60f992012-08-16 16:28:27 -0700889 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700890 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700891 }
892 }
893 }
894
895 postFramebuffer();
896
897 if (mDebugRegion > 1) {
898 usleep(mDebugRegion * 1000);
899 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700900
901 HWComposer& hwc(getHwComposer());
902 if (hwc.initCheck() == NO_ERROR) {
903 status_t err = hwc.prepare();
904 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
905 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700906}
907
908void SurfaceFlinger::preComposition()
909{
910 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700911 const LayerVector& layers(mDrawingState.layersSortedByZ);
912 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700913 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700914 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700915 needExtraInvalidate = true;
916 }
917 }
918 if (needExtraInvalidate) {
919 signalLayerUpdate();
920 }
921}
922
923void SurfaceFlinger::postComposition()
924{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700925 const LayerVector& layers(mDrawingState.layersSortedByZ);
926 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700927 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700928 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700929 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800930
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700931 const HWComposer& hwc = getHwComposer();
932 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
933
934 if (presentFence->isValid()) {
935 if (mPrimaryDispSync.addPresentFence(presentFence)) {
936 enableHardwareVsync();
937 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700938 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700939 }
940 }
941
Andy McFadden5167ec62014-05-22 13:08:43 -0700942 if (kIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700943 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700944 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700945 enableHardwareVsync();
946 }
947 }
948
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800949 if (mAnimCompositionPending) {
950 mAnimCompositionPending = false;
951
Jesse Halla9a1b002013-02-27 16:39:25 -0800952 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800953 mAnimFrameTracker.setActualPresentFence(presentFence);
954 } else {
955 // The HWC doesn't support present fences, so use the refresh
956 // timestamp instead.
957 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
958 mAnimFrameTracker.setActualPresentTime(presentTime);
959 }
960 mAnimFrameTracker.advanceFrame();
961 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700962}
963
964void SurfaceFlinger::rebuildLayerStacks() {
965 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700966 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700967 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700968 mVisibleRegionsDirty = false;
969 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700970
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700971 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700972 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700973 Region opaqueRegion;
974 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800975 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700976 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700977 const Transform& tr(hw->getTransform());
978 const Rect bounds(hw->getBounds());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700979 if (hw->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700980 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700981 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700982
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700983 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700984 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700985 const sp<Layer>& layer(layers[i]);
986 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700987 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700988 Region drawRegion(tr.transform(
989 layer->visibleNonTransparentRegion));
990 drawRegion.andSelf(bounds);
991 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700992 layersSortedByZ.add(layer);
993 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700994 }
995 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700996 }
Mathias Agopian42977342012-08-05 00:40:46 -0700997 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700998 hw->undefinedRegion.set(bounds);
999 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
1000 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001001 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001002 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001003}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001004
Mathias Agopiancd60f992012-08-16 16:28:27 -07001005void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001006 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001007 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1008 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1009 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1010
1011 // If nothing has changed (!dirty), don't recompose.
1012 // If something changed, but we don't currently have any visible layers,
1013 // and didn't when we last did a composition, then skip it this time.
1014 // The second rule does two things:
1015 // - When all layers are removed from a display, we'll emit one black
1016 // frame, then nothing more until we get new layers.
1017 // - When a display is created with a private layer stack, we won't
1018 // emit any black frames until a layer is added to the layer stack.
1019 bool mustRecompose = dirty && !(empty && wasEmpty);
1020
1021 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1022 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1023 mustRecompose ? "doing" : "skipping",
1024 dirty ? "+" : "-",
1025 empty ? "+" : "-",
1026 wasEmpty ? "+" : "-");
1027
Dan Stoza71433162014-02-04 16:22:36 -08001028 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001029
1030 if (mustRecompose) {
1031 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1032 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001033 }
1034
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001035 HWComposer& hwc(getHwComposer());
1036 if (hwc.initCheck() == NO_ERROR) {
1037 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001038 if (CC_UNLIKELY(mHwWorkListDirty)) {
1039 mHwWorkListDirty = false;
1040 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1041 sp<const DisplayDevice> hw(mDisplays[dpy]);
1042 const int32_t id = hw->getHwcDisplayId();
1043 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001044 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001045 hw->getVisibleLayersSortedByZ());
1046 const size_t count = currentLayers.size();
1047 if (hwc.createWorkList(id, count) == NO_ERROR) {
1048 HWComposer::LayerListIterator cur = hwc.begin(id);
1049 const HWComposer::LayerListIterator end = hwc.end(id);
1050 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001051 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001052 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001053 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001054 cur->setSkip(true);
1055 }
1056 }
1057 }
1058 }
1059 }
1060 }
1061
1062 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001063 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001064 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001065 const int32_t id = hw->getHwcDisplayId();
1066 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001067 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001068 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001069 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001070 HWComposer::LayerListIterator cur = hwc.begin(id);
1071 const HWComposer::LayerListIterator end = hwc.end(id);
1072 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1073 /*
1074 * update the per-frame h/w composer data for each layer
1075 * and build the transparent region of the FB
1076 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001077 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001078 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -07001079 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001080 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001081 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001082
Riley Andrews03414a12014-07-01 14:22:59 -07001083 // If possible, attempt to use the cursor overlay on each display.
1084 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1085 sp<const DisplayDevice> hw(mDisplays[dpy]);
1086 const int32_t id = hw->getHwcDisplayId();
1087 if (id >= 0) {
1088 const Vector< sp<Layer> >& currentLayers(
1089 hw->getVisibleLayersSortedByZ());
1090 const size_t count = currentLayers.size();
1091 HWComposer::LayerListIterator cur = hwc.begin(id);
1092 const HWComposer::LayerListIterator end = hwc.end(id);
1093 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1094 const sp<Layer>& layer(currentLayers[i]);
1095 if (layer->isPotentialCursor()) {
1096 cur->setIsCursorLayerHint();
1097 break;
1098 }
1099 }
1100 }
1101 }
1102
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001103 status_t err = hwc.prepare();
1104 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001105
1106 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1107 sp<const DisplayDevice> hw(mDisplays[dpy]);
1108 hw->prepareFrame(hwc);
1109 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001110 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001111}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001112
Mathias Agopiancd60f992012-08-16 16:28:27 -07001113void SurfaceFlinger::doComposition() {
1114 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001115 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001116 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001117 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001118 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001119 // transform the dirty region into this screen's coordinate space
1120 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001121
1122 // repaint the framebuffer (if needed)
1123 doDisplayComposition(hw, dirtyRegion);
1124
Mathias Agopiancd60f992012-08-16 16:28:27 -07001125 hw->dirtyRegion.clear();
1126 hw->flip(hw->swapRegion);
1127 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001128 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001129 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001130 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001131 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001132 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001133}
1134
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001135void SurfaceFlinger::postFramebuffer()
1136{
Mathias Agopian841cde52012-03-01 15:44:37 -08001137 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001138
Mathias Agopiana44b0412011-10-16 18:46:35 -07001139 const nsecs_t now = systemTime();
1140 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001141
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001142 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001143 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001144 if (!hwc.supportsFramebufferTarget()) {
1145 // EGL spec says:
1146 // "surface must be bound to the calling thread's current context,
1147 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001148 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001149 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001150 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001151 }
1152
Mathias Agopian6da15f42013-09-25 20:40:07 -07001153 // make the default display current because the VirtualDisplayDevice code cannot
1154 // deal with dequeueBuffer() being called outside of the composition loop; however
1155 // the code below can call glFlush() which is allowed (and does in some case) call
1156 // dequeueBuffer().
1157 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1158
Mathias Agopian92a979a2012-08-02 18:32:23 -07001159 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001160 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001161 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001162 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001163 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001164 int32_t id = hw->getHwcDisplayId();
1165 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001166 HWComposer::LayerListIterator cur = hwc.begin(id);
1167 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001168 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001169 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001170 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001171 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001172 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001173 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001174 }
Jesse Hallef194142012-06-14 14:45:17 -07001175 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001176 }
1177
Mathias Agopiana44b0412011-10-16 18:46:35 -07001178 mLastSwapBufferTime = systemTime() - now;
1179 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001180
1181 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1182 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1183 logFrameStats();
1184 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001185}
1186
Mathias Agopian87baae12012-07-31 12:38:26 -07001187void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188{
Mathias Agopian841cde52012-03-01 15:44:37 -08001189 ATRACE_CALL();
1190
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001191 // here we keep a copy of the drawing state (that is the state that's
1192 // going to be overwritten by handleTransactionLocked()) outside of
1193 // mStateLock so that the side-effects of the State assignment
1194 // don't happen with mStateLock held (which can cause deadlocks).
1195 State drawingState(mDrawingState);
1196
Mathias Agopianca4d3602011-05-19 15:38:14 -07001197 Mutex::Autolock _l(mStateLock);
1198 const nsecs_t now = systemTime();
1199 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001200
Mathias Agopianca4d3602011-05-19 15:38:14 -07001201 // Here we're guaranteed that some transaction flags are set
1202 // so we can call handleTransactionLocked() unconditionally.
1203 // We call getTransactionFlags(), which will also clear the flags,
1204 // with mStateLock held to guarantee that mCurrentState won't change
1205 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001206
Mathias Agopiane57f2922012-08-09 16:29:12 -07001207 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001208 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001209
Mathias Agopianca4d3602011-05-19 15:38:14 -07001210 mLastTransactionTime = systemTime() - now;
1211 mDebugInTransaction = 0;
1212 invalidateHwcGeometry();
1213 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001214}
1215
Mathias Agopian87baae12012-07-31 12:38:26 -07001216void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001217{
1218 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219 const size_t count = currentLayers.size();
1220
1221 /*
1222 * Traversal of the children
1223 * (perform the transaction for each of them if needed)
1224 */
1225
Mathias Agopian3559b072012-08-15 13:46:03 -07001226 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001227 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001228 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001229 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1230 if (!trFlags) continue;
1231
1232 const uint32_t flags = layer->doTransaction(0);
1233 if (flags & Layer::eVisibleRegion)
1234 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001235 }
1236 }
1237
1238 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001239 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001240 */
1241
Mathias Agopiane57f2922012-08-09 16:29:12 -07001242 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001243 // here we take advantage of Vector's copy-on-write semantics to
1244 // improve performance by skipping the transaction entirely when
1245 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001246 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1247 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001248 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001249 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001250 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001251 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001252
1253 // find the displays that were removed
1254 // (ie: in drawing state but not in current state)
1255 // also handle displays that changed
1256 // (ie: displays that are in both lists)
1257 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001258 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1259 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001260 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001261 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001262 // Call makeCurrent() on the primary display so we can
1263 // be sure that nothing associated with this display
1264 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001265 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001266 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001267 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1268 if (hw != NULL)
1269 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001270 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001271 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001272 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001273 } else {
1274 ALOGW("trying to remove the main display");
1275 }
1276 } else {
1277 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001278 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001279 const wp<IBinder>& display(curr.keyAt(j));
Dan Albert1474f882014-09-08 18:59:09 -07001280 const sp<IBinder> state_binder =
1281 state.surface != NULL ? state.surface->asBinder() : NULL;
1282 const sp<IBinder> draw_binder =
1283 draw[i].surface != NULL ? draw[i].surface->asBinder() : NULL;
1284 if (state_binder != draw_binder) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001285 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001286 // recreating the DisplayDevice, so we just remove it
1287 // from the drawing state, so that it get re-added
1288 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001289 sp<DisplayDevice> hw(getDisplayDevice(display));
1290 if (hw != NULL)
1291 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001292 mDisplays.removeItem(display);
1293 mDrawingState.displays.removeItemsAt(i);
1294 dc--; i--;
1295 // at this point we must loop to the next item
1296 continue;
1297 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001298
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001299 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001300 if (disp != NULL) {
1301 if (state.layerStack != draw[i].layerStack) {
1302 disp->setLayerStack(state.layerStack);
1303 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001304 if ((state.orientation != draw[i].orientation)
1305 || (state.viewport != draw[i].viewport)
1306 || (state.frame != draw[i].frame))
1307 {
1308 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001309 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001310 }
Michael Lentine47e45402014-07-18 15:34:25 -07001311 if (state.width != draw[i].width || state.height != draw[i].height) {
1312 disp->setDisplaySize(state.width, state.height);
1313 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001314 }
1315 }
1316 }
1317
1318 // find displays that were added
1319 // (ie: in current state but not in drawing state)
1320 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001321 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001322 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001323
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001324 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001325 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001326 sp<IGraphicBufferProducer> bqProducer;
1327 sp<IGraphicBufferConsumer> bqConsumer;
1328 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1329 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001330
Jesse Hall02d86562013-03-25 14:43:23 -07001331 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001332 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001333 // Virtual displays without a surface are dormant:
1334 // they have external state (layer stack, projection,
1335 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001336 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001337
Dan Stoza1f3efb12014-10-15 16:34:55 -07001338 int width = 0;
1339 int status = state.surface->query(
1340 NATIVE_WINDOW_WIDTH, &width);
1341 ALOGE_IF(status != NO_ERROR,
1342 "Unable to query width (%d)", status);
1343 int height = 0;
1344 status = state.surface->query(
1345 NATIVE_WINDOW_HEIGHT, &height);
1346 ALOGE_IF(status != NO_ERROR,
1347 "Unable to query height (%d)", status);
1348 if (MAX_VIRTUAL_DISPLAY_DIMENSION == 0 ||
1349 (width <= MAX_VIRTUAL_DISPLAY_DIMENSION &&
1350 height <= MAX_VIRTUAL_DISPLAY_DIMENSION)) {
1351 hwcDisplayId = allocateHwcDisplayId(state.type);
1352 }
1353
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001354 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
Dan Stozab9b08832014-03-13 11:55:57 -07001355 *mHwc, hwcDisplayId, state.surface,
1356 bqProducer, bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001357
1358 dispSurface = vds;
Michael Lentine47e45402014-07-18 15:34:25 -07001359 producer = vds;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001360 }
1361 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001362 ALOGE_IF(state.surface!=NULL,
1363 "adding a supported display, but rendering "
1364 "surface is provided (%p), ignoring it",
1365 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001366 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001367 // for supported (by hwc) displays we provide our
1368 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001369 dispSurface = new FramebufferSurface(*mHwc, state.type,
1370 bqConsumer);
1371 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001372 }
1373
1374 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001375 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001376 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001377 state.type, hwcDisplayId,
1378 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001379 display, dispSurface, producer,
1380 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001381 hw->setLayerStack(state.layerStack);
1382 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001383 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001384 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001385 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001386 if (state.isVirtualDisplay()) {
1387 if (hwcDisplayId >= 0) {
1388 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1389 hw->getWidth(), hw->getHeight(),
1390 hw->getFormat());
1391 }
1392 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001393 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001394 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001395 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001396 }
1397 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001399 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001400
Mathias Agopian84300952012-11-21 16:02:13 -08001401 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1402 // The transform hint might have changed for some layers
1403 // (either because a display has changed, or because a layer
1404 // as changed).
1405 //
1406 // Walk through all the layers in currentLayers,
1407 // and update their transform hint.
1408 //
1409 // If a layer is visible only on a single display, then that
1410 // display is used to calculate the hint, otherwise we use the
1411 // default display.
1412 //
1413 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1414 // the hint is set before we acquire a buffer from the surface texture.
1415 //
1416 // NOTE: layer transactions have taken place already, so we use their
1417 // drawing state. However, SurfaceFlinger's own transaction has not
1418 // happened yet, so we must use the current state layer list
1419 // (soon to become the drawing state list).
1420 //
1421 sp<const DisplayDevice> disp;
1422 uint32_t currentlayerStack = 0;
1423 for (size_t i=0; i<count; i++) {
1424 // NOTE: we rely on the fact that layers are sorted by
1425 // layerStack first (so we don't have to traverse the list
1426 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001427 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001428 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001429 if (i==0 || currentlayerStack != layerStack) {
1430 currentlayerStack = layerStack;
1431 // figure out if this layerstack is mirrored
1432 // (more than one display) if so, pick the default display,
1433 // if not, pick the only display it's on.
1434 disp.clear();
1435 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1436 sp<const DisplayDevice> hw(mDisplays[dpy]);
1437 if (hw->getLayerStack() == currentlayerStack) {
1438 if (disp == NULL) {
1439 disp = hw;
1440 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001441 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001442 break;
1443 }
1444 }
1445 }
1446 }
Chet Haase91d25932013-04-11 15:24:55 -07001447 if (disp == NULL) {
1448 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1449 // redraw after transform hint changes. See bug 8508397.
1450
1451 // could be null when this layer is using a layerStack
1452 // that is not visible on any display. Also can occur at
1453 // screen off/on times.
1454 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001455 }
Chet Haase91d25932013-04-11 15:24:55 -07001456 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001457 }
1458 }
1459
1460
Mathias Agopian3559b072012-08-15 13:46:03 -07001461 /*
1462 * Perform our own transaction if needed
1463 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001464
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001465 const LayerVector& layers(mDrawingState.layersSortedByZ);
1466 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001467 // layers have been added
1468 mVisibleRegionsDirty = true;
1469 }
1470
1471 // some layers might have been removed, so
1472 // we need to update the regions they're exposing.
1473 if (mLayersRemoved) {
1474 mLayersRemoved = false;
1475 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001476 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001477 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001478 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001479 if (currentLayers.indexOf(layer) < 0) {
1480 // this layer is not visible anymore
1481 // TODO: we could traverse the tree from front to back and
1482 // compute the actual visible region
1483 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001484 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001485 Region visibleReg = s.transform.transform(
1486 Region(Rect(s.active.w, s.active.h)));
1487 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001488 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001490 }
1491
1492 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001493
1494 updateCursorAsync();
1495}
1496
1497void SurfaceFlinger::updateCursorAsync()
1498{
1499 HWComposer& hwc(getHwComposer());
1500 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1501 sp<const DisplayDevice> hw(mDisplays[dpy]);
1502 const int32_t id = hw->getHwcDisplayId();
1503 if (id < 0) {
1504 continue;
1505 }
1506 const Vector< sp<Layer> >& currentLayers(
1507 hw->getVisibleLayersSortedByZ());
1508 const size_t count = currentLayers.size();
1509 HWComposer::LayerListIterator cur = hwc.begin(id);
1510 const HWComposer::LayerListIterator end = hwc.end(id);
1511 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1512 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1513 continue;
1514 }
1515 const sp<Layer>& layer(currentLayers[i]);
1516 Rect cursorPos = layer->getPosition(hw);
1517 hwc.setCursorPositionAsync(id, cursorPos);
1518 break;
1519 }
1520 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001521}
1522
1523void SurfaceFlinger::commitTransaction()
1524{
1525 if (!mLayersPendingRemoval.isEmpty()) {
1526 // Notify removed layers now that they can't be drawn from
1527 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1528 mLayersPendingRemoval[i]->onRemoved();
1529 }
1530 mLayersPendingRemoval.clear();
1531 }
1532
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001533 // If this transaction is part of a window animation then the next frame
1534 // we composite should be considered an animation as well.
1535 mAnimCompositionPending = mAnimTransactionPending;
1536
Mathias Agopian4fec8732012-06-29 14:12:52 -07001537 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001538 mTransactionPending = false;
1539 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001540 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001541}
1542
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001543void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001544 const LayerVector& currentLayers, uint32_t layerStack,
1545 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001546{
Mathias Agopian841cde52012-03-01 15:44:37 -08001547 ATRACE_CALL();
1548
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001549 Region aboveOpaqueLayers;
1550 Region aboveCoveredLayers;
1551 Region dirty;
1552
Mathias Agopian87baae12012-07-31 12:38:26 -07001553 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001554
1555 size_t i = currentLayers.size();
1556 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001557 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001558
1559 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001560 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001561
Jesse Hall01e29052013-02-19 16:13:35 -08001562 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001563 if (s.layerStack != layerStack)
1564 continue;
1565
Mathias Agopianab028732010-03-16 16:41:46 -07001566 /*
1567 * opaqueRegion: area of a surface that is fully opaque.
1568 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001569 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001570
1571 /*
1572 * visibleRegion: area of a surface that is visible on screen
1573 * and not fully transparent. This is essentially the layer's
1574 * footprint minus the opaque regions above it.
1575 * Areas covered by a translucent surface are considered visible.
1576 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001577 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001578
1579 /*
1580 * coveredRegion: area of a surface that is covered by all
1581 * visible regions above it (which includes the translucent areas).
1582 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001583 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001584
Jesse Halla8026d22012-09-25 13:25:04 -07001585 /*
1586 * transparentRegion: area of a surface that is hinted to be completely
1587 * transparent. This is only used to tell when the layer has no visible
1588 * non-transparent regions and can be removed from the layer list. It
1589 * does not affect the visibleRegion of this layer or any layers
1590 * beneath it. The hint may not be correct if apps don't respect the
1591 * SurfaceView restrictions (which, sadly, some don't).
1592 */
1593 Region transparentRegion;
1594
Mathias Agopianab028732010-03-16 16:41:46 -07001595
1596 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001597 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001598 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001599 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001600 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001601 if (!visibleRegion.isEmpty()) {
1602 // Remove the transparent area from the visible region
1603 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001604 const Transform tr(s.transform);
1605 if (tr.transformed()) {
1606 if (tr.preserveRects()) {
1607 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001608 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001609 } else {
1610 // transformation too complex, can't do the
1611 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001612 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001613 }
1614 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001615 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001616 }
Mathias Agopianab028732010-03-16 16:41:46 -07001617 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001618
Mathias Agopianab028732010-03-16 16:41:46 -07001619 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001620 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001621 if (s.alpha==255 && !translucent &&
1622 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1623 // the opaque region is the layer's footprint
1624 opaqueRegion = visibleRegion;
1625 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001626 }
1627 }
1628
Mathias Agopianab028732010-03-16 16:41:46 -07001629 // Clip the covered region to the visible region
1630 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1631
1632 // Update aboveCoveredLayers for next (lower) layer
1633 aboveCoveredLayers.orSelf(visibleRegion);
1634
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001635 // subtract the opaque region covered by the layers above us
1636 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001637
1638 // compute this layer's dirty region
1639 if (layer->contentDirty) {
1640 // we need to invalidate the whole region
1641 dirty = visibleRegion;
1642 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001643 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001644 layer->contentDirty = false;
1645 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001646 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001647 * the exposed region consists of two components:
1648 * 1) what's VISIBLE now and was COVERED before
1649 * 2) what's EXPOSED now less what was EXPOSED before
1650 *
1651 * note that (1) is conservative, we start with the whole
1652 * visible region but only keep what used to be covered by
1653 * something -- which mean it may have been exposed.
1654 *
1655 * (2) handles areas that were not covered by anything but got
1656 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001657 */
Mathias Agopianab028732010-03-16 16:41:46 -07001658 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001659 const Region oldVisibleRegion = layer->visibleRegion;
1660 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001661 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1662 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663 }
1664 dirty.subtractSelf(aboveOpaqueLayers);
1665
1666 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001667 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001668
Mathias Agopianab028732010-03-16 16:41:46 -07001669 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001670 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001671
Jesse Halla8026d22012-09-25 13:25:04 -07001672 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001673 layer->setVisibleRegion(visibleRegion);
1674 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001675 layer->setVisibleNonTransparentRegion(
1676 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001677 }
1678
Mathias Agopian87baae12012-07-31 12:38:26 -07001679 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001680}
1681
Mathias Agopian87baae12012-07-31 12:38:26 -07001682void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1683 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001684 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001685 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1686 if (hw->getLayerStack() == layerStack) {
1687 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001688 }
1689 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001690}
1691
1692void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001693{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001694 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001695
Mathias Agopian4fec8732012-06-29 14:12:52 -07001696 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001697 const LayerVector& layers(mDrawingState.layersSortedByZ);
Eric Penner51c59cd2014-07-28 19:51:58 -07001698
1699 // Store the set of layers that need updates. This set must not change as
1700 // buffers are being latched, as this could result in a deadlock.
1701 // Example: Two producers share the same command stream and:
1702 // 1.) Layer 0 is latched
1703 // 2.) Layer 0 gets a new frame
1704 // 2.) Layer 1 gets a new frame
1705 // 3.) Layer 1 is latched.
1706 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1707 // second frame. But layer 0's second frame could be waiting on display.
1708 Vector<Layer*> layersWithQueuedFrames;
1709 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001710 const sp<Layer>& layer(layers[i]);
Eric Penner51c59cd2014-07-28 19:51:58 -07001711 if (layer->hasQueuedFrame())
1712 layersWithQueuedFrames.push_back(layer.get());
1713 }
1714 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
1715 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07001716 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001717 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001718 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001719 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001720
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001721 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001722}
1723
Mathias Agopianad456f92011-01-13 17:53:01 -08001724void SurfaceFlinger::invalidateHwcGeometry()
1725{
1726 mHwWorkListDirty = true;
1727}
1728
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001729
Mathias Agopiancd60f992012-08-16 16:28:27 -07001730void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001731 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001732{
Dan Stoza71433162014-02-04 16:22:36 -08001733 // We only need to actually compose the display if:
1734 // 1) It is being handled by hardware composer, which may need this to
1735 // keep its virtual display state machine in sync, or
1736 // 2) There is work to be done (the dirty region isn't empty)
1737 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1738 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1739 return;
1740 }
1741
Mathias Agopian87baae12012-07-31 12:38:26 -07001742 Region dirtyRegion(inDirtyRegion);
1743
Mathias Agopianb8a55602009-06-26 19:06:36 -07001744 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001745 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001746
Mathias Agopian42977342012-08-05 00:40:46 -07001747 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001748 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001749 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1750 // takes a rectangle, we must make sure to update that whole
1751 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001752 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001753 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001754 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001755 // We need to redraw the rectangle that will be updated
1756 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001757 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001758 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001759 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001760 } else {
1761 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001762 dirtyRegion.set(hw->bounds());
1763 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001764 }
1765 }
1766
Alan Viverette9c5a3332013-09-12 20:04:35 -07001767 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Michael Lentine3f121fc2014-10-01 11:17:28 -07001768 if (!doComposeSurfaces(hw, dirtyRegion)) return;
Mathias Agopianff2ed702013-09-01 21:36:12 -07001769 } else {
1770 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001771 mat4 colorMatrix = mColorMatrix;
1772 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001773 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001774 }
1775 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001776 doComposeSurfaces(hw, dirtyRegion);
1777 engine.endGroup();
1778 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001779
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001780 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001781 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001782
1783 // swap buffers (presentation)
1784 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001785}
1786
Michael Lentine3f121fc2014-10-01 11:17:28 -07001787bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001788{
Mathias Agopian3f844832013-08-07 21:24:32 -07001789 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001790 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001791 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001792 HWComposer::LayerListIterator cur = hwc.begin(id);
1793 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001794
Jesse Halld05a17f2013-09-30 16:49:30 -07001795 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001796 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001797 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001798 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1799 hw->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07001800 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1801 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
1802 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
1803 }
1804 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08001805 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001806
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001807 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001808 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001809 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001810 // when using overlays, we assume a fully transparent framebuffer
1811 // NOTE: we could reduce how much we need to clear, for instance
1812 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001813 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001814 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001815 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001816 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001817 // we start with the whole screen area
1818 const Region bounds(hw->getBounds());
1819
1820 // we remove the scissor part
1821 // we're left with the letterbox region
1822 // (common case is that letterbox ends-up being empty)
1823 const Region letterbox(bounds.subtract(hw->getScissor()));
1824
1825 // compute the area to clear
1826 Region region(hw->undefinedRegion.merge(letterbox));
1827
1828 // but limit it to the dirty region
1829 region.andSelf(dirty);
1830
Mathias Agopianb9494d52012-04-18 02:28:45 -07001831 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001832 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001833 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001834 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001835 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001836 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001837
Mathias Agopian766dc492012-10-30 18:08:06 -07001838 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1839 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001840 // scissor on the main display. It should never be needed
1841 // anyways (though in theory it could since the API allows it).
1842 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001843 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001844 if (scissor != bounds) {
1845 // scissor doesn't match the screen's dimensions, so we
1846 // need to clear everything outside of it and enable
1847 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001848
Mathias Agopianf45c5102012-10-24 16:29:17 -07001849 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001850 const uint32_t height = hw->getHeight();
1851 engine.setScissor(scissor.left, height - scissor.bottom,
1852 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001853 }
1854 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001855 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001856
Mathias Agopian85d751c2012-08-29 16:59:24 -07001857 /*
1858 * and then, render the layers targeted at the framebuffer
1859 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001860
Mathias Agopian13127d82013-03-05 17:47:11 -08001861 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001862 const size_t count = layers.size();
1863 const Transform& tr = hw->getTransform();
1864 if (cur != end) {
1865 // we're using h/w composer
1866 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001867 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001868 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001869 if (!clip.isEmpty()) {
1870 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07001871 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07001872 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001873 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001874 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1875 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001876 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001877 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001878 // never clear the very first layer since we're
1879 // guaranteed the FB is already cleared
1880 layer->clearWithOpenGL(hw, clip);
1881 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001882 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001883 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001884 case HWC_FRAMEBUFFER: {
1885 layer->draw(hw, clip);
1886 break;
1887 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001888 case HWC_FRAMEBUFFER_TARGET: {
1889 // this should not happen as the iterator shouldn't
1890 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001891 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001892 break;
1893 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001894 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001895 }
1896 layer->setAcquireFence(hw, *cur);
1897 }
1898 } else {
1899 // we're not using h/w composer
1900 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001901 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001902 const Region clip(dirty.intersect(
1903 tr.transform(layer->visibleRegion)));
1904 if (!clip.isEmpty()) {
1905 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001906 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001907 }
1908 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001909
1910 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001911 engine.disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07001912 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001913}
1914
Mathias Agopian3f844832013-08-07 21:24:32 -07001915void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001916 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001917 RenderEngine& engine(getRenderEngine());
1918 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001919}
1920
Mathias Agopianac9fa422013-02-11 16:40:36 -08001921void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1922 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001923 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001924 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001925{
Mathias Agopian96f08192010-06-02 23:28:45 -07001926 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001927 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001928
Mathias Agopian96f08192010-06-02 23:28:45 -07001929 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001930 Mutex::Autolock _l(mStateLock);
1931 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001932 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001933}
1934
Mathias Agopian3f844832013-08-07 21:24:32 -07001935status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001936 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001937 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001938 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001939 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001940 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001941 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001942 return NO_ERROR;
1943 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001944 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001945}
1946
Dan Stozac7014012014-02-14 15:03:43 -08001947uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001948 return android_atomic_release_load(&mTransactionFlags);
1949}
1950
Mathias Agopian3f844832013-08-07 21:24:32 -07001951uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001952 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1953}
1954
Mathias Agopian3f844832013-08-07 21:24:32 -07001955uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001956 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1957 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001958 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001959 }
1960 return old;
1961}
1962
Mathias Agopian8b33f032012-07-24 20:43:54 -07001963void SurfaceFlinger::setTransactionState(
1964 const Vector<ComposerState>& state,
1965 const Vector<DisplayState>& displays,
1966 uint32_t flags)
1967{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001968 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001969 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001970 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001971
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001972 if (flags & eAnimation) {
1973 // For window updates that are part of an animation we must wait for
1974 // previous animation "frames" to be handled.
1975 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001976 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001977 if (CC_UNLIKELY(err != NO_ERROR)) {
1978 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001979 // caller after a few seconds.
1980 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1981 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001982 mAnimTransactionPending = false;
1983 break;
1984 }
1985 }
1986 }
1987
Mathias Agopiane57f2922012-08-09 16:29:12 -07001988 size_t count = displays.size();
1989 for (size_t i=0 ; i<count ; i++) {
1990 const DisplayState& s(displays[i]);
1991 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001992 }
1993
Mathias Agopiane57f2922012-08-09 16:29:12 -07001994 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001995 for (size_t i=0 ; i<count ; i++) {
1996 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001997 // Here we need to check that the interface we're given is indeed
1998 // one of our own. A malicious client could give us a NULL
1999 // IInterface, or one of its own or even one of our own but a
2000 // different type. All these situations would cause us to crash.
2001 //
2002 // NOTE: it would be better to use RTTI as we could directly check
2003 // that we have a Client*. however, RTTI is disabled in Android.
2004 if (s.client != NULL) {
2005 sp<IBinder> binder = s.client->asBinder();
2006 if (binder != NULL) {
2007 String16 desc(binder->getInterfaceDescriptor());
2008 if (desc == ISurfaceComposerClient::descriptor) {
2009 sp<Client> client( static_cast<Client *>(s.client.get()) );
2010 transactionFlags |= setClientStateLocked(client, s.state);
2011 }
2012 }
2013 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002014 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002015
Mathias Agopian386aa982011-11-07 21:58:03 -08002016 if (transactionFlags) {
2017 // this triggers the transaction
2018 setTransactionFlags(transactionFlags);
2019
2020 // if this is a synchronous transaction, wait for it to take effect
2021 // before returning.
2022 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002023 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002024 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002025 if (flags & eAnimation) {
2026 mAnimTransactionPending = true;
2027 }
2028 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002029 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2030 if (CC_UNLIKELY(err != NO_ERROR)) {
2031 // just in case something goes wrong in SF, return to the
2032 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002033 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2034 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002035 break;
2036 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002037 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002038 }
2039}
2040
Mathias Agopiane57f2922012-08-09 16:29:12 -07002041uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2042{
Jesse Hall9a143922012-10-04 16:29:19 -07002043 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2044 if (dpyIdx < 0)
2045 return 0;
2046
Mathias Agopiane57f2922012-08-09 16:29:12 -07002047 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002048 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002049 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002050 const uint32_t what = s.what;
2051 if (what & DisplayState::eSurfaceChanged) {
2052 if (disp.surface->asBinder() != s.surface->asBinder()) {
2053 disp.surface = s.surface;
2054 flags |= eDisplayTransactionNeeded;
2055 }
2056 }
2057 if (what & DisplayState::eLayerStackChanged) {
2058 if (disp.layerStack != s.layerStack) {
2059 disp.layerStack = s.layerStack;
2060 flags |= eDisplayTransactionNeeded;
2061 }
2062 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002063 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002064 if (disp.orientation != s.orientation) {
2065 disp.orientation = s.orientation;
2066 flags |= eDisplayTransactionNeeded;
2067 }
2068 if (disp.frame != s.frame) {
2069 disp.frame = s.frame;
2070 flags |= eDisplayTransactionNeeded;
2071 }
2072 if (disp.viewport != s.viewport) {
2073 disp.viewport = s.viewport;
2074 flags |= eDisplayTransactionNeeded;
2075 }
2076 }
Michael Lentine47e45402014-07-18 15:34:25 -07002077 if (what & DisplayState::eDisplaySizeChanged) {
2078 if (disp.width != s.width) {
2079 disp.width = s.width;
2080 flags |= eDisplayTransactionNeeded;
2081 }
2082 if (disp.height != s.height) {
2083 disp.height = s.height;
2084 flags |= eDisplayTransactionNeeded;
2085 }
2086 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002087 }
2088 return flags;
2089}
2090
2091uint32_t SurfaceFlinger::setClientStateLocked(
2092 const sp<Client>& client,
2093 const layer_state_t& s)
2094{
2095 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002096 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002097 if (layer != 0) {
2098 const uint32_t what = s.what;
2099 if (what & layer_state_t::ePositionChanged) {
2100 if (layer->setPosition(s.x, s.y))
2101 flags |= eTraversalNeeded;
2102 }
2103 if (what & layer_state_t::eLayerChanged) {
2104 // NOTE: index needs to be calculated before we update the state
2105 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2106 if (layer->setLayer(s.z)) {
2107 mCurrentState.layersSortedByZ.removeAt(idx);
2108 mCurrentState.layersSortedByZ.add(layer);
2109 // we need traversal (state changed)
2110 // AND transaction (list changed)
2111 flags |= eTransactionNeeded|eTraversalNeeded;
2112 }
2113 }
2114 if (what & layer_state_t::eSizeChanged) {
2115 if (layer->setSize(s.w, s.h)) {
2116 flags |= eTraversalNeeded;
2117 }
2118 }
2119 if (what & layer_state_t::eAlphaChanged) {
2120 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2121 flags |= eTraversalNeeded;
2122 }
2123 if (what & layer_state_t::eMatrixChanged) {
2124 if (layer->setMatrix(s.matrix))
2125 flags |= eTraversalNeeded;
2126 }
2127 if (what & layer_state_t::eTransparentRegionChanged) {
2128 if (layer->setTransparentRegionHint(s.transparentRegion))
2129 flags |= eTraversalNeeded;
2130 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08002131 if ((what & layer_state_t::eVisibilityChanged) ||
2132 (what & layer_state_t::eOpacityChanged)) {
2133 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07002134 if (layer->setFlags(s.flags, s.mask))
2135 flags |= eTraversalNeeded;
2136 }
2137 if (what & layer_state_t::eCropChanged) {
2138 if (layer->setCrop(s.crop))
2139 flags |= eTraversalNeeded;
2140 }
2141 if (what & layer_state_t::eLayerStackChanged) {
2142 // NOTE: index needs to be calculated before we update the state
2143 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2144 if (layer->setLayerStack(s.layerStack)) {
2145 mCurrentState.layersSortedByZ.removeAt(idx);
2146 mCurrentState.layersSortedByZ.add(layer);
2147 // we need traversal (state changed)
2148 // AND transaction (list changed)
2149 flags |= eTransactionNeeded|eTraversalNeeded;
2150 }
2151 }
2152 }
2153 return flags;
2154}
2155
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002156status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002157 const String8& name,
2158 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002159 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2160 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002161{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002162 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002163 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002164 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002165 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002166 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002167 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002168
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002169 status_t result = NO_ERROR;
2170
2171 sp<Layer> layer;
2172
Mathias Agopian3165cc22012-08-08 19:42:09 -07002173 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2174 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002175 result = createNormalLayer(client,
2176 name, w, h, flags, format,
2177 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002178 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002179 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002180 result = createDimLayer(client,
2181 name, w, h, flags,
2182 handle, gbp, &layer);
2183 break;
2184 default:
2185 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002186 break;
2187 }
2188
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002189 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002190 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002191 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002192 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002193 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002194}
2195
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002196status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2197 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2198 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002199{
2200 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002201 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002202 case PIXEL_FORMAT_TRANSPARENT:
2203 case PIXEL_FORMAT_TRANSLUCENT:
2204 format = PIXEL_FORMAT_RGBA_8888;
2205 break;
2206 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002207 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002208 break;
2209 }
2210
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002211 *outLayer = new Layer(this, client, name, w, h, flags);
2212 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2213 if (err == NO_ERROR) {
2214 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002215 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002216 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002217
2218 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2219 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002220}
2221
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002222status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2223 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2224 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002225{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002226 *outLayer = new LayerDim(this, client, name, w, h, flags);
2227 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002228 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002229 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002230}
2231
Mathias Agopianac9fa422013-02-11 16:40:36 -08002232status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002233{
Mathias Agopian67106042013-03-14 19:18:13 -07002234 // called by the window manager when it wants to remove a Layer
2235 status_t err = NO_ERROR;
2236 sp<Layer> l(client->getLayerUser(handle));
2237 if (l != NULL) {
2238 err = removeLayer(l);
2239 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2240 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002241 }
2242 return err;
2243}
2244
Mathias Agopian13127d82013-03-05 17:47:11 -08002245status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002246{
Mathias Agopian67106042013-03-14 19:18:13 -07002247 // called by ~LayerCleaner() when all references to the IBinder (handle)
2248 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002249 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002250 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002251 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002252 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002253 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002254 "error removing layer=%p (%s)", l.get(), strerror(-err));
2255 }
2256 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002257}
2258
Mathias Agopianb60314a2012-04-10 22:09:54 -07002259// ---------------------------------------------------------------------------
2260
Andy McFadden13a082e2012-08-24 10:16:42 -07002261void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002262 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002263 Vector<ComposerState> state;
2264 Vector<DisplayState> displays;
2265 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002266 d.what = DisplayState::eDisplayProjectionChanged |
2267 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002268 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002269 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002270 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002271 d.frame.makeInvalid();
2272 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002273 d.width = 0;
2274 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002275 displays.add(d);
2276 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002277 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002278
2279 const nsecs_t period =
2280 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2281 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002282}
2283
2284void SurfaceFlinger::initializeDisplays() {
2285 class MessageScreenInitialized : public MessageBase {
2286 SurfaceFlinger* flinger;
2287 public:
2288 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2289 virtual bool handler() {
2290 flinger->onInitializeDisplays();
2291 return true;
2292 }
2293 };
2294 sp<MessageBase> msg = new MessageScreenInitialized(this);
2295 postMessageAsync(msg); // we may be called from main thread, use async message
2296}
2297
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002298void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2299 int mode) {
2300 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2301 this);
2302 int32_t type = hw->getDisplayType();
2303 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002304
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002305 if (mode == currentMode) {
2306 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002307 return;
2308 }
2309
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002310 hw->setPowerMode(mode);
2311 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2312 ALOGW("Trying to set power mode for virtual display");
2313 return;
2314 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002315
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002316 if (currentMode == HWC_POWER_MODE_OFF) {
2317 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002318 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2319 // FIXME: eventthread only knows about the main display right now
2320 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002321 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002322 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002323
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002324 mVisibleRegionsDirty = true;
2325 repaintEverything();
2326 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002327 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002328 disableHardwareVsync(true); // also cancels any in-progress resync
2329
Mathias Agopiancde87a32012-09-13 14:09:01 -07002330 // FIXME: eventthread only knows about the main display right now
2331 mEventThread->onScreenReleased();
2332 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002333
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002334 getHwComposer().setPowerMode(type, mode);
2335 mVisibleRegionsDirty = true;
2336 // from this point on, SF will stop drawing on this display
2337 } else {
2338 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002339 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002340}
2341
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002342void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2343 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002344 SurfaceFlinger& mFlinger;
2345 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002346 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002347 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002348 MessageSetPowerMode(SurfaceFlinger& flinger,
2349 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2350 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002351 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002352 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002353 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002354 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002355 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002356 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002357 ALOGW("Attempt to set power mode = %d for virtual display",
2358 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002359 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002360 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002361 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002362 return true;
2363 }
2364 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002365 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002366 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002367}
2368
2369// ---------------------------------------------------------------------------
2370
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002371status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2372{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002373 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002374
Mathias Agopianbd115332013-04-18 16:41:04 -07002375 IPCThreadState* ipc = IPCThreadState::self();
2376 const int pid = ipc->getCallingPid();
2377 const int uid = ipc->getCallingUid();
2378 if ((uid != AID_SHELL) &&
2379 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002380 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002381 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002382 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002383 // Try to get the main lock, but don't insist if we can't
2384 // (this would indicate SF is stuck, but we want to be able to
2385 // print something in dumpsys).
2386 int retry = 3;
2387 while (mStateLock.tryLock()<0 && --retry>=0) {
2388 usleep(1000000);
2389 }
2390 const bool locked(retry >= 0);
2391 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002392 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002393 "SurfaceFlinger appears to be unresponsive, "
2394 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002395 }
2396
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002397 bool dumpAll = true;
2398 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002399 size_t numArgs = args.size();
2400 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002401 if ((index < numArgs) &&
2402 (args[index] == String16("--list"))) {
2403 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002404 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002405 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002406 }
2407
2408 if ((index < numArgs) &&
2409 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002410 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002411 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002412 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002413 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002414
2415 if ((index < numArgs) &&
2416 (args[index] == String16("--latency-clear"))) {
2417 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002418 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002419 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002420 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002421
2422 if ((index < numArgs) &&
2423 (args[index] == String16("--dispsync"))) {
2424 index++;
2425 mPrimaryDispSync.dump(result);
2426 dumpAll = false;
2427 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002428 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002429
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002430 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002431 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002432 }
2433
Mathias Agopian9795c422009-08-26 16:36:26 -07002434 if (locked) {
2435 mStateLock.unlock();
2436 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002437 }
2438 write(fd, result.string(), result.size());
2439 return NO_ERROR;
2440}
2441
Dan Stozac7014012014-02-14 15:03:43 -08002442void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2443 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002444{
2445 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2446 const size_t count = currentLayers.size();
2447 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002448 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002449 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002450 }
2451}
2452
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002453void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002454 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002455{
2456 String8 name;
2457 if (index < args.size()) {
2458 name = String8(args[index]);
2459 index++;
2460 }
2461
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002462 const nsecs_t period =
2463 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002464 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002465
2466 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002467 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002468 } else {
2469 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2470 const size_t count = currentLayers.size();
2471 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002472 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002473 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002474 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002475 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002476 }
2477 }
2478}
2479
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002480void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002481 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002482{
2483 String8 name;
2484 if (index < args.size()) {
2485 name = String8(args[index]);
2486 index++;
2487 }
2488
2489 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2490 const size_t count = currentLayers.size();
2491 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002492 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002493 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002494 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002495 }
2496 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002497
Svetoslavd85084b2014-03-20 10:28:31 -07002498 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002499}
2500
Jamie Gennis6547ff42013-07-16 20:12:42 -07002501// This should only be called from the main thread. Otherwise it would need
2502// the lock and should use mCurrentState rather than mDrawingState.
2503void SurfaceFlinger::logFrameStats() {
2504 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2505 const size_t count = drawingLayers.size();
2506 for (size_t i=0 ; i<count ; i++) {
2507 const sp<Layer>& layer(drawingLayers[i]);
2508 layer->logFrameStats();
2509 }
2510
2511 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2512}
2513
Andy McFadden4803b742012-09-24 19:07:20 -07002514/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2515{
2516 static const char* config =
2517 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002518#ifdef HAS_CONTEXT_PRIORITY
2519 " HAS_CONTEXT_PRIORITY"
2520#endif
2521#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2522 " NEVER_DEFAULT_TO_ASYNC_MODE"
2523#endif
2524#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2525 " TARGET_DISABLE_TRIPLE_BUFFERING"
2526#endif
2527 "]";
2528 result.append(config);
2529}
2530
Mathias Agopian74d211a2013-04-22 16:55:35 +02002531void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2532 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002533{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002534 bool colorize = false;
2535 if (index < args.size()
2536 && (args[index] == String16("--color"))) {
2537 colorize = true;
2538 index++;
2539 }
2540
2541 Colorizer colorizer(colorize);
2542
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002543 // figure out if we're stuck somewhere
2544 const nsecs_t now = systemTime();
2545 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2546 const nsecs_t inTransaction(mDebugInTransaction);
2547 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2548 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2549
2550 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002551 * Dump library configuration.
2552 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002553
2554 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002555 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002556 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002557 appendSfConfigString(result);
2558 appendUiConfigString(result);
2559 appendGuiConfigString(result);
2560 result.append("\n");
2561
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002562 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002563 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002564 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002565 result.append(SyncFeatures::getInstance().toString());
2566 result.append("\n");
2567
Andy McFadden41d67d72014-04-25 16:58:34 -07002568 colorizer.bold(result);
2569 result.append("DispSync configuration: ");
2570 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002571 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2572 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002573 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2574 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2575 result.append("\n");
2576
Andy McFadden4803b742012-09-24 19:07:20 -07002577 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002578 * Dump the visible layer list
2579 */
2580 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2581 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002582 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002583 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002584 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002585 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002586 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002587 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002588 }
2589
2590 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002591 * Dump Display state
2592 */
2593
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002594 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002595 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002596 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002597 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2598 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002599 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002600 }
2601
2602 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002603 * Dump SurfaceFlinger global state
2604 */
2605
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002606 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002607 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002608 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002609
Mathias Agopian888c8222012-08-04 21:10:38 -07002610 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002611 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002612
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002613 colorizer.bold(result);
2614 result.appendFormat("EGL implementation : %s\n",
2615 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2616 colorizer.reset(result);
2617 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002618 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002619
Mathias Agopian875d8e12013-06-07 15:35:48 -07002620 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002621
Mathias Agopian42977342012-08-05 00:40:46 -07002622 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002623 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2624 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002625 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002626 " last eglSwapBuffers() time: %f us\n"
2627 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002628 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002629 " refresh-rate : %f fps\n"
2630 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002631 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002632 " gpu_to_cpu_unsupported : %d\n"
2633 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002634 mLastSwapBufferTime/1000.0,
2635 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002636 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002637 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2638 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002639 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002640 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002641
Mathias Agopian74d211a2013-04-22 16:55:35 +02002642 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002643 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002644
Mathias Agopian74d211a2013-04-22 16:55:35 +02002645 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002646 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002647
2648 /*
2649 * VSYNC state
2650 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002651 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002652
2653 /*
2654 * Dump HWComposer state
2655 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002656 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002657 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002658 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002659 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002660 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002661 (mDebugDisableHWC || mDebugRegion || mDaltonize
2662 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002663 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002664
2665 /*
2666 * Dump gralloc state
2667 */
2668 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2669 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002670}
2671
Mathias Agopian13127d82013-03-05 17:47:11 -08002672const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002673SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002674 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002675 wp<IBinder> dpy;
2676 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2677 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2678 dpy = mDisplays.keyAt(i);
2679 break;
2680 }
2681 }
2682 if (dpy == NULL) {
2683 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2684 // Just use the primary display so we have something to return
2685 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2686 }
2687 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002688}
2689
Keun young Park63f165f2012-08-31 10:53:36 -07002690bool SurfaceFlinger::startDdmConnection()
2691{
2692 void* libddmconnection_dso =
2693 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2694 if (!libddmconnection_dso) {
2695 return false;
2696 }
2697 void (*DdmConnection_start)(const char* name);
2698 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07002699 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07002700 if (!DdmConnection_start) {
2701 dlclose(libddmconnection_dso);
2702 return false;
2703 }
2704 (*DdmConnection_start)(getServiceName());
2705 return true;
2706}
2707
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002708status_t SurfaceFlinger::onTransact(
2709 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2710{
2711 switch (code) {
2712 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002713 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002714 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002715 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002716 case CLEAR_ANIMATION_FRAME_STATS:
2717 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002718 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002719 {
2720 // codes that require permission check
2721 IPCThreadState* ipc = IPCThreadState::self();
2722 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002723 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002724 if ((uid != AID_GRAPHICS) &&
2725 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002726 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002727 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2728 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002729 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002730 break;
2731 }
2732 case CAPTURE_SCREEN:
2733 {
2734 // codes that require permission check
2735 IPCThreadState* ipc = IPCThreadState::self();
2736 const int pid = ipc->getCallingPid();
2737 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002738 if ((uid != AID_GRAPHICS) &&
2739 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002740 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002741 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2742 return PERMISSION_DENIED;
2743 }
2744 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002745 }
2746 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002747
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002748 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2749 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002750 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002751 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002752 IPCThreadState* ipc = IPCThreadState::self();
2753 const int pid = ipc->getCallingPid();
2754 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002755 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002756 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002757 return PERMISSION_DENIED;
2758 }
2759 int n;
2760 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002761 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002762 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002763 return NO_ERROR;
2764 case 1002: // SHOW_UPDATES
2765 n = data.readInt32();
2766 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002767 invalidateHwcGeometry();
2768 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002769 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002770 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002771 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002772 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002773 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002774 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002775 setTransactionFlags(
2776 eTransactionNeeded|
2777 eDisplayTransactionNeeded|
2778 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002779 return NO_ERROR;
2780 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002781 case 1006:{ // send empty update
2782 signalRefresh();
2783 return NO_ERROR;
2784 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002785 case 1008: // toggle use of hw composer
2786 n = data.readInt32();
2787 mDebugDisableHWC = n ? 1 : 0;
2788 invalidateHwcGeometry();
2789 repaintEverything();
2790 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002791 case 1009: // toggle use of transform hint
2792 n = data.readInt32();
2793 mDebugDisableTransformHint = n ? 1 : 0;
2794 invalidateHwcGeometry();
2795 repaintEverything();
2796 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002797 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002798 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002799 reply->writeInt32(0);
2800 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002801 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002802 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002803 return NO_ERROR;
2804 case 1013: {
2805 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002806 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2807 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002808 return NO_ERROR;
2809 }
2810 case 1014: {
2811 // daltonize
2812 n = data.readInt32();
2813 switch (n % 10) {
2814 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2815 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2816 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2817 }
2818 if (n >= 10) {
2819 mDaltonizer.setMode(Daltonizer::correction);
2820 } else {
2821 mDaltonizer.setMode(Daltonizer::simulation);
2822 }
2823 mDaltonize = n > 0;
2824 invalidateHwcGeometry();
2825 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002826 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002827 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002828 case 1015: {
2829 // apply a color matrix
2830 n = data.readInt32();
2831 mHasColorMatrix = n ? 1 : 0;
2832 if (n) {
2833 // color matrix is sent as mat3 matrix followed by vec3
2834 // offset, then packed into a mat4 where the last row is
2835 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002836 for (size_t i = 0 ; i < 4; i++) {
2837 for (size_t j = 0; j < 4; j++) {
2838 mColorMatrix[i][j] = data.readFloat();
2839 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002840 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002841 } else {
2842 mColorMatrix = mat4();
2843 }
2844 invalidateHwcGeometry();
2845 repaintEverything();
2846 return NO_ERROR;
2847 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002848 // This is an experimental interface
2849 // Needs to be shifted to proper binder interface when we productize
2850 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07002851 n = data.readInt32();
2852 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002853 return NO_ERROR;
2854 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002855 }
2856 }
2857 return err;
2858}
2859
Mathias Agopian53331da2011-08-22 21:44:41 -07002860void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002861 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002862 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002863}
2864
Mathias Agopian59119e62010-10-11 12:37:43 -07002865// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002866// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002867// ---------------------------------------------------------------------------
2868
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002869/* The code below is here to handle b/8734824
2870 *
2871 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07002872 * from the surfaceflinger thread to the calling binder thread, where they
2873 * are executed. This allows the calling thread in the calling process to be
2874 * reused and not depend on having "enough" binder threads to handle the
2875 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002876 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002877class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07002878 /* Parts of GraphicProducerWrapper are run on two different threads,
2879 * communicating by sending messages via Looper but also by shared member
2880 * data. Coherence maintenance is subtle and in places implicit (ugh).
2881 *
2882 * Don't rely on Looper's sendMessage/handleMessage providing
2883 * release/acquire semantics for any data not actually in the Message.
2884 * Data going from surfaceflinger to binder threads needs to be
2885 * synchronized explicitly.
2886 *
2887 * Barrier open/wait do provide release/acquire semantics. This provides
2888 * implicit synchronization for data coming back from binder to
2889 * surfaceflinger threads.
2890 */
2891
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002892 sp<IGraphicBufferProducer> impl;
2893 sp<Looper> looper;
2894 status_t result;
2895 bool exitPending;
2896 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07002897 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002898 uint32_t code;
2899 Parcel const* data;
2900 Parcel* reply;
2901
2902 enum {
2903 MSG_API_CALL,
2904 MSG_EXIT
2905 };
2906
2907 /*
Jesse Hallb154c422014-07-13 12:47:02 -07002908 * Called on surfaceflinger thread. This is called by our "fake"
2909 * BpGraphicBufferProducer. We package the data and reply Parcel and
2910 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002911 */
2912 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08002913 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002914 this->code = code;
2915 this->data = &data;
2916 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002917 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07002918 // if we've exited, we run the message synchronously right here.
2919 // note (JH): as far as I can tell from looking at the code, this
2920 // never actually happens. if it does, i'm not sure if it happens
2921 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002922 handleMessage(Message(MSG_API_CALL));
2923 } else {
2924 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07002925 // Prevent stores to this->{code, data, reply} from being
2926 // reordered later than the construction of Message.
2927 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002928 looper->sendMessage(this, Message(MSG_API_CALL));
2929 barrier.wait();
2930 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08002931 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002932 }
2933
2934 /*
Jesse Hallb154c422014-07-13 12:47:02 -07002935 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002936 * call the real BpGraphicBufferProducer.
2937 */
2938 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07002939 int what = message.what;
2940 // Prevent reads below from happening before the read from Message
2941 atomic_thread_fence(memory_order_acquire);
2942 if (what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08002943 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002944 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07002945 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002946 exitRequested = true;
2947 }
2948 }
2949
2950public:
Jesse Hallb154c422014-07-13 12:47:02 -07002951 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
2952 : impl(impl),
2953 looper(new Looper(true)),
2954 exitPending(false),
2955 exitRequested(false)
2956 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002957
Jesse Hallb154c422014-07-13 12:47:02 -07002958 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002959 status_t waitForResponse() {
2960 do {
2961 looper->pollOnce(-1);
2962 } while (!exitRequested);
2963 return result;
2964 }
2965
Jesse Hallb154c422014-07-13 12:47:02 -07002966 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002967 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002968 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002969 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07002970 // Ensure this->result is visible to the binder thread before it
2971 // handles the message.
2972 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002973 looper->sendMessage(this, Message(MSG_EXIT));
2974 }
2975};
2976
2977
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002978status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2979 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002980 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002981 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07002982 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002983
2984 if (CC_UNLIKELY(display == 0))
2985 return BAD_VALUE;
2986
2987 if (CC_UNLIKELY(producer == 0))
2988 return BAD_VALUE;
2989
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002990 // if we have secure windows on this display, never allow the screen capture
2991 // unless the producer interface is local (i.e.: we can take a screenshot for
2992 // ourselves).
2993 if (!producer->asBinder()->localBinder()) {
2994 Mutex::Autolock _l(mStateLock);
2995 sp<const DisplayDevice> hw(getDisplayDevice(display));
2996 if (hw->getSecureLayerVisible()) {
2997 ALOGW("FB is protected: PERMISSION_DENIED");
2998 return PERMISSION_DENIED;
2999 }
3000 }
3001
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003002 // Convert to surfaceflinger's internal rotation type.
3003 Transform::orientation_flags rotationFlags;
3004 switch (rotation) {
3005 case ISurfaceComposer::eRotateNone:
3006 rotationFlags = Transform::ROT_0;
3007 break;
3008 case ISurfaceComposer::eRotate90:
3009 rotationFlags = Transform::ROT_90;
3010 break;
3011 case ISurfaceComposer::eRotate180:
3012 rotationFlags = Transform::ROT_180;
3013 break;
3014 case ISurfaceComposer::eRotate270:
3015 rotationFlags = Transform::ROT_270;
3016 break;
3017 default:
3018 rotationFlags = Transform::ROT_0;
3019 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3020 break;
3021 }
3022
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003023 class MessageCaptureScreen : public MessageBase {
3024 SurfaceFlinger* flinger;
3025 sp<IBinder> display;
3026 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003027 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003028 uint32_t reqWidth, reqHeight;
3029 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003030 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003031 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003032 status_t result;
3033 public:
3034 MessageCaptureScreen(SurfaceFlinger* flinger,
3035 const sp<IBinder>& display,
3036 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003037 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003038 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003039 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003040 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003041 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003042 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003043 useIdentityTransform(useIdentityTransform),
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003044 rotation(rotation),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003045 result(PERMISSION_DENIED)
3046 {
3047 }
3048 status_t getResult() const {
3049 return result;
3050 }
3051 virtual bool handler() {
3052 Mutex::Autolock _l(flinger->mStateLock);
3053 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003054 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003055 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003056 useIdentityTransform, rotation);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003057 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003058 return true;
3059 }
3060 };
3061
Mathias Agopian9eb1f052013-04-10 16:27:17 -07003062 // make sure to process transactions before screenshots -- a transaction
3063 // might already be pending but scheduled for VSYNC; this guarantees we
3064 // will handle it before the screenshot. When VSYNC finally arrives
3065 // the scheduled transaction will be a no-op. If no transactions are
3066 // scheduled at this time, this will end-up being a no-op as well.
3067 mEventQueue.invalidateTransactionNow();
3068
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003069 // this creates a "fake" BBinder which will serve as a "fake" remote
3070 // binder to receive the marshaled calls and forward them to the
3071 // real remote (a BpGraphicBufferProducer)
3072 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3073
3074 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3075 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003076 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003077 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003078 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003079 useIdentityTransform, rotationFlags);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003080
3081 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003082 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003083 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003084 }
3085 return res;
3086}
3087
Mathias Agopian180f10d2013-04-10 22:55:41 -07003088
3089void SurfaceFlinger::renderScreenImplLocked(
3090 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003091 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003092 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003093 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003094{
3095 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003096 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003097
3098 // get screen geometry
3099 const uint32_t hw_w = hw->getWidth();
3100 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003101 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
3102
Dan Stozac1879002014-05-22 15:59:05 -07003103 // if a default or invalid sourceCrop is passed in, set reasonable values
3104 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3105 !sourceCrop.isValid()) {
3106 sourceCrop.setLeftTop(Point(0, 0));
3107 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3108 }
3109
3110 // ensure that sourceCrop is inside screen
3111 if (sourceCrop.left < 0) {
3112 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3113 }
Jesse Hall78442112014-08-07 22:43:06 -07003114 if (static_cast<uint32_t>(sourceCrop.right) > hw_w) {
Dan Stozabe31f442014-06-11 11:20:54 -07003115 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003116 }
3117 if (sourceCrop.top < 0) {
3118 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3119 }
Jesse Hall78442112014-08-07 22:43:06 -07003120 if (static_cast<uint32_t>(sourceCrop.bottom) > hw_h) {
Dan Stozabe31f442014-06-11 11:20:54 -07003121 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003122 }
3123
Mathias Agopian180f10d2013-04-10 22:55:41 -07003124 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003125 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003126
3127 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003128 engine.setViewportAndProjection(
3129 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003130 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003131
3132 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003133 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003134
3135 const LayerVector& layers( mDrawingState.layersSortedByZ );
3136 const size_t count = layers.size();
3137 for (size_t i=0 ; i<count ; ++i) {
3138 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003139 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003140 if (state.layerStack == hw->getLayerStack()) {
3141 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3142 if (layer->isVisible()) {
3143 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08003144 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003145 if (filtering) layer->setFiltering(false);
3146 }
3147 }
3148 }
3149 }
3150
3151 // compositionComplete is needed for older driver
3152 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003153 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003154}
3155
3156
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003157status_t SurfaceFlinger::captureScreenImplLocked(
3158 const sp<const DisplayDevice>& hw,
3159 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003160 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003161 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003162 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003163{
3164 ATRACE_CALL();
3165
Mathias Agopian180f10d2013-04-10 22:55:41 -07003166 // get screen geometry
3167 const uint32_t hw_w = hw->getWidth();
3168 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003169
Mathias Agopian180f10d2013-04-10 22:55:41 -07003170 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3171 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3172 reqWidth, reqHeight, hw_w, hw_h);
3173 return BAD_VALUE;
3174 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003175
Mathias Agopian180f10d2013-04-10 22:55:41 -07003176 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3177 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3178
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003179 // create a surface (because we're a producer, and we need to
3180 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003181 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003182 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003183
3184 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003185 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003186 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3187 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003188
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003189 int err = 0;
3190 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003191 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003192 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3193 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003194
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003195 if (err == NO_ERROR) {
3196 ANativeWindowBuffer* buffer;
3197 /* TODO: Once we have the sync framework everywhere this can use
3198 * server-side waits on the fence that dequeueBuffer returns.
3199 */
3200 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3201 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003202 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003203 // create an EGLImage from the buffer so we can later
3204 // turn it into a texture
3205 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3206 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3207 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003208 // this binds the given EGLImage as a framebuffer for the
3209 // duration of this scope.
3210 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3211 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003212 // this will in fact render into our dequeued buffer
3213 // via an FBO, which means we didn't have to create
3214 // an EGLSurface and therefore we're not
3215 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003216 renderScreenImplLocked(
3217 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3218 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003219
Riley Andrews86639902014-08-15 12:27:24 -07003220 // Attempt to create a sync khr object that can produce a sync point. If that
3221 // isn't available, create a non-dupable sync object in the fallback path and
3222 // wait on it directly.
3223 EGLSyncKHR sync;
3224 if (!DEBUG_SCREENSHOTS) {
3225 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Riley Andrews9707f4d2014-10-23 16:17:04 -07003226 // native fence fd will not be populated until flush() is done.
3227 getRenderEngine().flush();
Andy McFadden2d8d1202013-10-09 16:38:02 -07003228 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003229 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003230 }
Riley Andrews86639902014-08-15 12:27:24 -07003231 if (sync != EGL_NO_SYNC_KHR) {
3232 // get the sync fd
3233 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3234 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3235 ALOGW("captureScreen: failed to dup sync khr object");
3236 syncFd = -1;
3237 }
3238 eglDestroySyncKHR(mEGLDisplay, sync);
3239 } else {
3240 // fallback path
3241 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3242 if (sync != EGL_NO_SYNC_KHR) {
3243 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3244 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3245 EGLint eglErr = eglGetError();
3246 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3247 ALOGW("captureScreen: fence wait timed out");
3248 } else {
3249 ALOGW_IF(eglErr != EGL_SUCCESS,
3250 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3251 }
3252 eglDestroySyncKHR(mEGLDisplay, sync);
3253 } else {
3254 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3255 }
3256 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003257 if (DEBUG_SCREENSHOTS) {
3258 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3259 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3260 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3261 hw, minLayerZ, maxLayerZ);
3262 delete [] pixels;
3263 }
3264
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003265 } else {
3266 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3267 result = INVALID_OPERATION;
3268 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003269 // destroy our image
3270 eglDestroyImageKHR(mEGLDisplay, image);
3271 } else {
3272 result = BAD_VALUE;
3273 }
Jesse Hallafe2b1f2014-10-21 11:09:17 -07003274 // queueBuffer takes ownership of syncFd
Riley Andrews86639902014-08-15 12:27:24 -07003275 window->queueBuffer(window, buffer, syncFd);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003276 }
3277 } else {
3278 result = BAD_VALUE;
3279 }
3280 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3281 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003282
Mathias Agopian74c40c02010-09-29 13:02:36 -07003283 return result;
3284}
3285
Mathias Agopiand5556842013-09-19 17:08:37 -07003286void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3287 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003288 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003289 for (size_t y=0 ; y<h ; y++) {
3290 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3291 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003292 if (p[x] != 0xFF000000) return;
3293 }
3294 }
3295 ALOGE("*** we just took a black screenshot ***\n"
3296 "requested minz=%d, maxz=%d, layerStack=%d",
3297 minLayerZ, maxLayerZ, hw->getLayerStack());
3298 const LayerVector& layers( mDrawingState.layersSortedByZ );
3299 const size_t count = layers.size();
3300 for (size_t i=0 ; i<count ; ++i) {
3301 const sp<Layer>& layer(layers[i]);
3302 const Layer::State& state(layer->getDrawingState());
3303 const bool visible = (state.layerStack == hw->getLayerStack())
3304 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3305 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003306 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003307 visible ? '+' : '-',
3308 i, layer->getName().string(), state.layerStack, state.z,
3309 layer->isVisible(), state.flags, state.alpha);
3310 }
3311 }
3312}
3313
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003314// ---------------------------------------------------------------------------
3315
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003316SurfaceFlinger::LayerVector::LayerVector() {
3317}
3318
3319SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003320 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003321}
3322
3323int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3324 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003325{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003326 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003327 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3328 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003329
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003330 uint32_t ls = l->getCurrentState().layerStack;
3331 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003332 if (ls != rs)
3333 return ls - rs;
3334
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003335 uint32_t lz = l->getCurrentState().z;
3336 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003337 if (lz != rz)
3338 return lz - rz;
3339
3340 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003341}
3342
3343// ---------------------------------------------------------------------------
3344
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003345SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Michael Lentine47e45402014-07-18 15:34:25 -07003346 : type(DisplayDevice::DISPLAY_ID_INVALID), width(0), height(0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003347}
3348
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003349SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Michael Lentine47e45402014-07-18 15:34:25 -07003350 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0), width(0), height(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003351 viewport.makeInvalid();
3352 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003353}
3354
3355// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003356
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003357}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003358
3359
3360#if defined(__gl_h_)
3361#error "don't include gl/gl.h in this file"
3362#endif
3363
3364#if defined(__gl2_h_)
3365#error "don't include gl2/gl2.h in this file"
3366#endif