blob: f8bf3681e530949a4f58d3747aa63bd211da92c8 [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
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -070081#ifdef QCOM_BSP
82#include <display_config.h>
83#endif
84
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085#define DISPLAY_COUNT 1
86
Mathias Agopianfee2b462013-07-03 12:34:01 -070087/*
88 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
89 * black pixels.
90 */
91#define DEBUG_SCREENSHOTS false
92
Mathias Agopianca088332013-03-28 17:44:13 -070093EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
94
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070096
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070097// This is the phase offset in nanoseconds of the software vsync event
98// relative to the vsync event reported by HWComposer. The software vsync
99// event is when SurfaceFlinger and Choreographer-based applications run each
100// frame.
101//
102// This phase offset allows adjustment of the minimum latency from application
103// wake-up (by Choregographer) time to the time at which the resulting window
104// image is displayed. This value may be either positive (after the HW vsync)
105// or negative (before the HW vsync). Setting it to 0 will result in a
106// minimum latency of two vsync periods because the app and SurfaceFlinger
107// will run just after the HW vsync. Setting it to a positive number will
108// result in the minimum latency being:
109//
110// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
111//
112// Note that reducing this latency makes it more likely for the applications
113// to not have their window content image ready in time. When this happens
114// the latency will end up being an additional vsync period, and animations
115// will hiccup. Therefore, this latency should be tuned somewhat
116// conservatively (or at least with awareness of the trade-off being made).
117static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
118
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700119// This is the phase offset at which SurfaceFlinger's composition runs.
120static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
121
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122// ---------------------------------------------------------------------------
123
Mathias Agopian99b49842011-06-27 16:05:52 -0700124const String16 sHardwareTest("android.permission.HARDWARE_TEST");
125const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
126const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
127const String16 sDump("android.permission.DUMP");
128
129// ---------------------------------------------------------------------------
130
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700132 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700134 mTransactionPending(false),
135 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700136 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700137 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700138 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700141 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800142 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700144 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700145 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700146 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700147 mDebugInSwapBuffers(0),
148 mLastSwapBufferTime(0),
149 mDebugInTransaction(0),
150 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700151 mBootFinished(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700152 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700153 mHWVsyncAvailable(false),
Alan Viverette9c5a3332013-09-12 20:04:35 -0700154 mDaltonize(false),
155 mHasColorMatrix(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156{
Steve Blocka19954a2012-01-04 20:05:49 +0000157 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158
159 // debugging stuff...
160 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700161
Mathias Agopianb4b17302013-03-20 18:36:41 -0700162 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700163 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700164
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 property_get("debug.sf.showupdates", value, "0");
166 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700167
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700168 property_get("debug.sf.ddms", value, "0");
169 mDebugDDMS = atoi(value);
170 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700171 if (!startDdmConnection()) {
172 // start failed, and DDMS debugging not enabled
173 mDebugDDMS = 0;
174 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700175 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700176 ALOGI_IF(mDebugRegion, "showupdates enabled");
177 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178}
179
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800180void SurfaceFlinger::onFirstRef()
181{
182 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800183}
184
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185SurfaceFlinger::~SurfaceFlinger()
186{
Mathias Agopiana4912602012-07-12 14:25:33 -0700187 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
188 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
189 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190}
191
Dan Stozac7014012014-02-14 15:03:43 -0800192void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800193{
194 // the window manager died on us. prepare its eulogy.
195
Andy McFadden13a082e2012-08-24 10:16:42 -0700196 // restore initial conditions (default device unblank, etc)
197 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800198
199 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700200 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800201}
202
Mathias Agopian7e27f052010-05-28 14:22:23 -0700203sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204{
Mathias Agopian96f08192010-06-02 23:28:45 -0700205 sp<ISurfaceComposerClient> bclient;
206 sp<Client> client(new Client(this));
207 status_t err = client->initCheck();
208 if (err == NO_ERROR) {
209 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800210 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 return bclient;
212}
213
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700214sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
215 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700216{
217 class DisplayToken : public BBinder {
218 sp<SurfaceFlinger> flinger;
219 virtual ~DisplayToken() {
220 // no more references, this display must be terminated
221 Mutex::Autolock _l(flinger->mStateLock);
222 flinger->mCurrentState.displays.removeItem(this);
223 flinger->setTransactionFlags(eDisplayTransactionNeeded);
224 }
225 public:
226 DisplayToken(const sp<SurfaceFlinger>& flinger)
227 : flinger(flinger) {
228 }
229 };
230
231 sp<BBinder> token = new DisplayToken(this);
232
233 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700234 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700235 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700236 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700237 mCurrentState.displays.add(token, info);
238
239 return token;
240}
241
Jesse Hall6c913be2013-08-08 12:15:49 -0700242void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
243 Mutex::Autolock _l(mStateLock);
244
245 ssize_t idx = mCurrentState.displays.indexOfKey(display);
246 if (idx < 0) {
247 ALOGW("destroyDisplay: invalid display token");
248 return;
249 }
250
251 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
252 if (!info.isVirtualDisplay()) {
253 ALOGE("destroyDisplay called for non-virtual display");
254 return;
255 }
256
257 mCurrentState.displays.removeItemsAt(idx);
258 setTransactionFlags(eDisplayTransactionNeeded);
259}
260
Jesse Hall692c7232012-11-08 15:41:56 -0800261void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
262 ALOGW_IF(mBuiltinDisplays[type],
263 "Overwriting display token for display type %d", type);
264 mBuiltinDisplays[type] = new BBinder();
265 DisplayDeviceState info(type);
266 // All non-virtual displays are currently considered secure.
267 info.isSecure = true;
268 mCurrentState.displays.add(mBuiltinDisplays[type], info);
269}
270
Mathias Agopiane57f2922012-08-09 16:29:12 -0700271sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700272 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700273 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
274 return NULL;
275 }
Jesse Hall692c7232012-11-08 15:41:56 -0800276 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700277}
278
Jamie Gennis9a78c902011-01-12 18:30:40 -0800279sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
280{
281 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
282 return gba;
283}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700284
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800285void SurfaceFlinger::bootFinished()
286{
287 const nsecs_t now = systemTime();
288 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000289 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700290 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700291
292 // wait patiently for the window manager death
293 const String16 name("window");
294 sp<IBinder> window(defaultServiceManager()->getService(name));
295 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700296 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700297 }
298
299 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700300 // formerly we would just kill the process, but we now ask it to exit so it
301 // can choose where to stop the animation.
302 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800303}
304
Mathias Agopian3f844832013-08-07 21:24:32 -0700305void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700306 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700307 RenderEngine& engine;
308 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700309 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700310 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
311 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700312 }
313 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700314 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700315 return true;
316 }
317 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700318 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700319}
320
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700321class DispSyncSource : public VSyncSource, private DispSync::Callback {
322public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700323 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
324 const char* label) :
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700325 mValue(0),
326 mPhaseOffset(phaseOffset),
327 mTraceVsync(traceVsync),
Andy McFadden5167ec62014-05-22 13:08:43 -0700328 mVsyncOnLabel(String8::format("VsyncOn-%s", label)),
329 mVsyncEventLabel(String8::format("VSYNC-%s", label)),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700330 mDispSync(dispSync) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700331
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700332 virtual ~DispSyncSource() {}
333
334 virtual void setVSyncEnabled(bool enable) {
335 // Do NOT lock the mutex here so as to avoid any mutex ordering issues
336 // with locking it in the onDispSyncEvent callback.
337 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700338 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700339 static_cast<DispSync::Callback*>(this));
340 if (err != NO_ERROR) {
341 ALOGE("error registering vsync callback: %s (%d)",
342 strerror(-err), err);
343 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700344 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700345 } else {
346 status_t err = mDispSync->removeEventListener(
347 static_cast<DispSync::Callback*>(this));
348 if (err != NO_ERROR) {
349 ALOGE("error unregistering vsync callback: %s (%d)",
350 strerror(-err), err);
351 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700352 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700353 }
354 }
355
356 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
357 Mutex::Autolock lock(mMutex);
358 mCallback = callback;
359 }
360
361private:
362 virtual void onDispSyncEvent(nsecs_t when) {
363 sp<VSyncSource::Callback> callback;
364 {
365 Mutex::Autolock lock(mMutex);
366 callback = mCallback;
367
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700368 if (mTraceVsync) {
369 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700370 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700371 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700372 }
373
374 if (callback != NULL) {
375 callback->onVSyncEvent(when);
376 }
377 }
378
379 int mValue;
380
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700381 const nsecs_t mPhaseOffset;
382 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700383 const String8 mVsyncOnLabel;
384 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700385
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700386 DispSync* mDispSync;
387 sp<VSyncSource::Callback> mCallback;
388 Mutex mMutex;
389};
390
391void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700392 ALOGI( "SurfaceFlinger's main thread ready to run. "
393 "Initializing graphics H/W...");
394
Jesse Hallb65f32e2013-09-27 22:10:47 -0700395 status_t err;
Jesse Hall692c7232012-11-08 15:41:56 -0800396 Mutex::Autolock _l(mStateLock);
397
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700398 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700399 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
400 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700401
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700402 // Initialize the H/W composer object. There may or may not be an
403 // actual hardware composer underneath.
404 mHwc = new HWComposer(this,
405 *static_cast<HWComposer::EventHandler *>(this));
406
Mathias Agopian875d8e12013-06-07 15:35:48 -0700407 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800408 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700409
410 // retrieve the EGL context that was selected/created
411 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700412
Mathias Agopianda27af92012-09-13 18:17:13 -0700413 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
414 "couldn't create EGLContext");
415
Mathias Agopiancde87a32012-09-13 14:09:01 -0700416 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700417 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700418 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700419 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700420 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700421 // All non-virtual displays are currently considered secure.
422 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800423 createBuiltinDisplayLocked(type);
424 wp<IBinder> token = mBuiltinDisplays[i];
425
Dan Stozab9b08832014-03-13 11:55:57 -0700426 sp<IGraphicBufferProducer> producer;
427 sp<IGraphicBufferConsumer> consumer;
428 BufferQueue::createBufferQueue(&producer, &consumer,
429 new GraphicBufferAlloc());
430
431 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
432 consumer);
Jesse Hall19e87292013-12-23 21:02:15 -0800433 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700434 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800435 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Dan Stozab9b08832014-03-13 11:55:57 -0700436 fbs, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -0800437 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700438 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700439 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700440 // for displays other than the main display, so we always
441 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800442 ALOGD("marking display %zu as acquired/unblanked", i);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700443 hw->setPowerMode(HWC_POWER_MODE_NORMAL);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700444 }
445 mDisplays.add(token, hw);
446 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700447 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700448
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700449 // make the GLContext current so that we can create textures when creating Layers
450 // (which may happens before we render something)
451 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
452
Mathias Agopian028508c2012-07-25 21:12:12 -0700453 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700454 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700455 vsyncPhaseOffsetNs, true, "app");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700456 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700457 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700458 sfVsyncPhaseOffsetNs, true, "sf");
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700459 mSFEventThread = new EventThread(sfVsyncSrc);
460 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700461
Jamie Gennisd1700752013-10-14 12:22:52 -0700462 mEventControlThread = new EventControlThread(this);
463 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
464
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700465 // set a fake vsync period if there is no HWComposer
466 if (mHwc->initCheck() != NO_ERROR) {
467 mPrimaryDispSync.setPeriod(16666667);
468 }
469
Mathias Agopian92a979a2012-08-02 18:32:23 -0700470 // initialize our drawing state
471 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700472
Andy McFadden13a082e2012-08-24 10:16:42 -0700473 // set initial conditions (e.g. unblank default device)
474 initializeDisplays();
475
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700476 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700477 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800478}
479
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700480int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700481 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700482 type : mHwc->allocateDisplayId();
483}
484
Mathias Agopiana67e4182012-06-19 17:26:12 -0700485void SurfaceFlinger::startBootAnim() {
486 // start boot animation
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -0700487 mBootFinished = false;
Mathias Agopiana67e4182012-06-19 17:26:12 -0700488 property_set("service.bootanim.exit", "0");
489 property_set("ctl.start", "bootanim");
490}
491
Mathias Agopian875d8e12013-06-07 15:35:48 -0700492size_t SurfaceFlinger::getMaxTextureSize() const {
493 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700494}
495
Mathias Agopian875d8e12013-06-07 15:35:48 -0700496size_t SurfaceFlinger::getMaxViewportDims() const {
497 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700498}
499
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800500// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800501
Jamie Gennis582270d2011-08-17 18:19:00 -0700502bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800503 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800504 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800505 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700506 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800507}
508
Dan Stoza7f7da322014-05-02 15:26:25 -0700509status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
510 Vector<DisplayInfo>* configs) {
511 if (configs == NULL) {
512 return BAD_VALUE;
513 }
514
Jesse Hall692c7232012-11-08 15:41:56 -0800515 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700516 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800517 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700518 type = i;
519 break;
520 }
521 }
522
523 if (type < 0) {
524 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700525 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700526
Mathias Agopian8b736f12012-08-13 17:54:26 -0700527 // TODO: Not sure if display density should handled by SF any longer
528 class Density {
529 static int getDensityFromProperty(char const* propName) {
530 char property[PROPERTY_VALUE_MAX];
531 int density = 0;
532 if (property_get(propName, property, NULL) > 0) {
533 density = atoi(property);
534 }
535 return density;
536 }
537 public:
538 static int getEmuDensity() {
539 return getDensityFromProperty("qemu.sf.lcd_density"); }
540 static int getBuildDensity() {
541 return getDensityFromProperty("ro.sf.lcd_density"); }
542 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700543
Dan Stoza7f7da322014-05-02 15:26:25 -0700544 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700545
Dan Stoza7f7da322014-05-02 15:26:25 -0700546 const Vector<HWComposer::DisplayConfig>& hwConfigs =
547 getHwComposer().getConfigs(type);
548 for (size_t c = 0; c < hwConfigs.size(); ++c) {
549 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
550 DisplayInfo info = DisplayInfo();
551
552 float xdpi = hwConfig.xdpi;
553 float ydpi = hwConfig.ydpi;
554
555 if (type == DisplayDevice::DISPLAY_PRIMARY) {
556 // The density of the device is provided by a build property
557 float density = Density::getBuildDensity() / 160.0f;
558 if (density == 0) {
559 // the build doesn't provide a density -- this is wrong!
560 // use xdpi instead
561 ALOGE("ro.sf.lcd_density must be defined as a build property");
562 density = xdpi / 160.0f;
563 }
564 if (Density::getEmuDensity()) {
565 // if "qemu.sf.lcd_density" is specified, it overrides everything
566 xdpi = ydpi = density = Density::getEmuDensity();
567 density /= 160.0f;
568 }
569 info.density = density;
570
571 // TODO: this needs to go away (currently needed only by webkit)
572 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
573 info.orientation = hw->getOrientation();
574 } else {
575 // TODO: where should this value come from?
576 static const int TV_DENSITY = 213;
577 info.density = TV_DENSITY / 160.0f;
578 info.orientation = 0;
579 }
580
581 info.w = hwConfig.width;
582 info.h = hwConfig.height;
583 info.xdpi = xdpi;
584 info.ydpi = ydpi;
585 info.fps = float(1e9 / hwConfig.refresh);
Andy McFadden91b2ca82014-06-13 14:04:23 -0700586 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
587
588 // This is how far in advance a buffer must be queued for
589 // presentation at a given time. If you want a buffer to appear
590 // on the screen at time N, you must submit the buffer before
591 // (N - presentationDeadline).
592 //
593 // Normally it's one full refresh period (to give SF a chance to
594 // latch the buffer), but this can be reduced by configuring a
595 // DispSync offset. Any additional delays introduced by the hardware
596 // composer or panel must be accounted for here.
597 //
598 // We add an additional 1ms to allow for processing time and
599 // differences between the ideal and actual refresh rate.
600 info.presentationDeadline =
601 hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700602
603 // All non-virtual displays are currently considered secure.
604 info.secure = true;
605
606 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700607 }
608
Dan Stoza7f7da322014-05-02 15:26:25 -0700609 return NO_ERROR;
610}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700611
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700612status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& display,
613 DisplayStatInfo* stats) {
614 if (stats == NULL) {
615 return BAD_VALUE;
616 }
617
618 // FIXME for now we always return stats for the primary display
619 memset(stats, 0, sizeof(*stats));
620 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
621 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
622 return NO_ERROR;
623}
624
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700625int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
626 return getDisplayDevice(display)->getActiveConfig();
Dan Stoza7f7da322014-05-02 15:26:25 -0700627}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700628
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700629void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
630 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
631 this);
632 int32_t type = hw->getDisplayType();
633 int currentMode = hw->getActiveConfig();
634
635 if (mode == currentMode) {
636 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
637 return;
638 }
639
640 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
641 ALOGW("Trying to set config for virtual display");
642 return;
643 }
644
645 hw->setActiveConfig(mode);
646 getHwComposer().setActiveConfig(type, mode);
647}
648
649status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
650 class MessageSetActiveConfig: public MessageBase {
651 SurfaceFlinger& mFlinger;
652 sp<IBinder> mDisplay;
653 int mMode;
654 public:
655 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
656 int mode) :
657 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
658 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700659 Vector<DisplayInfo> configs;
660 mFlinger.getDisplayConfigs(mDisplay, &configs);
661 if(mMode < 0 || mMode >= configs.size()) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700662 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700663 mMode, configs.size());
664 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700665 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
666 if (hw == NULL) {
667 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700668 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700669 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
670 ALOGW("Attempt to set active config = %d for virtual display",
671 mMode);
672 } else {
673 mFlinger.setActiveConfigInternal(hw, mMode);
674 }
675 return true;
676 }
677 };
678 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
679 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700680 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700681}
682
Svetoslavd85084b2014-03-20 10:28:31 -0700683status_t SurfaceFlinger::clearAnimationFrameStats() {
684 Mutex::Autolock _l(mStateLock);
685 mAnimFrameTracker.clearStats();
686 return NO_ERROR;
687}
688
689status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
690 Mutex::Autolock _l(mStateLock);
691 mAnimFrameTracker.getStats(outStats);
692 return NO_ERROR;
693}
694
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800695// ----------------------------------------------------------------------------
696
697sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800698 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700699}
700
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800701// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800702
703void SurfaceFlinger::waitForEvent() {
704 mEventQueue.waitMessage();
705}
706
707void SurfaceFlinger::signalTransaction() {
708 mEventQueue.invalidate();
709}
710
711void SurfaceFlinger::signalLayerUpdate() {
712 mEventQueue.invalidate();
713}
714
715void SurfaceFlinger::signalRefresh() {
716 mEventQueue.refresh();
717}
718
719status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800720 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800721 return mEventQueue.postMessage(msg, reltime);
722}
723
724status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800725 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800726 status_t res = mEventQueue.postMessage(msg, reltime);
727 if (res == NO_ERROR) {
728 msg->wait();
729 }
730 return res;
731}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800732
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700733void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700734 do {
735 waitForEvent();
736 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737}
738
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700739void SurfaceFlinger::enableHardwareVsync() {
740 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700741 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700742 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700743 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
744 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700745 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700746 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700747}
748
Jesse Hall948fe0c2013-10-14 12:56:09 -0700749void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700750 Mutex::Autolock _l(mHWVsyncLock);
751
Jesse Hall948fe0c2013-10-14 12:56:09 -0700752 if (makeAvailable) {
753 mHWVsyncAvailable = true;
754 } else if (!mHWVsyncAvailable) {
755 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
756 return;
757 }
758
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700759 const nsecs_t period =
760 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
761
762 mPrimaryDispSync.reset();
763 mPrimaryDispSync.setPeriod(period);
764
765 if (!mPrimaryHWVsyncEnabled) {
766 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700767 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
768 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700769 mPrimaryHWVsyncEnabled = true;
770 }
771}
772
Jesse Hall948fe0c2013-10-14 12:56:09 -0700773void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700774 Mutex::Autolock _l(mHWVsyncLock);
775 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700776 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
777 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700778 mPrimaryDispSync.endResync();
779 mPrimaryHWVsyncEnabled = false;
780 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700781 if (makeUnavailable) {
782 mHWVsyncAvailable = false;
783 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700784}
785
786void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700787 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700788
Jamie Gennisd1700752013-10-14 12:22:52 -0700789 { // Scope for the lock
790 Mutex::Autolock _l(mHWVsyncLock);
791 if (type == 0 && mPrimaryHWVsyncEnabled) {
792 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700793 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700794 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700795
796 if (needsHwVsync) {
797 enableHardwareVsync();
798 } else {
799 disableHardwareVsync(false);
800 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700801}
802
803void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
804 if (mEventThread == NULL) {
805 // This is a temporary workaround for b/7145521. A non-null pointer
806 // does not mean EventThread has finished initializing, so this
807 // is not a correct fix.
808 ALOGW("WARNING: EventThread not started, ignoring hotplug");
809 return;
810 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700811
Jesse Hall9e663de2013-08-16 14:28:37 -0700812 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700813 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800814 if (connected) {
815 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700816 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800817 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
818 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700819 }
820 setTransactionFlags(eDisplayTransactionNeeded);
821
Andy McFadden9e9689c2012-10-10 18:17:51 -0700822 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700823 }
Mathias Agopian86303202012-07-24 22:46:10 -0700824}
825
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700826void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700827 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700828 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700829}
830
Mathias Agopian4fec8732012-06-29 14:12:52 -0700831void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800832 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800833 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700834 case MessageQueue::TRANSACTION:
835 handleMessageTransaction();
836 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700837 case MessageQueue::INVALIDATE:
838 handleMessageTransaction();
839 handleMessageInvalidate();
840 signalRefresh();
841 break;
842 case MessageQueue::REFRESH:
843 handleMessageRefresh();
844 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800845 }
846}
847
Mathias Agopian4fec8732012-06-29 14:12:52 -0700848void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700849 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700850 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700851 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700852 }
853}
854
855void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700856 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700857 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700858}
859
860void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700861 ATRACE_CALL();
862 preComposition();
863 rebuildLayerStacks();
864 setUpHWComposer();
865 doDebugFlashRegions();
866 doComposition();
867 postComposition();
868}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700869
Mathias Agopiancd60f992012-08-16 16:28:27 -0700870void SurfaceFlinger::doDebugFlashRegions()
871{
872 // is debugging enabled
873 if (CC_LIKELY(!mDebugRegion))
874 return;
875
876 const bool repaintEverything = mRepaintEverything;
877 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
878 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700879 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700880 // transform the dirty region into this screen's coordinate space
881 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
882 if (!dirtyRegion.isEmpty()) {
883 // redraw the whole screen
884 doComposeSurfaces(hw, Region(hw->bounds()));
885
886 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700887 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700888 RenderEngine& engine(getRenderEngine());
889 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
890
Mathias Agopiancd60f992012-08-16 16:28:27 -0700891 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700892 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700893 }
894 }
895 }
896
897 postFramebuffer();
898
899 if (mDebugRegion > 1) {
900 usleep(mDebugRegion * 1000);
901 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700902
903 HWComposer& hwc(getHwComposer());
904 if (hwc.initCheck() == NO_ERROR) {
905 status_t err = hwc.prepare();
906 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
907 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700908}
909
910void SurfaceFlinger::preComposition()
911{
912 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700913 const LayerVector& layers(mDrawingState.layersSortedByZ);
914 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700915 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700916 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700917 needExtraInvalidate = true;
918 }
919 }
920 if (needExtraInvalidate) {
921 signalLayerUpdate();
922 }
923}
924
925void SurfaceFlinger::postComposition()
926{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700927 const LayerVector& layers(mDrawingState.layersSortedByZ);
928 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700929 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700930 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700931 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800932
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700933 const HWComposer& hwc = getHwComposer();
934 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
935
936 if (presentFence->isValid()) {
937 if (mPrimaryDispSync.addPresentFence(presentFence)) {
938 enableHardwareVsync();
939 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700940 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700941 }
942 }
943
Andy McFadden5167ec62014-05-22 13:08:43 -0700944 if (kIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700945 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700946 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700947 enableHardwareVsync();
948 }
949 }
950
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800951 if (mAnimCompositionPending) {
952 mAnimCompositionPending = false;
953
Jesse Halla9a1b002013-02-27 16:39:25 -0800954 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800955 mAnimFrameTracker.setActualPresentFence(presentFence);
956 } else {
957 // The HWC doesn't support present fences, so use the refresh
958 // timestamp instead.
959 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
960 mAnimFrameTracker.setActualPresentTime(presentTime);
961 }
962 mAnimFrameTracker.advanceFrame();
963 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700964}
965
966void SurfaceFlinger::rebuildLayerStacks() {
967 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700968 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700969 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700970 mVisibleRegionsDirty = false;
971 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700972
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700973 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700974 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700975 Region opaqueRegion;
976 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800977 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700978 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700979 const Transform& tr(hw->getTransform());
980 const Rect bounds(hw->getBounds());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700981 if (hw->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700982 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700983 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700984
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700985 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700986 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700987 const sp<Layer>& layer(layers[i]);
988 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700989 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700990 Region drawRegion(tr.transform(
991 layer->visibleNonTransparentRegion));
992 drawRegion.andSelf(bounds);
993 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700994 layersSortedByZ.add(layer);
995 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700996 }
997 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700998 }
Mathias Agopian42977342012-08-05 00:40:46 -0700999 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001000 hw->undefinedRegion.set(bounds);
1001 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
1002 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001003 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001004 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001005}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001006
Mathias Agopiancd60f992012-08-16 16:28:27 -07001007void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001008 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001009 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1010 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1011 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1012
1013 // If nothing has changed (!dirty), don't recompose.
1014 // If something changed, but we don't currently have any visible layers,
1015 // and didn't when we last did a composition, then skip it this time.
1016 // The second rule does two things:
1017 // - When all layers are removed from a display, we'll emit one black
1018 // frame, then nothing more until we get new layers.
1019 // - When a display is created with a private layer stack, we won't
1020 // emit any black frames until a layer is added to the layer stack.
1021 bool mustRecompose = dirty && !(empty && wasEmpty);
1022
1023 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1024 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1025 mustRecompose ? "doing" : "skipping",
1026 dirty ? "+" : "-",
1027 empty ? "+" : "-",
1028 wasEmpty ? "+" : "-");
1029
Dan Stoza71433162014-02-04 16:22:36 -08001030 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001031
1032 if (mustRecompose) {
1033 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1034 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001035 }
1036
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001037 HWComposer& hwc(getHwComposer());
1038 if (hwc.initCheck() == NO_ERROR) {
1039 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001040 if (CC_UNLIKELY(mHwWorkListDirty)) {
1041 mHwWorkListDirty = false;
1042 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1043 sp<const DisplayDevice> hw(mDisplays[dpy]);
1044 const int32_t id = hw->getHwcDisplayId();
1045 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001046 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001047 hw->getVisibleLayersSortedByZ());
1048 const size_t count = currentLayers.size();
1049 if (hwc.createWorkList(id, count) == NO_ERROR) {
1050 HWComposer::LayerListIterator cur = hwc.begin(id);
1051 const HWComposer::LayerListIterator end = hwc.end(id);
1052 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001053 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001054 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001055 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001056 cur->setSkip(true);
1057 }
1058 }
1059 }
1060 }
1061 }
1062 }
1063
1064 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001065 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001066 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001067 const int32_t id = hw->getHwcDisplayId();
1068 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001069 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001070 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001071 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001072 HWComposer::LayerListIterator cur = hwc.begin(id);
1073 const HWComposer::LayerListIterator end = hwc.end(id);
1074 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1075 /*
1076 * update the per-frame h/w composer data for each layer
1077 * and build the transparent region of the FB
1078 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001079 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001080 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -07001081 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001082 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001083 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001084
Riley Andrews03414a12014-07-01 14:22:59 -07001085 // If possible, attempt to use the cursor overlay on each display.
1086 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1087 sp<const DisplayDevice> hw(mDisplays[dpy]);
1088 const int32_t id = hw->getHwcDisplayId();
1089 if (id >= 0) {
1090 const Vector< sp<Layer> >& currentLayers(
1091 hw->getVisibleLayersSortedByZ());
1092 const size_t count = currentLayers.size();
1093 HWComposer::LayerListIterator cur = hwc.begin(id);
1094 const HWComposer::LayerListIterator end = hwc.end(id);
1095 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1096 const sp<Layer>& layer(currentLayers[i]);
1097 if (layer->isPotentialCursor()) {
1098 cur->setIsCursorLayerHint();
1099 break;
1100 }
1101 }
1102 }
1103 }
1104
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001105 status_t err = hwc.prepare();
1106 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001107
1108 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1109 sp<const DisplayDevice> hw(mDisplays[dpy]);
1110 hw->prepareFrame(hwc);
1111 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001112 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001113}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001114
Mathias Agopiancd60f992012-08-16 16:28:27 -07001115void SurfaceFlinger::doComposition() {
1116 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001117 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001118 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001119 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001120 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001121 // transform the dirty region into this screen's coordinate space
1122 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001123
1124 // repaint the framebuffer (if needed)
1125 doDisplayComposition(hw, dirtyRegion);
1126
Mathias Agopiancd60f992012-08-16 16:28:27 -07001127 hw->dirtyRegion.clear();
1128 hw->flip(hw->swapRegion);
1129 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001130 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001131 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001132 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001133 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001134 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001135}
1136
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001137void SurfaceFlinger::postFramebuffer()
1138{
Mathias Agopian841cde52012-03-01 15:44:37 -08001139 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001140
Mathias Agopiana44b0412011-10-16 18:46:35 -07001141 const nsecs_t now = systemTime();
1142 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001143
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001144 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001145 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001146 if (!hwc.supportsFramebufferTarget()) {
1147 // EGL spec says:
1148 // "surface must be bound to the calling thread's current context,
1149 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001150 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001151 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001152 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001153 }
1154
Mathias Agopian6da15f42013-09-25 20:40:07 -07001155 // make the default display current because the VirtualDisplayDevice code cannot
1156 // deal with dequeueBuffer() being called outside of the composition loop; however
1157 // the code below can call glFlush() which is allowed (and does in some case) call
1158 // dequeueBuffer().
1159 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1160
Mathias Agopian92a979a2012-08-02 18:32:23 -07001161 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001162 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001163 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001164 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001165 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001166 int32_t id = hw->getHwcDisplayId();
1167 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001168 HWComposer::LayerListIterator cur = hwc.begin(id);
1169 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001170 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001171 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001172 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001173 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001174 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001175 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001176 }
Jesse Hallef194142012-06-14 14:45:17 -07001177 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001178 }
1179
Mathias Agopiana44b0412011-10-16 18:46:35 -07001180 mLastSwapBufferTime = systemTime() - now;
1181 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001182
1183 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1184 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1185 logFrameStats();
1186 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187}
1188
Mathias Agopian87baae12012-07-31 12:38:26 -07001189void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001190{
Mathias Agopian841cde52012-03-01 15:44:37 -08001191 ATRACE_CALL();
1192
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001193 // here we keep a copy of the drawing state (that is the state that's
1194 // going to be overwritten by handleTransactionLocked()) outside of
1195 // mStateLock so that the side-effects of the State assignment
1196 // don't happen with mStateLock held (which can cause deadlocks).
1197 State drawingState(mDrawingState);
1198
Mathias Agopianca4d3602011-05-19 15:38:14 -07001199 Mutex::Autolock _l(mStateLock);
1200 const nsecs_t now = systemTime();
1201 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001202
Mathias Agopianca4d3602011-05-19 15:38:14 -07001203 // Here we're guaranteed that some transaction flags are set
1204 // so we can call handleTransactionLocked() unconditionally.
1205 // We call getTransactionFlags(), which will also clear the flags,
1206 // with mStateLock held to guarantee that mCurrentState won't change
1207 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001208
Mathias Agopiane57f2922012-08-09 16:29:12 -07001209 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001210 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001211
Mathias Agopianca4d3602011-05-19 15:38:14 -07001212 mLastTransactionTime = systemTime() - now;
1213 mDebugInTransaction = 0;
1214 invalidateHwcGeometry();
1215 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001216}
1217
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301218void SurfaceFlinger::setVirtualDisplayData(
1219 int32_t hwcDisplayId,
1220 const sp<IGraphicBufferProducer>& sink)
1221{
1222 sp<ANativeWindow> mNativeWindow = new Surface(sink);
1223 ANativeWindow* const window = mNativeWindow.get();
1224
1225 int format;
1226 window->query(window, NATIVE_WINDOW_FORMAT, &format);
1227
1228 EGLSurface surface;
1229 EGLint w, h;
1230 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1231 surface = eglCreateWindowSurface(display, mRenderEngine->getEGLConfig(), window, NULL);
1232 eglQuerySurface(display, surface, EGL_WIDTH, &w);
1233 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
1234
1235 mHwc->setVirtualDisplayProperties(hwcDisplayId, w, h, format);
1236}
1237
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001238void SurfaceFlinger::configureVirtualDisplay(int32_t &hwcDisplayId,
1239 sp<DisplaySurface> &dispSurface,
1240 sp<IGraphicBufferProducer> &producer,
1241 const DisplayDeviceState state,
1242 sp<IGraphicBufferProducer> bqProducer,
1243 sp<IGraphicBufferConsumer> bqConsumer)
1244{
1245 bool vdsEnabled = mHwc->isVDSEnabled();
1246
1247 //for V4L2 based virtual display implementation
1248 if(!vdsEnabled) {
1249 // persist.sys.wfd.virtual will be set if WFD is launched via
1250 // settings app. This is currently being done in
1251 // ExtendedRemoteDisplay-WFD stack.
1252 // This flag will be reset at the time of disconnection of virtual WFD
1253 // display.
1254 // This flag is set to zero if WFD is launched via QCOM WFD
1255 // proprietary APIs which use HDMI piggyback approach.
1256 char value[PROPERTY_VALUE_MAX];
1257 property_get("persist.sys.wfd.virtual", value, "0");
1258 int wfdVirtual = atoi(value);
1259 if(!wfdVirtual) {
1260 // This is for non-wfd virtual display scenarios(e.g. SSD/SR/CTS)
1261 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001262 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1263 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001264 dispSurface = vds;
1265 // There won't be any interaction with HWC for this virtual display.
1266 // so the GLES driver can pass buffers directly to the sink.
1267 producer = state.surface;
1268 } else {
1269 hwcDisplayId = allocateHwcDisplayId(state.type);
1270 if (hwcDisplayId >= 0) {
1271 // This is for WFD virtual display scenario.
1272 // Read virtual display properties and create a
1273 // rendering surface for it inorder to be handled by hwc.
1274 setVirtualDisplayData(hwcDisplayId, state.surface);
1275 dispSurface = new FramebufferSurface(*mHwc, state.type,
1276 bqConsumer);
1277 producer = bqProducer;
1278 } else {
1279 // in case of WFD Virtual + SSD/SR concurrency scenario,
1280 // WFD virtual display instance gets valid hwcDisplayId and
1281 // SSD/SR will get invalid hwcDisplayId
1282 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001283 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1284 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001285 dispSurface = vds;
1286 // There won't be any interaction with HWC for this virtual
1287 // display, so the GLES driver can pass buffers directly to the
1288 // sink.
1289 producer = state.surface;
1290 }
1291 }
1292 } else {
1293 // VDS solution is enabled
1294 // HWC is allocated for first virtual display.
1295 // Subsequent virtual display sessions will be composed by GLES driver.
1296 // ToDo: Modify VDS component to allocate hwcDisplayId based on
1297 // mForceHwcCopy (which is based on Usage Flags)
1298
1299 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001300 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1301 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001302 dispSurface = vds;
1303 if (hwcDisplayId >= 0) {
1304 producer = vds;
1305 } else {
1306 // There won't be any interaction with HWC for this virtual display,
1307 // so the GLES driver can pass buffers directly to the sink.
1308 producer = state.surface;
1309 }
1310 }
1311}
1312
Mathias Agopian87baae12012-07-31 12:38:26 -07001313void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001314{
1315 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001316 const size_t count = currentLayers.size();
1317
1318 /*
1319 * Traversal of the children
1320 * (perform the transaction for each of them if needed)
1321 */
1322
Mathias Agopian3559b072012-08-15 13:46:03 -07001323 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001325 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001326 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1327 if (!trFlags) continue;
1328
1329 const uint32_t flags = layer->doTransaction(0);
1330 if (flags & Layer::eVisibleRegion)
1331 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001332 }
1333 }
1334
1335 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001336 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001337 */
1338
Mathias Agopiane57f2922012-08-09 16:29:12 -07001339 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001340 // here we take advantage of Vector's copy-on-write semantics to
1341 // improve performance by skipping the transaction entirely when
1342 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001343 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1344 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001345 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001346 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001347 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001348 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001349
1350 // find the displays that were removed
1351 // (ie: in drawing state but not in current state)
1352 // also handle displays that changed
1353 // (ie: displays that are in both lists)
1354 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001355 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1356 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001357 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001358 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001359 // Call makeCurrent() on the primary display so we can
1360 // be sure that nothing associated with this display
1361 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001362 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001363 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001364 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1365 if (hw != NULL)
1366 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001367 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001368 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001369 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001370 } else {
1371 ALOGW("trying to remove the main display");
1372 }
1373 } else {
1374 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001375 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001376 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001377 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001378 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001379 // recreating the DisplayDevice, so we just remove it
1380 // from the drawing state, so that it get re-added
1381 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001382 sp<DisplayDevice> hw(getDisplayDevice(display));
1383 if (hw != NULL)
1384 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001385 mDisplays.removeItem(display);
1386 mDrawingState.displays.removeItemsAt(i);
1387 dc--; i--;
1388 // at this point we must loop to the next item
1389 continue;
1390 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001391
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001392 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001393 if (disp != NULL) {
1394 if (state.layerStack != draw[i].layerStack) {
1395 disp->setLayerStack(state.layerStack);
1396 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001397 if ((state.orientation != draw[i].orientation)
1398 || (state.viewport != draw[i].viewport)
1399 || (state.frame != draw[i].frame))
1400 {
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001401#ifdef QCOM_BSP
1402 int orient = state.orientation;
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001403 // Honor the orientation change after boot
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301404 // animation completes and make sure boot
1405 // animation is shown in panel orientation always.
1406 if(mBootFinished){
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001407 disp->setProjection(state.orientation,
1408 state.viewport, state.frame);
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001409 orient = state.orientation;
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001410 }
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301411 else{
1412 char property[PROPERTY_VALUE_MAX];
1413 int panelOrientation =
1414 DisplayState::eOrientationDefault;
1415 if(property_get("persist.panel.orientation",
1416 property, "0") > 0){
1417 panelOrientation = atoi(property) / 90;
1418 }
1419 disp->setProjection(panelOrientation,
1420 state.viewport, state.frame);
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001421 orient = panelOrientation;
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301422 }
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001423 // Set the view frame of each display only of its
1424 // default orientation.
Saurabh Shah027b3852014-08-21 16:09:32 -07001425 if(orient == DisplayState::eOrientationDefault and
1426 state.frame.isValid()) {
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001427 qdutils::setViewFrame(disp->getHwcDisplayId(),
1428 state.frame.left, state.frame.top,
1429 state.frame.right, state.frame.bottom);
1430 }
1431#else
1432 disp->setProjection(state.orientation,
1433 state.viewport, state.frame);
1434#endif
Mathias Agopian93997a82012-08-29 17:30:36 -07001435 }
Michael Lentine47e45402014-07-18 15:34:25 -07001436 if (state.width != draw[i].width || state.height != draw[i].height) {
1437 disp->setDisplaySize(state.width, state.height);
1438 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001439 }
1440 }
1441 }
1442
1443 // find displays that were added
1444 // (ie: in current state but not in drawing state)
1445 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001446 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001447 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001448
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001449 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001450 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001451 sp<IGraphicBufferProducer> bqProducer;
1452 sp<IGraphicBufferConsumer> bqConsumer;
1453 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1454 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001455
Jesse Hall02d86562013-03-25 14:43:23 -07001456 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001457 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001458 // Virtual displays without a surface are dormant:
1459 // they have external state (layer stack, projection,
1460 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001461 if (state.surface != NULL) {
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001462 configureVirtualDisplay(hwcDisplayId,
1463 dispSurface, producer, state, bqProducer,
1464 bqConsumer);
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001465 }
1466 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001467 ALOGE_IF(state.surface!=NULL,
1468 "adding a supported display, but rendering "
1469 "surface is provided (%p), ignoring it",
1470 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001471 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001472 // for supported (by hwc) displays we provide our
1473 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001474 dispSurface = new FramebufferSurface(*mHwc, state.type,
1475 bqConsumer);
1476 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001477 }
1478
1479 const wp<IBinder>& display(curr.keyAt(i));
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301480 if (dispSurface != NULL && producer != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001481 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001482 state.type, hwcDisplayId,
1483 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001484 display, dispSurface, producer,
1485 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001486 hw->setLayerStack(state.layerStack);
1487 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001488 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001489 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001490 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001491 if (state.isVirtualDisplay()) {
1492 if (hwcDisplayId >= 0) {
1493 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1494 hw->getWidth(), hw->getHeight(),
1495 hw->getFormat());
1496 }
1497 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001498 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001499 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001500 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001501 }
1502 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001503 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001504 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001505
Mathias Agopian84300952012-11-21 16:02:13 -08001506 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1507 // The transform hint might have changed for some layers
1508 // (either because a display has changed, or because a layer
1509 // as changed).
1510 //
1511 // Walk through all the layers in currentLayers,
1512 // and update their transform hint.
1513 //
1514 // If a layer is visible only on a single display, then that
1515 // display is used to calculate the hint, otherwise we use the
1516 // default display.
1517 //
1518 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1519 // the hint is set before we acquire a buffer from the surface texture.
1520 //
1521 // NOTE: layer transactions have taken place already, so we use their
1522 // drawing state. However, SurfaceFlinger's own transaction has not
1523 // happened yet, so we must use the current state layer list
1524 // (soon to become the drawing state list).
1525 //
1526 sp<const DisplayDevice> disp;
1527 uint32_t currentlayerStack = 0;
1528 for (size_t i=0; i<count; i++) {
1529 // NOTE: we rely on the fact that layers are sorted by
1530 // layerStack first (so we don't have to traverse the list
1531 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001532 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001533 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001534 if (i==0 || currentlayerStack != layerStack) {
1535 currentlayerStack = layerStack;
1536 // figure out if this layerstack is mirrored
1537 // (more than one display) if so, pick the default display,
1538 // if not, pick the only display it's on.
1539 disp.clear();
1540 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1541 sp<const DisplayDevice> hw(mDisplays[dpy]);
1542 if (hw->getLayerStack() == currentlayerStack) {
1543 if (disp == NULL) {
1544 disp = hw;
1545 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001546 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001547 break;
1548 }
1549 }
1550 }
1551 }
Chet Haase91d25932013-04-11 15:24:55 -07001552 if (disp == NULL) {
1553 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1554 // redraw after transform hint changes. See bug 8508397.
1555
1556 // could be null when this layer is using a layerStack
1557 // that is not visible on any display. Also can occur at
1558 // screen off/on times.
1559 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001560 }
Chet Haase91d25932013-04-11 15:24:55 -07001561 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001562 }
1563 }
1564
1565
Mathias Agopian3559b072012-08-15 13:46:03 -07001566 /*
1567 * Perform our own transaction if needed
1568 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001569
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001570 const LayerVector& layers(mDrawingState.layersSortedByZ);
1571 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001572 // layers have been added
1573 mVisibleRegionsDirty = true;
1574 }
1575
1576 // some layers might have been removed, so
1577 // we need to update the regions they're exposing.
1578 if (mLayersRemoved) {
1579 mLayersRemoved = false;
1580 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001581 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001582 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001583 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001584 if (currentLayers.indexOf(layer) < 0) {
1585 // this layer is not visible anymore
1586 // TODO: we could traverse the tree from front to back and
1587 // compute the actual visible region
1588 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001589 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001590 Region visibleReg = s.transform.transform(
1591 Region(Rect(s.active.w, s.active.h)));
1592 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001593 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001594 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001595 }
1596
1597 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001598
1599 updateCursorAsync();
1600}
1601
1602void SurfaceFlinger::updateCursorAsync()
1603{
1604 HWComposer& hwc(getHwComposer());
1605 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1606 sp<const DisplayDevice> hw(mDisplays[dpy]);
1607 const int32_t id = hw->getHwcDisplayId();
1608 if (id < 0) {
1609 continue;
1610 }
1611 const Vector< sp<Layer> >& currentLayers(
1612 hw->getVisibleLayersSortedByZ());
1613 const size_t count = currentLayers.size();
1614 HWComposer::LayerListIterator cur = hwc.begin(id);
1615 const HWComposer::LayerListIterator end = hwc.end(id);
1616 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1617 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1618 continue;
1619 }
1620 const sp<Layer>& layer(currentLayers[i]);
1621 Rect cursorPos = layer->getPosition(hw);
1622 hwc.setCursorPositionAsync(id, cursorPos);
1623 break;
1624 }
1625 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001626}
1627
1628void SurfaceFlinger::commitTransaction()
1629{
1630 if (!mLayersPendingRemoval.isEmpty()) {
1631 // Notify removed layers now that they can't be drawn from
1632 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1633 mLayersPendingRemoval[i]->onRemoved();
1634 }
1635 mLayersPendingRemoval.clear();
1636 }
1637
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001638 // If this transaction is part of a window animation then the next frame
1639 // we composite should be considered an animation as well.
1640 mAnimCompositionPending = mAnimTransactionPending;
1641
Mathias Agopian4fec8732012-06-29 14:12:52 -07001642 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001643 mTransactionPending = false;
1644 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001645 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001646}
1647
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001648void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001649 const LayerVector& currentLayers, uint32_t layerStack,
1650 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001651{
Mathias Agopian841cde52012-03-01 15:44:37 -08001652 ATRACE_CALL();
1653
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654 Region aboveOpaqueLayers;
1655 Region aboveCoveredLayers;
1656 Region dirty;
1657
Mathias Agopian87baae12012-07-31 12:38:26 -07001658 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001659
1660 size_t i = currentLayers.size();
1661 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001662 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663
1664 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001665 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001666
Jesse Hall01e29052013-02-19 16:13:35 -08001667 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001668 if (s.layerStack != layerStack)
1669 continue;
1670
Mathias Agopianab028732010-03-16 16:41:46 -07001671 /*
1672 * opaqueRegion: area of a surface that is fully opaque.
1673 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001674 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001675
1676 /*
1677 * visibleRegion: area of a surface that is visible on screen
1678 * and not fully transparent. This is essentially the layer's
1679 * footprint minus the opaque regions above it.
1680 * Areas covered by a translucent surface are considered visible.
1681 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001682 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001683
1684 /*
1685 * coveredRegion: area of a surface that is covered by all
1686 * visible regions above it (which includes the translucent areas).
1687 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001688 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001689
Jesse Halla8026d22012-09-25 13:25:04 -07001690 /*
1691 * transparentRegion: area of a surface that is hinted to be completely
1692 * transparent. This is only used to tell when the layer has no visible
1693 * non-transparent regions and can be removed from the layer list. It
1694 * does not affect the visibleRegion of this layer or any layers
1695 * beneath it. The hint may not be correct if apps don't respect the
1696 * SurfaceView restrictions (which, sadly, some don't).
1697 */
1698 Region transparentRegion;
1699
Mathias Agopianab028732010-03-16 16:41:46 -07001700
1701 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001702 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001703 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001704 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001706 if (!visibleRegion.isEmpty()) {
1707 // Remove the transparent area from the visible region
1708 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001709 const Transform tr(s.transform);
1710 if (tr.transformed()) {
1711 if (tr.preserveRects()) {
1712 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001713 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001714 } else {
1715 // transformation too complex, can't do the
1716 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001717 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001718 }
1719 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001720 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001721 }
Mathias Agopianab028732010-03-16 16:41:46 -07001722 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001723
Mathias Agopianab028732010-03-16 16:41:46 -07001724 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001725 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001726 if (s.alpha==255 && !translucent &&
1727 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1728 // the opaque region is the layer's footprint
1729 opaqueRegion = visibleRegion;
1730 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001731 }
1732 }
1733
Mathias Agopianab028732010-03-16 16:41:46 -07001734 // Clip the covered region to the visible region
1735 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1736
1737 // Update aboveCoveredLayers for next (lower) layer
1738 aboveCoveredLayers.orSelf(visibleRegion);
1739
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001740 // subtract the opaque region covered by the layers above us
1741 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001742
1743 // compute this layer's dirty region
1744 if (layer->contentDirty) {
1745 // we need to invalidate the whole region
1746 dirty = visibleRegion;
1747 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001748 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001749 layer->contentDirty = false;
1750 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001751 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001752 * the exposed region consists of two components:
1753 * 1) what's VISIBLE now and was COVERED before
1754 * 2) what's EXPOSED now less what was EXPOSED before
1755 *
1756 * note that (1) is conservative, we start with the whole
1757 * visible region but only keep what used to be covered by
1758 * something -- which mean it may have been exposed.
1759 *
1760 * (2) handles areas that were not covered by anything but got
1761 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001762 */
Mathias Agopianab028732010-03-16 16:41:46 -07001763 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001764 const Region oldVisibleRegion = layer->visibleRegion;
1765 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001766 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1767 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001768 }
1769 dirty.subtractSelf(aboveOpaqueLayers);
1770
1771 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001772 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001773
Mathias Agopianab028732010-03-16 16:41:46 -07001774 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001775 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001776
Jesse Halla8026d22012-09-25 13:25:04 -07001777 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001778 layer->setVisibleRegion(visibleRegion);
1779 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001780 layer->setVisibleNonTransparentRegion(
1781 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001782 }
1783
Mathias Agopian87baae12012-07-31 12:38:26 -07001784 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001785}
1786
Mathias Agopian87baae12012-07-31 12:38:26 -07001787void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1788 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001789 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001790 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1791 if (hw->getLayerStack() == layerStack) {
1792 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001793 }
1794 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001795}
1796
1797void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001798{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001799 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001800
Mathias Agopian4fec8732012-06-29 14:12:52 -07001801 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001802 const LayerVector& layers(mDrawingState.layersSortedByZ);
Eric Penner51c59cd2014-07-28 19:51:58 -07001803
1804 // Store the set of layers that need updates. This set must not change as
1805 // buffers are being latched, as this could result in a deadlock.
1806 // Example: Two producers share the same command stream and:
1807 // 1.) Layer 0 is latched
1808 // 2.) Layer 0 gets a new frame
1809 // 2.) Layer 1 gets a new frame
1810 // 3.) Layer 1 is latched.
1811 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1812 // second frame. But layer 0's second frame could be waiting on display.
1813 Vector<Layer*> layersWithQueuedFrames;
1814 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001815 const sp<Layer>& layer(layers[i]);
Eric Penner51c59cd2014-07-28 19:51:58 -07001816 if (layer->hasQueuedFrame())
1817 layersWithQueuedFrames.push_back(layer.get());
1818 }
1819 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
1820 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07001821 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001822 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001823 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001824 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001825
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001826 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001827}
1828
Mathias Agopianad456f92011-01-13 17:53:01 -08001829void SurfaceFlinger::invalidateHwcGeometry()
1830{
1831 mHwWorkListDirty = true;
1832}
1833
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001834
Mathias Agopiancd60f992012-08-16 16:28:27 -07001835void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001836 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001837{
Dan Stoza71433162014-02-04 16:22:36 -08001838 // We only need to actually compose the display if:
1839 // 1) It is being handled by hardware composer, which may need this to
1840 // keep its virtual display state machine in sync, or
1841 // 2) There is work to be done (the dirty region isn't empty)
1842 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1843 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1844 return;
1845 }
1846
Mathias Agopian87baae12012-07-31 12:38:26 -07001847 Region dirtyRegion(inDirtyRegion);
1848
Mathias Agopianb8a55602009-06-26 19:06:36 -07001849 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001850 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001851
Mathias Agopian42977342012-08-05 00:40:46 -07001852 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001853 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001854 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1855 // takes a rectangle, we must make sure to update that whole
1856 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001857 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001858 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001859 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001860 // We need to redraw the rectangle that will be updated
1861 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001862 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001863 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001864 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001865 } else {
1866 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001867 dirtyRegion.set(hw->bounds());
1868 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001869 }
1870 }
1871
Alan Viverette9c5a3332013-09-12 20:04:35 -07001872 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Michael Lentine3f121fc2014-10-01 11:17:28 -07001873 if (!doComposeSurfaces(hw, dirtyRegion)) return;
Mathias Agopianff2ed702013-09-01 21:36:12 -07001874 } else {
1875 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001876 mat4 colorMatrix = mColorMatrix;
1877 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001878 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001879 }
1880 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001881 doComposeSurfaces(hw, dirtyRegion);
1882 engine.endGroup();
1883 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001884
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001885 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001886 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001887
1888 // swap buffers (presentation)
1889 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001890}
1891
Michael Lentine3f121fc2014-10-01 11:17:28 -07001892bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001893{
Mathias Agopian3f844832013-08-07 21:24:32 -07001894 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001895 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001896 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001897 HWComposer::LayerListIterator cur = hwc.begin(id);
1898 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001899
Jesse Halld05a17f2013-09-30 16:49:30 -07001900 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001901 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001902 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001903 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1904 hw->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07001905 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1906 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
1907 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
1908 }
1909 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08001910 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001911
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001912 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001913 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001914 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001915 // when using overlays, we assume a fully transparent framebuffer
1916 // NOTE: we could reduce how much we need to clear, for instance
1917 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001918 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001919 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001920 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001921 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001922 // we start with the whole screen area
1923 const Region bounds(hw->getBounds());
1924
1925 // we remove the scissor part
1926 // we're left with the letterbox region
1927 // (common case is that letterbox ends-up being empty)
1928 const Region letterbox(bounds.subtract(hw->getScissor()));
1929
1930 // compute the area to clear
1931 Region region(hw->undefinedRegion.merge(letterbox));
1932
1933 // but limit it to the dirty region
1934 region.andSelf(dirty);
1935
Mathias Agopianb9494d52012-04-18 02:28:45 -07001936 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001937 if (!region.isEmpty()) {
Radhika Ranjan Soni35c9ff22013-12-17 19:55:17 +05301938 if (cur != end) {
1939 if (cur->getCompositionType() != HWC_BLIT)
1940 // can happen with SurfaceView
1941 drawWormhole(hw, region);
1942 } else
1943 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001944 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001945 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001946
Mathias Agopian766dc492012-10-30 18:08:06 -07001947 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1948 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001949 // scissor on the main display. It should never be needed
1950 // anyways (though in theory it could since the API allows it).
1951 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001952 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001953 if (scissor != bounds) {
1954 // scissor doesn't match the screen's dimensions, so we
1955 // need to clear everything outside of it and enable
1956 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001957
Mathias Agopianf45c5102012-10-24 16:29:17 -07001958 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001959 const uint32_t height = hw->getHeight();
1960 engine.setScissor(scissor.left, height - scissor.bottom,
1961 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001962 }
1963 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001964 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001965
Mathias Agopian85d751c2012-08-29 16:59:24 -07001966 /*
1967 * and then, render the layers targeted at the framebuffer
1968 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001969
Mathias Agopian13127d82013-03-05 17:47:11 -08001970 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001971 const size_t count = layers.size();
1972 const Transform& tr = hw->getTransform();
1973 if (cur != end) {
1974 // we're using h/w composer
1975 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001976 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001977 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001978 if (!clip.isEmpty()) {
1979 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07001980 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07001981 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001982 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001983 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1984 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001985 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001986 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001987 // never clear the very first layer since we're
1988 // guaranteed the FB is already cleared
1989 layer->clearWithOpenGL(hw, clip);
1990 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001991 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001992 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001993 case HWC_FRAMEBUFFER: {
1994 layer->draw(hw, clip);
1995 break;
1996 }
Arun Kumar K.R5111ff02012-12-19 18:10:46 -08001997 case HWC_BLIT:
1998 //Do nothing
1999 break;
Mathias Agopianda27af92012-09-13 18:17:13 -07002000 case HWC_FRAMEBUFFER_TARGET: {
2001 // this should not happen as the iterator shouldn't
2002 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08002003 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07002004 break;
2005 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07002006 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002007 }
2008 layer->setAcquireFence(hw, *cur);
2009 }
2010 } else {
2011 // we're not using h/w composer
2012 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002013 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002014 const Region clip(dirty.intersect(
2015 tr.transform(layer->visibleRegion)));
2016 if (!clip.isEmpty()) {
2017 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07002018 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002019 }
2020 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002021
2022 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002023 engine.disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07002024 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002025}
2026
Mathias Agopian3f844832013-08-07 21:24:32 -07002027void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07002028 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07002029 RenderEngine& engine(getRenderEngine());
2030 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002031}
2032
Mathias Agopianac9fa422013-02-11 16:40:36 -08002033void SurfaceFlinger::addClientLayer(const sp<Client>& client,
2034 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07002035 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08002036 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07002037{
Mathias Agopian96f08192010-06-02 23:28:45 -07002038 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08002039 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07002040
Mathias Agopian96f08192010-06-02 23:28:45 -07002041 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002042 Mutex::Autolock _l(mStateLock);
2043 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07002044 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07002045}
2046
Mathias Agopian3f844832013-08-07 21:24:32 -07002047status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002048 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08002049 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002050 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07002051 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002052 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07002053 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002054 return NO_ERROR;
2055 }
Mathias Agopian3d579642009-06-04 18:46:21 -07002056 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002057}
2058
Dan Stozac7014012014-02-14 15:03:43 -08002059uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07002060 return android_atomic_release_load(&mTransactionFlags);
2061}
2062
Mathias Agopian3f844832013-08-07 21:24:32 -07002063uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002064 return android_atomic_and(~flags, &mTransactionFlags) & flags;
2065}
2066
Mathias Agopian3f844832013-08-07 21:24:32 -07002067uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002068 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
2069 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002070 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002071 }
2072 return old;
2073}
2074
Mathias Agopian8b33f032012-07-24 20:43:54 -07002075void SurfaceFlinger::setTransactionState(
2076 const Vector<ComposerState>& state,
2077 const Vector<DisplayState>& displays,
2078 uint32_t flags)
2079{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002080 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07002081 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07002082 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002083
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002084 if (flags & eAnimation) {
2085 // For window updates that are part of an animation we must wait for
2086 // previous animation "frames" to be handled.
2087 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002088 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002089 if (CC_UNLIKELY(err != NO_ERROR)) {
2090 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002091 // caller after a few seconds.
2092 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
2093 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002094 mAnimTransactionPending = false;
2095 break;
2096 }
2097 }
2098 }
2099
Mathias Agopiane57f2922012-08-09 16:29:12 -07002100 size_t count = displays.size();
2101 for (size_t i=0 ; i<count ; i++) {
2102 const DisplayState& s(displays[i]);
2103 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002104 }
2105
Mathias Agopiane57f2922012-08-09 16:29:12 -07002106 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002107 for (size_t i=0 ; i<count ; i++) {
2108 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002109 // Here we need to check that the interface we're given is indeed
2110 // one of our own. A malicious client could give us a NULL
2111 // IInterface, or one of its own or even one of our own but a
2112 // different type. All these situations would cause us to crash.
2113 //
2114 // NOTE: it would be better to use RTTI as we could directly check
2115 // that we have a Client*. however, RTTI is disabled in Android.
2116 if (s.client != NULL) {
2117 sp<IBinder> binder = s.client->asBinder();
2118 if (binder != NULL) {
2119 String16 desc(binder->getInterfaceDescriptor());
2120 if (desc == ISurfaceComposerClient::descriptor) {
2121 sp<Client> client( static_cast<Client *>(s.client.get()) );
2122 transactionFlags |= setClientStateLocked(client, s.state);
2123 }
2124 }
2125 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002126 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002127
Mathias Agopian386aa982011-11-07 21:58:03 -08002128 if (transactionFlags) {
2129 // this triggers the transaction
2130 setTransactionFlags(transactionFlags);
2131
2132 // if this is a synchronous transaction, wait for it to take effect
2133 // before returning.
2134 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002135 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002136 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002137 if (flags & eAnimation) {
2138 mAnimTransactionPending = true;
2139 }
2140 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002141 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2142 if (CC_UNLIKELY(err != NO_ERROR)) {
2143 // just in case something goes wrong in SF, return to the
2144 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002145 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2146 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002147 break;
2148 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002149 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002150 }
2151}
2152
Mathias Agopiane57f2922012-08-09 16:29:12 -07002153uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2154{
Jesse Hall9a143922012-10-04 16:29:19 -07002155 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2156 if (dpyIdx < 0)
2157 return 0;
2158
Mathias Agopiane57f2922012-08-09 16:29:12 -07002159 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002160 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002161 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002162 const uint32_t what = s.what;
2163 if (what & DisplayState::eSurfaceChanged) {
2164 if (disp.surface->asBinder() != s.surface->asBinder()) {
2165 disp.surface = s.surface;
2166 flags |= eDisplayTransactionNeeded;
2167 }
2168 }
2169 if (what & DisplayState::eLayerStackChanged) {
2170 if (disp.layerStack != s.layerStack) {
2171 disp.layerStack = s.layerStack;
2172 flags |= eDisplayTransactionNeeded;
2173 }
2174 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002175 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002176 if (disp.orientation != s.orientation) {
2177 disp.orientation = s.orientation;
2178 flags |= eDisplayTransactionNeeded;
2179 }
2180 if (disp.frame != s.frame) {
2181 disp.frame = s.frame;
2182 flags |= eDisplayTransactionNeeded;
2183 }
2184 if (disp.viewport != s.viewport) {
2185 disp.viewport = s.viewport;
2186 flags |= eDisplayTransactionNeeded;
2187 }
2188 }
Michael Lentine47e45402014-07-18 15:34:25 -07002189 if (what & DisplayState::eDisplaySizeChanged) {
2190 if (disp.width != s.width) {
2191 disp.width = s.width;
2192 flags |= eDisplayTransactionNeeded;
2193 }
2194 if (disp.height != s.height) {
2195 disp.height = s.height;
2196 flags |= eDisplayTransactionNeeded;
2197 }
2198 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002199 }
2200 return flags;
2201}
2202
2203uint32_t SurfaceFlinger::setClientStateLocked(
2204 const sp<Client>& client,
2205 const layer_state_t& s)
2206{
2207 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002208 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002209 if (layer != 0) {
2210 const uint32_t what = s.what;
2211 if (what & layer_state_t::ePositionChanged) {
2212 if (layer->setPosition(s.x, s.y))
2213 flags |= eTraversalNeeded;
2214 }
2215 if (what & layer_state_t::eLayerChanged) {
2216 // NOTE: index needs to be calculated before we update the state
2217 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2218 if (layer->setLayer(s.z)) {
2219 mCurrentState.layersSortedByZ.removeAt(idx);
2220 mCurrentState.layersSortedByZ.add(layer);
2221 // we need traversal (state changed)
2222 // AND transaction (list changed)
2223 flags |= eTransactionNeeded|eTraversalNeeded;
2224 }
2225 }
2226 if (what & layer_state_t::eSizeChanged) {
2227 if (layer->setSize(s.w, s.h)) {
2228 flags |= eTraversalNeeded;
2229 }
2230 }
2231 if (what & layer_state_t::eAlphaChanged) {
2232 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2233 flags |= eTraversalNeeded;
2234 }
2235 if (what & layer_state_t::eMatrixChanged) {
2236 if (layer->setMatrix(s.matrix))
2237 flags |= eTraversalNeeded;
2238 }
2239 if (what & layer_state_t::eTransparentRegionChanged) {
2240 if (layer->setTransparentRegionHint(s.transparentRegion))
2241 flags |= eTraversalNeeded;
2242 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08002243 if ((what & layer_state_t::eVisibilityChanged) ||
2244 (what & layer_state_t::eOpacityChanged)) {
2245 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07002246 if (layer->setFlags(s.flags, s.mask))
2247 flags |= eTraversalNeeded;
2248 }
2249 if (what & layer_state_t::eCropChanged) {
2250 if (layer->setCrop(s.crop))
2251 flags |= eTraversalNeeded;
2252 }
2253 if (what & layer_state_t::eLayerStackChanged) {
2254 // NOTE: index needs to be calculated before we update the state
2255 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2256 if (layer->setLayerStack(s.layerStack)) {
2257 mCurrentState.layersSortedByZ.removeAt(idx);
2258 mCurrentState.layersSortedByZ.add(layer);
2259 // we need traversal (state changed)
2260 // AND transaction (list changed)
2261 flags |= eTransactionNeeded|eTraversalNeeded;
2262 }
2263 }
2264 }
2265 return flags;
2266}
2267
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002268status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002269 const String8& name,
2270 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002271 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2272 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002273{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002274 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002275 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002276 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002277 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002278 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002279 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002280
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002281 status_t result = NO_ERROR;
2282
2283 sp<Layer> layer;
2284
Mathias Agopian3165cc22012-08-08 19:42:09 -07002285 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2286 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002287 result = createNormalLayer(client,
2288 name, w, h, flags, format,
2289 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002290 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002291 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002292 result = createDimLayer(client,
2293 name, w, h, flags,
2294 handle, gbp, &layer);
2295 break;
2296 default:
2297 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002298 break;
2299 }
2300
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002301 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002302 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002303 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002304 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002305 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002306}
2307
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002308status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2309 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2310 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002311{
2312 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002313 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002314 case PIXEL_FORMAT_TRANSPARENT:
2315 case PIXEL_FORMAT_TRANSLUCENT:
2316 format = PIXEL_FORMAT_RGBA_8888;
2317 break;
2318 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002319 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002320 break;
2321 }
2322
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002323 *outLayer = new Layer(this, client, name, w, h, flags);
2324 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2325 if (err == NO_ERROR) {
2326 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002327 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002328 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002329
2330 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2331 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002332}
2333
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002334status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2335 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2336 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002337{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002338 *outLayer = new LayerDim(this, client, name, w, h, flags);
2339 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002340 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002341 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002342}
2343
Mathias Agopianac9fa422013-02-11 16:40:36 -08002344status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002345{
Mathias Agopian67106042013-03-14 19:18:13 -07002346 // called by the window manager when it wants to remove a Layer
2347 status_t err = NO_ERROR;
2348 sp<Layer> l(client->getLayerUser(handle));
2349 if (l != NULL) {
2350 err = removeLayer(l);
2351 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2352 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002353 }
2354 return err;
2355}
2356
Mathias Agopian13127d82013-03-05 17:47:11 -08002357status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002358{
Mathias Agopian67106042013-03-14 19:18:13 -07002359 // called by ~LayerCleaner() when all references to the IBinder (handle)
2360 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002361 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002362 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002363 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002364 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002365 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002366 "error removing layer=%p (%s)", l.get(), strerror(-err));
2367 }
2368 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002369}
2370
Mathias Agopianb60314a2012-04-10 22:09:54 -07002371// ---------------------------------------------------------------------------
2372
Andy McFadden13a082e2012-08-24 10:16:42 -07002373void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002374 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002375 Vector<ComposerState> state;
2376 Vector<DisplayState> displays;
2377 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002378 d.what = DisplayState::eDisplayProjectionChanged |
2379 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002380 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002381 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002382 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002383 d.frame.makeInvalid();
2384 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002385 d.width = 0;
2386 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002387 displays.add(d);
2388 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002389 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002390
2391 const nsecs_t period =
2392 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2393 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002394}
2395
2396void SurfaceFlinger::initializeDisplays() {
2397 class MessageScreenInitialized : public MessageBase {
2398 SurfaceFlinger* flinger;
2399 public:
2400 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2401 virtual bool handler() {
2402 flinger->onInitializeDisplays();
2403 return true;
2404 }
2405 };
2406 sp<MessageBase> msg = new MessageScreenInitialized(this);
2407 postMessageAsync(msg); // we may be called from main thread, use async message
2408}
2409
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002410void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2411 int mode) {
2412 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2413 this);
2414 int32_t type = hw->getDisplayType();
2415 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002416
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002417 if (mode == currentMode) {
2418 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002419 return;
2420 }
2421
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002422 hw->setPowerMode(mode);
2423 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2424 ALOGW("Trying to set power mode for virtual display");
2425 return;
2426 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002427
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002428 if (currentMode == HWC_POWER_MODE_OFF) {
2429 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002430 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2431 // FIXME: eventthread only knows about the main display right now
2432 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002433 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002434 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002435
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002436 mVisibleRegionsDirty = true;
2437 repaintEverything();
2438 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002439 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002440 disableHardwareVsync(true); // also cancels any in-progress resync
2441
Mathias Agopiancde87a32012-09-13 14:09:01 -07002442 // FIXME: eventthread only knows about the main display right now
2443 mEventThread->onScreenReleased();
2444 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002445
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002446 getHwComposer().setPowerMode(type, mode);
2447 mVisibleRegionsDirty = true;
2448 // from this point on, SF will stop drawing on this display
2449 } else {
2450 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002451 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002452}
2453
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002454void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2455 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002456 SurfaceFlinger& mFlinger;
2457 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002458 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002459 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002460 MessageSetPowerMode(SurfaceFlinger& flinger,
2461 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2462 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002463 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002464 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002465 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002466 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002467 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002468 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002469 ALOGW("Attempt to set power mode = %d for virtual display",
2470 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002471 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002472 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002473 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002474 return true;
2475 }
2476 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002477 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002478 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002479}
2480
2481// ---------------------------------------------------------------------------
2482
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002483status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2484{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002485 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002486
Mathias Agopianbd115332013-04-18 16:41:04 -07002487 IPCThreadState* ipc = IPCThreadState::self();
2488 const int pid = ipc->getCallingPid();
2489 const int uid = ipc->getCallingUid();
2490 if ((uid != AID_SHELL) &&
2491 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002492 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002493 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002494 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002495 // Try to get the main lock, but don't insist if we can't
2496 // (this would indicate SF is stuck, but we want to be able to
2497 // print something in dumpsys).
2498 int retry = 3;
2499 while (mStateLock.tryLock()<0 && --retry>=0) {
2500 usleep(1000000);
2501 }
2502 const bool locked(retry >= 0);
2503 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002504 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002505 "SurfaceFlinger appears to be unresponsive, "
2506 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002507 }
2508
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002509 bool dumpAll = true;
2510 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002511 size_t numArgs = args.size();
2512 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002513 if ((index < numArgs) &&
2514 (args[index] == String16("--list"))) {
2515 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002516 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002517 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002518 }
2519
2520 if ((index < numArgs) &&
2521 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002522 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002523 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002524 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002525 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002526
2527 if ((index < numArgs) &&
2528 (args[index] == String16("--latency-clear"))) {
2529 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002530 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002531 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002532 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002533
2534 if ((index < numArgs) &&
2535 (args[index] == String16("--dispsync"))) {
2536 index++;
2537 mPrimaryDispSync.dump(result);
2538 dumpAll = false;
2539 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002540 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002541
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002542 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002543 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002544 }
2545
Mathias Agopian9795c422009-08-26 16:36:26 -07002546 if (locked) {
2547 mStateLock.unlock();
2548 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002549 }
2550 write(fd, result.string(), result.size());
2551 return NO_ERROR;
2552}
2553
Dan Stozac7014012014-02-14 15:03:43 -08002554void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2555 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002556{
2557 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2558 const size_t count = currentLayers.size();
2559 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002560 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002561 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002562 }
2563}
2564
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002565void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002566 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002567{
2568 String8 name;
2569 if (index < args.size()) {
2570 name = String8(args[index]);
2571 index++;
2572 }
2573
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002574 const nsecs_t period =
2575 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002576 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002577
2578 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002579 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002580 } else {
2581 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2582 const size_t count = currentLayers.size();
2583 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002584 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002585 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002586 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002587 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002588 }
2589 }
2590}
2591
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002592void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002593 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002594{
2595 String8 name;
2596 if (index < args.size()) {
2597 name = String8(args[index]);
2598 index++;
2599 }
2600
2601 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2602 const size_t count = currentLayers.size();
2603 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002604 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002605 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002606 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002607 }
2608 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002609
Svetoslavd85084b2014-03-20 10:28:31 -07002610 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002611}
2612
Jamie Gennis6547ff42013-07-16 20:12:42 -07002613// This should only be called from the main thread. Otherwise it would need
2614// the lock and should use mCurrentState rather than mDrawingState.
2615void SurfaceFlinger::logFrameStats() {
2616 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2617 const size_t count = drawingLayers.size();
2618 for (size_t i=0 ; i<count ; i++) {
2619 const sp<Layer>& layer(drawingLayers[i]);
2620 layer->logFrameStats();
2621 }
2622
2623 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2624}
2625
Andy McFadden4803b742012-09-24 19:07:20 -07002626/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2627{
2628 static const char* config =
2629 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002630#ifdef HAS_CONTEXT_PRIORITY
2631 " HAS_CONTEXT_PRIORITY"
2632#endif
2633#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2634 " NEVER_DEFAULT_TO_ASYNC_MODE"
2635#endif
2636#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2637 " TARGET_DISABLE_TRIPLE_BUFFERING"
2638#endif
2639 "]";
2640 result.append(config);
2641}
2642
Mathias Agopian74d211a2013-04-22 16:55:35 +02002643void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2644 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002645{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002646 bool colorize = false;
2647 if (index < args.size()
2648 && (args[index] == String16("--color"))) {
2649 colorize = true;
2650 index++;
2651 }
2652
2653 Colorizer colorizer(colorize);
2654
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002655 // figure out if we're stuck somewhere
2656 const nsecs_t now = systemTime();
2657 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2658 const nsecs_t inTransaction(mDebugInTransaction);
2659 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2660 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2661
2662 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002663 * Dump library configuration.
2664 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002665
2666 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002667 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002668 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002669 appendSfConfigString(result);
2670 appendUiConfigString(result);
2671 appendGuiConfigString(result);
2672 result.append("\n");
2673
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002674 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002675 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002676 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002677 result.append(SyncFeatures::getInstance().toString());
2678 result.append("\n");
2679
Andy McFadden41d67d72014-04-25 16:58:34 -07002680 colorizer.bold(result);
2681 result.append("DispSync configuration: ");
2682 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002683 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2684 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002685 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2686 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2687 result.append("\n");
2688
Andy McFadden4803b742012-09-24 19:07:20 -07002689 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002690 * Dump the visible layer list
2691 */
2692 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2693 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002694 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002695 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002696 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002697 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002698 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002699 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002700 }
2701
2702 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002703 * Dump Display state
2704 */
2705
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002706 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002707 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002708 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002709 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2710 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002711 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002712 }
2713
2714 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002715 * Dump SurfaceFlinger global state
2716 */
2717
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002718 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002719 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002720 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002721
Mathias Agopian888c8222012-08-04 21:10:38 -07002722 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002723 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002724
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002725 colorizer.bold(result);
2726 result.appendFormat("EGL implementation : %s\n",
2727 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2728 colorizer.reset(result);
2729 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002730 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002731
Mathias Agopian875d8e12013-06-07 15:35:48 -07002732 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002733
Mathias Agopian42977342012-08-05 00:40:46 -07002734 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002735 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2736 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002737 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002738 " last eglSwapBuffers() time: %f us\n"
2739 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002740 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002741 " refresh-rate : %f fps\n"
2742 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002743 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002744 " gpu_to_cpu_unsupported : %d\n"
2745 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002746 mLastSwapBufferTime/1000.0,
2747 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002748 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002749 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2750 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002751 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002752 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002753
Mathias Agopian74d211a2013-04-22 16:55:35 +02002754 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002755 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002756
Mathias Agopian74d211a2013-04-22 16:55:35 +02002757 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002758 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002759
2760 /*
2761 * VSYNC state
2762 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002763 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002764
2765 /*
2766 * Dump HWComposer state
2767 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002768 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002769 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002770 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002771 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002772 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002773 (mDebugDisableHWC || mDebugRegion || mDaltonize
2774 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002775 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002776
2777 /*
2778 * Dump gralloc state
2779 */
2780 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2781 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002782}
2783
Mathias Agopian13127d82013-03-05 17:47:11 -08002784const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002785SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002786 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002787 wp<IBinder> dpy;
2788 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2789 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2790 dpy = mDisplays.keyAt(i);
2791 break;
2792 }
2793 }
2794 if (dpy == NULL) {
2795 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2796 // Just use the primary display so we have something to return
2797 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2798 }
2799 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002800}
2801
Keun young Park63f165f2012-08-31 10:53:36 -07002802bool SurfaceFlinger::startDdmConnection()
2803{
2804 void* libddmconnection_dso =
2805 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2806 if (!libddmconnection_dso) {
2807 return false;
2808 }
2809 void (*DdmConnection_start)(const char* name);
2810 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07002811 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07002812 if (!DdmConnection_start) {
2813 dlclose(libddmconnection_dso);
2814 return false;
2815 }
2816 (*DdmConnection_start)(getServiceName());
2817 return true;
2818}
2819
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002820status_t SurfaceFlinger::onTransact(
2821 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2822{
2823 switch (code) {
2824 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002825 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002826 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002827 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002828 case CLEAR_ANIMATION_FRAME_STATS:
2829 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002830 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002831 {
2832 // codes that require permission check
2833 IPCThreadState* ipc = IPCThreadState::self();
2834 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002835 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002836 if ((uid != AID_GRAPHICS) &&
2837 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002838 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002839 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2840 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002841 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002842 break;
2843 }
2844 case CAPTURE_SCREEN:
2845 {
2846 // codes that require permission check
2847 IPCThreadState* ipc = IPCThreadState::self();
2848 const int pid = ipc->getCallingPid();
2849 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002850 if ((uid != AID_GRAPHICS) &&
2851 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002852 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002853 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2854 return PERMISSION_DENIED;
2855 }
2856 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002857 }
2858 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002859
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002860 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2861 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002862 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002863 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002864 IPCThreadState* ipc = IPCThreadState::self();
2865 const int pid = ipc->getCallingPid();
2866 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002867 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002868 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002869 return PERMISSION_DENIED;
2870 }
2871 int n;
2872 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002873 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002874 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002875 return NO_ERROR;
2876 case 1002: // SHOW_UPDATES
2877 n = data.readInt32();
2878 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002879 invalidateHwcGeometry();
2880 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002881 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002882 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002883 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002884 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002885 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002886 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002887 setTransactionFlags(
2888 eTransactionNeeded|
2889 eDisplayTransactionNeeded|
2890 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002891 return NO_ERROR;
2892 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002893 case 1006:{ // send empty update
2894 signalRefresh();
2895 return NO_ERROR;
2896 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002897 case 1008: // toggle use of hw composer
2898 n = data.readInt32();
2899 mDebugDisableHWC = n ? 1 : 0;
2900 invalidateHwcGeometry();
2901 repaintEverything();
2902 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002903 case 1009: // toggle use of transform hint
2904 n = data.readInt32();
2905 mDebugDisableTransformHint = n ? 1 : 0;
2906 invalidateHwcGeometry();
2907 repaintEverything();
2908 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002909 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002910 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002911 reply->writeInt32(0);
2912 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002913 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002914 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002915 return NO_ERROR;
2916 case 1013: {
2917 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002918 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2919 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002920 return NO_ERROR;
2921 }
2922 case 1014: {
2923 // daltonize
2924 n = data.readInt32();
2925 switch (n % 10) {
2926 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2927 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2928 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2929 }
2930 if (n >= 10) {
2931 mDaltonizer.setMode(Daltonizer::correction);
2932 } else {
2933 mDaltonizer.setMode(Daltonizer::simulation);
2934 }
2935 mDaltonize = n > 0;
2936 invalidateHwcGeometry();
2937 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002938 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002939 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002940 case 1015: {
2941 // apply a color matrix
2942 n = data.readInt32();
2943 mHasColorMatrix = n ? 1 : 0;
2944 if (n) {
2945 // color matrix is sent as mat3 matrix followed by vec3
2946 // offset, then packed into a mat4 where the last row is
2947 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002948 for (size_t i = 0 ; i < 4; i++) {
2949 for (size_t j = 0; j < 4; j++) {
2950 mColorMatrix[i][j] = data.readFloat();
2951 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002952 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002953 } else {
2954 mColorMatrix = mat4();
2955 }
2956 invalidateHwcGeometry();
2957 repaintEverything();
2958 return NO_ERROR;
2959 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002960 // This is an experimental interface
2961 // Needs to be shifted to proper binder interface when we productize
2962 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07002963 n = data.readInt32();
2964 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002965 return NO_ERROR;
2966 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002967 }
2968 }
2969 return err;
2970}
2971
Mathias Agopian53331da2011-08-22 21:44:41 -07002972void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002973 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002974 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002975}
2976
Mathias Agopian59119e62010-10-11 12:37:43 -07002977// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002978// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002979// ---------------------------------------------------------------------------
2980
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002981/* The code below is here to handle b/8734824
2982 *
2983 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07002984 * from the surfaceflinger thread to the calling binder thread, where they
2985 * are executed. This allows the calling thread in the calling process to be
2986 * reused and not depend on having "enough" binder threads to handle the
2987 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002988 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002989class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07002990 /* Parts of GraphicProducerWrapper are run on two different threads,
2991 * communicating by sending messages via Looper but also by shared member
2992 * data. Coherence maintenance is subtle and in places implicit (ugh).
2993 *
2994 * Don't rely on Looper's sendMessage/handleMessage providing
2995 * release/acquire semantics for any data not actually in the Message.
2996 * Data going from surfaceflinger to binder threads needs to be
2997 * synchronized explicitly.
2998 *
2999 * Barrier open/wait do provide release/acquire semantics. This provides
3000 * implicit synchronization for data coming back from binder to
3001 * surfaceflinger threads.
3002 */
3003
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003004 sp<IGraphicBufferProducer> impl;
3005 sp<Looper> looper;
3006 status_t result;
3007 bool exitPending;
3008 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07003009 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003010 uint32_t code;
3011 Parcel const* data;
3012 Parcel* reply;
3013
3014 enum {
3015 MSG_API_CALL,
3016 MSG_EXIT
3017 };
3018
3019 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003020 * Called on surfaceflinger thread. This is called by our "fake"
3021 * BpGraphicBufferProducer. We package the data and reply Parcel and
3022 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003023 */
3024 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08003025 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003026 this->code = code;
3027 this->data = &data;
3028 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003029 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07003030 // if we've exited, we run the message synchronously right here.
3031 // note (JH): as far as I can tell from looking at the code, this
3032 // never actually happens. if it does, i'm not sure if it happens
3033 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003034 handleMessage(Message(MSG_API_CALL));
3035 } else {
3036 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07003037 // Prevent stores to this->{code, data, reply} from being
3038 // reordered later than the construction of Message.
3039 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003040 looper->sendMessage(this, Message(MSG_API_CALL));
3041 barrier.wait();
3042 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08003043 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003044 }
3045
3046 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003047 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003048 * call the real BpGraphicBufferProducer.
3049 */
3050 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07003051 int what = message.what;
3052 // Prevent reads below from happening before the read from Message
3053 atomic_thread_fence(memory_order_acquire);
3054 if (what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08003055 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003056 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07003057 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003058 exitRequested = true;
3059 }
3060 }
3061
3062public:
Jesse Hallb154c422014-07-13 12:47:02 -07003063 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
3064 : impl(impl),
3065 looper(new Looper(true)),
3066 exitPending(false),
3067 exitRequested(false)
3068 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003069
Jesse Hallb154c422014-07-13 12:47:02 -07003070 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003071 status_t waitForResponse() {
3072 do {
3073 looper->pollOnce(-1);
3074 } while (!exitRequested);
3075 return result;
3076 }
3077
Jesse Hallb154c422014-07-13 12:47:02 -07003078 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003079 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07003080 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003081 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07003082 // Ensure this->result is visible to the binder thread before it
3083 // handles the message.
3084 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003085 looper->sendMessage(this, Message(MSG_EXIT));
3086 }
3087};
3088
3089
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003090status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
3091 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003092 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003093 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003094 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003095
3096 if (CC_UNLIKELY(display == 0))
3097 return BAD_VALUE;
3098
3099 if (CC_UNLIKELY(producer == 0))
3100 return BAD_VALUE;
3101
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003102 // if we have secure windows on this display, never allow the screen capture
3103 // unless the producer interface is local (i.e.: we can take a screenshot for
3104 // ourselves).
3105 if (!producer->asBinder()->localBinder()) {
3106 Mutex::Autolock _l(mStateLock);
3107 sp<const DisplayDevice> hw(getDisplayDevice(display));
3108 if (hw->getSecureLayerVisible()) {
3109 ALOGW("FB is protected: PERMISSION_DENIED");
3110 return PERMISSION_DENIED;
3111 }
3112 }
3113
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003114 // Convert to surfaceflinger's internal rotation type.
3115 Transform::orientation_flags rotationFlags;
3116 switch (rotation) {
3117 case ISurfaceComposer::eRotateNone:
3118 rotationFlags = Transform::ROT_0;
3119 break;
3120 case ISurfaceComposer::eRotate90:
3121 rotationFlags = Transform::ROT_90;
3122 break;
3123 case ISurfaceComposer::eRotate180:
3124 rotationFlags = Transform::ROT_180;
3125 break;
3126 case ISurfaceComposer::eRotate270:
3127 rotationFlags = Transform::ROT_270;
3128 break;
3129 default:
3130 rotationFlags = Transform::ROT_0;
3131 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3132 break;
3133 }
3134
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003135 class MessageCaptureScreen : public MessageBase {
3136 SurfaceFlinger* flinger;
3137 sp<IBinder> display;
3138 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003139 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003140 uint32_t reqWidth, reqHeight;
3141 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003142 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003143 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003144 status_t result;
3145 public:
3146 MessageCaptureScreen(SurfaceFlinger* flinger,
3147 const sp<IBinder>& display,
3148 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003149 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003150 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003151 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003152 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003153 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003154 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003155 useIdentityTransform(useIdentityTransform),
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003156 rotation(rotation),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003157 result(PERMISSION_DENIED)
3158 {
3159 }
3160 status_t getResult() const {
3161 return result;
3162 }
3163 virtual bool handler() {
3164 Mutex::Autolock _l(flinger->mStateLock);
3165 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003166 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003167 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003168 useIdentityTransform, rotation);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003169 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003170 return true;
3171 }
3172 };
3173
Mathias Agopian9eb1f052013-04-10 16:27:17 -07003174 // make sure to process transactions before screenshots -- a transaction
3175 // might already be pending but scheduled for VSYNC; this guarantees we
3176 // will handle it before the screenshot. When VSYNC finally arrives
3177 // the scheduled transaction will be a no-op. If no transactions are
3178 // scheduled at this time, this will end-up being a no-op as well.
3179 mEventQueue.invalidateTransactionNow();
3180
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003181 // this creates a "fake" BBinder which will serve as a "fake" remote
3182 // binder to receive the marshaled calls and forward them to the
3183 // real remote (a BpGraphicBufferProducer)
3184 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3185
3186 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3187 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003188 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003189 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003190 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003191 useIdentityTransform, rotationFlags);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003192
3193 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003194 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003195 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003196 }
3197 return res;
3198}
3199
Mathias Agopian180f10d2013-04-10 22:55:41 -07003200
3201void SurfaceFlinger::renderScreenImplLocked(
3202 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003203 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003204 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003205 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003206{
3207 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003208 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003209
3210 // get screen geometry
3211 const uint32_t hw_w = hw->getWidth();
3212 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003213 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
3214
Dan Stozac1879002014-05-22 15:59:05 -07003215 // if a default or invalid sourceCrop is passed in, set reasonable values
3216 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3217 !sourceCrop.isValid()) {
3218 sourceCrop.setLeftTop(Point(0, 0));
3219 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3220 }
3221
3222 // ensure that sourceCrop is inside screen
3223 if (sourceCrop.left < 0) {
3224 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3225 }
Dan Stozabe31f442014-06-11 11:20:54 -07003226 if (sourceCrop.right > hw_w) {
3227 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003228 }
3229 if (sourceCrop.top < 0) {
3230 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3231 }
Dan Stozabe31f442014-06-11 11:20:54 -07003232 if (sourceCrop.bottom > hw_h) {
3233 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003234 }
3235
Mathias Agopian180f10d2013-04-10 22:55:41 -07003236 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003237 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003238
3239 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003240 engine.setViewportAndProjection(
3241 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003242 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003243
3244 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003245 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003246
3247 const LayerVector& layers( mDrawingState.layersSortedByZ );
3248 const size_t count = layers.size();
3249 for (size_t i=0 ; i<count ; ++i) {
3250 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003251 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003252 if (state.layerStack == hw->getLayerStack()) {
3253 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3254 if (layer->isVisible()) {
3255 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08003256 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003257 if (filtering) layer->setFiltering(false);
3258 }
3259 }
3260 }
3261 }
3262
3263 // compositionComplete is needed for older driver
3264 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003265 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003266}
3267
3268
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003269status_t SurfaceFlinger::captureScreenImplLocked(
3270 const sp<const DisplayDevice>& hw,
3271 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003272 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003273 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003274 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003275{
3276 ATRACE_CALL();
3277
Mathias Agopian180f10d2013-04-10 22:55:41 -07003278 // get screen geometry
3279 const uint32_t hw_w = hw->getWidth();
3280 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003281
Mathias Agopian180f10d2013-04-10 22:55:41 -07003282 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3283 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3284 reqWidth, reqHeight, hw_w, hw_h);
3285 return BAD_VALUE;
3286 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003287
Mathias Agopian180f10d2013-04-10 22:55:41 -07003288 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3289 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3290
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003291 // create a surface (because we're a producer, and we need to
3292 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003293 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003294 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003295
3296 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003297 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003298 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3299 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003300
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003301 int err = 0;
3302 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003303 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003304 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3305 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003306
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003307 if (err == NO_ERROR) {
3308 ANativeWindowBuffer* buffer;
3309 /* TODO: Once we have the sync framework everywhere this can use
3310 * server-side waits on the fence that dequeueBuffer returns.
3311 */
3312 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3313 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003314 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003315 // create an EGLImage from the buffer so we can later
3316 // turn it into a texture
3317 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3318 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3319 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003320 // this binds the given EGLImage as a framebuffer for the
3321 // duration of this scope.
3322 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3323 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003324 // this will in fact render into our dequeued buffer
3325 // via an FBO, which means we didn't have to create
3326 // an EGLSurface and therefore we're not
3327 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003328 renderScreenImplLocked(
3329 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3330 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003331
Riley Andrews86639902014-08-15 12:27:24 -07003332 // Attempt to create a sync khr object that can produce a sync point. If that
3333 // isn't available, create a non-dupable sync object in the fallback path and
3334 // wait on it directly.
3335 EGLSyncKHR sync;
3336 if (!DEBUG_SCREENSHOTS) {
3337 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Andy McFadden2d8d1202013-10-09 16:38:02 -07003338 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003339 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003340 }
Riley Andrews86639902014-08-15 12:27:24 -07003341 if (sync != EGL_NO_SYNC_KHR) {
3342 // get the sync fd
3343 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3344 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3345 ALOGW("captureScreen: failed to dup sync khr object");
3346 syncFd = -1;
3347 }
3348 eglDestroySyncKHR(mEGLDisplay, sync);
3349 } else {
3350 // fallback path
3351 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3352 if (sync != EGL_NO_SYNC_KHR) {
3353 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3354 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3355 EGLint eglErr = eglGetError();
3356 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3357 ALOGW("captureScreen: fence wait timed out");
3358 } else {
3359 ALOGW_IF(eglErr != EGL_SUCCESS,
3360 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3361 }
3362 eglDestroySyncKHR(mEGLDisplay, sync);
3363 } else {
3364 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3365 }
3366 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003367 if (DEBUG_SCREENSHOTS) {
3368 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3369 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3370 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3371 hw, minLayerZ, maxLayerZ);
3372 delete [] pixels;
3373 }
3374
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003375 } else {
3376 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3377 result = INVALID_OPERATION;
3378 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003379 // destroy our image
3380 eglDestroyImageKHR(mEGLDisplay, image);
3381 } else {
3382 result = BAD_VALUE;
3383 }
Jesse Hallafe2b1f2014-10-21 11:09:17 -07003384 // queueBuffer takes ownership of syncFd
Riley Andrews86639902014-08-15 12:27:24 -07003385 window->queueBuffer(window, buffer, syncFd);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003386 }
3387 } else {
3388 result = BAD_VALUE;
3389 }
3390 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3391 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003392
Mathias Agopian74c40c02010-09-29 13:02:36 -07003393 return result;
3394}
3395
Mathias Agopiand5556842013-09-19 17:08:37 -07003396void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3397 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003398 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003399 for (size_t y=0 ; y<h ; y++) {
3400 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3401 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003402 if (p[x] != 0xFF000000) return;
3403 }
3404 }
3405 ALOGE("*** we just took a black screenshot ***\n"
3406 "requested minz=%d, maxz=%d, layerStack=%d",
3407 minLayerZ, maxLayerZ, hw->getLayerStack());
3408 const LayerVector& layers( mDrawingState.layersSortedByZ );
3409 const size_t count = layers.size();
3410 for (size_t i=0 ; i<count ; ++i) {
3411 const sp<Layer>& layer(layers[i]);
3412 const Layer::State& state(layer->getDrawingState());
3413 const bool visible = (state.layerStack == hw->getLayerStack())
3414 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3415 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003416 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003417 visible ? '+' : '-',
3418 i, layer->getName().string(), state.layerStack, state.z,
3419 layer->isVisible(), state.flags, state.alpha);
3420 }
3421 }
3422}
3423
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003424// ---------------------------------------------------------------------------
3425
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003426SurfaceFlinger::LayerVector::LayerVector() {
3427}
3428
3429SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003430 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003431}
3432
3433int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3434 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003435{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003436 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003437 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3438 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003439
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003440 uint32_t ls = l->getCurrentState().layerStack;
3441 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003442 if (ls != rs)
3443 return ls - rs;
3444
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003445 uint32_t lz = l->getCurrentState().z;
3446 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003447 if (lz != rz)
3448 return lz - rz;
3449
3450 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003451}
3452
3453// ---------------------------------------------------------------------------
3454
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003455SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Michael Lentine47e45402014-07-18 15:34:25 -07003456 : type(DisplayDevice::DISPLAY_ID_INVALID), width(0), height(0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003457}
3458
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003459SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Michael Lentine47e45402014-07-18 15:34:25 -07003460 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0), width(0), height(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003461 viewport.makeInvalid();
3462 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003463}
3464
3465// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003466
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003467}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003468
3469
3470#if defined(__gl_h_)
3471#error "don't include gl/gl.h in this file"
3472#endif
3473
3474#if defined(__gl2_h_)
3475#error "don't include gl2/gl2.h in this file"
3476#endif