blob: ff8ece6b6fcbcd93980386557de333d461689e72 [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) {
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001069 // Get the layers in the current drawying state
1070 const LayerVector& layers(mDrawingState.layersSortedByZ);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001071#ifdef QCOM_BSP
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001072 bool freezeSurfacePresent = false;
1073 const size_t layerCount = layers.size();
1074 char value[PROPERTY_VALUE_MAX];
1075 property_get("sys.disable_ext_animation", value, "0");
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001076 if(atoi(value) && (id != HWC_DISPLAY_PRIMARY)) {
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001077 for (size_t i = 0 ; i < layerCount ; ++i) {
1078 static int screenShotLen = strlen("ScreenshotSurface");
1079 const sp<Layer>& layer(layers[i]);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001080 const Layer::State& s(layer->getDrawingState());
1081 // check the layers associated with external display
1082 if(s.layerStack == hw->getLayerStack()) {
1083 if(!strncmp(layer->getName(), "ScreenshotSurface",
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001084 screenShotLen)) {
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001085 // Screenshot layer is present, and animation in
1086 // progress
1087 freezeSurfacePresent = true;
1088 break;
1089 }
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001090 }
1091 }
1092 }
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001093#endif
Mathias Agopian13127d82013-03-05 17:47:11 -08001094 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001095 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001096 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001097 HWComposer::LayerListIterator cur = hwc.begin(id);
1098 const HWComposer::LayerListIterator end = hwc.end(id);
1099 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1100 /*
1101 * update the per-frame h/w composer data for each layer
1102 * and build the transparent region of the FB
1103 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001104 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001105 layer->setPerFrameData(hw, *cur);
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001106#ifdef QCOM_BSP
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001107 if(freezeSurfacePresent) {
1108 // if freezeSurfacePresent, set ANIMATING flag
1109 cur->setAnimating(true);
1110 } else {
1111 const KeyedVector<wp<IBinder>, DisplayDeviceState>&
1112 draw(mDrawingState.displays);
1113 size_t dc = draw.size();
1114 for (size_t i=0 ; i<dc ; i++) {
1115 if (draw[i].isMainDisplay()) {
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001116 // Pass the current orientation to HWC
1117 hwc.eventControl(HWC_DISPLAY_PRIMARY,
1118 SurfaceFlinger::EVENT_ORIENTATION,
1119 uint32_t(draw[i].orientation));
Arun Kumar K.R9f6181d2013-02-15 19:44:32 -08001120 }
1121 }
1122 }
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08001123#endif
Mathias Agopian1e260872012-08-08 18:35:12 -07001124 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001125 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001126 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001127
Riley Andrews03414a12014-07-01 14:22:59 -07001128 // If possible, attempt to use the cursor overlay on each display.
1129 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1130 sp<const DisplayDevice> hw(mDisplays[dpy]);
1131 const int32_t id = hw->getHwcDisplayId();
1132 if (id >= 0) {
1133 const Vector< sp<Layer> >& currentLayers(
1134 hw->getVisibleLayersSortedByZ());
1135 const size_t count = currentLayers.size();
1136 HWComposer::LayerListIterator cur = hwc.begin(id);
1137 const HWComposer::LayerListIterator end = hwc.end(id);
1138 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1139 const sp<Layer>& layer(currentLayers[i]);
1140 if (layer->isPotentialCursor()) {
1141 cur->setIsCursorLayerHint();
1142 break;
1143 }
1144 }
1145 }
1146 }
1147
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001148 status_t err = hwc.prepare();
1149 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001150
1151 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1152 sp<const DisplayDevice> hw(mDisplays[dpy]);
1153 hw->prepareFrame(hwc);
1154 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001155 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001156}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001157
Mathias Agopiancd60f992012-08-16 16:28:27 -07001158void SurfaceFlinger::doComposition() {
1159 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001160 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
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 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001163 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001164 // transform the dirty region into this screen's coordinate space
1165 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001166
1167 // repaint the framebuffer (if needed)
1168 doDisplayComposition(hw, dirtyRegion);
1169
Mathias Agopiancd60f992012-08-16 16:28:27 -07001170 hw->dirtyRegion.clear();
1171 hw->flip(hw->swapRegion);
1172 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001173 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001174 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001175 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001176 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001177 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001178}
1179
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001180void SurfaceFlinger::postFramebuffer()
1181{
Mathias Agopian841cde52012-03-01 15:44:37 -08001182 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001183
Mathias Agopiana44b0412011-10-16 18:46:35 -07001184 const nsecs_t now = systemTime();
1185 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001186
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001187 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001188 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001189 if (!hwc.supportsFramebufferTarget()) {
1190 // EGL spec says:
1191 // "surface must be bound to the calling thread's current context,
1192 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001193 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001194 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001195 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001196 }
1197
Mathias Agopian6da15f42013-09-25 20:40:07 -07001198 // make the default display current because the VirtualDisplayDevice code cannot
1199 // deal with dequeueBuffer() being called outside of the composition loop; however
1200 // the code below can call glFlush() which is allowed (and does in some case) call
1201 // dequeueBuffer().
1202 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1203
Mathias Agopian92a979a2012-08-02 18:32:23 -07001204 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001205 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001206 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001207 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001208 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001209 int32_t id = hw->getHwcDisplayId();
1210 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001211 HWComposer::LayerListIterator cur = hwc.begin(id);
1212 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001213 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001214 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001215 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001216 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001217 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001218 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001219 }
Jesse Hallef194142012-06-14 14:45:17 -07001220 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001221 }
1222
Mathias Agopiana44b0412011-10-16 18:46:35 -07001223 mLastSwapBufferTime = systemTime() - now;
1224 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001225
1226 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1227 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1228 logFrameStats();
1229 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001230}
1231
Mathias Agopian87baae12012-07-31 12:38:26 -07001232void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233{
Mathias Agopian841cde52012-03-01 15:44:37 -08001234 ATRACE_CALL();
1235
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001236 // here we keep a copy of the drawing state (that is the state that's
1237 // going to be overwritten by handleTransactionLocked()) outside of
1238 // mStateLock so that the side-effects of the State assignment
1239 // don't happen with mStateLock held (which can cause deadlocks).
1240 State drawingState(mDrawingState);
1241
Mathias Agopianca4d3602011-05-19 15:38:14 -07001242 Mutex::Autolock _l(mStateLock);
1243 const nsecs_t now = systemTime();
1244 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001245
Mathias Agopianca4d3602011-05-19 15:38:14 -07001246 // Here we're guaranteed that some transaction flags are set
1247 // so we can call handleTransactionLocked() unconditionally.
1248 // We call getTransactionFlags(), which will also clear the flags,
1249 // with mStateLock held to guarantee that mCurrentState won't change
1250 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001251
Mathias Agopiane57f2922012-08-09 16:29:12 -07001252 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001253 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001254
Mathias Agopianca4d3602011-05-19 15:38:14 -07001255 mLastTransactionTime = systemTime() - now;
1256 mDebugInTransaction = 0;
1257 invalidateHwcGeometry();
1258 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001259}
1260
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301261void SurfaceFlinger::setVirtualDisplayData(
1262 int32_t hwcDisplayId,
1263 const sp<IGraphicBufferProducer>& sink)
1264{
1265 sp<ANativeWindow> mNativeWindow = new Surface(sink);
1266 ANativeWindow* const window = mNativeWindow.get();
1267
1268 int format;
1269 window->query(window, NATIVE_WINDOW_FORMAT, &format);
1270
1271 EGLSurface surface;
1272 EGLint w, h;
1273 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
1274 surface = eglCreateWindowSurface(display, mRenderEngine->getEGLConfig(), window, NULL);
1275 eglQuerySurface(display, surface, EGL_WIDTH, &w);
1276 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
1277
1278 mHwc->setVirtualDisplayProperties(hwcDisplayId, w, h, format);
1279}
1280
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001281void SurfaceFlinger::configureVirtualDisplay(int32_t &hwcDisplayId,
1282 sp<DisplaySurface> &dispSurface,
1283 sp<IGraphicBufferProducer> &producer,
1284 const DisplayDeviceState state,
1285 sp<IGraphicBufferProducer> bqProducer,
1286 sp<IGraphicBufferConsumer> bqConsumer)
1287{
1288 bool vdsEnabled = mHwc->isVDSEnabled();
1289
1290 //for V4L2 based virtual display implementation
1291 if(!vdsEnabled) {
1292 // persist.sys.wfd.virtual will be set if WFD is launched via
1293 // settings app. This is currently being done in
1294 // ExtendedRemoteDisplay-WFD stack.
1295 // This flag will be reset at the time of disconnection of virtual WFD
1296 // display.
1297 // This flag is set to zero if WFD is launched via QCOM WFD
1298 // proprietary APIs which use HDMI piggyback approach.
1299 char value[PROPERTY_VALUE_MAX];
1300 property_get("persist.sys.wfd.virtual", value, "0");
1301 int wfdVirtual = atoi(value);
1302 if(!wfdVirtual) {
1303 // This is for non-wfd virtual display scenarios(e.g. SSD/SR/CTS)
1304 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001305 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1306 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001307 dispSurface = vds;
1308 // There won't be any interaction with HWC for this virtual display.
1309 // so the GLES driver can pass buffers directly to the sink.
1310 producer = state.surface;
1311 } else {
1312 hwcDisplayId = allocateHwcDisplayId(state.type);
1313 if (hwcDisplayId >= 0) {
1314 // This is for WFD virtual display scenario.
1315 // Read virtual display properties and create a
1316 // rendering surface for it inorder to be handled by hwc.
1317 setVirtualDisplayData(hwcDisplayId, state.surface);
1318 dispSurface = new FramebufferSurface(*mHwc, state.type,
1319 bqConsumer);
1320 producer = bqProducer;
1321 } else {
1322 // in case of WFD Virtual + SSD/SR concurrency scenario,
1323 // WFD virtual display instance gets valid hwcDisplayId and
1324 // SSD/SR will get invalid hwcDisplayId
1325 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001326 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1327 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001328 dispSurface = vds;
1329 // There won't be any interaction with HWC for this virtual
1330 // display, so the GLES driver can pass buffers directly to the
1331 // sink.
1332 producer = state.surface;
1333 }
1334 }
1335 } else {
1336 // VDS solution is enabled
1337 // HWC is allocated for first virtual display.
1338 // Subsequent virtual display sessions will be composed by GLES driver.
1339 // ToDo: Modify VDS component to allocate hwcDisplayId based on
1340 // mForceHwcCopy (which is based on Usage Flags)
1341
1342 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(*mHwc,
Tatenda Chipeperekwa5757b082013-11-06 15:05:31 -08001343 hwcDisplayId, state.surface, bqProducer, bqConsumer,
1344 state.displayName, state.isSecure);
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001345 dispSurface = vds;
1346 if (hwcDisplayId >= 0) {
1347 producer = vds;
1348 } else {
1349 // There won't be any interaction with HWC for this virtual display,
1350 // so the GLES driver can pass buffers directly to the sink.
1351 producer = state.surface;
1352 }
1353 }
1354}
1355
Mathias Agopian87baae12012-07-31 12:38:26 -07001356void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001357{
1358 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001359 const size_t count = currentLayers.size();
1360
1361 /*
1362 * Traversal of the children
1363 * (perform the transaction for each of them if needed)
1364 */
1365
Mathias Agopian3559b072012-08-15 13:46:03 -07001366 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001367 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001368 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001369 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1370 if (!trFlags) continue;
1371
1372 const uint32_t flags = layer->doTransaction(0);
1373 if (flags & Layer::eVisibleRegion)
1374 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375 }
1376 }
1377
1378 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001379 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001380 */
1381
Mathias Agopiane57f2922012-08-09 16:29:12 -07001382 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001383 // here we take advantage of Vector's copy-on-write semantics to
1384 // improve performance by skipping the transaction entirely when
1385 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001386 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1387 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001388 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001389 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001390 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001391 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001392
1393 // find the displays that were removed
1394 // (ie: in drawing state but not in current state)
1395 // also handle displays that changed
1396 // (ie: displays that are in both lists)
1397 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001398 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1399 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001400 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001401 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001402 // Call makeCurrent() on the primary display so we can
1403 // be sure that nothing associated with this display
1404 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001405 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001406 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001407 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1408 if (hw != NULL)
1409 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001410 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001411 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001412 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001413 } else {
1414 ALOGW("trying to remove the main display");
1415 }
1416 } else {
1417 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001418 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001419 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001420 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001421 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001422 // recreating the DisplayDevice, so we just remove it
1423 // from the drawing state, so that it get re-added
1424 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001425 sp<DisplayDevice> hw(getDisplayDevice(display));
1426 if (hw != NULL)
1427 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001428 mDisplays.removeItem(display);
1429 mDrawingState.displays.removeItemsAt(i);
1430 dc--; i--;
1431 // at this point we must loop to the next item
1432 continue;
1433 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001434
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001435 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001436 if (disp != NULL) {
1437 if (state.layerStack != draw[i].layerStack) {
1438 disp->setLayerStack(state.layerStack);
1439 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001440 if ((state.orientation != draw[i].orientation)
1441 || (state.viewport != draw[i].viewport)
1442 || (state.frame != draw[i].frame))
1443 {
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001444#ifdef QCOM_BSP
1445 int orient = state.orientation;
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001446 // Honor the orientation change after boot
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301447 // animation completes and make sure boot
1448 // animation is shown in panel orientation always.
1449 if(mBootFinished){
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001450 disp->setProjection(state.orientation,
1451 state.viewport, state.frame);
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001452 orient = state.orientation;
Sushil Chauhan2ae2eaf2013-03-22 13:38:13 -07001453 }
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301454 else{
1455 char property[PROPERTY_VALUE_MAX];
1456 int panelOrientation =
1457 DisplayState::eOrientationDefault;
1458 if(property_get("persist.panel.orientation",
1459 property, "0") > 0){
1460 panelOrientation = atoi(property) / 90;
1461 }
1462 disp->setProjection(panelOrientation,
1463 state.viewport, state.frame);
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001464 orient = panelOrientation;
radhakrishna7bf5dbf2014-01-31 14:43:51 +05301465 }
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001466 // Set the view frame of each display only of its
1467 // default orientation.
Saurabh Shah027b3852014-08-21 16:09:32 -07001468 if(orient == DisplayState::eOrientationDefault and
1469 state.frame.isValid()) {
Ramkumar Radhakrishnan0b13eab2014-05-19 19:37:51 -07001470 qdutils::setViewFrame(disp->getHwcDisplayId(),
1471 state.frame.left, state.frame.top,
1472 state.frame.right, state.frame.bottom);
1473 }
1474#else
1475 disp->setProjection(state.orientation,
1476 state.viewport, state.frame);
1477#endif
Mathias Agopian93997a82012-08-29 17:30:36 -07001478 }
Michael Lentine47e45402014-07-18 15:34:25 -07001479 if (state.width != draw[i].width || state.height != draw[i].height) {
1480 disp->setDisplaySize(state.width, state.height);
1481 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001482 }
1483 }
1484 }
1485
1486 // find displays that were added
1487 // (ie: in current state but not in drawing state)
1488 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001489 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001490 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001491
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001492 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001493 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001494 sp<IGraphicBufferProducer> bqProducer;
1495 sp<IGraphicBufferConsumer> bqConsumer;
1496 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1497 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001498
Jesse Hall02d86562013-03-25 14:43:23 -07001499 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001500 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001501 // Virtual displays without a surface are dormant:
1502 // they have external state (layer stack, projection,
1503 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001504 if (state.surface != NULL) {
Manoj Kumar AVM55a56532014-02-11 23:12:50 -08001505 configureVirtualDisplay(hwcDisplayId,
1506 dispSurface, producer, state, bqProducer,
1507 bqConsumer);
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001508 }
1509 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001510 ALOGE_IF(state.surface!=NULL,
1511 "adding a supported display, but rendering "
1512 "surface is provided (%p), ignoring it",
1513 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001514 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001515 // for supported (by hwc) displays we provide our
1516 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001517 dispSurface = new FramebufferSurface(*mHwc, state.type,
1518 bqConsumer);
1519 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001520 }
1521
1522 const wp<IBinder>& display(curr.keyAt(i));
Jeykumar Sankaran46dc8d62013-09-27 08:35:44 +05301523 if (dispSurface != NULL && producer != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001524 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001525 state.type, hwcDisplayId,
1526 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001527 display, dispSurface, producer,
1528 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001529 hw->setLayerStack(state.layerStack);
1530 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001531 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001532 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001533 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001534 if (state.isVirtualDisplay()) {
1535 if (hwcDisplayId >= 0) {
1536 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1537 hw->getWidth(), hw->getHeight(),
1538 hw->getFormat());
1539 }
1540 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001541 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001542 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001543 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001544 }
1545 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001546 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001547 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001548
Mathias Agopian84300952012-11-21 16:02:13 -08001549 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1550 // The transform hint might have changed for some layers
1551 // (either because a display has changed, or because a layer
1552 // as changed).
1553 //
1554 // Walk through all the layers in currentLayers,
1555 // and update their transform hint.
1556 //
1557 // If a layer is visible only on a single display, then that
1558 // display is used to calculate the hint, otherwise we use the
1559 // default display.
1560 //
1561 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1562 // the hint is set before we acquire a buffer from the surface texture.
1563 //
1564 // NOTE: layer transactions have taken place already, so we use their
1565 // drawing state. However, SurfaceFlinger's own transaction has not
1566 // happened yet, so we must use the current state layer list
1567 // (soon to become the drawing state list).
1568 //
1569 sp<const DisplayDevice> disp;
1570 uint32_t currentlayerStack = 0;
1571 for (size_t i=0; i<count; i++) {
1572 // NOTE: we rely on the fact that layers are sorted by
1573 // layerStack first (so we don't have to traverse the list
1574 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001575 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001576 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001577 if (i==0 || currentlayerStack != layerStack) {
1578 currentlayerStack = layerStack;
1579 // figure out if this layerstack is mirrored
1580 // (more than one display) if so, pick the default display,
1581 // if not, pick the only display it's on.
1582 disp.clear();
1583 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1584 sp<const DisplayDevice> hw(mDisplays[dpy]);
1585 if (hw->getLayerStack() == currentlayerStack) {
1586 if (disp == NULL) {
1587 disp = hw;
1588 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001589 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001590 break;
1591 }
1592 }
1593 }
1594 }
Chet Haase91d25932013-04-11 15:24:55 -07001595 if (disp == NULL) {
1596 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1597 // redraw after transform hint changes. See bug 8508397.
1598
1599 // could be null when this layer is using a layerStack
1600 // that is not visible on any display. Also can occur at
1601 // screen off/on times.
1602 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001603 }
Chet Haase91d25932013-04-11 15:24:55 -07001604 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001605 }
1606 }
1607
1608
Mathias Agopian3559b072012-08-15 13:46:03 -07001609 /*
1610 * Perform our own transaction if needed
1611 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001612
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001613 const LayerVector& layers(mDrawingState.layersSortedByZ);
1614 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001615 // layers have been added
1616 mVisibleRegionsDirty = true;
1617 }
1618
1619 // some layers might have been removed, so
1620 // we need to update the regions they're exposing.
1621 if (mLayersRemoved) {
1622 mLayersRemoved = false;
1623 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001624 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001625 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001626 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001627 if (currentLayers.indexOf(layer) < 0) {
1628 // this layer is not visible anymore
1629 // TODO: we could traverse the tree from front to back and
1630 // compute the actual visible region
1631 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001632 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001633 Region visibleReg = s.transform.transform(
1634 Region(Rect(s.active.w, s.active.h)));
1635 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001636 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001637 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001638 }
1639
1640 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001641
1642 updateCursorAsync();
1643}
1644
1645void SurfaceFlinger::updateCursorAsync()
1646{
1647 HWComposer& hwc(getHwComposer());
1648 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1649 sp<const DisplayDevice> hw(mDisplays[dpy]);
1650 const int32_t id = hw->getHwcDisplayId();
1651 if (id < 0) {
1652 continue;
1653 }
1654 const Vector< sp<Layer> >& currentLayers(
1655 hw->getVisibleLayersSortedByZ());
1656 const size_t count = currentLayers.size();
1657 HWComposer::LayerListIterator cur = hwc.begin(id);
1658 const HWComposer::LayerListIterator end = hwc.end(id);
1659 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1660 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1661 continue;
1662 }
1663 const sp<Layer>& layer(currentLayers[i]);
1664 Rect cursorPos = layer->getPosition(hw);
1665 hwc.setCursorPositionAsync(id, cursorPos);
1666 break;
1667 }
1668 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001669}
1670
1671void SurfaceFlinger::commitTransaction()
1672{
1673 if (!mLayersPendingRemoval.isEmpty()) {
1674 // Notify removed layers now that they can't be drawn from
1675 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1676 mLayersPendingRemoval[i]->onRemoved();
1677 }
1678 mLayersPendingRemoval.clear();
1679 }
1680
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001681 // If this transaction is part of a window animation then the next frame
1682 // we composite should be considered an animation as well.
1683 mAnimCompositionPending = mAnimTransactionPending;
1684
Mathias Agopian4fec8732012-06-29 14:12:52 -07001685 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001686 mTransactionPending = false;
1687 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001688 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001689}
1690
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001691void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001692 const LayerVector& currentLayers, uint32_t layerStack,
1693 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001694{
Mathias Agopian841cde52012-03-01 15:44:37 -08001695 ATRACE_CALL();
1696
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001697 Region aboveOpaqueLayers;
1698 Region aboveCoveredLayers;
1699 Region dirty;
1700
Mathias Agopian87baae12012-07-31 12:38:26 -07001701 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001702
1703 size_t i = currentLayers.size();
1704 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001705 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001706
1707 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001708 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001709
Jesse Hall01e29052013-02-19 16:13:35 -08001710 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001711 if (s.layerStack != layerStack)
1712 continue;
1713
Mathias Agopianab028732010-03-16 16:41:46 -07001714 /*
1715 * opaqueRegion: area of a surface that is fully opaque.
1716 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001717 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001718
1719 /*
1720 * visibleRegion: area of a surface that is visible on screen
1721 * and not fully transparent. This is essentially the layer's
1722 * footprint minus the opaque regions above it.
1723 * Areas covered by a translucent surface are considered visible.
1724 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001725 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001726
1727 /*
1728 * coveredRegion: area of a surface that is covered by all
1729 * visible regions above it (which includes the translucent areas).
1730 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001731 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001732
Jesse Halla8026d22012-09-25 13:25:04 -07001733 /*
1734 * transparentRegion: area of a surface that is hinted to be completely
1735 * transparent. This is only used to tell when the layer has no visible
1736 * non-transparent regions and can be removed from the layer list. It
1737 * does not affect the visibleRegion of this layer or any layers
1738 * beneath it. The hint may not be correct if apps don't respect the
1739 * SurfaceView restrictions (which, sadly, some don't).
1740 */
1741 Region transparentRegion;
1742
Mathias Agopianab028732010-03-16 16:41:46 -07001743
1744 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001745 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001746 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001747 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001748 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001749 if (!visibleRegion.isEmpty()) {
1750 // Remove the transparent area from the visible region
1751 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001752 const Transform tr(s.transform);
1753 if (tr.transformed()) {
1754 if (tr.preserveRects()) {
1755 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001756 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001757 } else {
1758 // transformation too complex, can't do the
1759 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001760 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001761 }
1762 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001763 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001764 }
Mathias Agopianab028732010-03-16 16:41:46 -07001765 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001766
Mathias Agopianab028732010-03-16 16:41:46 -07001767 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001768 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001769 if (s.alpha==255 && !translucent &&
1770 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1771 // the opaque region is the layer's footprint
1772 opaqueRegion = visibleRegion;
1773 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001774 }
1775 }
1776
Mathias Agopianab028732010-03-16 16:41:46 -07001777 // Clip the covered region to the visible region
1778 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1779
1780 // Update aboveCoveredLayers for next (lower) layer
1781 aboveCoveredLayers.orSelf(visibleRegion);
1782
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001783 // subtract the opaque region covered by the layers above us
1784 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001785
1786 // compute this layer's dirty region
1787 if (layer->contentDirty) {
1788 // we need to invalidate the whole region
1789 dirty = visibleRegion;
1790 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001791 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001792 layer->contentDirty = false;
1793 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001794 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001795 * the exposed region consists of two components:
1796 * 1) what's VISIBLE now and was COVERED before
1797 * 2) what's EXPOSED now less what was EXPOSED before
1798 *
1799 * note that (1) is conservative, we start with the whole
1800 * visible region but only keep what used to be covered by
1801 * something -- which mean it may have been exposed.
1802 *
1803 * (2) handles areas that were not covered by anything but got
1804 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001805 */
Mathias Agopianab028732010-03-16 16:41:46 -07001806 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001807 const Region oldVisibleRegion = layer->visibleRegion;
1808 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001809 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1810 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001811 }
1812 dirty.subtractSelf(aboveOpaqueLayers);
1813
1814 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001815 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001816
Mathias Agopianab028732010-03-16 16:41:46 -07001817 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001818 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001819
Jesse Halla8026d22012-09-25 13:25:04 -07001820 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001821 layer->setVisibleRegion(visibleRegion);
1822 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001823 layer->setVisibleNonTransparentRegion(
1824 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001825 }
1826
Mathias Agopian87baae12012-07-31 12:38:26 -07001827 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001828}
1829
Mathias Agopian87baae12012-07-31 12:38:26 -07001830void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1831 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001832 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001833 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1834 if (hw->getLayerStack() == layerStack) {
1835 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001836 }
1837 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001838}
1839
1840void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001841{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001842 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001843
Mathias Agopian4fec8732012-06-29 14:12:52 -07001844 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001845 const LayerVector& layers(mDrawingState.layersSortedByZ);
Eric Penner51c59cd2014-07-28 19:51:58 -07001846
1847 // Store the set of layers that need updates. This set must not change as
1848 // buffers are being latched, as this could result in a deadlock.
1849 // Example: Two producers share the same command stream and:
1850 // 1.) Layer 0 is latched
1851 // 2.) Layer 0 gets a new frame
1852 // 2.) Layer 1 gets a new frame
1853 // 3.) Layer 1 is latched.
1854 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1855 // second frame. But layer 0's second frame could be waiting on display.
1856 Vector<Layer*> layersWithQueuedFrames;
1857 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001858 const sp<Layer>& layer(layers[i]);
Eric Penner51c59cd2014-07-28 19:51:58 -07001859 if (layer->hasQueuedFrame())
1860 layersWithQueuedFrames.push_back(layer.get());
1861 }
1862 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
1863 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07001864 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001865 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001866 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001867 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001868
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001869 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001870}
1871
Mathias Agopianad456f92011-01-13 17:53:01 -08001872void SurfaceFlinger::invalidateHwcGeometry()
1873{
1874 mHwWorkListDirty = true;
1875}
1876
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001877
Mathias Agopiancd60f992012-08-16 16:28:27 -07001878void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001879 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001880{
Dan Stoza71433162014-02-04 16:22:36 -08001881 // We only need to actually compose the display if:
1882 // 1) It is being handled by hardware composer, which may need this to
1883 // keep its virtual display state machine in sync, or
1884 // 2) There is work to be done (the dirty region isn't empty)
1885 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1886 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1887 return;
1888 }
1889
Mathias Agopian87baae12012-07-31 12:38:26 -07001890 Region dirtyRegion(inDirtyRegion);
1891
Mathias Agopianb8a55602009-06-26 19:06:36 -07001892 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001893 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001894
Mathias Agopian42977342012-08-05 00:40:46 -07001895 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001896 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001897 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1898 // takes a rectangle, we must make sure to update that whole
1899 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001900 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001901 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001902 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001903 // We need to redraw the rectangle that will be updated
1904 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001905 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001906 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001907 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001908 } else {
1909 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001910 dirtyRegion.set(hw->bounds());
1911 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001912 }
1913 }
1914
Alan Viverette9c5a3332013-09-12 20:04:35 -07001915 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Michael Lentine3f121fc2014-10-01 11:17:28 -07001916 if (!doComposeSurfaces(hw, dirtyRegion)) return;
Mathias Agopianff2ed702013-09-01 21:36:12 -07001917 } else {
1918 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001919 mat4 colorMatrix = mColorMatrix;
1920 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001921 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001922 }
1923 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001924 doComposeSurfaces(hw, dirtyRegion);
1925 engine.endGroup();
1926 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001927
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001928 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001929 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001930
1931 // swap buffers (presentation)
1932 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001933}
1934
Michael Lentine3f121fc2014-10-01 11:17:28 -07001935bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001936{
Mathias Agopian3f844832013-08-07 21:24:32 -07001937 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001938 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001939 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001940 HWComposer::LayerListIterator cur = hwc.begin(id);
1941 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001942
Jesse Halld05a17f2013-09-30 16:49:30 -07001943 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001944 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001945 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001946 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1947 hw->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07001948 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1949 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
1950 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
1951 }
1952 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08001953 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001954
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001955 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001956 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001957 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001958 // when using overlays, we assume a fully transparent framebuffer
1959 // NOTE: we could reduce how much we need to clear, for instance
1960 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001961 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001962 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001963 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001964 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001965 // we start with the whole screen area
1966 const Region bounds(hw->getBounds());
1967
1968 // we remove the scissor part
1969 // we're left with the letterbox region
1970 // (common case is that letterbox ends-up being empty)
1971 const Region letterbox(bounds.subtract(hw->getScissor()));
1972
1973 // compute the area to clear
1974 Region region(hw->undefinedRegion.merge(letterbox));
1975
1976 // but limit it to the dirty region
1977 region.andSelf(dirty);
1978
Mathias Agopianb9494d52012-04-18 02:28:45 -07001979 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001980 if (!region.isEmpty()) {
Radhika Ranjan Soni35c9ff22013-12-17 19:55:17 +05301981 if (cur != end) {
1982 if (cur->getCompositionType() != HWC_BLIT)
1983 // can happen with SurfaceView
1984 drawWormhole(hw, region);
1985 } else
1986 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001987 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001988 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001989
Mathias Agopian766dc492012-10-30 18:08:06 -07001990 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1991 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001992 // scissor on the main display. It should never be needed
1993 // anyways (though in theory it could since the API allows it).
1994 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001995 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001996 if (scissor != bounds) {
1997 // scissor doesn't match the screen's dimensions, so we
1998 // need to clear everything outside of it and enable
1999 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07002000
Mathias Agopianf45c5102012-10-24 16:29:17 -07002001 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002002 const uint32_t height = hw->getHeight();
2003 engine.setScissor(scissor.left, height - scissor.bottom,
2004 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07002005 }
2006 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002007 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002008
Mathias Agopian85d751c2012-08-29 16:59:24 -07002009 /*
2010 * and then, render the layers targeted at the framebuffer
2011 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002012
Mathias Agopian13127d82013-03-05 17:47:11 -08002013 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002014 const size_t count = layers.size();
2015 const Transform& tr = hw->getTransform();
2016 if (cur != end) {
2017 // we're using h/w composer
2018 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002019 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07002020 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07002021 if (!clip.isEmpty()) {
2022 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07002023 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07002024 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07002025 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07002026 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
2027 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08002028 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07002029 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07002030 // never clear the very first layer since we're
2031 // guaranteed the FB is already cleared
2032 layer->clearWithOpenGL(hw, clip);
2033 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002034 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07002035 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002036 case HWC_FRAMEBUFFER: {
2037 layer->draw(hw, clip);
2038 break;
2039 }
Arun Kumar K.R5111ff02012-12-19 18:10:46 -08002040 case HWC_BLIT:
2041 //Do nothing
2042 break;
Mathias Agopianda27af92012-09-13 18:17:13 -07002043 case HWC_FRAMEBUFFER_TARGET: {
2044 // this should not happen as the iterator shouldn't
2045 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08002046 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07002047 break;
2048 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07002049 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07002050 }
2051 layer->setAcquireFence(hw, *cur);
2052 }
2053 } else {
2054 // we're not using h/w composer
2055 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002056 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07002057 const Region clip(dirty.intersect(
2058 tr.transform(layer->visibleRegion)));
2059 if (!clip.isEmpty()) {
2060 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07002061 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07002062 }
2063 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07002064
2065 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07002066 engine.disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07002067 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002068}
2069
Mathias Agopian3f844832013-08-07 21:24:32 -07002070void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07002071 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07002072 RenderEngine& engine(getRenderEngine());
2073 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002074}
2075
Mathias Agopianac9fa422013-02-11 16:40:36 -08002076void SurfaceFlinger::addClientLayer(const sp<Client>& client,
2077 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07002078 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08002079 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07002080{
Mathias Agopian96f08192010-06-02 23:28:45 -07002081 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08002082 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07002083
Mathias Agopian96f08192010-06-02 23:28:45 -07002084 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002085 Mutex::Autolock _l(mStateLock);
2086 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07002087 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07002088}
2089
Mathias Agopian3f844832013-08-07 21:24:32 -07002090status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002091 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08002092 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002093 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07002094 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07002095 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07002096 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002097 return NO_ERROR;
2098 }
Mathias Agopian3d579642009-06-04 18:46:21 -07002099 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002100}
2101
Dan Stozac7014012014-02-14 15:03:43 -08002102uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07002103 return android_atomic_release_load(&mTransactionFlags);
2104}
2105
Mathias Agopian3f844832013-08-07 21:24:32 -07002106uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002107 return android_atomic_and(~flags, &mTransactionFlags) & flags;
2108}
2109
Mathias Agopian3f844832013-08-07 21:24:32 -07002110uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002111 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
2112 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002113 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002114 }
2115 return old;
2116}
2117
Mathias Agopian8b33f032012-07-24 20:43:54 -07002118void SurfaceFlinger::setTransactionState(
2119 const Vector<ComposerState>& state,
2120 const Vector<DisplayState>& displays,
2121 uint32_t flags)
2122{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002123 ATRACE_CALL();
Ramkumar Radhakrishnanab2f30f2013-12-11 22:13:14 -08002124 size_t count = displays.size();
2125#ifdef QCOM_BSP
2126 for (size_t i=0 ; i<count ; i++) {
2127 const DisplayState& s(displays[i]);
2128 if(s.token != mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY]) {
2129 const uint32_t what = s.what;
2130 // Invalidate and Delay the binder thread by 50 ms on
2131 // eDisplayProjectionChanged to trigger a draw cycle so that
2132 // it can fix one incorrect frame on the External, when we disable
2133 // external animation
2134 if (what & DisplayState::eDisplayProjectionChanged) {
2135 invalidateHwcGeometry();
2136 repaintEverything();
2137 usleep(50000);
2138 }
2139 }
2140 }
2141#endif
Mathias Agopian698c0872011-06-28 19:09:31 -07002142 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07002143 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002144
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002145 if (flags & eAnimation) {
2146 // For window updates that are part of an animation we must wait for
2147 // previous animation "frames" to be handled.
2148 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002149 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002150 if (CC_UNLIKELY(err != NO_ERROR)) {
2151 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002152 // caller after a few seconds.
2153 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
2154 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002155 mAnimTransactionPending = false;
2156 break;
2157 }
2158 }
2159 }
2160
Mathias Agopiane57f2922012-08-09 16:29:12 -07002161 for (size_t i=0 ; i<count ; i++) {
2162 const DisplayState& s(displays[i]);
2163 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002164 }
2165
Mathias Agopiane57f2922012-08-09 16:29:12 -07002166 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002167 for (size_t i=0 ; i<count ; i++) {
2168 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002169 // Here we need to check that the interface we're given is indeed
2170 // one of our own. A malicious client could give us a NULL
2171 // IInterface, or one of its own or even one of our own but a
2172 // different type. All these situations would cause us to crash.
2173 //
2174 // NOTE: it would be better to use RTTI as we could directly check
2175 // that we have a Client*. however, RTTI is disabled in Android.
2176 if (s.client != NULL) {
2177 sp<IBinder> binder = s.client->asBinder();
2178 if (binder != NULL) {
2179 String16 desc(binder->getInterfaceDescriptor());
2180 if (desc == ISurfaceComposerClient::descriptor) {
2181 sp<Client> client( static_cast<Client *>(s.client.get()) );
2182 transactionFlags |= setClientStateLocked(client, s.state);
2183 }
2184 }
2185 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002186 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002187
Mathias Agopian386aa982011-11-07 21:58:03 -08002188 if (transactionFlags) {
2189 // this triggers the transaction
2190 setTransactionFlags(transactionFlags);
2191
2192 // if this is a synchronous transaction, wait for it to take effect
2193 // before returning.
2194 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002195 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002196 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002197 if (flags & eAnimation) {
2198 mAnimTransactionPending = true;
2199 }
2200 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002201 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2202 if (CC_UNLIKELY(err != NO_ERROR)) {
2203 // just in case something goes wrong in SF, return to the
2204 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002205 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2206 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002207 break;
2208 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002209 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002210 }
2211}
2212
Mathias Agopiane57f2922012-08-09 16:29:12 -07002213uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2214{
Jesse Hall9a143922012-10-04 16:29:19 -07002215 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2216 if (dpyIdx < 0)
2217 return 0;
2218
Mathias Agopiane57f2922012-08-09 16:29:12 -07002219 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002220 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002221 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002222 const uint32_t what = s.what;
2223 if (what & DisplayState::eSurfaceChanged) {
2224 if (disp.surface->asBinder() != s.surface->asBinder()) {
2225 disp.surface = s.surface;
2226 flags |= eDisplayTransactionNeeded;
2227 }
2228 }
2229 if (what & DisplayState::eLayerStackChanged) {
2230 if (disp.layerStack != s.layerStack) {
2231 disp.layerStack = s.layerStack;
2232 flags |= eDisplayTransactionNeeded;
2233 }
2234 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002235 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002236 if (disp.orientation != s.orientation) {
2237 disp.orientation = s.orientation;
2238 flags |= eDisplayTransactionNeeded;
2239 }
2240 if (disp.frame != s.frame) {
2241 disp.frame = s.frame;
2242 flags |= eDisplayTransactionNeeded;
2243 }
2244 if (disp.viewport != s.viewport) {
2245 disp.viewport = s.viewport;
2246 flags |= eDisplayTransactionNeeded;
2247 }
2248 }
Michael Lentine47e45402014-07-18 15:34:25 -07002249 if (what & DisplayState::eDisplaySizeChanged) {
2250 if (disp.width != s.width) {
2251 disp.width = s.width;
2252 flags |= eDisplayTransactionNeeded;
2253 }
2254 if (disp.height != s.height) {
2255 disp.height = s.height;
2256 flags |= eDisplayTransactionNeeded;
2257 }
2258 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002259 }
2260 return flags;
2261}
2262
2263uint32_t SurfaceFlinger::setClientStateLocked(
2264 const sp<Client>& client,
2265 const layer_state_t& s)
2266{
2267 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002268 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002269 if (layer != 0) {
2270 const uint32_t what = s.what;
2271 if (what & layer_state_t::ePositionChanged) {
2272 if (layer->setPosition(s.x, s.y))
2273 flags |= eTraversalNeeded;
2274 }
2275 if (what & layer_state_t::eLayerChanged) {
2276 // NOTE: index needs to be calculated before we update the state
2277 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2278 if (layer->setLayer(s.z)) {
2279 mCurrentState.layersSortedByZ.removeAt(idx);
2280 mCurrentState.layersSortedByZ.add(layer);
2281 // we need traversal (state changed)
2282 // AND transaction (list changed)
2283 flags |= eTransactionNeeded|eTraversalNeeded;
2284 }
2285 }
2286 if (what & layer_state_t::eSizeChanged) {
2287 if (layer->setSize(s.w, s.h)) {
2288 flags |= eTraversalNeeded;
2289 }
2290 }
2291 if (what & layer_state_t::eAlphaChanged) {
2292 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2293 flags |= eTraversalNeeded;
2294 }
2295 if (what & layer_state_t::eMatrixChanged) {
2296 if (layer->setMatrix(s.matrix))
2297 flags |= eTraversalNeeded;
2298 }
2299 if (what & layer_state_t::eTransparentRegionChanged) {
2300 if (layer->setTransparentRegionHint(s.transparentRegion))
2301 flags |= eTraversalNeeded;
2302 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08002303 if ((what & layer_state_t::eVisibilityChanged) ||
2304 (what & layer_state_t::eOpacityChanged)) {
2305 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07002306 if (layer->setFlags(s.flags, s.mask))
2307 flags |= eTraversalNeeded;
2308 }
2309 if (what & layer_state_t::eCropChanged) {
2310 if (layer->setCrop(s.crop))
2311 flags |= eTraversalNeeded;
2312 }
2313 if (what & layer_state_t::eLayerStackChanged) {
2314 // NOTE: index needs to be calculated before we update the state
2315 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2316 if (layer->setLayerStack(s.layerStack)) {
2317 mCurrentState.layersSortedByZ.removeAt(idx);
2318 mCurrentState.layersSortedByZ.add(layer);
2319 // we need traversal (state changed)
2320 // AND transaction (list changed)
2321 flags |= eTransactionNeeded|eTraversalNeeded;
2322 }
2323 }
2324 }
2325 return flags;
2326}
2327
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002328status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002329 const String8& name,
2330 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002331 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2332 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002333{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002334 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002335 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002336 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002337 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002338 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002339 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002340
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002341 status_t result = NO_ERROR;
2342
2343 sp<Layer> layer;
2344
Mathias Agopian3165cc22012-08-08 19:42:09 -07002345 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2346 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002347 result = createNormalLayer(client,
2348 name, w, h, flags, format,
2349 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002350 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002351 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002352 result = createDimLayer(client,
2353 name, w, h, flags,
2354 handle, gbp, &layer);
2355 break;
2356 default:
2357 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002358 break;
2359 }
2360
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002361 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002362 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002363 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002364 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002365 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002366}
2367
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002368status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2369 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2370 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002371{
2372 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002373 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002374 case PIXEL_FORMAT_TRANSPARENT:
2375 case PIXEL_FORMAT_TRANSLUCENT:
2376 format = PIXEL_FORMAT_RGBA_8888;
2377 break;
2378 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002379 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002380 break;
2381 }
2382
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002383 *outLayer = new Layer(this, client, name, w, h, flags);
2384 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2385 if (err == NO_ERROR) {
2386 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002387 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002388 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002389
2390 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2391 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002392}
2393
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002394status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2395 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2396 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002397{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002398 *outLayer = new LayerDim(this, client, name, w, h, flags);
2399 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002400 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002401 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002402}
2403
Mathias Agopianac9fa422013-02-11 16:40:36 -08002404status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002405{
Mathias Agopian67106042013-03-14 19:18:13 -07002406 // called by the window manager when it wants to remove a Layer
2407 status_t err = NO_ERROR;
2408 sp<Layer> l(client->getLayerUser(handle));
2409 if (l != NULL) {
2410 err = removeLayer(l);
2411 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2412 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002413 }
2414 return err;
2415}
2416
Mathias Agopian13127d82013-03-05 17:47:11 -08002417status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002418{
Mathias Agopian67106042013-03-14 19:18:13 -07002419 // called by ~LayerCleaner() when all references to the IBinder (handle)
2420 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002421 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002422 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002423 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002424 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002425 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002426 "error removing layer=%p (%s)", l.get(), strerror(-err));
2427 }
2428 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002429}
2430
Mathias Agopianb60314a2012-04-10 22:09:54 -07002431// ---------------------------------------------------------------------------
2432
Andy McFadden13a082e2012-08-24 10:16:42 -07002433void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002434 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002435 Vector<ComposerState> state;
2436 Vector<DisplayState> displays;
2437 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002438 d.what = DisplayState::eDisplayProjectionChanged |
2439 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002440 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002441 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002442 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002443 d.frame.makeInvalid();
2444 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002445 d.width = 0;
2446 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002447 displays.add(d);
2448 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002449 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002450
2451 const nsecs_t period =
2452 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2453 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002454}
2455
2456void SurfaceFlinger::initializeDisplays() {
2457 class MessageScreenInitialized : public MessageBase {
2458 SurfaceFlinger* flinger;
2459 public:
2460 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2461 virtual bool handler() {
2462 flinger->onInitializeDisplays();
2463 return true;
2464 }
2465 };
2466 sp<MessageBase> msg = new MessageScreenInitialized(this);
2467 postMessageAsync(msg); // we may be called from main thread, use async message
2468}
2469
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002470void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2471 int mode) {
2472 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2473 this);
2474 int32_t type = hw->getDisplayType();
2475 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002476
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002477 if (mode == currentMode) {
2478 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002479 return;
2480 }
2481
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002482 hw->setPowerMode(mode);
2483 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2484 ALOGW("Trying to set power mode for virtual display");
2485 return;
2486 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002487
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002488 if (currentMode == HWC_POWER_MODE_OFF) {
2489 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002490 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2491 // FIXME: eventthread only knows about the main display right now
2492 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002493 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002494 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002495
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002496 mVisibleRegionsDirty = true;
2497 repaintEverything();
2498 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002499 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002500 disableHardwareVsync(true); // also cancels any in-progress resync
2501
Mathias Agopiancde87a32012-09-13 14:09:01 -07002502 // FIXME: eventthread only knows about the main display right now
2503 mEventThread->onScreenReleased();
2504 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002505
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002506 getHwComposer().setPowerMode(type, mode);
2507 mVisibleRegionsDirty = true;
2508 // from this point on, SF will stop drawing on this display
2509 } else {
2510 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002511 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002512}
2513
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002514void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2515 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002516 SurfaceFlinger& mFlinger;
2517 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002518 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002519 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002520 MessageSetPowerMode(SurfaceFlinger& flinger,
2521 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2522 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002523 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002524 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002525 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002526 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002527 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002528 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002529 ALOGW("Attempt to set power mode = %d for virtual display",
2530 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002531 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002532 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002533 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002534 return true;
2535 }
2536 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002537 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002538 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002539}
2540
2541// ---------------------------------------------------------------------------
2542
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002543status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2544{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002545 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002546
Mathias Agopianbd115332013-04-18 16:41:04 -07002547 IPCThreadState* ipc = IPCThreadState::self();
2548 const int pid = ipc->getCallingPid();
2549 const int uid = ipc->getCallingUid();
2550 if ((uid != AID_SHELL) &&
2551 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002552 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002553 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002554 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002555 // Try to get the main lock, but don't insist if we can't
2556 // (this would indicate SF is stuck, but we want to be able to
2557 // print something in dumpsys).
2558 int retry = 3;
2559 while (mStateLock.tryLock()<0 && --retry>=0) {
2560 usleep(1000000);
2561 }
2562 const bool locked(retry >= 0);
2563 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002564 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002565 "SurfaceFlinger appears to be unresponsive, "
2566 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002567 }
2568
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002569 bool dumpAll = true;
2570 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002571 size_t numArgs = args.size();
2572 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002573 if ((index < numArgs) &&
2574 (args[index] == String16("--list"))) {
2575 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002576 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002577 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002578 }
2579
2580 if ((index < numArgs) &&
2581 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002582 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002583 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002584 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002585 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002586
2587 if ((index < numArgs) &&
2588 (args[index] == String16("--latency-clear"))) {
2589 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002590 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002591 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002592 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002593
2594 if ((index < numArgs) &&
2595 (args[index] == String16("--dispsync"))) {
2596 index++;
2597 mPrimaryDispSync.dump(result);
2598 dumpAll = false;
2599 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002600 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002601
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002602 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002603 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002604 }
2605
Mathias Agopian9795c422009-08-26 16:36:26 -07002606 if (locked) {
2607 mStateLock.unlock();
2608 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002609 }
2610 write(fd, result.string(), result.size());
2611 return NO_ERROR;
2612}
2613
Dan Stozac7014012014-02-14 15:03:43 -08002614void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2615 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002616{
2617 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2618 const size_t count = currentLayers.size();
2619 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002620 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002621 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002622 }
2623}
2624
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002625void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002626 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002627{
2628 String8 name;
2629 if (index < args.size()) {
2630 name = String8(args[index]);
2631 index++;
2632 }
2633
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002634 const nsecs_t period =
2635 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002636 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002637
2638 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002639 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002640 } else {
2641 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2642 const size_t count = currentLayers.size();
2643 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002644 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002645 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002646 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002647 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002648 }
2649 }
2650}
2651
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002652void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002653 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002654{
2655 String8 name;
2656 if (index < args.size()) {
2657 name = String8(args[index]);
2658 index++;
2659 }
2660
2661 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2662 const size_t count = currentLayers.size();
2663 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002664 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002665 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002666 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002667 }
2668 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002669
Svetoslavd85084b2014-03-20 10:28:31 -07002670 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002671}
2672
Jamie Gennis6547ff42013-07-16 20:12:42 -07002673// This should only be called from the main thread. Otherwise it would need
2674// the lock and should use mCurrentState rather than mDrawingState.
2675void SurfaceFlinger::logFrameStats() {
2676 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2677 const size_t count = drawingLayers.size();
2678 for (size_t i=0 ; i<count ; i++) {
2679 const sp<Layer>& layer(drawingLayers[i]);
2680 layer->logFrameStats();
2681 }
2682
2683 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2684}
2685
Andy McFadden4803b742012-09-24 19:07:20 -07002686/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2687{
2688 static const char* config =
2689 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002690#ifdef HAS_CONTEXT_PRIORITY
2691 " HAS_CONTEXT_PRIORITY"
2692#endif
2693#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2694 " NEVER_DEFAULT_TO_ASYNC_MODE"
2695#endif
2696#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2697 " TARGET_DISABLE_TRIPLE_BUFFERING"
2698#endif
2699 "]";
2700 result.append(config);
2701}
2702
Mathias Agopian74d211a2013-04-22 16:55:35 +02002703void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2704 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002705{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002706 bool colorize = false;
2707 if (index < args.size()
2708 && (args[index] == String16("--color"))) {
2709 colorize = true;
2710 index++;
2711 }
2712
2713 Colorizer colorizer(colorize);
2714
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002715 // figure out if we're stuck somewhere
2716 const nsecs_t now = systemTime();
2717 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2718 const nsecs_t inTransaction(mDebugInTransaction);
2719 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2720 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2721
2722 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002723 * Dump library configuration.
2724 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002725
2726 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002727 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002728 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002729 appendSfConfigString(result);
2730 appendUiConfigString(result);
2731 appendGuiConfigString(result);
2732 result.append("\n");
2733
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002734 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002735 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002736 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002737 result.append(SyncFeatures::getInstance().toString());
2738 result.append("\n");
2739
Andy McFadden41d67d72014-04-25 16:58:34 -07002740 colorizer.bold(result);
2741 result.append("DispSync configuration: ");
2742 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002743 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2744 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002745 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2746 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2747 result.append("\n");
2748
Andy McFadden4803b742012-09-24 19:07:20 -07002749 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002750 * Dump the visible layer list
2751 */
2752 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2753 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002754 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002755 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002756 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002757 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002758 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002759 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002760 }
2761
2762 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002763 * Dump Display state
2764 */
2765
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002766 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002767 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002768 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002769 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2770 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002771 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002772 }
2773
2774 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002775 * Dump SurfaceFlinger global state
2776 */
2777
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002778 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002779 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002780 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002781
Mathias Agopian888c8222012-08-04 21:10:38 -07002782 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002783 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002784
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002785 colorizer.bold(result);
2786 result.appendFormat("EGL implementation : %s\n",
2787 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2788 colorizer.reset(result);
2789 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002790 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002791
Mathias Agopian875d8e12013-06-07 15:35:48 -07002792 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002793
Mathias Agopian42977342012-08-05 00:40:46 -07002794 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002795 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2796 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002797 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002798 " last eglSwapBuffers() time: %f us\n"
2799 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002800 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002801 " refresh-rate : %f fps\n"
2802 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002803 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002804 " gpu_to_cpu_unsupported : %d\n"
2805 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002806 mLastSwapBufferTime/1000.0,
2807 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002808 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002809 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2810 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002811 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002812 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002813
Mathias Agopian74d211a2013-04-22 16:55:35 +02002814 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002815 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002816
Mathias Agopian74d211a2013-04-22 16:55:35 +02002817 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002818 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002819
2820 /*
2821 * VSYNC state
2822 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002823 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002824
2825 /*
2826 * Dump HWComposer state
2827 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002828 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002829 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002830 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002831 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002832 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002833 (mDebugDisableHWC || mDebugRegion || mDaltonize
2834 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002835 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002836
2837 /*
2838 * Dump gralloc state
2839 */
2840 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2841 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002842}
2843
Mathias Agopian13127d82013-03-05 17:47:11 -08002844const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002845SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002846 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002847 wp<IBinder> dpy;
2848 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2849 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2850 dpy = mDisplays.keyAt(i);
2851 break;
2852 }
2853 }
2854 if (dpy == NULL) {
2855 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2856 // Just use the primary display so we have something to return
2857 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2858 }
2859 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002860}
2861
Keun young Park63f165f2012-08-31 10:53:36 -07002862bool SurfaceFlinger::startDdmConnection()
2863{
2864 void* libddmconnection_dso =
2865 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2866 if (!libddmconnection_dso) {
2867 return false;
2868 }
2869 void (*DdmConnection_start)(const char* name);
2870 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07002871 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07002872 if (!DdmConnection_start) {
2873 dlclose(libddmconnection_dso);
2874 return false;
2875 }
2876 (*DdmConnection_start)(getServiceName());
2877 return true;
2878}
2879
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002880status_t SurfaceFlinger::onTransact(
2881 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2882{
2883 switch (code) {
2884 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002885 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002886 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002887 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002888 case CLEAR_ANIMATION_FRAME_STATS:
2889 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002890 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002891 {
2892 // codes that require permission check
2893 IPCThreadState* ipc = IPCThreadState::self();
2894 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002895 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002896 if ((uid != AID_GRAPHICS) &&
2897 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002898 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002899 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2900 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002901 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002902 break;
2903 }
2904 case CAPTURE_SCREEN:
2905 {
2906 // codes that require permission check
2907 IPCThreadState* ipc = IPCThreadState::self();
2908 const int pid = ipc->getCallingPid();
2909 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002910 if ((uid != AID_GRAPHICS) &&
2911 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002912 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002913 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2914 return PERMISSION_DENIED;
2915 }
2916 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002917 }
2918 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002919
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002920 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2921 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002922 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002923 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002924 IPCThreadState* ipc = IPCThreadState::self();
2925 const int pid = ipc->getCallingPid();
2926 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002927 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002928 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002929 return PERMISSION_DENIED;
2930 }
2931 int n;
2932 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002933 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002934 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002935 return NO_ERROR;
2936 case 1002: // SHOW_UPDATES
2937 n = data.readInt32();
2938 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002939 invalidateHwcGeometry();
2940 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002941 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002942 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002943 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002944 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002945 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002946 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002947 setTransactionFlags(
2948 eTransactionNeeded|
2949 eDisplayTransactionNeeded|
2950 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002951 return NO_ERROR;
2952 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002953 case 1006:{ // send empty update
2954 signalRefresh();
2955 return NO_ERROR;
2956 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002957 case 1008: // toggle use of hw composer
2958 n = data.readInt32();
2959 mDebugDisableHWC = n ? 1 : 0;
2960 invalidateHwcGeometry();
2961 repaintEverything();
2962 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002963 case 1009: // toggle use of transform hint
2964 n = data.readInt32();
2965 mDebugDisableTransformHint = n ? 1 : 0;
2966 invalidateHwcGeometry();
2967 repaintEverything();
2968 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002969 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002970 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002971 reply->writeInt32(0);
2972 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002973 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002974 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002975 return NO_ERROR;
2976 case 1013: {
2977 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002978 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2979 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002980 return NO_ERROR;
2981 }
2982 case 1014: {
2983 // daltonize
2984 n = data.readInt32();
2985 switch (n % 10) {
2986 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2987 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2988 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2989 }
2990 if (n >= 10) {
2991 mDaltonizer.setMode(Daltonizer::correction);
2992 } else {
2993 mDaltonizer.setMode(Daltonizer::simulation);
2994 }
2995 mDaltonize = n > 0;
2996 invalidateHwcGeometry();
2997 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002998 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002999 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003000 case 1015: {
3001 // apply a color matrix
3002 n = data.readInt32();
3003 mHasColorMatrix = n ? 1 : 0;
3004 if (n) {
3005 // color matrix is sent as mat3 matrix followed by vec3
3006 // offset, then packed into a mat4 where the last row is
3007 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07003008 for (size_t i = 0 ; i < 4; i++) {
3009 for (size_t j = 0; j < 4; j++) {
3010 mColorMatrix[i][j] = data.readFloat();
3011 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003012 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07003013 } else {
3014 mColorMatrix = mat4();
3015 }
3016 invalidateHwcGeometry();
3017 repaintEverything();
3018 return NO_ERROR;
3019 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003020 // This is an experimental interface
3021 // Needs to be shifted to proper binder interface when we productize
3022 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07003023 n = data.readInt32();
3024 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07003025 return NO_ERROR;
3026 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003027 }
3028 }
3029 return err;
3030}
3031
Mathias Agopian53331da2011-08-22 21:44:41 -07003032void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07003033 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08003034 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07003035}
3036
Mathias Agopian59119e62010-10-11 12:37:43 -07003037// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003038// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07003039// ---------------------------------------------------------------------------
3040
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003041/* The code below is here to handle b/8734824
3042 *
3043 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07003044 * from the surfaceflinger thread to the calling binder thread, where they
3045 * are executed. This allows the calling thread in the calling process to be
3046 * reused and not depend on having "enough" binder threads to handle the
3047 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003048 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003049class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07003050 /* Parts of GraphicProducerWrapper are run on two different threads,
3051 * communicating by sending messages via Looper but also by shared member
3052 * data. Coherence maintenance is subtle and in places implicit (ugh).
3053 *
3054 * Don't rely on Looper's sendMessage/handleMessage providing
3055 * release/acquire semantics for any data not actually in the Message.
3056 * Data going from surfaceflinger to binder threads needs to be
3057 * synchronized explicitly.
3058 *
3059 * Barrier open/wait do provide release/acquire semantics. This provides
3060 * implicit synchronization for data coming back from binder to
3061 * surfaceflinger threads.
3062 */
3063
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003064 sp<IGraphicBufferProducer> impl;
3065 sp<Looper> looper;
3066 status_t result;
3067 bool exitPending;
3068 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07003069 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003070 uint32_t code;
3071 Parcel const* data;
3072 Parcel* reply;
3073
3074 enum {
3075 MSG_API_CALL,
3076 MSG_EXIT
3077 };
3078
3079 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003080 * Called on surfaceflinger thread. This is called by our "fake"
3081 * BpGraphicBufferProducer. We package the data and reply Parcel and
3082 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003083 */
3084 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08003085 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003086 this->code = code;
3087 this->data = &data;
3088 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003089 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07003090 // if we've exited, we run the message synchronously right here.
3091 // note (JH): as far as I can tell from looking at the code, this
3092 // never actually happens. if it does, i'm not sure if it happens
3093 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003094 handleMessage(Message(MSG_API_CALL));
3095 } else {
3096 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07003097 // Prevent stores to this->{code, data, reply} from being
3098 // reordered later than the construction of Message.
3099 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003100 looper->sendMessage(this, Message(MSG_API_CALL));
3101 barrier.wait();
3102 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08003103 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003104 }
3105
3106 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003107 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003108 * call the real BpGraphicBufferProducer.
3109 */
3110 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07003111 int what = message.what;
3112 // Prevent reads below from happening before the read from Message
3113 atomic_thread_fence(memory_order_acquire);
3114 if (what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08003115 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003116 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07003117 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003118 exitRequested = true;
3119 }
3120 }
3121
3122public:
Jesse Hallb154c422014-07-13 12:47:02 -07003123 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
3124 : impl(impl),
3125 looper(new Looper(true)),
3126 exitPending(false),
3127 exitRequested(false)
3128 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003129
Jesse Hallb154c422014-07-13 12:47:02 -07003130 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003131 status_t waitForResponse() {
3132 do {
3133 looper->pollOnce(-1);
3134 } while (!exitRequested);
3135 return result;
3136 }
3137
Jesse Hallb154c422014-07-13 12:47:02 -07003138 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003139 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07003140 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003141 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07003142 // Ensure this->result is visible to the binder thread before it
3143 // handles the message.
3144 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003145 looper->sendMessage(this, Message(MSG_EXIT));
3146 }
3147};
3148
3149
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003150status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
3151 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003152 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003153 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003154 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003155
3156 if (CC_UNLIKELY(display == 0))
3157 return BAD_VALUE;
3158
3159 if (CC_UNLIKELY(producer == 0))
3160 return BAD_VALUE;
3161
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003162 // if we have secure windows on this display, never allow the screen capture
3163 // unless the producer interface is local (i.e.: we can take a screenshot for
3164 // ourselves).
3165 if (!producer->asBinder()->localBinder()) {
3166 Mutex::Autolock _l(mStateLock);
3167 sp<const DisplayDevice> hw(getDisplayDevice(display));
3168 if (hw->getSecureLayerVisible()) {
3169 ALOGW("FB is protected: PERMISSION_DENIED");
3170 return PERMISSION_DENIED;
3171 }
3172 }
3173
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003174 // Convert to surfaceflinger's internal rotation type.
3175 Transform::orientation_flags rotationFlags;
3176 switch (rotation) {
3177 case ISurfaceComposer::eRotateNone:
3178 rotationFlags = Transform::ROT_0;
3179 break;
3180 case ISurfaceComposer::eRotate90:
3181 rotationFlags = Transform::ROT_90;
3182 break;
3183 case ISurfaceComposer::eRotate180:
3184 rotationFlags = Transform::ROT_180;
3185 break;
3186 case ISurfaceComposer::eRotate270:
3187 rotationFlags = Transform::ROT_270;
3188 break;
3189 default:
3190 rotationFlags = Transform::ROT_0;
3191 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3192 break;
3193 }
3194
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003195 class MessageCaptureScreen : public MessageBase {
3196 SurfaceFlinger* flinger;
3197 sp<IBinder> display;
3198 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003199 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003200 uint32_t reqWidth, reqHeight;
3201 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003202 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003203 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003204 status_t result;
3205 public:
3206 MessageCaptureScreen(SurfaceFlinger* flinger,
3207 const sp<IBinder>& display,
3208 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003209 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003210 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003211 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003212 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003213 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003214 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003215 useIdentityTransform(useIdentityTransform),
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003216 rotation(rotation),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003217 result(PERMISSION_DENIED)
3218 {
3219 }
3220 status_t getResult() const {
3221 return result;
3222 }
3223 virtual bool handler() {
3224 Mutex::Autolock _l(flinger->mStateLock);
3225 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003226 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003227 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003228 useIdentityTransform, rotation);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003229 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003230 return true;
3231 }
3232 };
3233
Mathias Agopian9eb1f052013-04-10 16:27:17 -07003234 // make sure to process transactions before screenshots -- a transaction
3235 // might already be pending but scheduled for VSYNC; this guarantees we
3236 // will handle it before the screenshot. When VSYNC finally arrives
3237 // the scheduled transaction will be a no-op. If no transactions are
3238 // scheduled at this time, this will end-up being a no-op as well.
3239 mEventQueue.invalidateTransactionNow();
3240
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003241 // this creates a "fake" BBinder which will serve as a "fake" remote
3242 // binder to receive the marshaled calls and forward them to the
3243 // real remote (a BpGraphicBufferProducer)
3244 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3245
3246 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3247 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003248 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003249 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003250 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003251 useIdentityTransform, rotationFlags);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003252
3253 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003254 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003255 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003256 }
3257 return res;
3258}
3259
Mathias Agopian180f10d2013-04-10 22:55:41 -07003260
3261void SurfaceFlinger::renderScreenImplLocked(
3262 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003263 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003264 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003265 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003266{
3267 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003268 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003269
3270 // get screen geometry
3271 const uint32_t hw_w = hw->getWidth();
3272 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003273 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
3274
Dan Stozac1879002014-05-22 15:59:05 -07003275 // if a default or invalid sourceCrop is passed in, set reasonable values
3276 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3277 !sourceCrop.isValid()) {
3278 sourceCrop.setLeftTop(Point(0, 0));
3279 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3280 }
3281
3282 // ensure that sourceCrop is inside screen
3283 if (sourceCrop.left < 0) {
3284 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3285 }
Dan Stozabe31f442014-06-11 11:20:54 -07003286 if (sourceCrop.right > hw_w) {
3287 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003288 }
3289 if (sourceCrop.top < 0) {
3290 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3291 }
Dan Stozabe31f442014-06-11 11:20:54 -07003292 if (sourceCrop.bottom > hw_h) {
3293 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003294 }
3295
Mathias Agopian180f10d2013-04-10 22:55:41 -07003296 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003297 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003298
3299 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003300 engine.setViewportAndProjection(
3301 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003302 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003303
3304 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003305 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003306
3307 const LayerVector& layers( mDrawingState.layersSortedByZ );
3308 const size_t count = layers.size();
3309 for (size_t i=0 ; i<count ; ++i) {
3310 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003311 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003312 if (state.layerStack == hw->getLayerStack()) {
3313 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3314 if (layer->isVisible()) {
3315 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08003316 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003317 if (filtering) layer->setFiltering(false);
3318 }
3319 }
3320 }
3321 }
3322
3323 // compositionComplete is needed for older driver
3324 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003325 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003326}
3327
3328
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003329status_t SurfaceFlinger::captureScreenImplLocked(
3330 const sp<const DisplayDevice>& hw,
3331 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003332 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003333 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003334 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003335{
3336 ATRACE_CALL();
3337
Mathias Agopian180f10d2013-04-10 22:55:41 -07003338 // get screen geometry
3339 const uint32_t hw_w = hw->getWidth();
3340 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003341
Mathias Agopian180f10d2013-04-10 22:55:41 -07003342 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3343 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3344 reqWidth, reqHeight, hw_w, hw_h);
3345 return BAD_VALUE;
3346 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003347
Mathias Agopian180f10d2013-04-10 22:55:41 -07003348 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3349 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3350
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003351 // create a surface (because we're a producer, and we need to
3352 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003353 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003354 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003355
3356 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003357 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003358 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3359 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003360
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003361 int err = 0;
3362 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003363 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003364 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3365 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003366
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003367 if (err == NO_ERROR) {
3368 ANativeWindowBuffer* buffer;
3369 /* TODO: Once we have the sync framework everywhere this can use
3370 * server-side waits on the fence that dequeueBuffer returns.
3371 */
3372 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3373 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003374 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003375 // create an EGLImage from the buffer so we can later
3376 // turn it into a texture
3377 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3378 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3379 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003380 // this binds the given EGLImage as a framebuffer for the
3381 // duration of this scope.
3382 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3383 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003384 // this will in fact render into our dequeued buffer
3385 // via an FBO, which means we didn't have to create
3386 // an EGLSurface and therefore we're not
3387 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003388 renderScreenImplLocked(
3389 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3390 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003391
Riley Andrews86639902014-08-15 12:27:24 -07003392 // Attempt to create a sync khr object that can produce a sync point. If that
3393 // isn't available, create a non-dupable sync object in the fallback path and
3394 // wait on it directly.
3395 EGLSyncKHR sync;
3396 if (!DEBUG_SCREENSHOTS) {
3397 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Andy McFadden2d8d1202013-10-09 16:38:02 -07003398 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003399 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003400 }
Riley Andrews86639902014-08-15 12:27:24 -07003401 if (sync != EGL_NO_SYNC_KHR) {
3402 // get the sync fd
3403 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3404 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3405 ALOGW("captureScreen: failed to dup sync khr object");
3406 syncFd = -1;
3407 }
3408 eglDestroySyncKHR(mEGLDisplay, sync);
3409 } else {
3410 // fallback path
3411 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3412 if (sync != EGL_NO_SYNC_KHR) {
3413 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3414 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3415 EGLint eglErr = eglGetError();
3416 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3417 ALOGW("captureScreen: fence wait timed out");
3418 } else {
3419 ALOGW_IF(eglErr != EGL_SUCCESS,
3420 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3421 }
3422 eglDestroySyncKHR(mEGLDisplay, sync);
3423 } else {
3424 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3425 }
3426 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003427 if (DEBUG_SCREENSHOTS) {
3428 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3429 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3430 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3431 hw, minLayerZ, maxLayerZ);
3432 delete [] pixels;
3433 }
3434
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003435 } else {
3436 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3437 result = INVALID_OPERATION;
3438 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003439 // destroy our image
3440 eglDestroyImageKHR(mEGLDisplay, image);
3441 } else {
3442 result = BAD_VALUE;
3443 }
Jesse Hallafe2b1f2014-10-21 11:09:17 -07003444 // queueBuffer takes ownership of syncFd
Riley Andrews86639902014-08-15 12:27:24 -07003445 window->queueBuffer(window, buffer, syncFd);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003446 }
3447 } else {
3448 result = BAD_VALUE;
3449 }
3450 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3451 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003452
Mathias Agopian74c40c02010-09-29 13:02:36 -07003453 return result;
3454}
3455
Mathias Agopiand5556842013-09-19 17:08:37 -07003456void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3457 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003458 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003459 for (size_t y=0 ; y<h ; y++) {
3460 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3461 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003462 if (p[x] != 0xFF000000) return;
3463 }
3464 }
3465 ALOGE("*** we just took a black screenshot ***\n"
3466 "requested minz=%d, maxz=%d, layerStack=%d",
3467 minLayerZ, maxLayerZ, hw->getLayerStack());
3468 const LayerVector& layers( mDrawingState.layersSortedByZ );
3469 const size_t count = layers.size();
3470 for (size_t i=0 ; i<count ; ++i) {
3471 const sp<Layer>& layer(layers[i]);
3472 const Layer::State& state(layer->getDrawingState());
3473 const bool visible = (state.layerStack == hw->getLayerStack())
3474 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3475 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003476 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003477 visible ? '+' : '-',
3478 i, layer->getName().string(), state.layerStack, state.z,
3479 layer->isVisible(), state.flags, state.alpha);
3480 }
3481 }
3482}
3483
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003484// ---------------------------------------------------------------------------
3485
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003486SurfaceFlinger::LayerVector::LayerVector() {
3487}
3488
3489SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003490 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003491}
3492
3493int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3494 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003495{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003496 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003497 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3498 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003499
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003500 uint32_t ls = l->getCurrentState().layerStack;
3501 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003502 if (ls != rs)
3503 return ls - rs;
3504
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003505 uint32_t lz = l->getCurrentState().z;
3506 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003507 if (lz != rz)
3508 return lz - rz;
3509
3510 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003511}
3512
3513// ---------------------------------------------------------------------------
3514
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003515SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Michael Lentine47e45402014-07-18 15:34:25 -07003516 : type(DisplayDevice::DISPLAY_ID_INVALID), width(0), height(0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003517}
3518
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003519SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Michael Lentine47e45402014-07-18 15:34:25 -07003520 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0), width(0), height(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003521 viewport.makeInvalid();
3522 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003523}
3524
3525// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003526
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003527}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003528
3529
3530#if defined(__gl_h_)
3531#error "don't include gl/gl.h in this file"
3532#endif
3533
3534#if defined(__gl2_h_)
3535#error "don't include gl2/gl2.h in this file"
3536#endif