blob: a15114213a9e305cae09ddb52c020e1e74bf7353 [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
Mathias Agopian4f4f0942013-08-19 17:26:18 -070025#if defined(HAVE_PTHREADS)
26#include <sys/resource.h>
27#endif
28
Mathias Agopian921e6ac2012-07-23 23:11:29 -070029#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include <cutils/log.h>
32#include <cutils/properties.h>
33
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/IPCThreadState.h>
35#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070037#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070038
Mathias Agopianc666cae2012-07-25 18:56:13 -070039#include <ui/DisplayInfo.h>
40
Mathias Agopian921e6ac2012-07-23 23:11:29 -070041#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070042#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070043#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070044#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080045#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080046#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070047
48#include <ui/GraphicBufferAllocator.h>
49#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070050#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080051
Mathias Agopiancde87a32012-09-13 14:09:01 -070052#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053#include <utils/String8.h>
54#include <utils/String16.h>
55#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080056#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
Mathias Agopian921e6ac2012-07-23 23:11:29 -070058#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070059#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
Mathias Agopian3e25fd82013-04-22 17:52:16 +020061#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020063#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080064#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070065#include "DisplayDevice.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080066#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070
Mathias Agopiana4912602012-07-12 14:25:33 -070071#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070072#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070073#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074
Mathias Agopian875d8e12013-06-07 15:35:48 -070075#include "RenderEngine/RenderEngine.h"
76
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077#define DISPLAY_COUNT 1
78
Mathias Agopianfee2b462013-07-03 12:34:01 -070079/*
80 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
81 * black pixels.
82 */
83#define DEBUG_SCREENSHOTS false
84
Mathias Agopianca088332013-03-28 17:44:13 -070085EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
86
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088// ---------------------------------------------------------------------------
89
Mathias Agopian99b49842011-06-27 16:05:52 -070090const String16 sHardwareTest("android.permission.HARDWARE_TEST");
91const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
92const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
93const String16 sDump("android.permission.DUMP");
94
95// ---------------------------------------------------------------------------
96
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -070098 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700100 mTransactionPending(false),
101 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700102 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700103 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700104 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700107 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800108 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700110 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700111 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700112 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700113 mDebugInSwapBuffers(0),
114 mLastSwapBufferTime(0),
115 mDebugInTransaction(0),
116 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700117 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118{
Steve Blocka19954a2012-01-04 20:05:49 +0000119 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120
121 // debugging stuff...
122 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700123
Mathias Agopianb4b17302013-03-20 18:36:41 -0700124 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700125 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700126
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127 property_get("debug.sf.showupdates", value, "0");
128 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700129
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700130 property_get("debug.sf.ddms", value, "0");
131 mDebugDDMS = atoi(value);
132 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700133 if (!startDdmConnection()) {
134 // start failed, and DDMS debugging not enabled
135 mDebugDDMS = 0;
136 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700137 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700138 ALOGI_IF(mDebugRegion, "showupdates enabled");
139 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140}
141
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800142void SurfaceFlinger::onFirstRef()
143{
144 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800145}
146
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147SurfaceFlinger::~SurfaceFlinger()
148{
Mathias Agopiana4912602012-07-12 14:25:33 -0700149 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
150 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
151 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152}
153
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800154void SurfaceFlinger::binderDied(const wp<IBinder>& who)
155{
156 // the window manager died on us. prepare its eulogy.
157
Andy McFadden13a082e2012-08-24 10:16:42 -0700158 // restore initial conditions (default device unblank, etc)
159 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800160
161 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700162 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800163}
164
Mathias Agopian7e27f052010-05-28 14:22:23 -0700165sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166{
Mathias Agopian96f08192010-06-02 23:28:45 -0700167 sp<ISurfaceComposerClient> bclient;
168 sp<Client> client(new Client(this));
169 status_t err = client->initCheck();
170 if (err == NO_ERROR) {
171 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 return bclient;
174}
175
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700176sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
177 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700178{
179 class DisplayToken : public BBinder {
180 sp<SurfaceFlinger> flinger;
181 virtual ~DisplayToken() {
182 // no more references, this display must be terminated
183 Mutex::Autolock _l(flinger->mStateLock);
184 flinger->mCurrentState.displays.removeItem(this);
185 flinger->setTransactionFlags(eDisplayTransactionNeeded);
186 }
187 public:
188 DisplayToken(const sp<SurfaceFlinger>& flinger)
189 : flinger(flinger) {
190 }
191 };
192
193 sp<BBinder> token = new DisplayToken(this);
194
195 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700196 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700197 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700198 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700199 mCurrentState.displays.add(token, info);
200
201 return token;
202}
203
Jesse Hall6c913be2013-08-08 12:15:49 -0700204void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
205 Mutex::Autolock _l(mStateLock);
206
207 ssize_t idx = mCurrentState.displays.indexOfKey(display);
208 if (idx < 0) {
209 ALOGW("destroyDisplay: invalid display token");
210 return;
211 }
212
213 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
214 if (!info.isVirtualDisplay()) {
215 ALOGE("destroyDisplay called for non-virtual display");
216 return;
217 }
218
219 mCurrentState.displays.removeItemsAt(idx);
220 setTransactionFlags(eDisplayTransactionNeeded);
221}
222
Jesse Hall692c7232012-11-08 15:41:56 -0800223void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
224 ALOGW_IF(mBuiltinDisplays[type],
225 "Overwriting display token for display type %d", type);
226 mBuiltinDisplays[type] = new BBinder();
227 DisplayDeviceState info(type);
228 // All non-virtual displays are currently considered secure.
229 info.isSecure = true;
230 mCurrentState.displays.add(mBuiltinDisplays[type], info);
231}
232
Mathias Agopiane57f2922012-08-09 16:29:12 -0700233sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700234 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700235 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
236 return NULL;
237 }
Jesse Hall692c7232012-11-08 15:41:56 -0800238 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700239}
240
Jamie Gennis9a78c902011-01-12 18:30:40 -0800241sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
242{
243 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
244 return gba;
245}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700246
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247void SurfaceFlinger::bootFinished()
248{
249 const nsecs_t now = systemTime();
250 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000251 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700252 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700253
254 // wait patiently for the window manager death
255 const String16 name("window");
256 sp<IBinder> window(defaultServiceManager()->getService(name));
257 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700258 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700259 }
260
261 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700262 // formerly we would just kill the process, but we now ask it to exit so it
263 // can choose where to stop the animation.
264 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265}
266
Mathias Agopian3f844832013-08-07 21:24:32 -0700267void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700268 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700269 RenderEngine& engine;
270 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700271 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700272 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
273 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700274 }
275 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700276 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700277 return true;
278 }
279 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700280 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700281}
282
Mathias Agopian722b98f2012-09-25 18:24:31 -0700283status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700284 EGLDisplay dpy,
285 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700286 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700287 EGLConfig* outConfig)
288{
289 EGLConfig config = NULL;
290 EGLint numConfigs = -1, n=0;
291 eglGetConfigs(dpy, NULL, 0, &numConfigs);
292 EGLConfig* const configs = new EGLConfig[numConfigs];
293 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700294
Mathias Agopian722b98f2012-09-25 18:24:31 -0700295 if (n) {
296 if (attribute != EGL_NONE) {
297 for (int i=0 ; i<n ; i++) {
298 EGLint value = 0;
299 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
300 if (wanted == value) {
301 *outConfig = configs[i];
302 delete [] configs;
303 return NO_ERROR;
304 }
305 }
306 } else {
307 // just pick the first one
308 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700309 delete [] configs;
310 return NO_ERROR;
311 }
312 }
313 delete [] configs;
314 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315}
316
Mathias Agopian722b98f2012-09-25 18:24:31 -0700317class EGLAttributeVector {
318 struct Attribute;
319 class Adder;
320 friend class Adder;
321 KeyedVector<Attribute, EGLint> mList;
322 struct Attribute {
323 Attribute() {};
324 Attribute(EGLint v) : v(v) { }
325 EGLint v;
326 bool operator < (const Attribute& other) const {
327 // this places EGL_NONE at the end
328 EGLint lhs(v);
329 EGLint rhs(other.v);
330 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
331 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
332 return lhs < rhs;
333 }
334 };
335 class Adder {
336 friend class EGLAttributeVector;
337 EGLAttributeVector& v;
338 EGLint attribute;
339 Adder(EGLAttributeVector& v, EGLint attribute)
340 : v(v), attribute(attribute) {
341 }
342 public:
343 void operator = (EGLint value) {
344 if (attribute != EGL_NONE) {
345 v.mList.add(attribute, value);
346 }
347 }
348 operator EGLint () const { return v.mList[attribute]; }
349 };
350public:
351 EGLAttributeVector() {
352 mList.add(EGL_NONE, EGL_NONE);
353 }
354 void remove(EGLint attribute) {
355 if (attribute != EGL_NONE) {
356 mList.removeItem(attribute);
357 }
358 }
359 Adder operator [] (EGLint attribute) {
360 return Adder(*this, attribute);
361 }
362 EGLint operator [] (EGLint attribute) const {
363 return mList[attribute];
364 }
365 // cast-operator to (EGLint const*)
366 operator EGLint const* () const { return &mList.keyAt(0).v; }
367};
368
Mathias Agopiana4912602012-07-12 14:25:33 -0700369EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
370 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
371 // it is to be used with WIFI displays
372 EGLConfig config;
373 EGLint dummy;
374 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700375
Mathias Agopian722b98f2012-09-25 18:24:31 -0700376 EGLAttributeVector attribs;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700377 // TODO: enable ES2
378 //attribs[EGL_RENDERABLE_TYPE] = EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT;
379 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
Mathias Agopian722b98f2012-09-25 18:24:31 -0700380 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
381 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
382 attribs[EGL_RED_SIZE] = 8;
383 attribs[EGL_GREEN_SIZE] = 8;
384 attribs[EGL_BLUE_SIZE] = 8;
385
386 err = selectConfigForAttribute(display, attribs, EGL_NONE, EGL_NONE, &config);
387 if (!err)
388 goto success;
389
Mathias Agopian875d8e12013-06-07 15:35:48 -0700390 // this didn't work, probably because we're on the emulator...
391 // try a simplified query
392 ALOGW("no suitable EGLConfig found, trying a simpler query");
393 attribs.remove(EGL_RENDERABLE_TYPE);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700394 attribs.remove(EGL_FRAMEBUFFER_TARGET_ANDROID);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700395 attribs.remove(EGL_RECORDABLE_ANDROID);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700396 attribs.remove(EGL_RED_SIZE);
397 attribs.remove(EGL_GREEN_SIZE);
398 attribs.remove(EGL_BLUE_SIZE);
399 err = selectConfigForAttribute(display, attribs,
400 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700401 if (!err)
402 goto success;
403
404 // this EGL is too lame for Android
Mathias Agopian875d8e12013-06-07 15:35:48 -0700405 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
Jesse Hallf21cffa2012-09-19 21:00:49 -0700406 return 0;
407
408success:
409 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700410 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700411 return config;
412}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700414void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700415
Mathias Agopiana4912602012-07-12 14:25:33 -0700416 ALOGI( "SurfaceFlinger's main thread ready to run. "
417 "Initializing graphics H/W...");
418
Jesse Hall692c7232012-11-08 15:41:56 -0800419 Mutex::Autolock _l(mStateLock);
420
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700421 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700422 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
423 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700424
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700425 // Initialize the H/W composer object. There may or may not be an
426 // actual hardware composer underneath.
427 mHwc = new HWComposer(this,
428 *static_cast<HWComposer::EventHandler *>(this));
429
Mathias Agopian875d8e12013-06-07 15:35:48 -0700430 // initialize the config and context (can't fail)
431 mEGLConfig = selectEGLConfig(mEGLDisplay, mHwc->getVisualID());
432
433 // print some debugging info
434 EGLint r,g,b,a;
435 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_RED_SIZE, &r);
436 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_GREEN_SIZE, &g);
437 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_BLUE_SIZE, &b);
438 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_ALPHA_SIZE, &a);
439 ALOGI("EGL informations:");
440 ALOGI("vendor : %s", eglQueryString(mEGLDisplay, EGL_VENDOR));
441 ALOGI("version : %s", eglQueryString(mEGLDisplay, EGL_VERSION));
442 ALOGI("extensions: %s", eglQueryString(mEGLDisplay, EGL_EXTENSIONS));
443 ALOGI("Client API: %s", eglQueryString(mEGLDisplay, EGL_CLIENT_APIS)?:"Not Supported");
444 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
445
446 // get a RenderEngine for the given display / config (can't fail)
447 mRenderEngine = RenderEngine::create(mEGLDisplay, mEGLConfig);
448
449 // retrieve the EGL context that was selected/created
450 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700451
Mathias Agopianed985572013-03-22 00:24:39 -0700452 // figure out which format we got
453 eglGetConfigAttrib(mEGLDisplay, mEGLConfig,
454 EGL_NATIVE_VISUAL_ID, &mEGLNativeVisualId);
455
Mathias Agopianda27af92012-09-13 18:17:13 -0700456 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
457 "couldn't create EGLContext");
458
Mathias Agopiancde87a32012-09-13 14:09:01 -0700459 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700460 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700461 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700462 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700463 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700464 // All non-virtual displays are currently considered secure.
465 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800466 createBuiltinDisplayLocked(type);
467 wp<IBinder> token = mBuiltinDisplays[i];
468
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700469 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
470 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i, bq);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700471 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700472 type, allocateHwcDisplayId(type), isSecure, token,
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700473 fbs, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700474 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700475 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700476 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700477 // for displays other than the main display, so we always
478 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700479 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700480 hw->acquireScreen();
481 }
482 mDisplays.add(token, hw);
483 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700484 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700485
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700486 // make the GLContext current so that we can create textures when creating Layers
487 // (which may happens before we render something)
488 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
489
Mathias Agopian028508c2012-07-25 21:12:12 -0700490 // start the EventThread
491 mEventThread = new EventThread(this);
492 mEventQueue.setEventThread(mEventThread);
493
Mathias Agopian92a979a2012-08-02 18:32:23 -0700494 // initialize our drawing state
495 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700496
Andy McFadden13a082e2012-08-24 10:16:42 -0700497 // set initial conditions (e.g. unblank default device)
498 initializeDisplays();
499
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700500 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700501 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800502}
503
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700504int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700505 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700506 type : mHwc->allocateDisplayId();
507}
508
Mathias Agopiana67e4182012-06-19 17:26:12 -0700509void SurfaceFlinger::startBootAnim() {
510 // start boot animation
511 property_set("service.bootanim.exit", "0");
512 property_set("ctl.start", "bootanim");
513}
514
Mathias Agopian875d8e12013-06-07 15:35:48 -0700515size_t SurfaceFlinger::getMaxTextureSize() const {
516 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700517}
518
Mathias Agopian875d8e12013-06-07 15:35:48 -0700519size_t SurfaceFlinger::getMaxViewportDims() const {
520 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700521}
522
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800523// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800524
Jamie Gennis582270d2011-08-17 18:19:00 -0700525bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800526 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800527 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800528 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700529 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800530}
531
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700532status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800533 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700534 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800535 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700536 type = i;
537 break;
538 }
539 }
540
541 if (type < 0) {
542 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700543 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700544
545 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700546 float xdpi = hwc.getDpiX(type);
547 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700548
549 // TODO: Not sure if display density should handled by SF any longer
550 class Density {
551 static int getDensityFromProperty(char const* propName) {
552 char property[PROPERTY_VALUE_MAX];
553 int density = 0;
554 if (property_get(propName, property, NULL) > 0) {
555 density = atoi(property);
556 }
557 return density;
558 }
559 public:
560 static int getEmuDensity() {
561 return getDensityFromProperty("qemu.sf.lcd_density"); }
562 static int getBuildDensity() {
563 return getDensityFromProperty("ro.sf.lcd_density"); }
564 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700565
566 if (type == DisplayDevice::DISPLAY_PRIMARY) {
567 // The density of the device is provided by a build property
568 float density = Density::getBuildDensity() / 160.0f;
569 if (density == 0) {
570 // the build doesn't provide a density -- this is wrong!
571 // use xdpi instead
572 ALOGE("ro.sf.lcd_density must be defined as a build property");
573 density = xdpi / 160.0f;
574 }
575 if (Density::getEmuDensity()) {
576 // if "qemu.sf.lcd_density" is specified, it overrides everything
577 xdpi = ydpi = density = Density::getEmuDensity();
578 density /= 160.0f;
579 }
580 info->density = density;
581
582 // TODO: this needs to go away (currently needed only by webkit)
583 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
584 info->orientation = hw->getOrientation();
Mathias Agopian1604f772012-09-18 21:54:42 -0700585 } else {
586 // TODO: where should this value come from?
587 static const int TV_DENSITY = 213;
588 info->density = TV_DENSITY / 160.0f;
589 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700590 }
591
Mathias Agopian1604f772012-09-18 21:54:42 -0700592 info->w = hwc.getWidth(type);
593 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700594 info->xdpi = xdpi;
595 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700596 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700597
598 // All non-virtual displays are currently considered secure.
599 info->secure = true;
600
Mathias Agopian888c8222012-08-04 21:10:38 -0700601 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700602}
603
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800604// ----------------------------------------------------------------------------
605
606sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800607 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700608}
609
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800610// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800611
612void SurfaceFlinger::waitForEvent() {
613 mEventQueue.waitMessage();
614}
615
616void SurfaceFlinger::signalTransaction() {
617 mEventQueue.invalidate();
618}
619
620void SurfaceFlinger::signalLayerUpdate() {
621 mEventQueue.invalidate();
622}
623
624void SurfaceFlinger::signalRefresh() {
625 mEventQueue.refresh();
626}
627
628status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
629 nsecs_t reltime, uint32_t flags) {
630 return mEventQueue.postMessage(msg, reltime);
631}
632
633status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
634 nsecs_t reltime, uint32_t flags) {
635 status_t res = mEventQueue.postMessage(msg, reltime);
636 if (res == NO_ERROR) {
637 msg->wait();
638 }
639 return res;
640}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700642void SurfaceFlinger::run() {
643#if defined(HAVE_PTHREADS)
644 setpriority(PRIO_PROCESS, 0, PRIORITY_URGENT_DISPLAY);
645#endif
646 do {
647 waitForEvent();
648 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800649}
650
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700651void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700652 if (mEventThread == NULL) {
653 // This is a temporary workaround for b/7145521. A non-null pointer
654 // does not mean EventThread has finished initializing, so this
655 // is not a correct fix.
656 ALOGW("WARNING: EventThread not started, ignoring vsync");
657 return;
658 }
Jesse Hall9e663de2013-08-16 14:28:37 -0700659 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700660 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700661 mEventThread->onVSyncReceived(type, timestamp);
662 }
663}
664
665void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
666 if (mEventThread == NULL) {
667 // This is a temporary workaround for b/7145521. A non-null pointer
668 // does not mean EventThread has finished initializing, so this
669 // is not a correct fix.
670 ALOGW("WARNING: EventThread not started, ignoring hotplug");
671 return;
672 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700673
Jesse Hall9e663de2013-08-16 14:28:37 -0700674 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700675 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800676 if (connected) {
677 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700678 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800679 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
680 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700681 }
682 setTransactionFlags(eDisplayTransactionNeeded);
683
Andy McFadden9e9689c2012-10-10 18:17:51 -0700684 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700685 }
Mathias Agopian86303202012-07-24 22:46:10 -0700686}
687
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700688void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
689 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700690}
691
Mathias Agopian4fec8732012-06-29 14:12:52 -0700692void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800693 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800694 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700695 case MessageQueue::TRANSACTION:
696 handleMessageTransaction();
697 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700698 case MessageQueue::INVALIDATE:
699 handleMessageTransaction();
700 handleMessageInvalidate();
701 signalRefresh();
702 break;
703 case MessageQueue::REFRESH:
704 handleMessageRefresh();
705 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800706 }
707}
708
Mathias Agopian4fec8732012-06-29 14:12:52 -0700709void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700710 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700711 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700712 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700713 }
714}
715
716void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700717 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700718 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700719}
720
721void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700722 ATRACE_CALL();
723 preComposition();
724 rebuildLayerStacks();
725 setUpHWComposer();
726 doDebugFlashRegions();
727 doComposition();
728 postComposition();
729}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700730
Mathias Agopiancd60f992012-08-16 16:28:27 -0700731void SurfaceFlinger::doDebugFlashRegions()
732{
733 // is debugging enabled
734 if (CC_LIKELY(!mDebugRegion))
735 return;
736
737 const bool repaintEverything = mRepaintEverything;
738 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
739 const sp<DisplayDevice>& hw(mDisplays[dpy]);
740 if (hw->canDraw()) {
741 // transform the dirty region into this screen's coordinate space
742 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
743 if (!dirtyRegion.isEmpty()) {
744 // redraw the whole screen
745 doComposeSurfaces(hw, Region(hw->bounds()));
746
747 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700748 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700749 RenderEngine& engine(getRenderEngine());
750 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
751
Mathias Agopiancd60f992012-08-16 16:28:27 -0700752 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700753 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700754 }
755 }
756 }
757
758 postFramebuffer();
759
760 if (mDebugRegion > 1) {
761 usleep(mDebugRegion * 1000);
762 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700763
764 HWComposer& hwc(getHwComposer());
765 if (hwc.initCheck() == NO_ERROR) {
766 status_t err = hwc.prepare();
767 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
768 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700769}
770
771void SurfaceFlinger::preComposition()
772{
773 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700774 const LayerVector& layers(mDrawingState.layersSortedByZ);
775 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700776 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700777 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700778 needExtraInvalidate = true;
779 }
780 }
781 if (needExtraInvalidate) {
782 signalLayerUpdate();
783 }
784}
785
786void SurfaceFlinger::postComposition()
787{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700788 const LayerVector& layers(mDrawingState.layersSortedByZ);
789 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700790 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700791 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700792 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800793
794 if (mAnimCompositionPending) {
795 mAnimCompositionPending = false;
796
797 const HWComposer& hwc = getHwComposer();
798 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800799 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800800 mAnimFrameTracker.setActualPresentFence(presentFence);
801 } else {
802 // The HWC doesn't support present fences, so use the refresh
803 // timestamp instead.
804 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
805 mAnimFrameTracker.setActualPresentTime(presentTime);
806 }
807 mAnimFrameTracker.advanceFrame();
808 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700809}
810
811void SurfaceFlinger::rebuildLayerStacks() {
812 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700813 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700814 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700815 mVisibleRegionsDirty = false;
816 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700817
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700818 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700819 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700820 Region opaqueRegion;
821 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800822 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700823 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700824 const Transform& tr(hw->getTransform());
825 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700826 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700827 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700828 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700829
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700830 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700831 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700832 const sp<Layer>& layer(layers[i]);
833 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700834 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700835 Region drawRegion(tr.transform(
836 layer->visibleNonTransparentRegion));
837 drawRegion.andSelf(bounds);
838 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700839 layersSortedByZ.add(layer);
840 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700841 }
842 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700843 }
Mathias Agopian42977342012-08-05 00:40:46 -0700844 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700845 hw->undefinedRegion.set(bounds);
846 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
847 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700848 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700849 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700850}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700851
Mathias Agopiancd60f992012-08-16 16:28:27 -0700852void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700853 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
854 mDisplays[dpy]->beginFrame();
855 }
856
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700857 HWComposer& hwc(getHwComposer());
858 if (hwc.initCheck() == NO_ERROR) {
859 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700860 if (CC_UNLIKELY(mHwWorkListDirty)) {
861 mHwWorkListDirty = false;
862 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
863 sp<const DisplayDevice> hw(mDisplays[dpy]);
864 const int32_t id = hw->getHwcDisplayId();
865 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800866 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700867 hw->getVisibleLayersSortedByZ());
868 const size_t count = currentLayers.size();
869 if (hwc.createWorkList(id, count) == NO_ERROR) {
870 HWComposer::LayerListIterator cur = hwc.begin(id);
871 const HWComposer::LayerListIterator end = hwc.end(id);
872 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800873 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700874 layer->setGeometry(hw, *cur);
875 if (mDebugDisableHWC || mDebugRegion) {
876 cur->setSkip(true);
877 }
878 }
879 }
880 }
881 }
882 }
883
884 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700885 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700886 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700887 const int32_t id = hw->getHwcDisplayId();
888 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800889 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700890 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700891 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700892 HWComposer::LayerListIterator cur = hwc.begin(id);
893 const HWComposer::LayerListIterator end = hwc.end(id);
894 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
895 /*
896 * update the per-frame h/w composer data for each layer
897 * and build the transparent region of the FB
898 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800899 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700900 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700901 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700902 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700903 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700904
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700905 status_t err = hwc.prepare();
906 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700907
908 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
909 sp<const DisplayDevice> hw(mDisplays[dpy]);
910 hw->prepareFrame(hwc);
911 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700912 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700913}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700914
Mathias Agopiancd60f992012-08-16 16:28:27 -0700915void SurfaceFlinger::doComposition() {
916 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700917 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700918 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700919 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700920 if (hw->canDraw()) {
921 // transform the dirty region into this screen's coordinate space
922 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800923
924 // repaint the framebuffer (if needed)
925 doDisplayComposition(hw, dirtyRegion);
926
Mathias Agopiancd60f992012-08-16 16:28:27 -0700927 hw->dirtyRegion.clear();
928 hw->flip(hw->swapRegion);
929 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700930 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700931 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700932 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700933 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700934 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700935}
936
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800937void SurfaceFlinger::postFramebuffer()
938{
Mathias Agopian841cde52012-03-01 15:44:37 -0800939 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800940
Mathias Agopiana44b0412011-10-16 18:46:35 -0700941 const nsecs_t now = systemTime();
942 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700943
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700944 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700945 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700946 if (!hwc.supportsFramebufferTarget()) {
947 // EGL spec says:
948 // "surface must be bound to the calling thread's current context,
949 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700950 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700951 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700952 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700953 }
954
Mathias Agopian92a979a2012-08-02 18:32:23 -0700955 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700956 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800957 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700958 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700959 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700960 int32_t id = hw->getHwcDisplayId();
961 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700962 HWComposer::LayerListIterator cur = hwc.begin(id);
963 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700964 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700965 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700966 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700967 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700968 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700969 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700970 }
Jesse Hallef194142012-06-14 14:45:17 -0700971 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800972 }
973
Mathias Agopiana44b0412011-10-16 18:46:35 -0700974 mLastSwapBufferTime = systemTime() - now;
975 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700976
977 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
978 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
979 logFrameStats();
980 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800981}
982
Mathias Agopian87baae12012-07-31 12:38:26 -0700983void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800984{
Mathias Agopian841cde52012-03-01 15:44:37 -0800985 ATRACE_CALL();
986
Mathias Agopian7cc6df52013-06-05 14:30:54 -0700987 // here we keep a copy of the drawing state (that is the state that's
988 // going to be overwritten by handleTransactionLocked()) outside of
989 // mStateLock so that the side-effects of the State assignment
990 // don't happen with mStateLock held (which can cause deadlocks).
991 State drawingState(mDrawingState);
992
Mathias Agopianca4d3602011-05-19 15:38:14 -0700993 Mutex::Autolock _l(mStateLock);
994 const nsecs_t now = systemTime();
995 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996
Mathias Agopianca4d3602011-05-19 15:38:14 -0700997 // Here we're guaranteed that some transaction flags are set
998 // so we can call handleTransactionLocked() unconditionally.
999 // We call getTransactionFlags(), which will also clear the flags,
1000 // with mStateLock held to guarantee that mCurrentState won't change
1001 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001002
Mathias Agopiane57f2922012-08-09 16:29:12 -07001003 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001004 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001005
Mathias Agopianca4d3602011-05-19 15:38:14 -07001006 mLastTransactionTime = systemTime() - now;
1007 mDebugInTransaction = 0;
1008 invalidateHwcGeometry();
1009 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001010}
1011
Mathias Agopian87baae12012-07-31 12:38:26 -07001012void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001013{
1014 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001015 const size_t count = currentLayers.size();
1016
1017 /*
1018 * Traversal of the children
1019 * (perform the transaction for each of them if needed)
1020 */
1021
Mathias Agopian3559b072012-08-15 13:46:03 -07001022 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001023 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001024 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001025 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1026 if (!trFlags) continue;
1027
1028 const uint32_t flags = layer->doTransaction(0);
1029 if (flags & Layer::eVisibleRegion)
1030 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001031 }
1032 }
1033
1034 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001035 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001036 */
1037
Mathias Agopiane57f2922012-08-09 16:29:12 -07001038 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001039 // here we take advantage of Vector's copy-on-write semantics to
1040 // improve performance by skipping the transaction entirely when
1041 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001042 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1043 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001044 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001045 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001046 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001047 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001048
1049 // find the displays that were removed
1050 // (ie: in drawing state but not in current state)
1051 // also handle displays that changed
1052 // (ie: displays that are in both lists)
1053 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001054 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1055 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001056 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001057 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001058 // Call makeCurrent() on the primary display so we can
1059 // be sure that nothing associated with this display
1060 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001061 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001062 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001063 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1064 if (hw != NULL)
1065 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001066 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001067 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001068 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001069 } else {
1070 ALOGW("trying to remove the main display");
1071 }
1072 } else {
1073 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001074 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001075 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001076 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001077 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001078 // recreating the DisplayDevice, so we just remove it
1079 // from the drawing state, so that it get re-added
1080 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001081 sp<DisplayDevice> hw(getDisplayDevice(display));
1082 if (hw != NULL)
1083 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001084 mDisplays.removeItem(display);
1085 mDrawingState.displays.removeItemsAt(i);
1086 dc--; i--;
1087 // at this point we must loop to the next item
1088 continue;
1089 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001090
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001091 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001092 if (disp != NULL) {
1093 if (state.layerStack != draw[i].layerStack) {
1094 disp->setLayerStack(state.layerStack);
1095 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001096 if ((state.orientation != draw[i].orientation)
1097 || (state.viewport != draw[i].viewport)
1098 || (state.frame != draw[i].frame))
1099 {
1100 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001101 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001102 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001103 }
1104 }
1105 }
1106
1107 // find displays that were added
1108 // (ie: in current state but not in drawing state)
1109 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001110 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001111 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001112
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001113 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001114 sp<IGraphicBufferProducer> producer;
1115 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
1116
Jesse Hall02d86562013-03-25 14:43:23 -07001117 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001118 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001119 // Virtual displays without a surface are dormant:
1120 // they have external state (layer stack, projection,
1121 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001122 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001123
Jesse Hall02d86562013-03-25 14:43:23 -07001124 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001125 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1126 *mHwc, hwcDisplayId, state.surface, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001127 state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001128
1129 dispSurface = vds;
1130 if (hwcDisplayId >= 0) {
1131 producer = vds;
1132 } else {
1133 // There won't be any interaction with HWC for this virtual display,
1134 // so the GLES driver can pass buffers directly to the sink.
1135 producer = state.surface;
1136 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001137 }
1138 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001139 ALOGE_IF(state.surface!=NULL,
1140 "adding a supported display, but rendering "
1141 "surface is provided (%p), ignoring it",
1142 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001143 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001144 // for supported (by hwc) displays we provide our
1145 // own rendering surface
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001146 dispSurface = new FramebufferSurface(*mHwc, state.type, bq);
1147 producer = bq;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001148 }
1149
1150 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001151 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001152 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001153 state.type, hwcDisplayId, state.isSecure,
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001154 display, dispSurface, producer, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001155 hw->setLayerStack(state.layerStack);
1156 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001157 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001158 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001159 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001160 if (state.isVirtualDisplay()) {
1161 if (hwcDisplayId >= 0) {
1162 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1163 hw->getWidth(), hw->getHeight(),
1164 hw->getFormat());
1165 }
1166 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001167 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001168 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001169 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001170 }
1171 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001172 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001173 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001174
Mathias Agopian84300952012-11-21 16:02:13 -08001175 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1176 // The transform hint might have changed for some layers
1177 // (either because a display has changed, or because a layer
1178 // as changed).
1179 //
1180 // Walk through all the layers in currentLayers,
1181 // and update their transform hint.
1182 //
1183 // If a layer is visible only on a single display, then that
1184 // display is used to calculate the hint, otherwise we use the
1185 // default display.
1186 //
1187 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1188 // the hint is set before we acquire a buffer from the surface texture.
1189 //
1190 // NOTE: layer transactions have taken place already, so we use their
1191 // drawing state. However, SurfaceFlinger's own transaction has not
1192 // happened yet, so we must use the current state layer list
1193 // (soon to become the drawing state list).
1194 //
1195 sp<const DisplayDevice> disp;
1196 uint32_t currentlayerStack = 0;
1197 for (size_t i=0; i<count; i++) {
1198 // NOTE: we rely on the fact that layers are sorted by
1199 // layerStack first (so we don't have to traverse the list
1200 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001201 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001202 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001203 if (i==0 || currentlayerStack != layerStack) {
1204 currentlayerStack = layerStack;
1205 // figure out if this layerstack is mirrored
1206 // (more than one display) if so, pick the default display,
1207 // if not, pick the only display it's on.
1208 disp.clear();
1209 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1210 sp<const DisplayDevice> hw(mDisplays[dpy]);
1211 if (hw->getLayerStack() == currentlayerStack) {
1212 if (disp == NULL) {
1213 disp = hw;
1214 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001215 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001216 break;
1217 }
1218 }
1219 }
1220 }
Chet Haase91d25932013-04-11 15:24:55 -07001221 if (disp == NULL) {
1222 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1223 // redraw after transform hint changes. See bug 8508397.
1224
1225 // could be null when this layer is using a layerStack
1226 // that is not visible on any display. Also can occur at
1227 // screen off/on times.
1228 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001229 }
Chet Haase91d25932013-04-11 15:24:55 -07001230 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001231 }
1232 }
1233
1234
Mathias Agopian3559b072012-08-15 13:46:03 -07001235 /*
1236 * Perform our own transaction if needed
1237 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001238
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001239 const LayerVector& layers(mDrawingState.layersSortedByZ);
1240 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001241 // layers have been added
1242 mVisibleRegionsDirty = true;
1243 }
1244
1245 // some layers might have been removed, so
1246 // we need to update the regions they're exposing.
1247 if (mLayersRemoved) {
1248 mLayersRemoved = false;
1249 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001250 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001251 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001252 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001253 if (currentLayers.indexOf(layer) < 0) {
1254 // this layer is not visible anymore
1255 // TODO: we could traverse the tree from front to back and
1256 // compute the actual visible region
1257 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001258 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001259 Region visibleReg = s.transform.transform(
1260 Region(Rect(s.active.w, s.active.h)));
1261 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001262 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001263 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001264 }
1265
1266 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001267}
1268
1269void SurfaceFlinger::commitTransaction()
1270{
1271 if (!mLayersPendingRemoval.isEmpty()) {
1272 // Notify removed layers now that they can't be drawn from
1273 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1274 mLayersPendingRemoval[i]->onRemoved();
1275 }
1276 mLayersPendingRemoval.clear();
1277 }
1278
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001279 // If this transaction is part of a window animation then the next frame
1280 // we composite should be considered an animation as well.
1281 mAnimCompositionPending = mAnimTransactionPending;
1282
Mathias Agopian4fec8732012-06-29 14:12:52 -07001283 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001284 mTransactionPending = false;
1285 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001286 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001287}
1288
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001290 const LayerVector& currentLayers, uint32_t layerStack,
1291 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292{
Mathias Agopian841cde52012-03-01 15:44:37 -08001293 ATRACE_CALL();
1294
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001295 Region aboveOpaqueLayers;
1296 Region aboveCoveredLayers;
1297 Region dirty;
1298
Mathias Agopian87baae12012-07-31 12:38:26 -07001299 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300
1301 size_t i = currentLayers.size();
1302 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001303 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304
1305 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001306 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307
Jesse Hall01e29052013-02-19 16:13:35 -08001308 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001309 if (s.layerStack != layerStack)
1310 continue;
1311
Mathias Agopianab028732010-03-16 16:41:46 -07001312 /*
1313 * opaqueRegion: area of a surface that is fully opaque.
1314 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001316
1317 /*
1318 * visibleRegion: area of a surface that is visible on screen
1319 * and not fully transparent. This is essentially the layer's
1320 * footprint minus the opaque regions above it.
1321 * Areas covered by a translucent surface are considered visible.
1322 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001323 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001324
1325 /*
1326 * coveredRegion: area of a surface that is covered by all
1327 * visible regions above it (which includes the translucent areas).
1328 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001329 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001330
Jesse Halla8026d22012-09-25 13:25:04 -07001331 /*
1332 * transparentRegion: area of a surface that is hinted to be completely
1333 * transparent. This is only used to tell when the layer has no visible
1334 * non-transparent regions and can be removed from the layer list. It
1335 * does not affect the visibleRegion of this layer or any layers
1336 * beneath it. The hint may not be correct if apps don't respect the
1337 * SurfaceView restrictions (which, sadly, some don't).
1338 */
1339 Region transparentRegion;
1340
Mathias Agopianab028732010-03-16 16:41:46 -07001341
1342 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001343 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001344 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001345 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001346 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001347 if (!visibleRegion.isEmpty()) {
1348 // Remove the transparent area from the visible region
1349 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001350 const Transform tr(s.transform);
1351 if (tr.transformed()) {
1352 if (tr.preserveRects()) {
1353 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001354 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001355 } else {
1356 // transformation too complex, can't do the
1357 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001358 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001359 }
1360 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001361 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001362 }
Mathias Agopianab028732010-03-16 16:41:46 -07001363 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001364
Mathias Agopianab028732010-03-16 16:41:46 -07001365 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001366 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001367 if (s.alpha==255 && !translucent &&
1368 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1369 // the opaque region is the layer's footprint
1370 opaqueRegion = visibleRegion;
1371 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001372 }
1373 }
1374
Mathias Agopianab028732010-03-16 16:41:46 -07001375 // Clip the covered region to the visible region
1376 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1377
1378 // Update aboveCoveredLayers for next (lower) layer
1379 aboveCoveredLayers.orSelf(visibleRegion);
1380
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381 // subtract the opaque region covered by the layers above us
1382 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383
1384 // compute this layer's dirty region
1385 if (layer->contentDirty) {
1386 // we need to invalidate the whole region
1387 dirty = visibleRegion;
1388 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001389 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390 layer->contentDirty = false;
1391 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001392 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001393 * the exposed region consists of two components:
1394 * 1) what's VISIBLE now and was COVERED before
1395 * 2) what's EXPOSED now less what was EXPOSED before
1396 *
1397 * note that (1) is conservative, we start with the whole
1398 * visible region but only keep what used to be covered by
1399 * something -- which mean it may have been exposed.
1400 *
1401 * (2) handles areas that were not covered by anything but got
1402 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001403 */
Mathias Agopianab028732010-03-16 16:41:46 -07001404 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001405 const Region oldVisibleRegion = layer->visibleRegion;
1406 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001407 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1408 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001409 }
1410 dirty.subtractSelf(aboveOpaqueLayers);
1411
1412 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001413 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414
Mathias Agopianab028732010-03-16 16:41:46 -07001415 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001417
Jesse Halla8026d22012-09-25 13:25:04 -07001418 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419 layer->setVisibleRegion(visibleRegion);
1420 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001421 layer->setVisibleNonTransparentRegion(
1422 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001423 }
1424
Mathias Agopian87baae12012-07-31 12:38:26 -07001425 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426}
1427
Mathias Agopian87baae12012-07-31 12:38:26 -07001428void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1429 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001430 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001431 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1432 if (hw->getLayerStack() == layerStack) {
1433 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001434 }
1435 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001436}
1437
1438void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001439{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001440 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001441
Mathias Agopian4fec8732012-06-29 14:12:52 -07001442 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001443 const LayerVector& layers(mDrawingState.layersSortedByZ);
1444 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001445 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001446 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001447 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001448 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001449 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001450 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001451
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001452 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001453}
1454
Mathias Agopianad456f92011-01-13 17:53:01 -08001455void SurfaceFlinger::invalidateHwcGeometry()
1456{
1457 mHwWorkListDirty = true;
1458}
1459
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001460
Mathias Agopiancd60f992012-08-16 16:28:27 -07001461void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001462 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463{
Mathias Agopian87baae12012-07-31 12:38:26 -07001464 Region dirtyRegion(inDirtyRegion);
1465
Mathias Agopianb8a55602009-06-26 19:06:36 -07001466 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001467 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001468
Mathias Agopian42977342012-08-05 00:40:46 -07001469 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001470 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001471 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1472 // takes a rectangle, we must make sure to update that whole
1473 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001474 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001475 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001476 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001477 // We need to redraw the rectangle that will be updated
1478 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001479 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001480 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001481 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001482 } else {
1483 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001484 dirtyRegion.set(hw->bounds());
1485 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486 }
1487 }
1488
Mathias Agopiancd60f992012-08-16 16:28:27 -07001489 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001490
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001491 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001492 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001493
1494 // swap buffers (presentation)
1495 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001496}
1497
Mathias Agopiancd60f992012-08-16 16:28:27 -07001498void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001499{
Mathias Agopian3f844832013-08-07 21:24:32 -07001500 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001501 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001502 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001503 HWComposer::LayerListIterator cur = hwc.begin(id);
1504 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001505
Mathias Agopian85d751c2012-08-29 16:59:24 -07001506 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1507 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001508 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001509 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1510 hw->getDisplayName().string());
1511 return;
1512 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001513
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001514 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001515 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001516 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001517 // when using overlays, we assume a fully transparent framebuffer
1518 // NOTE: we could reduce how much we need to clear, for instance
1519 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001520 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001521 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001522 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001523 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001524 // we start with the whole screen area
1525 const Region bounds(hw->getBounds());
1526
1527 // we remove the scissor part
1528 // we're left with the letterbox region
1529 // (common case is that letterbox ends-up being empty)
1530 const Region letterbox(bounds.subtract(hw->getScissor()));
1531
1532 // compute the area to clear
1533 Region region(hw->undefinedRegion.merge(letterbox));
1534
1535 // but limit it to the dirty region
1536 region.andSelf(dirty);
1537
Mathias Agopianb9494d52012-04-18 02:28:45 -07001538 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001539 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001540 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001541 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001542 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001543 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001544
Mathias Agopian766dc492012-10-30 18:08:06 -07001545 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1546 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001547 // scissor on the main display. It should never be needed
1548 // anyways (though in theory it could since the API allows it).
1549 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001550 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001551 if (scissor != bounds) {
1552 // scissor doesn't match the screen's dimensions, so we
1553 // need to clear everything outside of it and enable
1554 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001555
Mathias Agopianf45c5102012-10-24 16:29:17 -07001556 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001557 const uint32_t height = hw->getHeight();
1558 engine.setScissor(scissor.left, height - scissor.bottom,
1559 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001560 }
1561 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001562 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001563
Mathias Agopian85d751c2012-08-29 16:59:24 -07001564 /*
1565 * and then, render the layers targeted at the framebuffer
1566 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001567
Mathias Agopian13127d82013-03-05 17:47:11 -08001568 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001569 const size_t count = layers.size();
1570 const Transform& tr = hw->getTransform();
1571 if (cur != end) {
1572 // we're using h/w composer
1573 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001574 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001575 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001576 if (!clip.isEmpty()) {
1577 switch (cur->getCompositionType()) {
1578 case HWC_OVERLAY: {
1579 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1580 && i
1581 && layer->isOpaque()
1582 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001583 // never clear the very first layer since we're
1584 // guaranteed the FB is already cleared
1585 layer->clearWithOpenGL(hw, clip);
1586 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001587 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001588 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001589 case HWC_FRAMEBUFFER: {
1590 layer->draw(hw, clip);
1591 break;
1592 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001593 case HWC_FRAMEBUFFER_TARGET: {
1594 // this should not happen as the iterator shouldn't
1595 // let us get there.
1596 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1597 break;
1598 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001599 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001600 }
1601 layer->setAcquireFence(hw, *cur);
1602 }
1603 } else {
1604 // we're not using h/w composer
1605 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001606 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001607 const Region clip(dirty.intersect(
1608 tr.transform(layer->visibleRegion)));
1609 if (!clip.isEmpty()) {
1610 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001611 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001612 }
1613 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001614
1615 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001616 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001617}
1618
Mathias Agopian3f844832013-08-07 21:24:32 -07001619void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001620 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001621 RenderEngine& engine(getRenderEngine());
1622 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001623}
1624
Mathias Agopianac9fa422013-02-11 16:40:36 -08001625void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1626 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001627 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001628 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001629{
Mathias Agopian96f08192010-06-02 23:28:45 -07001630 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001631 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001632
Mathias Agopian96f08192010-06-02 23:28:45 -07001633 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001634 Mutex::Autolock _l(mStateLock);
1635 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001636 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001637}
1638
Mathias Agopian3f844832013-08-07 21:24:32 -07001639status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001640 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001641 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001642 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001643 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001644 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001645 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001646 return NO_ERROR;
1647 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001648 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001649}
1650
Mathias Agopian3f844832013-08-07 21:24:32 -07001651uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001652 return android_atomic_release_load(&mTransactionFlags);
1653}
1654
Mathias Agopian3f844832013-08-07 21:24:32 -07001655uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001656 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1657}
1658
Mathias Agopian3f844832013-08-07 21:24:32 -07001659uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001660 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1661 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001662 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663 }
1664 return old;
1665}
1666
Mathias Agopian8b33f032012-07-24 20:43:54 -07001667void SurfaceFlinger::setTransactionState(
1668 const Vector<ComposerState>& state,
1669 const Vector<DisplayState>& displays,
1670 uint32_t flags)
1671{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001672 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001673 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001674 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001675
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001676 if (flags & eAnimation) {
1677 // For window updates that are part of an animation we must wait for
1678 // previous animation "frames" to be handled.
1679 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001680 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001681 if (CC_UNLIKELY(err != NO_ERROR)) {
1682 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001683 // caller after a few seconds.
1684 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1685 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001686 mAnimTransactionPending = false;
1687 break;
1688 }
1689 }
1690 }
1691
Mathias Agopiane57f2922012-08-09 16:29:12 -07001692 size_t count = displays.size();
1693 for (size_t i=0 ; i<count ; i++) {
1694 const DisplayState& s(displays[i]);
1695 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001696 }
1697
Mathias Agopiane57f2922012-08-09 16:29:12 -07001698 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001699 for (size_t i=0 ; i<count ; i++) {
1700 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001701 // Here we need to check that the interface we're given is indeed
1702 // one of our own. A malicious client could give us a NULL
1703 // IInterface, or one of its own or even one of our own but a
1704 // different type. All these situations would cause us to crash.
1705 //
1706 // NOTE: it would be better to use RTTI as we could directly check
1707 // that we have a Client*. however, RTTI is disabled in Android.
1708 if (s.client != NULL) {
1709 sp<IBinder> binder = s.client->asBinder();
1710 if (binder != NULL) {
1711 String16 desc(binder->getInterfaceDescriptor());
1712 if (desc == ISurfaceComposerClient::descriptor) {
1713 sp<Client> client( static_cast<Client *>(s.client.get()) );
1714 transactionFlags |= setClientStateLocked(client, s.state);
1715 }
1716 }
1717 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001718 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001719
Mathias Agopian386aa982011-11-07 21:58:03 -08001720 if (transactionFlags) {
1721 // this triggers the transaction
1722 setTransactionFlags(transactionFlags);
1723
1724 // if this is a synchronous transaction, wait for it to take effect
1725 // before returning.
1726 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001727 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001728 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001729 if (flags & eAnimation) {
1730 mAnimTransactionPending = true;
1731 }
1732 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001733 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1734 if (CC_UNLIKELY(err != NO_ERROR)) {
1735 // just in case something goes wrong in SF, return to the
1736 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001737 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1738 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001739 break;
1740 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001741 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001742 }
1743}
1744
Mathias Agopiane57f2922012-08-09 16:29:12 -07001745uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1746{
Jesse Hall9a143922012-10-04 16:29:19 -07001747 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1748 if (dpyIdx < 0)
1749 return 0;
1750
Mathias Agopiane57f2922012-08-09 16:29:12 -07001751 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001752 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001753 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001754 const uint32_t what = s.what;
1755 if (what & DisplayState::eSurfaceChanged) {
1756 if (disp.surface->asBinder() != s.surface->asBinder()) {
1757 disp.surface = s.surface;
1758 flags |= eDisplayTransactionNeeded;
1759 }
1760 }
1761 if (what & DisplayState::eLayerStackChanged) {
1762 if (disp.layerStack != s.layerStack) {
1763 disp.layerStack = s.layerStack;
1764 flags |= eDisplayTransactionNeeded;
1765 }
1766 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001767 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001768 if (disp.orientation != s.orientation) {
1769 disp.orientation = s.orientation;
1770 flags |= eDisplayTransactionNeeded;
1771 }
1772 if (disp.frame != s.frame) {
1773 disp.frame = s.frame;
1774 flags |= eDisplayTransactionNeeded;
1775 }
1776 if (disp.viewport != s.viewport) {
1777 disp.viewport = s.viewport;
1778 flags |= eDisplayTransactionNeeded;
1779 }
1780 }
1781 }
1782 return flags;
1783}
1784
1785uint32_t SurfaceFlinger::setClientStateLocked(
1786 const sp<Client>& client,
1787 const layer_state_t& s)
1788{
1789 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001790 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001791 if (layer != 0) {
1792 const uint32_t what = s.what;
1793 if (what & layer_state_t::ePositionChanged) {
1794 if (layer->setPosition(s.x, s.y))
1795 flags |= eTraversalNeeded;
1796 }
1797 if (what & layer_state_t::eLayerChanged) {
1798 // NOTE: index needs to be calculated before we update the state
1799 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1800 if (layer->setLayer(s.z)) {
1801 mCurrentState.layersSortedByZ.removeAt(idx);
1802 mCurrentState.layersSortedByZ.add(layer);
1803 // we need traversal (state changed)
1804 // AND transaction (list changed)
1805 flags |= eTransactionNeeded|eTraversalNeeded;
1806 }
1807 }
1808 if (what & layer_state_t::eSizeChanged) {
1809 if (layer->setSize(s.w, s.h)) {
1810 flags |= eTraversalNeeded;
1811 }
1812 }
1813 if (what & layer_state_t::eAlphaChanged) {
1814 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1815 flags |= eTraversalNeeded;
1816 }
1817 if (what & layer_state_t::eMatrixChanged) {
1818 if (layer->setMatrix(s.matrix))
1819 flags |= eTraversalNeeded;
1820 }
1821 if (what & layer_state_t::eTransparentRegionChanged) {
1822 if (layer->setTransparentRegionHint(s.transparentRegion))
1823 flags |= eTraversalNeeded;
1824 }
1825 if (what & layer_state_t::eVisibilityChanged) {
1826 if (layer->setFlags(s.flags, s.mask))
1827 flags |= eTraversalNeeded;
1828 }
1829 if (what & layer_state_t::eCropChanged) {
1830 if (layer->setCrop(s.crop))
1831 flags |= eTraversalNeeded;
1832 }
1833 if (what & layer_state_t::eLayerStackChanged) {
1834 // NOTE: index needs to be calculated before we update the state
1835 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1836 if (layer->setLayerStack(s.layerStack)) {
1837 mCurrentState.layersSortedByZ.removeAt(idx);
1838 mCurrentState.layersSortedByZ.add(layer);
1839 // we need traversal (state changed)
1840 // AND transaction (list changed)
1841 flags |= eTransactionNeeded|eTraversalNeeded;
1842 }
1843 }
1844 }
1845 return flags;
1846}
1847
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001848status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001849 const String8& name,
1850 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001851 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1852 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001853{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001854 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001855 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001856 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001857 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001858 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001859 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001860
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001861 status_t result = NO_ERROR;
1862
1863 sp<Layer> layer;
1864
Mathias Agopian3165cc22012-08-08 19:42:09 -07001865 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1866 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001867 result = createNormalLayer(client,
1868 name, w, h, flags, format,
1869 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001870 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001871 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001872 result = createDimLayer(client,
1873 name, w, h, flags,
1874 handle, gbp, &layer);
1875 break;
1876 default:
1877 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001878 break;
1879 }
1880
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001881 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001882 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001883 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001884 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001885 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001886}
1887
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001888status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1889 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1890 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001891{
1892 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001893 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001894 case PIXEL_FORMAT_TRANSPARENT:
1895 case PIXEL_FORMAT_TRANSLUCENT:
1896 format = PIXEL_FORMAT_RGBA_8888;
1897 break;
1898 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001899#ifdef NO_RGBX_8888
1900 format = PIXEL_FORMAT_RGB_565;
1901#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001902 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001903#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001904 break;
1905 }
1906
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001907#ifdef NO_RGBX_8888
1908 if (format == PIXEL_FORMAT_RGBX_8888)
1909 format = PIXEL_FORMAT_RGBA_8888;
1910#endif
1911
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001912 *outLayer = new Layer(this, client, name, w, h, flags);
1913 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1914 if (err == NO_ERROR) {
1915 *handle = (*outLayer)->getHandle();
1916 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001917 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001918
1919 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1920 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001921}
1922
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001923status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1924 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1925 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001926{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001927 *outLayer = new LayerDim(this, client, name, w, h, flags);
1928 *handle = (*outLayer)->getHandle();
1929 *gbp = (*outLayer)->getBufferQueue();
1930 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001931}
1932
Mathias Agopianac9fa422013-02-11 16:40:36 -08001933status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001934{
Mathias Agopian67106042013-03-14 19:18:13 -07001935 // called by the window manager when it wants to remove a Layer
1936 status_t err = NO_ERROR;
1937 sp<Layer> l(client->getLayerUser(handle));
1938 if (l != NULL) {
1939 err = removeLayer(l);
1940 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1941 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001942 }
1943 return err;
1944}
1945
Mathias Agopian13127d82013-03-05 17:47:11 -08001946status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001947{
Mathias Agopian67106042013-03-14 19:18:13 -07001948 // called by ~LayerCleaner() when all references to the IBinder (handle)
1949 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001950 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001951 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001952 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07001953 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001954 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001955 "error removing layer=%p (%s)", l.get(), strerror(-err));
1956 }
1957 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001958}
1959
Mathias Agopianb60314a2012-04-10 22:09:54 -07001960// ---------------------------------------------------------------------------
1961
Andy McFadden13a082e2012-08-24 10:16:42 -07001962void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08001963 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07001964 Vector<ComposerState> state;
1965 Vector<DisplayState> displays;
1966 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08001967 d.what = DisplayState::eDisplayProjectionChanged |
1968 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08001969 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08001970 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07001971 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001972 d.frame.makeInvalid();
1973 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001974 displays.add(d);
1975 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001976 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07001977
1978 const nsecs_t period =
1979 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
1980 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07001981}
1982
1983void SurfaceFlinger::initializeDisplays() {
1984 class MessageScreenInitialized : public MessageBase {
1985 SurfaceFlinger* flinger;
1986 public:
1987 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1988 virtual bool handler() {
1989 flinger->onInitializeDisplays();
1990 return true;
1991 }
1992 };
1993 sp<MessageBase> msg = new MessageScreenInitialized(this);
1994 postMessageAsync(msg); // we may be called from main thread, use async message
1995}
1996
1997
Mathias Agopiancde87a32012-09-13 14:09:01 -07001998void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07001999 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2000 if (hw->isScreenAcquired()) {
2001 // this is expected, e.g. when power manager wakes up during boot
2002 ALOGD(" screen was previously acquired");
2003 return;
2004 }
2005
Mathias Agopian42977342012-08-05 00:40:46 -07002006 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002007 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002008 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002009 // built-in display, tell the HWC
2010 getHwComposer().acquire(type);
2011
2012 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2013 // FIXME: eventthread only knows about the main display right now
2014 mEventThread->onScreenAcquired();
2015 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002016 }
Mathias Agopian20128302012-08-13 18:32:13 -07002017 mVisibleRegionsDirty = true;
2018 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002019}
2020
Mathias Agopiancde87a32012-09-13 14:09:01 -07002021void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002022 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2023 if (!hw->isScreenAcquired()) {
2024 ALOGD(" screen was previously released");
2025 return;
2026 }
2027
2028 hw->releaseScreen();
2029 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002030 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002031 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002032 // FIXME: eventthread only knows about the main display right now
2033 mEventThread->onScreenReleased();
2034 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002035
2036 // built-in display, tell the HWC
2037 getHwComposer().release(type);
2038 }
2039 mVisibleRegionsDirty = true;
2040 // from this point on, SF will stop drawing on this display
2041}
2042
2043void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2044 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002045 SurfaceFlinger& mFlinger;
2046 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002047 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002048 MessageScreenAcquired(SurfaceFlinger& flinger,
2049 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002050 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002051 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2052 if (hw == NULL) {
2053 ALOGE("Attempt to unblank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002054 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002055 ALOGW("Attempt to unblank virtual display");
2056 } else {
2057 mFlinger.onScreenAcquired(hw);
2058 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002059 return true;
2060 }
2061 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002062 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2063 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002064}
2065
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002066void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002067 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002068 SurfaceFlinger& mFlinger;
2069 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002070 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002071 MessageScreenReleased(SurfaceFlinger& flinger,
2072 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002073 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002074 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2075 if (hw == NULL) {
2076 ALOGE("Attempt to blank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002077 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002078 ALOGW("Attempt to blank virtual display");
2079 } else {
2080 mFlinger.onScreenReleased(hw);
2081 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002082 return true;
2083 }
2084 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002085 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2086 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002087}
2088
2089// ---------------------------------------------------------------------------
2090
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002091status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2092{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002093 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002094
Mathias Agopianbd115332013-04-18 16:41:04 -07002095 IPCThreadState* ipc = IPCThreadState::self();
2096 const int pid = ipc->getCallingPid();
2097 const int uid = ipc->getCallingUid();
2098 if ((uid != AID_SHELL) &&
2099 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002100 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002101 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002102 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002103 // Try to get the main lock, but don't insist if we can't
2104 // (this would indicate SF is stuck, but we want to be able to
2105 // print something in dumpsys).
2106 int retry = 3;
2107 while (mStateLock.tryLock()<0 && --retry>=0) {
2108 usleep(1000000);
2109 }
2110 const bool locked(retry >= 0);
2111 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002112 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002113 "SurfaceFlinger appears to be unresponsive, "
2114 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002115 }
2116
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002117 bool dumpAll = true;
2118 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002119 size_t numArgs = args.size();
2120 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002121 if ((index < numArgs) &&
2122 (args[index] == String16("--list"))) {
2123 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002124 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002125 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002126 }
2127
2128 if ((index < numArgs) &&
2129 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002130 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002131 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002132 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002133 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002134
2135 if ((index < numArgs) &&
2136 (args[index] == String16("--latency-clear"))) {
2137 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002138 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002139 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002140 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002141 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002142
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002143 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002144 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002145 }
2146
Mathias Agopian9795c422009-08-26 16:36:26 -07002147 if (locked) {
2148 mStateLock.unlock();
2149 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002150 }
2151 write(fd, result.string(), result.size());
2152 return NO_ERROR;
2153}
2154
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002155void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002156 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002157{
2158 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2159 const size_t count = currentLayers.size();
2160 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002161 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002162 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002163 }
2164}
2165
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002166void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002167 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002168{
2169 String8 name;
2170 if (index < args.size()) {
2171 name = String8(args[index]);
2172 index++;
2173 }
2174
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002175 const nsecs_t period =
2176 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2177 result.appendFormat("%lld\n", period);
2178
2179 if (name.isEmpty()) {
2180 mAnimFrameTracker.dump(result);
2181 } else {
2182 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2183 const size_t count = currentLayers.size();
2184 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002185 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002186 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002187 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002188 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002189 }
2190 }
2191}
2192
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002193void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002194 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002195{
2196 String8 name;
2197 if (index < args.size()) {
2198 name = String8(args[index]);
2199 index++;
2200 }
2201
2202 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2203 const size_t count = currentLayers.size();
2204 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002205 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002206 if (name.isEmpty() || (name == layer->getName())) {
2207 layer->clearStats();
2208 }
2209 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002210
2211 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002212}
2213
Jamie Gennis6547ff42013-07-16 20:12:42 -07002214// This should only be called from the main thread. Otherwise it would need
2215// the lock and should use mCurrentState rather than mDrawingState.
2216void SurfaceFlinger::logFrameStats() {
2217 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2218 const size_t count = drawingLayers.size();
2219 for (size_t i=0 ; i<count ; i++) {
2220 const sp<Layer>& layer(drawingLayers[i]);
2221 layer->logFrameStats();
2222 }
2223
2224 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2225}
2226
Andy McFadden4803b742012-09-24 19:07:20 -07002227/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2228{
2229 static const char* config =
2230 " [sf"
2231#ifdef NO_RGBX_8888
2232 " NO_RGBX_8888"
2233#endif
2234#ifdef HAS_CONTEXT_PRIORITY
2235 " HAS_CONTEXT_PRIORITY"
2236#endif
2237#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2238 " NEVER_DEFAULT_TO_ASYNC_MODE"
2239#endif
2240#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2241 " TARGET_DISABLE_TRIPLE_BUFFERING"
2242#endif
2243 "]";
2244 result.append(config);
2245}
2246
Mathias Agopian74d211a2013-04-22 16:55:35 +02002247void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2248 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002249{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002250 bool colorize = false;
2251 if (index < args.size()
2252 && (args[index] == String16("--color"))) {
2253 colorize = true;
2254 index++;
2255 }
2256
2257 Colorizer colorizer(colorize);
2258
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002259 // figure out if we're stuck somewhere
2260 const nsecs_t now = systemTime();
2261 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2262 const nsecs_t inTransaction(mDebugInTransaction);
2263 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2264 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2265
2266 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002267 * Dump library configuration.
2268 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002269
2270 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002271 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002272 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002273 appendSfConfigString(result);
2274 appendUiConfigString(result);
2275 appendGuiConfigString(result);
2276 result.append("\n");
2277
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002278 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002279 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002280 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002281 result.append(SyncFeatures::getInstance().toString());
2282 result.append("\n");
2283
Andy McFadden4803b742012-09-24 19:07:20 -07002284 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002285 * Dump the visible layer list
2286 */
2287 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2288 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002289 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002290 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002291 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002292 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002293 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002294 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002295 }
2296
2297 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002298 * Dump Display state
2299 */
2300
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002301 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002302 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002303 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002304 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2305 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002306 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002307 }
2308
2309 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002310 * Dump SurfaceFlinger global state
2311 */
2312
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002313 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002314 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002315 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002316
Mathias Agopian888c8222012-08-04 21:10:38 -07002317 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002318 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002319
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002320 colorizer.bold(result);
2321 result.appendFormat("EGL implementation : %s\n",
2322 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2323 colorizer.reset(result);
2324 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002325 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002326
Mathias Agopian875d8e12013-06-07 15:35:48 -07002327 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002328
Mathias Agopian42977342012-08-05 00:40:46 -07002329 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002330 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002331 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002332 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002333 " last eglSwapBuffers() time: %f us\n"
2334 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002335 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002336 " refresh-rate : %f fps\n"
2337 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002338 " y-dpi : %f\n"
2339 " EGL_NATIVE_VISUAL_ID : %d\n"
2340 " gpu_to_cpu_unsupported : %d\n"
2341 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002342 mLastSwapBufferTime/1000.0,
2343 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002344 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002345 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2346 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002347 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2348 mEGLNativeVisualId,
2349 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002350
Mathias Agopian74d211a2013-04-22 16:55:35 +02002351 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002352 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002353
Mathias Agopian74d211a2013-04-22 16:55:35 +02002354 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002355 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002356
2357 /*
2358 * VSYNC state
2359 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002360 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002361
2362 /*
2363 * Dump HWComposer state
2364 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002365 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002366 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002367 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002368 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002369 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2370 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002371 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002372
2373 /*
2374 * Dump gralloc state
2375 */
2376 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2377 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002378}
2379
Mathias Agopian13127d82013-03-05 17:47:11 -08002380const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002381SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002382 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002383 wp<IBinder> dpy;
2384 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2385 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2386 dpy = mDisplays.keyAt(i);
2387 break;
2388 }
2389 }
2390 if (dpy == NULL) {
2391 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2392 // Just use the primary display so we have something to return
2393 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2394 }
2395 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002396}
2397
Keun young Park63f165f2012-08-31 10:53:36 -07002398bool SurfaceFlinger::startDdmConnection()
2399{
2400 void* libddmconnection_dso =
2401 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2402 if (!libddmconnection_dso) {
2403 return false;
2404 }
2405 void (*DdmConnection_start)(const char* name);
2406 DdmConnection_start =
2407 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2408 if (!DdmConnection_start) {
2409 dlclose(libddmconnection_dso);
2410 return false;
2411 }
2412 (*DdmConnection_start)(getServiceName());
2413 return true;
2414}
2415
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002416status_t SurfaceFlinger::onTransact(
2417 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2418{
2419 switch (code) {
2420 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002421 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002422 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002423 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002424 case BLANK:
2425 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002426 {
2427 // codes that require permission check
2428 IPCThreadState* ipc = IPCThreadState::self();
2429 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002430 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002431 if ((uid != AID_GRAPHICS) &&
2432 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002433 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002434 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2435 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002436 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002437 break;
2438 }
2439 case CAPTURE_SCREEN:
2440 {
2441 // codes that require permission check
2442 IPCThreadState* ipc = IPCThreadState::self();
2443 const int pid = ipc->getCallingPid();
2444 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002445 if ((uid != AID_GRAPHICS) &&
2446 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002447 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002448 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2449 return PERMISSION_DENIED;
2450 }
2451 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002452 }
2453 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002454
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002455 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2456 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002457 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002458 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002459 IPCThreadState* ipc = IPCThreadState::self();
2460 const int pid = ipc->getCallingPid();
2461 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002462 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002463 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002464 return PERMISSION_DENIED;
2465 }
2466 int n;
2467 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002468 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002469 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002470 return NO_ERROR;
2471 case 1002: // SHOW_UPDATES
2472 n = data.readInt32();
2473 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002474 invalidateHwcGeometry();
2475 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002476 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002477 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002478 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002479 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002480 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002481 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002482 setTransactionFlags(
2483 eTransactionNeeded|
2484 eDisplayTransactionNeeded|
2485 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002486 return NO_ERROR;
2487 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002488 case 1006:{ // send empty update
2489 signalRefresh();
2490 return NO_ERROR;
2491 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002492 case 1008: // toggle use of hw composer
2493 n = data.readInt32();
2494 mDebugDisableHWC = n ? 1 : 0;
2495 invalidateHwcGeometry();
2496 repaintEverything();
2497 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002498 case 1009: // toggle use of transform hint
2499 n = data.readInt32();
2500 mDebugDisableTransformHint = n ? 1 : 0;
2501 invalidateHwcGeometry();
2502 repaintEverything();
2503 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002504 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002505 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002506 reply->writeInt32(0);
2507 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002508 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002509 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002510 return NO_ERROR;
2511 case 1013: {
2512 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002513 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2514 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002515 }
2516 return NO_ERROR;
2517 }
2518 }
2519 return err;
2520}
2521
Mathias Agopian53331da2011-08-22 21:44:41 -07002522void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002523 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002524 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002525}
2526
Mathias Agopian59119e62010-10-11 12:37:43 -07002527// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002528// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002529// ---------------------------------------------------------------------------
2530
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002531/* The code below is here to handle b/8734824
2532 *
2533 * We create a IGraphicBufferProducer wrapper that forwards all calls
2534 * to the calling binder thread, where they are executed. This allows
2535 * the calling thread to be reused (on the other side) and not
2536 * depend on having "enough" binder threads to handle the requests.
2537 *
2538 */
2539
2540class GraphicProducerWrapper : public BBinder, public MessageHandler {
2541 sp<IGraphicBufferProducer> impl;
2542 sp<Looper> looper;
2543 status_t result;
2544 bool exitPending;
2545 bool exitRequested;
2546 mutable Barrier barrier;
2547 volatile int32_t memoryBarrier;
2548 uint32_t code;
2549 Parcel const* data;
2550 Parcel* reply;
2551
2552 enum {
2553 MSG_API_CALL,
2554 MSG_EXIT
2555 };
2556
2557 /*
2558 * this is called by our "fake" BpGraphicBufferProducer. We package the
2559 * data and reply Parcel and forward them to the calling thread.
2560 */
2561 virtual status_t transact(uint32_t code,
2562 const Parcel& data, Parcel* reply, uint32_t flags) {
2563 this->code = code;
2564 this->data = &data;
2565 this->reply = reply;
2566 android_atomic_acquire_store(0, &memoryBarrier);
2567 if (exitPending) {
2568 // if we've exited, we run the message synchronously right here
2569 handleMessage(Message(MSG_API_CALL));
2570 } else {
2571 barrier.close();
2572 looper->sendMessage(this, Message(MSG_API_CALL));
2573 barrier.wait();
2574 }
2575 return NO_ERROR;
2576 }
2577
2578 /*
2579 * here we run on the binder calling thread. All we've got to do is
2580 * call the real BpGraphicBufferProducer.
2581 */
2582 virtual void handleMessage(const Message& message) {
2583 android_atomic_release_load(&memoryBarrier);
2584 if (message.what == MSG_API_CALL) {
2585 impl->asBinder()->transact(code, data[0], reply);
2586 barrier.open();
2587 } else if (message.what == MSG_EXIT) {
2588 exitRequested = true;
2589 }
2590 }
2591
2592public:
2593 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2594 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2595 exitPending(false), exitRequested(false) {
2596 }
2597
2598 status_t waitForResponse() {
2599 do {
2600 looper->pollOnce(-1);
2601 } while (!exitRequested);
2602 return result;
2603 }
2604
2605 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002606 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002607 exitPending = true;
2608 looper->sendMessage(this, Message(MSG_EXIT));
2609 }
2610};
2611
2612
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002613status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2614 const sp<IGraphicBufferProducer>& producer,
2615 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002616 uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002617
2618 if (CC_UNLIKELY(display == 0))
2619 return BAD_VALUE;
2620
2621 if (CC_UNLIKELY(producer == 0))
2622 return BAD_VALUE;
2623
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002624 // if we have secure windows on this display, never allow the screen capture
2625 // unless the producer interface is local (i.e.: we can take a screenshot for
2626 // ourselves).
2627 if (!producer->asBinder()->localBinder()) {
2628 Mutex::Autolock _l(mStateLock);
2629 sp<const DisplayDevice> hw(getDisplayDevice(display));
2630 if (hw->getSecureLayerVisible()) {
2631 ALOGW("FB is protected: PERMISSION_DENIED");
2632 return PERMISSION_DENIED;
2633 }
2634 }
2635
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002636 class MessageCaptureScreen : public MessageBase {
2637 SurfaceFlinger* flinger;
2638 sp<IBinder> display;
2639 sp<IGraphicBufferProducer> producer;
2640 uint32_t reqWidth, reqHeight;
2641 uint32_t minLayerZ,maxLayerZ;
2642 status_t result;
2643 public:
2644 MessageCaptureScreen(SurfaceFlinger* flinger,
2645 const sp<IBinder>& display,
2646 const sp<IGraphicBufferProducer>& producer,
2647 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002648 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002649 : flinger(flinger), display(display), producer(producer),
2650 reqWidth(reqWidth), reqHeight(reqHeight),
2651 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2652 result(PERMISSION_DENIED)
2653 {
2654 }
2655 status_t getResult() const {
2656 return result;
2657 }
2658 virtual bool handler() {
2659 Mutex::Autolock _l(flinger->mStateLock);
2660 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002661 result = flinger->captureScreenImplLocked(hw,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002662 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002663 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002664 return true;
2665 }
2666 };
2667
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002668 // make sure to process transactions before screenshots -- a transaction
2669 // might already be pending but scheduled for VSYNC; this guarantees we
2670 // will handle it before the screenshot. When VSYNC finally arrives
2671 // the scheduled transaction will be a no-op. If no transactions are
2672 // scheduled at this time, this will end-up being a no-op as well.
2673 mEventQueue.invalidateTransactionNow();
2674
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002675 // this creates a "fake" BBinder which will serve as a "fake" remote
2676 // binder to receive the marshaled calls and forward them to the
2677 // real remote (a BpGraphicBufferProducer)
2678 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2679
2680 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2681 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002682 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002683 display, IGraphicBufferProducer::asInterface( wrapper ),
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002684 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002685
2686 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002687 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002688 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002689 }
2690 return res;
2691}
2692
Mathias Agopian180f10d2013-04-10 22:55:41 -07002693
2694void SurfaceFlinger::renderScreenImplLocked(
2695 const sp<const DisplayDevice>& hw,
2696 uint32_t reqWidth, uint32_t reqHeight,
2697 uint32_t minLayerZ, uint32_t maxLayerZ,
2698 bool yswap)
2699{
2700 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002701 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002702
2703 // get screen geometry
2704 const uint32_t hw_w = hw->getWidth();
2705 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002706 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2707
2708 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002709 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002710
2711 // set-up our viewport
Mathias Agopian3f844832013-08-07 21:24:32 -07002712 engine.setViewportAndProjection(reqWidth, reqHeight, hw_w, hw_h, yswap);
2713 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002714
2715 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002716 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002717
2718 const LayerVector& layers( mDrawingState.layersSortedByZ );
2719 const size_t count = layers.size();
2720 for (size_t i=0 ; i<count ; ++i) {
2721 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002722 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002723 if (state.layerStack == hw->getLayerStack()) {
2724 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2725 if (layer->isVisible()) {
2726 if (filtering) layer->setFiltering(true);
2727 layer->draw(hw);
2728 if (filtering) layer->setFiltering(false);
2729 }
2730 }
2731 }
2732 }
2733
2734 // compositionComplete is needed for older driver
2735 hw->compositionComplete();
2736}
2737
2738
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002739status_t SurfaceFlinger::captureScreenImplLocked(
2740 const sp<const DisplayDevice>& hw,
2741 const sp<IGraphicBufferProducer>& producer,
2742 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002743 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002744{
2745 ATRACE_CALL();
2746
Mathias Agopian180f10d2013-04-10 22:55:41 -07002747 // get screen geometry
2748 const uint32_t hw_w = hw->getWidth();
2749 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002750
Mathias Agopian180f10d2013-04-10 22:55:41 -07002751 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2752 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2753 reqWidth, reqHeight, hw_w, hw_h);
2754 return BAD_VALUE;
2755 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002756
Mathias Agopian180f10d2013-04-10 22:55:41 -07002757 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2758 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2759
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002760 // create a surface (because we're a producer, and we need to
2761 // dequeue/queue a buffer)
2762 sp<Surface> sur = new Surface(producer);
2763 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002764
2765 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002766 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002767 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2768 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002769
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002770 int err = 0;
2771 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07002772 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002773 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2774 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002775
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002776 if (err == NO_ERROR) {
2777 ANativeWindowBuffer* buffer;
2778 /* TODO: Once we have the sync framework everywhere this can use
2779 * server-side waits on the fence that dequeueBuffer returns.
2780 */
2781 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2782 if (result == NO_ERROR) {
2783 // create an EGLImage from the buffer so we can later
2784 // turn it into a texture
2785 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2786 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2787 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002788 // this binds the given EGLImage as a framebuffer for the
2789 // duration of this scope.
2790 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2791 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002792 // this will in fact render into our dequeued buffer
2793 // via an FBO, which means we didn't have to create
2794 // an EGLSurface and therefore we're not
2795 // dependent on the context's EGLConfig.
2796 renderScreenImplLocked(hw, reqWidth, reqHeight,
2797 minLayerZ, maxLayerZ, true);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002798 } else {
2799 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2800 result = INVALID_OPERATION;
2801 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002802 // destroy our image
2803 eglDestroyImageKHR(mEGLDisplay, image);
2804 } else {
2805 result = BAD_VALUE;
2806 }
2807 window->queueBuffer(window, buffer, -1);
2808 }
2809 } else {
2810 result = BAD_VALUE;
2811 }
2812 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2813 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002814
Mathias Agopian875d8e12013-06-07 15:35:48 -07002815 hw->setViewportAndProjection();
Mathias Agopian2be4e8f2013-03-06 20:42:56 -08002816
Mathias Agopian74c40c02010-09-29 13:02:36 -07002817 return result;
2818}
2819
Mathias Agopianfee2b462013-07-03 12:34:01 -07002820void SurfaceFlinger::checkScreenshot(const sp<GraphicBuffer>& buf, void const* vaddr,
2821 const sp<const DisplayDevice>& hw,
2822 uint32_t minLayerZ, uint32_t maxLayerZ) {
2823 if (DEBUG_SCREENSHOTS) {
2824 for (ssize_t y=0 ; y<buf->height ; y++) {
2825 uint32_t const * p = (uint32_t const *)vaddr + y*buf->stride;
2826 for (ssize_t x=0 ; x<buf->width ; x++) {
2827 if (p[x] != 0xFF000000) return;
2828 }
2829 }
2830 ALOGE("*** we just took a black screenshot ***\n"
2831 "requested minz=%d, maxz=%d, layerStack=%d",
2832 minLayerZ, maxLayerZ, hw->getLayerStack());
2833 const LayerVector& layers( mDrawingState.layersSortedByZ );
2834 const size_t count = layers.size();
2835 for (size_t i=0 ; i<count ; ++i) {
2836 const sp<Layer>& layer(layers[i]);
2837 const Layer::State& state(layer->getDrawingState());
2838 const bool visible = (state.layerStack == hw->getLayerStack())
2839 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2840 && (layer->isVisible());
2841 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
2842 visible ? '+' : '-',
2843 i, layer->getName().string(), state.layerStack, state.z,
2844 layer->isVisible(), state.flags, state.alpha);
2845 }
2846 }
2847}
2848
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002849// ---------------------------------------------------------------------------
2850
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002851SurfaceFlinger::LayerVector::LayerVector() {
2852}
2853
2854SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002855 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002856}
2857
2858int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2859 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002860{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002861 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002862 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2863 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002864
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002865 uint32_t ls = l->getCurrentState().layerStack;
2866 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002867 if (ls != rs)
2868 return ls - rs;
2869
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002870 uint32_t lz = l->getCurrentState().z;
2871 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002872 if (lz != rz)
2873 return lz - rz;
2874
2875 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002876}
2877
2878// ---------------------------------------------------------------------------
2879
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002880SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2881 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002882}
2883
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002884SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002885 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002886 viewport.makeInvalid();
2887 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002888}
2889
2890// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002891
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002892}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002893
2894
2895#if defined(__gl_h_)
2896#error "don't include gl/gl.h in this file"
2897#endif
2898
2899#if defined(__gl2_h_)
2900#error "don't include gl2/gl2.h in this file"
2901#endif