blob: 3ae335f893aabdc039980f9893187cfadf02dbdf [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>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070024
25#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <cutils/log.h>
28#include <cutils/properties.h>
29
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070032#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070033#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034
Mathias Agopianc666cae2012-07-25 18:56:13 -070035#include <ui/DisplayInfo.h>
36
Mathias Agopian921e6ac2012-07-23 23:11:29 -070037#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070038#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070039#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070040#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080041#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080042#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070043
44#include <ui/GraphicBufferAllocator.h>
45#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070046#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080047
Mathias Agopiancde87a32012-09-13 14:09:01 -070048#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <utils/String8.h>
50#include <utils/String16.h>
51#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080052#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian921e6ac2012-07-23 23:11:29 -070054#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070055#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopian3e25fd82013-04-22 17:52:16 +020057#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020059#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080060#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070061#include "DisplayDevice.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080062#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066
Mathias Agopiana4912602012-07-12 14:25:33 -070067#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070068#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070069#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070
Mathias Agopianff2ed702013-09-01 21:36:12 -070071#include "Effects/Daltonizer.h"
72
Mathias Agopian875d8e12013-06-07 15:35:48 -070073#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070074#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070075
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076#define DISPLAY_COUNT 1
77
Mathias Agopianfee2b462013-07-03 12:34:01 -070078/*
79 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
80 * black pixels.
81 */
82#define DEBUG_SCREENSHOTS false
83
Mathias Agopianca088332013-03-28 17:44:13 -070084EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
85
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087// ---------------------------------------------------------------------------
88
Mathias Agopian99b49842011-06-27 16:05:52 -070089const String16 sHardwareTest("android.permission.HARDWARE_TEST");
90const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
91const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
92const String16 sDump("android.permission.DUMP");
93
94// ---------------------------------------------------------------------------
95
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -070097 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -070099 mTransactionPending(false),
100 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700101 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700102 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700103 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700106 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800107 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700109 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700110 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700111 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700112 mDebugInSwapBuffers(0),
113 mLastSwapBufferTime(0),
114 mDebugInTransaction(0),
115 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700116 mBootFinished(false),
Alan Viverette9c5a3332013-09-12 20:04:35 -0700117 mDaltonize(false),
118 mHasColorMatrix(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119{
Steve Blocka19954a2012-01-04 20:05:49 +0000120 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121
122 // debugging stuff...
123 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700124
Mathias Agopianb4b17302013-03-20 18:36:41 -0700125 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700126 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700127
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128 property_get("debug.sf.showupdates", value, "0");
129 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700130
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700131 property_get("debug.sf.ddms", value, "0");
132 mDebugDDMS = atoi(value);
133 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700134 if (!startDdmConnection()) {
135 // start failed, and DDMS debugging not enabled
136 mDebugDDMS = 0;
137 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700138 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700139 ALOGI_IF(mDebugRegion, "showupdates enabled");
140 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141}
142
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800143void SurfaceFlinger::onFirstRef()
144{
145 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800146}
147
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148SurfaceFlinger::~SurfaceFlinger()
149{
Mathias Agopiana4912602012-07-12 14:25:33 -0700150 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
151 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
152 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153}
154
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800155void SurfaceFlinger::binderDied(const wp<IBinder>& who)
156{
157 // the window manager died on us. prepare its eulogy.
158
Andy McFadden13a082e2012-08-24 10:16:42 -0700159 // restore initial conditions (default device unblank, etc)
160 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800161
162 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700163 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800164}
165
Mathias Agopian7e27f052010-05-28 14:22:23 -0700166sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167{
Mathias Agopian96f08192010-06-02 23:28:45 -0700168 sp<ISurfaceComposerClient> bclient;
169 sp<Client> client(new Client(this));
170 status_t err = client->initCheck();
171 if (err == NO_ERROR) {
172 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 return bclient;
175}
176
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700177sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
178 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700179{
180 class DisplayToken : public BBinder {
181 sp<SurfaceFlinger> flinger;
182 virtual ~DisplayToken() {
183 // no more references, this display must be terminated
184 Mutex::Autolock _l(flinger->mStateLock);
185 flinger->mCurrentState.displays.removeItem(this);
186 flinger->setTransactionFlags(eDisplayTransactionNeeded);
187 }
188 public:
189 DisplayToken(const sp<SurfaceFlinger>& flinger)
190 : flinger(flinger) {
191 }
192 };
193
194 sp<BBinder> token = new DisplayToken(this);
195
196 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700197 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700198 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700199 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700200 mCurrentState.displays.add(token, info);
201
202 return token;
203}
204
Jesse Hall6c913be2013-08-08 12:15:49 -0700205void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
206 Mutex::Autolock _l(mStateLock);
207
208 ssize_t idx = mCurrentState.displays.indexOfKey(display);
209 if (idx < 0) {
210 ALOGW("destroyDisplay: invalid display token");
211 return;
212 }
213
214 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
215 if (!info.isVirtualDisplay()) {
216 ALOGE("destroyDisplay called for non-virtual display");
217 return;
218 }
219
220 mCurrentState.displays.removeItemsAt(idx);
221 setTransactionFlags(eDisplayTransactionNeeded);
222}
223
Jesse Hall692c7232012-11-08 15:41:56 -0800224void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
225 ALOGW_IF(mBuiltinDisplays[type],
226 "Overwriting display token for display type %d", type);
227 mBuiltinDisplays[type] = new BBinder();
228 DisplayDeviceState info(type);
229 // All non-virtual displays are currently considered secure.
230 info.isSecure = true;
231 mCurrentState.displays.add(mBuiltinDisplays[type], info);
232}
233
Mathias Agopiane57f2922012-08-09 16:29:12 -0700234sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700235 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700236 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
237 return NULL;
238 }
Jesse Hall692c7232012-11-08 15:41:56 -0800239 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700240}
241
Jamie Gennis9a78c902011-01-12 18:30:40 -0800242sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
243{
244 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
245 return gba;
246}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700247
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800248void SurfaceFlinger::bootFinished()
249{
250 const nsecs_t now = systemTime();
251 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000252 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700253 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700254
255 // wait patiently for the window manager death
256 const String16 name("window");
257 sp<IBinder> window(defaultServiceManager()->getService(name));
258 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700259 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700260 }
261
262 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700263 // formerly we would just kill the process, but we now ask it to exit so it
264 // can choose where to stop the animation.
265 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800266}
267
Mathias Agopian3f844832013-08-07 21:24:32 -0700268void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700269 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700270 RenderEngine& engine;
271 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700272 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700273 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
274 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700275 }
276 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700277 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700278 return true;
279 }
280 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700281 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700282}
283
Mathias Agopian722b98f2012-09-25 18:24:31 -0700284status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700285 EGLDisplay dpy,
286 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700287 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700288 EGLConfig* outConfig)
289{
290 EGLConfig config = NULL;
291 EGLint numConfigs = -1, n=0;
292 eglGetConfigs(dpy, NULL, 0, &numConfigs);
293 EGLConfig* const configs = new EGLConfig[numConfigs];
294 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700295
Mathias Agopian722b98f2012-09-25 18:24:31 -0700296 if (n) {
297 if (attribute != EGL_NONE) {
298 for (int i=0 ; i<n ; i++) {
299 EGLint value = 0;
300 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
301 if (wanted == value) {
302 *outConfig = configs[i];
303 delete [] configs;
304 return NO_ERROR;
305 }
306 }
307 } else {
308 // just pick the first one
309 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700310 delete [] configs;
311 return NO_ERROR;
312 }
313 }
314 delete [] configs;
315 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800316}
317
Mathias Agopian722b98f2012-09-25 18:24:31 -0700318class EGLAttributeVector {
319 struct Attribute;
320 class Adder;
321 friend class Adder;
322 KeyedVector<Attribute, EGLint> mList;
323 struct Attribute {
324 Attribute() {};
325 Attribute(EGLint v) : v(v) { }
326 EGLint v;
327 bool operator < (const Attribute& other) const {
328 // this places EGL_NONE at the end
329 EGLint lhs(v);
330 EGLint rhs(other.v);
331 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
332 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
333 return lhs < rhs;
334 }
335 };
336 class Adder {
337 friend class EGLAttributeVector;
338 EGLAttributeVector& v;
339 EGLint attribute;
340 Adder(EGLAttributeVector& v, EGLint attribute)
341 : v(v), attribute(attribute) {
342 }
343 public:
344 void operator = (EGLint value) {
345 if (attribute != EGL_NONE) {
346 v.mList.add(attribute, value);
347 }
348 }
349 operator EGLint () const { return v.mList[attribute]; }
350 };
351public:
352 EGLAttributeVector() {
353 mList.add(EGL_NONE, EGL_NONE);
354 }
355 void remove(EGLint attribute) {
356 if (attribute != EGL_NONE) {
357 mList.removeItem(attribute);
358 }
359 }
360 Adder operator [] (EGLint attribute) {
361 return Adder(*this, attribute);
362 }
363 EGLint operator [] (EGLint attribute) const {
364 return mList[attribute];
365 }
366 // cast-operator to (EGLint const*)
367 operator EGLint const* () const { return &mList.keyAt(0).v; }
368};
369
Jesse Hallb65f32e2013-09-27 22:10:47 -0700370status_t SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId,
371 EGLint renderableType, EGLConfig* config) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700372 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
373 // it is to be used with WIFI displays
Mathias Agopiana4912602012-07-12 14:25:33 -0700374 status_t err;
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700375 EGLint wantedAttribute;
376 EGLint wantedAttributeValue;
Mathias Agopianda27af92012-09-13 18:17:13 -0700377
Mathias Agopian722b98f2012-09-25 18:24:31 -0700378 EGLAttributeVector attribs;
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700379 if (renderableType) {
380 attribs[EGL_RENDERABLE_TYPE] = renderableType;
381 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
382 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT|EGL_PBUFFER_BIT;
383 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
384 attribs[EGL_RED_SIZE] = 8;
385 attribs[EGL_GREEN_SIZE] = 8;
386 attribs[EGL_BLUE_SIZE] = 8;
387 wantedAttribute = EGL_NONE;
388 wantedAttributeValue = EGL_NONE;
Mathias Agopian722b98f2012-09-25 18:24:31 -0700389
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700390 } else {
391 // if no renderable type specified, fallback to a simplified query
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700392 wantedAttribute = EGL_NATIVE_VISUAL_ID;
393 wantedAttributeValue = nativeVisualId;
394 }
395
396 err = selectConfigForAttribute(display, attribs, wantedAttribute,
Jesse Hallb65f32e2013-09-27 22:10:47 -0700397 wantedAttributeValue, config);
398 if (err == NO_ERROR) {
399 EGLint caveat;
400 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
401 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
402 }
403 return err;
Mathias Agopiana4912602012-07-12 14:25:33 -0700404}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800405
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700406void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700407
Mathias Agopiana4912602012-07-12 14:25:33 -0700408 ALOGI( "SurfaceFlinger's main thread ready to run. "
409 "Initializing graphics H/W...");
410
Jesse Hallb65f32e2013-09-27 22:10:47 -0700411 status_t err;
Jesse Hall692c7232012-11-08 15:41:56 -0800412 Mutex::Autolock _l(mStateLock);
413
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700414 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700415 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
416 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700417
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700418 // Initialize the H/W composer object. There may or may not be an
419 // actual hardware composer underneath.
420 mHwc = new HWComposer(this,
421 *static_cast<HWComposer::EventHandler *>(this));
422
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700423 // First try to get an ES2 config
Jesse Hallb65f32e2013-09-27 22:10:47 -0700424 err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(), EGL_OPENGL_ES2_BIT,
425 &mEGLConfig);
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700426
Jesse Hallb65f32e2013-09-27 22:10:47 -0700427 if (err != NO_ERROR) {
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700428 // If ES2 fails, try ES1
Jesse Hallb65f32e2013-09-27 22:10:47 -0700429 err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(),
430 EGL_OPENGL_ES_BIT, &mEGLConfig);
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700431 }
432
Jesse Hallb65f32e2013-09-27 22:10:47 -0700433 if (err != NO_ERROR) {
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700434 // still didn't work, probably because we're on the emulator...
435 // try a simplified query
436 ALOGW("no suitable EGLConfig found, trying a simpler query");
Jesse Hallb65f32e2013-09-27 22:10:47 -0700437 err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(), 0, &mEGLConfig);
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700438 }
439
Jesse Hallb65f32e2013-09-27 22:10:47 -0700440 if (err != NO_ERROR) {
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700441 // this EGL is too lame for android
442 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
443 }
Mathias Agopian875d8e12013-06-07 15:35:48 -0700444
445 // print some debugging info
446 EGLint r,g,b,a;
447 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_RED_SIZE, &r);
448 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_GREEN_SIZE, &g);
449 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_BLUE_SIZE, &b);
450 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_ALPHA_SIZE, &a);
451 ALOGI("EGL informations:");
452 ALOGI("vendor : %s", eglQueryString(mEGLDisplay, EGL_VENDOR));
453 ALOGI("version : %s", eglQueryString(mEGLDisplay, EGL_VERSION));
454 ALOGI("extensions: %s", eglQueryString(mEGLDisplay, EGL_EXTENSIONS));
455 ALOGI("Client API: %s", eglQueryString(mEGLDisplay, EGL_CLIENT_APIS)?:"Not Supported");
456 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
457
458 // get a RenderEngine for the given display / config (can't fail)
459 mRenderEngine = RenderEngine::create(mEGLDisplay, mEGLConfig);
460
461 // retrieve the EGL context that was selected/created
462 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700463
Mathias Agopianed985572013-03-22 00:24:39 -0700464 // figure out which format we got
465 eglGetConfigAttrib(mEGLDisplay, mEGLConfig,
466 EGL_NATIVE_VISUAL_ID, &mEGLNativeVisualId);
467
Mathias Agopianda27af92012-09-13 18:17:13 -0700468 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
469 "couldn't create EGLContext");
470
Mathias Agopiancde87a32012-09-13 14:09:01 -0700471 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700472 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700473 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700474 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700475 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700476 // All non-virtual displays are currently considered secure.
477 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800478 createBuiltinDisplayLocked(type);
479 wp<IBinder> token = mBuiltinDisplays[i];
480
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700481 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
482 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i, bq);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700483 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700484 type, allocateHwcDisplayId(type), isSecure, token,
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700485 fbs, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700486 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700487 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700488 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700489 // for displays other than the main display, so we always
490 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700491 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700492 hw->acquireScreen();
493 }
494 mDisplays.add(token, hw);
495 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700496 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700497
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700498 // make the GLContext current so that we can create textures when creating Layers
499 // (which may happens before we render something)
500 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
501
Mathias Agopian028508c2012-07-25 21:12:12 -0700502 // start the EventThread
503 mEventThread = new EventThread(this);
504 mEventQueue.setEventThread(mEventThread);
505
Mathias Agopian92a979a2012-08-02 18:32:23 -0700506 // initialize our drawing state
507 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700508
Andy McFadden13a082e2012-08-24 10:16:42 -0700509 // set initial conditions (e.g. unblank default device)
510 initializeDisplays();
511
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700512 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700513 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800514}
515
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700516int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700517 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700518 type : mHwc->allocateDisplayId();
519}
520
Mathias Agopiana67e4182012-06-19 17:26:12 -0700521void SurfaceFlinger::startBootAnim() {
522 // start boot animation
523 property_set("service.bootanim.exit", "0");
524 property_set("ctl.start", "bootanim");
525}
526
Mathias Agopian875d8e12013-06-07 15:35:48 -0700527size_t SurfaceFlinger::getMaxTextureSize() const {
528 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700529}
530
Mathias Agopian875d8e12013-06-07 15:35:48 -0700531size_t SurfaceFlinger::getMaxViewportDims() const {
532 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700533}
534
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800535// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800536
Jamie Gennis582270d2011-08-17 18:19:00 -0700537bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800538 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800539 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800540 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700541 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800542}
543
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700544status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800545 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700546 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800547 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700548 type = i;
549 break;
550 }
551 }
552
553 if (type < 0) {
554 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700555 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700556
557 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700558 float xdpi = hwc.getDpiX(type);
559 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700560
561 // TODO: Not sure if display density should handled by SF any longer
562 class Density {
563 static int getDensityFromProperty(char const* propName) {
564 char property[PROPERTY_VALUE_MAX];
565 int density = 0;
566 if (property_get(propName, property, NULL) > 0) {
567 density = atoi(property);
568 }
569 return density;
570 }
571 public:
572 static int getEmuDensity() {
573 return getDensityFromProperty("qemu.sf.lcd_density"); }
574 static int getBuildDensity() {
575 return getDensityFromProperty("ro.sf.lcd_density"); }
576 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700577
578 if (type == DisplayDevice::DISPLAY_PRIMARY) {
579 // The density of the device is provided by a build property
580 float density = Density::getBuildDensity() / 160.0f;
581 if (density == 0) {
582 // the build doesn't provide a density -- this is wrong!
583 // use xdpi instead
584 ALOGE("ro.sf.lcd_density must be defined as a build property");
585 density = xdpi / 160.0f;
586 }
587 if (Density::getEmuDensity()) {
588 // if "qemu.sf.lcd_density" is specified, it overrides everything
589 xdpi = ydpi = density = Density::getEmuDensity();
590 density /= 160.0f;
591 }
592 info->density = density;
593
594 // TODO: this needs to go away (currently needed only by webkit)
595 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
596 info->orientation = hw->getOrientation();
Mathias Agopian1604f772012-09-18 21:54:42 -0700597 } else {
598 // TODO: where should this value come from?
599 static const int TV_DENSITY = 213;
600 info->density = TV_DENSITY / 160.0f;
601 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700602 }
603
Mathias Agopian1604f772012-09-18 21:54:42 -0700604 info->w = hwc.getWidth(type);
605 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700606 info->xdpi = xdpi;
607 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700608 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700609
610 // All non-virtual displays are currently considered secure.
611 info->secure = true;
612
Mathias Agopian888c8222012-08-04 21:10:38 -0700613 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700614}
615
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800616// ----------------------------------------------------------------------------
617
618sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800619 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700620}
621
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800622// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800623
624void SurfaceFlinger::waitForEvent() {
625 mEventQueue.waitMessage();
626}
627
628void SurfaceFlinger::signalTransaction() {
629 mEventQueue.invalidate();
630}
631
632void SurfaceFlinger::signalLayerUpdate() {
633 mEventQueue.invalidate();
634}
635
636void SurfaceFlinger::signalRefresh() {
637 mEventQueue.refresh();
638}
639
640status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
641 nsecs_t reltime, uint32_t flags) {
642 return mEventQueue.postMessage(msg, reltime);
643}
644
645status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
646 nsecs_t reltime, uint32_t flags) {
647 status_t res = mEventQueue.postMessage(msg, reltime);
648 if (res == NO_ERROR) {
649 msg->wait();
650 }
651 return res;
652}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800653
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700654void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700655 do {
656 waitForEvent();
657 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800658}
659
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700660void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700661 if (mEventThread == NULL) {
662 // This is a temporary workaround for b/7145521. A non-null pointer
663 // does not mean EventThread has finished initializing, so this
664 // is not a correct fix.
665 ALOGW("WARNING: EventThread not started, ignoring vsync");
666 return;
667 }
Jesse Hall9e663de2013-08-16 14:28:37 -0700668 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700669 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700670 mEventThread->onVSyncReceived(type, timestamp);
671 }
672}
673
674void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
675 if (mEventThread == NULL) {
676 // This is a temporary workaround for b/7145521. A non-null pointer
677 // does not mean EventThread has finished initializing, so this
678 // is not a correct fix.
679 ALOGW("WARNING: EventThread not started, ignoring hotplug");
680 return;
681 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700682
Jesse Hall9e663de2013-08-16 14:28:37 -0700683 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700684 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800685 if (connected) {
686 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700687 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800688 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
689 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700690 }
691 setTransactionFlags(eDisplayTransactionNeeded);
692
Andy McFadden9e9689c2012-10-10 18:17:51 -0700693 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700694 }
Mathias Agopian86303202012-07-24 22:46:10 -0700695}
696
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700697void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
698 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700699}
700
Mathias Agopian4fec8732012-06-29 14:12:52 -0700701void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800702 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800703 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700704 case MessageQueue::TRANSACTION:
705 handleMessageTransaction();
706 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700707 case MessageQueue::INVALIDATE:
708 handleMessageTransaction();
709 handleMessageInvalidate();
710 signalRefresh();
711 break;
712 case MessageQueue::REFRESH:
713 handleMessageRefresh();
714 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800715 }
716}
717
Mathias Agopian4fec8732012-06-29 14:12:52 -0700718void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700719 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700720 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700721 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700722 }
723}
724
725void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700726 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700727 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700728}
729
730void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700731 ATRACE_CALL();
732 preComposition();
733 rebuildLayerStacks();
734 setUpHWComposer();
735 doDebugFlashRegions();
736 doComposition();
737 postComposition();
738}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700739
Mathias Agopiancd60f992012-08-16 16:28:27 -0700740void SurfaceFlinger::doDebugFlashRegions()
741{
742 // is debugging enabled
743 if (CC_LIKELY(!mDebugRegion))
744 return;
745
746 const bool repaintEverything = mRepaintEverything;
747 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
748 const sp<DisplayDevice>& hw(mDisplays[dpy]);
749 if (hw->canDraw()) {
750 // transform the dirty region into this screen's coordinate space
751 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
752 if (!dirtyRegion.isEmpty()) {
753 // redraw the whole screen
754 doComposeSurfaces(hw, Region(hw->bounds()));
755
756 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700757 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700758 RenderEngine& engine(getRenderEngine());
759 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
760
Mathias Agopiancd60f992012-08-16 16:28:27 -0700761 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700762 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700763 }
764 }
765 }
766
767 postFramebuffer();
768
769 if (mDebugRegion > 1) {
770 usleep(mDebugRegion * 1000);
771 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700772
773 HWComposer& hwc(getHwComposer());
774 if (hwc.initCheck() == NO_ERROR) {
775 status_t err = hwc.prepare();
776 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
777 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700778}
779
780void SurfaceFlinger::preComposition()
781{
782 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700783 const LayerVector& layers(mDrawingState.layersSortedByZ);
784 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700785 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700786 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700787 needExtraInvalidate = true;
788 }
789 }
790 if (needExtraInvalidate) {
791 signalLayerUpdate();
792 }
793}
794
795void SurfaceFlinger::postComposition()
796{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700797 const LayerVector& layers(mDrawingState.layersSortedByZ);
798 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700799 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700800 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700801 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800802
803 if (mAnimCompositionPending) {
804 mAnimCompositionPending = false;
805
806 const HWComposer& hwc = getHwComposer();
807 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800808 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800809 mAnimFrameTracker.setActualPresentFence(presentFence);
810 } else {
811 // The HWC doesn't support present fences, so use the refresh
812 // timestamp instead.
813 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
814 mAnimFrameTracker.setActualPresentTime(presentTime);
815 }
816 mAnimFrameTracker.advanceFrame();
817 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700818}
819
820void SurfaceFlinger::rebuildLayerStacks() {
821 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700822 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700823 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700824 mVisibleRegionsDirty = false;
825 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700826
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700827 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700828 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700829 Region opaqueRegion;
830 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800831 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700832 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700833 const Transform& tr(hw->getTransform());
834 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700835 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700836 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700837 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700838
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700839 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700840 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700841 const sp<Layer>& layer(layers[i]);
842 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700843 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700844 Region drawRegion(tr.transform(
845 layer->visibleNonTransparentRegion));
846 drawRegion.andSelf(bounds);
847 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700848 layersSortedByZ.add(layer);
849 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700850 }
851 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700852 }
Mathias Agopian42977342012-08-05 00:40:46 -0700853 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700854 hw->undefinedRegion.set(bounds);
855 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
856 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700857 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700858 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700859}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700860
Mathias Agopiancd60f992012-08-16 16:28:27 -0700861void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700862 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
863 mDisplays[dpy]->beginFrame();
864 }
865
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700866 HWComposer& hwc(getHwComposer());
867 if (hwc.initCheck() == NO_ERROR) {
868 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700869 if (CC_UNLIKELY(mHwWorkListDirty)) {
870 mHwWorkListDirty = false;
871 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
872 sp<const DisplayDevice> hw(mDisplays[dpy]);
873 const int32_t id = hw->getHwcDisplayId();
874 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800875 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700876 hw->getVisibleLayersSortedByZ());
877 const size_t count = currentLayers.size();
878 if (hwc.createWorkList(id, count) == NO_ERROR) {
879 HWComposer::LayerListIterator cur = hwc.begin(id);
880 const HWComposer::LayerListIterator end = hwc.end(id);
881 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800882 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700883 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -0700884 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -0700885 cur->setSkip(true);
886 }
887 }
888 }
889 }
890 }
891 }
892
893 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700894 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700895 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700896 const int32_t id = hw->getHwcDisplayId();
897 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800898 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700899 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700900 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700901 HWComposer::LayerListIterator cur = hwc.begin(id);
902 const HWComposer::LayerListIterator end = hwc.end(id);
903 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
904 /*
905 * update the per-frame h/w composer data for each layer
906 * and build the transparent region of the FB
907 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800908 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700909 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700910 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700911 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700912 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700913
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700914 status_t err = hwc.prepare();
915 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700916
917 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
918 sp<const DisplayDevice> hw(mDisplays[dpy]);
919 hw->prepareFrame(hwc);
920 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700921 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700922}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700923
Mathias Agopiancd60f992012-08-16 16:28:27 -0700924void SurfaceFlinger::doComposition() {
925 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700926 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700927 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700928 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700929 if (hw->canDraw()) {
930 // transform the dirty region into this screen's coordinate space
931 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800932
933 // repaint the framebuffer (if needed)
934 doDisplayComposition(hw, dirtyRegion);
935
Mathias Agopiancd60f992012-08-16 16:28:27 -0700936 hw->dirtyRegion.clear();
937 hw->flip(hw->swapRegion);
938 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700939 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700940 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700941 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700942 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700943 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700944}
945
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800946void SurfaceFlinger::postFramebuffer()
947{
Mathias Agopian841cde52012-03-01 15:44:37 -0800948 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800949
Mathias Agopiana44b0412011-10-16 18:46:35 -0700950 const nsecs_t now = systemTime();
951 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700952
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700953 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700954 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700955 if (!hwc.supportsFramebufferTarget()) {
956 // EGL spec says:
957 // "surface must be bound to the calling thread's current context,
958 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700959 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700960 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700961 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700962 }
963
Mathias Agopian6da15f42013-09-25 20:40:07 -0700964 // make the default display current because the VirtualDisplayDevice code cannot
965 // deal with dequeueBuffer() being called outside of the composition loop; however
966 // the code below can call glFlush() which is allowed (and does in some case) call
967 // dequeueBuffer().
968 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
969
Mathias Agopian92a979a2012-08-02 18:32:23 -0700970 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700971 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800972 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700973 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700974 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700975 int32_t id = hw->getHwcDisplayId();
976 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700977 HWComposer::LayerListIterator cur = hwc.begin(id);
978 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700979 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700980 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700981 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700982 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700983 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700984 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700985 }
Jesse Hallef194142012-06-14 14:45:17 -0700986 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800987 }
988
Mathias Agopiana44b0412011-10-16 18:46:35 -0700989 mLastSwapBufferTime = systemTime() - now;
990 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700991
992 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
993 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
994 logFrameStats();
995 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996}
997
Mathias Agopian87baae12012-07-31 12:38:26 -0700998void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800999{
Mathias Agopian841cde52012-03-01 15:44:37 -08001000 ATRACE_CALL();
1001
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001002 // here we keep a copy of the drawing state (that is the state that's
1003 // going to be overwritten by handleTransactionLocked()) outside of
1004 // mStateLock so that the side-effects of the State assignment
1005 // don't happen with mStateLock held (which can cause deadlocks).
1006 State drawingState(mDrawingState);
1007
Mathias Agopianca4d3602011-05-19 15:38:14 -07001008 Mutex::Autolock _l(mStateLock);
1009 const nsecs_t now = systemTime();
1010 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011
Mathias Agopianca4d3602011-05-19 15:38:14 -07001012 // Here we're guaranteed that some transaction flags are set
1013 // so we can call handleTransactionLocked() unconditionally.
1014 // We call getTransactionFlags(), which will also clear the flags,
1015 // with mStateLock held to guarantee that mCurrentState won't change
1016 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001017
Mathias Agopiane57f2922012-08-09 16:29:12 -07001018 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001019 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001020
Mathias Agopianca4d3602011-05-19 15:38:14 -07001021 mLastTransactionTime = systemTime() - now;
1022 mDebugInTransaction = 0;
1023 invalidateHwcGeometry();
1024 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001025}
1026
Mathias Agopian87baae12012-07-31 12:38:26 -07001027void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001028{
1029 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001030 const size_t count = currentLayers.size();
1031
1032 /*
1033 * Traversal of the children
1034 * (perform the transaction for each of them if needed)
1035 */
1036
Mathias Agopian3559b072012-08-15 13:46:03 -07001037 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001039 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1041 if (!trFlags) continue;
1042
1043 const uint32_t flags = layer->doTransaction(0);
1044 if (flags & Layer::eVisibleRegion)
1045 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046 }
1047 }
1048
1049 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001050 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051 */
1052
Mathias Agopiane57f2922012-08-09 16:29:12 -07001053 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001054 // here we take advantage of Vector's copy-on-write semantics to
1055 // improve performance by skipping the transaction entirely when
1056 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001057 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1058 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001059 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001061 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001062 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001063
1064 // find the displays that were removed
1065 // (ie: in drawing state but not in current state)
1066 // also handle displays that changed
1067 // (ie: displays that are in both lists)
1068 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001069 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1070 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001071 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001072 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001073 // Call makeCurrent() on the primary display so we can
1074 // be sure that nothing associated with this display
1075 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001076 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001077 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001078 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1079 if (hw != NULL)
1080 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001081 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001082 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001083 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001084 } else {
1085 ALOGW("trying to remove the main display");
1086 }
1087 } else {
1088 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001089 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001090 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001091 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001092 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001093 // recreating the DisplayDevice, so we just remove it
1094 // from the drawing state, so that it get re-added
1095 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001096 sp<DisplayDevice> hw(getDisplayDevice(display));
1097 if (hw != NULL)
1098 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001099 mDisplays.removeItem(display);
1100 mDrawingState.displays.removeItemsAt(i);
1101 dc--; i--;
1102 // at this point we must loop to the next item
1103 continue;
1104 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001105
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001106 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001107 if (disp != NULL) {
1108 if (state.layerStack != draw[i].layerStack) {
1109 disp->setLayerStack(state.layerStack);
1110 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001111 if ((state.orientation != draw[i].orientation)
1112 || (state.viewport != draw[i].viewport)
1113 || (state.frame != draw[i].frame))
1114 {
1115 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001116 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001117 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001118 }
1119 }
1120 }
1121
1122 // find displays that were added
1123 // (ie: in current state but not in drawing state)
1124 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001125 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001126 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001127
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001128 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001129 sp<IGraphicBufferProducer> producer;
1130 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
1131
Jesse Hall02d86562013-03-25 14:43:23 -07001132 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001133 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001134 // Virtual displays without a surface are dormant:
1135 // they have external state (layer stack, projection,
1136 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001137 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001138
Jesse Hall02d86562013-03-25 14:43:23 -07001139 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001140 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1141 *mHwc, hwcDisplayId, state.surface, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001142 state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001143
1144 dispSurface = vds;
1145 if (hwcDisplayId >= 0) {
1146 producer = vds;
1147 } else {
1148 // There won't be any interaction with HWC for this virtual display,
1149 // so the GLES driver can pass buffers directly to the sink.
1150 producer = state.surface;
1151 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001152 }
1153 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001154 ALOGE_IF(state.surface!=NULL,
1155 "adding a supported display, but rendering "
1156 "surface is provided (%p), ignoring it",
1157 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001158 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001159 // for supported (by hwc) displays we provide our
1160 // own rendering surface
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001161 dispSurface = new FramebufferSurface(*mHwc, state.type, bq);
1162 producer = bq;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001163 }
1164
1165 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001166 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001167 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001168 state.type, hwcDisplayId, state.isSecure,
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001169 display, dispSurface, producer, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001170 hw->setLayerStack(state.layerStack);
1171 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001172 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001173 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001174 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001175 if (state.isVirtualDisplay()) {
1176 if (hwcDisplayId >= 0) {
1177 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1178 hw->getWidth(), hw->getHeight(),
1179 hw->getFormat());
1180 }
1181 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001182 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001183 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001184 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001185 }
1186 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001188 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189
Mathias Agopian84300952012-11-21 16:02:13 -08001190 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1191 // The transform hint might have changed for some layers
1192 // (either because a display has changed, or because a layer
1193 // as changed).
1194 //
1195 // Walk through all the layers in currentLayers,
1196 // and update their transform hint.
1197 //
1198 // If a layer is visible only on a single display, then that
1199 // display is used to calculate the hint, otherwise we use the
1200 // default display.
1201 //
1202 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1203 // the hint is set before we acquire a buffer from the surface texture.
1204 //
1205 // NOTE: layer transactions have taken place already, so we use their
1206 // drawing state. However, SurfaceFlinger's own transaction has not
1207 // happened yet, so we must use the current state layer list
1208 // (soon to become the drawing state list).
1209 //
1210 sp<const DisplayDevice> disp;
1211 uint32_t currentlayerStack = 0;
1212 for (size_t i=0; i<count; i++) {
1213 // NOTE: we rely on the fact that layers are sorted by
1214 // layerStack first (so we don't have to traverse the list
1215 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001216 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001217 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001218 if (i==0 || currentlayerStack != layerStack) {
1219 currentlayerStack = layerStack;
1220 // figure out if this layerstack is mirrored
1221 // (more than one display) if so, pick the default display,
1222 // if not, pick the only display it's on.
1223 disp.clear();
1224 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1225 sp<const DisplayDevice> hw(mDisplays[dpy]);
1226 if (hw->getLayerStack() == currentlayerStack) {
1227 if (disp == NULL) {
1228 disp = hw;
1229 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001230 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001231 break;
1232 }
1233 }
1234 }
1235 }
Chet Haase91d25932013-04-11 15:24:55 -07001236 if (disp == NULL) {
1237 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1238 // redraw after transform hint changes. See bug 8508397.
1239
1240 // could be null when this layer is using a layerStack
1241 // that is not visible on any display. Also can occur at
1242 // screen off/on times.
1243 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001244 }
Chet Haase91d25932013-04-11 15:24:55 -07001245 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001246 }
1247 }
1248
1249
Mathias Agopian3559b072012-08-15 13:46:03 -07001250 /*
1251 * Perform our own transaction if needed
1252 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001253
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001254 const LayerVector& layers(mDrawingState.layersSortedByZ);
1255 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001256 // layers have been added
1257 mVisibleRegionsDirty = true;
1258 }
1259
1260 // some layers might have been removed, so
1261 // we need to update the regions they're exposing.
1262 if (mLayersRemoved) {
1263 mLayersRemoved = false;
1264 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001265 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001266 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001267 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001268 if (currentLayers.indexOf(layer) < 0) {
1269 // this layer is not visible anymore
1270 // TODO: we could traverse the tree from front to back and
1271 // compute the actual visible region
1272 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001273 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001274 Region visibleReg = s.transform.transform(
1275 Region(Rect(s.active.w, s.active.h)));
1276 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001277 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001278 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279 }
1280
1281 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001282}
1283
1284void SurfaceFlinger::commitTransaction()
1285{
1286 if (!mLayersPendingRemoval.isEmpty()) {
1287 // Notify removed layers now that they can't be drawn from
1288 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1289 mLayersPendingRemoval[i]->onRemoved();
1290 }
1291 mLayersPendingRemoval.clear();
1292 }
1293
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001294 // If this transaction is part of a window animation then the next frame
1295 // we composite should be considered an animation as well.
1296 mAnimCompositionPending = mAnimTransactionPending;
1297
Mathias Agopian4fec8732012-06-29 14:12:52 -07001298 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001299 mTransactionPending = false;
1300 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001301 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302}
1303
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001305 const LayerVector& currentLayers, uint32_t layerStack,
1306 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307{
Mathias Agopian841cde52012-03-01 15:44:37 -08001308 ATRACE_CALL();
1309
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310 Region aboveOpaqueLayers;
1311 Region aboveCoveredLayers;
1312 Region dirty;
1313
Mathias Agopian87baae12012-07-31 12:38:26 -07001314 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315
1316 size_t i = currentLayers.size();
1317 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001318 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001319
1320 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001321 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322
Jesse Hall01e29052013-02-19 16:13:35 -08001323 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001324 if (s.layerStack != layerStack)
1325 continue;
1326
Mathias Agopianab028732010-03-16 16:41:46 -07001327 /*
1328 * opaqueRegion: area of a surface that is fully opaque.
1329 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001330 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001331
1332 /*
1333 * visibleRegion: area of a surface that is visible on screen
1334 * and not fully transparent. This is essentially the layer's
1335 * footprint minus the opaque regions above it.
1336 * Areas covered by a translucent surface are considered visible.
1337 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001338 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001339
1340 /*
1341 * coveredRegion: area of a surface that is covered by all
1342 * visible regions above it (which includes the translucent areas).
1343 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001345
Jesse Halla8026d22012-09-25 13:25:04 -07001346 /*
1347 * transparentRegion: area of a surface that is hinted to be completely
1348 * transparent. This is only used to tell when the layer has no visible
1349 * non-transparent regions and can be removed from the layer list. It
1350 * does not affect the visibleRegion of this layer or any layers
1351 * beneath it. The hint may not be correct if apps don't respect the
1352 * SurfaceView restrictions (which, sadly, some don't).
1353 */
1354 Region transparentRegion;
1355
Mathias Agopianab028732010-03-16 16:41:46 -07001356
1357 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001358 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001359 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001360 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001361 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001362 if (!visibleRegion.isEmpty()) {
1363 // Remove the transparent area from the visible region
1364 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001365 const Transform tr(s.transform);
1366 if (tr.transformed()) {
1367 if (tr.preserveRects()) {
1368 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001369 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001370 } else {
1371 // transformation too complex, can't do the
1372 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001373 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001374 }
1375 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001376 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001377 }
Mathias Agopianab028732010-03-16 16:41:46 -07001378 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001379
Mathias Agopianab028732010-03-16 16:41:46 -07001380 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001381 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001382 if (s.alpha==255 && !translucent &&
1383 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1384 // the opaque region is the layer's footprint
1385 opaqueRegion = visibleRegion;
1386 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001387 }
1388 }
1389
Mathias Agopianab028732010-03-16 16:41:46 -07001390 // Clip the covered region to the visible region
1391 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1392
1393 // Update aboveCoveredLayers for next (lower) layer
1394 aboveCoveredLayers.orSelf(visibleRegion);
1395
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396 // subtract the opaque region covered by the layers above us
1397 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398
1399 // compute this layer's dirty region
1400 if (layer->contentDirty) {
1401 // we need to invalidate the whole region
1402 dirty = visibleRegion;
1403 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001404 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405 layer->contentDirty = false;
1406 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001407 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001408 * the exposed region consists of two components:
1409 * 1) what's VISIBLE now and was COVERED before
1410 * 2) what's EXPOSED now less what was EXPOSED before
1411 *
1412 * note that (1) is conservative, we start with the whole
1413 * visible region but only keep what used to be covered by
1414 * something -- which mean it may have been exposed.
1415 *
1416 * (2) handles areas that were not covered by anything but got
1417 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001418 */
Mathias Agopianab028732010-03-16 16:41:46 -07001419 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001420 const Region oldVisibleRegion = layer->visibleRegion;
1421 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001422 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1423 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424 }
1425 dirty.subtractSelf(aboveOpaqueLayers);
1426
1427 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001428 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001429
Mathias Agopianab028732010-03-16 16:41:46 -07001430 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001432
Jesse Halla8026d22012-09-25 13:25:04 -07001433 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001434 layer->setVisibleRegion(visibleRegion);
1435 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001436 layer->setVisibleNonTransparentRegion(
1437 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001438 }
1439
Mathias Agopian87baae12012-07-31 12:38:26 -07001440 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001441}
1442
Mathias Agopian87baae12012-07-31 12:38:26 -07001443void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1444 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001445 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001446 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1447 if (hw->getLayerStack() == layerStack) {
1448 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001449 }
1450 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001451}
1452
1453void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001454{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001455 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001456
Mathias Agopian4fec8732012-06-29 14:12:52 -07001457 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001458 const LayerVector& layers(mDrawingState.layersSortedByZ);
1459 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001460 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001461 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001462 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001463 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001464 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001465 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001466
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001467 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001468}
1469
Mathias Agopianad456f92011-01-13 17:53:01 -08001470void SurfaceFlinger::invalidateHwcGeometry()
1471{
1472 mHwWorkListDirty = true;
1473}
1474
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001475
Mathias Agopiancd60f992012-08-16 16:28:27 -07001476void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001477 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001478{
Mathias Agopian87baae12012-07-31 12:38:26 -07001479 Region dirtyRegion(inDirtyRegion);
1480
Mathias Agopianb8a55602009-06-26 19:06:36 -07001481 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001482 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483
Mathias Agopian42977342012-08-05 00:40:46 -07001484 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001485 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001486 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1487 // takes a rectangle, we must make sure to update that whole
1488 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001489 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001490 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001491 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001492 // We need to redraw the rectangle that will be updated
1493 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001494 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001495 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001496 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001497 } else {
1498 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001499 dirtyRegion.set(hw->bounds());
1500 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001501 }
1502 }
1503
Alan Viverette9c5a3332013-09-12 20:04:35 -07001504 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Mathias Agopianff2ed702013-09-01 21:36:12 -07001505 doComposeSurfaces(hw, dirtyRegion);
1506 } else {
1507 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001508 mat4 colorMatrix = mColorMatrix;
1509 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001510 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001511 }
1512 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001513 doComposeSurfaces(hw, dirtyRegion);
1514 engine.endGroup();
1515 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001516
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001517 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001518 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001519
1520 // swap buffers (presentation)
1521 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001522}
1523
Mathias Agopiancd60f992012-08-16 16:28:27 -07001524void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001525{
Mathias Agopian3f844832013-08-07 21:24:32 -07001526 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001527 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001528 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001529 HWComposer::LayerListIterator cur = hwc.begin(id);
1530 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001531
Jesse Halld05a17f2013-09-30 16:49:30 -07001532 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001533 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001534 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001535 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1536 hw->getDisplayName().string());
1537 return;
1538 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001539
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001540 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001541 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001542 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001543 // when using overlays, we assume a fully transparent framebuffer
1544 // NOTE: we could reduce how much we need to clear, for instance
1545 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001546 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001547 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001548 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001549 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001550 // we start with the whole screen area
1551 const Region bounds(hw->getBounds());
1552
1553 // we remove the scissor part
1554 // we're left with the letterbox region
1555 // (common case is that letterbox ends-up being empty)
1556 const Region letterbox(bounds.subtract(hw->getScissor()));
1557
1558 // compute the area to clear
1559 Region region(hw->undefinedRegion.merge(letterbox));
1560
1561 // but limit it to the dirty region
1562 region.andSelf(dirty);
1563
Mathias Agopianb9494d52012-04-18 02:28:45 -07001564 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001565 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001566 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001567 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001568 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001569 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001570
Mathias Agopian766dc492012-10-30 18:08:06 -07001571 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1572 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001573 // scissor on the main display. It should never be needed
1574 // anyways (though in theory it could since the API allows it).
1575 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001576 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001577 if (scissor != bounds) {
1578 // scissor doesn't match the screen's dimensions, so we
1579 // need to clear everything outside of it and enable
1580 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001581
Mathias Agopianf45c5102012-10-24 16:29:17 -07001582 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001583 const uint32_t height = hw->getHeight();
1584 engine.setScissor(scissor.left, height - scissor.bottom,
1585 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001586 }
1587 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001588 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001589
Mathias Agopian85d751c2012-08-29 16:59:24 -07001590 /*
1591 * and then, render the layers targeted at the framebuffer
1592 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001593
Mathias Agopian13127d82013-03-05 17:47:11 -08001594 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001595 const size_t count = layers.size();
1596 const Transform& tr = hw->getTransform();
1597 if (cur != end) {
1598 // we're using h/w composer
1599 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001600 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001601 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001602 if (!clip.isEmpty()) {
1603 switch (cur->getCompositionType()) {
1604 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001605 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001606 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1607 && i
Mathias Agopianac683022013-10-01 15:36:52 -07001608 && layer->isOpaque() && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001609 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001610 // never clear the very first layer since we're
1611 // guaranteed the FB is already cleared
1612 layer->clearWithOpenGL(hw, clip);
1613 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001614 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001615 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001616 case HWC_FRAMEBUFFER: {
1617 layer->draw(hw, clip);
1618 break;
1619 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001620 case HWC_FRAMEBUFFER_TARGET: {
1621 // this should not happen as the iterator shouldn't
1622 // let us get there.
1623 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1624 break;
1625 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001626 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001627 }
1628 layer->setAcquireFence(hw, *cur);
1629 }
1630 } else {
1631 // we're not using h/w composer
1632 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001633 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001634 const Region clip(dirty.intersect(
1635 tr.transform(layer->visibleRegion)));
1636 if (!clip.isEmpty()) {
1637 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001638 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001639 }
1640 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001641
1642 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001643 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001644}
1645
Mathias Agopian3f844832013-08-07 21:24:32 -07001646void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001647 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001648 RenderEngine& engine(getRenderEngine());
1649 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001650}
1651
Mathias Agopianac9fa422013-02-11 16:40:36 -08001652void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1653 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001654 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001655 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001656{
Mathias Agopian96f08192010-06-02 23:28:45 -07001657 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001658 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001659
Mathias Agopian96f08192010-06-02 23:28:45 -07001660 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001661 Mutex::Autolock _l(mStateLock);
1662 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001663 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001664}
1665
Mathias Agopian3f844832013-08-07 21:24:32 -07001666status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001668 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001669 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001670 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001671 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001672 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001673 return NO_ERROR;
1674 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001675 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001676}
1677
Mathias Agopian3f844832013-08-07 21:24:32 -07001678uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001679 return android_atomic_release_load(&mTransactionFlags);
1680}
1681
Mathias Agopian3f844832013-08-07 21:24:32 -07001682uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1684}
1685
Mathias Agopian3f844832013-08-07 21:24:32 -07001686uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1688 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001689 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001690 }
1691 return old;
1692}
1693
Mathias Agopian8b33f032012-07-24 20:43:54 -07001694void SurfaceFlinger::setTransactionState(
1695 const Vector<ComposerState>& state,
1696 const Vector<DisplayState>& displays,
1697 uint32_t flags)
1698{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001699 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001700 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001701 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001702
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001703 if (flags & eAnimation) {
1704 // For window updates that are part of an animation we must wait for
1705 // previous animation "frames" to be handled.
1706 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001707 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001708 if (CC_UNLIKELY(err != NO_ERROR)) {
1709 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001710 // caller after a few seconds.
1711 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1712 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001713 mAnimTransactionPending = false;
1714 break;
1715 }
1716 }
1717 }
1718
Mathias Agopiane57f2922012-08-09 16:29:12 -07001719 size_t count = displays.size();
1720 for (size_t i=0 ; i<count ; i++) {
1721 const DisplayState& s(displays[i]);
1722 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001723 }
1724
Mathias Agopiane57f2922012-08-09 16:29:12 -07001725 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001726 for (size_t i=0 ; i<count ; i++) {
1727 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001728 // Here we need to check that the interface we're given is indeed
1729 // one of our own. A malicious client could give us a NULL
1730 // IInterface, or one of its own or even one of our own but a
1731 // different type. All these situations would cause us to crash.
1732 //
1733 // NOTE: it would be better to use RTTI as we could directly check
1734 // that we have a Client*. however, RTTI is disabled in Android.
1735 if (s.client != NULL) {
1736 sp<IBinder> binder = s.client->asBinder();
1737 if (binder != NULL) {
1738 String16 desc(binder->getInterfaceDescriptor());
1739 if (desc == ISurfaceComposerClient::descriptor) {
1740 sp<Client> client( static_cast<Client *>(s.client.get()) );
1741 transactionFlags |= setClientStateLocked(client, s.state);
1742 }
1743 }
1744 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001745 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001746
Mathias Agopian386aa982011-11-07 21:58:03 -08001747 if (transactionFlags) {
1748 // this triggers the transaction
1749 setTransactionFlags(transactionFlags);
1750
1751 // if this is a synchronous transaction, wait for it to take effect
1752 // before returning.
1753 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001754 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001755 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001756 if (flags & eAnimation) {
1757 mAnimTransactionPending = true;
1758 }
1759 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001760 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1761 if (CC_UNLIKELY(err != NO_ERROR)) {
1762 // just in case something goes wrong in SF, return to the
1763 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001764 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1765 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001766 break;
1767 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001768 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001769 }
1770}
1771
Mathias Agopiane57f2922012-08-09 16:29:12 -07001772uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1773{
Jesse Hall9a143922012-10-04 16:29:19 -07001774 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1775 if (dpyIdx < 0)
1776 return 0;
1777
Mathias Agopiane57f2922012-08-09 16:29:12 -07001778 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001779 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001780 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001781 const uint32_t what = s.what;
1782 if (what & DisplayState::eSurfaceChanged) {
1783 if (disp.surface->asBinder() != s.surface->asBinder()) {
1784 disp.surface = s.surface;
1785 flags |= eDisplayTransactionNeeded;
1786 }
1787 }
1788 if (what & DisplayState::eLayerStackChanged) {
1789 if (disp.layerStack != s.layerStack) {
1790 disp.layerStack = s.layerStack;
1791 flags |= eDisplayTransactionNeeded;
1792 }
1793 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001794 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001795 if (disp.orientation != s.orientation) {
1796 disp.orientation = s.orientation;
1797 flags |= eDisplayTransactionNeeded;
1798 }
1799 if (disp.frame != s.frame) {
1800 disp.frame = s.frame;
1801 flags |= eDisplayTransactionNeeded;
1802 }
1803 if (disp.viewport != s.viewport) {
1804 disp.viewport = s.viewport;
1805 flags |= eDisplayTransactionNeeded;
1806 }
1807 }
1808 }
1809 return flags;
1810}
1811
1812uint32_t SurfaceFlinger::setClientStateLocked(
1813 const sp<Client>& client,
1814 const layer_state_t& s)
1815{
1816 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001817 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001818 if (layer != 0) {
1819 const uint32_t what = s.what;
1820 if (what & layer_state_t::ePositionChanged) {
1821 if (layer->setPosition(s.x, s.y))
1822 flags |= eTraversalNeeded;
1823 }
1824 if (what & layer_state_t::eLayerChanged) {
1825 // NOTE: index needs to be calculated before we update the state
1826 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1827 if (layer->setLayer(s.z)) {
1828 mCurrentState.layersSortedByZ.removeAt(idx);
1829 mCurrentState.layersSortedByZ.add(layer);
1830 // we need traversal (state changed)
1831 // AND transaction (list changed)
1832 flags |= eTransactionNeeded|eTraversalNeeded;
1833 }
1834 }
1835 if (what & layer_state_t::eSizeChanged) {
1836 if (layer->setSize(s.w, s.h)) {
1837 flags |= eTraversalNeeded;
1838 }
1839 }
1840 if (what & layer_state_t::eAlphaChanged) {
1841 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1842 flags |= eTraversalNeeded;
1843 }
1844 if (what & layer_state_t::eMatrixChanged) {
1845 if (layer->setMatrix(s.matrix))
1846 flags |= eTraversalNeeded;
1847 }
1848 if (what & layer_state_t::eTransparentRegionChanged) {
1849 if (layer->setTransparentRegionHint(s.transparentRegion))
1850 flags |= eTraversalNeeded;
1851 }
1852 if (what & layer_state_t::eVisibilityChanged) {
1853 if (layer->setFlags(s.flags, s.mask))
1854 flags |= eTraversalNeeded;
1855 }
1856 if (what & layer_state_t::eCropChanged) {
1857 if (layer->setCrop(s.crop))
1858 flags |= eTraversalNeeded;
1859 }
1860 if (what & layer_state_t::eLayerStackChanged) {
1861 // NOTE: index needs to be calculated before we update the state
1862 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1863 if (layer->setLayerStack(s.layerStack)) {
1864 mCurrentState.layersSortedByZ.removeAt(idx);
1865 mCurrentState.layersSortedByZ.add(layer);
1866 // we need traversal (state changed)
1867 // AND transaction (list changed)
1868 flags |= eTransactionNeeded|eTraversalNeeded;
1869 }
1870 }
1871 }
1872 return flags;
1873}
1874
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001875status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001876 const String8& name,
1877 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001878 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1879 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001880{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001881 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001882 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001883 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001884 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001885 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001886 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001887
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001888 status_t result = NO_ERROR;
1889
1890 sp<Layer> layer;
1891
Mathias Agopian3165cc22012-08-08 19:42:09 -07001892 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1893 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001894 result = createNormalLayer(client,
1895 name, w, h, flags, format,
1896 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001897 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001898 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001899 result = createDimLayer(client,
1900 name, w, h, flags,
1901 handle, gbp, &layer);
1902 break;
1903 default:
1904 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001905 break;
1906 }
1907
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001908 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001909 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001910 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001911 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001912 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001913}
1914
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001915status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1916 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1917 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001918{
1919 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001920 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001921 case PIXEL_FORMAT_TRANSPARENT:
1922 case PIXEL_FORMAT_TRANSLUCENT:
1923 format = PIXEL_FORMAT_RGBA_8888;
1924 break;
1925 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001926#ifdef NO_RGBX_8888
1927 format = PIXEL_FORMAT_RGB_565;
1928#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001929 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001930#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001931 break;
1932 }
1933
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001934#ifdef NO_RGBX_8888
1935 if (format == PIXEL_FORMAT_RGBX_8888)
1936 format = PIXEL_FORMAT_RGBA_8888;
1937#endif
1938
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001939 *outLayer = new Layer(this, client, name, w, h, flags);
1940 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1941 if (err == NO_ERROR) {
1942 *handle = (*outLayer)->getHandle();
1943 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001944 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001945
1946 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1947 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001948}
1949
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001950status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1951 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1952 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001953{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001954 *outLayer = new LayerDim(this, client, name, w, h, flags);
1955 *handle = (*outLayer)->getHandle();
1956 *gbp = (*outLayer)->getBufferQueue();
1957 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001958}
1959
Mathias Agopianac9fa422013-02-11 16:40:36 -08001960status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001961{
Mathias Agopian67106042013-03-14 19:18:13 -07001962 // called by the window manager when it wants to remove a Layer
1963 status_t err = NO_ERROR;
1964 sp<Layer> l(client->getLayerUser(handle));
1965 if (l != NULL) {
1966 err = removeLayer(l);
1967 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1968 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001969 }
1970 return err;
1971}
1972
Mathias Agopian13127d82013-03-05 17:47:11 -08001973status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001974{
Mathias Agopian67106042013-03-14 19:18:13 -07001975 // called by ~LayerCleaner() when all references to the IBinder (handle)
1976 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001977 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001978 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001979 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07001980 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001981 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001982 "error removing layer=%p (%s)", l.get(), strerror(-err));
1983 }
1984 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001985}
1986
Mathias Agopianb60314a2012-04-10 22:09:54 -07001987// ---------------------------------------------------------------------------
1988
Andy McFadden13a082e2012-08-24 10:16:42 -07001989void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08001990 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07001991 Vector<ComposerState> state;
1992 Vector<DisplayState> displays;
1993 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08001994 d.what = DisplayState::eDisplayProjectionChanged |
1995 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08001996 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08001997 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07001998 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001999 d.frame.makeInvalid();
2000 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002001 displays.add(d);
2002 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002003 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07002004
2005 const nsecs_t period =
2006 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2007 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002008}
2009
2010void SurfaceFlinger::initializeDisplays() {
2011 class MessageScreenInitialized : public MessageBase {
2012 SurfaceFlinger* flinger;
2013 public:
2014 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2015 virtual bool handler() {
2016 flinger->onInitializeDisplays();
2017 return true;
2018 }
2019 };
2020 sp<MessageBase> msg = new MessageScreenInitialized(this);
2021 postMessageAsync(msg); // we may be called from main thread, use async message
2022}
2023
2024
Mathias Agopiancde87a32012-09-13 14:09:01 -07002025void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002026 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2027 if (hw->isScreenAcquired()) {
2028 // this is expected, e.g. when power manager wakes up during boot
2029 ALOGD(" screen was previously acquired");
2030 return;
2031 }
2032
Mathias Agopian42977342012-08-05 00:40:46 -07002033 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002034 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002035 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002036 // built-in display, tell the HWC
2037 getHwComposer().acquire(type);
2038
2039 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2040 // FIXME: eventthread only knows about the main display right now
2041 mEventThread->onScreenAcquired();
2042 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002043 }
Mathias Agopian20128302012-08-13 18:32:13 -07002044 mVisibleRegionsDirty = true;
2045 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002046}
2047
Mathias Agopiancde87a32012-09-13 14:09:01 -07002048void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002049 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2050 if (!hw->isScreenAcquired()) {
2051 ALOGD(" screen was previously released");
2052 return;
2053 }
2054
2055 hw->releaseScreen();
2056 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002057 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002058 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002059 // FIXME: eventthread only knows about the main display right now
2060 mEventThread->onScreenReleased();
2061 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002062
2063 // built-in display, tell the HWC
2064 getHwComposer().release(type);
2065 }
2066 mVisibleRegionsDirty = true;
2067 // from this point on, SF will stop drawing on this display
2068}
2069
2070void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2071 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002072 SurfaceFlinger& mFlinger;
2073 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002074 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002075 MessageScreenAcquired(SurfaceFlinger& flinger,
2076 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002077 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002078 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2079 if (hw == NULL) {
2080 ALOGE("Attempt to unblank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002081 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002082 ALOGW("Attempt to unblank virtual display");
2083 } else {
2084 mFlinger.onScreenAcquired(hw);
2085 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002086 return true;
2087 }
2088 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002089 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2090 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002091}
2092
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002093void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002094 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002095 SurfaceFlinger& mFlinger;
2096 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002097 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002098 MessageScreenReleased(SurfaceFlinger& flinger,
2099 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002100 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002101 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2102 if (hw == NULL) {
2103 ALOGE("Attempt to blank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002104 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002105 ALOGW("Attempt to blank virtual display");
2106 } else {
2107 mFlinger.onScreenReleased(hw);
2108 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002109 return true;
2110 }
2111 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002112 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2113 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002114}
2115
2116// ---------------------------------------------------------------------------
2117
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002118status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2119{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002120 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002121
Mathias Agopianbd115332013-04-18 16:41:04 -07002122 IPCThreadState* ipc = IPCThreadState::self();
2123 const int pid = ipc->getCallingPid();
2124 const int uid = ipc->getCallingUid();
2125 if ((uid != AID_SHELL) &&
2126 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002127 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002128 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002129 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002130 // Try to get the main lock, but don't insist if we can't
2131 // (this would indicate SF is stuck, but we want to be able to
2132 // print something in dumpsys).
2133 int retry = 3;
2134 while (mStateLock.tryLock()<0 && --retry>=0) {
2135 usleep(1000000);
2136 }
2137 const bool locked(retry >= 0);
2138 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002139 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002140 "SurfaceFlinger appears to be unresponsive, "
2141 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002142 }
2143
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002144 bool dumpAll = true;
2145 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002146 size_t numArgs = args.size();
2147 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002148 if ((index < numArgs) &&
2149 (args[index] == String16("--list"))) {
2150 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002151 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002152 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002153 }
2154
2155 if ((index < numArgs) &&
2156 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002157 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002158 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002159 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002160 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002161
2162 if ((index < numArgs) &&
2163 (args[index] == String16("--latency-clear"))) {
2164 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002165 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002166 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002167 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002168 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002169
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002170 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002171 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002172 }
2173
Mathias Agopian9795c422009-08-26 16:36:26 -07002174 if (locked) {
2175 mStateLock.unlock();
2176 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002177 }
2178 write(fd, result.string(), result.size());
2179 return NO_ERROR;
2180}
2181
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002182void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002183 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002184{
2185 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2186 const size_t count = currentLayers.size();
2187 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002188 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002189 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002190 }
2191}
2192
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002193void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002194 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002195{
2196 String8 name;
2197 if (index < args.size()) {
2198 name = String8(args[index]);
2199 index++;
2200 }
2201
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002202 const nsecs_t period =
2203 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2204 result.appendFormat("%lld\n", period);
2205
2206 if (name.isEmpty()) {
2207 mAnimFrameTracker.dump(result);
2208 } else {
2209 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2210 const size_t count = currentLayers.size();
2211 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002212 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002213 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002214 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002215 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002216 }
2217 }
2218}
2219
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002220void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002221 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002222{
2223 String8 name;
2224 if (index < args.size()) {
2225 name = String8(args[index]);
2226 index++;
2227 }
2228
2229 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2230 const size_t count = currentLayers.size();
2231 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002232 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002233 if (name.isEmpty() || (name == layer->getName())) {
2234 layer->clearStats();
2235 }
2236 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002237
2238 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002239}
2240
Jamie Gennis6547ff42013-07-16 20:12:42 -07002241// This should only be called from the main thread. Otherwise it would need
2242// the lock and should use mCurrentState rather than mDrawingState.
2243void SurfaceFlinger::logFrameStats() {
2244 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2245 const size_t count = drawingLayers.size();
2246 for (size_t i=0 ; i<count ; i++) {
2247 const sp<Layer>& layer(drawingLayers[i]);
2248 layer->logFrameStats();
2249 }
2250
2251 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2252}
2253
Andy McFadden4803b742012-09-24 19:07:20 -07002254/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2255{
2256 static const char* config =
2257 " [sf"
2258#ifdef NO_RGBX_8888
2259 " NO_RGBX_8888"
2260#endif
2261#ifdef HAS_CONTEXT_PRIORITY
2262 " HAS_CONTEXT_PRIORITY"
2263#endif
2264#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2265 " NEVER_DEFAULT_TO_ASYNC_MODE"
2266#endif
2267#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2268 " TARGET_DISABLE_TRIPLE_BUFFERING"
2269#endif
2270 "]";
2271 result.append(config);
2272}
2273
Mathias Agopian74d211a2013-04-22 16:55:35 +02002274void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2275 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002276{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002277 bool colorize = false;
2278 if (index < args.size()
2279 && (args[index] == String16("--color"))) {
2280 colorize = true;
2281 index++;
2282 }
2283
2284 Colorizer colorizer(colorize);
2285
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002286 // figure out if we're stuck somewhere
2287 const nsecs_t now = systemTime();
2288 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2289 const nsecs_t inTransaction(mDebugInTransaction);
2290 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2291 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2292
2293 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002294 * Dump library configuration.
2295 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002296
2297 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002298 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002299 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002300 appendSfConfigString(result);
2301 appendUiConfigString(result);
2302 appendGuiConfigString(result);
2303 result.append("\n");
2304
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002305 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002306 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002307 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002308 result.append(SyncFeatures::getInstance().toString());
2309 result.append("\n");
2310
Andy McFadden4803b742012-09-24 19:07:20 -07002311 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002312 * Dump the visible layer list
2313 */
2314 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2315 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002316 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002317 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002318 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002319 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002320 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002321 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002322 }
2323
2324 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002325 * Dump Display state
2326 */
2327
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002328 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002329 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002330 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002331 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2332 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002333 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002334 }
2335
2336 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002337 * Dump SurfaceFlinger global state
2338 */
2339
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002340 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002341 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002342 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002343
Mathias Agopian888c8222012-08-04 21:10:38 -07002344 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002345 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002346
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002347 colorizer.bold(result);
2348 result.appendFormat("EGL implementation : %s\n",
2349 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2350 colorizer.reset(result);
2351 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002352 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002353
Mathias Agopian875d8e12013-06-07 15:35:48 -07002354 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002355
Mathias Agopian42977342012-08-05 00:40:46 -07002356 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002357 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002358 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002359 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002360 " last eglSwapBuffers() time: %f us\n"
2361 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002362 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002363 " refresh-rate : %f fps\n"
2364 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002365 " y-dpi : %f\n"
2366 " EGL_NATIVE_VISUAL_ID : %d\n"
2367 " gpu_to_cpu_unsupported : %d\n"
2368 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002369 mLastSwapBufferTime/1000.0,
2370 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002371 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002372 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2373 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002374 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2375 mEGLNativeVisualId,
2376 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002377
Mathias Agopian74d211a2013-04-22 16:55:35 +02002378 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002379 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002380
Mathias Agopian74d211a2013-04-22 16:55:35 +02002381 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002382 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002383
2384 /*
2385 * VSYNC state
2386 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002387 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002388
2389 /*
2390 * Dump HWComposer state
2391 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002392 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002393 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002394 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002395 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002396 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002397 (mDebugDisableHWC || mDebugRegion || mDaltonize
2398 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002399 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002400
2401 /*
2402 * Dump gralloc state
2403 */
2404 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2405 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002406}
2407
Mathias Agopian13127d82013-03-05 17:47:11 -08002408const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002409SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002410 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002411 wp<IBinder> dpy;
2412 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2413 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2414 dpy = mDisplays.keyAt(i);
2415 break;
2416 }
2417 }
2418 if (dpy == NULL) {
2419 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2420 // Just use the primary display so we have something to return
2421 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2422 }
2423 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002424}
2425
Keun young Park63f165f2012-08-31 10:53:36 -07002426bool SurfaceFlinger::startDdmConnection()
2427{
2428 void* libddmconnection_dso =
2429 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2430 if (!libddmconnection_dso) {
2431 return false;
2432 }
2433 void (*DdmConnection_start)(const char* name);
2434 DdmConnection_start =
2435 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2436 if (!DdmConnection_start) {
2437 dlclose(libddmconnection_dso);
2438 return false;
2439 }
2440 (*DdmConnection_start)(getServiceName());
2441 return true;
2442}
2443
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002444status_t SurfaceFlinger::onTransact(
2445 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2446{
2447 switch (code) {
2448 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002449 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002450 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002451 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002452 case BLANK:
2453 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002454 {
2455 // codes that require permission check
2456 IPCThreadState* ipc = IPCThreadState::self();
2457 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002458 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002459 if ((uid != AID_GRAPHICS) &&
2460 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002461 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002462 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2463 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002464 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002465 break;
2466 }
2467 case CAPTURE_SCREEN:
2468 {
2469 // codes that require permission check
2470 IPCThreadState* ipc = IPCThreadState::self();
2471 const int pid = ipc->getCallingPid();
2472 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002473 if ((uid != AID_GRAPHICS) &&
2474 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002475 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002476 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2477 return PERMISSION_DENIED;
2478 }
2479 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002480 }
2481 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002482
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002483 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2484 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002485 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002486 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002487 IPCThreadState* ipc = IPCThreadState::self();
2488 const int pid = ipc->getCallingPid();
2489 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002490 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002491 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002492 return PERMISSION_DENIED;
2493 }
2494 int n;
2495 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002496 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002497 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002498 return NO_ERROR;
2499 case 1002: // SHOW_UPDATES
2500 n = data.readInt32();
2501 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002502 invalidateHwcGeometry();
2503 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002504 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002505 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002506 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002507 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002508 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002509 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002510 setTransactionFlags(
2511 eTransactionNeeded|
2512 eDisplayTransactionNeeded|
2513 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002514 return NO_ERROR;
2515 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002516 case 1006:{ // send empty update
2517 signalRefresh();
2518 return NO_ERROR;
2519 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002520 case 1008: // toggle use of hw composer
2521 n = data.readInt32();
2522 mDebugDisableHWC = n ? 1 : 0;
2523 invalidateHwcGeometry();
2524 repaintEverything();
2525 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002526 case 1009: // toggle use of transform hint
2527 n = data.readInt32();
2528 mDebugDisableTransformHint = n ? 1 : 0;
2529 invalidateHwcGeometry();
2530 repaintEverything();
2531 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002532 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002533 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002534 reply->writeInt32(0);
2535 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002536 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002537 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002538 return NO_ERROR;
2539 case 1013: {
2540 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002541 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2542 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002543 return NO_ERROR;
2544 }
2545 case 1014: {
2546 // daltonize
2547 n = data.readInt32();
2548 switch (n % 10) {
2549 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2550 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2551 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2552 }
2553 if (n >= 10) {
2554 mDaltonizer.setMode(Daltonizer::correction);
2555 } else {
2556 mDaltonizer.setMode(Daltonizer::simulation);
2557 }
2558 mDaltonize = n > 0;
2559 invalidateHwcGeometry();
2560 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002561 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002562 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002563 case 1015: {
2564 // apply a color matrix
2565 n = data.readInt32();
2566 mHasColorMatrix = n ? 1 : 0;
2567 if (n) {
2568 // color matrix is sent as mat3 matrix followed by vec3
2569 // offset, then packed into a mat4 where the last row is
2570 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002571 for (size_t i = 0 ; i < 4; i++) {
2572 for (size_t j = 0; j < 4; j++) {
2573 mColorMatrix[i][j] = data.readFloat();
2574 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002575 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002576 } else {
2577 mColorMatrix = mat4();
2578 }
2579 invalidateHwcGeometry();
2580 repaintEverything();
2581 return NO_ERROR;
2582 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002583 }
2584 }
2585 return err;
2586}
2587
Mathias Agopian53331da2011-08-22 21:44:41 -07002588void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002589 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002590 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002591}
2592
Mathias Agopian59119e62010-10-11 12:37:43 -07002593// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002594// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002595// ---------------------------------------------------------------------------
2596
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002597/* The code below is here to handle b/8734824
2598 *
2599 * We create a IGraphicBufferProducer wrapper that forwards all calls
2600 * to the calling binder thread, where they are executed. This allows
2601 * the calling thread to be reused (on the other side) and not
2602 * depend on having "enough" binder threads to handle the requests.
2603 *
2604 */
2605
2606class GraphicProducerWrapper : public BBinder, public MessageHandler {
2607 sp<IGraphicBufferProducer> impl;
2608 sp<Looper> looper;
2609 status_t result;
2610 bool exitPending;
2611 bool exitRequested;
2612 mutable Barrier barrier;
2613 volatile int32_t memoryBarrier;
2614 uint32_t code;
2615 Parcel const* data;
2616 Parcel* reply;
2617
2618 enum {
2619 MSG_API_CALL,
2620 MSG_EXIT
2621 };
2622
2623 /*
2624 * this is called by our "fake" BpGraphicBufferProducer. We package the
2625 * data and reply Parcel and forward them to the calling thread.
2626 */
2627 virtual status_t transact(uint32_t code,
2628 const Parcel& data, Parcel* reply, uint32_t flags) {
2629 this->code = code;
2630 this->data = &data;
2631 this->reply = reply;
2632 android_atomic_acquire_store(0, &memoryBarrier);
2633 if (exitPending) {
2634 // if we've exited, we run the message synchronously right here
2635 handleMessage(Message(MSG_API_CALL));
2636 } else {
2637 barrier.close();
2638 looper->sendMessage(this, Message(MSG_API_CALL));
2639 barrier.wait();
2640 }
2641 return NO_ERROR;
2642 }
2643
2644 /*
2645 * here we run on the binder calling thread. All we've got to do is
2646 * call the real BpGraphicBufferProducer.
2647 */
2648 virtual void handleMessage(const Message& message) {
2649 android_atomic_release_load(&memoryBarrier);
2650 if (message.what == MSG_API_CALL) {
2651 impl->asBinder()->transact(code, data[0], reply);
2652 barrier.open();
2653 } else if (message.what == MSG_EXIT) {
2654 exitRequested = true;
2655 }
2656 }
2657
2658public:
2659 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2660 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2661 exitPending(false), exitRequested(false) {
2662 }
2663
2664 status_t waitForResponse() {
2665 do {
2666 looper->pollOnce(-1);
2667 } while (!exitRequested);
2668 return result;
2669 }
2670
2671 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002672 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002673 exitPending = true;
2674 looper->sendMessage(this, Message(MSG_EXIT));
2675 }
2676};
2677
2678
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002679status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2680 const sp<IGraphicBufferProducer>& producer,
2681 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002682 uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002683
2684 if (CC_UNLIKELY(display == 0))
2685 return BAD_VALUE;
2686
2687 if (CC_UNLIKELY(producer == 0))
2688 return BAD_VALUE;
2689
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002690 // if we have secure windows on this display, never allow the screen capture
2691 // unless the producer interface is local (i.e.: we can take a screenshot for
2692 // ourselves).
2693 if (!producer->asBinder()->localBinder()) {
2694 Mutex::Autolock _l(mStateLock);
2695 sp<const DisplayDevice> hw(getDisplayDevice(display));
2696 if (hw->getSecureLayerVisible()) {
2697 ALOGW("FB is protected: PERMISSION_DENIED");
2698 return PERMISSION_DENIED;
2699 }
2700 }
2701
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002702 class MessageCaptureScreen : public MessageBase {
2703 SurfaceFlinger* flinger;
2704 sp<IBinder> display;
2705 sp<IGraphicBufferProducer> producer;
2706 uint32_t reqWidth, reqHeight;
2707 uint32_t minLayerZ,maxLayerZ;
2708 status_t result;
2709 public:
2710 MessageCaptureScreen(SurfaceFlinger* flinger,
2711 const sp<IBinder>& display,
2712 const sp<IGraphicBufferProducer>& producer,
2713 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002714 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002715 : flinger(flinger), display(display), producer(producer),
2716 reqWidth(reqWidth), reqHeight(reqHeight),
2717 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2718 result(PERMISSION_DENIED)
2719 {
2720 }
2721 status_t getResult() const {
2722 return result;
2723 }
2724 virtual bool handler() {
2725 Mutex::Autolock _l(flinger->mStateLock);
2726 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002727 result = flinger->captureScreenImplLocked(hw,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002728 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002729 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002730 return true;
2731 }
2732 };
2733
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002734 // make sure to process transactions before screenshots -- a transaction
2735 // might already be pending but scheduled for VSYNC; this guarantees we
2736 // will handle it before the screenshot. When VSYNC finally arrives
2737 // the scheduled transaction will be a no-op. If no transactions are
2738 // scheduled at this time, this will end-up being a no-op as well.
2739 mEventQueue.invalidateTransactionNow();
2740
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002741 // this creates a "fake" BBinder which will serve as a "fake" remote
2742 // binder to receive the marshaled calls and forward them to the
2743 // real remote (a BpGraphicBufferProducer)
2744 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2745
2746 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2747 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002748 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002749 display, IGraphicBufferProducer::asInterface( wrapper ),
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002750 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002751
2752 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002753 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002754 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002755 }
2756 return res;
2757}
2758
Mathias Agopian180f10d2013-04-10 22:55:41 -07002759
2760void SurfaceFlinger::renderScreenImplLocked(
2761 const sp<const DisplayDevice>& hw,
2762 uint32_t reqWidth, uint32_t reqHeight,
2763 uint32_t minLayerZ, uint32_t maxLayerZ,
2764 bool yswap)
2765{
2766 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002767 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002768
2769 // get screen geometry
2770 const uint32_t hw_w = hw->getWidth();
2771 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002772 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2773
2774 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002775 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002776
2777 // set-up our viewport
Mathias Agopian3f844832013-08-07 21:24:32 -07002778 engine.setViewportAndProjection(reqWidth, reqHeight, hw_w, hw_h, yswap);
2779 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002780
2781 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002782 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002783
2784 const LayerVector& layers( mDrawingState.layersSortedByZ );
2785 const size_t count = layers.size();
2786 for (size_t i=0 ; i<count ; ++i) {
2787 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002788 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002789 if (state.layerStack == hw->getLayerStack()) {
2790 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2791 if (layer->isVisible()) {
2792 if (filtering) layer->setFiltering(true);
2793 layer->draw(hw);
2794 if (filtering) layer->setFiltering(false);
2795 }
2796 }
2797 }
2798 }
2799
2800 // compositionComplete is needed for older driver
2801 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002802 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002803}
2804
2805
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002806status_t SurfaceFlinger::captureScreenImplLocked(
2807 const sp<const DisplayDevice>& hw,
2808 const sp<IGraphicBufferProducer>& producer,
2809 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002810 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002811{
2812 ATRACE_CALL();
2813
Mathias Agopian180f10d2013-04-10 22:55:41 -07002814 // get screen geometry
2815 const uint32_t hw_w = hw->getWidth();
2816 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002817
Mathias Agopian180f10d2013-04-10 22:55:41 -07002818 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2819 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2820 reqWidth, reqHeight, hw_w, hw_h);
2821 return BAD_VALUE;
2822 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002823
Mathias Agopian180f10d2013-04-10 22:55:41 -07002824 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2825 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2826
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002827 // create a surface (because we're a producer, and we need to
2828 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07002829 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002830 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002831
2832 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002833 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002834 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2835 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002836
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002837 int err = 0;
2838 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07002839 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002840 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2841 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002842
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002843 if (err == NO_ERROR) {
2844 ANativeWindowBuffer* buffer;
2845 /* TODO: Once we have the sync framework everywhere this can use
2846 * server-side waits on the fence that dequeueBuffer returns.
2847 */
2848 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2849 if (result == NO_ERROR) {
2850 // create an EGLImage from the buffer so we can later
2851 // turn it into a texture
2852 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2853 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2854 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002855 // this binds the given EGLImage as a framebuffer for the
2856 // duration of this scope.
2857 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2858 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002859 // this will in fact render into our dequeued buffer
2860 // via an FBO, which means we didn't have to create
2861 // an EGLSurface and therefore we're not
2862 // dependent on the context's EGLConfig.
2863 renderScreenImplLocked(hw, reqWidth, reqHeight,
2864 minLayerZ, maxLayerZ, true);
Mathias Agopiand5556842013-09-19 17:08:37 -07002865
2866 if (DEBUG_SCREENSHOTS) {
2867 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
2868 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
2869 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
2870 hw, minLayerZ, maxLayerZ);
2871 delete [] pixels;
2872 }
2873
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002874 } else {
2875 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2876 result = INVALID_OPERATION;
2877 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002878 // destroy our image
2879 eglDestroyImageKHR(mEGLDisplay, image);
2880 } else {
2881 result = BAD_VALUE;
2882 }
2883 window->queueBuffer(window, buffer, -1);
2884 }
2885 } else {
2886 result = BAD_VALUE;
2887 }
2888 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2889 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002890
Mathias Agopian74c40c02010-09-29 13:02:36 -07002891 return result;
2892}
2893
Mathias Agopiand5556842013-09-19 17:08:37 -07002894void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
2895 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07002896 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07002897 for (size_t y=0 ; y<h ; y++) {
2898 uint32_t const * p = (uint32_t const *)vaddr + y*s;
2899 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07002900 if (p[x] != 0xFF000000) return;
2901 }
2902 }
2903 ALOGE("*** we just took a black screenshot ***\n"
2904 "requested minz=%d, maxz=%d, layerStack=%d",
2905 minLayerZ, maxLayerZ, hw->getLayerStack());
2906 const LayerVector& layers( mDrawingState.layersSortedByZ );
2907 const size_t count = layers.size();
2908 for (size_t i=0 ; i<count ; ++i) {
2909 const sp<Layer>& layer(layers[i]);
2910 const Layer::State& state(layer->getDrawingState());
2911 const bool visible = (state.layerStack == hw->getLayerStack())
2912 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2913 && (layer->isVisible());
2914 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
2915 visible ? '+' : '-',
2916 i, layer->getName().string(), state.layerStack, state.z,
2917 layer->isVisible(), state.flags, state.alpha);
2918 }
2919 }
2920}
2921
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002922// ---------------------------------------------------------------------------
2923
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002924SurfaceFlinger::LayerVector::LayerVector() {
2925}
2926
2927SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002928 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002929}
2930
2931int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2932 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002933{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002934 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002935 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2936 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002937
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002938 uint32_t ls = l->getCurrentState().layerStack;
2939 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002940 if (ls != rs)
2941 return ls - rs;
2942
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002943 uint32_t lz = l->getCurrentState().z;
2944 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002945 if (lz != rz)
2946 return lz - rz;
2947
2948 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002949}
2950
2951// ---------------------------------------------------------------------------
2952
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002953SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2954 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002955}
2956
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002957SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002958 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002959 viewport.makeInvalid();
2960 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002961}
2962
2963// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002964
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002965}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002966
2967
2968#if defined(__gl_h_)
2969#error "don't include gl/gl.h in this file"
2970#endif
2971
2972#if defined(__gl2_h_)
2973#error "don't include gl2/gl2.h in this file"
2974#endif