blob: f83cc06692fcf2060b3433005da99be237de13fe [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 Agopian2185f8b2013-09-18 16:20:26 -0700369EGLConfig SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId,
370 EGLint renderableType) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700371 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
372 // it is to be used with WIFI displays
373 EGLConfig config;
374 EGLint dummy;
375 status_t err;
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700376 EGLint wantedAttribute;
377 EGLint wantedAttributeValue;
Mathias Agopianda27af92012-09-13 18:17:13 -0700378
Mathias Agopian722b98f2012-09-25 18:24:31 -0700379 EGLAttributeVector attribs;
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700380 if (renderableType) {
381 attribs[EGL_RENDERABLE_TYPE] = renderableType;
382 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
383 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT|EGL_PBUFFER_BIT;
384 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
385 attribs[EGL_RED_SIZE] = 8;
386 attribs[EGL_GREEN_SIZE] = 8;
387 attribs[EGL_BLUE_SIZE] = 8;
388 wantedAttribute = EGL_NONE;
389 wantedAttributeValue = EGL_NONE;
Mathias Agopian722b98f2012-09-25 18:24:31 -0700390
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700391 } else {
392 // if no renderable type specified, fallback to a simplified query
393 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
394 wantedAttribute = EGL_NATIVE_VISUAL_ID;
395 wantedAttributeValue = nativeVisualId;
396 }
397
398 err = selectConfigForAttribute(display, attribs, wantedAttribute,
399 wantedAttributeValue, &config);
Mathias Agopian722b98f2012-09-25 18:24:31 -0700400 if (!err)
401 goto success;
402
Jesse Hallf21cffa2012-09-19 21:00:49 -0700403 return 0;
404
405success:
406 if (eglGetConfigAttrib(display, config, EGL_CONFIG_CAVEAT, &dummy))
Mathias Agopiana4912602012-07-12 14:25:33 -0700407 ALOGW_IF(dummy == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
Mathias Agopiana4912602012-07-12 14:25:33 -0700408 return config;
409}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800410
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700411void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700412
Mathias Agopiana4912602012-07-12 14:25:33 -0700413 ALOGI( "SurfaceFlinger's main thread ready to run. "
414 "Initializing graphics H/W...");
415
Jesse Hall692c7232012-11-08 15:41:56 -0800416 Mutex::Autolock _l(mStateLock);
417
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700418 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700419 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
420 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700421
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700422 // Initialize the H/W composer object. There may or may not be an
423 // actual hardware composer underneath.
424 mHwc = new HWComposer(this,
425 *static_cast<HWComposer::EventHandler *>(this));
426
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700427 // First try to get an ES2 config
428 mEGLConfig = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(), EGL_OPENGL_ES2_BIT);
429
430 if (!mEGLConfig) {
431 // If ES2 fails, try ES1
432 mEGLConfig = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(), EGL_OPENGL_ES_BIT);
433 }
434
435 if (!mEGLConfig) {
436 // still didn't work, probably because we're on the emulator...
437 // try a simplified query
438 ALOGW("no suitable EGLConfig found, trying a simpler query");
439 mEGLConfig = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(), 0);
440 }
441
442 if (!mEGLConfig) {
443 // this EGL is too lame for android
444 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
445 }
Mathias Agopian875d8e12013-06-07 15:35:48 -0700446
447 // print some debugging info
448 EGLint r,g,b,a;
449 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_RED_SIZE, &r);
450 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_GREEN_SIZE, &g);
451 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_BLUE_SIZE, &b);
452 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_ALPHA_SIZE, &a);
453 ALOGI("EGL informations:");
454 ALOGI("vendor : %s", eglQueryString(mEGLDisplay, EGL_VENDOR));
455 ALOGI("version : %s", eglQueryString(mEGLDisplay, EGL_VERSION));
456 ALOGI("extensions: %s", eglQueryString(mEGLDisplay, EGL_EXTENSIONS));
457 ALOGI("Client API: %s", eglQueryString(mEGLDisplay, EGL_CLIENT_APIS)?:"Not Supported");
458 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
459
460 // get a RenderEngine for the given display / config (can't fail)
461 mRenderEngine = RenderEngine::create(mEGLDisplay, mEGLConfig);
462
463 // retrieve the EGL context that was selected/created
464 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700465
Mathias Agopianed985572013-03-22 00:24:39 -0700466 // figure out which format we got
467 eglGetConfigAttrib(mEGLDisplay, mEGLConfig,
468 EGL_NATIVE_VISUAL_ID, &mEGLNativeVisualId);
469
Mathias Agopianda27af92012-09-13 18:17:13 -0700470 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
471 "couldn't create EGLContext");
472
Mathias Agopiancde87a32012-09-13 14:09:01 -0700473 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700474 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700475 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700476 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700477 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700478 // All non-virtual displays are currently considered secure.
479 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800480 createBuiltinDisplayLocked(type);
481 wp<IBinder> token = mBuiltinDisplays[i];
482
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700483 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
484 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i, bq);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700485 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700486 type, allocateHwcDisplayId(type), isSecure, token,
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700487 fbs, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700488 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700489 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700490 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700491 // for displays other than the main display, so we always
492 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700493 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700494 hw->acquireScreen();
495 }
496 mDisplays.add(token, hw);
497 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700498 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700499
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700500 // make the GLContext current so that we can create textures when creating Layers
501 // (which may happens before we render something)
502 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
503
Mathias Agopian028508c2012-07-25 21:12:12 -0700504 // start the EventThread
505 mEventThread = new EventThread(this);
506 mEventQueue.setEventThread(mEventThread);
507
Mathias Agopian92a979a2012-08-02 18:32:23 -0700508 // initialize our drawing state
509 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700510
Andy McFadden13a082e2012-08-24 10:16:42 -0700511 // set initial conditions (e.g. unblank default device)
512 initializeDisplays();
513
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700514 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700515 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800516}
517
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700518int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700519 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700520 type : mHwc->allocateDisplayId();
521}
522
Mathias Agopiana67e4182012-06-19 17:26:12 -0700523void SurfaceFlinger::startBootAnim() {
524 // start boot animation
525 property_set("service.bootanim.exit", "0");
526 property_set("ctl.start", "bootanim");
527}
528
Mathias Agopian875d8e12013-06-07 15:35:48 -0700529size_t SurfaceFlinger::getMaxTextureSize() const {
530 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700531}
532
Mathias Agopian875d8e12013-06-07 15:35:48 -0700533size_t SurfaceFlinger::getMaxViewportDims() const {
534 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700535}
536
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800537// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800538
Jamie Gennis582270d2011-08-17 18:19:00 -0700539bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800540 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800541 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800542 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700543 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800544}
545
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700546status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800547 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700548 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800549 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700550 type = i;
551 break;
552 }
553 }
554
555 if (type < 0) {
556 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700557 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700558
559 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700560 float xdpi = hwc.getDpiX(type);
561 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700562
563 // TODO: Not sure if display density should handled by SF any longer
564 class Density {
565 static int getDensityFromProperty(char const* propName) {
566 char property[PROPERTY_VALUE_MAX];
567 int density = 0;
568 if (property_get(propName, property, NULL) > 0) {
569 density = atoi(property);
570 }
571 return density;
572 }
573 public:
574 static int getEmuDensity() {
575 return getDensityFromProperty("qemu.sf.lcd_density"); }
576 static int getBuildDensity() {
577 return getDensityFromProperty("ro.sf.lcd_density"); }
578 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700579
580 if (type == DisplayDevice::DISPLAY_PRIMARY) {
581 // The density of the device is provided by a build property
582 float density = Density::getBuildDensity() / 160.0f;
583 if (density == 0) {
584 // the build doesn't provide a density -- this is wrong!
585 // use xdpi instead
586 ALOGE("ro.sf.lcd_density must be defined as a build property");
587 density = xdpi / 160.0f;
588 }
589 if (Density::getEmuDensity()) {
590 // if "qemu.sf.lcd_density" is specified, it overrides everything
591 xdpi = ydpi = density = Density::getEmuDensity();
592 density /= 160.0f;
593 }
594 info->density = density;
595
596 // TODO: this needs to go away (currently needed only by webkit)
597 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
598 info->orientation = hw->getOrientation();
Mathias Agopian1604f772012-09-18 21:54:42 -0700599 } else {
600 // TODO: where should this value come from?
601 static const int TV_DENSITY = 213;
602 info->density = TV_DENSITY / 160.0f;
603 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700604 }
605
Mathias Agopian1604f772012-09-18 21:54:42 -0700606 info->w = hwc.getWidth(type);
607 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700608 info->xdpi = xdpi;
609 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700610 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700611
612 // All non-virtual displays are currently considered secure.
613 info->secure = true;
614
Mathias Agopian888c8222012-08-04 21:10:38 -0700615 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700616}
617
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800618// ----------------------------------------------------------------------------
619
620sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800621 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700622}
623
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800624// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800625
626void SurfaceFlinger::waitForEvent() {
627 mEventQueue.waitMessage();
628}
629
630void SurfaceFlinger::signalTransaction() {
631 mEventQueue.invalidate();
632}
633
634void SurfaceFlinger::signalLayerUpdate() {
635 mEventQueue.invalidate();
636}
637
638void SurfaceFlinger::signalRefresh() {
639 mEventQueue.refresh();
640}
641
642status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
643 nsecs_t reltime, uint32_t flags) {
644 return mEventQueue.postMessage(msg, reltime);
645}
646
647status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
648 nsecs_t reltime, uint32_t flags) {
649 status_t res = mEventQueue.postMessage(msg, reltime);
650 if (res == NO_ERROR) {
651 msg->wait();
652 }
653 return res;
654}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800655
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700656void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700657 do {
658 waitForEvent();
659 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800660}
661
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700662void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700663 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 vsync");
668 return;
669 }
Jesse Hall9e663de2013-08-16 14:28:37 -0700670 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700671 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700672 mEventThread->onVSyncReceived(type, timestamp);
673 }
674}
675
676void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
677 if (mEventThread == NULL) {
678 // This is a temporary workaround for b/7145521. A non-null pointer
679 // does not mean EventThread has finished initializing, so this
680 // is not a correct fix.
681 ALOGW("WARNING: EventThread not started, ignoring hotplug");
682 return;
683 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700684
Jesse Hall9e663de2013-08-16 14:28:37 -0700685 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700686 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800687 if (connected) {
688 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700689 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800690 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
691 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700692 }
693 setTransactionFlags(eDisplayTransactionNeeded);
694
Andy McFadden9e9689c2012-10-10 18:17:51 -0700695 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700696 }
Mathias Agopian86303202012-07-24 22:46:10 -0700697}
698
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700699void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
700 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700701}
702
Mathias Agopian4fec8732012-06-29 14:12:52 -0700703void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800704 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800705 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700706 case MessageQueue::TRANSACTION:
707 handleMessageTransaction();
708 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700709 case MessageQueue::INVALIDATE:
710 handleMessageTransaction();
711 handleMessageInvalidate();
712 signalRefresh();
713 break;
714 case MessageQueue::REFRESH:
715 handleMessageRefresh();
716 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800717 }
718}
719
Mathias Agopian4fec8732012-06-29 14:12:52 -0700720void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700721 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700722 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700723 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700724 }
725}
726
727void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700728 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700729 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700730}
731
732void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700733 ATRACE_CALL();
734 preComposition();
735 rebuildLayerStacks();
736 setUpHWComposer();
737 doDebugFlashRegions();
738 doComposition();
739 postComposition();
740}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700741
Mathias Agopiancd60f992012-08-16 16:28:27 -0700742void SurfaceFlinger::doDebugFlashRegions()
743{
744 // is debugging enabled
745 if (CC_LIKELY(!mDebugRegion))
746 return;
747
748 const bool repaintEverything = mRepaintEverything;
749 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
750 const sp<DisplayDevice>& hw(mDisplays[dpy]);
751 if (hw->canDraw()) {
752 // transform the dirty region into this screen's coordinate space
753 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
754 if (!dirtyRegion.isEmpty()) {
755 // redraw the whole screen
756 doComposeSurfaces(hw, Region(hw->bounds()));
757
758 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700759 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700760 RenderEngine& engine(getRenderEngine());
761 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
762
Mathias Agopiancd60f992012-08-16 16:28:27 -0700763 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700764 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700765 }
766 }
767 }
768
769 postFramebuffer();
770
771 if (mDebugRegion > 1) {
772 usleep(mDebugRegion * 1000);
773 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700774
775 HWComposer& hwc(getHwComposer());
776 if (hwc.initCheck() == NO_ERROR) {
777 status_t err = hwc.prepare();
778 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
779 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700780}
781
782void SurfaceFlinger::preComposition()
783{
784 bool needExtraInvalidate = false;
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 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700789 needExtraInvalidate = true;
790 }
791 }
792 if (needExtraInvalidate) {
793 signalLayerUpdate();
794 }
795}
796
797void SurfaceFlinger::postComposition()
798{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700799 const LayerVector& layers(mDrawingState.layersSortedByZ);
800 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700801 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700802 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700803 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800804
805 if (mAnimCompositionPending) {
806 mAnimCompositionPending = false;
807
808 const HWComposer& hwc = getHwComposer();
809 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800810 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800811 mAnimFrameTracker.setActualPresentFence(presentFence);
812 } else {
813 // The HWC doesn't support present fences, so use the refresh
814 // timestamp instead.
815 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
816 mAnimFrameTracker.setActualPresentTime(presentTime);
817 }
818 mAnimFrameTracker.advanceFrame();
819 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700820}
821
822void SurfaceFlinger::rebuildLayerStacks() {
823 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700824 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700825 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700826 mVisibleRegionsDirty = false;
827 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700828
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700829 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700830 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700831 Region opaqueRegion;
832 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800833 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700834 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700835 const Transform& tr(hw->getTransform());
836 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700837 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700838 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700839 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700840
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700841 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700842 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700843 const sp<Layer>& layer(layers[i]);
844 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700845 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700846 Region drawRegion(tr.transform(
847 layer->visibleNonTransparentRegion));
848 drawRegion.andSelf(bounds);
849 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700850 layersSortedByZ.add(layer);
851 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700852 }
853 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700854 }
Mathias Agopian42977342012-08-05 00:40:46 -0700855 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700856 hw->undefinedRegion.set(bounds);
857 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
858 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700859 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700860 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700861}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700862
Mathias Agopiancd60f992012-08-16 16:28:27 -0700863void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700864 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
865 mDisplays[dpy]->beginFrame();
866 }
867
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700868 HWComposer& hwc(getHwComposer());
869 if (hwc.initCheck() == NO_ERROR) {
870 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700871 if (CC_UNLIKELY(mHwWorkListDirty)) {
872 mHwWorkListDirty = false;
873 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
874 sp<const DisplayDevice> hw(mDisplays[dpy]);
875 const int32_t id = hw->getHwcDisplayId();
876 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800877 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700878 hw->getVisibleLayersSortedByZ());
879 const size_t count = currentLayers.size();
880 if (hwc.createWorkList(id, count) == NO_ERROR) {
881 HWComposer::LayerListIterator cur = hwc.begin(id);
882 const HWComposer::LayerListIterator end = hwc.end(id);
883 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800884 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700885 layer->setGeometry(hw, *cur);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700886 if (mDebugDisableHWC || mDebugRegion || mDaltonize) {
Jamie Gennisa4310c82012-09-25 20:26:00 -0700887 cur->setSkip(true);
888 }
889 }
890 }
891 }
892 }
893 }
894
895 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700896 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700897 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700898 const int32_t id = hw->getHwcDisplayId();
899 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800900 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700901 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700902 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700903 HWComposer::LayerListIterator cur = hwc.begin(id);
904 const HWComposer::LayerListIterator end = hwc.end(id);
905 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
906 /*
907 * update the per-frame h/w composer data for each layer
908 * and build the transparent region of the FB
909 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800910 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700911 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700912 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700913 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700914 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700915
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700916 status_t err = hwc.prepare();
917 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700918
919 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
920 sp<const DisplayDevice> hw(mDisplays[dpy]);
921 hw->prepareFrame(hwc);
922 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700923 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700924}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700925
Mathias Agopiancd60f992012-08-16 16:28:27 -0700926void SurfaceFlinger::doComposition() {
927 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700928 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700929 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700930 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700931 if (hw->canDraw()) {
932 // transform the dirty region into this screen's coordinate space
933 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800934
935 // repaint the framebuffer (if needed)
936 doDisplayComposition(hw, dirtyRegion);
937
Mathias Agopiancd60f992012-08-16 16:28:27 -0700938 hw->dirtyRegion.clear();
939 hw->flip(hw->swapRegion);
940 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700941 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700942 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700943 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700944 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700945 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700946}
947
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800948void SurfaceFlinger::postFramebuffer()
949{
Mathias Agopian841cde52012-03-01 15:44:37 -0800950 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800951
Mathias Agopiana44b0412011-10-16 18:46:35 -0700952 const nsecs_t now = systemTime();
953 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700954
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700955 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700956 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700957 if (!hwc.supportsFramebufferTarget()) {
958 // EGL spec says:
959 // "surface must be bound to the calling thread's current context,
960 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700961 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700962 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700963 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700964 }
965
Mathias Agopian6da15f42013-09-25 20:40:07 -0700966 // make the default display current because the VirtualDisplayDevice code cannot
967 // deal with dequeueBuffer() being called outside of the composition loop; however
968 // the code below can call glFlush() which is allowed (and does in some case) call
969 // dequeueBuffer().
970 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
971
Mathias Agopian92a979a2012-08-02 18:32:23 -0700972 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700973 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800974 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700975 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700976 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700977 int32_t id = hw->getHwcDisplayId();
978 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700979 HWComposer::LayerListIterator cur = hwc.begin(id);
980 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700981 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700982 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700983 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700984 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700985 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700986 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700987 }
Jesse Hallef194142012-06-14 14:45:17 -0700988 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800989 }
990
Mathias Agopiana44b0412011-10-16 18:46:35 -0700991 mLastSwapBufferTime = systemTime() - now;
992 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700993
994 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
995 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
996 logFrameStats();
997 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800998}
999
Mathias Agopian87baae12012-07-31 12:38:26 -07001000void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001001{
Mathias Agopian841cde52012-03-01 15:44:37 -08001002 ATRACE_CALL();
1003
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001004 // here we keep a copy of the drawing state (that is the state that's
1005 // going to be overwritten by handleTransactionLocked()) outside of
1006 // mStateLock so that the side-effects of the State assignment
1007 // don't happen with mStateLock held (which can cause deadlocks).
1008 State drawingState(mDrawingState);
1009
Mathias Agopianca4d3602011-05-19 15:38:14 -07001010 Mutex::Autolock _l(mStateLock);
1011 const nsecs_t now = systemTime();
1012 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001013
Mathias Agopianca4d3602011-05-19 15:38:14 -07001014 // Here we're guaranteed that some transaction flags are set
1015 // so we can call handleTransactionLocked() unconditionally.
1016 // We call getTransactionFlags(), which will also clear the flags,
1017 // with mStateLock held to guarantee that mCurrentState won't change
1018 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001019
Mathias Agopiane57f2922012-08-09 16:29:12 -07001020 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001021 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001022
Mathias Agopianca4d3602011-05-19 15:38:14 -07001023 mLastTransactionTime = systemTime() - now;
1024 mDebugInTransaction = 0;
1025 invalidateHwcGeometry();
1026 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001027}
1028
Mathias Agopian87baae12012-07-31 12:38:26 -07001029void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001030{
1031 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001032 const size_t count = currentLayers.size();
1033
1034 /*
1035 * Traversal of the children
1036 * (perform the transaction for each of them if needed)
1037 */
1038
Mathias Agopian3559b072012-08-15 13:46:03 -07001039 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001040 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001041 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001042 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1043 if (!trFlags) continue;
1044
1045 const uint32_t flags = layer->doTransaction(0);
1046 if (flags & Layer::eVisibleRegion)
1047 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001048 }
1049 }
1050
1051 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001052 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001053 */
1054
Mathias Agopiane57f2922012-08-09 16:29:12 -07001055 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001056 // here we take advantage of Vector's copy-on-write semantics to
1057 // improve performance by skipping the transaction entirely when
1058 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001059 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1060 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001061 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001062 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001063 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001064 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001065
1066 // find the displays that were removed
1067 // (ie: in drawing state but not in current state)
1068 // also handle displays that changed
1069 // (ie: displays that are in both lists)
1070 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001071 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1072 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001073 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001074 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001075 // Call makeCurrent() on the primary display so we can
1076 // be sure that nothing associated with this display
1077 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001078 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001079 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001080 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1081 if (hw != NULL)
1082 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001083 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001084 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001085 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001086 } else {
1087 ALOGW("trying to remove the main display");
1088 }
1089 } else {
1090 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001091 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001092 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001093 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001094 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001095 // recreating the DisplayDevice, so we just remove it
1096 // from the drawing state, so that it get re-added
1097 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001098 sp<DisplayDevice> hw(getDisplayDevice(display));
1099 if (hw != NULL)
1100 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001101 mDisplays.removeItem(display);
1102 mDrawingState.displays.removeItemsAt(i);
1103 dc--; i--;
1104 // at this point we must loop to the next item
1105 continue;
1106 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001107
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001108 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001109 if (disp != NULL) {
1110 if (state.layerStack != draw[i].layerStack) {
1111 disp->setLayerStack(state.layerStack);
1112 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001113 if ((state.orientation != draw[i].orientation)
1114 || (state.viewport != draw[i].viewport)
1115 || (state.frame != draw[i].frame))
1116 {
1117 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001118 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001119 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001120 }
1121 }
1122 }
1123
1124 // find displays that were added
1125 // (ie: in current state but not in drawing state)
1126 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001127 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001128 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001129
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001130 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001131 sp<IGraphicBufferProducer> producer;
1132 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
1133
Jesse Hall02d86562013-03-25 14:43:23 -07001134 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001135 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001136 // Virtual displays without a surface are dormant:
1137 // they have external state (layer stack, projection,
1138 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001139 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001140
Jesse Hall02d86562013-03-25 14:43:23 -07001141 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001142 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1143 *mHwc, hwcDisplayId, state.surface, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001144 state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001145
1146 dispSurface = vds;
1147 if (hwcDisplayId >= 0) {
1148 producer = vds;
1149 } else {
1150 // There won't be any interaction with HWC for this virtual display,
1151 // so the GLES driver can pass buffers directly to the sink.
1152 producer = state.surface;
1153 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001154 }
1155 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001156 ALOGE_IF(state.surface!=NULL,
1157 "adding a supported display, but rendering "
1158 "surface is provided (%p), ignoring it",
1159 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001160 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001161 // for supported (by hwc) displays we provide our
1162 // own rendering surface
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001163 dispSurface = new FramebufferSurface(*mHwc, state.type, bq);
1164 producer = bq;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001165 }
1166
1167 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001168 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001169 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001170 state.type, hwcDisplayId, state.isSecure,
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001171 display, dispSurface, producer, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001172 hw->setLayerStack(state.layerStack);
1173 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001174 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001175 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001176 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001177 if (state.isVirtualDisplay()) {
1178 if (hwcDisplayId >= 0) {
1179 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1180 hw->getWidth(), hw->getHeight(),
1181 hw->getFormat());
1182 }
1183 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001184 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001185 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001186 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001187 }
1188 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001189 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001190 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001191
Mathias Agopian84300952012-11-21 16:02:13 -08001192 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1193 // The transform hint might have changed for some layers
1194 // (either because a display has changed, or because a layer
1195 // as changed).
1196 //
1197 // Walk through all the layers in currentLayers,
1198 // and update their transform hint.
1199 //
1200 // If a layer is visible only on a single display, then that
1201 // display is used to calculate the hint, otherwise we use the
1202 // default display.
1203 //
1204 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1205 // the hint is set before we acquire a buffer from the surface texture.
1206 //
1207 // NOTE: layer transactions have taken place already, so we use their
1208 // drawing state. However, SurfaceFlinger's own transaction has not
1209 // happened yet, so we must use the current state layer list
1210 // (soon to become the drawing state list).
1211 //
1212 sp<const DisplayDevice> disp;
1213 uint32_t currentlayerStack = 0;
1214 for (size_t i=0; i<count; i++) {
1215 // NOTE: we rely on the fact that layers are sorted by
1216 // layerStack first (so we don't have to traverse the list
1217 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001218 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001219 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001220 if (i==0 || currentlayerStack != layerStack) {
1221 currentlayerStack = layerStack;
1222 // figure out if this layerstack is mirrored
1223 // (more than one display) if so, pick the default display,
1224 // if not, pick the only display it's on.
1225 disp.clear();
1226 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1227 sp<const DisplayDevice> hw(mDisplays[dpy]);
1228 if (hw->getLayerStack() == currentlayerStack) {
1229 if (disp == NULL) {
1230 disp = hw;
1231 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001232 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001233 break;
1234 }
1235 }
1236 }
1237 }
Chet Haase91d25932013-04-11 15:24:55 -07001238 if (disp == NULL) {
1239 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1240 // redraw after transform hint changes. See bug 8508397.
1241
1242 // could be null when this layer is using a layerStack
1243 // that is not visible on any display. Also can occur at
1244 // screen off/on times.
1245 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001246 }
Chet Haase91d25932013-04-11 15:24:55 -07001247 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001248 }
1249 }
1250
1251
Mathias Agopian3559b072012-08-15 13:46:03 -07001252 /*
1253 * Perform our own transaction if needed
1254 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001255
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001256 const LayerVector& layers(mDrawingState.layersSortedByZ);
1257 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001258 // layers have been added
1259 mVisibleRegionsDirty = true;
1260 }
1261
1262 // some layers might have been removed, so
1263 // we need to update the regions they're exposing.
1264 if (mLayersRemoved) {
1265 mLayersRemoved = false;
1266 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001267 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001268 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001269 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001270 if (currentLayers.indexOf(layer) < 0) {
1271 // this layer is not visible anymore
1272 // TODO: we could traverse the tree from front to back and
1273 // compute the actual visible region
1274 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001275 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001276 Region visibleReg = s.transform.transform(
1277 Region(Rect(s.active.w, s.active.h)));
1278 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001279 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001280 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001281 }
1282
1283 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001284}
1285
1286void SurfaceFlinger::commitTransaction()
1287{
1288 if (!mLayersPendingRemoval.isEmpty()) {
1289 // Notify removed layers now that they can't be drawn from
1290 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1291 mLayersPendingRemoval[i]->onRemoved();
1292 }
1293 mLayersPendingRemoval.clear();
1294 }
1295
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001296 // If this transaction is part of a window animation then the next frame
1297 // we composite should be considered an animation as well.
1298 mAnimCompositionPending = mAnimTransactionPending;
1299
Mathias Agopian4fec8732012-06-29 14:12:52 -07001300 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001301 mTransactionPending = false;
1302 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001303 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001304}
1305
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001307 const LayerVector& currentLayers, uint32_t layerStack,
1308 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309{
Mathias Agopian841cde52012-03-01 15:44:37 -08001310 ATRACE_CALL();
1311
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001312 Region aboveOpaqueLayers;
1313 Region aboveCoveredLayers;
1314 Region dirty;
1315
Mathias Agopian87baae12012-07-31 12:38:26 -07001316 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001317
1318 size_t i = currentLayers.size();
1319 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001320 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321
1322 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001323 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001324
Jesse Hall01e29052013-02-19 16:13:35 -08001325 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001326 if (s.layerStack != layerStack)
1327 continue;
1328
Mathias Agopianab028732010-03-16 16:41:46 -07001329 /*
1330 * opaqueRegion: area of a surface that is fully opaque.
1331 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001332 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001333
1334 /*
1335 * visibleRegion: area of a surface that is visible on screen
1336 * and not fully transparent. This is essentially the layer's
1337 * footprint minus the opaque regions above it.
1338 * Areas covered by a translucent surface are considered visible.
1339 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001340 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001341
1342 /*
1343 * coveredRegion: area of a surface that is covered by all
1344 * visible regions above it (which includes the translucent areas).
1345 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001346 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001347
Jesse Halla8026d22012-09-25 13:25:04 -07001348 /*
1349 * transparentRegion: area of a surface that is hinted to be completely
1350 * transparent. This is only used to tell when the layer has no visible
1351 * non-transparent regions and can be removed from the layer list. It
1352 * does not affect the visibleRegion of this layer or any layers
1353 * beneath it. The hint may not be correct if apps don't respect the
1354 * SurfaceView restrictions (which, sadly, some don't).
1355 */
1356 Region transparentRegion;
1357
Mathias Agopianab028732010-03-16 16:41:46 -07001358
1359 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001360 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001361 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001362 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001363 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001364 if (!visibleRegion.isEmpty()) {
1365 // Remove the transparent area from the visible region
1366 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001367 const Transform tr(s.transform);
1368 if (tr.transformed()) {
1369 if (tr.preserveRects()) {
1370 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001371 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001372 } else {
1373 // transformation too complex, can't do the
1374 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001375 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001376 }
1377 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001378 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001379 }
Mathias Agopianab028732010-03-16 16:41:46 -07001380 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381
Mathias Agopianab028732010-03-16 16:41:46 -07001382 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001383 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001384 if (s.alpha==255 && !translucent &&
1385 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1386 // the opaque region is the layer's footprint
1387 opaqueRegion = visibleRegion;
1388 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001389 }
1390 }
1391
Mathias Agopianab028732010-03-16 16:41:46 -07001392 // Clip the covered region to the visible region
1393 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1394
1395 // Update aboveCoveredLayers for next (lower) layer
1396 aboveCoveredLayers.orSelf(visibleRegion);
1397
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001398 // subtract the opaque region covered by the layers above us
1399 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001400
1401 // compute this layer's dirty region
1402 if (layer->contentDirty) {
1403 // we need to invalidate the whole region
1404 dirty = visibleRegion;
1405 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001406 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001407 layer->contentDirty = false;
1408 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001409 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001410 * the exposed region consists of two components:
1411 * 1) what's VISIBLE now and was COVERED before
1412 * 2) what's EXPOSED now less what was EXPOSED before
1413 *
1414 * note that (1) is conservative, we start with the whole
1415 * visible region but only keep what used to be covered by
1416 * something -- which mean it may have been exposed.
1417 *
1418 * (2) handles areas that were not covered by anything but got
1419 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001420 */
Mathias Agopianab028732010-03-16 16:41:46 -07001421 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001422 const Region oldVisibleRegion = layer->visibleRegion;
1423 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001424 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1425 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001426 }
1427 dirty.subtractSelf(aboveOpaqueLayers);
1428
1429 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001430 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431
Mathias Agopianab028732010-03-16 16:41:46 -07001432 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001433 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001434
Jesse Halla8026d22012-09-25 13:25:04 -07001435 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001436 layer->setVisibleRegion(visibleRegion);
1437 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001438 layer->setVisibleNonTransparentRegion(
1439 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001440 }
1441
Mathias Agopian87baae12012-07-31 12:38:26 -07001442 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001443}
1444
Mathias Agopian87baae12012-07-31 12:38:26 -07001445void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1446 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001447 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001448 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1449 if (hw->getLayerStack() == layerStack) {
1450 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001451 }
1452 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001453}
1454
1455void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001456{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001457 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001458
Mathias Agopian4fec8732012-06-29 14:12:52 -07001459 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001460 const LayerVector& layers(mDrawingState.layersSortedByZ);
1461 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001462 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001463 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001464 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001465 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001466 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001467 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001468
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001469 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001470}
1471
Mathias Agopianad456f92011-01-13 17:53:01 -08001472void SurfaceFlinger::invalidateHwcGeometry()
1473{
1474 mHwWorkListDirty = true;
1475}
1476
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001477
Mathias Agopiancd60f992012-08-16 16:28:27 -07001478void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001479 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480{
Mathias Agopian87baae12012-07-31 12:38:26 -07001481 Region dirtyRegion(inDirtyRegion);
1482
Mathias Agopianb8a55602009-06-26 19:06:36 -07001483 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001484 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001485
Mathias Agopian42977342012-08-05 00:40:46 -07001486 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001487 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001488 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1489 // takes a rectangle, we must make sure to update that whole
1490 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001491 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001492 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001493 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001494 // We need to redraw the rectangle that will be updated
1495 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001496 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001497 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001498 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001499 } else {
1500 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001501 dirtyRegion.set(hw->bounds());
1502 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001503 }
1504 }
1505
Mathias Agopianff2ed702013-09-01 21:36:12 -07001506 if (CC_LIKELY(!mDaltonize)) {
1507 doComposeSurfaces(hw, dirtyRegion);
1508 } else {
1509 RenderEngine& engine(getRenderEngine());
1510 engine.beginGroup(mDaltonizer());
1511 doComposeSurfaces(hw, dirtyRegion);
1512 engine.endGroup();
1513 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001514
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001515 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001516 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001517
1518 // swap buffers (presentation)
1519 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001520}
1521
Mathias Agopiancd60f992012-08-16 16:28:27 -07001522void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001523{
Mathias Agopian3f844832013-08-07 21:24:32 -07001524 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001525 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001526 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001527 HWComposer::LayerListIterator cur = hwc.begin(id);
1528 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001529
Mathias Agopian85d751c2012-08-29 16:59:24 -07001530 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1531 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001532 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001533 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1534 hw->getDisplayName().string());
1535 return;
1536 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001537
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001538 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001539 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001540 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001541 // when using overlays, we assume a fully transparent framebuffer
1542 // NOTE: we could reduce how much we need to clear, for instance
1543 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001544 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001545 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001546 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001547 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001548 // we start with the whole screen area
1549 const Region bounds(hw->getBounds());
1550
1551 // we remove the scissor part
1552 // we're left with the letterbox region
1553 // (common case is that letterbox ends-up being empty)
1554 const Region letterbox(bounds.subtract(hw->getScissor()));
1555
1556 // compute the area to clear
1557 Region region(hw->undefinedRegion.merge(letterbox));
1558
1559 // but limit it to the dirty region
1560 region.andSelf(dirty);
1561
Mathias Agopianb9494d52012-04-18 02:28:45 -07001562 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001563 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001564 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001565 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001566 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001567 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001568
Mathias Agopian766dc492012-10-30 18:08:06 -07001569 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1570 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001571 // scissor on the main display. It should never be needed
1572 // anyways (though in theory it could since the API allows it).
1573 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001574 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001575 if (scissor != bounds) {
1576 // scissor doesn't match the screen's dimensions, so we
1577 // need to clear everything outside of it and enable
1578 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001579
Mathias Agopianf45c5102012-10-24 16:29:17 -07001580 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001581 const uint32_t height = hw->getHeight();
1582 engine.setScissor(scissor.left, height - scissor.bottom,
1583 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001584 }
1585 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001586 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001587
Mathias Agopian85d751c2012-08-29 16:59:24 -07001588 /*
1589 * and then, render the layers targeted at the framebuffer
1590 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001591
Mathias Agopian13127d82013-03-05 17:47:11 -08001592 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001593 const size_t count = layers.size();
1594 const Transform& tr = hw->getTransform();
1595 if (cur != end) {
1596 // we're using h/w composer
1597 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001598 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001599 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001600 if (!clip.isEmpty()) {
1601 switch (cur->getCompositionType()) {
1602 case HWC_OVERLAY: {
1603 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1604 && i
1605 && layer->isOpaque()
1606 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001607 // never clear the very first layer since we're
1608 // guaranteed the FB is already cleared
1609 layer->clearWithOpenGL(hw, clip);
1610 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001611 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001612 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001613 case HWC_FRAMEBUFFER: {
1614 layer->draw(hw, clip);
1615 break;
1616 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001617 case HWC_FRAMEBUFFER_TARGET: {
1618 // this should not happen as the iterator shouldn't
1619 // let us get there.
1620 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1621 break;
1622 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001623 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001624 }
1625 layer->setAcquireFence(hw, *cur);
1626 }
1627 } else {
1628 // we're not using h/w composer
1629 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001630 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001631 const Region clip(dirty.intersect(
1632 tr.transform(layer->visibleRegion)));
1633 if (!clip.isEmpty()) {
1634 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001635 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001636 }
1637 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001638
1639 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001640 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001641}
1642
Mathias Agopian3f844832013-08-07 21:24:32 -07001643void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001644 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001645 RenderEngine& engine(getRenderEngine());
1646 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001647}
1648
Mathias Agopianac9fa422013-02-11 16:40:36 -08001649void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1650 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001651 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001652 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001653{
Mathias Agopian96f08192010-06-02 23:28:45 -07001654 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001655 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001656
Mathias Agopian96f08192010-06-02 23:28:45 -07001657 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001658 Mutex::Autolock _l(mStateLock);
1659 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001660 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001661}
1662
Mathias Agopian3f844832013-08-07 21:24:32 -07001663status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001664 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001665 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001666 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001667 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001668 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001669 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001670 return NO_ERROR;
1671 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001672 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001673}
1674
Mathias Agopian3f844832013-08-07 21:24:32 -07001675uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001676 return android_atomic_release_load(&mTransactionFlags);
1677}
1678
Mathias Agopian3f844832013-08-07 21:24:32 -07001679uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001680 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1681}
1682
Mathias Agopian3f844832013-08-07 21:24:32 -07001683uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001684 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1685 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001686 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001687 }
1688 return old;
1689}
1690
Mathias Agopian8b33f032012-07-24 20:43:54 -07001691void SurfaceFlinger::setTransactionState(
1692 const Vector<ComposerState>& state,
1693 const Vector<DisplayState>& displays,
1694 uint32_t flags)
1695{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001696 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001697 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001698 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001699
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001700 if (flags & eAnimation) {
1701 // For window updates that are part of an animation we must wait for
1702 // previous animation "frames" to be handled.
1703 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001704 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001705 if (CC_UNLIKELY(err != NO_ERROR)) {
1706 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001707 // caller after a few seconds.
1708 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1709 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001710 mAnimTransactionPending = false;
1711 break;
1712 }
1713 }
1714 }
1715
Mathias Agopiane57f2922012-08-09 16:29:12 -07001716 size_t count = displays.size();
1717 for (size_t i=0 ; i<count ; i++) {
1718 const DisplayState& s(displays[i]);
1719 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001720 }
1721
Mathias Agopiane57f2922012-08-09 16:29:12 -07001722 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001723 for (size_t i=0 ; i<count ; i++) {
1724 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001725 // Here we need to check that the interface we're given is indeed
1726 // one of our own. A malicious client could give us a NULL
1727 // IInterface, or one of its own or even one of our own but a
1728 // different type. All these situations would cause us to crash.
1729 //
1730 // NOTE: it would be better to use RTTI as we could directly check
1731 // that we have a Client*. however, RTTI is disabled in Android.
1732 if (s.client != NULL) {
1733 sp<IBinder> binder = s.client->asBinder();
1734 if (binder != NULL) {
1735 String16 desc(binder->getInterfaceDescriptor());
1736 if (desc == ISurfaceComposerClient::descriptor) {
1737 sp<Client> client( static_cast<Client *>(s.client.get()) );
1738 transactionFlags |= setClientStateLocked(client, s.state);
1739 }
1740 }
1741 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001742 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001743
Mathias Agopian386aa982011-11-07 21:58:03 -08001744 if (transactionFlags) {
1745 // this triggers the transaction
1746 setTransactionFlags(transactionFlags);
1747
1748 // if this is a synchronous transaction, wait for it to take effect
1749 // before returning.
1750 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001751 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001752 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001753 if (flags & eAnimation) {
1754 mAnimTransactionPending = true;
1755 }
1756 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001757 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1758 if (CC_UNLIKELY(err != NO_ERROR)) {
1759 // just in case something goes wrong in SF, return to the
1760 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001761 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1762 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001763 break;
1764 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001765 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001766 }
1767}
1768
Mathias Agopiane57f2922012-08-09 16:29:12 -07001769uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1770{
Jesse Hall9a143922012-10-04 16:29:19 -07001771 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1772 if (dpyIdx < 0)
1773 return 0;
1774
Mathias Agopiane57f2922012-08-09 16:29:12 -07001775 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001776 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001777 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001778 const uint32_t what = s.what;
1779 if (what & DisplayState::eSurfaceChanged) {
1780 if (disp.surface->asBinder() != s.surface->asBinder()) {
1781 disp.surface = s.surface;
1782 flags |= eDisplayTransactionNeeded;
1783 }
1784 }
1785 if (what & DisplayState::eLayerStackChanged) {
1786 if (disp.layerStack != s.layerStack) {
1787 disp.layerStack = s.layerStack;
1788 flags |= eDisplayTransactionNeeded;
1789 }
1790 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001791 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001792 if (disp.orientation != s.orientation) {
1793 disp.orientation = s.orientation;
1794 flags |= eDisplayTransactionNeeded;
1795 }
1796 if (disp.frame != s.frame) {
1797 disp.frame = s.frame;
1798 flags |= eDisplayTransactionNeeded;
1799 }
1800 if (disp.viewport != s.viewport) {
1801 disp.viewport = s.viewport;
1802 flags |= eDisplayTransactionNeeded;
1803 }
1804 }
1805 }
1806 return flags;
1807}
1808
1809uint32_t SurfaceFlinger::setClientStateLocked(
1810 const sp<Client>& client,
1811 const layer_state_t& s)
1812{
1813 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001814 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001815 if (layer != 0) {
1816 const uint32_t what = s.what;
1817 if (what & layer_state_t::ePositionChanged) {
1818 if (layer->setPosition(s.x, s.y))
1819 flags |= eTraversalNeeded;
1820 }
1821 if (what & layer_state_t::eLayerChanged) {
1822 // NOTE: index needs to be calculated before we update the state
1823 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1824 if (layer->setLayer(s.z)) {
1825 mCurrentState.layersSortedByZ.removeAt(idx);
1826 mCurrentState.layersSortedByZ.add(layer);
1827 // we need traversal (state changed)
1828 // AND transaction (list changed)
1829 flags |= eTransactionNeeded|eTraversalNeeded;
1830 }
1831 }
1832 if (what & layer_state_t::eSizeChanged) {
1833 if (layer->setSize(s.w, s.h)) {
1834 flags |= eTraversalNeeded;
1835 }
1836 }
1837 if (what & layer_state_t::eAlphaChanged) {
1838 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1839 flags |= eTraversalNeeded;
1840 }
1841 if (what & layer_state_t::eMatrixChanged) {
1842 if (layer->setMatrix(s.matrix))
1843 flags |= eTraversalNeeded;
1844 }
1845 if (what & layer_state_t::eTransparentRegionChanged) {
1846 if (layer->setTransparentRegionHint(s.transparentRegion))
1847 flags |= eTraversalNeeded;
1848 }
1849 if (what & layer_state_t::eVisibilityChanged) {
1850 if (layer->setFlags(s.flags, s.mask))
1851 flags |= eTraversalNeeded;
1852 }
1853 if (what & layer_state_t::eCropChanged) {
1854 if (layer->setCrop(s.crop))
1855 flags |= eTraversalNeeded;
1856 }
1857 if (what & layer_state_t::eLayerStackChanged) {
1858 // NOTE: index needs to be calculated before we update the state
1859 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1860 if (layer->setLayerStack(s.layerStack)) {
1861 mCurrentState.layersSortedByZ.removeAt(idx);
1862 mCurrentState.layersSortedByZ.add(layer);
1863 // we need traversal (state changed)
1864 // AND transaction (list changed)
1865 flags |= eTransactionNeeded|eTraversalNeeded;
1866 }
1867 }
1868 }
1869 return flags;
1870}
1871
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001872status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001873 const String8& name,
1874 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001875 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1876 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001877{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001878 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001879 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001880 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001881 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001882 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001883 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001884
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001885 status_t result = NO_ERROR;
1886
1887 sp<Layer> layer;
1888
Mathias Agopian3165cc22012-08-08 19:42:09 -07001889 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1890 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001891 result = createNormalLayer(client,
1892 name, w, h, flags, format,
1893 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001894 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001895 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001896 result = createDimLayer(client,
1897 name, w, h, flags,
1898 handle, gbp, &layer);
1899 break;
1900 default:
1901 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001902 break;
1903 }
1904
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001905 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001906 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001907 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001908 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001909 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001910}
1911
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001912status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1913 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1914 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001915{
1916 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001917 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001918 case PIXEL_FORMAT_TRANSPARENT:
1919 case PIXEL_FORMAT_TRANSLUCENT:
1920 format = PIXEL_FORMAT_RGBA_8888;
1921 break;
1922 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001923#ifdef NO_RGBX_8888
1924 format = PIXEL_FORMAT_RGB_565;
1925#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001926 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001927#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001928 break;
1929 }
1930
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001931#ifdef NO_RGBX_8888
1932 if (format == PIXEL_FORMAT_RGBX_8888)
1933 format = PIXEL_FORMAT_RGBA_8888;
1934#endif
1935
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001936 *outLayer = new Layer(this, client, name, w, h, flags);
1937 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1938 if (err == NO_ERROR) {
1939 *handle = (*outLayer)->getHandle();
1940 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001941 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001942
1943 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1944 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001945}
1946
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001947status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1948 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1949 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001950{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001951 *outLayer = new LayerDim(this, client, name, w, h, flags);
1952 *handle = (*outLayer)->getHandle();
1953 *gbp = (*outLayer)->getBufferQueue();
1954 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001955}
1956
Mathias Agopianac9fa422013-02-11 16:40:36 -08001957status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001958{
Mathias Agopian67106042013-03-14 19:18:13 -07001959 // called by the window manager when it wants to remove a Layer
1960 status_t err = NO_ERROR;
1961 sp<Layer> l(client->getLayerUser(handle));
1962 if (l != NULL) {
1963 err = removeLayer(l);
1964 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1965 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001966 }
1967 return err;
1968}
1969
Mathias Agopian13127d82013-03-05 17:47:11 -08001970status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001971{
Mathias Agopian67106042013-03-14 19:18:13 -07001972 // called by ~LayerCleaner() when all references to the IBinder (handle)
1973 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001974 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001975 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001976 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07001977 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001978 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001979 "error removing layer=%p (%s)", l.get(), strerror(-err));
1980 }
1981 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001982}
1983
Mathias Agopianb60314a2012-04-10 22:09:54 -07001984// ---------------------------------------------------------------------------
1985
Andy McFadden13a082e2012-08-24 10:16:42 -07001986void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08001987 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07001988 Vector<ComposerState> state;
1989 Vector<DisplayState> displays;
1990 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08001991 d.what = DisplayState::eDisplayProjectionChanged |
1992 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08001993 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08001994 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07001995 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001996 d.frame.makeInvalid();
1997 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001998 displays.add(d);
1999 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002000 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07002001
2002 const nsecs_t period =
2003 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2004 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002005}
2006
2007void SurfaceFlinger::initializeDisplays() {
2008 class MessageScreenInitialized : public MessageBase {
2009 SurfaceFlinger* flinger;
2010 public:
2011 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2012 virtual bool handler() {
2013 flinger->onInitializeDisplays();
2014 return true;
2015 }
2016 };
2017 sp<MessageBase> msg = new MessageScreenInitialized(this);
2018 postMessageAsync(msg); // we may be called from main thread, use async message
2019}
2020
2021
Mathias Agopiancde87a32012-09-13 14:09:01 -07002022void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002023 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2024 if (hw->isScreenAcquired()) {
2025 // this is expected, e.g. when power manager wakes up during boot
2026 ALOGD(" screen was previously acquired");
2027 return;
2028 }
2029
Mathias Agopian42977342012-08-05 00:40:46 -07002030 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002031 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002032 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002033 // built-in display, tell the HWC
2034 getHwComposer().acquire(type);
2035
2036 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2037 // FIXME: eventthread only knows about the main display right now
2038 mEventThread->onScreenAcquired();
2039 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002040 }
Mathias Agopian20128302012-08-13 18:32:13 -07002041 mVisibleRegionsDirty = true;
2042 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002043}
2044
Mathias Agopiancde87a32012-09-13 14:09:01 -07002045void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002046 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2047 if (!hw->isScreenAcquired()) {
2048 ALOGD(" screen was previously released");
2049 return;
2050 }
2051
2052 hw->releaseScreen();
2053 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002054 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002055 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002056 // FIXME: eventthread only knows about the main display right now
2057 mEventThread->onScreenReleased();
2058 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002059
2060 // built-in display, tell the HWC
2061 getHwComposer().release(type);
2062 }
2063 mVisibleRegionsDirty = true;
2064 // from this point on, SF will stop drawing on this display
2065}
2066
2067void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2068 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002069 SurfaceFlinger& mFlinger;
2070 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002071 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002072 MessageScreenAcquired(SurfaceFlinger& flinger,
2073 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002074 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002075 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2076 if (hw == NULL) {
2077 ALOGE("Attempt to unblank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002078 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002079 ALOGW("Attempt to unblank virtual display");
2080 } else {
2081 mFlinger.onScreenAcquired(hw);
2082 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002083 return true;
2084 }
2085 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002086 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2087 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002088}
2089
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002090void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002091 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002092 SurfaceFlinger& mFlinger;
2093 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002094 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002095 MessageScreenReleased(SurfaceFlinger& flinger,
2096 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002097 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002098 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2099 if (hw == NULL) {
2100 ALOGE("Attempt to blank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002101 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002102 ALOGW("Attempt to blank virtual display");
2103 } else {
2104 mFlinger.onScreenReleased(hw);
2105 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002106 return true;
2107 }
2108 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002109 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2110 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002111}
2112
2113// ---------------------------------------------------------------------------
2114
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002115status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2116{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002117 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002118
Mathias Agopianbd115332013-04-18 16:41:04 -07002119 IPCThreadState* ipc = IPCThreadState::self();
2120 const int pid = ipc->getCallingPid();
2121 const int uid = ipc->getCallingUid();
2122 if ((uid != AID_SHELL) &&
2123 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002124 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002125 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002126 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002127 // Try to get the main lock, but don't insist if we can't
2128 // (this would indicate SF is stuck, but we want to be able to
2129 // print something in dumpsys).
2130 int retry = 3;
2131 while (mStateLock.tryLock()<0 && --retry>=0) {
2132 usleep(1000000);
2133 }
2134 const bool locked(retry >= 0);
2135 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002136 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002137 "SurfaceFlinger appears to be unresponsive, "
2138 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002139 }
2140
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002141 bool dumpAll = true;
2142 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002143 size_t numArgs = args.size();
2144 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002145 if ((index < numArgs) &&
2146 (args[index] == String16("--list"))) {
2147 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002148 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002149 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002150 }
2151
2152 if ((index < numArgs) &&
2153 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002154 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002155 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002156 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002157 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002158
2159 if ((index < numArgs) &&
2160 (args[index] == String16("--latency-clear"))) {
2161 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002162 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002163 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002164 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002165 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002166
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002167 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002168 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002169 }
2170
Mathias Agopian9795c422009-08-26 16:36:26 -07002171 if (locked) {
2172 mStateLock.unlock();
2173 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002174 }
2175 write(fd, result.string(), result.size());
2176 return NO_ERROR;
2177}
2178
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002179void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002180 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002181{
2182 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2183 const size_t count = currentLayers.size();
2184 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002185 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002186 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002187 }
2188}
2189
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002190void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002191 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002192{
2193 String8 name;
2194 if (index < args.size()) {
2195 name = String8(args[index]);
2196 index++;
2197 }
2198
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002199 const nsecs_t period =
2200 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2201 result.appendFormat("%lld\n", period);
2202
2203 if (name.isEmpty()) {
2204 mAnimFrameTracker.dump(result);
2205 } else {
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]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002210 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002211 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002212 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002213 }
2214 }
2215}
2216
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002217void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002218 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002219{
2220 String8 name;
2221 if (index < args.size()) {
2222 name = String8(args[index]);
2223 index++;
2224 }
2225
2226 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2227 const size_t count = currentLayers.size();
2228 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002229 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002230 if (name.isEmpty() || (name == layer->getName())) {
2231 layer->clearStats();
2232 }
2233 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002234
2235 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002236}
2237
Jamie Gennis6547ff42013-07-16 20:12:42 -07002238// This should only be called from the main thread. Otherwise it would need
2239// the lock and should use mCurrentState rather than mDrawingState.
2240void SurfaceFlinger::logFrameStats() {
2241 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2242 const size_t count = drawingLayers.size();
2243 for (size_t i=0 ; i<count ; i++) {
2244 const sp<Layer>& layer(drawingLayers[i]);
2245 layer->logFrameStats();
2246 }
2247
2248 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2249}
2250
Andy McFadden4803b742012-09-24 19:07:20 -07002251/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2252{
2253 static const char* config =
2254 " [sf"
2255#ifdef NO_RGBX_8888
2256 " NO_RGBX_8888"
2257#endif
2258#ifdef HAS_CONTEXT_PRIORITY
2259 " HAS_CONTEXT_PRIORITY"
2260#endif
2261#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2262 " NEVER_DEFAULT_TO_ASYNC_MODE"
2263#endif
2264#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2265 " TARGET_DISABLE_TRIPLE_BUFFERING"
2266#endif
2267 "]";
2268 result.append(config);
2269}
2270
Mathias Agopian74d211a2013-04-22 16:55:35 +02002271void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2272 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002273{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002274 bool colorize = false;
2275 if (index < args.size()
2276 && (args[index] == String16("--color"))) {
2277 colorize = true;
2278 index++;
2279 }
2280
2281 Colorizer colorizer(colorize);
2282
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002283 // figure out if we're stuck somewhere
2284 const nsecs_t now = systemTime();
2285 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2286 const nsecs_t inTransaction(mDebugInTransaction);
2287 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2288 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2289
2290 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002291 * Dump library configuration.
2292 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002293
2294 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002295 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002296 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002297 appendSfConfigString(result);
2298 appendUiConfigString(result);
2299 appendGuiConfigString(result);
2300 result.append("\n");
2301
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002302 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002303 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002304 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002305 result.append(SyncFeatures::getInstance().toString());
2306 result.append("\n");
2307
Andy McFadden4803b742012-09-24 19:07:20 -07002308 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002309 * Dump the visible layer list
2310 */
2311 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2312 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002313 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002314 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002315 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002316 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002317 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002318 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002319 }
2320
2321 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002322 * Dump Display state
2323 */
2324
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002325 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002326 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002327 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002328 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2329 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002330 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002331 }
2332
2333 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002334 * Dump SurfaceFlinger global state
2335 */
2336
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002337 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002338 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002339 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002340
Mathias Agopian888c8222012-08-04 21:10:38 -07002341 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002342 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002343
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002344 colorizer.bold(result);
2345 result.appendFormat("EGL implementation : %s\n",
2346 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2347 colorizer.reset(result);
2348 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002349 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002350
Mathias Agopian875d8e12013-06-07 15:35:48 -07002351 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002352
Mathias Agopian42977342012-08-05 00:40:46 -07002353 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002354 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002355 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002356 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002357 " last eglSwapBuffers() time: %f us\n"
2358 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002359 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002360 " refresh-rate : %f fps\n"
2361 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002362 " y-dpi : %f\n"
2363 " EGL_NATIVE_VISUAL_ID : %d\n"
2364 " gpu_to_cpu_unsupported : %d\n"
2365 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002366 mLastSwapBufferTime/1000.0,
2367 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002368 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002369 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2370 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002371 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2372 mEGLNativeVisualId,
2373 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002374
Mathias Agopian74d211a2013-04-22 16:55:35 +02002375 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002376 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002377
Mathias Agopian74d211a2013-04-22 16:55:35 +02002378 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002379 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002380
2381 /*
2382 * VSYNC state
2383 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002384 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002385
2386 /*
2387 * Dump HWComposer state
2388 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002389 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002390 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002391 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002392 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002393 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopianff2ed702013-09-01 21:36:12 -07002394 (mDebugDisableHWC || mDebugRegion || mDaltonize) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002395 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002396
2397 /*
2398 * Dump gralloc state
2399 */
2400 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2401 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002402}
2403
Mathias Agopian13127d82013-03-05 17:47:11 -08002404const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002405SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002406 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002407 wp<IBinder> dpy;
2408 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2409 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2410 dpy = mDisplays.keyAt(i);
2411 break;
2412 }
2413 }
2414 if (dpy == NULL) {
2415 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2416 // Just use the primary display so we have something to return
2417 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2418 }
2419 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002420}
2421
Keun young Park63f165f2012-08-31 10:53:36 -07002422bool SurfaceFlinger::startDdmConnection()
2423{
2424 void* libddmconnection_dso =
2425 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2426 if (!libddmconnection_dso) {
2427 return false;
2428 }
2429 void (*DdmConnection_start)(const char* name);
2430 DdmConnection_start =
2431 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2432 if (!DdmConnection_start) {
2433 dlclose(libddmconnection_dso);
2434 return false;
2435 }
2436 (*DdmConnection_start)(getServiceName());
2437 return true;
2438}
2439
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002440status_t SurfaceFlinger::onTransact(
2441 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2442{
2443 switch (code) {
2444 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002445 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002446 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002447 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002448 case BLANK:
2449 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002450 {
2451 // codes that require permission check
2452 IPCThreadState* ipc = IPCThreadState::self();
2453 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002454 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002455 if ((uid != AID_GRAPHICS) &&
2456 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002457 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002458 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2459 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002460 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002461 break;
2462 }
2463 case CAPTURE_SCREEN:
2464 {
2465 // codes that require permission check
2466 IPCThreadState* ipc = IPCThreadState::self();
2467 const int pid = ipc->getCallingPid();
2468 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002469 if ((uid != AID_GRAPHICS) &&
2470 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002471 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002472 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2473 return PERMISSION_DENIED;
2474 }
2475 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002476 }
2477 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002478
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002479 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2480 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002481 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002482 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002483 IPCThreadState* ipc = IPCThreadState::self();
2484 const int pid = ipc->getCallingPid();
2485 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002486 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002487 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002488 return PERMISSION_DENIED;
2489 }
2490 int n;
2491 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002492 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002493 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002494 return NO_ERROR;
2495 case 1002: // SHOW_UPDATES
2496 n = data.readInt32();
2497 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002498 invalidateHwcGeometry();
2499 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002500 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002501 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002502 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002503 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002504 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002505 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002506 setTransactionFlags(
2507 eTransactionNeeded|
2508 eDisplayTransactionNeeded|
2509 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002510 return NO_ERROR;
2511 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002512 case 1006:{ // send empty update
2513 signalRefresh();
2514 return NO_ERROR;
2515 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002516 case 1008: // toggle use of hw composer
2517 n = data.readInt32();
2518 mDebugDisableHWC = n ? 1 : 0;
2519 invalidateHwcGeometry();
2520 repaintEverything();
2521 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002522 case 1009: // toggle use of transform hint
2523 n = data.readInt32();
2524 mDebugDisableTransformHint = n ? 1 : 0;
2525 invalidateHwcGeometry();
2526 repaintEverything();
2527 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002528 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002529 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002530 reply->writeInt32(0);
2531 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002532 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002533 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002534 return NO_ERROR;
2535 case 1013: {
2536 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002537 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2538 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002539 return NO_ERROR;
2540 }
2541 case 1014: {
2542 // daltonize
2543 n = data.readInt32();
2544 switch (n % 10) {
2545 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2546 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2547 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2548 }
2549 if (n >= 10) {
2550 mDaltonizer.setMode(Daltonizer::correction);
2551 } else {
2552 mDaltonizer.setMode(Daltonizer::simulation);
2553 }
2554 mDaltonize = n > 0;
2555 invalidateHwcGeometry();
2556 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002557 }
2558 return NO_ERROR;
2559 }
2560 }
2561 return err;
2562}
2563
Mathias Agopian53331da2011-08-22 21:44:41 -07002564void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002565 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002566 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002567}
2568
Mathias Agopian59119e62010-10-11 12:37:43 -07002569// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002570// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002571// ---------------------------------------------------------------------------
2572
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002573/* The code below is here to handle b/8734824
2574 *
2575 * We create a IGraphicBufferProducer wrapper that forwards all calls
2576 * to the calling binder thread, where they are executed. This allows
2577 * the calling thread to be reused (on the other side) and not
2578 * depend on having "enough" binder threads to handle the requests.
2579 *
2580 */
2581
2582class GraphicProducerWrapper : public BBinder, public MessageHandler {
2583 sp<IGraphicBufferProducer> impl;
2584 sp<Looper> looper;
2585 status_t result;
2586 bool exitPending;
2587 bool exitRequested;
2588 mutable Barrier barrier;
2589 volatile int32_t memoryBarrier;
2590 uint32_t code;
2591 Parcel const* data;
2592 Parcel* reply;
2593
2594 enum {
2595 MSG_API_CALL,
2596 MSG_EXIT
2597 };
2598
2599 /*
2600 * this is called by our "fake" BpGraphicBufferProducer. We package the
2601 * data and reply Parcel and forward them to the calling thread.
2602 */
2603 virtual status_t transact(uint32_t code,
2604 const Parcel& data, Parcel* reply, uint32_t flags) {
2605 this->code = code;
2606 this->data = &data;
2607 this->reply = reply;
2608 android_atomic_acquire_store(0, &memoryBarrier);
2609 if (exitPending) {
2610 // if we've exited, we run the message synchronously right here
2611 handleMessage(Message(MSG_API_CALL));
2612 } else {
2613 barrier.close();
2614 looper->sendMessage(this, Message(MSG_API_CALL));
2615 barrier.wait();
2616 }
2617 return NO_ERROR;
2618 }
2619
2620 /*
2621 * here we run on the binder calling thread. All we've got to do is
2622 * call the real BpGraphicBufferProducer.
2623 */
2624 virtual void handleMessage(const Message& message) {
2625 android_atomic_release_load(&memoryBarrier);
2626 if (message.what == MSG_API_CALL) {
2627 impl->asBinder()->transact(code, data[0], reply);
2628 barrier.open();
2629 } else if (message.what == MSG_EXIT) {
2630 exitRequested = true;
2631 }
2632 }
2633
2634public:
2635 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2636 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2637 exitPending(false), exitRequested(false) {
2638 }
2639
2640 status_t waitForResponse() {
2641 do {
2642 looper->pollOnce(-1);
2643 } while (!exitRequested);
2644 return result;
2645 }
2646
2647 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002648 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002649 exitPending = true;
2650 looper->sendMessage(this, Message(MSG_EXIT));
2651 }
2652};
2653
2654
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002655status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2656 const sp<IGraphicBufferProducer>& producer,
2657 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002658 uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002659
2660 if (CC_UNLIKELY(display == 0))
2661 return BAD_VALUE;
2662
2663 if (CC_UNLIKELY(producer == 0))
2664 return BAD_VALUE;
2665
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002666 // if we have secure windows on this display, never allow the screen capture
2667 // unless the producer interface is local (i.e.: we can take a screenshot for
2668 // ourselves).
2669 if (!producer->asBinder()->localBinder()) {
2670 Mutex::Autolock _l(mStateLock);
2671 sp<const DisplayDevice> hw(getDisplayDevice(display));
2672 if (hw->getSecureLayerVisible()) {
2673 ALOGW("FB is protected: PERMISSION_DENIED");
2674 return PERMISSION_DENIED;
2675 }
2676 }
2677
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002678 class MessageCaptureScreen : public MessageBase {
2679 SurfaceFlinger* flinger;
2680 sp<IBinder> display;
2681 sp<IGraphicBufferProducer> producer;
2682 uint32_t reqWidth, reqHeight;
2683 uint32_t minLayerZ,maxLayerZ;
2684 status_t result;
2685 public:
2686 MessageCaptureScreen(SurfaceFlinger* flinger,
2687 const sp<IBinder>& display,
2688 const sp<IGraphicBufferProducer>& producer,
2689 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002690 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002691 : flinger(flinger), display(display), producer(producer),
2692 reqWidth(reqWidth), reqHeight(reqHeight),
2693 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2694 result(PERMISSION_DENIED)
2695 {
2696 }
2697 status_t getResult() const {
2698 return result;
2699 }
2700 virtual bool handler() {
2701 Mutex::Autolock _l(flinger->mStateLock);
2702 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002703 result = flinger->captureScreenImplLocked(hw,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002704 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002705 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002706 return true;
2707 }
2708 };
2709
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002710 // make sure to process transactions before screenshots -- a transaction
2711 // might already be pending but scheduled for VSYNC; this guarantees we
2712 // will handle it before the screenshot. When VSYNC finally arrives
2713 // the scheduled transaction will be a no-op. If no transactions are
2714 // scheduled at this time, this will end-up being a no-op as well.
2715 mEventQueue.invalidateTransactionNow();
2716
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002717 // this creates a "fake" BBinder which will serve as a "fake" remote
2718 // binder to receive the marshaled calls and forward them to the
2719 // real remote (a BpGraphicBufferProducer)
2720 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2721
2722 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2723 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002724 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002725 display, IGraphicBufferProducer::asInterface( wrapper ),
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002726 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002727
2728 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002729 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002730 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002731 }
2732 return res;
2733}
2734
Mathias Agopian180f10d2013-04-10 22:55:41 -07002735
2736void SurfaceFlinger::renderScreenImplLocked(
2737 const sp<const DisplayDevice>& hw,
2738 uint32_t reqWidth, uint32_t reqHeight,
2739 uint32_t minLayerZ, uint32_t maxLayerZ,
2740 bool yswap)
2741{
2742 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002743 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002744
2745 // get screen geometry
2746 const uint32_t hw_w = hw->getWidth();
2747 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002748 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2749
2750 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002751 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002752
2753 // set-up our viewport
Mathias Agopian3f844832013-08-07 21:24:32 -07002754 engine.setViewportAndProjection(reqWidth, reqHeight, hw_w, hw_h, yswap);
2755 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002756
2757 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002758 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002759
2760 const LayerVector& layers( mDrawingState.layersSortedByZ );
2761 const size_t count = layers.size();
2762 for (size_t i=0 ; i<count ; ++i) {
2763 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002764 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002765 if (state.layerStack == hw->getLayerStack()) {
2766 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2767 if (layer->isVisible()) {
2768 if (filtering) layer->setFiltering(true);
2769 layer->draw(hw);
2770 if (filtering) layer->setFiltering(false);
2771 }
2772 }
2773 }
2774 }
2775
2776 // compositionComplete is needed for older driver
2777 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002778 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002779}
2780
2781
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002782status_t SurfaceFlinger::captureScreenImplLocked(
2783 const sp<const DisplayDevice>& hw,
2784 const sp<IGraphicBufferProducer>& producer,
2785 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002786 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002787{
2788 ATRACE_CALL();
2789
Mathias Agopian180f10d2013-04-10 22:55:41 -07002790 // get screen geometry
2791 const uint32_t hw_w = hw->getWidth();
2792 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002793
Mathias Agopian180f10d2013-04-10 22:55:41 -07002794 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2795 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2796 reqWidth, reqHeight, hw_w, hw_h);
2797 return BAD_VALUE;
2798 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002799
Mathias Agopian180f10d2013-04-10 22:55:41 -07002800 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2801 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2802
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002803 // create a surface (because we're a producer, and we need to
2804 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07002805 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002806 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002807
2808 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002809 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002810 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2811 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002812
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002813 int err = 0;
2814 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07002815 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002816 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2817 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002818
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002819 if (err == NO_ERROR) {
2820 ANativeWindowBuffer* buffer;
2821 /* TODO: Once we have the sync framework everywhere this can use
2822 * server-side waits on the fence that dequeueBuffer returns.
2823 */
2824 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2825 if (result == NO_ERROR) {
2826 // create an EGLImage from the buffer so we can later
2827 // turn it into a texture
2828 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2829 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2830 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002831 // this binds the given EGLImage as a framebuffer for the
2832 // duration of this scope.
2833 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2834 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002835 // this will in fact render into our dequeued buffer
2836 // via an FBO, which means we didn't have to create
2837 // an EGLSurface and therefore we're not
2838 // dependent on the context's EGLConfig.
2839 renderScreenImplLocked(hw, reqWidth, reqHeight,
2840 minLayerZ, maxLayerZ, true);
Mathias Agopiand5556842013-09-19 17:08:37 -07002841
2842 if (DEBUG_SCREENSHOTS) {
2843 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
2844 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
2845 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
2846 hw, minLayerZ, maxLayerZ);
2847 delete [] pixels;
2848 }
2849
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002850 } else {
2851 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2852 result = INVALID_OPERATION;
2853 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002854 // destroy our image
2855 eglDestroyImageKHR(mEGLDisplay, image);
2856 } else {
2857 result = BAD_VALUE;
2858 }
2859 window->queueBuffer(window, buffer, -1);
2860 }
2861 } else {
2862 result = BAD_VALUE;
2863 }
2864 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2865 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002866
Mathias Agopian74c40c02010-09-29 13:02:36 -07002867 return result;
2868}
2869
Mathias Agopiand5556842013-09-19 17:08:37 -07002870void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
2871 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07002872 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07002873 for (size_t y=0 ; y<h ; y++) {
2874 uint32_t const * p = (uint32_t const *)vaddr + y*s;
2875 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07002876 if (p[x] != 0xFF000000) return;
2877 }
2878 }
2879 ALOGE("*** we just took a black screenshot ***\n"
2880 "requested minz=%d, maxz=%d, layerStack=%d",
2881 minLayerZ, maxLayerZ, hw->getLayerStack());
2882 const LayerVector& layers( mDrawingState.layersSortedByZ );
2883 const size_t count = layers.size();
2884 for (size_t i=0 ; i<count ; ++i) {
2885 const sp<Layer>& layer(layers[i]);
2886 const Layer::State& state(layer->getDrawingState());
2887 const bool visible = (state.layerStack == hw->getLayerStack())
2888 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2889 && (layer->isVisible());
2890 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
2891 visible ? '+' : '-',
2892 i, layer->getName().string(), state.layerStack, state.z,
2893 layer->isVisible(), state.flags, state.alpha);
2894 }
2895 }
2896}
2897
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002898// ---------------------------------------------------------------------------
2899
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002900SurfaceFlinger::LayerVector::LayerVector() {
2901}
2902
2903SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002904 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002905}
2906
2907int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2908 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002909{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002910 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002911 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2912 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002913
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002914 uint32_t ls = l->getCurrentState().layerStack;
2915 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002916 if (ls != rs)
2917 return ls - rs;
2918
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002919 uint32_t lz = l->getCurrentState().z;
2920 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002921 if (lz != rz)
2922 return lz - rz;
2923
2924 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002925}
2926
2927// ---------------------------------------------------------------------------
2928
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002929SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2930 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002931}
2932
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002933SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002934 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002935 viewport.makeInvalid();
2936 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002937}
2938
2939// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002940
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002941}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002942
2943
2944#if defined(__gl_h_)
2945#error "don't include gl/gl.h in this file"
2946#endif
2947
2948#if defined(__gl2_h_)
2949#error "don't include gl2/gl2.h in this file"
2950#endif