blob: 8ef4d6f27f1b2a2992ec49dcf91699fdee0431dc [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"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070062#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070063#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080064#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
Mathias Agopiana4912602012-07-12 14:25:33 -070069#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070070#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070071#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072
Mathias Agopianff2ed702013-09-01 21:36:12 -070073#include "Effects/Daltonizer.h"
74
Mathias Agopian875d8e12013-06-07 15:35:48 -070075#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070076#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070077
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078#define DISPLAY_COUNT 1
79
Mathias Agopianfee2b462013-07-03 12:34:01 -070080/*
81 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
82 * black pixels.
83 */
84#define DEBUG_SCREENSHOTS false
85
Mathias Agopianca088332013-03-28 17:44:13 -070086EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
87
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070089
90// This works around the lack of support for the sync framework on some
91// devices.
92#ifdef RUNNING_WITHOUT_SYNC_FRAMEWORK
93static const bool runningWithoutSyncFramework = true;
94#else
95static const bool runningWithoutSyncFramework = false;
96#endif
97
98// This is the phase offset in nanoseconds of the software vsync event
99// relative to the vsync event reported by HWComposer. The software vsync
100// event is when SurfaceFlinger and Choreographer-based applications run each
101// frame.
102//
103// This phase offset allows adjustment of the minimum latency from application
104// wake-up (by Choregographer) time to the time at which the resulting window
105// image is displayed. This value may be either positive (after the HW vsync)
106// or negative (before the HW vsync). Setting it to 0 will result in a
107// minimum latency of two vsync periods because the app and SurfaceFlinger
108// will run just after the HW vsync. Setting it to a positive number will
109// result in the minimum latency being:
110//
111// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
112//
113// Note that reducing this latency makes it more likely for the applications
114// to not have their window content image ready in time. When this happens
115// the latency will end up being an additional vsync period, and animations
116// will hiccup. Therefore, this latency should be tuned somewhat
117// conservatively (or at least with awareness of the trade-off being made).
118static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
119
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120// ---------------------------------------------------------------------------
121
Mathias Agopian99b49842011-06-27 16:05:52 -0700122const String16 sHardwareTest("android.permission.HARDWARE_TEST");
123const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
124const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
125const String16 sDump("android.permission.DUMP");
126
127// ---------------------------------------------------------------------------
128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700130 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700132 mTransactionPending(false),
133 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700134 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700135 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700136 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700139 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800140 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700142 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700143 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700144 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700145 mDebugInSwapBuffers(0),
146 mLastSwapBufferTime(0),
147 mDebugInTransaction(0),
148 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700149 mBootFinished(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700150 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700151 mHWVsyncAvailable(false),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700152 mDaltonize(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153{
Steve Blocka19954a2012-01-04 20:05:49 +0000154 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155
156 // debugging stuff...
157 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700158
Mathias Agopianb4b17302013-03-20 18:36:41 -0700159 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700160 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700161
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 property_get("debug.sf.showupdates", value, "0");
163 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700164
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700165 property_get("debug.sf.ddms", value, "0");
166 mDebugDDMS = atoi(value);
167 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700168 if (!startDdmConnection()) {
169 // start failed, and DDMS debugging not enabled
170 mDebugDDMS = 0;
171 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700172 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700173 ALOGI_IF(mDebugRegion, "showupdates enabled");
174 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175}
176
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800177void SurfaceFlinger::onFirstRef()
178{
179 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800180}
181
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182SurfaceFlinger::~SurfaceFlinger()
183{
Mathias Agopiana4912602012-07-12 14:25:33 -0700184 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
185 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
186 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187}
188
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800189void SurfaceFlinger::binderDied(const wp<IBinder>& who)
190{
191 // the window manager died on us. prepare its eulogy.
192
Andy McFadden13a082e2012-08-24 10:16:42 -0700193 // restore initial conditions (default device unblank, etc)
194 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800195
196 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700197 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800198}
199
Mathias Agopian7e27f052010-05-28 14:22:23 -0700200sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201{
Mathias Agopian96f08192010-06-02 23:28:45 -0700202 sp<ISurfaceComposerClient> bclient;
203 sp<Client> client(new Client(this));
204 status_t err = client->initCheck();
205 if (err == NO_ERROR) {
206 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 return bclient;
209}
210
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700211sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
212 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700213{
214 class DisplayToken : public BBinder {
215 sp<SurfaceFlinger> flinger;
216 virtual ~DisplayToken() {
217 // no more references, this display must be terminated
218 Mutex::Autolock _l(flinger->mStateLock);
219 flinger->mCurrentState.displays.removeItem(this);
220 flinger->setTransactionFlags(eDisplayTransactionNeeded);
221 }
222 public:
223 DisplayToken(const sp<SurfaceFlinger>& flinger)
224 : flinger(flinger) {
225 }
226 };
227
228 sp<BBinder> token = new DisplayToken(this);
229
230 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700231 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700232 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700233 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700234 mCurrentState.displays.add(token, info);
235
236 return token;
237}
238
Jesse Hall6c913be2013-08-08 12:15:49 -0700239void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
240 Mutex::Autolock _l(mStateLock);
241
242 ssize_t idx = mCurrentState.displays.indexOfKey(display);
243 if (idx < 0) {
244 ALOGW("destroyDisplay: invalid display token");
245 return;
246 }
247
248 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
249 if (!info.isVirtualDisplay()) {
250 ALOGE("destroyDisplay called for non-virtual display");
251 return;
252 }
253
254 mCurrentState.displays.removeItemsAt(idx);
255 setTransactionFlags(eDisplayTransactionNeeded);
256}
257
Jesse Hall692c7232012-11-08 15:41:56 -0800258void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
259 ALOGW_IF(mBuiltinDisplays[type],
260 "Overwriting display token for display type %d", type);
261 mBuiltinDisplays[type] = new BBinder();
262 DisplayDeviceState info(type);
263 // All non-virtual displays are currently considered secure.
264 info.isSecure = true;
265 mCurrentState.displays.add(mBuiltinDisplays[type], info);
266}
267
Mathias Agopiane57f2922012-08-09 16:29:12 -0700268sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700269 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700270 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
271 return NULL;
272 }
Jesse Hall692c7232012-11-08 15:41:56 -0800273 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700274}
275
Jamie Gennis9a78c902011-01-12 18:30:40 -0800276sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
277{
278 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
279 return gba;
280}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700281
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800282void SurfaceFlinger::bootFinished()
283{
284 const nsecs_t now = systemTime();
285 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000286 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700287 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700288
289 // wait patiently for the window manager death
290 const String16 name("window");
291 sp<IBinder> window(defaultServiceManager()->getService(name));
292 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700293 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700294 }
295
296 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700297 // formerly we would just kill the process, but we now ask it to exit so it
298 // can choose where to stop the animation.
299 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300}
301
Mathias Agopian3f844832013-08-07 21:24:32 -0700302void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700303 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700304 RenderEngine& engine;
305 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700306 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700307 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
308 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700309 }
310 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700311 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700312 return true;
313 }
314 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700315 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700316}
317
Mathias Agopian722b98f2012-09-25 18:24:31 -0700318status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700319 EGLDisplay dpy,
320 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700321 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700322 EGLConfig* outConfig)
323{
324 EGLConfig config = NULL;
325 EGLint numConfigs = -1, n=0;
326 eglGetConfigs(dpy, NULL, 0, &numConfigs);
327 EGLConfig* const configs = new EGLConfig[numConfigs];
328 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700329
Mathias Agopian722b98f2012-09-25 18:24:31 -0700330 if (n) {
331 if (attribute != EGL_NONE) {
332 for (int i=0 ; i<n ; i++) {
333 EGLint value = 0;
334 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
335 if (wanted == value) {
336 *outConfig = configs[i];
337 delete [] configs;
338 return NO_ERROR;
339 }
340 }
341 } else {
342 // just pick the first one
343 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700344 delete [] configs;
345 return NO_ERROR;
346 }
347 }
348 delete [] configs;
349 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800350}
351
Mathias Agopian722b98f2012-09-25 18:24:31 -0700352class EGLAttributeVector {
353 struct Attribute;
354 class Adder;
355 friend class Adder;
356 KeyedVector<Attribute, EGLint> mList;
357 struct Attribute {
358 Attribute() {};
359 Attribute(EGLint v) : v(v) { }
360 EGLint v;
361 bool operator < (const Attribute& other) const {
362 // this places EGL_NONE at the end
363 EGLint lhs(v);
364 EGLint rhs(other.v);
365 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
366 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
367 return lhs < rhs;
368 }
369 };
370 class Adder {
371 friend class EGLAttributeVector;
372 EGLAttributeVector& v;
373 EGLint attribute;
374 Adder(EGLAttributeVector& v, EGLint attribute)
375 : v(v), attribute(attribute) {
376 }
377 public:
378 void operator = (EGLint value) {
379 if (attribute != EGL_NONE) {
380 v.mList.add(attribute, value);
381 }
382 }
383 operator EGLint () const { return v.mList[attribute]; }
384 };
385public:
386 EGLAttributeVector() {
387 mList.add(EGL_NONE, EGL_NONE);
388 }
389 void remove(EGLint attribute) {
390 if (attribute != EGL_NONE) {
391 mList.removeItem(attribute);
392 }
393 }
394 Adder operator [] (EGLint attribute) {
395 return Adder(*this, attribute);
396 }
397 EGLint operator [] (EGLint attribute) const {
398 return mList[attribute];
399 }
400 // cast-operator to (EGLint const*)
401 operator EGLint const* () const { return &mList.keyAt(0).v; }
402};
403
Jesse Hallb65f32e2013-09-27 22:10:47 -0700404status_t SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId,
405 EGLint renderableType, EGLConfig* config) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700406 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
407 // it is to be used with WIFI displays
Mathias Agopiana4912602012-07-12 14:25:33 -0700408 status_t err;
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700409 EGLint wantedAttribute;
410 EGLint wantedAttributeValue;
Mathias Agopianda27af92012-09-13 18:17:13 -0700411
Mathias Agopian722b98f2012-09-25 18:24:31 -0700412 EGLAttributeVector attribs;
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700413 if (renderableType) {
414 attribs[EGL_RENDERABLE_TYPE] = renderableType;
415 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
416 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT|EGL_PBUFFER_BIT;
417 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
418 attribs[EGL_RED_SIZE] = 8;
419 attribs[EGL_GREEN_SIZE] = 8;
420 attribs[EGL_BLUE_SIZE] = 8;
421 wantedAttribute = EGL_NONE;
422 wantedAttributeValue = EGL_NONE;
Mathias Agopian722b98f2012-09-25 18:24:31 -0700423
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700424 } else {
425 // if no renderable type specified, fallback to a simplified query
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700426 wantedAttribute = EGL_NATIVE_VISUAL_ID;
427 wantedAttributeValue = nativeVisualId;
428 }
429
430 err = selectConfigForAttribute(display, attribs, wantedAttribute,
Jesse Hallb65f32e2013-09-27 22:10:47 -0700431 wantedAttributeValue, config);
432 if (err == NO_ERROR) {
433 EGLint caveat;
434 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
435 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
436 }
437 return err;
Mathias Agopiana4912602012-07-12 14:25:33 -0700438}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800439
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700440class DispSyncSource : public VSyncSource, private DispSync::Callback {
441public:
442 DispSyncSource(DispSync* dispSync) : mValue(0), mDispSync(dispSync) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700443
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700444 virtual ~DispSyncSource() {}
445
446 virtual void setVSyncEnabled(bool enable) {
447 // Do NOT lock the mutex here so as to avoid any mutex ordering issues
448 // with locking it in the onDispSyncEvent callback.
449 if (enable) {
450 status_t err = mDispSync->addEventListener(vsyncPhaseOffsetNs,
451 static_cast<DispSync::Callback*>(this));
452 if (err != NO_ERROR) {
453 ALOGE("error registering vsync callback: %s (%d)",
454 strerror(-err), err);
455 }
456 ATRACE_INT("VsyncOn", 1);
457 } else {
458 status_t err = mDispSync->removeEventListener(
459 static_cast<DispSync::Callback*>(this));
460 if (err != NO_ERROR) {
461 ALOGE("error unregistering vsync callback: %s (%d)",
462 strerror(-err), err);
463 }
464 ATRACE_INT("VsyncOn", 0);
465 }
466 }
467
468 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
469 Mutex::Autolock lock(mMutex);
470 mCallback = callback;
471 }
472
473private:
474 virtual void onDispSyncEvent(nsecs_t when) {
475 sp<VSyncSource::Callback> callback;
476 {
477 Mutex::Autolock lock(mMutex);
478 callback = mCallback;
479
480 mValue = (mValue + 1) % 2;
481 ATRACE_INT("VSYNC", mValue);
482 }
483
484 if (callback != NULL) {
485 callback->onVSyncEvent(when);
486 }
487 }
488
489 int mValue;
490
491 DispSync* mDispSync;
492 sp<VSyncSource::Callback> mCallback;
493 Mutex mMutex;
494};
495
496void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700497 ALOGI( "SurfaceFlinger's main thread ready to run. "
498 "Initializing graphics H/W...");
499
Jesse Hallb65f32e2013-09-27 22:10:47 -0700500 status_t err;
Jesse Hall692c7232012-11-08 15:41:56 -0800501 Mutex::Autolock _l(mStateLock);
502
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700503 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700504 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
505 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700506
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700507 // Initialize the H/W composer object. There may or may not be an
508 // actual hardware composer underneath.
509 mHwc = new HWComposer(this,
510 *static_cast<HWComposer::EventHandler *>(this));
511
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700512 // First try to get an ES2 config
Jesse Hallb65f32e2013-09-27 22:10:47 -0700513 err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(), EGL_OPENGL_ES2_BIT,
514 &mEGLConfig);
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700515
Jesse Hallb65f32e2013-09-27 22:10:47 -0700516 if (err != NO_ERROR) {
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700517 // If ES2 fails, try ES1
Jesse Hallb65f32e2013-09-27 22:10:47 -0700518 err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(),
519 EGL_OPENGL_ES_BIT, &mEGLConfig);
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700520 }
521
Jesse Hallb65f32e2013-09-27 22:10:47 -0700522 if (err != NO_ERROR) {
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700523 // still didn't work, probably because we're on the emulator...
524 // try a simplified query
525 ALOGW("no suitable EGLConfig found, trying a simpler query");
Jesse Hallb65f32e2013-09-27 22:10:47 -0700526 err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(), 0, &mEGLConfig);
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700527 }
528
Jesse Hallb65f32e2013-09-27 22:10:47 -0700529 if (err != NO_ERROR) {
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700530 // this EGL is too lame for android
531 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
532 }
Mathias Agopian875d8e12013-06-07 15:35:48 -0700533
534 // print some debugging info
535 EGLint r,g,b,a;
536 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_RED_SIZE, &r);
537 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_GREEN_SIZE, &g);
538 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_BLUE_SIZE, &b);
539 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_ALPHA_SIZE, &a);
540 ALOGI("EGL informations:");
541 ALOGI("vendor : %s", eglQueryString(mEGLDisplay, EGL_VENDOR));
542 ALOGI("version : %s", eglQueryString(mEGLDisplay, EGL_VERSION));
543 ALOGI("extensions: %s", eglQueryString(mEGLDisplay, EGL_EXTENSIONS));
544 ALOGI("Client API: %s", eglQueryString(mEGLDisplay, EGL_CLIENT_APIS)?:"Not Supported");
545 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
546
547 // get a RenderEngine for the given display / config (can't fail)
548 mRenderEngine = RenderEngine::create(mEGLDisplay, mEGLConfig);
549
550 // retrieve the EGL context that was selected/created
551 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700552
Mathias Agopianed985572013-03-22 00:24:39 -0700553 // figure out which format we got
554 eglGetConfigAttrib(mEGLDisplay, mEGLConfig,
555 EGL_NATIVE_VISUAL_ID, &mEGLNativeVisualId);
556
Mathias Agopianda27af92012-09-13 18:17:13 -0700557 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
558 "couldn't create EGLContext");
559
Mathias Agopiancde87a32012-09-13 14:09:01 -0700560 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700561 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700562 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700563 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700564 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700565 // All non-virtual displays are currently considered secure.
566 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800567 createBuiltinDisplayLocked(type);
568 wp<IBinder> token = mBuiltinDisplays[i];
569
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700570 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
571 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i, bq);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700572 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700573 type, allocateHwcDisplayId(type), isSecure, token,
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700574 fbs, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700575 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700576 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700577 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700578 // for displays other than the main display, so we always
579 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700580 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700581 hw->acquireScreen();
582 }
583 mDisplays.add(token, hw);
584 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700585 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700586
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700587 // make the GLContext current so that we can create textures when creating Layers
588 // (which may happens before we render something)
589 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
590
Mathias Agopian028508c2012-07-25 21:12:12 -0700591 // start the EventThread
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700592 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync);
593 mEventThread = new EventThread(vsyncSrc);
Mathias Agopian028508c2012-07-25 21:12:12 -0700594 mEventQueue.setEventThread(mEventThread);
595
Jamie Gennisd1700752013-10-14 12:22:52 -0700596 mEventControlThread = new EventControlThread(this);
597 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
598
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700599 // set a fake vsync period if there is no HWComposer
600 if (mHwc->initCheck() != NO_ERROR) {
601 mPrimaryDispSync.setPeriod(16666667);
602 }
603
Mathias Agopian92a979a2012-08-02 18:32:23 -0700604 // initialize our drawing state
605 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700606
Andy McFadden13a082e2012-08-24 10:16:42 -0700607 // set initial conditions (e.g. unblank default device)
608 initializeDisplays();
609
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700610 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700611 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800612}
613
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700614int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700615 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700616 type : mHwc->allocateDisplayId();
617}
618
Mathias Agopiana67e4182012-06-19 17:26:12 -0700619void SurfaceFlinger::startBootAnim() {
620 // start boot animation
621 property_set("service.bootanim.exit", "0");
622 property_set("ctl.start", "bootanim");
623}
624
Mathias Agopian875d8e12013-06-07 15:35:48 -0700625size_t SurfaceFlinger::getMaxTextureSize() const {
626 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700627}
628
Mathias Agopian875d8e12013-06-07 15:35:48 -0700629size_t SurfaceFlinger::getMaxViewportDims() const {
630 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700631}
632
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800633// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800634
Jamie Gennis582270d2011-08-17 18:19:00 -0700635bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800636 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800637 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800638 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700639 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800640}
641
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700642status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800643 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700644 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800645 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700646 type = i;
647 break;
648 }
649 }
650
651 if (type < 0) {
652 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700653 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700654
655 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700656 float xdpi = hwc.getDpiX(type);
657 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700658
659 // TODO: Not sure if display density should handled by SF any longer
660 class Density {
661 static int getDensityFromProperty(char const* propName) {
662 char property[PROPERTY_VALUE_MAX];
663 int density = 0;
664 if (property_get(propName, property, NULL) > 0) {
665 density = atoi(property);
666 }
667 return density;
668 }
669 public:
670 static int getEmuDensity() {
671 return getDensityFromProperty("qemu.sf.lcd_density"); }
672 static int getBuildDensity() {
673 return getDensityFromProperty("ro.sf.lcd_density"); }
674 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700675
676 if (type == DisplayDevice::DISPLAY_PRIMARY) {
677 // The density of the device is provided by a build property
678 float density = Density::getBuildDensity() / 160.0f;
679 if (density == 0) {
680 // the build doesn't provide a density -- this is wrong!
681 // use xdpi instead
682 ALOGE("ro.sf.lcd_density must be defined as a build property");
683 density = xdpi / 160.0f;
684 }
685 if (Density::getEmuDensity()) {
686 // if "qemu.sf.lcd_density" is specified, it overrides everything
687 xdpi = ydpi = density = Density::getEmuDensity();
688 density /= 160.0f;
689 }
690 info->density = density;
691
692 // TODO: this needs to go away (currently needed only by webkit)
693 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
694 info->orientation = hw->getOrientation();
Mathias Agopian1604f772012-09-18 21:54:42 -0700695 } else {
696 // TODO: where should this value come from?
697 static const int TV_DENSITY = 213;
698 info->density = TV_DENSITY / 160.0f;
699 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700700 }
701
Mathias Agopian1604f772012-09-18 21:54:42 -0700702 info->w = hwc.getWidth(type);
703 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700704 info->xdpi = xdpi;
705 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700706 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700707
708 // All non-virtual displays are currently considered secure.
709 info->secure = true;
710
Mathias Agopian888c8222012-08-04 21:10:38 -0700711 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700712}
713
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800714// ----------------------------------------------------------------------------
715
716sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800717 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700718}
719
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800720// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800721
722void SurfaceFlinger::waitForEvent() {
723 mEventQueue.waitMessage();
724}
725
726void SurfaceFlinger::signalTransaction() {
727 mEventQueue.invalidate();
728}
729
730void SurfaceFlinger::signalLayerUpdate() {
731 mEventQueue.invalidate();
732}
733
734void SurfaceFlinger::signalRefresh() {
735 mEventQueue.refresh();
736}
737
738status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
739 nsecs_t reltime, uint32_t flags) {
740 return mEventQueue.postMessage(msg, reltime);
741}
742
743status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
744 nsecs_t reltime, uint32_t flags) {
745 status_t res = mEventQueue.postMessage(msg, reltime);
746 if (res == NO_ERROR) {
747 msg->wait();
748 }
749 return res;
750}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800751
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700752void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700753 do {
754 waitForEvent();
755 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800756}
757
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700758void SurfaceFlinger::enableHardwareVsync() {
759 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700760 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700761 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700762 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
763 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700764 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700765 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700766}
767
Jesse Hall948fe0c2013-10-14 12:56:09 -0700768void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700769 Mutex::Autolock _l(mHWVsyncLock);
770
Jesse Hall948fe0c2013-10-14 12:56:09 -0700771 if (makeAvailable) {
772 mHWVsyncAvailable = true;
773 } else if (!mHWVsyncAvailable) {
774 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
775 return;
776 }
777
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700778 const nsecs_t period =
779 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
780
781 mPrimaryDispSync.reset();
782 mPrimaryDispSync.setPeriod(period);
783
784 if (!mPrimaryHWVsyncEnabled) {
785 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700786 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
787 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700788 mPrimaryHWVsyncEnabled = true;
789 }
790}
791
Jesse Hall948fe0c2013-10-14 12:56:09 -0700792void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700793 Mutex::Autolock _l(mHWVsyncLock);
794 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700795 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
796 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700797 mPrimaryDispSync.endResync();
798 mPrimaryHWVsyncEnabled = false;
799 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700800 if (makeUnavailable) {
801 mHWVsyncAvailable = false;
802 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700803}
804
805void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700806 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700807
Jamie Gennisd1700752013-10-14 12:22:52 -0700808 { // Scope for the lock
809 Mutex::Autolock _l(mHWVsyncLock);
810 if (type == 0 && mPrimaryHWVsyncEnabled) {
811 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700812 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700813 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700814
815 if (needsHwVsync) {
816 enableHardwareVsync();
817 } else {
818 disableHardwareVsync(false);
819 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700820}
821
822void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
823 if (mEventThread == NULL) {
824 // This is a temporary workaround for b/7145521. A non-null pointer
825 // does not mean EventThread has finished initializing, so this
826 // is not a correct fix.
827 ALOGW("WARNING: EventThread not started, ignoring hotplug");
828 return;
829 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700830
Jesse Hall9e663de2013-08-16 14:28:37 -0700831 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700832 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800833 if (connected) {
834 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700835 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800836 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
837 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700838 }
839 setTransactionFlags(eDisplayTransactionNeeded);
840
Andy McFadden9e9689c2012-10-10 18:17:51 -0700841 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700842 }
Mathias Agopian86303202012-07-24 22:46:10 -0700843}
844
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700845void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700846 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700847 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700848}
849
Mathias Agopian4fec8732012-06-29 14:12:52 -0700850void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800851 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800852 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700853 case MessageQueue::TRANSACTION:
854 handleMessageTransaction();
855 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700856 case MessageQueue::INVALIDATE:
857 handleMessageTransaction();
858 handleMessageInvalidate();
859 signalRefresh();
860 break;
861 case MessageQueue::REFRESH:
862 handleMessageRefresh();
863 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800864 }
865}
866
Mathias Agopian4fec8732012-06-29 14:12:52 -0700867void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700868 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700869 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700870 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700871 }
872}
873
874void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700875 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700876 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700877}
878
879void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700880 ATRACE_CALL();
881 preComposition();
882 rebuildLayerStacks();
883 setUpHWComposer();
884 doDebugFlashRegions();
885 doComposition();
886 postComposition();
887}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700888
Mathias Agopiancd60f992012-08-16 16:28:27 -0700889void SurfaceFlinger::doDebugFlashRegions()
890{
891 // is debugging enabled
892 if (CC_LIKELY(!mDebugRegion))
893 return;
894
895 const bool repaintEverything = mRepaintEverything;
896 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
897 const sp<DisplayDevice>& hw(mDisplays[dpy]);
898 if (hw->canDraw()) {
899 // transform the dirty region into this screen's coordinate space
900 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
901 if (!dirtyRegion.isEmpty()) {
902 // redraw the whole screen
903 doComposeSurfaces(hw, Region(hw->bounds()));
904
905 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700906 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700907 RenderEngine& engine(getRenderEngine());
908 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
909
Mathias Agopiancd60f992012-08-16 16:28:27 -0700910 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700911 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700912 }
913 }
914 }
915
916 postFramebuffer();
917
918 if (mDebugRegion > 1) {
919 usleep(mDebugRegion * 1000);
920 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700921
922 HWComposer& hwc(getHwComposer());
923 if (hwc.initCheck() == NO_ERROR) {
924 status_t err = hwc.prepare();
925 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
926 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700927}
928
929void SurfaceFlinger::preComposition()
930{
931 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700932 const LayerVector& layers(mDrawingState.layersSortedByZ);
933 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700934 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700935 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700936 needExtraInvalidate = true;
937 }
938 }
939 if (needExtraInvalidate) {
940 signalLayerUpdate();
941 }
942}
943
944void SurfaceFlinger::postComposition()
945{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700946 const LayerVector& layers(mDrawingState.layersSortedByZ);
947 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700948 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700949 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700950 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800951
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700952 const HWComposer& hwc = getHwComposer();
953 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
954
955 if (presentFence->isValid()) {
956 if (mPrimaryDispSync.addPresentFence(presentFence)) {
957 enableHardwareVsync();
958 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700959 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700960 }
961 }
962
963 if (runningWithoutSyncFramework) {
964 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
965 if (hw->isScreenAcquired()) {
966 enableHardwareVsync();
967 }
968 }
969
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800970 if (mAnimCompositionPending) {
971 mAnimCompositionPending = false;
972
Jesse Halla9a1b002013-02-27 16:39:25 -0800973 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800974 mAnimFrameTracker.setActualPresentFence(presentFence);
975 } else {
976 // The HWC doesn't support present fences, so use the refresh
977 // timestamp instead.
978 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
979 mAnimFrameTracker.setActualPresentTime(presentTime);
980 }
981 mAnimFrameTracker.advanceFrame();
982 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700983}
984
985void SurfaceFlinger::rebuildLayerStacks() {
986 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700987 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700988 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700989 mVisibleRegionsDirty = false;
990 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700991
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700992 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700993 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700994 Region opaqueRegion;
995 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800996 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700997 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700998 const Transform& tr(hw->getTransform());
999 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001000 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001001 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001002 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001003
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001004 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001005 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001006 const sp<Layer>& layer(layers[i]);
1007 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001008 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -07001009 Region drawRegion(tr.transform(
1010 layer->visibleNonTransparentRegion));
1011 drawRegion.andSelf(bounds);
1012 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001013 layersSortedByZ.add(layer);
1014 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001015 }
1016 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001017 }
Mathias Agopian42977342012-08-05 00:40:46 -07001018 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001019 hw->undefinedRegion.set(bounds);
1020 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
1021 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001022 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001023 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001024}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001025
Mathias Agopiancd60f992012-08-16 16:28:27 -07001026void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001027 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1028 mDisplays[dpy]->beginFrame();
1029 }
1030
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001031 HWComposer& hwc(getHwComposer());
1032 if (hwc.initCheck() == NO_ERROR) {
1033 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001034 if (CC_UNLIKELY(mHwWorkListDirty)) {
1035 mHwWorkListDirty = false;
1036 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1037 sp<const DisplayDevice> hw(mDisplays[dpy]);
1038 const int32_t id = hw->getHwcDisplayId();
1039 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001040 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001041 hw->getVisibleLayersSortedByZ());
1042 const size_t count = currentLayers.size();
1043 if (hwc.createWorkList(id, count) == NO_ERROR) {
1044 HWComposer::LayerListIterator cur = hwc.begin(id);
1045 const HWComposer::LayerListIterator end = hwc.end(id);
1046 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001047 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001048 layer->setGeometry(hw, *cur);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001049 if (mDebugDisableHWC || mDebugRegion || mDaltonize) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001050 cur->setSkip(true);
1051 }
1052 }
1053 }
1054 }
1055 }
1056 }
1057
1058 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001059 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001060 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001061 const int32_t id = hw->getHwcDisplayId();
1062 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001063 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001064 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001065 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001066 HWComposer::LayerListIterator cur = hwc.begin(id);
1067 const HWComposer::LayerListIterator end = hwc.end(id);
1068 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1069 /*
1070 * update the per-frame h/w composer data for each layer
1071 * and build the transparent region of the FB
1072 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001073 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001074 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -07001075 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001076 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001077 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001078
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001079 status_t err = hwc.prepare();
1080 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001081
1082 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1083 sp<const DisplayDevice> hw(mDisplays[dpy]);
1084 hw->prepareFrame(hwc);
1085 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001086 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001087}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001088
Mathias Agopiancd60f992012-08-16 16:28:27 -07001089void SurfaceFlinger::doComposition() {
1090 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001091 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001092 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001093 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -07001094 if (hw->canDraw()) {
1095 // transform the dirty region into this screen's coordinate space
1096 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001097
1098 // repaint the framebuffer (if needed)
1099 doDisplayComposition(hw, dirtyRegion);
1100
Mathias Agopiancd60f992012-08-16 16:28:27 -07001101 hw->dirtyRegion.clear();
1102 hw->flip(hw->swapRegion);
1103 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001104 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001105 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001106 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001107 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001108 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001109}
1110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111void SurfaceFlinger::postFramebuffer()
1112{
Mathias Agopian841cde52012-03-01 15:44:37 -08001113 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001114
Mathias Agopiana44b0412011-10-16 18:46:35 -07001115 const nsecs_t now = systemTime();
1116 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001117
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001118 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001119 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001120 if (!hwc.supportsFramebufferTarget()) {
1121 // EGL spec says:
1122 // "surface must be bound to the calling thread's current context,
1123 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001124 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001125 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001126 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001127 }
1128
Mathias Agopian6da15f42013-09-25 20:40:07 -07001129 // make the default display current because the VirtualDisplayDevice code cannot
1130 // deal with dequeueBuffer() being called outside of the composition loop; however
1131 // the code below can call glFlush() which is allowed (and does in some case) call
1132 // dequeueBuffer().
1133 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1134
Mathias Agopian92a979a2012-08-02 18:32:23 -07001135 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001136 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001137 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001138 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001139 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001140 int32_t id = hw->getHwcDisplayId();
1141 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001142 HWComposer::LayerListIterator cur = hwc.begin(id);
1143 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001144 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001145 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001146 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001147 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001148 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001149 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001150 }
Jesse Hallef194142012-06-14 14:45:17 -07001151 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001152 }
1153
Mathias Agopiana44b0412011-10-16 18:46:35 -07001154 mLastSwapBufferTime = systemTime() - now;
1155 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001156
1157 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1158 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1159 logFrameStats();
1160 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001161}
1162
Mathias Agopian87baae12012-07-31 12:38:26 -07001163void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164{
Mathias Agopian841cde52012-03-01 15:44:37 -08001165 ATRACE_CALL();
1166
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001167 // here we keep a copy of the drawing state (that is the state that's
1168 // going to be overwritten by handleTransactionLocked()) outside of
1169 // mStateLock so that the side-effects of the State assignment
1170 // don't happen with mStateLock held (which can cause deadlocks).
1171 State drawingState(mDrawingState);
1172
Mathias Agopianca4d3602011-05-19 15:38:14 -07001173 Mutex::Autolock _l(mStateLock);
1174 const nsecs_t now = systemTime();
1175 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001176
Mathias Agopianca4d3602011-05-19 15:38:14 -07001177 // Here we're guaranteed that some transaction flags are set
1178 // so we can call handleTransactionLocked() unconditionally.
1179 // We call getTransactionFlags(), which will also clear the flags,
1180 // with mStateLock held to guarantee that mCurrentState won't change
1181 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001182
Mathias Agopiane57f2922012-08-09 16:29:12 -07001183 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001184 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001185
Mathias Agopianca4d3602011-05-19 15:38:14 -07001186 mLastTransactionTime = systemTime() - now;
1187 mDebugInTransaction = 0;
1188 invalidateHwcGeometry();
1189 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001190}
1191
Mathias Agopian87baae12012-07-31 12:38:26 -07001192void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001193{
1194 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001195 const size_t count = currentLayers.size();
1196
1197 /*
1198 * Traversal of the children
1199 * (perform the transaction for each of them if needed)
1200 */
1201
Mathias Agopian3559b072012-08-15 13:46:03 -07001202 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001203 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001204 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001205 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1206 if (!trFlags) continue;
1207
1208 const uint32_t flags = layer->doTransaction(0);
1209 if (flags & Layer::eVisibleRegion)
1210 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001211 }
1212 }
1213
1214 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001215 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001216 */
1217
Mathias Agopiane57f2922012-08-09 16:29:12 -07001218 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001219 // here we take advantage of Vector's copy-on-write semantics to
1220 // improve performance by skipping the transaction entirely when
1221 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001222 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1223 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001224 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001225 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001226 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001227 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001228
1229 // find the displays that were removed
1230 // (ie: in drawing state but not in current state)
1231 // also handle displays that changed
1232 // (ie: displays that are in both lists)
1233 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001234 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1235 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001236 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001237 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001238 // Call makeCurrent() on the primary display so we can
1239 // be sure that nothing associated with this display
1240 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001241 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001242 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001243 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1244 if (hw != NULL)
1245 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001246 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001247 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001248 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001249 } else {
1250 ALOGW("trying to remove the main display");
1251 }
1252 } else {
1253 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001254 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001255 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001256 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001257 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001258 // recreating the DisplayDevice, so we just remove it
1259 // from the drawing state, so that it get re-added
1260 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001261 sp<DisplayDevice> hw(getDisplayDevice(display));
1262 if (hw != NULL)
1263 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001264 mDisplays.removeItem(display);
1265 mDrawingState.displays.removeItemsAt(i);
1266 dc--; i--;
1267 // at this point we must loop to the next item
1268 continue;
1269 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001270
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001271 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001272 if (disp != NULL) {
1273 if (state.layerStack != draw[i].layerStack) {
1274 disp->setLayerStack(state.layerStack);
1275 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001276 if ((state.orientation != draw[i].orientation)
1277 || (state.viewport != draw[i].viewport)
1278 || (state.frame != draw[i].frame))
1279 {
1280 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001281 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001282 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001283 }
1284 }
1285 }
1286
1287 // find displays that were added
1288 // (ie: in current state but not in drawing state)
1289 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001290 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001291 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001292
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001293 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001294 sp<IGraphicBufferProducer> producer;
1295 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
1296
Jesse Hall02d86562013-03-25 14:43:23 -07001297 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001298 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001299 // Virtual displays without a surface are dormant:
1300 // they have external state (layer stack, projection,
1301 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001302 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001303
Jesse Hall02d86562013-03-25 14:43:23 -07001304 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001305 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1306 *mHwc, hwcDisplayId, state.surface, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001307 state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001308
1309 dispSurface = vds;
1310 if (hwcDisplayId >= 0) {
1311 producer = vds;
1312 } else {
1313 // There won't be any interaction with HWC for this virtual display,
1314 // so the GLES driver can pass buffers directly to the sink.
1315 producer = state.surface;
1316 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001317 }
1318 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001319 ALOGE_IF(state.surface!=NULL,
1320 "adding a supported display, but rendering "
1321 "surface is provided (%p), ignoring it",
1322 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001323 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001324 // for supported (by hwc) displays we provide our
1325 // own rendering surface
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001326 dispSurface = new FramebufferSurface(*mHwc, state.type, bq);
1327 producer = bq;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001328 }
1329
1330 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001331 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001332 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001333 state.type, hwcDisplayId, state.isSecure,
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001334 display, dispSurface, producer, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001335 hw->setLayerStack(state.layerStack);
1336 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001337 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001338 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001339 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001340 if (state.isVirtualDisplay()) {
1341 if (hwcDisplayId >= 0) {
1342 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1343 hw->getWidth(), hw->getHeight(),
1344 hw->getFormat());
1345 }
1346 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001347 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001348 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001349 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001350 }
1351 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001352 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001353 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001354
Mathias Agopian84300952012-11-21 16:02:13 -08001355 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1356 // The transform hint might have changed for some layers
1357 // (either because a display has changed, or because a layer
1358 // as changed).
1359 //
1360 // Walk through all the layers in currentLayers,
1361 // and update their transform hint.
1362 //
1363 // If a layer is visible only on a single display, then that
1364 // display is used to calculate the hint, otherwise we use the
1365 // default display.
1366 //
1367 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1368 // the hint is set before we acquire a buffer from the surface texture.
1369 //
1370 // NOTE: layer transactions have taken place already, so we use their
1371 // drawing state. However, SurfaceFlinger's own transaction has not
1372 // happened yet, so we must use the current state layer list
1373 // (soon to become the drawing state list).
1374 //
1375 sp<const DisplayDevice> disp;
1376 uint32_t currentlayerStack = 0;
1377 for (size_t i=0; i<count; i++) {
1378 // NOTE: we rely on the fact that layers are sorted by
1379 // layerStack first (so we don't have to traverse the list
1380 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001381 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001382 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001383 if (i==0 || currentlayerStack != layerStack) {
1384 currentlayerStack = layerStack;
1385 // figure out if this layerstack is mirrored
1386 // (more than one display) if so, pick the default display,
1387 // if not, pick the only display it's on.
1388 disp.clear();
1389 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1390 sp<const DisplayDevice> hw(mDisplays[dpy]);
1391 if (hw->getLayerStack() == currentlayerStack) {
1392 if (disp == NULL) {
1393 disp = hw;
1394 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001395 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001396 break;
1397 }
1398 }
1399 }
1400 }
Chet Haase91d25932013-04-11 15:24:55 -07001401 if (disp == NULL) {
1402 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1403 // redraw after transform hint changes. See bug 8508397.
1404
1405 // could be null when this layer is using a layerStack
1406 // that is not visible on any display. Also can occur at
1407 // screen off/on times.
1408 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001409 }
Chet Haase91d25932013-04-11 15:24:55 -07001410 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001411 }
1412 }
1413
1414
Mathias Agopian3559b072012-08-15 13:46:03 -07001415 /*
1416 * Perform our own transaction if needed
1417 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001418
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001419 const LayerVector& layers(mDrawingState.layersSortedByZ);
1420 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001421 // layers have been added
1422 mVisibleRegionsDirty = true;
1423 }
1424
1425 // some layers might have been removed, so
1426 // we need to update the regions they're exposing.
1427 if (mLayersRemoved) {
1428 mLayersRemoved = false;
1429 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001430 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001431 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001432 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001433 if (currentLayers.indexOf(layer) < 0) {
1434 // this layer is not visible anymore
1435 // TODO: we could traverse the tree from front to back and
1436 // compute the actual visible region
1437 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001438 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001439 Region visibleReg = s.transform.transform(
1440 Region(Rect(s.active.w, s.active.h)));
1441 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001442 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001443 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001444 }
1445
1446 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001447}
1448
1449void SurfaceFlinger::commitTransaction()
1450{
1451 if (!mLayersPendingRemoval.isEmpty()) {
1452 // Notify removed layers now that they can't be drawn from
1453 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1454 mLayersPendingRemoval[i]->onRemoved();
1455 }
1456 mLayersPendingRemoval.clear();
1457 }
1458
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001459 // If this transaction is part of a window animation then the next frame
1460 // we composite should be considered an animation as well.
1461 mAnimCompositionPending = mAnimTransactionPending;
1462
Mathias Agopian4fec8732012-06-29 14:12:52 -07001463 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001464 mTransactionPending = false;
1465 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001466 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467}
1468
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001469void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001470 const LayerVector& currentLayers, uint32_t layerStack,
1471 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001472{
Mathias Agopian841cde52012-03-01 15:44:37 -08001473 ATRACE_CALL();
1474
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001475 Region aboveOpaqueLayers;
1476 Region aboveCoveredLayers;
1477 Region dirty;
1478
Mathias Agopian87baae12012-07-31 12:38:26 -07001479 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001480
1481 size_t i = currentLayers.size();
1482 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001483 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001484
1485 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001486 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001487
Jesse Hall01e29052013-02-19 16:13:35 -08001488 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001489 if (s.layerStack != layerStack)
1490 continue;
1491
Mathias Agopianab028732010-03-16 16:41:46 -07001492 /*
1493 * opaqueRegion: area of a surface that is fully opaque.
1494 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001496
1497 /*
1498 * visibleRegion: area of a surface that is visible on screen
1499 * and not fully transparent. This is essentially the layer's
1500 * footprint minus the opaque regions above it.
1501 * Areas covered by a translucent surface are considered visible.
1502 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001503 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001504
1505 /*
1506 * coveredRegion: area of a surface that is covered by all
1507 * visible regions above it (which includes the translucent areas).
1508 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001509 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001510
Jesse Halla8026d22012-09-25 13:25:04 -07001511 /*
1512 * transparentRegion: area of a surface that is hinted to be completely
1513 * transparent. This is only used to tell when the layer has no visible
1514 * non-transparent regions and can be removed from the layer list. It
1515 * does not affect the visibleRegion of this layer or any layers
1516 * beneath it. The hint may not be correct if apps don't respect the
1517 * SurfaceView restrictions (which, sadly, some don't).
1518 */
1519 Region transparentRegion;
1520
Mathias Agopianab028732010-03-16 16:41:46 -07001521
1522 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001523 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001524 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001525 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001526 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001527 if (!visibleRegion.isEmpty()) {
1528 // Remove the transparent area from the visible region
1529 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001530 const Transform tr(s.transform);
1531 if (tr.transformed()) {
1532 if (tr.preserveRects()) {
1533 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001534 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001535 } else {
1536 // transformation too complex, can't do the
1537 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001538 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001539 }
1540 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001541 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001542 }
Mathias Agopianab028732010-03-16 16:41:46 -07001543 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001544
Mathias Agopianab028732010-03-16 16:41:46 -07001545 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001546 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001547 if (s.alpha==255 && !translucent &&
1548 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1549 // the opaque region is the layer's footprint
1550 opaqueRegion = visibleRegion;
1551 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001552 }
1553 }
1554
Mathias Agopianab028732010-03-16 16:41:46 -07001555 // Clip the covered region to the visible region
1556 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1557
1558 // Update aboveCoveredLayers for next (lower) layer
1559 aboveCoveredLayers.orSelf(visibleRegion);
1560
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001561 // subtract the opaque region covered by the layers above us
1562 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001563
1564 // compute this layer's dirty region
1565 if (layer->contentDirty) {
1566 // we need to invalidate the whole region
1567 dirty = visibleRegion;
1568 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001569 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001570 layer->contentDirty = false;
1571 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001572 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001573 * the exposed region consists of two components:
1574 * 1) what's VISIBLE now and was COVERED before
1575 * 2) what's EXPOSED now less what was EXPOSED before
1576 *
1577 * note that (1) is conservative, we start with the whole
1578 * visible region but only keep what used to be covered by
1579 * something -- which mean it may have been exposed.
1580 *
1581 * (2) handles areas that were not covered by anything but got
1582 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001583 */
Mathias Agopianab028732010-03-16 16:41:46 -07001584 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001585 const Region oldVisibleRegion = layer->visibleRegion;
1586 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001587 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1588 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001589 }
1590 dirty.subtractSelf(aboveOpaqueLayers);
1591
1592 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001593 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001594
Mathias Agopianab028732010-03-16 16:41:46 -07001595 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001596 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001597
Jesse Halla8026d22012-09-25 13:25:04 -07001598 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001599 layer->setVisibleRegion(visibleRegion);
1600 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001601 layer->setVisibleNonTransparentRegion(
1602 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001603 }
1604
Mathias Agopian87baae12012-07-31 12:38:26 -07001605 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001606}
1607
Mathias Agopian87baae12012-07-31 12:38:26 -07001608void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1609 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001610 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001611 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1612 if (hw->getLayerStack() == layerStack) {
1613 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001614 }
1615 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001616}
1617
1618void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001619{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001620 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001621
Mathias Agopian4fec8732012-06-29 14:12:52 -07001622 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001623 const LayerVector& layers(mDrawingState.layersSortedByZ);
1624 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001625 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001626 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001627 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001628 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001629 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001630 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001631
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001632 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001633}
1634
Mathias Agopianad456f92011-01-13 17:53:01 -08001635void SurfaceFlinger::invalidateHwcGeometry()
1636{
1637 mHwWorkListDirty = true;
1638}
1639
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001640
Mathias Agopiancd60f992012-08-16 16:28:27 -07001641void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001642 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001643{
Mathias Agopian87baae12012-07-31 12:38:26 -07001644 Region dirtyRegion(inDirtyRegion);
1645
Mathias Agopianb8a55602009-06-26 19:06:36 -07001646 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001647 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001648
Mathias Agopian42977342012-08-05 00:40:46 -07001649 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001650 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001651 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1652 // takes a rectangle, we must make sure to update that whole
1653 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001654 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001655 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001656 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001657 // We need to redraw the rectangle that will be updated
1658 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001659 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001660 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001661 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001662 } else {
1663 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001664 dirtyRegion.set(hw->bounds());
1665 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001666 }
1667 }
1668
Mathias Agopianff2ed702013-09-01 21:36:12 -07001669 if (CC_LIKELY(!mDaltonize)) {
1670 doComposeSurfaces(hw, dirtyRegion);
1671 } else {
1672 RenderEngine& engine(getRenderEngine());
1673 engine.beginGroup(mDaltonizer());
1674 doComposeSurfaces(hw, dirtyRegion);
1675 engine.endGroup();
1676 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001677
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001678 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001679 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001680
1681 // swap buffers (presentation)
1682 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683}
1684
Mathias Agopiancd60f992012-08-16 16:28:27 -07001685void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001686{
Mathias Agopian3f844832013-08-07 21:24:32 -07001687 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001688 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001689 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001690 HWComposer::LayerListIterator cur = hwc.begin(id);
1691 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001692
Jesse Halld05a17f2013-09-30 16:49:30 -07001693 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001694 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001695 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001696 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1697 hw->getDisplayName().string());
1698 return;
1699 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001700
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001701 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001702 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001703 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001704 // when using overlays, we assume a fully transparent framebuffer
1705 // NOTE: we could reduce how much we need to clear, for instance
1706 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001707 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001708 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001709 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001710 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001711 // we start with the whole screen area
1712 const Region bounds(hw->getBounds());
1713
1714 // we remove the scissor part
1715 // we're left with the letterbox region
1716 // (common case is that letterbox ends-up being empty)
1717 const Region letterbox(bounds.subtract(hw->getScissor()));
1718
1719 // compute the area to clear
1720 Region region(hw->undefinedRegion.merge(letterbox));
1721
1722 // but limit it to the dirty region
1723 region.andSelf(dirty);
1724
Mathias Agopianb9494d52012-04-18 02:28:45 -07001725 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001726 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001727 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001728 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001729 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001730 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001731
Mathias Agopian766dc492012-10-30 18:08:06 -07001732 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1733 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001734 // scissor on the main display. It should never be needed
1735 // anyways (though in theory it could since the API allows it).
1736 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001737 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001738 if (scissor != bounds) {
1739 // scissor doesn't match the screen's dimensions, so we
1740 // need to clear everything outside of it and enable
1741 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001742
Mathias Agopianf45c5102012-10-24 16:29:17 -07001743 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001744 const uint32_t height = hw->getHeight();
1745 engine.setScissor(scissor.left, height - scissor.bottom,
1746 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001747 }
1748 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001749 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001750
Mathias Agopian85d751c2012-08-29 16:59:24 -07001751 /*
1752 * and then, render the layers targeted at the framebuffer
1753 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001754
Mathias Agopian13127d82013-03-05 17:47:11 -08001755 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001756 const size_t count = layers.size();
1757 const Transform& tr = hw->getTransform();
1758 if (cur != end) {
1759 // we're using h/w composer
1760 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001761 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001762 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001763 if (!clip.isEmpty()) {
1764 switch (cur->getCompositionType()) {
1765 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001766 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001767 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1768 && i
Mathias Agopianac683022013-10-01 15:36:52 -07001769 && layer->isOpaque() && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001770 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001771 // never clear the very first layer since we're
1772 // guaranteed the FB is already cleared
1773 layer->clearWithOpenGL(hw, clip);
1774 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001775 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001776 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001777 case HWC_FRAMEBUFFER: {
1778 layer->draw(hw, clip);
1779 break;
1780 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001781 case HWC_FRAMEBUFFER_TARGET: {
1782 // this should not happen as the iterator shouldn't
1783 // let us get there.
1784 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1785 break;
1786 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001787 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001788 }
1789 layer->setAcquireFence(hw, *cur);
1790 }
1791 } else {
1792 // we're not using h/w composer
1793 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001794 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001795 const Region clip(dirty.intersect(
1796 tr.transform(layer->visibleRegion)));
1797 if (!clip.isEmpty()) {
1798 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001799 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001800 }
1801 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001802
1803 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001804 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001805}
1806
Mathias Agopian3f844832013-08-07 21:24:32 -07001807void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001808 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001809 RenderEngine& engine(getRenderEngine());
1810 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001811}
1812
Mathias Agopianac9fa422013-02-11 16:40:36 -08001813void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1814 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001815 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001816 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001817{
Mathias Agopian96f08192010-06-02 23:28:45 -07001818 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001819 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001820
Mathias Agopian96f08192010-06-02 23:28:45 -07001821 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001822 Mutex::Autolock _l(mStateLock);
1823 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001824 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001825}
1826
Mathias Agopian3f844832013-08-07 21:24:32 -07001827status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001828 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001829 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001830 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001831 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001832 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001833 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001834 return NO_ERROR;
1835 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001836 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001837}
1838
Mathias Agopian3f844832013-08-07 21:24:32 -07001839uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001840 return android_atomic_release_load(&mTransactionFlags);
1841}
1842
Mathias Agopian3f844832013-08-07 21:24:32 -07001843uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001844 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1845}
1846
Mathias Agopian3f844832013-08-07 21:24:32 -07001847uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001848 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1849 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001850 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001851 }
1852 return old;
1853}
1854
Mathias Agopian8b33f032012-07-24 20:43:54 -07001855void SurfaceFlinger::setTransactionState(
1856 const Vector<ComposerState>& state,
1857 const Vector<DisplayState>& displays,
1858 uint32_t flags)
1859{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001860 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001861 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001862 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001863
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001864 if (flags & eAnimation) {
1865 // For window updates that are part of an animation we must wait for
1866 // previous animation "frames" to be handled.
1867 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001868 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001869 if (CC_UNLIKELY(err != NO_ERROR)) {
1870 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001871 // caller after a few seconds.
1872 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1873 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001874 mAnimTransactionPending = false;
1875 break;
1876 }
1877 }
1878 }
1879
Mathias Agopiane57f2922012-08-09 16:29:12 -07001880 size_t count = displays.size();
1881 for (size_t i=0 ; i<count ; i++) {
1882 const DisplayState& s(displays[i]);
1883 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001884 }
1885
Mathias Agopiane57f2922012-08-09 16:29:12 -07001886 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001887 for (size_t i=0 ; i<count ; i++) {
1888 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001889 // Here we need to check that the interface we're given is indeed
1890 // one of our own. A malicious client could give us a NULL
1891 // IInterface, or one of its own or even one of our own but a
1892 // different type. All these situations would cause us to crash.
1893 //
1894 // NOTE: it would be better to use RTTI as we could directly check
1895 // that we have a Client*. however, RTTI is disabled in Android.
1896 if (s.client != NULL) {
1897 sp<IBinder> binder = s.client->asBinder();
1898 if (binder != NULL) {
1899 String16 desc(binder->getInterfaceDescriptor());
1900 if (desc == ISurfaceComposerClient::descriptor) {
1901 sp<Client> client( static_cast<Client *>(s.client.get()) );
1902 transactionFlags |= setClientStateLocked(client, s.state);
1903 }
1904 }
1905 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001906 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001907
Mathias Agopian386aa982011-11-07 21:58:03 -08001908 if (transactionFlags) {
1909 // this triggers the transaction
1910 setTransactionFlags(transactionFlags);
1911
1912 // if this is a synchronous transaction, wait for it to take effect
1913 // before returning.
1914 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001915 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001916 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001917 if (flags & eAnimation) {
1918 mAnimTransactionPending = true;
1919 }
1920 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001921 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1922 if (CC_UNLIKELY(err != NO_ERROR)) {
1923 // just in case something goes wrong in SF, return to the
1924 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001925 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1926 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001927 break;
1928 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001929 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001930 }
1931}
1932
Mathias Agopiane57f2922012-08-09 16:29:12 -07001933uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1934{
Jesse Hall9a143922012-10-04 16:29:19 -07001935 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1936 if (dpyIdx < 0)
1937 return 0;
1938
Mathias Agopiane57f2922012-08-09 16:29:12 -07001939 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001940 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001941 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001942 const uint32_t what = s.what;
1943 if (what & DisplayState::eSurfaceChanged) {
1944 if (disp.surface->asBinder() != s.surface->asBinder()) {
1945 disp.surface = s.surface;
1946 flags |= eDisplayTransactionNeeded;
1947 }
1948 }
1949 if (what & DisplayState::eLayerStackChanged) {
1950 if (disp.layerStack != s.layerStack) {
1951 disp.layerStack = s.layerStack;
1952 flags |= eDisplayTransactionNeeded;
1953 }
1954 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001955 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001956 if (disp.orientation != s.orientation) {
1957 disp.orientation = s.orientation;
1958 flags |= eDisplayTransactionNeeded;
1959 }
1960 if (disp.frame != s.frame) {
1961 disp.frame = s.frame;
1962 flags |= eDisplayTransactionNeeded;
1963 }
1964 if (disp.viewport != s.viewport) {
1965 disp.viewport = s.viewport;
1966 flags |= eDisplayTransactionNeeded;
1967 }
1968 }
1969 }
1970 return flags;
1971}
1972
1973uint32_t SurfaceFlinger::setClientStateLocked(
1974 const sp<Client>& client,
1975 const layer_state_t& s)
1976{
1977 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001978 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001979 if (layer != 0) {
1980 const uint32_t what = s.what;
1981 if (what & layer_state_t::ePositionChanged) {
1982 if (layer->setPosition(s.x, s.y))
1983 flags |= eTraversalNeeded;
1984 }
1985 if (what & layer_state_t::eLayerChanged) {
1986 // NOTE: index needs to be calculated before we update the state
1987 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1988 if (layer->setLayer(s.z)) {
1989 mCurrentState.layersSortedByZ.removeAt(idx);
1990 mCurrentState.layersSortedByZ.add(layer);
1991 // we need traversal (state changed)
1992 // AND transaction (list changed)
1993 flags |= eTransactionNeeded|eTraversalNeeded;
1994 }
1995 }
1996 if (what & layer_state_t::eSizeChanged) {
1997 if (layer->setSize(s.w, s.h)) {
1998 flags |= eTraversalNeeded;
1999 }
2000 }
2001 if (what & layer_state_t::eAlphaChanged) {
2002 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2003 flags |= eTraversalNeeded;
2004 }
2005 if (what & layer_state_t::eMatrixChanged) {
2006 if (layer->setMatrix(s.matrix))
2007 flags |= eTraversalNeeded;
2008 }
2009 if (what & layer_state_t::eTransparentRegionChanged) {
2010 if (layer->setTransparentRegionHint(s.transparentRegion))
2011 flags |= eTraversalNeeded;
2012 }
2013 if (what & layer_state_t::eVisibilityChanged) {
2014 if (layer->setFlags(s.flags, s.mask))
2015 flags |= eTraversalNeeded;
2016 }
2017 if (what & layer_state_t::eCropChanged) {
2018 if (layer->setCrop(s.crop))
2019 flags |= eTraversalNeeded;
2020 }
2021 if (what & layer_state_t::eLayerStackChanged) {
2022 // NOTE: index needs to be calculated before we update the state
2023 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2024 if (layer->setLayerStack(s.layerStack)) {
2025 mCurrentState.layersSortedByZ.removeAt(idx);
2026 mCurrentState.layersSortedByZ.add(layer);
2027 // we need traversal (state changed)
2028 // AND transaction (list changed)
2029 flags |= eTransactionNeeded|eTraversalNeeded;
2030 }
2031 }
2032 }
2033 return flags;
2034}
2035
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002036status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002037 const String8& name,
2038 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002039 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2040 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002041{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002042 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002043 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002044 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002045 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002046 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002047 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002048
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002049 status_t result = NO_ERROR;
2050
2051 sp<Layer> layer;
2052
Mathias Agopian3165cc22012-08-08 19:42:09 -07002053 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2054 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002055 result = createNormalLayer(client,
2056 name, w, h, flags, format,
2057 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002058 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002059 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002060 result = createDimLayer(client,
2061 name, w, h, flags,
2062 handle, gbp, &layer);
2063 break;
2064 default:
2065 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002066 break;
2067 }
2068
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002069 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002070 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002071 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002072 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002073 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002074}
2075
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002076status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2077 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2078 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002079{
2080 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002081 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002082 case PIXEL_FORMAT_TRANSPARENT:
2083 case PIXEL_FORMAT_TRANSLUCENT:
2084 format = PIXEL_FORMAT_RGBA_8888;
2085 break;
2086 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07002087#ifdef NO_RGBX_8888
2088 format = PIXEL_FORMAT_RGB_565;
2089#else
Mathias Agopian8f105402010-04-05 18:01:24 -07002090 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07002091#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002092 break;
2093 }
2094
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07002095#ifdef NO_RGBX_8888
2096 if (format == PIXEL_FORMAT_RGBX_8888)
2097 format = PIXEL_FORMAT_RGBA_8888;
2098#endif
2099
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002100 *outLayer = new Layer(this, client, name, w, h, flags);
2101 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2102 if (err == NO_ERROR) {
2103 *handle = (*outLayer)->getHandle();
2104 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002105 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002106
2107 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2108 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002109}
2110
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002111status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2112 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2113 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002114{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002115 *outLayer = new LayerDim(this, client, name, w, h, flags);
2116 *handle = (*outLayer)->getHandle();
2117 *gbp = (*outLayer)->getBufferQueue();
2118 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002119}
2120
Mathias Agopianac9fa422013-02-11 16:40:36 -08002121status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002122{
Mathias Agopian67106042013-03-14 19:18:13 -07002123 // called by the window manager when it wants to remove a Layer
2124 status_t err = NO_ERROR;
2125 sp<Layer> l(client->getLayerUser(handle));
2126 if (l != NULL) {
2127 err = removeLayer(l);
2128 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2129 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002130 }
2131 return err;
2132}
2133
Mathias Agopian13127d82013-03-05 17:47:11 -08002134status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002135{
Mathias Agopian67106042013-03-14 19:18:13 -07002136 // called by ~LayerCleaner() when all references to the IBinder (handle)
2137 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002138 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002139 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002140 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002141 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002142 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002143 "error removing layer=%p (%s)", l.get(), strerror(-err));
2144 }
2145 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002146}
2147
Mathias Agopianb60314a2012-04-10 22:09:54 -07002148// ---------------------------------------------------------------------------
2149
Andy McFadden13a082e2012-08-24 10:16:42 -07002150void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002151 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002152 Vector<ComposerState> state;
2153 Vector<DisplayState> displays;
2154 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002155 d.what = DisplayState::eDisplayProjectionChanged |
2156 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002157 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002158 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002159 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002160 d.frame.makeInvalid();
2161 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002162 displays.add(d);
2163 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002164 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07002165
2166 const nsecs_t period =
2167 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2168 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002169}
2170
2171void SurfaceFlinger::initializeDisplays() {
2172 class MessageScreenInitialized : public MessageBase {
2173 SurfaceFlinger* flinger;
2174 public:
2175 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2176 virtual bool handler() {
2177 flinger->onInitializeDisplays();
2178 return true;
2179 }
2180 };
2181 sp<MessageBase> msg = new MessageScreenInitialized(this);
2182 postMessageAsync(msg); // we may be called from main thread, use async message
2183}
2184
2185
Mathias Agopiancde87a32012-09-13 14:09:01 -07002186void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002187 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2188 if (hw->isScreenAcquired()) {
2189 // this is expected, e.g. when power manager wakes up during boot
2190 ALOGD(" screen was previously acquired");
2191 return;
2192 }
2193
Mathias Agopian42977342012-08-05 00:40:46 -07002194 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002195 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002196 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002197 // built-in display, tell the HWC
2198 getHwComposer().acquire(type);
2199
2200 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2201 // FIXME: eventthread only knows about the main display right now
2202 mEventThread->onScreenAcquired();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07002203
Jesse Hall948fe0c2013-10-14 12:56:09 -07002204 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002205 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002206 }
Mathias Agopian20128302012-08-13 18:32:13 -07002207 mVisibleRegionsDirty = true;
2208 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002209}
2210
Mathias Agopiancde87a32012-09-13 14:09:01 -07002211void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002212 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2213 if (!hw->isScreenAcquired()) {
2214 ALOGD(" screen was previously released");
2215 return;
2216 }
2217
2218 hw->releaseScreen();
2219 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002220 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002221 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002222 disableHardwareVsync(true); // also cancels any in-progress resync
2223
Mathias Agopiancde87a32012-09-13 14:09:01 -07002224 // FIXME: eventthread only knows about the main display right now
2225 mEventThread->onScreenReleased();
2226 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002227
2228 // built-in display, tell the HWC
2229 getHwComposer().release(type);
2230 }
2231 mVisibleRegionsDirty = true;
2232 // from this point on, SF will stop drawing on this display
2233}
2234
2235void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2236 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002237 SurfaceFlinger& mFlinger;
2238 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002239 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002240 MessageScreenAcquired(SurfaceFlinger& flinger,
2241 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002242 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002243 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2244 if (hw == NULL) {
2245 ALOGE("Attempt to unblank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002246 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002247 ALOGW("Attempt to unblank virtual display");
2248 } else {
2249 mFlinger.onScreenAcquired(hw);
2250 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002251 return true;
2252 }
2253 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002254 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2255 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002256}
2257
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002258void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002259 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002260 SurfaceFlinger& mFlinger;
2261 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002262 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002263 MessageScreenReleased(SurfaceFlinger& flinger,
2264 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002265 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002266 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2267 if (hw == NULL) {
2268 ALOGE("Attempt to blank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002269 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002270 ALOGW("Attempt to blank virtual display");
2271 } else {
2272 mFlinger.onScreenReleased(hw);
2273 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002274 return true;
2275 }
2276 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002277 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2278 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002279}
2280
2281// ---------------------------------------------------------------------------
2282
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002283status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2284{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002285 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002286
Mathias Agopianbd115332013-04-18 16:41:04 -07002287 IPCThreadState* ipc = IPCThreadState::self();
2288 const int pid = ipc->getCallingPid();
2289 const int uid = ipc->getCallingUid();
2290 if ((uid != AID_SHELL) &&
2291 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002292 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002293 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002294 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002295 // Try to get the main lock, but don't insist if we can't
2296 // (this would indicate SF is stuck, but we want to be able to
2297 // print something in dumpsys).
2298 int retry = 3;
2299 while (mStateLock.tryLock()<0 && --retry>=0) {
2300 usleep(1000000);
2301 }
2302 const bool locked(retry >= 0);
2303 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002304 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002305 "SurfaceFlinger appears to be unresponsive, "
2306 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002307 }
2308
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002309 bool dumpAll = true;
2310 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002311 size_t numArgs = args.size();
2312 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002313 if ((index < numArgs) &&
2314 (args[index] == String16("--list"))) {
2315 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002316 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002317 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002318 }
2319
2320 if ((index < numArgs) &&
2321 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002322 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002323 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002324 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002325 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002326
2327 if ((index < numArgs) &&
2328 (args[index] == String16("--latency-clear"))) {
2329 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002330 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002331 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002332 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002333 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002334
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002335 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002336 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002337 }
2338
Mathias Agopian9795c422009-08-26 16:36:26 -07002339 if (locked) {
2340 mStateLock.unlock();
2341 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002342 }
2343 write(fd, result.string(), result.size());
2344 return NO_ERROR;
2345}
2346
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002347void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002348 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002349{
2350 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2351 const size_t count = currentLayers.size();
2352 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002353 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002354 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002355 }
2356}
2357
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002358void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002359 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002360{
2361 String8 name;
2362 if (index < args.size()) {
2363 name = String8(args[index]);
2364 index++;
2365 }
2366
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002367 const nsecs_t period =
2368 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2369 result.appendFormat("%lld\n", period);
2370
2371 if (name.isEmpty()) {
2372 mAnimFrameTracker.dump(result);
2373 } else {
2374 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2375 const size_t count = currentLayers.size();
2376 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002377 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002378 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002379 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002380 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002381 }
2382 }
2383}
2384
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002385void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002386 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002387{
2388 String8 name;
2389 if (index < args.size()) {
2390 name = String8(args[index]);
2391 index++;
2392 }
2393
2394 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2395 const size_t count = currentLayers.size();
2396 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002397 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002398 if (name.isEmpty() || (name == layer->getName())) {
2399 layer->clearStats();
2400 }
2401 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002402
2403 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002404}
2405
Jamie Gennis6547ff42013-07-16 20:12:42 -07002406// This should only be called from the main thread. Otherwise it would need
2407// the lock and should use mCurrentState rather than mDrawingState.
2408void SurfaceFlinger::logFrameStats() {
2409 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2410 const size_t count = drawingLayers.size();
2411 for (size_t i=0 ; i<count ; i++) {
2412 const sp<Layer>& layer(drawingLayers[i]);
2413 layer->logFrameStats();
2414 }
2415
2416 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2417}
2418
Andy McFadden4803b742012-09-24 19:07:20 -07002419/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2420{
2421 static const char* config =
2422 " [sf"
2423#ifdef NO_RGBX_8888
2424 " NO_RGBX_8888"
2425#endif
2426#ifdef HAS_CONTEXT_PRIORITY
2427 " HAS_CONTEXT_PRIORITY"
2428#endif
2429#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2430 " NEVER_DEFAULT_TO_ASYNC_MODE"
2431#endif
2432#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2433 " TARGET_DISABLE_TRIPLE_BUFFERING"
2434#endif
2435 "]";
2436 result.append(config);
2437}
2438
Mathias Agopian74d211a2013-04-22 16:55:35 +02002439void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2440 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002441{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002442 bool colorize = false;
2443 if (index < args.size()
2444 && (args[index] == String16("--color"))) {
2445 colorize = true;
2446 index++;
2447 }
2448
2449 Colorizer colorizer(colorize);
2450
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002451 // figure out if we're stuck somewhere
2452 const nsecs_t now = systemTime();
2453 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2454 const nsecs_t inTransaction(mDebugInTransaction);
2455 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2456 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2457
2458 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002459 * Dump library configuration.
2460 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002461
2462 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002463 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002464 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002465 appendSfConfigString(result);
2466 appendUiConfigString(result);
2467 appendGuiConfigString(result);
2468 result.append("\n");
2469
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002470 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002471 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002472 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002473 result.append(SyncFeatures::getInstance().toString());
2474 result.append("\n");
2475
Andy McFadden4803b742012-09-24 19:07:20 -07002476 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002477 * Dump the visible layer list
2478 */
2479 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2480 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002481 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002482 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002483 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002484 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002485 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002486 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002487 }
2488
2489 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002490 * Dump Display state
2491 */
2492
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002493 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002494 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002495 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002496 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2497 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002498 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002499 }
2500
2501 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002502 * Dump SurfaceFlinger global state
2503 */
2504
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002505 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002506 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002507 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002508
Mathias Agopian888c8222012-08-04 21:10:38 -07002509 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002510 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002511
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002512 colorizer.bold(result);
2513 result.appendFormat("EGL implementation : %s\n",
2514 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2515 colorizer.reset(result);
2516 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002517 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002518
Mathias Agopian875d8e12013-06-07 15:35:48 -07002519 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002520
Mathias Agopian42977342012-08-05 00:40:46 -07002521 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002522 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002523 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002524 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002525 " last eglSwapBuffers() time: %f us\n"
2526 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002527 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002528 " refresh-rate : %f fps\n"
2529 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002530 " y-dpi : %f\n"
2531 " EGL_NATIVE_VISUAL_ID : %d\n"
2532 " gpu_to_cpu_unsupported : %d\n"
2533 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002534 mLastSwapBufferTime/1000.0,
2535 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002536 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002537 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2538 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002539 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2540 mEGLNativeVisualId,
2541 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002542
Mathias Agopian74d211a2013-04-22 16:55:35 +02002543 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002544 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002545
Mathias Agopian74d211a2013-04-22 16:55:35 +02002546 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002547 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002548
2549 /*
2550 * VSYNC state
2551 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002552 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002553
2554 /*
2555 * Dump HWComposer state
2556 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002557 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002558 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002559 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002560 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002561 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopianff2ed702013-09-01 21:36:12 -07002562 (mDebugDisableHWC || mDebugRegion || mDaltonize) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002563 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002564
2565 /*
2566 * Dump gralloc state
2567 */
2568 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2569 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002570}
2571
Mathias Agopian13127d82013-03-05 17:47:11 -08002572const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002573SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002574 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002575 wp<IBinder> dpy;
2576 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2577 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2578 dpy = mDisplays.keyAt(i);
2579 break;
2580 }
2581 }
2582 if (dpy == NULL) {
2583 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2584 // Just use the primary display so we have something to return
2585 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2586 }
2587 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002588}
2589
Keun young Park63f165f2012-08-31 10:53:36 -07002590bool SurfaceFlinger::startDdmConnection()
2591{
2592 void* libddmconnection_dso =
2593 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2594 if (!libddmconnection_dso) {
2595 return false;
2596 }
2597 void (*DdmConnection_start)(const char* name);
2598 DdmConnection_start =
2599 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2600 if (!DdmConnection_start) {
2601 dlclose(libddmconnection_dso);
2602 return false;
2603 }
2604 (*DdmConnection_start)(getServiceName());
2605 return true;
2606}
2607
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002608status_t SurfaceFlinger::onTransact(
2609 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2610{
2611 switch (code) {
2612 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002613 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002614 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002615 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002616 case BLANK:
2617 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002618 {
2619 // codes that require permission check
2620 IPCThreadState* ipc = IPCThreadState::self();
2621 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002622 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002623 if ((uid != AID_GRAPHICS) &&
2624 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002625 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002626 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2627 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002628 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002629 break;
2630 }
2631 case CAPTURE_SCREEN:
2632 {
2633 // codes that require permission check
2634 IPCThreadState* ipc = IPCThreadState::self();
2635 const int pid = ipc->getCallingPid();
2636 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002637 if ((uid != AID_GRAPHICS) &&
2638 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002639 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002640 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2641 return PERMISSION_DENIED;
2642 }
2643 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002644 }
2645 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002646
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002647 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2648 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002649 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002650 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002651 IPCThreadState* ipc = IPCThreadState::self();
2652 const int pid = ipc->getCallingPid();
2653 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002654 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002655 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002656 return PERMISSION_DENIED;
2657 }
2658 int n;
2659 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002660 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002661 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002662 return NO_ERROR;
2663 case 1002: // SHOW_UPDATES
2664 n = data.readInt32();
2665 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002666 invalidateHwcGeometry();
2667 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002668 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002669 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002670 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002671 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002672 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002673 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002674 setTransactionFlags(
2675 eTransactionNeeded|
2676 eDisplayTransactionNeeded|
2677 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002678 return NO_ERROR;
2679 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002680 case 1006:{ // send empty update
2681 signalRefresh();
2682 return NO_ERROR;
2683 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002684 case 1008: // toggle use of hw composer
2685 n = data.readInt32();
2686 mDebugDisableHWC = n ? 1 : 0;
2687 invalidateHwcGeometry();
2688 repaintEverything();
2689 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002690 case 1009: // toggle use of transform hint
2691 n = data.readInt32();
2692 mDebugDisableTransformHint = n ? 1 : 0;
2693 invalidateHwcGeometry();
2694 repaintEverything();
2695 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002696 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002697 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002698 reply->writeInt32(0);
2699 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002700 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002701 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002702 return NO_ERROR;
2703 case 1013: {
2704 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002705 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2706 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002707 return NO_ERROR;
2708 }
2709 case 1014: {
2710 // daltonize
2711 n = data.readInt32();
2712 switch (n % 10) {
2713 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2714 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2715 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2716 }
2717 if (n >= 10) {
2718 mDaltonizer.setMode(Daltonizer::correction);
2719 } else {
2720 mDaltonizer.setMode(Daltonizer::simulation);
2721 }
2722 mDaltonize = n > 0;
2723 invalidateHwcGeometry();
2724 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002725 }
2726 return NO_ERROR;
2727 }
2728 }
2729 return err;
2730}
2731
Mathias Agopian53331da2011-08-22 21:44:41 -07002732void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002733 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002734 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002735}
2736
Mathias Agopian59119e62010-10-11 12:37:43 -07002737// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002738// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002739// ---------------------------------------------------------------------------
2740
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002741/* The code below is here to handle b/8734824
2742 *
2743 * We create a IGraphicBufferProducer wrapper that forwards all calls
2744 * to the calling binder thread, where they are executed. This allows
2745 * the calling thread to be reused (on the other side) and not
2746 * depend on having "enough" binder threads to handle the requests.
2747 *
2748 */
2749
2750class GraphicProducerWrapper : public BBinder, public MessageHandler {
2751 sp<IGraphicBufferProducer> impl;
2752 sp<Looper> looper;
2753 status_t result;
2754 bool exitPending;
2755 bool exitRequested;
2756 mutable Barrier barrier;
2757 volatile int32_t memoryBarrier;
2758 uint32_t code;
2759 Parcel const* data;
2760 Parcel* reply;
2761
2762 enum {
2763 MSG_API_CALL,
2764 MSG_EXIT
2765 };
2766
2767 /*
2768 * this is called by our "fake" BpGraphicBufferProducer. We package the
2769 * data and reply Parcel and forward them to the calling thread.
2770 */
2771 virtual status_t transact(uint32_t code,
2772 const Parcel& data, Parcel* reply, uint32_t flags) {
2773 this->code = code;
2774 this->data = &data;
2775 this->reply = reply;
2776 android_atomic_acquire_store(0, &memoryBarrier);
2777 if (exitPending) {
2778 // if we've exited, we run the message synchronously right here
2779 handleMessage(Message(MSG_API_CALL));
2780 } else {
2781 barrier.close();
2782 looper->sendMessage(this, Message(MSG_API_CALL));
2783 barrier.wait();
2784 }
2785 return NO_ERROR;
2786 }
2787
2788 /*
2789 * here we run on the binder calling thread. All we've got to do is
2790 * call the real BpGraphicBufferProducer.
2791 */
2792 virtual void handleMessage(const Message& message) {
2793 android_atomic_release_load(&memoryBarrier);
2794 if (message.what == MSG_API_CALL) {
2795 impl->asBinder()->transact(code, data[0], reply);
2796 barrier.open();
2797 } else if (message.what == MSG_EXIT) {
2798 exitRequested = true;
2799 }
2800 }
2801
2802public:
2803 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2804 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2805 exitPending(false), exitRequested(false) {
2806 }
2807
2808 status_t waitForResponse() {
2809 do {
2810 looper->pollOnce(-1);
2811 } while (!exitRequested);
2812 return result;
2813 }
2814
2815 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002816 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002817 exitPending = true;
2818 looper->sendMessage(this, Message(MSG_EXIT));
2819 }
2820};
2821
2822
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002823status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2824 const sp<IGraphicBufferProducer>& producer,
2825 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002826 uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002827
2828 if (CC_UNLIKELY(display == 0))
2829 return BAD_VALUE;
2830
2831 if (CC_UNLIKELY(producer == 0))
2832 return BAD_VALUE;
2833
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002834 // if we have secure windows on this display, never allow the screen capture
2835 // unless the producer interface is local (i.e.: we can take a screenshot for
2836 // ourselves).
2837 if (!producer->asBinder()->localBinder()) {
2838 Mutex::Autolock _l(mStateLock);
2839 sp<const DisplayDevice> hw(getDisplayDevice(display));
2840 if (hw->getSecureLayerVisible()) {
2841 ALOGW("FB is protected: PERMISSION_DENIED");
2842 return PERMISSION_DENIED;
2843 }
2844 }
2845
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002846 class MessageCaptureScreen : public MessageBase {
2847 SurfaceFlinger* flinger;
2848 sp<IBinder> display;
2849 sp<IGraphicBufferProducer> producer;
2850 uint32_t reqWidth, reqHeight;
2851 uint32_t minLayerZ,maxLayerZ;
2852 status_t result;
2853 public:
2854 MessageCaptureScreen(SurfaceFlinger* flinger,
2855 const sp<IBinder>& display,
2856 const sp<IGraphicBufferProducer>& producer,
2857 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002858 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002859 : flinger(flinger), display(display), producer(producer),
2860 reqWidth(reqWidth), reqHeight(reqHeight),
2861 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2862 result(PERMISSION_DENIED)
2863 {
2864 }
2865 status_t getResult() const {
2866 return result;
2867 }
2868 virtual bool handler() {
2869 Mutex::Autolock _l(flinger->mStateLock);
2870 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002871 result = flinger->captureScreenImplLocked(hw,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002872 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002873 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002874 return true;
2875 }
2876 };
2877
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002878 // make sure to process transactions before screenshots -- a transaction
2879 // might already be pending but scheduled for VSYNC; this guarantees we
2880 // will handle it before the screenshot. When VSYNC finally arrives
2881 // the scheduled transaction will be a no-op. If no transactions are
2882 // scheduled at this time, this will end-up being a no-op as well.
2883 mEventQueue.invalidateTransactionNow();
2884
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002885 // this creates a "fake" BBinder which will serve as a "fake" remote
2886 // binder to receive the marshaled calls and forward them to the
2887 // real remote (a BpGraphicBufferProducer)
2888 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2889
2890 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2891 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002892 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002893 display, IGraphicBufferProducer::asInterface( wrapper ),
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002894 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002895
2896 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002897 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002898 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002899 }
2900 return res;
2901}
2902
Mathias Agopian180f10d2013-04-10 22:55:41 -07002903
2904void SurfaceFlinger::renderScreenImplLocked(
2905 const sp<const DisplayDevice>& hw,
2906 uint32_t reqWidth, uint32_t reqHeight,
2907 uint32_t minLayerZ, uint32_t maxLayerZ,
2908 bool yswap)
2909{
2910 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002911 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002912
2913 // get screen geometry
2914 const uint32_t hw_w = hw->getWidth();
2915 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002916 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2917
2918 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002919 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002920
2921 // set-up our viewport
Mathias Agopian3f844832013-08-07 21:24:32 -07002922 engine.setViewportAndProjection(reqWidth, reqHeight, hw_w, hw_h, yswap);
2923 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002924
2925 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002926 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002927
2928 const LayerVector& layers( mDrawingState.layersSortedByZ );
2929 const size_t count = layers.size();
2930 for (size_t i=0 ; i<count ; ++i) {
2931 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002932 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002933 if (state.layerStack == hw->getLayerStack()) {
2934 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2935 if (layer->isVisible()) {
2936 if (filtering) layer->setFiltering(true);
2937 layer->draw(hw);
2938 if (filtering) layer->setFiltering(false);
2939 }
2940 }
2941 }
2942 }
2943
2944 // compositionComplete is needed for older driver
2945 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002946 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002947}
2948
2949
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002950status_t SurfaceFlinger::captureScreenImplLocked(
2951 const sp<const DisplayDevice>& hw,
2952 const sp<IGraphicBufferProducer>& producer,
2953 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002954 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002955{
2956 ATRACE_CALL();
2957
Mathias Agopian180f10d2013-04-10 22:55:41 -07002958 // get screen geometry
2959 const uint32_t hw_w = hw->getWidth();
2960 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002961
Mathias Agopian180f10d2013-04-10 22:55:41 -07002962 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2963 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2964 reqWidth, reqHeight, hw_w, hw_h);
2965 return BAD_VALUE;
2966 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002967
Mathias Agopian180f10d2013-04-10 22:55:41 -07002968 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2969 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2970
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002971 // create a surface (because we're a producer, and we need to
2972 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07002973 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002974 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002975
2976 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002977 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002978 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2979 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002980
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002981 int err = 0;
2982 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07002983 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002984 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2985 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002986
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002987 if (err == NO_ERROR) {
2988 ANativeWindowBuffer* buffer;
2989 /* TODO: Once we have the sync framework everywhere this can use
2990 * server-side waits on the fence that dequeueBuffer returns.
2991 */
2992 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2993 if (result == NO_ERROR) {
2994 // create an EGLImage from the buffer so we can later
2995 // turn it into a texture
2996 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2997 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2998 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002999 // this binds the given EGLImage as a framebuffer for the
3000 // duration of this scope.
3001 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3002 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003003 // this will in fact render into our dequeued buffer
3004 // via an FBO, which means we didn't have to create
3005 // an EGLSurface and therefore we're not
3006 // dependent on the context's EGLConfig.
3007 renderScreenImplLocked(hw, reqWidth, reqHeight,
3008 minLayerZ, maxLayerZ, true);
Mathias Agopiand5556842013-09-19 17:08:37 -07003009
Andy McFadden2d8d1202013-10-09 16:38:02 -07003010 // Create a sync point and wait on it, so we know the buffer is
3011 // ready before we pass it along. We can't trivially call glFlush(),
3012 // so we use a wait flag instead.
3013 // TODO: pass a sync fd to queueBuffer() and let the consumer wait.
3014 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3015 if (sync != EGL_NO_SYNC_KHR) {
3016 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3017 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3018 EGLint eglErr = eglGetError();
3019 eglDestroySyncKHR(mEGLDisplay, sync);
3020 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3021 ALOGW("captureScreen: fence wait timed out");
3022 } else {
3023 ALOGW_IF(eglErr != EGL_SUCCESS,
3024 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3025 }
3026 } else {
3027 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3028 // not fatal
3029 }
3030
Mathias Agopiand5556842013-09-19 17:08:37 -07003031 if (DEBUG_SCREENSHOTS) {
3032 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3033 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3034 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3035 hw, minLayerZ, maxLayerZ);
3036 delete [] pixels;
3037 }
3038
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003039 } else {
3040 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3041 result = INVALID_OPERATION;
3042 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003043 // destroy our image
3044 eglDestroyImageKHR(mEGLDisplay, image);
3045 } else {
3046 result = BAD_VALUE;
3047 }
3048 window->queueBuffer(window, buffer, -1);
3049 }
3050 } else {
3051 result = BAD_VALUE;
3052 }
3053 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3054 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003055
Mathias Agopian74c40c02010-09-29 13:02:36 -07003056 return result;
3057}
3058
Mathias Agopiand5556842013-09-19 17:08:37 -07003059void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3060 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003061 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003062 for (size_t y=0 ; y<h ; y++) {
3063 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3064 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003065 if (p[x] != 0xFF000000) return;
3066 }
3067 }
3068 ALOGE("*** we just took a black screenshot ***\n"
3069 "requested minz=%d, maxz=%d, layerStack=%d",
3070 minLayerZ, maxLayerZ, hw->getLayerStack());
3071 const LayerVector& layers( mDrawingState.layersSortedByZ );
3072 const size_t count = layers.size();
3073 for (size_t i=0 ; i<count ; ++i) {
3074 const sp<Layer>& layer(layers[i]);
3075 const Layer::State& state(layer->getDrawingState());
3076 const bool visible = (state.layerStack == hw->getLayerStack())
3077 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3078 && (layer->isVisible());
3079 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
3080 visible ? '+' : '-',
3081 i, layer->getName().string(), state.layerStack, state.z,
3082 layer->isVisible(), state.flags, state.alpha);
3083 }
3084 }
3085}
3086
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003087// ---------------------------------------------------------------------------
3088
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003089SurfaceFlinger::LayerVector::LayerVector() {
3090}
3091
3092SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003093 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003094}
3095
3096int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3097 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003098{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003099 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003100 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3101 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003102
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003103 uint32_t ls = l->getCurrentState().layerStack;
3104 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003105 if (ls != rs)
3106 return ls - rs;
3107
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003108 uint32_t lz = l->getCurrentState().z;
3109 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003110 if (lz != rz)
3111 return lz - rz;
3112
3113 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003114}
3115
3116// ---------------------------------------------------------------------------
3117
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003118SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
3119 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003120}
3121
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003122SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08003123 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003124 viewport.makeInvalid();
3125 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003126}
3127
3128// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003129
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003130}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003131
3132
3133#if defined(__gl_h_)
3134#error "don't include gl/gl.h in this file"
3135#endif
3136
3137#if defined(__gl2_h_)
3138#error "don't include gl2/gl2.h in this file"
3139#endif