blob: 4f139feda40288cc97ba453ed9c1aa403d629f9c [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
Jesse Hallb65f32e2013-09-27 22:10:47 -0700369status_t SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId,
370 EGLint renderableType, EGLConfig* config) {
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
Mathias Agopiana4912602012-07-12 14:25:33 -0700373 status_t err;
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700374 EGLint wantedAttribute;
375 EGLint wantedAttributeValue;
Mathias Agopianda27af92012-09-13 18:17:13 -0700376
Mathias Agopian722b98f2012-09-25 18:24:31 -0700377 EGLAttributeVector attribs;
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700378 if (renderableType) {
379 attribs[EGL_RENDERABLE_TYPE] = renderableType;
380 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
381 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT|EGL_PBUFFER_BIT;
382 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
383 attribs[EGL_RED_SIZE] = 8;
384 attribs[EGL_GREEN_SIZE] = 8;
385 attribs[EGL_BLUE_SIZE] = 8;
386 wantedAttribute = EGL_NONE;
387 wantedAttributeValue = EGL_NONE;
Mathias Agopian722b98f2012-09-25 18:24:31 -0700388
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700389 } else {
390 // if no renderable type specified, fallback to a simplified query
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700391 wantedAttribute = EGL_NATIVE_VISUAL_ID;
392 wantedAttributeValue = nativeVisualId;
393 }
394
395 err = selectConfigForAttribute(display, attribs, wantedAttribute,
Jesse Hallb65f32e2013-09-27 22:10:47 -0700396 wantedAttributeValue, config);
397 if (err == NO_ERROR) {
398 EGLint caveat;
399 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
400 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
401 }
402 return err;
Mathias Agopiana4912602012-07-12 14:25:33 -0700403}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800404
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700405void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700406
Mathias Agopiana4912602012-07-12 14:25:33 -0700407 ALOGI( "SurfaceFlinger's main thread ready to run. "
408 "Initializing graphics H/W...");
409
Jesse Hallb65f32e2013-09-27 22:10:47 -0700410 status_t err;
Jesse Hall692c7232012-11-08 15:41:56 -0800411 Mutex::Autolock _l(mStateLock);
412
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700413 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700414 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
415 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700416
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700417 // Initialize the H/W composer object. There may or may not be an
418 // actual hardware composer underneath.
419 mHwc = new HWComposer(this,
420 *static_cast<HWComposer::EventHandler *>(this));
421
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700422 // First try to get an ES2 config
Jesse Hallb65f32e2013-09-27 22:10:47 -0700423 err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(), EGL_OPENGL_ES2_BIT,
424 &mEGLConfig);
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700425
Jesse Hallb65f32e2013-09-27 22:10:47 -0700426 if (err != NO_ERROR) {
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700427 // If ES2 fails, try ES1
Jesse Hallb65f32e2013-09-27 22:10:47 -0700428 err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(),
429 EGL_OPENGL_ES_BIT, &mEGLConfig);
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700430 }
431
Jesse Hallb65f32e2013-09-27 22:10:47 -0700432 if (err != NO_ERROR) {
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700433 // still didn't work, probably because we're on the emulator...
434 // try a simplified query
435 ALOGW("no suitable EGLConfig found, trying a simpler query");
Jesse Hallb65f32e2013-09-27 22:10:47 -0700436 err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(), 0, &mEGLConfig);
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700437 }
438
Jesse Hallb65f32e2013-09-27 22:10:47 -0700439 if (err != NO_ERROR) {
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700440 // this EGL is too lame for android
441 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
442 }
Mathias Agopian875d8e12013-06-07 15:35:48 -0700443
444 // print some debugging info
445 EGLint r,g,b,a;
446 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_RED_SIZE, &r);
447 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_GREEN_SIZE, &g);
448 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_BLUE_SIZE, &b);
449 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_ALPHA_SIZE, &a);
450 ALOGI("EGL informations:");
451 ALOGI("vendor : %s", eglQueryString(mEGLDisplay, EGL_VENDOR));
452 ALOGI("version : %s", eglQueryString(mEGLDisplay, EGL_VERSION));
453 ALOGI("extensions: %s", eglQueryString(mEGLDisplay, EGL_EXTENSIONS));
454 ALOGI("Client API: %s", eglQueryString(mEGLDisplay, EGL_CLIENT_APIS)?:"Not Supported");
455 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
456
457 // get a RenderEngine for the given display / config (can't fail)
458 mRenderEngine = RenderEngine::create(mEGLDisplay, mEGLConfig);
459
460 // retrieve the EGL context that was selected/created
461 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700462
Mathias Agopianed985572013-03-22 00:24:39 -0700463 // figure out which format we got
464 eglGetConfigAttrib(mEGLDisplay, mEGLConfig,
465 EGL_NATIVE_VISUAL_ID, &mEGLNativeVisualId);
466
Mathias Agopianda27af92012-09-13 18:17:13 -0700467 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
468 "couldn't create EGLContext");
469
Mathias Agopiancde87a32012-09-13 14:09:01 -0700470 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700471 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700472 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700473 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700474 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700475 // All non-virtual displays are currently considered secure.
476 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800477 createBuiltinDisplayLocked(type);
478 wp<IBinder> token = mBuiltinDisplays[i];
479
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700480 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
481 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i, bq);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700482 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700483 type, allocateHwcDisplayId(type), isSecure, token,
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700484 fbs, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700485 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700486 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700487 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700488 // for displays other than the main display, so we always
489 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700490 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700491 hw->acquireScreen();
492 }
493 mDisplays.add(token, hw);
494 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700495 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700496
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700497 // make the GLContext current so that we can create textures when creating Layers
498 // (which may happens before we render something)
499 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
500
Mathias Agopian028508c2012-07-25 21:12:12 -0700501 // start the EventThread
502 mEventThread = new EventThread(this);
503 mEventQueue.setEventThread(mEventThread);
504
Mathias Agopian92a979a2012-08-02 18:32:23 -0700505 // initialize our drawing state
506 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700507
Andy McFadden13a082e2012-08-24 10:16:42 -0700508 // set initial conditions (e.g. unblank default device)
509 initializeDisplays();
510
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700511 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700512 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800513}
514
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700515int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700516 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700517 type : mHwc->allocateDisplayId();
518}
519
Mathias Agopiana67e4182012-06-19 17:26:12 -0700520void SurfaceFlinger::startBootAnim() {
521 // start boot animation
522 property_set("service.bootanim.exit", "0");
523 property_set("ctl.start", "bootanim");
524}
525
Mathias Agopian875d8e12013-06-07 15:35:48 -0700526size_t SurfaceFlinger::getMaxTextureSize() const {
527 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700528}
529
Mathias Agopian875d8e12013-06-07 15:35:48 -0700530size_t SurfaceFlinger::getMaxViewportDims() const {
531 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700532}
533
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800534// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800535
Jamie Gennis582270d2011-08-17 18:19:00 -0700536bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800537 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800538 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800539 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700540 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800541}
542
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700543status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800544 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700545 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800546 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700547 type = i;
548 break;
549 }
550 }
551
552 if (type < 0) {
553 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700554 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700555
556 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700557 float xdpi = hwc.getDpiX(type);
558 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700559
560 // TODO: Not sure if display density should handled by SF any longer
561 class Density {
562 static int getDensityFromProperty(char const* propName) {
563 char property[PROPERTY_VALUE_MAX];
564 int density = 0;
565 if (property_get(propName, property, NULL) > 0) {
566 density = atoi(property);
567 }
568 return density;
569 }
570 public:
571 static int getEmuDensity() {
572 return getDensityFromProperty("qemu.sf.lcd_density"); }
573 static int getBuildDensity() {
574 return getDensityFromProperty("ro.sf.lcd_density"); }
575 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700576
577 if (type == DisplayDevice::DISPLAY_PRIMARY) {
578 // The density of the device is provided by a build property
579 float density = Density::getBuildDensity() / 160.0f;
580 if (density == 0) {
581 // the build doesn't provide a density -- this is wrong!
582 // use xdpi instead
583 ALOGE("ro.sf.lcd_density must be defined as a build property");
584 density = xdpi / 160.0f;
585 }
586 if (Density::getEmuDensity()) {
587 // if "qemu.sf.lcd_density" is specified, it overrides everything
588 xdpi = ydpi = density = Density::getEmuDensity();
589 density /= 160.0f;
590 }
591 info->density = density;
592
593 // TODO: this needs to go away (currently needed only by webkit)
594 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
595 info->orientation = hw->getOrientation();
Mathias Agopian1604f772012-09-18 21:54:42 -0700596 } else {
597 // TODO: where should this value come from?
598 static const int TV_DENSITY = 213;
599 info->density = TV_DENSITY / 160.0f;
600 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700601 }
602
Mathias Agopian1604f772012-09-18 21:54:42 -0700603 info->w = hwc.getWidth(type);
604 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700605 info->xdpi = xdpi;
606 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700607 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700608
609 // All non-virtual displays are currently considered secure.
610 info->secure = true;
611
Mathias Agopian888c8222012-08-04 21:10:38 -0700612 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700613}
614
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800615// ----------------------------------------------------------------------------
616
617sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800618 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700619}
620
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800621// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800622
623void SurfaceFlinger::waitForEvent() {
624 mEventQueue.waitMessage();
625}
626
627void SurfaceFlinger::signalTransaction() {
628 mEventQueue.invalidate();
629}
630
631void SurfaceFlinger::signalLayerUpdate() {
632 mEventQueue.invalidate();
633}
634
635void SurfaceFlinger::signalRefresh() {
636 mEventQueue.refresh();
637}
638
639status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
640 nsecs_t reltime, uint32_t flags) {
641 return mEventQueue.postMessage(msg, reltime);
642}
643
644status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
645 nsecs_t reltime, uint32_t flags) {
646 status_t res = mEventQueue.postMessage(msg, reltime);
647 if (res == NO_ERROR) {
648 msg->wait();
649 }
650 return res;
651}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800652
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700653void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700654 do {
655 waitForEvent();
656 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800657}
658
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700659void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Andy McFadden43601a22012-09-11 15:15:13 -0700660 if (mEventThread == NULL) {
661 // This is a temporary workaround for b/7145521. A non-null pointer
662 // does not mean EventThread has finished initializing, so this
663 // is not a correct fix.
664 ALOGW("WARNING: EventThread not started, ignoring vsync");
665 return;
666 }
Jesse Hall9e663de2013-08-16 14:28:37 -0700667 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700668 // we should only receive DisplayDevice::DisplayType from the vsync callback
Mathias Agopian148994e2012-09-19 17:31:36 -0700669 mEventThread->onVSyncReceived(type, timestamp);
670 }
671}
672
673void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
674 if (mEventThread == NULL) {
675 // This is a temporary workaround for b/7145521. A non-null pointer
676 // does not mean EventThread has finished initializing, so this
677 // is not a correct fix.
678 ALOGW("WARNING: EventThread not started, ignoring hotplug");
679 return;
680 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700681
Jesse Hall9e663de2013-08-16 14:28:37 -0700682 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700683 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800684 if (connected) {
685 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700686 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800687 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
688 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700689 }
690 setTransactionFlags(eDisplayTransactionNeeded);
691
Andy McFadden9e9689c2012-10-10 18:17:51 -0700692 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700693 }
Mathias Agopian86303202012-07-24 22:46:10 -0700694}
695
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700696void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
697 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700698}
699
Mathias Agopian4fec8732012-06-29 14:12:52 -0700700void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800701 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800702 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700703 case MessageQueue::TRANSACTION:
704 handleMessageTransaction();
705 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700706 case MessageQueue::INVALIDATE:
707 handleMessageTransaction();
708 handleMessageInvalidate();
709 signalRefresh();
710 break;
711 case MessageQueue::REFRESH:
712 handleMessageRefresh();
713 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800714 }
715}
716
Mathias Agopian4fec8732012-06-29 14:12:52 -0700717void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700718 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700719 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700720 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700721 }
722}
723
724void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700725 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700726 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700727}
728
729void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700730 ATRACE_CALL();
731 preComposition();
732 rebuildLayerStacks();
733 setUpHWComposer();
734 doDebugFlashRegions();
735 doComposition();
736 postComposition();
737}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700738
Mathias Agopiancd60f992012-08-16 16:28:27 -0700739void SurfaceFlinger::doDebugFlashRegions()
740{
741 // is debugging enabled
742 if (CC_LIKELY(!mDebugRegion))
743 return;
744
745 const bool repaintEverything = mRepaintEverything;
746 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
747 const sp<DisplayDevice>& hw(mDisplays[dpy]);
748 if (hw->canDraw()) {
749 // transform the dirty region into this screen's coordinate space
750 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
751 if (!dirtyRegion.isEmpty()) {
752 // redraw the whole screen
753 doComposeSurfaces(hw, Region(hw->bounds()));
754
755 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700756 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700757 RenderEngine& engine(getRenderEngine());
758 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
759
Mathias Agopiancd60f992012-08-16 16:28:27 -0700760 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700761 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700762 }
763 }
764 }
765
766 postFramebuffer();
767
768 if (mDebugRegion > 1) {
769 usleep(mDebugRegion * 1000);
770 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700771
772 HWComposer& hwc(getHwComposer());
773 if (hwc.initCheck() == NO_ERROR) {
774 status_t err = hwc.prepare();
775 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
776 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700777}
778
779void SurfaceFlinger::preComposition()
780{
781 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700782 const LayerVector& layers(mDrawingState.layersSortedByZ);
783 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700784 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700785 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700786 needExtraInvalidate = true;
787 }
788 }
789 if (needExtraInvalidate) {
790 signalLayerUpdate();
791 }
792}
793
794void SurfaceFlinger::postComposition()
795{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700796 const LayerVector& layers(mDrawingState.layersSortedByZ);
797 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700798 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700799 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700800 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800801
802 if (mAnimCompositionPending) {
803 mAnimCompositionPending = false;
804
805 const HWComposer& hwc = getHwComposer();
806 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
Jesse Halla9a1b002013-02-27 16:39:25 -0800807 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800808 mAnimFrameTracker.setActualPresentFence(presentFence);
809 } else {
810 // The HWC doesn't support present fences, so use the refresh
811 // timestamp instead.
812 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
813 mAnimFrameTracker.setActualPresentTime(presentTime);
814 }
815 mAnimFrameTracker.advanceFrame();
816 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700817}
818
819void SurfaceFlinger::rebuildLayerStacks() {
820 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700821 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700822 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700823 mVisibleRegionsDirty = false;
824 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700825
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700826 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700827 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700828 Region opaqueRegion;
829 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800830 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700831 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700832 const Transform& tr(hw->getTransform());
833 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700834 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700835 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700836 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700837
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700838 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700839 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700840 const sp<Layer>& layer(layers[i]);
841 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700842 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700843 Region drawRegion(tr.transform(
844 layer->visibleNonTransparentRegion));
845 drawRegion.andSelf(bounds);
846 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700847 layersSortedByZ.add(layer);
848 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700849 }
850 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700851 }
Mathias Agopian42977342012-08-05 00:40:46 -0700852 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700853 hw->undefinedRegion.set(bounds);
854 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
855 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700856 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700857 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700858}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700859
Mathias Agopiancd60f992012-08-16 16:28:27 -0700860void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700861 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
862 mDisplays[dpy]->beginFrame();
863 }
864
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700865 HWComposer& hwc(getHwComposer());
866 if (hwc.initCheck() == NO_ERROR) {
867 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700868 if (CC_UNLIKELY(mHwWorkListDirty)) {
869 mHwWorkListDirty = false;
870 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
871 sp<const DisplayDevice> hw(mDisplays[dpy]);
872 const int32_t id = hw->getHwcDisplayId();
873 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800874 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700875 hw->getVisibleLayersSortedByZ());
876 const size_t count = currentLayers.size();
877 if (hwc.createWorkList(id, count) == NO_ERROR) {
878 HWComposer::LayerListIterator cur = hwc.begin(id);
879 const HWComposer::LayerListIterator end = hwc.end(id);
880 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800881 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700882 layer->setGeometry(hw, *cur);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700883 if (mDebugDisableHWC || mDebugRegion || mDaltonize) {
Jamie Gennisa4310c82012-09-25 20:26:00 -0700884 cur->setSkip(true);
885 }
886 }
887 }
888 }
889 }
890 }
891
892 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700893 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700894 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700895 const int32_t id = hw->getHwcDisplayId();
896 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800897 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700898 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700899 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700900 HWComposer::LayerListIterator cur = hwc.begin(id);
901 const HWComposer::LayerListIterator end = hwc.end(id);
902 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
903 /*
904 * update the per-frame h/w composer data for each layer
905 * and build the transparent region of the FB
906 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800907 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700908 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700909 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700910 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700911 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700912
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700913 status_t err = hwc.prepare();
914 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700915
916 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
917 sp<const DisplayDevice> hw(mDisplays[dpy]);
918 hw->prepareFrame(hwc);
919 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700920 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700921}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700922
Mathias Agopiancd60f992012-08-16 16:28:27 -0700923void SurfaceFlinger::doComposition() {
924 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700925 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700926 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700927 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700928 if (hw->canDraw()) {
929 // transform the dirty region into this screen's coordinate space
930 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800931
932 // repaint the framebuffer (if needed)
933 doDisplayComposition(hw, dirtyRegion);
934
Mathias Agopiancd60f992012-08-16 16:28:27 -0700935 hw->dirtyRegion.clear();
936 hw->flip(hw->swapRegion);
937 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700938 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700939 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700940 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700941 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700942 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700943}
944
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800945void SurfaceFlinger::postFramebuffer()
946{
Mathias Agopian841cde52012-03-01 15:44:37 -0800947 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800948
Mathias Agopiana44b0412011-10-16 18:46:35 -0700949 const nsecs_t now = systemTime();
950 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700951
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700952 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700953 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700954 if (!hwc.supportsFramebufferTarget()) {
955 // EGL spec says:
956 // "surface must be bound to the calling thread's current context,
957 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700958 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700959 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700960 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700961 }
962
Mathias Agopian6da15f42013-09-25 20:40:07 -0700963 // make the default display current because the VirtualDisplayDevice code cannot
964 // deal with dequeueBuffer() being called outside of the composition loop; however
965 // the code below can call glFlush() which is allowed (and does in some case) call
966 // dequeueBuffer().
967 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
968
Mathias Agopian92a979a2012-08-02 18:32:23 -0700969 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700970 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800971 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700972 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700973 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -0700974 int32_t id = hw->getHwcDisplayId();
975 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -0700976 HWComposer::LayerListIterator cur = hwc.begin(id);
977 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700978 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700979 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700980 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700981 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700982 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -0700983 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700984 }
Jesse Hallef194142012-06-14 14:45:17 -0700985 }
Jamie Gennise8696a42012-01-15 18:54:57 -0800986 }
987
Mathias Agopiana44b0412011-10-16 18:46:35 -0700988 mLastSwapBufferTime = systemTime() - now;
989 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -0700990
991 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
992 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
993 logFrameStats();
994 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800995}
996
Mathias Agopian87baae12012-07-31 12:38:26 -0700997void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800998{
Mathias Agopian841cde52012-03-01 15:44:37 -0800999 ATRACE_CALL();
1000
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001001 // here we keep a copy of the drawing state (that is the state that's
1002 // going to be overwritten by handleTransactionLocked()) outside of
1003 // mStateLock so that the side-effects of the State assignment
1004 // don't happen with mStateLock held (which can cause deadlocks).
1005 State drawingState(mDrawingState);
1006
Mathias Agopianca4d3602011-05-19 15:38:14 -07001007 Mutex::Autolock _l(mStateLock);
1008 const nsecs_t now = systemTime();
1009 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001010
Mathias Agopianca4d3602011-05-19 15:38:14 -07001011 // Here we're guaranteed that some transaction flags are set
1012 // so we can call handleTransactionLocked() unconditionally.
1013 // We call getTransactionFlags(), which will also clear the flags,
1014 // with mStateLock held to guarantee that mCurrentState won't change
1015 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001016
Mathias Agopiane57f2922012-08-09 16:29:12 -07001017 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001018 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001019
Mathias Agopianca4d3602011-05-19 15:38:14 -07001020 mLastTransactionTime = systemTime() - now;
1021 mDebugInTransaction = 0;
1022 invalidateHwcGeometry();
1023 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001024}
1025
Mathias Agopian87baae12012-07-31 12:38:26 -07001026void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001027{
1028 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001029 const size_t count = currentLayers.size();
1030
1031 /*
1032 * Traversal of the children
1033 * (perform the transaction for each of them if needed)
1034 */
1035
Mathias Agopian3559b072012-08-15 13:46:03 -07001036 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001037 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001038 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001039 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1040 if (!trFlags) continue;
1041
1042 const uint32_t flags = layer->doTransaction(0);
1043 if (flags & Layer::eVisibleRegion)
1044 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001045 }
1046 }
1047
1048 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001049 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001050 */
1051
Mathias Agopiane57f2922012-08-09 16:29:12 -07001052 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001053 // here we take advantage of Vector's copy-on-write semantics to
1054 // improve performance by skipping the transaction entirely when
1055 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001056 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1057 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001058 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001059 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001060 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001061 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001062
1063 // find the displays that were removed
1064 // (ie: in drawing state but not in current state)
1065 // also handle displays that changed
1066 // (ie: displays that are in both lists)
1067 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001068 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1069 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001070 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001071 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001072 // Call makeCurrent() on the primary display so we can
1073 // be sure that nothing associated with this display
1074 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001075 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001076 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001077 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1078 if (hw != NULL)
1079 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001080 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001081 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001082 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001083 } else {
1084 ALOGW("trying to remove the main display");
1085 }
1086 } else {
1087 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001088 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001089 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001090 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001091 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001092 // recreating the DisplayDevice, so we just remove it
1093 // from the drawing state, so that it get re-added
1094 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001095 sp<DisplayDevice> hw(getDisplayDevice(display));
1096 if (hw != NULL)
1097 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001098 mDisplays.removeItem(display);
1099 mDrawingState.displays.removeItemsAt(i);
1100 dc--; i--;
1101 // at this point we must loop to the next item
1102 continue;
1103 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001104
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001105 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001106 if (disp != NULL) {
1107 if (state.layerStack != draw[i].layerStack) {
1108 disp->setLayerStack(state.layerStack);
1109 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001110 if ((state.orientation != draw[i].orientation)
1111 || (state.viewport != draw[i].viewport)
1112 || (state.frame != draw[i].frame))
1113 {
1114 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001115 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001116 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001117 }
1118 }
1119 }
1120
1121 // find displays that were added
1122 // (ie: in current state but not in drawing state)
1123 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001124 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001125 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001126
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001127 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001128 sp<IGraphicBufferProducer> producer;
1129 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
1130
Jesse Hall02d86562013-03-25 14:43:23 -07001131 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001132 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001133 // Virtual displays without a surface are dormant:
1134 // they have external state (layer stack, projection,
1135 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001136 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001137
Jesse Hall02d86562013-03-25 14:43:23 -07001138 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001139 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1140 *mHwc, hwcDisplayId, state.surface, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001141 state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001142
1143 dispSurface = vds;
1144 if (hwcDisplayId >= 0) {
1145 producer = vds;
1146 } else {
1147 // There won't be any interaction with HWC for this virtual display,
1148 // so the GLES driver can pass buffers directly to the sink.
1149 producer = state.surface;
1150 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001151 }
1152 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001153 ALOGE_IF(state.surface!=NULL,
1154 "adding a supported display, but rendering "
1155 "surface is provided (%p), ignoring it",
1156 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001157 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001158 // for supported (by hwc) displays we provide our
1159 // own rendering surface
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001160 dispSurface = new FramebufferSurface(*mHwc, state.type, bq);
1161 producer = bq;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001162 }
1163
1164 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001165 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001166 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001167 state.type, hwcDisplayId, state.isSecure,
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001168 display, dispSurface, producer, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001169 hw->setLayerStack(state.layerStack);
1170 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001171 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001172 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001173 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001174 if (state.isVirtualDisplay()) {
1175 if (hwcDisplayId >= 0) {
1176 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1177 hw->getWidth(), hw->getHeight(),
1178 hw->getFormat());
1179 }
1180 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001181 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001182 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001183 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001184 }
1185 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001186 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001187 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001188
Mathias Agopian84300952012-11-21 16:02:13 -08001189 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1190 // The transform hint might have changed for some layers
1191 // (either because a display has changed, or because a layer
1192 // as changed).
1193 //
1194 // Walk through all the layers in currentLayers,
1195 // and update their transform hint.
1196 //
1197 // If a layer is visible only on a single display, then that
1198 // display is used to calculate the hint, otherwise we use the
1199 // default display.
1200 //
1201 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1202 // the hint is set before we acquire a buffer from the surface texture.
1203 //
1204 // NOTE: layer transactions have taken place already, so we use their
1205 // drawing state. However, SurfaceFlinger's own transaction has not
1206 // happened yet, so we must use the current state layer list
1207 // (soon to become the drawing state list).
1208 //
1209 sp<const DisplayDevice> disp;
1210 uint32_t currentlayerStack = 0;
1211 for (size_t i=0; i<count; i++) {
1212 // NOTE: we rely on the fact that layers are sorted by
1213 // layerStack first (so we don't have to traverse the list
1214 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001215 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001216 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001217 if (i==0 || currentlayerStack != layerStack) {
1218 currentlayerStack = layerStack;
1219 // figure out if this layerstack is mirrored
1220 // (more than one display) if so, pick the default display,
1221 // if not, pick the only display it's on.
1222 disp.clear();
1223 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1224 sp<const DisplayDevice> hw(mDisplays[dpy]);
1225 if (hw->getLayerStack() == currentlayerStack) {
1226 if (disp == NULL) {
1227 disp = hw;
1228 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001229 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001230 break;
1231 }
1232 }
1233 }
1234 }
Chet Haase91d25932013-04-11 15:24:55 -07001235 if (disp == NULL) {
1236 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1237 // redraw after transform hint changes. See bug 8508397.
1238
1239 // could be null when this layer is using a layerStack
1240 // that is not visible on any display. Also can occur at
1241 // screen off/on times.
1242 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001243 }
Chet Haase91d25932013-04-11 15:24:55 -07001244 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001245 }
1246 }
1247
1248
Mathias Agopian3559b072012-08-15 13:46:03 -07001249 /*
1250 * Perform our own transaction if needed
1251 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001252
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001253 const LayerVector& layers(mDrawingState.layersSortedByZ);
1254 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001255 // layers have been added
1256 mVisibleRegionsDirty = true;
1257 }
1258
1259 // some layers might have been removed, so
1260 // we need to update the regions they're exposing.
1261 if (mLayersRemoved) {
1262 mLayersRemoved = false;
1263 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001264 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001265 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001266 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001267 if (currentLayers.indexOf(layer) < 0) {
1268 // this layer is not visible anymore
1269 // TODO: we could traverse the tree from front to back and
1270 // compute the actual visible region
1271 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001272 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001273 Region visibleReg = s.transform.transform(
1274 Region(Rect(s.active.w, s.active.h)));
1275 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001276 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001277 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001278 }
1279
1280 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001281}
1282
1283void SurfaceFlinger::commitTransaction()
1284{
1285 if (!mLayersPendingRemoval.isEmpty()) {
1286 // Notify removed layers now that they can't be drawn from
1287 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1288 mLayersPendingRemoval[i]->onRemoved();
1289 }
1290 mLayersPendingRemoval.clear();
1291 }
1292
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001293 // If this transaction is part of a window animation then the next frame
1294 // we composite should be considered an animation as well.
1295 mAnimCompositionPending = mAnimTransactionPending;
1296
Mathias Agopian4fec8732012-06-29 14:12:52 -07001297 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001298 mTransactionPending = false;
1299 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001300 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001301}
1302
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001303void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001304 const LayerVector& currentLayers, uint32_t layerStack,
1305 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306{
Mathias Agopian841cde52012-03-01 15:44:37 -08001307 ATRACE_CALL();
1308
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001309 Region aboveOpaqueLayers;
1310 Region aboveCoveredLayers;
1311 Region dirty;
1312
Mathias Agopian87baae12012-07-31 12:38:26 -07001313 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001314
1315 size_t i = currentLayers.size();
1316 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001317 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001318
1319 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001320 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001321
Jesse Hall01e29052013-02-19 16:13:35 -08001322 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001323 if (s.layerStack != layerStack)
1324 continue;
1325
Mathias Agopianab028732010-03-16 16:41:46 -07001326 /*
1327 * opaqueRegion: area of a surface that is fully opaque.
1328 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001329 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001330
1331 /*
1332 * visibleRegion: area of a surface that is visible on screen
1333 * and not fully transparent. This is essentially the layer's
1334 * footprint minus the opaque regions above it.
1335 * Areas covered by a translucent surface are considered visible.
1336 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001337 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001338
1339 /*
1340 * coveredRegion: area of a surface that is covered by all
1341 * visible regions above it (which includes the translucent areas).
1342 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001343 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001344
Jesse Halla8026d22012-09-25 13:25:04 -07001345 /*
1346 * transparentRegion: area of a surface that is hinted to be completely
1347 * transparent. This is only used to tell when the layer has no visible
1348 * non-transparent regions and can be removed from the layer list. It
1349 * does not affect the visibleRegion of this layer or any layers
1350 * beneath it. The hint may not be correct if apps don't respect the
1351 * SurfaceView restrictions (which, sadly, some don't).
1352 */
1353 Region transparentRegion;
1354
Mathias Agopianab028732010-03-16 16:41:46 -07001355
1356 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001357 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001358 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001359 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001360 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001361 if (!visibleRegion.isEmpty()) {
1362 // Remove the transparent area from the visible region
1363 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001364 const Transform tr(s.transform);
1365 if (tr.transformed()) {
1366 if (tr.preserveRects()) {
1367 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001368 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001369 } else {
1370 // transformation too complex, can't do the
1371 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001372 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001373 }
1374 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001375 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001376 }
Mathias Agopianab028732010-03-16 16:41:46 -07001377 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378
Mathias Agopianab028732010-03-16 16:41:46 -07001379 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001380 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001381 if (s.alpha==255 && !translucent &&
1382 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1383 // the opaque region is the layer's footprint
1384 opaqueRegion = visibleRegion;
1385 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386 }
1387 }
1388
Mathias Agopianab028732010-03-16 16:41:46 -07001389 // Clip the covered region to the visible region
1390 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1391
1392 // Update aboveCoveredLayers for next (lower) layer
1393 aboveCoveredLayers.orSelf(visibleRegion);
1394
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001395 // subtract the opaque region covered by the layers above us
1396 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001397
1398 // compute this layer's dirty region
1399 if (layer->contentDirty) {
1400 // we need to invalidate the whole region
1401 dirty = visibleRegion;
1402 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001403 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001404 layer->contentDirty = false;
1405 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001406 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001407 * the exposed region consists of two components:
1408 * 1) what's VISIBLE now and was COVERED before
1409 * 2) what's EXPOSED now less what was EXPOSED before
1410 *
1411 * note that (1) is conservative, we start with the whole
1412 * visible region but only keep what used to be covered by
1413 * something -- which mean it may have been exposed.
1414 *
1415 * (2) handles areas that were not covered by anything but got
1416 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001417 */
Mathias Agopianab028732010-03-16 16:41:46 -07001418 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001419 const Region oldVisibleRegion = layer->visibleRegion;
1420 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001421 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1422 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001423 }
1424 dirty.subtractSelf(aboveOpaqueLayers);
1425
1426 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001427 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001428
Mathias Agopianab028732010-03-16 16:41:46 -07001429 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001430 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001431
Jesse Halla8026d22012-09-25 13:25:04 -07001432 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001433 layer->setVisibleRegion(visibleRegion);
1434 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001435 layer->setVisibleNonTransparentRegion(
1436 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001437 }
1438
Mathias Agopian87baae12012-07-31 12:38:26 -07001439 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001440}
1441
Mathias Agopian87baae12012-07-31 12:38:26 -07001442void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1443 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001444 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001445 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1446 if (hw->getLayerStack() == layerStack) {
1447 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001448 }
1449 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001450}
1451
1452void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001453{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001454 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455
Mathias Agopian4fec8732012-06-29 14:12:52 -07001456 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001457 const LayerVector& layers(mDrawingState.layersSortedByZ);
1458 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001459 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001460 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001461 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001462 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001463 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001464 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001465
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001466 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467}
1468
Mathias Agopianad456f92011-01-13 17:53:01 -08001469void SurfaceFlinger::invalidateHwcGeometry()
1470{
1471 mHwWorkListDirty = true;
1472}
1473
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001474
Mathias Agopiancd60f992012-08-16 16:28:27 -07001475void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001476 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001477{
Mathias Agopian87baae12012-07-31 12:38:26 -07001478 Region dirtyRegion(inDirtyRegion);
1479
Mathias Agopianb8a55602009-06-26 19:06:36 -07001480 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001481 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001482
Mathias Agopian42977342012-08-05 00:40:46 -07001483 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001484 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001485 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1486 // takes a rectangle, we must make sure to update that whole
1487 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001488 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001489 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001490 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001491 // We need to redraw the rectangle that will be updated
1492 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001493 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001494 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001495 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001496 } else {
1497 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001498 dirtyRegion.set(hw->bounds());
1499 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500 }
1501 }
1502
Mathias Agopianff2ed702013-09-01 21:36:12 -07001503 if (CC_LIKELY(!mDaltonize)) {
1504 doComposeSurfaces(hw, dirtyRegion);
1505 } else {
1506 RenderEngine& engine(getRenderEngine());
1507 engine.beginGroup(mDaltonizer());
1508 doComposeSurfaces(hw, dirtyRegion);
1509 engine.endGroup();
1510 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001511
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001512 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001513 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001514
1515 // swap buffers (presentation)
1516 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001517}
1518
Mathias Agopiancd60f992012-08-16 16:28:27 -07001519void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001520{
Mathias Agopian3f844832013-08-07 21:24:32 -07001521 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001522 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001523 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001524 HWComposer::LayerListIterator cur = hwc.begin(id);
1525 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001526
Mathias Agopian85d751c2012-08-29 16:59:24 -07001527 const bool hasGlesComposition = hwc.hasGlesComposition(id) || (cur==end);
1528 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001529 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001530 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1531 hw->getDisplayName().string());
1532 return;
1533 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001534
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001535 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001536 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001537 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001538 // when using overlays, we assume a fully transparent framebuffer
1539 // NOTE: we could reduce how much we need to clear, for instance
1540 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001541 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001542 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001543 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001544 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001545 // we start with the whole screen area
1546 const Region bounds(hw->getBounds());
1547
1548 // we remove the scissor part
1549 // we're left with the letterbox region
1550 // (common case is that letterbox ends-up being empty)
1551 const Region letterbox(bounds.subtract(hw->getScissor()));
1552
1553 // compute the area to clear
1554 Region region(hw->undefinedRegion.merge(letterbox));
1555
1556 // but limit it to the dirty region
1557 region.andSelf(dirty);
1558
Mathias Agopianb9494d52012-04-18 02:28:45 -07001559 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001560 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001561 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001562 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001563 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001564 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001565
Mathias Agopian766dc492012-10-30 18:08:06 -07001566 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1567 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001568 // scissor on the main display. It should never be needed
1569 // anyways (though in theory it could since the API allows it).
1570 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001571 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001572 if (scissor != bounds) {
1573 // scissor doesn't match the screen's dimensions, so we
1574 // need to clear everything outside of it and enable
1575 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001576
Mathias Agopianf45c5102012-10-24 16:29:17 -07001577 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001578 const uint32_t height = hw->getHeight();
1579 engine.setScissor(scissor.left, height - scissor.bottom,
1580 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001581 }
1582 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001583 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001584
Mathias Agopian85d751c2012-08-29 16:59:24 -07001585 /*
1586 * and then, render the layers targeted at the framebuffer
1587 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001588
Mathias Agopian13127d82013-03-05 17:47:11 -08001589 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001590 const size_t count = layers.size();
1591 const Transform& tr = hw->getTransform();
1592 if (cur != end) {
1593 // we're using h/w composer
1594 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001595 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001596 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001597 if (!clip.isEmpty()) {
1598 switch (cur->getCompositionType()) {
1599 case HWC_OVERLAY: {
1600 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1601 && i
1602 && layer->isOpaque()
1603 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001604 // never clear the very first layer since we're
1605 // guaranteed the FB is already cleared
1606 layer->clearWithOpenGL(hw, clip);
1607 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001608 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001609 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001610 case HWC_FRAMEBUFFER: {
1611 layer->draw(hw, clip);
1612 break;
1613 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001614 case HWC_FRAMEBUFFER_TARGET: {
1615 // this should not happen as the iterator shouldn't
1616 // let us get there.
1617 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1618 break;
1619 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001620 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001621 }
1622 layer->setAcquireFence(hw, *cur);
1623 }
1624 } else {
1625 // we're not using h/w composer
1626 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001627 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001628 const Region clip(dirty.intersect(
1629 tr.transform(layer->visibleRegion)));
1630 if (!clip.isEmpty()) {
1631 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001632 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001633 }
1634 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001635
1636 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001637 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001638}
1639
Mathias Agopian3f844832013-08-07 21:24:32 -07001640void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001641 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001642 RenderEngine& engine(getRenderEngine());
1643 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001644}
1645
Mathias Agopianac9fa422013-02-11 16:40:36 -08001646void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1647 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001648 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001649 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001650{
Mathias Agopian96f08192010-06-02 23:28:45 -07001651 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001652 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001653
Mathias Agopian96f08192010-06-02 23:28:45 -07001654 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001655 Mutex::Autolock _l(mStateLock);
1656 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001657 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001658}
1659
Mathias Agopian3f844832013-08-07 21:24:32 -07001660status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001661 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001662 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001663 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001664 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001665 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001666 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001667 return NO_ERROR;
1668 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001669 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001670}
1671
Mathias Agopian3f844832013-08-07 21:24:32 -07001672uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001673 return android_atomic_release_load(&mTransactionFlags);
1674}
1675
Mathias Agopian3f844832013-08-07 21:24:32 -07001676uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001677 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1678}
1679
Mathias Agopian3f844832013-08-07 21:24:32 -07001680uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001681 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1682 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001683 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001684 }
1685 return old;
1686}
1687
Mathias Agopian8b33f032012-07-24 20:43:54 -07001688void SurfaceFlinger::setTransactionState(
1689 const Vector<ComposerState>& state,
1690 const Vector<DisplayState>& displays,
1691 uint32_t flags)
1692{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001693 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001694 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001695 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001696
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001697 if (flags & eAnimation) {
1698 // For window updates that are part of an animation we must wait for
1699 // previous animation "frames" to be handled.
1700 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001701 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001702 if (CC_UNLIKELY(err != NO_ERROR)) {
1703 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001704 // caller after a few seconds.
1705 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1706 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001707 mAnimTransactionPending = false;
1708 break;
1709 }
1710 }
1711 }
1712
Mathias Agopiane57f2922012-08-09 16:29:12 -07001713 size_t count = displays.size();
1714 for (size_t i=0 ; i<count ; i++) {
1715 const DisplayState& s(displays[i]);
1716 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001717 }
1718
Mathias Agopiane57f2922012-08-09 16:29:12 -07001719 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001720 for (size_t i=0 ; i<count ; i++) {
1721 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001722 // Here we need to check that the interface we're given is indeed
1723 // one of our own. A malicious client could give us a NULL
1724 // IInterface, or one of its own or even one of our own but a
1725 // different type. All these situations would cause us to crash.
1726 //
1727 // NOTE: it would be better to use RTTI as we could directly check
1728 // that we have a Client*. however, RTTI is disabled in Android.
1729 if (s.client != NULL) {
1730 sp<IBinder> binder = s.client->asBinder();
1731 if (binder != NULL) {
1732 String16 desc(binder->getInterfaceDescriptor());
1733 if (desc == ISurfaceComposerClient::descriptor) {
1734 sp<Client> client( static_cast<Client *>(s.client.get()) );
1735 transactionFlags |= setClientStateLocked(client, s.state);
1736 }
1737 }
1738 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001739 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001740
Mathias Agopian386aa982011-11-07 21:58:03 -08001741 if (transactionFlags) {
1742 // this triggers the transaction
1743 setTransactionFlags(transactionFlags);
1744
1745 // if this is a synchronous transaction, wait for it to take effect
1746 // before returning.
1747 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001748 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001749 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001750 if (flags & eAnimation) {
1751 mAnimTransactionPending = true;
1752 }
1753 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001754 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1755 if (CC_UNLIKELY(err != NO_ERROR)) {
1756 // just in case something goes wrong in SF, return to the
1757 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001758 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1759 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001760 break;
1761 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001762 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001763 }
1764}
1765
Mathias Agopiane57f2922012-08-09 16:29:12 -07001766uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1767{
Jesse Hall9a143922012-10-04 16:29:19 -07001768 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1769 if (dpyIdx < 0)
1770 return 0;
1771
Mathias Agopiane57f2922012-08-09 16:29:12 -07001772 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001773 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001774 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001775 const uint32_t what = s.what;
1776 if (what & DisplayState::eSurfaceChanged) {
1777 if (disp.surface->asBinder() != s.surface->asBinder()) {
1778 disp.surface = s.surface;
1779 flags |= eDisplayTransactionNeeded;
1780 }
1781 }
1782 if (what & DisplayState::eLayerStackChanged) {
1783 if (disp.layerStack != s.layerStack) {
1784 disp.layerStack = s.layerStack;
1785 flags |= eDisplayTransactionNeeded;
1786 }
1787 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001788 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001789 if (disp.orientation != s.orientation) {
1790 disp.orientation = s.orientation;
1791 flags |= eDisplayTransactionNeeded;
1792 }
1793 if (disp.frame != s.frame) {
1794 disp.frame = s.frame;
1795 flags |= eDisplayTransactionNeeded;
1796 }
1797 if (disp.viewport != s.viewport) {
1798 disp.viewport = s.viewport;
1799 flags |= eDisplayTransactionNeeded;
1800 }
1801 }
1802 }
1803 return flags;
1804}
1805
1806uint32_t SurfaceFlinger::setClientStateLocked(
1807 const sp<Client>& client,
1808 const layer_state_t& s)
1809{
1810 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001811 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001812 if (layer != 0) {
1813 const uint32_t what = s.what;
1814 if (what & layer_state_t::ePositionChanged) {
1815 if (layer->setPosition(s.x, s.y))
1816 flags |= eTraversalNeeded;
1817 }
1818 if (what & layer_state_t::eLayerChanged) {
1819 // NOTE: index needs to be calculated before we update the state
1820 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1821 if (layer->setLayer(s.z)) {
1822 mCurrentState.layersSortedByZ.removeAt(idx);
1823 mCurrentState.layersSortedByZ.add(layer);
1824 // we need traversal (state changed)
1825 // AND transaction (list changed)
1826 flags |= eTransactionNeeded|eTraversalNeeded;
1827 }
1828 }
1829 if (what & layer_state_t::eSizeChanged) {
1830 if (layer->setSize(s.w, s.h)) {
1831 flags |= eTraversalNeeded;
1832 }
1833 }
1834 if (what & layer_state_t::eAlphaChanged) {
1835 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1836 flags |= eTraversalNeeded;
1837 }
1838 if (what & layer_state_t::eMatrixChanged) {
1839 if (layer->setMatrix(s.matrix))
1840 flags |= eTraversalNeeded;
1841 }
1842 if (what & layer_state_t::eTransparentRegionChanged) {
1843 if (layer->setTransparentRegionHint(s.transparentRegion))
1844 flags |= eTraversalNeeded;
1845 }
1846 if (what & layer_state_t::eVisibilityChanged) {
1847 if (layer->setFlags(s.flags, s.mask))
1848 flags |= eTraversalNeeded;
1849 }
1850 if (what & layer_state_t::eCropChanged) {
1851 if (layer->setCrop(s.crop))
1852 flags |= eTraversalNeeded;
1853 }
1854 if (what & layer_state_t::eLayerStackChanged) {
1855 // NOTE: index needs to be calculated before we update the state
1856 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1857 if (layer->setLayerStack(s.layerStack)) {
1858 mCurrentState.layersSortedByZ.removeAt(idx);
1859 mCurrentState.layersSortedByZ.add(layer);
1860 // we need traversal (state changed)
1861 // AND transaction (list changed)
1862 flags |= eTransactionNeeded|eTraversalNeeded;
1863 }
1864 }
1865 }
1866 return flags;
1867}
1868
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001869status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001870 const String8& name,
1871 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001872 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1873 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001874{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001875 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001876 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001877 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001878 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001879 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001880 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001881
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001882 status_t result = NO_ERROR;
1883
1884 sp<Layer> layer;
1885
Mathias Agopian3165cc22012-08-08 19:42:09 -07001886 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1887 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001888 result = createNormalLayer(client,
1889 name, w, h, flags, format,
1890 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001891 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001892 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001893 result = createDimLayer(client,
1894 name, w, h, flags,
1895 handle, gbp, &layer);
1896 break;
1897 default:
1898 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001899 break;
1900 }
1901
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001902 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001903 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001904 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001905 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001906 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001907}
1908
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001909status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1910 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1911 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001912{
1913 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001914 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001915 case PIXEL_FORMAT_TRANSPARENT:
1916 case PIXEL_FORMAT_TRANSLUCENT:
1917 format = PIXEL_FORMAT_RGBA_8888;
1918 break;
1919 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001920#ifdef NO_RGBX_8888
1921 format = PIXEL_FORMAT_RGB_565;
1922#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001923 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001924#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001925 break;
1926 }
1927
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001928#ifdef NO_RGBX_8888
1929 if (format == PIXEL_FORMAT_RGBX_8888)
1930 format = PIXEL_FORMAT_RGBA_8888;
1931#endif
1932
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001933 *outLayer = new Layer(this, client, name, w, h, flags);
1934 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1935 if (err == NO_ERROR) {
1936 *handle = (*outLayer)->getHandle();
1937 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001938 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001939
1940 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1941 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001942}
1943
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001944status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1945 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1946 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001947{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001948 *outLayer = new LayerDim(this, client, name, w, h, flags);
1949 *handle = (*outLayer)->getHandle();
1950 *gbp = (*outLayer)->getBufferQueue();
1951 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001952}
1953
Mathias Agopianac9fa422013-02-11 16:40:36 -08001954status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001955{
Mathias Agopian67106042013-03-14 19:18:13 -07001956 // called by the window manager when it wants to remove a Layer
1957 status_t err = NO_ERROR;
1958 sp<Layer> l(client->getLayerUser(handle));
1959 if (l != NULL) {
1960 err = removeLayer(l);
1961 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
1962 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07001963 }
1964 return err;
1965}
1966
Mathias Agopian13127d82013-03-05 17:47:11 -08001967status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07001968{
Mathias Agopian67106042013-03-14 19:18:13 -07001969 // called by ~LayerCleaner() when all references to the IBinder (handle)
1970 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07001971 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08001972 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07001973 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07001974 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00001975 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07001976 "error removing layer=%p (%s)", l.get(), strerror(-err));
1977 }
1978 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001979}
1980
Mathias Agopianb60314a2012-04-10 22:09:54 -07001981// ---------------------------------------------------------------------------
1982
Andy McFadden13a082e2012-08-24 10:16:42 -07001983void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08001984 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07001985 Vector<ComposerState> state;
1986 Vector<DisplayState> displays;
1987 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08001988 d.what = DisplayState::eDisplayProjectionChanged |
1989 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08001990 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08001991 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07001992 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07001993 d.frame.makeInvalid();
1994 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07001995 displays.add(d);
1996 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001997 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07001998
1999 const nsecs_t period =
2000 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2001 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002002}
2003
2004void SurfaceFlinger::initializeDisplays() {
2005 class MessageScreenInitialized : public MessageBase {
2006 SurfaceFlinger* flinger;
2007 public:
2008 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2009 virtual bool handler() {
2010 flinger->onInitializeDisplays();
2011 return true;
2012 }
2013 };
2014 sp<MessageBase> msg = new MessageScreenInitialized(this);
2015 postMessageAsync(msg); // we may be called from main thread, use async message
2016}
2017
2018
Mathias Agopiancde87a32012-09-13 14:09:01 -07002019void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002020 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2021 if (hw->isScreenAcquired()) {
2022 // this is expected, e.g. when power manager wakes up during boot
2023 ALOGD(" screen was previously acquired");
2024 return;
2025 }
2026
Mathias Agopian42977342012-08-05 00:40:46 -07002027 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002028 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002029 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002030 // built-in display, tell the HWC
2031 getHwComposer().acquire(type);
2032
2033 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2034 // FIXME: eventthread only knows about the main display right now
2035 mEventThread->onScreenAcquired();
2036 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002037 }
Mathias Agopian20128302012-08-13 18:32:13 -07002038 mVisibleRegionsDirty = true;
2039 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002040}
2041
Mathias Agopiancde87a32012-09-13 14:09:01 -07002042void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002043 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2044 if (!hw->isScreenAcquired()) {
2045 ALOGD(" screen was previously released");
2046 return;
2047 }
2048
2049 hw->releaseScreen();
2050 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002051 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002052 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07002053 // FIXME: eventthread only knows about the main display right now
2054 mEventThread->onScreenReleased();
2055 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002056
2057 // built-in display, tell the HWC
2058 getHwComposer().release(type);
2059 }
2060 mVisibleRegionsDirty = true;
2061 // from this point on, SF will stop drawing on this display
2062}
2063
2064void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2065 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002066 SurfaceFlinger& mFlinger;
2067 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002068 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002069 MessageScreenAcquired(SurfaceFlinger& flinger,
2070 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002071 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002072 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2073 if (hw == NULL) {
2074 ALOGE("Attempt to unblank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002075 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002076 ALOGW("Attempt to unblank virtual display");
2077 } else {
2078 mFlinger.onScreenAcquired(hw);
2079 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002080 return true;
2081 }
2082 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002083 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2084 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002085}
2086
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002087void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002088 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002089 SurfaceFlinger& mFlinger;
2090 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002091 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002092 MessageScreenReleased(SurfaceFlinger& flinger,
2093 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002094 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002095 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2096 if (hw == NULL) {
2097 ALOGE("Attempt to blank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002098 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002099 ALOGW("Attempt to blank virtual display");
2100 } else {
2101 mFlinger.onScreenReleased(hw);
2102 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002103 return true;
2104 }
2105 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002106 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2107 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002108}
2109
2110// ---------------------------------------------------------------------------
2111
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002112status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2113{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002114 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002115
Mathias Agopianbd115332013-04-18 16:41:04 -07002116 IPCThreadState* ipc = IPCThreadState::self();
2117 const int pid = ipc->getCallingPid();
2118 const int uid = ipc->getCallingUid();
2119 if ((uid != AID_SHELL) &&
2120 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002121 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002122 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002123 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002124 // Try to get the main lock, but don't insist if we can't
2125 // (this would indicate SF is stuck, but we want to be able to
2126 // print something in dumpsys).
2127 int retry = 3;
2128 while (mStateLock.tryLock()<0 && --retry>=0) {
2129 usleep(1000000);
2130 }
2131 const bool locked(retry >= 0);
2132 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002133 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002134 "SurfaceFlinger appears to be unresponsive, "
2135 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002136 }
2137
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002138 bool dumpAll = true;
2139 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002140 size_t numArgs = args.size();
2141 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002142 if ((index < numArgs) &&
2143 (args[index] == String16("--list"))) {
2144 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002145 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002146 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002147 }
2148
2149 if ((index < numArgs) &&
2150 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002151 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002152 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002153 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002154 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002155
2156 if ((index < numArgs) &&
2157 (args[index] == String16("--latency-clear"))) {
2158 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002159 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002160 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002161 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002162 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002163
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002164 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002165 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002166 }
2167
Mathias Agopian9795c422009-08-26 16:36:26 -07002168 if (locked) {
2169 mStateLock.unlock();
2170 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002171 }
2172 write(fd, result.string(), result.size());
2173 return NO_ERROR;
2174}
2175
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002176void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002177 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002178{
2179 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2180 const size_t count = currentLayers.size();
2181 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002182 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002183 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002184 }
2185}
2186
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002187void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002188 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002189{
2190 String8 name;
2191 if (index < args.size()) {
2192 name = String8(args[index]);
2193 index++;
2194 }
2195
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002196 const nsecs_t period =
2197 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2198 result.appendFormat("%lld\n", period);
2199
2200 if (name.isEmpty()) {
2201 mAnimFrameTracker.dump(result);
2202 } else {
2203 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2204 const size_t count = currentLayers.size();
2205 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002206 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002207 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002208 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002209 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002210 }
2211 }
2212}
2213
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002214void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002215 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002216{
2217 String8 name;
2218 if (index < args.size()) {
2219 name = String8(args[index]);
2220 index++;
2221 }
2222
2223 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2224 const size_t count = currentLayers.size();
2225 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002226 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002227 if (name.isEmpty() || (name == layer->getName())) {
2228 layer->clearStats();
2229 }
2230 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002231
2232 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002233}
2234
Jamie Gennis6547ff42013-07-16 20:12:42 -07002235// This should only be called from the main thread. Otherwise it would need
2236// the lock and should use mCurrentState rather than mDrawingState.
2237void SurfaceFlinger::logFrameStats() {
2238 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2239 const size_t count = drawingLayers.size();
2240 for (size_t i=0 ; i<count ; i++) {
2241 const sp<Layer>& layer(drawingLayers[i]);
2242 layer->logFrameStats();
2243 }
2244
2245 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2246}
2247
Andy McFadden4803b742012-09-24 19:07:20 -07002248/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2249{
2250 static const char* config =
2251 " [sf"
2252#ifdef NO_RGBX_8888
2253 " NO_RGBX_8888"
2254#endif
2255#ifdef HAS_CONTEXT_PRIORITY
2256 " HAS_CONTEXT_PRIORITY"
2257#endif
2258#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2259 " NEVER_DEFAULT_TO_ASYNC_MODE"
2260#endif
2261#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2262 " TARGET_DISABLE_TRIPLE_BUFFERING"
2263#endif
2264 "]";
2265 result.append(config);
2266}
2267
Mathias Agopian74d211a2013-04-22 16:55:35 +02002268void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2269 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002270{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002271 bool colorize = false;
2272 if (index < args.size()
2273 && (args[index] == String16("--color"))) {
2274 colorize = true;
2275 index++;
2276 }
2277
2278 Colorizer colorizer(colorize);
2279
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002280 // figure out if we're stuck somewhere
2281 const nsecs_t now = systemTime();
2282 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2283 const nsecs_t inTransaction(mDebugInTransaction);
2284 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2285 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2286
2287 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002288 * Dump library configuration.
2289 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002290
2291 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002292 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002293 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002294 appendSfConfigString(result);
2295 appendUiConfigString(result);
2296 appendGuiConfigString(result);
2297 result.append("\n");
2298
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002299 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002300 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002301 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002302 result.append(SyncFeatures::getInstance().toString());
2303 result.append("\n");
2304
Andy McFadden4803b742012-09-24 19:07:20 -07002305 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002306 * Dump the visible layer list
2307 */
2308 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2309 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002310 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002311 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002312 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002313 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002314 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002315 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002316 }
2317
2318 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002319 * Dump Display state
2320 */
2321
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002322 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002323 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002324 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002325 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2326 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002327 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002328 }
2329
2330 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002331 * Dump SurfaceFlinger global state
2332 */
2333
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002334 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002335 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002336 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002337
Mathias Agopian888c8222012-08-04 21:10:38 -07002338 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002339 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002340
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002341 colorizer.bold(result);
2342 result.appendFormat("EGL implementation : %s\n",
2343 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2344 colorizer.reset(result);
2345 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002346 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002347
Mathias Agopian875d8e12013-06-07 15:35:48 -07002348 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002349
Mathias Agopian42977342012-08-05 00:40:46 -07002350 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002351 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002352 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002353 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002354 " last eglSwapBuffers() time: %f us\n"
2355 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002356 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002357 " refresh-rate : %f fps\n"
2358 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002359 " y-dpi : %f\n"
2360 " EGL_NATIVE_VISUAL_ID : %d\n"
2361 " gpu_to_cpu_unsupported : %d\n"
2362 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002363 mLastSwapBufferTime/1000.0,
2364 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002365 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002366 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2367 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002368 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2369 mEGLNativeVisualId,
2370 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002371
Mathias Agopian74d211a2013-04-22 16:55:35 +02002372 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002373 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002374
Mathias Agopian74d211a2013-04-22 16:55:35 +02002375 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002376 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002377
2378 /*
2379 * VSYNC state
2380 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002381 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002382
2383 /*
2384 * Dump HWComposer state
2385 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002386 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002387 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002388 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002389 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002390 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopianff2ed702013-09-01 21:36:12 -07002391 (mDebugDisableHWC || mDebugRegion || mDaltonize) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002392 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002393
2394 /*
2395 * Dump gralloc state
2396 */
2397 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2398 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002399}
2400
Mathias Agopian13127d82013-03-05 17:47:11 -08002401const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002402SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002403 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002404 wp<IBinder> dpy;
2405 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2406 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2407 dpy = mDisplays.keyAt(i);
2408 break;
2409 }
2410 }
2411 if (dpy == NULL) {
2412 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2413 // Just use the primary display so we have something to return
2414 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2415 }
2416 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002417}
2418
Keun young Park63f165f2012-08-31 10:53:36 -07002419bool SurfaceFlinger::startDdmConnection()
2420{
2421 void* libddmconnection_dso =
2422 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2423 if (!libddmconnection_dso) {
2424 return false;
2425 }
2426 void (*DdmConnection_start)(const char* name);
2427 DdmConnection_start =
2428 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2429 if (!DdmConnection_start) {
2430 dlclose(libddmconnection_dso);
2431 return false;
2432 }
2433 (*DdmConnection_start)(getServiceName());
2434 return true;
2435}
2436
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002437status_t SurfaceFlinger::onTransact(
2438 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2439{
2440 switch (code) {
2441 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002442 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002443 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002444 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002445 case BLANK:
2446 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002447 {
2448 // codes that require permission check
2449 IPCThreadState* ipc = IPCThreadState::self();
2450 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002451 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002452 if ((uid != AID_GRAPHICS) &&
2453 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002454 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002455 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2456 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002457 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002458 break;
2459 }
2460 case CAPTURE_SCREEN:
2461 {
2462 // codes that require permission check
2463 IPCThreadState* ipc = IPCThreadState::self();
2464 const int pid = ipc->getCallingPid();
2465 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002466 if ((uid != AID_GRAPHICS) &&
2467 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002468 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002469 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2470 return PERMISSION_DENIED;
2471 }
2472 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002473 }
2474 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002475
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002476 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2477 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002478 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002479 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002480 IPCThreadState* ipc = IPCThreadState::self();
2481 const int pid = ipc->getCallingPid();
2482 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002483 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002484 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002485 return PERMISSION_DENIED;
2486 }
2487 int n;
2488 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002489 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002490 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002491 return NO_ERROR;
2492 case 1002: // SHOW_UPDATES
2493 n = data.readInt32();
2494 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002495 invalidateHwcGeometry();
2496 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002497 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002498 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002499 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002500 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002501 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002502 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002503 setTransactionFlags(
2504 eTransactionNeeded|
2505 eDisplayTransactionNeeded|
2506 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002507 return NO_ERROR;
2508 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002509 case 1006:{ // send empty update
2510 signalRefresh();
2511 return NO_ERROR;
2512 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002513 case 1008: // toggle use of hw composer
2514 n = data.readInt32();
2515 mDebugDisableHWC = n ? 1 : 0;
2516 invalidateHwcGeometry();
2517 repaintEverything();
2518 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002519 case 1009: // toggle use of transform hint
2520 n = data.readInt32();
2521 mDebugDisableTransformHint = n ? 1 : 0;
2522 invalidateHwcGeometry();
2523 repaintEverything();
2524 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002525 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002526 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002527 reply->writeInt32(0);
2528 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002529 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002530 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002531 return NO_ERROR;
2532 case 1013: {
2533 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002534 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2535 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002536 return NO_ERROR;
2537 }
2538 case 1014: {
2539 // daltonize
2540 n = data.readInt32();
2541 switch (n % 10) {
2542 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2543 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2544 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2545 }
2546 if (n >= 10) {
2547 mDaltonizer.setMode(Daltonizer::correction);
2548 } else {
2549 mDaltonizer.setMode(Daltonizer::simulation);
2550 }
2551 mDaltonize = n > 0;
2552 invalidateHwcGeometry();
2553 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002554 }
2555 return NO_ERROR;
2556 }
2557 }
2558 return err;
2559}
2560
Mathias Agopian53331da2011-08-22 21:44:41 -07002561void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002562 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002563 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002564}
2565
Mathias Agopian59119e62010-10-11 12:37:43 -07002566// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002567// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002568// ---------------------------------------------------------------------------
2569
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002570/* The code below is here to handle b/8734824
2571 *
2572 * We create a IGraphicBufferProducer wrapper that forwards all calls
2573 * to the calling binder thread, where they are executed. This allows
2574 * the calling thread to be reused (on the other side) and not
2575 * depend on having "enough" binder threads to handle the requests.
2576 *
2577 */
2578
2579class GraphicProducerWrapper : public BBinder, public MessageHandler {
2580 sp<IGraphicBufferProducer> impl;
2581 sp<Looper> looper;
2582 status_t result;
2583 bool exitPending;
2584 bool exitRequested;
2585 mutable Barrier barrier;
2586 volatile int32_t memoryBarrier;
2587 uint32_t code;
2588 Parcel const* data;
2589 Parcel* reply;
2590
2591 enum {
2592 MSG_API_CALL,
2593 MSG_EXIT
2594 };
2595
2596 /*
2597 * this is called by our "fake" BpGraphicBufferProducer. We package the
2598 * data and reply Parcel and forward them to the calling thread.
2599 */
2600 virtual status_t transact(uint32_t code,
2601 const Parcel& data, Parcel* reply, uint32_t flags) {
2602 this->code = code;
2603 this->data = &data;
2604 this->reply = reply;
2605 android_atomic_acquire_store(0, &memoryBarrier);
2606 if (exitPending) {
2607 // if we've exited, we run the message synchronously right here
2608 handleMessage(Message(MSG_API_CALL));
2609 } else {
2610 barrier.close();
2611 looper->sendMessage(this, Message(MSG_API_CALL));
2612 barrier.wait();
2613 }
2614 return NO_ERROR;
2615 }
2616
2617 /*
2618 * here we run on the binder calling thread. All we've got to do is
2619 * call the real BpGraphicBufferProducer.
2620 */
2621 virtual void handleMessage(const Message& message) {
2622 android_atomic_release_load(&memoryBarrier);
2623 if (message.what == MSG_API_CALL) {
2624 impl->asBinder()->transact(code, data[0], reply);
2625 barrier.open();
2626 } else if (message.what == MSG_EXIT) {
2627 exitRequested = true;
2628 }
2629 }
2630
2631public:
2632 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2633 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2634 exitPending(false), exitRequested(false) {
2635 }
2636
2637 status_t waitForResponse() {
2638 do {
2639 looper->pollOnce(-1);
2640 } while (!exitRequested);
2641 return result;
2642 }
2643
2644 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002645 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002646 exitPending = true;
2647 looper->sendMessage(this, Message(MSG_EXIT));
2648 }
2649};
2650
2651
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002652status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2653 const sp<IGraphicBufferProducer>& producer,
2654 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002655 uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002656
2657 if (CC_UNLIKELY(display == 0))
2658 return BAD_VALUE;
2659
2660 if (CC_UNLIKELY(producer == 0))
2661 return BAD_VALUE;
2662
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002663 // if we have secure windows on this display, never allow the screen capture
2664 // unless the producer interface is local (i.e.: we can take a screenshot for
2665 // ourselves).
2666 if (!producer->asBinder()->localBinder()) {
2667 Mutex::Autolock _l(mStateLock);
2668 sp<const DisplayDevice> hw(getDisplayDevice(display));
2669 if (hw->getSecureLayerVisible()) {
2670 ALOGW("FB is protected: PERMISSION_DENIED");
2671 return PERMISSION_DENIED;
2672 }
2673 }
2674
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002675 class MessageCaptureScreen : public MessageBase {
2676 SurfaceFlinger* flinger;
2677 sp<IBinder> display;
2678 sp<IGraphicBufferProducer> producer;
2679 uint32_t reqWidth, reqHeight;
2680 uint32_t minLayerZ,maxLayerZ;
2681 status_t result;
2682 public:
2683 MessageCaptureScreen(SurfaceFlinger* flinger,
2684 const sp<IBinder>& display,
2685 const sp<IGraphicBufferProducer>& producer,
2686 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002687 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002688 : flinger(flinger), display(display), producer(producer),
2689 reqWidth(reqWidth), reqHeight(reqHeight),
2690 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2691 result(PERMISSION_DENIED)
2692 {
2693 }
2694 status_t getResult() const {
2695 return result;
2696 }
2697 virtual bool handler() {
2698 Mutex::Autolock _l(flinger->mStateLock);
2699 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002700 result = flinger->captureScreenImplLocked(hw,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002701 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002702 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002703 return true;
2704 }
2705 };
2706
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002707 // make sure to process transactions before screenshots -- a transaction
2708 // might already be pending but scheduled for VSYNC; this guarantees we
2709 // will handle it before the screenshot. When VSYNC finally arrives
2710 // the scheduled transaction will be a no-op. If no transactions are
2711 // scheduled at this time, this will end-up being a no-op as well.
2712 mEventQueue.invalidateTransactionNow();
2713
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002714 // this creates a "fake" BBinder which will serve as a "fake" remote
2715 // binder to receive the marshaled calls and forward them to the
2716 // real remote (a BpGraphicBufferProducer)
2717 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2718
2719 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2720 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002721 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002722 display, IGraphicBufferProducer::asInterface( wrapper ),
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002723 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002724
2725 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002726 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002727 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002728 }
2729 return res;
2730}
2731
Mathias Agopian180f10d2013-04-10 22:55:41 -07002732
2733void SurfaceFlinger::renderScreenImplLocked(
2734 const sp<const DisplayDevice>& hw,
2735 uint32_t reqWidth, uint32_t reqHeight,
2736 uint32_t minLayerZ, uint32_t maxLayerZ,
2737 bool yswap)
2738{
2739 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002740 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002741
2742 // get screen geometry
2743 const uint32_t hw_w = hw->getWidth();
2744 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002745 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2746
2747 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002748 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002749
2750 // set-up our viewport
Mathias Agopian3f844832013-08-07 21:24:32 -07002751 engine.setViewportAndProjection(reqWidth, reqHeight, hw_w, hw_h, yswap);
2752 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002753
2754 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002755 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002756
2757 const LayerVector& layers( mDrawingState.layersSortedByZ );
2758 const size_t count = layers.size();
2759 for (size_t i=0 ; i<count ; ++i) {
2760 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002761 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002762 if (state.layerStack == hw->getLayerStack()) {
2763 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2764 if (layer->isVisible()) {
2765 if (filtering) layer->setFiltering(true);
2766 layer->draw(hw);
2767 if (filtering) layer->setFiltering(false);
2768 }
2769 }
2770 }
2771 }
2772
2773 // compositionComplete is needed for older driver
2774 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002775 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002776}
2777
2778
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002779status_t SurfaceFlinger::captureScreenImplLocked(
2780 const sp<const DisplayDevice>& hw,
2781 const sp<IGraphicBufferProducer>& producer,
2782 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002783 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002784{
2785 ATRACE_CALL();
2786
Mathias Agopian180f10d2013-04-10 22:55:41 -07002787 // get screen geometry
2788 const uint32_t hw_w = hw->getWidth();
2789 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002790
Mathias Agopian180f10d2013-04-10 22:55:41 -07002791 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2792 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2793 reqWidth, reqHeight, hw_w, hw_h);
2794 return BAD_VALUE;
2795 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002796
Mathias Agopian180f10d2013-04-10 22:55:41 -07002797 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2798 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2799
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002800 // create a surface (because we're a producer, and we need to
2801 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07002802 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002803 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002804
2805 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002806 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002807 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2808 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002809
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002810 int err = 0;
2811 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07002812 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002813 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2814 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002815
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002816 if (err == NO_ERROR) {
2817 ANativeWindowBuffer* buffer;
2818 /* TODO: Once we have the sync framework everywhere this can use
2819 * server-side waits on the fence that dequeueBuffer returns.
2820 */
2821 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2822 if (result == NO_ERROR) {
2823 // create an EGLImage from the buffer so we can later
2824 // turn it into a texture
2825 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2826 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2827 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002828 // this binds the given EGLImage as a framebuffer for the
2829 // duration of this scope.
2830 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2831 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002832 // this will in fact render into our dequeued buffer
2833 // via an FBO, which means we didn't have to create
2834 // an EGLSurface and therefore we're not
2835 // dependent on the context's EGLConfig.
2836 renderScreenImplLocked(hw, reqWidth, reqHeight,
2837 minLayerZ, maxLayerZ, true);
Mathias Agopiand5556842013-09-19 17:08:37 -07002838
2839 if (DEBUG_SCREENSHOTS) {
2840 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
2841 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
2842 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
2843 hw, minLayerZ, maxLayerZ);
2844 delete [] pixels;
2845 }
2846
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002847 } else {
2848 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2849 result = INVALID_OPERATION;
2850 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002851 // destroy our image
2852 eglDestroyImageKHR(mEGLDisplay, image);
2853 } else {
2854 result = BAD_VALUE;
2855 }
2856 window->queueBuffer(window, buffer, -1);
2857 }
2858 } else {
2859 result = BAD_VALUE;
2860 }
2861 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2862 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002863
Mathias Agopian74c40c02010-09-29 13:02:36 -07002864 return result;
2865}
2866
Mathias Agopiand5556842013-09-19 17:08:37 -07002867void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
2868 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07002869 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07002870 for (size_t y=0 ; y<h ; y++) {
2871 uint32_t const * p = (uint32_t const *)vaddr + y*s;
2872 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07002873 if (p[x] != 0xFF000000) return;
2874 }
2875 }
2876 ALOGE("*** we just took a black screenshot ***\n"
2877 "requested minz=%d, maxz=%d, layerStack=%d",
2878 minLayerZ, maxLayerZ, hw->getLayerStack());
2879 const LayerVector& layers( mDrawingState.layersSortedByZ );
2880 const size_t count = layers.size();
2881 for (size_t i=0 ; i<count ; ++i) {
2882 const sp<Layer>& layer(layers[i]);
2883 const Layer::State& state(layer->getDrawingState());
2884 const bool visible = (state.layerStack == hw->getLayerStack())
2885 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2886 && (layer->isVisible());
2887 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
2888 visible ? '+' : '-',
2889 i, layer->getName().string(), state.layerStack, state.z,
2890 layer->isVisible(), state.flags, state.alpha);
2891 }
2892 }
2893}
2894
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002895// ---------------------------------------------------------------------------
2896
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002897SurfaceFlinger::LayerVector::LayerVector() {
2898}
2899
2900SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002901 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002902}
2903
2904int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2905 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002906{
Mathias Agopianbe246f82012-07-31 22:59:38 -07002907 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08002908 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
2909 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07002910
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002911 uint32_t ls = l->getCurrentState().layerStack;
2912 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002913 if (ls != rs)
2914 return ls - rs;
2915
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002916 uint32_t lz = l->getCurrentState().z;
2917 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07002918 if (lz != rz)
2919 return lz - rz;
2920
2921 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002922}
2923
2924// ---------------------------------------------------------------------------
2925
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002926SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
2927 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07002928}
2929
Mathias Agopian3ee454a2012-08-27 16:28:24 -07002930SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08002931 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07002932 viewport.makeInvalid();
2933 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07002934}
2935
2936// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002937
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002938}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07002939
2940
2941#if defined(__gl_h_)
2942#error "don't include gl/gl.h in this file"
2943#endif
2944
2945#if defined(__gl2_h_)
2946#error "don't include gl2/gl2.h in this file"
2947#endif