blob: bc8cfb8a61dba9984599642c5409c2f1fc15a91e [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"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080063#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067
Mathias Agopiana4912602012-07-12 14:25:33 -070068#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070069#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070070#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071
Mathias Agopianff2ed702013-09-01 21:36:12 -070072#include "Effects/Daltonizer.h"
73
Mathias Agopian875d8e12013-06-07 15:35:48 -070074#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070075#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070076
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077#define DISPLAY_COUNT 1
78
Mathias Agopianfee2b462013-07-03 12:34:01 -070079/*
80 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
81 * black pixels.
82 */
83#define DEBUG_SCREENSHOTS false
84
Mathias Agopianca088332013-03-28 17:44:13 -070085EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
86
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070088
89// This works around the lack of support for the sync framework on some
90// devices.
91#ifdef RUNNING_WITHOUT_SYNC_FRAMEWORK
92static const bool runningWithoutSyncFramework = true;
93#else
94static const bool runningWithoutSyncFramework = false;
95#endif
96
97// This is the phase offset in nanoseconds of the software vsync event
98// relative to the vsync event reported by HWComposer. The software vsync
99// event is when SurfaceFlinger and Choreographer-based applications run each
100// frame.
101//
102// This phase offset allows adjustment of the minimum latency from application
103// wake-up (by Choregographer) time to the time at which the resulting window
104// image is displayed. This value may be either positive (after the HW vsync)
105// or negative (before the HW vsync). Setting it to 0 will result in a
106// minimum latency of two vsync periods because the app and SurfaceFlinger
107// will run just after the HW vsync. Setting it to a positive number will
108// result in the minimum latency being:
109//
110// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
111//
112// Note that reducing this latency makes it more likely for the applications
113// to not have their window content image ready in time. When this happens
114// the latency will end up being an additional vsync period, and animations
115// will hiccup. Therefore, this latency should be tuned somewhat
116// conservatively (or at least with awareness of the trade-off being made).
117static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
118
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119// ---------------------------------------------------------------------------
120
Mathias Agopian99b49842011-06-27 16:05:52 -0700121const String16 sHardwareTest("android.permission.HARDWARE_TEST");
122const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
123const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
124const String16 sDump("android.permission.DUMP");
125
126// ---------------------------------------------------------------------------
127
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700129 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700131 mTransactionPending(false),
132 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700133 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700134 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700135 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700138 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800139 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700141 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700142 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700143 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700144 mDebugInSwapBuffers(0),
145 mLastSwapBufferTime(0),
146 mDebugInTransaction(0),
147 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700148 mBootFinished(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700149 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700150 mHWVsyncAvailable(false),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700151 mDaltonize(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152{
Steve Blocka19954a2012-01-04 20:05:49 +0000153 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800154
155 // debugging stuff...
156 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700157
Mathias Agopianb4b17302013-03-20 18:36:41 -0700158 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700159 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700160
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800161 property_get("debug.sf.showupdates", value, "0");
162 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700163
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700164 property_get("debug.sf.ddms", value, "0");
165 mDebugDDMS = atoi(value);
166 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700167 if (!startDdmConnection()) {
168 // start failed, and DDMS debugging not enabled
169 mDebugDDMS = 0;
170 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700171 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700172 ALOGI_IF(mDebugRegion, "showupdates enabled");
173 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174}
175
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800176void SurfaceFlinger::onFirstRef()
177{
178 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800179}
180
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181SurfaceFlinger::~SurfaceFlinger()
182{
Mathias Agopiana4912602012-07-12 14:25:33 -0700183 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
184 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
185 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186}
187
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800188void SurfaceFlinger::binderDied(const wp<IBinder>& who)
189{
190 // the window manager died on us. prepare its eulogy.
191
Andy McFadden13a082e2012-08-24 10:16:42 -0700192 // restore initial conditions (default device unblank, etc)
193 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800194
195 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700196 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800197}
198
Mathias Agopian7e27f052010-05-28 14:22:23 -0700199sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200{
Mathias Agopian96f08192010-06-02 23:28:45 -0700201 sp<ISurfaceComposerClient> bclient;
202 sp<Client> client(new Client(this));
203 status_t err = client->initCheck();
204 if (err == NO_ERROR) {
205 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207 return bclient;
208}
209
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700210sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
211 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700212{
213 class DisplayToken : public BBinder {
214 sp<SurfaceFlinger> flinger;
215 virtual ~DisplayToken() {
216 // no more references, this display must be terminated
217 Mutex::Autolock _l(flinger->mStateLock);
218 flinger->mCurrentState.displays.removeItem(this);
219 flinger->setTransactionFlags(eDisplayTransactionNeeded);
220 }
221 public:
222 DisplayToken(const sp<SurfaceFlinger>& flinger)
223 : flinger(flinger) {
224 }
225 };
226
227 sp<BBinder> token = new DisplayToken(this);
228
229 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700230 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700231 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700232 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700233 mCurrentState.displays.add(token, info);
234
235 return token;
236}
237
Jesse Hall6c913be2013-08-08 12:15:49 -0700238void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
239 Mutex::Autolock _l(mStateLock);
240
241 ssize_t idx = mCurrentState.displays.indexOfKey(display);
242 if (idx < 0) {
243 ALOGW("destroyDisplay: invalid display token");
244 return;
245 }
246
247 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
248 if (!info.isVirtualDisplay()) {
249 ALOGE("destroyDisplay called for non-virtual display");
250 return;
251 }
252
253 mCurrentState.displays.removeItemsAt(idx);
254 setTransactionFlags(eDisplayTransactionNeeded);
255}
256
Jesse Hall692c7232012-11-08 15:41:56 -0800257void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
258 ALOGW_IF(mBuiltinDisplays[type],
259 "Overwriting display token for display type %d", type);
260 mBuiltinDisplays[type] = new BBinder();
261 DisplayDeviceState info(type);
262 // All non-virtual displays are currently considered secure.
263 info.isSecure = true;
264 mCurrentState.displays.add(mBuiltinDisplays[type], info);
265}
266
Mathias Agopiane57f2922012-08-09 16:29:12 -0700267sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700268 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700269 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
270 return NULL;
271 }
Jesse Hall692c7232012-11-08 15:41:56 -0800272 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700273}
274
Jamie Gennis9a78c902011-01-12 18:30:40 -0800275sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
276{
277 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
278 return gba;
279}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700280
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800281void SurfaceFlinger::bootFinished()
282{
283 const nsecs_t now = systemTime();
284 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000285 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700286 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700287
288 // wait patiently for the window manager death
289 const String16 name("window");
290 sp<IBinder> window(defaultServiceManager()->getService(name));
291 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700292 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700293 }
294
295 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700296 // formerly we would just kill the process, but we now ask it to exit so it
297 // can choose where to stop the animation.
298 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800299}
300
Mathias Agopian3f844832013-08-07 21:24:32 -0700301void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700302 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700303 RenderEngine& engine;
304 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700305 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700306 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
307 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700308 }
309 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700310 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700311 return true;
312 }
313 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700314 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700315}
316
Mathias Agopian722b98f2012-09-25 18:24:31 -0700317status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700318 EGLDisplay dpy,
319 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700320 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700321 EGLConfig* outConfig)
322{
323 EGLConfig config = NULL;
324 EGLint numConfigs = -1, n=0;
325 eglGetConfigs(dpy, NULL, 0, &numConfigs);
326 EGLConfig* const configs = new EGLConfig[numConfigs];
327 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700328
Mathias Agopian722b98f2012-09-25 18:24:31 -0700329 if (n) {
330 if (attribute != EGL_NONE) {
331 for (int i=0 ; i<n ; i++) {
332 EGLint value = 0;
333 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
334 if (wanted == value) {
335 *outConfig = configs[i];
336 delete [] configs;
337 return NO_ERROR;
338 }
339 }
340 } else {
341 // just pick the first one
342 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700343 delete [] configs;
344 return NO_ERROR;
345 }
346 }
347 delete [] configs;
348 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800349}
350
Mathias Agopian722b98f2012-09-25 18:24:31 -0700351class EGLAttributeVector {
352 struct Attribute;
353 class Adder;
354 friend class Adder;
355 KeyedVector<Attribute, EGLint> mList;
356 struct Attribute {
357 Attribute() {};
358 Attribute(EGLint v) : v(v) { }
359 EGLint v;
360 bool operator < (const Attribute& other) const {
361 // this places EGL_NONE at the end
362 EGLint lhs(v);
363 EGLint rhs(other.v);
364 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
365 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
366 return lhs < rhs;
367 }
368 };
369 class Adder {
370 friend class EGLAttributeVector;
371 EGLAttributeVector& v;
372 EGLint attribute;
373 Adder(EGLAttributeVector& v, EGLint attribute)
374 : v(v), attribute(attribute) {
375 }
376 public:
377 void operator = (EGLint value) {
378 if (attribute != EGL_NONE) {
379 v.mList.add(attribute, value);
380 }
381 }
382 operator EGLint () const { return v.mList[attribute]; }
383 };
384public:
385 EGLAttributeVector() {
386 mList.add(EGL_NONE, EGL_NONE);
387 }
388 void remove(EGLint attribute) {
389 if (attribute != EGL_NONE) {
390 mList.removeItem(attribute);
391 }
392 }
393 Adder operator [] (EGLint attribute) {
394 return Adder(*this, attribute);
395 }
396 EGLint operator [] (EGLint attribute) const {
397 return mList[attribute];
398 }
399 // cast-operator to (EGLint const*)
400 operator EGLint const* () const { return &mList.keyAt(0).v; }
401};
402
Jesse Hallb65f32e2013-09-27 22:10:47 -0700403status_t SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId,
404 EGLint renderableType, EGLConfig* config) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700405 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
406 // it is to be used with WIFI displays
Mathias Agopiana4912602012-07-12 14:25:33 -0700407 status_t err;
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700408 EGLint wantedAttribute;
409 EGLint wantedAttributeValue;
Mathias Agopianda27af92012-09-13 18:17:13 -0700410
Mathias Agopian722b98f2012-09-25 18:24:31 -0700411 EGLAttributeVector attribs;
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700412 if (renderableType) {
413 attribs[EGL_RENDERABLE_TYPE] = renderableType;
414 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
415 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT|EGL_PBUFFER_BIT;
416 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
417 attribs[EGL_RED_SIZE] = 8;
418 attribs[EGL_GREEN_SIZE] = 8;
419 attribs[EGL_BLUE_SIZE] = 8;
420 wantedAttribute = EGL_NONE;
421 wantedAttributeValue = EGL_NONE;
Mathias Agopian722b98f2012-09-25 18:24:31 -0700422
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700423 } else {
424 // if no renderable type specified, fallback to a simplified query
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700425 wantedAttribute = EGL_NATIVE_VISUAL_ID;
426 wantedAttributeValue = nativeVisualId;
427 }
428
429 err = selectConfigForAttribute(display, attribs, wantedAttribute,
Jesse Hallb65f32e2013-09-27 22:10:47 -0700430 wantedAttributeValue, config);
431 if (err == NO_ERROR) {
432 EGLint caveat;
433 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
434 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
435 }
436 return err;
Mathias Agopiana4912602012-07-12 14:25:33 -0700437}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800438
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700439class DispSyncSource : public VSyncSource, private DispSync::Callback {
440public:
441 DispSyncSource(DispSync* dispSync) : mValue(0), mDispSync(dispSync) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700442
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700443 virtual ~DispSyncSource() {}
444
445 virtual void setVSyncEnabled(bool enable) {
446 // Do NOT lock the mutex here so as to avoid any mutex ordering issues
447 // with locking it in the onDispSyncEvent callback.
448 if (enable) {
449 status_t err = mDispSync->addEventListener(vsyncPhaseOffsetNs,
450 static_cast<DispSync::Callback*>(this));
451 if (err != NO_ERROR) {
452 ALOGE("error registering vsync callback: %s (%d)",
453 strerror(-err), err);
454 }
455 ATRACE_INT("VsyncOn", 1);
456 } else {
457 status_t err = mDispSync->removeEventListener(
458 static_cast<DispSync::Callback*>(this));
459 if (err != NO_ERROR) {
460 ALOGE("error unregistering vsync callback: %s (%d)",
461 strerror(-err), err);
462 }
463 ATRACE_INT("VsyncOn", 0);
464 }
465 }
466
467 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
468 Mutex::Autolock lock(mMutex);
469 mCallback = callback;
470 }
471
472private:
473 virtual void onDispSyncEvent(nsecs_t when) {
474 sp<VSyncSource::Callback> callback;
475 {
476 Mutex::Autolock lock(mMutex);
477 callback = mCallback;
478
479 mValue = (mValue + 1) % 2;
480 ATRACE_INT("VSYNC", mValue);
481 }
482
483 if (callback != NULL) {
484 callback->onVSyncEvent(when);
485 }
486 }
487
488 int mValue;
489
490 DispSync* mDispSync;
491 sp<VSyncSource::Callback> mCallback;
492 Mutex mMutex;
493};
494
495void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700496 ALOGI( "SurfaceFlinger's main thread ready to run. "
497 "Initializing graphics H/W...");
498
Jesse Hallb65f32e2013-09-27 22:10:47 -0700499 status_t err;
Jesse Hall692c7232012-11-08 15:41:56 -0800500 Mutex::Autolock _l(mStateLock);
501
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700502 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700503 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
504 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700505
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700506 // Initialize the H/W composer object. There may or may not be an
507 // actual hardware composer underneath.
508 mHwc = new HWComposer(this,
509 *static_cast<HWComposer::EventHandler *>(this));
510
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700511 // First try to get an ES2 config
Jesse Hallb65f32e2013-09-27 22:10:47 -0700512 err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(), EGL_OPENGL_ES2_BIT,
513 &mEGLConfig);
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700514
Jesse Hallb65f32e2013-09-27 22:10:47 -0700515 if (err != NO_ERROR) {
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700516 // If ES2 fails, try ES1
Jesse Hallb65f32e2013-09-27 22:10:47 -0700517 err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(),
518 EGL_OPENGL_ES_BIT, &mEGLConfig);
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700519 }
520
Jesse Hallb65f32e2013-09-27 22:10:47 -0700521 if (err != NO_ERROR) {
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700522 // still didn't work, probably because we're on the emulator...
523 // try a simplified query
524 ALOGW("no suitable EGLConfig found, trying a simpler query");
Jesse Hallb65f32e2013-09-27 22:10:47 -0700525 err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(), 0, &mEGLConfig);
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700526 }
527
Jesse Hallb65f32e2013-09-27 22:10:47 -0700528 if (err != NO_ERROR) {
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700529 // this EGL is too lame for android
530 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
531 }
Mathias Agopian875d8e12013-06-07 15:35:48 -0700532
533 // print some debugging info
534 EGLint r,g,b,a;
535 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_RED_SIZE, &r);
536 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_GREEN_SIZE, &g);
537 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_BLUE_SIZE, &b);
538 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_ALPHA_SIZE, &a);
539 ALOGI("EGL informations:");
540 ALOGI("vendor : %s", eglQueryString(mEGLDisplay, EGL_VENDOR));
541 ALOGI("version : %s", eglQueryString(mEGLDisplay, EGL_VERSION));
542 ALOGI("extensions: %s", eglQueryString(mEGLDisplay, EGL_EXTENSIONS));
543 ALOGI("Client API: %s", eglQueryString(mEGLDisplay, EGL_CLIENT_APIS)?:"Not Supported");
544 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
545
546 // get a RenderEngine for the given display / config (can't fail)
547 mRenderEngine = RenderEngine::create(mEGLDisplay, mEGLConfig);
548
549 // retrieve the EGL context that was selected/created
550 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700551
Mathias Agopianed985572013-03-22 00:24:39 -0700552 // figure out which format we got
553 eglGetConfigAttrib(mEGLDisplay, mEGLConfig,
554 EGL_NATIVE_VISUAL_ID, &mEGLNativeVisualId);
555
Mathias Agopianda27af92012-09-13 18:17:13 -0700556 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
557 "couldn't create EGLContext");
558
Mathias Agopiancde87a32012-09-13 14:09:01 -0700559 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700560 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700561 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700562 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700563 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700564 // All non-virtual displays are currently considered secure.
565 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800566 createBuiltinDisplayLocked(type);
567 wp<IBinder> token = mBuiltinDisplays[i];
568
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700569 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
570 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i, bq);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700571 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700572 type, allocateHwcDisplayId(type), isSecure, token,
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700573 fbs, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700574 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700575 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700576 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700577 // for displays other than the main display, so we always
578 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700579 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700580 hw->acquireScreen();
581 }
582 mDisplays.add(token, hw);
583 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700584 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700585
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700586 // make the GLContext current so that we can create textures when creating Layers
587 // (which may happens before we render something)
588 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
589
Mathias Agopian028508c2012-07-25 21:12:12 -0700590 // start the EventThread
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700591 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync);
592 mEventThread = new EventThread(vsyncSrc);
Mathias Agopian028508c2012-07-25 21:12:12 -0700593 mEventQueue.setEventThread(mEventThread);
594
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700595 // set a fake vsync period if there is no HWComposer
596 if (mHwc->initCheck() != NO_ERROR) {
597 mPrimaryDispSync.setPeriod(16666667);
598 }
599
Mathias Agopian92a979a2012-08-02 18:32:23 -0700600 // initialize our drawing state
601 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700602
Andy McFadden13a082e2012-08-24 10:16:42 -0700603 // set initial conditions (e.g. unblank default device)
604 initializeDisplays();
605
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700606 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700607 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800608}
609
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700610int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700611 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700612 type : mHwc->allocateDisplayId();
613}
614
Mathias Agopiana67e4182012-06-19 17:26:12 -0700615void SurfaceFlinger::startBootAnim() {
616 // start boot animation
617 property_set("service.bootanim.exit", "0");
618 property_set("ctl.start", "bootanim");
619}
620
Mathias Agopian875d8e12013-06-07 15:35:48 -0700621size_t SurfaceFlinger::getMaxTextureSize() const {
622 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700623}
624
Mathias Agopian875d8e12013-06-07 15:35:48 -0700625size_t SurfaceFlinger::getMaxViewportDims() const {
626 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700627}
628
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800630
Jamie Gennis582270d2011-08-17 18:19:00 -0700631bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800632 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800633 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800634 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700635 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800636}
637
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700638status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800639 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700640 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800641 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700642 type = i;
643 break;
644 }
645 }
646
647 if (type < 0) {
648 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700649 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700650
651 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700652 float xdpi = hwc.getDpiX(type);
653 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700654
655 // TODO: Not sure if display density should handled by SF any longer
656 class Density {
657 static int getDensityFromProperty(char const* propName) {
658 char property[PROPERTY_VALUE_MAX];
659 int density = 0;
660 if (property_get(propName, property, NULL) > 0) {
661 density = atoi(property);
662 }
663 return density;
664 }
665 public:
666 static int getEmuDensity() {
667 return getDensityFromProperty("qemu.sf.lcd_density"); }
668 static int getBuildDensity() {
669 return getDensityFromProperty("ro.sf.lcd_density"); }
670 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700671
672 if (type == DisplayDevice::DISPLAY_PRIMARY) {
673 // The density of the device is provided by a build property
674 float density = Density::getBuildDensity() / 160.0f;
675 if (density == 0) {
676 // the build doesn't provide a density -- this is wrong!
677 // use xdpi instead
678 ALOGE("ro.sf.lcd_density must be defined as a build property");
679 density = xdpi / 160.0f;
680 }
681 if (Density::getEmuDensity()) {
682 // if "qemu.sf.lcd_density" is specified, it overrides everything
683 xdpi = ydpi = density = Density::getEmuDensity();
684 density /= 160.0f;
685 }
686 info->density = density;
687
688 // TODO: this needs to go away (currently needed only by webkit)
689 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
690 info->orientation = hw->getOrientation();
Mathias Agopian1604f772012-09-18 21:54:42 -0700691 } else {
692 // TODO: where should this value come from?
693 static const int TV_DENSITY = 213;
694 info->density = TV_DENSITY / 160.0f;
695 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700696 }
697
Mathias Agopian1604f772012-09-18 21:54:42 -0700698 info->w = hwc.getWidth(type);
699 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700700 info->xdpi = xdpi;
701 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700702 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700703
704 // All non-virtual displays are currently considered secure.
705 info->secure = true;
706
Mathias Agopian888c8222012-08-04 21:10:38 -0700707 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700708}
709
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800710// ----------------------------------------------------------------------------
711
712sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800713 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700714}
715
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800716// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800717
718void SurfaceFlinger::waitForEvent() {
719 mEventQueue.waitMessage();
720}
721
722void SurfaceFlinger::signalTransaction() {
723 mEventQueue.invalidate();
724}
725
726void SurfaceFlinger::signalLayerUpdate() {
727 mEventQueue.invalidate();
728}
729
730void SurfaceFlinger::signalRefresh() {
731 mEventQueue.refresh();
732}
733
734status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
735 nsecs_t reltime, uint32_t flags) {
736 return mEventQueue.postMessage(msg, reltime);
737}
738
739status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
740 nsecs_t reltime, uint32_t flags) {
741 status_t res = mEventQueue.postMessage(msg, reltime);
742 if (res == NO_ERROR) {
743 msg->wait();
744 }
745 return res;
746}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800747
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700748void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700749 do {
750 waitForEvent();
751 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800752}
753
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700754void SurfaceFlinger::enableHardwareVsync() {
755 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700756 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700757 mPrimaryDispSync.beginResync();
758 eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
759 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700760 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700761}
762
Jesse Hall948fe0c2013-10-14 12:56:09 -0700763void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700764 Mutex::Autolock _l(mHWVsyncLock);
765
Jesse Hall948fe0c2013-10-14 12:56:09 -0700766 if (makeAvailable) {
767 mHWVsyncAvailable = true;
768 } else if (!mHWVsyncAvailable) {
769 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
770 return;
771 }
772
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700773 const nsecs_t period =
774 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
775
776 mPrimaryDispSync.reset();
777 mPrimaryDispSync.setPeriod(period);
778
779 if (!mPrimaryHWVsyncEnabled) {
780 mPrimaryDispSync.beginResync();
781 eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
782 mPrimaryHWVsyncEnabled = true;
783 }
784}
785
Jesse Hall948fe0c2013-10-14 12:56:09 -0700786void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700787 Mutex::Autolock _l(mHWVsyncLock);
788 if (mPrimaryHWVsyncEnabled) {
789 eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
790 mPrimaryDispSync.endResync();
791 mPrimaryHWVsyncEnabled = false;
792 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700793 if (makeUnavailable) {
794 mHWVsyncAvailable = false;
795 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700796}
797
798void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
799 if (type == 0) {
800 bool needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
801
802 if (needsHwVsync) {
803 enableHardwareVsync();
804 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700805 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700806 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700807 }
808}
809
810void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
811 if (mEventThread == NULL) {
812 // This is a temporary workaround for b/7145521. A non-null pointer
813 // does not mean EventThread has finished initializing, so this
814 // is not a correct fix.
815 ALOGW("WARNING: EventThread not started, ignoring hotplug");
816 return;
817 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700818
Jesse Hall9e663de2013-08-16 14:28:37 -0700819 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700820 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800821 if (connected) {
822 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700823 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800824 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
825 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700826 }
827 setTransactionFlags(eDisplayTransactionNeeded);
828
Andy McFadden9e9689c2012-10-10 18:17:51 -0700829 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700830 }
Mathias Agopian86303202012-07-24 22:46:10 -0700831}
832
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700833void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700834 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700835 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700836}
837
Mathias Agopian4fec8732012-06-29 14:12:52 -0700838void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800839 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800840 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700841 case MessageQueue::TRANSACTION:
842 handleMessageTransaction();
843 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700844 case MessageQueue::INVALIDATE:
845 handleMessageTransaction();
846 handleMessageInvalidate();
847 signalRefresh();
848 break;
849 case MessageQueue::REFRESH:
850 handleMessageRefresh();
851 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800852 }
853}
854
Mathias Agopian4fec8732012-06-29 14:12:52 -0700855void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700856 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700857 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700858 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700859 }
860}
861
862void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700863 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700864 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700865}
866
867void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700868 ATRACE_CALL();
869 preComposition();
870 rebuildLayerStacks();
871 setUpHWComposer();
872 doDebugFlashRegions();
873 doComposition();
874 postComposition();
875}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700876
Mathias Agopiancd60f992012-08-16 16:28:27 -0700877void SurfaceFlinger::doDebugFlashRegions()
878{
879 // is debugging enabled
880 if (CC_LIKELY(!mDebugRegion))
881 return;
882
883 const bool repaintEverything = mRepaintEverything;
884 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
885 const sp<DisplayDevice>& hw(mDisplays[dpy]);
886 if (hw->canDraw()) {
887 // transform the dirty region into this screen's coordinate space
888 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
889 if (!dirtyRegion.isEmpty()) {
890 // redraw the whole screen
891 doComposeSurfaces(hw, Region(hw->bounds()));
892
893 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700894 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700895 RenderEngine& engine(getRenderEngine());
896 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
897
Mathias Agopiancd60f992012-08-16 16:28:27 -0700898 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700899 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700900 }
901 }
902 }
903
904 postFramebuffer();
905
906 if (mDebugRegion > 1) {
907 usleep(mDebugRegion * 1000);
908 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700909
910 HWComposer& hwc(getHwComposer());
911 if (hwc.initCheck() == NO_ERROR) {
912 status_t err = hwc.prepare();
913 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
914 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700915}
916
917void SurfaceFlinger::preComposition()
918{
919 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700920 const LayerVector& layers(mDrawingState.layersSortedByZ);
921 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700922 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700923 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700924 needExtraInvalidate = true;
925 }
926 }
927 if (needExtraInvalidate) {
928 signalLayerUpdate();
929 }
930}
931
932void SurfaceFlinger::postComposition()
933{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700934 const LayerVector& layers(mDrawingState.layersSortedByZ);
935 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700936 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700937 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700938 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800939
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700940 const HWComposer& hwc = getHwComposer();
941 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
942
943 if (presentFence->isValid()) {
944 if (mPrimaryDispSync.addPresentFence(presentFence)) {
945 enableHardwareVsync();
946 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700947 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700948 }
949 }
950
951 if (runningWithoutSyncFramework) {
952 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
953 if (hw->isScreenAcquired()) {
954 enableHardwareVsync();
955 }
956 }
957
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800958 if (mAnimCompositionPending) {
959 mAnimCompositionPending = false;
960
Jesse Halla9a1b002013-02-27 16:39:25 -0800961 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800962 mAnimFrameTracker.setActualPresentFence(presentFence);
963 } else {
964 // The HWC doesn't support present fences, so use the refresh
965 // timestamp instead.
966 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
967 mAnimFrameTracker.setActualPresentTime(presentTime);
968 }
969 mAnimFrameTracker.advanceFrame();
970 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700971}
972
973void SurfaceFlinger::rebuildLayerStacks() {
974 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700975 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700976 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700977 mVisibleRegionsDirty = false;
978 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700979
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700980 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700981 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700982 Region opaqueRegion;
983 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800984 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700985 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700986 const Transform& tr(hw->getTransform());
987 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700988 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700989 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700990 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700991
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700992 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700993 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700994 const sp<Layer>& layer(layers[i]);
995 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700996 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700997 Region drawRegion(tr.transform(
998 layer->visibleNonTransparentRegion));
999 drawRegion.andSelf(bounds);
1000 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001001 layersSortedByZ.add(layer);
1002 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001003 }
1004 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001005 }
Mathias Agopian42977342012-08-05 00:40:46 -07001006 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001007 hw->undefinedRegion.set(bounds);
1008 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
1009 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001010 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001011 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001012}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001013
Mathias Agopiancd60f992012-08-16 16:28:27 -07001014void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001015 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1016 mDisplays[dpy]->beginFrame();
1017 }
1018
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001019 HWComposer& hwc(getHwComposer());
1020 if (hwc.initCheck() == NO_ERROR) {
1021 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001022 if (CC_UNLIKELY(mHwWorkListDirty)) {
1023 mHwWorkListDirty = false;
1024 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1025 sp<const DisplayDevice> hw(mDisplays[dpy]);
1026 const int32_t id = hw->getHwcDisplayId();
1027 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001028 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001029 hw->getVisibleLayersSortedByZ());
1030 const size_t count = currentLayers.size();
1031 if (hwc.createWorkList(id, count) == NO_ERROR) {
1032 HWComposer::LayerListIterator cur = hwc.begin(id);
1033 const HWComposer::LayerListIterator end = hwc.end(id);
1034 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001035 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001036 layer->setGeometry(hw, *cur);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001037 if (mDebugDisableHWC || mDebugRegion || mDaltonize) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001038 cur->setSkip(true);
1039 }
1040 }
1041 }
1042 }
1043 }
1044 }
1045
1046 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001047 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001048 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001049 const int32_t id = hw->getHwcDisplayId();
1050 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001051 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001052 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001053 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001054 HWComposer::LayerListIterator cur = hwc.begin(id);
1055 const HWComposer::LayerListIterator end = hwc.end(id);
1056 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1057 /*
1058 * update the per-frame h/w composer data for each layer
1059 * and build the transparent region of the FB
1060 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001061 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001062 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -07001063 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001064 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001065 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001066
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001067 status_t err = hwc.prepare();
1068 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001069
1070 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1071 sp<const DisplayDevice> hw(mDisplays[dpy]);
1072 hw->prepareFrame(hwc);
1073 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001074 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001075}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001076
Mathias Agopiancd60f992012-08-16 16:28:27 -07001077void SurfaceFlinger::doComposition() {
1078 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001079 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001080 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001081 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -07001082 if (hw->canDraw()) {
1083 // transform the dirty region into this screen's coordinate space
1084 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001085
1086 // repaint the framebuffer (if needed)
1087 doDisplayComposition(hw, dirtyRegion);
1088
Mathias Agopiancd60f992012-08-16 16:28:27 -07001089 hw->dirtyRegion.clear();
1090 hw->flip(hw->swapRegion);
1091 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001092 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001093 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001094 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001095 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001096 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001097}
1098
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001099void SurfaceFlinger::postFramebuffer()
1100{
Mathias Agopian841cde52012-03-01 15:44:37 -08001101 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001102
Mathias Agopiana44b0412011-10-16 18:46:35 -07001103 const nsecs_t now = systemTime();
1104 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001105
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001106 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001107 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001108 if (!hwc.supportsFramebufferTarget()) {
1109 // EGL spec says:
1110 // "surface must be bound to the calling thread's current context,
1111 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001112 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001113 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001114 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001115 }
1116
Mathias Agopian6da15f42013-09-25 20:40:07 -07001117 // make the default display current because the VirtualDisplayDevice code cannot
1118 // deal with dequeueBuffer() being called outside of the composition loop; however
1119 // the code below can call glFlush() which is allowed (and does in some case) call
1120 // dequeueBuffer().
1121 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1122
Mathias Agopian92a979a2012-08-02 18:32:23 -07001123 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001124 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001125 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001126 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001127 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001128 int32_t id = hw->getHwcDisplayId();
1129 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001130 HWComposer::LayerListIterator cur = hwc.begin(id);
1131 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001132 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001133 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001134 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001135 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001136 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001137 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001138 }
Jesse Hallef194142012-06-14 14:45:17 -07001139 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001140 }
1141
Mathias Agopiana44b0412011-10-16 18:46:35 -07001142 mLastSwapBufferTime = systemTime() - now;
1143 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001144
1145 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1146 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1147 logFrameStats();
1148 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001149}
1150
Mathias Agopian87baae12012-07-31 12:38:26 -07001151void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001152{
Mathias Agopian841cde52012-03-01 15:44:37 -08001153 ATRACE_CALL();
1154
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001155 // here we keep a copy of the drawing state (that is the state that's
1156 // going to be overwritten by handleTransactionLocked()) outside of
1157 // mStateLock so that the side-effects of the State assignment
1158 // don't happen with mStateLock held (which can cause deadlocks).
1159 State drawingState(mDrawingState);
1160
Mathias Agopianca4d3602011-05-19 15:38:14 -07001161 Mutex::Autolock _l(mStateLock);
1162 const nsecs_t now = systemTime();
1163 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001164
Mathias Agopianca4d3602011-05-19 15:38:14 -07001165 // Here we're guaranteed that some transaction flags are set
1166 // so we can call handleTransactionLocked() unconditionally.
1167 // We call getTransactionFlags(), which will also clear the flags,
1168 // with mStateLock held to guarantee that mCurrentState won't change
1169 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001170
Mathias Agopiane57f2922012-08-09 16:29:12 -07001171 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001172 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001173
Mathias Agopianca4d3602011-05-19 15:38:14 -07001174 mLastTransactionTime = systemTime() - now;
1175 mDebugInTransaction = 0;
1176 invalidateHwcGeometry();
1177 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001178}
1179
Mathias Agopian87baae12012-07-31 12:38:26 -07001180void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001181{
1182 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001183 const size_t count = currentLayers.size();
1184
1185 /*
1186 * Traversal of the children
1187 * (perform the transaction for each of them if needed)
1188 */
1189
Mathias Agopian3559b072012-08-15 13:46:03 -07001190 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001191 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001192 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1194 if (!trFlags) continue;
1195
1196 const uint32_t flags = layer->doTransaction(0);
1197 if (flags & Layer::eVisibleRegion)
1198 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001199 }
1200 }
1201
1202 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001203 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001204 */
1205
Mathias Agopiane57f2922012-08-09 16:29:12 -07001206 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001207 // here we take advantage of Vector's copy-on-write semantics to
1208 // improve performance by skipping the transaction entirely when
1209 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001210 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1211 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001212 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001213 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001214 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001215 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001216
1217 // find the displays that were removed
1218 // (ie: in drawing state but not in current state)
1219 // also handle displays that changed
1220 // (ie: displays that are in both lists)
1221 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001222 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1223 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001224 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001225 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001226 // Call makeCurrent() on the primary display so we can
1227 // be sure that nothing associated with this display
1228 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001229 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001230 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001231 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1232 if (hw != NULL)
1233 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001234 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001235 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001236 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001237 } else {
1238 ALOGW("trying to remove the main display");
1239 }
1240 } else {
1241 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001242 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001243 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001244 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001245 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001246 // recreating the DisplayDevice, so we just remove it
1247 // from the drawing state, so that it get re-added
1248 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001249 sp<DisplayDevice> hw(getDisplayDevice(display));
1250 if (hw != NULL)
1251 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001252 mDisplays.removeItem(display);
1253 mDrawingState.displays.removeItemsAt(i);
1254 dc--; i--;
1255 // at this point we must loop to the next item
1256 continue;
1257 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001258
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001259 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001260 if (disp != NULL) {
1261 if (state.layerStack != draw[i].layerStack) {
1262 disp->setLayerStack(state.layerStack);
1263 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001264 if ((state.orientation != draw[i].orientation)
1265 || (state.viewport != draw[i].viewport)
1266 || (state.frame != draw[i].frame))
1267 {
1268 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001269 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001270 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001271 }
1272 }
1273 }
1274
1275 // find displays that were added
1276 // (ie: in current state but not in drawing state)
1277 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001278 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001279 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001280
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001281 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001282 sp<IGraphicBufferProducer> producer;
1283 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
1284
Jesse Hall02d86562013-03-25 14:43:23 -07001285 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001286 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001287 // Virtual displays without a surface are dormant:
1288 // they have external state (layer stack, projection,
1289 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001290 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001291
Jesse Hall02d86562013-03-25 14:43:23 -07001292 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001293 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1294 *mHwc, hwcDisplayId, state.surface, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001295 state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001296
1297 dispSurface = vds;
1298 if (hwcDisplayId >= 0) {
1299 producer = vds;
1300 } else {
1301 // There won't be any interaction with HWC for this virtual display,
1302 // so the GLES driver can pass buffers directly to the sink.
1303 producer = state.surface;
1304 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001305 }
1306 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001307 ALOGE_IF(state.surface!=NULL,
1308 "adding a supported display, but rendering "
1309 "surface is provided (%p), ignoring it",
1310 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001311 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001312 // for supported (by hwc) displays we provide our
1313 // own rendering surface
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001314 dispSurface = new FramebufferSurface(*mHwc, state.type, bq);
1315 producer = bq;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001316 }
1317
1318 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001319 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001320 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001321 state.type, hwcDisplayId, state.isSecure,
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001322 display, dispSurface, producer, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001323 hw->setLayerStack(state.layerStack);
1324 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001325 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001326 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001327 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001328 if (state.isVirtualDisplay()) {
1329 if (hwcDisplayId >= 0) {
1330 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1331 hw->getWidth(), hw->getHeight(),
1332 hw->getFormat());
1333 }
1334 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001335 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001336 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001337 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001338 }
1339 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001340 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001341 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001342
Mathias Agopian84300952012-11-21 16:02:13 -08001343 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1344 // The transform hint might have changed for some layers
1345 // (either because a display has changed, or because a layer
1346 // as changed).
1347 //
1348 // Walk through all the layers in currentLayers,
1349 // and update their transform hint.
1350 //
1351 // If a layer is visible only on a single display, then that
1352 // display is used to calculate the hint, otherwise we use the
1353 // default display.
1354 //
1355 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1356 // the hint is set before we acquire a buffer from the surface texture.
1357 //
1358 // NOTE: layer transactions have taken place already, so we use their
1359 // drawing state. However, SurfaceFlinger's own transaction has not
1360 // happened yet, so we must use the current state layer list
1361 // (soon to become the drawing state list).
1362 //
1363 sp<const DisplayDevice> disp;
1364 uint32_t currentlayerStack = 0;
1365 for (size_t i=0; i<count; i++) {
1366 // NOTE: we rely on the fact that layers are sorted by
1367 // layerStack first (so we don't have to traverse the list
1368 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001369 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001370 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001371 if (i==0 || currentlayerStack != layerStack) {
1372 currentlayerStack = layerStack;
1373 // figure out if this layerstack is mirrored
1374 // (more than one display) if so, pick the default display,
1375 // if not, pick the only display it's on.
1376 disp.clear();
1377 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1378 sp<const DisplayDevice> hw(mDisplays[dpy]);
1379 if (hw->getLayerStack() == currentlayerStack) {
1380 if (disp == NULL) {
1381 disp = hw;
1382 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001383 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001384 break;
1385 }
1386 }
1387 }
1388 }
Chet Haase91d25932013-04-11 15:24:55 -07001389 if (disp == NULL) {
1390 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1391 // redraw after transform hint changes. See bug 8508397.
1392
1393 // could be null when this layer is using a layerStack
1394 // that is not visible on any display. Also can occur at
1395 // screen off/on times.
1396 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001397 }
Chet Haase91d25932013-04-11 15:24:55 -07001398 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001399 }
1400 }
1401
1402
Mathias Agopian3559b072012-08-15 13:46:03 -07001403 /*
1404 * Perform our own transaction if needed
1405 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001406
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001407 const LayerVector& layers(mDrawingState.layersSortedByZ);
1408 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001409 // layers have been added
1410 mVisibleRegionsDirty = true;
1411 }
1412
1413 // some layers might have been removed, so
1414 // we need to update the regions they're exposing.
1415 if (mLayersRemoved) {
1416 mLayersRemoved = false;
1417 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001418 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001419 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001420 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001421 if (currentLayers.indexOf(layer) < 0) {
1422 // this layer is not visible anymore
1423 // TODO: we could traverse the tree from front to back and
1424 // compute the actual visible region
1425 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001426 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001427 Region visibleReg = s.transform.transform(
1428 Region(Rect(s.active.w, s.active.h)));
1429 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001430 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001431 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001432 }
1433
1434 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001435}
1436
1437void SurfaceFlinger::commitTransaction()
1438{
1439 if (!mLayersPendingRemoval.isEmpty()) {
1440 // Notify removed layers now that they can't be drawn from
1441 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1442 mLayersPendingRemoval[i]->onRemoved();
1443 }
1444 mLayersPendingRemoval.clear();
1445 }
1446
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001447 // If this transaction is part of a window animation then the next frame
1448 // we composite should be considered an animation as well.
1449 mAnimCompositionPending = mAnimTransactionPending;
1450
Mathias Agopian4fec8732012-06-29 14:12:52 -07001451 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001452 mTransactionPending = false;
1453 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001454 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001455}
1456
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001457void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001458 const LayerVector& currentLayers, uint32_t layerStack,
1459 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001460{
Mathias Agopian841cde52012-03-01 15:44:37 -08001461 ATRACE_CALL();
1462
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001463 Region aboveOpaqueLayers;
1464 Region aboveCoveredLayers;
1465 Region dirty;
1466
Mathias Agopian87baae12012-07-31 12:38:26 -07001467 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001468
1469 size_t i = currentLayers.size();
1470 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001471 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001472
1473 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001474 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001475
Jesse Hall01e29052013-02-19 16:13:35 -08001476 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001477 if (s.layerStack != layerStack)
1478 continue;
1479
Mathias Agopianab028732010-03-16 16:41:46 -07001480 /*
1481 * opaqueRegion: area of a surface that is fully opaque.
1482 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001484
1485 /*
1486 * visibleRegion: area of a surface that is visible on screen
1487 * and not fully transparent. This is essentially the layer's
1488 * footprint minus the opaque regions above it.
1489 * Areas covered by a translucent surface are considered visible.
1490 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001491 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001492
1493 /*
1494 * coveredRegion: area of a surface that is covered by all
1495 * visible regions above it (which includes the translucent areas).
1496 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001497 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001498
Jesse Halla8026d22012-09-25 13:25:04 -07001499 /*
1500 * transparentRegion: area of a surface that is hinted to be completely
1501 * transparent. This is only used to tell when the layer has no visible
1502 * non-transparent regions and can be removed from the layer list. It
1503 * does not affect the visibleRegion of this layer or any layers
1504 * beneath it. The hint may not be correct if apps don't respect the
1505 * SurfaceView restrictions (which, sadly, some don't).
1506 */
1507 Region transparentRegion;
1508
Mathias Agopianab028732010-03-16 16:41:46 -07001509
1510 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001511 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001512 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001513 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001514 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001515 if (!visibleRegion.isEmpty()) {
1516 // Remove the transparent area from the visible region
1517 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001518 const Transform tr(s.transform);
1519 if (tr.transformed()) {
1520 if (tr.preserveRects()) {
1521 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001522 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001523 } else {
1524 // transformation too complex, can't do the
1525 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001526 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001527 }
1528 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001529 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001530 }
Mathias Agopianab028732010-03-16 16:41:46 -07001531 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001532
Mathias Agopianab028732010-03-16 16:41:46 -07001533 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001534 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001535 if (s.alpha==255 && !translucent &&
1536 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1537 // the opaque region is the layer's footprint
1538 opaqueRegion = visibleRegion;
1539 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001540 }
1541 }
1542
Mathias Agopianab028732010-03-16 16:41:46 -07001543 // Clip the covered region to the visible region
1544 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1545
1546 // Update aboveCoveredLayers for next (lower) layer
1547 aboveCoveredLayers.orSelf(visibleRegion);
1548
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001549 // subtract the opaque region covered by the layers above us
1550 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001551
1552 // compute this layer's dirty region
1553 if (layer->contentDirty) {
1554 // we need to invalidate the whole region
1555 dirty = visibleRegion;
1556 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001557 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001558 layer->contentDirty = false;
1559 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001560 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001561 * the exposed region consists of two components:
1562 * 1) what's VISIBLE now and was COVERED before
1563 * 2) what's EXPOSED now less what was EXPOSED before
1564 *
1565 * note that (1) is conservative, we start with the whole
1566 * visible region but only keep what used to be covered by
1567 * something -- which mean it may have been exposed.
1568 *
1569 * (2) handles areas that were not covered by anything but got
1570 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001571 */
Mathias Agopianab028732010-03-16 16:41:46 -07001572 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001573 const Region oldVisibleRegion = layer->visibleRegion;
1574 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001575 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1576 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001577 }
1578 dirty.subtractSelf(aboveOpaqueLayers);
1579
1580 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001581 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001582
Mathias Agopianab028732010-03-16 16:41:46 -07001583 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001584 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001585
Jesse Halla8026d22012-09-25 13:25:04 -07001586 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001587 layer->setVisibleRegion(visibleRegion);
1588 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001589 layer->setVisibleNonTransparentRegion(
1590 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001591 }
1592
Mathias Agopian87baae12012-07-31 12:38:26 -07001593 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001594}
1595
Mathias Agopian87baae12012-07-31 12:38:26 -07001596void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1597 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001598 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001599 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1600 if (hw->getLayerStack() == layerStack) {
1601 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001602 }
1603 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001604}
1605
1606void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001607{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001608 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001609
Mathias Agopian4fec8732012-06-29 14:12:52 -07001610 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001611 const LayerVector& layers(mDrawingState.layersSortedByZ);
1612 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001613 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001614 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001615 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001616 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001617 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001618 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001619
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001620 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001621}
1622
Mathias Agopianad456f92011-01-13 17:53:01 -08001623void SurfaceFlinger::invalidateHwcGeometry()
1624{
1625 mHwWorkListDirty = true;
1626}
1627
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001628
Mathias Agopiancd60f992012-08-16 16:28:27 -07001629void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001630 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001631{
Mathias Agopian87baae12012-07-31 12:38:26 -07001632 Region dirtyRegion(inDirtyRegion);
1633
Mathias Agopianb8a55602009-06-26 19:06:36 -07001634 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001635 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001636
Mathias Agopian42977342012-08-05 00:40:46 -07001637 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001638 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001639 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1640 // takes a rectangle, we must make sure to update that whole
1641 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001642 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001643 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001644 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001645 // We need to redraw the rectangle that will be updated
1646 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001647 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001648 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001649 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001650 } else {
1651 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001652 dirtyRegion.set(hw->bounds());
1653 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001654 }
1655 }
1656
Mathias Agopianff2ed702013-09-01 21:36:12 -07001657 if (CC_LIKELY(!mDaltonize)) {
1658 doComposeSurfaces(hw, dirtyRegion);
1659 } else {
1660 RenderEngine& engine(getRenderEngine());
1661 engine.beginGroup(mDaltonizer());
1662 doComposeSurfaces(hw, dirtyRegion);
1663 engine.endGroup();
1664 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001665
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001666 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001667 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001668
1669 // swap buffers (presentation)
1670 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001671}
1672
Mathias Agopiancd60f992012-08-16 16:28:27 -07001673void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001674{
Mathias Agopian3f844832013-08-07 21:24:32 -07001675 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001676 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001677 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001678 HWComposer::LayerListIterator cur = hwc.begin(id);
1679 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001680
Jesse Halld05a17f2013-09-30 16:49:30 -07001681 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001682 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001683 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001684 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1685 hw->getDisplayName().string());
1686 return;
1687 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001688
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001689 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001690 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001691 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001692 // when using overlays, we assume a fully transparent framebuffer
1693 // NOTE: we could reduce how much we need to clear, for instance
1694 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001695 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001696 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001697 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001698 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001699 // we start with the whole screen area
1700 const Region bounds(hw->getBounds());
1701
1702 // we remove the scissor part
1703 // we're left with the letterbox region
1704 // (common case is that letterbox ends-up being empty)
1705 const Region letterbox(bounds.subtract(hw->getScissor()));
1706
1707 // compute the area to clear
1708 Region region(hw->undefinedRegion.merge(letterbox));
1709
1710 // but limit it to the dirty region
1711 region.andSelf(dirty);
1712
Mathias Agopianb9494d52012-04-18 02:28:45 -07001713 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001714 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001715 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001716 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001717 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001718 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001719
Mathias Agopian766dc492012-10-30 18:08:06 -07001720 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1721 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001722 // scissor on the main display. It should never be needed
1723 // anyways (though in theory it could since the API allows it).
1724 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001725 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001726 if (scissor != bounds) {
1727 // scissor doesn't match the screen's dimensions, so we
1728 // need to clear everything outside of it and enable
1729 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001730
Mathias Agopianf45c5102012-10-24 16:29:17 -07001731 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001732 const uint32_t height = hw->getHeight();
1733 engine.setScissor(scissor.left, height - scissor.bottom,
1734 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001735 }
1736 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001737 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001738
Mathias Agopian85d751c2012-08-29 16:59:24 -07001739 /*
1740 * and then, render the layers targeted at the framebuffer
1741 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001742
Mathias Agopian13127d82013-03-05 17:47:11 -08001743 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001744 const size_t count = layers.size();
1745 const Transform& tr = hw->getTransform();
1746 if (cur != end) {
1747 // we're using h/w composer
1748 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001749 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001750 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001751 if (!clip.isEmpty()) {
1752 switch (cur->getCompositionType()) {
1753 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001754 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001755 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1756 && i
Mathias Agopianac683022013-10-01 15:36:52 -07001757 && layer->isOpaque() && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001758 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001759 // never clear the very first layer since we're
1760 // guaranteed the FB is already cleared
1761 layer->clearWithOpenGL(hw, clip);
1762 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001763 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001764 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001765 case HWC_FRAMEBUFFER: {
1766 layer->draw(hw, clip);
1767 break;
1768 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001769 case HWC_FRAMEBUFFER_TARGET: {
1770 // this should not happen as the iterator shouldn't
1771 // let us get there.
1772 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1773 break;
1774 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001775 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001776 }
1777 layer->setAcquireFence(hw, *cur);
1778 }
1779 } else {
1780 // we're not using h/w composer
1781 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001782 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001783 const Region clip(dirty.intersect(
1784 tr.transform(layer->visibleRegion)));
1785 if (!clip.isEmpty()) {
1786 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001787 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001788 }
1789 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001790
1791 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001792 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001793}
1794
Mathias Agopian3f844832013-08-07 21:24:32 -07001795void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001796 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001797 RenderEngine& engine(getRenderEngine());
1798 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001799}
1800
Mathias Agopianac9fa422013-02-11 16:40:36 -08001801void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1802 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001803 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001804 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001805{
Mathias Agopian96f08192010-06-02 23:28:45 -07001806 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001807 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001808
Mathias Agopian96f08192010-06-02 23:28:45 -07001809 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001810 Mutex::Autolock _l(mStateLock);
1811 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001812 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001813}
1814
Mathias Agopian3f844832013-08-07 21:24:32 -07001815status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001816 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001817 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001818 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001819 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001820 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001821 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001822 return NO_ERROR;
1823 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001824 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001825}
1826
Mathias Agopian3f844832013-08-07 21:24:32 -07001827uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001828 return android_atomic_release_load(&mTransactionFlags);
1829}
1830
Mathias Agopian3f844832013-08-07 21:24:32 -07001831uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001832 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1833}
1834
Mathias Agopian3f844832013-08-07 21:24:32 -07001835uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001836 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1837 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001838 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001839 }
1840 return old;
1841}
1842
Mathias Agopian8b33f032012-07-24 20:43:54 -07001843void SurfaceFlinger::setTransactionState(
1844 const Vector<ComposerState>& state,
1845 const Vector<DisplayState>& displays,
1846 uint32_t flags)
1847{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001848 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001849 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001850 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001851
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001852 if (flags & eAnimation) {
1853 // For window updates that are part of an animation we must wait for
1854 // previous animation "frames" to be handled.
1855 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001856 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001857 if (CC_UNLIKELY(err != NO_ERROR)) {
1858 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001859 // caller after a few seconds.
1860 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1861 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001862 mAnimTransactionPending = false;
1863 break;
1864 }
1865 }
1866 }
1867
Mathias Agopiane57f2922012-08-09 16:29:12 -07001868 size_t count = displays.size();
1869 for (size_t i=0 ; i<count ; i++) {
1870 const DisplayState& s(displays[i]);
1871 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001872 }
1873
Mathias Agopiane57f2922012-08-09 16:29:12 -07001874 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001875 for (size_t i=0 ; i<count ; i++) {
1876 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001877 // Here we need to check that the interface we're given is indeed
1878 // one of our own. A malicious client could give us a NULL
1879 // IInterface, or one of its own or even one of our own but a
1880 // different type. All these situations would cause us to crash.
1881 //
1882 // NOTE: it would be better to use RTTI as we could directly check
1883 // that we have a Client*. however, RTTI is disabled in Android.
1884 if (s.client != NULL) {
1885 sp<IBinder> binder = s.client->asBinder();
1886 if (binder != NULL) {
1887 String16 desc(binder->getInterfaceDescriptor());
1888 if (desc == ISurfaceComposerClient::descriptor) {
1889 sp<Client> client( static_cast<Client *>(s.client.get()) );
1890 transactionFlags |= setClientStateLocked(client, s.state);
1891 }
1892 }
1893 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001894 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001895
Mathias Agopian386aa982011-11-07 21:58:03 -08001896 if (transactionFlags) {
1897 // this triggers the transaction
1898 setTransactionFlags(transactionFlags);
1899
1900 // if this is a synchronous transaction, wait for it to take effect
1901 // before returning.
1902 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001903 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001904 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001905 if (flags & eAnimation) {
1906 mAnimTransactionPending = true;
1907 }
1908 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001909 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1910 if (CC_UNLIKELY(err != NO_ERROR)) {
1911 // just in case something goes wrong in SF, return to the
1912 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001913 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1914 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001915 break;
1916 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001917 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001918 }
1919}
1920
Mathias Agopiane57f2922012-08-09 16:29:12 -07001921uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1922{
Jesse Hall9a143922012-10-04 16:29:19 -07001923 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1924 if (dpyIdx < 0)
1925 return 0;
1926
Mathias Agopiane57f2922012-08-09 16:29:12 -07001927 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001928 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001929 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001930 const uint32_t what = s.what;
1931 if (what & DisplayState::eSurfaceChanged) {
1932 if (disp.surface->asBinder() != s.surface->asBinder()) {
1933 disp.surface = s.surface;
1934 flags |= eDisplayTransactionNeeded;
1935 }
1936 }
1937 if (what & DisplayState::eLayerStackChanged) {
1938 if (disp.layerStack != s.layerStack) {
1939 disp.layerStack = s.layerStack;
1940 flags |= eDisplayTransactionNeeded;
1941 }
1942 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001943 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001944 if (disp.orientation != s.orientation) {
1945 disp.orientation = s.orientation;
1946 flags |= eDisplayTransactionNeeded;
1947 }
1948 if (disp.frame != s.frame) {
1949 disp.frame = s.frame;
1950 flags |= eDisplayTransactionNeeded;
1951 }
1952 if (disp.viewport != s.viewport) {
1953 disp.viewport = s.viewport;
1954 flags |= eDisplayTransactionNeeded;
1955 }
1956 }
1957 }
1958 return flags;
1959}
1960
1961uint32_t SurfaceFlinger::setClientStateLocked(
1962 const sp<Client>& client,
1963 const layer_state_t& s)
1964{
1965 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001966 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001967 if (layer != 0) {
1968 const uint32_t what = s.what;
1969 if (what & layer_state_t::ePositionChanged) {
1970 if (layer->setPosition(s.x, s.y))
1971 flags |= eTraversalNeeded;
1972 }
1973 if (what & layer_state_t::eLayerChanged) {
1974 // NOTE: index needs to be calculated before we update the state
1975 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1976 if (layer->setLayer(s.z)) {
1977 mCurrentState.layersSortedByZ.removeAt(idx);
1978 mCurrentState.layersSortedByZ.add(layer);
1979 // we need traversal (state changed)
1980 // AND transaction (list changed)
1981 flags |= eTransactionNeeded|eTraversalNeeded;
1982 }
1983 }
1984 if (what & layer_state_t::eSizeChanged) {
1985 if (layer->setSize(s.w, s.h)) {
1986 flags |= eTraversalNeeded;
1987 }
1988 }
1989 if (what & layer_state_t::eAlphaChanged) {
1990 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1991 flags |= eTraversalNeeded;
1992 }
1993 if (what & layer_state_t::eMatrixChanged) {
1994 if (layer->setMatrix(s.matrix))
1995 flags |= eTraversalNeeded;
1996 }
1997 if (what & layer_state_t::eTransparentRegionChanged) {
1998 if (layer->setTransparentRegionHint(s.transparentRegion))
1999 flags |= eTraversalNeeded;
2000 }
2001 if (what & layer_state_t::eVisibilityChanged) {
2002 if (layer->setFlags(s.flags, s.mask))
2003 flags |= eTraversalNeeded;
2004 }
2005 if (what & layer_state_t::eCropChanged) {
2006 if (layer->setCrop(s.crop))
2007 flags |= eTraversalNeeded;
2008 }
2009 if (what & layer_state_t::eLayerStackChanged) {
2010 // NOTE: index needs to be calculated before we update the state
2011 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2012 if (layer->setLayerStack(s.layerStack)) {
2013 mCurrentState.layersSortedByZ.removeAt(idx);
2014 mCurrentState.layersSortedByZ.add(layer);
2015 // we need traversal (state changed)
2016 // AND transaction (list changed)
2017 flags |= eTransactionNeeded|eTraversalNeeded;
2018 }
2019 }
2020 }
2021 return flags;
2022}
2023
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002024status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002025 const String8& name,
2026 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002027 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2028 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002029{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002030 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002031 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002032 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002033 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002034 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002035 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002036
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002037 status_t result = NO_ERROR;
2038
2039 sp<Layer> layer;
2040
Mathias Agopian3165cc22012-08-08 19:42:09 -07002041 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2042 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002043 result = createNormalLayer(client,
2044 name, w, h, flags, format,
2045 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002046 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002047 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002048 result = createDimLayer(client,
2049 name, w, h, flags,
2050 handle, gbp, &layer);
2051 break;
2052 default:
2053 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002054 break;
2055 }
2056
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002057 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002058 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002059 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002060 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002061 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002062}
2063
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002064status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2065 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2066 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002067{
2068 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002069 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002070 case PIXEL_FORMAT_TRANSPARENT:
2071 case PIXEL_FORMAT_TRANSLUCENT:
2072 format = PIXEL_FORMAT_RGBA_8888;
2073 break;
2074 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07002075#ifdef NO_RGBX_8888
2076 format = PIXEL_FORMAT_RGB_565;
2077#else
Mathias Agopian8f105402010-04-05 18:01:24 -07002078 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07002079#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002080 break;
2081 }
2082
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07002083#ifdef NO_RGBX_8888
2084 if (format == PIXEL_FORMAT_RGBX_8888)
2085 format = PIXEL_FORMAT_RGBA_8888;
2086#endif
2087
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002088 *outLayer = new Layer(this, client, name, w, h, flags);
2089 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2090 if (err == NO_ERROR) {
2091 *handle = (*outLayer)->getHandle();
2092 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002093 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002094
2095 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2096 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002097}
2098
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002099status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2100 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2101 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002102{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002103 *outLayer = new LayerDim(this, client, name, w, h, flags);
2104 *handle = (*outLayer)->getHandle();
2105 *gbp = (*outLayer)->getBufferQueue();
2106 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002107}
2108
Mathias Agopianac9fa422013-02-11 16:40:36 -08002109status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002110{
Mathias Agopian67106042013-03-14 19:18:13 -07002111 // called by the window manager when it wants to remove a Layer
2112 status_t err = NO_ERROR;
2113 sp<Layer> l(client->getLayerUser(handle));
2114 if (l != NULL) {
2115 err = removeLayer(l);
2116 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2117 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002118 }
2119 return err;
2120}
2121
Mathias Agopian13127d82013-03-05 17:47:11 -08002122status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002123{
Mathias Agopian67106042013-03-14 19:18:13 -07002124 // called by ~LayerCleaner() when all references to the IBinder (handle)
2125 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002126 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002127 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002128 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002129 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002130 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002131 "error removing layer=%p (%s)", l.get(), strerror(-err));
2132 }
2133 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002134}
2135
Mathias Agopianb60314a2012-04-10 22:09:54 -07002136// ---------------------------------------------------------------------------
2137
Andy McFadden13a082e2012-08-24 10:16:42 -07002138void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002139 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002140 Vector<ComposerState> state;
2141 Vector<DisplayState> displays;
2142 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002143 d.what = DisplayState::eDisplayProjectionChanged |
2144 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002145 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002146 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002147 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002148 d.frame.makeInvalid();
2149 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002150 displays.add(d);
2151 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002152 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07002153
2154 const nsecs_t period =
2155 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2156 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002157}
2158
2159void SurfaceFlinger::initializeDisplays() {
2160 class MessageScreenInitialized : public MessageBase {
2161 SurfaceFlinger* flinger;
2162 public:
2163 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2164 virtual bool handler() {
2165 flinger->onInitializeDisplays();
2166 return true;
2167 }
2168 };
2169 sp<MessageBase> msg = new MessageScreenInitialized(this);
2170 postMessageAsync(msg); // we may be called from main thread, use async message
2171}
2172
2173
Mathias Agopiancde87a32012-09-13 14:09:01 -07002174void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002175 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2176 if (hw->isScreenAcquired()) {
2177 // this is expected, e.g. when power manager wakes up during boot
2178 ALOGD(" screen was previously acquired");
2179 return;
2180 }
2181
Mathias Agopian42977342012-08-05 00:40:46 -07002182 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002183 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002184 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002185 // built-in display, tell the HWC
2186 getHwComposer().acquire(type);
2187
2188 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2189 // FIXME: eventthread only knows about the main display right now
2190 mEventThread->onScreenAcquired();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07002191
Jesse Hall948fe0c2013-10-14 12:56:09 -07002192 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002193 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002194 }
Mathias Agopian20128302012-08-13 18:32:13 -07002195 mVisibleRegionsDirty = true;
2196 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002197}
2198
Mathias Agopiancde87a32012-09-13 14:09:01 -07002199void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002200 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2201 if (!hw->isScreenAcquired()) {
2202 ALOGD(" screen was previously released");
2203 return;
2204 }
2205
2206 hw->releaseScreen();
2207 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002208 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002209 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002210 disableHardwareVsync(true); // also cancels any in-progress resync
2211
Mathias Agopiancde87a32012-09-13 14:09:01 -07002212 // FIXME: eventthread only knows about the main display right now
2213 mEventThread->onScreenReleased();
2214 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002215
2216 // built-in display, tell the HWC
2217 getHwComposer().release(type);
2218 }
2219 mVisibleRegionsDirty = true;
2220 // from this point on, SF will stop drawing on this display
2221}
2222
2223void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2224 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002225 SurfaceFlinger& mFlinger;
2226 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002227 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002228 MessageScreenAcquired(SurfaceFlinger& flinger,
2229 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002230 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002231 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2232 if (hw == NULL) {
2233 ALOGE("Attempt to unblank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002234 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002235 ALOGW("Attempt to unblank virtual display");
2236 } else {
2237 mFlinger.onScreenAcquired(hw);
2238 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002239 return true;
2240 }
2241 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002242 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2243 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002244}
2245
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002246void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002247 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002248 SurfaceFlinger& mFlinger;
2249 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002250 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002251 MessageScreenReleased(SurfaceFlinger& flinger,
2252 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002253 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002254 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2255 if (hw == NULL) {
2256 ALOGE("Attempt to blank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002257 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002258 ALOGW("Attempt to blank virtual display");
2259 } else {
2260 mFlinger.onScreenReleased(hw);
2261 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002262 return true;
2263 }
2264 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002265 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2266 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002267}
2268
2269// ---------------------------------------------------------------------------
2270
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002271status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2272{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002273 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002274
Mathias Agopianbd115332013-04-18 16:41:04 -07002275 IPCThreadState* ipc = IPCThreadState::self();
2276 const int pid = ipc->getCallingPid();
2277 const int uid = ipc->getCallingUid();
2278 if ((uid != AID_SHELL) &&
2279 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002280 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002281 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002282 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002283 // Try to get the main lock, but don't insist if we can't
2284 // (this would indicate SF is stuck, but we want to be able to
2285 // print something in dumpsys).
2286 int retry = 3;
2287 while (mStateLock.tryLock()<0 && --retry>=0) {
2288 usleep(1000000);
2289 }
2290 const bool locked(retry >= 0);
2291 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002292 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002293 "SurfaceFlinger appears to be unresponsive, "
2294 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002295 }
2296
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002297 bool dumpAll = true;
2298 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002299 size_t numArgs = args.size();
2300 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002301 if ((index < numArgs) &&
2302 (args[index] == String16("--list"))) {
2303 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002304 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002305 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002306 }
2307
2308 if ((index < numArgs) &&
2309 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002310 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002311 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002312 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002313 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002314
2315 if ((index < numArgs) &&
2316 (args[index] == String16("--latency-clear"))) {
2317 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002318 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002319 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002320 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002321 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002322
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002323 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002324 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002325 }
2326
Mathias Agopian9795c422009-08-26 16:36:26 -07002327 if (locked) {
2328 mStateLock.unlock();
2329 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002330 }
2331 write(fd, result.string(), result.size());
2332 return NO_ERROR;
2333}
2334
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002335void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002336 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002337{
2338 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2339 const size_t count = currentLayers.size();
2340 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002341 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002342 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002343 }
2344}
2345
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002346void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002347 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002348{
2349 String8 name;
2350 if (index < args.size()) {
2351 name = String8(args[index]);
2352 index++;
2353 }
2354
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002355 const nsecs_t period =
2356 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2357 result.appendFormat("%lld\n", period);
2358
2359 if (name.isEmpty()) {
2360 mAnimFrameTracker.dump(result);
2361 } else {
2362 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2363 const size_t count = currentLayers.size();
2364 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002365 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002366 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002367 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002368 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002369 }
2370 }
2371}
2372
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002373void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002374 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002375{
2376 String8 name;
2377 if (index < args.size()) {
2378 name = String8(args[index]);
2379 index++;
2380 }
2381
2382 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2383 const size_t count = currentLayers.size();
2384 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002385 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002386 if (name.isEmpty() || (name == layer->getName())) {
2387 layer->clearStats();
2388 }
2389 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002390
2391 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002392}
2393
Jamie Gennis6547ff42013-07-16 20:12:42 -07002394// This should only be called from the main thread. Otherwise it would need
2395// the lock and should use mCurrentState rather than mDrawingState.
2396void SurfaceFlinger::logFrameStats() {
2397 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2398 const size_t count = drawingLayers.size();
2399 for (size_t i=0 ; i<count ; i++) {
2400 const sp<Layer>& layer(drawingLayers[i]);
2401 layer->logFrameStats();
2402 }
2403
2404 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2405}
2406
Andy McFadden4803b742012-09-24 19:07:20 -07002407/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2408{
2409 static const char* config =
2410 " [sf"
2411#ifdef NO_RGBX_8888
2412 " NO_RGBX_8888"
2413#endif
2414#ifdef HAS_CONTEXT_PRIORITY
2415 " HAS_CONTEXT_PRIORITY"
2416#endif
2417#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2418 " NEVER_DEFAULT_TO_ASYNC_MODE"
2419#endif
2420#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2421 " TARGET_DISABLE_TRIPLE_BUFFERING"
2422#endif
2423 "]";
2424 result.append(config);
2425}
2426
Mathias Agopian74d211a2013-04-22 16:55:35 +02002427void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2428 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002429{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002430 bool colorize = false;
2431 if (index < args.size()
2432 && (args[index] == String16("--color"))) {
2433 colorize = true;
2434 index++;
2435 }
2436
2437 Colorizer colorizer(colorize);
2438
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002439 // figure out if we're stuck somewhere
2440 const nsecs_t now = systemTime();
2441 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2442 const nsecs_t inTransaction(mDebugInTransaction);
2443 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2444 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2445
2446 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002447 * Dump library configuration.
2448 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002449
2450 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002451 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002452 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002453 appendSfConfigString(result);
2454 appendUiConfigString(result);
2455 appendGuiConfigString(result);
2456 result.append("\n");
2457
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002458 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002459 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002460 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002461 result.append(SyncFeatures::getInstance().toString());
2462 result.append("\n");
2463
Andy McFadden4803b742012-09-24 19:07:20 -07002464 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002465 * Dump the visible layer list
2466 */
2467 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2468 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002469 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002470 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002471 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002472 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002473 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002474 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002475 }
2476
2477 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002478 * Dump Display state
2479 */
2480
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002481 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002482 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002483 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002484 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2485 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002486 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002487 }
2488
2489 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002490 * Dump SurfaceFlinger global state
2491 */
2492
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002493 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002494 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002495 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002496
Mathias Agopian888c8222012-08-04 21:10:38 -07002497 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002498 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002499
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002500 colorizer.bold(result);
2501 result.appendFormat("EGL implementation : %s\n",
2502 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2503 colorizer.reset(result);
2504 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002505 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002506
Mathias Agopian875d8e12013-06-07 15:35:48 -07002507 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002508
Mathias Agopian42977342012-08-05 00:40:46 -07002509 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002510 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002511 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002512 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002513 " last eglSwapBuffers() time: %f us\n"
2514 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002515 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002516 " refresh-rate : %f fps\n"
2517 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002518 " y-dpi : %f\n"
2519 " EGL_NATIVE_VISUAL_ID : %d\n"
2520 " gpu_to_cpu_unsupported : %d\n"
2521 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002522 mLastSwapBufferTime/1000.0,
2523 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002524 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002525 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2526 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002527 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2528 mEGLNativeVisualId,
2529 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002530
Mathias Agopian74d211a2013-04-22 16:55:35 +02002531 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002532 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002533
Mathias Agopian74d211a2013-04-22 16:55:35 +02002534 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002535 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002536
2537 /*
2538 * VSYNC state
2539 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002540 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002541
2542 /*
2543 * Dump HWComposer state
2544 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002545 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002546 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002547 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002548 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002549 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Mathias Agopianff2ed702013-09-01 21:36:12 -07002550 (mDebugDisableHWC || mDebugRegion || mDaltonize) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002551 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002552
2553 /*
2554 * Dump gralloc state
2555 */
2556 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2557 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002558}
2559
Mathias Agopian13127d82013-03-05 17:47:11 -08002560const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002561SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002562 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002563 wp<IBinder> dpy;
2564 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2565 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2566 dpy = mDisplays.keyAt(i);
2567 break;
2568 }
2569 }
2570 if (dpy == NULL) {
2571 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2572 // Just use the primary display so we have something to return
2573 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2574 }
2575 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002576}
2577
Keun young Park63f165f2012-08-31 10:53:36 -07002578bool SurfaceFlinger::startDdmConnection()
2579{
2580 void* libddmconnection_dso =
2581 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2582 if (!libddmconnection_dso) {
2583 return false;
2584 }
2585 void (*DdmConnection_start)(const char* name);
2586 DdmConnection_start =
2587 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2588 if (!DdmConnection_start) {
2589 dlclose(libddmconnection_dso);
2590 return false;
2591 }
2592 (*DdmConnection_start)(getServiceName());
2593 return true;
2594}
2595
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002596status_t SurfaceFlinger::onTransact(
2597 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2598{
2599 switch (code) {
2600 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002601 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002602 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002603 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002604 case BLANK:
2605 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002606 {
2607 // codes that require permission check
2608 IPCThreadState* ipc = IPCThreadState::self();
2609 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002610 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002611 if ((uid != AID_GRAPHICS) &&
2612 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002613 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002614 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2615 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002616 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002617 break;
2618 }
2619 case CAPTURE_SCREEN:
2620 {
2621 // codes that require permission check
2622 IPCThreadState* ipc = IPCThreadState::self();
2623 const int pid = ipc->getCallingPid();
2624 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002625 if ((uid != AID_GRAPHICS) &&
2626 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002627 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002628 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2629 return PERMISSION_DENIED;
2630 }
2631 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002632 }
2633 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002634
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002635 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2636 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002637 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002638 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002639 IPCThreadState* ipc = IPCThreadState::self();
2640 const int pid = ipc->getCallingPid();
2641 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002642 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002643 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002644 return PERMISSION_DENIED;
2645 }
2646 int n;
2647 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002648 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002649 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002650 return NO_ERROR;
2651 case 1002: // SHOW_UPDATES
2652 n = data.readInt32();
2653 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002654 invalidateHwcGeometry();
2655 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002656 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002657 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002658 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002659 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002660 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002661 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002662 setTransactionFlags(
2663 eTransactionNeeded|
2664 eDisplayTransactionNeeded|
2665 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002666 return NO_ERROR;
2667 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002668 case 1006:{ // send empty update
2669 signalRefresh();
2670 return NO_ERROR;
2671 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002672 case 1008: // toggle use of hw composer
2673 n = data.readInt32();
2674 mDebugDisableHWC = n ? 1 : 0;
2675 invalidateHwcGeometry();
2676 repaintEverything();
2677 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002678 case 1009: // toggle use of transform hint
2679 n = data.readInt32();
2680 mDebugDisableTransformHint = n ? 1 : 0;
2681 invalidateHwcGeometry();
2682 repaintEverything();
2683 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002684 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002685 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002686 reply->writeInt32(0);
2687 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002688 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002689 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002690 return NO_ERROR;
2691 case 1013: {
2692 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002693 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2694 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002695 return NO_ERROR;
2696 }
2697 case 1014: {
2698 // daltonize
2699 n = data.readInt32();
2700 switch (n % 10) {
2701 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2702 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2703 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2704 }
2705 if (n >= 10) {
2706 mDaltonizer.setMode(Daltonizer::correction);
2707 } else {
2708 mDaltonizer.setMode(Daltonizer::simulation);
2709 }
2710 mDaltonize = n > 0;
2711 invalidateHwcGeometry();
2712 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002713 }
2714 return NO_ERROR;
2715 }
2716 }
2717 return err;
2718}
2719
Mathias Agopian53331da2011-08-22 21:44:41 -07002720void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002721 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002722 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002723}
2724
Mathias Agopian59119e62010-10-11 12:37:43 -07002725// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002726// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002727// ---------------------------------------------------------------------------
2728
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002729/* The code below is here to handle b/8734824
2730 *
2731 * We create a IGraphicBufferProducer wrapper that forwards all calls
2732 * to the calling binder thread, where they are executed. This allows
2733 * the calling thread to be reused (on the other side) and not
2734 * depend on having "enough" binder threads to handle the requests.
2735 *
2736 */
2737
2738class GraphicProducerWrapper : public BBinder, public MessageHandler {
2739 sp<IGraphicBufferProducer> impl;
2740 sp<Looper> looper;
2741 status_t result;
2742 bool exitPending;
2743 bool exitRequested;
2744 mutable Barrier barrier;
2745 volatile int32_t memoryBarrier;
2746 uint32_t code;
2747 Parcel const* data;
2748 Parcel* reply;
2749
2750 enum {
2751 MSG_API_CALL,
2752 MSG_EXIT
2753 };
2754
2755 /*
2756 * this is called by our "fake" BpGraphicBufferProducer. We package the
2757 * data and reply Parcel and forward them to the calling thread.
2758 */
2759 virtual status_t transact(uint32_t code,
2760 const Parcel& data, Parcel* reply, uint32_t flags) {
2761 this->code = code;
2762 this->data = &data;
2763 this->reply = reply;
2764 android_atomic_acquire_store(0, &memoryBarrier);
2765 if (exitPending) {
2766 // if we've exited, we run the message synchronously right here
2767 handleMessage(Message(MSG_API_CALL));
2768 } else {
2769 barrier.close();
2770 looper->sendMessage(this, Message(MSG_API_CALL));
2771 barrier.wait();
2772 }
2773 return NO_ERROR;
2774 }
2775
2776 /*
2777 * here we run on the binder calling thread. All we've got to do is
2778 * call the real BpGraphicBufferProducer.
2779 */
2780 virtual void handleMessage(const Message& message) {
2781 android_atomic_release_load(&memoryBarrier);
2782 if (message.what == MSG_API_CALL) {
2783 impl->asBinder()->transact(code, data[0], reply);
2784 barrier.open();
2785 } else if (message.what == MSG_EXIT) {
2786 exitRequested = true;
2787 }
2788 }
2789
2790public:
2791 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2792 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2793 exitPending(false), exitRequested(false) {
2794 }
2795
2796 status_t waitForResponse() {
2797 do {
2798 looper->pollOnce(-1);
2799 } while (!exitRequested);
2800 return result;
2801 }
2802
2803 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002804 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002805 exitPending = true;
2806 looper->sendMessage(this, Message(MSG_EXIT));
2807 }
2808};
2809
2810
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002811status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2812 const sp<IGraphicBufferProducer>& producer,
2813 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002814 uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002815
2816 if (CC_UNLIKELY(display == 0))
2817 return BAD_VALUE;
2818
2819 if (CC_UNLIKELY(producer == 0))
2820 return BAD_VALUE;
2821
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002822 // if we have secure windows on this display, never allow the screen capture
2823 // unless the producer interface is local (i.e.: we can take a screenshot for
2824 // ourselves).
2825 if (!producer->asBinder()->localBinder()) {
2826 Mutex::Autolock _l(mStateLock);
2827 sp<const DisplayDevice> hw(getDisplayDevice(display));
2828 if (hw->getSecureLayerVisible()) {
2829 ALOGW("FB is protected: PERMISSION_DENIED");
2830 return PERMISSION_DENIED;
2831 }
2832 }
2833
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002834 class MessageCaptureScreen : public MessageBase {
2835 SurfaceFlinger* flinger;
2836 sp<IBinder> display;
2837 sp<IGraphicBufferProducer> producer;
2838 uint32_t reqWidth, reqHeight;
2839 uint32_t minLayerZ,maxLayerZ;
2840 status_t result;
2841 public:
2842 MessageCaptureScreen(SurfaceFlinger* flinger,
2843 const sp<IBinder>& display,
2844 const sp<IGraphicBufferProducer>& producer,
2845 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002846 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002847 : flinger(flinger), display(display), producer(producer),
2848 reqWidth(reqWidth), reqHeight(reqHeight),
2849 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2850 result(PERMISSION_DENIED)
2851 {
2852 }
2853 status_t getResult() const {
2854 return result;
2855 }
2856 virtual bool handler() {
2857 Mutex::Autolock _l(flinger->mStateLock);
2858 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002859 result = flinger->captureScreenImplLocked(hw,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002860 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002861 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002862 return true;
2863 }
2864 };
2865
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002866 // make sure to process transactions before screenshots -- a transaction
2867 // might already be pending but scheduled for VSYNC; this guarantees we
2868 // will handle it before the screenshot. When VSYNC finally arrives
2869 // the scheduled transaction will be a no-op. If no transactions are
2870 // scheduled at this time, this will end-up being a no-op as well.
2871 mEventQueue.invalidateTransactionNow();
2872
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002873 // this creates a "fake" BBinder which will serve as a "fake" remote
2874 // binder to receive the marshaled calls and forward them to the
2875 // real remote (a BpGraphicBufferProducer)
2876 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2877
2878 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2879 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002880 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002881 display, IGraphicBufferProducer::asInterface( wrapper ),
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002882 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002883
2884 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002885 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002886 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002887 }
2888 return res;
2889}
2890
Mathias Agopian180f10d2013-04-10 22:55:41 -07002891
2892void SurfaceFlinger::renderScreenImplLocked(
2893 const sp<const DisplayDevice>& hw,
2894 uint32_t reqWidth, uint32_t reqHeight,
2895 uint32_t minLayerZ, uint32_t maxLayerZ,
2896 bool yswap)
2897{
2898 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002899 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002900
2901 // get screen geometry
2902 const uint32_t hw_w = hw->getWidth();
2903 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002904 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2905
2906 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002907 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002908
2909 // set-up our viewport
Mathias Agopian3f844832013-08-07 21:24:32 -07002910 engine.setViewportAndProjection(reqWidth, reqHeight, hw_w, hw_h, yswap);
2911 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002912
2913 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002914 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002915
2916 const LayerVector& layers( mDrawingState.layersSortedByZ );
2917 const size_t count = layers.size();
2918 for (size_t i=0 ; i<count ; ++i) {
2919 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002920 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002921 if (state.layerStack == hw->getLayerStack()) {
2922 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2923 if (layer->isVisible()) {
2924 if (filtering) layer->setFiltering(true);
2925 layer->draw(hw);
2926 if (filtering) layer->setFiltering(false);
2927 }
2928 }
2929 }
2930 }
2931
2932 // compositionComplete is needed for older driver
2933 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002934 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002935}
2936
2937
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002938status_t SurfaceFlinger::captureScreenImplLocked(
2939 const sp<const DisplayDevice>& hw,
2940 const sp<IGraphicBufferProducer>& producer,
2941 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002942 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002943{
2944 ATRACE_CALL();
2945
Mathias Agopian180f10d2013-04-10 22:55:41 -07002946 // get screen geometry
2947 const uint32_t hw_w = hw->getWidth();
2948 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002949
Mathias Agopian180f10d2013-04-10 22:55:41 -07002950 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2951 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2952 reqWidth, reqHeight, hw_w, hw_h);
2953 return BAD_VALUE;
2954 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002955
Mathias Agopian180f10d2013-04-10 22:55:41 -07002956 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2957 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2958
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002959 // create a surface (because we're a producer, and we need to
2960 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07002961 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002962 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002963
2964 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002965 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002966 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2967 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002968
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002969 int err = 0;
2970 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07002971 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002972 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2973 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002974
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002975 if (err == NO_ERROR) {
2976 ANativeWindowBuffer* buffer;
2977 /* TODO: Once we have the sync framework everywhere this can use
2978 * server-side waits on the fence that dequeueBuffer returns.
2979 */
2980 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2981 if (result == NO_ERROR) {
2982 // create an EGLImage from the buffer so we can later
2983 // turn it into a texture
2984 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2985 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2986 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002987 // this binds the given EGLImage as a framebuffer for the
2988 // duration of this scope.
2989 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2990 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002991 // this will in fact render into our dequeued buffer
2992 // via an FBO, which means we didn't have to create
2993 // an EGLSurface and therefore we're not
2994 // dependent on the context's EGLConfig.
2995 renderScreenImplLocked(hw, reqWidth, reqHeight,
2996 minLayerZ, maxLayerZ, true);
Mathias Agopiand5556842013-09-19 17:08:37 -07002997
Andy McFadden2d8d1202013-10-09 16:38:02 -07002998 // Create a sync point and wait on it, so we know the buffer is
2999 // ready before we pass it along. We can't trivially call glFlush(),
3000 // so we use a wait flag instead.
3001 // TODO: pass a sync fd to queueBuffer() and let the consumer wait.
3002 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3003 if (sync != EGL_NO_SYNC_KHR) {
3004 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3005 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3006 EGLint eglErr = eglGetError();
3007 eglDestroySyncKHR(mEGLDisplay, sync);
3008 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3009 ALOGW("captureScreen: fence wait timed out");
3010 } else {
3011 ALOGW_IF(eglErr != EGL_SUCCESS,
3012 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3013 }
3014 } else {
3015 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3016 // not fatal
3017 }
3018
Mathias Agopiand5556842013-09-19 17:08:37 -07003019 if (DEBUG_SCREENSHOTS) {
3020 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3021 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3022 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3023 hw, minLayerZ, maxLayerZ);
3024 delete [] pixels;
3025 }
3026
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003027 } else {
3028 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3029 result = INVALID_OPERATION;
3030 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003031 // destroy our image
3032 eglDestroyImageKHR(mEGLDisplay, image);
3033 } else {
3034 result = BAD_VALUE;
3035 }
3036 window->queueBuffer(window, buffer, -1);
3037 }
3038 } else {
3039 result = BAD_VALUE;
3040 }
3041 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3042 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003043
Mathias Agopian74c40c02010-09-29 13:02:36 -07003044 return result;
3045}
3046
Mathias Agopiand5556842013-09-19 17:08:37 -07003047void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3048 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003049 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003050 for (size_t y=0 ; y<h ; y++) {
3051 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3052 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003053 if (p[x] != 0xFF000000) return;
3054 }
3055 }
3056 ALOGE("*** we just took a black screenshot ***\n"
3057 "requested minz=%d, maxz=%d, layerStack=%d",
3058 minLayerZ, maxLayerZ, hw->getLayerStack());
3059 const LayerVector& layers( mDrawingState.layersSortedByZ );
3060 const size_t count = layers.size();
3061 for (size_t i=0 ; i<count ; ++i) {
3062 const sp<Layer>& layer(layers[i]);
3063 const Layer::State& state(layer->getDrawingState());
3064 const bool visible = (state.layerStack == hw->getLayerStack())
3065 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3066 && (layer->isVisible());
3067 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
3068 visible ? '+' : '-',
3069 i, layer->getName().string(), state.layerStack, state.z,
3070 layer->isVisible(), state.flags, state.alpha);
3071 }
3072 }
3073}
3074
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003075// ---------------------------------------------------------------------------
3076
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003077SurfaceFlinger::LayerVector::LayerVector() {
3078}
3079
3080SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003081 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003082}
3083
3084int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3085 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003086{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003087 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003088 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3089 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003090
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003091 uint32_t ls = l->getCurrentState().layerStack;
3092 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003093 if (ls != rs)
3094 return ls - rs;
3095
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003096 uint32_t lz = l->getCurrentState().z;
3097 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003098 if (lz != rz)
3099 return lz - rz;
3100
3101 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003102}
3103
3104// ---------------------------------------------------------------------------
3105
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003106SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
3107 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003108}
3109
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003110SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08003111 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003112 viewport.makeInvalid();
3113 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003114}
3115
3116// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003117
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003118}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003119
3120
3121#if defined(__gl_h_)
3122#error "don't include gl/gl.h in this file"
3123#endif
3124
3125#if defined(__gl2_h_)
3126#error "don't include gl2/gl2.h in this file"
3127#endif