blob: 53b0dc4d65004b77f641450bb2b9684ab9ce48bf [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Greg Hackmann86efcc02014-03-07 12:44:02 -080024#include <inttypes.h>
Jesse Hallb154c422014-07-13 12:47:02 -070025#include <stdatomic.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070026
27#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036
Mathias Agopianc666cae2012-07-25 18:56:13 -070037#include <ui/DisplayInfo.h>
38
Mathias Agopian921e6ac2012-07-23 23:11:29 -070039#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070040#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070041#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070042#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080043#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080044#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070045
46#include <ui/GraphicBufferAllocator.h>
47#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070048#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080049
Mathias Agopiancde87a32012-09-13 14:09:01 -070050#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include <utils/String8.h>
52#include <utils/String16.h>
53#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080054#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Mathias Agopian921e6ac2012-07-23 23:11:29 -070056#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070057#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
Mathias Agopian3e25fd82013-04-22 17:52:16 +020059#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020061#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080062#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070063#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070064#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070065#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080066#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070
Mathias Agopiana4912602012-07-12 14:25:33 -070071#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070072#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070073#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074
Mathias Agopianff2ed702013-09-01 21:36:12 -070075#include "Effects/Daltonizer.h"
76
Mathias Agopian875d8e12013-06-07 15:35:48 -070077#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070078#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070079
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080#define DISPLAY_COUNT 1
81
Mathias Agopianfee2b462013-07-03 12:34:01 -070082/*
83 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
84 * black pixels.
85 */
86#define DEBUG_SCREENSHOTS false
87
Mathias Agopianca088332013-03-28 17:44:13 -070088EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
89
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070091
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070092// This is the phase offset in nanoseconds of the software vsync event
93// relative to the vsync event reported by HWComposer. The software vsync
94// event is when SurfaceFlinger and Choreographer-based applications run each
95// frame.
96//
97// This phase offset allows adjustment of the minimum latency from application
98// wake-up (by Choregographer) time to the time at which the resulting window
99// image is displayed. This value may be either positive (after the HW vsync)
100// or negative (before the HW vsync). Setting it to 0 will result in a
101// minimum latency of two vsync periods because the app and SurfaceFlinger
102// will run just after the HW vsync. Setting it to a positive number will
103// result in the minimum latency being:
104//
105// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
106//
107// Note that reducing this latency makes it more likely for the applications
108// to not have their window content image ready in time. When this happens
109// the latency will end up being an additional vsync period, and animations
110// will hiccup. Therefore, this latency should be tuned somewhat
111// conservatively (or at least with awareness of the trade-off being made).
112static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
113
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700114// This is the phase offset at which SurfaceFlinger's composition runs.
115static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
116
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117// ---------------------------------------------------------------------------
118
Mathias Agopian99b49842011-06-27 16:05:52 -0700119const String16 sHardwareTest("android.permission.HARDWARE_TEST");
120const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
121const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
122const String16 sDump("android.permission.DUMP");
123
124// ---------------------------------------------------------------------------
125
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800126SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700127 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700129 mTransactionPending(false),
130 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700131 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700132 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700133 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700136 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800137 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700139 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700140 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700141 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700142 mDebugInSwapBuffers(0),
143 mLastSwapBufferTime(0),
144 mDebugInTransaction(0),
145 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700146 mBootFinished(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700147 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700148 mHWVsyncAvailable(false),
Alan Viverette9c5a3332013-09-12 20:04:35 -0700149 mDaltonize(false),
150 mHasColorMatrix(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151{
Steve Blocka19954a2012-01-04 20:05:49 +0000152 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153
154 // debugging stuff...
155 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700156
Mathias Agopianb4b17302013-03-20 18:36:41 -0700157 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700158 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700159
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 property_get("debug.sf.showupdates", value, "0");
161 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700162
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700163 property_get("debug.sf.ddms", value, "0");
164 mDebugDDMS = atoi(value);
165 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700166 if (!startDdmConnection()) {
167 // start failed, and DDMS debugging not enabled
168 mDebugDDMS = 0;
169 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700170 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700171 ALOGI_IF(mDebugRegion, "showupdates enabled");
172 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173}
174
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800175void SurfaceFlinger::onFirstRef()
176{
177 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800178}
179
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180SurfaceFlinger::~SurfaceFlinger()
181{
Mathias Agopiana4912602012-07-12 14:25:33 -0700182 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
183 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
184 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185}
186
Dan Stozac7014012014-02-14 15:03:43 -0800187void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800188{
189 // the window manager died on us. prepare its eulogy.
190
Andy McFadden13a082e2012-08-24 10:16:42 -0700191 // restore initial conditions (default device unblank, etc)
192 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800193
194 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700195 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800196}
197
Mathias Agopian7e27f052010-05-28 14:22:23 -0700198sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199{
Mathias Agopian96f08192010-06-02 23:28:45 -0700200 sp<ISurfaceComposerClient> bclient;
201 sp<Client> client(new Client(this));
202 status_t err = client->initCheck();
203 if (err == NO_ERROR) {
204 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 return bclient;
207}
208
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700209sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
210 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700211{
212 class DisplayToken : public BBinder {
213 sp<SurfaceFlinger> flinger;
214 virtual ~DisplayToken() {
215 // no more references, this display must be terminated
216 Mutex::Autolock _l(flinger->mStateLock);
217 flinger->mCurrentState.displays.removeItem(this);
218 flinger->setTransactionFlags(eDisplayTransactionNeeded);
219 }
220 public:
221 DisplayToken(const sp<SurfaceFlinger>& flinger)
222 : flinger(flinger) {
223 }
224 };
225
226 sp<BBinder> token = new DisplayToken(this);
227
228 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700229 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700230 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700231 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700232 mCurrentState.displays.add(token, info);
233
234 return token;
235}
236
Jesse Hall6c913be2013-08-08 12:15:49 -0700237void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
238 Mutex::Autolock _l(mStateLock);
239
240 ssize_t idx = mCurrentState.displays.indexOfKey(display);
241 if (idx < 0) {
242 ALOGW("destroyDisplay: invalid display token");
243 return;
244 }
245
246 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
247 if (!info.isVirtualDisplay()) {
248 ALOGE("destroyDisplay called for non-virtual display");
249 return;
250 }
251
252 mCurrentState.displays.removeItemsAt(idx);
253 setTransactionFlags(eDisplayTransactionNeeded);
254}
255
Jesse Hall692c7232012-11-08 15:41:56 -0800256void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
257 ALOGW_IF(mBuiltinDisplays[type],
258 "Overwriting display token for display type %d", type);
259 mBuiltinDisplays[type] = new BBinder();
260 DisplayDeviceState info(type);
261 // All non-virtual displays are currently considered secure.
262 info.isSecure = true;
263 mCurrentState.displays.add(mBuiltinDisplays[type], info);
264}
265
Mathias Agopiane57f2922012-08-09 16:29:12 -0700266sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700267 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700268 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
269 return NULL;
270 }
Jesse Hall692c7232012-11-08 15:41:56 -0800271 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700272}
273
Jamie Gennis9a78c902011-01-12 18:30:40 -0800274sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
275{
276 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
277 return gba;
278}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700279
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280void SurfaceFlinger::bootFinished()
281{
282 const nsecs_t now = systemTime();
283 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000284 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700285 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700286
287 // wait patiently for the window manager death
288 const String16 name("window");
289 sp<IBinder> window(defaultServiceManager()->getService(name));
290 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700291 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700292 }
293
294 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700295 // formerly we would just kill the process, but we now ask it to exit so it
296 // can choose where to stop the animation.
297 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800298}
299
Mathias Agopian3f844832013-08-07 21:24:32 -0700300void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700301 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700302 RenderEngine& engine;
303 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700304 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700305 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
306 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700307 }
308 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700309 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700310 return true;
311 }
312 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700313 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700314}
315
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700316class DispSyncSource : public VSyncSource, private DispSync::Callback {
317public:
Andy McFadden5167ec62014-05-22 13:08:43 -0700318 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync,
319 const char* label) :
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700320 mValue(0),
321 mPhaseOffset(phaseOffset),
322 mTraceVsync(traceVsync),
Andy McFadden5167ec62014-05-22 13:08:43 -0700323 mVsyncOnLabel(String8::format("VsyncOn-%s", label)),
324 mVsyncEventLabel(String8::format("VSYNC-%s", label)),
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700325 mDispSync(dispSync) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700326
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700327 virtual ~DispSyncSource() {}
328
329 virtual void setVSyncEnabled(bool enable) {
330 // Do NOT lock the mutex here so as to avoid any mutex ordering issues
331 // with locking it in the onDispSyncEvent callback.
332 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700333 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700334 static_cast<DispSync::Callback*>(this));
335 if (err != NO_ERROR) {
336 ALOGE("error registering vsync callback: %s (%d)",
337 strerror(-err), err);
338 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700339 //ATRACE_INT(mVsyncOnLabel.string(), 1);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700340 } else {
341 status_t err = mDispSync->removeEventListener(
342 static_cast<DispSync::Callback*>(this));
343 if (err != NO_ERROR) {
344 ALOGE("error unregistering vsync callback: %s (%d)",
345 strerror(-err), err);
346 }
Andy McFadden5167ec62014-05-22 13:08:43 -0700347 //ATRACE_INT(mVsyncOnLabel.string(), 0);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700348 }
349 }
350
351 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
352 Mutex::Autolock lock(mMutex);
353 mCallback = callback;
354 }
355
356private:
357 virtual void onDispSyncEvent(nsecs_t when) {
358 sp<VSyncSource::Callback> callback;
359 {
360 Mutex::Autolock lock(mMutex);
361 callback = mCallback;
362
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700363 if (mTraceVsync) {
364 mValue = (mValue + 1) % 2;
Andy McFadden5167ec62014-05-22 13:08:43 -0700365 ATRACE_INT(mVsyncEventLabel.string(), mValue);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700366 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700367 }
368
369 if (callback != NULL) {
370 callback->onVSyncEvent(when);
371 }
372 }
373
374 int mValue;
375
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700376 const nsecs_t mPhaseOffset;
377 const bool mTraceVsync;
Andy McFadden5167ec62014-05-22 13:08:43 -0700378 const String8 mVsyncOnLabel;
379 const String8 mVsyncEventLabel;
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700380
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700381 DispSync* mDispSync;
382 sp<VSyncSource::Callback> mCallback;
383 Mutex mMutex;
384};
385
386void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700387 ALOGI( "SurfaceFlinger's main thread ready to run. "
388 "Initializing graphics H/W...");
389
Jesse Hallb65f32e2013-09-27 22:10:47 -0700390 status_t err;
Jesse Hall692c7232012-11-08 15:41:56 -0800391 Mutex::Autolock _l(mStateLock);
392
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700393 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700394 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
395 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700396
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700397 // Initialize the H/W composer object. There may or may not be an
398 // actual hardware composer underneath.
399 mHwc = new HWComposer(this,
400 *static_cast<HWComposer::EventHandler *>(this));
401
Mathias Agopian875d8e12013-06-07 15:35:48 -0700402 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800403 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700404
405 // retrieve the EGL context that was selected/created
406 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700407
Mathias Agopianda27af92012-09-13 18:17:13 -0700408 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
409 "couldn't create EGLContext");
410
Mathias Agopiancde87a32012-09-13 14:09:01 -0700411 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700412 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700413 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700414 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700415 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700416 // All non-virtual displays are currently considered secure.
417 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800418 createBuiltinDisplayLocked(type);
419 wp<IBinder> token = mBuiltinDisplays[i];
420
Dan Stozab9b08832014-03-13 11:55:57 -0700421 sp<IGraphicBufferProducer> producer;
422 sp<IGraphicBufferConsumer> consumer;
423 BufferQueue::createBufferQueue(&producer, &consumer,
424 new GraphicBufferAlloc());
425
426 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
427 consumer);
Jesse Hall19e87292013-12-23 21:02:15 -0800428 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700429 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800430 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Dan Stozab9b08832014-03-13 11:55:57 -0700431 fbs, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -0800432 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700433 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700434 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700435 // for displays other than the main display, so we always
436 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800437 ALOGD("marking display %zu as acquired/unblanked", i);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700438 hw->setPowerMode(HWC_POWER_MODE_NORMAL);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700439 }
440 mDisplays.add(token, hw);
441 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700442 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700443
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700444 // make the GLContext current so that we can create textures when creating Layers
445 // (which may happens before we render something)
446 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
447
Mathias Agopian028508c2012-07-25 21:12:12 -0700448 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700449 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700450 vsyncPhaseOffsetNs, true, "app");
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700451 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700452 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
Andy McFadden5167ec62014-05-22 13:08:43 -0700453 sfVsyncPhaseOffsetNs, true, "sf");
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700454 mSFEventThread = new EventThread(sfVsyncSrc);
455 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700456
Jamie Gennisd1700752013-10-14 12:22:52 -0700457 mEventControlThread = new EventControlThread(this);
458 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
459
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700460 // set a fake vsync period if there is no HWComposer
461 if (mHwc->initCheck() != NO_ERROR) {
462 mPrimaryDispSync.setPeriod(16666667);
463 }
464
Mathias Agopian92a979a2012-08-02 18:32:23 -0700465 // initialize our drawing state
466 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700467
Andy McFadden13a082e2012-08-24 10:16:42 -0700468 // set initial conditions (e.g. unblank default device)
469 initializeDisplays();
470
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700471 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700472 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800473}
474
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700475int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700476 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700477 type : mHwc->allocateDisplayId();
478}
479
Mathias Agopiana67e4182012-06-19 17:26:12 -0700480void SurfaceFlinger::startBootAnim() {
481 // start boot animation
482 property_set("service.bootanim.exit", "0");
483 property_set("ctl.start", "bootanim");
484}
485
Mathias Agopian875d8e12013-06-07 15:35:48 -0700486size_t SurfaceFlinger::getMaxTextureSize() const {
487 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700488}
489
Mathias Agopian875d8e12013-06-07 15:35:48 -0700490size_t SurfaceFlinger::getMaxViewportDims() const {
491 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700492}
493
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800494// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800495
Jamie Gennis582270d2011-08-17 18:19:00 -0700496bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800497 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800498 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800499 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700500 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800501}
502
Dan Stoza7f7da322014-05-02 15:26:25 -0700503status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
504 Vector<DisplayInfo>* configs) {
505 if (configs == NULL) {
506 return BAD_VALUE;
507 }
508
Jesse Hall692c7232012-11-08 15:41:56 -0800509 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700510 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800511 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700512 type = i;
513 break;
514 }
515 }
516
517 if (type < 0) {
518 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700519 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700520
Mathias Agopian8b736f12012-08-13 17:54:26 -0700521 // TODO: Not sure if display density should handled by SF any longer
522 class Density {
523 static int getDensityFromProperty(char const* propName) {
524 char property[PROPERTY_VALUE_MAX];
525 int density = 0;
526 if (property_get(propName, property, NULL) > 0) {
527 density = atoi(property);
528 }
529 return density;
530 }
531 public:
532 static int getEmuDensity() {
533 return getDensityFromProperty("qemu.sf.lcd_density"); }
534 static int getBuildDensity() {
535 return getDensityFromProperty("ro.sf.lcd_density"); }
536 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700537
Dan Stoza7f7da322014-05-02 15:26:25 -0700538 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700539
Dan Stoza7f7da322014-05-02 15:26:25 -0700540 const Vector<HWComposer::DisplayConfig>& hwConfigs =
541 getHwComposer().getConfigs(type);
542 for (size_t c = 0; c < hwConfigs.size(); ++c) {
543 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
544 DisplayInfo info = DisplayInfo();
545
546 float xdpi = hwConfig.xdpi;
547 float ydpi = hwConfig.ydpi;
548
549 if (type == DisplayDevice::DISPLAY_PRIMARY) {
550 // The density of the device is provided by a build property
551 float density = Density::getBuildDensity() / 160.0f;
552 if (density == 0) {
553 // the build doesn't provide a density -- this is wrong!
554 // use xdpi instead
555 ALOGE("ro.sf.lcd_density must be defined as a build property");
556 density = xdpi / 160.0f;
557 }
558 if (Density::getEmuDensity()) {
559 // if "qemu.sf.lcd_density" is specified, it overrides everything
560 xdpi = ydpi = density = Density::getEmuDensity();
561 density /= 160.0f;
562 }
563 info.density = density;
564
565 // TODO: this needs to go away (currently needed only by webkit)
566 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
567 info.orientation = hw->getOrientation();
568 } else {
569 // TODO: where should this value come from?
570 static const int TV_DENSITY = 213;
571 info.density = TV_DENSITY / 160.0f;
572 info.orientation = 0;
573 }
574
575 info.w = hwConfig.width;
576 info.h = hwConfig.height;
577 info.xdpi = xdpi;
578 info.ydpi = ydpi;
579 info.fps = float(1e9 / hwConfig.refresh);
Andy McFadden91b2ca82014-06-13 14:04:23 -0700580 info.appVsyncOffset = VSYNC_EVENT_PHASE_OFFSET_NS;
581
582 // This is how far in advance a buffer must be queued for
583 // presentation at a given time. If you want a buffer to appear
584 // on the screen at time N, you must submit the buffer before
585 // (N - presentationDeadline).
586 //
587 // Normally it's one full refresh period (to give SF a chance to
588 // latch the buffer), but this can be reduced by configuring a
589 // DispSync offset. Any additional delays introduced by the hardware
590 // composer or panel must be accounted for here.
591 //
592 // We add an additional 1ms to allow for processing time and
593 // differences between the ideal and actual refresh rate.
594 info.presentationDeadline =
595 hwConfig.refresh - SF_VSYNC_EVENT_PHASE_OFFSET_NS + 1000000;
Dan Stoza7f7da322014-05-02 15:26:25 -0700596
597 // All non-virtual displays are currently considered secure.
598 info.secure = true;
599
600 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700601 }
602
Dan Stoza7f7da322014-05-02 15:26:25 -0700603 return NO_ERROR;
604}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700605
Andy McFadden91b2ca82014-06-13 14:04:23 -0700606int SurfaceFlinger::getActiveConfig(const sp<IBinder>&) {
Dan Stoza7f7da322014-05-02 15:26:25 -0700607 return 0;
608}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700609
Andy McFadden91b2ca82014-06-13 14:04:23 -0700610status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>&, int) {
Mathias Agopian888c8222012-08-04 21:10:38 -0700611 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700612}
613
Svetoslavd85084b2014-03-20 10:28:31 -0700614status_t SurfaceFlinger::clearAnimationFrameStats() {
615 Mutex::Autolock _l(mStateLock);
616 mAnimFrameTracker.clearStats();
617 return NO_ERROR;
618}
619
620status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
621 Mutex::Autolock _l(mStateLock);
622 mAnimFrameTracker.getStats(outStats);
623 return NO_ERROR;
624}
625
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800626// ----------------------------------------------------------------------------
627
628sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800629 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700630}
631
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800632// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800633
634void SurfaceFlinger::waitForEvent() {
635 mEventQueue.waitMessage();
636}
637
638void SurfaceFlinger::signalTransaction() {
639 mEventQueue.invalidate();
640}
641
642void SurfaceFlinger::signalLayerUpdate() {
643 mEventQueue.invalidate();
644}
645
646void SurfaceFlinger::signalRefresh() {
647 mEventQueue.refresh();
648}
649
650status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800651 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800652 return mEventQueue.postMessage(msg, reltime);
653}
654
655status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800656 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800657 status_t res = mEventQueue.postMessage(msg, reltime);
658 if (res == NO_ERROR) {
659 msg->wait();
660 }
661 return res;
662}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800663
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700664void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700665 do {
666 waitForEvent();
667 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800668}
669
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700670void SurfaceFlinger::enableHardwareVsync() {
671 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700672 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700673 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700674 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
675 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700676 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700677 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700678}
679
Jesse Hall948fe0c2013-10-14 12:56:09 -0700680void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700681 Mutex::Autolock _l(mHWVsyncLock);
682
Jesse Hall948fe0c2013-10-14 12:56:09 -0700683 if (makeAvailable) {
684 mHWVsyncAvailable = true;
685 } else if (!mHWVsyncAvailable) {
686 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
687 return;
688 }
689
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700690 const nsecs_t period =
691 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
692
693 mPrimaryDispSync.reset();
694 mPrimaryDispSync.setPeriod(period);
695
696 if (!mPrimaryHWVsyncEnabled) {
697 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700698 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
699 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700700 mPrimaryHWVsyncEnabled = true;
701 }
702}
703
Jesse Hall948fe0c2013-10-14 12:56:09 -0700704void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700705 Mutex::Autolock _l(mHWVsyncLock);
706 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700707 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
708 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700709 mPrimaryDispSync.endResync();
710 mPrimaryHWVsyncEnabled = false;
711 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700712 if (makeUnavailable) {
713 mHWVsyncAvailable = false;
714 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700715}
716
717void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700718 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700719
Jamie Gennisd1700752013-10-14 12:22:52 -0700720 { // Scope for the lock
721 Mutex::Autolock _l(mHWVsyncLock);
722 if (type == 0 && mPrimaryHWVsyncEnabled) {
723 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700724 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700725 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700726
727 if (needsHwVsync) {
728 enableHardwareVsync();
729 } else {
730 disableHardwareVsync(false);
731 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700732}
733
734void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
735 if (mEventThread == NULL) {
736 // This is a temporary workaround for b/7145521. A non-null pointer
737 // does not mean EventThread has finished initializing, so this
738 // is not a correct fix.
739 ALOGW("WARNING: EventThread not started, ignoring hotplug");
740 return;
741 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700742
Jesse Hall9e663de2013-08-16 14:28:37 -0700743 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700744 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800745 if (connected) {
746 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700747 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800748 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
749 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700750 }
751 setTransactionFlags(eDisplayTransactionNeeded);
752
Andy McFadden9e9689c2012-10-10 18:17:51 -0700753 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700754 }
Mathias Agopian86303202012-07-24 22:46:10 -0700755}
756
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700757void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700758 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700759 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700760}
761
Mathias Agopian4fec8732012-06-29 14:12:52 -0700762void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800763 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800764 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700765 case MessageQueue::TRANSACTION:
766 handleMessageTransaction();
767 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700768 case MessageQueue::INVALIDATE:
769 handleMessageTransaction();
770 handleMessageInvalidate();
771 signalRefresh();
772 break;
773 case MessageQueue::REFRESH:
774 handleMessageRefresh();
775 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800776 }
777}
778
Mathias Agopian4fec8732012-06-29 14:12:52 -0700779void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700780 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700781 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700782 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700783 }
784}
785
786void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700787 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700788 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700789}
790
791void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700792 ATRACE_CALL();
793 preComposition();
794 rebuildLayerStacks();
795 setUpHWComposer();
796 doDebugFlashRegions();
797 doComposition();
798 postComposition();
799}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700800
Mathias Agopiancd60f992012-08-16 16:28:27 -0700801void SurfaceFlinger::doDebugFlashRegions()
802{
803 // is debugging enabled
804 if (CC_LIKELY(!mDebugRegion))
805 return;
806
807 const bool repaintEverything = mRepaintEverything;
808 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
809 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700810 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700811 // transform the dirty region into this screen's coordinate space
812 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
813 if (!dirtyRegion.isEmpty()) {
814 // redraw the whole screen
815 doComposeSurfaces(hw, Region(hw->bounds()));
816
817 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700818 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700819 RenderEngine& engine(getRenderEngine());
820 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
821
Mathias Agopiancd60f992012-08-16 16:28:27 -0700822 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700823 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700824 }
825 }
826 }
827
828 postFramebuffer();
829
830 if (mDebugRegion > 1) {
831 usleep(mDebugRegion * 1000);
832 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700833
834 HWComposer& hwc(getHwComposer());
835 if (hwc.initCheck() == NO_ERROR) {
836 status_t err = hwc.prepare();
837 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
838 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700839}
840
841void SurfaceFlinger::preComposition()
842{
843 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700844 const LayerVector& layers(mDrawingState.layersSortedByZ);
845 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700846 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700847 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700848 needExtraInvalidate = true;
849 }
850 }
851 if (needExtraInvalidate) {
852 signalLayerUpdate();
853 }
854}
855
856void SurfaceFlinger::postComposition()
857{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700858 const LayerVector& layers(mDrawingState.layersSortedByZ);
859 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700860 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700861 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700862 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800863
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700864 const HWComposer& hwc = getHwComposer();
865 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
866
867 if (presentFence->isValid()) {
868 if (mPrimaryDispSync.addPresentFence(presentFence)) {
869 enableHardwareVsync();
870 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700871 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700872 }
873 }
874
Andy McFadden5167ec62014-05-22 13:08:43 -0700875 if (kIgnorePresentFences) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700876 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700877 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700878 enableHardwareVsync();
879 }
880 }
881
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800882 if (mAnimCompositionPending) {
883 mAnimCompositionPending = false;
884
Jesse Halla9a1b002013-02-27 16:39:25 -0800885 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800886 mAnimFrameTracker.setActualPresentFence(presentFence);
887 } else {
888 // The HWC doesn't support present fences, so use the refresh
889 // timestamp instead.
890 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
891 mAnimFrameTracker.setActualPresentTime(presentTime);
892 }
893 mAnimFrameTracker.advanceFrame();
894 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700895}
896
897void SurfaceFlinger::rebuildLayerStacks() {
898 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700899 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700900 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700901 mVisibleRegionsDirty = false;
902 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700903
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700904 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700905 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700906 Region opaqueRegion;
907 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800908 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700909 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700910 const Transform& tr(hw->getTransform());
911 const Rect bounds(hw->getBounds());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700912 if (hw->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700913 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700914 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700915
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700916 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700917 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700918 const sp<Layer>& layer(layers[i]);
919 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700920 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700921 Region drawRegion(tr.transform(
922 layer->visibleNonTransparentRegion));
923 drawRegion.andSelf(bounds);
924 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700925 layersSortedByZ.add(layer);
926 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700927 }
928 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700929 }
Mathias Agopian42977342012-08-05 00:40:46 -0700930 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700931 hw->undefinedRegion.set(bounds);
932 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
933 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700934 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700935 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700936}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700937
Mathias Agopiancd60f992012-08-16 16:28:27 -0700938void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700939 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Dan Stoza71433162014-02-04 16:22:36 -0800940 bool mustRecompose =
941 !(mDisplays[dpy]->getDirtyRegion(false).isEmpty());
942 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700943 }
944
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700945 HWComposer& hwc(getHwComposer());
946 if (hwc.initCheck() == NO_ERROR) {
947 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700948 if (CC_UNLIKELY(mHwWorkListDirty)) {
949 mHwWorkListDirty = false;
950 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
951 sp<const DisplayDevice> hw(mDisplays[dpy]);
952 const int32_t id = hw->getHwcDisplayId();
953 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800954 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700955 hw->getVisibleLayersSortedByZ());
956 const size_t count = currentLayers.size();
957 if (hwc.createWorkList(id, count) == NO_ERROR) {
958 HWComposer::LayerListIterator cur = hwc.begin(id);
959 const HWComposer::LayerListIterator end = hwc.end(id);
960 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800961 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700962 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -0700963 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -0700964 cur->setSkip(true);
965 }
966 }
967 }
968 }
969 }
970 }
971
972 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700973 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700974 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700975 const int32_t id = hw->getHwcDisplayId();
976 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800977 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700978 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700979 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700980 HWComposer::LayerListIterator cur = hwc.begin(id);
981 const HWComposer::LayerListIterator end = hwc.end(id);
982 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
983 /*
984 * update the per-frame h/w composer data for each layer
985 * and build the transparent region of the FB
986 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800987 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700988 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700989 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700990 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700991 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700992
Riley Andrews03414a12014-07-01 14:22:59 -0700993 // If possible, attempt to use the cursor overlay on each display.
994 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
995 sp<const DisplayDevice> hw(mDisplays[dpy]);
996 const int32_t id = hw->getHwcDisplayId();
997 if (id >= 0) {
998 const Vector< sp<Layer> >& currentLayers(
999 hw->getVisibleLayersSortedByZ());
1000 const size_t count = currentLayers.size();
1001 HWComposer::LayerListIterator cur = hwc.begin(id);
1002 const HWComposer::LayerListIterator end = hwc.end(id);
1003 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1004 const sp<Layer>& layer(currentLayers[i]);
1005 if (layer->isPotentialCursor()) {
1006 cur->setIsCursorLayerHint();
1007 break;
1008 }
1009 }
1010 }
1011 }
1012
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001013 status_t err = hwc.prepare();
1014 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001015
1016 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1017 sp<const DisplayDevice> hw(mDisplays[dpy]);
1018 hw->prepareFrame(hwc);
1019 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001020 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001021}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001022
Mathias Agopiancd60f992012-08-16 16:28:27 -07001023void SurfaceFlinger::doComposition() {
1024 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001025 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001026 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001027 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07001028 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001029 // transform the dirty region into this screen's coordinate space
1030 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001031
1032 // repaint the framebuffer (if needed)
1033 doDisplayComposition(hw, dirtyRegion);
1034
Mathias Agopiancd60f992012-08-16 16:28:27 -07001035 hw->dirtyRegion.clear();
1036 hw->flip(hw->swapRegion);
1037 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001038 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001039 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001040 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001041 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001042 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001043}
1044
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001045void SurfaceFlinger::postFramebuffer()
1046{
Mathias Agopian841cde52012-03-01 15:44:37 -08001047 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001048
Mathias Agopiana44b0412011-10-16 18:46:35 -07001049 const nsecs_t now = systemTime();
1050 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001051
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001052 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001053 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001054 if (!hwc.supportsFramebufferTarget()) {
1055 // EGL spec says:
1056 // "surface must be bound to the calling thread's current context,
1057 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001058 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001059 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001060 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001061 }
1062
Mathias Agopian6da15f42013-09-25 20:40:07 -07001063 // make the default display current because the VirtualDisplayDevice code cannot
1064 // deal with dequeueBuffer() being called outside of the composition loop; however
1065 // the code below can call glFlush() which is allowed (and does in some case) call
1066 // dequeueBuffer().
1067 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1068
Mathias Agopian92a979a2012-08-02 18:32:23 -07001069 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001070 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001071 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001072 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001073 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001074 int32_t id = hw->getHwcDisplayId();
1075 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001076 HWComposer::LayerListIterator cur = hwc.begin(id);
1077 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001078 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001079 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001080 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001081 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001082 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001083 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001084 }
Jesse Hallef194142012-06-14 14:45:17 -07001085 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001086 }
1087
Mathias Agopiana44b0412011-10-16 18:46:35 -07001088 mLastSwapBufferTime = systemTime() - now;
1089 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001090
1091 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1092 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1093 logFrameStats();
1094 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095}
1096
Mathias Agopian87baae12012-07-31 12:38:26 -07001097void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001098{
Mathias Agopian841cde52012-03-01 15:44:37 -08001099 ATRACE_CALL();
1100
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001101 // here we keep a copy of the drawing state (that is the state that's
1102 // going to be overwritten by handleTransactionLocked()) outside of
1103 // mStateLock so that the side-effects of the State assignment
1104 // don't happen with mStateLock held (which can cause deadlocks).
1105 State drawingState(mDrawingState);
1106
Mathias Agopianca4d3602011-05-19 15:38:14 -07001107 Mutex::Autolock _l(mStateLock);
1108 const nsecs_t now = systemTime();
1109 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001110
Mathias Agopianca4d3602011-05-19 15:38:14 -07001111 // Here we're guaranteed that some transaction flags are set
1112 // so we can call handleTransactionLocked() unconditionally.
1113 // We call getTransactionFlags(), which will also clear the flags,
1114 // with mStateLock held to guarantee that mCurrentState won't change
1115 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001116
Mathias Agopiane57f2922012-08-09 16:29:12 -07001117 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001118 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001119
Mathias Agopianca4d3602011-05-19 15:38:14 -07001120 mLastTransactionTime = systemTime() - now;
1121 mDebugInTransaction = 0;
1122 invalidateHwcGeometry();
1123 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001124}
1125
Mathias Agopian87baae12012-07-31 12:38:26 -07001126void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001127{
1128 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001129 const size_t count = currentLayers.size();
1130
1131 /*
1132 * Traversal of the children
1133 * (perform the transaction for each of them if needed)
1134 */
1135
Mathias Agopian3559b072012-08-15 13:46:03 -07001136 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001137 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001138 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001139 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1140 if (!trFlags) continue;
1141
1142 const uint32_t flags = layer->doTransaction(0);
1143 if (flags & Layer::eVisibleRegion)
1144 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001145 }
1146 }
1147
1148 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001149 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001150 */
1151
Mathias Agopiane57f2922012-08-09 16:29:12 -07001152 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001153 // here we take advantage of Vector's copy-on-write semantics to
1154 // improve performance by skipping the transaction entirely when
1155 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001156 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1157 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001158 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001159 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001160 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001161 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001162
1163 // find the displays that were removed
1164 // (ie: in drawing state but not in current state)
1165 // also handle displays that changed
1166 // (ie: displays that are in both lists)
1167 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001168 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1169 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001170 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001171 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001172 // Call makeCurrent() on the primary display so we can
1173 // be sure that nothing associated with this display
1174 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001175 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001176 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001177 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1178 if (hw != NULL)
1179 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001180 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001181 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001182 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001183 } else {
1184 ALOGW("trying to remove the main display");
1185 }
1186 } else {
1187 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001188 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001189 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001190 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001191 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001192 // recreating the DisplayDevice, so we just remove it
1193 // from the drawing state, so that it get re-added
1194 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001195 sp<DisplayDevice> hw(getDisplayDevice(display));
1196 if (hw != NULL)
1197 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001198 mDisplays.removeItem(display);
1199 mDrawingState.displays.removeItemsAt(i);
1200 dc--; i--;
1201 // at this point we must loop to the next item
1202 continue;
1203 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001204
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001205 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001206 if (disp != NULL) {
1207 if (state.layerStack != draw[i].layerStack) {
1208 disp->setLayerStack(state.layerStack);
1209 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001210 if ((state.orientation != draw[i].orientation)
1211 || (state.viewport != draw[i].viewport)
1212 || (state.frame != draw[i].frame))
1213 {
1214 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001215 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001216 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001217 }
1218 }
1219 }
1220
1221 // find displays that were added
1222 // (ie: in current state but not in drawing state)
1223 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001224 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001225 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001226
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001227 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001228 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001229 sp<IGraphicBufferProducer> bqProducer;
1230 sp<IGraphicBufferConsumer> bqConsumer;
1231 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1232 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001233
Jesse Hall02d86562013-03-25 14:43:23 -07001234 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001235 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001236 // Virtual displays without a surface are dormant:
1237 // they have external state (layer stack, projection,
1238 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001239 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001240
Jesse Hall02d86562013-03-25 14:43:23 -07001241 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001242 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
Dan Stozab9b08832014-03-13 11:55:57 -07001243 *mHwc, hwcDisplayId, state.surface,
1244 bqProducer, bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001245
1246 dispSurface = vds;
1247 if (hwcDisplayId >= 0) {
1248 producer = vds;
1249 } else {
1250 // There won't be any interaction with HWC for this virtual display,
1251 // so the GLES driver can pass buffers directly to the sink.
1252 producer = state.surface;
1253 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001254 }
1255 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001256 ALOGE_IF(state.surface!=NULL,
1257 "adding a supported display, but rendering "
1258 "surface is provided (%p), ignoring it",
1259 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001260 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001261 // for supported (by hwc) displays we provide our
1262 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001263 dispSurface = new FramebufferSurface(*mHwc, state.type,
1264 bqConsumer);
1265 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001266 }
1267
1268 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001269 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001270 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001271 state.type, hwcDisplayId,
1272 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001273 display, dispSurface, producer,
1274 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001275 hw->setLayerStack(state.layerStack);
1276 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001277 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001278 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001279 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001280 if (state.isVirtualDisplay()) {
1281 if (hwcDisplayId >= 0) {
1282 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1283 hw->getWidth(), hw->getHeight(),
1284 hw->getFormat());
1285 }
1286 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001287 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001288 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001289 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001290 }
1291 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001293 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001294
Mathias Agopian84300952012-11-21 16:02:13 -08001295 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1296 // The transform hint might have changed for some layers
1297 // (either because a display has changed, or because a layer
1298 // as changed).
1299 //
1300 // Walk through all the layers in currentLayers,
1301 // and update their transform hint.
1302 //
1303 // If a layer is visible only on a single display, then that
1304 // display is used to calculate the hint, otherwise we use the
1305 // default display.
1306 //
1307 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1308 // the hint is set before we acquire a buffer from the surface texture.
1309 //
1310 // NOTE: layer transactions have taken place already, so we use their
1311 // drawing state. However, SurfaceFlinger's own transaction has not
1312 // happened yet, so we must use the current state layer list
1313 // (soon to become the drawing state list).
1314 //
1315 sp<const DisplayDevice> disp;
1316 uint32_t currentlayerStack = 0;
1317 for (size_t i=0; i<count; i++) {
1318 // NOTE: we rely on the fact that layers are sorted by
1319 // layerStack first (so we don't have to traverse the list
1320 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001321 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001322 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001323 if (i==0 || currentlayerStack != layerStack) {
1324 currentlayerStack = layerStack;
1325 // figure out if this layerstack is mirrored
1326 // (more than one display) if so, pick the default display,
1327 // if not, pick the only display it's on.
1328 disp.clear();
1329 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1330 sp<const DisplayDevice> hw(mDisplays[dpy]);
1331 if (hw->getLayerStack() == currentlayerStack) {
1332 if (disp == NULL) {
1333 disp = hw;
1334 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001335 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001336 break;
1337 }
1338 }
1339 }
1340 }
Chet Haase91d25932013-04-11 15:24:55 -07001341 if (disp == NULL) {
1342 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1343 // redraw after transform hint changes. See bug 8508397.
1344
1345 // could be null when this layer is using a layerStack
1346 // that is not visible on any display. Also can occur at
1347 // screen off/on times.
1348 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001349 }
Chet Haase91d25932013-04-11 15:24:55 -07001350 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001351 }
1352 }
1353
1354
Mathias Agopian3559b072012-08-15 13:46:03 -07001355 /*
1356 * Perform our own transaction if needed
1357 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001358
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001359 const LayerVector& layers(mDrawingState.layersSortedByZ);
1360 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001361 // layers have been added
1362 mVisibleRegionsDirty = true;
1363 }
1364
1365 // some layers might have been removed, so
1366 // we need to update the regions they're exposing.
1367 if (mLayersRemoved) {
1368 mLayersRemoved = false;
1369 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001370 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001371 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001372 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001373 if (currentLayers.indexOf(layer) < 0) {
1374 // this layer is not visible anymore
1375 // TODO: we could traverse the tree from front to back and
1376 // compute the actual visible region
1377 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001378 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001379 Region visibleReg = s.transform.transform(
1380 Region(Rect(s.active.w, s.active.h)));
1381 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001382 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001384 }
1385
1386 commitTransaction();
Riley Andrews03414a12014-07-01 14:22:59 -07001387
1388 updateCursorAsync();
1389}
1390
1391void SurfaceFlinger::updateCursorAsync()
1392{
1393 HWComposer& hwc(getHwComposer());
1394 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1395 sp<const DisplayDevice> hw(mDisplays[dpy]);
1396 const int32_t id = hw->getHwcDisplayId();
1397 if (id < 0) {
1398 continue;
1399 }
1400 const Vector< sp<Layer> >& currentLayers(
1401 hw->getVisibleLayersSortedByZ());
1402 const size_t count = currentLayers.size();
1403 HWComposer::LayerListIterator cur = hwc.begin(id);
1404 const HWComposer::LayerListIterator end = hwc.end(id);
1405 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1406 if (cur->getCompositionType() != HWC_CURSOR_OVERLAY) {
1407 continue;
1408 }
1409 const sp<Layer>& layer(currentLayers[i]);
1410 Rect cursorPos = layer->getPosition(hw);
1411 hwc.setCursorPositionAsync(id, cursorPos);
1412 break;
1413 }
1414 }
Mathias Agopian4fec8732012-06-29 14:12:52 -07001415}
1416
1417void SurfaceFlinger::commitTransaction()
1418{
1419 if (!mLayersPendingRemoval.isEmpty()) {
1420 // Notify removed layers now that they can't be drawn from
1421 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1422 mLayersPendingRemoval[i]->onRemoved();
1423 }
1424 mLayersPendingRemoval.clear();
1425 }
1426
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001427 // If this transaction is part of a window animation then the next frame
1428 // we composite should be considered an animation as well.
1429 mAnimCompositionPending = mAnimTransactionPending;
1430
Mathias Agopian4fec8732012-06-29 14:12:52 -07001431 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001432 mTransactionPending = false;
1433 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001434 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435}
1436
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001437void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001438 const LayerVector& currentLayers, uint32_t layerStack,
1439 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001440{
Mathias Agopian841cde52012-03-01 15:44:37 -08001441 ATRACE_CALL();
1442
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001443 Region aboveOpaqueLayers;
1444 Region aboveCoveredLayers;
1445 Region dirty;
1446
Mathias Agopian87baae12012-07-31 12:38:26 -07001447 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001448
1449 size_t i = currentLayers.size();
1450 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001451 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001452
1453 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001454 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455
Jesse Hall01e29052013-02-19 16:13:35 -08001456 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001457 if (s.layerStack != layerStack)
1458 continue;
1459
Mathias Agopianab028732010-03-16 16:41:46 -07001460 /*
1461 * opaqueRegion: area of a surface that is fully opaque.
1462 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001464
1465 /*
1466 * visibleRegion: area of a surface that is visible on screen
1467 * and not fully transparent. This is essentially the layer's
1468 * footprint minus the opaque regions above it.
1469 * Areas covered by a translucent surface are considered visible.
1470 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001471 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001472
1473 /*
1474 * coveredRegion: area of a surface that is covered by all
1475 * visible regions above it (which includes the translucent areas).
1476 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001477 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001478
Jesse Halla8026d22012-09-25 13:25:04 -07001479 /*
1480 * transparentRegion: area of a surface that is hinted to be completely
1481 * transparent. This is only used to tell when the layer has no visible
1482 * non-transparent regions and can be removed from the layer list. It
1483 * does not affect the visibleRegion of this layer or any layers
1484 * beneath it. The hint may not be correct if apps don't respect the
1485 * SurfaceView restrictions (which, sadly, some don't).
1486 */
1487 Region transparentRegion;
1488
Mathias Agopianab028732010-03-16 16:41:46 -07001489
1490 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001491 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001492 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001493 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001494 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001495 if (!visibleRegion.isEmpty()) {
1496 // Remove the transparent area from the visible region
1497 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001498 const Transform tr(s.transform);
1499 if (tr.transformed()) {
1500 if (tr.preserveRects()) {
1501 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001502 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001503 } else {
1504 // transformation too complex, can't do the
1505 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001506 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001507 }
1508 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001509 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001510 }
Mathias Agopianab028732010-03-16 16:41:46 -07001511 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001512
Mathias Agopianab028732010-03-16 16:41:46 -07001513 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001514 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001515 if (s.alpha==255 && !translucent &&
1516 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1517 // the opaque region is the layer's footprint
1518 opaqueRegion = visibleRegion;
1519 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001520 }
1521 }
1522
Mathias Agopianab028732010-03-16 16:41:46 -07001523 // Clip the covered region to the visible region
1524 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1525
1526 // Update aboveCoveredLayers for next (lower) layer
1527 aboveCoveredLayers.orSelf(visibleRegion);
1528
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001529 // subtract the opaque region covered by the layers above us
1530 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001531
1532 // compute this layer's dirty region
1533 if (layer->contentDirty) {
1534 // we need to invalidate the whole region
1535 dirty = visibleRegion;
1536 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001537 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001538 layer->contentDirty = false;
1539 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001540 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001541 * the exposed region consists of two components:
1542 * 1) what's VISIBLE now and was COVERED before
1543 * 2) what's EXPOSED now less what was EXPOSED before
1544 *
1545 * note that (1) is conservative, we start with the whole
1546 * visible region but only keep what used to be covered by
1547 * something -- which mean it may have been exposed.
1548 *
1549 * (2) handles areas that were not covered by anything but got
1550 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001551 */
Mathias Agopianab028732010-03-16 16:41:46 -07001552 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001553 const Region oldVisibleRegion = layer->visibleRegion;
1554 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001555 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1556 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001557 }
1558 dirty.subtractSelf(aboveOpaqueLayers);
1559
1560 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001561 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001562
Mathias Agopianab028732010-03-16 16:41:46 -07001563 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001564 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001565
Jesse Halla8026d22012-09-25 13:25:04 -07001566 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001567 layer->setVisibleRegion(visibleRegion);
1568 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001569 layer->setVisibleNonTransparentRegion(
1570 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001571 }
1572
Mathias Agopian87baae12012-07-31 12:38:26 -07001573 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001574}
1575
Mathias Agopian87baae12012-07-31 12:38:26 -07001576void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1577 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001578 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001579 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1580 if (hw->getLayerStack() == layerStack) {
1581 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001582 }
1583 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001584}
1585
1586void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001587{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001588 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001589
Mathias Agopian4fec8732012-06-29 14:12:52 -07001590 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001591 const LayerVector& layers(mDrawingState.layersSortedByZ);
1592 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001593 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001594 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001595 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001596 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001597 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001598 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001599
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001600 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001601}
1602
Mathias Agopianad456f92011-01-13 17:53:01 -08001603void SurfaceFlinger::invalidateHwcGeometry()
1604{
1605 mHwWorkListDirty = true;
1606}
1607
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001608
Mathias Agopiancd60f992012-08-16 16:28:27 -07001609void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001610 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001611{
Dan Stoza71433162014-02-04 16:22:36 -08001612 // We only need to actually compose the display if:
1613 // 1) It is being handled by hardware composer, which may need this to
1614 // keep its virtual display state machine in sync, or
1615 // 2) There is work to be done (the dirty region isn't empty)
1616 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1617 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1618 return;
1619 }
1620
Mathias Agopian87baae12012-07-31 12:38:26 -07001621 Region dirtyRegion(inDirtyRegion);
1622
Mathias Agopianb8a55602009-06-26 19:06:36 -07001623 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001624 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001625
Mathias Agopian42977342012-08-05 00:40:46 -07001626 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001627 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001628 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1629 // takes a rectangle, we must make sure to update that whole
1630 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001631 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001632 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001633 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001634 // We need to redraw the rectangle that will be updated
1635 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001636 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001637 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001638 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001639 } else {
1640 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001641 dirtyRegion.set(hw->bounds());
1642 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001643 }
1644 }
1645
Alan Viverette9c5a3332013-09-12 20:04:35 -07001646 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Mathias Agopianff2ed702013-09-01 21:36:12 -07001647 doComposeSurfaces(hw, dirtyRegion);
1648 } else {
1649 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001650 mat4 colorMatrix = mColorMatrix;
1651 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001652 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001653 }
1654 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001655 doComposeSurfaces(hw, dirtyRegion);
1656 engine.endGroup();
1657 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001658
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001659 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001660 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001661
1662 // swap buffers (presentation)
1663 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001664}
1665
Mathias Agopiancd60f992012-08-16 16:28:27 -07001666void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001667{
Mathias Agopian3f844832013-08-07 21:24:32 -07001668 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001669 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001670 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001671 HWComposer::LayerListIterator cur = hwc.begin(id);
1672 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001673
Jesse Halld05a17f2013-09-30 16:49:30 -07001674 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001675 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001676 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001677 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1678 hw->getDisplayName().string());
1679 return;
1680 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001681
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001682 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001683 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001684 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001685 // when using overlays, we assume a fully transparent framebuffer
1686 // NOTE: we could reduce how much we need to clear, for instance
1687 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001688 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001689 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001690 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001691 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001692 // we start with the whole screen area
1693 const Region bounds(hw->getBounds());
1694
1695 // we remove the scissor part
1696 // we're left with the letterbox region
1697 // (common case is that letterbox ends-up being empty)
1698 const Region letterbox(bounds.subtract(hw->getScissor()));
1699
1700 // compute the area to clear
1701 Region region(hw->undefinedRegion.merge(letterbox));
1702
1703 // but limit it to the dirty region
1704 region.andSelf(dirty);
1705
Mathias Agopianb9494d52012-04-18 02:28:45 -07001706 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001707 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001708 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001709 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001710 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001711 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001712
Mathias Agopian766dc492012-10-30 18:08:06 -07001713 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1714 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001715 // scissor on the main display. It should never be needed
1716 // anyways (though in theory it could since the API allows it).
1717 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001718 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001719 if (scissor != bounds) {
1720 // scissor doesn't match the screen's dimensions, so we
1721 // need to clear everything outside of it and enable
1722 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001723
Mathias Agopianf45c5102012-10-24 16:29:17 -07001724 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001725 const uint32_t height = hw->getHeight();
1726 engine.setScissor(scissor.left, height - scissor.bottom,
1727 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001728 }
1729 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001730 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001731
Mathias Agopian85d751c2012-08-29 16:59:24 -07001732 /*
1733 * and then, render the layers targeted at the framebuffer
1734 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001735
Mathias Agopian13127d82013-03-05 17:47:11 -08001736 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001737 const size_t count = layers.size();
1738 const Transform& tr = hw->getTransform();
1739 if (cur != end) {
1740 // we're using h/w composer
1741 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001742 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001743 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001744 if (!clip.isEmpty()) {
1745 switch (cur->getCompositionType()) {
Riley Andrews03414a12014-07-01 14:22:59 -07001746 case HWC_CURSOR_OVERLAY:
Mathias Agopian85d751c2012-08-29 16:59:24 -07001747 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001748 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001749 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1750 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001751 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001752 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001753 // never clear the very first layer since we're
1754 // guaranteed the FB is already cleared
1755 layer->clearWithOpenGL(hw, clip);
1756 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001757 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001758 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001759 case HWC_FRAMEBUFFER: {
1760 layer->draw(hw, clip);
1761 break;
1762 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001763 case HWC_FRAMEBUFFER_TARGET: {
1764 // this should not happen as the iterator shouldn't
1765 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001766 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001767 break;
1768 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001769 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001770 }
1771 layer->setAcquireFence(hw, *cur);
1772 }
1773 } else {
1774 // we're not using h/w composer
1775 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001776 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001777 const Region clip(dirty.intersect(
1778 tr.transform(layer->visibleRegion)));
1779 if (!clip.isEmpty()) {
1780 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001781 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001782 }
1783 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001784
1785 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001786 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001787}
1788
Mathias Agopian3f844832013-08-07 21:24:32 -07001789void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001790 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001791 RenderEngine& engine(getRenderEngine());
1792 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001793}
1794
Mathias Agopianac9fa422013-02-11 16:40:36 -08001795void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1796 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001797 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001798 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001799{
Mathias Agopian96f08192010-06-02 23:28:45 -07001800 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001801 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001802
Mathias Agopian96f08192010-06-02 23:28:45 -07001803 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001804 Mutex::Autolock _l(mStateLock);
1805 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001806 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001807}
1808
Mathias Agopian3f844832013-08-07 21:24:32 -07001809status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001810 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001811 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001812 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001813 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001814 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001815 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001816 return NO_ERROR;
1817 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001818 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001819}
1820
Dan Stozac7014012014-02-14 15:03:43 -08001821uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001822 return android_atomic_release_load(&mTransactionFlags);
1823}
1824
Mathias Agopian3f844832013-08-07 21:24:32 -07001825uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001826 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1827}
1828
Mathias Agopian3f844832013-08-07 21:24:32 -07001829uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001830 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1831 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001832 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001833 }
1834 return old;
1835}
1836
Mathias Agopian8b33f032012-07-24 20:43:54 -07001837void SurfaceFlinger::setTransactionState(
1838 const Vector<ComposerState>& state,
1839 const Vector<DisplayState>& displays,
1840 uint32_t flags)
1841{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001842 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001843 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001844 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001845
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001846 if (flags & eAnimation) {
1847 // For window updates that are part of an animation we must wait for
1848 // previous animation "frames" to be handled.
1849 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001850 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001851 if (CC_UNLIKELY(err != NO_ERROR)) {
1852 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001853 // caller after a few seconds.
1854 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1855 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001856 mAnimTransactionPending = false;
1857 break;
1858 }
1859 }
1860 }
1861
Mathias Agopiane57f2922012-08-09 16:29:12 -07001862 size_t count = displays.size();
1863 for (size_t i=0 ; i<count ; i++) {
1864 const DisplayState& s(displays[i]);
1865 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001866 }
1867
Mathias Agopiane57f2922012-08-09 16:29:12 -07001868 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001869 for (size_t i=0 ; i<count ; i++) {
1870 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001871 // Here we need to check that the interface we're given is indeed
1872 // one of our own. A malicious client could give us a NULL
1873 // IInterface, or one of its own or even one of our own but a
1874 // different type. All these situations would cause us to crash.
1875 //
1876 // NOTE: it would be better to use RTTI as we could directly check
1877 // that we have a Client*. however, RTTI is disabled in Android.
1878 if (s.client != NULL) {
1879 sp<IBinder> binder = s.client->asBinder();
1880 if (binder != NULL) {
1881 String16 desc(binder->getInterfaceDescriptor());
1882 if (desc == ISurfaceComposerClient::descriptor) {
1883 sp<Client> client( static_cast<Client *>(s.client.get()) );
1884 transactionFlags |= setClientStateLocked(client, s.state);
1885 }
1886 }
1887 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001888 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001889
Mathias Agopian386aa982011-11-07 21:58:03 -08001890 if (transactionFlags) {
1891 // this triggers the transaction
1892 setTransactionFlags(transactionFlags);
1893
1894 // if this is a synchronous transaction, wait for it to take effect
1895 // before returning.
1896 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001897 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001898 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001899 if (flags & eAnimation) {
1900 mAnimTransactionPending = true;
1901 }
1902 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001903 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1904 if (CC_UNLIKELY(err != NO_ERROR)) {
1905 // just in case something goes wrong in SF, return to the
1906 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001907 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1908 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001909 break;
1910 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001911 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001912 }
1913}
1914
Mathias Agopiane57f2922012-08-09 16:29:12 -07001915uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1916{
Jesse Hall9a143922012-10-04 16:29:19 -07001917 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1918 if (dpyIdx < 0)
1919 return 0;
1920
Mathias Agopiane57f2922012-08-09 16:29:12 -07001921 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001922 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001923 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001924 const uint32_t what = s.what;
1925 if (what & DisplayState::eSurfaceChanged) {
1926 if (disp.surface->asBinder() != s.surface->asBinder()) {
1927 disp.surface = s.surface;
1928 flags |= eDisplayTransactionNeeded;
1929 }
1930 }
1931 if (what & DisplayState::eLayerStackChanged) {
1932 if (disp.layerStack != s.layerStack) {
1933 disp.layerStack = s.layerStack;
1934 flags |= eDisplayTransactionNeeded;
1935 }
1936 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001937 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001938 if (disp.orientation != s.orientation) {
1939 disp.orientation = s.orientation;
1940 flags |= eDisplayTransactionNeeded;
1941 }
1942 if (disp.frame != s.frame) {
1943 disp.frame = s.frame;
1944 flags |= eDisplayTransactionNeeded;
1945 }
1946 if (disp.viewport != s.viewport) {
1947 disp.viewport = s.viewport;
1948 flags |= eDisplayTransactionNeeded;
1949 }
1950 }
1951 }
1952 return flags;
1953}
1954
1955uint32_t SurfaceFlinger::setClientStateLocked(
1956 const sp<Client>& client,
1957 const layer_state_t& s)
1958{
1959 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001960 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001961 if (layer != 0) {
1962 const uint32_t what = s.what;
1963 if (what & layer_state_t::ePositionChanged) {
1964 if (layer->setPosition(s.x, s.y))
1965 flags |= eTraversalNeeded;
1966 }
1967 if (what & layer_state_t::eLayerChanged) {
1968 // NOTE: index needs to be calculated before we update the state
1969 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1970 if (layer->setLayer(s.z)) {
1971 mCurrentState.layersSortedByZ.removeAt(idx);
1972 mCurrentState.layersSortedByZ.add(layer);
1973 // we need traversal (state changed)
1974 // AND transaction (list changed)
1975 flags |= eTransactionNeeded|eTraversalNeeded;
1976 }
1977 }
1978 if (what & layer_state_t::eSizeChanged) {
1979 if (layer->setSize(s.w, s.h)) {
1980 flags |= eTraversalNeeded;
1981 }
1982 }
1983 if (what & layer_state_t::eAlphaChanged) {
1984 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1985 flags |= eTraversalNeeded;
1986 }
1987 if (what & layer_state_t::eMatrixChanged) {
1988 if (layer->setMatrix(s.matrix))
1989 flags |= eTraversalNeeded;
1990 }
1991 if (what & layer_state_t::eTransparentRegionChanged) {
1992 if (layer->setTransparentRegionHint(s.transparentRegion))
1993 flags |= eTraversalNeeded;
1994 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08001995 if ((what & layer_state_t::eVisibilityChanged) ||
1996 (what & layer_state_t::eOpacityChanged)) {
1997 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07001998 if (layer->setFlags(s.flags, s.mask))
1999 flags |= eTraversalNeeded;
2000 }
2001 if (what & layer_state_t::eCropChanged) {
2002 if (layer->setCrop(s.crop))
2003 flags |= eTraversalNeeded;
2004 }
2005 if (what & layer_state_t::eLayerStackChanged) {
2006 // NOTE: index needs to be calculated before we update the state
2007 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2008 if (layer->setLayerStack(s.layerStack)) {
2009 mCurrentState.layersSortedByZ.removeAt(idx);
2010 mCurrentState.layersSortedByZ.add(layer);
2011 // we need traversal (state changed)
2012 // AND transaction (list changed)
2013 flags |= eTransactionNeeded|eTraversalNeeded;
2014 }
2015 }
2016 }
2017 return flags;
2018}
2019
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002020status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002021 const String8& name,
2022 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002023 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2024 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002025{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002026 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002027 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002028 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002029 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002030 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002031 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002032
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002033 status_t result = NO_ERROR;
2034
2035 sp<Layer> layer;
2036
Mathias Agopian3165cc22012-08-08 19:42:09 -07002037 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2038 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002039 result = createNormalLayer(client,
2040 name, w, h, flags, format,
2041 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002042 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002043 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002044 result = createDimLayer(client,
2045 name, w, h, flags,
2046 handle, gbp, &layer);
2047 break;
2048 default:
2049 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002050 break;
2051 }
2052
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002053 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002054 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002055 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002056 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002057 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002058}
2059
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002060status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2061 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2062 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002063{
2064 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002065 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002066 case PIXEL_FORMAT_TRANSPARENT:
2067 case PIXEL_FORMAT_TRANSLUCENT:
2068 format = PIXEL_FORMAT_RGBA_8888;
2069 break;
2070 case PIXEL_FORMAT_OPAQUE:
Mathias Agopian8f105402010-04-05 18:01:24 -07002071 format = PIXEL_FORMAT_RGBX_8888;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002072 break;
2073 }
2074
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002075 *outLayer = new Layer(this, client, name, w, h, flags);
2076 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2077 if (err == NO_ERROR) {
2078 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002079 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002080 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002081
2082 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2083 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002084}
2085
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002086status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2087 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2088 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002089{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002090 *outLayer = new LayerDim(this, client, name, w, h, flags);
2091 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002092 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002093 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002094}
2095
Mathias Agopianac9fa422013-02-11 16:40:36 -08002096status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002097{
Mathias Agopian67106042013-03-14 19:18:13 -07002098 // called by the window manager when it wants to remove a Layer
2099 status_t err = NO_ERROR;
2100 sp<Layer> l(client->getLayerUser(handle));
2101 if (l != NULL) {
2102 err = removeLayer(l);
2103 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2104 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002105 }
2106 return err;
2107}
2108
Mathias Agopian13127d82013-03-05 17:47:11 -08002109status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002110{
Mathias Agopian67106042013-03-14 19:18:13 -07002111 // called by ~LayerCleaner() when all references to the IBinder (handle)
2112 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002113 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002114 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002115 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002116 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002117 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002118 "error removing layer=%p (%s)", l.get(), strerror(-err));
2119 }
2120 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002121}
2122
Mathias Agopianb60314a2012-04-10 22:09:54 -07002123// ---------------------------------------------------------------------------
2124
Andy McFadden13a082e2012-08-24 10:16:42 -07002125void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002126 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002127 Vector<ComposerState> state;
2128 Vector<DisplayState> displays;
2129 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002130 d.what = DisplayState::eDisplayProjectionChanged |
2131 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002132 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002133 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002134 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002135 d.frame.makeInvalid();
2136 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002137 displays.add(d);
2138 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002139 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002140
2141 const nsecs_t period =
2142 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2143 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002144}
2145
2146void SurfaceFlinger::initializeDisplays() {
2147 class MessageScreenInitialized : public MessageBase {
2148 SurfaceFlinger* flinger;
2149 public:
2150 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2151 virtual bool handler() {
2152 flinger->onInitializeDisplays();
2153 return true;
2154 }
2155 };
2156 sp<MessageBase> msg = new MessageScreenInitialized(this);
2157 postMessageAsync(msg); // we may be called from main thread, use async message
2158}
2159
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002160void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2161 int mode) {
2162 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2163 this);
2164 int32_t type = hw->getDisplayType();
2165 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002166
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002167 if (mode == currentMode) {
2168 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002169 return;
2170 }
2171
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002172 hw->setPowerMode(mode);
2173 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2174 ALOGW("Trying to set power mode for virtual display");
2175 return;
2176 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002177
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002178 if (currentMode == HWC_POWER_MODE_OFF) {
2179 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002180 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2181 // FIXME: eventthread only knows about the main display right now
2182 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002183 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002184 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002185
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002186 mVisibleRegionsDirty = true;
2187 repaintEverything();
2188 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002189 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002190 disableHardwareVsync(true); // also cancels any in-progress resync
2191
Mathias Agopiancde87a32012-09-13 14:09:01 -07002192 // FIXME: eventthread only knows about the main display right now
2193 mEventThread->onScreenReleased();
2194 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002195
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002196 getHwComposer().setPowerMode(type, mode);
2197 mVisibleRegionsDirty = true;
2198 // from this point on, SF will stop drawing on this display
2199 } else {
2200 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002201 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002202}
2203
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002204void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2205 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002206 SurfaceFlinger& mFlinger;
2207 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002208 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002209 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002210 MessageSetPowerMode(SurfaceFlinger& flinger,
2211 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2212 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002213 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002214 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002215 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002216 ALOGE("Attempt to set power mode = %d for null display %p",
2217 mDisplay.get(), mMode);
Jesse Hall9e663de2013-08-16 14:28:37 -07002218 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002219 ALOGW("Attempt to set power mode = %d for virtual display",
2220 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002221 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002222 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002223 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002224 return true;
2225 }
2226 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002227 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002228 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002229}
2230
2231// ---------------------------------------------------------------------------
2232
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002233status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2234{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002235 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002236
Mathias Agopianbd115332013-04-18 16:41:04 -07002237 IPCThreadState* ipc = IPCThreadState::self();
2238 const int pid = ipc->getCallingPid();
2239 const int uid = ipc->getCallingUid();
2240 if ((uid != AID_SHELL) &&
2241 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002242 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002243 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002244 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002245 // Try to get the main lock, but don't insist if we can't
2246 // (this would indicate SF is stuck, but we want to be able to
2247 // print something in dumpsys).
2248 int retry = 3;
2249 while (mStateLock.tryLock()<0 && --retry>=0) {
2250 usleep(1000000);
2251 }
2252 const bool locked(retry >= 0);
2253 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002254 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002255 "SurfaceFlinger appears to be unresponsive, "
2256 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002257 }
2258
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002259 bool dumpAll = true;
2260 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002261 size_t numArgs = args.size();
2262 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002263 if ((index < numArgs) &&
2264 (args[index] == String16("--list"))) {
2265 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002266 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002267 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002268 }
2269
2270 if ((index < numArgs) &&
2271 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002272 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002273 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002274 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002275 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002276
2277 if ((index < numArgs) &&
2278 (args[index] == String16("--latency-clear"))) {
2279 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002280 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002281 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002282 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002283
2284 if ((index < numArgs) &&
2285 (args[index] == String16("--dispsync"))) {
2286 index++;
2287 mPrimaryDispSync.dump(result);
2288 dumpAll = false;
2289 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002290 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002291
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002292 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002293 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002294 }
2295
Mathias Agopian9795c422009-08-26 16:36:26 -07002296 if (locked) {
2297 mStateLock.unlock();
2298 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002299 }
2300 write(fd, result.string(), result.size());
2301 return NO_ERROR;
2302}
2303
Dan Stozac7014012014-02-14 15:03:43 -08002304void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2305 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002306{
2307 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2308 const size_t count = currentLayers.size();
2309 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002310 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002311 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002312 }
2313}
2314
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002315void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002316 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002317{
2318 String8 name;
2319 if (index < args.size()) {
2320 name = String8(args[index]);
2321 index++;
2322 }
2323
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002324 const nsecs_t period =
2325 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002326 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002327
2328 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002329 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002330 } else {
2331 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2332 const size_t count = currentLayers.size();
2333 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002334 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002335 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002336 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002337 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002338 }
2339 }
2340}
2341
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002342void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002343 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002344{
2345 String8 name;
2346 if (index < args.size()) {
2347 name = String8(args[index]);
2348 index++;
2349 }
2350
2351 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2352 const size_t count = currentLayers.size();
2353 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002354 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002355 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002356 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002357 }
2358 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002359
Svetoslavd85084b2014-03-20 10:28:31 -07002360 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002361}
2362
Jamie Gennis6547ff42013-07-16 20:12:42 -07002363// This should only be called from the main thread. Otherwise it would need
2364// the lock and should use mCurrentState rather than mDrawingState.
2365void SurfaceFlinger::logFrameStats() {
2366 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2367 const size_t count = drawingLayers.size();
2368 for (size_t i=0 ; i<count ; i++) {
2369 const sp<Layer>& layer(drawingLayers[i]);
2370 layer->logFrameStats();
2371 }
2372
2373 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2374}
2375
Andy McFadden4803b742012-09-24 19:07:20 -07002376/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2377{
2378 static const char* config =
2379 " [sf"
Andy McFadden4803b742012-09-24 19:07:20 -07002380#ifdef HAS_CONTEXT_PRIORITY
2381 " HAS_CONTEXT_PRIORITY"
2382#endif
2383#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2384 " NEVER_DEFAULT_TO_ASYNC_MODE"
2385#endif
2386#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2387 " TARGET_DISABLE_TRIPLE_BUFFERING"
2388#endif
2389 "]";
2390 result.append(config);
2391}
2392
Mathias Agopian74d211a2013-04-22 16:55:35 +02002393void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2394 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002395{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002396 bool colorize = false;
2397 if (index < args.size()
2398 && (args[index] == String16("--color"))) {
2399 colorize = true;
2400 index++;
2401 }
2402
2403 Colorizer colorizer(colorize);
2404
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002405 // figure out if we're stuck somewhere
2406 const nsecs_t now = systemTime();
2407 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2408 const nsecs_t inTransaction(mDebugInTransaction);
2409 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2410 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2411
2412 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002413 * Dump library configuration.
2414 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002415
2416 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002417 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002418 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002419 appendSfConfigString(result);
2420 appendUiConfigString(result);
2421 appendGuiConfigString(result);
2422 result.append("\n");
2423
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002424 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002425 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002426 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002427 result.append(SyncFeatures::getInstance().toString());
2428 result.append("\n");
2429
Andy McFadden41d67d72014-04-25 16:58:34 -07002430 colorizer.bold(result);
2431 result.append("DispSync configuration: ");
2432 colorizer.reset(result);
Jesse Hall24cd98e2014-07-13 14:37:16 -07002433 result.appendFormat("app phase %" PRId64 " ns, sf phase %" PRId64 " ns, "
2434 "present offset %d ns (refresh %" PRId64 " ns)",
Andy McFadden41d67d72014-04-25 16:58:34 -07002435 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2436 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2437 result.append("\n");
2438
Andy McFadden4803b742012-09-24 19:07:20 -07002439 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002440 * Dump the visible layer list
2441 */
2442 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2443 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002444 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002445 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002446 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002447 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002448 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002449 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002450 }
2451
2452 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002453 * Dump Display state
2454 */
2455
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002456 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002457 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002458 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002459 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2460 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002461 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002462 }
2463
2464 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002465 * Dump SurfaceFlinger global state
2466 */
2467
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002468 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002469 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002470 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002471
Mathias Agopian888c8222012-08-04 21:10:38 -07002472 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002473 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002474
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002475 colorizer.bold(result);
2476 result.appendFormat("EGL implementation : %s\n",
2477 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2478 colorizer.reset(result);
2479 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002480 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002481
Mathias Agopian875d8e12013-06-07 15:35:48 -07002482 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002483
Mathias Agopian42977342012-08-05 00:40:46 -07002484 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002485 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2486 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002487 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002488 " last eglSwapBuffers() time: %f us\n"
2489 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002490 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002491 " refresh-rate : %f fps\n"
2492 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002493 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002494 " gpu_to_cpu_unsupported : %d\n"
2495 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002496 mLastSwapBufferTime/1000.0,
2497 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002498 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002499 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2500 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002501 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002502 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002503
Mathias Agopian74d211a2013-04-22 16:55:35 +02002504 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002505 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002506
Mathias Agopian74d211a2013-04-22 16:55:35 +02002507 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002508 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002509
2510 /*
2511 * VSYNC state
2512 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002513 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002514
2515 /*
2516 * Dump HWComposer state
2517 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002518 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002519 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002520 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002521 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002522 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002523 (mDebugDisableHWC || mDebugRegion || mDaltonize
2524 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002525 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002526
2527 /*
2528 * Dump gralloc state
2529 */
2530 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2531 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002532}
2533
Mathias Agopian13127d82013-03-05 17:47:11 -08002534const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002535SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002536 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002537 wp<IBinder> dpy;
2538 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2539 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2540 dpy = mDisplays.keyAt(i);
2541 break;
2542 }
2543 }
2544 if (dpy == NULL) {
2545 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2546 // Just use the primary display so we have something to return
2547 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2548 }
2549 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002550}
2551
Keun young Park63f165f2012-08-31 10:53:36 -07002552bool SurfaceFlinger::startDdmConnection()
2553{
2554 void* libddmconnection_dso =
2555 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2556 if (!libddmconnection_dso) {
2557 return false;
2558 }
2559 void (*DdmConnection_start)(const char* name);
2560 DdmConnection_start =
Jesse Hall24cd98e2014-07-13 14:37:16 -07002561 (decltype(DdmConnection_start))dlsym(libddmconnection_dso, "DdmConnection_start");
Keun young Park63f165f2012-08-31 10:53:36 -07002562 if (!DdmConnection_start) {
2563 dlclose(libddmconnection_dso);
2564 return false;
2565 }
2566 (*DdmConnection_start)(getServiceName());
2567 return true;
2568}
2569
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002570status_t SurfaceFlinger::onTransact(
2571 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2572{
2573 switch (code) {
2574 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002575 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002576 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002577 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002578 case CLEAR_ANIMATION_FRAME_STATS:
2579 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002580 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002581 {
2582 // codes that require permission check
2583 IPCThreadState* ipc = IPCThreadState::self();
2584 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002585 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002586 if ((uid != AID_GRAPHICS) &&
2587 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002588 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002589 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2590 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002591 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002592 break;
2593 }
2594 case CAPTURE_SCREEN:
2595 {
2596 // codes that require permission check
2597 IPCThreadState* ipc = IPCThreadState::self();
2598 const int pid = ipc->getCallingPid();
2599 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002600 if ((uid != AID_GRAPHICS) &&
2601 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002602 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002603 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2604 return PERMISSION_DENIED;
2605 }
2606 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002607 }
2608 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002609
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002610 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2611 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002612 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002613 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002614 IPCThreadState* ipc = IPCThreadState::self();
2615 const int pid = ipc->getCallingPid();
2616 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002617 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002618 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002619 return PERMISSION_DENIED;
2620 }
2621 int n;
2622 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002623 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002624 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002625 return NO_ERROR;
2626 case 1002: // SHOW_UPDATES
2627 n = data.readInt32();
2628 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002629 invalidateHwcGeometry();
2630 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002631 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002632 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002633 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002634 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002635 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002636 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002637 setTransactionFlags(
2638 eTransactionNeeded|
2639 eDisplayTransactionNeeded|
2640 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002641 return NO_ERROR;
2642 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002643 case 1006:{ // send empty update
2644 signalRefresh();
2645 return NO_ERROR;
2646 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002647 case 1008: // toggle use of hw composer
2648 n = data.readInt32();
2649 mDebugDisableHWC = n ? 1 : 0;
2650 invalidateHwcGeometry();
2651 repaintEverything();
2652 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002653 case 1009: // toggle use of transform hint
2654 n = data.readInt32();
2655 mDebugDisableTransformHint = n ? 1 : 0;
2656 invalidateHwcGeometry();
2657 repaintEverything();
2658 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002659 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002660 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002661 reply->writeInt32(0);
2662 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002663 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002664 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002665 return NO_ERROR;
2666 case 1013: {
2667 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002668 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2669 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002670 return NO_ERROR;
2671 }
2672 case 1014: {
2673 // daltonize
2674 n = data.readInt32();
2675 switch (n % 10) {
2676 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2677 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2678 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2679 }
2680 if (n >= 10) {
2681 mDaltonizer.setMode(Daltonizer::correction);
2682 } else {
2683 mDaltonizer.setMode(Daltonizer::simulation);
2684 }
2685 mDaltonize = n > 0;
2686 invalidateHwcGeometry();
2687 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002688 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002689 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002690 case 1015: {
2691 // apply a color matrix
2692 n = data.readInt32();
2693 mHasColorMatrix = n ? 1 : 0;
2694 if (n) {
2695 // color matrix is sent as mat3 matrix followed by vec3
2696 // offset, then packed into a mat4 where the last row is
2697 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002698 for (size_t i = 0 ; i < 4; i++) {
2699 for (size_t j = 0; j < 4; j++) {
2700 mColorMatrix[i][j] = data.readFloat();
2701 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002702 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002703 } else {
2704 mColorMatrix = mat4();
2705 }
2706 invalidateHwcGeometry();
2707 repaintEverything();
2708 return NO_ERROR;
2709 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002710 // This is an experimental interface
2711 // Needs to be shifted to proper binder interface when we productize
2712 case 1016: {
Andy McFadden645b1f72014-06-10 14:43:32 -07002713 n = data.readInt32();
2714 mPrimaryDispSync.setRefreshSkipCount(n);
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002715 return NO_ERROR;
2716 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002717 }
2718 }
2719 return err;
2720}
2721
Mathias Agopian53331da2011-08-22 21:44:41 -07002722void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002723 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002724 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002725}
2726
Mathias Agopian59119e62010-10-11 12:37:43 -07002727// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002728// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002729// ---------------------------------------------------------------------------
2730
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002731/* The code below is here to handle b/8734824
2732 *
2733 * We create a IGraphicBufferProducer wrapper that forwards all calls
Jesse Hallb154c422014-07-13 12:47:02 -07002734 * from the surfaceflinger thread to the calling binder thread, where they
2735 * are executed. This allows the calling thread in the calling process to be
2736 * reused and not depend on having "enough" binder threads to handle the
2737 * requests.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002738 */
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002739class GraphicProducerWrapper : public BBinder, public MessageHandler {
Jesse Hallb154c422014-07-13 12:47:02 -07002740 /* Parts of GraphicProducerWrapper are run on two different threads,
2741 * communicating by sending messages via Looper but also by shared member
2742 * data. Coherence maintenance is subtle and in places implicit (ugh).
2743 *
2744 * Don't rely on Looper's sendMessage/handleMessage providing
2745 * release/acquire semantics for any data not actually in the Message.
2746 * Data going from surfaceflinger to binder threads needs to be
2747 * synchronized explicitly.
2748 *
2749 * Barrier open/wait do provide release/acquire semantics. This provides
2750 * implicit synchronization for data coming back from binder to
2751 * surfaceflinger threads.
2752 */
2753
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002754 sp<IGraphicBufferProducer> impl;
2755 sp<Looper> looper;
2756 status_t result;
2757 bool exitPending;
2758 bool exitRequested;
Jesse Hallb154c422014-07-13 12:47:02 -07002759 Barrier barrier;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002760 uint32_t code;
2761 Parcel const* data;
2762 Parcel* reply;
2763
2764 enum {
2765 MSG_API_CALL,
2766 MSG_EXIT
2767 };
2768
2769 /*
Jesse Hallb154c422014-07-13 12:47:02 -07002770 * Called on surfaceflinger thread. This is called by our "fake"
2771 * BpGraphicBufferProducer. We package the data and reply Parcel and
2772 * forward them to the binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002773 */
2774 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08002775 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002776 this->code = code;
2777 this->data = &data;
2778 this->reply = reply;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002779 if (exitPending) {
Jesse Hallb154c422014-07-13 12:47:02 -07002780 // if we've exited, we run the message synchronously right here.
2781 // note (JH): as far as I can tell from looking at the code, this
2782 // never actually happens. if it does, i'm not sure if it happens
2783 // on the surfaceflinger or binder thread.
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002784 handleMessage(Message(MSG_API_CALL));
2785 } else {
2786 barrier.close();
Jesse Hallb154c422014-07-13 12:47:02 -07002787 // Prevent stores to this->{code, data, reply} from being
2788 // reordered later than the construction of Message.
2789 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002790 looper->sendMessage(this, Message(MSG_API_CALL));
2791 barrier.wait();
2792 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08002793 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002794 }
2795
2796 /*
Jesse Hallb154c422014-07-13 12:47:02 -07002797 * here we run on the binder thread. All we've got to do is
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002798 * call the real BpGraphicBufferProducer.
2799 */
2800 virtual void handleMessage(const Message& message) {
Jesse Hallb154c422014-07-13 12:47:02 -07002801 int what = message.what;
2802 // Prevent reads below from happening before the read from Message
2803 atomic_thread_fence(memory_order_acquire);
2804 if (what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08002805 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002806 barrier.open();
Jesse Hallb154c422014-07-13 12:47:02 -07002807 } else if (what == MSG_EXIT) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002808 exitRequested = true;
2809 }
2810 }
2811
2812public:
Jesse Hallb154c422014-07-13 12:47:02 -07002813 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl)
2814 : impl(impl),
2815 looper(new Looper(true)),
2816 exitPending(false),
2817 exitRequested(false)
2818 {}
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002819
Jesse Hallb154c422014-07-13 12:47:02 -07002820 // Binder thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002821 status_t waitForResponse() {
2822 do {
2823 looper->pollOnce(-1);
2824 } while (!exitRequested);
2825 return result;
2826 }
2827
Jesse Hallb154c422014-07-13 12:47:02 -07002828 // Client thread
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002829 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002830 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002831 exitPending = true;
Jesse Hallb154c422014-07-13 12:47:02 -07002832 // Ensure this->result is visible to the binder thread before it
2833 // handles the message.
2834 atomic_thread_fence(memory_order_release);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002835 looper->sendMessage(this, Message(MSG_EXIT));
2836 }
2837};
2838
2839
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002840status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2841 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002842 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002843 uint32_t minLayerZ, uint32_t maxLayerZ,
2844 bool useIdentityTransform) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002845
2846 if (CC_UNLIKELY(display == 0))
2847 return BAD_VALUE;
2848
2849 if (CC_UNLIKELY(producer == 0))
2850 return BAD_VALUE;
2851
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002852 // if we have secure windows on this display, never allow the screen capture
2853 // unless the producer interface is local (i.e.: we can take a screenshot for
2854 // ourselves).
2855 if (!producer->asBinder()->localBinder()) {
2856 Mutex::Autolock _l(mStateLock);
2857 sp<const DisplayDevice> hw(getDisplayDevice(display));
2858 if (hw->getSecureLayerVisible()) {
2859 ALOGW("FB is protected: PERMISSION_DENIED");
2860 return PERMISSION_DENIED;
2861 }
2862 }
2863
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002864 class MessageCaptureScreen : public MessageBase {
2865 SurfaceFlinger* flinger;
2866 sp<IBinder> display;
2867 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07002868 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002869 uint32_t reqWidth, reqHeight;
2870 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08002871 bool useIdentityTransform;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002872 status_t result;
2873 public:
2874 MessageCaptureScreen(SurfaceFlinger* flinger,
2875 const sp<IBinder>& display,
2876 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002877 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002878 uint32_t minLayerZ, uint32_t maxLayerZ,
2879 bool useIdentityTransform)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002880 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07002881 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002882 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08002883 useIdentityTransform(useIdentityTransform),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002884 result(PERMISSION_DENIED)
2885 {
2886 }
2887 status_t getResult() const {
2888 return result;
2889 }
2890 virtual bool handler() {
2891 Mutex::Autolock _l(flinger->mStateLock);
2892 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08002893 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07002894 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Dan Stozac7014012014-02-14 15:03:43 -08002895 useIdentityTransform);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002896 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002897 return true;
2898 }
2899 };
2900
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002901 // make sure to process transactions before screenshots -- a transaction
2902 // might already be pending but scheduled for VSYNC; this guarantees we
2903 // will handle it before the screenshot. When VSYNC finally arrives
2904 // the scheduled transaction will be a no-op. If no transactions are
2905 // scheduled at this time, this will end-up being a no-op as well.
2906 mEventQueue.invalidateTransactionNow();
2907
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002908 // this creates a "fake" BBinder which will serve as a "fake" remote
2909 // binder to receive the marshaled calls and forward them to the
2910 // real remote (a BpGraphicBufferProducer)
2911 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2912
2913 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2914 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002915 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002916 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07002917 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
2918 useIdentityTransform);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002919
2920 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002921 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002922 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002923 }
2924 return res;
2925}
2926
Mathias Agopian180f10d2013-04-10 22:55:41 -07002927
2928void SurfaceFlinger::renderScreenImplLocked(
2929 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07002930 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07002931 uint32_t minLayerZ, uint32_t maxLayerZ,
Dan Stozac7014012014-02-14 15:03:43 -08002932 bool yswap, bool useIdentityTransform)
Mathias Agopian180f10d2013-04-10 22:55:41 -07002933{
2934 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002935 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002936
2937 // get screen geometry
2938 const uint32_t hw_w = hw->getWidth();
2939 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002940 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2941
Dan Stozac1879002014-05-22 15:59:05 -07002942 // if a default or invalid sourceCrop is passed in, set reasonable values
2943 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
2944 !sourceCrop.isValid()) {
2945 sourceCrop.setLeftTop(Point(0, 0));
2946 sourceCrop.setRightBottom(Point(hw_w, hw_h));
2947 }
2948
2949 // ensure that sourceCrop is inside screen
2950 if (sourceCrop.left < 0) {
2951 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
2952 }
Dan Stozabe31f442014-06-11 11:20:54 -07002953 if (sourceCrop.right > hw_w) {
2954 ALOGE("Invalid crop rect: r = %d (> %d)", sourceCrop.right, hw_w);
Dan Stozac1879002014-05-22 15:59:05 -07002955 }
2956 if (sourceCrop.top < 0) {
2957 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
2958 }
Dan Stozabe31f442014-06-11 11:20:54 -07002959 if (sourceCrop.bottom > hw_h) {
2960 ALOGE("Invalid crop rect: b = %d (> %d)", sourceCrop.bottom, hw_h);
Dan Stozac1879002014-05-22 15:59:05 -07002961 }
2962
Mathias Agopian180f10d2013-04-10 22:55:41 -07002963 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002964 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002965
2966 // set-up our viewport
Dan Stozac1879002014-05-22 15:59:05 -07002967 engine.setViewportAndProjection(reqWidth, reqHeight, sourceCrop, hw_h, yswap);
Mathias Agopian3f844832013-08-07 21:24:32 -07002968 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002969
2970 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002971 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002972
2973 const LayerVector& layers( mDrawingState.layersSortedByZ );
2974 const size_t count = layers.size();
2975 for (size_t i=0 ; i<count ; ++i) {
2976 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002977 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002978 if (state.layerStack == hw->getLayerStack()) {
2979 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2980 if (layer->isVisible()) {
2981 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08002982 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002983 if (filtering) layer->setFiltering(false);
2984 }
2985 }
2986 }
2987 }
2988
2989 // compositionComplete is needed for older driver
2990 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002991 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002992}
2993
2994
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002995status_t SurfaceFlinger::captureScreenImplLocked(
2996 const sp<const DisplayDevice>& hw,
2997 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002998 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002999 uint32_t minLayerZ, uint32_t maxLayerZ,
3000 bool useIdentityTransform)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003001{
3002 ATRACE_CALL();
3003
Mathias Agopian180f10d2013-04-10 22:55:41 -07003004 // get screen geometry
3005 const uint32_t hw_w = hw->getWidth();
3006 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003007
Mathias Agopian180f10d2013-04-10 22:55:41 -07003008 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3009 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3010 reqWidth, reqHeight, hw_w, hw_h);
3011 return BAD_VALUE;
3012 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003013
Mathias Agopian180f10d2013-04-10 22:55:41 -07003014 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3015 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3016
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003017 // create a surface (because we're a producer, and we need to
3018 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003019 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003020 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003021
3022 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003023 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003024 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3025 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003026
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003027 int err = 0;
3028 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003029 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003030 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3031 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003032
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003033 if (err == NO_ERROR) {
3034 ANativeWindowBuffer* buffer;
3035 /* TODO: Once we have the sync framework everywhere this can use
3036 * server-side waits on the fence that dequeueBuffer returns.
3037 */
3038 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3039 if (result == NO_ERROR) {
3040 // create an EGLImage from the buffer so we can later
3041 // turn it into a texture
3042 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3043 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3044 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003045 // this binds the given EGLImage as a framebuffer for the
3046 // duration of this scope.
3047 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3048 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003049 // this will in fact render into our dequeued buffer
3050 // via an FBO, which means we didn't have to create
3051 // an EGLSurface and therefore we're not
3052 // dependent on the context's EGLConfig.
Dan Stozac1879002014-05-22 15:59:05 -07003053 renderScreenImplLocked(hw, sourceCrop, reqWidth, reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08003054 minLayerZ, maxLayerZ, true, useIdentityTransform);
Mathias Agopiand5556842013-09-19 17:08:37 -07003055
Andy McFadden2d8d1202013-10-09 16:38:02 -07003056 // Create a sync point and wait on it, so we know the buffer is
3057 // ready before we pass it along. We can't trivially call glFlush(),
3058 // so we use a wait flag instead.
3059 // TODO: pass a sync fd to queueBuffer() and let the consumer wait.
3060 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3061 if (sync != EGL_NO_SYNC_KHR) {
3062 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3063 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3064 EGLint eglErr = eglGetError();
3065 eglDestroySyncKHR(mEGLDisplay, sync);
3066 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3067 ALOGW("captureScreen: fence wait timed out");
3068 } else {
3069 ALOGW_IF(eglErr != EGL_SUCCESS,
3070 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3071 }
3072 } else {
3073 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3074 // not fatal
3075 }
3076
Mathias Agopiand5556842013-09-19 17:08:37 -07003077 if (DEBUG_SCREENSHOTS) {
3078 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3079 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3080 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3081 hw, minLayerZ, maxLayerZ);
3082 delete [] pixels;
3083 }
3084
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003085 } else {
3086 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3087 result = INVALID_OPERATION;
3088 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003089 // destroy our image
3090 eglDestroyImageKHR(mEGLDisplay, image);
3091 } else {
3092 result = BAD_VALUE;
3093 }
3094 window->queueBuffer(window, buffer, -1);
3095 }
3096 } else {
3097 result = BAD_VALUE;
3098 }
3099 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3100 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003101
Mathias Agopian74c40c02010-09-29 13:02:36 -07003102 return result;
3103}
3104
Mathias Agopiand5556842013-09-19 17:08:37 -07003105void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3106 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003107 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003108 for (size_t y=0 ; y<h ; y++) {
3109 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3110 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003111 if (p[x] != 0xFF000000) return;
3112 }
3113 }
3114 ALOGE("*** we just took a black screenshot ***\n"
3115 "requested minz=%d, maxz=%d, layerStack=%d",
3116 minLayerZ, maxLayerZ, hw->getLayerStack());
3117 const LayerVector& layers( mDrawingState.layersSortedByZ );
3118 const size_t count = layers.size();
3119 for (size_t i=0 ; i<count ; ++i) {
3120 const sp<Layer>& layer(layers[i]);
3121 const Layer::State& state(layer->getDrawingState());
3122 const bool visible = (state.layerStack == hw->getLayerStack())
3123 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3124 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003125 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003126 visible ? '+' : '-',
3127 i, layer->getName().string(), state.layerStack, state.z,
3128 layer->isVisible(), state.flags, state.alpha);
3129 }
3130 }
3131}
3132
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003133// ---------------------------------------------------------------------------
3134
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003135SurfaceFlinger::LayerVector::LayerVector() {
3136}
3137
3138SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003139 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003140}
3141
3142int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3143 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003144{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003145 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003146 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3147 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003148
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003149 uint32_t ls = l->getCurrentState().layerStack;
3150 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003151 if (ls != rs)
3152 return ls - rs;
3153
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003154 uint32_t lz = l->getCurrentState().z;
3155 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003156 if (lz != rz)
3157 return lz - rz;
3158
3159 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003160}
3161
3162// ---------------------------------------------------------------------------
3163
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003164SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
3165 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003166}
3167
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003168SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08003169 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003170 viewport.makeInvalid();
3171 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003172}
3173
3174// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003175
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003176}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003177
3178
3179#if defined(__gl_h_)
3180#error "don't include gl/gl.h in this file"
3181#endif
3182
3183#if defined(__gl2_h_)
3184#error "don't include gl2/gl2.h in this file"
3185#endif