blob: edf867a9674b760d877992db5cbb99b650935c9f [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>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070025
26#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
28#include <cutils/log.h>
29#include <cutils/properties.h>
30
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070031#include <binder/IPCThreadState.h>
32#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070033#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070034#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070035
Mathias Agopianc666cae2012-07-25 18:56:13 -070036#include <ui/DisplayInfo.h>
37
Mathias Agopian921e6ac2012-07-23 23:11:29 -070038#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070039#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070040#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070041#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080042#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080043#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070044
45#include <ui/GraphicBufferAllocator.h>
46#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070047#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080048
Mathias Agopiancde87a32012-09-13 14:09:01 -070049#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050#include <utils/String8.h>
51#include <utils/String16.h>
52#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080053#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
Mathias Agopian921e6ac2012-07-23 23:11:29 -070055#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070056#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
Mathias Agopian3e25fd82013-04-22 17:52:16 +020058#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020060#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080061#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070062#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070063#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070064#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080065#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069
Mathias Agopiana4912602012-07-12 14:25:33 -070070#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070071#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070072#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073
Mathias Agopianff2ed702013-09-01 21:36:12 -070074#include "Effects/Daltonizer.h"
75
Mathias Agopian875d8e12013-06-07 15:35:48 -070076#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070077#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070078
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079#define DISPLAY_COUNT 1
80
Mathias Agopianfee2b462013-07-03 12:34:01 -070081/*
82 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
83 * black pixels.
84 */
85#define DEBUG_SCREENSHOTS false
86
Mathias Agopianca088332013-03-28 17:44:13 -070087EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
88
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070090
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070091// This is the phase offset in nanoseconds of the software vsync event
92// relative to the vsync event reported by HWComposer. The software vsync
93// event is when SurfaceFlinger and Choreographer-based applications run each
94// frame.
95//
96// This phase offset allows adjustment of the minimum latency from application
97// wake-up (by Choregographer) time to the time at which the resulting window
98// image is displayed. This value may be either positive (after the HW vsync)
99// or negative (before the HW vsync). Setting it to 0 will result in a
100// minimum latency of two vsync periods because the app and SurfaceFlinger
101// will run just after the HW vsync. Setting it to a positive number will
102// result in the minimum latency being:
103//
104// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
105//
106// Note that reducing this latency makes it more likely for the applications
107// to not have their window content image ready in time. When this happens
108// the latency will end up being an additional vsync period, and animations
109// will hiccup. Therefore, this latency should be tuned somewhat
110// conservatively (or at least with awareness of the trade-off being made).
111static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
112
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700113// This is the phase offset at which SurfaceFlinger's composition runs.
114static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
115
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116// ---------------------------------------------------------------------------
117
Mathias Agopian99b49842011-06-27 16:05:52 -0700118const String16 sHardwareTest("android.permission.HARDWARE_TEST");
119const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
120const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
121const String16 sDump("android.permission.DUMP");
122
123// ---------------------------------------------------------------------------
124
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700126 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700128 mTransactionPending(false),
129 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700130 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700131 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700132 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700135 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800136 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700138 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700139 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700140 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700141 mDebugInSwapBuffers(0),
142 mLastSwapBufferTime(0),
143 mDebugInTransaction(0),
144 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700145 mBootFinished(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700146 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700147 mHWVsyncAvailable(false),
Alan Viverette9c5a3332013-09-12 20:04:35 -0700148 mDaltonize(false),
149 mHasColorMatrix(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150{
Steve Blocka19954a2012-01-04 20:05:49 +0000151 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152
153 // debugging stuff...
154 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700155
Mathias Agopianb4b17302013-03-20 18:36:41 -0700156 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700157 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700158
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 property_get("debug.sf.showupdates", value, "0");
160 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700161
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700162 property_get("debug.sf.ddms", value, "0");
163 mDebugDDMS = atoi(value);
164 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700165 if (!startDdmConnection()) {
166 // start failed, and DDMS debugging not enabled
167 mDebugDDMS = 0;
168 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700169 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700170 ALOGI_IF(mDebugRegion, "showupdates enabled");
171 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172}
173
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800174void SurfaceFlinger::onFirstRef()
175{
176 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800177}
178
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179SurfaceFlinger::~SurfaceFlinger()
180{
Mathias Agopiana4912602012-07-12 14:25:33 -0700181 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
182 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
183 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184}
185
Dan Stozac7014012014-02-14 15:03:43 -0800186void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800187{
188 // the window manager died on us. prepare its eulogy.
189
Andy McFadden13a082e2012-08-24 10:16:42 -0700190 // restore initial conditions (default device unblank, etc)
191 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800192
193 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700194 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800195}
196
Mathias Agopian7e27f052010-05-28 14:22:23 -0700197sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800198{
Mathias Agopian96f08192010-06-02 23:28:45 -0700199 sp<ISurfaceComposerClient> bclient;
200 sp<Client> client(new Client(this));
201 status_t err = client->initCheck();
202 if (err == NO_ERROR) {
203 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205 return bclient;
206}
207
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700208sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
209 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700210{
211 class DisplayToken : public BBinder {
212 sp<SurfaceFlinger> flinger;
213 virtual ~DisplayToken() {
214 // no more references, this display must be terminated
215 Mutex::Autolock _l(flinger->mStateLock);
216 flinger->mCurrentState.displays.removeItem(this);
217 flinger->setTransactionFlags(eDisplayTransactionNeeded);
218 }
219 public:
220 DisplayToken(const sp<SurfaceFlinger>& flinger)
221 : flinger(flinger) {
222 }
223 };
224
225 sp<BBinder> token = new DisplayToken(this);
226
227 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700228 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700229 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700230 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700231 mCurrentState.displays.add(token, info);
232
233 return token;
234}
235
Jesse Hall6c913be2013-08-08 12:15:49 -0700236void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
237 Mutex::Autolock _l(mStateLock);
238
239 ssize_t idx = mCurrentState.displays.indexOfKey(display);
240 if (idx < 0) {
241 ALOGW("destroyDisplay: invalid display token");
242 return;
243 }
244
245 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
246 if (!info.isVirtualDisplay()) {
247 ALOGE("destroyDisplay called for non-virtual display");
248 return;
249 }
250
251 mCurrentState.displays.removeItemsAt(idx);
252 setTransactionFlags(eDisplayTransactionNeeded);
253}
254
Jesse Hall692c7232012-11-08 15:41:56 -0800255void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
256 ALOGW_IF(mBuiltinDisplays[type],
257 "Overwriting display token for display type %d", type);
258 mBuiltinDisplays[type] = new BBinder();
259 DisplayDeviceState info(type);
260 // All non-virtual displays are currently considered secure.
261 info.isSecure = true;
262 mCurrentState.displays.add(mBuiltinDisplays[type], info);
263}
264
Mathias Agopiane57f2922012-08-09 16:29:12 -0700265sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700266 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700267 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
268 return NULL;
269 }
Jesse Hall692c7232012-11-08 15:41:56 -0800270 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700271}
272
Jamie Gennis9a78c902011-01-12 18:30:40 -0800273sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
274{
275 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
276 return gba;
277}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700278
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800279void SurfaceFlinger::bootFinished()
280{
281 const nsecs_t now = systemTime();
282 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000283 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700284 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700285
286 // wait patiently for the window manager death
287 const String16 name("window");
288 sp<IBinder> window(defaultServiceManager()->getService(name));
289 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700290 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700291 }
292
293 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700294 // formerly we would just kill the process, but we now ask it to exit so it
295 // can choose where to stop the animation.
296 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800297}
298
Mathias Agopian3f844832013-08-07 21:24:32 -0700299void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700300 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700301 RenderEngine& engine;
302 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700303 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700304 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
305 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700306 }
307 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700308 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700309 return true;
310 }
311 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700312 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700313}
314
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700315class DispSyncSource : public VSyncSource, private DispSync::Callback {
316public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700317 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
318 const char* label) :
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700319 mValue(0),
320 mPhaseOffset(phaseOffset),
321 mTraceVsync(traceVsync),
Andy McFadden5167ec62014-05-22 13:08:43 -0700322 mVsyncOnLabel(String8::format("VsyncOn-%s", label)),
323 mVsyncEventLabel(String8::format("VSYNC-%s", label)),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700324 mDispSync(dispSync) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700325
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700326 virtual ~DispSyncSource() {}
327
328 virtual void setVSyncEnabled(bool enable) {
329 // Do NOT lock the mutex here so as to avoid any mutex ordering issues
330 // with locking it in the onDispSyncEvent callback.
331 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700332 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700333 static_cast<DispSync::Callback*>(this));
334 if (err != NO_ERROR) {
335 ALOGE("error registering vsync callback: %s (%d)",
336 strerror(-err), err);
337 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700338 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700339 } else {
340 status_t err = mDispSync->removeEventListener(
341 static_cast<DispSync::Callback*>(this));
342 if (err != NO_ERROR) {
343 ALOGE("error unregistering vsync callback: %s (%d)",
344 strerror(-err), err);
345 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700346 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700347 }
348 }
349
350 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
351 Mutex::Autolock lock(mMutex);
352 mCallback = callback;
353 }
354
355private:
356 virtual void onDispSyncEvent(nsecs_t when) {
357 sp<VSyncSource::Callback> callback;
358 {
359 Mutex::Autolock lock(mMutex);
360 callback = mCallback;
361
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700362 if (mTraceVsync) {
363 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700364 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700365 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700366 }
367
368 if (callback != NULL) {
369 callback->onVSyncEvent(when);
370 }
371 }
372
373 int mValue;
374
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700375 const nsecs_t mPhaseOffset;
376 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700377 const String8 mVsyncOnLabel;
378 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700379
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700380 DispSync* mDispSync;
381 sp<VSyncSource::Callback> mCallback;
382 Mutex mMutex;
383};
384
385void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700386 ALOGI( "SurfaceFlinger's main thread ready to run. "
387 "Initializing graphics H/W...");
388
Jesse Hallb65f32e2013-09-27 22:10:47 -0700389 status_t err;
Jesse Hall692c7232012-11-08 15:41:56 -0800390 Mutex::Autolock _l(mStateLock);
391
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700392 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700393 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
394 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700395
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700396 // Initialize the H/W composer object. There may or may not be an
397 // actual hardware composer underneath.
398 mHwc = new HWComposer(this,
399 *static_cast<HWComposer::EventHandler *>(this));
400
Mathias Agopian875d8e12013-06-07 15:35:48 -0700401 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800402 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700403
404 // retrieve the EGL context that was selected/created
405 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700406
Mathias Agopianda27af92012-09-13 18:17:13 -0700407 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
408 "couldn't create EGLContext");
409
Mathias Agopiancde87a32012-09-13 14:09:01 -0700410 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700411 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700412 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700413 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700414 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700415 // All non-virtual displays are currently considered secure.
416 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800417 createBuiltinDisplayLocked(type);
418 wp<IBinder> token = mBuiltinDisplays[i];
419
Dan Stozab9b08832014-03-13 11:55:57 -0700420 sp<IGraphicBufferProducer> producer;
421 sp<IGraphicBufferConsumer> consumer;
422 BufferQueue::createBufferQueue(&producer, &consumer,
423 new GraphicBufferAlloc());
424
425 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
426 consumer);
Jesse Hall19e87292013-12-23 21:02:15 -0800427 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700428 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800429 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Dan Stozab9b08832014-03-13 11:55:57 -0700430 fbs, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -0800431 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700432 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700433 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700434 // for displays other than the main display, so we always
435 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800436 ALOGD("marking display %zu as acquired/unblanked", i);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700437 hw->setPowerMode(HWC_POWER_MODE_NORMAL);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700438 }
439 mDisplays.add(token, hw);
440 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700441 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700442
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700443 // make the GLContext current so that we can create textures when creating Layers
444 // (which may happens before we render something)
445 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
446
Mathias Agopian028508c2012-07-25 21:12:12 -0700447 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700448 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700449 vsyncPhaseOffsetNs, true, "app");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700450 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700451 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700452 sfVsyncPhaseOffsetNs, true, "sf");
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700453 mSFEventThread = new EventThread(sfVsyncSrc);
454 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700455
Jamie Gennisd1700752013-10-14 12:22:52 -0700456 mEventControlThread = new EventControlThread(this);
457 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
458
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700459 // set a fake vsync period if there is no HWComposer
460 if (mHwc->initCheck() != NO_ERROR) {
461 mPrimaryDispSync.setPeriod(16666667);
462 }
463
Mathias Agopian92a979a2012-08-02 18:32:23 -0700464 // initialize our drawing state
465 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700466
Andy McFadden13a082e2012-08-24 10:16:42 -0700467 // set initial conditions (e.g. unblank default device)
468 initializeDisplays();
469
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700470 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700471 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800472}
473
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700474int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700475 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700476 type : mHwc->allocateDisplayId();
477}
478
Mathias Agopiana67e4182012-06-19 17:26:12 -0700479void SurfaceFlinger::startBootAnim() {
480 // start boot animation
481 property_set("service.bootanim.exit", "0");
482 property_set("ctl.start", "bootanim");
483}
484
Mathias Agopian875d8e12013-06-07 15:35:48 -0700485size_t SurfaceFlinger::getMaxTextureSize() const {
486 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700487}
488
Mathias Agopian875d8e12013-06-07 15:35:48 -0700489size_t SurfaceFlinger::getMaxViewportDims() const {
490 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700491}
492
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800493// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800494
Jamie Gennis582270d2011-08-17 18:19:00 -0700495bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800496 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800497 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800498 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700499 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800500}
501
Dan Stoza7f7da322014-05-02 15:26:25 -0700502status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
503 Vector<DisplayInfo>* configs) {
504 if (configs == NULL) {
505 return BAD_VALUE;
506 }
507
Jesse Hall692c7232012-11-08 15:41:56 -0800508 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700509 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800510 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700511 type = i;
512 break;
513 }
514 }
515
516 if (type < 0) {
517 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700518 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700519
Mathias Agopian8b736f12012-08-13 17:54:26 -0700520 // TODO: Not sure if display density should handled by SF any longer
521 class Density {
522 static int getDensityFromProperty(char const* propName) {
523 char property[PROPERTY_VALUE_MAX];
524 int density = 0;
525 if (property_get(propName, property, NULL) > 0) {
526 density = atoi(property);
527 }
528 return density;
529 }
530 public:
531 static int getEmuDensity() {
532 return getDensityFromProperty("qemu.sf.lcd_density"); }
533 static int getBuildDensity() {
534 return getDensityFromProperty("ro.sf.lcd_density"); }
535 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700536
Dan Stoza7f7da322014-05-02 15:26:25 -0700537 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700538
Dan Stoza7f7da322014-05-02 15:26:25 -0700539 const Vector<HWComposer::DisplayConfig>& hwConfigs =
540 getHwComposer().getConfigs(type);
541 for (size_t c = 0; c < hwConfigs.size(); ++c) {
542 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
543 DisplayInfo info = DisplayInfo();
544
545 float xdpi = hwConfig.xdpi;
546 float ydpi = hwConfig.ydpi;
547
548 if (type == DisplayDevice::DISPLAY_PRIMARY) {
549 // The density of the device is provided by a build property
550 float density = Density::getBuildDensity() / 160.0f;
551 if (density == 0) {
552 // the build doesn't provide a density -- this is wrong!
553 // use xdpi instead
554 ALOGE("ro.sf.lcd_density must be defined as a build property");
555 density = xdpi / 160.0f;
556 }
557 if (Density::getEmuDensity()) {
558 // if "qemu.sf.lcd_density" is specified, it overrides everything
559 xdpi = ydpi = density = Density::getEmuDensity();
560 density /= 160.0f;
561 }
562 info.density = density;
563
564 // TODO: this needs to go away (currently needed only by webkit)
565 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
566 info.orientation = hw->getOrientation();
567 } else {
568 // TODO: where should this value come from?
569 static const int TV_DENSITY = 213;
570 info.density = TV_DENSITY / 160.0f;
571 info.orientation = 0;
572 }
573
574 info.w = hwConfig.width;
575 info.h = hwConfig.height;
576 info.xdpi = xdpi;
577 info.ydpi = ydpi;
578 info.fps = float(1e9 / hwConfig.refresh);
579
580 // All non-virtual displays are currently considered secure.
581 info.secure = true;
582
583 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700584 }
585
Dan Stoza7f7da322014-05-02 15:26:25 -0700586 return NO_ERROR;
587}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700588
Dan Stoza7f7da322014-05-02 15:26:25 -0700589int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
590 return 0;
591}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700592
Dan Stoza7f7da322014-05-02 15:26:25 -0700593status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int id) {
Mathias Agopian888c8222012-08-04 21:10:38 -0700594 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700595}
596
Svetoslavd85084b2014-03-20 10:28:31 -0700597status_t SurfaceFlinger::clearAnimationFrameStats() {
598 Mutex::Autolock _l(mStateLock);
599 mAnimFrameTracker.clearStats();
600 return NO_ERROR;
601}
602
603status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
604 Mutex::Autolock _l(mStateLock);
605 mAnimFrameTracker.getStats(outStats);
606 return NO_ERROR;
607}
608
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800609// ----------------------------------------------------------------------------
610
611sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800612 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700613}
614
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800616
617void SurfaceFlinger::waitForEvent() {
618 mEventQueue.waitMessage();
619}
620
621void SurfaceFlinger::signalTransaction() {
622 mEventQueue.invalidate();
623}
624
625void SurfaceFlinger::signalLayerUpdate() {
626 mEventQueue.invalidate();
627}
628
629void SurfaceFlinger::signalRefresh() {
630 mEventQueue.refresh();
631}
632
633status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800634 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800635 return mEventQueue.postMessage(msg, reltime);
636}
637
638status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800639 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800640 status_t res = mEventQueue.postMessage(msg, reltime);
641 if (res == NO_ERROR) {
642 msg->wait();
643 }
644 return res;
645}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700647void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700648 do {
649 waitForEvent();
650 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800651}
652
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700653void SurfaceFlinger::enableHardwareVsync() {
654 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700655 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700656 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700657 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
658 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700659 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700660 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700661}
662
Jesse Hall948fe0c2013-10-14 12:56:09 -0700663void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700664 Mutex::Autolock _l(mHWVsyncLock);
665
Jesse Hall948fe0c2013-10-14 12:56:09 -0700666 if (makeAvailable) {
667 mHWVsyncAvailable = true;
668 } else if (!mHWVsyncAvailable) {
669 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
670 return;
671 }
672
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700673 const nsecs_t period =
674 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
675
676 mPrimaryDispSync.reset();
677 mPrimaryDispSync.setPeriod(period);
678
679 if (!mPrimaryHWVsyncEnabled) {
680 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700681 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
682 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700683 mPrimaryHWVsyncEnabled = true;
684 }
685}
686
Jesse Hall948fe0c2013-10-14 12:56:09 -0700687void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700688 Mutex::Autolock _l(mHWVsyncLock);
689 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700690 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
691 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700692 mPrimaryDispSync.endResync();
693 mPrimaryHWVsyncEnabled = false;
694 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700695 if (makeUnavailable) {
696 mHWVsyncAvailable = false;
697 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700698}
699
700void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700701 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700702
Jamie Gennisd1700752013-10-14 12:22:52 -0700703 { // Scope for the lock
704 Mutex::Autolock _l(mHWVsyncLock);
705 if (type == 0 && mPrimaryHWVsyncEnabled) {
706 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700707 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700708 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700709
710 if (needsHwVsync) {
711 enableHardwareVsync();
712 } else {
713 disableHardwareVsync(false);
714 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700715}
716
717void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
718 if (mEventThread == NULL) {
719 // This is a temporary workaround for b/7145521. A non-null pointer
720 // does not mean EventThread has finished initializing, so this
721 // is not a correct fix.
722 ALOGW("WARNING: EventThread not started, ignoring hotplug");
723 return;
724 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700725
Jesse Hall9e663de2013-08-16 14:28:37 -0700726 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700727 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800728 if (connected) {
729 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700730 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800731 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
732 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700733 }
734 setTransactionFlags(eDisplayTransactionNeeded);
735
Andy McFadden9e9689c2012-10-10 18:17:51 -0700736 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700737 }
Mathias Agopian86303202012-07-24 22:46:10 -0700738}
739
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700740void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700741 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700742 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700743}
744
Mathias Agopian4fec8732012-06-29 14:12:52 -0700745void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800746 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800747 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700748 case MessageQueue::TRANSACTION:
749 handleMessageTransaction();
750 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700751 case MessageQueue::INVALIDATE:
752 handleMessageTransaction();
753 handleMessageInvalidate();
754 signalRefresh();
755 break;
756 case MessageQueue::REFRESH:
757 handleMessageRefresh();
758 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800759 }
760}
761
Mathias Agopian4fec8732012-06-29 14:12:52 -0700762void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700763 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700764 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700765 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700766 }
767}
768
769void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700770 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700771 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700772}
773
774void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700775 ATRACE_CALL();
776 preComposition();
777 rebuildLayerStacks();
778 setUpHWComposer();
779 doDebugFlashRegions();
780 doComposition();
781 postComposition();
782}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700783
Mathias Agopiancd60f992012-08-16 16:28:27 -0700784void SurfaceFlinger::doDebugFlashRegions()
785{
786 // is debugging enabled
787 if (CC_LIKELY(!mDebugRegion))
788 return;
789
790 const bool repaintEverything = mRepaintEverything;
791 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
792 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700793 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700794 // transform the dirty region into this screen's coordinate space
795 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
796 if (!dirtyRegion.isEmpty()) {
797 // redraw the whole screen
798 doComposeSurfaces(hw, Region(hw->bounds()));
799
800 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700801 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700802 RenderEngine& engine(getRenderEngine());
803 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
804
Mathias Agopiancd60f992012-08-16 16:28:27 -0700805 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700806 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700807 }
808 }
809 }
810
811 postFramebuffer();
812
813 if (mDebugRegion > 1) {
814 usleep(mDebugRegion * 1000);
815 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700816
817 HWComposer& hwc(getHwComposer());
818 if (hwc.initCheck() == NO_ERROR) {
819 status_t err = hwc.prepare();
820 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
821 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700822}
823
824void SurfaceFlinger::preComposition()
825{
826 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700827 const LayerVector& layers(mDrawingState.layersSortedByZ);
828 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700829 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700830 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700831 needExtraInvalidate = true;
832 }
833 }
834 if (needExtraInvalidate) {
835 signalLayerUpdate();
836 }
837}
838
839void SurfaceFlinger::postComposition()
840{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700841 const LayerVector& layers(mDrawingState.layersSortedByZ);
842 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700843 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700844 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700845 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800846
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700847 const HWComposer& hwc = getHwComposer();
848 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
849
850 if (presentFence->isValid()) {
851 if (mPrimaryDispSync.addPresentFence(presentFence)) {
852 enableHardwareVsync();
853 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700854 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700855 }
856 }
857
Andy McFadden5167ec62014-05-22 13:08:43 -0700858 if (kIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700859 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700860 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700861 enableHardwareVsync();
862 }
863 }
864
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800865 if (mAnimCompositionPending) {
866 mAnimCompositionPending = false;
867
Jesse Halla9a1b002013-02-27 16:39:25 -0800868 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800869 mAnimFrameTracker.setActualPresentFence(presentFence);
870 } else {
871 // The HWC doesn't support present fences, so use the refresh
872 // timestamp instead.
873 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
874 mAnimFrameTracker.setActualPresentTime(presentTime);
875 }
876 mAnimFrameTracker.advanceFrame();
877 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700878}
879
880void SurfaceFlinger::rebuildLayerStacks() {
881 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700882 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700883 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700884 mVisibleRegionsDirty = false;
885 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700886
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700887 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700888 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700889 Region opaqueRegion;
890 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800891 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700892 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700893 const Transform& tr(hw->getTransform());
894 const Rect bounds(hw->getBounds());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700895 if (hw->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700896 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700897 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700898
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700899 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700900 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700901 const sp<Layer>& layer(layers[i]);
902 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700903 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700904 Region drawRegion(tr.transform(
905 layer->visibleNonTransparentRegion));
906 drawRegion.andSelf(bounds);
907 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700908 layersSortedByZ.add(layer);
909 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700910 }
911 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700912 }
Mathias Agopian42977342012-08-05 00:40:46 -0700913 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700914 hw->undefinedRegion.set(bounds);
915 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
916 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700917 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700918 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700919}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700920
Mathias Agopiancd60f992012-08-16 16:28:27 -0700921void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700922 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Dan Stoza71433162014-02-04 16:22:36 -0800923 bool mustRecompose =
924 !(mDisplays[dpy]->getDirtyRegion(false).isEmpty());
925 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700926 }
927
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700928 HWComposer& hwc(getHwComposer());
929 if (hwc.initCheck() == NO_ERROR) {
930 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700931 if (CC_UNLIKELY(mHwWorkListDirty)) {
932 mHwWorkListDirty = false;
933 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
934 sp<const DisplayDevice> hw(mDisplays[dpy]);
935 const int32_t id = hw->getHwcDisplayId();
936 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800937 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700938 hw->getVisibleLayersSortedByZ());
939 const size_t count = currentLayers.size();
940 if (hwc.createWorkList(id, count) == NO_ERROR) {
941 HWComposer::LayerListIterator cur = hwc.begin(id);
942 const HWComposer::LayerListIterator end = hwc.end(id);
943 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800944 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700945 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -0700946 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -0700947 cur->setSkip(true);
948 }
949 }
950 }
951 }
952 }
953 }
954
955 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700956 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700957 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700958 const int32_t id = hw->getHwcDisplayId();
959 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800960 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700961 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700962 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700963 HWComposer::LayerListIterator cur = hwc.begin(id);
964 const HWComposer::LayerListIterator end = hwc.end(id);
965 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
966 /*
967 * update the per-frame h/w composer data for each layer
968 * and build the transparent region of the FB
969 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800970 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700971 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700972 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700973 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700974 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700975
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700976 status_t err = hwc.prepare();
977 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700978
979 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
980 sp<const DisplayDevice> hw(mDisplays[dpy]);
981 hw->prepareFrame(hwc);
982 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700983 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700984}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700985
Mathias Agopiancd60f992012-08-16 16:28:27 -0700986void SurfaceFlinger::doComposition() {
987 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700988 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700989 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700990 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700991 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700992 // transform the dirty region into this screen's coordinate space
993 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800994
995 // repaint the framebuffer (if needed)
996 doDisplayComposition(hw, dirtyRegion);
997
Mathias Agopiancd60f992012-08-16 16:28:27 -0700998 hw->dirtyRegion.clear();
999 hw->flip(hw->swapRegion);
1000 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001001 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001002 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001003 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001004 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001005 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001006}
1007
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001008void SurfaceFlinger::postFramebuffer()
1009{
Mathias Agopian841cde52012-03-01 15:44:37 -08001010 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001011
Mathias Agopiana44b0412011-10-16 18:46:35 -07001012 const nsecs_t now = systemTime();
1013 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001014
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001015 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001016 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001017 if (!hwc.supportsFramebufferTarget()) {
1018 // EGL spec says:
1019 // "surface must be bound to the calling thread's current context,
1020 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001021 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001022 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001023 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001024 }
1025
Mathias Agopian6da15f42013-09-25 20:40:07 -07001026 // make the default display current because the VirtualDisplayDevice code cannot
1027 // deal with dequeueBuffer() being called outside of the composition loop; however
1028 // the code below can call glFlush() which is allowed (and does in some case) call
1029 // dequeueBuffer().
1030 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1031
Mathias Agopian92a979a2012-08-02 18:32:23 -07001032 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001033 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001034 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001035 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001036 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001037 int32_t id = hw->getHwcDisplayId();
1038 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001039 HWComposer::LayerListIterator cur = hwc.begin(id);
1040 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001041 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001042 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001043 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001044 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001045 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001046 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001047 }
Jesse Hallef194142012-06-14 14:45:17 -07001048 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001049 }
1050
Mathias Agopiana44b0412011-10-16 18:46:35 -07001051 mLastSwapBufferTime = systemTime() - now;
1052 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001053
1054 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1055 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1056 logFrameStats();
1057 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058}
1059
Mathias Agopian87baae12012-07-31 12:38:26 -07001060void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001061{
Mathias Agopian841cde52012-03-01 15:44:37 -08001062 ATRACE_CALL();
1063
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001064 // here we keep a copy of the drawing state (that is the state that's
1065 // going to be overwritten by handleTransactionLocked()) outside of
1066 // mStateLock so that the side-effects of the State assignment
1067 // don't happen with mStateLock held (which can cause deadlocks).
1068 State drawingState(mDrawingState);
1069
Mathias Agopianca4d3602011-05-19 15:38:14 -07001070 Mutex::Autolock _l(mStateLock);
1071 const nsecs_t now = systemTime();
1072 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073
Mathias Agopianca4d3602011-05-19 15:38:14 -07001074 // Here we're guaranteed that some transaction flags are set
1075 // so we can call handleTransactionLocked() unconditionally.
1076 // We call getTransactionFlags(), which will also clear the flags,
1077 // with mStateLock held to guarantee that mCurrentState won't change
1078 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001079
Mathias Agopiane57f2922012-08-09 16:29:12 -07001080 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001081 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001082
Mathias Agopianca4d3602011-05-19 15:38:14 -07001083 mLastTransactionTime = systemTime() - now;
1084 mDebugInTransaction = 0;
1085 invalidateHwcGeometry();
1086 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001087}
1088
Mathias Agopian87baae12012-07-31 12:38:26 -07001089void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001090{
1091 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 const size_t count = currentLayers.size();
1093
1094 /*
1095 * Traversal of the children
1096 * (perform the transaction for each of them if needed)
1097 */
1098
Mathias Agopian3559b072012-08-15 13:46:03 -07001099 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001100 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001101 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1103 if (!trFlags) continue;
1104
1105 const uint32_t flags = layer->doTransaction(0);
1106 if (flags & Layer::eVisibleRegion)
1107 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108 }
1109 }
1110
1111 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001112 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113 */
1114
Mathias Agopiane57f2922012-08-09 16:29:12 -07001115 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001116 // here we take advantage of Vector's copy-on-write semantics to
1117 // improve performance by skipping the transaction entirely when
1118 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001119 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1120 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001121 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001122 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001123 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001124 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001125
1126 // find the displays that were removed
1127 // (ie: in drawing state but not in current state)
1128 // also handle displays that changed
1129 // (ie: displays that are in both lists)
1130 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001131 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1132 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001133 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001134 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001135 // Call makeCurrent() on the primary display so we can
1136 // be sure that nothing associated with this display
1137 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001138 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001139 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001140 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1141 if (hw != NULL)
1142 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001143 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001144 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001145 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001146 } else {
1147 ALOGW("trying to remove the main display");
1148 }
1149 } else {
1150 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001151 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001152 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001153 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001154 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001155 // recreating the DisplayDevice, so we just remove it
1156 // from the drawing state, so that it get re-added
1157 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001158 sp<DisplayDevice> hw(getDisplayDevice(display));
1159 if (hw != NULL)
1160 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001161 mDisplays.removeItem(display);
1162 mDrawingState.displays.removeItemsAt(i);
1163 dc--; i--;
1164 // at this point we must loop to the next item
1165 continue;
1166 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001167
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001168 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001169 if (disp != NULL) {
1170 if (state.layerStack != draw[i].layerStack) {
1171 disp->setLayerStack(state.layerStack);
1172 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001173 if ((state.orientation != draw[i].orientation)
1174 || (state.viewport != draw[i].viewport)
1175 || (state.frame != draw[i].frame))
1176 {
1177 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001178 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001179 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001180 }
1181 }
1182 }
1183
1184 // find displays that were added
1185 // (ie: in current state but not in drawing state)
1186 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001187 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001188 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001189
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001190 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001191 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001192 sp<IGraphicBufferProducer> bqProducer;
1193 sp<IGraphicBufferConsumer> bqConsumer;
1194 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1195 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001196
Jesse Hall02d86562013-03-25 14:43:23 -07001197 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001198 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001199 // Virtual displays without a surface are dormant:
1200 // they have external state (layer stack, projection,
1201 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001202 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001203
Jesse Hall02d86562013-03-25 14:43:23 -07001204 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001205 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
Dan Stozab9b08832014-03-13 11:55:57 -07001206 *mHwc, hwcDisplayId, state.surface,
1207 bqProducer, bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001208
1209 dispSurface = vds;
1210 if (hwcDisplayId >= 0) {
1211 producer = vds;
1212 } else {
1213 // There won't be any interaction with HWC for this virtual display,
1214 // so the GLES driver can pass buffers directly to the sink.
1215 producer = state.surface;
1216 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001217 }
1218 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001219 ALOGE_IF(state.surface!=NULL,
1220 "adding a supported display, but rendering "
1221 "surface is provided (%p), ignoring it",
1222 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001223 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001224 // for supported (by hwc) displays we provide our
1225 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001226 dispSurface = new FramebufferSurface(*mHwc, state.type,
1227 bqConsumer);
1228 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001229 }
1230
1231 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001232 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001233 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001234 state.type, hwcDisplayId,
1235 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001236 display, dispSurface, producer,
1237 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001238 hw->setLayerStack(state.layerStack);
1239 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001240 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001241 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001242 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001243 if (state.isVirtualDisplay()) {
1244 if (hwcDisplayId >= 0) {
1245 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1246 hw->getWidth(), hw->getHeight(),
1247 hw->getFormat());
1248 }
1249 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001250 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001251 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001252 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001253 }
1254 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001256 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001257
Mathias Agopian84300952012-11-21 16:02:13 -08001258 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1259 // The transform hint might have changed for some layers
1260 // (either because a display has changed, or because a layer
1261 // as changed).
1262 //
1263 // Walk through all the layers in currentLayers,
1264 // and update their transform hint.
1265 //
1266 // If a layer is visible only on a single display, then that
1267 // display is used to calculate the hint, otherwise we use the
1268 // default display.
1269 //
1270 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1271 // the hint is set before we acquire a buffer from the surface texture.
1272 //
1273 // NOTE: layer transactions have taken place already, so we use their
1274 // drawing state. However, SurfaceFlinger's own transaction has not
1275 // happened yet, so we must use the current state layer list
1276 // (soon to become the drawing state list).
1277 //
1278 sp<const DisplayDevice> disp;
1279 uint32_t currentlayerStack = 0;
1280 for (size_t i=0; i<count; i++) {
1281 // NOTE: we rely on the fact that layers are sorted by
1282 // layerStack first (so we don't have to traverse the list
1283 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001284 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001285 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001286 if (i==0 || currentlayerStack != layerStack) {
1287 currentlayerStack = layerStack;
1288 // figure out if this layerstack is mirrored
1289 // (more than one display) if so, pick the default display,
1290 // if not, pick the only display it's on.
1291 disp.clear();
1292 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1293 sp<const DisplayDevice> hw(mDisplays[dpy]);
1294 if (hw->getLayerStack() == currentlayerStack) {
1295 if (disp == NULL) {
1296 disp = hw;
1297 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001298 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001299 break;
1300 }
1301 }
1302 }
1303 }
Chet Haase91d25932013-04-11 15:24:55 -07001304 if (disp == NULL) {
1305 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1306 // redraw after transform hint changes. See bug 8508397.
1307
1308 // could be null when this layer is using a layerStack
1309 // that is not visible on any display. Also can occur at
1310 // screen off/on times.
1311 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001312 }
Chet Haase91d25932013-04-11 15:24:55 -07001313 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001314 }
1315 }
1316
1317
Mathias Agopian3559b072012-08-15 13:46:03 -07001318 /*
1319 * Perform our own transaction if needed
1320 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001321
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001322 const LayerVector& layers(mDrawingState.layersSortedByZ);
1323 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001324 // layers have been added
1325 mVisibleRegionsDirty = true;
1326 }
1327
1328 // some layers might have been removed, so
1329 // we need to update the regions they're exposing.
1330 if (mLayersRemoved) {
1331 mLayersRemoved = false;
1332 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001333 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001334 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001335 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001336 if (currentLayers.indexOf(layer) < 0) {
1337 // this layer is not visible anymore
1338 // TODO: we could traverse the tree from front to back and
1339 // compute the actual visible region
1340 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001341 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001342 Region visibleReg = s.transform.transform(
1343 Region(Rect(s.active.w, s.active.h)));
1344 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001345 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001346 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001347 }
1348
1349 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001350}
1351
1352void SurfaceFlinger::commitTransaction()
1353{
1354 if (!mLayersPendingRemoval.isEmpty()) {
1355 // Notify removed layers now that they can't be drawn from
1356 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1357 mLayersPendingRemoval[i]->onRemoved();
1358 }
1359 mLayersPendingRemoval.clear();
1360 }
1361
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001362 // If this transaction is part of a window animation then the next frame
1363 // we composite should be considered an animation as well.
1364 mAnimCompositionPending = mAnimTransactionPending;
1365
Mathias Agopian4fec8732012-06-29 14:12:52 -07001366 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001367 mTransactionPending = false;
1368 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001369 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001370}
1371
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001372void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001373 const LayerVector& currentLayers, uint32_t layerStack,
1374 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375{
Mathias Agopian841cde52012-03-01 15:44:37 -08001376 ATRACE_CALL();
1377
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378 Region aboveOpaqueLayers;
1379 Region aboveCoveredLayers;
1380 Region dirty;
1381
Mathias Agopian87baae12012-07-31 12:38:26 -07001382 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383
1384 size_t i = currentLayers.size();
1385 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001386 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001387
1388 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001389 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390
Jesse Hall01e29052013-02-19 16:13:35 -08001391 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001392 if (s.layerStack != layerStack)
1393 continue;
1394
Mathias Agopianab028732010-03-16 16:41:46 -07001395 /*
1396 * opaqueRegion: area of a surface that is fully opaque.
1397 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001399
1400 /*
1401 * visibleRegion: area of a surface that is visible on screen
1402 * and not fully transparent. This is essentially the layer's
1403 * footprint minus the opaque regions above it.
1404 * Areas covered by a translucent surface are considered visible.
1405 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001407
1408 /*
1409 * coveredRegion: area of a surface that is covered by all
1410 * visible regions above it (which includes the translucent areas).
1411 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001413
Jesse Halla8026d22012-09-25 13:25:04 -07001414 /*
1415 * transparentRegion: area of a surface that is hinted to be completely
1416 * transparent. This is only used to tell when the layer has no visible
1417 * non-transparent regions and can be removed from the layer list. It
1418 * does not affect the visibleRegion of this layer or any layers
1419 * beneath it. The hint may not be correct if apps don't respect the
1420 * SurfaceView restrictions (which, sadly, some don't).
1421 */
1422 Region transparentRegion;
1423
Mathias Agopianab028732010-03-16 16:41:46 -07001424
1425 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001426 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001427 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001428 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001429 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001430 if (!visibleRegion.isEmpty()) {
1431 // Remove the transparent area from the visible region
1432 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001433 const Transform tr(s.transform);
1434 if (tr.transformed()) {
1435 if (tr.preserveRects()) {
1436 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001437 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001438 } else {
1439 // transformation too complex, can't do the
1440 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001441 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001442 }
1443 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001444 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001445 }
Mathias Agopianab028732010-03-16 16:41:46 -07001446 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001447
Mathias Agopianab028732010-03-16 16:41:46 -07001448 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001449 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001450 if (s.alpha==255 && !translucent &&
1451 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1452 // the opaque region is the layer's footprint
1453 opaqueRegion = visibleRegion;
1454 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455 }
1456 }
1457
Mathias Agopianab028732010-03-16 16:41:46 -07001458 // Clip the covered region to the visible region
1459 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1460
1461 // Update aboveCoveredLayers for next (lower) layer
1462 aboveCoveredLayers.orSelf(visibleRegion);
1463
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001464 // subtract the opaque region covered by the layers above us
1465 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001466
1467 // compute this layer's dirty region
1468 if (layer->contentDirty) {
1469 // we need to invalidate the whole region
1470 dirty = visibleRegion;
1471 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001472 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001473 layer->contentDirty = false;
1474 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001475 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001476 * the exposed region consists of two components:
1477 * 1) what's VISIBLE now and was COVERED before
1478 * 2) what's EXPOSED now less what was EXPOSED before
1479 *
1480 * note that (1) is conservative, we start with the whole
1481 * visible region but only keep what used to be covered by
1482 * something -- which mean it may have been exposed.
1483 *
1484 * (2) handles areas that were not covered by anything but got
1485 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001486 */
Mathias Agopianab028732010-03-16 16:41:46 -07001487 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001488 const Region oldVisibleRegion = layer->visibleRegion;
1489 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001490 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1491 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001492 }
1493 dirty.subtractSelf(aboveOpaqueLayers);
1494
1495 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001496 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001497
Mathias Agopianab028732010-03-16 16:41:46 -07001498 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001499 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001500
Jesse Halla8026d22012-09-25 13:25:04 -07001501 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001502 layer->setVisibleRegion(visibleRegion);
1503 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001504 layer->setVisibleNonTransparentRegion(
1505 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001506 }
1507
Mathias Agopian87baae12012-07-31 12:38:26 -07001508 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001509}
1510
Mathias Agopian87baae12012-07-31 12:38:26 -07001511void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1512 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001513 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001514 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1515 if (hw->getLayerStack() == layerStack) {
1516 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001517 }
1518 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001519}
1520
1521void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001522{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001523 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001524
Mathias Agopian4fec8732012-06-29 14:12:52 -07001525 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001526 const LayerVector& layers(mDrawingState.layersSortedByZ);
1527 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001528 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001529 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001530 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001531 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001532 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001533 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001534
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001535 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001536}
1537
Mathias Agopianad456f92011-01-13 17:53:01 -08001538void SurfaceFlinger::invalidateHwcGeometry()
1539{
1540 mHwWorkListDirty = true;
1541}
1542
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001543
Mathias Agopiancd60f992012-08-16 16:28:27 -07001544void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001545 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001546{
Dan Stoza71433162014-02-04 16:22:36 -08001547 // We only need to actually compose the display if:
1548 // 1) It is being handled by hardware composer, which may need this to
1549 // keep its virtual display state machine in sync, or
1550 // 2) There is work to be done (the dirty region isn't empty)
1551 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1552 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1553 return;
1554 }
1555
Mathias Agopian87baae12012-07-31 12:38:26 -07001556 Region dirtyRegion(inDirtyRegion);
1557
Mathias Agopianb8a55602009-06-26 19:06:36 -07001558 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001559 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001560
Mathias Agopian42977342012-08-05 00:40:46 -07001561 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001562 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001563 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1564 // takes a rectangle, we must make sure to update that whole
1565 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001566 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001567 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001568 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001569 // We need to redraw the rectangle that will be updated
1570 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001571 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001572 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001573 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001574 } else {
1575 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001576 dirtyRegion.set(hw->bounds());
1577 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001578 }
1579 }
1580
Alan Viverette9c5a3332013-09-12 20:04:35 -07001581 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Mathias Agopianff2ed702013-09-01 21:36:12 -07001582 doComposeSurfaces(hw, dirtyRegion);
1583 } else {
1584 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001585 mat4 colorMatrix = mColorMatrix;
1586 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001587 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001588 }
1589 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001590 doComposeSurfaces(hw, dirtyRegion);
1591 engine.endGroup();
1592 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001593
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001594 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001595 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001596
1597 // swap buffers (presentation)
1598 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001599}
1600
Mathias Agopiancd60f992012-08-16 16:28:27 -07001601void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001602{
Mathias Agopian3f844832013-08-07 21:24:32 -07001603 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001604 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001605 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001606 HWComposer::LayerListIterator cur = hwc.begin(id);
1607 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001608
Jesse Halld05a17f2013-09-30 16:49:30 -07001609 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001610 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001611 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001612 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1613 hw->getDisplayName().string());
1614 return;
1615 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001616
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001617 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001618 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001619 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001620 // when using overlays, we assume a fully transparent framebuffer
1621 // NOTE: we could reduce how much we need to clear, for instance
1622 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001623 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001624 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001625 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001626 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001627 // we start with the whole screen area
1628 const Region bounds(hw->getBounds());
1629
1630 // we remove the scissor part
1631 // we're left with the letterbox region
1632 // (common case is that letterbox ends-up being empty)
1633 const Region letterbox(bounds.subtract(hw->getScissor()));
1634
1635 // compute the area to clear
1636 Region region(hw->undefinedRegion.merge(letterbox));
1637
1638 // but limit it to the dirty region
1639 region.andSelf(dirty);
1640
Mathias Agopianb9494d52012-04-18 02:28:45 -07001641 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001642 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001643 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001644 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001645 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001646 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001647
Mathias Agopian766dc492012-10-30 18:08:06 -07001648 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1649 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001650 // scissor on the main display. It should never be needed
1651 // anyways (though in theory it could since the API allows it).
1652 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001653 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001654 if (scissor != bounds) {
1655 // scissor doesn't match the screen's dimensions, so we
1656 // need to clear everything outside of it and enable
1657 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001658
Mathias Agopianf45c5102012-10-24 16:29:17 -07001659 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001660 const uint32_t height = hw->getHeight();
1661 engine.setScissor(scissor.left, height - scissor.bottom,
1662 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001663 }
1664 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001665 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001666
Mathias Agopian85d751c2012-08-29 16:59:24 -07001667 /*
1668 * and then, render the layers targeted at the framebuffer
1669 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001670
Mathias Agopian13127d82013-03-05 17:47:11 -08001671 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001672 const size_t count = layers.size();
1673 const Transform& tr = hw->getTransform();
1674 if (cur != end) {
1675 // we're using h/w composer
1676 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001677 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001678 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001679 if (!clip.isEmpty()) {
1680 switch (cur->getCompositionType()) {
1681 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001682 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001683 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1684 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001685 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001686 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001687 // never clear the very first layer since we're
1688 // guaranteed the FB is already cleared
1689 layer->clearWithOpenGL(hw, clip);
1690 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001691 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001692 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001693 case HWC_FRAMEBUFFER: {
1694 layer->draw(hw, clip);
1695 break;
1696 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001697 case HWC_FRAMEBUFFER_TARGET: {
1698 // this should not happen as the iterator shouldn't
1699 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001700 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001701 break;
1702 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001703 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001704 }
1705 layer->setAcquireFence(hw, *cur);
1706 }
1707 } else {
1708 // we're not using h/w composer
1709 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001710 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001711 const Region clip(dirty.intersect(
1712 tr.transform(layer->visibleRegion)));
1713 if (!clip.isEmpty()) {
1714 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001715 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001716 }
1717 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001718
1719 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001720 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001721}
1722
Mathias Agopian3f844832013-08-07 21:24:32 -07001723void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001724 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001725 RenderEngine& engine(getRenderEngine());
1726 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001727}
1728
Mathias Agopianac9fa422013-02-11 16:40:36 -08001729void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1730 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001731 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001732 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001733{
Mathias Agopian96f08192010-06-02 23:28:45 -07001734 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001735 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001736
Mathias Agopian96f08192010-06-02 23:28:45 -07001737 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001738 Mutex::Autolock _l(mStateLock);
1739 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001740 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001741}
1742
Mathias Agopian3f844832013-08-07 21:24:32 -07001743status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001744 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001745 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001746 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001747 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001748 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001749 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001750 return NO_ERROR;
1751 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001752 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001753}
1754
Dan Stozac7014012014-02-14 15:03:43 -08001755uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001756 return android_atomic_release_load(&mTransactionFlags);
1757}
1758
Mathias Agopian3f844832013-08-07 21:24:32 -07001759uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001760 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1761}
1762
Mathias Agopian3f844832013-08-07 21:24:32 -07001763uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001764 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1765 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001766 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001767 }
1768 return old;
1769}
1770
Mathias Agopian8b33f032012-07-24 20:43:54 -07001771void SurfaceFlinger::setTransactionState(
1772 const Vector<ComposerState>& state,
1773 const Vector<DisplayState>& displays,
1774 uint32_t flags)
1775{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001776 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001777 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001778 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001779
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001780 if (flags & eAnimation) {
1781 // For window updates that are part of an animation we must wait for
1782 // previous animation "frames" to be handled.
1783 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001784 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001785 if (CC_UNLIKELY(err != NO_ERROR)) {
1786 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001787 // caller after a few seconds.
1788 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1789 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001790 mAnimTransactionPending = false;
1791 break;
1792 }
1793 }
1794 }
1795
Mathias Agopiane57f2922012-08-09 16:29:12 -07001796 size_t count = displays.size();
1797 for (size_t i=0 ; i<count ; i++) {
1798 const DisplayState& s(displays[i]);
1799 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001800 }
1801
Mathias Agopiane57f2922012-08-09 16:29:12 -07001802 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001803 for (size_t i=0 ; i<count ; i++) {
1804 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001805 // Here we need to check that the interface we're given is indeed
1806 // one of our own. A malicious client could give us a NULL
1807 // IInterface, or one of its own or even one of our own but a
1808 // different type. All these situations would cause us to crash.
1809 //
1810 // NOTE: it would be better to use RTTI as we could directly check
1811 // that we have a Client*. however, RTTI is disabled in Android.
1812 if (s.client != NULL) {
1813 sp<IBinder> binder = s.client->asBinder();
1814 if (binder != NULL) {
1815 String16 desc(binder->getInterfaceDescriptor());
1816 if (desc == ISurfaceComposerClient::descriptor) {
1817 sp<Client> client( static_cast<Client *>(s.client.get()) );
1818 transactionFlags |= setClientStateLocked(client, s.state);
1819 }
1820 }
1821 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001822 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001823
Mathias Agopian386aa982011-11-07 21:58:03 -08001824 if (transactionFlags) {
1825 // this triggers the transaction
1826 setTransactionFlags(transactionFlags);
1827
1828 // if this is a synchronous transaction, wait for it to take effect
1829 // before returning.
1830 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001831 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001832 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001833 if (flags & eAnimation) {
1834 mAnimTransactionPending = true;
1835 }
1836 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001837 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1838 if (CC_UNLIKELY(err != NO_ERROR)) {
1839 // just in case something goes wrong in SF, return to the
1840 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001841 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1842 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001843 break;
1844 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001845 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001846 }
1847}
1848
Mathias Agopiane57f2922012-08-09 16:29:12 -07001849uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1850{
Jesse Hall9a143922012-10-04 16:29:19 -07001851 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1852 if (dpyIdx < 0)
1853 return 0;
1854
Mathias Agopiane57f2922012-08-09 16:29:12 -07001855 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001856 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001857 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001858 const uint32_t what = s.what;
1859 if (what & DisplayState::eSurfaceChanged) {
1860 if (disp.surface->asBinder() != s.surface->asBinder()) {
1861 disp.surface = s.surface;
1862 flags |= eDisplayTransactionNeeded;
1863 }
1864 }
1865 if (what & DisplayState::eLayerStackChanged) {
1866 if (disp.layerStack != s.layerStack) {
1867 disp.layerStack = s.layerStack;
1868 flags |= eDisplayTransactionNeeded;
1869 }
1870 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001871 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001872 if (disp.orientation != s.orientation) {
1873 disp.orientation = s.orientation;
1874 flags |= eDisplayTransactionNeeded;
1875 }
1876 if (disp.frame != s.frame) {
1877 disp.frame = s.frame;
1878 flags |= eDisplayTransactionNeeded;
1879 }
1880 if (disp.viewport != s.viewport) {
1881 disp.viewport = s.viewport;
1882 flags |= eDisplayTransactionNeeded;
1883 }
1884 }
1885 }
1886 return flags;
1887}
1888
1889uint32_t SurfaceFlinger::setClientStateLocked(
1890 const sp<Client>& client,
1891 const layer_state_t& s)
1892{
1893 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001894 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001895 if (layer != 0) {
1896 const uint32_t what = s.what;
1897 if (what & layer_state_t::ePositionChanged) {
1898 if (layer->setPosition(s.x, s.y))
1899 flags |= eTraversalNeeded;
1900 }
1901 if (what & layer_state_t::eLayerChanged) {
1902 // NOTE: index needs to be calculated before we update the state
1903 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1904 if (layer->setLayer(s.z)) {
1905 mCurrentState.layersSortedByZ.removeAt(idx);
1906 mCurrentState.layersSortedByZ.add(layer);
1907 // we need traversal (state changed)
1908 // AND transaction (list changed)
1909 flags |= eTransactionNeeded|eTraversalNeeded;
1910 }
1911 }
1912 if (what & layer_state_t::eSizeChanged) {
1913 if (layer->setSize(s.w, s.h)) {
1914 flags |= eTraversalNeeded;
1915 }
1916 }
1917 if (what & layer_state_t::eAlphaChanged) {
1918 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1919 flags |= eTraversalNeeded;
1920 }
1921 if (what & layer_state_t::eMatrixChanged) {
1922 if (layer->setMatrix(s.matrix))
1923 flags |= eTraversalNeeded;
1924 }
1925 if (what & layer_state_t::eTransparentRegionChanged) {
1926 if (layer->setTransparentRegionHint(s.transparentRegion))
1927 flags |= eTraversalNeeded;
1928 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08001929 if ((what & layer_state_t::eVisibilityChanged) ||
1930 (what & layer_state_t::eOpacityChanged)) {
1931 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07001932 if (layer->setFlags(s.flags, s.mask))
1933 flags |= eTraversalNeeded;
1934 }
1935 if (what & layer_state_t::eCropChanged) {
1936 if (layer->setCrop(s.crop))
1937 flags |= eTraversalNeeded;
1938 }
1939 if (what & layer_state_t::eLayerStackChanged) {
1940 // NOTE: index needs to be calculated before we update the state
1941 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1942 if (layer->setLayerStack(s.layerStack)) {
1943 mCurrentState.layersSortedByZ.removeAt(idx);
1944 mCurrentState.layersSortedByZ.add(layer);
1945 // we need traversal (state changed)
1946 // AND transaction (list changed)
1947 flags |= eTransactionNeeded|eTraversalNeeded;
1948 }
1949 }
1950 }
1951 return flags;
1952}
1953
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001954status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001955 const String8& name,
1956 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001957 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1958 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001959{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001960 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001961 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001962 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001963 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001964 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001965 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001966
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001967 status_t result = NO_ERROR;
1968
1969 sp<Layer> layer;
1970
Mathias Agopian3165cc22012-08-08 19:42:09 -07001971 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1972 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001973 result = createNormalLayer(client,
1974 name, w, h, flags, format,
1975 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001976 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001977 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001978 result = createDimLayer(client,
1979 name, w, h, flags,
1980 handle, gbp, &layer);
1981 break;
1982 default:
1983 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001984 break;
1985 }
1986
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001987 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001988 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001989 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001990 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001991 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001992}
1993
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001994status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1995 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1996 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001997{
1998 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001999 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002000 case PIXEL_FORMAT_TRANSPARENT:
2001 case PIXEL_FORMAT_TRANSLUCENT:
2002 format = PIXEL_FORMAT_RGBA_8888;
2003 break;
2004 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002005 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002006 break;
2007 }
2008
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002009 *outLayer = new Layer(this, client, name, w, h, flags);
2010 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2011 if (err == NO_ERROR) {
2012 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002013 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002014 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002015
2016 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2017 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002018}
2019
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002020status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2021 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2022 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002023{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002024 *outLayer = new LayerDim(this, client, name, w, h, flags);
2025 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002026 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002027 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002028}
2029
Mathias Agopianac9fa422013-02-11 16:40:36 -08002030status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002031{
Mathias Agopian67106042013-03-14 19:18:13 -07002032 // called by the window manager when it wants to remove a Layer
2033 status_t err = NO_ERROR;
2034 sp<Layer> l(client->getLayerUser(handle));
2035 if (l != NULL) {
2036 err = removeLayer(l);
2037 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2038 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002039 }
2040 return err;
2041}
2042
Mathias Agopian13127d82013-03-05 17:47:11 -08002043status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002044{
Mathias Agopian67106042013-03-14 19:18:13 -07002045 // called by ~LayerCleaner() when all references to the IBinder (handle)
2046 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002047 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002048 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002049 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002050 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002051 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002052 "error removing layer=%p (%s)", l.get(), strerror(-err));
2053 }
2054 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002055}
2056
Mathias Agopianb60314a2012-04-10 22:09:54 -07002057// ---------------------------------------------------------------------------
2058
Andy McFadden13a082e2012-08-24 10:16:42 -07002059void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002060 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002061 Vector<ComposerState> state;
2062 Vector<DisplayState> displays;
2063 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002064 d.what = DisplayState::eDisplayProjectionChanged |
2065 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002066 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002067 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002068 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002069 d.frame.makeInvalid();
2070 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002071 displays.add(d);
2072 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002073 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002074
2075 const nsecs_t period =
2076 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2077 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002078}
2079
2080void SurfaceFlinger::initializeDisplays() {
2081 class MessageScreenInitialized : public MessageBase {
2082 SurfaceFlinger* flinger;
2083 public:
2084 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2085 virtual bool handler() {
2086 flinger->onInitializeDisplays();
2087 return true;
2088 }
2089 };
2090 sp<MessageBase> msg = new MessageScreenInitialized(this);
2091 postMessageAsync(msg); // we may be called from main thread, use async message
2092}
2093
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002094void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2095 int mode) {
2096 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2097 this);
2098 int32_t type = hw->getDisplayType();
2099 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002100
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002101 if (mode == currentMode) {
2102 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002103 return;
2104 }
2105
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002106 hw->setPowerMode(mode);
2107 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2108 ALOGW("Trying to set power mode for virtual display");
2109 return;
2110 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002111
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002112 if (currentMode == HWC_POWER_MODE_OFF) {
2113 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002114 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2115 // FIXME: eventthread only knows about the main display right now
2116 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002117 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002118 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002119
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002120 mVisibleRegionsDirty = true;
2121 repaintEverything();
2122 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002123 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002124 disableHardwareVsync(true); // also cancels any in-progress resync
2125
Mathias Agopiancde87a32012-09-13 14:09:01 -07002126 // FIXME: eventthread only knows about the main display right now
2127 mEventThread->onScreenReleased();
2128 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002129
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002130 getHwComposer().setPowerMode(type, mode);
2131 mVisibleRegionsDirty = true;
2132 // from this point on, SF will stop drawing on this display
2133 } else {
2134 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002135 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002136}
2137
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002138void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2139 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002140 SurfaceFlinger& mFlinger;
2141 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002142 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002143 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002144 MessageSetPowerMode(SurfaceFlinger& flinger,
2145 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2146 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002147 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002148 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002149 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002150 ALOGE("Attempt to set power mode = %d for null display %p",
2151 mDisplay.get(), mMode);
Jesse Hall9e663de2013-08-16 14:28:37 -07002152 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002153 ALOGW("Attempt to set power mode = %d for virtual display",
2154 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002155 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002156 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002157 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002158 return true;
2159 }
2160 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002161 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002162 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002163}
2164
2165// ---------------------------------------------------------------------------
2166
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002167status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2168{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002169 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002170
Mathias Agopianbd115332013-04-18 16:41:04 -07002171 IPCThreadState* ipc = IPCThreadState::self();
2172 const int pid = ipc->getCallingPid();
2173 const int uid = ipc->getCallingUid();
2174 if ((uid != AID_SHELL) &&
2175 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002176 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002177 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002178 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002179 // Try to get the main lock, but don't insist if we can't
2180 // (this would indicate SF is stuck, but we want to be able to
2181 // print something in dumpsys).
2182 int retry = 3;
2183 while (mStateLock.tryLock()<0 && --retry>=0) {
2184 usleep(1000000);
2185 }
2186 const bool locked(retry >= 0);
2187 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002188 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002189 "SurfaceFlinger appears to be unresponsive, "
2190 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002191 }
2192
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002193 bool dumpAll = true;
2194 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002195 size_t numArgs = args.size();
2196 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002197 if ((index < numArgs) &&
2198 (args[index] == String16("--list"))) {
2199 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002200 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002201 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002202 }
2203
2204 if ((index < numArgs) &&
2205 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002206 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002207 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002208 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002209 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002210
2211 if ((index < numArgs) &&
2212 (args[index] == String16("--latency-clear"))) {
2213 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002214 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002215 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002216 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002217
2218 if ((index < numArgs) &&
2219 (args[index] == String16("--dispsync"))) {
2220 index++;
2221 mPrimaryDispSync.dump(result);
2222 dumpAll = false;
2223 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002224 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002225
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002226 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002227 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002228 }
2229
Mathias Agopian9795c422009-08-26 16:36:26 -07002230 if (locked) {
2231 mStateLock.unlock();
2232 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002233 }
2234 write(fd, result.string(), result.size());
2235 return NO_ERROR;
2236}
2237
Dan Stozac7014012014-02-14 15:03:43 -08002238void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2239 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002240{
2241 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2242 const size_t count = currentLayers.size();
2243 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002244 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002245 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002246 }
2247}
2248
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002249void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002250 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002251{
2252 String8 name;
2253 if (index < args.size()) {
2254 name = String8(args[index]);
2255 index++;
2256 }
2257
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002258 const nsecs_t period =
2259 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002260 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002261
2262 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002263 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002264 } else {
2265 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2266 const size_t count = currentLayers.size();
2267 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002268 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002269 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002270 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002271 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002272 }
2273 }
2274}
2275
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002276void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002277 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002278{
2279 String8 name;
2280 if (index < args.size()) {
2281 name = String8(args[index]);
2282 index++;
2283 }
2284
2285 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2286 const size_t count = currentLayers.size();
2287 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002288 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002289 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002290 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002291 }
2292 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002293
Svetoslavd85084b2014-03-20 10:28:31 -07002294 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002295}
2296
Jamie Gennis6547ff42013-07-16 20:12:42 -07002297// This should only be called from the main thread. Otherwise it would need
2298// the lock and should use mCurrentState rather than mDrawingState.
2299void SurfaceFlinger::logFrameStats() {
2300 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2301 const size_t count = drawingLayers.size();
2302 for (size_t i=0 ; i<count ; i++) {
2303 const sp<Layer>& layer(drawingLayers[i]);
2304 layer->logFrameStats();
2305 }
2306
2307 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2308}
2309
Andy McFadden4803b742012-09-24 19:07:20 -07002310/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2311{
2312 static const char* config =
2313 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002314#ifdef HAS_CONTEXT_PRIORITY
2315 " HAS_CONTEXT_PRIORITY"
2316#endif
2317#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2318 " NEVER_DEFAULT_TO_ASYNC_MODE"
2319#endif
2320#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2321 " TARGET_DISABLE_TRIPLE_BUFFERING"
2322#endif
2323 "]";
2324 result.append(config);
2325}
2326
Mathias Agopian74d211a2013-04-22 16:55:35 +02002327void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2328 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002329{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002330 bool colorize = false;
2331 if (index < args.size()
2332 && (args[index] == String16("--color"))) {
2333 colorize = true;
2334 index++;
2335 }
2336
2337 Colorizer colorizer(colorize);
2338
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002339 // figure out if we're stuck somewhere
2340 const nsecs_t now = systemTime();
2341 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2342 const nsecs_t inTransaction(mDebugInTransaction);
2343 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2344 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2345
2346 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002347 * Dump library configuration.
2348 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002349
2350 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002351 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002352 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002353 appendSfConfigString(result);
2354 appendUiConfigString(result);
2355 appendGuiConfigString(result);
2356 result.append("\n");
2357
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002358 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002359 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002360 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002361 result.append(SyncFeatures::getInstance().toString());
2362 result.append("\n");
2363
Andy McFadden41d67d72014-04-25 16:58:34 -07002364 colorizer.bold(result);
2365 result.append("DispSync configuration: ");
2366 colorizer.reset(result);
2367 result.appendFormat("app phase %"PRId64" ns, sf phase %"PRId64" ns, "
2368 "present offset %d ns (refresh %"PRId64" ns)",
2369 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2370 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2371 result.append("\n");
2372
Andy McFadden4803b742012-09-24 19:07:20 -07002373 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002374 * Dump the visible layer list
2375 */
2376 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2377 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002378 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002379 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002380 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002381 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002382 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002383 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002384 }
2385
2386 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002387 * Dump Display state
2388 */
2389
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002390 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002391 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002392 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002393 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2394 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002395 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002396 }
2397
2398 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002399 * Dump SurfaceFlinger global state
2400 */
2401
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002402 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002403 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002404 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002405
Mathias Agopian888c8222012-08-04 21:10:38 -07002406 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002407 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002408
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002409 colorizer.bold(result);
2410 result.appendFormat("EGL implementation : %s\n",
2411 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2412 colorizer.reset(result);
2413 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002414 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002415
Mathias Agopian875d8e12013-06-07 15:35:48 -07002416 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002417
Mathias Agopian42977342012-08-05 00:40:46 -07002418 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002419 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2420 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002421 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002422 " last eglSwapBuffers() time: %f us\n"
2423 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002424 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002425 " refresh-rate : %f fps\n"
2426 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002427 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002428 " gpu_to_cpu_unsupported : %d\n"
2429 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002430 mLastSwapBufferTime/1000.0,
2431 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002432 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002433 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2434 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002435 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002436 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002437
Mathias Agopian74d211a2013-04-22 16:55:35 +02002438 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002439 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002440
Mathias Agopian74d211a2013-04-22 16:55:35 +02002441 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002442 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002443
2444 /*
2445 * VSYNC state
2446 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002447 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002448
2449 /*
2450 * Dump HWComposer state
2451 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002452 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002453 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002454 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002455 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002456 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002457 (mDebugDisableHWC || mDebugRegion || mDaltonize
2458 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002459 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002460
2461 /*
2462 * Dump gralloc state
2463 */
2464 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2465 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002466}
2467
Mathias Agopian13127d82013-03-05 17:47:11 -08002468const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002469SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002470 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002471 wp<IBinder> dpy;
2472 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2473 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2474 dpy = mDisplays.keyAt(i);
2475 break;
2476 }
2477 }
2478 if (dpy == NULL) {
2479 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2480 // Just use the primary display so we have something to return
2481 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2482 }
2483 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002484}
2485
Keun young Park63f165f2012-08-31 10:53:36 -07002486bool SurfaceFlinger::startDdmConnection()
2487{
2488 void* libddmconnection_dso =
2489 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2490 if (!libddmconnection_dso) {
2491 return false;
2492 }
2493 void (*DdmConnection_start)(const char* name);
2494 DdmConnection_start =
2495 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2496 if (!DdmConnection_start) {
2497 dlclose(libddmconnection_dso);
2498 return false;
2499 }
2500 (*DdmConnection_start)(getServiceName());
2501 return true;
2502}
2503
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002504status_t SurfaceFlinger::onTransact(
2505 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2506{
2507 switch (code) {
2508 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002509 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002510 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002511 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002512 case CLEAR_ANIMATION_FRAME_STATS:
2513 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002514 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002515 {
2516 // codes that require permission check
2517 IPCThreadState* ipc = IPCThreadState::self();
2518 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002519 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002520 if ((uid != AID_GRAPHICS) &&
2521 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002522 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002523 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2524 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002525 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002526 break;
2527 }
2528 case CAPTURE_SCREEN:
2529 {
2530 // codes that require permission check
2531 IPCThreadState* ipc = IPCThreadState::self();
2532 const int pid = ipc->getCallingPid();
2533 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002534 if ((uid != AID_GRAPHICS) &&
2535 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002536 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002537 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2538 return PERMISSION_DENIED;
2539 }
2540 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002541 }
2542 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002543
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002544 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2545 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002546 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002547 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002548 IPCThreadState* ipc = IPCThreadState::self();
2549 const int pid = ipc->getCallingPid();
2550 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002551 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002552 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002553 return PERMISSION_DENIED;
2554 }
2555 int n;
2556 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002557 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002558 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002559 return NO_ERROR;
2560 case 1002: // SHOW_UPDATES
2561 n = data.readInt32();
2562 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002563 invalidateHwcGeometry();
2564 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002565 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002566 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002567 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002568 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002569 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002570 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002571 setTransactionFlags(
2572 eTransactionNeeded|
2573 eDisplayTransactionNeeded|
2574 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002575 return NO_ERROR;
2576 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002577 case 1006:{ // send empty update
2578 signalRefresh();
2579 return NO_ERROR;
2580 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002581 case 1008: // toggle use of hw composer
2582 n = data.readInt32();
2583 mDebugDisableHWC = n ? 1 : 0;
2584 invalidateHwcGeometry();
2585 repaintEverything();
2586 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002587 case 1009: // toggle use of transform hint
2588 n = data.readInt32();
2589 mDebugDisableTransformHint = n ? 1 : 0;
2590 invalidateHwcGeometry();
2591 repaintEverything();
2592 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002593 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002594 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002595 reply->writeInt32(0);
2596 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002597 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002598 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002599 return NO_ERROR;
2600 case 1013: {
2601 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002602 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2603 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002604 return NO_ERROR;
2605 }
2606 case 1014: {
2607 // daltonize
2608 n = data.readInt32();
2609 switch (n % 10) {
2610 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2611 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2612 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2613 }
2614 if (n >= 10) {
2615 mDaltonizer.setMode(Daltonizer::correction);
2616 } else {
2617 mDaltonizer.setMode(Daltonizer::simulation);
2618 }
2619 mDaltonize = n > 0;
2620 invalidateHwcGeometry();
2621 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002622 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002623 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002624 case 1015: {
2625 // apply a color matrix
2626 n = data.readInt32();
2627 mHasColorMatrix = n ? 1 : 0;
2628 if (n) {
2629 // color matrix is sent as mat3 matrix followed by vec3
2630 // offset, then packed into a mat4 where the last row is
2631 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002632 for (size_t i = 0 ; i < 4; i++) {
2633 for (size_t j = 0; j < 4; j++) {
2634 mColorMatrix[i][j] = data.readFloat();
2635 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002636 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002637 } else {
2638 mColorMatrix = mat4();
2639 }
2640 invalidateHwcGeometry();
2641 repaintEverything();
2642 return NO_ERROR;
2643 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002644 // This is an experimental interface
2645 // Needs to be shifted to proper binder interface when we productize
2646 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07002647 n = data.readInt32();
2648 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002649 return NO_ERROR;
2650 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002651 }
2652 }
2653 return err;
2654}
2655
Mathias Agopian53331da2011-08-22 21:44:41 -07002656void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002657 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002658 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002659}
2660
Mathias Agopian59119e62010-10-11 12:37:43 -07002661// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002662// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002663// ---------------------------------------------------------------------------
2664
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002665/* The code below is here to handle b/8734824
2666 *
2667 * We create a IGraphicBufferProducer wrapper that forwards all calls
2668 * to the calling binder thread, where they are executed. This allows
2669 * the calling thread to be reused (on the other side) and not
2670 * depend on having "enough" binder threads to handle the requests.
2671 *
2672 */
2673
2674class GraphicProducerWrapper : public BBinder, public MessageHandler {
2675 sp<IGraphicBufferProducer> impl;
2676 sp<Looper> looper;
2677 status_t result;
2678 bool exitPending;
2679 bool exitRequested;
2680 mutable Barrier barrier;
2681 volatile int32_t memoryBarrier;
2682 uint32_t code;
2683 Parcel const* data;
2684 Parcel* reply;
2685
2686 enum {
2687 MSG_API_CALL,
2688 MSG_EXIT
2689 };
2690
2691 /*
2692 * this is called by our "fake" BpGraphicBufferProducer. We package the
2693 * data and reply Parcel and forward them to the calling thread.
2694 */
2695 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08002696 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002697 this->code = code;
2698 this->data = &data;
2699 this->reply = reply;
2700 android_atomic_acquire_store(0, &memoryBarrier);
2701 if (exitPending) {
2702 // if we've exited, we run the message synchronously right here
2703 handleMessage(Message(MSG_API_CALL));
2704 } else {
2705 barrier.close();
2706 looper->sendMessage(this, Message(MSG_API_CALL));
2707 barrier.wait();
2708 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08002709 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002710 }
2711
2712 /*
2713 * here we run on the binder calling thread. All we've got to do is
2714 * call the real BpGraphicBufferProducer.
2715 */
2716 virtual void handleMessage(const Message& message) {
2717 android_atomic_release_load(&memoryBarrier);
2718 if (message.what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08002719 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002720 barrier.open();
2721 } else if (message.what == MSG_EXIT) {
2722 exitRequested = true;
2723 }
2724 }
2725
2726public:
2727 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2728 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2729 exitPending(false), exitRequested(false) {
2730 }
2731
2732 status_t waitForResponse() {
2733 do {
2734 looper->pollOnce(-1);
2735 } while (!exitRequested);
2736 return result;
2737 }
2738
2739 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002740 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002741 exitPending = true;
2742 looper->sendMessage(this, Message(MSG_EXIT));
2743 }
2744};
2745
2746
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002747status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2748 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002749 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002750 uint32_t minLayerZ, uint32_t maxLayerZ,
2751 bool useIdentityTransform) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002752
2753 if (CC_UNLIKELY(display == 0))
2754 return BAD_VALUE;
2755
2756 if (CC_UNLIKELY(producer == 0))
2757 return BAD_VALUE;
2758
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002759 // if we have secure windows on this display, never allow the screen capture
2760 // unless the producer interface is local (i.e.: we can take a screenshot for
2761 // ourselves).
2762 if (!producer->asBinder()->localBinder()) {
2763 Mutex::Autolock _l(mStateLock);
2764 sp<const DisplayDevice> hw(getDisplayDevice(display));
2765 if (hw->getSecureLayerVisible()) {
2766 ALOGW("FB is protected: PERMISSION_DENIED");
2767 return PERMISSION_DENIED;
2768 }
2769 }
2770
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002771 class MessageCaptureScreen : public MessageBase {
2772 SurfaceFlinger* flinger;
2773 sp<IBinder> display;
2774 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07002775 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002776 uint32_t reqWidth, reqHeight;
2777 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08002778 bool useIdentityTransform;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002779 status_t result;
2780 public:
2781 MessageCaptureScreen(SurfaceFlinger* flinger,
2782 const sp<IBinder>& display,
2783 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002784 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002785 uint32_t minLayerZ, uint32_t maxLayerZ,
2786 bool useIdentityTransform)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002787 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07002788 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002789 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08002790 useIdentityTransform(useIdentityTransform),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002791 result(PERMISSION_DENIED)
2792 {
2793 }
2794 status_t getResult() const {
2795 return result;
2796 }
2797 virtual bool handler() {
2798 Mutex::Autolock _l(flinger->mStateLock);
2799 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08002800 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07002801 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Dan Stozac7014012014-02-14 15:03:43 -08002802 useIdentityTransform);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002803 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002804 return true;
2805 }
2806 };
2807
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002808 // make sure to process transactions before screenshots -- a transaction
2809 // might already be pending but scheduled for VSYNC; this guarantees we
2810 // will handle it before the screenshot. When VSYNC finally arrives
2811 // the scheduled transaction will be a no-op. If no transactions are
2812 // scheduled at this time, this will end-up being a no-op as well.
2813 mEventQueue.invalidateTransactionNow();
2814
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002815 // this creates a "fake" BBinder which will serve as a "fake" remote
2816 // binder to receive the marshaled calls and forward them to the
2817 // real remote (a BpGraphicBufferProducer)
2818 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2819
2820 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2821 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002822 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002823 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07002824 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
2825 useIdentityTransform);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002826
2827 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002828 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002829 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002830 }
2831 return res;
2832}
2833
Mathias Agopian180f10d2013-04-10 22:55:41 -07002834
2835void SurfaceFlinger::renderScreenImplLocked(
2836 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07002837 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07002838 uint32_t minLayerZ, uint32_t maxLayerZ,
Dan Stozac7014012014-02-14 15:03:43 -08002839 bool yswap, bool useIdentityTransform)
Mathias Agopian180f10d2013-04-10 22:55:41 -07002840{
2841 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002842 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002843
2844 // get screen geometry
2845 const uint32_t hw_w = hw->getWidth();
2846 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002847 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2848
Dan Stozac1879002014-05-22 15:59:05 -07002849 // if a default or invalid sourceCrop is passed in, set reasonable values
2850 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
2851 !sourceCrop.isValid()) {
2852 sourceCrop.setLeftTop(Point(0, 0));
2853 sourceCrop.setRightBottom(Point(hw_w, hw_h));
2854 }
2855
2856 // ensure that sourceCrop is inside screen
2857 if (sourceCrop.left < 0) {
2858 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
2859 }
2860 if (sourceCrop.right >= hw_w) {
2861 ALOGE("Invalid crop rect: r = %d (>= %d)", sourceCrop.right, hw_w);
2862 }
2863 if (sourceCrop.top < 0) {
2864 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
2865 }
2866 if (sourceCrop.bottom >= hw_h) {
2867 ALOGE("Invalid crop rect: b = %d (>= %d)", sourceCrop.bottom, hw_h);
2868 }
2869
Mathias Agopian180f10d2013-04-10 22:55:41 -07002870 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002871 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002872
2873 // set-up our viewport
Dan Stozac1879002014-05-22 15:59:05 -07002874 engine.setViewportAndProjection(reqWidth, reqHeight, sourceCrop, hw_h, yswap);
Mathias Agopian3f844832013-08-07 21:24:32 -07002875 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002876
2877 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002878 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002879
2880 const LayerVector& layers( mDrawingState.layersSortedByZ );
2881 const size_t count = layers.size();
2882 for (size_t i=0 ; i<count ; ++i) {
2883 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002884 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002885 if (state.layerStack == hw->getLayerStack()) {
2886 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2887 if (layer->isVisible()) {
2888 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08002889 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002890 if (filtering) layer->setFiltering(false);
2891 }
2892 }
2893 }
2894 }
2895
2896 // compositionComplete is needed for older driver
2897 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002898 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002899}
2900
2901
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002902status_t SurfaceFlinger::captureScreenImplLocked(
2903 const sp<const DisplayDevice>& hw,
2904 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002905 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002906 uint32_t minLayerZ, uint32_t maxLayerZ,
2907 bool useIdentityTransform)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002908{
2909 ATRACE_CALL();
2910
Mathias Agopian180f10d2013-04-10 22:55:41 -07002911 // get screen geometry
2912 const uint32_t hw_w = hw->getWidth();
2913 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002914
Mathias Agopian180f10d2013-04-10 22:55:41 -07002915 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2916 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2917 reqWidth, reqHeight, hw_w, hw_h);
2918 return BAD_VALUE;
2919 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002920
Mathias Agopian180f10d2013-04-10 22:55:41 -07002921 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2922 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2923
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002924 // create a surface (because we're a producer, and we need to
2925 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07002926 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002927 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002928
2929 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002930 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002931 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2932 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002933
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002934 int err = 0;
2935 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07002936 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002937 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2938 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002939
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002940 if (err == NO_ERROR) {
2941 ANativeWindowBuffer* buffer;
2942 /* TODO: Once we have the sync framework everywhere this can use
2943 * server-side waits on the fence that dequeueBuffer returns.
2944 */
2945 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2946 if (result == NO_ERROR) {
2947 // create an EGLImage from the buffer so we can later
2948 // turn it into a texture
2949 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2950 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2951 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002952 // this binds the given EGLImage as a framebuffer for the
2953 // duration of this scope.
2954 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2955 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002956 // this will in fact render into our dequeued buffer
2957 // via an FBO, which means we didn't have to create
2958 // an EGLSurface and therefore we're not
2959 // dependent on the context's EGLConfig.
Dan Stozac1879002014-05-22 15:59:05 -07002960 renderScreenImplLocked(hw, sourceCrop, reqWidth, reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002961 minLayerZ, maxLayerZ, true, useIdentityTransform);
Mathias Agopiand5556842013-09-19 17:08:37 -07002962
Andy McFadden2d8d1202013-10-09 16:38:02 -07002963 // Create a sync point and wait on it, so we know the buffer is
2964 // ready before we pass it along. We can't trivially call glFlush(),
2965 // so we use a wait flag instead.
2966 // TODO: pass a sync fd to queueBuffer() and let the consumer wait.
2967 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
2968 if (sync != EGL_NO_SYNC_KHR) {
2969 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
2970 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
2971 EGLint eglErr = eglGetError();
2972 eglDestroySyncKHR(mEGLDisplay, sync);
2973 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
2974 ALOGW("captureScreen: fence wait timed out");
2975 } else {
2976 ALOGW_IF(eglErr != EGL_SUCCESS,
2977 "captureScreen: error waiting on EGL fence: %#x", eglErr);
2978 }
2979 } else {
2980 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
2981 // not fatal
2982 }
2983
Mathias Agopiand5556842013-09-19 17:08:37 -07002984 if (DEBUG_SCREENSHOTS) {
2985 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
2986 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
2987 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
2988 hw, minLayerZ, maxLayerZ);
2989 delete [] pixels;
2990 }
2991
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002992 } else {
2993 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2994 result = INVALID_OPERATION;
2995 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002996 // destroy our image
2997 eglDestroyImageKHR(mEGLDisplay, image);
2998 } else {
2999 result = BAD_VALUE;
3000 }
3001 window->queueBuffer(window, buffer, -1);
3002 }
3003 } else {
3004 result = BAD_VALUE;
3005 }
3006 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3007 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003008
Mathias Agopian74c40c02010-09-29 13:02:36 -07003009 return result;
3010}
3011
Mathias Agopiand5556842013-09-19 17:08:37 -07003012void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3013 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003014 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003015 for (size_t y=0 ; y<h ; y++) {
3016 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3017 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003018 if (p[x] != 0xFF000000) return;
3019 }
3020 }
3021 ALOGE("*** we just took a black screenshot ***\n"
3022 "requested minz=%d, maxz=%d, layerStack=%d",
3023 minLayerZ, maxLayerZ, hw->getLayerStack());
3024 const LayerVector& layers( mDrawingState.layersSortedByZ );
3025 const size_t count = layers.size();
3026 for (size_t i=0 ; i<count ; ++i) {
3027 const sp<Layer>& layer(layers[i]);
3028 const Layer::State& state(layer->getDrawingState());
3029 const bool visible = (state.layerStack == hw->getLayerStack())
3030 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3031 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003032 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003033 visible ? '+' : '-',
3034 i, layer->getName().string(), state.layerStack, state.z,
3035 layer->isVisible(), state.flags, state.alpha);
3036 }
3037 }
3038}
3039
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003040// ---------------------------------------------------------------------------
3041
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003042SurfaceFlinger::LayerVector::LayerVector() {
3043}
3044
3045SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003046 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003047}
3048
3049int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3050 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003051{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003052 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003053 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3054 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003055
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003056 uint32_t ls = l->getCurrentState().layerStack;
3057 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003058 if (ls != rs)
3059 return ls - rs;
3060
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003061 uint32_t lz = l->getCurrentState().z;
3062 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003063 if (lz != rz)
3064 return lz - rz;
3065
3066 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003067}
3068
3069// ---------------------------------------------------------------------------
3070
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003071SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
3072 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003073}
3074
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003075SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08003076 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003077 viewport.makeInvalid();
3078 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003079}
3080
3081// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003082
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003083}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003084
3085
3086#if defined(__gl_h_)
3087#error "don't include gl/gl.h in this file"
3088#endif
3089
3090#if defined(__gl2_h_)
3091#error "don't include gl2/gl2.h in this file"
3092#endif