blob: 4a84e5a530cba69c22415a85b24a66b324c7b7d8 [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>
26#include <GLES/gl.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070027#include <GLES/glext.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
29#include <cutils/log.h>
30#include <cutils/properties.h>
31
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070035#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036
Mathias Agopianc666cae2012-07-25 18:56:13 -070037#include <ui/DisplayInfo.h>
38
Mathias Agopian921e6ac2012-07-23 23:11:29 -070039#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070040#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070041#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070042#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080043#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080044#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070045
46#include <ui/GraphicBufferAllocator.h>
47#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070048#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080049
Mathias Agopiancde87a32012-09-13 14:09:01 -070050#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051#include <utils/String8.h>
52#include <utils/String16.h>
53#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080054#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055
Mathias Agopian921e6ac2012-07-23 23:11:29 -070056#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070057#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058
Mathias Agopian3e25fd82013-04-22 17:52:16 +020059#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020061#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080062#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070063#include "DisplayDevice.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080064#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
Mathias Agopiana4912602012-07-12 14:25:33 -070069#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070070#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070071#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072
Mathias Agopian875d8e12013-06-07 15:35:48 -070073#include "RenderEngine/RenderEngine.h"
74
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075#define DISPLAY_COUNT 1
76
Mathias Agopianfee2b462013-07-03 12:34:01 -070077/*
78 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
79 * black pixels.
80 */
81#define DEBUG_SCREENSHOTS false
82
Mathias Agopianca088332013-03-28 17:44:13 -070083EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
84
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086// ---------------------------------------------------------------------------
87
Mathias Agopian99b49842011-06-27 16:05:52 -070088const String16 sHardwareTest("android.permission.HARDWARE_TEST");
89const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
90const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
91const String16 sDump("android.permission.DUMP");
92
93// ---------------------------------------------------------------------------
94
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095SurfaceFlinger::SurfaceFlinger()
96 : BnSurfaceComposer(), Thread(false),
97 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -070098 mTransactionPending(false),
99 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700100 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700101 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700102 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700105 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800106 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700108 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700109 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700110 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700111 mDebugInSwapBuffers(0),
112 mLastSwapBufferTime(0),
113 mDebugInTransaction(0),
114 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700115 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116{
Steve Blocka19954a2012-01-04 20:05:49 +0000117 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118
119 // debugging stuff...
120 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700121
Mathias Agopianb4b17302013-03-20 18:36:41 -0700122 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700123 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700124
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125 property_get("debug.sf.showupdates", value, "0");
126 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700127
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700128 property_get("debug.sf.ddms", value, "0");
129 mDebugDDMS = atoi(value);
130 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700131 if (!startDdmConnection()) {
132 // start failed, and DDMS debugging not enabled
133 mDebugDDMS = 0;
134 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700135 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700136 ALOGI_IF(mDebugRegion, "showupdates enabled");
137 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138}
139
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800140void SurfaceFlinger::onFirstRef()
141{
142 mEventQueue.init(this);
143
144 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
145
146 // Wait for the main thread to be done with its initialization
147 mReadyToRunBarrier.wait();
148}
149
150
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151SurfaceFlinger::~SurfaceFlinger()
152{
Mathias Agopiana4912602012-07-12 14:25:33 -0700153 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
154 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
155 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156}
157
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800158void SurfaceFlinger::binderDied(const wp<IBinder>& who)
159{
160 // the window manager died on us. prepare its eulogy.
161
Andy McFadden13a082e2012-08-24 10:16:42 -0700162 // restore initial conditions (default device unblank, etc)
163 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800164
165 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700166 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800167}
168
Mathias Agopian7e27f052010-05-28 14:22:23 -0700169sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170{
Mathias Agopian96f08192010-06-02 23:28:45 -0700171 sp<ISurfaceComposerClient> bclient;
172 sp<Client> client(new Client(this));
173 status_t err = client->initCheck();
174 if (err == NO_ERROR) {
175 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 return bclient;
178}
179
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700180sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
181 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700182{
183 class DisplayToken : public BBinder {
184 sp<SurfaceFlinger> flinger;
185 virtual ~DisplayToken() {
186 // no more references, this display must be terminated
187 Mutex::Autolock _l(flinger->mStateLock);
188 flinger->mCurrentState.displays.removeItem(this);
189 flinger->setTransactionFlags(eDisplayTransactionNeeded);
190 }
191 public:
192 DisplayToken(const sp<SurfaceFlinger>& flinger)
193 : flinger(flinger) {
194 }
195 };
196
197 sp<BBinder> token = new DisplayToken(this);
198
199 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700200 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700201 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700202 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700203 mCurrentState.displays.add(token, info);
204
205 return token;
206}
207
Jesse Hall6c913be2013-08-08 12:15:49 -0700208void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
209 Mutex::Autolock _l(mStateLock);
210
211 ssize_t idx = mCurrentState.displays.indexOfKey(display);
212 if (idx < 0) {
213 ALOGW("destroyDisplay: invalid display token");
214 return;
215 }
216
217 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
218 if (!info.isVirtualDisplay()) {
219 ALOGE("destroyDisplay called for non-virtual display");
220 return;
221 }
222
223 mCurrentState.displays.removeItemsAt(idx);
224 setTransactionFlags(eDisplayTransactionNeeded);
225}
226
Jesse Hall692c7232012-11-08 15:41:56 -0800227void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
228 ALOGW_IF(mBuiltinDisplays[type],
229 "Overwriting display token for display type %d", type);
230 mBuiltinDisplays[type] = new BBinder();
231 DisplayDeviceState info(type);
232 // All non-virtual displays are currently considered secure.
233 info.isSecure = true;
234 mCurrentState.displays.add(mBuiltinDisplays[type], info);
235}
236
Mathias Agopiane57f2922012-08-09 16:29:12 -0700237sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700238 if (uint32_t(id) >= DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700239 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
240 return NULL;
241 }
Jesse Hall692c7232012-11-08 15:41:56 -0800242 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700243}
244
Jamie Gennis9a78c902011-01-12 18:30:40 -0800245sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
246{
247 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
248 return gba;
249}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700250
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800251void SurfaceFlinger::bootFinished()
252{
253 const nsecs_t now = systemTime();
254 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000255 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700256 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700257
258 // wait patiently for the window manager death
259 const String16 name("window");
260 sp<IBinder> window(defaultServiceManager()->getService(name));
261 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700262 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700263 }
264
265 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700266 // formerly we would just kill the process, but we now ask it to exit so it
267 // can choose where to stop the animation.
268 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800269}
270
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700271void SurfaceFlinger::deleteTextureAsync(GLuint texture) {
272 class MessageDestroyGLTexture : public MessageBase {
273 GLuint texture;
274 public:
275 MessageDestroyGLTexture(GLuint texture)
276 : texture(texture) {
277 }
278 virtual bool handler() {
279 glDeleteTextures(1, &texture);
280 return true;
281 }
282 };
283 postMessageAsync(new MessageDestroyGLTexture(texture));
284}
285
Mathias Agopian722b98f2012-09-25 18:24:31 -0700286status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700287 EGLDisplay dpy,
288 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700289 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700290 EGLConfig* outConfig)
291{
292 EGLConfig config = NULL;
293 EGLint numConfigs = -1, n=0;
294 eglGetConfigs(dpy, NULL, 0, &numConfigs);
295 EGLConfig* const configs = new EGLConfig[numConfigs];
296 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700297
Mathias Agopian722b98f2012-09-25 18:24:31 -0700298 if (n) {
299 if (attribute != EGL_NONE) {
300 for (int i=0 ; i<n ; i++) {
301 EGLint value = 0;
302 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
303 if (wanted == value) {
304 *outConfig = configs[i];
305 delete [] configs;
306 return NO_ERROR;
307 }
308 }
309 } else {
310 // just pick the first one
311 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700312 delete [] configs;
313 return NO_ERROR;
314 }
315 }
316 delete [] configs;
317 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800318}
319
Mathias Agopian722b98f2012-09-25 18:24:31 -0700320class EGLAttributeVector {
321 struct Attribute;
322 class Adder;
323 friend class Adder;
324 KeyedVector<Attribute, EGLint> mList;
325 struct Attribute {
326 Attribute() {};
327 Attribute(EGLint v) : v(v) { }
328 EGLint v;
329 bool operator < (const Attribute& other) const {
330 // this places EGL_NONE at the end
331 EGLint lhs(v);
332 EGLint rhs(other.v);
333 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
334 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
335 return lhs < rhs;
336 }
337 };
338 class Adder {
339 friend class EGLAttributeVector;
340 EGLAttributeVector& v;
341 EGLint attribute;
342 Adder(EGLAttributeVector& v, EGLint attribute)
343 : v(v), attribute(attribute) {
344 }
345 public:
346 void operator = (EGLint value) {
347 if (attribute != EGL_NONE) {
348 v.mList.add(attribute, value);
349 }
350 }
351 operator EGLint () const { return v.mList[attribute]; }
352 };
353public:
354 EGLAttributeVector() {
355 mList.add(EGL_NONE, EGL_NONE);
356 }
357 void remove(EGLint attribute) {
358 if (attribute != EGL_NONE) {
359 mList.removeItem(attribute);
360 }
361 }
362 Adder operator [] (EGLint attribute) {
363 return Adder(*this, attribute);
364 }
365 EGLint operator [] (EGLint attribute) const {
366 return mList[attribute];
367 }
368 // cast-operator to (EGLint const*)
369 operator EGLint const* () const { return &mList.keyAt(0).v; }
370};
371
Mathias Agopiana4912602012-07-12 14:25:33 -0700372EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
373 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
374 // it is to be used with WIFI displays
375 EGLConfig config;
376 EGLint dummy;
377 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700378
Mathias Agopian722b98f2012-09-25 18:24:31 -0700379 EGLAttributeVector attribs;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700380 // TODO: enable ES2
381 //attribs[EGL_RENDERABLE_TYPE] = EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT;
382 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
Mathias Agopian722b98f2012-09-25 18:24:31 -0700383 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
384 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
385 attribs[EGL_RED_SIZE] = 8;
386 attribs[EGL_GREEN_SIZE] = 8;
387 attribs[EGL_BLUE_SIZE] = 8;
388
389 err = selectConfigForAttribute(display, attribs, EGL_NONE, EGL_NONE, &config);
390 if (!err)
391 goto success;
392
Mathias Agopian875d8e12013-06-07 15:35:48 -0700393 // this didn't work, probably because we're on the emulator...
394 // try a simplified query
395 ALOGW("no suitable EGLConfig found, trying a simpler query");
396 attribs.remove(EGL_RENDERABLE_TYPE);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700397 attribs.remove(EGL_FRAMEBUFFER_TARGET_ANDROID);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700398 attribs.remove(EGL_RECORDABLE_ANDROID);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700399 attribs.remove(EGL_RED_SIZE);
400 attribs.remove(EGL_GREEN_SIZE);
401 attribs.remove(EGL_BLUE_SIZE);
402 err = selectConfigForAttribute(display, attribs,
403 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700404 if (!err)
405 goto success;
406
407 // this EGL is too lame for Android
Mathias Agopian875d8e12013-06-07 15:35:48 -0700408 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
Jesse Hallf21cffa2012-09-19 21:00:49 -0700409 return 0;
410
411success:
412 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700413 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700414 return config;
415}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800416
Mathias Agopiana4912602012-07-12 14:25:33 -0700417
Mathias Agopiana4912602012-07-12 14:25:33 -0700418status_t SurfaceFlinger::readyToRun()
419{
420 ALOGI( "SurfaceFlinger's main thread ready to run. "
421 "Initializing graphics H/W...");
422
Jesse Hall692c7232012-11-08 15:41:56 -0800423 Mutex::Autolock _l(mStateLock);
424
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700425 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700426 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
427 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700428
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700429 // Initialize the H/W composer object. There may or may not be an
430 // actual hardware composer underneath.
431 mHwc = new HWComposer(this,
432 *static_cast<HWComposer::EventHandler *>(this));
433
Mathias Agopian875d8e12013-06-07 15:35:48 -0700434 // initialize the config and context (can't fail)
435 mEGLConfig = selectEGLConfig(mEGLDisplay, mHwc->getVisualID());
436
437 // print some debugging info
438 EGLint r,g,b,a;
439 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_RED_SIZE, &r);
440 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_GREEN_SIZE, &g);
441 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_BLUE_SIZE, &b);
442 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_ALPHA_SIZE, &a);
443 ALOGI("EGL informations:");
444 ALOGI("vendor : %s", eglQueryString(mEGLDisplay, EGL_VENDOR));
445 ALOGI("version : %s", eglQueryString(mEGLDisplay, EGL_VERSION));
446 ALOGI("extensions: %s", eglQueryString(mEGLDisplay, EGL_EXTENSIONS));
447 ALOGI("Client API: %s", eglQueryString(mEGLDisplay, EGL_CLIENT_APIS)?:"Not Supported");
448 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
449
450 // get a RenderEngine for the given display / config (can't fail)
451 mRenderEngine = RenderEngine::create(mEGLDisplay, mEGLConfig);
452
453 // retrieve the EGL context that was selected/created
454 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700455
Mathias Agopianed985572013-03-22 00:24:39 -0700456 // figure out which format we got
457 eglGetConfigAttrib(mEGLDisplay, mEGLConfig,
458 EGL_NATIVE_VISUAL_ID, &mEGLNativeVisualId);
459
Mathias Agopianda27af92012-09-13 18:17:13 -0700460 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
461 "couldn't create EGLContext");
462
Mathias Agopiancde87a32012-09-13 14:09:01 -0700463 // initialize our non-virtual displays
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700464 for (size_t i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700465 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700466 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700467 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700468 // All non-virtual displays are currently considered secure.
469 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800470 createBuiltinDisplayLocked(type);
471 wp<IBinder> token = mBuiltinDisplays[i];
472
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700473 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
474 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i, bq);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700475 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700476 type, allocateHwcDisplayId(type), isSecure, token,
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700477 fbs, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700478 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700479 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700480 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700481 // for displays other than the main display, so we always
482 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700483 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700484 hw->acquireScreen();
485 }
486 mDisplays.add(token, hw);
487 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700488 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700489
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700490 // make the GLContext current so that we can create textures when creating Layers
491 // (which may happens before we render something)
492 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
493
Mathias Agopian028508c2012-07-25 21:12:12 -0700494 // start the EventThread
495 mEventThread = new EventThread(this);
496 mEventQueue.setEventThread(mEventThread);
497
Mathias Agopian92a979a2012-08-02 18:32:23 -0700498 // initialize our drawing state
499 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700500
Mathias Agopiana4912602012-07-12 14:25:33 -0700501 // We're now ready to accept clients...
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800502 mReadyToRunBarrier.open();
503
Andy McFadden13a082e2012-08-24 10:16:42 -0700504 // set initial conditions (e.g. unblank default device)
505 initializeDisplays();
506
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700507 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700508 startBootAnim();
Andreas Huber8b42e8a2010-08-16 08:49:37 -0700509
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800510 return NO_ERROR;
511}
512
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700513int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
514 return (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) ?
515 type : mHwc->allocateDisplayId();
516}
517
Mathias Agopiana67e4182012-06-19 17:26:12 -0700518void SurfaceFlinger::startBootAnim() {
519 // start boot animation
520 property_set("service.bootanim.exit", "0");
521 property_set("ctl.start", "bootanim");
522}
523
Mathias Agopian875d8e12013-06-07 15:35:48 -0700524size_t SurfaceFlinger::getMaxTextureSize() const {
525 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700526}
527
Mathias Agopian875d8e12013-06-07 15:35:48 -0700528size_t SurfaceFlinger::getMaxViewportDims() const {
529 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700530}
531
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800532// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800533
Jamie Gennis582270d2011-08-17 18:19:00 -0700534bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800535 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800536 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800537 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700538 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800539}
540
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700541status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800542 int32_t type = NAME_NOT_FOUND;
Mathias Agopian1604f772012-09-18 21:54:42 -0700543 for (int i=0 ; i<DisplayDevice::NUM_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800544 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700545 type = i;
546 break;
547 }
548 }
549
550 if (type < 0) {
551 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700552 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700553
554 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700555 float xdpi = hwc.getDpiX(type);
556 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700557
558 // TODO: Not sure if display density should handled by SF any longer
559 class Density {
560 static int getDensityFromProperty(char const* propName) {
561 char property[PROPERTY_VALUE_MAX];
562 int density = 0;
563 if (property_get(propName, property, NULL) > 0) {
564 density = atoi(property);
565 }
566 return density;
567 }
568 public:
569 static int getEmuDensity() {
570 return getDensityFromProperty("qemu.sf.lcd_density"); }
571 static int getBuildDensity() {
572 return getDensityFromProperty("ro.sf.lcd_density"); }
573 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700574
575 if (type == DisplayDevice::DISPLAY_PRIMARY) {
576 // The density of the device is provided by a build property
577 float density = Density::getBuildDensity() / 160.0f;
578 if (density == 0) {
579 // the build doesn't provide a density -- this is wrong!
580 // use xdpi instead
581 ALOGE("ro.sf.lcd_density must be defined as a build property");
582 density = xdpi / 160.0f;
583 }
584 if (Density::getEmuDensity()) {
585 // if "qemu.sf.lcd_density" is specified, it overrides everything
586 xdpi = ydpi = density = Density::getEmuDensity();
587 density /= 160.0f;
588 }
589 info->density = density;
590
591 // TODO: this needs to go away (currently needed only by webkit)
592 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
593 info->orientation = hw->getOrientation();
Mathias Agopian1604f772012-09-18 21:54:42 -0700594 } else {
595 // TODO: where should this value come from?
596 static const int TV_DENSITY = 213;
597 info->density = TV_DENSITY / 160.0f;
598 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700599 }
600
Mathias Agopian1604f772012-09-18 21:54:42 -0700601 info->w = hwc.getWidth(type);
602 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700603 info->xdpi = xdpi;
604 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700605 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700606
607 // All non-virtual displays are currently considered secure.
608 info->secure = true;
609
Mathias Agopian888c8222012-08-04 21:10:38 -0700610 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700611}
612
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800613// ----------------------------------------------------------------------------
614
615sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800616 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700617}
618
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800619// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800620
621void SurfaceFlinger::waitForEvent() {
622 mEventQueue.waitMessage();
623}
624
625void SurfaceFlinger::signalTransaction() {
626 mEventQueue.invalidate();
627}
628
629void SurfaceFlinger::signalLayerUpdate() {
630 mEventQueue.invalidate();
631}
632
633void SurfaceFlinger::signalRefresh() {
634 mEventQueue.refresh();
635}
636
637status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
638 nsecs_t reltime, uint32_t flags) {
639 return mEventQueue.postMessage(msg, reltime);
640}
641
642status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
643 nsecs_t reltime, uint32_t flags) {
644 status_t res = mEventQueue.postMessage(msg, reltime);
645 if (res == NO_ERROR) {
646 msg->wait();
647 }
648 return res;
649}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650
Mathias Agopian4fec8732012-06-29 14:12:52 -0700651bool SurfaceFlinger::threadLoop() {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800652 waitForEvent();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800653 return true;
654}
655
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700656void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700657 if (mEventThread == NULL) {
658 // This is a temporary workaround for b/7145521. A non-null pointer
659 // does not mean EventThread has finished initializing, so this
660 // is not a correct fix.
661 ALOGW("WARNING: EventThread not started, ignoring vsync");
662 return;
663 }
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700664 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
665 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700666 mEventThread->onVSyncReceived(type, timestamp);
667 }
668}
669
670void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
671 if (mEventThread == NULL) {
672 // This is a temporary workaround for b/7145521. A non-null pointer
673 // does not mean EventThread has finished initializing, so this
674 // is not a correct fix.
675 ALOGW("WARNING: EventThread not started, ignoring hotplug");
676 return;
677 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700678
Mathias Agopian148994e2012-09-19 17:31:36 -0700679 if (uint32_t(type) < DisplayDevice::NUM_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700680 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800681 if (connected) {
682 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700683 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800684 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
685 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700686 }
687 setTransactionFlags(eDisplayTransactionNeeded);
688
Andy McFadden9e9689c2012-10-10 18:17:51 -0700689 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700690 }
Mathias Agopian86303202012-07-24 22:46:10 -0700691}
692
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700693void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
694 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700695}
696
Mathias Agopian4fec8732012-06-29 14:12:52 -0700697void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800698 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800699 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700700 case MessageQueue::TRANSACTION:
701 handleMessageTransaction();
702 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700703 case MessageQueue::INVALIDATE:
704 handleMessageTransaction();
705 handleMessageInvalidate();
706 signalRefresh();
707 break;
708 case MessageQueue::REFRESH:
709 handleMessageRefresh();
710 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800711 }
712}
713
Mathias Agopian4fec8732012-06-29 14:12:52 -0700714void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700715 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700716 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700717 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700718 }
719}
720
721void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700722 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700723 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700724}
725
726void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700727 ATRACE_CALL();
728 preComposition();
729 rebuildLayerStacks();
730 setUpHWComposer();
731 doDebugFlashRegions();
732 doComposition();
733 postComposition();
734}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700735
Mathias Agopiancd60f992012-08-16 16:28:27 -0700736void SurfaceFlinger::doDebugFlashRegions()
737{
738 // is debugging enabled
739 if (CC_LIKELY(!mDebugRegion))
740 return;
741
742 const bool repaintEverything = mRepaintEverything;
743 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
744 const sp<DisplayDevice>& hw(mDisplays[dpy]);
745 if (hw->canDraw()) {
746 // transform the dirty region into this screen's coordinate space
747 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
748 if (!dirtyRegion.isEmpty()) {
749 // redraw the whole screen
750 doComposeSurfaces(hw, Region(hw->bounds()));
751
752 // and draw the dirty region
753 glDisable(GL_TEXTURE_EXTERNAL_OES);
754 glDisable(GL_TEXTURE_2D);
755 glDisable(GL_BLEND);
756 glColor4f(1, 0, 1, 1);
757 const int32_t height = hw->getHeight();
758 Region::const_iterator it = dirtyRegion.begin();
759 Region::const_iterator const end = dirtyRegion.end();
760 while (it != end) {
761 const Rect& r = *it++;
762 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -0800763 { (GLfloat) r.left, (GLfloat) (height - r.top) },
764 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
765 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
766 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700767 };
768 glVertexPointer(2, GL_FLOAT, 0, vertices);
769 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
770 }
771 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700772 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700773 }
774 }
775 }
776
777 postFramebuffer();
778
779 if (mDebugRegion > 1) {
780 usleep(mDebugRegion * 1000);
781 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700782
783 HWComposer& hwc(getHwComposer());
784 if (hwc.initCheck() == NO_ERROR) {
785 status_t err = hwc.prepare();
786 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
787 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700788}
789
790void SurfaceFlinger::preComposition()
791{
792 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700793 const LayerVector& layers(mDrawingState.layersSortedByZ);
794 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700795 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700796 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700797 needExtraInvalidate = true;
798 }
799 }
800 if (needExtraInvalidate) {
801 signalLayerUpdate();
802 }
803}
804
805void SurfaceFlinger::postComposition()
806{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700807 const LayerVector& layers(mDrawingState.layersSortedByZ);
808 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700809 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700810 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700811 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800812
813 if (mAnimCompositionPending) {
814 mAnimCompositionPending = false;
815
816 const HWComposer& hwc = getHwComposer();
817 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800818 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800819 mAnimFrameTracker.setActualPresentFence(presentFence);
820 } else {
821 // The HWC doesn't support present fences, so use the refresh
822 // timestamp instead.
823 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
824 mAnimFrameTracker.setActualPresentTime(presentTime);
825 }
826 mAnimFrameTracker.advanceFrame();
827 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700828}
829
830void SurfaceFlinger::rebuildLayerStacks() {
831 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700832 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700833 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700834 mVisibleRegionsDirty = false;
835 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700836
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700837 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700838 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700839 Region opaqueRegion;
840 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800841 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700842 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700843 const Transform& tr(hw->getTransform());
844 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700845 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700846 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700847 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700848
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700849 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700850 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700851 const sp<Layer>& layer(layers[i]);
852 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700853 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700854 Region drawRegion(tr.transform(
855 layer->visibleNonTransparentRegion));
856 drawRegion.andSelf(bounds);
857 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700858 layersSortedByZ.add(layer);
859 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700860 }
861 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700862 }
Mathias Agopian42977342012-08-05 00:40:46 -0700863 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700864 hw->undefinedRegion.set(bounds);
865 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
866 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700867 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700868 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700869}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700870
Mathias Agopiancd60f992012-08-16 16:28:27 -0700871void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700872 HWComposer& hwc(getHwComposer());
873 if (hwc.initCheck() == NO_ERROR) {
874 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700875 if (CC_UNLIKELY(mHwWorkListDirty)) {
876 mHwWorkListDirty = false;
877 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
878 sp<const DisplayDevice> hw(mDisplays[dpy]);
879 const int32_t id = hw->getHwcDisplayId();
880 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800881 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700882 hw->getVisibleLayersSortedByZ());
883 const size_t count = currentLayers.size();
884 if (hwc.createWorkList(id, count) == NO_ERROR) {
885 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) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800888 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700889 layer->setGeometry(hw, *cur);
890 if (mDebugDisableHWC || mDebugRegion) {
891 cur->setSkip(true);
892 }
893 }
894 }
895 }
896 }
897 }
898
899 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700900 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700901 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700902 const int32_t id = hw->getHwcDisplayId();
903 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800904 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700905 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700906 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700907 HWComposer::LayerListIterator cur = hwc.begin(id);
908 const HWComposer::LayerListIterator end = hwc.end(id);
909 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
910 /*
911 * update the per-frame h/w composer data for each layer
912 * and build the transparent region of the FB
913 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800914 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700915 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700916 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700917 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700918 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700919
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700920 status_t err = hwc.prepare();
921 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700922
923 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
924 sp<const DisplayDevice> hw(mDisplays[dpy]);
925 hw->prepareFrame(hwc);
926 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700927 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700928}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700929
Mathias Agopiancd60f992012-08-16 16:28:27 -0700930void SurfaceFlinger::doComposition() {
931 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700932 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700933 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700934 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700935 if (hw->canDraw()) {
936 // transform the dirty region into this screen's coordinate space
937 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800938
939 // repaint the framebuffer (if needed)
940 doDisplayComposition(hw, dirtyRegion);
941
Mathias Agopiancd60f992012-08-16 16:28:27 -0700942 hw->dirtyRegion.clear();
943 hw->flip(hw->swapRegion);
944 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700945 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700946 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700947 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700948 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700949 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700950}
951
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800952void SurfaceFlinger::postFramebuffer()
953{
Mathias Agopian841cde52012-03-01 15:44:37 -0800954 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800955
Mathias Agopiana44b0412011-10-16 18:46:35 -0700956 const nsecs_t now = systemTime();
957 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700958
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700959 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700960 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700961 if (!hwc.supportsFramebufferTarget()) {
962 // EGL spec says:
963 // "surface must be bound to the calling thread's current context,
964 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700965 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700966 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700967 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700968 }
969
Mathias Agopian92a979a2012-08-02 18:32:23 -0700970 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700971 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800972 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700973 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700974 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700975 int32_t id = hw->getHwcDisplayId();
976 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700977 HWComposer::LayerListIterator cur = hwc.begin(id);
978 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700979 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700980 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700981 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700982 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700983 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700984 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700985 }
Jesse Hallef194142012-06-14 14:45:17 -0700986 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800987 }
988
Mathias Agopiana44b0412011-10-16 18:46:35 -0700989 mLastSwapBufferTime = systemTime() - now;
990 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700991
992 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
993 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
994 logFrameStats();
995 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800996}
997
Mathias Agopian87baae12012-07-31 12:38:26 -0700998void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800999{
Mathias Agopian841cde52012-03-01 15:44:37 -08001000 ATRACE_CALL();
1001
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001002 // here we keep a copy of the drawing state (that is the state that's
1003 // going to be overwritten by handleTransactionLocked()) outside of
1004 // mStateLock so that the side-effects of the State assignment
1005 // don't happen with mStateLock held (which can cause deadlocks).
1006 State drawingState(mDrawingState);
1007
Mathias Agopianca4d3602011-05-19 15:38:14 -07001008 Mutex::Autolock _l(mStateLock);
1009 const nsecs_t now = systemTime();
1010 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011
Mathias Agopianca4d3602011-05-19 15:38:14 -07001012 // Here we're guaranteed that some transaction flags are set
1013 // so we can call handleTransactionLocked() unconditionally.
1014 // We call getTransactionFlags(), which will also clear the flags,
1015 // with mStateLock held to guarantee that mCurrentState won't change
1016 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001017
Mathias Agopiane57f2922012-08-09 16:29:12 -07001018 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001019 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001020
Mathias Agopianca4d3602011-05-19 15:38:14 -07001021 mLastTransactionTime = systemTime() - now;
1022 mDebugInTransaction = 0;
1023 invalidateHwcGeometry();
1024 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001025}
1026
Mathias Agopian87baae12012-07-31 12:38:26 -07001027void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001028{
1029 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001030 const size_t count = currentLayers.size();
1031
1032 /*
1033 * Traversal of the children
1034 * (perform the transaction for each of them if needed)
1035 */
1036
Mathias Agopian3559b072012-08-15 13:46:03 -07001037 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001039 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1041 if (!trFlags) continue;
1042
1043 const uint32_t flags = layer->doTransaction(0);
1044 if (flags & Layer::eVisibleRegion)
1045 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001046 }
1047 }
1048
1049 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001050 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001051 */
1052
Mathias Agopiane57f2922012-08-09 16:29:12 -07001053 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001054 // here we take advantage of Vector's copy-on-write semantics to
1055 // improve performance by skipping the transaction entirely when
1056 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001057 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1058 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001059 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001060 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001061 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001062 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001063
1064 // find the displays that were removed
1065 // (ie: in drawing state but not in current state)
1066 // also handle displays that changed
1067 // (ie: displays that are in both lists)
1068 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001069 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1070 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001071 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001072 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001073 // Call makeCurrent() on the primary display so we can
1074 // be sure that nothing associated with this display
1075 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001076 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001077 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001078 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1079 if (hw != NULL)
1080 hw->disconnect(getHwComposer());
Jesse Hall7adb0f82013-03-06 16:13:49 -08001081 if (draw[i].type < DisplayDevice::NUM_DISPLAY_TYPES)
1082 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001083 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001084 } else {
1085 ALOGW("trying to remove the main display");
1086 }
1087 } else {
1088 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001089 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001090 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001091 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001092 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001093 // recreating the DisplayDevice, so we just remove it
1094 // from the drawing state, so that it get re-added
1095 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001096 sp<DisplayDevice> hw(getDisplayDevice(display));
1097 if (hw != NULL)
1098 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001099 mDisplays.removeItem(display);
1100 mDrawingState.displays.removeItemsAt(i);
1101 dc--; i--;
1102 // at this point we must loop to the next item
1103 continue;
1104 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001105
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001106 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001107 if (disp != NULL) {
1108 if (state.layerStack != draw[i].layerStack) {
1109 disp->setLayerStack(state.layerStack);
1110 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001111 if ((state.orientation != draw[i].orientation)
1112 || (state.viewport != draw[i].viewport)
1113 || (state.frame != draw[i].frame))
1114 {
1115 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001116 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001117 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001118 }
1119 }
1120 }
1121
1122 // find displays that were added
1123 // (ie: in current state but not in drawing state)
1124 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001125 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001126 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001127
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001128 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001129 sp<IGraphicBufferProducer> producer;
1130 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
1131
Jesse Hall02d86562013-03-25 14:43:23 -07001132 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001133 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001134 // Virtual displays without a surface are dormant:
1135 // they have external state (layer stack, projection,
1136 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001137 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001138
Jesse Hall02d86562013-03-25 14:43:23 -07001139 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001140 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1141 *mHwc, hwcDisplayId, state.surface, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001142 state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001143
1144 dispSurface = vds;
1145 if (hwcDisplayId >= 0) {
1146 producer = vds;
1147 } else {
1148 // There won't be any interaction with HWC for this virtual display,
1149 // so the GLES driver can pass buffers directly to the sink.
1150 producer = state.surface;
1151 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001152 }
1153 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001154 ALOGE_IF(state.surface!=NULL,
1155 "adding a supported display, but rendering "
1156 "surface is provided (%p), ignoring it",
1157 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001158 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001159 // for supported (by hwc) displays we provide our
1160 // own rendering surface
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001161 dispSurface = new FramebufferSurface(*mHwc, state.type, bq);
1162 producer = bq;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001163 }
1164
1165 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001166 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001167 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001168 state.type, hwcDisplayId, state.isSecure,
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001169 display, dispSurface, producer, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001170 hw->setLayerStack(state.layerStack);
1171 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001172 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001173 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001174 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001175 if (state.isVirtualDisplay()) {
1176 if (hwcDisplayId >= 0) {
1177 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1178 hw->getWidth(), hw->getHeight(),
1179 hw->getFormat());
1180 }
1181 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001182 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001183 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001184 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001185 }
1186 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001187 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001188 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189
Mathias Agopian84300952012-11-21 16:02:13 -08001190 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1191 // The transform hint might have changed for some layers
1192 // (either because a display has changed, or because a layer
1193 // as changed).
1194 //
1195 // Walk through all the layers in currentLayers,
1196 // and update their transform hint.
1197 //
1198 // If a layer is visible only on a single display, then that
1199 // display is used to calculate the hint, otherwise we use the
1200 // default display.
1201 //
1202 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1203 // the hint is set before we acquire a buffer from the surface texture.
1204 //
1205 // NOTE: layer transactions have taken place already, so we use their
1206 // drawing state. However, SurfaceFlinger's own transaction has not
1207 // happened yet, so we must use the current state layer list
1208 // (soon to become the drawing state list).
1209 //
1210 sp<const DisplayDevice> disp;
1211 uint32_t currentlayerStack = 0;
1212 for (size_t i=0; i<count; i++) {
1213 // NOTE: we rely on the fact that layers are sorted by
1214 // layerStack first (so we don't have to traverse the list
1215 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001216 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001217 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001218 if (i==0 || currentlayerStack != layerStack) {
1219 currentlayerStack = layerStack;
1220 // figure out if this layerstack is mirrored
1221 // (more than one display) if so, pick the default display,
1222 // if not, pick the only display it's on.
1223 disp.clear();
1224 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1225 sp<const DisplayDevice> hw(mDisplays[dpy]);
1226 if (hw->getLayerStack() == currentlayerStack) {
1227 if (disp == NULL) {
1228 disp = hw;
1229 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001230 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001231 break;
1232 }
1233 }
1234 }
1235 }
Chet Haase91d25932013-04-11 15:24:55 -07001236 if (disp == NULL) {
1237 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1238 // redraw after transform hint changes. See bug 8508397.
1239
1240 // could be null when this layer is using a layerStack
1241 // that is not visible on any display. Also can occur at
1242 // screen off/on times.
1243 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001244 }
Chet Haase91d25932013-04-11 15:24:55 -07001245 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001246 }
1247 }
1248
1249
Mathias Agopian3559b072012-08-15 13:46:03 -07001250 /*
1251 * Perform our own transaction if needed
1252 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001253
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001254 const LayerVector& layers(mDrawingState.layersSortedByZ);
1255 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001256 // layers have been added
1257 mVisibleRegionsDirty = true;
1258 }
1259
1260 // some layers might have been removed, so
1261 // we need to update the regions they're exposing.
1262 if (mLayersRemoved) {
1263 mLayersRemoved = false;
1264 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001265 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001266 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001267 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001268 if (currentLayers.indexOf(layer) < 0) {
1269 // this layer is not visible anymore
1270 // TODO: we could traverse the tree from front to back and
1271 // compute the actual visible region
1272 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001273 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001274 Region visibleReg = s.transform.transform(
1275 Region(Rect(s.active.w, s.active.h)));
1276 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001277 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001278 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001279 }
1280
1281 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001282}
1283
1284void SurfaceFlinger::commitTransaction()
1285{
1286 if (!mLayersPendingRemoval.isEmpty()) {
1287 // Notify removed layers now that they can't be drawn from
1288 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1289 mLayersPendingRemoval[i]->onRemoved();
1290 }
1291 mLayersPendingRemoval.clear();
1292 }
1293
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001294 // If this transaction is part of a window animation then the next frame
1295 // we composite should be considered an animation as well.
1296 mAnimCompositionPending = mAnimTransactionPending;
1297
Mathias Agopian4fec8732012-06-29 14:12:52 -07001298 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001299 mTransactionPending = false;
1300 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001301 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001302}
1303
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001305 const LayerVector& currentLayers, uint32_t layerStack,
1306 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001307{
Mathias Agopian841cde52012-03-01 15:44:37 -08001308 ATRACE_CALL();
1309
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001310 Region aboveOpaqueLayers;
1311 Region aboveCoveredLayers;
1312 Region dirty;
1313
Mathias Agopian87baae12012-07-31 12:38:26 -07001314 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001315
1316 size_t i = currentLayers.size();
1317 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001318 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001319
1320 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001321 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001322
Jesse Hall01e29052013-02-19 16:13:35 -08001323 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001324 if (s.layerStack != layerStack)
1325 continue;
1326
Mathias Agopianab028732010-03-16 16:41:46 -07001327 /*
1328 * opaqueRegion: area of a surface that is fully opaque.
1329 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001330 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001331
1332 /*
1333 * visibleRegion: area of a surface that is visible on screen
1334 * and not fully transparent. This is essentially the layer's
1335 * footprint minus the opaque regions above it.
1336 * Areas covered by a translucent surface are considered visible.
1337 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001338 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001339
1340 /*
1341 * coveredRegion: area of a surface that is covered by all
1342 * visible regions above it (which includes the translucent areas).
1343 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001344 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001345
Jesse Halla8026d22012-09-25 13:25:04 -07001346 /*
1347 * transparentRegion: area of a surface that is hinted to be completely
1348 * transparent. This is only used to tell when the layer has no visible
1349 * non-transparent regions and can be removed from the layer list. It
1350 * does not affect the visibleRegion of this layer or any layers
1351 * beneath it. The hint may not be correct if apps don't respect the
1352 * SurfaceView restrictions (which, sadly, some don't).
1353 */
1354 Region transparentRegion;
1355
Mathias Agopianab028732010-03-16 16:41:46 -07001356
1357 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001358 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001359 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001360 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001361 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001362 if (!visibleRegion.isEmpty()) {
1363 // Remove the transparent area from the visible region
1364 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001365 const Transform tr(s.transform);
1366 if (tr.transformed()) {
1367 if (tr.preserveRects()) {
1368 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001369 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001370 } else {
1371 // transformation too complex, can't do the
1372 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001373 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001374 }
1375 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001376 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001377 }
Mathias Agopianab028732010-03-16 16:41:46 -07001378 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001379
Mathias Agopianab028732010-03-16 16:41:46 -07001380 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001381 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001382 if (s.alpha==255 && !translucent &&
1383 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1384 // the opaque region is the layer's footprint
1385 opaqueRegion = visibleRegion;
1386 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001387 }
1388 }
1389
Mathias Agopianab028732010-03-16 16:41:46 -07001390 // Clip the covered region to the visible region
1391 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1392
1393 // Update aboveCoveredLayers for next (lower) layer
1394 aboveCoveredLayers.orSelf(visibleRegion);
1395
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001396 // subtract the opaque region covered by the layers above us
1397 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398
1399 // compute this layer's dirty region
1400 if (layer->contentDirty) {
1401 // we need to invalidate the whole region
1402 dirty = visibleRegion;
1403 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001404 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405 layer->contentDirty = false;
1406 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001407 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001408 * the exposed region consists of two components:
1409 * 1) what's VISIBLE now and was COVERED before
1410 * 2) what's EXPOSED now less what was EXPOSED before
1411 *
1412 * note that (1) is conservative, we start with the whole
1413 * visible region but only keep what used to be covered by
1414 * something -- which mean it may have been exposed.
1415 *
1416 * (2) handles areas that were not covered by anything but got
1417 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001418 */
Mathias Agopianab028732010-03-16 16:41:46 -07001419 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001420 const Region oldVisibleRegion = layer->visibleRegion;
1421 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001422 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1423 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424 }
1425 dirty.subtractSelf(aboveOpaqueLayers);
1426
1427 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001428 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001429
Mathias Agopianab028732010-03-16 16:41:46 -07001430 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001432
Jesse Halla8026d22012-09-25 13:25:04 -07001433 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001434 layer->setVisibleRegion(visibleRegion);
1435 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001436 layer->setVisibleNonTransparentRegion(
1437 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001438 }
1439
Mathias Agopian87baae12012-07-31 12:38:26 -07001440 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001441}
1442
Mathias Agopian87baae12012-07-31 12:38:26 -07001443void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1444 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001445 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001446 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1447 if (hw->getLayerStack() == layerStack) {
1448 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001449 }
1450 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001451}
1452
1453void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001454{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001455 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001456
Mathias Agopian4fec8732012-06-29 14:12:52 -07001457 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001458 const LayerVector& layers(mDrawingState.layersSortedByZ);
1459 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001460 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001461 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001462 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001463 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001464 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001465 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001466
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001467 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001468}
1469
Mathias Agopianad456f92011-01-13 17:53:01 -08001470void SurfaceFlinger::invalidateHwcGeometry()
1471{
1472 mHwWorkListDirty = true;
1473}
1474
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001475
Mathias Agopiancd60f992012-08-16 16:28:27 -07001476void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001477 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001478{
Mathias Agopian87baae12012-07-31 12:38:26 -07001479 Region dirtyRegion(inDirtyRegion);
1480
Mathias Agopianb8a55602009-06-26 19:06:36 -07001481 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001482 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483
Mathias Agopian42977342012-08-05 00:40:46 -07001484 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001485 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001486 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1487 // takes a rectangle, we must make sure to update that whole
1488 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001489 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001490 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001491 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001492 // We need to redraw the rectangle that will be updated
1493 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001494 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001495 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001496 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001497 } else {
1498 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001499 dirtyRegion.set(hw->bounds());
1500 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001501 }
1502 }
1503
Mathias Agopiancd60f992012-08-16 16:28:27 -07001504 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001505
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001506 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001507 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001508
1509 // swap buffers (presentation)
1510 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001511}
1512
Mathias Agopiancd60f992012-08-16 16:28:27 -07001513void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001514{
Mathias Agopian85d751c2012-08-29 16:59:24 -07001515 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001516 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001517 HWComposer::LayerListIterator cur = hwc.begin(id);
1518 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001519
Mathias Agopian85d751c2012-08-29 16:59:24 -07001520 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1521 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001522 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001523 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1524 hw->getDisplayName().string());
1525 return;
1526 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001527
1528 // set the frame buffer
1529 glMatrixMode(GL_MODELVIEW);
1530 glLoadIdentity();
1531
1532 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001533 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001534 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001535 // when using overlays, we assume a fully transparent framebuffer
1536 // NOTE: we could reduce how much we need to clear, for instance
1537 // remove where there are opaque FB layers. however, on some
1538 // GPUs doing a "clean slate" glClear might be more efficient.
1539 // We'll revisit later if needed.
1540 glClearColor(0, 0, 0, 0);
1541 glClear(GL_COLOR_BUFFER_BIT);
1542 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001543 // we start with the whole screen area
1544 const Region bounds(hw->getBounds());
1545
1546 // we remove the scissor part
1547 // we're left with the letterbox region
1548 // (common case is that letterbox ends-up being empty)
1549 const Region letterbox(bounds.subtract(hw->getScissor()));
1550
1551 // compute the area to clear
1552 Region region(hw->undefinedRegion.merge(letterbox));
1553
1554 // but limit it to the dirty region
1555 region.andSelf(dirty);
1556
Mathias Agopianb9494d52012-04-18 02:28:45 -07001557 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001558 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001559 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001560 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001561 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001562 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001563
Mathias Agopian766dc492012-10-30 18:08:06 -07001564 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1565 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001566 // scissor on the main display. It should never be needed
1567 // anyways (though in theory it could since the API allows it).
1568 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001569 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001570 if (scissor != bounds) {
1571 // scissor doesn't match the screen's dimensions, so we
1572 // need to clear everything outside of it and enable
1573 // the GL scissor so we don't draw anything where we shouldn't
1574 const GLint height = hw->getHeight();
1575 glScissor(scissor.left, height - scissor.bottom,
1576 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001577 // enable scissor for this frame
1578 glEnable(GL_SCISSOR_TEST);
1579 }
1580 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001581 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001582
Mathias Agopian85d751c2012-08-29 16:59:24 -07001583 /*
1584 * and then, render the layers targeted at the framebuffer
1585 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001586
Mathias Agopian13127d82013-03-05 17:47:11 -08001587 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001588 const size_t count = layers.size();
1589 const Transform& tr = hw->getTransform();
1590 if (cur != end) {
1591 // we're using h/w composer
1592 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001593 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001594 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001595 if (!clip.isEmpty()) {
1596 switch (cur->getCompositionType()) {
1597 case HWC_OVERLAY: {
1598 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1599 && i
1600 && layer->isOpaque()
1601 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001602 // never clear the very first layer since we're
1603 // guaranteed the FB is already cleared
1604 layer->clearWithOpenGL(hw, clip);
1605 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001606 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001607 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001608 case HWC_FRAMEBUFFER: {
1609 layer->draw(hw, clip);
1610 break;
1611 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001612 case HWC_FRAMEBUFFER_TARGET: {
1613 // this should not happen as the iterator shouldn't
1614 // let us get there.
1615 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1616 break;
1617 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001618 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001619 }
1620 layer->setAcquireFence(hw, *cur);
1621 }
1622 } else {
1623 // we're not using h/w composer
1624 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001625 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001626 const Region clip(dirty.intersect(
1627 tr.transform(layer->visibleRegion)));
1628 if (!clip.isEmpty()) {
1629 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001630 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001631 }
1632 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001633
1634 // disable scissor at the end of the frame
1635 glDisable(GL_SCISSOR_TEST);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001636}
1637
Mathias Agopian55801e42012-08-27 18:54:24 -07001638void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw,
1639 const Region& region) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001640{
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001641 glDisable(GL_TEXTURE_EXTERNAL_OES);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001642 glDisable(GL_TEXTURE_2D);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001643 glDisable(GL_BLEND);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001644 glColor4f(0,0,0,0);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001645
Mathias Agopian55801e42012-08-27 18:54:24 -07001646 const int32_t height = hw->getHeight();
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001647 Region::const_iterator it = region.begin();
1648 Region::const_iterator const end = region.end();
1649 while (it != end) {
1650 const Rect& r = *it++;
Mathias Agopian55801e42012-08-27 18:54:24 -07001651 GLfloat vertices[][2] = {
Andy McFadden8f06a8c2013-01-11 10:24:34 -08001652 { (GLfloat) r.left, (GLfloat) (height - r.top) },
1653 { (GLfloat) r.left, (GLfloat) (height - r.bottom) },
1654 { (GLfloat) r.right, (GLfloat) (height - r.bottom) },
1655 { (GLfloat) r.right, (GLfloat) (height - r.top) }
Mathias Agopian55801e42012-08-27 18:54:24 -07001656 };
1657 glVertexPointer(2, GL_FLOAT, 0, vertices);
Mathias Agopianf74e8e02012-04-16 03:14:05 -07001658 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
1659 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001660}
1661
Mathias Agopianac9fa422013-02-11 16:40:36 -08001662void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1663 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001664 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001665 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001666{
Mathias Agopian96f08192010-06-02 23:28:45 -07001667 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001668 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001669
Mathias Agopian96f08192010-06-02 23:28:45 -07001670 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001671 Mutex::Autolock _l(mStateLock);
1672 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001673 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001674}
1675
Mathias Agopian13127d82013-03-05 17:47:11 -08001676status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001677{
1678 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001679 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001680 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001681 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001682 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001683 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001684 return NO_ERROR;
1685 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001686 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687}
1688
Mathias Agopiandea20b12011-05-03 17:04:02 -07001689uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags)
1690{
1691 return android_atomic_release_load(&mTransactionFlags);
1692}
1693
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001694uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags)
1695{
1696 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1697}
1698
Mathias Agopianbb641242010-05-18 17:06:55 -07001699uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001700{
1701 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1702 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001703 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001704 }
1705 return old;
1706}
1707
Mathias Agopian8b33f032012-07-24 20:43:54 -07001708void SurfaceFlinger::setTransactionState(
1709 const Vector<ComposerState>& state,
1710 const Vector<DisplayState>& displays,
1711 uint32_t flags)
1712{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001713 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001714 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001715 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001716
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001717 if (flags & eAnimation) {
1718 // For window updates that are part of an animation we must wait for
1719 // previous animation "frames" to be handled.
1720 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001721 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001722 if (CC_UNLIKELY(err != NO_ERROR)) {
1723 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001724 // caller after a few seconds.
1725 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1726 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001727 mAnimTransactionPending = false;
1728 break;
1729 }
1730 }
1731 }
1732
Mathias Agopiane57f2922012-08-09 16:29:12 -07001733 size_t count = displays.size();
1734 for (size_t i=0 ; i<count ; i++) {
1735 const DisplayState& s(displays[i]);
1736 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001737 }
1738
Mathias Agopiane57f2922012-08-09 16:29:12 -07001739 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001740 for (size_t i=0 ; i<count ; i++) {
1741 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001742 // Here we need to check that the interface we're given is indeed
1743 // one of our own. A malicious client could give us a NULL
1744 // IInterface, or one of its own or even one of our own but a
1745 // different type. All these situations would cause us to crash.
1746 //
1747 // NOTE: it would be better to use RTTI as we could directly check
1748 // that we have a Client*. however, RTTI is disabled in Android.
1749 if (s.client != NULL) {
1750 sp<IBinder> binder = s.client->asBinder();
1751 if (binder != NULL) {
1752 String16 desc(binder->getInterfaceDescriptor());
1753 if (desc == ISurfaceComposerClient::descriptor) {
1754 sp<Client> client( static_cast<Client *>(s.client.get()) );
1755 transactionFlags |= setClientStateLocked(client, s.state);
1756 }
1757 }
1758 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001759 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001760
Mathias Agopian386aa982011-11-07 21:58:03 -08001761 if (transactionFlags) {
1762 // this triggers the transaction
1763 setTransactionFlags(transactionFlags);
1764
1765 // if this is a synchronous transaction, wait for it to take effect
1766 // before returning.
1767 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001768 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001769 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001770 if (flags & eAnimation) {
1771 mAnimTransactionPending = true;
1772 }
1773 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001774 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1775 if (CC_UNLIKELY(err != NO_ERROR)) {
1776 // just in case something goes wrong in SF, return to the
1777 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001778 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1779 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001780 break;
1781 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001782 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001783 }
1784}
1785
Mathias Agopiane57f2922012-08-09 16:29:12 -07001786uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1787{
Jesse Hall9a143922012-10-04 16:29:19 -07001788 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1789 if (dpyIdx < 0)
1790 return 0;
1791
Mathias Agopiane57f2922012-08-09 16:29:12 -07001792 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001793 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001794 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001795 const uint32_t what = s.what;
1796 if (what & DisplayState::eSurfaceChanged) {
1797 if (disp.surface->asBinder() != s.surface->asBinder()) {
1798 disp.surface = s.surface;
1799 flags |= eDisplayTransactionNeeded;
1800 }
1801 }
1802 if (what & DisplayState::eLayerStackChanged) {
1803 if (disp.layerStack != s.layerStack) {
1804 disp.layerStack = s.layerStack;
1805 flags |= eDisplayTransactionNeeded;
1806 }
1807 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001808 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001809 if (disp.orientation != s.orientation) {
1810 disp.orientation = s.orientation;
1811 flags |= eDisplayTransactionNeeded;
1812 }
1813 if (disp.frame != s.frame) {
1814 disp.frame = s.frame;
1815 flags |= eDisplayTransactionNeeded;
1816 }
1817 if (disp.viewport != s.viewport) {
1818 disp.viewport = s.viewport;
1819 flags |= eDisplayTransactionNeeded;
1820 }
1821 }
1822 }
1823 return flags;
1824}
1825
1826uint32_t SurfaceFlinger::setClientStateLocked(
1827 const sp<Client>& client,
1828 const layer_state_t& s)
1829{
1830 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001831 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001832 if (layer != 0) {
1833 const uint32_t what = s.what;
1834 if (what & layer_state_t::ePositionChanged) {
1835 if (layer->setPosition(s.x, s.y))
1836 flags |= eTraversalNeeded;
1837 }
1838 if (what & layer_state_t::eLayerChanged) {
1839 // NOTE: index needs to be calculated before we update the state
1840 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1841 if (layer->setLayer(s.z)) {
1842 mCurrentState.layersSortedByZ.removeAt(idx);
1843 mCurrentState.layersSortedByZ.add(layer);
1844 // we need traversal (state changed)
1845 // AND transaction (list changed)
1846 flags |= eTransactionNeeded|eTraversalNeeded;
1847 }
1848 }
1849 if (what & layer_state_t::eSizeChanged) {
1850 if (layer->setSize(s.w, s.h)) {
1851 flags |= eTraversalNeeded;
1852 }
1853 }
1854 if (what & layer_state_t::eAlphaChanged) {
1855 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1856 flags |= eTraversalNeeded;
1857 }
1858 if (what & layer_state_t::eMatrixChanged) {
1859 if (layer->setMatrix(s.matrix))
1860 flags |= eTraversalNeeded;
1861 }
1862 if (what & layer_state_t::eTransparentRegionChanged) {
1863 if (layer->setTransparentRegionHint(s.transparentRegion))
1864 flags |= eTraversalNeeded;
1865 }
1866 if (what & layer_state_t::eVisibilityChanged) {
1867 if (layer->setFlags(s.flags, s.mask))
1868 flags |= eTraversalNeeded;
1869 }
1870 if (what & layer_state_t::eCropChanged) {
1871 if (layer->setCrop(s.crop))
1872 flags |= eTraversalNeeded;
1873 }
1874 if (what & layer_state_t::eLayerStackChanged) {
1875 // NOTE: index needs to be calculated before we update the state
1876 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1877 if (layer->setLayerStack(s.layerStack)) {
1878 mCurrentState.layersSortedByZ.removeAt(idx);
1879 mCurrentState.layersSortedByZ.add(layer);
1880 // we need traversal (state changed)
1881 // AND transaction (list changed)
1882 flags |= eTransactionNeeded|eTraversalNeeded;
1883 }
1884 }
1885 }
1886 return flags;
1887}
1888
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001889status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001890 const String8& name,
1891 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001892 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1893 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001894{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001895 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001896 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001897 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001898 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001899 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001900 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001901
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001902 status_t result = NO_ERROR;
1903
1904 sp<Layer> layer;
1905
Mathias Agopian3165cc22012-08-08 19:42:09 -07001906 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1907 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001908 result = createNormalLayer(client,
1909 name, w, h, flags, format,
1910 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001911 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001912 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001913 result = createDimLayer(client,
1914 name, w, h, flags,
1915 handle, gbp, &layer);
1916 break;
1917 default:
1918 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001919 break;
1920 }
1921
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001922 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001923 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001924 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001925 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001926 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001927}
1928
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001929status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1930 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1931 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001932{
1933 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001934 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001935 case PIXEL_FORMAT_TRANSPARENT:
1936 case PIXEL_FORMAT_TRANSLUCENT:
1937 format = PIXEL_FORMAT_RGBA_8888;
1938 break;
1939 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001940#ifdef NO_RGBX_8888
1941 format = PIXEL_FORMAT_RGB_565;
1942#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001943 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001944#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001945 break;
1946 }
1947
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001948#ifdef NO_RGBX_8888
1949 if (format == PIXEL_FORMAT_RGBX_8888)
1950 format = PIXEL_FORMAT_RGBA_8888;
1951#endif
1952
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001953 *outLayer = new Layer(this, client, name, w, h, flags);
1954 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1955 if (err == NO_ERROR) {
1956 *handle = (*outLayer)->getHandle();
1957 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001958 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001959
1960 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1961 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001962}
1963
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001964status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1965 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1966 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001967{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001968 *outLayer = new LayerDim(this, client, name, w, h, flags);
1969 *handle = (*outLayer)->getHandle();
1970 *gbp = (*outLayer)->getBufferQueue();
1971 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001972}
1973
Mathias Agopianac9fa422013-02-11 16:40:36 -08001974status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001975{
Mathias Agopian67106042013-03-14 19:18:13 -07001976 // called by the window manager when it wants to remove a Layer
1977 status_t err = NO_ERROR;
1978 sp<Layer> l(client->getLayerUser(handle));
1979 if (l != NULL) {
1980 err = removeLayer(l);
1981 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1982 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001983 }
1984 return err;
1985}
1986
Mathias Agopian13127d82013-03-05 17:47:11 -08001987status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001988{
Mathias Agopian67106042013-03-14 19:18:13 -07001989 // called by ~LayerCleaner() when all references to the IBinder (handle)
1990 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001991 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001992 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001993 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07001994 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001995 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001996 "error removing layer=%p (%s)", l.get(), strerror(-err));
1997 }
1998 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001999}
2000
Mathias Agopianb60314a2012-04-10 22:09:54 -07002001// ---------------------------------------------------------------------------
2002
Andy McFadden13a082e2012-08-24 10:16:42 -07002003void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002004 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002005 Vector<ComposerState> state;
2006 Vector<DisplayState> displays;
2007 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002008 d.what = DisplayState::eDisplayProjectionChanged |
2009 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002010 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002011 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002012 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002013 d.frame.makeInvalid();
2014 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002015 displays.add(d);
2016 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002017 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07002018
2019 const nsecs_t period =
2020 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2021 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002022}
2023
2024void SurfaceFlinger::initializeDisplays() {
2025 class MessageScreenInitialized : public MessageBase {
2026 SurfaceFlinger* flinger;
2027 public:
2028 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2029 virtual bool handler() {
2030 flinger->onInitializeDisplays();
2031 return true;
2032 }
2033 };
2034 sp<MessageBase> msg = new MessageScreenInitialized(this);
2035 postMessageAsync(msg); // we may be called from main thread, use async message
2036}
2037
2038
Mathias Agopiancde87a32012-09-13 14:09:01 -07002039void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002040 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2041 if (hw->isScreenAcquired()) {
2042 // this is expected, e.g. when power manager wakes up during boot
2043 ALOGD(" screen was previously acquired");
2044 return;
2045 }
2046
Mathias Agopian42977342012-08-05 00:40:46 -07002047 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002048 int32_t type = hw->getDisplayType();
2049 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2050 // built-in display, tell the HWC
2051 getHwComposer().acquire(type);
2052
2053 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2054 // FIXME: eventthread only knows about the main display right now
2055 mEventThread->onScreenAcquired();
2056 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002057 }
Mathias Agopian20128302012-08-13 18:32:13 -07002058 mVisibleRegionsDirty = true;
2059 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002060}
2061
Mathias Agopiancde87a32012-09-13 14:09:01 -07002062void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002063 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2064 if (!hw->isScreenAcquired()) {
2065 ALOGD(" screen was previously released");
2066 return;
2067 }
2068
2069 hw->releaseScreen();
2070 int32_t type = hw->getDisplayType();
2071 if (type < DisplayDevice::NUM_DISPLAY_TYPES) {
2072 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002073 // FIXME: eventthread only knows about the main display right now
2074 mEventThread->onScreenReleased();
2075 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002076
2077 // built-in display, tell the HWC
2078 getHwComposer().release(type);
2079 }
2080 mVisibleRegionsDirty = true;
2081 // from this point on, SF will stop drawing on this display
2082}
2083
2084void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2085 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002086 SurfaceFlinger& mFlinger;
2087 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002088 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002089 MessageScreenAcquired(SurfaceFlinger& flinger,
2090 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002091 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002092 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2093 if (hw == NULL) {
2094 ALOGE("Attempt to unblank null display %p", mDisplay.get());
2095 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2096 ALOGW("Attempt to unblank virtual display");
2097 } else {
2098 mFlinger.onScreenAcquired(hw);
2099 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002100 return true;
2101 }
2102 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002103 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2104 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002105}
2106
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002107void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002108 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002109 SurfaceFlinger& mFlinger;
2110 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002111 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002112 MessageScreenReleased(SurfaceFlinger& flinger,
2113 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002114 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002115 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2116 if (hw == NULL) {
2117 ALOGE("Attempt to blank null display %p", mDisplay.get());
2118 } else if (hw->getDisplayType() >= DisplayDevice::NUM_DISPLAY_TYPES) {
2119 ALOGW("Attempt to blank virtual display");
2120 } else {
2121 mFlinger.onScreenReleased(hw);
2122 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002123 return true;
2124 }
2125 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002126 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2127 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002128}
2129
2130// ---------------------------------------------------------------------------
2131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002132status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2133{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002134 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002135
Mathias Agopianbd115332013-04-18 16:41:04 -07002136 IPCThreadState* ipc = IPCThreadState::self();
2137 const int pid = ipc->getCallingPid();
2138 const int uid = ipc->getCallingUid();
2139 if ((uid != AID_SHELL) &&
2140 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002141 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002142 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002143 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002144 // Try to get the main lock, but don't insist if we can't
2145 // (this would indicate SF is stuck, but we want to be able to
2146 // print something in dumpsys).
2147 int retry = 3;
2148 while (mStateLock.tryLock()<0 && --retry>=0) {
2149 usleep(1000000);
2150 }
2151 const bool locked(retry >= 0);
2152 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002153 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002154 "SurfaceFlinger appears to be unresponsive, "
2155 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002156 }
2157
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002158 bool dumpAll = true;
2159 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002160 size_t numArgs = args.size();
2161 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002162 if ((index < numArgs) &&
2163 (args[index] == String16("--list"))) {
2164 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002165 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002166 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002167 }
2168
2169 if ((index < numArgs) &&
2170 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002171 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002172 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002173 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002174 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002175
2176 if ((index < numArgs) &&
2177 (args[index] == String16("--latency-clear"))) {
2178 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002179 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002180 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002181 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002182 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002183
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002184 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002185 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002186 }
2187
Mathias Agopian9795c422009-08-26 16:36:26 -07002188 if (locked) {
2189 mStateLock.unlock();
2190 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002191 }
2192 write(fd, result.string(), result.size());
2193 return NO_ERROR;
2194}
2195
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002196void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002197 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002198{
2199 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2200 const size_t count = currentLayers.size();
2201 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002202 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002203 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002204 }
2205}
2206
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002207void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002208 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002209{
2210 String8 name;
2211 if (index < args.size()) {
2212 name = String8(args[index]);
2213 index++;
2214 }
2215
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002216 const nsecs_t period =
2217 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2218 result.appendFormat("%lld\n", period);
2219
2220 if (name.isEmpty()) {
2221 mAnimFrameTracker.dump(result);
2222 } else {
2223 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2224 const size_t count = currentLayers.size();
2225 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002226 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002227 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002228 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002229 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002230 }
2231 }
2232}
2233
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002234void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002235 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002236{
2237 String8 name;
2238 if (index < args.size()) {
2239 name = String8(args[index]);
2240 index++;
2241 }
2242
2243 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2244 const size_t count = currentLayers.size();
2245 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002246 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002247 if (name.isEmpty() || (name == layer->getName())) {
2248 layer->clearStats();
2249 }
2250 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002251
2252 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002253}
2254
Jamie Gennis6547ff42013-07-16 20:12:42 -07002255// This should only be called from the main thread. Otherwise it would need
2256// the lock and should use mCurrentState rather than mDrawingState.
2257void SurfaceFlinger::logFrameStats() {
2258 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2259 const size_t count = drawingLayers.size();
2260 for (size_t i=0 ; i<count ; i++) {
2261 const sp<Layer>& layer(drawingLayers[i]);
2262 layer->logFrameStats();
2263 }
2264
2265 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2266}
2267
Andy McFadden4803b742012-09-24 19:07:20 -07002268/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2269{
2270 static const char* config =
2271 " [sf"
2272#ifdef NO_RGBX_8888
2273 " NO_RGBX_8888"
2274#endif
2275#ifdef HAS_CONTEXT_PRIORITY
2276 " HAS_CONTEXT_PRIORITY"
2277#endif
2278#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2279 " NEVER_DEFAULT_TO_ASYNC_MODE"
2280#endif
2281#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2282 " TARGET_DISABLE_TRIPLE_BUFFERING"
2283#endif
2284 "]";
2285 result.append(config);
2286}
2287
Mathias Agopian74d211a2013-04-22 16:55:35 +02002288void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2289 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002290{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002291 bool colorize = false;
2292 if (index < args.size()
2293 && (args[index] == String16("--color"))) {
2294 colorize = true;
2295 index++;
2296 }
2297
2298 Colorizer colorizer(colorize);
2299
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002300 // figure out if we're stuck somewhere
2301 const nsecs_t now = systemTime();
2302 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2303 const nsecs_t inTransaction(mDebugInTransaction);
2304 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2305 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2306
2307 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002308 * Dump library configuration.
2309 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002310
2311 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002312 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002313 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002314 appendSfConfigString(result);
2315 appendUiConfigString(result);
2316 appendGuiConfigString(result);
2317 result.append("\n");
2318
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002319 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002320 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002321 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002322 result.append(SyncFeatures::getInstance().toString());
2323 result.append("\n");
2324
Andy McFadden4803b742012-09-24 19:07:20 -07002325 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002326 * Dump the visible layer list
2327 */
2328 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2329 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002330 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002331 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002332 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002333 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002334 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002335 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002336 }
2337
2338 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002339 * Dump Display state
2340 */
2341
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002342 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002343 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002344 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002345 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2346 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002347 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002348 }
2349
2350 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002351 * Dump SurfaceFlinger global state
2352 */
2353
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002354 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002355 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002356 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002357
Mathias Agopian888c8222012-08-04 21:10:38 -07002358 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002359 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002360
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002361 colorizer.bold(result);
2362 result.appendFormat("EGL implementation : %s\n",
2363 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2364 colorizer.reset(result);
2365 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002366 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002367
Mathias Agopian875d8e12013-06-07 15:35:48 -07002368 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002369
Mathias Agopian42977342012-08-05 00:40:46 -07002370 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002371 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002372 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002373 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002374 " last eglSwapBuffers() time: %f us\n"
2375 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002376 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002377 " refresh-rate : %f fps\n"
2378 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002379 " y-dpi : %f\n"
2380 " EGL_NATIVE_VISUAL_ID : %d\n"
2381 " gpu_to_cpu_unsupported : %d\n"
2382 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002383 mLastSwapBufferTime/1000.0,
2384 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002385 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002386 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2387 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002388 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2389 mEGLNativeVisualId,
2390 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002391
Mathias Agopian74d211a2013-04-22 16:55:35 +02002392 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002393 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002394
Mathias Agopian74d211a2013-04-22 16:55:35 +02002395 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002396 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002397
2398 /*
2399 * VSYNC state
2400 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002401 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002402
2403 /*
2404 * Dump HWComposer state
2405 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002406 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002407 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002408 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002409 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002410 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2411 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002412 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002413
2414 /*
2415 * Dump gralloc state
2416 */
2417 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2418 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002419}
2420
Mathias Agopian13127d82013-03-05 17:47:11 -08002421const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002422SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002423 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002424 wp<IBinder> dpy;
2425 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2426 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2427 dpy = mDisplays.keyAt(i);
2428 break;
2429 }
2430 }
2431 if (dpy == NULL) {
2432 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2433 // Just use the primary display so we have something to return
2434 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2435 }
2436 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002437}
2438
Keun young Park63f165f2012-08-31 10:53:36 -07002439bool SurfaceFlinger::startDdmConnection()
2440{
2441 void* libddmconnection_dso =
2442 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2443 if (!libddmconnection_dso) {
2444 return false;
2445 }
2446 void (*DdmConnection_start)(const char* name);
2447 DdmConnection_start =
2448 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2449 if (!DdmConnection_start) {
2450 dlclose(libddmconnection_dso);
2451 return false;
2452 }
2453 (*DdmConnection_start)(getServiceName());
2454 return true;
2455}
2456
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002457status_t SurfaceFlinger::onTransact(
2458 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2459{
2460 switch (code) {
2461 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002462 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002463 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002464 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002465 case BLANK:
2466 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002467 {
2468 // codes that require permission check
2469 IPCThreadState* ipc = IPCThreadState::self();
2470 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002471 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002472 if ((uid != AID_GRAPHICS) &&
2473 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002474 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002475 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2476 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002477 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002478 break;
2479 }
2480 case CAPTURE_SCREEN:
2481 {
2482 // codes that require permission check
2483 IPCThreadState* ipc = IPCThreadState::self();
2484 const int pid = ipc->getCallingPid();
2485 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002486 if ((uid != AID_GRAPHICS) &&
2487 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002488 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002489 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2490 return PERMISSION_DENIED;
2491 }
2492 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002493 }
2494 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002495
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002496 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2497 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002498 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002499 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002500 IPCThreadState* ipc = IPCThreadState::self();
2501 const int pid = ipc->getCallingPid();
2502 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002503 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002504 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002505 return PERMISSION_DENIED;
2506 }
2507 int n;
2508 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002509 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002510 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002511 return NO_ERROR;
2512 case 1002: // SHOW_UPDATES
2513 n = data.readInt32();
2514 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002515 invalidateHwcGeometry();
2516 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002517 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002518 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002519 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002520 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002521 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002522 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002523 setTransactionFlags(
2524 eTransactionNeeded|
2525 eDisplayTransactionNeeded|
2526 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002527 return NO_ERROR;
2528 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002529 case 1006:{ // send empty update
2530 signalRefresh();
2531 return NO_ERROR;
2532 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002533 case 1008: // toggle use of hw composer
2534 n = data.readInt32();
2535 mDebugDisableHWC = n ? 1 : 0;
2536 invalidateHwcGeometry();
2537 repaintEverything();
2538 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002539 case 1009: // toggle use of transform hint
2540 n = data.readInt32();
2541 mDebugDisableTransformHint = n ? 1 : 0;
2542 invalidateHwcGeometry();
2543 repaintEverything();
2544 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002545 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002546 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002547 reply->writeInt32(0);
2548 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002549 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002550 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002551 return NO_ERROR;
2552 case 1013: {
2553 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002554 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2555 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002556 }
2557 return NO_ERROR;
2558 }
2559 }
2560 return err;
2561}
2562
Mathias Agopian53331da2011-08-22 21:44:41 -07002563void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002564 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002565 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002566}
2567
Mathias Agopian59119e62010-10-11 12:37:43 -07002568// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002569// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002570// ---------------------------------------------------------------------------
2571
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002572/* The code below is here to handle b/8734824
2573 *
2574 * We create a IGraphicBufferProducer wrapper that forwards all calls
2575 * to the calling binder thread, where they are executed. This allows
2576 * the calling thread to be reused (on the other side) and not
2577 * depend on having "enough" binder threads to handle the requests.
2578 *
2579 */
2580
2581class GraphicProducerWrapper : public BBinder, public MessageHandler {
2582 sp<IGraphicBufferProducer> impl;
2583 sp<Looper> looper;
2584 status_t result;
2585 bool exitPending;
2586 bool exitRequested;
2587 mutable Barrier barrier;
2588 volatile int32_t memoryBarrier;
2589 uint32_t code;
2590 Parcel const* data;
2591 Parcel* reply;
2592
2593 enum {
2594 MSG_API_CALL,
2595 MSG_EXIT
2596 };
2597
2598 /*
2599 * this is called by our "fake" BpGraphicBufferProducer. We package the
2600 * data and reply Parcel and forward them to the calling thread.
2601 */
2602 virtual status_t transact(uint32_t code,
2603 const Parcel& data, Parcel* reply, uint32_t flags) {
2604 this->code = code;
2605 this->data = &data;
2606 this->reply = reply;
2607 android_atomic_acquire_store(0, &memoryBarrier);
2608 if (exitPending) {
2609 // if we've exited, we run the message synchronously right here
2610 handleMessage(Message(MSG_API_CALL));
2611 } else {
2612 barrier.close();
2613 looper->sendMessage(this, Message(MSG_API_CALL));
2614 barrier.wait();
2615 }
2616 return NO_ERROR;
2617 }
2618
2619 /*
2620 * here we run on the binder calling thread. All we've got to do is
2621 * call the real BpGraphicBufferProducer.
2622 */
2623 virtual void handleMessage(const Message& message) {
2624 android_atomic_release_load(&memoryBarrier);
2625 if (message.what == MSG_API_CALL) {
2626 impl->asBinder()->transact(code, data[0], reply);
2627 barrier.open();
2628 } else if (message.what == MSG_EXIT) {
2629 exitRequested = true;
2630 }
2631 }
2632
2633public:
2634 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2635 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2636 exitPending(false), exitRequested(false) {
2637 }
2638
2639 status_t waitForResponse() {
2640 do {
2641 looper->pollOnce(-1);
2642 } while (!exitRequested);
2643 return result;
2644 }
2645
2646 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002647 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002648 exitPending = true;
2649 looper->sendMessage(this, Message(MSG_EXIT));
2650 }
2651};
2652
2653
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002654status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2655 const sp<IGraphicBufferProducer>& producer,
2656 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002657 uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002658
2659 if (CC_UNLIKELY(display == 0))
2660 return BAD_VALUE;
2661
2662 if (CC_UNLIKELY(producer == 0))
2663 return BAD_VALUE;
2664
2665 class MessageCaptureScreen : public MessageBase {
2666 SurfaceFlinger* flinger;
2667 sp<IBinder> display;
2668 sp<IGraphicBufferProducer> producer;
2669 uint32_t reqWidth, reqHeight;
2670 uint32_t minLayerZ,maxLayerZ;
2671 status_t result;
2672 public:
2673 MessageCaptureScreen(SurfaceFlinger* flinger,
2674 const sp<IBinder>& display,
2675 const sp<IGraphicBufferProducer>& producer,
2676 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002677 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002678 : flinger(flinger), display(display), producer(producer),
2679 reqWidth(reqWidth), reqHeight(reqHeight),
2680 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2681 result(PERMISSION_DENIED)
2682 {
2683 }
2684 status_t getResult() const {
2685 return result;
2686 }
2687 virtual bool handler() {
2688 Mutex::Autolock _l(flinger->mStateLock);
2689 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002690 result = flinger->captureScreenImplLocked(hw,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002691 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002692 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002693 return true;
2694 }
2695 };
2696
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002697 // make sure to process transactions before screenshots -- a transaction
2698 // might already be pending but scheduled for VSYNC; this guarantees we
2699 // will handle it before the screenshot. When VSYNC finally arrives
2700 // the scheduled transaction will be a no-op. If no transactions are
2701 // scheduled at this time, this will end-up being a no-op as well.
2702 mEventQueue.invalidateTransactionNow();
2703
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002704 // this creates a "fake" BBinder which will serve as a "fake" remote
2705 // binder to receive the marshaled calls and forward them to the
2706 // real remote (a BpGraphicBufferProducer)
2707 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2708
2709 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2710 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002711 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002712 display, IGraphicBufferProducer::asInterface( wrapper ),
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002713 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002714
2715 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002716 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002717 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002718 }
2719 return res;
2720}
2721
Mathias Agopian180f10d2013-04-10 22:55:41 -07002722
2723void SurfaceFlinger::renderScreenImplLocked(
2724 const sp<const DisplayDevice>& hw,
2725 uint32_t reqWidth, uint32_t reqHeight,
2726 uint32_t minLayerZ, uint32_t maxLayerZ,
2727 bool yswap)
2728{
2729 ATRACE_CALL();
2730
2731 // get screen geometry
2732 const uint32_t hw_w = hw->getWidth();
2733 const uint32_t hw_h = hw->getHeight();
2734
2735 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2736
2737 // make sure to clear all GL error flags
2738 while ( glGetError() != GL_NO_ERROR ) ;
2739
2740 // set-up our viewport
2741 glViewport(0, 0, reqWidth, reqHeight);
2742 glMatrixMode(GL_PROJECTION);
2743 glLoadIdentity();
2744 if (yswap) glOrthof(0, hw_w, hw_h, 0, 0, 1);
2745 else glOrthof(0, hw_w, 0, hw_h, 0, 1);
2746 glMatrixMode(GL_MODELVIEW);
2747 glLoadIdentity();
2748
2749 // redraw the screen entirely...
2750 glDisable(GL_SCISSOR_TEST);
2751 glClearColor(0,0,0,1);
2752 glClear(GL_COLOR_BUFFER_BIT);
2753 glDisable(GL_TEXTURE_EXTERNAL_OES);
2754 glDisable(GL_TEXTURE_2D);
2755
2756 const LayerVector& layers( mDrawingState.layersSortedByZ );
2757 const size_t count = layers.size();
2758 for (size_t i=0 ; i<count ; ++i) {
2759 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002760 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002761 if (state.layerStack == hw->getLayerStack()) {
2762 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2763 if (layer->isVisible()) {
2764 if (filtering) layer->setFiltering(true);
2765 layer->draw(hw);
2766 if (filtering) layer->setFiltering(false);
2767 }
2768 }
2769 }
2770 }
2771
2772 // compositionComplete is needed for older driver
2773 hw->compositionComplete();
2774}
2775
2776
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002777status_t SurfaceFlinger::captureScreenImplLocked(
2778 const sp<const DisplayDevice>& hw,
2779 const sp<IGraphicBufferProducer>& producer,
2780 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002781 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002782{
2783 ATRACE_CALL();
2784
Mathias Agopian180f10d2013-04-10 22:55:41 -07002785 // get screen geometry
2786 const uint32_t hw_w = hw->getWidth();
2787 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002788
Mathias Agopian180f10d2013-04-10 22:55:41 -07002789 // if we have secure windows on this display, never allow the screen capture
2790 if (hw->getSecureLayerVisible()) {
2791 ALOGW("FB is protected: PERMISSION_DENIED");
2792 return PERMISSION_DENIED;
Mathias Agopian74c40c02010-09-29 13:02:36 -07002793 }
2794
Mathias Agopian180f10d2013-04-10 22:55:41 -07002795 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2796 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2797 reqWidth, reqHeight, hw_w, hw_h);
2798 return BAD_VALUE;
2799 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002800
Mathias Agopian180f10d2013-04-10 22:55:41 -07002801 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2802 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2803
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002804 // create a surface (because we're a producer, and we need to
2805 // dequeue/queue a buffer)
2806 sp<Surface> sur = new Surface(producer);
2807 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002808
2809 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002810 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002811 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2812 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002813
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002814 int err = 0;
2815 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
2816 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2817 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002818
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002819 if (err == NO_ERROR) {
2820 ANativeWindowBuffer* buffer;
2821 /* TODO: Once we have the sync framework everywhere this can use
2822 * server-side waits on the fence that dequeueBuffer returns.
2823 */
2824 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2825 if (result == NO_ERROR) {
2826 // create an EGLImage from the buffer so we can later
2827 // turn it into a texture
2828 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2829 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2830 if (image != EGL_NO_IMAGE_KHR) {
2831 GLuint tname, name;
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002832
2833 // turn our EGLImage into a texture
2834 glGenTextures(1, &tname);
2835 glBindTexture(GL_TEXTURE_2D, tname);
2836 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
2837 // create a Framebuffer Object to render into
2838 glGenFramebuffersOES(1, &name);
2839 glBindFramebufferOES(GL_FRAMEBUFFER_OES, name);
2840 glFramebufferTexture2DOES(GL_FRAMEBUFFER_OES,
2841 GL_COLOR_ATTACHMENT0_OES, GL_TEXTURE_2D, tname, 0);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002842
2843 GLenum status = glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES);
2844 if (status == GL_FRAMEBUFFER_COMPLETE_OES) {
2845 // this will in fact render into our dequeued buffer
2846 // via an FBO, which means we didn't have to create
2847 // an EGLSurface and therefore we're not
2848 // dependent on the context's EGLConfig.
2849 renderScreenImplLocked(hw, reqWidth, reqHeight,
2850 minLayerZ, maxLayerZ, true);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002851 } else {
2852 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2853 result = INVALID_OPERATION;
2854 }
2855
2856 // back to main framebuffer
2857 glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);
2858 glDeleteFramebuffersOES(1, &name);
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002859 glDeleteTextures(1, &tname);
2860
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002861 // destroy our image
2862 eglDestroyImageKHR(mEGLDisplay, image);
2863 } else {
2864 result = BAD_VALUE;
2865 }
2866 window->queueBuffer(window, buffer, -1);
2867 }
2868 } else {
2869 result = BAD_VALUE;
2870 }
2871 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2872 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002873
Mathias Agopian875d8e12013-06-07 15:35:48 -07002874 hw->setViewportAndProjection();
Mathias Agopian2be4e8f2013-03-06 20:42:56 -08002875
Mathias Agopian74c40c02010-09-29 13:02:36 -07002876 return result;
2877}
2878
Mathias Agopianfee2b462013-07-03 12:34:01 -07002879void SurfaceFlinger::checkScreenshot(const sp<GraphicBuffer>& buf, void const* vaddr,
2880 const sp<const DisplayDevice>& hw,
2881 uint32_t minLayerZ, uint32_t maxLayerZ) {
2882 if (DEBUG_SCREENSHOTS) {
2883 for (ssize_t y=0 ; y<buf->height ; y++) {
2884 uint32_t const * p = (uint32_t const *)vaddr + y*buf->stride;
2885 for (ssize_t x=0 ; x<buf->width ; x++) {
2886 if (p[x] != 0xFF000000) return;
2887 }
2888 }
2889 ALOGE("*** we just took a black screenshot ***\n"
2890 "requested minz=%d, maxz=%d, layerStack=%d",
2891 minLayerZ, maxLayerZ, hw->getLayerStack());
2892 const LayerVector& layers( mDrawingState.layersSortedByZ );
2893 const size_t count = layers.size();
2894 for (size_t i=0 ; i<count ; ++i) {
2895 const sp<Layer>& layer(layers[i]);
2896 const Layer::State& state(layer->getDrawingState());
2897 const bool visible = (state.layerStack == hw->getLayerStack())
2898 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2899 && (layer->isVisible());
2900 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
2901 visible ? '+' : '-',
2902 i, layer->getName().string(), state.layerStack, state.z,
2903 layer->isVisible(), state.flags, state.alpha);
2904 }
2905 }
2906}
2907
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002908// ---------------------------------------------------------------------------
2909
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002910SurfaceFlinger::LayerVector::LayerVector() {
2911}
2912
2913SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002914 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002915}
2916
2917int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2918 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002919{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002920 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002921 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2922 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002923
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002924 uint32_t ls = l->getCurrentState().layerStack;
2925 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002926 if (ls != rs)
2927 return ls - rs;
2928
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002929 uint32_t lz = l->getCurrentState().z;
2930 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002931 if (lz != rz)
2932 return lz - rz;
2933
2934 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002935}
2936
2937// ---------------------------------------------------------------------------
2938
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002939SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2940 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002941}
2942
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002943SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002944 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002945 viewport.makeInvalid();
2946 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002947}
2948
2949// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002950
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002951}; // namespace android