blob: 0323cb73a1622caa0e5fd0fb3fb3f468fbd178a8 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070024
25#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <cutils/log.h>
28#include <cutils/properties.h>
29
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070032#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070033#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034
Mathias Agopianc666cae2012-07-25 18:56:13 -070035#include <ui/DisplayInfo.h>
36
Mathias Agopian921e6ac2012-07-23 23:11:29 -070037#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070038#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070039#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070040#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080041#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080042#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070043
44#include <ui/GraphicBufferAllocator.h>
45#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070046#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080047
Mathias Agopiancde87a32012-09-13 14:09:01 -070048#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <utils/String8.h>
50#include <utils/String16.h>
51#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080052#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian921e6ac2012-07-23 23:11:29 -070054#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070055#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopian3e25fd82013-04-22 17:52:16 +020057#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020059#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080060#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070061#include "DisplayDevice.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080062#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066
Mathias Agopiana4912602012-07-12 14:25:33 -070067#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070068#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070069#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070
Mathias Agopian875d8e12013-06-07 15:35:48 -070071#include "RenderEngine/RenderEngine.h"
72
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073#define DISPLAY_COUNT 1
74
Mathias Agopianfee2b462013-07-03 12:34:01 -070075/*
76 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
77 * black pixels.
78 */
79#define DEBUG_SCREENSHOTS false
80
Mathias Agopianca088332013-03-28 17:44:13 -070081EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
82
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084// ---------------------------------------------------------------------------
85
Mathias Agopian99b49842011-06-27 16:05:52 -070086const String16 sHardwareTest("android.permission.HARDWARE_TEST");
87const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
88const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
89const String16 sDump("android.permission.DUMP");
90
91// ---------------------------------------------------------------------------
92
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -070094 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -070096 mTransactionPending(false),
97 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070098 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -070099 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700100 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700103 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800104 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700106 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700107 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700108 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700109 mDebugInSwapBuffers(0),
110 mLastSwapBufferTime(0),
111 mDebugInTransaction(0),
112 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700113 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114{
Steve Blocka19954a2012-01-04 20:05:49 +0000115 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116
117 // debugging stuff...
118 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700119
Mathias Agopianb4b17302013-03-20 18:36:41 -0700120 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700121 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700122
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123 property_get("debug.sf.showupdates", value, "0");
124 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700125
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700126 property_get("debug.sf.ddms", value, "0");
127 mDebugDDMS = atoi(value);
128 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700129 if (!startDdmConnection()) {
130 // start failed, and DDMS debugging not enabled
131 mDebugDDMS = 0;
132 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700133 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700134 ALOGI_IF(mDebugRegion, "showupdates enabled");
135 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136}
137
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800138void SurfaceFlinger::onFirstRef()
139{
140 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800141}
142
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143SurfaceFlinger::~SurfaceFlinger()
144{
Mathias Agopiana4912602012-07-12 14:25:33 -0700145 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
146 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
147 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148}
149
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800150void SurfaceFlinger::binderDied(const wp<IBinder>& who)
151{
152 // the window manager died on us. prepare its eulogy.
153
Andy McFadden13a082e2012-08-24 10:16:42 -0700154 // restore initial conditions (default device unblank, etc)
155 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800156
157 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700158 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800159}
160
Mathias Agopian7e27f052010-05-28 14:22:23 -0700161sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162{
Mathias Agopian96f08192010-06-02 23:28:45 -0700163 sp<ISurfaceComposerClient> bclient;
164 sp<Client> client(new Client(this));
165 status_t err = client->initCheck();
166 if (err == NO_ERROR) {
167 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800168 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 return bclient;
170}
171
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700172sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
173 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700174{
175 class DisplayToken : public BBinder {
176 sp<SurfaceFlinger> flinger;
177 virtual ~DisplayToken() {
178 // no more references, this display must be terminated
179 Mutex::Autolock _l(flinger->mStateLock);
180 flinger->mCurrentState.displays.removeItem(this);
181 flinger->setTransactionFlags(eDisplayTransactionNeeded);
182 }
183 public:
184 DisplayToken(const sp<SurfaceFlinger>& flinger)
185 : flinger(flinger) {
186 }
187 };
188
189 sp<BBinder> token = new DisplayToken(this);
190
191 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700192 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700193 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700194 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700195 mCurrentState.displays.add(token, info);
196
197 return token;
198}
199
Jesse Hall6c913be2013-08-08 12:15:49 -0700200void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
201 Mutex::Autolock _l(mStateLock);
202
203 ssize_t idx = mCurrentState.displays.indexOfKey(display);
204 if (idx < 0) {
205 ALOGW("destroyDisplay: invalid display token");
206 return;
207 }
208
209 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
210 if (!info.isVirtualDisplay()) {
211 ALOGE("destroyDisplay called for non-virtual display");
212 return;
213 }
214
215 mCurrentState.displays.removeItemsAt(idx);
216 setTransactionFlags(eDisplayTransactionNeeded);
217}
218
Jesse Hall692c7232012-11-08 15:41:56 -0800219void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
220 ALOGW_IF(mBuiltinDisplays[type],
221 "Overwriting display token for display type %d", type);
222 mBuiltinDisplays[type] = new BBinder();
223 DisplayDeviceState info(type);
224 // All non-virtual displays are currently considered secure.
225 info.isSecure = true;
226 mCurrentState.displays.add(mBuiltinDisplays[type], info);
227}
228
Mathias Agopiane57f2922012-08-09 16:29:12 -0700229sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700230 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700231 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
232 return NULL;
233 }
Jesse Hall692c7232012-11-08 15:41:56 -0800234 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700235}
236
Jamie Gennis9a78c902011-01-12 18:30:40 -0800237sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
238{
239 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
240 return gba;
241}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700242
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800243void SurfaceFlinger::bootFinished()
244{
245 const nsecs_t now = systemTime();
246 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000247 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700248 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700249
250 // wait patiently for the window manager death
251 const String16 name("window");
252 sp<IBinder> window(defaultServiceManager()->getService(name));
253 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700254 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700255 }
256
257 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700258 // formerly we would just kill the process, but we now ask it to exit so it
259 // can choose where to stop the animation.
260 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800261}
262
Mathias Agopian3f844832013-08-07 21:24:32 -0700263void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700264 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700265 RenderEngine& engine;
266 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700267 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700268 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
269 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700270 }
271 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700272 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700273 return true;
274 }
275 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700276 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700277}
278
Mathias Agopian722b98f2012-09-25 18:24:31 -0700279status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700280 EGLDisplay dpy,
281 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700282 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700283 EGLConfig* outConfig)
284{
285 EGLConfig config = NULL;
286 EGLint numConfigs = -1, n=0;
287 eglGetConfigs(dpy, NULL, 0, &numConfigs);
288 EGLConfig* const configs = new EGLConfig[numConfigs];
289 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700290
Mathias Agopian722b98f2012-09-25 18:24:31 -0700291 if (n) {
292 if (attribute != EGL_NONE) {
293 for (int i=0 ; i<n ; i++) {
294 EGLint value = 0;
295 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
296 if (wanted == value) {
297 *outConfig = configs[i];
298 delete [] configs;
299 return NO_ERROR;
300 }
301 }
302 } else {
303 // just pick the first one
304 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700305 delete [] configs;
306 return NO_ERROR;
307 }
308 }
309 delete [] configs;
310 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800311}
312
Mathias Agopian722b98f2012-09-25 18:24:31 -0700313class EGLAttributeVector {
314 struct Attribute;
315 class Adder;
316 friend class Adder;
317 KeyedVector<Attribute, EGLint> mList;
318 struct Attribute {
319 Attribute() {};
320 Attribute(EGLint v) : v(v) { }
321 EGLint v;
322 bool operator < (const Attribute& other) const {
323 // this places EGL_NONE at the end
324 EGLint lhs(v);
325 EGLint rhs(other.v);
326 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
327 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
328 return lhs < rhs;
329 }
330 };
331 class Adder {
332 friend class EGLAttributeVector;
333 EGLAttributeVector& v;
334 EGLint attribute;
335 Adder(EGLAttributeVector& v, EGLint attribute)
336 : v(v), attribute(attribute) {
337 }
338 public:
339 void operator = (EGLint value) {
340 if (attribute != EGL_NONE) {
341 v.mList.add(attribute, value);
342 }
343 }
344 operator EGLint () const { return v.mList[attribute]; }
345 };
346public:
347 EGLAttributeVector() {
348 mList.add(EGL_NONE, EGL_NONE);
349 }
350 void remove(EGLint attribute) {
351 if (attribute != EGL_NONE) {
352 mList.removeItem(attribute);
353 }
354 }
355 Adder operator [] (EGLint attribute) {
356 return Adder(*this, attribute);
357 }
358 EGLint operator [] (EGLint attribute) const {
359 return mList[attribute];
360 }
361 // cast-operator to (EGLint const*)
362 operator EGLint const* () const { return &mList.keyAt(0).v; }
363};
364
Mathias Agopiana4912602012-07-12 14:25:33 -0700365EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
366 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
367 // it is to be used with WIFI displays
368 EGLConfig config;
369 EGLint dummy;
370 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700371
Mathias Agopian722b98f2012-09-25 18:24:31 -0700372 EGLAttributeVector attribs;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700373 // TODO: enable ES2
374 //attribs[EGL_RENDERABLE_TYPE] = EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT;
375 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
Mathias Agopian722b98f2012-09-25 18:24:31 -0700376 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
377 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
378 attribs[EGL_RED_SIZE] = 8;
379 attribs[EGL_GREEN_SIZE] = 8;
380 attribs[EGL_BLUE_SIZE] = 8;
381
382 err = selectConfigForAttribute(display, attribs, EGL_NONE, EGL_NONE, &config);
383 if (!err)
384 goto success;
385
Mathias Agopian875d8e12013-06-07 15:35:48 -0700386 // this didn't work, probably because we're on the emulator...
387 // try a simplified query
388 ALOGW("no suitable EGLConfig found, trying a simpler query");
389 attribs.remove(EGL_RENDERABLE_TYPE);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700390 attribs.remove(EGL_FRAMEBUFFER_TARGET_ANDROID);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700391 attribs.remove(EGL_RECORDABLE_ANDROID);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700392 attribs.remove(EGL_RED_SIZE);
393 attribs.remove(EGL_GREEN_SIZE);
394 attribs.remove(EGL_BLUE_SIZE);
395 err = selectConfigForAttribute(display, attribs,
396 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700397 if (!err)
398 goto success;
399
400 // this EGL is too lame for Android
Mathias Agopian875d8e12013-06-07 15:35:48 -0700401 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
Jesse Hallf21cffa2012-09-19 21:00:49 -0700402 return 0;
403
404success:
405 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700406 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700407 return config;
408}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800409
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700410void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700411
Mathias Agopiana4912602012-07-12 14:25:33 -0700412 ALOGI( "SurfaceFlinger's main thread ready to run. "
413 "Initializing graphics H/W...");
414
Jesse Hall692c7232012-11-08 15:41:56 -0800415 Mutex::Autolock _l(mStateLock);
416
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700417 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700418 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
419 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700420
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700421 // Initialize the H/W composer object. There may or may not be an
422 // actual hardware composer underneath.
423 mHwc = new HWComposer(this,
424 *static_cast<HWComposer::EventHandler *>(this));
425
Mathias Agopian875d8e12013-06-07 15:35:48 -0700426 // initialize the config and context (can't fail)
427 mEGLConfig = selectEGLConfig(mEGLDisplay, mHwc->getVisualID());
428
429 // print some debugging info
430 EGLint r,g,b,a;
431 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_RED_SIZE, &r);
432 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_GREEN_SIZE, &g);
433 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_BLUE_SIZE, &b);
434 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_ALPHA_SIZE, &a);
435 ALOGI("EGL informations:");
436 ALOGI("vendor : %s", eglQueryString(mEGLDisplay, EGL_VENDOR));
437 ALOGI("version : %s", eglQueryString(mEGLDisplay, EGL_VERSION));
438 ALOGI("extensions: %s", eglQueryString(mEGLDisplay, EGL_EXTENSIONS));
439 ALOGI("Client API: %s", eglQueryString(mEGLDisplay, EGL_CLIENT_APIS)?:"Not Supported");
440 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
441
442 // get a RenderEngine for the given display / config (can't fail)
443 mRenderEngine = RenderEngine::create(mEGLDisplay, mEGLConfig);
444
445 // retrieve the EGL context that was selected/created
446 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700447
Mathias Agopianed985572013-03-22 00:24:39 -0700448 // figure out which format we got
449 eglGetConfigAttrib(mEGLDisplay, mEGLConfig,
450 EGL_NATIVE_VISUAL_ID, &mEGLNativeVisualId);
451
Mathias Agopianda27af92012-09-13 18:17:13 -0700452 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
453 "couldn't create EGLContext");
454
Mathias Agopiancde87a32012-09-13 14:09:01 -0700455 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700456 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700457 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700458 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700459 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700460 // All non-virtual displays are currently considered secure.
461 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800462 createBuiltinDisplayLocked(type);
463 wp<IBinder> token = mBuiltinDisplays[i];
464
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700465 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
466 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i, bq);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700467 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700468 type, allocateHwcDisplayId(type), isSecure, token,
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700469 fbs, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700470 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700471 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700472 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700473 // for displays other than the main display, so we always
474 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700475 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700476 hw->acquireScreen();
477 }
478 mDisplays.add(token, hw);
479 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700480 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700481
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700482 // make the GLContext current so that we can create textures when creating Layers
483 // (which may happens before we render something)
484 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
485
Mathias Agopian028508c2012-07-25 21:12:12 -0700486 // start the EventThread
487 mEventThread = new EventThread(this);
488 mEventQueue.setEventThread(mEventThread);
489
Mathias Agopian92a979a2012-08-02 18:32:23 -0700490 // initialize our drawing state
491 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700492
Andy McFadden13a082e2012-08-24 10:16:42 -0700493 // set initial conditions (e.g. unblank default device)
494 initializeDisplays();
495
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700496 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700497 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800498}
499
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700500int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700501 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700502 type : mHwc->allocateDisplayId();
503}
504
Mathias Agopiana67e4182012-06-19 17:26:12 -0700505void SurfaceFlinger::startBootAnim() {
506 // start boot animation
507 property_set("service.bootanim.exit", "0");
508 property_set("ctl.start", "bootanim");
509}
510
Mathias Agopian875d8e12013-06-07 15:35:48 -0700511size_t SurfaceFlinger::getMaxTextureSize() const {
512 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700513}
514
Mathias Agopian875d8e12013-06-07 15:35:48 -0700515size_t SurfaceFlinger::getMaxViewportDims() const {
516 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700517}
518
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800519// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800520
Jamie Gennis582270d2011-08-17 18:19:00 -0700521bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800522 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800523 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800524 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700525 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800526}
527
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700528status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800529 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700530 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800531 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700532 type = i;
533 break;
534 }
535 }
536
537 if (type < 0) {
538 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700539 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700540
541 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700542 float xdpi = hwc.getDpiX(type);
543 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700544
545 // TODO: Not sure if display density should handled by SF any longer
546 class Density {
547 static int getDensityFromProperty(char const* propName) {
548 char property[PROPERTY_VALUE_MAX];
549 int density = 0;
550 if (property_get(propName, property, NULL) > 0) {
551 density = atoi(property);
552 }
553 return density;
554 }
555 public:
556 static int getEmuDensity() {
557 return getDensityFromProperty("qemu.sf.lcd_density"); }
558 static int getBuildDensity() {
559 return getDensityFromProperty("ro.sf.lcd_density"); }
560 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700561
562 if (type == DisplayDevice::DISPLAY_PRIMARY) {
563 // The density of the device is provided by a build property
564 float density = Density::getBuildDensity() / 160.0f;
565 if (density == 0) {
566 // the build doesn't provide a density -- this is wrong!
567 // use xdpi instead
568 ALOGE("ro.sf.lcd_density must be defined as a build property");
569 density = xdpi / 160.0f;
570 }
571 if (Density::getEmuDensity()) {
572 // if "qemu.sf.lcd_density" is specified, it overrides everything
573 xdpi = ydpi = density = Density::getEmuDensity();
574 density /= 160.0f;
575 }
576 info->density = density;
577
578 // TODO: this needs to go away (currently needed only by webkit)
579 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
580 info->orientation = hw->getOrientation();
Mathias Agopian1604f772012-09-18 21:54:42 -0700581 } else {
582 // TODO: where should this value come from?
583 static const int TV_DENSITY = 213;
584 info->density = TV_DENSITY / 160.0f;
585 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700586 }
587
Mathias Agopian1604f772012-09-18 21:54:42 -0700588 info->w = hwc.getWidth(type);
589 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700590 info->xdpi = xdpi;
591 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700592 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700593
594 // All non-virtual displays are currently considered secure.
595 info->secure = true;
596
Mathias Agopian888c8222012-08-04 21:10:38 -0700597 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700598}
599
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800600// ----------------------------------------------------------------------------
601
602sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800603 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700604}
605
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800606// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800607
608void SurfaceFlinger::waitForEvent() {
609 mEventQueue.waitMessage();
610}
611
612void SurfaceFlinger::signalTransaction() {
613 mEventQueue.invalidate();
614}
615
616void SurfaceFlinger::signalLayerUpdate() {
617 mEventQueue.invalidate();
618}
619
620void SurfaceFlinger::signalRefresh() {
621 mEventQueue.refresh();
622}
623
624status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
625 nsecs_t reltime, uint32_t flags) {
626 return mEventQueue.postMessage(msg, reltime);
627}
628
629status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
630 nsecs_t reltime, uint32_t flags) {
631 status_t res = mEventQueue.postMessage(msg, reltime);
632 if (res == NO_ERROR) {
633 msg->wait();
634 }
635 return res;
636}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800637
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700638void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700639 do {
640 waitForEvent();
641 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800642}
643
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700644void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700645 if (mEventThread == NULL) {
646 // This is a temporary workaround for b/7145521. A non-null pointer
647 // does not mean EventThread has finished initializing, so this
648 // is not a correct fix.
649 ALOGW("WARNING: EventThread not started, ignoring vsync");
650 return;
651 }
Jesse Hall9e663de2013-08-16 14:28:37 -0700652 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700653 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700654 mEventThread->onVSyncReceived(type, timestamp);
655 }
656}
657
658void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
659 if (mEventThread == NULL) {
660 // This is a temporary workaround for b/7145521. A non-null pointer
661 // does not mean EventThread has finished initializing, so this
662 // is not a correct fix.
663 ALOGW("WARNING: EventThread not started, ignoring hotplug");
664 return;
665 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700666
Jesse Hall9e663de2013-08-16 14:28:37 -0700667 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700668 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800669 if (connected) {
670 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700671 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800672 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
673 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700674 }
675 setTransactionFlags(eDisplayTransactionNeeded);
676
Andy McFadden9e9689c2012-10-10 18:17:51 -0700677 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700678 }
Mathias Agopian86303202012-07-24 22:46:10 -0700679}
680
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700681void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
682 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700683}
684
Mathias Agopian4fec8732012-06-29 14:12:52 -0700685void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800686 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800687 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700688 case MessageQueue::TRANSACTION:
689 handleMessageTransaction();
690 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700691 case MessageQueue::INVALIDATE:
692 handleMessageTransaction();
693 handleMessageInvalidate();
694 signalRefresh();
695 break;
696 case MessageQueue::REFRESH:
697 handleMessageRefresh();
698 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800699 }
700}
701
Mathias Agopian4fec8732012-06-29 14:12:52 -0700702void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700703 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700704 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700705 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700706 }
707}
708
709void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700710 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700711 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700712}
713
714void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700715 ATRACE_CALL();
716 preComposition();
717 rebuildLayerStacks();
718 setUpHWComposer();
719 doDebugFlashRegions();
720 doComposition();
721 postComposition();
722}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700723
Mathias Agopiancd60f992012-08-16 16:28:27 -0700724void SurfaceFlinger::doDebugFlashRegions()
725{
726 // is debugging enabled
727 if (CC_LIKELY(!mDebugRegion))
728 return;
729
730 const bool repaintEverything = mRepaintEverything;
731 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
732 const sp<DisplayDevice>& hw(mDisplays[dpy]);
733 if (hw->canDraw()) {
734 // transform the dirty region into this screen's coordinate space
735 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
736 if (!dirtyRegion.isEmpty()) {
737 // redraw the whole screen
738 doComposeSurfaces(hw, Region(hw->bounds()));
739
740 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700741 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700742 RenderEngine& engine(getRenderEngine());
743 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
744
Mathias Agopiancd60f992012-08-16 16:28:27 -0700745 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700746 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700747 }
748 }
749 }
750
751 postFramebuffer();
752
753 if (mDebugRegion > 1) {
754 usleep(mDebugRegion * 1000);
755 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700756
757 HWComposer& hwc(getHwComposer());
758 if (hwc.initCheck() == NO_ERROR) {
759 status_t err = hwc.prepare();
760 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
761 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700762}
763
764void SurfaceFlinger::preComposition()
765{
766 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700767 const LayerVector& layers(mDrawingState.layersSortedByZ);
768 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700769 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700770 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700771 needExtraInvalidate = true;
772 }
773 }
774 if (needExtraInvalidate) {
775 signalLayerUpdate();
776 }
777}
778
779void SurfaceFlinger::postComposition()
780{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700781 const LayerVector& layers(mDrawingState.layersSortedByZ);
782 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700783 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700784 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700785 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800786
787 if (mAnimCompositionPending) {
788 mAnimCompositionPending = false;
789
790 const HWComposer& hwc = getHwComposer();
791 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800792 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800793 mAnimFrameTracker.setActualPresentFence(presentFence);
794 } else {
795 // The HWC doesn't support present fences, so use the refresh
796 // timestamp instead.
797 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
798 mAnimFrameTracker.setActualPresentTime(presentTime);
799 }
800 mAnimFrameTracker.advanceFrame();
801 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700802}
803
804void SurfaceFlinger::rebuildLayerStacks() {
805 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700806 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700807 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700808 mVisibleRegionsDirty = false;
809 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700810
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700811 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700812 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700813 Region opaqueRegion;
814 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800815 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700816 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700817 const Transform& tr(hw->getTransform());
818 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700819 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700820 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700821 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700822
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700823 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700824 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700825 const sp<Layer>& layer(layers[i]);
826 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700827 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700828 Region drawRegion(tr.transform(
829 layer->visibleNonTransparentRegion));
830 drawRegion.andSelf(bounds);
831 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700832 layersSortedByZ.add(layer);
833 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700834 }
835 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700836 }
Mathias Agopian42977342012-08-05 00:40:46 -0700837 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700838 hw->undefinedRegion.set(bounds);
839 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
840 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700841 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700842 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700843}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700844
Mathias Agopiancd60f992012-08-16 16:28:27 -0700845void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700846 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
847 mDisplays[dpy]->beginFrame();
848 }
849
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700850 HWComposer& hwc(getHwComposer());
851 if (hwc.initCheck() == NO_ERROR) {
852 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700853 if (CC_UNLIKELY(mHwWorkListDirty)) {
854 mHwWorkListDirty = false;
855 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
856 sp<const DisplayDevice> hw(mDisplays[dpy]);
857 const int32_t id = hw->getHwcDisplayId();
858 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800859 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700860 hw->getVisibleLayersSortedByZ());
861 const size_t count = currentLayers.size();
862 if (hwc.createWorkList(id, count) == NO_ERROR) {
863 HWComposer::LayerListIterator cur = hwc.begin(id);
864 const HWComposer::LayerListIterator end = hwc.end(id);
865 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800866 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700867 layer->setGeometry(hw, *cur);
868 if (mDebugDisableHWC || mDebugRegion) {
869 cur->setSkip(true);
870 }
871 }
872 }
873 }
874 }
875 }
876
877 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700878 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700879 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700880 const int32_t id = hw->getHwcDisplayId();
881 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800882 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700883 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700884 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700885 HWComposer::LayerListIterator cur = hwc.begin(id);
886 const HWComposer::LayerListIterator end = hwc.end(id);
887 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
888 /*
889 * update the per-frame h/w composer data for each layer
890 * and build the transparent region of the FB
891 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800892 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700893 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700894 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700895 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700896 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700897
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700898 status_t err = hwc.prepare();
899 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700900
901 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
902 sp<const DisplayDevice> hw(mDisplays[dpy]);
903 hw->prepareFrame(hwc);
904 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700905 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700906}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700907
Mathias Agopiancd60f992012-08-16 16:28:27 -0700908void SurfaceFlinger::doComposition() {
909 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700910 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700911 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700912 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700913 if (hw->canDraw()) {
914 // transform the dirty region into this screen's coordinate space
915 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800916
917 // repaint the framebuffer (if needed)
918 doDisplayComposition(hw, dirtyRegion);
919
Mathias Agopiancd60f992012-08-16 16:28:27 -0700920 hw->dirtyRegion.clear();
921 hw->flip(hw->swapRegion);
922 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700923 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700924 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700925 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700926 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700927 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700928}
929
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800930void SurfaceFlinger::postFramebuffer()
931{
Mathias Agopian841cde52012-03-01 15:44:37 -0800932 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800933
Mathias Agopiana44b0412011-10-16 18:46:35 -0700934 const nsecs_t now = systemTime();
935 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700936
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700937 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700938 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700939 if (!hwc.supportsFramebufferTarget()) {
940 // EGL spec says:
941 // "surface must be bound to the calling thread's current context,
942 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700943 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700944 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700945 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700946 }
947
Mathias Agopian92a979a2012-08-02 18:32:23 -0700948 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700949 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800950 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700951 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700952 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700953 int32_t id = hw->getHwcDisplayId();
954 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700955 HWComposer::LayerListIterator cur = hwc.begin(id);
956 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700957 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700958 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700959 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700960 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700961 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700962 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700963 }
Jesse Hallef194142012-06-14 14:45:17 -0700964 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800965 }
966
Mathias Agopiana44b0412011-10-16 18:46:35 -0700967 mLastSwapBufferTime = systemTime() - now;
968 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700969
970 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
971 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
972 logFrameStats();
973 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800974}
975
Mathias Agopian87baae12012-07-31 12:38:26 -0700976void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800977{
Mathias Agopian841cde52012-03-01 15:44:37 -0800978 ATRACE_CALL();
979
Mathias Agopian7cc6df52013-06-05 14:30:54 -0700980 // here we keep a copy of the drawing state (that is the state that's
981 // going to be overwritten by handleTransactionLocked()) outside of
982 // mStateLock so that the side-effects of the State assignment
983 // don't happen with mStateLock held (which can cause deadlocks).
984 State drawingState(mDrawingState);
985
Mathias Agopianca4d3602011-05-19 15:38:14 -0700986 Mutex::Autolock _l(mStateLock);
987 const nsecs_t now = systemTime();
988 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800989
Mathias Agopianca4d3602011-05-19 15:38:14 -0700990 // Here we're guaranteed that some transaction flags are set
991 // so we can call handleTransactionLocked() unconditionally.
992 // We call getTransactionFlags(), which will also clear the flags,
993 // with mStateLock held to guarantee that mCurrentState won't change
994 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700995
Mathias Agopiane57f2922012-08-09 16:29:12 -0700996 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -0700997 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -0700998
Mathias Agopianca4d3602011-05-19 15:38:14 -0700999 mLastTransactionTime = systemTime() - now;
1000 mDebugInTransaction = 0;
1001 invalidateHwcGeometry();
1002 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001003}
1004
Mathias Agopian87baae12012-07-31 12:38:26 -07001005void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001006{
1007 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001008 const size_t count = currentLayers.size();
1009
1010 /*
1011 * Traversal of the children
1012 * (perform the transaction for each of them if needed)
1013 */
1014
Mathias Agopian3559b072012-08-15 13:46:03 -07001015 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001016 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001017 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001018 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1019 if (!trFlags) continue;
1020
1021 const uint32_t flags = layer->doTransaction(0);
1022 if (flags & Layer::eVisibleRegion)
1023 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024 }
1025 }
1026
1027 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001028 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001029 */
1030
Mathias Agopiane57f2922012-08-09 16:29:12 -07001031 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001032 // here we take advantage of Vector's copy-on-write semantics to
1033 // improve performance by skipping the transaction entirely when
1034 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001035 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1036 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001037 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001039 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001040 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001041
1042 // find the displays that were removed
1043 // (ie: in drawing state but not in current state)
1044 // also handle displays that changed
1045 // (ie: displays that are in both lists)
1046 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001047 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1048 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001049 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001050 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001051 // Call makeCurrent() on the primary display so we can
1052 // be sure that nothing associated with this display
1053 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001054 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001055 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001056 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1057 if (hw != NULL)
1058 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001059 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001060 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001061 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001062 } else {
1063 ALOGW("trying to remove the main display");
1064 }
1065 } else {
1066 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001067 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001068 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001069 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001070 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001071 // recreating the DisplayDevice, so we just remove it
1072 // from the drawing state, so that it get re-added
1073 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001074 sp<DisplayDevice> hw(getDisplayDevice(display));
1075 if (hw != NULL)
1076 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001077 mDisplays.removeItem(display);
1078 mDrawingState.displays.removeItemsAt(i);
1079 dc--; i--;
1080 // at this point we must loop to the next item
1081 continue;
1082 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001083
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001084 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001085 if (disp != NULL) {
1086 if (state.layerStack != draw[i].layerStack) {
1087 disp->setLayerStack(state.layerStack);
1088 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001089 if ((state.orientation != draw[i].orientation)
1090 || (state.viewport != draw[i].viewport)
1091 || (state.frame != draw[i].frame))
1092 {
1093 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001094 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001095 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001096 }
1097 }
1098 }
1099
1100 // find displays that were added
1101 // (ie: in current state but not in drawing state)
1102 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001103 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001104 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001105
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001106 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001107 sp<IGraphicBufferProducer> producer;
1108 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
1109
Jesse Hall02d86562013-03-25 14:43:23 -07001110 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001111 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001112 // Virtual displays without a surface are dormant:
1113 // they have external state (layer stack, projection,
1114 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001115 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001116
Jesse Hall02d86562013-03-25 14:43:23 -07001117 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001118 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1119 *mHwc, hwcDisplayId, state.surface, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001120 state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001121
1122 dispSurface = vds;
1123 if (hwcDisplayId >= 0) {
1124 producer = vds;
1125 } else {
1126 // There won't be any interaction with HWC for this virtual display,
1127 // so the GLES driver can pass buffers directly to the sink.
1128 producer = state.surface;
1129 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001130 }
1131 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001132 ALOGE_IF(state.surface!=NULL,
1133 "adding a supported display, but rendering "
1134 "surface is provided (%p), ignoring it",
1135 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001136 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001137 // for supported (by hwc) displays we provide our
1138 // own rendering surface
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001139 dispSurface = new FramebufferSurface(*mHwc, state.type, bq);
1140 producer = bq;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001141 }
1142
1143 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001144 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001145 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001146 state.type, hwcDisplayId, state.isSecure,
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001147 display, dispSurface, producer, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001148 hw->setLayerStack(state.layerStack);
1149 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001150 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001151 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001152 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001153 if (state.isVirtualDisplay()) {
1154 if (hwcDisplayId >= 0) {
1155 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1156 hw->getWidth(), hw->getHeight(),
1157 hw->getFormat());
1158 }
1159 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001160 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001161 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001162 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001163 }
1164 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001165 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001166 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001167
Mathias Agopian84300952012-11-21 16:02:13 -08001168 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1169 // The transform hint might have changed for some layers
1170 // (either because a display has changed, or because a layer
1171 // as changed).
1172 //
1173 // Walk through all the layers in currentLayers,
1174 // and update their transform hint.
1175 //
1176 // If a layer is visible only on a single display, then that
1177 // display is used to calculate the hint, otherwise we use the
1178 // default display.
1179 //
1180 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1181 // the hint is set before we acquire a buffer from the surface texture.
1182 //
1183 // NOTE: layer transactions have taken place already, so we use their
1184 // drawing state. However, SurfaceFlinger's own transaction has not
1185 // happened yet, so we must use the current state layer list
1186 // (soon to become the drawing state list).
1187 //
1188 sp<const DisplayDevice> disp;
1189 uint32_t currentlayerStack = 0;
1190 for (size_t i=0; i<count; i++) {
1191 // NOTE: we rely on the fact that layers are sorted by
1192 // layerStack first (so we don't have to traverse the list
1193 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001194 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001195 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001196 if (i==0 || currentlayerStack != layerStack) {
1197 currentlayerStack = layerStack;
1198 // figure out if this layerstack is mirrored
1199 // (more than one display) if so, pick the default display,
1200 // if not, pick the only display it's on.
1201 disp.clear();
1202 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1203 sp<const DisplayDevice> hw(mDisplays[dpy]);
1204 if (hw->getLayerStack() == currentlayerStack) {
1205 if (disp == NULL) {
1206 disp = hw;
1207 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001208 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001209 break;
1210 }
1211 }
1212 }
1213 }
Chet Haase91d25932013-04-11 15:24:55 -07001214 if (disp == NULL) {
1215 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1216 // redraw after transform hint changes. See bug 8508397.
1217
1218 // could be null when this layer is using a layerStack
1219 // that is not visible on any display. Also can occur at
1220 // screen off/on times.
1221 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001222 }
Chet Haase91d25932013-04-11 15:24:55 -07001223 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001224 }
1225 }
1226
1227
Mathias Agopian3559b072012-08-15 13:46:03 -07001228 /*
1229 * Perform our own transaction if needed
1230 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001231
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001232 const LayerVector& layers(mDrawingState.layersSortedByZ);
1233 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001234 // layers have been added
1235 mVisibleRegionsDirty = true;
1236 }
1237
1238 // some layers might have been removed, so
1239 // we need to update the regions they're exposing.
1240 if (mLayersRemoved) {
1241 mLayersRemoved = false;
1242 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001243 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001244 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001245 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001246 if (currentLayers.indexOf(layer) < 0) {
1247 // this layer is not visible anymore
1248 // TODO: we could traverse the tree from front to back and
1249 // compute the actual visible region
1250 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001251 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001252 Region visibleReg = s.transform.transform(
1253 Region(Rect(s.active.w, s.active.h)));
1254 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001255 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001256 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001257 }
1258
1259 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001260}
1261
1262void SurfaceFlinger::commitTransaction()
1263{
1264 if (!mLayersPendingRemoval.isEmpty()) {
1265 // Notify removed layers now that they can't be drawn from
1266 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1267 mLayersPendingRemoval[i]->onRemoved();
1268 }
1269 mLayersPendingRemoval.clear();
1270 }
1271
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001272 // If this transaction is part of a window animation then the next frame
1273 // we composite should be considered an animation as well.
1274 mAnimCompositionPending = mAnimTransactionPending;
1275
Mathias Agopian4fec8732012-06-29 14:12:52 -07001276 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001277 mTransactionPending = false;
1278 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001279 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001280}
1281
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001282void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001283 const LayerVector& currentLayers, uint32_t layerStack,
1284 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285{
Mathias Agopian841cde52012-03-01 15:44:37 -08001286 ATRACE_CALL();
1287
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288 Region aboveOpaqueLayers;
1289 Region aboveCoveredLayers;
1290 Region dirty;
1291
Mathias Agopian87baae12012-07-31 12:38:26 -07001292 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001293
1294 size_t i = currentLayers.size();
1295 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001296 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297
1298 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001299 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300
Jesse Hall01e29052013-02-19 16:13:35 -08001301 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001302 if (s.layerStack != layerStack)
1303 continue;
1304
Mathias Agopianab028732010-03-16 16:41:46 -07001305 /*
1306 * opaqueRegion: area of a surface that is fully opaque.
1307 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001308 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001309
1310 /*
1311 * visibleRegion: area of a surface that is visible on screen
1312 * and not fully transparent. This is essentially the layer's
1313 * footprint minus the opaque regions above it.
1314 * Areas covered by a translucent surface are considered visible.
1315 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001316 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001317
1318 /*
1319 * coveredRegion: area of a surface that is covered by all
1320 * visible regions above it (which includes the translucent areas).
1321 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001323
Jesse Halla8026d22012-09-25 13:25:04 -07001324 /*
1325 * transparentRegion: area of a surface that is hinted to be completely
1326 * transparent. This is only used to tell when the layer has no visible
1327 * non-transparent regions and can be removed from the layer list. It
1328 * does not affect the visibleRegion of this layer or any layers
1329 * beneath it. The hint may not be correct if apps don't respect the
1330 * SurfaceView restrictions (which, sadly, some don't).
1331 */
1332 Region transparentRegion;
1333
Mathias Agopianab028732010-03-16 16:41:46 -07001334
1335 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001336 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001337 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001338 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001339 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001340 if (!visibleRegion.isEmpty()) {
1341 // Remove the transparent area from the visible region
1342 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001343 const Transform tr(s.transform);
1344 if (tr.transformed()) {
1345 if (tr.preserveRects()) {
1346 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001347 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001348 } else {
1349 // transformation too complex, can't do the
1350 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001351 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001352 }
1353 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001354 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001355 }
Mathias Agopianab028732010-03-16 16:41:46 -07001356 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001357
Mathias Agopianab028732010-03-16 16:41:46 -07001358 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001359 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001360 if (s.alpha==255 && !translucent &&
1361 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1362 // the opaque region is the layer's footprint
1363 opaqueRegion = visibleRegion;
1364 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001365 }
1366 }
1367
Mathias Agopianab028732010-03-16 16:41:46 -07001368 // Clip the covered region to the visible region
1369 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1370
1371 // Update aboveCoveredLayers for next (lower) layer
1372 aboveCoveredLayers.orSelf(visibleRegion);
1373
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001374 // subtract the opaque region covered by the layers above us
1375 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001376
1377 // compute this layer's dirty region
1378 if (layer->contentDirty) {
1379 // we need to invalidate the whole region
1380 dirty = visibleRegion;
1381 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001382 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001383 layer->contentDirty = false;
1384 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001385 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001386 * the exposed region consists of two components:
1387 * 1) what's VISIBLE now and was COVERED before
1388 * 2) what's EXPOSED now less what was EXPOSED before
1389 *
1390 * note that (1) is conservative, we start with the whole
1391 * visible region but only keep what used to be covered by
1392 * something -- which mean it may have been exposed.
1393 *
1394 * (2) handles areas that were not covered by anything but got
1395 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001396 */
Mathias Agopianab028732010-03-16 16:41:46 -07001397 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001398 const Region oldVisibleRegion = layer->visibleRegion;
1399 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001400 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1401 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001402 }
1403 dirty.subtractSelf(aboveOpaqueLayers);
1404
1405 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001406 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001407
Mathias Agopianab028732010-03-16 16:41:46 -07001408 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001409 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001410
Jesse Halla8026d22012-09-25 13:25:04 -07001411 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412 layer->setVisibleRegion(visibleRegion);
1413 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001414 layer->setVisibleNonTransparentRegion(
1415 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416 }
1417
Mathias Agopian87baae12012-07-31 12:38:26 -07001418 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419}
1420
Mathias Agopian87baae12012-07-31 12:38:26 -07001421void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1422 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001423 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001424 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1425 if (hw->getLayerStack() == layerStack) {
1426 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001427 }
1428 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001429}
1430
1431void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001432{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001433 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001434
Mathias Agopian4fec8732012-06-29 14:12:52 -07001435 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001436 const LayerVector& layers(mDrawingState.layersSortedByZ);
1437 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001438 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001439 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001440 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001441 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001442 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001443 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001444
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001445 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001446}
1447
Mathias Agopianad456f92011-01-13 17:53:01 -08001448void SurfaceFlinger::invalidateHwcGeometry()
1449{
1450 mHwWorkListDirty = true;
1451}
1452
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001453
Mathias Agopiancd60f992012-08-16 16:28:27 -07001454void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001455 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001456{
Mathias Agopian87baae12012-07-31 12:38:26 -07001457 Region dirtyRegion(inDirtyRegion);
1458
Mathias Agopianb8a55602009-06-26 19:06:36 -07001459 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001460 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001461
Mathias Agopian42977342012-08-05 00:40:46 -07001462 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001463 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001464 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1465 // takes a rectangle, we must make sure to update that whole
1466 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001467 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001468 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001469 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001470 // We need to redraw the rectangle that will be updated
1471 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001472 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001473 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001474 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001475 } else {
1476 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001477 dirtyRegion.set(hw->bounds());
1478 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001479 }
1480 }
1481
Mathias Agopiancd60f992012-08-16 16:28:27 -07001482 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001484 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001485 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001486
1487 // swap buffers (presentation)
1488 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489}
1490
Mathias Agopiancd60f992012-08-16 16:28:27 -07001491void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001492{
Mathias Agopian3f844832013-08-07 21:24:32 -07001493 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001494 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001495 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001496 HWComposer::LayerListIterator cur = hwc.begin(id);
1497 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001498
Mathias Agopian85d751c2012-08-29 16:59:24 -07001499 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1500 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001501 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001502 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1503 hw->getDisplayName().string());
1504 return;
1505 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001506
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001507 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001508 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001509 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001510 // when using overlays, we assume a fully transparent framebuffer
1511 // NOTE: we could reduce how much we need to clear, for instance
1512 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001513 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001514 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001515 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001516 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001517 // we start with the whole screen area
1518 const Region bounds(hw->getBounds());
1519
1520 // we remove the scissor part
1521 // we're left with the letterbox region
1522 // (common case is that letterbox ends-up being empty)
1523 const Region letterbox(bounds.subtract(hw->getScissor()));
1524
1525 // compute the area to clear
1526 Region region(hw->undefinedRegion.merge(letterbox));
1527
1528 // but limit it to the dirty region
1529 region.andSelf(dirty);
1530
Mathias Agopianb9494d52012-04-18 02:28:45 -07001531 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001532 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001533 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001534 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001535 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001536 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001537
Mathias Agopian766dc492012-10-30 18:08:06 -07001538 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1539 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001540 // scissor on the main display. It should never be needed
1541 // anyways (though in theory it could since the API allows it).
1542 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001543 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001544 if (scissor != bounds) {
1545 // scissor doesn't match the screen's dimensions, so we
1546 // need to clear everything outside of it and enable
1547 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001548
Mathias Agopianf45c5102012-10-24 16:29:17 -07001549 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001550 const uint32_t height = hw->getHeight();
1551 engine.setScissor(scissor.left, height - scissor.bottom,
1552 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001553 }
1554 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001555 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001556
Mathias Agopian85d751c2012-08-29 16:59:24 -07001557 /*
1558 * and then, render the layers targeted at the framebuffer
1559 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001560
Mathias Agopian13127d82013-03-05 17:47:11 -08001561 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001562 const size_t count = layers.size();
1563 const Transform& tr = hw->getTransform();
1564 if (cur != end) {
1565 // we're using h/w composer
1566 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001567 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001568 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001569 if (!clip.isEmpty()) {
1570 switch (cur->getCompositionType()) {
1571 case HWC_OVERLAY: {
1572 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1573 && i
1574 && layer->isOpaque()
1575 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001576 // never clear the very first layer since we're
1577 // guaranteed the FB is already cleared
1578 layer->clearWithOpenGL(hw, clip);
1579 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001580 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001581 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001582 case HWC_FRAMEBUFFER: {
1583 layer->draw(hw, clip);
1584 break;
1585 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001586 case HWC_FRAMEBUFFER_TARGET: {
1587 // this should not happen as the iterator shouldn't
1588 // let us get there.
1589 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1590 break;
1591 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001592 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001593 }
1594 layer->setAcquireFence(hw, *cur);
1595 }
1596 } else {
1597 // we're not using h/w composer
1598 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001599 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001600 const Region clip(dirty.intersect(
1601 tr.transform(layer->visibleRegion)));
1602 if (!clip.isEmpty()) {
1603 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001604 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001605 }
1606 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001607
1608 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001609 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001610}
1611
Mathias Agopian3f844832013-08-07 21:24:32 -07001612void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001613 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001614 RenderEngine& engine(getRenderEngine());
1615 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001616}
1617
Mathias Agopianac9fa422013-02-11 16:40:36 -08001618void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1619 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001620 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001621 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001622{
Mathias Agopian96f08192010-06-02 23:28:45 -07001623 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001624 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001625
Mathias Agopian96f08192010-06-02 23:28:45 -07001626 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001627 Mutex::Autolock _l(mStateLock);
1628 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001629 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001630}
1631
Mathias Agopian3f844832013-08-07 21:24:32 -07001632status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001633 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001634 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001635 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001636 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001637 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001638 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001639 return NO_ERROR;
1640 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001641 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001642}
1643
Mathias Agopian3f844832013-08-07 21:24:32 -07001644uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001645 return android_atomic_release_load(&mTransactionFlags);
1646}
1647
Mathias Agopian3f844832013-08-07 21:24:32 -07001648uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001649 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1650}
1651
Mathias Agopian3f844832013-08-07 21:24:32 -07001652uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001653 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1654 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001655 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001656 }
1657 return old;
1658}
1659
Mathias Agopian8b33f032012-07-24 20:43:54 -07001660void SurfaceFlinger::setTransactionState(
1661 const Vector<ComposerState>& state,
1662 const Vector<DisplayState>& displays,
1663 uint32_t flags)
1664{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001665 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001666 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001667 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001668
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001669 if (flags & eAnimation) {
1670 // For window updates that are part of an animation we must wait for
1671 // previous animation "frames" to be handled.
1672 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001673 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001674 if (CC_UNLIKELY(err != NO_ERROR)) {
1675 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001676 // caller after a few seconds.
1677 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1678 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001679 mAnimTransactionPending = false;
1680 break;
1681 }
1682 }
1683 }
1684
Mathias Agopiane57f2922012-08-09 16:29:12 -07001685 size_t count = displays.size();
1686 for (size_t i=0 ; i<count ; i++) {
1687 const DisplayState& s(displays[i]);
1688 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001689 }
1690
Mathias Agopiane57f2922012-08-09 16:29:12 -07001691 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001692 for (size_t i=0 ; i<count ; i++) {
1693 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001694 // Here we need to check that the interface we're given is indeed
1695 // one of our own. A malicious client could give us a NULL
1696 // IInterface, or one of its own or even one of our own but a
1697 // different type. All these situations would cause us to crash.
1698 //
1699 // NOTE: it would be better to use RTTI as we could directly check
1700 // that we have a Client*. however, RTTI is disabled in Android.
1701 if (s.client != NULL) {
1702 sp<IBinder> binder = s.client->asBinder();
1703 if (binder != NULL) {
1704 String16 desc(binder->getInterfaceDescriptor());
1705 if (desc == ISurfaceComposerClient::descriptor) {
1706 sp<Client> client( static_cast<Client *>(s.client.get()) );
1707 transactionFlags |= setClientStateLocked(client, s.state);
1708 }
1709 }
1710 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001711 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001712
Mathias Agopian386aa982011-11-07 21:58:03 -08001713 if (transactionFlags) {
1714 // this triggers the transaction
1715 setTransactionFlags(transactionFlags);
1716
1717 // if this is a synchronous transaction, wait for it to take effect
1718 // before returning.
1719 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001720 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001721 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001722 if (flags & eAnimation) {
1723 mAnimTransactionPending = true;
1724 }
1725 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001726 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1727 if (CC_UNLIKELY(err != NO_ERROR)) {
1728 // just in case something goes wrong in SF, return to the
1729 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001730 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1731 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001732 break;
1733 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001734 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001735 }
1736}
1737
Mathias Agopiane57f2922012-08-09 16:29:12 -07001738uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1739{
Jesse Hall9a143922012-10-04 16:29:19 -07001740 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1741 if (dpyIdx < 0)
1742 return 0;
1743
Mathias Agopiane57f2922012-08-09 16:29:12 -07001744 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001745 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001746 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001747 const uint32_t what = s.what;
1748 if (what & DisplayState::eSurfaceChanged) {
1749 if (disp.surface->asBinder() != s.surface->asBinder()) {
1750 disp.surface = s.surface;
1751 flags |= eDisplayTransactionNeeded;
1752 }
1753 }
1754 if (what & DisplayState::eLayerStackChanged) {
1755 if (disp.layerStack != s.layerStack) {
1756 disp.layerStack = s.layerStack;
1757 flags |= eDisplayTransactionNeeded;
1758 }
1759 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001760 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001761 if (disp.orientation != s.orientation) {
1762 disp.orientation = s.orientation;
1763 flags |= eDisplayTransactionNeeded;
1764 }
1765 if (disp.frame != s.frame) {
1766 disp.frame = s.frame;
1767 flags |= eDisplayTransactionNeeded;
1768 }
1769 if (disp.viewport != s.viewport) {
1770 disp.viewport = s.viewport;
1771 flags |= eDisplayTransactionNeeded;
1772 }
1773 }
1774 }
1775 return flags;
1776}
1777
1778uint32_t SurfaceFlinger::setClientStateLocked(
1779 const sp<Client>& client,
1780 const layer_state_t& s)
1781{
1782 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001783 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001784 if (layer != 0) {
1785 const uint32_t what = s.what;
1786 if (what & layer_state_t::ePositionChanged) {
1787 if (layer->setPosition(s.x, s.y))
1788 flags |= eTraversalNeeded;
1789 }
1790 if (what & layer_state_t::eLayerChanged) {
1791 // NOTE: index needs to be calculated before we update the state
1792 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1793 if (layer->setLayer(s.z)) {
1794 mCurrentState.layersSortedByZ.removeAt(idx);
1795 mCurrentState.layersSortedByZ.add(layer);
1796 // we need traversal (state changed)
1797 // AND transaction (list changed)
1798 flags |= eTransactionNeeded|eTraversalNeeded;
1799 }
1800 }
1801 if (what & layer_state_t::eSizeChanged) {
1802 if (layer->setSize(s.w, s.h)) {
1803 flags |= eTraversalNeeded;
1804 }
1805 }
1806 if (what & layer_state_t::eAlphaChanged) {
1807 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1808 flags |= eTraversalNeeded;
1809 }
1810 if (what & layer_state_t::eMatrixChanged) {
1811 if (layer->setMatrix(s.matrix))
1812 flags |= eTraversalNeeded;
1813 }
1814 if (what & layer_state_t::eTransparentRegionChanged) {
1815 if (layer->setTransparentRegionHint(s.transparentRegion))
1816 flags |= eTraversalNeeded;
1817 }
1818 if (what & layer_state_t::eVisibilityChanged) {
1819 if (layer->setFlags(s.flags, s.mask))
1820 flags |= eTraversalNeeded;
1821 }
1822 if (what & layer_state_t::eCropChanged) {
1823 if (layer->setCrop(s.crop))
1824 flags |= eTraversalNeeded;
1825 }
1826 if (what & layer_state_t::eLayerStackChanged) {
1827 // NOTE: index needs to be calculated before we update the state
1828 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1829 if (layer->setLayerStack(s.layerStack)) {
1830 mCurrentState.layersSortedByZ.removeAt(idx);
1831 mCurrentState.layersSortedByZ.add(layer);
1832 // we need traversal (state changed)
1833 // AND transaction (list changed)
1834 flags |= eTransactionNeeded|eTraversalNeeded;
1835 }
1836 }
1837 }
1838 return flags;
1839}
1840
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001841status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001842 const String8& name,
1843 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001844 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1845 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001846{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001847 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001848 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001849 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001850 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001851 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001852 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001853
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001854 status_t result = NO_ERROR;
1855
1856 sp<Layer> layer;
1857
Mathias Agopian3165cc22012-08-08 19:42:09 -07001858 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1859 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001860 result = createNormalLayer(client,
1861 name, w, h, flags, format,
1862 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001863 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001864 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001865 result = createDimLayer(client,
1866 name, w, h, flags,
1867 handle, gbp, &layer);
1868 break;
1869 default:
1870 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001871 break;
1872 }
1873
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001874 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001875 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001876 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001877 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001878 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001879}
1880
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001881status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1882 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1883 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001884{
1885 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001886 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001887 case PIXEL_FORMAT_TRANSPARENT:
1888 case PIXEL_FORMAT_TRANSLUCENT:
1889 format = PIXEL_FORMAT_RGBA_8888;
1890 break;
1891 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001892#ifdef NO_RGBX_8888
1893 format = PIXEL_FORMAT_RGB_565;
1894#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001895 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001896#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001897 break;
1898 }
1899
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001900#ifdef NO_RGBX_8888
1901 if (format == PIXEL_FORMAT_RGBX_8888)
1902 format = PIXEL_FORMAT_RGBA_8888;
1903#endif
1904
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001905 *outLayer = new Layer(this, client, name, w, h, flags);
1906 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1907 if (err == NO_ERROR) {
1908 *handle = (*outLayer)->getHandle();
1909 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001910 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001911
1912 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1913 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001914}
1915
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001916status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1917 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1918 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001919{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001920 *outLayer = new LayerDim(this, client, name, w, h, flags);
1921 *handle = (*outLayer)->getHandle();
1922 *gbp = (*outLayer)->getBufferQueue();
1923 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001924}
1925
Mathias Agopianac9fa422013-02-11 16:40:36 -08001926status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001927{
Mathias Agopian67106042013-03-14 19:18:13 -07001928 // called by the window manager when it wants to remove a Layer
1929 status_t err = NO_ERROR;
1930 sp<Layer> l(client->getLayerUser(handle));
1931 if (l != NULL) {
1932 err = removeLayer(l);
1933 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1934 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001935 }
1936 return err;
1937}
1938
Mathias Agopian13127d82013-03-05 17:47:11 -08001939status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001940{
Mathias Agopian67106042013-03-14 19:18:13 -07001941 // called by ~LayerCleaner() when all references to the IBinder (handle)
1942 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001943 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001944 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001945 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07001946 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001947 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001948 "error removing layer=%p (%s)", l.get(), strerror(-err));
1949 }
1950 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001951}
1952
Mathias Agopianb60314a2012-04-10 22:09:54 -07001953// ---------------------------------------------------------------------------
1954
Andy McFadden13a082e2012-08-24 10:16:42 -07001955void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08001956 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07001957 Vector<ComposerState> state;
1958 Vector<DisplayState> displays;
1959 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08001960 d.what = DisplayState::eDisplayProjectionChanged |
1961 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08001962 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08001963 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07001964 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001965 d.frame.makeInvalid();
1966 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001967 displays.add(d);
1968 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001969 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07001970
1971 const nsecs_t period =
1972 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
1973 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07001974}
1975
1976void SurfaceFlinger::initializeDisplays() {
1977 class MessageScreenInitialized : public MessageBase {
1978 SurfaceFlinger* flinger;
1979 public:
1980 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1981 virtual bool handler() {
1982 flinger->onInitializeDisplays();
1983 return true;
1984 }
1985 };
1986 sp<MessageBase> msg = new MessageScreenInitialized(this);
1987 postMessageAsync(msg); // we may be called from main thread, use async message
1988}
1989
1990
Mathias Agopiancde87a32012-09-13 14:09:01 -07001991void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07001992 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
1993 if (hw->isScreenAcquired()) {
1994 // this is expected, e.g. when power manager wakes up during boot
1995 ALOGD(" screen was previously acquired");
1996 return;
1997 }
1998
Mathias Agopian42977342012-08-05 00:40:46 -07001999 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002000 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002001 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002002 // built-in display, tell the HWC
2003 getHwComposer().acquire(type);
2004
2005 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2006 // FIXME: eventthread only knows about the main display right now
2007 mEventThread->onScreenAcquired();
2008 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002009 }
Mathias Agopian20128302012-08-13 18:32:13 -07002010 mVisibleRegionsDirty = true;
2011 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002012}
2013
Mathias Agopiancde87a32012-09-13 14:09:01 -07002014void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002015 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2016 if (!hw->isScreenAcquired()) {
2017 ALOGD(" screen was previously released");
2018 return;
2019 }
2020
2021 hw->releaseScreen();
2022 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002023 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002024 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002025 // FIXME: eventthread only knows about the main display right now
2026 mEventThread->onScreenReleased();
2027 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002028
2029 // built-in display, tell the HWC
2030 getHwComposer().release(type);
2031 }
2032 mVisibleRegionsDirty = true;
2033 // from this point on, SF will stop drawing on this display
2034}
2035
2036void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2037 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002038 SurfaceFlinger& mFlinger;
2039 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002040 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002041 MessageScreenAcquired(SurfaceFlinger& flinger,
2042 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002043 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002044 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2045 if (hw == NULL) {
2046 ALOGE("Attempt to unblank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002047 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002048 ALOGW("Attempt to unblank virtual display");
2049 } else {
2050 mFlinger.onScreenAcquired(hw);
2051 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002052 return true;
2053 }
2054 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002055 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2056 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002057}
2058
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002059void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002060 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002061 SurfaceFlinger& mFlinger;
2062 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002063 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002064 MessageScreenReleased(SurfaceFlinger& flinger,
2065 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002066 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002067 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2068 if (hw == NULL) {
2069 ALOGE("Attempt to blank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002070 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002071 ALOGW("Attempt to blank virtual display");
2072 } else {
2073 mFlinger.onScreenReleased(hw);
2074 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002075 return true;
2076 }
2077 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002078 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2079 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002080}
2081
2082// ---------------------------------------------------------------------------
2083
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002084status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2085{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002086 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002087
Mathias Agopianbd115332013-04-18 16:41:04 -07002088 IPCThreadState* ipc = IPCThreadState::self();
2089 const int pid = ipc->getCallingPid();
2090 const int uid = ipc->getCallingUid();
2091 if ((uid != AID_SHELL) &&
2092 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002093 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002094 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002095 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002096 // Try to get the main lock, but don't insist if we can't
2097 // (this would indicate SF is stuck, but we want to be able to
2098 // print something in dumpsys).
2099 int retry = 3;
2100 while (mStateLock.tryLock()<0 && --retry>=0) {
2101 usleep(1000000);
2102 }
2103 const bool locked(retry >= 0);
2104 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002105 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002106 "SurfaceFlinger appears to be unresponsive, "
2107 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002108 }
2109
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002110 bool dumpAll = true;
2111 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002112 size_t numArgs = args.size();
2113 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002114 if ((index < numArgs) &&
2115 (args[index] == String16("--list"))) {
2116 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002117 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002118 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002119 }
2120
2121 if ((index < numArgs) &&
2122 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002123 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002124 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002125 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002126 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002127
2128 if ((index < numArgs) &&
2129 (args[index] == String16("--latency-clear"))) {
2130 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002131 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002132 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002133 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002134 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002135
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002136 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002137 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002138 }
2139
Mathias Agopian9795c422009-08-26 16:36:26 -07002140 if (locked) {
2141 mStateLock.unlock();
2142 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002143 }
2144 write(fd, result.string(), result.size());
2145 return NO_ERROR;
2146}
2147
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002148void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002149 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002150{
2151 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2152 const size_t count = currentLayers.size();
2153 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002154 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002155 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002156 }
2157}
2158
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002159void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002160 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002161{
2162 String8 name;
2163 if (index < args.size()) {
2164 name = String8(args[index]);
2165 index++;
2166 }
2167
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002168 const nsecs_t period =
2169 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2170 result.appendFormat("%lld\n", period);
2171
2172 if (name.isEmpty()) {
2173 mAnimFrameTracker.dump(result);
2174 } else {
2175 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2176 const size_t count = currentLayers.size();
2177 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002178 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002179 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002180 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002181 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002182 }
2183 }
2184}
2185
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002186void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002187 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002188{
2189 String8 name;
2190 if (index < args.size()) {
2191 name = String8(args[index]);
2192 index++;
2193 }
2194
2195 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2196 const size_t count = currentLayers.size();
2197 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002198 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002199 if (name.isEmpty() || (name == layer->getName())) {
2200 layer->clearStats();
2201 }
2202 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002203
2204 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002205}
2206
Jamie Gennis6547ff42013-07-16 20:12:42 -07002207// This should only be called from the main thread. Otherwise it would need
2208// the lock and should use mCurrentState rather than mDrawingState.
2209void SurfaceFlinger::logFrameStats() {
2210 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2211 const size_t count = drawingLayers.size();
2212 for (size_t i=0 ; i<count ; i++) {
2213 const sp<Layer>& layer(drawingLayers[i]);
2214 layer->logFrameStats();
2215 }
2216
2217 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2218}
2219
Andy McFadden4803b742012-09-24 19:07:20 -07002220/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2221{
2222 static const char* config =
2223 " [sf"
2224#ifdef NO_RGBX_8888
2225 " NO_RGBX_8888"
2226#endif
2227#ifdef HAS_CONTEXT_PRIORITY
2228 " HAS_CONTEXT_PRIORITY"
2229#endif
2230#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2231 " NEVER_DEFAULT_TO_ASYNC_MODE"
2232#endif
2233#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2234 " TARGET_DISABLE_TRIPLE_BUFFERING"
2235#endif
2236 "]";
2237 result.append(config);
2238}
2239
Mathias Agopian74d211a2013-04-22 16:55:35 +02002240void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2241 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002242{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002243 bool colorize = false;
2244 if (index < args.size()
2245 && (args[index] == String16("--color"))) {
2246 colorize = true;
2247 index++;
2248 }
2249
2250 Colorizer colorizer(colorize);
2251
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002252 // figure out if we're stuck somewhere
2253 const nsecs_t now = systemTime();
2254 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2255 const nsecs_t inTransaction(mDebugInTransaction);
2256 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2257 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2258
2259 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002260 * Dump library configuration.
2261 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002262
2263 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002264 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002265 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002266 appendSfConfigString(result);
2267 appendUiConfigString(result);
2268 appendGuiConfigString(result);
2269 result.append("\n");
2270
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002271 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002272 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002273 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002274 result.append(SyncFeatures::getInstance().toString());
2275 result.append("\n");
2276
Andy McFadden4803b742012-09-24 19:07:20 -07002277 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002278 * Dump the visible layer list
2279 */
2280 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2281 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002282 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002283 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002284 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002285 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002286 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002287 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002288 }
2289
2290 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002291 * Dump Display state
2292 */
2293
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002294 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002295 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002296 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002297 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2298 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002299 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002300 }
2301
2302 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002303 * Dump SurfaceFlinger global state
2304 */
2305
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002306 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002307 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002308 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002309
Mathias Agopian888c8222012-08-04 21:10:38 -07002310 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002311 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002312
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002313 colorizer.bold(result);
2314 result.appendFormat("EGL implementation : %s\n",
2315 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2316 colorizer.reset(result);
2317 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002318 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002319
Mathias Agopian875d8e12013-06-07 15:35:48 -07002320 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002321
Mathias Agopian42977342012-08-05 00:40:46 -07002322 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002323 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002324 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002325 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002326 " last eglSwapBuffers() time: %f us\n"
2327 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002328 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002329 " refresh-rate : %f fps\n"
2330 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002331 " y-dpi : %f\n"
2332 " EGL_NATIVE_VISUAL_ID : %d\n"
2333 " gpu_to_cpu_unsupported : %d\n"
2334 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002335 mLastSwapBufferTime/1000.0,
2336 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002337 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002338 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2339 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002340 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2341 mEGLNativeVisualId,
2342 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002343
Mathias Agopian74d211a2013-04-22 16:55:35 +02002344 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002345 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002346
Mathias Agopian74d211a2013-04-22 16:55:35 +02002347 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002348 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002349
2350 /*
2351 * VSYNC state
2352 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002353 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002354
2355 /*
2356 * Dump HWComposer state
2357 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002358 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002359 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002360 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002361 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002362 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2363 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002364 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002365
2366 /*
2367 * Dump gralloc state
2368 */
2369 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2370 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002371}
2372
Mathias Agopian13127d82013-03-05 17:47:11 -08002373const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002374SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002375 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002376 wp<IBinder> dpy;
2377 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2378 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2379 dpy = mDisplays.keyAt(i);
2380 break;
2381 }
2382 }
2383 if (dpy == NULL) {
2384 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2385 // Just use the primary display so we have something to return
2386 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2387 }
2388 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002389}
2390
Keun young Park63f165f2012-08-31 10:53:36 -07002391bool SurfaceFlinger::startDdmConnection()
2392{
2393 void* libddmconnection_dso =
2394 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2395 if (!libddmconnection_dso) {
2396 return false;
2397 }
2398 void (*DdmConnection_start)(const char* name);
2399 DdmConnection_start =
2400 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2401 if (!DdmConnection_start) {
2402 dlclose(libddmconnection_dso);
2403 return false;
2404 }
2405 (*DdmConnection_start)(getServiceName());
2406 return true;
2407}
2408
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002409status_t SurfaceFlinger::onTransact(
2410 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2411{
2412 switch (code) {
2413 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002414 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002415 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002416 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002417 case BLANK:
2418 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002419 {
2420 // codes that require permission check
2421 IPCThreadState* ipc = IPCThreadState::self();
2422 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002423 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002424 if ((uid != AID_GRAPHICS) &&
2425 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002426 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002427 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2428 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002429 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002430 break;
2431 }
2432 case CAPTURE_SCREEN:
2433 {
2434 // codes that require permission check
2435 IPCThreadState* ipc = IPCThreadState::self();
2436 const int pid = ipc->getCallingPid();
2437 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002438 if ((uid != AID_GRAPHICS) &&
2439 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002440 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002441 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2442 return PERMISSION_DENIED;
2443 }
2444 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002445 }
2446 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002447
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002448 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2449 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002450 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002451 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002452 IPCThreadState* ipc = IPCThreadState::self();
2453 const int pid = ipc->getCallingPid();
2454 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002455 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002456 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002457 return PERMISSION_DENIED;
2458 }
2459 int n;
2460 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002461 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002462 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002463 return NO_ERROR;
2464 case 1002: // SHOW_UPDATES
2465 n = data.readInt32();
2466 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002467 invalidateHwcGeometry();
2468 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002469 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002470 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002471 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002472 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002473 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002474 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002475 setTransactionFlags(
2476 eTransactionNeeded|
2477 eDisplayTransactionNeeded|
2478 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002479 return NO_ERROR;
2480 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002481 case 1006:{ // send empty update
2482 signalRefresh();
2483 return NO_ERROR;
2484 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002485 case 1008: // toggle use of hw composer
2486 n = data.readInt32();
2487 mDebugDisableHWC = n ? 1 : 0;
2488 invalidateHwcGeometry();
2489 repaintEverything();
2490 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002491 case 1009: // toggle use of transform hint
2492 n = data.readInt32();
2493 mDebugDisableTransformHint = n ? 1 : 0;
2494 invalidateHwcGeometry();
2495 repaintEverything();
2496 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002497 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002498 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002499 reply->writeInt32(0);
2500 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002501 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002502 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002503 return NO_ERROR;
2504 case 1013: {
2505 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002506 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2507 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002508 }
2509 return NO_ERROR;
2510 }
2511 }
2512 return err;
2513}
2514
Mathias Agopian53331da2011-08-22 21:44:41 -07002515void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002516 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002517 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002518}
2519
Mathias Agopian59119e62010-10-11 12:37:43 -07002520// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002521// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002522// ---------------------------------------------------------------------------
2523
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002524/* The code below is here to handle b/8734824
2525 *
2526 * We create a IGraphicBufferProducer wrapper that forwards all calls
2527 * to the calling binder thread, where they are executed. This allows
2528 * the calling thread to be reused (on the other side) and not
2529 * depend on having "enough" binder threads to handle the requests.
2530 *
2531 */
2532
2533class GraphicProducerWrapper : public BBinder, public MessageHandler {
2534 sp<IGraphicBufferProducer> impl;
2535 sp<Looper> looper;
2536 status_t result;
2537 bool exitPending;
2538 bool exitRequested;
2539 mutable Barrier barrier;
2540 volatile int32_t memoryBarrier;
2541 uint32_t code;
2542 Parcel const* data;
2543 Parcel* reply;
2544
2545 enum {
2546 MSG_API_CALL,
2547 MSG_EXIT
2548 };
2549
2550 /*
2551 * this is called by our "fake" BpGraphicBufferProducer. We package the
2552 * data and reply Parcel and forward them to the calling thread.
2553 */
2554 virtual status_t transact(uint32_t code,
2555 const Parcel& data, Parcel* reply, uint32_t flags) {
2556 this->code = code;
2557 this->data = &data;
2558 this->reply = reply;
2559 android_atomic_acquire_store(0, &memoryBarrier);
2560 if (exitPending) {
2561 // if we've exited, we run the message synchronously right here
2562 handleMessage(Message(MSG_API_CALL));
2563 } else {
2564 barrier.close();
2565 looper->sendMessage(this, Message(MSG_API_CALL));
2566 barrier.wait();
2567 }
2568 return NO_ERROR;
2569 }
2570
2571 /*
2572 * here we run on the binder calling thread. All we've got to do is
2573 * call the real BpGraphicBufferProducer.
2574 */
2575 virtual void handleMessage(const Message& message) {
2576 android_atomic_release_load(&memoryBarrier);
2577 if (message.what == MSG_API_CALL) {
2578 impl->asBinder()->transact(code, data[0], reply);
2579 barrier.open();
2580 } else if (message.what == MSG_EXIT) {
2581 exitRequested = true;
2582 }
2583 }
2584
2585public:
2586 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2587 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2588 exitPending(false), exitRequested(false) {
2589 }
2590
2591 status_t waitForResponse() {
2592 do {
2593 looper->pollOnce(-1);
2594 } while (!exitRequested);
2595 return result;
2596 }
2597
2598 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002599 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002600 exitPending = true;
2601 looper->sendMessage(this, Message(MSG_EXIT));
2602 }
2603};
2604
2605
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002606status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2607 const sp<IGraphicBufferProducer>& producer,
2608 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002609 uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002610
2611 if (CC_UNLIKELY(display == 0))
2612 return BAD_VALUE;
2613
2614 if (CC_UNLIKELY(producer == 0))
2615 return BAD_VALUE;
2616
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002617 // if we have secure windows on this display, never allow the screen capture
2618 // unless the producer interface is local (i.e.: we can take a screenshot for
2619 // ourselves).
2620 if (!producer->asBinder()->localBinder()) {
2621 Mutex::Autolock _l(mStateLock);
2622 sp<const DisplayDevice> hw(getDisplayDevice(display));
2623 if (hw->getSecureLayerVisible()) {
2624 ALOGW("FB is protected: PERMISSION_DENIED");
2625 return PERMISSION_DENIED;
2626 }
2627 }
2628
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002629 class MessageCaptureScreen : public MessageBase {
2630 SurfaceFlinger* flinger;
2631 sp<IBinder> display;
2632 sp<IGraphicBufferProducer> producer;
2633 uint32_t reqWidth, reqHeight;
2634 uint32_t minLayerZ,maxLayerZ;
2635 status_t result;
2636 public:
2637 MessageCaptureScreen(SurfaceFlinger* flinger,
2638 const sp<IBinder>& display,
2639 const sp<IGraphicBufferProducer>& producer,
2640 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002641 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002642 : flinger(flinger), display(display), producer(producer),
2643 reqWidth(reqWidth), reqHeight(reqHeight),
2644 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2645 result(PERMISSION_DENIED)
2646 {
2647 }
2648 status_t getResult() const {
2649 return result;
2650 }
2651 virtual bool handler() {
2652 Mutex::Autolock _l(flinger->mStateLock);
2653 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002654 result = flinger->captureScreenImplLocked(hw,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002655 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002656 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002657 return true;
2658 }
2659 };
2660
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002661 // make sure to process transactions before screenshots -- a transaction
2662 // might already be pending but scheduled for VSYNC; this guarantees we
2663 // will handle it before the screenshot. When VSYNC finally arrives
2664 // the scheduled transaction will be a no-op. If no transactions are
2665 // scheduled at this time, this will end-up being a no-op as well.
2666 mEventQueue.invalidateTransactionNow();
2667
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002668 // this creates a "fake" BBinder which will serve as a "fake" remote
2669 // binder to receive the marshaled calls and forward them to the
2670 // real remote (a BpGraphicBufferProducer)
2671 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2672
2673 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2674 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002675 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002676 display, IGraphicBufferProducer::asInterface( wrapper ),
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002677 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002678
2679 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002680 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002681 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002682 }
2683 return res;
2684}
2685
Mathias Agopian180f10d2013-04-10 22:55:41 -07002686
2687void SurfaceFlinger::renderScreenImplLocked(
2688 const sp<const DisplayDevice>& hw,
2689 uint32_t reqWidth, uint32_t reqHeight,
2690 uint32_t minLayerZ, uint32_t maxLayerZ,
2691 bool yswap)
2692{
2693 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002694 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002695
2696 // get screen geometry
2697 const uint32_t hw_w = hw->getWidth();
2698 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002699 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2700
2701 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002702 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002703
2704 // set-up our viewport
Mathias Agopian3f844832013-08-07 21:24:32 -07002705 engine.setViewportAndProjection(reqWidth, reqHeight, hw_w, hw_h, yswap);
2706 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002707
2708 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002709 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002710
2711 const LayerVector& layers( mDrawingState.layersSortedByZ );
2712 const size_t count = layers.size();
2713 for (size_t i=0 ; i<count ; ++i) {
2714 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002715 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002716 if (state.layerStack == hw->getLayerStack()) {
2717 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2718 if (layer->isVisible()) {
2719 if (filtering) layer->setFiltering(true);
2720 layer->draw(hw);
2721 if (filtering) layer->setFiltering(false);
2722 }
2723 }
2724 }
2725 }
2726
2727 // compositionComplete is needed for older driver
2728 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002729 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002730}
2731
2732
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002733status_t SurfaceFlinger::captureScreenImplLocked(
2734 const sp<const DisplayDevice>& hw,
2735 const sp<IGraphicBufferProducer>& producer,
2736 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002737 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002738{
2739 ATRACE_CALL();
2740
Mathias Agopian180f10d2013-04-10 22:55:41 -07002741 // get screen geometry
2742 const uint32_t hw_w = hw->getWidth();
2743 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002744
Mathias Agopian180f10d2013-04-10 22:55:41 -07002745 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2746 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2747 reqWidth, reqHeight, hw_w, hw_h);
2748 return BAD_VALUE;
2749 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002750
Mathias Agopian180f10d2013-04-10 22:55:41 -07002751 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2752 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2753
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002754 // create a surface (because we're a producer, and we need to
2755 // dequeue/queue a buffer)
2756 sp<Surface> sur = new Surface(producer);
2757 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002758
2759 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002760 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002761 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2762 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002763
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002764 int err = 0;
2765 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07002766 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002767 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2768 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002769
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002770 if (err == NO_ERROR) {
2771 ANativeWindowBuffer* buffer;
2772 /* TODO: Once we have the sync framework everywhere this can use
2773 * server-side waits on the fence that dequeueBuffer returns.
2774 */
2775 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2776 if (result == NO_ERROR) {
2777 // create an EGLImage from the buffer so we can later
2778 // turn it into a texture
2779 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2780 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2781 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002782 // this binds the given EGLImage as a framebuffer for the
2783 // duration of this scope.
2784 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2785 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002786 // this will in fact render into our dequeued buffer
2787 // via an FBO, which means we didn't have to create
2788 // an EGLSurface and therefore we're not
2789 // dependent on the context's EGLConfig.
2790 renderScreenImplLocked(hw, reqWidth, reqHeight,
2791 minLayerZ, maxLayerZ, true);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002792 } else {
2793 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2794 result = INVALID_OPERATION;
2795 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002796 // destroy our image
2797 eglDestroyImageKHR(mEGLDisplay, image);
2798 } else {
2799 result = BAD_VALUE;
2800 }
2801 window->queueBuffer(window, buffer, -1);
2802 }
2803 } else {
2804 result = BAD_VALUE;
2805 }
2806 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2807 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002808
Mathias Agopian74c40c02010-09-29 13:02:36 -07002809 return result;
2810}
2811
Mathias Agopianfee2b462013-07-03 12:34:01 -07002812void SurfaceFlinger::checkScreenshot(const sp<GraphicBuffer>& buf, void const* vaddr,
2813 const sp<const DisplayDevice>& hw,
2814 uint32_t minLayerZ, uint32_t maxLayerZ) {
2815 if (DEBUG_SCREENSHOTS) {
2816 for (ssize_t y=0 ; y<buf->height ; y++) {
2817 uint32_t const * p = (uint32_t const *)vaddr + y*buf->stride;
2818 for (ssize_t x=0 ; x<buf->width ; x++) {
2819 if (p[x] != 0xFF000000) return;
2820 }
2821 }
2822 ALOGE("*** we just took a black screenshot ***\n"
2823 "requested minz=%d, maxz=%d, layerStack=%d",
2824 minLayerZ, maxLayerZ, hw->getLayerStack());
2825 const LayerVector& layers( mDrawingState.layersSortedByZ );
2826 const size_t count = layers.size();
2827 for (size_t i=0 ; i<count ; ++i) {
2828 const sp<Layer>& layer(layers[i]);
2829 const Layer::State& state(layer->getDrawingState());
2830 const bool visible = (state.layerStack == hw->getLayerStack())
2831 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2832 && (layer->isVisible());
2833 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
2834 visible ? '+' : '-',
2835 i, layer->getName().string(), state.layerStack, state.z,
2836 layer->isVisible(), state.flags, state.alpha);
2837 }
2838 }
2839}
2840
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002841// ---------------------------------------------------------------------------
2842
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002843SurfaceFlinger::LayerVector::LayerVector() {
2844}
2845
2846SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002847 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002848}
2849
2850int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2851 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002852{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002853 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002854 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2855 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002856
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002857 uint32_t ls = l->getCurrentState().layerStack;
2858 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002859 if (ls != rs)
2860 return ls - rs;
2861
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002862 uint32_t lz = l->getCurrentState().z;
2863 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002864 if (lz != rz)
2865 return lz - rz;
2866
2867 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002868}
2869
2870// ---------------------------------------------------------------------------
2871
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002872SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2873 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002874}
2875
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002876SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002877 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002878 viewport.makeInvalid();
2879 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002880}
2881
2882// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002883
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002884}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002885
2886
2887#if defined(__gl_h_)
2888#error "don't include gl/gl.h in this file"
2889#endif
2890
2891#if defined(__gl2_h_)
2892#error "don't include gl2/gl2.h in this file"
2893#endif