blob: 7e62dae9df6ab43214d62ea3edef9c771027e8c3 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070024
Mathias Agopian4f4f0942013-08-19 17:26:18 -070025#if defined(HAVE_PTHREADS)
26#include <sys/resource.h>
27#endif
28
Mathias Agopian921e6ac2012-07-23 23:11:29 -070029#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include <cutils/log.h>
32#include <cutils/properties.h>
33
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070034#include <binder/IPCThreadState.h>
35#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070036#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070037#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070038
Mathias Agopianc666cae2012-07-25 18:56:13 -070039#include <ui/DisplayInfo.h>
40
Mathias Agopian921e6ac2012-07-23 23:11:29 -070041#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070042#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070043#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070044#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080045#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080046#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070047
48#include <ui/GraphicBufferAllocator.h>
49#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070050#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080051
Mathias Agopiancde87a32012-09-13 14:09:01 -070052#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053#include <utils/String8.h>
54#include <utils/String16.h>
55#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080056#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
Mathias Agopian921e6ac2012-07-23 23:11:29 -070058#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070059#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060
Mathias Agopian3e25fd82013-04-22 17:52:16 +020061#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080062#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020063#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080064#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070065#include "DisplayDevice.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080066#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070
Mathias Agopiana4912602012-07-12 14:25:33 -070071#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070072#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070073#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074
Mathias Agopian875d8e12013-06-07 15:35:48 -070075#include "RenderEngine/RenderEngine.h"
76
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077#define DISPLAY_COUNT 1
78
Mathias Agopianfee2b462013-07-03 12:34:01 -070079/*
80 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
81 * black pixels.
82 */
83#define DEBUG_SCREENSHOTS false
84
Mathias Agopianca088332013-03-28 17:44:13 -070085EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
86
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088// ---------------------------------------------------------------------------
89
Mathias Agopian99b49842011-06-27 16:05:52 -070090const String16 sHardwareTest("android.permission.HARDWARE_TEST");
91const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
92const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
93const String16 sDump("android.permission.DUMP");
94
95// ---------------------------------------------------------------------------
96
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -070098 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700100 mTransactionPending(false),
101 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700102 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700103 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700104 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700107 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800108 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700110 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700111 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700112 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700113 mDebugInSwapBuffers(0),
114 mLastSwapBufferTime(0),
115 mDebugInTransaction(0),
116 mLastTransactionTime(0),
Mathias Agopian5f20e2d2012-08-10 18:50:38 -0700117 mBootFinished(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118{
Steve Blocka19954a2012-01-04 20:05:49 +0000119 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120
121 // debugging stuff...
122 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700123
Mathias Agopianb4b17302013-03-20 18:36:41 -0700124 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700125 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700126
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127 property_get("debug.sf.showupdates", value, "0");
128 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700129
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700130 property_get("debug.sf.ddms", value, "0");
131 mDebugDDMS = atoi(value);
132 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700133 if (!startDdmConnection()) {
134 // start failed, and DDMS debugging not enabled
135 mDebugDDMS = 0;
136 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700137 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700138 ALOGI_IF(mDebugRegion, "showupdates enabled");
139 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140}
141
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800142void SurfaceFlinger::onFirstRef()
143{
144 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800145}
146
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147SurfaceFlinger::~SurfaceFlinger()
148{
Mathias Agopiana4912602012-07-12 14:25:33 -0700149 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
150 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
151 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152}
153
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800154void SurfaceFlinger::binderDied(const wp<IBinder>& who)
155{
156 // the window manager died on us. prepare its eulogy.
157
Andy McFadden13a082e2012-08-24 10:16:42 -0700158 // restore initial conditions (default device unblank, etc)
159 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800160
161 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700162 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800163}
164
Mathias Agopian7e27f052010-05-28 14:22:23 -0700165sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166{
Mathias Agopian96f08192010-06-02 23:28:45 -0700167 sp<ISurfaceComposerClient> bclient;
168 sp<Client> client(new Client(this));
169 status_t err = client->initCheck();
170 if (err == NO_ERROR) {
171 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800172 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 return bclient;
174}
175
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700176sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
177 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700178{
179 class DisplayToken : public BBinder {
180 sp<SurfaceFlinger> flinger;
181 virtual ~DisplayToken() {
182 // no more references, this display must be terminated
183 Mutex::Autolock _l(flinger->mStateLock);
184 flinger->mCurrentState.displays.removeItem(this);
185 flinger->setTransactionFlags(eDisplayTransactionNeeded);
186 }
187 public:
188 DisplayToken(const sp<SurfaceFlinger>& flinger)
189 : flinger(flinger) {
190 }
191 };
192
193 sp<BBinder> token = new DisplayToken(this);
194
195 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700196 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700197 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700198 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700199 mCurrentState.displays.add(token, info);
200
201 return token;
202}
203
Jesse Hall6c913be2013-08-08 12:15:49 -0700204void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
205 Mutex::Autolock _l(mStateLock);
206
207 ssize_t idx = mCurrentState.displays.indexOfKey(display);
208 if (idx < 0) {
209 ALOGW("destroyDisplay: invalid display token");
210 return;
211 }
212
213 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
214 if (!info.isVirtualDisplay()) {
215 ALOGE("destroyDisplay called for non-virtual display");
216 return;
217 }
218
219 mCurrentState.displays.removeItemsAt(idx);
220 setTransactionFlags(eDisplayTransactionNeeded);
221}
222
Jesse Hall692c7232012-11-08 15:41:56 -0800223void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
224 ALOGW_IF(mBuiltinDisplays[type],
225 "Overwriting display token for display type %d", type);
226 mBuiltinDisplays[type] = new BBinder();
227 DisplayDeviceState info(type);
228 // All non-virtual displays are currently considered secure.
229 info.isSecure = true;
230 mCurrentState.displays.add(mBuiltinDisplays[type], info);
231}
232
Mathias Agopiane57f2922012-08-09 16:29:12 -0700233sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700234 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700235 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
236 return NULL;
237 }
Jesse Hall692c7232012-11-08 15:41:56 -0800238 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700239}
240
Jamie Gennis9a78c902011-01-12 18:30:40 -0800241sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
242{
243 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
244 return gba;
245}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700246
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800247void SurfaceFlinger::bootFinished()
248{
249 const nsecs_t now = systemTime();
250 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000251 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700252 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700253
254 // wait patiently for the window manager death
255 const String16 name("window");
256 sp<IBinder> window(defaultServiceManager()->getService(name));
257 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700258 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700259 }
260
261 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700262 // formerly we would just kill the process, but we now ask it to exit so it
263 // can choose where to stop the animation.
264 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800265}
266
Mathias Agopian3f844832013-08-07 21:24:32 -0700267void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700268 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700269 RenderEngine& engine;
270 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700271 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700272 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
273 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700274 }
275 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700276 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700277 return true;
278 }
279 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700280 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700281}
282
Mathias Agopian722b98f2012-09-25 18:24:31 -0700283status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700284 EGLDisplay dpy,
285 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700286 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700287 EGLConfig* outConfig)
288{
289 EGLConfig config = NULL;
290 EGLint numConfigs = -1, n=0;
291 eglGetConfigs(dpy, NULL, 0, &numConfigs);
292 EGLConfig* const configs = new EGLConfig[numConfigs];
293 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700294
Mathias Agopian722b98f2012-09-25 18:24:31 -0700295 if (n) {
296 if (attribute != EGL_NONE) {
297 for (int i=0 ; i<n ; i++) {
298 EGLint value = 0;
299 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
300 if (wanted == value) {
301 *outConfig = configs[i];
302 delete [] configs;
303 return NO_ERROR;
304 }
305 }
306 } else {
307 // just pick the first one
308 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700309 delete [] configs;
310 return NO_ERROR;
311 }
312 }
313 delete [] configs;
314 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800315}
316
Mathias Agopian722b98f2012-09-25 18:24:31 -0700317class EGLAttributeVector {
318 struct Attribute;
319 class Adder;
320 friend class Adder;
321 KeyedVector<Attribute, EGLint> mList;
322 struct Attribute {
323 Attribute() {};
324 Attribute(EGLint v) : v(v) { }
325 EGLint v;
326 bool operator < (const Attribute& other) const {
327 // this places EGL_NONE at the end
328 EGLint lhs(v);
329 EGLint rhs(other.v);
330 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
331 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
332 return lhs < rhs;
333 }
334 };
335 class Adder {
336 friend class EGLAttributeVector;
337 EGLAttributeVector& v;
338 EGLint attribute;
339 Adder(EGLAttributeVector& v, EGLint attribute)
340 : v(v), attribute(attribute) {
341 }
342 public:
343 void operator = (EGLint value) {
344 if (attribute != EGL_NONE) {
345 v.mList.add(attribute, value);
346 }
347 }
348 operator EGLint () const { return v.mList[attribute]; }
349 };
350public:
351 EGLAttributeVector() {
352 mList.add(EGL_NONE, EGL_NONE);
353 }
354 void remove(EGLint attribute) {
355 if (attribute != EGL_NONE) {
356 mList.removeItem(attribute);
357 }
358 }
359 Adder operator [] (EGLint attribute) {
360 return Adder(*this, attribute);
361 }
362 EGLint operator [] (EGLint attribute) const {
363 return mList[attribute];
364 }
365 // cast-operator to (EGLint const*)
366 operator EGLint const* () const { return &mList.keyAt(0).v; }
367};
368
Mathias Agopiana4912602012-07-12 14:25:33 -0700369EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId) {
370 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
371 // it is to be used with WIFI displays
372 EGLConfig config;
373 EGLint dummy;
374 status_t err;
Mathias Agopianda27af92012-09-13 18:17:13 -0700375
Mathias Agopian722b98f2012-09-25 18:24:31 -0700376 EGLAttributeVector attribs;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700377 // TODO: enable ES2
378 //attribs[EGL_RENDERABLE_TYPE] = EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT;
379 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
Mathias Agopian722b98f2012-09-25 18:24:31 -0700380 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
381 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
382 attribs[EGL_RED_SIZE] = 8;
383 attribs[EGL_GREEN_SIZE] = 8;
384 attribs[EGL_BLUE_SIZE] = 8;
385
386 err = selectConfigForAttribute(display, attribs, EGL_NONE, EGL_NONE, &config);
387 if (!err)
388 goto success;
389
Mathias Agopian875d8e12013-06-07 15:35:48 -0700390 // this didn't work, probably because we're on the emulator...
391 // try a simplified query
392 ALOGW("no suitable EGLConfig found, trying a simpler query");
393 attribs.remove(EGL_RENDERABLE_TYPE);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700394 attribs.remove(EGL_FRAMEBUFFER_TARGET_ANDROID);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700395 attribs.remove(EGL_RECORDABLE_ANDROID);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700396 attribs.remove(EGL_RED_SIZE);
397 attribs.remove(EGL_GREEN_SIZE);
398 attribs.remove(EGL_BLUE_SIZE);
399 err = selectConfigForAttribute(display, attribs,
400 EGL_NATIVE_VISUAL_ID, nativeVisualId, &config);
Jesse Hallf21cffa2012-09-19 21:00:49 -0700401 if (!err)
402 goto success;
403
404 // this EGL is too lame for Android
Mathias Agopian875d8e12013-06-07 15:35:48 -0700405 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
Jesse Hallf21cffa2012-09-19 21:00:49 -0700406 return 0;
407
408success:
409 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700410 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700411 return config;
412}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800413
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700414void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700415
Mathias Agopiana4912602012-07-12 14:25:33 -0700416 ALOGI( "SurfaceFlinger's main thread ready to run. "
417 "Initializing graphics H/W...");
418
Jesse Hall692c7232012-11-08 15:41:56 -0800419 Mutex::Autolock _l(mStateLock);
420
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700421 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700422 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
423 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700424
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700425 // Initialize the H/W composer object. There may or may not be an
426 // actual hardware composer underneath.
427 mHwc = new HWComposer(this,
428 *static_cast<HWComposer::EventHandler *>(this));
429
Mathias Agopian875d8e12013-06-07 15:35:48 -0700430 // initialize the config and context (can't fail)
431 mEGLConfig = selectEGLConfig(mEGLDisplay, mHwc->getVisualID());
432
433 // print some debugging info
434 EGLint r,g,b,a;
435 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_RED_SIZE, &r);
436 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_GREEN_SIZE, &g);
437 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_BLUE_SIZE, &b);
438 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_ALPHA_SIZE, &a);
439 ALOGI("EGL informations:");
440 ALOGI("vendor : %s", eglQueryString(mEGLDisplay, EGL_VENDOR));
441 ALOGI("version : %s", eglQueryString(mEGLDisplay, EGL_VERSION));
442 ALOGI("extensions: %s", eglQueryString(mEGLDisplay, EGL_EXTENSIONS));
443 ALOGI("Client API: %s", eglQueryString(mEGLDisplay, EGL_CLIENT_APIS)?:"Not Supported");
444 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
445
446 // get a RenderEngine for the given display / config (can't fail)
447 mRenderEngine = RenderEngine::create(mEGLDisplay, mEGLConfig);
448
449 // retrieve the EGL context that was selected/created
450 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700451
Mathias Agopianed985572013-03-22 00:24:39 -0700452 // figure out which format we got
453 eglGetConfigAttrib(mEGLDisplay, mEGLConfig,
454 EGL_NATIVE_VISUAL_ID, &mEGLNativeVisualId);
455
Mathias Agopianda27af92012-09-13 18:17:13 -0700456 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
457 "couldn't create EGLContext");
458
Mathias Agopiancde87a32012-09-13 14:09:01 -0700459 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700460 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700461 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700462 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700463 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700464 // All non-virtual displays are currently considered secure.
465 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800466 createBuiltinDisplayLocked(type);
467 wp<IBinder> token = mBuiltinDisplays[i];
468
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700469 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
470 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i, bq);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700471 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700472 type, allocateHwcDisplayId(type), isSecure, token,
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700473 fbs, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700474 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700475 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700476 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700477 // for displays other than the main display, so we always
478 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700479 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700480 hw->acquireScreen();
481 }
482 mDisplays.add(token, hw);
483 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700484 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700485
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700486 // make the GLContext current so that we can create textures when creating Layers
487 // (which may happens before we render something)
488 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
489
Mathias Agopian028508c2012-07-25 21:12:12 -0700490 // start the EventThread
491 mEventThread = new EventThread(this);
492 mEventQueue.setEventThread(mEventThread);
493
Mathias Agopian92a979a2012-08-02 18:32:23 -0700494 // initialize our drawing state
495 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700496
Andy McFadden13a082e2012-08-24 10:16:42 -0700497 // set initial conditions (e.g. unblank default device)
498 initializeDisplays();
499
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700500 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700501 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800502}
503
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700504int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700505 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700506 type : mHwc->allocateDisplayId();
507}
508
Mathias Agopiana67e4182012-06-19 17:26:12 -0700509void SurfaceFlinger::startBootAnim() {
510 // start boot animation
511 property_set("service.bootanim.exit", "0");
512 property_set("ctl.start", "bootanim");
513}
514
Mathias Agopian875d8e12013-06-07 15:35:48 -0700515size_t SurfaceFlinger::getMaxTextureSize() const {
516 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700517}
518
Mathias Agopian875d8e12013-06-07 15:35:48 -0700519size_t SurfaceFlinger::getMaxViewportDims() const {
520 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700521}
522
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800523// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800524
Jamie Gennis582270d2011-08-17 18:19:00 -0700525bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800526 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800527 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800528 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700529 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800530}
531
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700532status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800533 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700534 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800535 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700536 type = i;
537 break;
538 }
539 }
540
541 if (type < 0) {
542 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700543 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700544
545 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700546 float xdpi = hwc.getDpiX(type);
547 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700548
549 // TODO: Not sure if display density should handled by SF any longer
550 class Density {
551 static int getDensityFromProperty(char const* propName) {
552 char property[PROPERTY_VALUE_MAX];
553 int density = 0;
554 if (property_get(propName, property, NULL) > 0) {
555 density = atoi(property);
556 }
557 return density;
558 }
559 public:
560 static int getEmuDensity() {
561 return getDensityFromProperty("qemu.sf.lcd_density"); }
562 static int getBuildDensity() {
563 return getDensityFromProperty("ro.sf.lcd_density"); }
564 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700565
566 if (type == DisplayDevice::DISPLAY_PRIMARY) {
567 // The density of the device is provided by a build property
568 float density = Density::getBuildDensity() / 160.0f;
569 if (density == 0) {
570 // the build doesn't provide a density -- this is wrong!
571 // use xdpi instead
572 ALOGE("ro.sf.lcd_density must be defined as a build property");
573 density = xdpi / 160.0f;
574 }
575 if (Density::getEmuDensity()) {
576 // if "qemu.sf.lcd_density" is specified, it overrides everything
577 xdpi = ydpi = density = Density::getEmuDensity();
578 density /= 160.0f;
579 }
580 info->density = density;
581
582 // TODO: this needs to go away (currently needed only by webkit)
583 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
584 info->orientation = hw->getOrientation();
Mathias Agopian1604f772012-09-18 21:54:42 -0700585 } else {
586 // TODO: where should this value come from?
587 static const int TV_DENSITY = 213;
588 info->density = TV_DENSITY / 160.0f;
589 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700590 }
591
Mathias Agopian1604f772012-09-18 21:54:42 -0700592 info->w = hwc.getWidth(type);
593 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700594 info->xdpi = xdpi;
595 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700596 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700597
598 // All non-virtual displays are currently considered secure.
599 info->secure = true;
600
Mathias Agopian888c8222012-08-04 21:10:38 -0700601 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700602}
603
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800604// ----------------------------------------------------------------------------
605
606sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800607 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700608}
609
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800610// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800611
612void SurfaceFlinger::waitForEvent() {
613 mEventQueue.waitMessage();
614}
615
616void SurfaceFlinger::signalTransaction() {
617 mEventQueue.invalidate();
618}
619
620void SurfaceFlinger::signalLayerUpdate() {
621 mEventQueue.invalidate();
622}
623
624void SurfaceFlinger::signalRefresh() {
625 mEventQueue.refresh();
626}
627
628status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
629 nsecs_t reltime, uint32_t flags) {
630 return mEventQueue.postMessage(msg, reltime);
631}
632
633status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
634 nsecs_t reltime, uint32_t flags) {
635 status_t res = mEventQueue.postMessage(msg, reltime);
636 if (res == NO_ERROR) {
637 msg->wait();
638 }
639 return res;
640}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800641
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700642void SurfaceFlinger::run() {
643#if defined(HAVE_PTHREADS)
644 setpriority(PRIO_PROCESS, 0, PRIORITY_URGENT_DISPLAY);
645#endif
646 do {
647 waitForEvent();
648 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800649}
650
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700651void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700652 if (mEventThread == NULL) {
653 // This is a temporary workaround for b/7145521. A non-null pointer
654 // does not mean EventThread has finished initializing, so this
655 // is not a correct fix.
656 ALOGW("WARNING: EventThread not started, ignoring vsync");
657 return;
658 }
Jesse Hall9e663de2013-08-16 14:28:37 -0700659 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700660 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700661 mEventThread->onVSyncReceived(type, timestamp);
662 }
663}
664
665void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
666 if (mEventThread == NULL) {
667 // This is a temporary workaround for b/7145521. A non-null pointer
668 // does not mean EventThread has finished initializing, so this
669 // is not a correct fix.
670 ALOGW("WARNING: EventThread not started, ignoring hotplug");
671 return;
672 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700673
Jesse Hall9e663de2013-08-16 14:28:37 -0700674 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700675 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800676 if (connected) {
677 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700678 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800679 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
680 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700681 }
682 setTransactionFlags(eDisplayTransactionNeeded);
683
Andy McFadden9e9689c2012-10-10 18:17:51 -0700684 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700685 }
Mathias Agopian86303202012-07-24 22:46:10 -0700686}
687
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700688void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
689 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700690}
691
Mathias Agopian4fec8732012-06-29 14:12:52 -0700692void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800693 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800694 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700695 case MessageQueue::TRANSACTION:
696 handleMessageTransaction();
697 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700698 case MessageQueue::INVALIDATE:
699 handleMessageTransaction();
700 handleMessageInvalidate();
701 signalRefresh();
702 break;
703 case MessageQueue::REFRESH:
704 handleMessageRefresh();
705 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800706 }
707}
708
Mathias Agopian4fec8732012-06-29 14:12:52 -0700709void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700710 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700711 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700712 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700713 }
714}
715
716void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700717 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700718 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700719}
720
721void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700722 ATRACE_CALL();
723 preComposition();
724 rebuildLayerStacks();
725 setUpHWComposer();
726 doDebugFlashRegions();
727 doComposition();
728 postComposition();
729}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700730
Mathias Agopiancd60f992012-08-16 16:28:27 -0700731void SurfaceFlinger::doDebugFlashRegions()
732{
733 // is debugging enabled
734 if (CC_LIKELY(!mDebugRegion))
735 return;
736
737 const bool repaintEverything = mRepaintEverything;
738 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
739 const sp<DisplayDevice>& hw(mDisplays[dpy]);
740 if (hw->canDraw()) {
741 // transform the dirty region into this screen's coordinate space
742 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
743 if (!dirtyRegion.isEmpty()) {
744 // redraw the whole screen
745 doComposeSurfaces(hw, Region(hw->bounds()));
746
747 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700748 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700749 RenderEngine& engine(getRenderEngine());
750 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
751
Mathias Agopiancd60f992012-08-16 16:28:27 -0700752 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700753 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700754 }
755 }
756 }
757
758 postFramebuffer();
759
760 if (mDebugRegion > 1) {
761 usleep(mDebugRegion * 1000);
762 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700763
764 HWComposer& hwc(getHwComposer());
765 if (hwc.initCheck() == NO_ERROR) {
766 status_t err = hwc.prepare();
767 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
768 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700769}
770
771void SurfaceFlinger::preComposition()
772{
773 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700774 const LayerVector& layers(mDrawingState.layersSortedByZ);
775 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700776 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700777 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700778 needExtraInvalidate = true;
779 }
780 }
781 if (needExtraInvalidate) {
782 signalLayerUpdate();
783 }
784}
785
786void SurfaceFlinger::postComposition()
787{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700788 const LayerVector& layers(mDrawingState.layersSortedByZ);
789 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700790 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700791 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700792 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800793
794 if (mAnimCompositionPending) {
795 mAnimCompositionPending = false;
796
797 const HWComposer& hwc = getHwComposer();
798 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800799 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800800 mAnimFrameTracker.setActualPresentFence(presentFence);
801 } else {
802 // The HWC doesn't support present fences, so use the refresh
803 // timestamp instead.
804 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
805 mAnimFrameTracker.setActualPresentTime(presentTime);
806 }
807 mAnimFrameTracker.advanceFrame();
808 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700809}
810
811void SurfaceFlinger::rebuildLayerStacks() {
812 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700813 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700814 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700815 mVisibleRegionsDirty = false;
816 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700817
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700818 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700819 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700820 Region opaqueRegion;
821 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800822 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700823 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700824 const Transform& tr(hw->getTransform());
825 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700826 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700827 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700828 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700829
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700830 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700831 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700832 const sp<Layer>& layer(layers[i]);
833 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700834 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700835 Region drawRegion(tr.transform(
836 layer->visibleNonTransparentRegion));
837 drawRegion.andSelf(bounds);
838 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700839 layersSortedByZ.add(layer);
840 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700841 }
842 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700843 }
Mathias Agopian42977342012-08-05 00:40:46 -0700844 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700845 hw->undefinedRegion.set(bounds);
846 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
847 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700848 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700849 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700850}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700851
Mathias Agopiancd60f992012-08-16 16:28:27 -0700852void SurfaceFlinger::setUpHWComposer() {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700853 HWComposer& hwc(getHwComposer());
854 if (hwc.initCheck() == NO_ERROR) {
855 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700856 if (CC_UNLIKELY(mHwWorkListDirty)) {
857 mHwWorkListDirty = false;
858 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
859 sp<const DisplayDevice> hw(mDisplays[dpy]);
860 const int32_t id = hw->getHwcDisplayId();
861 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800862 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700863 hw->getVisibleLayersSortedByZ());
864 const size_t count = currentLayers.size();
865 if (hwc.createWorkList(id, count) == NO_ERROR) {
866 HWComposer::LayerListIterator cur = hwc.begin(id);
867 const HWComposer::LayerListIterator end = hwc.end(id);
868 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800869 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700870 layer->setGeometry(hw, *cur);
871 if (mDebugDisableHWC || mDebugRegion) {
872 cur->setSkip(true);
873 }
874 }
875 }
876 }
877 }
878 }
879
880 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700881 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700882 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700883 const int32_t id = hw->getHwcDisplayId();
884 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800885 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700886 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700887 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700888 HWComposer::LayerListIterator cur = hwc.begin(id);
889 const HWComposer::LayerListIterator end = hwc.end(id);
890 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
891 /*
892 * update the per-frame h/w composer data for each layer
893 * and build the transparent region of the FB
894 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800895 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700896 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700897 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700898 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700899 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700900
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700901 status_t err = hwc.prepare();
902 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700903
904 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
905 sp<const DisplayDevice> hw(mDisplays[dpy]);
906 hw->prepareFrame(hwc);
907 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700908 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700909}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700910
Mathias Agopiancd60f992012-08-16 16:28:27 -0700911void SurfaceFlinger::doComposition() {
912 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700913 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700914 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700915 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700916 if (hw->canDraw()) {
917 // transform the dirty region into this screen's coordinate space
918 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800919
920 // repaint the framebuffer (if needed)
921 doDisplayComposition(hw, dirtyRegion);
922
Mathias Agopiancd60f992012-08-16 16:28:27 -0700923 hw->dirtyRegion.clear();
924 hw->flip(hw->swapRegion);
925 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700926 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700927 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700928 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700929 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700930 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700931}
932
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800933void SurfaceFlinger::postFramebuffer()
934{
Mathias Agopian841cde52012-03-01 15:44:37 -0800935 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800936
Mathias Agopiana44b0412011-10-16 18:46:35 -0700937 const nsecs_t now = systemTime();
938 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700939
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700940 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700941 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700942 if (!hwc.supportsFramebufferTarget()) {
943 // EGL spec says:
944 // "surface must be bound to the calling thread's current context,
945 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700946 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700947 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700948 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700949 }
950
Mathias Agopian92a979a2012-08-02 18:32:23 -0700951 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700952 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800953 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700954 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700955 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700956 int32_t id = hw->getHwcDisplayId();
957 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700958 HWComposer::LayerListIterator cur = hwc.begin(id);
959 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700960 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700961 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700962 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700963 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700964 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700965 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700966 }
Jesse Hallef194142012-06-14 14:45:17 -0700967 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800968 }
969
Mathias Agopiana44b0412011-10-16 18:46:35 -0700970 mLastSwapBufferTime = systemTime() - now;
971 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700972
973 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
974 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
975 logFrameStats();
976 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800977}
978
Mathias Agopian87baae12012-07-31 12:38:26 -0700979void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800980{
Mathias Agopian841cde52012-03-01 15:44:37 -0800981 ATRACE_CALL();
982
Mathias Agopian7cc6df52013-06-05 14:30:54 -0700983 // here we keep a copy of the drawing state (that is the state that's
984 // going to be overwritten by handleTransactionLocked()) outside of
985 // mStateLock so that the side-effects of the State assignment
986 // don't happen with mStateLock held (which can cause deadlocks).
987 State drawingState(mDrawingState);
988
Mathias Agopianca4d3602011-05-19 15:38:14 -0700989 Mutex::Autolock _l(mStateLock);
990 const nsecs_t now = systemTime();
991 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800992
Mathias Agopianca4d3602011-05-19 15:38:14 -0700993 // Here we're guaranteed that some transaction flags are set
994 // so we can call handleTransactionLocked() unconditionally.
995 // We call getTransactionFlags(), which will also clear the flags,
996 // with mStateLock held to guarantee that mCurrentState won't change
997 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700998
Mathias Agopiane57f2922012-08-09 16:29:12 -0700999 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001000 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001001
Mathias Agopianca4d3602011-05-19 15:38:14 -07001002 mLastTransactionTime = systemTime() - now;
1003 mDebugInTransaction = 0;
1004 invalidateHwcGeometry();
1005 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001006}
1007
Mathias Agopian87baae12012-07-31 12:38:26 -07001008void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001009{
1010 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011 const size_t count = currentLayers.size();
1012
1013 /*
1014 * Traversal of the children
1015 * (perform the transaction for each of them if needed)
1016 */
1017
Mathias Agopian3559b072012-08-15 13:46:03 -07001018 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001019 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001020 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1022 if (!trFlags) continue;
1023
1024 const uint32_t flags = layer->doTransaction(0);
1025 if (flags & Layer::eVisibleRegion)
1026 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001027 }
1028 }
1029
1030 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001031 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032 */
1033
Mathias Agopiane57f2922012-08-09 16:29:12 -07001034 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001035 // here we take advantage of Vector's copy-on-write semantics to
1036 // improve performance by skipping the transaction entirely when
1037 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001038 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1039 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001040 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001042 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001043 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001044
1045 // find the displays that were removed
1046 // (ie: in drawing state but not in current state)
1047 // also handle displays that changed
1048 // (ie: displays that are in both lists)
1049 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001050 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1051 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001052 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001053 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001054 // Call makeCurrent() on the primary display so we can
1055 // be sure that nothing associated with this display
1056 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001057 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001058 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001059 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1060 if (hw != NULL)
1061 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001062 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001063 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001064 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001065 } else {
1066 ALOGW("trying to remove the main display");
1067 }
1068 } else {
1069 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001070 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001071 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001072 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001073 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001074 // recreating the DisplayDevice, so we just remove it
1075 // from the drawing state, so that it get re-added
1076 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001077 sp<DisplayDevice> hw(getDisplayDevice(display));
1078 if (hw != NULL)
1079 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001080 mDisplays.removeItem(display);
1081 mDrawingState.displays.removeItemsAt(i);
1082 dc--; i--;
1083 // at this point we must loop to the next item
1084 continue;
1085 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001086
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001087 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001088 if (disp != NULL) {
1089 if (state.layerStack != draw[i].layerStack) {
1090 disp->setLayerStack(state.layerStack);
1091 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001092 if ((state.orientation != draw[i].orientation)
1093 || (state.viewport != draw[i].viewport)
1094 || (state.frame != draw[i].frame))
1095 {
1096 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001097 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001098 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001099 }
1100 }
1101 }
1102
1103 // find displays that were added
1104 // (ie: in current state but not in drawing state)
1105 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001106 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001107 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001108
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001109 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001110 sp<IGraphicBufferProducer> producer;
1111 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
1112
Jesse Hall02d86562013-03-25 14:43:23 -07001113 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001114 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001115 // Virtual displays without a surface are dormant:
1116 // they have external state (layer stack, projection,
1117 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001118 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001119
Jesse Hall02d86562013-03-25 14:43:23 -07001120 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001121 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1122 *mHwc, hwcDisplayId, state.surface, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001123 state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001124
1125 dispSurface = vds;
1126 if (hwcDisplayId >= 0) {
1127 producer = vds;
1128 } else {
1129 // There won't be any interaction with HWC for this virtual display,
1130 // so the GLES driver can pass buffers directly to the sink.
1131 producer = state.surface;
1132 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001133 }
1134 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001135 ALOGE_IF(state.surface!=NULL,
1136 "adding a supported display, but rendering "
1137 "surface is provided (%p), ignoring it",
1138 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001139 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001140 // for supported (by hwc) displays we provide our
1141 // own rendering surface
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001142 dispSurface = new FramebufferSurface(*mHwc, state.type, bq);
1143 producer = bq;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001144 }
1145
1146 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001147 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001148 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001149 state.type, hwcDisplayId, state.isSecure,
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001150 display, dispSurface, producer, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001151 hw->setLayerStack(state.layerStack);
1152 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001153 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001154 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001155 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001156 if (state.isVirtualDisplay()) {
1157 if (hwcDisplayId >= 0) {
1158 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1159 hw->getWidth(), hw->getHeight(),
1160 hw->getFormat());
1161 }
1162 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001163 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001164 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001165 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001166 }
1167 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001168 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001169 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001170
Mathias Agopian84300952012-11-21 16:02:13 -08001171 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1172 // The transform hint might have changed for some layers
1173 // (either because a display has changed, or because a layer
1174 // as changed).
1175 //
1176 // Walk through all the layers in currentLayers,
1177 // and update their transform hint.
1178 //
1179 // If a layer is visible only on a single display, then that
1180 // display is used to calculate the hint, otherwise we use the
1181 // default display.
1182 //
1183 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1184 // the hint is set before we acquire a buffer from the surface texture.
1185 //
1186 // NOTE: layer transactions have taken place already, so we use their
1187 // drawing state. However, SurfaceFlinger's own transaction has not
1188 // happened yet, so we must use the current state layer list
1189 // (soon to become the drawing state list).
1190 //
1191 sp<const DisplayDevice> disp;
1192 uint32_t currentlayerStack = 0;
1193 for (size_t i=0; i<count; i++) {
1194 // NOTE: we rely on the fact that layers are sorted by
1195 // layerStack first (so we don't have to traverse the list
1196 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001197 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001198 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001199 if (i==0 || currentlayerStack != layerStack) {
1200 currentlayerStack = layerStack;
1201 // figure out if this layerstack is mirrored
1202 // (more than one display) if so, pick the default display,
1203 // if not, pick the only display it's on.
1204 disp.clear();
1205 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1206 sp<const DisplayDevice> hw(mDisplays[dpy]);
1207 if (hw->getLayerStack() == currentlayerStack) {
1208 if (disp == NULL) {
1209 disp = hw;
1210 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001211 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001212 break;
1213 }
1214 }
1215 }
1216 }
Chet Haase91d25932013-04-11 15:24:55 -07001217 if (disp == NULL) {
1218 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1219 // redraw after transform hint changes. See bug 8508397.
1220
1221 // could be null when this layer is using a layerStack
1222 // that is not visible on any display. Also can occur at
1223 // screen off/on times.
1224 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001225 }
Chet Haase91d25932013-04-11 15:24:55 -07001226 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001227 }
1228 }
1229
1230
Mathias Agopian3559b072012-08-15 13:46:03 -07001231 /*
1232 * Perform our own transaction if needed
1233 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001234
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001235 const LayerVector& layers(mDrawingState.layersSortedByZ);
1236 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001237 // layers have been added
1238 mVisibleRegionsDirty = true;
1239 }
1240
1241 // some layers might have been removed, so
1242 // we need to update the regions they're exposing.
1243 if (mLayersRemoved) {
1244 mLayersRemoved = false;
1245 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001246 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001247 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001248 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001249 if (currentLayers.indexOf(layer) < 0) {
1250 // this layer is not visible anymore
1251 // TODO: we could traverse the tree from front to back and
1252 // compute the actual visible region
1253 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001254 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001255 Region visibleReg = s.transform.transform(
1256 Region(Rect(s.active.w, s.active.h)));
1257 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001258 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001259 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001260 }
1261
1262 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001263}
1264
1265void SurfaceFlinger::commitTransaction()
1266{
1267 if (!mLayersPendingRemoval.isEmpty()) {
1268 // Notify removed layers now that they can't be drawn from
1269 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1270 mLayersPendingRemoval[i]->onRemoved();
1271 }
1272 mLayersPendingRemoval.clear();
1273 }
1274
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001275 // If this transaction is part of a window animation then the next frame
1276 // we composite should be considered an animation as well.
1277 mAnimCompositionPending = mAnimTransactionPending;
1278
Mathias Agopian4fec8732012-06-29 14:12:52 -07001279 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001280 mTransactionPending = false;
1281 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001282 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001283}
1284
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001285void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001286 const LayerVector& currentLayers, uint32_t layerStack,
1287 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001288{
Mathias Agopian841cde52012-03-01 15:44:37 -08001289 ATRACE_CALL();
1290
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001291 Region aboveOpaqueLayers;
1292 Region aboveCoveredLayers;
1293 Region dirty;
1294
Mathias Agopian87baae12012-07-31 12:38:26 -07001295 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001296
1297 size_t i = currentLayers.size();
1298 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001299 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001300
1301 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001302 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303
Jesse Hall01e29052013-02-19 16:13:35 -08001304 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001305 if (s.layerStack != layerStack)
1306 continue;
1307
Mathias Agopianab028732010-03-16 16:41:46 -07001308 /*
1309 * opaqueRegion: area of a surface that is fully opaque.
1310 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001311 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001312
1313 /*
1314 * visibleRegion: area of a surface that is visible on screen
1315 * and not fully transparent. This is essentially the layer's
1316 * footprint minus the opaque regions above it.
1317 * Areas covered by a translucent surface are considered visible.
1318 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001319 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001320
1321 /*
1322 * coveredRegion: area of a surface that is covered by all
1323 * visible regions above it (which includes the translucent areas).
1324 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001325 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001326
Jesse Halla8026d22012-09-25 13:25:04 -07001327 /*
1328 * transparentRegion: area of a surface that is hinted to be completely
1329 * transparent. This is only used to tell when the layer has no visible
1330 * non-transparent regions and can be removed from the layer list. It
1331 * does not affect the visibleRegion of this layer or any layers
1332 * beneath it. The hint may not be correct if apps don't respect the
1333 * SurfaceView restrictions (which, sadly, some don't).
1334 */
1335 Region transparentRegion;
1336
Mathias Agopianab028732010-03-16 16:41:46 -07001337
1338 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001339 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001340 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001341 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001342 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001343 if (!visibleRegion.isEmpty()) {
1344 // Remove the transparent area from the visible region
1345 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001346 const Transform tr(s.transform);
1347 if (tr.transformed()) {
1348 if (tr.preserveRects()) {
1349 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001350 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001351 } else {
1352 // transformation too complex, can't do the
1353 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001354 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001355 }
1356 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001357 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001358 }
Mathias Agopianab028732010-03-16 16:41:46 -07001359 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001360
Mathias Agopianab028732010-03-16 16:41:46 -07001361 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001362 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001363 if (s.alpha==255 && !translucent &&
1364 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1365 // the opaque region is the layer's footprint
1366 opaqueRegion = visibleRegion;
1367 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001368 }
1369 }
1370
Mathias Agopianab028732010-03-16 16:41:46 -07001371 // Clip the covered region to the visible region
1372 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1373
1374 // Update aboveCoveredLayers for next (lower) layer
1375 aboveCoveredLayers.orSelf(visibleRegion);
1376
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001377 // subtract the opaque region covered by the layers above us
1378 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001379
1380 // compute this layer's dirty region
1381 if (layer->contentDirty) {
1382 // we need to invalidate the whole region
1383 dirty = visibleRegion;
1384 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001385 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386 layer->contentDirty = false;
1387 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001388 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001389 * the exposed region consists of two components:
1390 * 1) what's VISIBLE now and was COVERED before
1391 * 2) what's EXPOSED now less what was EXPOSED before
1392 *
1393 * note that (1) is conservative, we start with the whole
1394 * visible region but only keep what used to be covered by
1395 * something -- which mean it may have been exposed.
1396 *
1397 * (2) handles areas that were not covered by anything but got
1398 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001399 */
Mathias Agopianab028732010-03-16 16:41:46 -07001400 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001401 const Region oldVisibleRegion = layer->visibleRegion;
1402 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001403 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1404 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001405 }
1406 dirty.subtractSelf(aboveOpaqueLayers);
1407
1408 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001409 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001410
Mathias Agopianab028732010-03-16 16:41:46 -07001411 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001412 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001413
Jesse Halla8026d22012-09-25 13:25:04 -07001414 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415 layer->setVisibleRegion(visibleRegion);
1416 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001417 layer->setVisibleNonTransparentRegion(
1418 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001419 }
1420
Mathias Agopian87baae12012-07-31 12:38:26 -07001421 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001422}
1423
Mathias Agopian87baae12012-07-31 12:38:26 -07001424void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1425 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001426 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001427 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1428 if (hw->getLayerStack() == layerStack) {
1429 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001430 }
1431 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001432}
1433
1434void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001436 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001437
Mathias Agopian4fec8732012-06-29 14:12:52 -07001438 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001439 const LayerVector& layers(mDrawingState.layersSortedByZ);
1440 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001441 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001442 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001443 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001444 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001445 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001446 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001447
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001448 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001449}
1450
Mathias Agopianad456f92011-01-13 17:53:01 -08001451void SurfaceFlinger::invalidateHwcGeometry()
1452{
1453 mHwWorkListDirty = true;
1454}
1455
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001456
Mathias Agopiancd60f992012-08-16 16:28:27 -07001457void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001458 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001459{
Mathias Agopian87baae12012-07-31 12:38:26 -07001460 Region dirtyRegion(inDirtyRegion);
1461
Mathias Agopianb8a55602009-06-26 19:06:36 -07001462 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001463 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001464
Mathias Agopian42977342012-08-05 00:40:46 -07001465 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001466 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001467 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1468 // takes a rectangle, we must make sure to update that whole
1469 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001470 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001471 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001472 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001473 // We need to redraw the rectangle that will be updated
1474 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001475 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001476 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001477 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001478 } else {
1479 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001480 dirtyRegion.set(hw->bounds());
1481 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001482 }
1483 }
1484
Mathias Agopiancd60f992012-08-16 16:28:27 -07001485 doComposeSurfaces(hw, dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001487 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001488 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001489
1490 // swap buffers (presentation)
1491 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001492}
1493
Mathias Agopiancd60f992012-08-16 16:28:27 -07001494void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001495{
Mathias Agopian3f844832013-08-07 21:24:32 -07001496 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001497 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001498 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001499 HWComposer::LayerListIterator cur = hwc.begin(id);
1500 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001501
Mathias Agopian85d751c2012-08-29 16:59:24 -07001502 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1503 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001504 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001505 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1506 hw->getDisplayName().string());
1507 return;
1508 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001509
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001510 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001511 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001512 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001513 // when using overlays, we assume a fully transparent framebuffer
1514 // NOTE: we could reduce how much we need to clear, for instance
1515 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001516 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001517 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001518 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001519 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001520 // we start with the whole screen area
1521 const Region bounds(hw->getBounds());
1522
1523 // we remove the scissor part
1524 // we're left with the letterbox region
1525 // (common case is that letterbox ends-up being empty)
1526 const Region letterbox(bounds.subtract(hw->getScissor()));
1527
1528 // compute the area to clear
1529 Region region(hw->undefinedRegion.merge(letterbox));
1530
1531 // but limit it to the dirty region
1532 region.andSelf(dirty);
1533
Mathias Agopianb9494d52012-04-18 02:28:45 -07001534 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001535 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001536 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001537 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001538 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001539 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001540
Mathias Agopian766dc492012-10-30 18:08:06 -07001541 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1542 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001543 // scissor on the main display. It should never be needed
1544 // anyways (though in theory it could since the API allows it).
1545 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001546 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001547 if (scissor != bounds) {
1548 // scissor doesn't match the screen's dimensions, so we
1549 // need to clear everything outside of it and enable
1550 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001551
Mathias Agopianf45c5102012-10-24 16:29:17 -07001552 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001553 const uint32_t height = hw->getHeight();
1554 engine.setScissor(scissor.left, height - scissor.bottom,
1555 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001556 }
1557 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001558 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001559
Mathias Agopian85d751c2012-08-29 16:59:24 -07001560 /*
1561 * and then, render the layers targeted at the framebuffer
1562 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001563
Mathias Agopian13127d82013-03-05 17:47:11 -08001564 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001565 const size_t count = layers.size();
1566 const Transform& tr = hw->getTransform();
1567 if (cur != end) {
1568 // we're using h/w composer
1569 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001570 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001571 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001572 if (!clip.isEmpty()) {
1573 switch (cur->getCompositionType()) {
1574 case HWC_OVERLAY: {
1575 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1576 && i
1577 && layer->isOpaque()
1578 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001579 // never clear the very first layer since we're
1580 // guaranteed the FB is already cleared
1581 layer->clearWithOpenGL(hw, clip);
1582 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001583 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001584 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001585 case HWC_FRAMEBUFFER: {
1586 layer->draw(hw, clip);
1587 break;
1588 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001589 case HWC_FRAMEBUFFER_TARGET: {
1590 // this should not happen as the iterator shouldn't
1591 // let us get there.
1592 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1593 break;
1594 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001595 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001596 }
1597 layer->setAcquireFence(hw, *cur);
1598 }
1599 } else {
1600 // we're not using h/w composer
1601 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001602 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001603 const Region clip(dirty.intersect(
1604 tr.transform(layer->visibleRegion)));
1605 if (!clip.isEmpty()) {
1606 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001607 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001608 }
1609 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001610
1611 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001612 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001613}
1614
Mathias Agopian3f844832013-08-07 21:24:32 -07001615void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001616 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001617 RenderEngine& engine(getRenderEngine());
1618 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001619}
1620
Mathias Agopianac9fa422013-02-11 16:40:36 -08001621void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1622 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001623 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001624 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001625{
Mathias Agopian96f08192010-06-02 23:28:45 -07001626 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001627 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001628
Mathias Agopian96f08192010-06-02 23:28:45 -07001629 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001630 Mutex::Autolock _l(mStateLock);
1631 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001632 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001633}
1634
Mathias Agopian3f844832013-08-07 21:24:32 -07001635status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001636 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001637 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001638 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001639 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001640 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001641 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001642 return NO_ERROR;
1643 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001644 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001645}
1646
Mathias Agopian3f844832013-08-07 21:24:32 -07001647uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001648 return android_atomic_release_load(&mTransactionFlags);
1649}
1650
Mathias Agopian3f844832013-08-07 21:24:32 -07001651uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001652 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1653}
1654
Mathias Agopian3f844832013-08-07 21:24:32 -07001655uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001656 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1657 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001658 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001659 }
1660 return old;
1661}
1662
Mathias Agopian8b33f032012-07-24 20:43:54 -07001663void SurfaceFlinger::setTransactionState(
1664 const Vector<ComposerState>& state,
1665 const Vector<DisplayState>& displays,
1666 uint32_t flags)
1667{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001668 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001669 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001670 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001671
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001672 if (flags & eAnimation) {
1673 // For window updates that are part of an animation we must wait for
1674 // previous animation "frames" to be handled.
1675 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001676 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001677 if (CC_UNLIKELY(err != NO_ERROR)) {
1678 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001679 // caller after a few seconds.
1680 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1681 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001682 mAnimTransactionPending = false;
1683 break;
1684 }
1685 }
1686 }
1687
Mathias Agopiane57f2922012-08-09 16:29:12 -07001688 size_t count = displays.size();
1689 for (size_t i=0 ; i<count ; i++) {
1690 const DisplayState& s(displays[i]);
1691 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001692 }
1693
Mathias Agopiane57f2922012-08-09 16:29:12 -07001694 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001695 for (size_t i=0 ; i<count ; i++) {
1696 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001697 // Here we need to check that the interface we're given is indeed
1698 // one of our own. A malicious client could give us a NULL
1699 // IInterface, or one of its own or even one of our own but a
1700 // different type. All these situations would cause us to crash.
1701 //
1702 // NOTE: it would be better to use RTTI as we could directly check
1703 // that we have a Client*. however, RTTI is disabled in Android.
1704 if (s.client != NULL) {
1705 sp<IBinder> binder = s.client->asBinder();
1706 if (binder != NULL) {
1707 String16 desc(binder->getInterfaceDescriptor());
1708 if (desc == ISurfaceComposerClient::descriptor) {
1709 sp<Client> client( static_cast<Client *>(s.client.get()) );
1710 transactionFlags |= setClientStateLocked(client, s.state);
1711 }
1712 }
1713 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001714 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001715
Mathias Agopian386aa982011-11-07 21:58:03 -08001716 if (transactionFlags) {
1717 // this triggers the transaction
1718 setTransactionFlags(transactionFlags);
1719
1720 // if this is a synchronous transaction, wait for it to take effect
1721 // before returning.
1722 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001723 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001724 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001725 if (flags & eAnimation) {
1726 mAnimTransactionPending = true;
1727 }
1728 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001729 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1730 if (CC_UNLIKELY(err != NO_ERROR)) {
1731 // just in case something goes wrong in SF, return to the
1732 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001733 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1734 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001735 break;
1736 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001737 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001738 }
1739}
1740
Mathias Agopiane57f2922012-08-09 16:29:12 -07001741uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1742{
Jesse Hall9a143922012-10-04 16:29:19 -07001743 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1744 if (dpyIdx < 0)
1745 return 0;
1746
Mathias Agopiane57f2922012-08-09 16:29:12 -07001747 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001748 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001749 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001750 const uint32_t what = s.what;
1751 if (what & DisplayState::eSurfaceChanged) {
1752 if (disp.surface->asBinder() != s.surface->asBinder()) {
1753 disp.surface = s.surface;
1754 flags |= eDisplayTransactionNeeded;
1755 }
1756 }
1757 if (what & DisplayState::eLayerStackChanged) {
1758 if (disp.layerStack != s.layerStack) {
1759 disp.layerStack = s.layerStack;
1760 flags |= eDisplayTransactionNeeded;
1761 }
1762 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001763 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001764 if (disp.orientation != s.orientation) {
1765 disp.orientation = s.orientation;
1766 flags |= eDisplayTransactionNeeded;
1767 }
1768 if (disp.frame != s.frame) {
1769 disp.frame = s.frame;
1770 flags |= eDisplayTransactionNeeded;
1771 }
1772 if (disp.viewport != s.viewport) {
1773 disp.viewport = s.viewport;
1774 flags |= eDisplayTransactionNeeded;
1775 }
1776 }
1777 }
1778 return flags;
1779}
1780
1781uint32_t SurfaceFlinger::setClientStateLocked(
1782 const sp<Client>& client,
1783 const layer_state_t& s)
1784{
1785 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001786 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001787 if (layer != 0) {
1788 const uint32_t what = s.what;
1789 if (what & layer_state_t::ePositionChanged) {
1790 if (layer->setPosition(s.x, s.y))
1791 flags |= eTraversalNeeded;
1792 }
1793 if (what & layer_state_t::eLayerChanged) {
1794 // NOTE: index needs to be calculated before we update the state
1795 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1796 if (layer->setLayer(s.z)) {
1797 mCurrentState.layersSortedByZ.removeAt(idx);
1798 mCurrentState.layersSortedByZ.add(layer);
1799 // we need traversal (state changed)
1800 // AND transaction (list changed)
1801 flags |= eTransactionNeeded|eTraversalNeeded;
1802 }
1803 }
1804 if (what & layer_state_t::eSizeChanged) {
1805 if (layer->setSize(s.w, s.h)) {
1806 flags |= eTraversalNeeded;
1807 }
1808 }
1809 if (what & layer_state_t::eAlphaChanged) {
1810 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1811 flags |= eTraversalNeeded;
1812 }
1813 if (what & layer_state_t::eMatrixChanged) {
1814 if (layer->setMatrix(s.matrix))
1815 flags |= eTraversalNeeded;
1816 }
1817 if (what & layer_state_t::eTransparentRegionChanged) {
1818 if (layer->setTransparentRegionHint(s.transparentRegion))
1819 flags |= eTraversalNeeded;
1820 }
1821 if (what & layer_state_t::eVisibilityChanged) {
1822 if (layer->setFlags(s.flags, s.mask))
1823 flags |= eTraversalNeeded;
1824 }
1825 if (what & layer_state_t::eCropChanged) {
1826 if (layer->setCrop(s.crop))
1827 flags |= eTraversalNeeded;
1828 }
1829 if (what & layer_state_t::eLayerStackChanged) {
1830 // NOTE: index needs to be calculated before we update the state
1831 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1832 if (layer->setLayerStack(s.layerStack)) {
1833 mCurrentState.layersSortedByZ.removeAt(idx);
1834 mCurrentState.layersSortedByZ.add(layer);
1835 // we need traversal (state changed)
1836 // AND transaction (list changed)
1837 flags |= eTransactionNeeded|eTraversalNeeded;
1838 }
1839 }
1840 }
1841 return flags;
1842}
1843
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001844status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001845 const String8& name,
1846 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001847 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1848 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001849{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001850 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001851 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001852 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001853 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001854 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001855 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001856
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001857 status_t result = NO_ERROR;
1858
1859 sp<Layer> layer;
1860
Mathias Agopian3165cc22012-08-08 19:42:09 -07001861 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1862 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001863 result = createNormalLayer(client,
1864 name, w, h, flags, format,
1865 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001866 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001867 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001868 result = createDimLayer(client,
1869 name, w, h, flags,
1870 handle, gbp, &layer);
1871 break;
1872 default:
1873 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001874 break;
1875 }
1876
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001877 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001878 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001879 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001880 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001881 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001882}
1883
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001884status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1885 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1886 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001887{
1888 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001889 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001890 case PIXEL_FORMAT_TRANSPARENT:
1891 case PIXEL_FORMAT_TRANSLUCENT:
1892 format = PIXEL_FORMAT_RGBA_8888;
1893 break;
1894 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001895#ifdef NO_RGBX_8888
1896 format = PIXEL_FORMAT_RGB_565;
1897#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001898 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001899#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001900 break;
1901 }
1902
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001903#ifdef NO_RGBX_8888
1904 if (format == PIXEL_FORMAT_RGBX_8888)
1905 format = PIXEL_FORMAT_RGBA_8888;
1906#endif
1907
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001908 *outLayer = new Layer(this, client, name, w, h, flags);
1909 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1910 if (err == NO_ERROR) {
1911 *handle = (*outLayer)->getHandle();
1912 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001913 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001914
1915 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1916 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001917}
1918
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001919status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1920 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1921 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001922{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001923 *outLayer = new LayerDim(this, client, name, w, h, flags);
1924 *handle = (*outLayer)->getHandle();
1925 *gbp = (*outLayer)->getBufferQueue();
1926 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001927}
1928
Mathias Agopianac9fa422013-02-11 16:40:36 -08001929status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001930{
Mathias Agopian67106042013-03-14 19:18:13 -07001931 // called by the window manager when it wants to remove a Layer
1932 status_t err = NO_ERROR;
1933 sp<Layer> l(client->getLayerUser(handle));
1934 if (l != NULL) {
1935 err = removeLayer(l);
1936 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1937 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001938 }
1939 return err;
1940}
1941
Mathias Agopian13127d82013-03-05 17:47:11 -08001942status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001943{
Mathias Agopian67106042013-03-14 19:18:13 -07001944 // called by ~LayerCleaner() when all references to the IBinder (handle)
1945 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001946 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001947 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001948 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07001949 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001950 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001951 "error removing layer=%p (%s)", l.get(), strerror(-err));
1952 }
1953 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001954}
1955
Mathias Agopianb60314a2012-04-10 22:09:54 -07001956// ---------------------------------------------------------------------------
1957
Andy McFadden13a082e2012-08-24 10:16:42 -07001958void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08001959 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07001960 Vector<ComposerState> state;
1961 Vector<DisplayState> displays;
1962 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08001963 d.what = DisplayState::eDisplayProjectionChanged |
1964 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08001965 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08001966 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07001967 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001968 d.frame.makeInvalid();
1969 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001970 displays.add(d);
1971 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001972 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07001973
1974 const nsecs_t period =
1975 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
1976 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07001977}
1978
1979void SurfaceFlinger::initializeDisplays() {
1980 class MessageScreenInitialized : public MessageBase {
1981 SurfaceFlinger* flinger;
1982 public:
1983 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1984 virtual bool handler() {
1985 flinger->onInitializeDisplays();
1986 return true;
1987 }
1988 };
1989 sp<MessageBase> msg = new MessageScreenInitialized(this);
1990 postMessageAsync(msg); // we may be called from main thread, use async message
1991}
1992
1993
Mathias Agopiancde87a32012-09-13 14:09:01 -07001994void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07001995 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
1996 if (hw->isScreenAcquired()) {
1997 // this is expected, e.g. when power manager wakes up during boot
1998 ALOGD(" screen was previously acquired");
1999 return;
2000 }
2001
Mathias Agopian42977342012-08-05 00:40:46 -07002002 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002003 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002004 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002005 // built-in display, tell the HWC
2006 getHwComposer().acquire(type);
2007
2008 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2009 // FIXME: eventthread only knows about the main display right now
2010 mEventThread->onScreenAcquired();
2011 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002012 }
Mathias Agopian20128302012-08-13 18:32:13 -07002013 mVisibleRegionsDirty = true;
2014 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002015}
2016
Mathias Agopiancde87a32012-09-13 14:09:01 -07002017void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002018 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2019 if (!hw->isScreenAcquired()) {
2020 ALOGD(" screen was previously released");
2021 return;
2022 }
2023
2024 hw->releaseScreen();
2025 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002026 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002027 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002028 // FIXME: eventthread only knows about the main display right now
2029 mEventThread->onScreenReleased();
2030 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002031
2032 // built-in display, tell the HWC
2033 getHwComposer().release(type);
2034 }
2035 mVisibleRegionsDirty = true;
2036 // from this point on, SF will stop drawing on this display
2037}
2038
2039void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2040 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002041 SurfaceFlinger& mFlinger;
2042 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002043 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002044 MessageScreenAcquired(SurfaceFlinger& flinger,
2045 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002046 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002047 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2048 if (hw == NULL) {
2049 ALOGE("Attempt to unblank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002050 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002051 ALOGW("Attempt to unblank virtual display");
2052 } else {
2053 mFlinger.onScreenAcquired(hw);
2054 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002055 return true;
2056 }
2057 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002058 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2059 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002060}
2061
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002062void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002063 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002064 SurfaceFlinger& mFlinger;
2065 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002066 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002067 MessageScreenReleased(SurfaceFlinger& flinger,
2068 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002069 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002070 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2071 if (hw == NULL) {
2072 ALOGE("Attempt to blank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002073 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002074 ALOGW("Attempt to blank virtual display");
2075 } else {
2076 mFlinger.onScreenReleased(hw);
2077 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002078 return true;
2079 }
2080 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002081 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2082 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002083}
2084
2085// ---------------------------------------------------------------------------
2086
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002087status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2088{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002089 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002090
Mathias Agopianbd115332013-04-18 16:41:04 -07002091 IPCThreadState* ipc = IPCThreadState::self();
2092 const int pid = ipc->getCallingPid();
2093 const int uid = ipc->getCallingUid();
2094 if ((uid != AID_SHELL) &&
2095 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002096 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002097 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002098 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002099 // Try to get the main lock, but don't insist if we can't
2100 // (this would indicate SF is stuck, but we want to be able to
2101 // print something in dumpsys).
2102 int retry = 3;
2103 while (mStateLock.tryLock()<0 && --retry>=0) {
2104 usleep(1000000);
2105 }
2106 const bool locked(retry >= 0);
2107 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002108 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002109 "SurfaceFlinger appears to be unresponsive, "
2110 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002111 }
2112
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002113 bool dumpAll = true;
2114 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002115 size_t numArgs = args.size();
2116 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002117 if ((index < numArgs) &&
2118 (args[index] == String16("--list"))) {
2119 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002120 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002121 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002122 }
2123
2124 if ((index < numArgs) &&
2125 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002126 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002127 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002128 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002129 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002130
2131 if ((index < numArgs) &&
2132 (args[index] == String16("--latency-clear"))) {
2133 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002134 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002135 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002136 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002137 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002138
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002139 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002140 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002141 }
2142
Mathias Agopian9795c422009-08-26 16:36:26 -07002143 if (locked) {
2144 mStateLock.unlock();
2145 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002146 }
2147 write(fd, result.string(), result.size());
2148 return NO_ERROR;
2149}
2150
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002151void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002152 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002153{
2154 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2155 const size_t count = currentLayers.size();
2156 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002157 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002158 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002159 }
2160}
2161
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002162void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002163 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002164{
2165 String8 name;
2166 if (index < args.size()) {
2167 name = String8(args[index]);
2168 index++;
2169 }
2170
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002171 const nsecs_t period =
2172 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2173 result.appendFormat("%lld\n", period);
2174
2175 if (name.isEmpty()) {
2176 mAnimFrameTracker.dump(result);
2177 } else {
2178 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2179 const size_t count = currentLayers.size();
2180 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002181 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002182 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002183 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002184 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002185 }
2186 }
2187}
2188
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002189void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002190 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002191{
2192 String8 name;
2193 if (index < args.size()) {
2194 name = String8(args[index]);
2195 index++;
2196 }
2197
2198 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2199 const size_t count = currentLayers.size();
2200 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002201 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002202 if (name.isEmpty() || (name == layer->getName())) {
2203 layer->clearStats();
2204 }
2205 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002206
2207 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002208}
2209
Jamie Gennis6547ff42013-07-16 20:12:42 -07002210// This should only be called from the main thread. Otherwise it would need
2211// the lock and should use mCurrentState rather than mDrawingState.
2212void SurfaceFlinger::logFrameStats() {
2213 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2214 const size_t count = drawingLayers.size();
2215 for (size_t i=0 ; i<count ; i++) {
2216 const sp<Layer>& layer(drawingLayers[i]);
2217 layer->logFrameStats();
2218 }
2219
2220 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2221}
2222
Andy McFadden4803b742012-09-24 19:07:20 -07002223/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2224{
2225 static const char* config =
2226 " [sf"
2227#ifdef NO_RGBX_8888
2228 " NO_RGBX_8888"
2229#endif
2230#ifdef HAS_CONTEXT_PRIORITY
2231 " HAS_CONTEXT_PRIORITY"
2232#endif
2233#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2234 " NEVER_DEFAULT_TO_ASYNC_MODE"
2235#endif
2236#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2237 " TARGET_DISABLE_TRIPLE_BUFFERING"
2238#endif
2239 "]";
2240 result.append(config);
2241}
2242
Mathias Agopian74d211a2013-04-22 16:55:35 +02002243void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2244 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002245{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002246 bool colorize = false;
2247 if (index < args.size()
2248 && (args[index] == String16("--color"))) {
2249 colorize = true;
2250 index++;
2251 }
2252
2253 Colorizer colorizer(colorize);
2254
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002255 // figure out if we're stuck somewhere
2256 const nsecs_t now = systemTime();
2257 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2258 const nsecs_t inTransaction(mDebugInTransaction);
2259 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2260 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2261
2262 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002263 * Dump library configuration.
2264 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002265
2266 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002267 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002268 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002269 appendSfConfigString(result);
2270 appendUiConfigString(result);
2271 appendGuiConfigString(result);
2272 result.append("\n");
2273
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002274 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002275 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002276 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002277 result.append(SyncFeatures::getInstance().toString());
2278 result.append("\n");
2279
Andy McFadden4803b742012-09-24 19:07:20 -07002280 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002281 * Dump the visible layer list
2282 */
2283 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2284 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002285 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002286 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002287 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002288 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002289 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002290 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002291 }
2292
2293 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002294 * Dump Display state
2295 */
2296
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002297 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002298 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002299 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002300 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2301 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002302 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002303 }
2304
2305 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002306 * Dump SurfaceFlinger global state
2307 */
2308
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002309 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002310 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002311 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002312
Mathias Agopian888c8222012-08-04 21:10:38 -07002313 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002314 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002315
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002316 colorizer.bold(result);
2317 result.appendFormat("EGL implementation : %s\n",
2318 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2319 colorizer.reset(result);
2320 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002321 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002322
Mathias Agopian875d8e12013-06-07 15:35:48 -07002323 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002324
Mathias Agopian42977342012-08-05 00:40:46 -07002325 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002326 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002327 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002328 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002329 " last eglSwapBuffers() time: %f us\n"
2330 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002331 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002332 " refresh-rate : %f fps\n"
2333 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002334 " y-dpi : %f\n"
2335 " EGL_NATIVE_VISUAL_ID : %d\n"
2336 " gpu_to_cpu_unsupported : %d\n"
2337 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002338 mLastSwapBufferTime/1000.0,
2339 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002340 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002341 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2342 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002343 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2344 mEGLNativeVisualId,
2345 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002346
Mathias Agopian74d211a2013-04-22 16:55:35 +02002347 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002348 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002349
Mathias Agopian74d211a2013-04-22 16:55:35 +02002350 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002351 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002352
2353 /*
2354 * VSYNC state
2355 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002356 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002357
2358 /*
2359 * Dump HWComposer state
2360 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002361 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002362 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002363 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002364 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002365 hwc.initCheck()==NO_ERROR ? "present" : "not present",
2366 (mDebugDisableHWC || mDebugRegion) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002367 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002368
2369 /*
2370 * Dump gralloc state
2371 */
2372 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2373 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002374}
2375
Mathias Agopian13127d82013-03-05 17:47:11 -08002376const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002377SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002378 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002379 wp<IBinder> dpy;
2380 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2381 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2382 dpy = mDisplays.keyAt(i);
2383 break;
2384 }
2385 }
2386 if (dpy == NULL) {
2387 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2388 // Just use the primary display so we have something to return
2389 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2390 }
2391 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002392}
2393
Keun young Park63f165f2012-08-31 10:53:36 -07002394bool SurfaceFlinger::startDdmConnection()
2395{
2396 void* libddmconnection_dso =
2397 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2398 if (!libddmconnection_dso) {
2399 return false;
2400 }
2401 void (*DdmConnection_start)(const char* name);
2402 DdmConnection_start =
2403 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2404 if (!DdmConnection_start) {
2405 dlclose(libddmconnection_dso);
2406 return false;
2407 }
2408 (*DdmConnection_start)(getServiceName());
2409 return true;
2410}
2411
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002412status_t SurfaceFlinger::onTransact(
2413 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2414{
2415 switch (code) {
2416 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002417 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002418 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002419 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002420 case BLANK:
2421 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002422 {
2423 // codes that require permission check
2424 IPCThreadState* ipc = IPCThreadState::self();
2425 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002426 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002427 if ((uid != AID_GRAPHICS) &&
2428 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002429 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002430 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2431 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002432 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002433 break;
2434 }
2435 case CAPTURE_SCREEN:
2436 {
2437 // codes that require permission check
2438 IPCThreadState* ipc = IPCThreadState::self();
2439 const int pid = ipc->getCallingPid();
2440 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002441 if ((uid != AID_GRAPHICS) &&
2442 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002443 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002444 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2445 return PERMISSION_DENIED;
2446 }
2447 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002448 }
2449 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002450
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002451 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2452 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002453 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002454 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002455 IPCThreadState* ipc = IPCThreadState::self();
2456 const int pid = ipc->getCallingPid();
2457 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002458 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002459 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002460 return PERMISSION_DENIED;
2461 }
2462 int n;
2463 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002464 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002465 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002466 return NO_ERROR;
2467 case 1002: // SHOW_UPDATES
2468 n = data.readInt32();
2469 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002470 invalidateHwcGeometry();
2471 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002472 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002473 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002474 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002475 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002476 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002477 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002478 setTransactionFlags(
2479 eTransactionNeeded|
2480 eDisplayTransactionNeeded|
2481 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002482 return NO_ERROR;
2483 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002484 case 1006:{ // send empty update
2485 signalRefresh();
2486 return NO_ERROR;
2487 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002488 case 1008: // toggle use of hw composer
2489 n = data.readInt32();
2490 mDebugDisableHWC = n ? 1 : 0;
2491 invalidateHwcGeometry();
2492 repaintEverything();
2493 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002494 case 1009: // toggle use of transform hint
2495 n = data.readInt32();
2496 mDebugDisableTransformHint = n ? 1 : 0;
2497 invalidateHwcGeometry();
2498 repaintEverything();
2499 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002500 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002501 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002502 reply->writeInt32(0);
2503 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002504 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002505 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002506 return NO_ERROR;
2507 case 1013: {
2508 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002509 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2510 reply->writeInt32(hw->getPageFlipCount());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002511 }
2512 return NO_ERROR;
2513 }
2514 }
2515 return err;
2516}
2517
Mathias Agopian53331da2011-08-22 21:44:41 -07002518void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002519 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002520 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002521}
2522
Mathias Agopian59119e62010-10-11 12:37:43 -07002523// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002524// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002525// ---------------------------------------------------------------------------
2526
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002527/* The code below is here to handle b/8734824
2528 *
2529 * We create a IGraphicBufferProducer wrapper that forwards all calls
2530 * to the calling binder thread, where they are executed. This allows
2531 * the calling thread to be reused (on the other side) and not
2532 * depend on having "enough" binder threads to handle the requests.
2533 *
2534 */
2535
2536class GraphicProducerWrapper : public BBinder, public MessageHandler {
2537 sp<IGraphicBufferProducer> impl;
2538 sp<Looper> looper;
2539 status_t result;
2540 bool exitPending;
2541 bool exitRequested;
2542 mutable Barrier barrier;
2543 volatile int32_t memoryBarrier;
2544 uint32_t code;
2545 Parcel const* data;
2546 Parcel* reply;
2547
2548 enum {
2549 MSG_API_CALL,
2550 MSG_EXIT
2551 };
2552
2553 /*
2554 * this is called by our "fake" BpGraphicBufferProducer. We package the
2555 * data and reply Parcel and forward them to the calling thread.
2556 */
2557 virtual status_t transact(uint32_t code,
2558 const Parcel& data, Parcel* reply, uint32_t flags) {
2559 this->code = code;
2560 this->data = &data;
2561 this->reply = reply;
2562 android_atomic_acquire_store(0, &memoryBarrier);
2563 if (exitPending) {
2564 // if we've exited, we run the message synchronously right here
2565 handleMessage(Message(MSG_API_CALL));
2566 } else {
2567 barrier.close();
2568 looper->sendMessage(this, Message(MSG_API_CALL));
2569 barrier.wait();
2570 }
2571 return NO_ERROR;
2572 }
2573
2574 /*
2575 * here we run on the binder calling thread. All we've got to do is
2576 * call the real BpGraphicBufferProducer.
2577 */
2578 virtual void handleMessage(const Message& message) {
2579 android_atomic_release_load(&memoryBarrier);
2580 if (message.what == MSG_API_CALL) {
2581 impl->asBinder()->transact(code, data[0], reply);
2582 barrier.open();
2583 } else if (message.what == MSG_EXIT) {
2584 exitRequested = true;
2585 }
2586 }
2587
2588public:
2589 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2590 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2591 exitPending(false), exitRequested(false) {
2592 }
2593
2594 status_t waitForResponse() {
2595 do {
2596 looper->pollOnce(-1);
2597 } while (!exitRequested);
2598 return result;
2599 }
2600
2601 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002602 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002603 exitPending = true;
2604 looper->sendMessage(this, Message(MSG_EXIT));
2605 }
2606};
2607
2608
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002609status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2610 const sp<IGraphicBufferProducer>& producer,
2611 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002612 uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002613
2614 if (CC_UNLIKELY(display == 0))
2615 return BAD_VALUE;
2616
2617 if (CC_UNLIKELY(producer == 0))
2618 return BAD_VALUE;
2619
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002620 // if we have secure windows on this display, never allow the screen capture
2621 // unless the producer interface is local (i.e.: we can take a screenshot for
2622 // ourselves).
2623 if (!producer->asBinder()->localBinder()) {
2624 Mutex::Autolock _l(mStateLock);
2625 sp<const DisplayDevice> hw(getDisplayDevice(display));
2626 if (hw->getSecureLayerVisible()) {
2627 ALOGW("FB is protected: PERMISSION_DENIED");
2628 return PERMISSION_DENIED;
2629 }
2630 }
2631
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002632 class MessageCaptureScreen : public MessageBase {
2633 SurfaceFlinger* flinger;
2634 sp<IBinder> display;
2635 sp<IGraphicBufferProducer> producer;
2636 uint32_t reqWidth, reqHeight;
2637 uint32_t minLayerZ,maxLayerZ;
2638 status_t result;
2639 public:
2640 MessageCaptureScreen(SurfaceFlinger* flinger,
2641 const sp<IBinder>& display,
2642 const sp<IGraphicBufferProducer>& producer,
2643 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002644 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002645 : flinger(flinger), display(display), producer(producer),
2646 reqWidth(reqWidth), reqHeight(reqHeight),
2647 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2648 result(PERMISSION_DENIED)
2649 {
2650 }
2651 status_t getResult() const {
2652 return result;
2653 }
2654 virtual bool handler() {
2655 Mutex::Autolock _l(flinger->mStateLock);
2656 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002657 result = flinger->captureScreenImplLocked(hw,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002658 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002659 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002660 return true;
2661 }
2662 };
2663
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002664 // make sure to process transactions before screenshots -- a transaction
2665 // might already be pending but scheduled for VSYNC; this guarantees we
2666 // will handle it before the screenshot. When VSYNC finally arrives
2667 // the scheduled transaction will be a no-op. If no transactions are
2668 // scheduled at this time, this will end-up being a no-op as well.
2669 mEventQueue.invalidateTransactionNow();
2670
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002671 // this creates a "fake" BBinder which will serve as a "fake" remote
2672 // binder to receive the marshaled calls and forward them to the
2673 // real remote (a BpGraphicBufferProducer)
2674 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2675
2676 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2677 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002678 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002679 display, IGraphicBufferProducer::asInterface( wrapper ),
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002680 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002681
2682 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002683 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002684 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002685 }
2686 return res;
2687}
2688
Mathias Agopian180f10d2013-04-10 22:55:41 -07002689
2690void SurfaceFlinger::renderScreenImplLocked(
2691 const sp<const DisplayDevice>& hw,
2692 uint32_t reqWidth, uint32_t reqHeight,
2693 uint32_t minLayerZ, uint32_t maxLayerZ,
2694 bool yswap)
2695{
2696 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002697 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002698
2699 // get screen geometry
2700 const uint32_t hw_w = hw->getWidth();
2701 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002702 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2703
2704 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002705 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002706
2707 // set-up our viewport
Mathias Agopian3f844832013-08-07 21:24:32 -07002708 engine.setViewportAndProjection(reqWidth, reqHeight, hw_w, hw_h, yswap);
2709 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002710
2711 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002712 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002713
2714 const LayerVector& layers( mDrawingState.layersSortedByZ );
2715 const size_t count = layers.size();
2716 for (size_t i=0 ; i<count ; ++i) {
2717 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002718 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002719 if (state.layerStack == hw->getLayerStack()) {
2720 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2721 if (layer->isVisible()) {
2722 if (filtering) layer->setFiltering(true);
2723 layer->draw(hw);
2724 if (filtering) layer->setFiltering(false);
2725 }
2726 }
2727 }
2728 }
2729
2730 // compositionComplete is needed for older driver
2731 hw->compositionComplete();
2732}
2733
2734
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002735status_t SurfaceFlinger::captureScreenImplLocked(
2736 const sp<const DisplayDevice>& hw,
2737 const sp<IGraphicBufferProducer>& producer,
2738 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002739 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002740{
2741 ATRACE_CALL();
2742
Mathias Agopian180f10d2013-04-10 22:55:41 -07002743 // get screen geometry
2744 const uint32_t hw_w = hw->getWidth();
2745 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002746
Mathias Agopian180f10d2013-04-10 22:55:41 -07002747 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2748 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2749 reqWidth, reqHeight, hw_w, hw_h);
2750 return BAD_VALUE;
2751 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002752
Mathias Agopian180f10d2013-04-10 22:55:41 -07002753 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2754 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2755
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002756 // create a surface (because we're a producer, and we need to
2757 // dequeue/queue a buffer)
2758 sp<Surface> sur = new Surface(producer);
2759 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002760
2761 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002762 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002763 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2764 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002765
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002766 int err = 0;
2767 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
2768 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2769 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002770
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002771 if (err == NO_ERROR) {
2772 ANativeWindowBuffer* buffer;
2773 /* TODO: Once we have the sync framework everywhere this can use
2774 * server-side waits on the fence that dequeueBuffer returns.
2775 */
2776 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2777 if (result == NO_ERROR) {
2778 // create an EGLImage from the buffer so we can later
2779 // turn it into a texture
2780 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2781 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2782 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002783 // this binds the given EGLImage as a framebuffer for the
2784 // duration of this scope.
2785 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2786 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002787 // this will in fact render into our dequeued buffer
2788 // via an FBO, which means we didn't have to create
2789 // an EGLSurface and therefore we're not
2790 // dependent on the context's EGLConfig.
2791 renderScreenImplLocked(hw, reqWidth, reqHeight,
2792 minLayerZ, maxLayerZ, true);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002793 } else {
2794 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2795 result = INVALID_OPERATION;
2796 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002797 // destroy our image
2798 eglDestroyImageKHR(mEGLDisplay, image);
2799 } else {
2800 result = BAD_VALUE;
2801 }
2802 window->queueBuffer(window, buffer, -1);
2803 }
2804 } else {
2805 result = BAD_VALUE;
2806 }
2807 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2808 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002809
Mathias Agopian875d8e12013-06-07 15:35:48 -07002810 hw->setViewportAndProjection();
Mathias Agopian2be4e8f2013-03-06 20:42:56 -08002811
Mathias Agopian74c40c02010-09-29 13:02:36 -07002812 return result;
2813}
2814
Mathias Agopianfee2b462013-07-03 12:34:01 -07002815void SurfaceFlinger::checkScreenshot(const sp<GraphicBuffer>& buf, void const* vaddr,
2816 const sp<const DisplayDevice>& hw,
2817 uint32_t minLayerZ, uint32_t maxLayerZ) {
2818 if (DEBUG_SCREENSHOTS) {
2819 for (ssize_t y=0 ; y<buf->height ; y++) {
2820 uint32_t const * p = (uint32_t const *)vaddr + y*buf->stride;
2821 for (ssize_t x=0 ; x<buf->width ; x++) {
2822 if (p[x] != 0xFF000000) return;
2823 }
2824 }
2825 ALOGE("*** we just took a black screenshot ***\n"
2826 "requested minz=%d, maxz=%d, layerStack=%d",
2827 minLayerZ, maxLayerZ, hw->getLayerStack());
2828 const LayerVector& layers( mDrawingState.layersSortedByZ );
2829 const size_t count = layers.size();
2830 for (size_t i=0 ; i<count ; ++i) {
2831 const sp<Layer>& layer(layers[i]);
2832 const Layer::State& state(layer->getDrawingState());
2833 const bool visible = (state.layerStack == hw->getLayerStack())
2834 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2835 && (layer->isVisible());
2836 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
2837 visible ? '+' : '-',
2838 i, layer->getName().string(), state.layerStack, state.z,
2839 layer->isVisible(), state.flags, state.alpha);
2840 }
2841 }
2842}
2843
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002844// ---------------------------------------------------------------------------
2845
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002846SurfaceFlinger::LayerVector::LayerVector() {
2847}
2848
2849SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002850 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002851}
2852
2853int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2854 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002855{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002856 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002857 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2858 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002859
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002860 uint32_t ls = l->getCurrentState().layerStack;
2861 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002862 if (ls != rs)
2863 return ls - rs;
2864
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002865 uint32_t lz = l->getCurrentState().z;
2866 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002867 if (lz != rz)
2868 return lz - rz;
2869
2870 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002871}
2872
2873// ---------------------------------------------------------------------------
2874
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002875SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2876 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002877}
2878
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002879SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002880 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002881 viewport.makeInvalid();
2882 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002883}
2884
2885// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002886
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002887}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002888
2889
2890#if defined(__gl_h_)
2891#error "don't include gl/gl.h in this file"
2892#endif
2893
2894#if defined(__gl2_h_)
2895#error "don't include gl2/gl2.h in this file"
2896#endif