blob: def4618118d29c91390078beadb7ce0350935042 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070024
25#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <cutils/log.h>
28#include <cutils/properties.h>
29
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070032#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070033#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034
Mathias Agopianc666cae2012-07-25 18:56:13 -070035#include <ui/DisplayInfo.h>
36
Mathias Agopian921e6ac2012-07-23 23:11:29 -070037#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070038#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070039#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070040#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080041#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080042#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070043
44#include <ui/GraphicBufferAllocator.h>
45#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070046#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080047
Mathias Agopiancde87a32012-09-13 14:09:01 -070048#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <utils/String8.h>
50#include <utils/String16.h>
51#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080052#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian921e6ac2012-07-23 23:11:29 -070054#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070055#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopian3e25fd82013-04-22 17:52:16 +020057#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020059#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080060#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070061#include "DisplayDevice.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080062#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066
Mathias Agopiana4912602012-07-12 14:25:33 -070067#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070068#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070069#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070
Mathias Agopianff2ed702013-09-01 21:36:12 -070071#include "Effects/Daltonizer.h"
72
Mathias Agopian875d8e12013-06-07 15:35:48 -070073#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070074#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070075
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076#define DISPLAY_COUNT 1
77
Mathias Agopianfee2b462013-07-03 12:34:01 -070078/*
79 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
80 * black pixels.
81 */
82#define DEBUG_SCREENSHOTS false
83
Mathias Agopianca088332013-03-28 17:44:13 -070084EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
85
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086namespace android {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087// ---------------------------------------------------------------------------
88
Mathias Agopian99b49842011-06-27 16:05:52 -070089const String16 sHardwareTest("android.permission.HARDWARE_TEST");
90const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
91const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
92const String16 sDump("android.permission.DUMP");
93
94// ---------------------------------------------------------------------------
95
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -070097 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -070099 mTransactionPending(false),
100 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700101 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700102 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700103 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700106 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800107 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700109 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700110 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700111 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700112 mDebugInSwapBuffers(0),
113 mLastSwapBufferTime(0),
114 mDebugInTransaction(0),
115 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700116 mBootFinished(false),
117 mDaltonize(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() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700643 do {
644 waitForEvent();
645 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800646}
647
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700648void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700649 if (mEventThread == NULL) {
650 // This is a temporary workaround for b/7145521. A non-null pointer
651 // does not mean EventThread has finished initializing, so this
652 // is not a correct fix.
653 ALOGW("WARNING: EventThread not started, ignoring vsync");
654 return;
655 }
Jesse Hall9e663de2013-08-16 14:28:37 -0700656 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700657 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700658 mEventThread->onVSyncReceived(type, timestamp);
659 }
660}
661
662void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
663 if (mEventThread == NULL) {
664 // This is a temporary workaround for b/7145521. A non-null pointer
665 // does not mean EventThread has finished initializing, so this
666 // is not a correct fix.
667 ALOGW("WARNING: EventThread not started, ignoring hotplug");
668 return;
669 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700670
Jesse Hall9e663de2013-08-16 14:28:37 -0700671 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700672 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800673 if (connected) {
674 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700675 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800676 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
677 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700678 }
679 setTransactionFlags(eDisplayTransactionNeeded);
680
Andy McFadden9e9689c2012-10-10 18:17:51 -0700681 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700682 }
Mathias Agopian86303202012-07-24 22:46:10 -0700683}
684
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700685void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
686 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700687}
688
Mathias Agopian4fec8732012-06-29 14:12:52 -0700689void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800690 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800691 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700692 case MessageQueue::TRANSACTION:
693 handleMessageTransaction();
694 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700695 case MessageQueue::INVALIDATE:
696 handleMessageTransaction();
697 handleMessageInvalidate();
698 signalRefresh();
699 break;
700 case MessageQueue::REFRESH:
701 handleMessageRefresh();
702 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800703 }
704}
705
Mathias Agopian4fec8732012-06-29 14:12:52 -0700706void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700707 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700708 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700709 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700710 }
711}
712
713void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700714 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700715 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700716}
717
718void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700719 ATRACE_CALL();
720 preComposition();
721 rebuildLayerStacks();
722 setUpHWComposer();
723 doDebugFlashRegions();
724 doComposition();
725 postComposition();
726}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700727
Mathias Agopiancd60f992012-08-16 16:28:27 -0700728void SurfaceFlinger::doDebugFlashRegions()
729{
730 // is debugging enabled
731 if (CC_LIKELY(!mDebugRegion))
732 return;
733
734 const bool repaintEverything = mRepaintEverything;
735 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
736 const sp<DisplayDevice>& hw(mDisplays[dpy]);
737 if (hw->canDraw()) {
738 // transform the dirty region into this screen's coordinate space
739 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
740 if (!dirtyRegion.isEmpty()) {
741 // redraw the whole screen
742 doComposeSurfaces(hw, Region(hw->bounds()));
743
744 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700745 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700746 RenderEngine& engine(getRenderEngine());
747 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
748
Mathias Agopiancd60f992012-08-16 16:28:27 -0700749 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700750 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700751 }
752 }
753 }
754
755 postFramebuffer();
756
757 if (mDebugRegion > 1) {
758 usleep(mDebugRegion * 1000);
759 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700760
761 HWComposer& hwc(getHwComposer());
762 if (hwc.initCheck() == NO_ERROR) {
763 status_t err = hwc.prepare();
764 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
765 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700766}
767
768void SurfaceFlinger::preComposition()
769{
770 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700771 const LayerVector& layers(mDrawingState.layersSortedByZ);
772 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700773 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700774 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700775 needExtraInvalidate = true;
776 }
777 }
778 if (needExtraInvalidate) {
779 signalLayerUpdate();
780 }
781}
782
783void SurfaceFlinger::postComposition()
784{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700785 const LayerVector& layers(mDrawingState.layersSortedByZ);
786 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700787 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700788 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700789 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800790
791 if (mAnimCompositionPending) {
792 mAnimCompositionPending = false;
793
794 const HWComposer& hwc = getHwComposer();
795 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800796 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800797 mAnimFrameTracker.setActualPresentFence(presentFence);
798 } else {
799 // The HWC doesn't support present fences, so use the refresh
800 // timestamp instead.
801 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
802 mAnimFrameTracker.setActualPresentTime(presentTime);
803 }
804 mAnimFrameTracker.advanceFrame();
805 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700806}
807
808void SurfaceFlinger::rebuildLayerStacks() {
809 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700810 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700811 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700812 mVisibleRegionsDirty = false;
813 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700814
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700815 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700816 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700817 Region opaqueRegion;
818 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800819 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700820 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700821 const Transform& tr(hw->getTransform());
822 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700823 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700824 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700825 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700826
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700827 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700828 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700829 const sp<Layer>& layer(layers[i]);
830 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700831 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700832 Region drawRegion(tr.transform(
833 layer->visibleNonTransparentRegion));
834 drawRegion.andSelf(bounds);
835 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700836 layersSortedByZ.add(layer);
837 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700838 }
839 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700840 }
Mathias Agopian42977342012-08-05 00:40:46 -0700841 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700842 hw->undefinedRegion.set(bounds);
843 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
844 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700845 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700846 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700847}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700848
Mathias Agopiancd60f992012-08-16 16:28:27 -0700849void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700850 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
851 mDisplays[dpy]->beginFrame();
852 }
853
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700854 HWComposer& hwc(getHwComposer());
855 if (hwc.initCheck() == NO_ERROR) {
856 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700857 if (CC_UNLIKELY(mHwWorkListDirty)) {
858 mHwWorkListDirty = false;
859 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
860 sp<const DisplayDevice> hw(mDisplays[dpy]);
861 const int32_t id = hw->getHwcDisplayId();
862 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800863 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700864 hw->getVisibleLayersSortedByZ());
865 const size_t count = currentLayers.size();
866 if (hwc.createWorkList(id, count) == NO_ERROR) {
867 HWComposer::LayerListIterator cur = hwc.begin(id);
868 const HWComposer::LayerListIterator end = hwc.end(id);
869 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800870 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700871 layer->setGeometry(hw, *cur);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700872 if (mDebugDisableHWC || mDebugRegion || mDaltonize) {
Jamie Gennisa4310c82012-09-25 20:26:00 -0700873 cur->setSkip(true);
874 }
875 }
876 }
877 }
878 }
879 }
880
881 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700882 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700883 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700884 const int32_t id = hw->getHwcDisplayId();
885 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800886 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700887 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700888 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700889 HWComposer::LayerListIterator cur = hwc.begin(id);
890 const HWComposer::LayerListIterator end = hwc.end(id);
891 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
892 /*
893 * update the per-frame h/w composer data for each layer
894 * and build the transparent region of the FB
895 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800896 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700897 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700898 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700899 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700900 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700901
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700902 status_t err = hwc.prepare();
903 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700904
905 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
906 sp<const DisplayDevice> hw(mDisplays[dpy]);
907 hw->prepareFrame(hwc);
908 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700909 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700910}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700911
Mathias Agopiancd60f992012-08-16 16:28:27 -0700912void SurfaceFlinger::doComposition() {
913 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700914 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700915 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700916 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700917 if (hw->canDraw()) {
918 // transform the dirty region into this screen's coordinate space
919 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800920
921 // repaint the framebuffer (if needed)
922 doDisplayComposition(hw, dirtyRegion);
923
Mathias Agopiancd60f992012-08-16 16:28:27 -0700924 hw->dirtyRegion.clear();
925 hw->flip(hw->swapRegion);
926 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700927 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700928 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700929 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700930 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700931 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700932}
933
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800934void SurfaceFlinger::postFramebuffer()
935{
Mathias Agopian841cde52012-03-01 15:44:37 -0800936 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800937
Mathias Agopiana44b0412011-10-16 18:46:35 -0700938 const nsecs_t now = systemTime();
939 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700940
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700941 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700942 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700943 if (!hwc.supportsFramebufferTarget()) {
944 // EGL spec says:
945 // "surface must be bound to the calling thread's current context,
946 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700947 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700948 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700949 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700950 }
951
Mathias Agopian92a979a2012-08-02 18:32:23 -0700952 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700953 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800954 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700955 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700956 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700957 int32_t id = hw->getHwcDisplayId();
958 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700959 HWComposer::LayerListIterator cur = hwc.begin(id);
960 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700961 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700962 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700963 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700964 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700965 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700966 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700967 }
Jesse Hallef194142012-06-14 14:45:17 -0700968 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800969 }
970
Mathias Agopiana44b0412011-10-16 18:46:35 -0700971 mLastSwapBufferTime = systemTime() - now;
972 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700973
974 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
975 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
976 logFrameStats();
977 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800978}
979
Mathias Agopian87baae12012-07-31 12:38:26 -0700980void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800981{
Mathias Agopian841cde52012-03-01 15:44:37 -0800982 ATRACE_CALL();
983
Mathias Agopian7cc6df52013-06-05 14:30:54 -0700984 // here we keep a copy of the drawing state (that is the state that's
985 // going to be overwritten by handleTransactionLocked()) outside of
986 // mStateLock so that the side-effects of the State assignment
987 // don't happen with mStateLock held (which can cause deadlocks).
988 State drawingState(mDrawingState);
989
Mathias Agopianca4d3602011-05-19 15:38:14 -0700990 Mutex::Autolock _l(mStateLock);
991 const nsecs_t now = systemTime();
992 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800993
Mathias Agopianca4d3602011-05-19 15:38:14 -0700994 // Here we're guaranteed that some transaction flags are set
995 // so we can call handleTransactionLocked() unconditionally.
996 // We call getTransactionFlags(), which will also clear the flags,
997 // with mStateLock held to guarantee that mCurrentState won't change
998 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -0700999
Mathias Agopiane57f2922012-08-09 16:29:12 -07001000 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001001 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001002
Mathias Agopianca4d3602011-05-19 15:38:14 -07001003 mLastTransactionTime = systemTime() - now;
1004 mDebugInTransaction = 0;
1005 invalidateHwcGeometry();
1006 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001007}
1008
Mathias Agopian87baae12012-07-31 12:38:26 -07001009void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001010{
1011 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001012 const size_t count = currentLayers.size();
1013
1014 /*
1015 * Traversal of the children
1016 * (perform the transaction for each of them if needed)
1017 */
1018
Mathias Agopian3559b072012-08-15 13:46:03 -07001019 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001020 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001021 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001022 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1023 if (!trFlags) continue;
1024
1025 const uint32_t flags = layer->doTransaction(0);
1026 if (flags & Layer::eVisibleRegion)
1027 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001028 }
1029 }
1030
1031 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001032 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001033 */
1034
Mathias Agopiane57f2922012-08-09 16:29:12 -07001035 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001036 // here we take advantage of Vector's copy-on-write semantics to
1037 // improve performance by skipping the transaction entirely when
1038 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001039 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1040 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001041 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001043 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001044 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001045
1046 // find the displays that were removed
1047 // (ie: in drawing state but not in current state)
1048 // also handle displays that changed
1049 // (ie: displays that are in both lists)
1050 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001051 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1052 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001053 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001054 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001055 // Call makeCurrent() on the primary display so we can
1056 // be sure that nothing associated with this display
1057 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001058 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001059 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001060 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1061 if (hw != NULL)
1062 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001063 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001064 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001065 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001066 } else {
1067 ALOGW("trying to remove the main display");
1068 }
1069 } else {
1070 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001071 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001072 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001073 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001074 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001075 // recreating the DisplayDevice, so we just remove it
1076 // from the drawing state, so that it get re-added
1077 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001078 sp<DisplayDevice> hw(getDisplayDevice(display));
1079 if (hw != NULL)
1080 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001081 mDisplays.removeItem(display);
1082 mDrawingState.displays.removeItemsAt(i);
1083 dc--; i--;
1084 // at this point we must loop to the next item
1085 continue;
1086 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001087
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001088 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001089 if (disp != NULL) {
1090 if (state.layerStack != draw[i].layerStack) {
1091 disp->setLayerStack(state.layerStack);
1092 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001093 if ((state.orientation != draw[i].orientation)
1094 || (state.viewport != draw[i].viewport)
1095 || (state.frame != draw[i].frame))
1096 {
1097 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001098 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001099 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001100 }
1101 }
1102 }
1103
1104 // find displays that were added
1105 // (ie: in current state but not in drawing state)
1106 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001107 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001108 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001109
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001110 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001111 sp<IGraphicBufferProducer> producer;
1112 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
1113
Jesse Hall02d86562013-03-25 14:43:23 -07001114 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001115 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001116 // Virtual displays without a surface are dormant:
1117 // they have external state (layer stack, projection,
1118 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001119 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001120
Jesse Hall02d86562013-03-25 14:43:23 -07001121 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001122 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1123 *mHwc, hwcDisplayId, state.surface, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001124 state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001125
1126 dispSurface = vds;
1127 if (hwcDisplayId >= 0) {
1128 producer = vds;
1129 } else {
1130 // There won't be any interaction with HWC for this virtual display,
1131 // so the GLES driver can pass buffers directly to the sink.
1132 producer = state.surface;
1133 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001134 }
1135 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001136 ALOGE_IF(state.surface!=NULL,
1137 "adding a supported display, but rendering "
1138 "surface is provided (%p), ignoring it",
1139 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001140 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001141 // for supported (by hwc) displays we provide our
1142 // own rendering surface
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001143 dispSurface = new FramebufferSurface(*mHwc, state.type, bq);
1144 producer = bq;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001145 }
1146
1147 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001148 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001149 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001150 state.type, hwcDisplayId, state.isSecure,
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001151 display, dispSurface, producer, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001152 hw->setLayerStack(state.layerStack);
1153 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001154 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001155 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001156 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001157 if (state.isVirtualDisplay()) {
1158 if (hwcDisplayId >= 0) {
1159 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1160 hw->getWidth(), hw->getHeight(),
1161 hw->getFormat());
1162 }
1163 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001164 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001165 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001166 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001167 }
1168 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001169 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001170 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001171
Mathias Agopian84300952012-11-21 16:02:13 -08001172 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1173 // The transform hint might have changed for some layers
1174 // (either because a display has changed, or because a layer
1175 // as changed).
1176 //
1177 // Walk through all the layers in currentLayers,
1178 // and update their transform hint.
1179 //
1180 // If a layer is visible only on a single display, then that
1181 // display is used to calculate the hint, otherwise we use the
1182 // default display.
1183 //
1184 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1185 // the hint is set before we acquire a buffer from the surface texture.
1186 //
1187 // NOTE: layer transactions have taken place already, so we use their
1188 // drawing state. However, SurfaceFlinger's own transaction has not
1189 // happened yet, so we must use the current state layer list
1190 // (soon to become the drawing state list).
1191 //
1192 sp<const DisplayDevice> disp;
1193 uint32_t currentlayerStack = 0;
1194 for (size_t i=0; i<count; i++) {
1195 // NOTE: we rely on the fact that layers are sorted by
1196 // layerStack first (so we don't have to traverse the list
1197 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001198 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001199 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001200 if (i==0 || currentlayerStack != layerStack) {
1201 currentlayerStack = layerStack;
1202 // figure out if this layerstack is mirrored
1203 // (more than one display) if so, pick the default display,
1204 // if not, pick the only display it's on.
1205 disp.clear();
1206 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1207 sp<const DisplayDevice> hw(mDisplays[dpy]);
1208 if (hw->getLayerStack() == currentlayerStack) {
1209 if (disp == NULL) {
1210 disp = hw;
1211 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001212 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001213 break;
1214 }
1215 }
1216 }
1217 }
Chet Haase91d25932013-04-11 15:24:55 -07001218 if (disp == NULL) {
1219 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1220 // redraw after transform hint changes. See bug 8508397.
1221
1222 // could be null when this layer is using a layerStack
1223 // that is not visible on any display. Also can occur at
1224 // screen off/on times.
1225 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001226 }
Chet Haase91d25932013-04-11 15:24:55 -07001227 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001228 }
1229 }
1230
1231
Mathias Agopian3559b072012-08-15 13:46:03 -07001232 /*
1233 * Perform our own transaction if needed
1234 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001235
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001236 const LayerVector& layers(mDrawingState.layersSortedByZ);
1237 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001238 // layers have been added
1239 mVisibleRegionsDirty = true;
1240 }
1241
1242 // some layers might have been removed, so
1243 // we need to update the regions they're exposing.
1244 if (mLayersRemoved) {
1245 mLayersRemoved = false;
1246 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001247 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001248 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001249 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001250 if (currentLayers.indexOf(layer) < 0) {
1251 // this layer is not visible anymore
1252 // TODO: we could traverse the tree from front to back and
1253 // compute the actual visible region
1254 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001255 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001256 Region visibleReg = s.transform.transform(
1257 Region(Rect(s.active.w, s.active.h)));
1258 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001259 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001260 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001261 }
1262
1263 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001264}
1265
1266void SurfaceFlinger::commitTransaction()
1267{
1268 if (!mLayersPendingRemoval.isEmpty()) {
1269 // Notify removed layers now that they can't be drawn from
1270 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1271 mLayersPendingRemoval[i]->onRemoved();
1272 }
1273 mLayersPendingRemoval.clear();
1274 }
1275
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001276 // If this transaction is part of a window animation then the next frame
1277 // we composite should be considered an animation as well.
1278 mAnimCompositionPending = mAnimTransactionPending;
1279
Mathias Agopian4fec8732012-06-29 14:12:52 -07001280 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001281 mTransactionPending = false;
1282 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001283 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001284}
1285
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001286void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001287 const LayerVector& currentLayers, uint32_t layerStack,
1288 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001289{
Mathias Agopian841cde52012-03-01 15:44:37 -08001290 ATRACE_CALL();
1291
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001292 Region aboveOpaqueLayers;
1293 Region aboveCoveredLayers;
1294 Region dirty;
1295
Mathias Agopian87baae12012-07-31 12:38:26 -07001296 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001297
1298 size_t i = currentLayers.size();
1299 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001300 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301
1302 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001303 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304
Jesse Hall01e29052013-02-19 16:13:35 -08001305 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001306 if (s.layerStack != layerStack)
1307 continue;
1308
Mathias Agopianab028732010-03-16 16:41:46 -07001309 /*
1310 * opaqueRegion: area of a surface that is fully opaque.
1311 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001313
1314 /*
1315 * visibleRegion: area of a surface that is visible on screen
1316 * and not fully transparent. This is essentially the layer's
1317 * footprint minus the opaque regions above it.
1318 * Areas covered by a translucent surface are considered visible.
1319 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001320 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001321
1322 /*
1323 * coveredRegion: area of a surface that is covered by all
1324 * visible regions above it (which includes the translucent areas).
1325 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001326 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001327
Jesse Halla8026d22012-09-25 13:25:04 -07001328 /*
1329 * transparentRegion: area of a surface that is hinted to be completely
1330 * transparent. This is only used to tell when the layer has no visible
1331 * non-transparent regions and can be removed from the layer list. It
1332 * does not affect the visibleRegion of this layer or any layers
1333 * beneath it. The hint may not be correct if apps don't respect the
1334 * SurfaceView restrictions (which, sadly, some don't).
1335 */
1336 Region transparentRegion;
1337
Mathias Agopianab028732010-03-16 16:41:46 -07001338
1339 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001340 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001341 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001342 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001343 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001344 if (!visibleRegion.isEmpty()) {
1345 // Remove the transparent area from the visible region
1346 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001347 const Transform tr(s.transform);
1348 if (tr.transformed()) {
1349 if (tr.preserveRects()) {
1350 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001351 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001352 } else {
1353 // transformation too complex, can't do the
1354 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001355 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001356 }
1357 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001358 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001359 }
Mathias Agopianab028732010-03-16 16:41:46 -07001360 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001361
Mathias Agopianab028732010-03-16 16:41:46 -07001362 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001363 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001364 if (s.alpha==255 && !translucent &&
1365 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1366 // the opaque region is the layer's footprint
1367 opaqueRegion = visibleRegion;
1368 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001369 }
1370 }
1371
Mathias Agopianab028732010-03-16 16:41:46 -07001372 // Clip the covered region to the visible region
1373 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1374
1375 // Update aboveCoveredLayers for next (lower) layer
1376 aboveCoveredLayers.orSelf(visibleRegion);
1377
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378 // subtract the opaque region covered by the layers above us
1379 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001380
1381 // compute this layer's dirty region
1382 if (layer->contentDirty) {
1383 // we need to invalidate the whole region
1384 dirty = visibleRegion;
1385 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001386 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001387 layer->contentDirty = false;
1388 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001389 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001390 * the exposed region consists of two components:
1391 * 1) what's VISIBLE now and was COVERED before
1392 * 2) what's EXPOSED now less what was EXPOSED before
1393 *
1394 * note that (1) is conservative, we start with the whole
1395 * visible region but only keep what used to be covered by
1396 * something -- which mean it may have been exposed.
1397 *
1398 * (2) handles areas that were not covered by anything but got
1399 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001400 */
Mathias Agopianab028732010-03-16 16:41:46 -07001401 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001402 const Region oldVisibleRegion = layer->visibleRegion;
1403 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001404 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1405 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406 }
1407 dirty.subtractSelf(aboveOpaqueLayers);
1408
1409 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001410 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001411
Mathias Agopianab028732010-03-16 16:41:46 -07001412 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001413 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001414
Jesse Halla8026d22012-09-25 13:25:04 -07001415 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001416 layer->setVisibleRegion(visibleRegion);
1417 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001418 layer->setVisibleNonTransparentRegion(
1419 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001420 }
1421
Mathias Agopian87baae12012-07-31 12:38:26 -07001422 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001423}
1424
Mathias Agopian87baae12012-07-31 12:38:26 -07001425void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1426 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001427 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001428 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1429 if (hw->getLayerStack() == layerStack) {
1430 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001431 }
1432 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001433}
1434
1435void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001436{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001437 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001438
Mathias Agopian4fec8732012-06-29 14:12:52 -07001439 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001440 const LayerVector& layers(mDrawingState.layersSortedByZ);
1441 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001442 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001443 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001444 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001445 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001446 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001447 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001448
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001449 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001450}
1451
Mathias Agopianad456f92011-01-13 17:53:01 -08001452void SurfaceFlinger::invalidateHwcGeometry()
1453{
1454 mHwWorkListDirty = true;
1455}
1456
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001457
Mathias Agopiancd60f992012-08-16 16:28:27 -07001458void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001459 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001460{
Mathias Agopian87baae12012-07-31 12:38:26 -07001461 Region dirtyRegion(inDirtyRegion);
1462
Mathias Agopianb8a55602009-06-26 19:06:36 -07001463 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001464 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001465
Mathias Agopian42977342012-08-05 00:40:46 -07001466 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001467 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001468 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1469 // takes a rectangle, we must make sure to update that whole
1470 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001471 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001472 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001473 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001474 // We need to redraw the rectangle that will be updated
1475 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001476 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001477 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001478 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001479 } else {
1480 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001481 dirtyRegion.set(hw->bounds());
1482 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483 }
1484 }
1485
Mathias Agopianff2ed702013-09-01 21:36:12 -07001486 if (CC_LIKELY(!mDaltonize)) {
1487 doComposeSurfaces(hw, dirtyRegion);
1488 } else {
1489 RenderEngine& engine(getRenderEngine());
1490 engine.beginGroup(mDaltonizer());
1491 doComposeSurfaces(hw, dirtyRegion);
1492 engine.endGroup();
1493 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001494
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001495 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001496 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001497
1498 // swap buffers (presentation)
1499 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500}
1501
Mathias Agopiancd60f992012-08-16 16:28:27 -07001502void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001503{
Mathias Agopian3f844832013-08-07 21:24:32 -07001504 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001505 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001506 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001507 HWComposer::LayerListIterator cur = hwc.begin(id);
1508 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001509
Mathias Agopian85d751c2012-08-29 16:59:24 -07001510 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1511 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001512 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001513 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1514 hw->getDisplayName().string());
1515 return;
1516 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001517
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001518 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001519 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001520 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001521 // when using overlays, we assume a fully transparent framebuffer
1522 // NOTE: we could reduce how much we need to clear, for instance
1523 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001524 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001525 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001526 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001527 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001528 // we start with the whole screen area
1529 const Region bounds(hw->getBounds());
1530
1531 // we remove the scissor part
1532 // we're left with the letterbox region
1533 // (common case is that letterbox ends-up being empty)
1534 const Region letterbox(bounds.subtract(hw->getScissor()));
1535
1536 // compute the area to clear
1537 Region region(hw->undefinedRegion.merge(letterbox));
1538
1539 // but limit it to the dirty region
1540 region.andSelf(dirty);
1541
Mathias Agopianb9494d52012-04-18 02:28:45 -07001542 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001543 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001544 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001545 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001546 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001547 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001548
Mathias Agopian766dc492012-10-30 18:08:06 -07001549 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1550 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001551 // scissor on the main display. It should never be needed
1552 // anyways (though in theory it could since the API allows it).
1553 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001554 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001555 if (scissor != bounds) {
1556 // scissor doesn't match the screen's dimensions, so we
1557 // need to clear everything outside of it and enable
1558 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001559
Mathias Agopianf45c5102012-10-24 16:29:17 -07001560 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001561 const uint32_t height = hw->getHeight();
1562 engine.setScissor(scissor.left, height - scissor.bottom,
1563 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001564 }
1565 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001566 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001567
Mathias Agopian85d751c2012-08-29 16:59:24 -07001568 /*
1569 * and then, render the layers targeted at the framebuffer
1570 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001571
Mathias Agopian13127d82013-03-05 17:47:11 -08001572 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001573 const size_t count = layers.size();
1574 const Transform& tr = hw->getTransform();
1575 if (cur != end) {
1576 // we're using h/w composer
1577 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001578 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001579 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001580 if (!clip.isEmpty()) {
1581 switch (cur->getCompositionType()) {
1582 case HWC_OVERLAY: {
1583 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1584 && i
1585 && layer->isOpaque()
1586 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001587 // never clear the very first layer since we're
1588 // guaranteed the FB is already cleared
1589 layer->clearWithOpenGL(hw, clip);
1590 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001591 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001592 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001593 case HWC_FRAMEBUFFER: {
1594 layer->draw(hw, clip);
1595 break;
1596 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001597 case HWC_FRAMEBUFFER_TARGET: {
1598 // this should not happen as the iterator shouldn't
1599 // let us get there.
1600 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1601 break;
1602 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001603 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001604 }
1605 layer->setAcquireFence(hw, *cur);
1606 }
1607 } else {
1608 // we're not using h/w composer
1609 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001610 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001611 const Region clip(dirty.intersect(
1612 tr.transform(layer->visibleRegion)));
1613 if (!clip.isEmpty()) {
1614 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001615 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001616 }
1617 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001618
1619 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001620 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001621}
1622
Mathias Agopian3f844832013-08-07 21:24:32 -07001623void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001624 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001625 RenderEngine& engine(getRenderEngine());
1626 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001627}
1628
Mathias Agopianac9fa422013-02-11 16:40:36 -08001629void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1630 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001631 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001632 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001633{
Mathias Agopian96f08192010-06-02 23:28:45 -07001634 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001635 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001636
Mathias Agopian96f08192010-06-02 23:28:45 -07001637 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001638 Mutex::Autolock _l(mStateLock);
1639 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001640 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001641}
1642
Mathias Agopian3f844832013-08-07 21:24:32 -07001643status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001644 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001645 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001646 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001647 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001648 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001649 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001650 return NO_ERROR;
1651 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001652 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001653}
1654
Mathias Agopian3f844832013-08-07 21:24:32 -07001655uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001656 return android_atomic_release_load(&mTransactionFlags);
1657}
1658
Mathias Agopian3f844832013-08-07 21:24:32 -07001659uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001660 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1661}
1662
Mathias Agopian3f844832013-08-07 21:24:32 -07001663uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001664 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1665 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001666 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667 }
1668 return old;
1669}
1670
Mathias Agopian8b33f032012-07-24 20:43:54 -07001671void SurfaceFlinger::setTransactionState(
1672 const Vector<ComposerState>& state,
1673 const Vector<DisplayState>& displays,
1674 uint32_t flags)
1675{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001676 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001677 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001678 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001679
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001680 if (flags & eAnimation) {
1681 // For window updates that are part of an animation we must wait for
1682 // previous animation "frames" to be handled.
1683 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001684 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001685 if (CC_UNLIKELY(err != NO_ERROR)) {
1686 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001687 // caller after a few seconds.
1688 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1689 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001690 mAnimTransactionPending = false;
1691 break;
1692 }
1693 }
1694 }
1695
Mathias Agopiane57f2922012-08-09 16:29:12 -07001696 size_t count = displays.size();
1697 for (size_t i=0 ; i<count ; i++) {
1698 const DisplayState& s(displays[i]);
1699 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001700 }
1701
Mathias Agopiane57f2922012-08-09 16:29:12 -07001702 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001703 for (size_t i=0 ; i<count ; i++) {
1704 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001705 // Here we need to check that the interface we're given is indeed
1706 // one of our own. A malicious client could give us a NULL
1707 // IInterface, or one of its own or even one of our own but a
1708 // different type. All these situations would cause us to crash.
1709 //
1710 // NOTE: it would be better to use RTTI as we could directly check
1711 // that we have a Client*. however, RTTI is disabled in Android.
1712 if (s.client != NULL) {
1713 sp<IBinder> binder = s.client->asBinder();
1714 if (binder != NULL) {
1715 String16 desc(binder->getInterfaceDescriptor());
1716 if (desc == ISurfaceComposerClient::descriptor) {
1717 sp<Client> client( static_cast<Client *>(s.client.get()) );
1718 transactionFlags |= setClientStateLocked(client, s.state);
1719 }
1720 }
1721 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001722 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001723
Mathias Agopian386aa982011-11-07 21:58:03 -08001724 if (transactionFlags) {
1725 // this triggers the transaction
1726 setTransactionFlags(transactionFlags);
1727
1728 // if this is a synchronous transaction, wait for it to take effect
1729 // before returning.
1730 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001731 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001732 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001733 if (flags & eAnimation) {
1734 mAnimTransactionPending = true;
1735 }
1736 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001737 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1738 if (CC_UNLIKELY(err != NO_ERROR)) {
1739 // just in case something goes wrong in SF, return to the
1740 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001741 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1742 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001743 break;
1744 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001745 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001746 }
1747}
1748
Mathias Agopiane57f2922012-08-09 16:29:12 -07001749uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1750{
Jesse Hall9a143922012-10-04 16:29:19 -07001751 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1752 if (dpyIdx < 0)
1753 return 0;
1754
Mathias Agopiane57f2922012-08-09 16:29:12 -07001755 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001756 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001757 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001758 const uint32_t what = s.what;
1759 if (what & DisplayState::eSurfaceChanged) {
1760 if (disp.surface->asBinder() != s.surface->asBinder()) {
1761 disp.surface = s.surface;
1762 flags |= eDisplayTransactionNeeded;
1763 }
1764 }
1765 if (what & DisplayState::eLayerStackChanged) {
1766 if (disp.layerStack != s.layerStack) {
1767 disp.layerStack = s.layerStack;
1768 flags |= eDisplayTransactionNeeded;
1769 }
1770 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001771 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001772 if (disp.orientation != s.orientation) {
1773 disp.orientation = s.orientation;
1774 flags |= eDisplayTransactionNeeded;
1775 }
1776 if (disp.frame != s.frame) {
1777 disp.frame = s.frame;
1778 flags |= eDisplayTransactionNeeded;
1779 }
1780 if (disp.viewport != s.viewport) {
1781 disp.viewport = s.viewport;
1782 flags |= eDisplayTransactionNeeded;
1783 }
1784 }
1785 }
1786 return flags;
1787}
1788
1789uint32_t SurfaceFlinger::setClientStateLocked(
1790 const sp<Client>& client,
1791 const layer_state_t& s)
1792{
1793 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001794 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001795 if (layer != 0) {
1796 const uint32_t what = s.what;
1797 if (what & layer_state_t::ePositionChanged) {
1798 if (layer->setPosition(s.x, s.y))
1799 flags |= eTraversalNeeded;
1800 }
1801 if (what & layer_state_t::eLayerChanged) {
1802 // NOTE: index needs to be calculated before we update the state
1803 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1804 if (layer->setLayer(s.z)) {
1805 mCurrentState.layersSortedByZ.removeAt(idx);
1806 mCurrentState.layersSortedByZ.add(layer);
1807 // we need traversal (state changed)
1808 // AND transaction (list changed)
1809 flags |= eTransactionNeeded|eTraversalNeeded;
1810 }
1811 }
1812 if (what & layer_state_t::eSizeChanged) {
1813 if (layer->setSize(s.w, s.h)) {
1814 flags |= eTraversalNeeded;
1815 }
1816 }
1817 if (what & layer_state_t::eAlphaChanged) {
1818 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1819 flags |= eTraversalNeeded;
1820 }
1821 if (what & layer_state_t::eMatrixChanged) {
1822 if (layer->setMatrix(s.matrix))
1823 flags |= eTraversalNeeded;
1824 }
1825 if (what & layer_state_t::eTransparentRegionChanged) {
1826 if (layer->setTransparentRegionHint(s.transparentRegion))
1827 flags |= eTraversalNeeded;
1828 }
1829 if (what & layer_state_t::eVisibilityChanged) {
1830 if (layer->setFlags(s.flags, s.mask))
1831 flags |= eTraversalNeeded;
1832 }
1833 if (what & layer_state_t::eCropChanged) {
1834 if (layer->setCrop(s.crop))
1835 flags |= eTraversalNeeded;
1836 }
1837 if (what & layer_state_t::eLayerStackChanged) {
1838 // NOTE: index needs to be calculated before we update the state
1839 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1840 if (layer->setLayerStack(s.layerStack)) {
1841 mCurrentState.layersSortedByZ.removeAt(idx);
1842 mCurrentState.layersSortedByZ.add(layer);
1843 // we need traversal (state changed)
1844 // AND transaction (list changed)
1845 flags |= eTransactionNeeded|eTraversalNeeded;
1846 }
1847 }
1848 }
1849 return flags;
1850}
1851
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001852status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001853 const String8& name,
1854 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001855 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1856 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001857{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001858 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001859 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001860 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001861 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001862 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001863 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001864
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001865 status_t result = NO_ERROR;
1866
1867 sp<Layer> layer;
1868
Mathias Agopian3165cc22012-08-08 19:42:09 -07001869 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1870 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001871 result = createNormalLayer(client,
1872 name, w, h, flags, format,
1873 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001874 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001875 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001876 result = createDimLayer(client,
1877 name, w, h, flags,
1878 handle, gbp, &layer);
1879 break;
1880 default:
1881 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001882 break;
1883 }
1884
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001885 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001886 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001887 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001888 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001889 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001890}
1891
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001892status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1893 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1894 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001895{
1896 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001897 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001898 case PIXEL_FORMAT_TRANSPARENT:
1899 case PIXEL_FORMAT_TRANSLUCENT:
1900 format = PIXEL_FORMAT_RGBA_8888;
1901 break;
1902 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001903#ifdef NO_RGBX_8888
1904 format = PIXEL_FORMAT_RGB_565;
1905#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001906 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001907#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001908 break;
1909 }
1910
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001911#ifdef NO_RGBX_8888
1912 if (format == PIXEL_FORMAT_RGBX_8888)
1913 format = PIXEL_FORMAT_RGBA_8888;
1914#endif
1915
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001916 *outLayer = new Layer(this, client, name, w, h, flags);
1917 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1918 if (err == NO_ERROR) {
1919 *handle = (*outLayer)->getHandle();
1920 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001921 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001922
1923 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1924 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001925}
1926
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001927status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1928 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1929 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001930{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001931 *outLayer = new LayerDim(this, client, name, w, h, flags);
1932 *handle = (*outLayer)->getHandle();
1933 *gbp = (*outLayer)->getBufferQueue();
1934 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001935}
1936
Mathias Agopianac9fa422013-02-11 16:40:36 -08001937status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001938{
Mathias Agopian67106042013-03-14 19:18:13 -07001939 // called by the window manager when it wants to remove a Layer
1940 status_t err = NO_ERROR;
1941 sp<Layer> l(client->getLayerUser(handle));
1942 if (l != NULL) {
1943 err = removeLayer(l);
1944 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1945 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001946 }
1947 return err;
1948}
1949
Mathias Agopian13127d82013-03-05 17:47:11 -08001950status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001951{
Mathias Agopian67106042013-03-14 19:18:13 -07001952 // called by ~LayerCleaner() when all references to the IBinder (handle)
1953 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001954 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001955 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001956 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07001957 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001958 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001959 "error removing layer=%p (%s)", l.get(), strerror(-err));
1960 }
1961 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001962}
1963
Mathias Agopianb60314a2012-04-10 22:09:54 -07001964// ---------------------------------------------------------------------------
1965
Andy McFadden13a082e2012-08-24 10:16:42 -07001966void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08001967 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07001968 Vector<ComposerState> state;
1969 Vector<DisplayState> displays;
1970 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08001971 d.what = DisplayState::eDisplayProjectionChanged |
1972 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08001973 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08001974 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07001975 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001976 d.frame.makeInvalid();
1977 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001978 displays.add(d);
1979 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001980 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07001981
1982 const nsecs_t period =
1983 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
1984 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07001985}
1986
1987void SurfaceFlinger::initializeDisplays() {
1988 class MessageScreenInitialized : public MessageBase {
1989 SurfaceFlinger* flinger;
1990 public:
1991 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
1992 virtual bool handler() {
1993 flinger->onInitializeDisplays();
1994 return true;
1995 }
1996 };
1997 sp<MessageBase> msg = new MessageScreenInitialized(this);
1998 postMessageAsync(msg); // we may be called from main thread, use async message
1999}
2000
2001
Mathias Agopiancde87a32012-09-13 14:09:01 -07002002void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002003 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2004 if (hw->isScreenAcquired()) {
2005 // this is expected, e.g. when power manager wakes up during boot
2006 ALOGD(" screen was previously acquired");
2007 return;
2008 }
2009
Mathias Agopian42977342012-08-05 00:40:46 -07002010 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002011 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002012 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002013 // built-in display, tell the HWC
2014 getHwComposer().acquire(type);
2015
2016 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2017 // FIXME: eventthread only knows about the main display right now
2018 mEventThread->onScreenAcquired();
2019 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002020 }
Mathias Agopian20128302012-08-13 18:32:13 -07002021 mVisibleRegionsDirty = true;
2022 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002023}
2024
Mathias Agopiancde87a32012-09-13 14:09:01 -07002025void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002026 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2027 if (!hw->isScreenAcquired()) {
2028 ALOGD(" screen was previously released");
2029 return;
2030 }
2031
2032 hw->releaseScreen();
2033 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002034 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002035 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002036 // FIXME: eventthread only knows about the main display right now
2037 mEventThread->onScreenReleased();
2038 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002039
2040 // built-in display, tell the HWC
2041 getHwComposer().release(type);
2042 }
2043 mVisibleRegionsDirty = true;
2044 // from this point on, SF will stop drawing on this display
2045}
2046
2047void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2048 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002049 SurfaceFlinger& mFlinger;
2050 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002051 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002052 MessageScreenAcquired(SurfaceFlinger& flinger,
2053 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002054 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002055 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2056 if (hw == NULL) {
2057 ALOGE("Attempt to unblank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002058 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002059 ALOGW("Attempt to unblank virtual display");
2060 } else {
2061 mFlinger.onScreenAcquired(hw);
2062 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002063 return true;
2064 }
2065 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002066 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2067 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002068}
2069
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002070void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002071 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002072 SurfaceFlinger& mFlinger;
2073 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002074 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002075 MessageScreenReleased(SurfaceFlinger& flinger,
2076 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002077 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002078 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2079 if (hw == NULL) {
2080 ALOGE("Attempt to blank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002081 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002082 ALOGW("Attempt to blank virtual display");
2083 } else {
2084 mFlinger.onScreenReleased(hw);
2085 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002086 return true;
2087 }
2088 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002089 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2090 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002091}
2092
2093// ---------------------------------------------------------------------------
2094
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002095status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2096{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002097 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002098
Mathias Agopianbd115332013-04-18 16:41:04 -07002099 IPCThreadState* ipc = IPCThreadState::self();
2100 const int pid = ipc->getCallingPid();
2101 const int uid = ipc->getCallingUid();
2102 if ((uid != AID_SHELL) &&
2103 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002104 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002105 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002106 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002107 // Try to get the main lock, but don't insist if we can't
2108 // (this would indicate SF is stuck, but we want to be able to
2109 // print something in dumpsys).
2110 int retry = 3;
2111 while (mStateLock.tryLock()<0 && --retry>=0) {
2112 usleep(1000000);
2113 }
2114 const bool locked(retry >= 0);
2115 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002116 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002117 "SurfaceFlinger appears to be unresponsive, "
2118 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002119 }
2120
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002121 bool dumpAll = true;
2122 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002123 size_t numArgs = args.size();
2124 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002125 if ((index < numArgs) &&
2126 (args[index] == String16("--list"))) {
2127 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002128 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002129 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002130 }
2131
2132 if ((index < numArgs) &&
2133 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002134 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002135 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002136 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002137 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002138
2139 if ((index < numArgs) &&
2140 (args[index] == String16("--latency-clear"))) {
2141 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002142 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002143 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002144 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002145 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002146
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002147 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002148 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002149 }
2150
Mathias Agopian9795c422009-08-26 16:36:26 -07002151 if (locked) {
2152 mStateLock.unlock();
2153 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002154 }
2155 write(fd, result.string(), result.size());
2156 return NO_ERROR;
2157}
2158
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002159void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002160 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002161{
2162 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2163 const size_t count = currentLayers.size();
2164 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002165 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002166 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002167 }
2168}
2169
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002170void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002171 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002172{
2173 String8 name;
2174 if (index < args.size()) {
2175 name = String8(args[index]);
2176 index++;
2177 }
2178
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002179 const nsecs_t period =
2180 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2181 result.appendFormat("%lld\n", period);
2182
2183 if (name.isEmpty()) {
2184 mAnimFrameTracker.dump(result);
2185 } else {
2186 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2187 const size_t count = currentLayers.size();
2188 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002189 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002190 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002191 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002192 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002193 }
2194 }
2195}
2196
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002197void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002198 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002199{
2200 String8 name;
2201 if (index < args.size()) {
2202 name = String8(args[index]);
2203 index++;
2204 }
2205
2206 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2207 const size_t count = currentLayers.size();
2208 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002209 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002210 if (name.isEmpty() || (name == layer->getName())) {
2211 layer->clearStats();
2212 }
2213 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002214
2215 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002216}
2217
Jamie Gennis6547ff42013-07-16 20:12:42 -07002218// This should only be called from the main thread. Otherwise it would need
2219// the lock and should use mCurrentState rather than mDrawingState.
2220void SurfaceFlinger::logFrameStats() {
2221 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2222 const size_t count = drawingLayers.size();
2223 for (size_t i=0 ; i<count ; i++) {
2224 const sp<Layer>& layer(drawingLayers[i]);
2225 layer->logFrameStats();
2226 }
2227
2228 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2229}
2230
Andy McFadden4803b742012-09-24 19:07:20 -07002231/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2232{
2233 static const char* config =
2234 " [sf"
2235#ifdef NO_RGBX_8888
2236 " NO_RGBX_8888"
2237#endif
2238#ifdef HAS_CONTEXT_PRIORITY
2239 " HAS_CONTEXT_PRIORITY"
2240#endif
2241#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2242 " NEVER_DEFAULT_TO_ASYNC_MODE"
2243#endif
2244#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2245 " TARGET_DISABLE_TRIPLE_BUFFERING"
2246#endif
2247 "]";
2248 result.append(config);
2249}
2250
Mathias Agopian74d211a2013-04-22 16:55:35 +02002251void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2252 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002253{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002254 bool colorize = false;
2255 if (index < args.size()
2256 && (args[index] == String16("--color"))) {
2257 colorize = true;
2258 index++;
2259 }
2260
2261 Colorizer colorizer(colorize);
2262
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002263 // figure out if we're stuck somewhere
2264 const nsecs_t now = systemTime();
2265 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2266 const nsecs_t inTransaction(mDebugInTransaction);
2267 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2268 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2269
2270 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002271 * Dump library configuration.
2272 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002273
2274 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002275 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002276 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002277 appendSfConfigString(result);
2278 appendUiConfigString(result);
2279 appendGuiConfigString(result);
2280 result.append("\n");
2281
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002282 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002283 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002284 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002285 result.append(SyncFeatures::getInstance().toString());
2286 result.append("\n");
2287
Andy McFadden4803b742012-09-24 19:07:20 -07002288 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002289 * Dump the visible layer list
2290 */
2291 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2292 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002293 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002294 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002295 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002296 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002297 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002298 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002299 }
2300
2301 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002302 * Dump Display state
2303 */
2304
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002305 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002306 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002307 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002308 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2309 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002310 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002311 }
2312
2313 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002314 * Dump SurfaceFlinger global state
2315 */
2316
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002317 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002318 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002319 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002320
Mathias Agopian888c8222012-08-04 21:10:38 -07002321 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002322 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002323
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002324 colorizer.bold(result);
2325 result.appendFormat("EGL implementation : %s\n",
2326 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2327 colorizer.reset(result);
2328 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002329 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002330
Mathias Agopian875d8e12013-06-07 15:35:48 -07002331 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002332
Mathias Agopian42977342012-08-05 00:40:46 -07002333 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002334 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002335 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002336 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002337 " last eglSwapBuffers() time: %f us\n"
2338 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002339 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002340 " refresh-rate : %f fps\n"
2341 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002342 " y-dpi : %f\n"
2343 " EGL_NATIVE_VISUAL_ID : %d\n"
2344 " gpu_to_cpu_unsupported : %d\n"
2345 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002346 mLastSwapBufferTime/1000.0,
2347 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002348 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002349 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2350 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002351 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2352 mEGLNativeVisualId,
2353 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002354
Mathias Agopian74d211a2013-04-22 16:55:35 +02002355 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002356 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002357
Mathias Agopian74d211a2013-04-22 16:55:35 +02002358 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002359 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002360
2361 /*
2362 * VSYNC state
2363 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002364 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002365
2366 /*
2367 * Dump HWComposer state
2368 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002369 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002370 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002371 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002372 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002373 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopianff2ed702013-09-01 21:36:12 -07002374 (mDebugDisableHWC || mDebugRegion || mDaltonize) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002375 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002376
2377 /*
2378 * Dump gralloc state
2379 */
2380 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2381 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002382}
2383
Mathias Agopian13127d82013-03-05 17:47:11 -08002384const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002385SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002386 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002387 wp<IBinder> dpy;
2388 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2389 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2390 dpy = mDisplays.keyAt(i);
2391 break;
2392 }
2393 }
2394 if (dpy == NULL) {
2395 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2396 // Just use the primary display so we have something to return
2397 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2398 }
2399 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002400}
2401
Keun young Park63f165f2012-08-31 10:53:36 -07002402bool SurfaceFlinger::startDdmConnection()
2403{
2404 void* libddmconnection_dso =
2405 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2406 if (!libddmconnection_dso) {
2407 return false;
2408 }
2409 void (*DdmConnection_start)(const char* name);
2410 DdmConnection_start =
2411 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2412 if (!DdmConnection_start) {
2413 dlclose(libddmconnection_dso);
2414 return false;
2415 }
2416 (*DdmConnection_start)(getServiceName());
2417 return true;
2418}
2419
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002420status_t SurfaceFlinger::onTransact(
2421 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2422{
2423 switch (code) {
2424 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002425 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002426 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002427 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002428 case BLANK:
2429 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002430 {
2431 // codes that require permission check
2432 IPCThreadState* ipc = IPCThreadState::self();
2433 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002434 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002435 if ((uid != AID_GRAPHICS) &&
2436 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002437 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002438 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2439 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002440 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002441 break;
2442 }
2443 case CAPTURE_SCREEN:
2444 {
2445 // codes that require permission check
2446 IPCThreadState* ipc = IPCThreadState::self();
2447 const int pid = ipc->getCallingPid();
2448 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002449 if ((uid != AID_GRAPHICS) &&
2450 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002451 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002452 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2453 return PERMISSION_DENIED;
2454 }
2455 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002456 }
2457 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002458
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002459 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2460 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002461 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002462 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002463 IPCThreadState* ipc = IPCThreadState::self();
2464 const int pid = ipc->getCallingPid();
2465 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002466 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002467 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002468 return PERMISSION_DENIED;
2469 }
2470 int n;
2471 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002472 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002473 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002474 return NO_ERROR;
2475 case 1002: // SHOW_UPDATES
2476 n = data.readInt32();
2477 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002478 invalidateHwcGeometry();
2479 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002480 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002481 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002482 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002483 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002484 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002485 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002486 setTransactionFlags(
2487 eTransactionNeeded|
2488 eDisplayTransactionNeeded|
2489 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002490 return NO_ERROR;
2491 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002492 case 1006:{ // send empty update
2493 signalRefresh();
2494 return NO_ERROR;
2495 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002496 case 1008: // toggle use of hw composer
2497 n = data.readInt32();
2498 mDebugDisableHWC = n ? 1 : 0;
2499 invalidateHwcGeometry();
2500 repaintEverything();
2501 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002502 case 1009: // toggle use of transform hint
2503 n = data.readInt32();
2504 mDebugDisableTransformHint = n ? 1 : 0;
2505 invalidateHwcGeometry();
2506 repaintEverything();
2507 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002508 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002509 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002510 reply->writeInt32(0);
2511 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002512 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002513 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002514 return NO_ERROR;
2515 case 1013: {
2516 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002517 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2518 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002519 return NO_ERROR;
2520 }
2521 case 1014: {
2522 // daltonize
2523 n = data.readInt32();
2524 switch (n % 10) {
2525 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2526 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2527 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2528 }
2529 if (n >= 10) {
2530 mDaltonizer.setMode(Daltonizer::correction);
2531 } else {
2532 mDaltonizer.setMode(Daltonizer::simulation);
2533 }
2534 mDaltonize = n > 0;
2535 invalidateHwcGeometry();
2536 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002537 }
2538 return NO_ERROR;
2539 }
2540 }
2541 return err;
2542}
2543
Mathias Agopian53331da2011-08-22 21:44:41 -07002544void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002545 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002546 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002547}
2548
Mathias Agopian59119e62010-10-11 12:37:43 -07002549// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002550// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002551// ---------------------------------------------------------------------------
2552
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002553/* The code below is here to handle b/8734824
2554 *
2555 * We create a IGraphicBufferProducer wrapper that forwards all calls
2556 * to the calling binder thread, where they are executed. This allows
2557 * the calling thread to be reused (on the other side) and not
2558 * depend on having "enough" binder threads to handle the requests.
2559 *
2560 */
2561
2562class GraphicProducerWrapper : public BBinder, public MessageHandler {
2563 sp<IGraphicBufferProducer> impl;
2564 sp<Looper> looper;
2565 status_t result;
2566 bool exitPending;
2567 bool exitRequested;
2568 mutable Barrier barrier;
2569 volatile int32_t memoryBarrier;
2570 uint32_t code;
2571 Parcel const* data;
2572 Parcel* reply;
2573
2574 enum {
2575 MSG_API_CALL,
2576 MSG_EXIT
2577 };
2578
2579 /*
2580 * this is called by our "fake" BpGraphicBufferProducer. We package the
2581 * data and reply Parcel and forward them to the calling thread.
2582 */
2583 virtual status_t transact(uint32_t code,
2584 const Parcel& data, Parcel* reply, uint32_t flags) {
2585 this->code = code;
2586 this->data = &data;
2587 this->reply = reply;
2588 android_atomic_acquire_store(0, &memoryBarrier);
2589 if (exitPending) {
2590 // if we've exited, we run the message synchronously right here
2591 handleMessage(Message(MSG_API_CALL));
2592 } else {
2593 barrier.close();
2594 looper->sendMessage(this, Message(MSG_API_CALL));
2595 barrier.wait();
2596 }
2597 return NO_ERROR;
2598 }
2599
2600 /*
2601 * here we run on the binder calling thread. All we've got to do is
2602 * call the real BpGraphicBufferProducer.
2603 */
2604 virtual void handleMessage(const Message& message) {
2605 android_atomic_release_load(&memoryBarrier);
2606 if (message.what == MSG_API_CALL) {
2607 impl->asBinder()->transact(code, data[0], reply);
2608 barrier.open();
2609 } else if (message.what == MSG_EXIT) {
2610 exitRequested = true;
2611 }
2612 }
2613
2614public:
2615 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2616 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2617 exitPending(false), exitRequested(false) {
2618 }
2619
2620 status_t waitForResponse() {
2621 do {
2622 looper->pollOnce(-1);
2623 } while (!exitRequested);
2624 return result;
2625 }
2626
2627 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002628 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002629 exitPending = true;
2630 looper->sendMessage(this, Message(MSG_EXIT));
2631 }
2632};
2633
2634
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002635status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2636 const sp<IGraphicBufferProducer>& producer,
2637 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002638 uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002639
2640 if (CC_UNLIKELY(display == 0))
2641 return BAD_VALUE;
2642
2643 if (CC_UNLIKELY(producer == 0))
2644 return BAD_VALUE;
2645
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002646 // if we have secure windows on this display, never allow the screen capture
2647 // unless the producer interface is local (i.e.: we can take a screenshot for
2648 // ourselves).
2649 if (!producer->asBinder()->localBinder()) {
2650 Mutex::Autolock _l(mStateLock);
2651 sp<const DisplayDevice> hw(getDisplayDevice(display));
2652 if (hw->getSecureLayerVisible()) {
2653 ALOGW("FB is protected: PERMISSION_DENIED");
2654 return PERMISSION_DENIED;
2655 }
2656 }
2657
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002658 class MessageCaptureScreen : public MessageBase {
2659 SurfaceFlinger* flinger;
2660 sp<IBinder> display;
2661 sp<IGraphicBufferProducer> producer;
2662 uint32_t reqWidth, reqHeight;
2663 uint32_t minLayerZ,maxLayerZ;
2664 status_t result;
2665 public:
2666 MessageCaptureScreen(SurfaceFlinger* flinger,
2667 const sp<IBinder>& display,
2668 const sp<IGraphicBufferProducer>& producer,
2669 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002670 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002671 : flinger(flinger), display(display), producer(producer),
2672 reqWidth(reqWidth), reqHeight(reqHeight),
2673 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2674 result(PERMISSION_DENIED)
2675 {
2676 }
2677 status_t getResult() const {
2678 return result;
2679 }
2680 virtual bool handler() {
2681 Mutex::Autolock _l(flinger->mStateLock);
2682 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002683 result = flinger->captureScreenImplLocked(hw,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002684 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002685 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002686 return true;
2687 }
2688 };
2689
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002690 // make sure to process transactions before screenshots -- a transaction
2691 // might already be pending but scheduled for VSYNC; this guarantees we
2692 // will handle it before the screenshot. When VSYNC finally arrives
2693 // the scheduled transaction will be a no-op. If no transactions are
2694 // scheduled at this time, this will end-up being a no-op as well.
2695 mEventQueue.invalidateTransactionNow();
2696
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002697 // this creates a "fake" BBinder which will serve as a "fake" remote
2698 // binder to receive the marshaled calls and forward them to the
2699 // real remote (a BpGraphicBufferProducer)
2700 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2701
2702 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2703 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002704 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002705 display, IGraphicBufferProducer::asInterface( wrapper ),
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002706 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002707
2708 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002709 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002710 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002711 }
2712 return res;
2713}
2714
Mathias Agopian180f10d2013-04-10 22:55:41 -07002715
2716void SurfaceFlinger::renderScreenImplLocked(
2717 const sp<const DisplayDevice>& hw,
2718 uint32_t reqWidth, uint32_t reqHeight,
2719 uint32_t minLayerZ, uint32_t maxLayerZ,
2720 bool yswap)
2721{
2722 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002723 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002724
2725 // get screen geometry
2726 const uint32_t hw_w = hw->getWidth();
2727 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002728 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2729
2730 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002731 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002732
2733 // set-up our viewport
Mathias Agopian3f844832013-08-07 21:24:32 -07002734 engine.setViewportAndProjection(reqWidth, reqHeight, hw_w, hw_h, yswap);
2735 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002736
2737 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002738 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002739
2740 const LayerVector& layers( mDrawingState.layersSortedByZ );
2741 const size_t count = layers.size();
2742 for (size_t i=0 ; i<count ; ++i) {
2743 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002744 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002745 if (state.layerStack == hw->getLayerStack()) {
2746 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2747 if (layer->isVisible()) {
2748 if (filtering) layer->setFiltering(true);
2749 layer->draw(hw);
2750 if (filtering) layer->setFiltering(false);
2751 }
2752 }
2753 }
2754 }
2755
2756 // compositionComplete is needed for older driver
2757 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002758 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002759}
2760
2761
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002762status_t SurfaceFlinger::captureScreenImplLocked(
2763 const sp<const DisplayDevice>& hw,
2764 const sp<IGraphicBufferProducer>& producer,
2765 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002766 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002767{
2768 ATRACE_CALL();
2769
Mathias Agopian180f10d2013-04-10 22:55:41 -07002770 // get screen geometry
2771 const uint32_t hw_w = hw->getWidth();
2772 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002773
Mathias Agopian180f10d2013-04-10 22:55:41 -07002774 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2775 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2776 reqWidth, reqHeight, hw_w, hw_h);
2777 return BAD_VALUE;
2778 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002779
Mathias Agopian180f10d2013-04-10 22:55:41 -07002780 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2781 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2782
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002783 // create a surface (because we're a producer, and we need to
2784 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07002785 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002786 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002787
2788 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002789 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002790 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2791 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002792
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002793 int err = 0;
2794 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07002795 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002796 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2797 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002798
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002799 if (err == NO_ERROR) {
2800 ANativeWindowBuffer* buffer;
2801 /* TODO: Once we have the sync framework everywhere this can use
2802 * server-side waits on the fence that dequeueBuffer returns.
2803 */
2804 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2805 if (result == NO_ERROR) {
2806 // create an EGLImage from the buffer so we can later
2807 // turn it into a texture
2808 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2809 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2810 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002811 // this binds the given EGLImage as a framebuffer for the
2812 // duration of this scope.
2813 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2814 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002815 // this will in fact render into our dequeued buffer
2816 // via an FBO, which means we didn't have to create
2817 // an EGLSurface and therefore we're not
2818 // dependent on the context's EGLConfig.
2819 renderScreenImplLocked(hw, reqWidth, reqHeight,
2820 minLayerZ, maxLayerZ, true);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002821 } else {
2822 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2823 result = INVALID_OPERATION;
2824 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002825 // destroy our image
2826 eglDestroyImageKHR(mEGLDisplay, image);
2827 } else {
2828 result = BAD_VALUE;
2829 }
2830 window->queueBuffer(window, buffer, -1);
2831 }
2832 } else {
2833 result = BAD_VALUE;
2834 }
2835 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2836 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002837
Mathias Agopian74c40c02010-09-29 13:02:36 -07002838 return result;
2839}
2840
Mathias Agopianfee2b462013-07-03 12:34:01 -07002841void SurfaceFlinger::checkScreenshot(const sp<GraphicBuffer>& buf, void const* vaddr,
2842 const sp<const DisplayDevice>& hw,
2843 uint32_t minLayerZ, uint32_t maxLayerZ) {
2844 if (DEBUG_SCREENSHOTS) {
2845 for (ssize_t y=0 ; y<buf->height ; y++) {
2846 uint32_t const * p = (uint32_t const *)vaddr + y*buf->stride;
2847 for (ssize_t x=0 ; x<buf->width ; x++) {
2848 if (p[x] != 0xFF000000) return;
2849 }
2850 }
2851 ALOGE("*** we just took a black screenshot ***\n"
2852 "requested minz=%d, maxz=%d, layerStack=%d",
2853 minLayerZ, maxLayerZ, hw->getLayerStack());
2854 const LayerVector& layers( mDrawingState.layersSortedByZ );
2855 const size_t count = layers.size();
2856 for (size_t i=0 ; i<count ; ++i) {
2857 const sp<Layer>& layer(layers[i]);
2858 const Layer::State& state(layer->getDrawingState());
2859 const bool visible = (state.layerStack == hw->getLayerStack())
2860 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2861 && (layer->isVisible());
2862 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
2863 visible ? '+' : '-',
2864 i, layer->getName().string(), state.layerStack, state.z,
2865 layer->isVisible(), state.flags, state.alpha);
2866 }
2867 }
2868}
2869
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002870// ---------------------------------------------------------------------------
2871
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002872SurfaceFlinger::LayerVector::LayerVector() {
2873}
2874
2875SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002876 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002877}
2878
2879int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2880 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002881{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002882 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002883 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2884 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002885
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002886 uint32_t ls = l->getCurrentState().layerStack;
2887 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002888 if (ls != rs)
2889 return ls - rs;
2890
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002891 uint32_t lz = l->getCurrentState().z;
2892 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002893 if (lz != rz)
2894 return lz - rz;
2895
2896 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002897}
2898
2899// ---------------------------------------------------------------------------
2900
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002901SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2902 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002903}
2904
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002905SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002906 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002907 viewport.makeInvalid();
2908 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002909}
2910
2911// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002912
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002913}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002914
2915
2916#if defined(__gl_h_)
2917#error "don't include gl/gl.h in this file"
2918#endif
2919
2920#if defined(__gl2_h_)
2921#error "don't include gl2/gl2.h in this file"
2922#endif