blob: 4ecdc8b6ad1c67f5a174980982ec4f37bd6b9aa5 [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);
Andy McFadden91b2ca82014-06-13 14:04:23 -0700579 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
580
581 // This is how far in advance a buffer must be queued for
582 // presentation at a given time. If you want a buffer to appear
583 // on the screen at time N, you must submit the buffer before
584 // (N - presentationDeadline).
585 //
586 // Normally it's one full refresh period (to give SF a chance to
587 // latch the buffer), but this can be reduced by configuring a
588 // DispSync offset. Any additional delays introduced by the hardware
589 // composer or panel must be accounted for here.
590 //
591 // We add an additional 1ms to allow for processing time and
592 // differences between the ideal and actual refresh rate.
593 info.presentationDeadline =
594 hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700595
596 // All non-virtual displays are currently considered secure.
597 info.secure = true;
598
599 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700600 }
601
Dan Stoza7f7da322014-05-02 15:26:25 -0700602 return NO_ERROR;
603}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700604
Andy McFadden91b2ca82014-06-13 14:04:23 -0700605int SurfaceFlinger::getActiveConfig(const sp<IBinder>&) {
Dan Stoza7f7da322014-05-02 15:26:25 -0700606 return 0;
607}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700608
Andy McFadden91b2ca82014-06-13 14:04:23 -0700609status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>&, int) {
Mathias Agopian888c8222012-08-04 21:10:38 -0700610 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700611}
612
Svetoslavd85084b2014-03-20 10:28:31 -0700613status_t SurfaceFlinger::clearAnimationFrameStats() {
614 Mutex::Autolock _l(mStateLock);
615 mAnimFrameTracker.clearStats();
616 return NO_ERROR;
617}
618
619status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
620 Mutex::Autolock _l(mStateLock);
621 mAnimFrameTracker.getStats(outStats);
622 return NO_ERROR;
623}
624
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800625// ----------------------------------------------------------------------------
626
627sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800628 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700629}
630
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800631// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800632
633void SurfaceFlinger::waitForEvent() {
634 mEventQueue.waitMessage();
635}
636
637void SurfaceFlinger::signalTransaction() {
638 mEventQueue.invalidate();
639}
640
641void SurfaceFlinger::signalLayerUpdate() {
642 mEventQueue.invalidate();
643}
644
645void SurfaceFlinger::signalRefresh() {
646 mEventQueue.refresh();
647}
648
649status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800650 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800651 return mEventQueue.postMessage(msg, reltime);
652}
653
654status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800655 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800656 status_t res = mEventQueue.postMessage(msg, reltime);
657 if (res == NO_ERROR) {
658 msg->wait();
659 }
660 return res;
661}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800662
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700663void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700664 do {
665 waitForEvent();
666 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800667}
668
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700669void SurfaceFlinger::enableHardwareVsync() {
670 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700671 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700672 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700673 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
674 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700675 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700676 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700677}
678
Jesse Hall948fe0c2013-10-14 12:56:09 -0700679void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700680 Mutex::Autolock _l(mHWVsyncLock);
681
Jesse Hall948fe0c2013-10-14 12:56:09 -0700682 if (makeAvailable) {
683 mHWVsyncAvailable = true;
684 } else if (!mHWVsyncAvailable) {
685 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
686 return;
687 }
688
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700689 const nsecs_t period =
690 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
691
692 mPrimaryDispSync.reset();
693 mPrimaryDispSync.setPeriod(period);
694
695 if (!mPrimaryHWVsyncEnabled) {
696 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700697 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
698 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700699 mPrimaryHWVsyncEnabled = true;
700 }
701}
702
Jesse Hall948fe0c2013-10-14 12:56:09 -0700703void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700704 Mutex::Autolock _l(mHWVsyncLock);
705 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700706 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
707 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700708 mPrimaryDispSync.endResync();
709 mPrimaryHWVsyncEnabled = false;
710 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700711 if (makeUnavailable) {
712 mHWVsyncAvailable = false;
713 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700714}
715
716void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700717 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700718
Jamie Gennisd1700752013-10-14 12:22:52 -0700719 { // Scope for the lock
720 Mutex::Autolock _l(mHWVsyncLock);
721 if (type == 0 && mPrimaryHWVsyncEnabled) {
722 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700723 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700724 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700725
726 if (needsHwVsync) {
727 enableHardwareVsync();
728 } else {
729 disableHardwareVsync(false);
730 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700731}
732
733void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
734 if (mEventThread == NULL) {
735 // This is a temporary workaround for b/7145521. A non-null pointer
736 // does not mean EventThread has finished initializing, so this
737 // is not a correct fix.
738 ALOGW("WARNING: EventThread not started, ignoring hotplug");
739 return;
740 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700741
Jesse Hall9e663de2013-08-16 14:28:37 -0700742 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700743 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800744 if (connected) {
745 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700746 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800747 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
748 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700749 }
750 setTransactionFlags(eDisplayTransactionNeeded);
751
Andy McFadden9e9689c2012-10-10 18:17:51 -0700752 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700753 }
Mathias Agopian86303202012-07-24 22:46:10 -0700754}
755
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700756void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700757 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700758 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700759}
760
Mathias Agopian4fec8732012-06-29 14:12:52 -0700761void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800762 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800763 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700764 case MessageQueue::TRANSACTION:
765 handleMessageTransaction();
766 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700767 case MessageQueue::INVALIDATE:
768 handleMessageTransaction();
769 handleMessageInvalidate();
770 signalRefresh();
771 break;
772 case MessageQueue::REFRESH:
773 handleMessageRefresh();
774 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800775 }
776}
777
Mathias Agopian4fec8732012-06-29 14:12:52 -0700778void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700779 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700780 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700781 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700782 }
783}
784
785void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700786 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700787 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700788}
789
790void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700791 ATRACE_CALL();
792 preComposition();
793 rebuildLayerStacks();
794 setUpHWComposer();
795 doDebugFlashRegions();
796 doComposition();
797 postComposition();
798}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700799
Mathias Agopiancd60f992012-08-16 16:28:27 -0700800void SurfaceFlinger::doDebugFlashRegions()
801{
802 // is debugging enabled
803 if (CC_LIKELY(!mDebugRegion))
804 return;
805
806 const bool repaintEverything = mRepaintEverything;
807 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
808 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700809 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700810 // transform the dirty region into this screen's coordinate space
811 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
812 if (!dirtyRegion.isEmpty()) {
813 // redraw the whole screen
814 doComposeSurfaces(hw, Region(hw->bounds()));
815
816 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700817 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700818 RenderEngine& engine(getRenderEngine());
819 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
820
Mathias Agopiancd60f992012-08-16 16:28:27 -0700821 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700822 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700823 }
824 }
825 }
826
827 postFramebuffer();
828
829 if (mDebugRegion > 1) {
830 usleep(mDebugRegion * 1000);
831 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700832
833 HWComposer& hwc(getHwComposer());
834 if (hwc.initCheck() == NO_ERROR) {
835 status_t err = hwc.prepare();
836 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
837 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700838}
839
840void SurfaceFlinger::preComposition()
841{
842 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700843 const LayerVector& layers(mDrawingState.layersSortedByZ);
844 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700845 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700846 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700847 needExtraInvalidate = true;
848 }
849 }
850 if (needExtraInvalidate) {
851 signalLayerUpdate();
852 }
853}
854
855void SurfaceFlinger::postComposition()
856{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700857 const LayerVector& layers(mDrawingState.layersSortedByZ);
858 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700859 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700860 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700861 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800862
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700863 const HWComposer& hwc = getHwComposer();
864 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
865
866 if (presentFence->isValid()) {
867 if (mPrimaryDispSync.addPresentFence(presentFence)) {
868 enableHardwareVsync();
869 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700870 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700871 }
872 }
873
Andy McFadden5167ec62014-05-22 13:08:43 -0700874 if (kIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700875 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700876 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700877 enableHardwareVsync();
878 }
879 }
880
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800881 if (mAnimCompositionPending) {
882 mAnimCompositionPending = false;
883
Jesse Halla9a1b002013-02-27 16:39:25 -0800884 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800885 mAnimFrameTracker.setActualPresentFence(presentFence);
886 } else {
887 // The HWC doesn't support present fences, so use the refresh
888 // timestamp instead.
889 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
890 mAnimFrameTracker.setActualPresentTime(presentTime);
891 }
892 mAnimFrameTracker.advanceFrame();
893 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700894}
895
896void SurfaceFlinger::rebuildLayerStacks() {
897 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700898 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700899 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700900 mVisibleRegionsDirty = false;
901 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700902
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700903 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700904 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700905 Region opaqueRegion;
906 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800907 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700908 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700909 const Transform& tr(hw->getTransform());
910 const Rect bounds(hw->getBounds());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700911 if (hw->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700912 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700913 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700914
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700915 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700916 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700917 const sp<Layer>& layer(layers[i]);
918 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700919 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700920 Region drawRegion(tr.transform(
921 layer->visibleNonTransparentRegion));
922 drawRegion.andSelf(bounds);
923 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700924 layersSortedByZ.add(layer);
925 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700926 }
927 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700928 }
Mathias Agopian42977342012-08-05 00:40:46 -0700929 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700930 hw->undefinedRegion.set(bounds);
931 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
932 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700933 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700934 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700935}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700936
Mathias Agopiancd60f992012-08-16 16:28:27 -0700937void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700938 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Dan Stoza71433162014-02-04 16:22:36 -0800939 bool mustRecompose =
940 !(mDisplays[dpy]->getDirtyRegion(false).isEmpty());
941 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700942 }
943
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700944 HWComposer& hwc(getHwComposer());
945 if (hwc.initCheck() == NO_ERROR) {
946 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700947 if (CC_UNLIKELY(mHwWorkListDirty)) {
948 mHwWorkListDirty = false;
949 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
950 sp<const DisplayDevice> hw(mDisplays[dpy]);
951 const int32_t id = hw->getHwcDisplayId();
952 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800953 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700954 hw->getVisibleLayersSortedByZ());
955 const size_t count = currentLayers.size();
956 if (hwc.createWorkList(id, count) == NO_ERROR) {
957 HWComposer::LayerListIterator cur = hwc.begin(id);
958 const HWComposer::LayerListIterator end = hwc.end(id);
959 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800960 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700961 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -0700962 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -0700963 cur->setSkip(true);
964 }
965 }
966 }
967 }
968 }
969 }
970
971 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700972 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700973 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700974 const int32_t id = hw->getHwcDisplayId();
975 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800976 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700977 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700978 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700979 HWComposer::LayerListIterator cur = hwc.begin(id);
980 const HWComposer::LayerListIterator end = hwc.end(id);
981 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
982 /*
983 * update the per-frame h/w composer data for each layer
984 * and build the transparent region of the FB
985 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800986 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700987 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700988 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700989 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700990 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700991
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700992 status_t err = hwc.prepare();
993 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700994
995 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
996 sp<const DisplayDevice> hw(mDisplays[dpy]);
997 hw->prepareFrame(hwc);
998 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700999 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001000}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001001
Mathias Agopiancd60f992012-08-16 16:28:27 -07001002void SurfaceFlinger::doComposition() {
1003 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001004 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001005 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001006 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001007 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001008 // transform the dirty region into this screen's coordinate space
1009 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001010
1011 // repaint the framebuffer (if needed)
1012 doDisplayComposition(hw, dirtyRegion);
1013
Mathias Agopiancd60f992012-08-16 16:28:27 -07001014 hw->dirtyRegion.clear();
1015 hw->flip(hw->swapRegion);
1016 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001017 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001018 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001019 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001020 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001021 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001022}
1023
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024void SurfaceFlinger::postFramebuffer()
1025{
Mathias Agopian841cde52012-03-01 15:44:37 -08001026 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001027
Mathias Agopiana44b0412011-10-16 18:46:35 -07001028 const nsecs_t now = systemTime();
1029 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001030
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001031 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001032 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001033 if (!hwc.supportsFramebufferTarget()) {
1034 // EGL spec says:
1035 // "surface must be bound to the calling thread's current context,
1036 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001037 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001038 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001039 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001040 }
1041
Mathias Agopian6da15f42013-09-25 20:40:07 -07001042 // make the default display current because the VirtualDisplayDevice code cannot
1043 // deal with dequeueBuffer() being called outside of the composition loop; however
1044 // the code below can call glFlush() which is allowed (and does in some case) call
1045 // dequeueBuffer().
1046 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1047
Mathias Agopian92a979a2012-08-02 18:32:23 -07001048 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001049 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001050 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001051 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001052 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001053 int32_t id = hw->getHwcDisplayId();
1054 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001055 HWComposer::LayerListIterator cur = hwc.begin(id);
1056 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001057 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001058 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001059 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001060 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001061 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001062 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001063 }
Jesse Hallef194142012-06-14 14:45:17 -07001064 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001065 }
1066
Mathias Agopiana44b0412011-10-16 18:46:35 -07001067 mLastSwapBufferTime = systemTime() - now;
1068 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001069
1070 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1071 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1072 logFrameStats();
1073 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001074}
1075
Mathias Agopian87baae12012-07-31 12:38:26 -07001076void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001077{
Mathias Agopian841cde52012-03-01 15:44:37 -08001078 ATRACE_CALL();
1079
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001080 // here we keep a copy of the drawing state (that is the state that's
1081 // going to be overwritten by handleTransactionLocked()) outside of
1082 // mStateLock so that the side-effects of the State assignment
1083 // don't happen with mStateLock held (which can cause deadlocks).
1084 State drawingState(mDrawingState);
1085
Mathias Agopianca4d3602011-05-19 15:38:14 -07001086 Mutex::Autolock _l(mStateLock);
1087 const nsecs_t now = systemTime();
1088 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001089
Mathias Agopianca4d3602011-05-19 15:38:14 -07001090 // Here we're guaranteed that some transaction flags are set
1091 // so we can call handleTransactionLocked() unconditionally.
1092 // We call getTransactionFlags(), which will also clear the flags,
1093 // with mStateLock held to guarantee that mCurrentState won't change
1094 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001095
Mathias Agopiane57f2922012-08-09 16:29:12 -07001096 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001097 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001098
Mathias Agopianca4d3602011-05-19 15:38:14 -07001099 mLastTransactionTime = systemTime() - now;
1100 mDebugInTransaction = 0;
1101 invalidateHwcGeometry();
1102 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001103}
1104
Mathias Agopian87baae12012-07-31 12:38:26 -07001105void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001106{
1107 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108 const size_t count = currentLayers.size();
1109
1110 /*
1111 * Traversal of the children
1112 * (perform the transaction for each of them if needed)
1113 */
1114
Mathias Agopian3559b072012-08-15 13:46:03 -07001115 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001117 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001118 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1119 if (!trFlags) continue;
1120
1121 const uint32_t flags = layer->doTransaction(0);
1122 if (flags & Layer::eVisibleRegion)
1123 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001124 }
1125 }
1126
1127 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001128 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129 */
1130
Mathias Agopiane57f2922012-08-09 16:29:12 -07001131 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001132 // here we take advantage of Vector's copy-on-write semantics to
1133 // improve performance by skipping the transaction entirely when
1134 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001135 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1136 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001137 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001138 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001139 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001140 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001141
1142 // find the displays that were removed
1143 // (ie: in drawing state but not in current state)
1144 // also handle displays that changed
1145 // (ie: displays that are in both lists)
1146 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001147 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1148 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001149 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001150 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001151 // Call makeCurrent() on the primary display so we can
1152 // be sure that nothing associated with this display
1153 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001154 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001155 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001156 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1157 if (hw != NULL)
1158 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001159 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001160 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001161 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001162 } else {
1163 ALOGW("trying to remove the main display");
1164 }
1165 } else {
1166 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001167 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001168 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001169 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001170 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001171 // recreating the DisplayDevice, so we just remove it
1172 // from the drawing state, so that it get re-added
1173 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001174 sp<DisplayDevice> hw(getDisplayDevice(display));
1175 if (hw != NULL)
1176 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001177 mDisplays.removeItem(display);
1178 mDrawingState.displays.removeItemsAt(i);
1179 dc--; i--;
1180 // at this point we must loop to the next item
1181 continue;
1182 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001183
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001184 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001185 if (disp != NULL) {
1186 if (state.layerStack != draw[i].layerStack) {
1187 disp->setLayerStack(state.layerStack);
1188 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001189 if ((state.orientation != draw[i].orientation)
1190 || (state.viewport != draw[i].viewport)
1191 || (state.frame != draw[i].frame))
1192 {
1193 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001194 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001195 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001196 }
1197 }
1198 }
1199
1200 // find displays that were added
1201 // (ie: in current state but not in drawing state)
1202 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001203 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001204 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001205
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001206 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001207 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001208 sp<IGraphicBufferProducer> bqProducer;
1209 sp<IGraphicBufferConsumer> bqConsumer;
1210 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1211 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001212
Jesse Hall02d86562013-03-25 14:43:23 -07001213 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001214 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001215 // Virtual displays without a surface are dormant:
1216 // they have external state (layer stack, projection,
1217 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001218 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001219
Jesse Hall02d86562013-03-25 14:43:23 -07001220 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001221 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
Dan Stozab9b08832014-03-13 11:55:57 -07001222 *mHwc, hwcDisplayId, state.surface,
1223 bqProducer, bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001224
1225 dispSurface = vds;
1226 if (hwcDisplayId >= 0) {
1227 producer = vds;
1228 } else {
1229 // There won't be any interaction with HWC for this virtual display,
1230 // so the GLES driver can pass buffers directly to the sink.
1231 producer = state.surface;
1232 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001233 }
1234 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001235 ALOGE_IF(state.surface!=NULL,
1236 "adding a supported display, but rendering "
1237 "surface is provided (%p), ignoring it",
1238 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001239 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001240 // for supported (by hwc) displays we provide our
1241 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001242 dispSurface = new FramebufferSurface(*mHwc, state.type,
1243 bqConsumer);
1244 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001245 }
1246
1247 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001248 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001249 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001250 state.type, hwcDisplayId,
1251 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001252 display, dispSurface, producer,
1253 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001254 hw->setLayerStack(state.layerStack);
1255 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001256 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001257 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001258 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001259 if (state.isVirtualDisplay()) {
1260 if (hwcDisplayId >= 0) {
1261 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1262 hw->getWidth(), hw->getHeight(),
1263 hw->getFormat());
1264 }
1265 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001266 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001267 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001268 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001269 }
1270 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001271 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001272 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001273
Mathias Agopian84300952012-11-21 16:02:13 -08001274 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1275 // The transform hint might have changed for some layers
1276 // (either because a display has changed, or because a layer
1277 // as changed).
1278 //
1279 // Walk through all the layers in currentLayers,
1280 // and update their transform hint.
1281 //
1282 // If a layer is visible only on a single display, then that
1283 // display is used to calculate the hint, otherwise we use the
1284 // default display.
1285 //
1286 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1287 // the hint is set before we acquire a buffer from the surface texture.
1288 //
1289 // NOTE: layer transactions have taken place already, so we use their
1290 // drawing state. However, SurfaceFlinger's own transaction has not
1291 // happened yet, so we must use the current state layer list
1292 // (soon to become the drawing state list).
1293 //
1294 sp<const DisplayDevice> disp;
1295 uint32_t currentlayerStack = 0;
1296 for (size_t i=0; i<count; i++) {
1297 // NOTE: we rely on the fact that layers are sorted by
1298 // layerStack first (so we don't have to traverse the list
1299 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001300 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001301 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001302 if (i==0 || currentlayerStack != layerStack) {
1303 currentlayerStack = layerStack;
1304 // figure out if this layerstack is mirrored
1305 // (more than one display) if so, pick the default display,
1306 // if not, pick the only display it's on.
1307 disp.clear();
1308 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1309 sp<const DisplayDevice> hw(mDisplays[dpy]);
1310 if (hw->getLayerStack() == currentlayerStack) {
1311 if (disp == NULL) {
1312 disp = hw;
1313 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001314 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001315 break;
1316 }
1317 }
1318 }
1319 }
Chet Haase91d25932013-04-11 15:24:55 -07001320 if (disp == NULL) {
1321 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1322 // redraw after transform hint changes. See bug 8508397.
1323
1324 // could be null when this layer is using a layerStack
1325 // that is not visible on any display. Also can occur at
1326 // screen off/on times.
1327 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001328 }
Chet Haase91d25932013-04-11 15:24:55 -07001329 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001330 }
1331 }
1332
1333
Mathias Agopian3559b072012-08-15 13:46:03 -07001334 /*
1335 * Perform our own transaction if needed
1336 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001337
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001338 const LayerVector& layers(mDrawingState.layersSortedByZ);
1339 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001340 // layers have been added
1341 mVisibleRegionsDirty = true;
1342 }
1343
1344 // some layers might have been removed, so
1345 // we need to update the regions they're exposing.
1346 if (mLayersRemoved) {
1347 mLayersRemoved = false;
1348 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001349 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001350 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001351 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001352 if (currentLayers.indexOf(layer) < 0) {
1353 // this layer is not visible anymore
1354 // TODO: we could traverse the tree from front to back and
1355 // compute the actual visible region
1356 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001357 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001358 Region visibleReg = s.transform.transform(
1359 Region(Rect(s.active.w, s.active.h)));
1360 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001361 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001362 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001363 }
1364
1365 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001366}
1367
1368void SurfaceFlinger::commitTransaction()
1369{
1370 if (!mLayersPendingRemoval.isEmpty()) {
1371 // Notify removed layers now that they can't be drawn from
1372 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1373 mLayersPendingRemoval[i]->onRemoved();
1374 }
1375 mLayersPendingRemoval.clear();
1376 }
1377
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001378 // If this transaction is part of a window animation then the next frame
1379 // we composite should be considered an animation as well.
1380 mAnimCompositionPending = mAnimTransactionPending;
1381
Mathias Agopian4fec8732012-06-29 14:12:52 -07001382 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001383 mTransactionPending = false;
1384 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001385 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386}
1387
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001388void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001389 const LayerVector& currentLayers, uint32_t layerStack,
1390 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001391{
Mathias Agopian841cde52012-03-01 15:44:37 -08001392 ATRACE_CALL();
1393
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001394 Region aboveOpaqueLayers;
1395 Region aboveCoveredLayers;
1396 Region dirty;
1397
Mathias Agopian87baae12012-07-31 12:38:26 -07001398 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001399
1400 size_t i = currentLayers.size();
1401 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001402 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001403
1404 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001405 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406
Jesse Hall01e29052013-02-19 16:13:35 -08001407 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001408 if (s.layerStack != layerStack)
1409 continue;
1410
Mathias Agopianab028732010-03-16 16:41:46 -07001411 /*
1412 * opaqueRegion: area of a surface that is fully opaque.
1413 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001415
1416 /*
1417 * visibleRegion: area of a surface that is visible on screen
1418 * and not fully transparent. This is essentially the layer's
1419 * footprint minus the opaque regions above it.
1420 * Areas covered by a translucent surface are considered visible.
1421 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001423
1424 /*
1425 * coveredRegion: area of a surface that is covered by all
1426 * visible regions above it (which includes the translucent areas).
1427 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001428 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001429
Jesse Halla8026d22012-09-25 13:25:04 -07001430 /*
1431 * transparentRegion: area of a surface that is hinted to be completely
1432 * transparent. This is only used to tell when the layer has no visible
1433 * non-transparent regions and can be removed from the layer list. It
1434 * does not affect the visibleRegion of this layer or any layers
1435 * beneath it. The hint may not be correct if apps don't respect the
1436 * SurfaceView restrictions (which, sadly, some don't).
1437 */
1438 Region transparentRegion;
1439
Mathias Agopianab028732010-03-16 16:41:46 -07001440
1441 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001442 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001443 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001444 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001445 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001446 if (!visibleRegion.isEmpty()) {
1447 // Remove the transparent area from the visible region
1448 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001449 const Transform tr(s.transform);
1450 if (tr.transformed()) {
1451 if (tr.preserveRects()) {
1452 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001453 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001454 } else {
1455 // transformation too complex, can't do the
1456 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001457 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001458 }
1459 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001460 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001461 }
Mathias Agopianab028732010-03-16 16:41:46 -07001462 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463
Mathias Agopianab028732010-03-16 16:41:46 -07001464 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001465 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001466 if (s.alpha==255 && !translucent &&
1467 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1468 // the opaque region is the layer's footprint
1469 opaqueRegion = visibleRegion;
1470 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001471 }
1472 }
1473
Mathias Agopianab028732010-03-16 16:41:46 -07001474 // Clip the covered region to the visible region
1475 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1476
1477 // Update aboveCoveredLayers for next (lower) layer
1478 aboveCoveredLayers.orSelf(visibleRegion);
1479
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480 // subtract the opaque region covered by the layers above us
1481 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001482
1483 // compute this layer's dirty region
1484 if (layer->contentDirty) {
1485 // we need to invalidate the whole region
1486 dirty = visibleRegion;
1487 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001488 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489 layer->contentDirty = false;
1490 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001491 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001492 * the exposed region consists of two components:
1493 * 1) what's VISIBLE now and was COVERED before
1494 * 2) what's EXPOSED now less what was EXPOSED before
1495 *
1496 * note that (1) is conservative, we start with the whole
1497 * visible region but only keep what used to be covered by
1498 * something -- which mean it may have been exposed.
1499 *
1500 * (2) handles areas that were not covered by anything but got
1501 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001502 */
Mathias Agopianab028732010-03-16 16:41:46 -07001503 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001504 const Region oldVisibleRegion = layer->visibleRegion;
1505 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001506 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1507 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001508 }
1509 dirty.subtractSelf(aboveOpaqueLayers);
1510
1511 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001512 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001513
Mathias Agopianab028732010-03-16 16:41:46 -07001514 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001515 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001516
Jesse Halla8026d22012-09-25 13:25:04 -07001517 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518 layer->setVisibleRegion(visibleRegion);
1519 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001520 layer->setVisibleNonTransparentRegion(
1521 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001522 }
1523
Mathias Agopian87baae12012-07-31 12:38:26 -07001524 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001525}
1526
Mathias Agopian87baae12012-07-31 12:38:26 -07001527void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1528 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001529 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001530 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1531 if (hw->getLayerStack() == layerStack) {
1532 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001533 }
1534 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001535}
1536
1537void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001538{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001539 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001540
Mathias Agopian4fec8732012-06-29 14:12:52 -07001541 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001542 const LayerVector& layers(mDrawingState.layersSortedByZ);
1543 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001544 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001545 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001546 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001547 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001548 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001549 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001550
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001551 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001552}
1553
Mathias Agopianad456f92011-01-13 17:53:01 -08001554void SurfaceFlinger::invalidateHwcGeometry()
1555{
1556 mHwWorkListDirty = true;
1557}
1558
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001559
Mathias Agopiancd60f992012-08-16 16:28:27 -07001560void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001561 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001562{
Dan Stoza71433162014-02-04 16:22:36 -08001563 // We only need to actually compose the display if:
1564 // 1) It is being handled by hardware composer, which may need this to
1565 // keep its virtual display state machine in sync, or
1566 // 2) There is work to be done (the dirty region isn't empty)
1567 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1568 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1569 return;
1570 }
1571
Mathias Agopian87baae12012-07-31 12:38:26 -07001572 Region dirtyRegion(inDirtyRegion);
1573
Mathias Agopianb8a55602009-06-26 19:06:36 -07001574 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001575 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001576
Mathias Agopian42977342012-08-05 00:40:46 -07001577 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001578 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001579 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1580 // takes a rectangle, we must make sure to update that whole
1581 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001582 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001583 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001584 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001585 // We need to redraw the rectangle that will be updated
1586 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001587 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001588 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001589 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001590 } else {
1591 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001592 dirtyRegion.set(hw->bounds());
1593 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001594 }
1595 }
1596
Alan Viverette9c5a3332013-09-12 20:04:35 -07001597 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Mathias Agopianff2ed702013-09-01 21:36:12 -07001598 doComposeSurfaces(hw, dirtyRegion);
1599 } else {
1600 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001601 mat4 colorMatrix = mColorMatrix;
1602 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001603 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001604 }
1605 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001606 doComposeSurfaces(hw, dirtyRegion);
1607 engine.endGroup();
1608 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001609
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001610 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001611 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001612
1613 // swap buffers (presentation)
1614 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001615}
1616
Mathias Agopiancd60f992012-08-16 16:28:27 -07001617void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001618{
Mathias Agopian3f844832013-08-07 21:24:32 -07001619 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001620 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001621 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001622 HWComposer::LayerListIterator cur = hwc.begin(id);
1623 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001624
Jesse Halld05a17f2013-09-30 16:49:30 -07001625 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001626 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001627 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001628 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1629 hw->getDisplayName().string());
1630 return;
1631 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001632
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001633 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001634 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001635 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001636 // when using overlays, we assume a fully transparent framebuffer
1637 // NOTE: we could reduce how much we need to clear, for instance
1638 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001639 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001640 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001641 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001642 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001643 // we start with the whole screen area
1644 const Region bounds(hw->getBounds());
1645
1646 // we remove the scissor part
1647 // we're left with the letterbox region
1648 // (common case is that letterbox ends-up being empty)
1649 const Region letterbox(bounds.subtract(hw->getScissor()));
1650
1651 // compute the area to clear
1652 Region region(hw->undefinedRegion.merge(letterbox));
1653
1654 // but limit it to the dirty region
1655 region.andSelf(dirty);
1656
Mathias Agopianb9494d52012-04-18 02:28:45 -07001657 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001658 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001659 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001660 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001661 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001662 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001663
Mathias Agopian766dc492012-10-30 18:08:06 -07001664 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1665 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001666 // scissor on the main display. It should never be needed
1667 // anyways (though in theory it could since the API allows it).
1668 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001669 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001670 if (scissor != bounds) {
1671 // scissor doesn't match the screen's dimensions, so we
1672 // need to clear everything outside of it and enable
1673 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001674
Mathias Agopianf45c5102012-10-24 16:29:17 -07001675 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001676 const uint32_t height = hw->getHeight();
1677 engine.setScissor(scissor.left, height - scissor.bottom,
1678 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001679 }
1680 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001681 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001682
Mathias Agopian85d751c2012-08-29 16:59:24 -07001683 /*
1684 * and then, render the layers targeted at the framebuffer
1685 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001686
Mathias Agopian13127d82013-03-05 17:47:11 -08001687 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001688 const size_t count = layers.size();
1689 const Transform& tr = hw->getTransform();
1690 if (cur != end) {
1691 // we're using h/w composer
1692 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001693 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001694 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001695 if (!clip.isEmpty()) {
1696 switch (cur->getCompositionType()) {
1697 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001698 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001699 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1700 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001701 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001702 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001703 // never clear the very first layer since we're
1704 // guaranteed the FB is already cleared
1705 layer->clearWithOpenGL(hw, clip);
1706 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001707 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001708 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001709 case HWC_FRAMEBUFFER: {
1710 layer->draw(hw, clip);
1711 break;
1712 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001713 case HWC_FRAMEBUFFER_TARGET: {
1714 // this should not happen as the iterator shouldn't
1715 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001716 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001717 break;
1718 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001719 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001720 }
1721 layer->setAcquireFence(hw, *cur);
1722 }
1723 } else {
1724 // we're not using h/w composer
1725 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001726 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001727 const Region clip(dirty.intersect(
1728 tr.transform(layer->visibleRegion)));
1729 if (!clip.isEmpty()) {
1730 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001731 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001732 }
1733 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001734
1735 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001736 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001737}
1738
Mathias Agopian3f844832013-08-07 21:24:32 -07001739void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001740 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001741 RenderEngine& engine(getRenderEngine());
1742 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001743}
1744
Mathias Agopianac9fa422013-02-11 16:40:36 -08001745void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1746 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001747 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001748 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001749{
Mathias Agopian96f08192010-06-02 23:28:45 -07001750 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001751 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001752
Mathias Agopian96f08192010-06-02 23:28:45 -07001753 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001754 Mutex::Autolock _l(mStateLock);
1755 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001756 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001757}
1758
Mathias Agopian3f844832013-08-07 21:24:32 -07001759status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001760 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001761 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001762 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001763 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001764 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001765 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001766 return NO_ERROR;
1767 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001768 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001769}
1770
Dan Stozac7014012014-02-14 15:03:43 -08001771uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001772 return android_atomic_release_load(&mTransactionFlags);
1773}
1774
Mathias Agopian3f844832013-08-07 21:24:32 -07001775uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001776 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1777}
1778
Mathias Agopian3f844832013-08-07 21:24:32 -07001779uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001780 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1781 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001782 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001783 }
1784 return old;
1785}
1786
Mathias Agopian8b33f032012-07-24 20:43:54 -07001787void SurfaceFlinger::setTransactionState(
1788 const Vector<ComposerState>& state,
1789 const Vector<DisplayState>& displays,
1790 uint32_t flags)
1791{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001792 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001793 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001794 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001795
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001796 if (flags & eAnimation) {
1797 // For window updates that are part of an animation we must wait for
1798 // previous animation "frames" to be handled.
1799 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001800 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001801 if (CC_UNLIKELY(err != NO_ERROR)) {
1802 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001803 // caller after a few seconds.
1804 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1805 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001806 mAnimTransactionPending = false;
1807 break;
1808 }
1809 }
1810 }
1811
Mathias Agopiane57f2922012-08-09 16:29:12 -07001812 size_t count = displays.size();
1813 for (size_t i=0 ; i<count ; i++) {
1814 const DisplayState& s(displays[i]);
1815 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001816 }
1817
Mathias Agopiane57f2922012-08-09 16:29:12 -07001818 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001819 for (size_t i=0 ; i<count ; i++) {
1820 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001821 // Here we need to check that the interface we're given is indeed
1822 // one of our own. A malicious client could give us a NULL
1823 // IInterface, or one of its own or even one of our own but a
1824 // different type. All these situations would cause us to crash.
1825 //
1826 // NOTE: it would be better to use RTTI as we could directly check
1827 // that we have a Client*. however, RTTI is disabled in Android.
1828 if (s.client != NULL) {
1829 sp<IBinder> binder = s.client->asBinder();
1830 if (binder != NULL) {
1831 String16 desc(binder->getInterfaceDescriptor());
1832 if (desc == ISurfaceComposerClient::descriptor) {
1833 sp<Client> client( static_cast<Client *>(s.client.get()) );
1834 transactionFlags |= setClientStateLocked(client, s.state);
1835 }
1836 }
1837 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001838 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001839
Mathias Agopian386aa982011-11-07 21:58:03 -08001840 if (transactionFlags) {
1841 // this triggers the transaction
1842 setTransactionFlags(transactionFlags);
1843
1844 // if this is a synchronous transaction, wait for it to take effect
1845 // before returning.
1846 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001847 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001848 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001849 if (flags & eAnimation) {
1850 mAnimTransactionPending = true;
1851 }
1852 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001853 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1854 if (CC_UNLIKELY(err != NO_ERROR)) {
1855 // just in case something goes wrong in SF, return to the
1856 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001857 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1858 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001859 break;
1860 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001861 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001862 }
1863}
1864
Mathias Agopiane57f2922012-08-09 16:29:12 -07001865uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1866{
Jesse Hall9a143922012-10-04 16:29:19 -07001867 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1868 if (dpyIdx < 0)
1869 return 0;
1870
Mathias Agopiane57f2922012-08-09 16:29:12 -07001871 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001872 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001873 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001874 const uint32_t what = s.what;
1875 if (what & DisplayState::eSurfaceChanged) {
1876 if (disp.surface->asBinder() != s.surface->asBinder()) {
1877 disp.surface = s.surface;
1878 flags |= eDisplayTransactionNeeded;
1879 }
1880 }
1881 if (what & DisplayState::eLayerStackChanged) {
1882 if (disp.layerStack != s.layerStack) {
1883 disp.layerStack = s.layerStack;
1884 flags |= eDisplayTransactionNeeded;
1885 }
1886 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001887 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001888 if (disp.orientation != s.orientation) {
1889 disp.orientation = s.orientation;
1890 flags |= eDisplayTransactionNeeded;
1891 }
1892 if (disp.frame != s.frame) {
1893 disp.frame = s.frame;
1894 flags |= eDisplayTransactionNeeded;
1895 }
1896 if (disp.viewport != s.viewport) {
1897 disp.viewport = s.viewport;
1898 flags |= eDisplayTransactionNeeded;
1899 }
1900 }
1901 }
1902 return flags;
1903}
1904
1905uint32_t SurfaceFlinger::setClientStateLocked(
1906 const sp<Client>& client,
1907 const layer_state_t& s)
1908{
1909 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001910 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001911 if (layer != 0) {
1912 const uint32_t what = s.what;
1913 if (what & layer_state_t::ePositionChanged) {
1914 if (layer->setPosition(s.x, s.y))
1915 flags |= eTraversalNeeded;
1916 }
1917 if (what & layer_state_t::eLayerChanged) {
1918 // NOTE: index needs to be calculated before we update the state
1919 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1920 if (layer->setLayer(s.z)) {
1921 mCurrentState.layersSortedByZ.removeAt(idx);
1922 mCurrentState.layersSortedByZ.add(layer);
1923 // we need traversal (state changed)
1924 // AND transaction (list changed)
1925 flags |= eTransactionNeeded|eTraversalNeeded;
1926 }
1927 }
1928 if (what & layer_state_t::eSizeChanged) {
1929 if (layer->setSize(s.w, s.h)) {
1930 flags |= eTraversalNeeded;
1931 }
1932 }
1933 if (what & layer_state_t::eAlphaChanged) {
1934 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1935 flags |= eTraversalNeeded;
1936 }
1937 if (what & layer_state_t::eMatrixChanged) {
1938 if (layer->setMatrix(s.matrix))
1939 flags |= eTraversalNeeded;
1940 }
1941 if (what & layer_state_t::eTransparentRegionChanged) {
1942 if (layer->setTransparentRegionHint(s.transparentRegion))
1943 flags |= eTraversalNeeded;
1944 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08001945 if ((what & layer_state_t::eVisibilityChanged) ||
1946 (what & layer_state_t::eOpacityChanged)) {
1947 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07001948 if (layer->setFlags(s.flags, s.mask))
1949 flags |= eTraversalNeeded;
1950 }
1951 if (what & layer_state_t::eCropChanged) {
1952 if (layer->setCrop(s.crop))
1953 flags |= eTraversalNeeded;
1954 }
1955 if (what & layer_state_t::eLayerStackChanged) {
1956 // NOTE: index needs to be calculated before we update the state
1957 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1958 if (layer->setLayerStack(s.layerStack)) {
1959 mCurrentState.layersSortedByZ.removeAt(idx);
1960 mCurrentState.layersSortedByZ.add(layer);
1961 // we need traversal (state changed)
1962 // AND transaction (list changed)
1963 flags |= eTransactionNeeded|eTraversalNeeded;
1964 }
1965 }
1966 }
1967 return flags;
1968}
1969
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001970status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001971 const String8& name,
1972 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001973 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1974 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001975{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001976 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001977 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001978 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001979 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001980 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001981 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001982
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001983 status_t result = NO_ERROR;
1984
1985 sp<Layer> layer;
1986
Mathias Agopian3165cc22012-08-08 19:42:09 -07001987 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1988 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001989 result = createNormalLayer(client,
1990 name, w, h, flags, format,
1991 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001992 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001993 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001994 result = createDimLayer(client,
1995 name, w, h, flags,
1996 handle, gbp, &layer);
1997 break;
1998 default:
1999 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002000 break;
2001 }
2002
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002003 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002004 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002005 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002006 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002007 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002008}
2009
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002010status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2011 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2012 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002013{
2014 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002015 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002016 case PIXEL_FORMAT_TRANSPARENT:
2017 case PIXEL_FORMAT_TRANSLUCENT:
2018 format = PIXEL_FORMAT_RGBA_8888;
2019 break;
2020 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002021 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002022 break;
2023 }
2024
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002025 *outLayer = new Layer(this, client, name, w, h, flags);
2026 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2027 if (err == NO_ERROR) {
2028 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002029 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002030 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002031
2032 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2033 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002034}
2035
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002036status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2037 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2038 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002039{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002040 *outLayer = new LayerDim(this, client, name, w, h, flags);
2041 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002042 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002043 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002044}
2045
Mathias Agopianac9fa422013-02-11 16:40:36 -08002046status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002047{
Mathias Agopian67106042013-03-14 19:18:13 -07002048 // called by the window manager when it wants to remove a Layer
2049 status_t err = NO_ERROR;
2050 sp<Layer> l(client->getLayerUser(handle));
2051 if (l != NULL) {
2052 err = removeLayer(l);
2053 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2054 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002055 }
2056 return err;
2057}
2058
Mathias Agopian13127d82013-03-05 17:47:11 -08002059status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002060{
Mathias Agopian67106042013-03-14 19:18:13 -07002061 // called by ~LayerCleaner() when all references to the IBinder (handle)
2062 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002063 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002064 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002065 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002066 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002067 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002068 "error removing layer=%p (%s)", l.get(), strerror(-err));
2069 }
2070 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002071}
2072
Mathias Agopianb60314a2012-04-10 22:09:54 -07002073// ---------------------------------------------------------------------------
2074
Andy McFadden13a082e2012-08-24 10:16:42 -07002075void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002076 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002077 Vector<ComposerState> state;
2078 Vector<DisplayState> displays;
2079 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002080 d.what = DisplayState::eDisplayProjectionChanged |
2081 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002082 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002083 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002084 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002085 d.frame.makeInvalid();
2086 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002087 displays.add(d);
2088 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002089 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002090
2091 const nsecs_t period =
2092 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2093 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002094}
2095
2096void SurfaceFlinger::initializeDisplays() {
2097 class MessageScreenInitialized : public MessageBase {
2098 SurfaceFlinger* flinger;
2099 public:
2100 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2101 virtual bool handler() {
2102 flinger->onInitializeDisplays();
2103 return true;
2104 }
2105 };
2106 sp<MessageBase> msg = new MessageScreenInitialized(this);
2107 postMessageAsync(msg); // we may be called from main thread, use async message
2108}
2109
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002110void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2111 int mode) {
2112 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2113 this);
2114 int32_t type = hw->getDisplayType();
2115 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002116
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002117 if (mode == currentMode) {
2118 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002119 return;
2120 }
2121
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002122 hw->setPowerMode(mode);
2123 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2124 ALOGW("Trying to set power mode for virtual display");
2125 return;
2126 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002127
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002128 if (currentMode == HWC_POWER_MODE_OFF) {
2129 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002130 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2131 // FIXME: eventthread only knows about the main display right now
2132 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002133 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002134 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002135
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002136 mVisibleRegionsDirty = true;
2137 repaintEverything();
2138 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002139 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002140 disableHardwareVsync(true); // also cancels any in-progress resync
2141
Mathias Agopiancde87a32012-09-13 14:09:01 -07002142 // FIXME: eventthread only knows about the main display right now
2143 mEventThread->onScreenReleased();
2144 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002145
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002146 getHwComposer().setPowerMode(type, mode);
2147 mVisibleRegionsDirty = true;
2148 // from this point on, SF will stop drawing on this display
2149 } else {
2150 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002151 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002152}
2153
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002154void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2155 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002156 SurfaceFlinger& mFlinger;
2157 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002158 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002159 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002160 MessageSetPowerMode(SurfaceFlinger& flinger,
2161 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2162 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002163 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002164 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002165 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002166 ALOGE("Attempt to set power mode = %d for null display %p",
2167 mDisplay.get(), mMode);
Jesse Hall9e663de2013-08-16 14:28:37 -07002168 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002169 ALOGW("Attempt to set power mode = %d for virtual display",
2170 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002171 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002172 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002173 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002174 return true;
2175 }
2176 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002177 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002178 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002179}
2180
2181// ---------------------------------------------------------------------------
2182
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002183status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2184{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002185 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002186
Mathias Agopianbd115332013-04-18 16:41:04 -07002187 IPCThreadState* ipc = IPCThreadState::self();
2188 const int pid = ipc->getCallingPid();
2189 const int uid = ipc->getCallingUid();
2190 if ((uid != AID_SHELL) &&
2191 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002192 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002193 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002194 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002195 // Try to get the main lock, but don't insist if we can't
2196 // (this would indicate SF is stuck, but we want to be able to
2197 // print something in dumpsys).
2198 int retry = 3;
2199 while (mStateLock.tryLock()<0 && --retry>=0) {
2200 usleep(1000000);
2201 }
2202 const bool locked(retry >= 0);
2203 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002204 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002205 "SurfaceFlinger appears to be unresponsive, "
2206 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002207 }
2208
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002209 bool dumpAll = true;
2210 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002211 size_t numArgs = args.size();
2212 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002213 if ((index < numArgs) &&
2214 (args[index] == String16("--list"))) {
2215 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002216 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002217 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002218 }
2219
2220 if ((index < numArgs) &&
2221 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002222 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002223 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002224 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002225 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002226
2227 if ((index < numArgs) &&
2228 (args[index] == String16("--latency-clear"))) {
2229 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002230 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002231 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002232 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002233
2234 if ((index < numArgs) &&
2235 (args[index] == String16("--dispsync"))) {
2236 index++;
2237 mPrimaryDispSync.dump(result);
2238 dumpAll = false;
2239 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002240 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002241
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002242 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002243 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002244 }
2245
Mathias Agopian9795c422009-08-26 16:36:26 -07002246 if (locked) {
2247 mStateLock.unlock();
2248 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002249 }
2250 write(fd, result.string(), result.size());
2251 return NO_ERROR;
2252}
2253
Dan Stozac7014012014-02-14 15:03:43 -08002254void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2255 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002256{
2257 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2258 const size_t count = currentLayers.size();
2259 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002260 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002261 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002262 }
2263}
2264
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002265void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002266 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002267{
2268 String8 name;
2269 if (index < args.size()) {
2270 name = String8(args[index]);
2271 index++;
2272 }
2273
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002274 const nsecs_t period =
2275 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002276 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002277
2278 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002279 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002280 } else {
2281 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2282 const size_t count = currentLayers.size();
2283 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002284 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002285 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002286 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002287 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002288 }
2289 }
2290}
2291
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002292void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002293 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002294{
2295 String8 name;
2296 if (index < args.size()) {
2297 name = String8(args[index]);
2298 index++;
2299 }
2300
2301 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2302 const size_t count = currentLayers.size();
2303 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002304 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002305 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002306 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002307 }
2308 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002309
Svetoslavd85084b2014-03-20 10:28:31 -07002310 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002311}
2312
Jamie Gennis6547ff42013-07-16 20:12:42 -07002313// This should only be called from the main thread. Otherwise it would need
2314// the lock and should use mCurrentState rather than mDrawingState.
2315void SurfaceFlinger::logFrameStats() {
2316 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2317 const size_t count = drawingLayers.size();
2318 for (size_t i=0 ; i<count ; i++) {
2319 const sp<Layer>& layer(drawingLayers[i]);
2320 layer->logFrameStats();
2321 }
2322
2323 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2324}
2325
Andy McFadden4803b742012-09-24 19:07:20 -07002326/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2327{
2328 static const char* config =
2329 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002330#ifdef HAS_CONTEXT_PRIORITY
2331 " HAS_CONTEXT_PRIORITY"
2332#endif
2333#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2334 " NEVER_DEFAULT_TO_ASYNC_MODE"
2335#endif
2336#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2337 " TARGET_DISABLE_TRIPLE_BUFFERING"
2338#endif
2339 "]";
2340 result.append(config);
2341}
2342
Mathias Agopian74d211a2013-04-22 16:55:35 +02002343void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2344 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002345{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002346 bool colorize = false;
2347 if (index < args.size()
2348 && (args[index] == String16("--color"))) {
2349 colorize = true;
2350 index++;
2351 }
2352
2353 Colorizer colorizer(colorize);
2354
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002355 // figure out if we're stuck somewhere
2356 const nsecs_t now = systemTime();
2357 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2358 const nsecs_t inTransaction(mDebugInTransaction);
2359 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2360 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2361
2362 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002363 * Dump library configuration.
2364 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002365
2366 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002367 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002368 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002369 appendSfConfigString(result);
2370 appendUiConfigString(result);
2371 appendGuiConfigString(result);
2372 result.append("\n");
2373
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002374 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002375 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002376 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002377 result.append(SyncFeatures::getInstance().toString());
2378 result.append("\n");
2379
Andy McFadden41d67d72014-04-25 16:58:34 -07002380 colorizer.bold(result);
2381 result.append("DispSync configuration: ");
2382 colorizer.reset(result);
2383 result.appendFormat("app phase %"PRId64" ns, sf phase %"PRId64" ns, "
2384 "present offset %d ns (refresh %"PRId64" ns)",
2385 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2386 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2387 result.append("\n");
2388
Andy McFadden4803b742012-09-24 19:07:20 -07002389 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002390 * Dump the visible layer list
2391 */
2392 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2393 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002394 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002395 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002396 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002397 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002398 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002399 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002400 }
2401
2402 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002403 * Dump Display state
2404 */
2405
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002406 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002407 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002408 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002409 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2410 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002411 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002412 }
2413
2414 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002415 * Dump SurfaceFlinger global state
2416 */
2417
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002418 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002419 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002420 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002421
Mathias Agopian888c8222012-08-04 21:10:38 -07002422 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002423 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002424
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002425 colorizer.bold(result);
2426 result.appendFormat("EGL implementation : %s\n",
2427 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2428 colorizer.reset(result);
2429 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002430 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002431
Mathias Agopian875d8e12013-06-07 15:35:48 -07002432 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002433
Mathias Agopian42977342012-08-05 00:40:46 -07002434 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002435 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2436 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002437 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002438 " last eglSwapBuffers() time: %f us\n"
2439 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002440 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002441 " refresh-rate : %f fps\n"
2442 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002443 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002444 " gpu_to_cpu_unsupported : %d\n"
2445 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002446 mLastSwapBufferTime/1000.0,
2447 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002448 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002449 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2450 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002451 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002452 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002453
Mathias Agopian74d211a2013-04-22 16:55:35 +02002454 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002455 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002456
Mathias Agopian74d211a2013-04-22 16:55:35 +02002457 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002458 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002459
2460 /*
2461 * VSYNC state
2462 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002463 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002464
2465 /*
2466 * Dump HWComposer state
2467 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002468 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002469 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002470 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002471 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002472 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002473 (mDebugDisableHWC || mDebugRegion || mDaltonize
2474 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002475 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002476
2477 /*
2478 * Dump gralloc state
2479 */
2480 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2481 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002482}
2483
Mathias Agopian13127d82013-03-05 17:47:11 -08002484const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002485SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002486 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002487 wp<IBinder> dpy;
2488 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2489 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2490 dpy = mDisplays.keyAt(i);
2491 break;
2492 }
2493 }
2494 if (dpy == NULL) {
2495 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2496 // Just use the primary display so we have something to return
2497 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2498 }
2499 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002500}
2501
Keun young Park63f165f2012-08-31 10:53:36 -07002502bool SurfaceFlinger::startDdmConnection()
2503{
2504 void* libddmconnection_dso =
2505 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2506 if (!libddmconnection_dso) {
2507 return false;
2508 }
2509 void (*DdmConnection_start)(const char* name);
2510 DdmConnection_start =
2511 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2512 if (!DdmConnection_start) {
2513 dlclose(libddmconnection_dso);
2514 return false;
2515 }
2516 (*DdmConnection_start)(getServiceName());
2517 return true;
2518}
2519
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002520status_t SurfaceFlinger::onTransact(
2521 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2522{
2523 switch (code) {
2524 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002525 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002526 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002527 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002528 case CLEAR_ANIMATION_FRAME_STATS:
2529 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002530 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002531 {
2532 // codes that require permission check
2533 IPCThreadState* ipc = IPCThreadState::self();
2534 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002535 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002536 if ((uid != AID_GRAPHICS) &&
2537 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002538 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002539 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2540 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002541 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002542 break;
2543 }
2544 case CAPTURE_SCREEN:
2545 {
2546 // codes that require permission check
2547 IPCThreadState* ipc = IPCThreadState::self();
2548 const int pid = ipc->getCallingPid();
2549 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002550 if ((uid != AID_GRAPHICS) &&
2551 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002552 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002553 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2554 return PERMISSION_DENIED;
2555 }
2556 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002557 }
2558 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002559
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002560 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2561 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002562 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002563 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002564 IPCThreadState* ipc = IPCThreadState::self();
2565 const int pid = ipc->getCallingPid();
2566 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002567 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002568 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002569 return PERMISSION_DENIED;
2570 }
2571 int n;
2572 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002573 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002574 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002575 return NO_ERROR;
2576 case 1002: // SHOW_UPDATES
2577 n = data.readInt32();
2578 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002579 invalidateHwcGeometry();
2580 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002581 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002582 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002583 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002584 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002585 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002586 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002587 setTransactionFlags(
2588 eTransactionNeeded|
2589 eDisplayTransactionNeeded|
2590 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002591 return NO_ERROR;
2592 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002593 case 1006:{ // send empty update
2594 signalRefresh();
2595 return NO_ERROR;
2596 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002597 case 1008: // toggle use of hw composer
2598 n = data.readInt32();
2599 mDebugDisableHWC = n ? 1 : 0;
2600 invalidateHwcGeometry();
2601 repaintEverything();
2602 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002603 case 1009: // toggle use of transform hint
2604 n = data.readInt32();
2605 mDebugDisableTransformHint = n ? 1 : 0;
2606 invalidateHwcGeometry();
2607 repaintEverything();
2608 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002609 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002610 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002611 reply->writeInt32(0);
2612 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002613 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002614 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002615 return NO_ERROR;
2616 case 1013: {
2617 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002618 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2619 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002620 return NO_ERROR;
2621 }
2622 case 1014: {
2623 // daltonize
2624 n = data.readInt32();
2625 switch (n % 10) {
2626 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2627 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2628 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2629 }
2630 if (n >= 10) {
2631 mDaltonizer.setMode(Daltonizer::correction);
2632 } else {
2633 mDaltonizer.setMode(Daltonizer::simulation);
2634 }
2635 mDaltonize = n > 0;
2636 invalidateHwcGeometry();
2637 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002638 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002639 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002640 case 1015: {
2641 // apply a color matrix
2642 n = data.readInt32();
2643 mHasColorMatrix = n ? 1 : 0;
2644 if (n) {
2645 // color matrix is sent as mat3 matrix followed by vec3
2646 // offset, then packed into a mat4 where the last row is
2647 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002648 for (size_t i = 0 ; i < 4; i++) {
2649 for (size_t j = 0; j < 4; j++) {
2650 mColorMatrix[i][j] = data.readFloat();
2651 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002652 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002653 } else {
2654 mColorMatrix = mat4();
2655 }
2656 invalidateHwcGeometry();
2657 repaintEverything();
2658 return NO_ERROR;
2659 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002660 // This is an experimental interface
2661 // Needs to be shifted to proper binder interface when we productize
2662 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07002663 n = data.readInt32();
2664 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002665 return NO_ERROR;
2666 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002667 }
2668 }
2669 return err;
2670}
2671
Mathias Agopian53331da2011-08-22 21:44:41 -07002672void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002673 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002674 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002675}
2676
Mathias Agopian59119e62010-10-11 12:37:43 -07002677// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002678// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002679// ---------------------------------------------------------------------------
2680
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002681/* The code below is here to handle b/8734824
2682 *
2683 * We create a IGraphicBufferProducer wrapper that forwards all calls
2684 * to the calling binder thread, where they are executed. This allows
2685 * the calling thread to be reused (on the other side) and not
2686 * depend on having "enough" binder threads to handle the requests.
2687 *
2688 */
2689
2690class GraphicProducerWrapper : public BBinder, public MessageHandler {
2691 sp<IGraphicBufferProducer> impl;
2692 sp<Looper> looper;
2693 status_t result;
2694 bool exitPending;
2695 bool exitRequested;
2696 mutable Barrier barrier;
2697 volatile int32_t memoryBarrier;
2698 uint32_t code;
2699 Parcel const* data;
2700 Parcel* reply;
2701
2702 enum {
2703 MSG_API_CALL,
2704 MSG_EXIT
2705 };
2706
2707 /*
2708 * this is called by our "fake" BpGraphicBufferProducer. We package the
2709 * data and reply Parcel and forward them to the calling thread.
2710 */
2711 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08002712 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002713 this->code = code;
2714 this->data = &data;
2715 this->reply = reply;
2716 android_atomic_acquire_store(0, &memoryBarrier);
2717 if (exitPending) {
2718 // if we've exited, we run the message synchronously right here
2719 handleMessage(Message(MSG_API_CALL));
2720 } else {
2721 barrier.close();
2722 looper->sendMessage(this, Message(MSG_API_CALL));
2723 barrier.wait();
2724 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08002725 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002726 }
2727
2728 /*
2729 * here we run on the binder calling thread. All we've got to do is
2730 * call the real BpGraphicBufferProducer.
2731 */
2732 virtual void handleMessage(const Message& message) {
2733 android_atomic_release_load(&memoryBarrier);
2734 if (message.what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08002735 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002736 barrier.open();
2737 } else if (message.what == MSG_EXIT) {
2738 exitRequested = true;
2739 }
2740 }
2741
2742public:
2743 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2744 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2745 exitPending(false), exitRequested(false) {
2746 }
2747
2748 status_t waitForResponse() {
2749 do {
2750 looper->pollOnce(-1);
2751 } while (!exitRequested);
2752 return result;
2753 }
2754
2755 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002756 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002757 exitPending = true;
2758 looper->sendMessage(this, Message(MSG_EXIT));
2759 }
2760};
2761
2762
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002763status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2764 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002765 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002766 uint32_t minLayerZ, uint32_t maxLayerZ,
2767 bool useIdentityTransform) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002768
2769 if (CC_UNLIKELY(display == 0))
2770 return BAD_VALUE;
2771
2772 if (CC_UNLIKELY(producer == 0))
2773 return BAD_VALUE;
2774
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002775 // if we have secure windows on this display, never allow the screen capture
2776 // unless the producer interface is local (i.e.: we can take a screenshot for
2777 // ourselves).
2778 if (!producer->asBinder()->localBinder()) {
2779 Mutex::Autolock _l(mStateLock);
2780 sp<const DisplayDevice> hw(getDisplayDevice(display));
2781 if (hw->getSecureLayerVisible()) {
2782 ALOGW("FB is protected: PERMISSION_DENIED");
2783 return PERMISSION_DENIED;
2784 }
2785 }
2786
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002787 class MessageCaptureScreen : public MessageBase {
2788 SurfaceFlinger* flinger;
2789 sp<IBinder> display;
2790 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07002791 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002792 uint32_t reqWidth, reqHeight;
2793 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08002794 bool useIdentityTransform;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002795 status_t result;
2796 public:
2797 MessageCaptureScreen(SurfaceFlinger* flinger,
2798 const sp<IBinder>& display,
2799 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002800 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002801 uint32_t minLayerZ, uint32_t maxLayerZ,
2802 bool useIdentityTransform)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002803 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07002804 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002805 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08002806 useIdentityTransform(useIdentityTransform),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002807 result(PERMISSION_DENIED)
2808 {
2809 }
2810 status_t getResult() const {
2811 return result;
2812 }
2813 virtual bool handler() {
2814 Mutex::Autolock _l(flinger->mStateLock);
2815 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08002816 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07002817 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Dan Stozac7014012014-02-14 15:03:43 -08002818 useIdentityTransform);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002819 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002820 return true;
2821 }
2822 };
2823
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002824 // make sure to process transactions before screenshots -- a transaction
2825 // might already be pending but scheduled for VSYNC; this guarantees we
2826 // will handle it before the screenshot. When VSYNC finally arrives
2827 // the scheduled transaction will be a no-op. If no transactions are
2828 // scheduled at this time, this will end-up being a no-op as well.
2829 mEventQueue.invalidateTransactionNow();
2830
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002831 // this creates a "fake" BBinder which will serve as a "fake" remote
2832 // binder to receive the marshaled calls and forward them to the
2833 // real remote (a BpGraphicBufferProducer)
2834 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2835
2836 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2837 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002838 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002839 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07002840 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
2841 useIdentityTransform);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002842
2843 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002844 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002845 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002846 }
2847 return res;
2848}
2849
Mathias Agopian180f10d2013-04-10 22:55:41 -07002850
2851void SurfaceFlinger::renderScreenImplLocked(
2852 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07002853 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07002854 uint32_t minLayerZ, uint32_t maxLayerZ,
Dan Stozac7014012014-02-14 15:03:43 -08002855 bool yswap, bool useIdentityTransform)
Mathias Agopian180f10d2013-04-10 22:55:41 -07002856{
2857 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002858 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002859
2860 // get screen geometry
2861 const uint32_t hw_w = hw->getWidth();
2862 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002863 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2864
Dan Stozac1879002014-05-22 15:59:05 -07002865 // if a default or invalid sourceCrop is passed in, set reasonable values
2866 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
2867 !sourceCrop.isValid()) {
2868 sourceCrop.setLeftTop(Point(0, 0));
2869 sourceCrop.setRightBottom(Point(hw_w, hw_h));
2870 }
2871
2872 // ensure that sourceCrop is inside screen
2873 if (sourceCrop.left < 0) {
2874 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
2875 }
2876 if (sourceCrop.right >= hw_w) {
2877 ALOGE("Invalid crop rect: r = %d (>= %d)", sourceCrop.right, hw_w);
2878 }
2879 if (sourceCrop.top < 0) {
2880 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
2881 }
2882 if (sourceCrop.bottom >= hw_h) {
2883 ALOGE("Invalid crop rect: b = %d (>= %d)", sourceCrop.bottom, hw_h);
2884 }
2885
Mathias Agopian180f10d2013-04-10 22:55:41 -07002886 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002887 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002888
2889 // set-up our viewport
Dan Stozac1879002014-05-22 15:59:05 -07002890 engine.setViewportAndProjection(reqWidth, reqHeight, sourceCrop, hw_h, yswap);
Mathias Agopian3f844832013-08-07 21:24:32 -07002891 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002892
2893 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002894 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002895
2896 const LayerVector& layers( mDrawingState.layersSortedByZ );
2897 const size_t count = layers.size();
2898 for (size_t i=0 ; i<count ; ++i) {
2899 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002900 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002901 if (state.layerStack == hw->getLayerStack()) {
2902 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2903 if (layer->isVisible()) {
2904 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08002905 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002906 if (filtering) layer->setFiltering(false);
2907 }
2908 }
2909 }
2910 }
2911
2912 // compositionComplete is needed for older driver
2913 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002914 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002915}
2916
2917
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002918status_t SurfaceFlinger::captureScreenImplLocked(
2919 const sp<const DisplayDevice>& hw,
2920 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002921 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002922 uint32_t minLayerZ, uint32_t maxLayerZ,
2923 bool useIdentityTransform)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002924{
2925 ATRACE_CALL();
2926
Mathias Agopian180f10d2013-04-10 22:55:41 -07002927 // get screen geometry
2928 const uint32_t hw_w = hw->getWidth();
2929 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002930
Mathias Agopian180f10d2013-04-10 22:55:41 -07002931 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2932 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2933 reqWidth, reqHeight, hw_w, hw_h);
2934 return BAD_VALUE;
2935 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002936
Mathias Agopian180f10d2013-04-10 22:55:41 -07002937 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2938 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2939
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002940 // create a surface (because we're a producer, and we need to
2941 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07002942 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002943 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002944
2945 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002946 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002947 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2948 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002949
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002950 int err = 0;
2951 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07002952 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002953 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2954 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002955
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002956 if (err == NO_ERROR) {
2957 ANativeWindowBuffer* buffer;
2958 /* TODO: Once we have the sync framework everywhere this can use
2959 * server-side waits on the fence that dequeueBuffer returns.
2960 */
2961 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2962 if (result == NO_ERROR) {
2963 // create an EGLImage from the buffer so we can later
2964 // turn it into a texture
2965 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2966 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2967 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002968 // this binds the given EGLImage as a framebuffer for the
2969 // duration of this scope.
2970 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2971 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002972 // this will in fact render into our dequeued buffer
2973 // via an FBO, which means we didn't have to create
2974 // an EGLSurface and therefore we're not
2975 // dependent on the context's EGLConfig.
Dan Stozac1879002014-05-22 15:59:05 -07002976 renderScreenImplLocked(hw, sourceCrop, reqWidth, reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002977 minLayerZ, maxLayerZ, true, useIdentityTransform);
Mathias Agopiand5556842013-09-19 17:08:37 -07002978
Andy McFadden2d8d1202013-10-09 16:38:02 -07002979 // Create a sync point and wait on it, so we know the buffer is
2980 // ready before we pass it along. We can't trivially call glFlush(),
2981 // so we use a wait flag instead.
2982 // TODO: pass a sync fd to queueBuffer() and let the consumer wait.
2983 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
2984 if (sync != EGL_NO_SYNC_KHR) {
2985 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
2986 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
2987 EGLint eglErr = eglGetError();
2988 eglDestroySyncKHR(mEGLDisplay, sync);
2989 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
2990 ALOGW("captureScreen: fence wait timed out");
2991 } else {
2992 ALOGW_IF(eglErr != EGL_SUCCESS,
2993 "captureScreen: error waiting on EGL fence: %#x", eglErr);
2994 }
2995 } else {
2996 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
2997 // not fatal
2998 }
2999
Mathias Agopiand5556842013-09-19 17:08:37 -07003000 if (DEBUG_SCREENSHOTS) {
3001 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3002 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3003 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3004 hw, minLayerZ, maxLayerZ);
3005 delete [] pixels;
3006 }
3007
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003008 } else {
3009 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3010 result = INVALID_OPERATION;
3011 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003012 // destroy our image
3013 eglDestroyImageKHR(mEGLDisplay, image);
3014 } else {
3015 result = BAD_VALUE;
3016 }
3017 window->queueBuffer(window, buffer, -1);
3018 }
3019 } else {
3020 result = BAD_VALUE;
3021 }
3022 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3023 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003024
Mathias Agopian74c40c02010-09-29 13:02:36 -07003025 return result;
3026}
3027
Mathias Agopiand5556842013-09-19 17:08:37 -07003028void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3029 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003030 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003031 for (size_t y=0 ; y<h ; y++) {
3032 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3033 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003034 if (p[x] != 0xFF000000) return;
3035 }
3036 }
3037 ALOGE("*** we just took a black screenshot ***\n"
3038 "requested minz=%d, maxz=%d, layerStack=%d",
3039 minLayerZ, maxLayerZ, hw->getLayerStack());
3040 const LayerVector& layers( mDrawingState.layersSortedByZ );
3041 const size_t count = layers.size();
3042 for (size_t i=0 ; i<count ; ++i) {
3043 const sp<Layer>& layer(layers[i]);
3044 const Layer::State& state(layer->getDrawingState());
3045 const bool visible = (state.layerStack == hw->getLayerStack())
3046 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3047 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003048 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003049 visible ? '+' : '-',
3050 i, layer->getName().string(), state.layerStack, state.z,
3051 layer->isVisible(), state.flags, state.alpha);
3052 }
3053 }
3054}
3055
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003056// ---------------------------------------------------------------------------
3057
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003058SurfaceFlinger::LayerVector::LayerVector() {
3059}
3060
3061SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003062 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003063}
3064
3065int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3066 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003067{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003068 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003069 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3070 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003071
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003072 uint32_t ls = l->getCurrentState().layerStack;
3073 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003074 if (ls != rs)
3075 return ls - rs;
3076
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003077 uint32_t lz = l->getCurrentState().z;
3078 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003079 if (lz != rz)
3080 return lz - rz;
3081
3082 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003083}
3084
3085// ---------------------------------------------------------------------------
3086
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003087SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
3088 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003089}
3090
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003091SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08003092 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003093 viewport.makeInvalid();
3094 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003095}
3096
3097// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003098
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003099}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003100
3101
3102#if defined(__gl_h_)
3103#error "don't include gl/gl.h in this file"
3104#endif
3105
3106#if defined(__gl2_h_)
3107#error "don't include gl2/gl2.h in this file"
3108#endif