blob: f06db5cb1a9a7063cab0f8a18b5abe090d692cfe [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);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700437 hw->acquireScreen();
438 }
439 mDisplays.add(token, hw);
440 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700441 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700442
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700443 // make the GLContext current so that we can create textures when creating Layers
444 // (which may happens before we render something)
445 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
446
Mathias Agopian028508c2012-07-25 21:12:12 -0700447 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700448 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700449 vsyncPhaseOffsetNs, true, "app");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700450 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700451 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700452 sfVsyncPhaseOffsetNs, true, "sf");
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700453 mSFEventThread = new EventThread(sfVsyncSrc);
454 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700455
Jamie Gennisd1700752013-10-14 12:22:52 -0700456 mEventControlThread = new EventControlThread(this);
457 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
458
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700459 // set a fake vsync period if there is no HWComposer
460 if (mHwc->initCheck() != NO_ERROR) {
461 mPrimaryDispSync.setPeriod(16666667);
462 }
463
Mathias Agopian92a979a2012-08-02 18:32:23 -0700464 // initialize our drawing state
465 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700466
Andy McFadden13a082e2012-08-24 10:16:42 -0700467 // set initial conditions (e.g. unblank default device)
468 initializeDisplays();
469
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700470 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700471 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800472}
473
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700474int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700475 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700476 type : mHwc->allocateDisplayId();
477}
478
Mathias Agopiana67e4182012-06-19 17:26:12 -0700479void SurfaceFlinger::startBootAnim() {
480 // start boot animation
481 property_set("service.bootanim.exit", "0");
482 property_set("ctl.start", "bootanim");
483}
484
Mathias Agopian875d8e12013-06-07 15:35:48 -0700485size_t SurfaceFlinger::getMaxTextureSize() const {
486 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700487}
488
Mathias Agopian875d8e12013-06-07 15:35:48 -0700489size_t SurfaceFlinger::getMaxViewportDims() const {
490 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700491}
492
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800493// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800494
Jamie Gennis582270d2011-08-17 18:19:00 -0700495bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800496 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800497 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800498 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700499 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800500}
501
Dan Stoza7f7da322014-05-02 15:26:25 -0700502status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
503 Vector<DisplayInfo>* configs) {
504 if (configs == NULL) {
505 return BAD_VALUE;
506 }
507
Jesse Hall692c7232012-11-08 15:41:56 -0800508 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700509 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800510 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700511 type = i;
512 break;
513 }
514 }
515
516 if (type < 0) {
517 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700518 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700519
Mathias Agopian8b736f12012-08-13 17:54:26 -0700520 // TODO: Not sure if display density should handled by SF any longer
521 class Density {
522 static int getDensityFromProperty(char const* propName) {
523 char property[PROPERTY_VALUE_MAX];
524 int density = 0;
525 if (property_get(propName, property, NULL) > 0) {
526 density = atoi(property);
527 }
528 return density;
529 }
530 public:
531 static int getEmuDensity() {
532 return getDensityFromProperty("qemu.sf.lcd_density"); }
533 static int getBuildDensity() {
534 return getDensityFromProperty("ro.sf.lcd_density"); }
535 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700536
Dan Stoza7f7da322014-05-02 15:26:25 -0700537 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700538
Dan Stoza7f7da322014-05-02 15:26:25 -0700539 const Vector<HWComposer::DisplayConfig>& hwConfigs =
540 getHwComposer().getConfigs(type);
541 for (size_t c = 0; c < hwConfigs.size(); ++c) {
542 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
543 DisplayInfo info = DisplayInfo();
544
545 float xdpi = hwConfig.xdpi;
546 float ydpi = hwConfig.ydpi;
547
548 if (type == DisplayDevice::DISPLAY_PRIMARY) {
549 // The density of the device is provided by a build property
550 float density = Density::getBuildDensity() / 160.0f;
551 if (density == 0) {
552 // the build doesn't provide a density -- this is wrong!
553 // use xdpi instead
554 ALOGE("ro.sf.lcd_density must be defined as a build property");
555 density = xdpi / 160.0f;
556 }
557 if (Density::getEmuDensity()) {
558 // if "qemu.sf.lcd_density" is specified, it overrides everything
559 xdpi = ydpi = density = Density::getEmuDensity();
560 density /= 160.0f;
561 }
562 info.density = density;
563
564 // TODO: this needs to go away (currently needed only by webkit)
565 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
566 info.orientation = hw->getOrientation();
567 } else {
568 // TODO: where should this value come from?
569 static const int TV_DENSITY = 213;
570 info.density = TV_DENSITY / 160.0f;
571 info.orientation = 0;
572 }
573
574 info.w = hwConfig.width;
575 info.h = hwConfig.height;
576 info.xdpi = xdpi;
577 info.ydpi = ydpi;
578 info.fps = float(1e9 / hwConfig.refresh);
579
580 // All non-virtual displays are currently considered secure.
581 info.secure = true;
582
583 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700584 }
585
Dan Stoza7f7da322014-05-02 15:26:25 -0700586 return NO_ERROR;
587}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700588
Dan Stoza7f7da322014-05-02 15:26:25 -0700589int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
590 return 0;
591}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700592
Dan Stoza7f7da322014-05-02 15:26:25 -0700593status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int id) {
Mathias Agopian888c8222012-08-04 21:10:38 -0700594 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700595}
596
Svetoslavd85084b2014-03-20 10:28:31 -0700597status_t SurfaceFlinger::clearAnimationFrameStats() {
598 Mutex::Autolock _l(mStateLock);
599 mAnimFrameTracker.clearStats();
600 return NO_ERROR;
601}
602
603status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
604 Mutex::Autolock _l(mStateLock);
605 mAnimFrameTracker.getStats(outStats);
606 return NO_ERROR;
607}
608
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800609// ----------------------------------------------------------------------------
610
611sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800612 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700613}
614
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800615// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800616
617void SurfaceFlinger::waitForEvent() {
618 mEventQueue.waitMessage();
619}
620
621void SurfaceFlinger::signalTransaction() {
622 mEventQueue.invalidate();
623}
624
625void SurfaceFlinger::signalLayerUpdate() {
626 mEventQueue.invalidate();
627}
628
629void SurfaceFlinger::signalRefresh() {
630 mEventQueue.refresh();
631}
632
633status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800634 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800635 return mEventQueue.postMessage(msg, reltime);
636}
637
638status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800639 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800640 status_t res = mEventQueue.postMessage(msg, reltime);
641 if (res == NO_ERROR) {
642 msg->wait();
643 }
644 return res;
645}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700647void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700648 do {
649 waitForEvent();
650 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800651}
652
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700653void SurfaceFlinger::enableHardwareVsync() {
654 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700655 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700656 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700657 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
658 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700659 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700660 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700661}
662
Jesse Hall948fe0c2013-10-14 12:56:09 -0700663void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700664 Mutex::Autolock _l(mHWVsyncLock);
665
Jesse Hall948fe0c2013-10-14 12:56:09 -0700666 if (makeAvailable) {
667 mHWVsyncAvailable = true;
668 } else if (!mHWVsyncAvailable) {
669 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
670 return;
671 }
672
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700673 const nsecs_t period =
674 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
675
676 mPrimaryDispSync.reset();
677 mPrimaryDispSync.setPeriod(period);
678
679 if (!mPrimaryHWVsyncEnabled) {
680 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700681 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
682 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700683 mPrimaryHWVsyncEnabled = true;
684 }
685}
686
Jesse Hall948fe0c2013-10-14 12:56:09 -0700687void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700688 Mutex::Autolock _l(mHWVsyncLock);
689 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700690 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
691 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700692 mPrimaryDispSync.endResync();
693 mPrimaryHWVsyncEnabled = false;
694 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700695 if (makeUnavailable) {
696 mHWVsyncAvailable = false;
697 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700698}
699
700void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700701 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700702
Jamie Gennisd1700752013-10-14 12:22:52 -0700703 { // Scope for the lock
704 Mutex::Autolock _l(mHWVsyncLock);
705 if (type == 0 && mPrimaryHWVsyncEnabled) {
706 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700707 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700708 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700709
710 if (needsHwVsync) {
711 enableHardwareVsync();
712 } else {
713 disableHardwareVsync(false);
714 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700715}
716
717void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
718 if (mEventThread == NULL) {
719 // This is a temporary workaround for b/7145521. A non-null pointer
720 // does not mean EventThread has finished initializing, so this
721 // is not a correct fix.
722 ALOGW("WARNING: EventThread not started, ignoring hotplug");
723 return;
724 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700725
Jesse Hall9e663de2013-08-16 14:28:37 -0700726 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700727 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800728 if (connected) {
729 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700730 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800731 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
732 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700733 }
734 setTransactionFlags(eDisplayTransactionNeeded);
735
Andy McFadden9e9689c2012-10-10 18:17:51 -0700736 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700737 }
Mathias Agopian86303202012-07-24 22:46:10 -0700738}
739
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700740void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700741 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700742 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700743}
744
Mathias Agopian4fec8732012-06-29 14:12:52 -0700745void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800746 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800747 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700748 case MessageQueue::TRANSACTION:
749 handleMessageTransaction();
750 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700751 case MessageQueue::INVALIDATE:
752 handleMessageTransaction();
753 handleMessageInvalidate();
754 signalRefresh();
755 break;
756 case MessageQueue::REFRESH:
757 handleMessageRefresh();
758 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800759 }
760}
761
Mathias Agopian4fec8732012-06-29 14:12:52 -0700762void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700763 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700764 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700765 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700766 }
767}
768
769void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700770 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700771 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700772}
773
774void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700775 ATRACE_CALL();
776 preComposition();
777 rebuildLayerStacks();
778 setUpHWComposer();
779 doDebugFlashRegions();
780 doComposition();
781 postComposition();
782}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700783
Mathias Agopiancd60f992012-08-16 16:28:27 -0700784void SurfaceFlinger::doDebugFlashRegions()
785{
786 // is debugging enabled
787 if (CC_LIKELY(!mDebugRegion))
788 return;
789
790 const bool repaintEverything = mRepaintEverything;
791 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
792 const sp<DisplayDevice>& hw(mDisplays[dpy]);
793 if (hw->canDraw()) {
794 // transform the dirty region into this screen's coordinate space
795 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
796 if (!dirtyRegion.isEmpty()) {
797 // redraw the whole screen
798 doComposeSurfaces(hw, Region(hw->bounds()));
799
800 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700801 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700802 RenderEngine& engine(getRenderEngine());
803 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
804
Mathias Agopiancd60f992012-08-16 16:28:27 -0700805 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700806 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700807 }
808 }
809 }
810
811 postFramebuffer();
812
813 if (mDebugRegion > 1) {
814 usleep(mDebugRegion * 1000);
815 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700816
817 HWComposer& hwc(getHwComposer());
818 if (hwc.initCheck() == NO_ERROR) {
819 status_t err = hwc.prepare();
820 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
821 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700822}
823
824void SurfaceFlinger::preComposition()
825{
826 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700827 const LayerVector& layers(mDrawingState.layersSortedByZ);
828 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700829 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700830 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700831 needExtraInvalidate = true;
832 }
833 }
834 if (needExtraInvalidate) {
835 signalLayerUpdate();
836 }
837}
838
839void SurfaceFlinger::postComposition()
840{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700841 const LayerVector& layers(mDrawingState.layersSortedByZ);
842 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700843 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700844 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700845 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800846
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700847 const HWComposer& hwc = getHwComposer();
848 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
849
850 if (presentFence->isValid()) {
851 if (mPrimaryDispSync.addPresentFence(presentFence)) {
852 enableHardwareVsync();
853 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700854 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700855 }
856 }
857
Andy McFadden5167ec62014-05-22 13:08:43 -0700858 if (kIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700859 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
860 if (hw->isScreenAcquired()) {
861 enableHardwareVsync();
862 }
863 }
864
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800865 if (mAnimCompositionPending) {
866 mAnimCompositionPending = false;
867
Jesse Halla9a1b002013-02-27 16:39:25 -0800868 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800869 mAnimFrameTracker.setActualPresentFence(presentFence);
870 } else {
871 // The HWC doesn't support present fences, so use the refresh
872 // timestamp instead.
873 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
874 mAnimFrameTracker.setActualPresentTime(presentTime);
875 }
876 mAnimFrameTracker.advanceFrame();
877 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700878}
879
880void SurfaceFlinger::rebuildLayerStacks() {
881 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700882 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700883 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700884 mVisibleRegionsDirty = false;
885 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700886
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700887 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700888 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700889 Region opaqueRegion;
890 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800891 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700892 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700893 const Transform& tr(hw->getTransform());
894 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700895 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700896 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700897 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700898
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700899 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700900 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700901 const sp<Layer>& layer(layers[i]);
902 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700903 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700904 Region drawRegion(tr.transform(
905 layer->visibleNonTransparentRegion));
906 drawRegion.andSelf(bounds);
907 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700908 layersSortedByZ.add(layer);
909 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700910 }
911 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700912 }
Mathias Agopian42977342012-08-05 00:40:46 -0700913 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700914 hw->undefinedRegion.set(bounds);
915 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
916 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700917 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700918 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700919}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700920
Mathias Agopiancd60f992012-08-16 16:28:27 -0700921void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700922 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Dan Stoza71433162014-02-04 16:22:36 -0800923 bool mustRecompose =
924 !(mDisplays[dpy]->getDirtyRegion(false).isEmpty());
925 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700926 }
927
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700928 HWComposer& hwc(getHwComposer());
929 if (hwc.initCheck() == NO_ERROR) {
930 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700931 if (CC_UNLIKELY(mHwWorkListDirty)) {
932 mHwWorkListDirty = false;
933 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
934 sp<const DisplayDevice> hw(mDisplays[dpy]);
935 const int32_t id = hw->getHwcDisplayId();
936 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800937 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700938 hw->getVisibleLayersSortedByZ());
939 const size_t count = currentLayers.size();
940 if (hwc.createWorkList(id, count) == NO_ERROR) {
941 HWComposer::LayerListIterator cur = hwc.begin(id);
942 const HWComposer::LayerListIterator end = hwc.end(id);
943 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800944 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700945 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -0700946 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -0700947 cur->setSkip(true);
948 }
949 }
950 }
951 }
952 }
953 }
954
955 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700956 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700957 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700958 const int32_t id = hw->getHwcDisplayId();
959 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800960 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700961 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700962 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700963 HWComposer::LayerListIterator cur = hwc.begin(id);
964 const HWComposer::LayerListIterator end = hwc.end(id);
965 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
966 /*
967 * update the per-frame h/w composer data for each layer
968 * and build the transparent region of the FB
969 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800970 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700971 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700972 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700973 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700974 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700975
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700976 status_t err = hwc.prepare();
977 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700978
979 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
980 sp<const DisplayDevice> hw(mDisplays[dpy]);
981 hw->prepareFrame(hwc);
982 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700983 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700984}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700985
Mathias Agopiancd60f992012-08-16 16:28:27 -0700986void SurfaceFlinger::doComposition() {
987 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700988 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700989 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700990 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700991 if (hw->canDraw()) {
992 // transform the dirty region into this screen's coordinate space
993 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800994
995 // repaint the framebuffer (if needed)
996 doDisplayComposition(hw, dirtyRegion);
997
Mathias Agopiancd60f992012-08-16 16:28:27 -0700998 hw->dirtyRegion.clear();
999 hw->flip(hw->swapRegion);
1000 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001001 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001002 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001003 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001004 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001005 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001006}
1007
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001008void SurfaceFlinger::postFramebuffer()
1009{
Mathias Agopian841cde52012-03-01 15:44:37 -08001010 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001011
Mathias Agopiana44b0412011-10-16 18:46:35 -07001012 const nsecs_t now = systemTime();
1013 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001014
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001015 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001016 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001017 if (!hwc.supportsFramebufferTarget()) {
1018 // EGL spec says:
1019 // "surface must be bound to the calling thread's current context,
1020 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001021 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001022 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001023 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001024 }
1025
Mathias Agopian6da15f42013-09-25 20:40:07 -07001026 // make the default display current because the VirtualDisplayDevice code cannot
1027 // deal with dequeueBuffer() being called outside of the composition loop; however
1028 // the code below can call glFlush() which is allowed (and does in some case) call
1029 // dequeueBuffer().
1030 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1031
Mathias Agopian92a979a2012-08-02 18:32:23 -07001032 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001033 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001034 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001035 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001036 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001037 int32_t id = hw->getHwcDisplayId();
1038 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001039 HWComposer::LayerListIterator cur = hwc.begin(id);
1040 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001041 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001042 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001043 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001044 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001045 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001046 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001047 }
Jesse Hallef194142012-06-14 14:45:17 -07001048 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001049 }
1050
Mathias Agopiana44b0412011-10-16 18:46:35 -07001051 mLastSwapBufferTime = systemTime() - now;
1052 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001053
1054 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1055 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1056 logFrameStats();
1057 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001058}
1059
Mathias Agopian87baae12012-07-31 12:38:26 -07001060void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001061{
Mathias Agopian841cde52012-03-01 15:44:37 -08001062 ATRACE_CALL();
1063
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001064 // here we keep a copy of the drawing state (that is the state that's
1065 // going to be overwritten by handleTransactionLocked()) outside of
1066 // mStateLock so that the side-effects of the State assignment
1067 // don't happen with mStateLock held (which can cause deadlocks).
1068 State drawingState(mDrawingState);
1069
Mathias Agopianca4d3602011-05-19 15:38:14 -07001070 Mutex::Autolock _l(mStateLock);
1071 const nsecs_t now = systemTime();
1072 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001073
Mathias Agopianca4d3602011-05-19 15:38:14 -07001074 // Here we're guaranteed that some transaction flags are set
1075 // so we can call handleTransactionLocked() unconditionally.
1076 // We call getTransactionFlags(), which will also clear the flags,
1077 // with mStateLock held to guarantee that mCurrentState won't change
1078 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001079
Mathias Agopiane57f2922012-08-09 16:29:12 -07001080 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001081 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001082
Mathias Agopianca4d3602011-05-19 15:38:14 -07001083 mLastTransactionTime = systemTime() - now;
1084 mDebugInTransaction = 0;
1085 invalidateHwcGeometry();
1086 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001087}
1088
Mathias Agopian87baae12012-07-31 12:38:26 -07001089void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001090{
1091 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001092 const size_t count = currentLayers.size();
1093
1094 /*
1095 * Traversal of the children
1096 * (perform the transaction for each of them if needed)
1097 */
1098
Mathias Agopian3559b072012-08-15 13:46:03 -07001099 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001100 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001101 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1103 if (!trFlags) continue;
1104
1105 const uint32_t flags = layer->doTransaction(0);
1106 if (flags & Layer::eVisibleRegion)
1107 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001108 }
1109 }
1110
1111 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001112 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001113 */
1114
Mathias Agopiane57f2922012-08-09 16:29:12 -07001115 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001116 // here we take advantage of Vector's copy-on-write semantics to
1117 // improve performance by skipping the transaction entirely when
1118 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001119 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1120 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001121 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001122 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001123 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001124 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001125
1126 // find the displays that were removed
1127 // (ie: in drawing state but not in current state)
1128 // also handle displays that changed
1129 // (ie: displays that are in both lists)
1130 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001131 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1132 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001133 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001134 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001135 // Call makeCurrent() on the primary display so we can
1136 // be sure that nothing associated with this display
1137 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001138 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001139 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001140 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1141 if (hw != NULL)
1142 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001143 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001144 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001145 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001146 } else {
1147 ALOGW("trying to remove the main display");
1148 }
1149 } else {
1150 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001151 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001152 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001153 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001154 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001155 // recreating the DisplayDevice, so we just remove it
1156 // from the drawing state, so that it get re-added
1157 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001158 sp<DisplayDevice> hw(getDisplayDevice(display));
1159 if (hw != NULL)
1160 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001161 mDisplays.removeItem(display);
1162 mDrawingState.displays.removeItemsAt(i);
1163 dc--; i--;
1164 // at this point we must loop to the next item
1165 continue;
1166 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001167
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001168 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001169 if (disp != NULL) {
1170 if (state.layerStack != draw[i].layerStack) {
1171 disp->setLayerStack(state.layerStack);
1172 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001173 if ((state.orientation != draw[i].orientation)
1174 || (state.viewport != draw[i].viewport)
1175 || (state.frame != draw[i].frame))
1176 {
1177 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001178 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001179 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001180 }
1181 }
1182 }
1183
1184 // find displays that were added
1185 // (ie: in current state but not in drawing state)
1186 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001187 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001188 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001189
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001190 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001191 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001192 sp<IGraphicBufferProducer> bqProducer;
1193 sp<IGraphicBufferConsumer> bqConsumer;
1194 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1195 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001196
Jesse Hall02d86562013-03-25 14:43:23 -07001197 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001198 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001199 // Virtual displays without a surface are dormant:
1200 // they have external state (layer stack, projection,
1201 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001202 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001203
Jesse Hall02d86562013-03-25 14:43:23 -07001204 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001205 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
Dan Stozab9b08832014-03-13 11:55:57 -07001206 *mHwc, hwcDisplayId, state.surface,
1207 bqProducer, bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001208
1209 dispSurface = vds;
1210 if (hwcDisplayId >= 0) {
1211 producer = vds;
1212 } else {
1213 // There won't be any interaction with HWC for this virtual display,
1214 // so the GLES driver can pass buffers directly to the sink.
1215 producer = state.surface;
1216 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001217 }
1218 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001219 ALOGE_IF(state.surface!=NULL,
1220 "adding a supported display, but rendering "
1221 "surface is provided (%p), ignoring it",
1222 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001223 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001224 // for supported (by hwc) displays we provide our
1225 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001226 dispSurface = new FramebufferSurface(*mHwc, state.type,
1227 bqConsumer);
1228 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001229 }
1230
1231 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001232 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001233 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001234 state.type, hwcDisplayId,
1235 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001236 display, dispSurface, producer,
1237 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001238 hw->setLayerStack(state.layerStack);
1239 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001240 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001241 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001242 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001243 if (state.isVirtualDisplay()) {
1244 if (hwcDisplayId >= 0) {
1245 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1246 hw->getWidth(), hw->getHeight(),
1247 hw->getFormat());
1248 }
1249 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001250 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001251 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001252 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001253 }
1254 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001255 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001256 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001257
Mathias Agopian84300952012-11-21 16:02:13 -08001258 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1259 // The transform hint might have changed for some layers
1260 // (either because a display has changed, or because a layer
1261 // as changed).
1262 //
1263 // Walk through all the layers in currentLayers,
1264 // and update their transform hint.
1265 //
1266 // If a layer is visible only on a single display, then that
1267 // display is used to calculate the hint, otherwise we use the
1268 // default display.
1269 //
1270 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1271 // the hint is set before we acquire a buffer from the surface texture.
1272 //
1273 // NOTE: layer transactions have taken place already, so we use their
1274 // drawing state. However, SurfaceFlinger's own transaction has not
1275 // happened yet, so we must use the current state layer list
1276 // (soon to become the drawing state list).
1277 //
1278 sp<const DisplayDevice> disp;
1279 uint32_t currentlayerStack = 0;
1280 for (size_t i=0; i<count; i++) {
1281 // NOTE: we rely on the fact that layers are sorted by
1282 // layerStack first (so we don't have to traverse the list
1283 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001284 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001285 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001286 if (i==0 || currentlayerStack != layerStack) {
1287 currentlayerStack = layerStack;
1288 // figure out if this layerstack is mirrored
1289 // (more than one display) if so, pick the default display,
1290 // if not, pick the only display it's on.
1291 disp.clear();
1292 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1293 sp<const DisplayDevice> hw(mDisplays[dpy]);
1294 if (hw->getLayerStack() == currentlayerStack) {
1295 if (disp == NULL) {
1296 disp = hw;
1297 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001298 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001299 break;
1300 }
1301 }
1302 }
1303 }
Chet Haase91d25932013-04-11 15:24:55 -07001304 if (disp == NULL) {
1305 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1306 // redraw after transform hint changes. See bug 8508397.
1307
1308 // could be null when this layer is using a layerStack
1309 // that is not visible on any display. Also can occur at
1310 // screen off/on times.
1311 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001312 }
Chet Haase91d25932013-04-11 15:24:55 -07001313 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001314 }
1315 }
1316
1317
Mathias Agopian3559b072012-08-15 13:46:03 -07001318 /*
1319 * Perform our own transaction if needed
1320 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001321
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001322 const LayerVector& layers(mDrawingState.layersSortedByZ);
1323 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001324 // layers have been added
1325 mVisibleRegionsDirty = true;
1326 }
1327
1328 // some layers might have been removed, so
1329 // we need to update the regions they're exposing.
1330 if (mLayersRemoved) {
1331 mLayersRemoved = false;
1332 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001333 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001334 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001335 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001336 if (currentLayers.indexOf(layer) < 0) {
1337 // this layer is not visible anymore
1338 // TODO: we could traverse the tree from front to back and
1339 // compute the actual visible region
1340 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001341 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001342 Region visibleReg = s.transform.transform(
1343 Region(Rect(s.active.w, s.active.h)));
1344 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001345 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001346 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001347 }
1348
1349 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001350}
1351
1352void SurfaceFlinger::commitTransaction()
1353{
1354 if (!mLayersPendingRemoval.isEmpty()) {
1355 // Notify removed layers now that they can't be drawn from
1356 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1357 mLayersPendingRemoval[i]->onRemoved();
1358 }
1359 mLayersPendingRemoval.clear();
1360 }
1361
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001362 // If this transaction is part of a window animation then the next frame
1363 // we composite should be considered an animation as well.
1364 mAnimCompositionPending = mAnimTransactionPending;
1365
Mathias Agopian4fec8732012-06-29 14:12:52 -07001366 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001367 mTransactionPending = false;
1368 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001369 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001370}
1371
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001372void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001373 const LayerVector& currentLayers, uint32_t layerStack,
1374 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375{
Mathias Agopian841cde52012-03-01 15:44:37 -08001376 ATRACE_CALL();
1377
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378 Region aboveOpaqueLayers;
1379 Region aboveCoveredLayers;
1380 Region dirty;
1381
Mathias Agopian87baae12012-07-31 12:38:26 -07001382 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383
1384 size_t i = currentLayers.size();
1385 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001386 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001387
1388 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001389 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390
Jesse Hall01e29052013-02-19 16:13:35 -08001391 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001392 if (s.layerStack != layerStack)
1393 continue;
1394
Mathias Agopianab028732010-03-16 16:41:46 -07001395 /*
1396 * opaqueRegion: area of a surface that is fully opaque.
1397 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001399
1400 /*
1401 * visibleRegion: area of a surface that is visible on screen
1402 * and not fully transparent. This is essentially the layer's
1403 * footprint minus the opaque regions above it.
1404 * Areas covered by a translucent surface are considered visible.
1405 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001407
1408 /*
1409 * coveredRegion: area of a surface that is covered by all
1410 * visible regions above it (which includes the translucent areas).
1411 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001413
Jesse Halla8026d22012-09-25 13:25:04 -07001414 /*
1415 * transparentRegion: area of a surface that is hinted to be completely
1416 * transparent. This is only used to tell when the layer has no visible
1417 * non-transparent regions and can be removed from the layer list. It
1418 * does not affect the visibleRegion of this layer or any layers
1419 * beneath it. The hint may not be correct if apps don't respect the
1420 * SurfaceView restrictions (which, sadly, some don't).
1421 */
1422 Region transparentRegion;
1423
Mathias Agopianab028732010-03-16 16:41:46 -07001424
1425 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001426 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001427 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001428 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001429 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001430 if (!visibleRegion.isEmpty()) {
1431 // Remove the transparent area from the visible region
1432 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001433 const Transform tr(s.transform);
1434 if (tr.transformed()) {
1435 if (tr.preserveRects()) {
1436 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001437 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001438 } else {
1439 // transformation too complex, can't do the
1440 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001441 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001442 }
1443 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001444 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001445 }
Mathias Agopianab028732010-03-16 16:41:46 -07001446 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001447
Mathias Agopianab028732010-03-16 16:41:46 -07001448 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001449 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001450 if (s.alpha==255 && !translucent &&
1451 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1452 // the opaque region is the layer's footprint
1453 opaqueRegion = visibleRegion;
1454 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455 }
1456 }
1457
Mathias Agopianab028732010-03-16 16:41:46 -07001458 // Clip the covered region to the visible region
1459 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1460
1461 // Update aboveCoveredLayers for next (lower) layer
1462 aboveCoveredLayers.orSelf(visibleRegion);
1463
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001464 // subtract the opaque region covered by the layers above us
1465 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001466
1467 // compute this layer's dirty region
1468 if (layer->contentDirty) {
1469 // we need to invalidate the whole region
1470 dirty = visibleRegion;
1471 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001472 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001473 layer->contentDirty = false;
1474 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001475 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001476 * the exposed region consists of two components:
1477 * 1) what's VISIBLE now and was COVERED before
1478 * 2) what's EXPOSED now less what was EXPOSED before
1479 *
1480 * note that (1) is conservative, we start with the whole
1481 * visible region but only keep what used to be covered by
1482 * something -- which mean it may have been exposed.
1483 *
1484 * (2) handles areas that were not covered by anything but got
1485 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001486 */
Mathias Agopianab028732010-03-16 16:41:46 -07001487 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001488 const Region oldVisibleRegion = layer->visibleRegion;
1489 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001490 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1491 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001492 }
1493 dirty.subtractSelf(aboveOpaqueLayers);
1494
1495 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001496 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001497
Mathias Agopianab028732010-03-16 16:41:46 -07001498 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001499 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001500
Jesse Halla8026d22012-09-25 13:25:04 -07001501 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001502 layer->setVisibleRegion(visibleRegion);
1503 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001504 layer->setVisibleNonTransparentRegion(
1505 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001506 }
1507
Mathias Agopian87baae12012-07-31 12:38:26 -07001508 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001509}
1510
Mathias Agopian87baae12012-07-31 12:38:26 -07001511void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1512 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001513 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001514 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1515 if (hw->getLayerStack() == layerStack) {
1516 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001517 }
1518 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001519}
1520
1521void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001522{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001523 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001524
Mathias Agopian4fec8732012-06-29 14:12:52 -07001525 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001526 const LayerVector& layers(mDrawingState.layersSortedByZ);
1527 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001528 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001529 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001530 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001531 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001532 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001533 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001534
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001535 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001536}
1537
Mathias Agopianad456f92011-01-13 17:53:01 -08001538void SurfaceFlinger::invalidateHwcGeometry()
1539{
1540 mHwWorkListDirty = true;
1541}
1542
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001543
Mathias Agopiancd60f992012-08-16 16:28:27 -07001544void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001545 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001546{
Dan Stoza71433162014-02-04 16:22:36 -08001547 // We only need to actually compose the display if:
1548 // 1) It is being handled by hardware composer, which may need this to
1549 // keep its virtual display state machine in sync, or
1550 // 2) There is work to be done (the dirty region isn't empty)
1551 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1552 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1553 return;
1554 }
1555
Mathias Agopian87baae12012-07-31 12:38:26 -07001556 Region dirtyRegion(inDirtyRegion);
1557
Mathias Agopianb8a55602009-06-26 19:06:36 -07001558 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001559 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001560
Mathias Agopian42977342012-08-05 00:40:46 -07001561 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001562 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001563 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1564 // takes a rectangle, we must make sure to update that whole
1565 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001566 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001567 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001568 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001569 // We need to redraw the rectangle that will be updated
1570 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001571 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001572 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001573 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001574 } else {
1575 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001576 dirtyRegion.set(hw->bounds());
1577 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001578 }
1579 }
1580
Alan Viverette9c5a3332013-09-12 20:04:35 -07001581 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Mathias Agopianff2ed702013-09-01 21:36:12 -07001582 doComposeSurfaces(hw, dirtyRegion);
1583 } else {
1584 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001585 mat4 colorMatrix = mColorMatrix;
1586 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001587 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001588 }
1589 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001590 doComposeSurfaces(hw, dirtyRegion);
1591 engine.endGroup();
1592 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001593
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001594 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001595 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001596
1597 // swap buffers (presentation)
1598 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001599}
1600
Mathias Agopiancd60f992012-08-16 16:28:27 -07001601void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001602{
Mathias Agopian3f844832013-08-07 21:24:32 -07001603 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001604 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001605 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001606 HWComposer::LayerListIterator cur = hwc.begin(id);
1607 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001608
Jesse Halld05a17f2013-09-30 16:49:30 -07001609 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001610 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001611 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001612 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1613 hw->getDisplayName().string());
1614 return;
1615 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001616
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001617 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001618 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001619 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001620 // when using overlays, we assume a fully transparent framebuffer
1621 // NOTE: we could reduce how much we need to clear, for instance
1622 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001623 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001624 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001625 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001626 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001627 // we start with the whole screen area
1628 const Region bounds(hw->getBounds());
1629
1630 // we remove the scissor part
1631 // we're left with the letterbox region
1632 // (common case is that letterbox ends-up being empty)
1633 const Region letterbox(bounds.subtract(hw->getScissor()));
1634
1635 // compute the area to clear
1636 Region region(hw->undefinedRegion.merge(letterbox));
1637
1638 // but limit it to the dirty region
1639 region.andSelf(dirty);
1640
Mathias Agopianb9494d52012-04-18 02:28:45 -07001641 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001642 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001643 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001644 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001645 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001646 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001647
Mathias Agopian766dc492012-10-30 18:08:06 -07001648 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1649 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001650 // scissor on the main display. It should never be needed
1651 // anyways (though in theory it could since the API allows it).
1652 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001653 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001654 if (scissor != bounds) {
1655 // scissor doesn't match the screen's dimensions, so we
1656 // need to clear everything outside of it and enable
1657 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001658
Mathias Agopianf45c5102012-10-24 16:29:17 -07001659 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001660 const uint32_t height = hw->getHeight();
1661 engine.setScissor(scissor.left, height - scissor.bottom,
1662 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001663 }
1664 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001665 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001666
Mathias Agopian85d751c2012-08-29 16:59:24 -07001667 /*
1668 * and then, render the layers targeted at the framebuffer
1669 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001670
Mathias Agopian13127d82013-03-05 17:47:11 -08001671 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001672 const size_t count = layers.size();
1673 const Transform& tr = hw->getTransform();
1674 if (cur != end) {
1675 // we're using h/w composer
1676 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001677 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001678 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001679 if (!clip.isEmpty()) {
1680 switch (cur->getCompositionType()) {
1681 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001682 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001683 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1684 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001685 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001686 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001687 // never clear the very first layer since we're
1688 // guaranteed the FB is already cleared
1689 layer->clearWithOpenGL(hw, clip);
1690 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001691 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001692 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001693 case HWC_FRAMEBUFFER: {
1694 layer->draw(hw, clip);
1695 break;
1696 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001697 case HWC_FRAMEBUFFER_TARGET: {
1698 // this should not happen as the iterator shouldn't
1699 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001700 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001701 break;
1702 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001703 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001704 }
1705 layer->setAcquireFence(hw, *cur);
1706 }
1707 } else {
1708 // we're not using h/w composer
1709 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001710 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001711 const Region clip(dirty.intersect(
1712 tr.transform(layer->visibleRegion)));
1713 if (!clip.isEmpty()) {
1714 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001715 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001716 }
1717 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001718
1719 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001720 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001721}
1722
Mathias Agopian3f844832013-08-07 21:24:32 -07001723void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001724 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001725 RenderEngine& engine(getRenderEngine());
1726 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001727}
1728
Mathias Agopianac9fa422013-02-11 16:40:36 -08001729void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1730 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001731 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001732 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001733{
Mathias Agopian96f08192010-06-02 23:28:45 -07001734 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001735 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001736
Mathias Agopian96f08192010-06-02 23:28:45 -07001737 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001738 Mutex::Autolock _l(mStateLock);
1739 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001740 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001741}
1742
Mathias Agopian3f844832013-08-07 21:24:32 -07001743status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001744 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001745 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001746 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001747 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001748 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001749 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001750 return NO_ERROR;
1751 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001752 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001753}
1754
Dan Stozac7014012014-02-14 15:03:43 -08001755uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001756 return android_atomic_release_load(&mTransactionFlags);
1757}
1758
Mathias Agopian3f844832013-08-07 21:24:32 -07001759uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001760 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1761}
1762
Mathias Agopian3f844832013-08-07 21:24:32 -07001763uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001764 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1765 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001766 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001767 }
1768 return old;
1769}
1770
Mathias Agopian8b33f032012-07-24 20:43:54 -07001771void SurfaceFlinger::setTransactionState(
1772 const Vector<ComposerState>& state,
1773 const Vector<DisplayState>& displays,
1774 uint32_t flags)
1775{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001776 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001777 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001778 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001779
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001780 if (flags & eAnimation) {
1781 // For window updates that are part of an animation we must wait for
1782 // previous animation "frames" to be handled.
1783 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001784 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001785 if (CC_UNLIKELY(err != NO_ERROR)) {
1786 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001787 // caller after a few seconds.
1788 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1789 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001790 mAnimTransactionPending = false;
1791 break;
1792 }
1793 }
1794 }
1795
Mathias Agopiane57f2922012-08-09 16:29:12 -07001796 size_t count = displays.size();
1797 for (size_t i=0 ; i<count ; i++) {
1798 const DisplayState& s(displays[i]);
1799 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001800 }
1801
Mathias Agopiane57f2922012-08-09 16:29:12 -07001802 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001803 for (size_t i=0 ; i<count ; i++) {
1804 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001805 // Here we need to check that the interface we're given is indeed
1806 // one of our own. A malicious client could give us a NULL
1807 // IInterface, or one of its own or even one of our own but a
1808 // different type. All these situations would cause us to crash.
1809 //
1810 // NOTE: it would be better to use RTTI as we could directly check
1811 // that we have a Client*. however, RTTI is disabled in Android.
1812 if (s.client != NULL) {
1813 sp<IBinder> binder = s.client->asBinder();
1814 if (binder != NULL) {
1815 String16 desc(binder->getInterfaceDescriptor());
1816 if (desc == ISurfaceComposerClient::descriptor) {
1817 sp<Client> client( static_cast<Client *>(s.client.get()) );
1818 transactionFlags |= setClientStateLocked(client, s.state);
1819 }
1820 }
1821 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001822 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001823
Mathias Agopian386aa982011-11-07 21:58:03 -08001824 if (transactionFlags) {
1825 // this triggers the transaction
1826 setTransactionFlags(transactionFlags);
1827
1828 // if this is a synchronous transaction, wait for it to take effect
1829 // before returning.
1830 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001831 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001832 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001833 if (flags & eAnimation) {
1834 mAnimTransactionPending = true;
1835 }
1836 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001837 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1838 if (CC_UNLIKELY(err != NO_ERROR)) {
1839 // just in case something goes wrong in SF, return to the
1840 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001841 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1842 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001843 break;
1844 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001845 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001846 }
1847}
1848
Mathias Agopiane57f2922012-08-09 16:29:12 -07001849uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1850{
Jesse Hall9a143922012-10-04 16:29:19 -07001851 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1852 if (dpyIdx < 0)
1853 return 0;
1854
Mathias Agopiane57f2922012-08-09 16:29:12 -07001855 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001856 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001857 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001858 const uint32_t what = s.what;
1859 if (what & DisplayState::eSurfaceChanged) {
1860 if (disp.surface->asBinder() != s.surface->asBinder()) {
1861 disp.surface = s.surface;
1862 flags |= eDisplayTransactionNeeded;
1863 }
1864 }
1865 if (what & DisplayState::eLayerStackChanged) {
1866 if (disp.layerStack != s.layerStack) {
1867 disp.layerStack = s.layerStack;
1868 flags |= eDisplayTransactionNeeded;
1869 }
1870 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001871 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001872 if (disp.orientation != s.orientation) {
1873 disp.orientation = s.orientation;
1874 flags |= eDisplayTransactionNeeded;
1875 }
1876 if (disp.frame != s.frame) {
1877 disp.frame = s.frame;
1878 flags |= eDisplayTransactionNeeded;
1879 }
1880 if (disp.viewport != s.viewport) {
1881 disp.viewport = s.viewport;
1882 flags |= eDisplayTransactionNeeded;
1883 }
1884 }
1885 }
1886 return flags;
1887}
1888
1889uint32_t SurfaceFlinger::setClientStateLocked(
1890 const sp<Client>& client,
1891 const layer_state_t& s)
1892{
1893 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001894 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001895 if (layer != 0) {
1896 const uint32_t what = s.what;
1897 if (what & layer_state_t::ePositionChanged) {
1898 if (layer->setPosition(s.x, s.y))
1899 flags |= eTraversalNeeded;
1900 }
1901 if (what & layer_state_t::eLayerChanged) {
1902 // NOTE: index needs to be calculated before we update the state
1903 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1904 if (layer->setLayer(s.z)) {
1905 mCurrentState.layersSortedByZ.removeAt(idx);
1906 mCurrentState.layersSortedByZ.add(layer);
1907 // we need traversal (state changed)
1908 // AND transaction (list changed)
1909 flags |= eTransactionNeeded|eTraversalNeeded;
1910 }
1911 }
1912 if (what & layer_state_t::eSizeChanged) {
1913 if (layer->setSize(s.w, s.h)) {
1914 flags |= eTraversalNeeded;
1915 }
1916 }
1917 if (what & layer_state_t::eAlphaChanged) {
1918 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1919 flags |= eTraversalNeeded;
1920 }
1921 if (what & layer_state_t::eMatrixChanged) {
1922 if (layer->setMatrix(s.matrix))
1923 flags |= eTraversalNeeded;
1924 }
1925 if (what & layer_state_t::eTransparentRegionChanged) {
1926 if (layer->setTransparentRegionHint(s.transparentRegion))
1927 flags |= eTraversalNeeded;
1928 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08001929 if ((what & layer_state_t::eVisibilityChanged) ||
1930 (what & layer_state_t::eOpacityChanged)) {
1931 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07001932 if (layer->setFlags(s.flags, s.mask))
1933 flags |= eTraversalNeeded;
1934 }
1935 if (what & layer_state_t::eCropChanged) {
1936 if (layer->setCrop(s.crop))
1937 flags |= eTraversalNeeded;
1938 }
1939 if (what & layer_state_t::eLayerStackChanged) {
1940 // NOTE: index needs to be calculated before we update the state
1941 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1942 if (layer->setLayerStack(s.layerStack)) {
1943 mCurrentState.layersSortedByZ.removeAt(idx);
1944 mCurrentState.layersSortedByZ.add(layer);
1945 // we need traversal (state changed)
1946 // AND transaction (list changed)
1947 flags |= eTransactionNeeded|eTraversalNeeded;
1948 }
1949 }
1950 }
1951 return flags;
1952}
1953
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001954status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001955 const String8& name,
1956 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001957 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1958 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001959{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001960 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001961 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001962 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001963 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001964 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001965 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001966
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001967 status_t result = NO_ERROR;
1968
1969 sp<Layer> layer;
1970
Mathias Agopian3165cc22012-08-08 19:42:09 -07001971 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1972 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001973 result = createNormalLayer(client,
1974 name, w, h, flags, format,
1975 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001976 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001977 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001978 result = createDimLayer(client,
1979 name, w, h, flags,
1980 handle, gbp, &layer);
1981 break;
1982 default:
1983 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001984 break;
1985 }
1986
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001987 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001988 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001989 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001990 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001991 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001992}
1993
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001994status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1995 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1996 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001997{
1998 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001999 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002000 case PIXEL_FORMAT_TRANSPARENT:
2001 case PIXEL_FORMAT_TRANSLUCENT:
2002 format = PIXEL_FORMAT_RGBA_8888;
2003 break;
2004 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07002005#ifdef NO_RGBX_8888
2006 format = PIXEL_FORMAT_RGB_565;
2007#else
Mathias Agopian8f105402010-04-05 18:01:24 -07002008 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07002009#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002010 break;
2011 }
2012
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07002013#ifdef NO_RGBX_8888
2014 if (format == PIXEL_FORMAT_RGBX_8888)
2015 format = PIXEL_FORMAT_RGBA_8888;
2016#endif
2017
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002018 *outLayer = new Layer(this, client, name, w, h, flags);
2019 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2020 if (err == NO_ERROR) {
2021 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002022 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002023 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002024
2025 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2026 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002027}
2028
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002029status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2030 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2031 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002032{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002033 *outLayer = new LayerDim(this, client, name, w, h, flags);
2034 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002035 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002036 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002037}
2038
Mathias Agopianac9fa422013-02-11 16:40:36 -08002039status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002040{
Mathias Agopian67106042013-03-14 19:18:13 -07002041 // called by the window manager when it wants to remove a Layer
2042 status_t err = NO_ERROR;
2043 sp<Layer> l(client->getLayerUser(handle));
2044 if (l != NULL) {
2045 err = removeLayer(l);
2046 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2047 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002048 }
2049 return err;
2050}
2051
Mathias Agopian13127d82013-03-05 17:47:11 -08002052status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002053{
Mathias Agopian67106042013-03-14 19:18:13 -07002054 // called by ~LayerCleaner() when all references to the IBinder (handle)
2055 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002056 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002057 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002058 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002059 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002060 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002061 "error removing layer=%p (%s)", l.get(), strerror(-err));
2062 }
2063 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002064}
2065
Mathias Agopianb60314a2012-04-10 22:09:54 -07002066// ---------------------------------------------------------------------------
2067
Andy McFadden13a082e2012-08-24 10:16:42 -07002068void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002069 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002070 Vector<ComposerState> state;
2071 Vector<DisplayState> displays;
2072 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002073 d.what = DisplayState::eDisplayProjectionChanged |
2074 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002075 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002076 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002077 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002078 d.frame.makeInvalid();
2079 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002080 displays.add(d);
2081 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002082 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07002083
2084 const nsecs_t period =
2085 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2086 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002087}
2088
2089void SurfaceFlinger::initializeDisplays() {
2090 class MessageScreenInitialized : public MessageBase {
2091 SurfaceFlinger* flinger;
2092 public:
2093 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2094 virtual bool handler() {
2095 flinger->onInitializeDisplays();
2096 return true;
2097 }
2098 };
2099 sp<MessageBase> msg = new MessageScreenInitialized(this);
2100 postMessageAsync(msg); // we may be called from main thread, use async message
2101}
2102
2103
Mathias Agopiancde87a32012-09-13 14:09:01 -07002104void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002105 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2106 if (hw->isScreenAcquired()) {
2107 // this is expected, e.g. when power manager wakes up during boot
2108 ALOGD(" screen was previously acquired");
2109 return;
2110 }
2111
Mathias Agopian42977342012-08-05 00:40:46 -07002112 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002113 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002114 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002115 // built-in display, tell the HWC
2116 getHwComposer().acquire(type);
2117
2118 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2119 // FIXME: eventthread only knows about the main display right now
2120 mEventThread->onScreenAcquired();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07002121
Jesse Hall948fe0c2013-10-14 12:56:09 -07002122 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002123 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002124 }
Mathias Agopian20128302012-08-13 18:32:13 -07002125 mVisibleRegionsDirty = true;
2126 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002127}
2128
Mathias Agopiancde87a32012-09-13 14:09:01 -07002129void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002130 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2131 if (!hw->isScreenAcquired()) {
2132 ALOGD(" screen was previously released");
2133 return;
2134 }
2135
2136 hw->releaseScreen();
2137 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002138 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
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
2146 // built-in display, tell the HWC
2147 getHwComposer().release(type);
2148 }
2149 mVisibleRegionsDirty = true;
2150 // from this point on, SF will stop drawing on this display
2151}
2152
2153void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2154 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002155 SurfaceFlinger& mFlinger;
2156 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002157 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002158 MessageScreenAcquired(SurfaceFlinger& flinger,
2159 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002160 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002161 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2162 if (hw == NULL) {
2163 ALOGE("Attempt to unblank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002164 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002165 ALOGW("Attempt to unblank virtual display");
2166 } else {
2167 mFlinger.onScreenAcquired(hw);
2168 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002169 return true;
2170 }
2171 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002172 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2173 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002174}
2175
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002176void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002177 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002178 SurfaceFlinger& mFlinger;
2179 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002180 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002181 MessageScreenReleased(SurfaceFlinger& flinger,
2182 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002183 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002184 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2185 if (hw == NULL) {
2186 ALOGE("Attempt to blank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002187 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002188 ALOGW("Attempt to blank virtual display");
2189 } else {
2190 mFlinger.onScreenReleased(hw);
2191 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002192 return true;
2193 }
2194 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002195 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2196 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002197}
2198
2199// ---------------------------------------------------------------------------
2200
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002201status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2202{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002203 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002204
Mathias Agopianbd115332013-04-18 16:41:04 -07002205 IPCThreadState* ipc = IPCThreadState::self();
2206 const int pid = ipc->getCallingPid();
2207 const int uid = ipc->getCallingUid();
2208 if ((uid != AID_SHELL) &&
2209 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002210 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002211 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002212 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002213 // Try to get the main lock, but don't insist if we can't
2214 // (this would indicate SF is stuck, but we want to be able to
2215 // print something in dumpsys).
2216 int retry = 3;
2217 while (mStateLock.tryLock()<0 && --retry>=0) {
2218 usleep(1000000);
2219 }
2220 const bool locked(retry >= 0);
2221 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002222 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002223 "SurfaceFlinger appears to be unresponsive, "
2224 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002225 }
2226
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002227 bool dumpAll = true;
2228 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002229 size_t numArgs = args.size();
2230 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002231 if ((index < numArgs) &&
2232 (args[index] == String16("--list"))) {
2233 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002234 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002235 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002236 }
2237
2238 if ((index < numArgs) &&
2239 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002240 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002241 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002242 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002243 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002244
2245 if ((index < numArgs) &&
2246 (args[index] == String16("--latency-clear"))) {
2247 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002248 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002249 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002250 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002251
2252 if ((index < numArgs) &&
2253 (args[index] == String16("--dispsync"))) {
2254 index++;
2255 mPrimaryDispSync.dump(result);
2256 dumpAll = false;
2257 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002258 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002259
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002260 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002261 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002262 }
2263
Mathias Agopian9795c422009-08-26 16:36:26 -07002264 if (locked) {
2265 mStateLock.unlock();
2266 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002267 }
2268 write(fd, result.string(), result.size());
2269 return NO_ERROR;
2270}
2271
Dan Stozac7014012014-02-14 15:03:43 -08002272void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2273 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002274{
2275 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2276 const size_t count = currentLayers.size();
2277 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002278 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002279 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002280 }
2281}
2282
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002283void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002284 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002285{
2286 String8 name;
2287 if (index < args.size()) {
2288 name = String8(args[index]);
2289 index++;
2290 }
2291
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002292 const nsecs_t period =
2293 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002294 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002295
2296 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002297 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002298 } else {
2299 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2300 const size_t count = currentLayers.size();
2301 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002302 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002303 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002304 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002305 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002306 }
2307 }
2308}
2309
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002310void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002311 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002312{
2313 String8 name;
2314 if (index < args.size()) {
2315 name = String8(args[index]);
2316 index++;
2317 }
2318
2319 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2320 const size_t count = currentLayers.size();
2321 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002322 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002323 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002324 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002325 }
2326 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002327
Svetoslavd85084b2014-03-20 10:28:31 -07002328 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002329}
2330
Jamie Gennis6547ff42013-07-16 20:12:42 -07002331// This should only be called from the main thread. Otherwise it would need
2332// the lock and should use mCurrentState rather than mDrawingState.
2333void SurfaceFlinger::logFrameStats() {
2334 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2335 const size_t count = drawingLayers.size();
2336 for (size_t i=0 ; i<count ; i++) {
2337 const sp<Layer>& layer(drawingLayers[i]);
2338 layer->logFrameStats();
2339 }
2340
2341 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2342}
2343
Andy McFadden4803b742012-09-24 19:07:20 -07002344/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2345{
2346 static const char* config =
2347 " [sf"
2348#ifdef NO_RGBX_8888
2349 " NO_RGBX_8888"
2350#endif
2351#ifdef HAS_CONTEXT_PRIORITY
2352 " HAS_CONTEXT_PRIORITY"
2353#endif
2354#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2355 " NEVER_DEFAULT_TO_ASYNC_MODE"
2356#endif
2357#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2358 " TARGET_DISABLE_TRIPLE_BUFFERING"
2359#endif
2360 "]";
2361 result.append(config);
2362}
2363
Mathias Agopian74d211a2013-04-22 16:55:35 +02002364void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2365 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002366{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002367 bool colorize = false;
2368 if (index < args.size()
2369 && (args[index] == String16("--color"))) {
2370 colorize = true;
2371 index++;
2372 }
2373
2374 Colorizer colorizer(colorize);
2375
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002376 // figure out if we're stuck somewhere
2377 const nsecs_t now = systemTime();
2378 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2379 const nsecs_t inTransaction(mDebugInTransaction);
2380 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2381 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2382
2383 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002384 * Dump library configuration.
2385 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002386
2387 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002388 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002389 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002390 appendSfConfigString(result);
2391 appendUiConfigString(result);
2392 appendGuiConfigString(result);
2393 result.append("\n");
2394
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002395 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002396 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002397 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002398 result.append(SyncFeatures::getInstance().toString());
2399 result.append("\n");
2400
Andy McFadden41d67d72014-04-25 16:58:34 -07002401 colorizer.bold(result);
2402 result.append("DispSync configuration: ");
2403 colorizer.reset(result);
2404 result.appendFormat("app phase %"PRId64" ns, sf phase %"PRId64" ns, "
2405 "present offset %d ns (refresh %"PRId64" ns)",
2406 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2407 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2408 result.append("\n");
2409
Andy McFadden4803b742012-09-24 19:07:20 -07002410 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002411 * Dump the visible layer list
2412 */
2413 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2414 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002415 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002416 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002417 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002418 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002419 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002420 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002421 }
2422
2423 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002424 * Dump Display state
2425 */
2426
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002427 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002428 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002429 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002430 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2431 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002432 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002433 }
2434
2435 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002436 * Dump SurfaceFlinger global state
2437 */
2438
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002439 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002440 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002441 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002442
Mathias Agopian888c8222012-08-04 21:10:38 -07002443 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002444 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002445
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002446 colorizer.bold(result);
2447 result.appendFormat("EGL implementation : %s\n",
2448 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2449 colorizer.reset(result);
2450 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002451 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002452
Mathias Agopian875d8e12013-06-07 15:35:48 -07002453 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002454
Mathias Agopian42977342012-08-05 00:40:46 -07002455 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002456 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002457 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002458 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002459 " last eglSwapBuffers() time: %f us\n"
2460 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002461 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002462 " refresh-rate : %f fps\n"
2463 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002464 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002465 " gpu_to_cpu_unsupported : %d\n"
2466 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002467 mLastSwapBufferTime/1000.0,
2468 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002469 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002470 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2471 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002472 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002473 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002474
Mathias Agopian74d211a2013-04-22 16:55:35 +02002475 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002476 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002477
Mathias Agopian74d211a2013-04-22 16:55:35 +02002478 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002479 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002480
2481 /*
2482 * VSYNC state
2483 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002484 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002485
2486 /*
2487 * Dump HWComposer state
2488 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002489 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002490 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002491 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002492 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002493 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002494 (mDebugDisableHWC || mDebugRegion || mDaltonize
2495 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002496 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002497
2498 /*
2499 * Dump gralloc state
2500 */
2501 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2502 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002503}
2504
Mathias Agopian13127d82013-03-05 17:47:11 -08002505const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002506SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002507 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002508 wp<IBinder> dpy;
2509 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2510 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2511 dpy = mDisplays.keyAt(i);
2512 break;
2513 }
2514 }
2515 if (dpy == NULL) {
2516 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2517 // Just use the primary display so we have something to return
2518 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2519 }
2520 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002521}
2522
Keun young Park63f165f2012-08-31 10:53:36 -07002523bool SurfaceFlinger::startDdmConnection()
2524{
2525 void* libddmconnection_dso =
2526 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2527 if (!libddmconnection_dso) {
2528 return false;
2529 }
2530 void (*DdmConnection_start)(const char* name);
2531 DdmConnection_start =
2532 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2533 if (!DdmConnection_start) {
2534 dlclose(libddmconnection_dso);
2535 return false;
2536 }
2537 (*DdmConnection_start)(getServiceName());
2538 return true;
2539}
2540
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002541status_t SurfaceFlinger::onTransact(
2542 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2543{
2544 switch (code) {
2545 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002546 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002547 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002548 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002549 case BLANK:
2550 case UNBLANK:
Svetoslavd85084b2014-03-20 10:28:31 -07002551 case CLEAR_ANIMATION_FRAME_STATS:
2552 case GET_ANIMATION_FRAME_STATS:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002553 {
2554 // codes that require permission check
2555 IPCThreadState* ipc = IPCThreadState::self();
2556 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002557 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002558 if ((uid != AID_GRAPHICS) &&
2559 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002560 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002561 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2562 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002563 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002564 break;
2565 }
2566 case CAPTURE_SCREEN:
2567 {
2568 // codes that require permission check
2569 IPCThreadState* ipc = IPCThreadState::self();
2570 const int pid = ipc->getCallingPid();
2571 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002572 if ((uid != AID_GRAPHICS) &&
2573 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002574 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002575 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2576 return PERMISSION_DENIED;
2577 }
2578 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002579 }
2580 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002581
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002582 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2583 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002584 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002585 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002586 IPCThreadState* ipc = IPCThreadState::self();
2587 const int pid = ipc->getCallingPid();
2588 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002589 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002590 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002591 return PERMISSION_DENIED;
2592 }
2593 int n;
2594 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002595 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002596 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002597 return NO_ERROR;
2598 case 1002: // SHOW_UPDATES
2599 n = data.readInt32();
2600 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002601 invalidateHwcGeometry();
2602 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002603 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002604 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002605 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002606 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002607 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002608 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002609 setTransactionFlags(
2610 eTransactionNeeded|
2611 eDisplayTransactionNeeded|
2612 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002613 return NO_ERROR;
2614 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002615 case 1006:{ // send empty update
2616 signalRefresh();
2617 return NO_ERROR;
2618 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002619 case 1008: // toggle use of hw composer
2620 n = data.readInt32();
2621 mDebugDisableHWC = n ? 1 : 0;
2622 invalidateHwcGeometry();
2623 repaintEverything();
2624 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002625 case 1009: // toggle use of transform hint
2626 n = data.readInt32();
2627 mDebugDisableTransformHint = n ? 1 : 0;
2628 invalidateHwcGeometry();
2629 repaintEverything();
2630 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002631 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002632 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002633 reply->writeInt32(0);
2634 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002635 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002636 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002637 return NO_ERROR;
2638 case 1013: {
2639 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002640 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2641 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002642 return NO_ERROR;
2643 }
2644 case 1014: {
2645 // daltonize
2646 n = data.readInt32();
2647 switch (n % 10) {
2648 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2649 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2650 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2651 }
2652 if (n >= 10) {
2653 mDaltonizer.setMode(Daltonizer::correction);
2654 } else {
2655 mDaltonizer.setMode(Daltonizer::simulation);
2656 }
2657 mDaltonize = n > 0;
2658 invalidateHwcGeometry();
2659 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002660 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002661 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002662 case 1015: {
2663 // apply a color matrix
2664 n = data.readInt32();
2665 mHasColorMatrix = n ? 1 : 0;
2666 if (n) {
2667 // color matrix is sent as mat3 matrix followed by vec3
2668 // offset, then packed into a mat4 where the last row is
2669 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002670 for (size_t i = 0 ; i < 4; i++) {
2671 for (size_t j = 0; j < 4; j++) {
2672 mColorMatrix[i][j] = data.readFloat();
2673 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002674 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002675 } else {
2676 mColorMatrix = mat4();
2677 }
2678 invalidateHwcGeometry();
2679 repaintEverything();
2680 return NO_ERROR;
2681 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002682 // This is an experimental interface
2683 // Needs to be shifted to proper binder interface when we productize
2684 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07002685 n = data.readInt32();
2686 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002687 return NO_ERROR;
2688 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002689 }
2690 }
2691 return err;
2692}
2693
Mathias Agopian53331da2011-08-22 21:44:41 -07002694void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002695 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002696 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002697}
2698
Mathias Agopian59119e62010-10-11 12:37:43 -07002699// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002700// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002701// ---------------------------------------------------------------------------
2702
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002703/* The code below is here to handle b/8734824
2704 *
2705 * We create a IGraphicBufferProducer wrapper that forwards all calls
2706 * to the calling binder thread, where they are executed. This allows
2707 * the calling thread to be reused (on the other side) and not
2708 * depend on having "enough" binder threads to handle the requests.
2709 *
2710 */
2711
2712class GraphicProducerWrapper : public BBinder, public MessageHandler {
2713 sp<IGraphicBufferProducer> impl;
2714 sp<Looper> looper;
2715 status_t result;
2716 bool exitPending;
2717 bool exitRequested;
2718 mutable Barrier barrier;
2719 volatile int32_t memoryBarrier;
2720 uint32_t code;
2721 Parcel const* data;
2722 Parcel* reply;
2723
2724 enum {
2725 MSG_API_CALL,
2726 MSG_EXIT
2727 };
2728
2729 /*
2730 * this is called by our "fake" BpGraphicBufferProducer. We package the
2731 * data and reply Parcel and forward them to the calling thread.
2732 */
2733 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08002734 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002735 this->code = code;
2736 this->data = &data;
2737 this->reply = reply;
2738 android_atomic_acquire_store(0, &memoryBarrier);
2739 if (exitPending) {
2740 // if we've exited, we run the message synchronously right here
2741 handleMessage(Message(MSG_API_CALL));
2742 } else {
2743 barrier.close();
2744 looper->sendMessage(this, Message(MSG_API_CALL));
2745 barrier.wait();
2746 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08002747 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002748 }
2749
2750 /*
2751 * here we run on the binder calling thread. All we've got to do is
2752 * call the real BpGraphicBufferProducer.
2753 */
2754 virtual void handleMessage(const Message& message) {
2755 android_atomic_release_load(&memoryBarrier);
2756 if (message.what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08002757 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002758 barrier.open();
2759 } else if (message.what == MSG_EXIT) {
2760 exitRequested = true;
2761 }
2762 }
2763
2764public:
2765 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2766 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2767 exitPending(false), exitRequested(false) {
2768 }
2769
2770 status_t waitForResponse() {
2771 do {
2772 looper->pollOnce(-1);
2773 } while (!exitRequested);
2774 return result;
2775 }
2776
2777 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002778 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002779 exitPending = true;
2780 looper->sendMessage(this, Message(MSG_EXIT));
2781 }
2782};
2783
2784
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002785status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2786 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002787 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002788 uint32_t minLayerZ, uint32_t maxLayerZ,
2789 bool useIdentityTransform) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002790
2791 if (CC_UNLIKELY(display == 0))
2792 return BAD_VALUE;
2793
2794 if (CC_UNLIKELY(producer == 0))
2795 return BAD_VALUE;
2796
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002797 // if we have secure windows on this display, never allow the screen capture
2798 // unless the producer interface is local (i.e.: we can take a screenshot for
2799 // ourselves).
2800 if (!producer->asBinder()->localBinder()) {
2801 Mutex::Autolock _l(mStateLock);
2802 sp<const DisplayDevice> hw(getDisplayDevice(display));
2803 if (hw->getSecureLayerVisible()) {
2804 ALOGW("FB is protected: PERMISSION_DENIED");
2805 return PERMISSION_DENIED;
2806 }
2807 }
2808
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002809 class MessageCaptureScreen : public MessageBase {
2810 SurfaceFlinger* flinger;
2811 sp<IBinder> display;
2812 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07002813 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002814 uint32_t reqWidth, reqHeight;
2815 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08002816 bool useIdentityTransform;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002817 status_t result;
2818 public:
2819 MessageCaptureScreen(SurfaceFlinger* flinger,
2820 const sp<IBinder>& display,
2821 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002822 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002823 uint32_t minLayerZ, uint32_t maxLayerZ,
2824 bool useIdentityTransform)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002825 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07002826 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002827 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08002828 useIdentityTransform(useIdentityTransform),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002829 result(PERMISSION_DENIED)
2830 {
2831 }
2832 status_t getResult() const {
2833 return result;
2834 }
2835 virtual bool handler() {
2836 Mutex::Autolock _l(flinger->mStateLock);
2837 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08002838 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07002839 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Dan Stozac7014012014-02-14 15:03:43 -08002840 useIdentityTransform);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002841 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002842 return true;
2843 }
2844 };
2845
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002846 // make sure to process transactions before screenshots -- a transaction
2847 // might already be pending but scheduled for VSYNC; this guarantees we
2848 // will handle it before the screenshot. When VSYNC finally arrives
2849 // the scheduled transaction will be a no-op. If no transactions are
2850 // scheduled at this time, this will end-up being a no-op as well.
2851 mEventQueue.invalidateTransactionNow();
2852
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002853 // this creates a "fake" BBinder which will serve as a "fake" remote
2854 // binder to receive the marshaled calls and forward them to the
2855 // real remote (a BpGraphicBufferProducer)
2856 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2857
2858 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2859 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002860 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002861 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07002862 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
2863 useIdentityTransform);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002864
2865 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002866 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002867 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002868 }
2869 return res;
2870}
2871
Mathias Agopian180f10d2013-04-10 22:55:41 -07002872
2873void SurfaceFlinger::renderScreenImplLocked(
2874 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07002875 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07002876 uint32_t minLayerZ, uint32_t maxLayerZ,
Dan Stozac7014012014-02-14 15:03:43 -08002877 bool yswap, bool useIdentityTransform)
Mathias Agopian180f10d2013-04-10 22:55:41 -07002878{
2879 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002880 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002881
2882 // get screen geometry
2883 const uint32_t hw_w = hw->getWidth();
2884 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002885 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2886
Dan Stozac1879002014-05-22 15:59:05 -07002887 // if a default or invalid sourceCrop is passed in, set reasonable values
2888 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
2889 !sourceCrop.isValid()) {
2890 sourceCrop.setLeftTop(Point(0, 0));
2891 sourceCrop.setRightBottom(Point(hw_w, hw_h));
2892 }
2893
2894 // ensure that sourceCrop is inside screen
2895 if (sourceCrop.left < 0) {
2896 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
2897 }
2898 if (sourceCrop.right >= hw_w) {
2899 ALOGE("Invalid crop rect: r = %d (>= %d)", sourceCrop.right, hw_w);
2900 }
2901 if (sourceCrop.top < 0) {
2902 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
2903 }
2904 if (sourceCrop.bottom >= hw_h) {
2905 ALOGE("Invalid crop rect: b = %d (>= %d)", sourceCrop.bottom, hw_h);
2906 }
2907
Mathias Agopian180f10d2013-04-10 22:55:41 -07002908 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002909 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002910
2911 // set-up our viewport
Dan Stozac1879002014-05-22 15:59:05 -07002912 engine.setViewportAndProjection(reqWidth, reqHeight, sourceCrop, hw_h, yswap);
Mathias Agopian3f844832013-08-07 21:24:32 -07002913 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002914
2915 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002916 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002917
2918 const LayerVector& layers( mDrawingState.layersSortedByZ );
2919 const size_t count = layers.size();
2920 for (size_t i=0 ; i<count ; ++i) {
2921 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002922 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002923 if (state.layerStack == hw->getLayerStack()) {
2924 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2925 if (layer->isVisible()) {
2926 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08002927 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002928 if (filtering) layer->setFiltering(false);
2929 }
2930 }
2931 }
2932 }
2933
2934 // compositionComplete is needed for older driver
2935 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002936 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002937}
2938
2939
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002940status_t SurfaceFlinger::captureScreenImplLocked(
2941 const sp<const DisplayDevice>& hw,
2942 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002943 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002944 uint32_t minLayerZ, uint32_t maxLayerZ,
2945 bool useIdentityTransform)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002946{
2947 ATRACE_CALL();
2948
Mathias Agopian180f10d2013-04-10 22:55:41 -07002949 // get screen geometry
2950 const uint32_t hw_w = hw->getWidth();
2951 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002952
Mathias Agopian180f10d2013-04-10 22:55:41 -07002953 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2954 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2955 reqWidth, reqHeight, hw_w, hw_h);
2956 return BAD_VALUE;
2957 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002958
Mathias Agopian180f10d2013-04-10 22:55:41 -07002959 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2960 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2961
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002962 // create a surface (because we're a producer, and we need to
2963 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07002964 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002965 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002966
2967 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002968 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002969 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2970 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002971
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002972 int err = 0;
2973 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07002974 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002975 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2976 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002977
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002978 if (err == NO_ERROR) {
2979 ANativeWindowBuffer* buffer;
2980 /* TODO: Once we have the sync framework everywhere this can use
2981 * server-side waits on the fence that dequeueBuffer returns.
2982 */
2983 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2984 if (result == NO_ERROR) {
2985 // create an EGLImage from the buffer so we can later
2986 // turn it into a texture
2987 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2988 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2989 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002990 // this binds the given EGLImage as a framebuffer for the
2991 // duration of this scope.
2992 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2993 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002994 // this will in fact render into our dequeued buffer
2995 // via an FBO, which means we didn't have to create
2996 // an EGLSurface and therefore we're not
2997 // dependent on the context's EGLConfig.
Dan Stozac1879002014-05-22 15:59:05 -07002998 renderScreenImplLocked(hw, sourceCrop, reqWidth, reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002999 minLayerZ, maxLayerZ, true, useIdentityTransform);
Mathias Agopiand5556842013-09-19 17:08:37 -07003000
Andy McFadden2d8d1202013-10-09 16:38:02 -07003001 // Create a sync point and wait on it, so we know the buffer is
3002 // ready before we pass it along. We can't trivially call glFlush(),
3003 // so we use a wait flag instead.
3004 // TODO: pass a sync fd to queueBuffer() and let the consumer wait.
3005 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3006 if (sync != EGL_NO_SYNC_KHR) {
3007 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3008 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3009 EGLint eglErr = eglGetError();
3010 eglDestroySyncKHR(mEGLDisplay, sync);
3011 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3012 ALOGW("captureScreen: fence wait timed out");
3013 } else {
3014 ALOGW_IF(eglErr != EGL_SUCCESS,
3015 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3016 }
3017 } else {
3018 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3019 // not fatal
3020 }
3021
Mathias Agopiand5556842013-09-19 17:08:37 -07003022 if (DEBUG_SCREENSHOTS) {
3023 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3024 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3025 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3026 hw, minLayerZ, maxLayerZ);
3027 delete [] pixels;
3028 }
3029
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003030 } else {
3031 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3032 result = INVALID_OPERATION;
3033 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003034 // destroy our image
3035 eglDestroyImageKHR(mEGLDisplay, image);
3036 } else {
3037 result = BAD_VALUE;
3038 }
3039 window->queueBuffer(window, buffer, -1);
3040 }
3041 } else {
3042 result = BAD_VALUE;
3043 }
3044 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3045 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003046
Mathias Agopian74c40c02010-09-29 13:02:36 -07003047 return result;
3048}
3049
Mathias Agopiand5556842013-09-19 17:08:37 -07003050void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3051 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003052 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003053 for (size_t y=0 ; y<h ; y++) {
3054 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3055 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003056 if (p[x] != 0xFF000000) return;
3057 }
3058 }
3059 ALOGE("*** we just took a black screenshot ***\n"
3060 "requested minz=%d, maxz=%d, layerStack=%d",
3061 minLayerZ, maxLayerZ, hw->getLayerStack());
3062 const LayerVector& layers( mDrawingState.layersSortedByZ );
3063 const size_t count = layers.size();
3064 for (size_t i=0 ; i<count ; ++i) {
3065 const sp<Layer>& layer(layers[i]);
3066 const Layer::State& state(layer->getDrawingState());
3067 const bool visible = (state.layerStack == hw->getLayerStack())
3068 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3069 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003070 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003071 visible ? '+' : '-',
3072 i, layer->getName().string(), state.layerStack, state.z,
3073 layer->isVisible(), state.flags, state.alpha);
3074 }
3075 }
3076}
3077
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003078// ---------------------------------------------------------------------------
3079
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003080SurfaceFlinger::LayerVector::LayerVector() {
3081}
3082
3083SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003084 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003085}
3086
3087int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3088 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003089{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003090 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003091 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3092 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003093
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003094 uint32_t ls = l->getCurrentState().layerStack;
3095 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003096 if (ls != rs)
3097 return ls - rs;
3098
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003099 uint32_t lz = l->getCurrentState().z;
3100 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003101 if (lz != rz)
3102 return lz - rz;
3103
3104 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003105}
3106
3107// ---------------------------------------------------------------------------
3108
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003109SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
3110 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003111}
3112
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003113SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08003114 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003115 viewport.makeInvalid();
3116 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003117}
3118
3119// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003120
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003121}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003122
3123
3124#if defined(__gl_h_)
3125#error "don't include gl/gl.h in this file"
3126#endif
3127
3128#if defined(__gl2_h_)
3129#error "don't include gl2/gl2.h in this file"
3130#endif