blob: 6d343fd5994eb173e55910d836bb87c69875dd2c [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) {
1510 // preserve last row of color matrix
1511 colorMatrix = colorMatrix * mDaltonizer();
1512 colorMatrix[3] = mColorMatrix[3];
1513 }
1514 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001515 doComposeSurfaces(hw, dirtyRegion);
1516 engine.endGroup();
1517 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001518
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001519 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001520 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001521
1522 // swap buffers (presentation)
1523 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001524}
1525
Mathias Agopiancd60f992012-08-16 16:28:27 -07001526void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001527{
Mathias Agopian3f844832013-08-07 21:24:32 -07001528 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001529 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001530 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001531 HWComposer::LayerListIterator cur = hwc.begin(id);
1532 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001533
Jesse Halld05a17f2013-09-30 16:49:30 -07001534 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001535 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001536 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001537 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1538 hw->getDisplayName().string());
1539 return;
1540 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001541
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001542 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001543 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001544 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001545 // when using overlays, we assume a fully transparent framebuffer
1546 // NOTE: we could reduce how much we need to clear, for instance
1547 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001548 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001549 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001550 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001551 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001552 // we start with the whole screen area
1553 const Region bounds(hw->getBounds());
1554
1555 // we remove the scissor part
1556 // we're left with the letterbox region
1557 // (common case is that letterbox ends-up being empty)
1558 const Region letterbox(bounds.subtract(hw->getScissor()));
1559
1560 // compute the area to clear
1561 Region region(hw->undefinedRegion.merge(letterbox));
1562
1563 // but limit it to the dirty region
1564 region.andSelf(dirty);
1565
Mathias Agopianb9494d52012-04-18 02:28:45 -07001566 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001567 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001568 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001569 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001570 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001571 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001572
Mathias Agopian766dc492012-10-30 18:08:06 -07001573 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1574 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001575 // scissor on the main display. It should never be needed
1576 // anyways (though in theory it could since the API allows it).
1577 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001578 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001579 if (scissor != bounds) {
1580 // scissor doesn't match the screen's dimensions, so we
1581 // need to clear everything outside of it and enable
1582 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001583
Mathias Agopianf45c5102012-10-24 16:29:17 -07001584 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001585 const uint32_t height = hw->getHeight();
1586 engine.setScissor(scissor.left, height - scissor.bottom,
1587 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001588 }
1589 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001590 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001591
Mathias Agopian85d751c2012-08-29 16:59:24 -07001592 /*
1593 * and then, render the layers targeted at the framebuffer
1594 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001595
Mathias Agopian13127d82013-03-05 17:47:11 -08001596 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001597 const size_t count = layers.size();
1598 const Transform& tr = hw->getTransform();
1599 if (cur != end) {
1600 // we're using h/w composer
1601 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001602 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001603 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001604 if (!clip.isEmpty()) {
1605 switch (cur->getCompositionType()) {
1606 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001607 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001608 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1609 && i
Mathias Agopianac683022013-10-01 15:36:52 -07001610 && layer->isOpaque() && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001611 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001612 // never clear the very first layer since we're
1613 // guaranteed the FB is already cleared
1614 layer->clearWithOpenGL(hw, clip);
1615 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001616 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001617 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001618 case HWC_FRAMEBUFFER: {
1619 layer->draw(hw, clip);
1620 break;
1621 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001622 case HWC_FRAMEBUFFER_TARGET: {
1623 // this should not happen as the iterator shouldn't
1624 // let us get there.
1625 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1626 break;
1627 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001628 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001629 }
1630 layer->setAcquireFence(hw, *cur);
1631 }
1632 } else {
1633 // we're not using h/w composer
1634 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001635 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001636 const Region clip(dirty.intersect(
1637 tr.transform(layer->visibleRegion)));
1638 if (!clip.isEmpty()) {
1639 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001640 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001641 }
1642 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001643
1644 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001645 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001646}
1647
Mathias Agopian3f844832013-08-07 21:24:32 -07001648void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001649 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001650 RenderEngine& engine(getRenderEngine());
1651 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652}
1653
Mathias Agopianac9fa422013-02-11 16:40:36 -08001654void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1655 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001656 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001657 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001658{
Mathias Agopian96f08192010-06-02 23:28:45 -07001659 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001660 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001661
Mathias Agopian96f08192010-06-02 23:28:45 -07001662 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001663 Mutex::Autolock _l(mStateLock);
1664 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001665 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001666}
1667
Mathias Agopian3f844832013-08-07 21:24:32 -07001668status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001669 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001670 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001671 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001672 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001673 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001674 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001675 return NO_ERROR;
1676 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001677 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001678}
1679
Mathias Agopian3f844832013-08-07 21:24:32 -07001680uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001681 return android_atomic_release_load(&mTransactionFlags);
1682}
1683
Mathias Agopian3f844832013-08-07 21:24:32 -07001684uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001685 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1686}
1687
Mathias Agopian3f844832013-08-07 21:24:32 -07001688uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001689 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1690 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001691 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001692 }
1693 return old;
1694}
1695
Mathias Agopian8b33f032012-07-24 20:43:54 -07001696void SurfaceFlinger::setTransactionState(
1697 const Vector<ComposerState>& state,
1698 const Vector<DisplayState>& displays,
1699 uint32_t flags)
1700{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001701 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001702 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001703 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001704
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001705 if (flags & eAnimation) {
1706 // For window updates that are part of an animation we must wait for
1707 // previous animation "frames" to be handled.
1708 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001709 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001710 if (CC_UNLIKELY(err != NO_ERROR)) {
1711 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001712 // caller after a few seconds.
1713 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1714 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001715 mAnimTransactionPending = false;
1716 break;
1717 }
1718 }
1719 }
1720
Mathias Agopiane57f2922012-08-09 16:29:12 -07001721 size_t count = displays.size();
1722 for (size_t i=0 ; i<count ; i++) {
1723 const DisplayState& s(displays[i]);
1724 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001725 }
1726
Mathias Agopiane57f2922012-08-09 16:29:12 -07001727 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001728 for (size_t i=0 ; i<count ; i++) {
1729 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001730 // Here we need to check that the interface we're given is indeed
1731 // one of our own. A malicious client could give us a NULL
1732 // IInterface, or one of its own or even one of our own but a
1733 // different type. All these situations would cause us to crash.
1734 //
1735 // NOTE: it would be better to use RTTI as we could directly check
1736 // that we have a Client*. however, RTTI is disabled in Android.
1737 if (s.client != NULL) {
1738 sp<IBinder> binder = s.client->asBinder();
1739 if (binder != NULL) {
1740 String16 desc(binder->getInterfaceDescriptor());
1741 if (desc == ISurfaceComposerClient::descriptor) {
1742 sp<Client> client( static_cast<Client *>(s.client.get()) );
1743 transactionFlags |= setClientStateLocked(client, s.state);
1744 }
1745 }
1746 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001747 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001748
Mathias Agopian386aa982011-11-07 21:58:03 -08001749 if (transactionFlags) {
1750 // this triggers the transaction
1751 setTransactionFlags(transactionFlags);
1752
1753 // if this is a synchronous transaction, wait for it to take effect
1754 // before returning.
1755 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001756 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001757 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001758 if (flags & eAnimation) {
1759 mAnimTransactionPending = true;
1760 }
1761 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001762 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1763 if (CC_UNLIKELY(err != NO_ERROR)) {
1764 // just in case something goes wrong in SF, return to the
1765 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001766 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1767 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001768 break;
1769 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001770 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001771 }
1772}
1773
Mathias Agopiane57f2922012-08-09 16:29:12 -07001774uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1775{
Jesse Hall9a143922012-10-04 16:29:19 -07001776 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1777 if (dpyIdx < 0)
1778 return 0;
1779
Mathias Agopiane57f2922012-08-09 16:29:12 -07001780 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001781 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001782 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001783 const uint32_t what = s.what;
1784 if (what & DisplayState::eSurfaceChanged) {
1785 if (disp.surface->asBinder() != s.surface->asBinder()) {
1786 disp.surface = s.surface;
1787 flags |= eDisplayTransactionNeeded;
1788 }
1789 }
1790 if (what & DisplayState::eLayerStackChanged) {
1791 if (disp.layerStack != s.layerStack) {
1792 disp.layerStack = s.layerStack;
1793 flags |= eDisplayTransactionNeeded;
1794 }
1795 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001796 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001797 if (disp.orientation != s.orientation) {
1798 disp.orientation = s.orientation;
1799 flags |= eDisplayTransactionNeeded;
1800 }
1801 if (disp.frame != s.frame) {
1802 disp.frame = s.frame;
1803 flags |= eDisplayTransactionNeeded;
1804 }
1805 if (disp.viewport != s.viewport) {
1806 disp.viewport = s.viewport;
1807 flags |= eDisplayTransactionNeeded;
1808 }
1809 }
1810 }
1811 return flags;
1812}
1813
1814uint32_t SurfaceFlinger::setClientStateLocked(
1815 const sp<Client>& client,
1816 const layer_state_t& s)
1817{
1818 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001819 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001820 if (layer != 0) {
1821 const uint32_t what = s.what;
1822 if (what & layer_state_t::ePositionChanged) {
1823 if (layer->setPosition(s.x, s.y))
1824 flags |= eTraversalNeeded;
1825 }
1826 if (what & layer_state_t::eLayerChanged) {
1827 // NOTE: index needs to be calculated before we update the state
1828 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1829 if (layer->setLayer(s.z)) {
1830 mCurrentState.layersSortedByZ.removeAt(idx);
1831 mCurrentState.layersSortedByZ.add(layer);
1832 // we need traversal (state changed)
1833 // AND transaction (list changed)
1834 flags |= eTransactionNeeded|eTraversalNeeded;
1835 }
1836 }
1837 if (what & layer_state_t::eSizeChanged) {
1838 if (layer->setSize(s.w, s.h)) {
1839 flags |= eTraversalNeeded;
1840 }
1841 }
1842 if (what & layer_state_t::eAlphaChanged) {
1843 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1844 flags |= eTraversalNeeded;
1845 }
1846 if (what & layer_state_t::eMatrixChanged) {
1847 if (layer->setMatrix(s.matrix))
1848 flags |= eTraversalNeeded;
1849 }
1850 if (what & layer_state_t::eTransparentRegionChanged) {
1851 if (layer->setTransparentRegionHint(s.transparentRegion))
1852 flags |= eTraversalNeeded;
1853 }
1854 if (what & layer_state_t::eVisibilityChanged) {
1855 if (layer->setFlags(s.flags, s.mask))
1856 flags |= eTraversalNeeded;
1857 }
1858 if (what & layer_state_t::eCropChanged) {
1859 if (layer->setCrop(s.crop))
1860 flags |= eTraversalNeeded;
1861 }
1862 if (what & layer_state_t::eLayerStackChanged) {
1863 // NOTE: index needs to be calculated before we update the state
1864 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1865 if (layer->setLayerStack(s.layerStack)) {
1866 mCurrentState.layersSortedByZ.removeAt(idx);
1867 mCurrentState.layersSortedByZ.add(layer);
1868 // we need traversal (state changed)
1869 // AND transaction (list changed)
1870 flags |= eTransactionNeeded|eTraversalNeeded;
1871 }
1872 }
1873 }
1874 return flags;
1875}
1876
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001877status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001878 const String8& name,
1879 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001880 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1881 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001882{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001883 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001884 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001885 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001886 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001887 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001888 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001889
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001890 status_t result = NO_ERROR;
1891
1892 sp<Layer> layer;
1893
Mathias Agopian3165cc22012-08-08 19:42:09 -07001894 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1895 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001896 result = createNormalLayer(client,
1897 name, w, h, flags, format,
1898 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001899 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001900 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001901 result = createDimLayer(client,
1902 name, w, h, flags,
1903 handle, gbp, &layer);
1904 break;
1905 default:
1906 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001907 break;
1908 }
1909
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001910 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001911 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001912 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001913 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001914 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001915}
1916
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001917status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1918 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1919 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001920{
1921 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001922 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001923 case PIXEL_FORMAT_TRANSPARENT:
1924 case PIXEL_FORMAT_TRANSLUCENT:
1925 format = PIXEL_FORMAT_RGBA_8888;
1926 break;
1927 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001928#ifdef NO_RGBX_8888
1929 format = PIXEL_FORMAT_RGB_565;
1930#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001931 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001932#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001933 break;
1934 }
1935
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001936#ifdef NO_RGBX_8888
1937 if (format == PIXEL_FORMAT_RGBX_8888)
1938 format = PIXEL_FORMAT_RGBA_8888;
1939#endif
1940
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001941 *outLayer = new Layer(this, client, name, w, h, flags);
1942 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1943 if (err == NO_ERROR) {
1944 *handle = (*outLayer)->getHandle();
1945 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001946 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001947
1948 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1949 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001950}
1951
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001952status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1953 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1954 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001955{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001956 *outLayer = new LayerDim(this, client, name, w, h, flags);
1957 *handle = (*outLayer)->getHandle();
1958 *gbp = (*outLayer)->getBufferQueue();
1959 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001960}
1961
Mathias Agopianac9fa422013-02-11 16:40:36 -08001962status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001963{
Mathias Agopian67106042013-03-14 19:18:13 -07001964 // called by the window manager when it wants to remove a Layer
1965 status_t err = NO_ERROR;
1966 sp<Layer> l(client->getLayerUser(handle));
1967 if (l != NULL) {
1968 err = removeLayer(l);
1969 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1970 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001971 }
1972 return err;
1973}
1974
Mathias Agopian13127d82013-03-05 17:47:11 -08001975status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001976{
Mathias Agopian67106042013-03-14 19:18:13 -07001977 // called by ~LayerCleaner() when all references to the IBinder (handle)
1978 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001979 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001980 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001981 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07001982 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001983 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001984 "error removing layer=%p (%s)", l.get(), strerror(-err));
1985 }
1986 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001987}
1988
Mathias Agopianb60314a2012-04-10 22:09:54 -07001989// ---------------------------------------------------------------------------
1990
Andy McFadden13a082e2012-08-24 10:16:42 -07001991void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08001992 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07001993 Vector<ComposerState> state;
1994 Vector<DisplayState> displays;
1995 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08001996 d.what = DisplayState::eDisplayProjectionChanged |
1997 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08001998 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08001999 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002000 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002001 d.frame.makeInvalid();
2002 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002003 displays.add(d);
2004 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002005 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07002006
2007 const nsecs_t period =
2008 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2009 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002010}
2011
2012void SurfaceFlinger::initializeDisplays() {
2013 class MessageScreenInitialized : public MessageBase {
2014 SurfaceFlinger* flinger;
2015 public:
2016 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2017 virtual bool handler() {
2018 flinger->onInitializeDisplays();
2019 return true;
2020 }
2021 };
2022 sp<MessageBase> msg = new MessageScreenInitialized(this);
2023 postMessageAsync(msg); // we may be called from main thread, use async message
2024}
2025
2026
Mathias Agopiancde87a32012-09-13 14:09:01 -07002027void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002028 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2029 if (hw->isScreenAcquired()) {
2030 // this is expected, e.g. when power manager wakes up during boot
2031 ALOGD(" screen was previously acquired");
2032 return;
2033 }
2034
Mathias Agopian42977342012-08-05 00:40:46 -07002035 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002036 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002037 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002038 // built-in display, tell the HWC
2039 getHwComposer().acquire(type);
2040
2041 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2042 // FIXME: eventthread only knows about the main display right now
2043 mEventThread->onScreenAcquired();
2044 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002045 }
Mathias Agopian20128302012-08-13 18:32:13 -07002046 mVisibleRegionsDirty = true;
2047 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002048}
2049
Mathias Agopiancde87a32012-09-13 14:09:01 -07002050void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002051 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2052 if (!hw->isScreenAcquired()) {
2053 ALOGD(" screen was previously released");
2054 return;
2055 }
2056
2057 hw->releaseScreen();
2058 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002059 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002060 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002061 // FIXME: eventthread only knows about the main display right now
2062 mEventThread->onScreenReleased();
2063 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002064
2065 // built-in display, tell the HWC
2066 getHwComposer().release(type);
2067 }
2068 mVisibleRegionsDirty = true;
2069 // from this point on, SF will stop drawing on this display
2070}
2071
2072void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2073 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002074 SurfaceFlinger& mFlinger;
2075 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002076 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002077 MessageScreenAcquired(SurfaceFlinger& flinger,
2078 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002079 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002080 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2081 if (hw == NULL) {
2082 ALOGE("Attempt to unblank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002083 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002084 ALOGW("Attempt to unblank virtual display");
2085 } else {
2086 mFlinger.onScreenAcquired(hw);
2087 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002088 return true;
2089 }
2090 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002091 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2092 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002093}
2094
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002095void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002096 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002097 SurfaceFlinger& mFlinger;
2098 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002099 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002100 MessageScreenReleased(SurfaceFlinger& flinger,
2101 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002102 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002103 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2104 if (hw == NULL) {
2105 ALOGE("Attempt to blank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002106 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002107 ALOGW("Attempt to blank virtual display");
2108 } else {
2109 mFlinger.onScreenReleased(hw);
2110 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002111 return true;
2112 }
2113 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002114 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2115 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002116}
2117
2118// ---------------------------------------------------------------------------
2119
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002120status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2121{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002122 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002123
Mathias Agopianbd115332013-04-18 16:41:04 -07002124 IPCThreadState* ipc = IPCThreadState::self();
2125 const int pid = ipc->getCallingPid();
2126 const int uid = ipc->getCallingUid();
2127 if ((uid != AID_SHELL) &&
2128 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002129 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002130 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002131 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002132 // Try to get the main lock, but don't insist if we can't
2133 // (this would indicate SF is stuck, but we want to be able to
2134 // print something in dumpsys).
2135 int retry = 3;
2136 while (mStateLock.tryLock()<0 && --retry>=0) {
2137 usleep(1000000);
2138 }
2139 const bool locked(retry >= 0);
2140 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002141 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002142 "SurfaceFlinger appears to be unresponsive, "
2143 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002144 }
2145
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002146 bool dumpAll = true;
2147 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002148 size_t numArgs = args.size();
2149 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002150 if ((index < numArgs) &&
2151 (args[index] == String16("--list"))) {
2152 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002153 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002154 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002155 }
2156
2157 if ((index < numArgs) &&
2158 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002159 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002160 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002161 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002162 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002163
2164 if ((index < numArgs) &&
2165 (args[index] == String16("--latency-clear"))) {
2166 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002167 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002168 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002169 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002170 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002171
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002172 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002173 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002174 }
2175
Mathias Agopian9795c422009-08-26 16:36:26 -07002176 if (locked) {
2177 mStateLock.unlock();
2178 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002179 }
2180 write(fd, result.string(), result.size());
2181 return NO_ERROR;
2182}
2183
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002184void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002185 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002186{
2187 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2188 const size_t count = currentLayers.size();
2189 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002190 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002191 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002192 }
2193}
2194
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002195void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002196 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002197{
2198 String8 name;
2199 if (index < args.size()) {
2200 name = String8(args[index]);
2201 index++;
2202 }
2203
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002204 const nsecs_t period =
2205 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2206 result.appendFormat("%lld\n", period);
2207
2208 if (name.isEmpty()) {
2209 mAnimFrameTracker.dump(result);
2210 } else {
2211 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2212 const size_t count = currentLayers.size();
2213 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002214 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002215 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002216 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002217 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002218 }
2219 }
2220}
2221
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002222void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002223 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002224{
2225 String8 name;
2226 if (index < args.size()) {
2227 name = String8(args[index]);
2228 index++;
2229 }
2230
2231 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2232 const size_t count = currentLayers.size();
2233 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002234 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002235 if (name.isEmpty() || (name == layer->getName())) {
2236 layer->clearStats();
2237 }
2238 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002239
2240 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002241}
2242
Jamie Gennis6547ff42013-07-16 20:12:42 -07002243// This should only be called from the main thread. Otherwise it would need
2244// the lock and should use mCurrentState rather than mDrawingState.
2245void SurfaceFlinger::logFrameStats() {
2246 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2247 const size_t count = drawingLayers.size();
2248 for (size_t i=0 ; i<count ; i++) {
2249 const sp<Layer>& layer(drawingLayers[i]);
2250 layer->logFrameStats();
2251 }
2252
2253 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2254}
2255
Andy McFadden4803b742012-09-24 19:07:20 -07002256/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2257{
2258 static const char* config =
2259 " [sf"
2260#ifdef NO_RGBX_8888
2261 " NO_RGBX_8888"
2262#endif
2263#ifdef HAS_CONTEXT_PRIORITY
2264 " HAS_CONTEXT_PRIORITY"
2265#endif
2266#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2267 " NEVER_DEFAULT_TO_ASYNC_MODE"
2268#endif
2269#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2270 " TARGET_DISABLE_TRIPLE_BUFFERING"
2271#endif
2272 "]";
2273 result.append(config);
2274}
2275
Mathias Agopian74d211a2013-04-22 16:55:35 +02002276void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2277 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002278{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002279 bool colorize = false;
2280 if (index < args.size()
2281 && (args[index] == String16("--color"))) {
2282 colorize = true;
2283 index++;
2284 }
2285
2286 Colorizer colorizer(colorize);
2287
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002288 // figure out if we're stuck somewhere
2289 const nsecs_t now = systemTime();
2290 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2291 const nsecs_t inTransaction(mDebugInTransaction);
2292 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2293 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2294
2295 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002296 * Dump library configuration.
2297 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002298
2299 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002300 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002301 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002302 appendSfConfigString(result);
2303 appendUiConfigString(result);
2304 appendGuiConfigString(result);
2305 result.append("\n");
2306
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002307 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002308 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002309 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002310 result.append(SyncFeatures::getInstance().toString());
2311 result.append("\n");
2312
Andy McFadden4803b742012-09-24 19:07:20 -07002313 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002314 * Dump the visible layer list
2315 */
2316 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2317 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002318 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002319 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002320 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002321 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002322 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002323 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002324 }
2325
2326 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002327 * Dump Display state
2328 */
2329
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002330 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002331 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002332 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002333 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2334 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002335 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002336 }
2337
2338 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002339 * Dump SurfaceFlinger global state
2340 */
2341
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002342 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002343 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002344 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002345
Mathias Agopian888c8222012-08-04 21:10:38 -07002346 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002347 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002348
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002349 colorizer.bold(result);
2350 result.appendFormat("EGL implementation : %s\n",
2351 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2352 colorizer.reset(result);
2353 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002354 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002355
Mathias Agopian875d8e12013-06-07 15:35:48 -07002356 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002357
Mathias Agopian42977342012-08-05 00:40:46 -07002358 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002359 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002360 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002361 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002362 " last eglSwapBuffers() time: %f us\n"
2363 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002364 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002365 " refresh-rate : %f fps\n"
2366 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002367 " y-dpi : %f\n"
2368 " EGL_NATIVE_VISUAL_ID : %d\n"
2369 " gpu_to_cpu_unsupported : %d\n"
2370 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002371 mLastSwapBufferTime/1000.0,
2372 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002373 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002374 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2375 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002376 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2377 mEGLNativeVisualId,
2378 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002379
Mathias Agopian74d211a2013-04-22 16:55:35 +02002380 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002381 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002382
Mathias Agopian74d211a2013-04-22 16:55:35 +02002383 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002384 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002385
2386 /*
2387 * VSYNC state
2388 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002389 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002390
2391 /*
2392 * Dump HWComposer state
2393 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002394 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002395 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002396 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002397 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002398 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002399 (mDebugDisableHWC || mDebugRegion || mDaltonize
2400 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002401 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002402
2403 /*
2404 * Dump gralloc state
2405 */
2406 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2407 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002408}
2409
Mathias Agopian13127d82013-03-05 17:47:11 -08002410const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002411SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002412 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002413 wp<IBinder> dpy;
2414 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2415 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2416 dpy = mDisplays.keyAt(i);
2417 break;
2418 }
2419 }
2420 if (dpy == NULL) {
2421 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2422 // Just use the primary display so we have something to return
2423 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2424 }
2425 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002426}
2427
Keun young Park63f165f2012-08-31 10:53:36 -07002428bool SurfaceFlinger::startDdmConnection()
2429{
2430 void* libddmconnection_dso =
2431 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2432 if (!libddmconnection_dso) {
2433 return false;
2434 }
2435 void (*DdmConnection_start)(const char* name);
2436 DdmConnection_start =
2437 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2438 if (!DdmConnection_start) {
2439 dlclose(libddmconnection_dso);
2440 return false;
2441 }
2442 (*DdmConnection_start)(getServiceName());
2443 return true;
2444}
2445
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002446status_t SurfaceFlinger::onTransact(
2447 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2448{
2449 switch (code) {
2450 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002451 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002452 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002453 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002454 case BLANK:
2455 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002456 {
2457 // codes that require permission check
2458 IPCThreadState* ipc = IPCThreadState::self();
2459 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002460 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002461 if ((uid != AID_GRAPHICS) &&
2462 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002463 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002464 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2465 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002466 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002467 break;
2468 }
2469 case CAPTURE_SCREEN:
2470 {
2471 // codes that require permission check
2472 IPCThreadState* ipc = IPCThreadState::self();
2473 const int pid = ipc->getCallingPid();
2474 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002475 if ((uid != AID_GRAPHICS) &&
2476 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002477 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002478 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2479 return PERMISSION_DENIED;
2480 }
2481 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002482 }
2483 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002484
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002485 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2486 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002487 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002488 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002489 IPCThreadState* ipc = IPCThreadState::self();
2490 const int pid = ipc->getCallingPid();
2491 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002492 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002493 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002494 return PERMISSION_DENIED;
2495 }
2496 int n;
2497 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002498 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002499 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002500 return NO_ERROR;
2501 case 1002: // SHOW_UPDATES
2502 n = data.readInt32();
2503 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002504 invalidateHwcGeometry();
2505 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002506 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002507 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002508 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002509 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002510 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002511 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002512 setTransactionFlags(
2513 eTransactionNeeded|
2514 eDisplayTransactionNeeded|
2515 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002516 return NO_ERROR;
2517 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002518 case 1006:{ // send empty update
2519 signalRefresh();
2520 return NO_ERROR;
2521 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002522 case 1008: // toggle use of hw composer
2523 n = data.readInt32();
2524 mDebugDisableHWC = n ? 1 : 0;
2525 invalidateHwcGeometry();
2526 repaintEverything();
2527 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002528 case 1009: // toggle use of transform hint
2529 n = data.readInt32();
2530 mDebugDisableTransformHint = n ? 1 : 0;
2531 invalidateHwcGeometry();
2532 repaintEverything();
2533 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002534 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002535 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002536 reply->writeInt32(0);
2537 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002538 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002539 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002540 return NO_ERROR;
2541 case 1013: {
2542 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002543 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2544 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002545 return NO_ERROR;
2546 }
2547 case 1014: {
2548 // daltonize
2549 n = data.readInt32();
2550 switch (n % 10) {
2551 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2552 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2553 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2554 }
2555 if (n >= 10) {
2556 mDaltonizer.setMode(Daltonizer::correction);
2557 } else {
2558 mDaltonizer.setMode(Daltonizer::simulation);
2559 }
2560 mDaltonize = n > 0;
2561 invalidateHwcGeometry();
2562 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002563 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002564 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002565 case 1015: {
2566 // apply a color matrix
2567 n = data.readInt32();
2568 mHasColorMatrix = n ? 1 : 0;
2569 if (n) {
2570 // color matrix is sent as mat3 matrix followed by vec3
2571 // offset, then packed into a mat4 where the last row is
2572 // the offset and extra values are 0
2573 for (size_t i = 0 ; i < 3 ; i++) {
2574 for (size_t j = 0; j < 3; j++) {
2575 mColorMatrix[i][j] = data.readFloat();
2576 }
2577 mColorMatrix[i][3] = 0;
2578 }
2579 for (size_t i = 0; i < 3; i++) {
2580 mColorMatrix[3][i] = data.readFloat();
2581 }
2582 mColorMatrix[3][3] = 0;
2583 } else {
2584 mColorMatrix = mat4();
2585 }
2586 invalidateHwcGeometry();
2587 repaintEverything();
2588 return NO_ERROR;
2589 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002590 }
2591 }
2592 return err;
2593}
2594
Mathias Agopian53331da2011-08-22 21:44:41 -07002595void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002596 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002597 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002598}
2599
Mathias Agopian59119e62010-10-11 12:37:43 -07002600// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002601// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002602// ---------------------------------------------------------------------------
2603
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002604/* The code below is here to handle b/8734824
2605 *
2606 * We create a IGraphicBufferProducer wrapper that forwards all calls
2607 * to the calling binder thread, where they are executed. This allows
2608 * the calling thread to be reused (on the other side) and not
2609 * depend on having "enough" binder threads to handle the requests.
2610 *
2611 */
2612
2613class GraphicProducerWrapper : public BBinder, public MessageHandler {
2614 sp<IGraphicBufferProducer> impl;
2615 sp<Looper> looper;
2616 status_t result;
2617 bool exitPending;
2618 bool exitRequested;
2619 mutable Barrier barrier;
2620 volatile int32_t memoryBarrier;
2621 uint32_t code;
2622 Parcel const* data;
2623 Parcel* reply;
2624
2625 enum {
2626 MSG_API_CALL,
2627 MSG_EXIT
2628 };
2629
2630 /*
2631 * this is called by our "fake" BpGraphicBufferProducer. We package the
2632 * data and reply Parcel and forward them to the calling thread.
2633 */
2634 virtual status_t transact(uint32_t code,
2635 const Parcel& data, Parcel* reply, uint32_t flags) {
2636 this->code = code;
2637 this->data = &data;
2638 this->reply = reply;
2639 android_atomic_acquire_store(0, &memoryBarrier);
2640 if (exitPending) {
2641 // if we've exited, we run the message synchronously right here
2642 handleMessage(Message(MSG_API_CALL));
2643 } else {
2644 barrier.close();
2645 looper->sendMessage(this, Message(MSG_API_CALL));
2646 barrier.wait();
2647 }
2648 return NO_ERROR;
2649 }
2650
2651 /*
2652 * here we run on the binder calling thread. All we've got to do is
2653 * call the real BpGraphicBufferProducer.
2654 */
2655 virtual void handleMessage(const Message& message) {
2656 android_atomic_release_load(&memoryBarrier);
2657 if (message.what == MSG_API_CALL) {
2658 impl->asBinder()->transact(code, data[0], reply);
2659 barrier.open();
2660 } else if (message.what == MSG_EXIT) {
2661 exitRequested = true;
2662 }
2663 }
2664
2665public:
2666 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2667 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2668 exitPending(false), exitRequested(false) {
2669 }
2670
2671 status_t waitForResponse() {
2672 do {
2673 looper->pollOnce(-1);
2674 } while (!exitRequested);
2675 return result;
2676 }
2677
2678 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002679 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002680 exitPending = true;
2681 looper->sendMessage(this, Message(MSG_EXIT));
2682 }
2683};
2684
2685
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002686status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2687 const sp<IGraphicBufferProducer>& producer,
2688 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002689 uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002690
2691 if (CC_UNLIKELY(display == 0))
2692 return BAD_VALUE;
2693
2694 if (CC_UNLIKELY(producer == 0))
2695 return BAD_VALUE;
2696
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002697 // if we have secure windows on this display, never allow the screen capture
2698 // unless the producer interface is local (i.e.: we can take a screenshot for
2699 // ourselves).
2700 if (!producer->asBinder()->localBinder()) {
2701 Mutex::Autolock _l(mStateLock);
2702 sp<const DisplayDevice> hw(getDisplayDevice(display));
2703 if (hw->getSecureLayerVisible()) {
2704 ALOGW("FB is protected: PERMISSION_DENIED");
2705 return PERMISSION_DENIED;
2706 }
2707 }
2708
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002709 class MessageCaptureScreen : public MessageBase {
2710 SurfaceFlinger* flinger;
2711 sp<IBinder> display;
2712 sp<IGraphicBufferProducer> producer;
2713 uint32_t reqWidth, reqHeight;
2714 uint32_t minLayerZ,maxLayerZ;
2715 status_t result;
2716 public:
2717 MessageCaptureScreen(SurfaceFlinger* flinger,
2718 const sp<IBinder>& display,
2719 const sp<IGraphicBufferProducer>& producer,
2720 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002721 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002722 : flinger(flinger), display(display), producer(producer),
2723 reqWidth(reqWidth), reqHeight(reqHeight),
2724 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2725 result(PERMISSION_DENIED)
2726 {
2727 }
2728 status_t getResult() const {
2729 return result;
2730 }
2731 virtual bool handler() {
2732 Mutex::Autolock _l(flinger->mStateLock);
2733 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002734 result = flinger->captureScreenImplLocked(hw,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002735 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002736 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002737 return true;
2738 }
2739 };
2740
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002741 // make sure to process transactions before screenshots -- a transaction
2742 // might already be pending but scheduled for VSYNC; this guarantees we
2743 // will handle it before the screenshot. When VSYNC finally arrives
2744 // the scheduled transaction will be a no-op. If no transactions are
2745 // scheduled at this time, this will end-up being a no-op as well.
2746 mEventQueue.invalidateTransactionNow();
2747
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002748 // this creates a "fake" BBinder which will serve as a "fake" remote
2749 // binder to receive the marshaled calls and forward them to the
2750 // real remote (a BpGraphicBufferProducer)
2751 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2752
2753 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2754 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002755 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002756 display, IGraphicBufferProducer::asInterface( wrapper ),
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002757 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002758
2759 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002760 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002761 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002762 }
2763 return res;
2764}
2765
Mathias Agopian180f10d2013-04-10 22:55:41 -07002766
2767void SurfaceFlinger::renderScreenImplLocked(
2768 const sp<const DisplayDevice>& hw,
2769 uint32_t reqWidth, uint32_t reqHeight,
2770 uint32_t minLayerZ, uint32_t maxLayerZ,
2771 bool yswap)
2772{
2773 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002774 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002775
2776 // get screen geometry
2777 const uint32_t hw_w = hw->getWidth();
2778 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002779 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2780
2781 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002782 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002783
2784 // set-up our viewport
Mathias Agopian3f844832013-08-07 21:24:32 -07002785 engine.setViewportAndProjection(reqWidth, reqHeight, hw_w, hw_h, yswap);
2786 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002787
2788 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002789 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002790
2791 const LayerVector& layers( mDrawingState.layersSortedByZ );
2792 const size_t count = layers.size();
2793 for (size_t i=0 ; i<count ; ++i) {
2794 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002795 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002796 if (state.layerStack == hw->getLayerStack()) {
2797 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2798 if (layer->isVisible()) {
2799 if (filtering) layer->setFiltering(true);
2800 layer->draw(hw);
2801 if (filtering) layer->setFiltering(false);
2802 }
2803 }
2804 }
2805 }
2806
2807 // compositionComplete is needed for older driver
2808 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002809 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002810}
2811
2812
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002813status_t SurfaceFlinger::captureScreenImplLocked(
2814 const sp<const DisplayDevice>& hw,
2815 const sp<IGraphicBufferProducer>& producer,
2816 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002817 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002818{
2819 ATRACE_CALL();
2820
Mathias Agopian180f10d2013-04-10 22:55:41 -07002821 // get screen geometry
2822 const uint32_t hw_w = hw->getWidth();
2823 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002824
Mathias Agopian180f10d2013-04-10 22:55:41 -07002825 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2826 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2827 reqWidth, reqHeight, hw_w, hw_h);
2828 return BAD_VALUE;
2829 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002830
Mathias Agopian180f10d2013-04-10 22:55:41 -07002831 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2832 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2833
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002834 // create a surface (because we're a producer, and we need to
2835 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07002836 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002837 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002838
2839 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002840 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002841 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2842 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002843
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002844 int err = 0;
2845 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07002846 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002847 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2848 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002849
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002850 if (err == NO_ERROR) {
2851 ANativeWindowBuffer* buffer;
2852 /* TODO: Once we have the sync framework everywhere this can use
2853 * server-side waits on the fence that dequeueBuffer returns.
2854 */
2855 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2856 if (result == NO_ERROR) {
2857 // create an EGLImage from the buffer so we can later
2858 // turn it into a texture
2859 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2860 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2861 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002862 // this binds the given EGLImage as a framebuffer for the
2863 // duration of this scope.
2864 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2865 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002866 // this will in fact render into our dequeued buffer
2867 // via an FBO, which means we didn't have to create
2868 // an EGLSurface and therefore we're not
2869 // dependent on the context's EGLConfig.
2870 renderScreenImplLocked(hw, reqWidth, reqHeight,
2871 minLayerZ, maxLayerZ, true);
Mathias Agopiand5556842013-09-19 17:08:37 -07002872
2873 if (DEBUG_SCREENSHOTS) {
2874 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
2875 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
2876 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
2877 hw, minLayerZ, maxLayerZ);
2878 delete [] pixels;
2879 }
2880
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002881 } else {
2882 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2883 result = INVALID_OPERATION;
2884 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002885 // destroy our image
2886 eglDestroyImageKHR(mEGLDisplay, image);
2887 } else {
2888 result = BAD_VALUE;
2889 }
2890 window->queueBuffer(window, buffer, -1);
2891 }
2892 } else {
2893 result = BAD_VALUE;
2894 }
2895 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2896 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002897
Mathias Agopian74c40c02010-09-29 13:02:36 -07002898 return result;
2899}
2900
Mathias Agopiand5556842013-09-19 17:08:37 -07002901void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
2902 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07002903 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07002904 for (size_t y=0 ; y<h ; y++) {
2905 uint32_t const * p = (uint32_t const *)vaddr + y*s;
2906 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07002907 if (p[x] != 0xFF000000) return;
2908 }
2909 }
2910 ALOGE("*** we just took a black screenshot ***\n"
2911 "requested minz=%d, maxz=%d, layerStack=%d",
2912 minLayerZ, maxLayerZ, hw->getLayerStack());
2913 const LayerVector& layers( mDrawingState.layersSortedByZ );
2914 const size_t count = layers.size();
2915 for (size_t i=0 ; i<count ; ++i) {
2916 const sp<Layer>& layer(layers[i]);
2917 const Layer::State& state(layer->getDrawingState());
2918 const bool visible = (state.layerStack == hw->getLayerStack())
2919 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2920 && (layer->isVisible());
2921 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
2922 visible ? '+' : '-',
2923 i, layer->getName().string(), state.layerStack, state.z,
2924 layer->isVisible(), state.flags, state.alpha);
2925 }
2926 }
2927}
2928
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002929// ---------------------------------------------------------------------------
2930
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002931SurfaceFlinger::LayerVector::LayerVector() {
2932}
2933
2934SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002935 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002936}
2937
2938int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2939 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002940{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002941 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002942 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2943 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002944
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002945 uint32_t ls = l->getCurrentState().layerStack;
2946 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002947 if (ls != rs)
2948 return ls - rs;
2949
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002950 uint32_t lz = l->getCurrentState().z;
2951 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002952 if (lz != rz)
2953 return lz - rz;
2954
2955 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002956}
2957
2958// ---------------------------------------------------------------------------
2959
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002960SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2961 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002962}
2963
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002964SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002965 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002966 viewport.makeInvalid();
2967 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002968}
2969
2970// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002971
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002972}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002973
2974
2975#if defined(__gl_h_)
2976#error "don't include gl/gl.h in this file"
2977#endif
2978
2979#if defined(__gl2_h_)
2980#error "don't include gl2/gl2.h in this file"
2981#endif