blob: f73da35915f00a029f5bcdb5c09bcc141077e5f5 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070024
25#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <cutils/log.h>
28#include <cutils/properties.h>
29
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070032#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070033#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034
Mathias Agopianc666cae2012-07-25 18:56:13 -070035#include <ui/DisplayInfo.h>
36
Mathias Agopian921e6ac2012-07-23 23:11:29 -070037#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070038#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070039#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070040#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080041#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080042#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070043
44#include <ui/GraphicBufferAllocator.h>
45#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070046#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080047
Mathias Agopiancde87a32012-09-13 14:09:01 -070048#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <utils/String8.h>
50#include <utils/String16.h>
51#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080052#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian921e6ac2012-07-23 23:11:29 -070054#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070055#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopian3e25fd82013-04-22 17:52:16 +020057#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020059#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080060#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070061#include "DisplayDevice.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080062#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066
Mathias Agopiana4912602012-07-12 14:25:33 -070067#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070068#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070069#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070
Mathias Agopian875d8e12013-06-07 15:35:48 -070071#include "RenderEngine/RenderEngine.h"
72
Mathias Agopian3f844832013-08-07 21:24:32 -070073
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074#define DISPLAY_COUNT 1
75
Mathias Agopianfee2b462013-07-03 12:34:01 -070076/*
77 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
78 * black pixels.
79 */
80#define DEBUG_SCREENSHOTS false
81
Mathias Agopianca088332013-03-28 17:44:13 -070082EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
83
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080084namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085// ---------------------------------------------------------------------------
86
Mathias Agopian99b49842011-06-27 16:05:52 -070087const String16 sHardwareTest("android.permission.HARDWARE_TEST");
88const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
89const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
90const String16 sDump("android.permission.DUMP");
91
92// ---------------------------------------------------------------------------
93
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080094SurfaceFlinger::SurfaceFlinger()
95 : BnSurfaceComposer(), Thread(false),
96 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -070097 mTransactionPending(false),
98 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -070099 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700100 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700101 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700104 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800105 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700107 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700108 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700109 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700110 mDebugInSwapBuffers(0),
111 mLastSwapBufferTime(0),
112 mDebugInTransaction(0),
113 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700114 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115{
Steve Blocka19954a2012-01-04 20:05:49 +0000116 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117
118 // debugging stuff...
119 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700120
Mathias Agopianb4b17302013-03-20 18:36:41 -0700121 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700122 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700123
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 property_get("debug.sf.showupdates", value, "0");
125 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700126
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700127 property_get("debug.sf.ddms", value, "0");
128 mDebugDDMS = atoi(value);
129 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700130 if (!startDdmConnection()) {
131 // start failed, and DDMS debugging not enabled
132 mDebugDDMS = 0;
133 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700134 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700135 ALOGI_IF(mDebugRegion, "showupdates enabled");
136 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137}
138
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800139void SurfaceFlinger::onFirstRef()
140{
141 mEventQueue.init(this);
142
143 run("SurfaceFlinger", PRIORITY_URGENT_DISPLAY);
144
145 // Wait for the main thread to be done with its initialization
146 mReadyToRunBarrier.wait();
147}
148
149
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150SurfaceFlinger::~SurfaceFlinger()
151{
Mathias Agopiana4912602012-07-12 14:25:33 -0700152 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
153 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
154 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155}
156
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800157void SurfaceFlinger::binderDied(const wp<IBinder>& who)
158{
159 // the window manager died on us. prepare its eulogy.
160
Andy McFadden13a082e2012-08-24 10:16:42 -0700161 // restore initial conditions (default device unblank, etc)
162 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800163
164 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700165 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800166}
167
Mathias Agopian7e27f052010-05-28 14:22:23 -0700168sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169{
Mathias Agopian96f08192010-06-02 23:28:45 -0700170 sp<ISurfaceComposerClient> bclient;
171 sp<Client> client(new Client(this));
172 status_t err = client->initCheck();
173 if (err == NO_ERROR) {
174 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 return bclient;
177}
178
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700179sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
180 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700181{
182 class DisplayToken : public BBinder {
183 sp<SurfaceFlinger> flinger;
184 virtual ~DisplayToken() {
185 // no more references, this display must be terminated
186 Mutex::Autolock _l(flinger->mStateLock);
187 flinger->mCurrentState.displays.removeItem(this);
188 flinger->setTransactionFlags(eDisplayTransactionNeeded);
189 }
190 public:
191 DisplayToken(const sp<SurfaceFlinger>& flinger)
192 : flinger(flinger) {
193 }
194 };
195
196 sp<BBinder> token = new DisplayToken(this);
197
198 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700199 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700200 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700201 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700202 mCurrentState.displays.add(token, info);
203
204 return token;
205}
206
Jesse Hall6c913be2013-08-08 12:15:49 -0700207void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
208 Mutex::Autolock _l(mStateLock);
209
210 ssize_t idx = mCurrentState.displays.indexOfKey(display);
211 if (idx < 0) {
212 ALOGW("destroyDisplay: invalid display token");
213 return;
214 }
215
216 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
217 if (!info.isVirtualDisplay()) {
218 ALOGE("destroyDisplay called for non-virtual display");
219 return;
220 }
221
222 mCurrentState.displays.removeItemsAt(idx);
223 setTransactionFlags(eDisplayTransactionNeeded);
224}
225
Jesse Hall692c7232012-11-08 15:41:56 -0800226void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
227 ALOGW_IF(mBuiltinDisplays[type],
228 "Overwriting display token for display type %d", type);
229 mBuiltinDisplays[type] = new BBinder();
230 DisplayDeviceState info(type);
231 // All non-virtual displays are currently considered secure.
232 info.isSecure = true;
233 mCurrentState.displays.add(mBuiltinDisplays[type], info);
234}
235
Mathias Agopiane57f2922012-08-09 16:29:12 -0700236sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700237 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700238 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
239 return NULL;
240 }
Jesse Hall692c7232012-11-08 15:41:56 -0800241 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700242}
243
Jamie Gennis9a78c902011-01-12 18:30:40 -0800244sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
245{
246 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
247 return gba;
248}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700249
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250void SurfaceFlinger::bootFinished()
251{
252 const nsecs_t now = systemTime();
253 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000254 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700255 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700256
257 // wait patiently for the window manager death
258 const String16 name("window");
259 sp<IBinder> window(defaultServiceManager()->getService(name));
260 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700261 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700262 }
263
264 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700265 // formerly we would just kill the process, but we now ask it to exit so it
266 // can choose where to stop the animation.
267 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800268}
269
Mathias Agopian3f844832013-08-07 21:24:32 -0700270void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700271 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700272 RenderEngine& engine;
273 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700274 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700275 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
276 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700277 }
278 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700279 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700280 return true;
281 }
282 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700283 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700284}
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
Jesse Hall9e663de2013-08-16 14:28:37 -0700464 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_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) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700514 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700515 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;
Jesse Hall9e663de2013-08-16 14:28:37 -0700543 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_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 }
Jesse Hall9e663de2013-08-16 14:28:37 -0700664 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700665 // 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
Jesse Hall9e663de2013-08-16 14:28:37 -0700679 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_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
Mathias Agopiancd60f992012-08-16 16:28:27 -0700753 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700754 RenderEngine& engine(getRenderEngine());
755 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
756
Mathias Agopiancd60f992012-08-16 16:28:27 -0700757 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700758 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700759 }
760 }
761 }
762
763 postFramebuffer();
764
765 if (mDebugRegion > 1) {
766 usleep(mDebugRegion * 1000);
767 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700768
769 HWComposer& hwc(getHwComposer());
770 if (hwc.initCheck() == NO_ERROR) {
771 status_t err = hwc.prepare();
772 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
773 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700774}
775
776void SurfaceFlinger::preComposition()
777{
778 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700779 const LayerVector& layers(mDrawingState.layersSortedByZ);
780 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700781 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700782 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700783 needExtraInvalidate = true;
784 }
785 }
786 if (needExtraInvalidate) {
787 signalLayerUpdate();
788 }
789}
790
791void SurfaceFlinger::postComposition()
792{
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 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700797 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800798
799 if (mAnimCompositionPending) {
800 mAnimCompositionPending = false;
801
802 const HWComposer& hwc = getHwComposer();
803 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800804 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800805 mAnimFrameTracker.setActualPresentFence(presentFence);
806 } else {
807 // The HWC doesn't support present fences, so use the refresh
808 // timestamp instead.
809 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
810 mAnimFrameTracker.setActualPresentTime(presentTime);
811 }
812 mAnimFrameTracker.advanceFrame();
813 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700814}
815
816void SurfaceFlinger::rebuildLayerStacks() {
817 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700818 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700819 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700820 mVisibleRegionsDirty = false;
821 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700822
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700823 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700824 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700825 Region opaqueRegion;
826 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800827 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700828 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700829 const Transform& tr(hw->getTransform());
830 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700831 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700832 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700833 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700834
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700835 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700836 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700837 const sp<Layer>& layer(layers[i]);
838 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700839 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700840 Region drawRegion(tr.transform(
841 layer->visibleNonTransparentRegion));
842 drawRegion.andSelf(bounds);
843 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700844 layersSortedByZ.add(layer);
845 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700846 }
847 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700848 }
Mathias Agopian42977342012-08-05 00:40:46 -0700849 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700850 hw->undefinedRegion.set(bounds);
851 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
852 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700853 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700854 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700855}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700856
Mathias Agopiancd60f992012-08-16 16:28:27 -0700857void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700858 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
859 mDisplays[dpy]->beginFrame();
860 }
861
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700862 HWComposer& hwc(getHwComposer());
863 if (hwc.initCheck() == NO_ERROR) {
864 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700865 if (CC_UNLIKELY(mHwWorkListDirty)) {
866 mHwWorkListDirty = false;
867 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
868 sp<const DisplayDevice> hw(mDisplays[dpy]);
869 const int32_t id = hw->getHwcDisplayId();
870 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800871 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700872 hw->getVisibleLayersSortedByZ());
873 const size_t count = currentLayers.size();
874 if (hwc.createWorkList(id, count) == NO_ERROR) {
875 HWComposer::LayerListIterator cur = hwc.begin(id);
876 const HWComposer::LayerListIterator end = hwc.end(id);
877 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800878 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700879 layer->setGeometry(hw, *cur);
880 if (mDebugDisableHWC || mDebugRegion) {
881 cur->setSkip(true);
882 }
883 }
884 }
885 }
886 }
887 }
888
889 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700890 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700891 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700892 const int32_t id = hw->getHwcDisplayId();
893 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800894 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700895 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700896 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700897 HWComposer::LayerListIterator cur = hwc.begin(id);
898 const HWComposer::LayerListIterator end = hwc.end(id);
899 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
900 /*
901 * update the per-frame h/w composer data for each layer
902 * and build the transparent region of the FB
903 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800904 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700905 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700906 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700907 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700908 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700909
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700910 status_t err = hwc.prepare();
911 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700912
913 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
914 sp<const DisplayDevice> hw(mDisplays[dpy]);
915 hw->prepareFrame(hwc);
916 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700917 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700918}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700919
Mathias Agopiancd60f992012-08-16 16:28:27 -0700920void SurfaceFlinger::doComposition() {
921 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700922 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700923 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700924 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700925 if (hw->canDraw()) {
926 // transform the dirty region into this screen's coordinate space
927 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800928
929 // repaint the framebuffer (if needed)
930 doDisplayComposition(hw, dirtyRegion);
931
Mathias Agopiancd60f992012-08-16 16:28:27 -0700932 hw->dirtyRegion.clear();
933 hw->flip(hw->swapRegion);
934 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700935 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700936 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700937 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700938 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700939 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700940}
941
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800942void SurfaceFlinger::postFramebuffer()
943{
Mathias Agopian841cde52012-03-01 15:44:37 -0800944 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800945
Mathias Agopiana44b0412011-10-16 18:46:35 -0700946 const nsecs_t now = systemTime();
947 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700948
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700949 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700950 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700951 if (!hwc.supportsFramebufferTarget()) {
952 // EGL spec says:
953 // "surface must be bound to the calling thread's current context,
954 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700955 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700956 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700957 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700958 }
959
Mathias Agopian92a979a2012-08-02 18:32:23 -0700960 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700961 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800962 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700963 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700964 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700965 int32_t id = hw->getHwcDisplayId();
966 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700967 HWComposer::LayerListIterator cur = hwc.begin(id);
968 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700969 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700970 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700971 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700972 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700973 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700974 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700975 }
Jesse Hallef194142012-06-14 14:45:17 -0700976 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800977 }
978
Mathias Agopiana44b0412011-10-16 18:46:35 -0700979 mLastSwapBufferTime = systemTime() - now;
980 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700981
982 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
983 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
984 logFrameStats();
985 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800986}
987
Mathias Agopian87baae12012-07-31 12:38:26 -0700988void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800989{
Mathias Agopian841cde52012-03-01 15:44:37 -0800990 ATRACE_CALL();
991
Mathias Agopian7cc6df52013-06-05 14:30:54 -0700992 // here we keep a copy of the drawing state (that is the state that's
993 // going to be overwritten by handleTransactionLocked()) outside of
994 // mStateLock so that the side-effects of the State assignment
995 // don't happen with mStateLock held (which can cause deadlocks).
996 State drawingState(mDrawingState);
997
Mathias Agopianca4d3602011-05-19 15:38:14 -0700998 Mutex::Autolock _l(mStateLock);
999 const nsecs_t now = systemTime();
1000 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001
Mathias Agopianca4d3602011-05-19 15:38:14 -07001002 // Here we're guaranteed that some transaction flags are set
1003 // so we can call handleTransactionLocked() unconditionally.
1004 // We call getTransactionFlags(), which will also clear the flags,
1005 // with mStateLock held to guarantee that mCurrentState won't change
1006 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001007
Mathias Agopiane57f2922012-08-09 16:29:12 -07001008 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001009 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001010
Mathias Agopianca4d3602011-05-19 15:38:14 -07001011 mLastTransactionTime = systemTime() - now;
1012 mDebugInTransaction = 0;
1013 invalidateHwcGeometry();
1014 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001015}
1016
Mathias Agopian87baae12012-07-31 12:38:26 -07001017void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001018{
1019 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001020 const size_t count = currentLayers.size();
1021
1022 /*
1023 * Traversal of the children
1024 * (perform the transaction for each of them if needed)
1025 */
1026
Mathias Agopian3559b072012-08-15 13:46:03 -07001027 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001028 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001029 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001030 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1031 if (!trFlags) continue;
1032
1033 const uint32_t flags = layer->doTransaction(0);
1034 if (flags & Layer::eVisibleRegion)
1035 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001036 }
1037 }
1038
1039 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001040 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041 */
1042
Mathias Agopiane57f2922012-08-09 16:29:12 -07001043 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001044 // here we take advantage of Vector's copy-on-write semantics to
1045 // improve performance by skipping the transaction entirely when
1046 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001047 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1048 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001049 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001051 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001052 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001053
1054 // find the displays that were removed
1055 // (ie: in drawing state but not in current state)
1056 // also handle displays that changed
1057 // (ie: displays that are in both lists)
1058 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001059 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1060 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001061 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001062 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001063 // Call makeCurrent() on the primary display so we can
1064 // be sure that nothing associated with this display
1065 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001066 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001067 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001068 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1069 if (hw != NULL)
1070 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001071 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001072 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001073 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001074 } else {
1075 ALOGW("trying to remove the main display");
1076 }
1077 } else {
1078 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001079 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001080 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001081 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001082 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001083 // recreating the DisplayDevice, so we just remove it
1084 // from the drawing state, so that it get re-added
1085 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001086 sp<DisplayDevice> hw(getDisplayDevice(display));
1087 if (hw != NULL)
1088 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001089 mDisplays.removeItem(display);
1090 mDrawingState.displays.removeItemsAt(i);
1091 dc--; i--;
1092 // at this point we must loop to the next item
1093 continue;
1094 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001095
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001096 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001097 if (disp != NULL) {
1098 if (state.layerStack != draw[i].layerStack) {
1099 disp->setLayerStack(state.layerStack);
1100 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001101 if ((state.orientation != draw[i].orientation)
1102 || (state.viewport != draw[i].viewport)
1103 || (state.frame != draw[i].frame))
1104 {
1105 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001106 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001107 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001108 }
1109 }
1110 }
1111
1112 // find displays that were added
1113 // (ie: in current state but not in drawing state)
1114 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001115 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001116 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001117
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001118 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001119 sp<IGraphicBufferProducer> producer;
1120 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
1121
Jesse Hall02d86562013-03-25 14:43:23 -07001122 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001123 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001124 // Virtual displays without a surface are dormant:
1125 // they have external state (layer stack, projection,
1126 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001127 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001128
Jesse Hall02d86562013-03-25 14:43:23 -07001129 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001130 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1131 *mHwc, hwcDisplayId, state.surface, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001132 state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001133
1134 dispSurface = vds;
1135 if (hwcDisplayId >= 0) {
1136 producer = vds;
1137 } else {
1138 // There won't be any interaction with HWC for this virtual display,
1139 // so the GLES driver can pass buffers directly to the sink.
1140 producer = state.surface;
1141 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001142 }
1143 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001144 ALOGE_IF(state.surface!=NULL,
1145 "adding a supported display, but rendering "
1146 "surface is provided (%p), ignoring it",
1147 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001148 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001149 // for supported (by hwc) displays we provide our
1150 // own rendering surface
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001151 dispSurface = new FramebufferSurface(*mHwc, state.type, bq);
1152 producer = bq;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001153 }
1154
1155 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001156 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001157 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001158 state.type, hwcDisplayId, state.isSecure,
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001159 display, dispSurface, producer, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001160 hw->setLayerStack(state.layerStack);
1161 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001162 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001163 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001164 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001165 if (state.isVirtualDisplay()) {
1166 if (hwcDisplayId >= 0) {
1167 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1168 hw->getWidth(), hw->getHeight(),
1169 hw->getFormat());
1170 }
1171 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001172 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001173 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001174 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001175 }
1176 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001177 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001178 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001179
Mathias Agopian84300952012-11-21 16:02:13 -08001180 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1181 // The transform hint might have changed for some layers
1182 // (either because a display has changed, or because a layer
1183 // as changed).
1184 //
1185 // Walk through all the layers in currentLayers,
1186 // and update their transform hint.
1187 //
1188 // If a layer is visible only on a single display, then that
1189 // display is used to calculate the hint, otherwise we use the
1190 // default display.
1191 //
1192 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1193 // the hint is set before we acquire a buffer from the surface texture.
1194 //
1195 // NOTE: layer transactions have taken place already, so we use their
1196 // drawing state. However, SurfaceFlinger's own transaction has not
1197 // happened yet, so we must use the current state layer list
1198 // (soon to become the drawing state list).
1199 //
1200 sp<const DisplayDevice> disp;
1201 uint32_t currentlayerStack = 0;
1202 for (size_t i=0; i<count; i++) {
1203 // NOTE: we rely on the fact that layers are sorted by
1204 // layerStack first (so we don't have to traverse the list
1205 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001206 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001207 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001208 if (i==0 || currentlayerStack != layerStack) {
1209 currentlayerStack = layerStack;
1210 // figure out if this layerstack is mirrored
1211 // (more than one display) if so, pick the default display,
1212 // if not, pick the only display it's on.
1213 disp.clear();
1214 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1215 sp<const DisplayDevice> hw(mDisplays[dpy]);
1216 if (hw->getLayerStack() == currentlayerStack) {
1217 if (disp == NULL) {
1218 disp = hw;
1219 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001220 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001221 break;
1222 }
1223 }
1224 }
1225 }
Chet Haase91d25932013-04-11 15:24:55 -07001226 if (disp == NULL) {
1227 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1228 // redraw after transform hint changes. See bug 8508397.
1229
1230 // could be null when this layer is using a layerStack
1231 // that is not visible on any display. Also can occur at
1232 // screen off/on times.
1233 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001234 }
Chet Haase91d25932013-04-11 15:24:55 -07001235 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001236 }
1237 }
1238
1239
Mathias Agopian3559b072012-08-15 13:46:03 -07001240 /*
1241 * Perform our own transaction if needed
1242 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001243
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001244 const LayerVector& layers(mDrawingState.layersSortedByZ);
1245 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001246 // layers have been added
1247 mVisibleRegionsDirty = true;
1248 }
1249
1250 // some layers might have been removed, so
1251 // we need to update the regions they're exposing.
1252 if (mLayersRemoved) {
1253 mLayersRemoved = false;
1254 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001255 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001256 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001257 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001258 if (currentLayers.indexOf(layer) < 0) {
1259 // this layer is not visible anymore
1260 // TODO: we could traverse the tree from front to back and
1261 // compute the actual visible region
1262 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001263 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001264 Region visibleReg = s.transform.transform(
1265 Region(Rect(s.active.w, s.active.h)));
1266 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001267 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001268 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001269 }
1270
1271 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001272}
1273
1274void SurfaceFlinger::commitTransaction()
1275{
1276 if (!mLayersPendingRemoval.isEmpty()) {
1277 // Notify removed layers now that they can't be drawn from
1278 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1279 mLayersPendingRemoval[i]->onRemoved();
1280 }
1281 mLayersPendingRemoval.clear();
1282 }
1283
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001284 // If this transaction is part of a window animation then the next frame
1285 // we composite should be considered an animation as well.
1286 mAnimCompositionPending = mAnimTransactionPending;
1287
Mathias Agopian4fec8732012-06-29 14:12:52 -07001288 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001289 mTransactionPending = false;
1290 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001291 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292}
1293
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001294void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001295 const LayerVector& currentLayers, uint32_t layerStack,
1296 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297{
Mathias Agopian841cde52012-03-01 15:44:37 -08001298 ATRACE_CALL();
1299
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300 Region aboveOpaqueLayers;
1301 Region aboveCoveredLayers;
1302 Region dirty;
1303
Mathias Agopian87baae12012-07-31 12:38:26 -07001304 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305
1306 size_t i = currentLayers.size();
1307 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001308 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309
1310 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001311 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312
Jesse Hall01e29052013-02-19 16:13:35 -08001313 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001314 if (s.layerStack != layerStack)
1315 continue;
1316
Mathias Agopianab028732010-03-16 16:41:46 -07001317 /*
1318 * opaqueRegion: area of a surface that is fully opaque.
1319 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001320 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001321
1322 /*
1323 * visibleRegion: area of a surface that is visible on screen
1324 * and not fully transparent. This is essentially the layer's
1325 * footprint minus the opaque regions above it.
1326 * Areas covered by a translucent surface are considered visible.
1327 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001328 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001329
1330 /*
1331 * coveredRegion: area of a surface that is covered by all
1332 * visible regions above it (which includes the translucent areas).
1333 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001334 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001335
Jesse Halla8026d22012-09-25 13:25:04 -07001336 /*
1337 * transparentRegion: area of a surface that is hinted to be completely
1338 * transparent. This is only used to tell when the layer has no visible
1339 * non-transparent regions and can be removed from the layer list. It
1340 * does not affect the visibleRegion of this layer or any layers
1341 * beneath it. The hint may not be correct if apps don't respect the
1342 * SurfaceView restrictions (which, sadly, some don't).
1343 */
1344 Region transparentRegion;
1345
Mathias Agopianab028732010-03-16 16:41:46 -07001346
1347 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001348 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001349 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001350 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001351 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001352 if (!visibleRegion.isEmpty()) {
1353 // Remove the transparent area from the visible region
1354 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001355 const Transform tr(s.transform);
1356 if (tr.transformed()) {
1357 if (tr.preserveRects()) {
1358 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001359 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001360 } else {
1361 // transformation too complex, can't do the
1362 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001363 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001364 }
1365 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001366 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001367 }
Mathias Agopianab028732010-03-16 16:41:46 -07001368 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001369
Mathias Agopianab028732010-03-16 16:41:46 -07001370 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001371 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001372 if (s.alpha==255 && !translucent &&
1373 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1374 // the opaque region is the layer's footprint
1375 opaqueRegion = visibleRegion;
1376 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001377 }
1378 }
1379
Mathias Agopianab028732010-03-16 16:41:46 -07001380 // Clip the covered region to the visible region
1381 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1382
1383 // Update aboveCoveredLayers for next (lower) layer
1384 aboveCoveredLayers.orSelf(visibleRegion);
1385
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386 // subtract the opaque region covered by the layers above us
1387 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001388
1389 // compute this layer's dirty region
1390 if (layer->contentDirty) {
1391 // we need to invalidate the whole region
1392 dirty = visibleRegion;
1393 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001394 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001395 layer->contentDirty = false;
1396 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001397 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001398 * the exposed region consists of two components:
1399 * 1) what's VISIBLE now and was COVERED before
1400 * 2) what's EXPOSED now less what was EXPOSED before
1401 *
1402 * note that (1) is conservative, we start with the whole
1403 * visible region but only keep what used to be covered by
1404 * something -- which mean it may have been exposed.
1405 *
1406 * (2) handles areas that were not covered by anything but got
1407 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001408 */
Mathias Agopianab028732010-03-16 16:41:46 -07001409 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001410 const Region oldVisibleRegion = layer->visibleRegion;
1411 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001412 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1413 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414 }
1415 dirty.subtractSelf(aboveOpaqueLayers);
1416
1417 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001418 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419
Mathias Agopianab028732010-03-16 16:41:46 -07001420 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001421 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001422
Jesse Halla8026d22012-09-25 13:25:04 -07001423 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001424 layer->setVisibleRegion(visibleRegion);
1425 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001426 layer->setVisibleNonTransparentRegion(
1427 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001428 }
1429
Mathias Agopian87baae12012-07-31 12:38:26 -07001430 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431}
1432
Mathias Agopian87baae12012-07-31 12:38:26 -07001433void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1434 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001435 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001436 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1437 if (hw->getLayerStack() == layerStack) {
1438 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001439 }
1440 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001441}
1442
1443void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001444{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001445 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001446
Mathias Agopian4fec8732012-06-29 14:12:52 -07001447 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001448 const LayerVector& layers(mDrawingState.layersSortedByZ);
1449 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001450 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001451 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001452 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001453 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001454 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001455 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001456
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001457 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001458}
1459
Mathias Agopianad456f92011-01-13 17:53:01 -08001460void SurfaceFlinger::invalidateHwcGeometry()
1461{
1462 mHwWorkListDirty = true;
1463}
1464
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001465
Mathias Agopiancd60f992012-08-16 16:28:27 -07001466void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001467 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001468{
Mathias Agopian87baae12012-07-31 12:38:26 -07001469 Region dirtyRegion(inDirtyRegion);
1470
Mathias Agopianb8a55602009-06-26 19:06:36 -07001471 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001472 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001473
Mathias Agopian42977342012-08-05 00:40:46 -07001474 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001475 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001476 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1477 // takes a rectangle, we must make sure to update that whole
1478 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001479 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001480 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001481 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001482 // We need to redraw the rectangle that will be updated
1483 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001484 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001485 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001486 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001487 } else {
1488 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001489 dirtyRegion.set(hw->bounds());
1490 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001491 }
1492 }
1493
Mathias Agopiancd60f992012-08-16 16:28:27 -07001494 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001496 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001497 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001498
1499 // swap buffers (presentation)
1500 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001501}
1502
Mathias Agopiancd60f992012-08-16 16:28:27 -07001503void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001504{
Mathias Agopian3f844832013-08-07 21:24:32 -07001505 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001506 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001507 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001508 HWComposer::LayerListIterator cur = hwc.begin(id);
1509 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001510
Mathias Agopian85d751c2012-08-29 16:59:24 -07001511 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1512 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001513 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001514 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1515 hw->getDisplayName().string());
1516 return;
1517 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001518
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001519 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001520 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001521 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001522 // when using overlays, we assume a fully transparent framebuffer
1523 // NOTE: we could reduce how much we need to clear, for instance
1524 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001525 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001526 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001527 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001528 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001529 // we start with the whole screen area
1530 const Region bounds(hw->getBounds());
1531
1532 // we remove the scissor part
1533 // we're left with the letterbox region
1534 // (common case is that letterbox ends-up being empty)
1535 const Region letterbox(bounds.subtract(hw->getScissor()));
1536
1537 // compute the area to clear
1538 Region region(hw->undefinedRegion.merge(letterbox));
1539
1540 // but limit it to the dirty region
1541 region.andSelf(dirty);
1542
Mathias Agopianb9494d52012-04-18 02:28:45 -07001543 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001544 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001545 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001546 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001547 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001548 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001549
Mathias Agopian766dc492012-10-30 18:08:06 -07001550 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1551 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001552 // scissor on the main display. It should never be needed
1553 // anyways (though in theory it could since the API allows it).
1554 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001555 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001556 if (scissor != bounds) {
1557 // scissor doesn't match the screen's dimensions, so we
1558 // need to clear everything outside of it and enable
1559 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001560
Mathias Agopianf45c5102012-10-24 16:29:17 -07001561 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001562 const uint32_t height = hw->getHeight();
1563 engine.setScissor(scissor.left, height - scissor.bottom,
1564 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001565 }
1566 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001567 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001568
Mathias Agopian85d751c2012-08-29 16:59:24 -07001569 /*
1570 * and then, render the layers targeted at the framebuffer
1571 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001572
Mathias Agopian13127d82013-03-05 17:47:11 -08001573 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001574 const size_t count = layers.size();
1575 const Transform& tr = hw->getTransform();
1576 if (cur != end) {
1577 // we're using h/w composer
1578 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001579 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001580 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001581 if (!clip.isEmpty()) {
1582 switch (cur->getCompositionType()) {
1583 case HWC_OVERLAY: {
1584 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1585 && i
1586 && layer->isOpaque()
1587 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001588 // never clear the very first layer since we're
1589 // guaranteed the FB is already cleared
1590 layer->clearWithOpenGL(hw, clip);
1591 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001592 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001593 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001594 case HWC_FRAMEBUFFER: {
1595 layer->draw(hw, clip);
1596 break;
1597 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001598 case HWC_FRAMEBUFFER_TARGET: {
1599 // this should not happen as the iterator shouldn't
1600 // let us get there.
1601 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1602 break;
1603 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001604 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001605 }
1606 layer->setAcquireFence(hw, *cur);
1607 }
1608 } else {
1609 // we're not using h/w composer
1610 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001611 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001612 const Region clip(dirty.intersect(
1613 tr.transform(layer->visibleRegion)));
1614 if (!clip.isEmpty()) {
1615 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001616 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001617 }
1618 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001619
1620 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001621 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001622}
1623
Mathias Agopian3f844832013-08-07 21:24:32 -07001624void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001625 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001626 RenderEngine& engine(getRenderEngine());
1627 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001628}
1629
Mathias Agopianac9fa422013-02-11 16:40:36 -08001630void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1631 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001632 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001633 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001634{
Mathias Agopian96f08192010-06-02 23:28:45 -07001635 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001636 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001637
Mathias Agopian96f08192010-06-02 23:28:45 -07001638 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001639 Mutex::Autolock _l(mStateLock);
1640 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001641 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001642}
1643
Mathias Agopian3f844832013-08-07 21:24:32 -07001644status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001645 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001646 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001647 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001648 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001649 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001650 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001651 return NO_ERROR;
1652 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001653 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654}
1655
Mathias Agopian3f844832013-08-07 21:24:32 -07001656uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001657 return android_atomic_release_load(&mTransactionFlags);
1658}
1659
Mathias Agopian3f844832013-08-07 21:24:32 -07001660uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001661 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1662}
1663
Mathias Agopian3f844832013-08-07 21:24:32 -07001664uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001665 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1666 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001667 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001668 }
1669 return old;
1670}
1671
Mathias Agopian8b33f032012-07-24 20:43:54 -07001672void SurfaceFlinger::setTransactionState(
1673 const Vector<ComposerState>& state,
1674 const Vector<DisplayState>& displays,
1675 uint32_t flags)
1676{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001677 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001678 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001679 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001680
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001681 if (flags & eAnimation) {
1682 // For window updates that are part of an animation we must wait for
1683 // previous animation "frames" to be handled.
1684 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001685 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001686 if (CC_UNLIKELY(err != NO_ERROR)) {
1687 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001688 // caller after a few seconds.
1689 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1690 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001691 mAnimTransactionPending = false;
1692 break;
1693 }
1694 }
1695 }
1696
Mathias Agopiane57f2922012-08-09 16:29:12 -07001697 size_t count = displays.size();
1698 for (size_t i=0 ; i<count ; i++) {
1699 const DisplayState& s(displays[i]);
1700 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001701 }
1702
Mathias Agopiane57f2922012-08-09 16:29:12 -07001703 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001704 for (size_t i=0 ; i<count ; i++) {
1705 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001706 // Here we need to check that the interface we're given is indeed
1707 // one of our own. A malicious client could give us a NULL
1708 // IInterface, or one of its own or even one of our own but a
1709 // different type. All these situations would cause us to crash.
1710 //
1711 // NOTE: it would be better to use RTTI as we could directly check
1712 // that we have a Client*. however, RTTI is disabled in Android.
1713 if (s.client != NULL) {
1714 sp<IBinder> binder = s.client->asBinder();
1715 if (binder != NULL) {
1716 String16 desc(binder->getInterfaceDescriptor());
1717 if (desc == ISurfaceComposerClient::descriptor) {
1718 sp<Client> client( static_cast<Client *>(s.client.get()) );
1719 transactionFlags |= setClientStateLocked(client, s.state);
1720 }
1721 }
1722 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001723 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001724
Mathias Agopian386aa982011-11-07 21:58:03 -08001725 if (transactionFlags) {
1726 // this triggers the transaction
1727 setTransactionFlags(transactionFlags);
1728
1729 // if this is a synchronous transaction, wait for it to take effect
1730 // before returning.
1731 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001732 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001733 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001734 if (flags & eAnimation) {
1735 mAnimTransactionPending = true;
1736 }
1737 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001738 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1739 if (CC_UNLIKELY(err != NO_ERROR)) {
1740 // just in case something goes wrong in SF, return to the
1741 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001742 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1743 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001744 break;
1745 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001746 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001747 }
1748}
1749
Mathias Agopiane57f2922012-08-09 16:29:12 -07001750uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1751{
Jesse Hall9a143922012-10-04 16:29:19 -07001752 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1753 if (dpyIdx < 0)
1754 return 0;
1755
Mathias Agopiane57f2922012-08-09 16:29:12 -07001756 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001757 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001758 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001759 const uint32_t what = s.what;
1760 if (what & DisplayState::eSurfaceChanged) {
1761 if (disp.surface->asBinder() != s.surface->asBinder()) {
1762 disp.surface = s.surface;
1763 flags |= eDisplayTransactionNeeded;
1764 }
1765 }
1766 if (what & DisplayState::eLayerStackChanged) {
1767 if (disp.layerStack != s.layerStack) {
1768 disp.layerStack = s.layerStack;
1769 flags |= eDisplayTransactionNeeded;
1770 }
1771 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001772 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001773 if (disp.orientation != s.orientation) {
1774 disp.orientation = s.orientation;
1775 flags |= eDisplayTransactionNeeded;
1776 }
1777 if (disp.frame != s.frame) {
1778 disp.frame = s.frame;
1779 flags |= eDisplayTransactionNeeded;
1780 }
1781 if (disp.viewport != s.viewport) {
1782 disp.viewport = s.viewport;
1783 flags |= eDisplayTransactionNeeded;
1784 }
1785 }
1786 }
1787 return flags;
1788}
1789
1790uint32_t SurfaceFlinger::setClientStateLocked(
1791 const sp<Client>& client,
1792 const layer_state_t& s)
1793{
1794 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001795 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001796 if (layer != 0) {
1797 const uint32_t what = s.what;
1798 if (what & layer_state_t::ePositionChanged) {
1799 if (layer->setPosition(s.x, s.y))
1800 flags |= eTraversalNeeded;
1801 }
1802 if (what & layer_state_t::eLayerChanged) {
1803 // NOTE: index needs to be calculated before we update the state
1804 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1805 if (layer->setLayer(s.z)) {
1806 mCurrentState.layersSortedByZ.removeAt(idx);
1807 mCurrentState.layersSortedByZ.add(layer);
1808 // we need traversal (state changed)
1809 // AND transaction (list changed)
1810 flags |= eTransactionNeeded|eTraversalNeeded;
1811 }
1812 }
1813 if (what & layer_state_t::eSizeChanged) {
1814 if (layer->setSize(s.w, s.h)) {
1815 flags |= eTraversalNeeded;
1816 }
1817 }
1818 if (what & layer_state_t::eAlphaChanged) {
1819 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1820 flags |= eTraversalNeeded;
1821 }
1822 if (what & layer_state_t::eMatrixChanged) {
1823 if (layer->setMatrix(s.matrix))
1824 flags |= eTraversalNeeded;
1825 }
1826 if (what & layer_state_t::eTransparentRegionChanged) {
1827 if (layer->setTransparentRegionHint(s.transparentRegion))
1828 flags |= eTraversalNeeded;
1829 }
1830 if (what & layer_state_t::eVisibilityChanged) {
1831 if (layer->setFlags(s.flags, s.mask))
1832 flags |= eTraversalNeeded;
1833 }
1834 if (what & layer_state_t::eCropChanged) {
1835 if (layer->setCrop(s.crop))
1836 flags |= eTraversalNeeded;
1837 }
1838 if (what & layer_state_t::eLayerStackChanged) {
1839 // NOTE: index needs to be calculated before we update the state
1840 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1841 if (layer->setLayerStack(s.layerStack)) {
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 }
1850 return flags;
1851}
1852
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001853status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001854 const String8& name,
1855 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001856 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1857 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001858{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001859 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001860 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001861 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001862 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001863 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001864 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001865
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001866 status_t result = NO_ERROR;
1867
1868 sp<Layer> layer;
1869
Mathias Agopian3165cc22012-08-08 19:42:09 -07001870 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1871 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001872 result = createNormalLayer(client,
1873 name, w, h, flags, format,
1874 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001875 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001876 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001877 result = createDimLayer(client,
1878 name, w, h, flags,
1879 handle, gbp, &layer);
1880 break;
1881 default:
1882 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001883 break;
1884 }
1885
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001886 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001887 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001888 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001889 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001890 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001891}
1892
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001893status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1894 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1895 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001896{
1897 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001898 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001899 case PIXEL_FORMAT_TRANSPARENT:
1900 case PIXEL_FORMAT_TRANSLUCENT:
1901 format = PIXEL_FORMAT_RGBA_8888;
1902 break;
1903 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001904#ifdef NO_RGBX_8888
1905 format = PIXEL_FORMAT_RGB_565;
1906#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001907 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001908#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001909 break;
1910 }
1911
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001912#ifdef NO_RGBX_8888
1913 if (format == PIXEL_FORMAT_RGBX_8888)
1914 format = PIXEL_FORMAT_RGBA_8888;
1915#endif
1916
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001917 *outLayer = new Layer(this, client, name, w, h, flags);
1918 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1919 if (err == NO_ERROR) {
1920 *handle = (*outLayer)->getHandle();
1921 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001922 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001923
1924 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1925 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001926}
1927
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001928status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1929 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1930 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001931{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001932 *outLayer = new LayerDim(this, client, name, w, h, flags);
1933 *handle = (*outLayer)->getHandle();
1934 *gbp = (*outLayer)->getBufferQueue();
1935 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001936}
1937
Mathias Agopianac9fa422013-02-11 16:40:36 -08001938status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001939{
Mathias Agopian67106042013-03-14 19:18:13 -07001940 // called by the window manager when it wants to remove a Layer
1941 status_t err = NO_ERROR;
1942 sp<Layer> l(client->getLayerUser(handle));
1943 if (l != NULL) {
1944 err = removeLayer(l);
1945 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1946 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001947 }
1948 return err;
1949}
1950
Mathias Agopian13127d82013-03-05 17:47:11 -08001951status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001952{
Mathias Agopian67106042013-03-14 19:18:13 -07001953 // called by ~LayerCleaner() when all references to the IBinder (handle)
1954 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001955 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001956 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001957 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07001958 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001959 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001960 "error removing layer=%p (%s)", l.get(), strerror(-err));
1961 }
1962 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001963}
1964
Mathias Agopianb60314a2012-04-10 22:09:54 -07001965// ---------------------------------------------------------------------------
1966
Andy McFadden13a082e2012-08-24 10:16:42 -07001967void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08001968 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07001969 Vector<ComposerState> state;
1970 Vector<DisplayState> displays;
1971 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08001972 d.what = DisplayState::eDisplayProjectionChanged |
1973 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08001974 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08001975 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07001976 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001977 d.frame.makeInvalid();
1978 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001979 displays.add(d);
1980 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001981 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07001982
1983 const nsecs_t period =
1984 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
1985 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07001986}
1987
1988void SurfaceFlinger::initializeDisplays() {
1989 class MessageScreenInitialized : public MessageBase {
1990 SurfaceFlinger* flinger;
1991 public:
1992 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1993 virtual bool handler() {
1994 flinger->onInitializeDisplays();
1995 return true;
1996 }
1997 };
1998 sp<MessageBase> msg = new MessageScreenInitialized(this);
1999 postMessageAsync(msg); // we may be called from main thread, use async message
2000}
2001
2002
Mathias Agopiancde87a32012-09-13 14:09:01 -07002003void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002004 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2005 if (hw->isScreenAcquired()) {
2006 // this is expected, e.g. when power manager wakes up during boot
2007 ALOGD(" screen was previously acquired");
2008 return;
2009 }
2010
Mathias Agopian42977342012-08-05 00:40:46 -07002011 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002012 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002013 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002014 // built-in display, tell the HWC
2015 getHwComposer().acquire(type);
2016
2017 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2018 // FIXME: eventthread only knows about the main display right now
2019 mEventThread->onScreenAcquired();
2020 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002021 }
Mathias Agopian20128302012-08-13 18:32:13 -07002022 mVisibleRegionsDirty = true;
2023 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002024}
2025
Mathias Agopiancde87a32012-09-13 14:09:01 -07002026void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002027 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2028 if (!hw->isScreenAcquired()) {
2029 ALOGD(" screen was previously released");
2030 return;
2031 }
2032
2033 hw->releaseScreen();
2034 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002035 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002036 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002037 // FIXME: eventthread only knows about the main display right now
2038 mEventThread->onScreenReleased();
2039 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002040
2041 // built-in display, tell the HWC
2042 getHwComposer().release(type);
2043 }
2044 mVisibleRegionsDirty = true;
2045 // from this point on, SF will stop drawing on this display
2046}
2047
2048void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2049 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002050 SurfaceFlinger& mFlinger;
2051 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002052 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002053 MessageScreenAcquired(SurfaceFlinger& flinger,
2054 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002055 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002056 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2057 if (hw == NULL) {
2058 ALOGE("Attempt to unblank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002059 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002060 ALOGW("Attempt to unblank virtual display");
2061 } else {
2062 mFlinger.onScreenAcquired(hw);
2063 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002064 return true;
2065 }
2066 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002067 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2068 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002069}
2070
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002071void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002072 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002073 SurfaceFlinger& mFlinger;
2074 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002075 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002076 MessageScreenReleased(SurfaceFlinger& flinger,
2077 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002078 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002079 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2080 if (hw == NULL) {
2081 ALOGE("Attempt to blank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002082 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002083 ALOGW("Attempt to blank virtual display");
2084 } else {
2085 mFlinger.onScreenReleased(hw);
2086 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002087 return true;
2088 }
2089 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002090 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2091 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002092}
2093
2094// ---------------------------------------------------------------------------
2095
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002096status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2097{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002098 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002099
Mathias Agopianbd115332013-04-18 16:41:04 -07002100 IPCThreadState* ipc = IPCThreadState::self();
2101 const int pid = ipc->getCallingPid();
2102 const int uid = ipc->getCallingUid();
2103 if ((uid != AID_SHELL) &&
2104 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002105 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002106 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002107 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002108 // Try to get the main lock, but don't insist if we can't
2109 // (this would indicate SF is stuck, but we want to be able to
2110 // print something in dumpsys).
2111 int retry = 3;
2112 while (mStateLock.tryLock()<0 && --retry>=0) {
2113 usleep(1000000);
2114 }
2115 const bool locked(retry >= 0);
2116 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002117 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002118 "SurfaceFlinger appears to be unresponsive, "
2119 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002120 }
2121
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002122 bool dumpAll = true;
2123 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002124 size_t numArgs = args.size();
2125 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002126 if ((index < numArgs) &&
2127 (args[index] == String16("--list"))) {
2128 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002129 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002130 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002131 }
2132
2133 if ((index < numArgs) &&
2134 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002135 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002136 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002137 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002138 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002139
2140 if ((index < numArgs) &&
2141 (args[index] == String16("--latency-clear"))) {
2142 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002143 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002144 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002145 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002146 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002147
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002148 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002149 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002150 }
2151
Mathias Agopian9795c422009-08-26 16:36:26 -07002152 if (locked) {
2153 mStateLock.unlock();
2154 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002155 }
2156 write(fd, result.string(), result.size());
2157 return NO_ERROR;
2158}
2159
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002160void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002161 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002162{
2163 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2164 const size_t count = currentLayers.size();
2165 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002166 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002167 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002168 }
2169}
2170
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002171void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002172 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002173{
2174 String8 name;
2175 if (index < args.size()) {
2176 name = String8(args[index]);
2177 index++;
2178 }
2179
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002180 const nsecs_t period =
2181 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2182 result.appendFormat("%lld\n", period);
2183
2184 if (name.isEmpty()) {
2185 mAnimFrameTracker.dump(result);
2186 } else {
2187 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2188 const size_t count = currentLayers.size();
2189 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002190 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002191 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002192 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002193 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002194 }
2195 }
2196}
2197
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002198void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002199 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002200{
2201 String8 name;
2202 if (index < args.size()) {
2203 name = String8(args[index]);
2204 index++;
2205 }
2206
2207 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2208 const size_t count = currentLayers.size();
2209 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002210 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002211 if (name.isEmpty() || (name == layer->getName())) {
2212 layer->clearStats();
2213 }
2214 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002215
2216 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002217}
2218
Jamie Gennis6547ff42013-07-16 20:12:42 -07002219// This should only be called from the main thread. Otherwise it would need
2220// the lock and should use mCurrentState rather than mDrawingState.
2221void SurfaceFlinger::logFrameStats() {
2222 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2223 const size_t count = drawingLayers.size();
2224 for (size_t i=0 ; i<count ; i++) {
2225 const sp<Layer>& layer(drawingLayers[i]);
2226 layer->logFrameStats();
2227 }
2228
2229 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2230}
2231
Andy McFadden4803b742012-09-24 19:07:20 -07002232/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2233{
2234 static const char* config =
2235 " [sf"
2236#ifdef NO_RGBX_8888
2237 " NO_RGBX_8888"
2238#endif
2239#ifdef HAS_CONTEXT_PRIORITY
2240 " HAS_CONTEXT_PRIORITY"
2241#endif
2242#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2243 " NEVER_DEFAULT_TO_ASYNC_MODE"
2244#endif
2245#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2246 " TARGET_DISABLE_TRIPLE_BUFFERING"
2247#endif
2248 "]";
2249 result.append(config);
2250}
2251
Mathias Agopian74d211a2013-04-22 16:55:35 +02002252void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2253 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002254{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002255 bool colorize = false;
2256 if (index < args.size()
2257 && (args[index] == String16("--color"))) {
2258 colorize = true;
2259 index++;
2260 }
2261
2262 Colorizer colorizer(colorize);
2263
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002264 // figure out if we're stuck somewhere
2265 const nsecs_t now = systemTime();
2266 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2267 const nsecs_t inTransaction(mDebugInTransaction);
2268 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2269 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2270
2271 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002272 * Dump library configuration.
2273 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002274
2275 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002276 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002277 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002278 appendSfConfigString(result);
2279 appendUiConfigString(result);
2280 appendGuiConfigString(result);
2281 result.append("\n");
2282
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002283 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002284 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002285 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002286 result.append(SyncFeatures::getInstance().toString());
2287 result.append("\n");
2288
Andy McFadden4803b742012-09-24 19:07:20 -07002289 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002290 * Dump the visible layer list
2291 */
2292 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2293 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002294 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002295 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002296 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002297 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002298 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002299 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002300 }
2301
2302 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002303 * Dump Display state
2304 */
2305
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002306 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002307 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002308 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002309 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2310 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002311 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002312 }
2313
2314 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002315 * Dump SurfaceFlinger global state
2316 */
2317
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002318 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002319 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002320 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002321
Mathias Agopian888c8222012-08-04 21:10:38 -07002322 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002323 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002324
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002325 colorizer.bold(result);
2326 result.appendFormat("EGL implementation : %s\n",
2327 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2328 colorizer.reset(result);
2329 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002330 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002331
Mathias Agopian875d8e12013-06-07 15:35:48 -07002332 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002333
Mathias Agopian42977342012-08-05 00:40:46 -07002334 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002335 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002336 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002337 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002338 " last eglSwapBuffers() time: %f us\n"
2339 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002340 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002341 " refresh-rate : %f fps\n"
2342 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002343 " y-dpi : %f\n"
2344 " EGL_NATIVE_VISUAL_ID : %d\n"
2345 " gpu_to_cpu_unsupported : %d\n"
2346 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002347 mLastSwapBufferTime/1000.0,
2348 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002349 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002350 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2351 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002352 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2353 mEGLNativeVisualId,
2354 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002355
Mathias Agopian74d211a2013-04-22 16:55:35 +02002356 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002357 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002358
Mathias Agopian74d211a2013-04-22 16:55:35 +02002359 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002360 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002361
2362 /*
2363 * VSYNC state
2364 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002365 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002366
2367 /*
2368 * Dump HWComposer state
2369 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002370 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002371 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002372 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002373 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002374 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2375 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002376 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002377
2378 /*
2379 * Dump gralloc state
2380 */
2381 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2382 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002383}
2384
Mathias Agopian13127d82013-03-05 17:47:11 -08002385const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002386SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002387 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002388 wp<IBinder> dpy;
2389 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2390 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2391 dpy = mDisplays.keyAt(i);
2392 break;
2393 }
2394 }
2395 if (dpy == NULL) {
2396 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2397 // Just use the primary display so we have something to return
2398 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2399 }
2400 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002401}
2402
Keun young Park63f165f2012-08-31 10:53:36 -07002403bool SurfaceFlinger::startDdmConnection()
2404{
2405 void* libddmconnection_dso =
2406 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2407 if (!libddmconnection_dso) {
2408 return false;
2409 }
2410 void (*DdmConnection_start)(const char* name);
2411 DdmConnection_start =
2412 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2413 if (!DdmConnection_start) {
2414 dlclose(libddmconnection_dso);
2415 return false;
2416 }
2417 (*DdmConnection_start)(getServiceName());
2418 return true;
2419}
2420
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002421status_t SurfaceFlinger::onTransact(
2422 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2423{
2424 switch (code) {
2425 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002426 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002427 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002428 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002429 case BLANK:
2430 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002431 {
2432 // codes that require permission check
2433 IPCThreadState* ipc = IPCThreadState::self();
2434 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002435 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002436 if ((uid != AID_GRAPHICS) &&
2437 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002438 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002439 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2440 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002441 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002442 break;
2443 }
2444 case CAPTURE_SCREEN:
2445 {
2446 // codes that require permission check
2447 IPCThreadState* ipc = IPCThreadState::self();
2448 const int pid = ipc->getCallingPid();
2449 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002450 if ((uid != AID_GRAPHICS) &&
2451 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002452 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002453 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2454 return PERMISSION_DENIED;
2455 }
2456 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002457 }
2458 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002459
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002460 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2461 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002462 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002463 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002464 IPCThreadState* ipc = IPCThreadState::self();
2465 const int pid = ipc->getCallingPid();
2466 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002467 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002468 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002469 return PERMISSION_DENIED;
2470 }
2471 int n;
2472 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002473 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002474 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002475 return NO_ERROR;
2476 case 1002: // SHOW_UPDATES
2477 n = data.readInt32();
2478 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002479 invalidateHwcGeometry();
2480 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002481 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002482 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002483 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002484 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002485 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002486 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002487 setTransactionFlags(
2488 eTransactionNeeded|
2489 eDisplayTransactionNeeded|
2490 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002491 return NO_ERROR;
2492 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002493 case 1006:{ // send empty update
2494 signalRefresh();
2495 return NO_ERROR;
2496 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002497 case 1008: // toggle use of hw composer
2498 n = data.readInt32();
2499 mDebugDisableHWC = n ? 1 : 0;
2500 invalidateHwcGeometry();
2501 repaintEverything();
2502 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002503 case 1009: // toggle use of transform hint
2504 n = data.readInt32();
2505 mDebugDisableTransformHint = n ? 1 : 0;
2506 invalidateHwcGeometry();
2507 repaintEverything();
2508 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002509 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002510 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002511 reply->writeInt32(0);
2512 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002513 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002514 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002515 return NO_ERROR;
2516 case 1013: {
2517 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002518 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2519 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002520 }
2521 return NO_ERROR;
2522 }
2523 }
2524 return err;
2525}
2526
Mathias Agopian53331da2011-08-22 21:44:41 -07002527void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002528 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002529 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002530}
2531
Mathias Agopian59119e62010-10-11 12:37:43 -07002532// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002533// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002534// ---------------------------------------------------------------------------
2535
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002536/* The code below is here to handle b/8734824
2537 *
2538 * We create a IGraphicBufferProducer wrapper that forwards all calls
2539 * to the calling binder thread, where they are executed. This allows
2540 * the calling thread to be reused (on the other side) and not
2541 * depend on having "enough" binder threads to handle the requests.
2542 *
2543 */
2544
2545class GraphicProducerWrapper : public BBinder, public MessageHandler {
2546 sp<IGraphicBufferProducer> impl;
2547 sp<Looper> looper;
2548 status_t result;
2549 bool exitPending;
2550 bool exitRequested;
2551 mutable Barrier barrier;
2552 volatile int32_t memoryBarrier;
2553 uint32_t code;
2554 Parcel const* data;
2555 Parcel* reply;
2556
2557 enum {
2558 MSG_API_CALL,
2559 MSG_EXIT
2560 };
2561
2562 /*
2563 * this is called by our "fake" BpGraphicBufferProducer. We package the
2564 * data and reply Parcel and forward them to the calling thread.
2565 */
2566 virtual status_t transact(uint32_t code,
2567 const Parcel& data, Parcel* reply, uint32_t flags) {
2568 this->code = code;
2569 this->data = &data;
2570 this->reply = reply;
2571 android_atomic_acquire_store(0, &memoryBarrier);
2572 if (exitPending) {
2573 // if we've exited, we run the message synchronously right here
2574 handleMessage(Message(MSG_API_CALL));
2575 } else {
2576 barrier.close();
2577 looper->sendMessage(this, Message(MSG_API_CALL));
2578 barrier.wait();
2579 }
2580 return NO_ERROR;
2581 }
2582
2583 /*
2584 * here we run on the binder calling thread. All we've got to do is
2585 * call the real BpGraphicBufferProducer.
2586 */
2587 virtual void handleMessage(const Message& message) {
2588 android_atomic_release_load(&memoryBarrier);
2589 if (message.what == MSG_API_CALL) {
2590 impl->asBinder()->transact(code, data[0], reply);
2591 barrier.open();
2592 } else if (message.what == MSG_EXIT) {
2593 exitRequested = true;
2594 }
2595 }
2596
2597public:
2598 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2599 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2600 exitPending(false), exitRequested(false) {
2601 }
2602
2603 status_t waitForResponse() {
2604 do {
2605 looper->pollOnce(-1);
2606 } while (!exitRequested);
2607 return result;
2608 }
2609
2610 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002611 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002612 exitPending = true;
2613 looper->sendMessage(this, Message(MSG_EXIT));
2614 }
2615};
2616
2617
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002618status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2619 const sp<IGraphicBufferProducer>& producer,
2620 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002621 uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002622
2623 if (CC_UNLIKELY(display == 0))
2624 return BAD_VALUE;
2625
2626 if (CC_UNLIKELY(producer == 0))
2627 return BAD_VALUE;
2628
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002629 // if we have secure windows on this display, never allow the screen capture
2630 // unless the producer interface is local (i.e.: we can take a screenshot for
2631 // ourselves).
2632 if (!producer->asBinder()->localBinder()) {
2633 Mutex::Autolock _l(mStateLock);
2634 sp<const DisplayDevice> hw(getDisplayDevice(display));
2635 if (hw->getSecureLayerVisible()) {
2636 ALOGW("FB is protected: PERMISSION_DENIED");
2637 return PERMISSION_DENIED;
2638 }
2639 }
2640
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002641 class MessageCaptureScreen : public MessageBase {
2642 SurfaceFlinger* flinger;
2643 sp<IBinder> display;
2644 sp<IGraphicBufferProducer> producer;
2645 uint32_t reqWidth, reqHeight;
2646 uint32_t minLayerZ,maxLayerZ;
2647 status_t result;
2648 public:
2649 MessageCaptureScreen(SurfaceFlinger* flinger,
2650 const sp<IBinder>& display,
2651 const sp<IGraphicBufferProducer>& producer,
2652 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002653 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002654 : flinger(flinger), display(display), producer(producer),
2655 reqWidth(reqWidth), reqHeight(reqHeight),
2656 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2657 result(PERMISSION_DENIED)
2658 {
2659 }
2660 status_t getResult() const {
2661 return result;
2662 }
2663 virtual bool handler() {
2664 Mutex::Autolock _l(flinger->mStateLock);
2665 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002666 result = flinger->captureScreenImplLocked(hw,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002667 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002668 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002669 return true;
2670 }
2671 };
2672
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002673 // make sure to process transactions before screenshots -- a transaction
2674 // might already be pending but scheduled for VSYNC; this guarantees we
2675 // will handle it before the screenshot. When VSYNC finally arrives
2676 // the scheduled transaction will be a no-op. If no transactions are
2677 // scheduled at this time, this will end-up being a no-op as well.
2678 mEventQueue.invalidateTransactionNow();
2679
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002680 // this creates a "fake" BBinder which will serve as a "fake" remote
2681 // binder to receive the marshaled calls and forward them to the
2682 // real remote (a BpGraphicBufferProducer)
2683 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2684
2685 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2686 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002687 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002688 display, IGraphicBufferProducer::asInterface( wrapper ),
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002689 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002690
2691 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002692 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002693 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002694 }
2695 return res;
2696}
2697
Mathias Agopian180f10d2013-04-10 22:55:41 -07002698
2699void SurfaceFlinger::renderScreenImplLocked(
2700 const sp<const DisplayDevice>& hw,
2701 uint32_t reqWidth, uint32_t reqHeight,
2702 uint32_t minLayerZ, uint32_t maxLayerZ,
2703 bool yswap)
2704{
2705 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002706 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002707
2708 // get screen geometry
2709 const uint32_t hw_w = hw->getWidth();
2710 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002711 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2712
2713 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002714 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002715
2716 // set-up our viewport
Mathias Agopian3f844832013-08-07 21:24:32 -07002717 engine.setViewportAndProjection(reqWidth, reqHeight, hw_w, hw_h, yswap);
2718 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002719
2720 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002721 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002722
2723 const LayerVector& layers( mDrawingState.layersSortedByZ );
2724 const size_t count = layers.size();
2725 for (size_t i=0 ; i<count ; ++i) {
2726 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002727 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002728 if (state.layerStack == hw->getLayerStack()) {
2729 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2730 if (layer->isVisible()) {
2731 if (filtering) layer->setFiltering(true);
2732 layer->draw(hw);
2733 if (filtering) layer->setFiltering(false);
2734 }
2735 }
2736 }
2737 }
2738
2739 // compositionComplete is needed for older driver
2740 hw->compositionComplete();
2741}
2742
2743
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002744status_t SurfaceFlinger::captureScreenImplLocked(
2745 const sp<const DisplayDevice>& hw,
2746 const sp<IGraphicBufferProducer>& producer,
2747 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002748 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002749{
2750 ATRACE_CALL();
2751
Mathias Agopian180f10d2013-04-10 22:55:41 -07002752 // get screen geometry
2753 const uint32_t hw_w = hw->getWidth();
2754 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002755
Mathias Agopian180f10d2013-04-10 22:55:41 -07002756 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2757 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2758 reqWidth, reqHeight, hw_w, hw_h);
2759 return BAD_VALUE;
2760 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002761
Mathias Agopian180f10d2013-04-10 22:55:41 -07002762 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2763 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2764
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002765 // create a surface (because we're a producer, and we need to
2766 // dequeue/queue a buffer)
2767 sp<Surface> sur = new Surface(producer);
2768 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002769
2770 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002771 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002772 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2773 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002774
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002775 int err = 0;
2776 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
2777 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2778 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002779
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002780 if (err == NO_ERROR) {
2781 ANativeWindowBuffer* buffer;
2782 /* TODO: Once we have the sync framework everywhere this can use
2783 * server-side waits on the fence that dequeueBuffer returns.
2784 */
2785 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2786 if (result == NO_ERROR) {
2787 // create an EGLImage from the buffer so we can later
2788 // turn it into a texture
2789 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2790 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2791 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002792 // this binds the given EGLImage as a framebuffer for the
2793 // duration of this scope.
2794 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2795 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002796 // this will in fact render into our dequeued buffer
2797 // via an FBO, which means we didn't have to create
2798 // an EGLSurface and therefore we're not
2799 // dependent on the context's EGLConfig.
2800 renderScreenImplLocked(hw, reqWidth, reqHeight,
2801 minLayerZ, maxLayerZ, true);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002802 } else {
2803 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2804 result = INVALID_OPERATION;
2805 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002806 // destroy our image
2807 eglDestroyImageKHR(mEGLDisplay, image);
2808 } else {
2809 result = BAD_VALUE;
2810 }
2811 window->queueBuffer(window, buffer, -1);
2812 }
2813 } else {
2814 result = BAD_VALUE;
2815 }
2816 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2817 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002818
Mathias Agopian875d8e12013-06-07 15:35:48 -07002819 hw->setViewportAndProjection();
Mathias Agopian2be4e8f2013-03-06 20:42:56 -08002820
Mathias Agopian74c40c02010-09-29 13:02:36 -07002821 return result;
2822}
2823
Mathias Agopianfee2b462013-07-03 12:34:01 -07002824void SurfaceFlinger::checkScreenshot(const sp<GraphicBuffer>& buf, void const* vaddr,
2825 const sp<const DisplayDevice>& hw,
2826 uint32_t minLayerZ, uint32_t maxLayerZ) {
2827 if (DEBUG_SCREENSHOTS) {
2828 for (ssize_t y=0 ; y<buf->height ; y++) {
2829 uint32_t const * p = (uint32_t const *)vaddr + y*buf->stride;
2830 for (ssize_t x=0 ; x<buf->width ; x++) {
2831 if (p[x] != 0xFF000000) return;
2832 }
2833 }
2834 ALOGE("*** we just took a black screenshot ***\n"
2835 "requested minz=%d, maxz=%d, layerStack=%d",
2836 minLayerZ, maxLayerZ, hw->getLayerStack());
2837 const LayerVector& layers( mDrawingState.layersSortedByZ );
2838 const size_t count = layers.size();
2839 for (size_t i=0 ; i<count ; ++i) {
2840 const sp<Layer>& layer(layers[i]);
2841 const Layer::State& state(layer->getDrawingState());
2842 const bool visible = (state.layerStack == hw->getLayerStack())
2843 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2844 && (layer->isVisible());
2845 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
2846 visible ? '+' : '-',
2847 i, layer->getName().string(), state.layerStack, state.z,
2848 layer->isVisible(), state.flags, state.alpha);
2849 }
2850 }
2851}
2852
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002853// ---------------------------------------------------------------------------
2854
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002855SurfaceFlinger::LayerVector::LayerVector() {
2856}
2857
2858SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002859 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002860}
2861
2862int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2863 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002864{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002865 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002866 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2867 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002868
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002869 uint32_t ls = l->getCurrentState().layerStack;
2870 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002871 if (ls != rs)
2872 return ls - rs;
2873
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002874 uint32_t lz = l->getCurrentState().z;
2875 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002876 if (lz != rz)
2877 return lz - rz;
2878
2879 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002880}
2881
2882// ---------------------------------------------------------------------------
2883
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002884SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2885 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002886}
2887
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002888SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002889 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002890 viewport.makeInvalid();
2891 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002892}
2893
2894// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002895
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002896}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002897
2898
2899#if defined(__gl_h_)
2900#error "don't include gl/gl.h in this file"
2901#endif
2902
2903#if defined(__gl2_h_)
2904#error "don't include gl2/gl2.h in this file"
2905#endif