blob: d01b945e42f30d57a6f6e9f36d3ad8457d1ea000 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Greg Hackmann86efcc02014-03-07 12:44:02 -080024#include <inttypes.h>
Jesse Hallb154c422014-07-13 12:47:02 -070025#include <stdatomic.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070026
27#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036
Mathias Agopianc666cae2012-07-25 18:56:13 -070037#include <ui/DisplayInfo.h>
Lajos Molnar67d8bd62014-09-11 14:58:45 -070038#include <ui/DisplayStatInfo.h>
Mathias Agopianc666cae2012-07-25 18:56:13 -070039
Mathias Agopian921e6ac2012-07-23 23:11:29 -070040#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070041#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070042#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070043#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080044#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080045#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070046
47#include <ui/GraphicBufferAllocator.h>
48#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070049#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080050
Mathias Agopiancde87a32012-09-13 14:09:01 -070051#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052#include <utils/String8.h>
53#include <utils/String16.h>
54#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080055#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopian921e6ac2012-07-23 23:11:29 -070057#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070058#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059
Mathias Agopian3e25fd82013-04-22 17:52:16 +020060#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020062#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080063#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070064#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070065#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070066#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080067#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071
Mathias Agopiana4912602012-07-12 14:25:33 -070072#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070073#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070074#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075
Mathias Agopianff2ed702013-09-01 21:36:12 -070076#include "Effects/Daltonizer.h"
77
Mathias Agopian875d8e12013-06-07 15:35:48 -070078#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070079#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070080
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081#define DISPLAY_COUNT 1
82
Mathias Agopianfee2b462013-07-03 12:34:01 -070083/*
84 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
85 * black pixels.
86 */
87#define DEBUG_SCREENSHOTS false
88
Mathias Agopianca088332013-03-28 17:44:13 -070089EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
90
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070092
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070093// This is the phase offset in nanoseconds of the software vsync event
94// relative to the vsync event reported by HWComposer. The software vsync
95// event is when SurfaceFlinger and Choreographer-based applications run each
96// frame.
97//
98// This phase offset allows adjustment of the minimum latency from application
99// wake-up (by Choregographer) time to the time at which the resulting window
100// image is displayed. This value may be either positive (after the HW vsync)
101// or negative (before the HW vsync). Setting it to 0 will result in a
102// minimum latency of two vsync periods because the app and SurfaceFlinger
103// will run just after the HW vsync. Setting it to a positive number will
104// result in the minimum latency being:
105//
106// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
107//
108// Note that reducing this latency makes it more likely for the applications
109// to not have their window content image ready in time. When this happens
110// the latency will end up being an additional vsync period, and animations
111// will hiccup. Therefore, this latency should be tuned somewhat
112// conservatively (or at least with awareness of the trade-off being made).
113static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
114
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700115// This is the phase offset at which SurfaceFlinger's composition runs.
116static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
117
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118// ---------------------------------------------------------------------------
119
Mathias Agopian99b49842011-06-27 16:05:52 -0700120const String16 sHardwareTest("android.permission.HARDWARE_TEST");
121const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
122const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
123const String16 sDump("android.permission.DUMP");
124
125// ---------------------------------------------------------------------------
126
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700128 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700130 mTransactionPending(false),
131 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700132 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700133 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700134 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700137 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800138 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700140 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700141 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700142 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700143 mDebugInSwapBuffers(0),
144 mLastSwapBufferTime(0),
145 mDebugInTransaction(0),
146 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700147 mBootFinished(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700148 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700149 mHWVsyncAvailable(false),
Alan Viverette9c5a3332013-09-12 20:04:35 -0700150 mDaltonize(false),
Dan Stozab90cf072015-03-05 11:05:59 -0800151 mHasColorMatrix(false),
152 mHasPoweredOff(false),
153 mFrameBuckets(),
154 mTotalTime(0),
155 mLastSwapTime(0)
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 Hall692c7232012-11-08 15:41:56 -0800395 Mutex::Autolock _l(mStateLock);
396
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700397 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700398 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
399 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700400
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700401 // Initialize the H/W composer object. There may or may not be an
402 // actual hardware composer underneath.
403 mHwc = new HWComposer(this,
404 *static_cast<HWComposer::EventHandler *>(this));
405
Mathias Agopian875d8e12013-06-07 15:35:48 -0700406 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800407 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700408
409 // retrieve the EGL context that was selected/created
410 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700411
Mathias Agopianda27af92012-09-13 18:17:13 -0700412 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
413 "couldn't create EGLContext");
414
Mathias Agopiancde87a32012-09-13 14:09:01 -0700415 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700416 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700417 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700418 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700419 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700420 // All non-virtual displays are currently considered secure.
421 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800422 createBuiltinDisplayLocked(type);
423 wp<IBinder> token = mBuiltinDisplays[i];
424
Dan Stozab9b08832014-03-13 11:55:57 -0700425 sp<IGraphicBufferProducer> producer;
426 sp<IGraphicBufferConsumer> consumer;
427 BufferQueue::createBufferQueue(&producer, &consumer,
428 new GraphicBufferAlloc());
429
430 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
431 consumer);
Jesse Hall19e87292013-12-23 21:02:15 -0800432 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700433 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800434 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Dan Stozab9b08832014-03-13 11:55:57 -0700435 fbs, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -0800436 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700437 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700438 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700439 // for displays other than the main display, so we always
440 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800441 ALOGD("marking display %zu as acquired/unblanked", i);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700442 hw->setPowerMode(HWC_POWER_MODE_NORMAL);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700443 }
444 mDisplays.add(token, hw);
445 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700446 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700447
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700448 // make the GLContext current so that we can create textures when creating Layers
449 // (which may happens before we render something)
450 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
451
Mathias Agopian028508c2012-07-25 21:12:12 -0700452 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700453 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700454 vsyncPhaseOffsetNs, true, "app");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700455 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700456 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700457 sfVsyncPhaseOffsetNs, true, "sf");
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700458 mSFEventThread = new EventThread(sfVsyncSrc);
459 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700460
Jamie Gennisd1700752013-10-14 12:22:52 -0700461 mEventControlThread = new EventControlThread(this);
462 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
463
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700464 // set a fake vsync period if there is no HWComposer
465 if (mHwc->initCheck() != NO_ERROR) {
466 mPrimaryDispSync.setPeriod(16666667);
467 }
468
Mathias Agopian92a979a2012-08-02 18:32:23 -0700469 // initialize our drawing state
470 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700471
Andy McFadden13a082e2012-08-24 10:16:42 -0700472 // set initial conditions (e.g. unblank default device)
473 initializeDisplays();
474
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700475 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700476 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800477}
478
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700479int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700480 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700481 type : mHwc->allocateDisplayId();
482}
483
Mathias Agopiana67e4182012-06-19 17:26:12 -0700484void SurfaceFlinger::startBootAnim() {
485 // start boot animation
486 property_set("service.bootanim.exit", "0");
487 property_set("ctl.start", "bootanim");
488}
489
Mathias Agopian875d8e12013-06-07 15:35:48 -0700490size_t SurfaceFlinger::getMaxTextureSize() const {
491 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700492}
493
Mathias Agopian875d8e12013-06-07 15:35:48 -0700494size_t SurfaceFlinger::getMaxViewportDims() const {
495 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700496}
497
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800498// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800499
Jamie Gennis582270d2011-08-17 18:19:00 -0700500bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800501 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800502 Mutex::Autolock _l(mStateLock);
Marco Nelissen097ca272014-11-14 08:01:01 -0800503 sp<IBinder> surfaceTextureBinder(IInterface::asBinder(bufferProducer));
Mathias Agopian67106042013-03-14 19:18:13 -0700504 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800505}
506
Dan Stoza7f7da322014-05-02 15:26:25 -0700507status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
508 Vector<DisplayInfo>* configs) {
509 if (configs == NULL) {
510 return BAD_VALUE;
511 }
512
Naseer Ahmed7aa0c472014-11-03 14:49:23 -0500513 if (!display.get())
514 return NAME_NOT_FOUND;
515
Jesse Hall692c7232012-11-08 15:41:56 -0800516 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700517 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800518 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700519 type = i;
520 break;
521 }
522 }
523
524 if (type < 0) {
525 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700526 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700527
Mathias Agopian8b736f12012-08-13 17:54:26 -0700528 // TODO: Not sure if display density should handled by SF any longer
529 class Density {
530 static int getDensityFromProperty(char const* propName) {
531 char property[PROPERTY_VALUE_MAX];
532 int density = 0;
533 if (property_get(propName, property, NULL) > 0) {
534 density = atoi(property);
535 }
536 return density;
537 }
538 public:
539 static int getEmuDensity() {
540 return getDensityFromProperty("qemu.sf.lcd_density"); }
541 static int getBuildDensity() {
542 return getDensityFromProperty("ro.sf.lcd_density"); }
543 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700544
Dan Stoza7f7da322014-05-02 15:26:25 -0700545 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700546
Dan Stoza7f7da322014-05-02 15:26:25 -0700547 const Vector<HWComposer::DisplayConfig>& hwConfigs =
548 getHwComposer().getConfigs(type);
549 for (size_t c = 0; c < hwConfigs.size(); ++c) {
550 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
551 DisplayInfo info = DisplayInfo();
552
553 float xdpi = hwConfig.xdpi;
554 float ydpi = hwConfig.ydpi;
555
556 if (type == DisplayDevice::DISPLAY_PRIMARY) {
557 // The density of the device is provided by a build property
558 float density = Density::getBuildDensity() / 160.0f;
559 if (density == 0) {
560 // the build doesn't provide a density -- this is wrong!
561 // use xdpi instead
562 ALOGE("ro.sf.lcd_density must be defined as a build property");
563 density = xdpi / 160.0f;
564 }
565 if (Density::getEmuDensity()) {
566 // if "qemu.sf.lcd_density" is specified, it overrides everything
567 xdpi = ydpi = density = Density::getEmuDensity();
568 density /= 160.0f;
569 }
570 info.density = density;
571
572 // TODO: this needs to go away (currently needed only by webkit)
573 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
574 info.orientation = hw->getOrientation();
575 } else {
576 // TODO: where should this value come from?
577 static const int TV_DENSITY = 213;
578 info.density = TV_DENSITY / 160.0f;
579 info.orientation = 0;
580 }
581
582 info.w = hwConfig.width;
583 info.h = hwConfig.height;
584 info.xdpi = xdpi;
585 info.ydpi = ydpi;
586 info.fps = float(1e9 / hwConfig.refresh);
Andy McFadden91b2ca82014-06-13 14:04:23 -0700587 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
588
589 // This is how far in advance a buffer must be queued for
590 // presentation at a given time. If you want a buffer to appear
591 // on the screen at time N, you must submit the buffer before
592 // (N - presentationDeadline).
593 //
594 // Normally it's one full refresh period (to give SF a chance to
595 // latch the buffer), but this can be reduced by configuring a
596 // DispSync offset. Any additional delays introduced by the hardware
597 // composer or panel must be accounted for here.
598 //
599 // We add an additional 1ms to allow for processing time and
600 // differences between the ideal and actual refresh rate.
601 info.presentationDeadline =
602 hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700603
604 // All non-virtual displays are currently considered secure.
605 info.secure = true;
606
607 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700608 }
609
Dan Stoza7f7da322014-05-02 15:26:25 -0700610 return NO_ERROR;
611}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700612
Andreas Gampe89fd4f72014-11-13 14:18:56 -0800613status_t SurfaceFlinger::getDisplayStats(const sp<IBinder>& /* display */,
Lajos Molnar67d8bd62014-09-11 14:58:45 -0700614 DisplayStatInfo* stats) {
615 if (stats == NULL) {
616 return BAD_VALUE;
617 }
618
619 // FIXME for now we always return stats for the primary display
620 memset(stats, 0, sizeof(*stats));
621 stats->vsyncTime = mPrimaryDispSync.computeNextRefresh(0);
622 stats->vsyncPeriod = mPrimaryDispSync.getPeriod();
623 return NO_ERROR;
624}
625
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700626int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
627 return getDisplayDevice(display)->getActiveConfig();
Dan Stoza7f7da322014-05-02 15:26:25 -0700628}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700629
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700630void SurfaceFlinger::setActiveConfigInternal(const sp<DisplayDevice>& hw, int mode) {
631 ALOGD("Set active config mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
632 this);
633 int32_t type = hw->getDisplayType();
634 int currentMode = hw->getActiveConfig();
635
636 if (mode == currentMode) {
637 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
638 return;
639 }
640
641 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
642 ALOGW("Trying to set config for virtual display");
643 return;
644 }
645
646 hw->setActiveConfig(mode);
647 getHwComposer().setActiveConfig(type, mode);
648}
649
650status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int mode) {
651 class MessageSetActiveConfig: public MessageBase {
652 SurfaceFlinger& mFlinger;
653 sp<IBinder> mDisplay;
654 int mMode;
655 public:
656 MessageSetActiveConfig(SurfaceFlinger& flinger, const sp<IBinder>& disp,
657 int mode) :
658 mFlinger(flinger), mDisplay(disp) { mMode = mode; }
659 virtual bool handler() {
Michael Lentine7306c672014-07-30 13:00:37 -0700660 Vector<DisplayInfo> configs;
661 mFlinger.getDisplayConfigs(mDisplay, &configs);
Jesse Hall78442112014-08-07 22:43:06 -0700662 if (mMode < 0 || mMode >= static_cast<int>(configs.size())) {
Michael Lentine9ae79d82014-07-30 16:42:12 -0700663 ALOGE("Attempt to set active config = %d for display with %zu configs",
Michael Lentine7306c672014-07-30 13:00:37 -0700664 mMode, configs.size());
665 }
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700666 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
667 if (hw == NULL) {
668 ALOGE("Attempt to set active config = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -0700669 mMode, mDisplay.get());
Michael Lentine6c9e34a2014-07-14 13:48:55 -0700670 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
671 ALOGW("Attempt to set active config = %d for virtual display",
672 mMode);
673 } else {
674 mFlinger.setActiveConfigInternal(hw, mMode);
675 }
676 return true;
677 }
678 };
679 sp<MessageBase> msg = new MessageSetActiveConfig(*this, display, mode);
680 postMessageSync(msg);
Mathias Agopian888c8222012-08-04 21:10:38 -0700681 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700682}
683
Svetoslavd85084b2014-03-20 10:28:31 -0700684status_t SurfaceFlinger::clearAnimationFrameStats() {
685 Mutex::Autolock _l(mStateLock);
686 mAnimFrameTracker.clearStats();
687 return NO_ERROR;
688}
689
690status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
691 Mutex::Autolock _l(mStateLock);
692 mAnimFrameTracker.getStats(outStats);
693 return NO_ERROR;
694}
695
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800696// ----------------------------------------------------------------------------
697
698sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800699 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700700}
701
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800702// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800703
704void SurfaceFlinger::waitForEvent() {
705 mEventQueue.waitMessage();
706}
707
708void SurfaceFlinger::signalTransaction() {
709 mEventQueue.invalidate();
710}
711
712void SurfaceFlinger::signalLayerUpdate() {
713 mEventQueue.invalidate();
714}
715
716void SurfaceFlinger::signalRefresh() {
717 mEventQueue.refresh();
718}
719
720status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800721 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800722 return mEventQueue.postMessage(msg, reltime);
723}
724
725status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800726 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800727 status_t res = mEventQueue.postMessage(msg, reltime);
728 if (res == NO_ERROR) {
729 msg->wait();
730 }
731 return res;
732}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800733
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700734void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700735 do {
736 waitForEvent();
737 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800738}
739
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700740void SurfaceFlinger::enableHardwareVsync() {
741 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700742 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700743 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700744 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
745 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700746 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700747 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700748}
749
Jesse Hall948fe0c2013-10-14 12:56:09 -0700750void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700751 Mutex::Autolock _l(mHWVsyncLock);
752
Jesse Hall948fe0c2013-10-14 12:56:09 -0700753 if (makeAvailable) {
754 mHWVsyncAvailable = true;
755 } else if (!mHWVsyncAvailable) {
756 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
757 return;
758 }
759
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700760 const nsecs_t period =
761 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
762
763 mPrimaryDispSync.reset();
764 mPrimaryDispSync.setPeriod(period);
765
766 if (!mPrimaryHWVsyncEnabled) {
767 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700768 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
769 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700770 mPrimaryHWVsyncEnabled = true;
771 }
772}
773
Jesse Hall948fe0c2013-10-14 12:56:09 -0700774void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700775 Mutex::Autolock _l(mHWVsyncLock);
776 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700777 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
778 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700779 mPrimaryDispSync.endResync();
780 mPrimaryHWVsyncEnabled = false;
781 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700782 if (makeUnavailable) {
783 mHWVsyncAvailable = false;
784 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700785}
786
787void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700788 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700789
Jamie Gennisd1700752013-10-14 12:22:52 -0700790 { // Scope for the lock
791 Mutex::Autolock _l(mHWVsyncLock);
792 if (type == 0 && mPrimaryHWVsyncEnabled) {
793 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700794 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700795 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700796
797 if (needsHwVsync) {
798 enableHardwareVsync();
799 } else {
800 disableHardwareVsync(false);
801 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700802}
803
804void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
805 if (mEventThread == NULL) {
806 // This is a temporary workaround for b/7145521. A non-null pointer
807 // does not mean EventThread has finished initializing, so this
808 // is not a correct fix.
809 ALOGW("WARNING: EventThread not started, ignoring hotplug");
810 return;
811 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700812
Jesse Hall9e663de2013-08-16 14:28:37 -0700813 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700814 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800815 if (connected) {
816 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700817 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800818 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
819 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700820 }
821 setTransactionFlags(eDisplayTransactionNeeded);
822
Andy McFadden9e9689c2012-10-10 18:17:51 -0700823 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700824 }
Mathias Agopian86303202012-07-24 22:46:10 -0700825}
826
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700827void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700828 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700829 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700830}
831
Mathias Agopian4fec8732012-06-29 14:12:52 -0700832void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800833 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800834 switch (what) {
Dan Stoza6b9454d2014-11-07 16:00:59 -0800835 case MessageQueue::TRANSACTION: {
836 handleMessageTransaction();
837 break;
838 }
839 case MessageQueue::INVALIDATE: {
840 bool refreshNeeded = handleMessageTransaction();
841 refreshNeeded |= handleMessageInvalidate();
Dan Stoza58784442014-12-02 16:58:17 -0800842 refreshNeeded |= mRepaintEverything;
Dan Stoza6b9454d2014-11-07 16:00:59 -0800843 if (refreshNeeded) {
Dan Stoza58784442014-12-02 16:58:17 -0800844 // Signal a refresh if a transaction modified the window state,
845 // a new buffer was latched, or if HWC has requested a full
846 // repaint
Dan Stoza6b9454d2014-11-07 16:00:59 -0800847 signalRefresh();
848 }
849 break;
850 }
851 case MessageQueue::REFRESH: {
852 handleMessageRefresh();
853 break;
854 }
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800855 }
856}
857
Dan Stoza6b9454d2014-11-07 16:00:59 -0800858bool SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700859 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700860 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700861 handleTransaction(transactionFlags);
Dan Stoza6b9454d2014-11-07 16:00:59 -0800862 return true;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700863 }
Dan Stoza6b9454d2014-11-07 16:00:59 -0800864 return false;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700865}
866
Dan Stoza6b9454d2014-11-07 16:00:59 -0800867bool SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700868 ATRACE_CALL();
Dan Stoza6b9454d2014-11-07 16:00:59 -0800869 return handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700870}
871
872void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700873 ATRACE_CALL();
874 preComposition();
875 rebuildLayerStacks();
876 setUpHWComposer();
877 doDebugFlashRegions();
878 doComposition();
879 postComposition();
880}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700881
Mathias Agopiancd60f992012-08-16 16:28:27 -0700882void SurfaceFlinger::doDebugFlashRegions()
883{
884 // is debugging enabled
885 if (CC_LIKELY(!mDebugRegion))
886 return;
887
888 const bool repaintEverything = mRepaintEverything;
889 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
890 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700891 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700892 // transform the dirty region into this screen's coordinate space
893 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
894 if (!dirtyRegion.isEmpty()) {
895 // redraw the whole screen
896 doComposeSurfaces(hw, Region(hw->bounds()));
897
898 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700899 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700900 RenderEngine& engine(getRenderEngine());
901 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
902
Mathias Agopiancd60f992012-08-16 16:28:27 -0700903 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700904 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700905 }
906 }
907 }
908
909 postFramebuffer();
910
911 if (mDebugRegion > 1) {
912 usleep(mDebugRegion * 1000);
913 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700914
915 HWComposer& hwc(getHwComposer());
916 if (hwc.initCheck() == NO_ERROR) {
917 status_t err = hwc.prepare();
918 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
919 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700920}
921
922void SurfaceFlinger::preComposition()
923{
924 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700925 const LayerVector& layers(mDrawingState.layersSortedByZ);
926 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700927 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700928 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700929 needExtraInvalidate = true;
930 }
931 }
932 if (needExtraInvalidate) {
933 signalLayerUpdate();
934 }
935}
936
937void SurfaceFlinger::postComposition()
938{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700939 const LayerVector& layers(mDrawingState.layersSortedByZ);
940 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700941 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700942 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700943 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800944
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700945 const HWComposer& hwc = getHwComposer();
946 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
947
948 if (presentFence->isValid()) {
949 if (mPrimaryDispSync.addPresentFence(presentFence)) {
950 enableHardwareVsync();
951 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700952 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700953 }
954 }
955
Dan Stozab90cf072015-03-05 11:05:59 -0800956 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Andy McFadden5167ec62014-05-22 13:08:43 -0700957 if (kIgnorePresentFences) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700958 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700959 enableHardwareVsync();
960 }
961 }
962
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800963 if (mAnimCompositionPending) {
964 mAnimCompositionPending = false;
965
Jesse Halla9a1b002013-02-27 16:39:25 -0800966 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800967 mAnimFrameTracker.setActualPresentFence(presentFence);
968 } else {
969 // The HWC doesn't support present fences, so use the refresh
970 // timestamp instead.
971 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
972 mAnimFrameTracker.setActualPresentTime(presentTime);
973 }
974 mAnimFrameTracker.advanceFrame();
975 }
Dan Stozab90cf072015-03-05 11:05:59 -0800976
977 if (hw->getPowerMode() == HWC_POWER_MODE_OFF) {
978 return;
979 }
980
981 nsecs_t currentTime = systemTime();
982 if (mHasPoweredOff) {
983 mHasPoweredOff = false;
984 } else {
985 nsecs_t period = mPrimaryDispSync.getPeriod();
986 nsecs_t elapsedTime = currentTime - mLastSwapTime;
987 size_t numPeriods = static_cast<size_t>(elapsedTime / period);
988 if (numPeriods < NUM_BUCKETS - 1) {
989 mFrameBuckets[numPeriods] += elapsedTime;
990 } else {
991 mFrameBuckets[NUM_BUCKETS - 1] += elapsedTime;
992 }
993 mTotalTime += elapsedTime;
994 }
995 mLastSwapTime = currentTime;
Mathias Agopiancd60f992012-08-16 16:28:27 -0700996}
997
998void SurfaceFlinger::rebuildLayerStacks() {
999 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001000 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001001 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -07001002 mVisibleRegionsDirty = false;
1003 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001004
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001005 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001006 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001007 Region opaqueRegion;
1008 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -08001009 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -07001010 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001011 const Transform& tr(hw->getTransform());
1012 const Rect bounds(hw->getBounds());
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001013 if (hw->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001014 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001015 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001016
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001017 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001018 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001019 const sp<Layer>& layer(layers[i]);
1020 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001021 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -07001022 Region drawRegion(tr.transform(
1023 layer->visibleNonTransparentRegion));
1024 drawRegion.andSelf(bounds);
1025 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001026 layersSortedByZ.add(layer);
1027 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001028 }
1029 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001030 }
Mathias Agopian42977342012-08-05 00:40:46 -07001031 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001032 hw->undefinedRegion.set(bounds);
1033 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
1034 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001035 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001036 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001037}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001038
Mathias Agopiancd60f992012-08-16 16:28:27 -07001039void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001040 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Jesse Hallb7a05492014-08-14 15:45:06 -07001041 bool dirty = !mDisplays[dpy]->getDirtyRegion(false).isEmpty();
1042 bool empty = mDisplays[dpy]->getVisibleLayersSortedByZ().size() == 0;
1043 bool wasEmpty = !mDisplays[dpy]->lastCompositionHadVisibleLayers;
1044
1045 // If nothing has changed (!dirty), don't recompose.
1046 // If something changed, but we don't currently have any visible layers,
1047 // and didn't when we last did a composition, then skip it this time.
1048 // The second rule does two things:
1049 // - When all layers are removed from a display, we'll emit one black
1050 // frame, then nothing more until we get new layers.
1051 // - When a display is created with a private layer stack, we won't
1052 // emit any black frames until a layer is added to the layer stack.
1053 bool mustRecompose = dirty && !(empty && wasEmpty);
1054
1055 ALOGV_IF(mDisplays[dpy]->getDisplayType() == DisplayDevice::DISPLAY_VIRTUAL,
1056 "dpy[%zu]: %s composition (%sdirty %sempty %swasEmpty)", dpy,
1057 mustRecompose ? "doing" : "skipping",
1058 dirty ? "+" : "-",
1059 empty ? "+" : "-",
1060 wasEmpty ? "+" : "-");
1061
Dan Stoza71433162014-02-04 16:22:36 -08001062 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hallb7a05492014-08-14 15:45:06 -07001063
1064 if (mustRecompose) {
1065 mDisplays[dpy]->lastCompositionHadVisibleLayers = !empty;
1066 }
Jesse Hall028dc8f2013-08-20 16:35:32 -07001067 }
1068
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001069 HWComposer& hwc(getHwComposer());
1070 if (hwc.initCheck() == NO_ERROR) {
1071 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001072 if (CC_UNLIKELY(mHwWorkListDirty)) {
1073 mHwWorkListDirty = false;
1074 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1075 sp<const DisplayDevice> hw(mDisplays[dpy]);
1076 const int32_t id = hw->getHwcDisplayId();
1077 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001078 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001079 hw->getVisibleLayersSortedByZ());
1080 const size_t count = currentLayers.size();
1081 if (hwc.createWorkList(id, count) == NO_ERROR) {
1082 HWComposer::LayerListIterator cur = hwc.begin(id);
1083 const HWComposer::LayerListIterator end = hwc.end(id);
1084 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001085 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001086 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001087 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001088 cur->setSkip(true);
1089 }
1090 }
1091 }
1092 }
1093 }
1094 }
1095
1096 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001097 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001098 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001099 const int32_t id = hw->getHwcDisplayId();
1100 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001101 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001102 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001103 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001104 HWComposer::LayerListIterator cur = hwc.begin(id);
1105 const HWComposer::LayerListIterator end = hwc.end(id);
1106 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1107 /*
1108 * update the per-frame h/w composer data for each layer
1109 * and build the transparent region of the FB
1110 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001111 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001112 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -07001113 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001114 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001115 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001116
Riley Andrews03414a12014-07-01 14:22:59 -07001117 // If possible, attempt to use the cursor overlay on each display.
1118 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1119 sp<const DisplayDevice> hw(mDisplays[dpy]);
1120 const int32_t id = hw->getHwcDisplayId();
1121 if (id >= 0) {
1122 const Vector< sp<Layer> >& currentLayers(
1123 hw->getVisibleLayersSortedByZ());
1124 const size_t count = currentLayers.size();
1125 HWComposer::LayerListIterator cur = hwc.begin(id);
1126 const HWComposer::LayerListIterator end = hwc.end(id);
1127 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1128 const sp<Layer>& layer(currentLayers[i]);
1129 if (layer->isPotentialCursor()) {
1130 cur->setIsCursorLayerHint();
1131 break;
1132 }
1133 }
1134 }
1135 }
1136
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001137 status_t err = hwc.prepare();
1138 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001139
1140 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1141 sp<const DisplayDevice> hw(mDisplays[dpy]);
1142 hw->prepareFrame(hwc);
1143 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001144 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001145}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001146
Mathias Agopiancd60f992012-08-16 16:28:27 -07001147void SurfaceFlinger::doComposition() {
1148 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001149 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001150 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001151 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001152 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001153 // transform the dirty region into this screen's coordinate space
1154 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001155
1156 // repaint the framebuffer (if needed)
1157 doDisplayComposition(hw, dirtyRegion);
1158
Mathias Agopiancd60f992012-08-16 16:28:27 -07001159 hw->dirtyRegion.clear();
1160 hw->flip(hw->swapRegion);
1161 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001162 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001163 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001164 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001165 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001166 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001167}
1168
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169void SurfaceFlinger::postFramebuffer()
1170{
Mathias Agopian841cde52012-03-01 15:44:37 -08001171 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001172
Mathias Agopiana44b0412011-10-16 18:46:35 -07001173 const nsecs_t now = systemTime();
1174 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001175
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001176 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001177 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001178 if (!hwc.supportsFramebufferTarget()) {
1179 // EGL spec says:
1180 // "surface must be bound to the calling thread's current context,
1181 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001182 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001183 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001184 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001185 }
1186
Mathias Agopian6da15f42013-09-25 20:40:07 -07001187 // make the default display current because the VirtualDisplayDevice code cannot
1188 // deal with dequeueBuffer() being called outside of the composition loop; however
1189 // the code below can call glFlush() which is allowed (and does in some case) call
1190 // dequeueBuffer().
1191 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1192
Mathias Agopian92a979a2012-08-02 18:32:23 -07001193 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001194 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001195 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001196 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001197 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001198 int32_t id = hw->getHwcDisplayId();
1199 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001200 HWComposer::LayerListIterator cur = hwc.begin(id);
1201 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001202 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001203 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001204 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001205 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001206 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001207 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001208 }
Jesse Hallef194142012-06-14 14:45:17 -07001209 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001210 }
1211
Mathias Agopiana44b0412011-10-16 18:46:35 -07001212 mLastSwapBufferTime = systemTime() - now;
1213 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001214
1215 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1216 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1217 logFrameStats();
1218 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001219}
1220
Mathias Agopian87baae12012-07-31 12:38:26 -07001221void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222{
Mathias Agopian841cde52012-03-01 15:44:37 -08001223 ATRACE_CALL();
1224
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001225 // here we keep a copy of the drawing state (that is the state that's
1226 // going to be overwritten by handleTransactionLocked()) outside of
1227 // mStateLock so that the side-effects of the State assignment
1228 // don't happen with mStateLock held (which can cause deadlocks).
1229 State drawingState(mDrawingState);
1230
Mathias Agopianca4d3602011-05-19 15:38:14 -07001231 Mutex::Autolock _l(mStateLock);
1232 const nsecs_t now = systemTime();
1233 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001234
Mathias Agopianca4d3602011-05-19 15:38:14 -07001235 // Here we're guaranteed that some transaction flags are set
1236 // so we can call handleTransactionLocked() unconditionally.
1237 // We call getTransactionFlags(), which will also clear the flags,
1238 // with mStateLock held to guarantee that mCurrentState won't change
1239 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001240
Mathias Agopiane57f2922012-08-09 16:29:12 -07001241 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001242 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001243
Mathias Agopianca4d3602011-05-19 15:38:14 -07001244 mLastTransactionTime = systemTime() - now;
1245 mDebugInTransaction = 0;
1246 invalidateHwcGeometry();
1247 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001248}
1249
Mathias Agopian87baae12012-07-31 12:38:26 -07001250void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001251{
1252 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001253 const size_t count = currentLayers.size();
1254
1255 /*
1256 * Traversal of the children
1257 * (perform the transaction for each of them if needed)
1258 */
1259
Mathias Agopian3559b072012-08-15 13:46:03 -07001260 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001262 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1264 if (!trFlags) continue;
1265
1266 const uint32_t flags = layer->doTransaction(0);
1267 if (flags & Layer::eVisibleRegion)
1268 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001269 }
1270 }
1271
1272 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001273 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001274 */
1275
Mathias Agopiane57f2922012-08-09 16:29:12 -07001276 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001277 // here we take advantage of Vector's copy-on-write semantics to
1278 // improve performance by skipping the transaction entirely when
1279 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001280 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1281 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001282 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001284 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001285 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001286
1287 // find the displays that were removed
1288 // (ie: in drawing state but not in current state)
1289 // also handle displays that changed
1290 // (ie: displays that are in both lists)
1291 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001292 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1293 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001294 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001295 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001296 // Call makeCurrent() on the primary display so we can
1297 // be sure that nothing associated with this display
1298 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001299 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001300 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001301 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1302 if (hw != NULL)
1303 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001304 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001305 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001306 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001307 } else {
1308 ALOGW("trying to remove the main display");
1309 }
1310 } else {
1311 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001312 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001313 const wp<IBinder>& display(curr.keyAt(j));
Marco Nelissen097ca272014-11-14 08:01:01 -08001314 const sp<IBinder> state_binder = IInterface::asBinder(state.surface);
1315 const sp<IBinder> draw_binder = IInterface::asBinder(draw[i].surface);
Dan Albert1474f882014-09-08 18:59:09 -07001316 if (state_binder != draw_binder) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001317 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001318 // recreating the DisplayDevice, so we just remove it
1319 // from the drawing state, so that it get re-added
1320 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001321 sp<DisplayDevice> hw(getDisplayDevice(display));
1322 if (hw != NULL)
1323 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001324 mDisplays.removeItem(display);
1325 mDrawingState.displays.removeItemsAt(i);
1326 dc--; i--;
1327 // at this point we must loop to the next item
1328 continue;
1329 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001330
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001331 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001332 if (disp != NULL) {
1333 if (state.layerStack != draw[i].layerStack) {
1334 disp->setLayerStack(state.layerStack);
1335 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001336 if ((state.orientation != draw[i].orientation)
1337 || (state.viewport != draw[i].viewport)
1338 || (state.frame != draw[i].frame))
1339 {
1340 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001341 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001342 }
Michael Lentine47e45402014-07-18 15:34:25 -07001343 if (state.width != draw[i].width || state.height != draw[i].height) {
1344 disp->setDisplaySize(state.width, state.height);
1345 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001346 }
1347 }
1348 }
1349
1350 // find displays that were added
1351 // (ie: in current state but not in drawing state)
1352 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001353 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001354 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001355
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001356 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001357 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001358 sp<IGraphicBufferProducer> bqProducer;
1359 sp<IGraphicBufferConsumer> bqConsumer;
1360 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1361 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001362
Jesse Hall02d86562013-03-25 14:43:23 -07001363 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001364 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001365 // Virtual displays without a surface are dormant:
1366 // they have external state (layer stack, projection,
1367 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001368 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001369
Dan Stoza1f3efb12014-10-15 16:34:55 -07001370 int width = 0;
1371 int status = state.surface->query(
1372 NATIVE_WINDOW_WIDTH, &width);
1373 ALOGE_IF(status != NO_ERROR,
1374 "Unable to query width (%d)", status);
1375 int height = 0;
1376 status = state.surface->query(
1377 NATIVE_WINDOW_HEIGHT, &height);
1378 ALOGE_IF(status != NO_ERROR,
1379 "Unable to query height (%d)", status);
1380 if (MAX_VIRTUAL_DISPLAY_DIMENSION == 0 ||
1381 (width <= MAX_VIRTUAL_DISPLAY_DIMENSION &&
1382 height <= MAX_VIRTUAL_DISPLAY_DIMENSION)) {
1383 hwcDisplayId = allocateHwcDisplayId(state.type);
1384 }
1385
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001386 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
Dan Stozab9b08832014-03-13 11:55:57 -07001387 *mHwc, hwcDisplayId, state.surface,
1388 bqProducer, bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001389
1390 dispSurface = vds;
Michael Lentine47e45402014-07-18 15:34:25 -07001391 producer = vds;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001392 }
1393 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001394 ALOGE_IF(state.surface!=NULL,
1395 "adding a supported display, but rendering "
1396 "surface is provided (%p), ignoring it",
1397 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001398 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001399 // for supported (by hwc) displays we provide our
1400 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001401 dispSurface = new FramebufferSurface(*mHwc, state.type,
1402 bqConsumer);
1403 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001404 }
1405
1406 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001407 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001408 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001409 state.type, hwcDisplayId,
1410 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001411 display, dispSurface, producer,
1412 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001413 hw->setLayerStack(state.layerStack);
1414 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001415 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001416 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001417 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001418 if (state.isVirtualDisplay()) {
1419 if (hwcDisplayId >= 0) {
1420 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1421 hw->getWidth(), hw->getHeight(),
1422 hw->getFormat());
1423 }
1424 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001425 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001426 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001427 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001428 }
1429 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001430 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001431 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001432
Mathias Agopian84300952012-11-21 16:02:13 -08001433 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1434 // The transform hint might have changed for some layers
1435 // (either because a display has changed, or because a layer
1436 // as changed).
1437 //
1438 // Walk through all the layers in currentLayers,
1439 // and update their transform hint.
1440 //
1441 // If a layer is visible only on a single display, then that
1442 // display is used to calculate the hint, otherwise we use the
1443 // default display.
1444 //
1445 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1446 // the hint is set before we acquire a buffer from the surface texture.
1447 //
1448 // NOTE: layer transactions have taken place already, so we use their
1449 // drawing state. However, SurfaceFlinger's own transaction has not
1450 // happened yet, so we must use the current state layer list
1451 // (soon to become the drawing state list).
1452 //
1453 sp<const DisplayDevice> disp;
1454 uint32_t currentlayerStack = 0;
1455 for (size_t i=0; i<count; i++) {
1456 // NOTE: we rely on the fact that layers are sorted by
1457 // layerStack first (so we don't have to traverse the list
1458 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001459 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001460 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001461 if (i==0 || currentlayerStack != layerStack) {
1462 currentlayerStack = layerStack;
1463 // figure out if this layerstack is mirrored
1464 // (more than one display) if so, pick the default display,
1465 // if not, pick the only display it's on.
1466 disp.clear();
1467 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1468 sp<const DisplayDevice> hw(mDisplays[dpy]);
1469 if (hw->getLayerStack() == currentlayerStack) {
1470 if (disp == NULL) {
1471 disp = hw;
1472 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001473 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001474 break;
1475 }
1476 }
1477 }
1478 }
Chet Haase91d25932013-04-11 15:24:55 -07001479 if (disp == NULL) {
1480 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1481 // redraw after transform hint changes. See bug 8508397.
1482
1483 // could be null when this layer is using a layerStack
1484 // that is not visible on any display. Also can occur at
1485 // screen off/on times.
1486 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001487 }
Chet Haase91d25932013-04-11 15:24:55 -07001488 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001489 }
1490 }
1491
1492
Mathias Agopian3559b072012-08-15 13:46:03 -07001493 /*
1494 * Perform our own transaction if needed
1495 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001496
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001497 const LayerVector& layers(mDrawingState.layersSortedByZ);
1498 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001499 // layers have been added
1500 mVisibleRegionsDirty = true;
1501 }
1502
1503 // some layers might have been removed, so
1504 // we need to update the regions they're exposing.
1505 if (mLayersRemoved) {
1506 mLayersRemoved = false;
1507 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001508 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001509 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001510 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001511 if (currentLayers.indexOf(layer) < 0) {
1512 // this layer is not visible anymore
1513 // TODO: we could traverse the tree from front to back and
1514 // compute the actual visible region
1515 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001516 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001517 Region visibleReg = s.transform.transform(
1518 Region(Rect(s.active.w, s.active.h)));
1519 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001520 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001521 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001522 }
1523
1524 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001525
1526 updateCursorAsync();
1527}
1528
1529void SurfaceFlinger::updateCursorAsync()
1530{
1531 HWComposer& hwc(getHwComposer());
1532 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1533 sp<const DisplayDevice> hw(mDisplays[dpy]);
1534 const int32_t id = hw->getHwcDisplayId();
1535 if (id < 0) {
1536 continue;
1537 }
1538 const Vector< sp<Layer> >& currentLayers(
1539 hw->getVisibleLayersSortedByZ());
1540 const size_t count = currentLayers.size();
1541 HWComposer::LayerListIterator cur = hwc.begin(id);
1542 const HWComposer::LayerListIterator end = hwc.end(id);
1543 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1544 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1545 continue;
1546 }
1547 const sp<Layer>& layer(currentLayers[i]);
1548 Rect cursorPos = layer->getPosition(hw);
1549 hwc.setCursorPositionAsync(id, cursorPos);
1550 break;
1551 }
1552 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001553}
1554
1555void SurfaceFlinger::commitTransaction()
1556{
1557 if (!mLayersPendingRemoval.isEmpty()) {
1558 // Notify removed layers now that they can't be drawn from
1559 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1560 mLayersPendingRemoval[i]->onRemoved();
1561 }
1562 mLayersPendingRemoval.clear();
1563 }
1564
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001565 // If this transaction is part of a window animation then the next frame
1566 // we composite should be considered an animation as well.
1567 mAnimCompositionPending = mAnimTransactionPending;
1568
Mathias Agopian4fec8732012-06-29 14:12:52 -07001569 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001570 mTransactionPending = false;
1571 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001572 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573}
1574
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001575void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001576 const LayerVector& currentLayers, uint32_t layerStack,
1577 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001578{
Mathias Agopian841cde52012-03-01 15:44:37 -08001579 ATRACE_CALL();
1580
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001581 Region aboveOpaqueLayers;
1582 Region aboveCoveredLayers;
1583 Region dirty;
1584
Mathias Agopian87baae12012-07-31 12:38:26 -07001585 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001586
1587 size_t i = currentLayers.size();
1588 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001589 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001590
1591 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001592 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001593
Jesse Hall01e29052013-02-19 16:13:35 -08001594 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001595 if (s.layerStack != layerStack)
1596 continue;
1597
Mathias Agopianab028732010-03-16 16:41:46 -07001598 /*
1599 * opaqueRegion: area of a surface that is fully opaque.
1600 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001601 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001602
1603 /*
1604 * visibleRegion: area of a surface that is visible on screen
1605 * and not fully transparent. This is essentially the layer's
1606 * footprint minus the opaque regions above it.
1607 * Areas covered by a translucent surface are considered visible.
1608 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001609 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001610
1611 /*
1612 * coveredRegion: area of a surface that is covered by all
1613 * visible regions above it (which includes the translucent areas).
1614 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001615 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001616
Jesse Halla8026d22012-09-25 13:25:04 -07001617 /*
1618 * transparentRegion: area of a surface that is hinted to be completely
1619 * transparent. This is only used to tell when the layer has no visible
1620 * non-transparent regions and can be removed from the layer list. It
1621 * does not affect the visibleRegion of this layer or any layers
1622 * beneath it. The hint may not be correct if apps don't respect the
1623 * SurfaceView restrictions (which, sadly, some don't).
1624 */
1625 Region transparentRegion;
1626
Mathias Agopianab028732010-03-16 16:41:46 -07001627
1628 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001629 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001630 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001631 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001632 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001633 if (!visibleRegion.isEmpty()) {
1634 // Remove the transparent area from the visible region
1635 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001636 const Transform tr(s.transform);
1637 if (tr.transformed()) {
1638 if (tr.preserveRects()) {
1639 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001640 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001641 } else {
1642 // transformation too complex, can't do the
1643 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001644 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001645 }
1646 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001647 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001648 }
Mathias Agopianab028732010-03-16 16:41:46 -07001649 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001650
Mathias Agopianab028732010-03-16 16:41:46 -07001651 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001652 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001653 if (s.alpha==255 && !translucent &&
1654 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1655 // the opaque region is the layer's footprint
1656 opaqueRegion = visibleRegion;
1657 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001658 }
1659 }
1660
Mathias Agopianab028732010-03-16 16:41:46 -07001661 // Clip the covered region to the visible region
1662 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1663
1664 // Update aboveCoveredLayers for next (lower) layer
1665 aboveCoveredLayers.orSelf(visibleRegion);
1666
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667 // subtract the opaque region covered by the layers above us
1668 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001669
1670 // compute this layer's dirty region
1671 if (layer->contentDirty) {
1672 // we need to invalidate the whole region
1673 dirty = visibleRegion;
1674 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001675 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001676 layer->contentDirty = false;
1677 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001678 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001679 * the exposed region consists of two components:
1680 * 1) what's VISIBLE now and was COVERED before
1681 * 2) what's EXPOSED now less what was EXPOSED before
1682 *
1683 * note that (1) is conservative, we start with the whole
1684 * visible region but only keep what used to be covered by
1685 * something -- which mean it may have been exposed.
1686 *
1687 * (2) handles areas that were not covered by anything but got
1688 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001689 */
Mathias Agopianab028732010-03-16 16:41:46 -07001690 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001691 const Region oldVisibleRegion = layer->visibleRegion;
1692 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001693 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1694 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001695 }
1696 dirty.subtractSelf(aboveOpaqueLayers);
1697
1698 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001699 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001700
Mathias Agopianab028732010-03-16 16:41:46 -07001701 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001702 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001703
Jesse Halla8026d22012-09-25 13:25:04 -07001704 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705 layer->setVisibleRegion(visibleRegion);
1706 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001707 layer->setVisibleNonTransparentRegion(
1708 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001709 }
1710
Mathias Agopian87baae12012-07-31 12:38:26 -07001711 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001712}
1713
Mathias Agopian87baae12012-07-31 12:38:26 -07001714void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1715 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001716 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001717 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1718 if (hw->getLayerStack() == layerStack) {
1719 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001720 }
1721 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001722}
1723
Dan Stoza6b9454d2014-11-07 16:00:59 -08001724bool SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001725{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001726 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001727
Mathias Agopian4fec8732012-06-29 14:12:52 -07001728 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001729 const LayerVector& layers(mDrawingState.layersSortedByZ);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001730 bool frameQueued = false;
Eric Penner51c59cd2014-07-28 19:51:58 -07001731
1732 // Store the set of layers that need updates. This set must not change as
1733 // buffers are being latched, as this could result in a deadlock.
1734 // Example: Two producers share the same command stream and:
1735 // 1.) Layer 0 is latched
1736 // 2.) Layer 0 gets a new frame
1737 // 2.) Layer 1 gets a new frame
1738 // 3.) Layer 1 is latched.
1739 // Display is now waiting on Layer 1's frame, which is behind layer 0's
1740 // second frame. But layer 0's second frame could be waiting on display.
1741 Vector<Layer*> layersWithQueuedFrames;
1742 for (size_t i = 0, count = layers.size(); i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001743 const sp<Layer>& layer(layers[i]);
Dan Stoza6b9454d2014-11-07 16:00:59 -08001744 if (layer->hasQueuedFrame()) {
1745 frameQueued = true;
1746 if (layer->shouldPresentNow(mPrimaryDispSync)) {
1747 layersWithQueuedFrames.push_back(layer.get());
1748 }
1749 }
Eric Penner51c59cd2014-07-28 19:51:58 -07001750 }
1751 for (size_t i = 0, count = layersWithQueuedFrames.size() ; i<count ; i++) {
1752 Layer* layer = layersWithQueuedFrames[i];
Mathias Agopian87baae12012-07-31 12:38:26 -07001753 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001754 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001755 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001756 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001757
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001758 mVisibleRegionsDirty |= visibleRegions;
Dan Stoza6b9454d2014-11-07 16:00:59 -08001759
1760 // If we will need to wake up at some time in the future to deal with a
1761 // queued frame that shouldn't be displayed during this vsync period, wake
1762 // up during the next vsync period to check again.
1763 if (frameQueued && layersWithQueuedFrames.empty()) {
1764 signalLayerUpdate();
1765 }
1766
1767 // Only continue with the refresh if there is actually new work to do
1768 return !layersWithQueuedFrames.empty();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001769}
1770
Mathias Agopianad456f92011-01-13 17:53:01 -08001771void SurfaceFlinger::invalidateHwcGeometry()
1772{
1773 mHwWorkListDirty = true;
1774}
1775
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001776
Mathias Agopiancd60f992012-08-16 16:28:27 -07001777void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001778 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001779{
Dan Stoza71433162014-02-04 16:22:36 -08001780 // We only need to actually compose the display if:
1781 // 1) It is being handled by hardware composer, which may need this to
1782 // keep its virtual display state machine in sync, or
1783 // 2) There is work to be done (the dirty region isn't empty)
1784 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1785 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1786 return;
1787 }
1788
Mathias Agopian87baae12012-07-31 12:38:26 -07001789 Region dirtyRegion(inDirtyRegion);
1790
Mathias Agopianb8a55602009-06-26 19:06:36 -07001791 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001792 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001793
Mathias Agopian42977342012-08-05 00:40:46 -07001794 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001795 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001796 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1797 // takes a rectangle, we must make sure to update that whole
1798 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001799 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001800 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001801 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001802 // We need to redraw the rectangle that will be updated
1803 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001804 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001805 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001806 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001807 } else {
1808 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001809 dirtyRegion.set(hw->bounds());
1810 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001811 }
1812 }
1813
Alan Viverette9c5a3332013-09-12 20:04:35 -07001814 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Michael Lentine3f121fc2014-10-01 11:17:28 -07001815 if (!doComposeSurfaces(hw, dirtyRegion)) return;
Mathias Agopianff2ed702013-09-01 21:36:12 -07001816 } else {
1817 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001818 mat4 colorMatrix = mColorMatrix;
1819 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001820 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001821 }
1822 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001823 doComposeSurfaces(hw, dirtyRegion);
1824 engine.endGroup();
1825 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001826
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001827 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001828 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001829
1830 // swap buffers (presentation)
1831 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001832}
1833
Michael Lentine3f121fc2014-10-01 11:17:28 -07001834bool SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001835{
Mathias Agopian3f844832013-08-07 21:24:32 -07001836 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001837 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001838 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001839 HWComposer::LayerListIterator cur = hwc.begin(id);
1840 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001841
Jesse Halld05a17f2013-09-30 16:49:30 -07001842 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001843 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001844 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001845 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1846 hw->getDisplayName().string());
Michael Lentine3f121fc2014-10-01 11:17:28 -07001847 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1848 if(!getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext)) {
1849 ALOGE("DisplayDevice::makeCurrent on default display failed. Aborting.");
1850 }
1851 return false;
Michael Chockc8c71092013-03-04 15:15:46 -08001852 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001853
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001854 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001855 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001856 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001857 // when using overlays, we assume a fully transparent framebuffer
1858 // NOTE: we could reduce how much we need to clear, for instance
1859 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001860 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001861 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001862 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001863 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001864 // we start with the whole screen area
1865 const Region bounds(hw->getBounds());
1866
1867 // we remove the scissor part
1868 // we're left with the letterbox region
1869 // (common case is that letterbox ends-up being empty)
1870 const Region letterbox(bounds.subtract(hw->getScissor()));
1871
1872 // compute the area to clear
1873 Region region(hw->undefinedRegion.merge(letterbox));
1874
1875 // but limit it to the dirty region
1876 region.andSelf(dirty);
1877
Mathias Agopianb9494d52012-04-18 02:28:45 -07001878 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001879 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001880 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001881 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001882 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001883 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001884
Mathias Agopian766dc492012-10-30 18:08:06 -07001885 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1886 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001887 // scissor on the main display. It should never be needed
1888 // anyways (though in theory it could since the API allows it).
1889 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001890 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001891 if (scissor != bounds) {
1892 // scissor doesn't match the screen's dimensions, so we
1893 // need to clear everything outside of it and enable
1894 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001895
Mathias Agopianf45c5102012-10-24 16:29:17 -07001896 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001897 const uint32_t height = hw->getHeight();
1898 engine.setScissor(scissor.left, height - scissor.bottom,
1899 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001900 }
1901 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001902 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001903
Mathias Agopian85d751c2012-08-29 16:59:24 -07001904 /*
1905 * and then, render the layers targeted at the framebuffer
1906 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001907
Mathias Agopian13127d82013-03-05 17:47:11 -08001908 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001909 const size_t count = layers.size();
1910 const Transform& tr = hw->getTransform();
1911 if (cur != end) {
1912 // we're using h/w composer
1913 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001914 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001915 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001916 if (!clip.isEmpty()) {
1917 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07001918 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07001919 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001920 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001921 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1922 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001923 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001924 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001925 // never clear the very first layer since we're
1926 // guaranteed the FB is already cleared
1927 layer->clearWithOpenGL(hw, clip);
1928 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001929 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001930 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001931 case HWC_FRAMEBUFFER: {
1932 layer->draw(hw, clip);
1933 break;
1934 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001935 case HWC_FRAMEBUFFER_TARGET: {
1936 // this should not happen as the iterator shouldn't
1937 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001938 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001939 break;
1940 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001941 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001942 }
1943 layer->setAcquireFence(hw, *cur);
1944 }
1945 } else {
1946 // we're not using h/w composer
1947 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001948 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001949 const Region clip(dirty.intersect(
1950 tr.transform(layer->visibleRegion)));
1951 if (!clip.isEmpty()) {
1952 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001953 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001954 }
1955 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001956
1957 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001958 engine.disableScissor();
Michael Lentine3f121fc2014-10-01 11:17:28 -07001959 return true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001960}
1961
Mathias Agopian3f844832013-08-07 21:24:32 -07001962void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001963 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001964 RenderEngine& engine(getRenderEngine());
1965 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001966}
1967
Mathias Agopianac9fa422013-02-11 16:40:36 -08001968void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1969 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001970 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001971 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001972{
Mathias Agopian96f08192010-06-02 23:28:45 -07001973 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001974 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001975
Mathias Agopian96f08192010-06-02 23:28:45 -07001976 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001977 Mutex::Autolock _l(mStateLock);
1978 mCurrentState.layersSortedByZ.add(lbc);
Marco Nelissen097ca272014-11-14 08:01:01 -08001979 mGraphicBufferProducerList.add(IInterface::asBinder(gbc));
Mathias Agopian96f08192010-06-02 23:28:45 -07001980}
1981
Mathias Agopian3f844832013-08-07 21:24:32 -07001982status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001983 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001984 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001985 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001986 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001987 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001988 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001989 return NO_ERROR;
1990 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001991 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001992}
1993
Dan Stozac7014012014-02-14 15:03:43 -08001994uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001995 return android_atomic_release_load(&mTransactionFlags);
1996}
1997
Mathias Agopian3f844832013-08-07 21:24:32 -07001998uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001999 return android_atomic_and(~flags, &mTransactionFlags) & flags;
2000}
2001
Mathias Agopian3f844832013-08-07 21:24:32 -07002002uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002003 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
2004 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002005 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002006 }
2007 return old;
2008}
2009
Mathias Agopian8b33f032012-07-24 20:43:54 -07002010void SurfaceFlinger::setTransactionState(
2011 const Vector<ComposerState>& state,
2012 const Vector<DisplayState>& displays,
2013 uint32_t flags)
2014{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002015 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07002016 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07002017 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07002018
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002019 if (flags & eAnimation) {
2020 // For window updates that are part of an animation we must wait for
2021 // previous animation "frames" to be handled.
2022 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002023 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002024 if (CC_UNLIKELY(err != NO_ERROR)) {
2025 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07002026 // caller after a few seconds.
2027 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
2028 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002029 mAnimTransactionPending = false;
2030 break;
2031 }
2032 }
2033 }
2034
Mathias Agopiane57f2922012-08-09 16:29:12 -07002035 size_t count = displays.size();
2036 for (size_t i=0 ; i<count ; i++) {
2037 const DisplayState& s(displays[i]);
2038 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07002039 }
2040
Mathias Agopiane57f2922012-08-09 16:29:12 -07002041 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07002042 for (size_t i=0 ; i<count ; i++) {
2043 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002044 // Here we need to check that the interface we're given is indeed
2045 // one of our own. A malicious client could give us a NULL
2046 // IInterface, or one of its own or even one of our own but a
2047 // different type. All these situations would cause us to crash.
2048 //
2049 // NOTE: it would be better to use RTTI as we could directly check
2050 // that we have a Client*. however, RTTI is disabled in Android.
2051 if (s.client != NULL) {
Marco Nelissen097ca272014-11-14 08:01:01 -08002052 sp<IBinder> binder = IInterface::asBinder(s.client);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07002053 if (binder != NULL) {
2054 String16 desc(binder->getInterfaceDescriptor());
2055 if (desc == ISurfaceComposerClient::descriptor) {
2056 sp<Client> client( static_cast<Client *>(s.client.get()) );
2057 transactionFlags |= setClientStateLocked(client, s.state);
2058 }
2059 }
2060 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002061 }
Mathias Agopian698c0872011-06-28 19:09:31 -07002062
Mathias Agopian386aa982011-11-07 21:58:03 -08002063 if (transactionFlags) {
2064 // this triggers the transaction
2065 setTransactionFlags(transactionFlags);
2066
2067 // if this is a synchronous transaction, wait for it to take effect
2068 // before returning.
2069 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002070 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08002071 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002072 if (flags & eAnimation) {
2073 mAnimTransactionPending = true;
2074 }
2075 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08002076 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
2077 if (CC_UNLIKELY(err != NO_ERROR)) {
2078 // just in case something goes wrong in SF, return to the
2079 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07002080 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
2081 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08002082 break;
2083 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002084 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002085 }
2086}
2087
Mathias Agopiane57f2922012-08-09 16:29:12 -07002088uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
2089{
Jesse Hall9a143922012-10-04 16:29:19 -07002090 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
2091 if (dpyIdx < 0)
2092 return 0;
2093
Mathias Agopiane57f2922012-08-09 16:29:12 -07002094 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07002095 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002096 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002097 const uint32_t what = s.what;
2098 if (what & DisplayState::eSurfaceChanged) {
Marco Nelissen097ca272014-11-14 08:01:01 -08002099 if (IInterface::asBinder(disp.surface) != IInterface::asBinder(s.surface)) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002100 disp.surface = s.surface;
2101 flags |= eDisplayTransactionNeeded;
2102 }
2103 }
2104 if (what & DisplayState::eLayerStackChanged) {
2105 if (disp.layerStack != s.layerStack) {
2106 disp.layerStack = s.layerStack;
2107 flags |= eDisplayTransactionNeeded;
2108 }
2109 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07002110 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002111 if (disp.orientation != s.orientation) {
2112 disp.orientation = s.orientation;
2113 flags |= eDisplayTransactionNeeded;
2114 }
2115 if (disp.frame != s.frame) {
2116 disp.frame = s.frame;
2117 flags |= eDisplayTransactionNeeded;
2118 }
2119 if (disp.viewport != s.viewport) {
2120 disp.viewport = s.viewport;
2121 flags |= eDisplayTransactionNeeded;
2122 }
2123 }
Michael Lentine47e45402014-07-18 15:34:25 -07002124 if (what & DisplayState::eDisplaySizeChanged) {
2125 if (disp.width != s.width) {
2126 disp.width = s.width;
2127 flags |= eDisplayTransactionNeeded;
2128 }
2129 if (disp.height != s.height) {
2130 disp.height = s.height;
2131 flags |= eDisplayTransactionNeeded;
2132 }
2133 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07002134 }
2135 return flags;
2136}
2137
2138uint32_t SurfaceFlinger::setClientStateLocked(
2139 const sp<Client>& client,
2140 const layer_state_t& s)
2141{
2142 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08002143 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002144 if (layer != 0) {
2145 const uint32_t what = s.what;
2146 if (what & layer_state_t::ePositionChanged) {
2147 if (layer->setPosition(s.x, s.y))
2148 flags |= eTraversalNeeded;
2149 }
2150 if (what & layer_state_t::eLayerChanged) {
2151 // NOTE: index needs to be calculated before we update the state
2152 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2153 if (layer->setLayer(s.z)) {
2154 mCurrentState.layersSortedByZ.removeAt(idx);
2155 mCurrentState.layersSortedByZ.add(layer);
2156 // we need traversal (state changed)
2157 // AND transaction (list changed)
2158 flags |= eTransactionNeeded|eTraversalNeeded;
2159 }
2160 }
2161 if (what & layer_state_t::eSizeChanged) {
2162 if (layer->setSize(s.w, s.h)) {
2163 flags |= eTraversalNeeded;
2164 }
2165 }
2166 if (what & layer_state_t::eAlphaChanged) {
2167 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2168 flags |= eTraversalNeeded;
2169 }
2170 if (what & layer_state_t::eMatrixChanged) {
2171 if (layer->setMatrix(s.matrix))
2172 flags |= eTraversalNeeded;
2173 }
2174 if (what & layer_state_t::eTransparentRegionChanged) {
2175 if (layer->setTransparentRegionHint(s.transparentRegion))
2176 flags |= eTraversalNeeded;
2177 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08002178 if ((what & layer_state_t::eVisibilityChanged) ||
2179 (what & layer_state_t::eOpacityChanged)) {
2180 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07002181 if (layer->setFlags(s.flags, s.mask))
2182 flags |= eTraversalNeeded;
2183 }
2184 if (what & layer_state_t::eCropChanged) {
2185 if (layer->setCrop(s.crop))
2186 flags |= eTraversalNeeded;
2187 }
2188 if (what & layer_state_t::eLayerStackChanged) {
2189 // NOTE: index needs to be calculated before we update the state
2190 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2191 if (layer->setLayerStack(s.layerStack)) {
2192 mCurrentState.layersSortedByZ.removeAt(idx);
2193 mCurrentState.layersSortedByZ.add(layer);
2194 // we need traversal (state changed)
2195 // AND transaction (list changed)
2196 flags |= eTransactionNeeded|eTraversalNeeded;
2197 }
2198 }
2199 }
2200 return flags;
2201}
2202
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002203status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002204 const String8& name,
2205 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002206 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2207 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002208{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002209 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002210 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002211 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002212 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002213 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002214 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002215
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002216 status_t result = NO_ERROR;
2217
2218 sp<Layer> layer;
2219
Mathias Agopian3165cc22012-08-08 19:42:09 -07002220 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2221 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002222 result = createNormalLayer(client,
2223 name, w, h, flags, format,
2224 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002225 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002226 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002227 result = createDimLayer(client,
2228 name, w, h, flags,
2229 handle, gbp, &layer);
2230 break;
2231 default:
2232 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002233 break;
2234 }
2235
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002236 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002237 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002238 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002239 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002240 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002241}
2242
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002243status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2244 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2245 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002246{
2247 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002248 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002249 case PIXEL_FORMAT_TRANSPARENT:
2250 case PIXEL_FORMAT_TRANSLUCENT:
2251 format = PIXEL_FORMAT_RGBA_8888;
2252 break;
2253 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002254 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002255 break;
2256 }
2257
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002258 *outLayer = new Layer(this, client, name, w, h, flags);
2259 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2260 if (err == NO_ERROR) {
2261 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002262 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002263 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002264
2265 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2266 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002267}
2268
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002269status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2270 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2271 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002272{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002273 *outLayer = new LayerDim(this, client, name, w, h, flags);
2274 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002275 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002276 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002277}
2278
Mathias Agopianac9fa422013-02-11 16:40:36 -08002279status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002280{
Mathias Agopian67106042013-03-14 19:18:13 -07002281 // called by the window manager when it wants to remove a Layer
2282 status_t err = NO_ERROR;
2283 sp<Layer> l(client->getLayerUser(handle));
2284 if (l != NULL) {
2285 err = removeLayer(l);
2286 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2287 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002288 }
2289 return err;
2290}
2291
Mathias Agopian13127d82013-03-05 17:47:11 -08002292status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002293{
Mathias Agopian67106042013-03-14 19:18:13 -07002294 // called by ~LayerCleaner() when all references to the IBinder (handle)
2295 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002296 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002297 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002298 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002299 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002300 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002301 "error removing layer=%p (%s)", l.get(), strerror(-err));
2302 }
2303 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002304}
2305
Mathias Agopianb60314a2012-04-10 22:09:54 -07002306// ---------------------------------------------------------------------------
2307
Andy McFadden13a082e2012-08-24 10:16:42 -07002308void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002309 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002310 Vector<ComposerState> state;
2311 Vector<DisplayState> displays;
2312 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002313 d.what = DisplayState::eDisplayProjectionChanged |
2314 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002315 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002316 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002317 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002318 d.frame.makeInvalid();
2319 d.viewport.makeInvalid();
Michael Lentine47e45402014-07-18 15:34:25 -07002320 d.width = 0;
2321 d.height = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002322 displays.add(d);
2323 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002324 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002325
2326 const nsecs_t period =
2327 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2328 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002329}
2330
2331void SurfaceFlinger::initializeDisplays() {
2332 class MessageScreenInitialized : public MessageBase {
2333 SurfaceFlinger* flinger;
2334 public:
2335 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2336 virtual bool handler() {
2337 flinger->onInitializeDisplays();
2338 return true;
2339 }
2340 };
2341 sp<MessageBase> msg = new MessageScreenInitialized(this);
2342 postMessageAsync(msg); // we may be called from main thread, use async message
2343}
2344
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002345void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2346 int mode) {
2347 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2348 this);
2349 int32_t type = hw->getDisplayType();
2350 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002351
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002352 if (mode == currentMode) {
2353 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002354 return;
2355 }
2356
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002357 hw->setPowerMode(mode);
2358 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2359 ALOGW("Trying to set power mode for virtual display");
2360 return;
2361 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002362
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002363 if (currentMode == HWC_POWER_MODE_OFF) {
2364 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002365 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2366 // FIXME: eventthread only knows about the main display right now
2367 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002368 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002369 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002370
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002371 mVisibleRegionsDirty = true;
Dan Stozab90cf072015-03-05 11:05:59 -08002372 mHasPoweredOff = true;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002373 repaintEverything();
2374 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002375 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002376 disableHardwareVsync(true); // also cancels any in-progress resync
2377
Mathias Agopiancde87a32012-09-13 14:09:01 -07002378 // FIXME: eventthread only knows about the main display right now
2379 mEventThread->onScreenReleased();
2380 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002381
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002382 getHwComposer().setPowerMode(type, mode);
2383 mVisibleRegionsDirty = true;
2384 // from this point on, SF will stop drawing on this display
2385 } else {
2386 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002387 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002388}
2389
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002390void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2391 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002392 SurfaceFlinger& mFlinger;
2393 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002394 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002395 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002396 MessageSetPowerMode(SurfaceFlinger& flinger,
2397 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2398 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002399 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002400 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002401 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002402 ALOGE("Attempt to set power mode = %d for null display %p",
Michael Lentine7306c672014-07-30 13:00:37 -07002403 mMode, mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002404 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002405 ALOGW("Attempt to set power mode = %d for virtual display",
2406 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002407 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002408 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002409 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002410 return true;
2411 }
2412 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002413 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002414 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002415}
2416
2417// ---------------------------------------------------------------------------
2418
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002419status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2420{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002421 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002422
Mathias Agopianbd115332013-04-18 16:41:04 -07002423 IPCThreadState* ipc = IPCThreadState::self();
2424 const int pid = ipc->getCallingPid();
2425 const int uid = ipc->getCallingUid();
2426 if ((uid != AID_SHELL) &&
2427 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002428 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002429 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002430 } else {
Jesse Hallfcd15b42014-12-23 13:57:23 -08002431 // Try to get the main lock, but give up after one second
Mathias Agopian9795c422009-08-26 16:36:26 -07002432 // (this would indicate SF is stuck, but we want to be able to
2433 // print something in dumpsys).
Jesse Hallfcd15b42014-12-23 13:57:23 -08002434 status_t err = mStateLock.timedLock(s2ns(1));
2435 bool locked = (err == NO_ERROR);
Mathias Agopian9795c422009-08-26 16:36:26 -07002436 if (!locked) {
Jesse Hallfcd15b42014-12-23 13:57:23 -08002437 result.appendFormat(
2438 "SurfaceFlinger appears to be unresponsive (%s [%d]), "
2439 "dumping anyways (no locks held)\n", strerror(-err), err);
Mathias Agopian9795c422009-08-26 16:36:26 -07002440 }
2441
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002442 bool dumpAll = true;
2443 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002444 size_t numArgs = args.size();
2445 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002446 if ((index < numArgs) &&
2447 (args[index] == String16("--list"))) {
2448 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002449 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002450 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002451 }
2452
2453 if ((index < numArgs) &&
2454 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002455 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002456 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002457 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002458 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002459
2460 if ((index < numArgs) &&
2461 (args[index] == String16("--latency-clear"))) {
2462 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002463 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002464 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002465 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002466
2467 if ((index < numArgs) &&
2468 (args[index] == String16("--dispsync"))) {
2469 index++;
2470 mPrimaryDispSync.dump(result);
2471 dumpAll = false;
2472 }
Dan Stozab90cf072015-03-05 11:05:59 -08002473
2474 if ((index < numArgs) &&
2475 (args[index] == String16("--static-screen"))) {
2476 index++;
2477 dumpStaticScreenStats(result);
2478 dumpAll = false;
2479 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002480 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002481
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002482 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002483 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002484 }
2485
Mathias Agopian9795c422009-08-26 16:36:26 -07002486 if (locked) {
2487 mStateLock.unlock();
2488 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002489 }
2490 write(fd, result.string(), result.size());
2491 return NO_ERROR;
2492}
2493
Dan Stozac7014012014-02-14 15:03:43 -08002494void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2495 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002496{
2497 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2498 const size_t count = currentLayers.size();
2499 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002500 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002501 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002502 }
2503}
2504
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002505void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002506 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002507{
2508 String8 name;
2509 if (index < args.size()) {
2510 name = String8(args[index]);
2511 index++;
2512 }
2513
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002514 const nsecs_t period =
2515 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002516 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002517
2518 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002519 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002520 } else {
2521 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2522 const size_t count = currentLayers.size();
2523 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002524 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002525 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002526 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002527 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002528 }
2529 }
2530}
2531
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002532void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002533 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002534{
2535 String8 name;
2536 if (index < args.size()) {
2537 name = String8(args[index]);
2538 index++;
2539 }
2540
2541 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2542 const size_t count = currentLayers.size();
2543 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002544 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002545 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002546 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002547 }
2548 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002549
Svetoslavd85084b2014-03-20 10:28:31 -07002550 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002551}
2552
Jamie Gennis6547ff42013-07-16 20:12:42 -07002553// This should only be called from the main thread. Otherwise it would need
2554// the lock and should use mCurrentState rather than mDrawingState.
2555void SurfaceFlinger::logFrameStats() {
2556 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2557 const size_t count = drawingLayers.size();
2558 for (size_t i=0 ; i<count ; i++) {
2559 const sp<Layer>& layer(drawingLayers[i]);
2560 layer->logFrameStats();
2561 }
2562
2563 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2564}
2565
Andy McFadden4803b742012-09-24 19:07:20 -07002566/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2567{
2568 static const char* config =
2569 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002570#ifdef HAS_CONTEXT_PRIORITY
2571 " HAS_CONTEXT_PRIORITY"
2572#endif
2573#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2574 " NEVER_DEFAULT_TO_ASYNC_MODE"
2575#endif
2576#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2577 " TARGET_DISABLE_TRIPLE_BUFFERING"
2578#endif
2579 "]";
2580 result.append(config);
2581}
2582
Dan Stozab90cf072015-03-05 11:05:59 -08002583void SurfaceFlinger::dumpStaticScreenStats(String8& result) const
2584{
2585 result.appendFormat("Static screen stats:\n");
2586 for (size_t b = 0; b < NUM_BUCKETS - 1; ++b) {
2587 float bucketTimeSec = mFrameBuckets[b] / 1e9;
2588 float percent = 100.0f *
2589 static_cast<float>(mFrameBuckets[b]) / mTotalTime;
2590 result.appendFormat(" < %zd frames: %.3f s (%.1f%%)\n",
2591 b + 1, bucketTimeSec, percent);
2592 }
2593 float bucketTimeSec = mFrameBuckets[NUM_BUCKETS - 1] / 1e9;
2594 float percent = 100.0f *
2595 static_cast<float>(mFrameBuckets[NUM_BUCKETS - 1]) / mTotalTime;
2596 result.appendFormat(" %zd+ frames: %.3f s (%.1f%%)\n",
2597 NUM_BUCKETS - 1, bucketTimeSec, percent);
2598}
2599
Mathias Agopian74d211a2013-04-22 16:55:35 +02002600void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2601 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002602{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002603 bool colorize = false;
2604 if (index < args.size()
2605 && (args[index] == String16("--color"))) {
2606 colorize = true;
2607 index++;
2608 }
2609
2610 Colorizer colorizer(colorize);
2611
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002612 // figure out if we're stuck somewhere
2613 const nsecs_t now = systemTime();
2614 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2615 const nsecs_t inTransaction(mDebugInTransaction);
2616 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2617 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2618
2619 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002620 * Dump library configuration.
2621 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002622
2623 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002624 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002625 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002626 appendSfConfigString(result);
2627 appendUiConfigString(result);
2628 appendGuiConfigString(result);
2629 result.append("\n");
2630
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002631 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002632 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002633 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002634 result.append(SyncFeatures::getInstance().toString());
2635 result.append("\n");
2636
Andy McFadden41d67d72014-04-25 16:58:34 -07002637 colorizer.bold(result);
2638 result.append("DispSync configuration: ");
2639 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002640 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2641 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002642 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2643 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2644 result.append("\n");
2645
Dan Stozab90cf072015-03-05 11:05:59 -08002646 // Dump static screen stats
2647 result.append("\n");
2648 dumpStaticScreenStats(result);
2649 result.append("\n");
2650
Andy McFadden4803b742012-09-24 19:07:20 -07002651 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002652 * Dump the visible layer list
2653 */
2654 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2655 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002656 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002657 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002658 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002659 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002660 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002661 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002662 }
2663
2664 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002665 * Dump Display state
2666 */
2667
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002668 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002669 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002670 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002671 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2672 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002673 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002674 }
2675
2676 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002677 * Dump SurfaceFlinger global state
2678 */
2679
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002680 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002681 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002682 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002683
Mathias Agopian888c8222012-08-04 21:10:38 -07002684 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002685 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002686
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002687 colorizer.bold(result);
2688 result.appendFormat("EGL implementation : %s\n",
2689 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2690 colorizer.reset(result);
2691 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002692 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002693
Mathias Agopian875d8e12013-06-07 15:35:48 -07002694 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002695
Mathias Agopian42977342012-08-05 00:40:46 -07002696 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002697 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2698 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002699 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002700 " last eglSwapBuffers() time: %f us\n"
2701 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002702 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002703 " refresh-rate : %f fps\n"
2704 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002705 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002706 " gpu_to_cpu_unsupported : %d\n"
2707 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002708 mLastSwapBufferTime/1000.0,
2709 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002710 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002711 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2712 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002713 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002714 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002715
Mathias Agopian74d211a2013-04-22 16:55:35 +02002716 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002717 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002718
Mathias Agopian74d211a2013-04-22 16:55:35 +02002719 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002720 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002721
2722 /*
2723 * VSYNC state
2724 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002725 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002726
2727 /*
2728 * Dump HWComposer state
2729 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002730 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002731 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002732 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002733 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002734 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002735 (mDebugDisableHWC || mDebugRegion || mDaltonize
2736 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002737 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002738
2739 /*
2740 * Dump gralloc state
2741 */
2742 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2743 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002744}
2745
Mathias Agopian13127d82013-03-05 17:47:11 -08002746const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002747SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002748 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002749 wp<IBinder> dpy;
2750 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2751 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2752 dpy = mDisplays.keyAt(i);
2753 break;
2754 }
2755 }
2756 if (dpy == NULL) {
2757 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2758 // Just use the primary display so we have something to return
2759 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2760 }
2761 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002762}
2763
Keun young Park63f165f2012-08-31 10:53:36 -07002764bool SurfaceFlinger::startDdmConnection()
2765{
2766 void* libddmconnection_dso =
2767 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2768 if (!libddmconnection_dso) {
2769 return false;
2770 }
2771 void (*DdmConnection_start)(const char* name);
2772 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07002773 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07002774 if (!DdmConnection_start) {
2775 dlclose(libddmconnection_dso);
2776 return false;
2777 }
2778 (*DdmConnection_start)(getServiceName());
2779 return true;
2780}
2781
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002782status_t SurfaceFlinger::onTransact(
2783 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2784{
2785 switch (code) {
2786 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002787 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002788 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002789 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002790 case CLEAR_ANIMATION_FRAME_STATS:
2791 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002792 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002793 {
2794 // codes that require permission check
2795 IPCThreadState* ipc = IPCThreadState::self();
2796 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002797 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002798 if ((uid != AID_GRAPHICS) &&
2799 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002800 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002801 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2802 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002803 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002804 break;
2805 }
2806 case CAPTURE_SCREEN:
2807 {
2808 // codes that require permission check
2809 IPCThreadState* ipc = IPCThreadState::self();
2810 const int pid = ipc->getCallingPid();
2811 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002812 if ((uid != AID_GRAPHICS) &&
2813 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002814 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002815 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2816 return PERMISSION_DENIED;
2817 }
2818 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002819 }
2820 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002821
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002822 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2823 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002824 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002825 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002826 IPCThreadState* ipc = IPCThreadState::self();
2827 const int pid = ipc->getCallingPid();
2828 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002829 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002830 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002831 return PERMISSION_DENIED;
2832 }
2833 int n;
2834 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002835 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002836 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002837 return NO_ERROR;
2838 case 1002: // SHOW_UPDATES
2839 n = data.readInt32();
2840 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002841 invalidateHwcGeometry();
2842 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002843 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002844 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002845 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002846 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002847 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002848 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002849 setTransactionFlags(
2850 eTransactionNeeded|
2851 eDisplayTransactionNeeded|
2852 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002853 return NO_ERROR;
2854 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002855 case 1006:{ // send empty update
2856 signalRefresh();
2857 return NO_ERROR;
2858 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002859 case 1008: // toggle use of hw composer
2860 n = data.readInt32();
2861 mDebugDisableHWC = n ? 1 : 0;
2862 invalidateHwcGeometry();
2863 repaintEverything();
2864 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002865 case 1009: // toggle use of transform hint
2866 n = data.readInt32();
2867 mDebugDisableTransformHint = n ? 1 : 0;
2868 invalidateHwcGeometry();
2869 repaintEverything();
2870 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002871 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002872 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002873 reply->writeInt32(0);
2874 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002875 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002876 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002877 return NO_ERROR;
2878 case 1013: {
2879 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002880 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2881 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002882 return NO_ERROR;
2883 }
2884 case 1014: {
2885 // daltonize
2886 n = data.readInt32();
2887 switch (n % 10) {
2888 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2889 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2890 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2891 }
2892 if (n >= 10) {
2893 mDaltonizer.setMode(Daltonizer::correction);
2894 } else {
2895 mDaltonizer.setMode(Daltonizer::simulation);
2896 }
2897 mDaltonize = n > 0;
2898 invalidateHwcGeometry();
2899 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002900 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002901 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002902 case 1015: {
2903 // apply a color matrix
2904 n = data.readInt32();
2905 mHasColorMatrix = n ? 1 : 0;
2906 if (n) {
2907 // color matrix is sent as mat3 matrix followed by vec3
2908 // offset, then packed into a mat4 where the last row is
2909 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002910 for (size_t i = 0 ; i < 4; i++) {
2911 for (size_t j = 0; j < 4; j++) {
2912 mColorMatrix[i][j] = data.readFloat();
2913 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002914 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002915 } else {
2916 mColorMatrix = mat4();
2917 }
2918 invalidateHwcGeometry();
2919 repaintEverything();
2920 return NO_ERROR;
2921 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002922 // This is an experimental interface
2923 // Needs to be shifted to proper binder interface when we productize
2924 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07002925 n = data.readInt32();
2926 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002927 return NO_ERROR;
2928 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002929 }
2930 }
2931 return err;
2932}
2933
Mathias Agopian53331da2011-08-22 21:44:41 -07002934void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002935 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002936 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002937}
2938
Mathias Agopian59119e62010-10-11 12:37:43 -07002939// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002940// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002941// ---------------------------------------------------------------------------
2942
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002943/* The code below is here to handle b/8734824
2944 *
2945 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07002946 * from the surfaceflinger thread to the calling binder thread, where they
2947 * are executed. This allows the calling thread in the calling process to be
2948 * reused and not depend on having "enough" binder threads to handle the
2949 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002950 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002951class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07002952 /* Parts of GraphicProducerWrapper are run on two different threads,
2953 * communicating by sending messages via Looper but also by shared member
2954 * data. Coherence maintenance is subtle and in places implicit (ugh).
2955 *
2956 * Don't rely on Looper's sendMessage/handleMessage providing
2957 * release/acquire semantics for any data not actually in the Message.
2958 * Data going from surfaceflinger to binder threads needs to be
2959 * synchronized explicitly.
2960 *
2961 * Barrier open/wait do provide release/acquire semantics. This provides
2962 * implicit synchronization for data coming back from binder to
2963 * surfaceflinger threads.
2964 */
2965
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002966 sp<IGraphicBufferProducer> impl;
2967 sp<Looper> looper;
2968 status_t result;
2969 bool exitPending;
2970 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07002971 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002972 uint32_t code;
2973 Parcel const* data;
2974 Parcel* reply;
2975
2976 enum {
2977 MSG_API_CALL,
2978 MSG_EXIT
2979 };
2980
2981 /*
Jesse Hallb154c422014-07-13 12:47:02 -07002982 * Called on surfaceflinger thread. This is called by our "fake"
2983 * BpGraphicBufferProducer. We package the data and reply Parcel and
2984 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002985 */
2986 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08002987 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002988 this->code = code;
2989 this->data = &data;
2990 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002991 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07002992 // if we've exited, we run the message synchronously right here.
2993 // note (JH): as far as I can tell from looking at the code, this
2994 // never actually happens. if it does, i'm not sure if it happens
2995 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002996 handleMessage(Message(MSG_API_CALL));
2997 } else {
2998 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07002999 // Prevent stores to this->{code, data, reply} from being
3000 // reordered later than the construction of Message.
3001 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003002 looper->sendMessage(this, Message(MSG_API_CALL));
3003 barrier.wait();
3004 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08003005 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003006 }
3007
3008 /*
Jesse Hallb154c422014-07-13 12:47:02 -07003009 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003010 * call the real BpGraphicBufferProducer.
3011 */
3012 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07003013 int what = message.what;
3014 // Prevent reads below from happening before the read from Message
3015 atomic_thread_fence(memory_order_acquire);
3016 if (what == MSG_API_CALL) {
Marco Nelissen097ca272014-11-14 08:01:01 -08003017 result = IInterface::asBinder(impl)->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003018 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07003019 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003020 exitRequested = true;
3021 }
3022 }
3023
3024public:
Jesse Hallb154c422014-07-13 12:47:02 -07003025 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
3026 : impl(impl),
3027 looper(new Looper(true)),
3028 exitPending(false),
3029 exitRequested(false)
3030 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003031
Jesse Hallb154c422014-07-13 12:47:02 -07003032 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003033 status_t waitForResponse() {
3034 do {
3035 looper->pollOnce(-1);
3036 } while (!exitRequested);
3037 return result;
3038 }
3039
Jesse Hallb154c422014-07-13 12:47:02 -07003040 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003041 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07003042 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003043 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07003044 // Ensure this->result is visible to the binder thread before it
3045 // handles the message.
3046 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003047 looper->sendMessage(this, Message(MSG_EXIT));
3048 }
3049};
3050
3051
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003052status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
3053 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003054 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003055 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003056 bool useIdentityTransform, ISurfaceComposer::Rotation rotation) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003057
3058 if (CC_UNLIKELY(display == 0))
3059 return BAD_VALUE;
3060
3061 if (CC_UNLIKELY(producer == 0))
3062 return BAD_VALUE;
3063
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003064 // if we have secure windows on this display, never allow the screen capture
3065 // unless the producer interface is local (i.e.: we can take a screenshot for
3066 // ourselves).
Marco Nelissen097ca272014-11-14 08:01:01 -08003067 if (!IInterface::asBinder(producer)->localBinder()) {
Mathias Agopian5ff5a842013-08-13 15:55:43 -07003068 Mutex::Autolock _l(mStateLock);
3069 sp<const DisplayDevice> hw(getDisplayDevice(display));
3070 if (hw->getSecureLayerVisible()) {
3071 ALOGW("FB is protected: PERMISSION_DENIED");
3072 return PERMISSION_DENIED;
3073 }
3074 }
3075
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003076 // Convert to surfaceflinger's internal rotation type.
3077 Transform::orientation_flags rotationFlags;
3078 switch (rotation) {
3079 case ISurfaceComposer::eRotateNone:
3080 rotationFlags = Transform::ROT_0;
3081 break;
3082 case ISurfaceComposer::eRotate90:
3083 rotationFlags = Transform::ROT_90;
3084 break;
3085 case ISurfaceComposer::eRotate180:
3086 rotationFlags = Transform::ROT_180;
3087 break;
3088 case ISurfaceComposer::eRotate270:
3089 rotationFlags = Transform::ROT_270;
3090 break;
3091 default:
3092 rotationFlags = Transform::ROT_0;
3093 ALOGE("Invalid rotation passed to captureScreen(): %d\n", rotation);
3094 break;
3095 }
3096
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003097 class MessageCaptureScreen : public MessageBase {
3098 SurfaceFlinger* flinger;
3099 sp<IBinder> display;
3100 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07003101 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003102 uint32_t reqWidth, reqHeight;
3103 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08003104 bool useIdentityTransform;
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003105 Transform::orientation_flags rotation;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003106 status_t result;
3107 public:
3108 MessageCaptureScreen(SurfaceFlinger* flinger,
3109 const sp<IBinder>& display,
3110 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003111 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003112 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003113 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003114 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07003115 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003116 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08003117 useIdentityTransform(useIdentityTransform),
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003118 rotation(rotation),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003119 result(PERMISSION_DENIED)
3120 {
3121 }
3122 status_t getResult() const {
3123 return result;
3124 }
3125 virtual bool handler() {
3126 Mutex::Autolock _l(flinger->mStateLock);
3127 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08003128 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07003129 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003130 useIdentityTransform, rotation);
Marco Nelissen097ca272014-11-14 08:01:01 -08003131 static_cast<GraphicProducerWrapper*>(IInterface::asBinder(producer).get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003132 return true;
3133 }
3134 };
3135
Mathias Agopian9eb1f052013-04-10 16:27:17 -07003136 // make sure to process transactions before screenshots -- a transaction
3137 // might already be pending but scheduled for VSYNC; this guarantees we
3138 // will handle it before the screenshot. When VSYNC finally arrives
3139 // the scheduled transaction will be a no-op. If no transactions are
3140 // scheduled at this time, this will end-up being a no-op as well.
3141 mEventQueue.invalidateTransactionNow();
3142
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003143 // this creates a "fake" BBinder which will serve as a "fake" remote
3144 // binder to receive the marshaled calls and forward them to the
3145 // real remote (a BpGraphicBufferProducer)
3146 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
3147
3148 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
3149 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003150 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003151 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07003152 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003153 useIdentityTransform, rotationFlags);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003154
3155 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003156 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07003157 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003158 }
3159 return res;
3160}
3161
Mathias Agopian180f10d2013-04-10 22:55:41 -07003162
3163void SurfaceFlinger::renderScreenImplLocked(
3164 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07003165 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07003166 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003167 bool yswap, bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian180f10d2013-04-10 22:55:41 -07003168{
3169 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07003170 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003171
3172 // get screen geometry
Andreas Gampe89fd4f72014-11-13 14:18:56 -08003173 const int32_t hw_w = hw->getWidth();
3174 const int32_t hw_h = hw->getHeight();
3175 const bool filtering = static_cast<int32_t>(reqWidth) != hw_w ||
3176 static_cast<int32_t>(reqWidth) != hw_h;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003177
Dan Stozac1879002014-05-22 15:59:05 -07003178 // if a default or invalid sourceCrop is passed in, set reasonable values
3179 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
3180 !sourceCrop.isValid()) {
3181 sourceCrop.setLeftTop(Point(0, 0));
3182 sourceCrop.setRightBottom(Point(hw_w, hw_h));
3183 }
3184
3185 // ensure that sourceCrop is inside screen
3186 if (sourceCrop.left < 0) {
3187 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
3188 }
Dan Stozabe31f442014-06-11 11:20:54 -07003189 if (sourceCrop.right > hw_w) {
3190 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07003191 }
3192 if (sourceCrop.top < 0) {
3193 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
3194 }
Dan Stozabe31f442014-06-11 11:20:54 -07003195 if (sourceCrop.bottom > hw_h) {
3196 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07003197 }
3198
Mathias Agopian180f10d2013-04-10 22:55:41 -07003199 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07003200 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003201
3202 // set-up our viewport
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003203 engine.setViewportAndProjection(
3204 reqWidth, reqHeight, sourceCrop, hw_h, yswap, rotation);
Mathias Agopian3f844832013-08-07 21:24:32 -07003205 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003206
3207 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07003208 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003209
3210 const LayerVector& layers( mDrawingState.layersSortedByZ );
3211 const size_t count = layers.size();
3212 for (size_t i=0 ; i<count ; ++i) {
3213 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003214 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07003215 if (state.layerStack == hw->getLayerStack()) {
3216 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
3217 if (layer->isVisible()) {
3218 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08003219 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003220 if (filtering) layer->setFiltering(false);
3221 }
3222 }
3223 }
3224 }
3225
3226 // compositionComplete is needed for older driver
3227 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07003228 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003229}
3230
3231
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003232status_t SurfaceFlinger::captureScreenImplLocked(
3233 const sp<const DisplayDevice>& hw,
3234 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07003235 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003236 uint32_t minLayerZ, uint32_t maxLayerZ,
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003237 bool useIdentityTransform, Transform::orientation_flags rotation)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003238{
3239 ATRACE_CALL();
3240
Mathias Agopian180f10d2013-04-10 22:55:41 -07003241 // get screen geometry
3242 const uint32_t hw_w = hw->getWidth();
3243 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003244
Mathias Agopian180f10d2013-04-10 22:55:41 -07003245 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3246 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3247 reqWidth, reqHeight, hw_w, hw_h);
3248 return BAD_VALUE;
3249 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003250
Mathias Agopian180f10d2013-04-10 22:55:41 -07003251 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3252 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3253
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003254 // create a surface (because we're a producer, and we need to
3255 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003256 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003257 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003258
3259 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003260 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003261 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3262 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003263
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003264 int err = 0;
3265 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003266 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003267 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3268 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003269
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003270 if (err == NO_ERROR) {
3271 ANativeWindowBuffer* buffer;
3272 /* TODO: Once we have the sync framework everywhere this can use
3273 * server-side waits on the fence that dequeueBuffer returns.
3274 */
3275 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3276 if (result == NO_ERROR) {
Riley Andrews86639902014-08-15 12:27:24 -07003277 int syncFd = -1;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003278 // create an EGLImage from the buffer so we can later
3279 // turn it into a texture
3280 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3281 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3282 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003283 // this binds the given EGLImage as a framebuffer for the
3284 // duration of this scope.
3285 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3286 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003287 // this will in fact render into our dequeued buffer
3288 // via an FBO, which means we didn't have to create
3289 // an EGLSurface and therefore we're not
3290 // dependent on the context's EGLConfig.
Riley Andrewsc3ebe662014-09-04 16:20:31 -07003291 renderScreenImplLocked(
3292 hw, sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ, true,
3293 useIdentityTransform, rotation);
Mathias Agopiand5556842013-09-19 17:08:37 -07003294
Riley Andrews86639902014-08-15 12:27:24 -07003295 // Attempt to create a sync khr object that can produce a sync point. If that
3296 // isn't available, create a non-dupable sync object in the fallback path and
3297 // wait on it directly.
3298 EGLSyncKHR sync;
3299 if (!DEBUG_SCREENSHOTS) {
3300 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
Riley Andrews9707f4d2014-10-23 16:17:04 -07003301 // native fence fd will not be populated until flush() is done.
3302 getRenderEngine().flush();
Andy McFadden2d8d1202013-10-09 16:38:02 -07003303 } else {
Riley Andrews86639902014-08-15 12:27:24 -07003304 sync = EGL_NO_SYNC_KHR;
Andy McFadden2d8d1202013-10-09 16:38:02 -07003305 }
Riley Andrews86639902014-08-15 12:27:24 -07003306 if (sync != EGL_NO_SYNC_KHR) {
3307 // get the sync fd
3308 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
3309 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
3310 ALOGW("captureScreen: failed to dup sync khr object");
3311 syncFd = -1;
3312 }
3313 eglDestroySyncKHR(mEGLDisplay, sync);
3314 } else {
3315 // fallback path
3316 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3317 if (sync != EGL_NO_SYNC_KHR) {
3318 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3319 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3320 EGLint eglErr = eglGetError();
3321 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3322 ALOGW("captureScreen: fence wait timed out");
3323 } else {
3324 ALOGW_IF(eglErr != EGL_SUCCESS,
3325 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3326 }
3327 eglDestroySyncKHR(mEGLDisplay, sync);
3328 } else {
3329 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3330 }
3331 }
Mathias Agopiand5556842013-09-19 17:08:37 -07003332 if (DEBUG_SCREENSHOTS) {
3333 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3334 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3335 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3336 hw, minLayerZ, maxLayerZ);
3337 delete [] pixels;
3338 }
3339
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003340 } else {
3341 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3342 result = INVALID_OPERATION;
3343 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003344 // destroy our image
3345 eglDestroyImageKHR(mEGLDisplay, image);
3346 } else {
3347 result = BAD_VALUE;
3348 }
Jesse Hallafe2b1f2014-10-21 11:09:17 -07003349 // queueBuffer takes ownership of syncFd
Riley Andrews86639902014-08-15 12:27:24 -07003350 window->queueBuffer(window, buffer, syncFd);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003351 }
3352 } else {
3353 result = BAD_VALUE;
3354 }
3355 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3356 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003357
Mathias Agopian74c40c02010-09-29 13:02:36 -07003358 return result;
3359}
3360
Mathias Agopiand5556842013-09-19 17:08:37 -07003361void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3362 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003363 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003364 for (size_t y=0 ; y<h ; y++) {
3365 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3366 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003367 if (p[x] != 0xFF000000) return;
3368 }
3369 }
3370 ALOGE("*** we just took a black screenshot ***\n"
3371 "requested minz=%d, maxz=%d, layerStack=%d",
3372 minLayerZ, maxLayerZ, hw->getLayerStack());
3373 const LayerVector& layers( mDrawingState.layersSortedByZ );
3374 const size_t count = layers.size();
3375 for (size_t i=0 ; i<count ; ++i) {
3376 const sp<Layer>& layer(layers[i]);
3377 const Layer::State& state(layer->getDrawingState());
3378 const bool visible = (state.layerStack == hw->getLayerStack())
3379 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3380 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003381 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003382 visible ? '+' : '-',
3383 i, layer->getName().string(), state.layerStack, state.z,
3384 layer->isVisible(), state.flags, state.alpha);
3385 }
3386 }
3387}
3388
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003389// ---------------------------------------------------------------------------
3390
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003391SurfaceFlinger::LayerVector::LayerVector() {
3392}
3393
3394SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003395 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003396}
3397
3398int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3399 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003400{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003401 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003402 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3403 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003404
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003405 uint32_t ls = l->getCurrentState().layerStack;
3406 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003407 if (ls != rs)
3408 return ls - rs;
3409
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003410 uint32_t lz = l->getCurrentState().z;
3411 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003412 if (lz != rz)
3413 return lz - rz;
3414
3415 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003416}
3417
3418// ---------------------------------------------------------------------------
3419
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003420SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
Michael Lentine47e45402014-07-18 15:34:25 -07003421 : type(DisplayDevice::DISPLAY_ID_INVALID), width(0), height(0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003422}
3423
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003424SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Michael Lentine47e45402014-07-18 15:34:25 -07003425 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0), width(0), height(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003426 viewport.makeInvalid();
3427 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003428}
3429
3430// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003431
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003432}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003433
3434
3435#if defined(__gl_h_)
3436#error "don't include gl/gl.h in this file"
3437#endif
3438
3439#if defined(__gl2_h_)
3440#error "don't include gl2/gl2.h in this file"
3441#endif