blob: 189049c24ced8b35cf77bc9ec1903a5d83868406 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <stdint.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070020#include <sys/types.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <errno.h>
22#include <math.h>
Keun young Park63f165f2012-08-31 10:53:36 -070023#include <dlfcn.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070024
25#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <cutils/log.h>
28#include <cutils/properties.h>
29
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070030#include <binder/IPCThreadState.h>
31#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070032#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070033#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070034
Mathias Agopianc666cae2012-07-25 18:56:13 -070035#include <ui/DisplayInfo.h>
36
Mathias Agopian921e6ac2012-07-23 23:11:29 -070037#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070038#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070039#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070040#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080041#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080042#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070043
44#include <ui/GraphicBufferAllocator.h>
45#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070046#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080047
Mathias Agopiancde87a32012-09-13 14:09:01 -070048#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049#include <utils/String8.h>
50#include <utils/String16.h>
51#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080052#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080053
Mathias Agopian921e6ac2012-07-23 23:11:29 -070054#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070055#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056
Mathias Agopian3e25fd82013-04-22 17:52:16 +020057#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020059#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080060#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070061#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070062#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070063#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080064#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068
Mathias Agopiana4912602012-07-12 14:25:33 -070069#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070070#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070071#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072
Mathias Agopianff2ed702013-09-01 21:36:12 -070073#include "Effects/Daltonizer.h"
74
Mathias Agopian875d8e12013-06-07 15:35:48 -070075#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070076#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070077
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078#define DISPLAY_COUNT 1
79
Mathias Agopianfee2b462013-07-03 12:34:01 -070080/*
81 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
82 * black pixels.
83 */
84#define DEBUG_SCREENSHOTS false
85
Mathias Agopianca088332013-03-28 17:44:13 -070086EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
87
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070089
90// This works around the lack of support for the sync framework on some
91// devices.
92#ifdef RUNNING_WITHOUT_SYNC_FRAMEWORK
93static const bool runningWithoutSyncFramework = true;
94#else
95static const bool runningWithoutSyncFramework = false;
96#endif
97
98// This is the phase offset in nanoseconds of the software vsync event
99// relative to the vsync event reported by HWComposer. The software vsync
100// event is when SurfaceFlinger and Choreographer-based applications run each
101// frame.
102//
103// This phase offset allows adjustment of the minimum latency from application
104// wake-up (by Choregographer) time to the time at which the resulting window
105// image is displayed. This value may be either positive (after the HW vsync)
106// or negative (before the HW vsync). Setting it to 0 will result in a
107// minimum latency of two vsync periods because the app and SurfaceFlinger
108// will run just after the HW vsync. Setting it to a positive number will
109// result in the minimum latency being:
110//
111// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
112//
113// Note that reducing this latency makes it more likely for the applications
114// to not have their window content image ready in time. When this happens
115// the latency will end up being an additional vsync period, and animations
116// will hiccup. Therefore, this latency should be tuned somewhat
117// conservatively (or at least with awareness of the trade-off being made).
118static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
119
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700120// This is the phase offset at which SurfaceFlinger's composition runs.
121static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
122
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800123// ---------------------------------------------------------------------------
124
Mathias Agopian99b49842011-06-27 16:05:52 -0700125const String16 sHardwareTest("android.permission.HARDWARE_TEST");
126const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
127const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
128const String16 sDump("android.permission.DUMP");
129
130// ---------------------------------------------------------------------------
131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700133 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700135 mTransactionPending(false),
136 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700137 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700138 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700139 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700142 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800143 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700145 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700146 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700147 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700148 mDebugInSwapBuffers(0),
149 mLastSwapBufferTime(0),
150 mDebugInTransaction(0),
151 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700152 mBootFinished(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700153 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700154 mHWVsyncAvailable(false),
Alan Viverette9c5a3332013-09-12 20:04:35 -0700155 mDaltonize(false),
156 mHasColorMatrix(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157{
Steve Blocka19954a2012-01-04 20:05:49 +0000158 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159
160 // debugging stuff...
161 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700162
Mathias Agopianb4b17302013-03-20 18:36:41 -0700163 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700164 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700165
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 property_get("debug.sf.showupdates", value, "0");
167 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700168
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700169 property_get("debug.sf.ddms", value, "0");
170 mDebugDDMS = atoi(value);
171 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700172 if (!startDdmConnection()) {
173 // start failed, and DDMS debugging not enabled
174 mDebugDDMS = 0;
175 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700176 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700177 ALOGI_IF(mDebugRegion, "showupdates enabled");
178 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179}
180
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800181void SurfaceFlinger::onFirstRef()
182{
183 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800184}
185
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186SurfaceFlinger::~SurfaceFlinger()
187{
Mathias Agopiana4912602012-07-12 14:25:33 -0700188 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
189 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
190 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191}
192
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800193void SurfaceFlinger::binderDied(const wp<IBinder>& who)
194{
195 // the window manager died on us. prepare its eulogy.
196
Andy McFadden13a082e2012-08-24 10:16:42 -0700197 // restore initial conditions (default device unblank, etc)
198 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800199
200 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700201 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800202}
203
Mathias Agopian7e27f052010-05-28 14:22:23 -0700204sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205{
Mathias Agopian96f08192010-06-02 23:28:45 -0700206 sp<ISurfaceComposerClient> bclient;
207 sp<Client> client(new Client(this));
208 status_t err = client->initCheck();
209 if (err == NO_ERROR) {
210 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 return bclient;
213}
214
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700215sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
216 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700217{
218 class DisplayToken : public BBinder {
219 sp<SurfaceFlinger> flinger;
220 virtual ~DisplayToken() {
221 // no more references, this display must be terminated
222 Mutex::Autolock _l(flinger->mStateLock);
223 flinger->mCurrentState.displays.removeItem(this);
224 flinger->setTransactionFlags(eDisplayTransactionNeeded);
225 }
226 public:
227 DisplayToken(const sp<SurfaceFlinger>& flinger)
228 : flinger(flinger) {
229 }
230 };
231
232 sp<BBinder> token = new DisplayToken(this);
233
234 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700235 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700236 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700237 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700238 mCurrentState.displays.add(token, info);
239
240 return token;
241}
242
Jesse Hall6c913be2013-08-08 12:15:49 -0700243void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
244 Mutex::Autolock _l(mStateLock);
245
246 ssize_t idx = mCurrentState.displays.indexOfKey(display);
247 if (idx < 0) {
248 ALOGW("destroyDisplay: invalid display token");
249 return;
250 }
251
252 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
253 if (!info.isVirtualDisplay()) {
254 ALOGE("destroyDisplay called for non-virtual display");
255 return;
256 }
257
258 mCurrentState.displays.removeItemsAt(idx);
259 setTransactionFlags(eDisplayTransactionNeeded);
260}
261
Jesse Hall692c7232012-11-08 15:41:56 -0800262void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
263 ALOGW_IF(mBuiltinDisplays[type],
264 "Overwriting display token for display type %d", type);
265 mBuiltinDisplays[type] = new BBinder();
266 DisplayDeviceState info(type);
267 // All non-virtual displays are currently considered secure.
268 info.isSecure = true;
269 mCurrentState.displays.add(mBuiltinDisplays[type], info);
270}
271
Mathias Agopiane57f2922012-08-09 16:29:12 -0700272sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700273 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700274 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
275 return NULL;
276 }
Jesse Hall692c7232012-11-08 15:41:56 -0800277 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700278}
279
Jamie Gennis9a78c902011-01-12 18:30:40 -0800280sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
281{
282 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
283 return gba;
284}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700285
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800286void SurfaceFlinger::bootFinished()
287{
288 const nsecs_t now = systemTime();
289 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000290 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700291 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700292
293 // wait patiently for the window manager death
294 const String16 name("window");
295 sp<IBinder> window(defaultServiceManager()->getService(name));
296 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700297 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700298 }
299
300 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700301 // formerly we would just kill the process, but we now ask it to exit so it
302 // can choose where to stop the animation.
303 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800304}
305
Mathias Agopian3f844832013-08-07 21:24:32 -0700306void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700307 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700308 RenderEngine& engine;
309 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700310 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700311 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
312 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700313 }
314 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700315 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700316 return true;
317 }
318 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700319 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700320}
321
Mathias Agopian722b98f2012-09-25 18:24:31 -0700322status_t SurfaceFlinger::selectConfigForAttribute(
Mathias Agopiana4912602012-07-12 14:25:33 -0700323 EGLDisplay dpy,
324 EGLint const* attrs,
Mathias Agopian722b98f2012-09-25 18:24:31 -0700325 EGLint attribute, EGLint wanted,
Mathias Agopiana4912602012-07-12 14:25:33 -0700326 EGLConfig* outConfig)
327{
328 EGLConfig config = NULL;
329 EGLint numConfigs = -1, n=0;
330 eglGetConfigs(dpy, NULL, 0, &numConfigs);
331 EGLConfig* const configs = new EGLConfig[numConfigs];
332 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
Mathias Agopiancde87a32012-09-13 14:09:01 -0700333
Mathias Agopian722b98f2012-09-25 18:24:31 -0700334 if (n) {
335 if (attribute != EGL_NONE) {
336 for (int i=0 ; i<n ; i++) {
337 EGLint value = 0;
338 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
339 if (wanted == value) {
340 *outConfig = configs[i];
341 delete [] configs;
342 return NO_ERROR;
343 }
344 }
345 } else {
346 // just pick the first one
347 *outConfig = configs[0];
Mathias Agopiana4912602012-07-12 14:25:33 -0700348 delete [] configs;
349 return NO_ERROR;
350 }
351 }
352 delete [] configs;
353 return NAME_NOT_FOUND;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800354}
355
Mathias Agopian722b98f2012-09-25 18:24:31 -0700356class EGLAttributeVector {
357 struct Attribute;
358 class Adder;
359 friend class Adder;
360 KeyedVector<Attribute, EGLint> mList;
361 struct Attribute {
362 Attribute() {};
363 Attribute(EGLint v) : v(v) { }
364 EGLint v;
365 bool operator < (const Attribute& other) const {
366 // this places EGL_NONE at the end
367 EGLint lhs(v);
368 EGLint rhs(other.v);
369 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
370 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
371 return lhs < rhs;
372 }
373 };
374 class Adder {
375 friend class EGLAttributeVector;
376 EGLAttributeVector& v;
377 EGLint attribute;
378 Adder(EGLAttributeVector& v, EGLint attribute)
379 : v(v), attribute(attribute) {
380 }
381 public:
382 void operator = (EGLint value) {
383 if (attribute != EGL_NONE) {
384 v.mList.add(attribute, value);
385 }
386 }
387 operator EGLint () const { return v.mList[attribute]; }
388 };
389public:
390 EGLAttributeVector() {
391 mList.add(EGL_NONE, EGL_NONE);
392 }
393 void remove(EGLint attribute) {
394 if (attribute != EGL_NONE) {
395 mList.removeItem(attribute);
396 }
397 }
398 Adder operator [] (EGLint attribute) {
399 return Adder(*this, attribute);
400 }
401 EGLint operator [] (EGLint attribute) const {
402 return mList[attribute];
403 }
404 // cast-operator to (EGLint const*)
405 operator EGLint const* () const { return &mList.keyAt(0).v; }
406};
407
Jesse Hallb65f32e2013-09-27 22:10:47 -0700408status_t SurfaceFlinger::selectEGLConfig(EGLDisplay display, EGLint nativeVisualId,
409 EGLint renderableType, EGLConfig* config) {
Mathias Agopiana4912602012-07-12 14:25:33 -0700410 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
411 // it is to be used with WIFI displays
Mathias Agopiana4912602012-07-12 14:25:33 -0700412 status_t err;
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700413 EGLint wantedAttribute;
414 EGLint wantedAttributeValue;
Mathias Agopianda27af92012-09-13 18:17:13 -0700415
Mathias Agopian722b98f2012-09-25 18:24:31 -0700416 EGLAttributeVector attribs;
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700417 if (renderableType) {
418 attribs[EGL_RENDERABLE_TYPE] = renderableType;
419 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
420 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT|EGL_PBUFFER_BIT;
421 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
422 attribs[EGL_RED_SIZE] = 8;
423 attribs[EGL_GREEN_SIZE] = 8;
424 attribs[EGL_BLUE_SIZE] = 8;
425 wantedAttribute = EGL_NONE;
426 wantedAttributeValue = EGL_NONE;
Mathias Agopian722b98f2012-09-25 18:24:31 -0700427
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700428 } else {
429 // if no renderable type specified, fallback to a simplified query
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700430 wantedAttribute = EGL_NATIVE_VISUAL_ID;
431 wantedAttributeValue = nativeVisualId;
432 }
433
434 err = selectConfigForAttribute(display, attribs, wantedAttribute,
Jesse Hallb65f32e2013-09-27 22:10:47 -0700435 wantedAttributeValue, config);
436 if (err == NO_ERROR) {
437 EGLint caveat;
438 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
439 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
440 }
441 return err;
Mathias Agopiana4912602012-07-12 14:25:33 -0700442}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800443
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700444class DispSyncSource : public VSyncSource, private DispSync::Callback {
445public:
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700446 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync) :
447 mValue(0),
448 mPhaseOffset(phaseOffset),
449 mTraceVsync(traceVsync),
450 mDispSync(dispSync) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700451
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700452 virtual ~DispSyncSource() {}
453
454 virtual void setVSyncEnabled(bool enable) {
455 // Do NOT lock the mutex here so as to avoid any mutex ordering issues
456 // with locking it in the onDispSyncEvent callback.
457 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700458 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700459 static_cast<DispSync::Callback*>(this));
460 if (err != NO_ERROR) {
461 ALOGE("error registering vsync callback: %s (%d)",
462 strerror(-err), err);
463 }
464 ATRACE_INT("VsyncOn", 1);
465 } else {
466 status_t err = mDispSync->removeEventListener(
467 static_cast<DispSync::Callback*>(this));
468 if (err != NO_ERROR) {
469 ALOGE("error unregistering vsync callback: %s (%d)",
470 strerror(-err), err);
471 }
472 ATRACE_INT("VsyncOn", 0);
473 }
474 }
475
476 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
477 Mutex::Autolock lock(mMutex);
478 mCallback = callback;
479 }
480
481private:
482 virtual void onDispSyncEvent(nsecs_t when) {
483 sp<VSyncSource::Callback> callback;
484 {
485 Mutex::Autolock lock(mMutex);
486 callback = mCallback;
487
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700488 if (mTraceVsync) {
489 mValue = (mValue + 1) % 2;
490 ATRACE_INT("VSYNC", mValue);
491 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700492 }
493
494 if (callback != NULL) {
495 callback->onVSyncEvent(when);
496 }
497 }
498
499 int mValue;
500
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700501 const nsecs_t mPhaseOffset;
502 const bool mTraceVsync;
503
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700504 DispSync* mDispSync;
505 sp<VSyncSource::Callback> mCallback;
506 Mutex mMutex;
507};
508
509void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700510 ALOGI( "SurfaceFlinger's main thread ready to run. "
511 "Initializing graphics H/W...");
512
Jesse Hallb65f32e2013-09-27 22:10:47 -0700513 status_t err;
Jesse Hall692c7232012-11-08 15:41:56 -0800514 Mutex::Autolock _l(mStateLock);
515
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700516 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700517 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
518 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700519
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700520 // Initialize the H/W composer object. There may or may not be an
521 // actual hardware composer underneath.
522 mHwc = new HWComposer(this,
523 *static_cast<HWComposer::EventHandler *>(this));
524
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700525 // First try to get an ES2 config
Jesse Hallb65f32e2013-09-27 22:10:47 -0700526 err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(), EGL_OPENGL_ES2_BIT,
527 &mEGLConfig);
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700528
Jesse Hallb65f32e2013-09-27 22:10:47 -0700529 if (err != NO_ERROR) {
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700530 // If ES2 fails, try ES1
Jesse Hallb65f32e2013-09-27 22:10:47 -0700531 err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(),
532 EGL_OPENGL_ES_BIT, &mEGLConfig);
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700533 }
534
Jesse Hallb65f32e2013-09-27 22:10:47 -0700535 if (err != NO_ERROR) {
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700536 // still didn't work, probably because we're on the emulator...
537 // try a simplified query
538 ALOGW("no suitable EGLConfig found, trying a simpler query");
Jesse Hallb65f32e2013-09-27 22:10:47 -0700539 err = selectEGLConfig(mEGLDisplay, mHwc->getVisualID(), 0, &mEGLConfig);
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700540 }
541
Jesse Hallb65f32e2013-09-27 22:10:47 -0700542 if (err != NO_ERROR) {
Mathias Agopian2185f8b2013-09-18 16:20:26 -0700543 // this EGL is too lame for android
544 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
545 }
Mathias Agopian875d8e12013-06-07 15:35:48 -0700546
547 // print some debugging info
548 EGLint r,g,b,a;
549 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_RED_SIZE, &r);
550 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_GREEN_SIZE, &g);
551 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_BLUE_SIZE, &b);
552 eglGetConfigAttrib(mEGLDisplay, mEGLConfig, EGL_ALPHA_SIZE, &a);
553 ALOGI("EGL informations:");
554 ALOGI("vendor : %s", eglQueryString(mEGLDisplay, EGL_VENDOR));
555 ALOGI("version : %s", eglQueryString(mEGLDisplay, EGL_VERSION));
556 ALOGI("extensions: %s", eglQueryString(mEGLDisplay, EGL_EXTENSIONS));
557 ALOGI("Client API: %s", eglQueryString(mEGLDisplay, EGL_CLIENT_APIS)?:"Not Supported");
558 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, mEGLConfig);
559
560 // get a RenderEngine for the given display / config (can't fail)
561 mRenderEngine = RenderEngine::create(mEGLDisplay, mEGLConfig);
562
563 // retrieve the EGL context that was selected/created
564 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700565
Mathias Agopianed985572013-03-22 00:24:39 -0700566 // figure out which format we got
567 eglGetConfigAttrib(mEGLDisplay, mEGLConfig,
568 EGL_NATIVE_VISUAL_ID, &mEGLNativeVisualId);
569
Mathias Agopianda27af92012-09-13 18:17:13 -0700570 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
571 "couldn't create EGLContext");
572
Mathias Agopiancde87a32012-09-13 14:09:01 -0700573 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700574 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700575 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700576 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700577 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700578 // All non-virtual displays are currently considered secure.
579 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800580 createBuiltinDisplayLocked(type);
581 wp<IBinder> token = mBuiltinDisplays[i];
582
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700583 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
584 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i, bq);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700585 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -0700586 type, allocateHwcDisplayId(type), isSecure, token,
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700587 fbs, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -0700588 mEGLConfig);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700589 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700590 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700591 // for displays other than the main display, so we always
592 // assume a connected display is unblanked.
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700593 ALOGD("marking display %d as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700594 hw->acquireScreen();
595 }
596 mDisplays.add(token, hw);
597 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700598 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700599
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700600 // make the GLContext current so that we can create textures when creating Layers
601 // (which may happens before we render something)
602 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
603
Mathias Agopian028508c2012-07-25 21:12:12 -0700604 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700605 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
606 vsyncPhaseOffsetNs, true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700607 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700608 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
609 sfVsyncPhaseOffsetNs, false);
610 mSFEventThread = new EventThread(sfVsyncSrc);
611 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700612
Jamie Gennisd1700752013-10-14 12:22:52 -0700613 mEventControlThread = new EventControlThread(this);
614 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
615
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700616 // set a fake vsync period if there is no HWComposer
617 if (mHwc->initCheck() != NO_ERROR) {
618 mPrimaryDispSync.setPeriod(16666667);
619 }
620
Mathias Agopian92a979a2012-08-02 18:32:23 -0700621 // initialize our drawing state
622 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700623
Andy McFadden13a082e2012-08-24 10:16:42 -0700624 // set initial conditions (e.g. unblank default device)
625 initializeDisplays();
626
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700627 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700628 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800629}
630
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700631int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700632 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700633 type : mHwc->allocateDisplayId();
634}
635
Mathias Agopiana67e4182012-06-19 17:26:12 -0700636void SurfaceFlinger::startBootAnim() {
637 // start boot animation
638 property_set("service.bootanim.exit", "0");
639 property_set("ctl.start", "bootanim");
640}
641
Mathias Agopian875d8e12013-06-07 15:35:48 -0700642size_t SurfaceFlinger::getMaxTextureSize() const {
643 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700644}
645
Mathias Agopian875d8e12013-06-07 15:35:48 -0700646size_t SurfaceFlinger::getMaxViewportDims() const {
647 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700648}
649
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800650// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800651
Jamie Gennis582270d2011-08-17 18:19:00 -0700652bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800653 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800654 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800655 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700656 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800657}
658
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700659status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800660 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700661 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800662 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700663 type = i;
664 break;
665 }
666 }
667
668 if (type < 0) {
669 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700670 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700671
672 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700673 float xdpi = hwc.getDpiX(type);
674 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700675
676 // TODO: Not sure if display density should handled by SF any longer
677 class Density {
678 static int getDensityFromProperty(char const* propName) {
679 char property[PROPERTY_VALUE_MAX];
680 int density = 0;
681 if (property_get(propName, property, NULL) > 0) {
682 density = atoi(property);
683 }
684 return density;
685 }
686 public:
687 static int getEmuDensity() {
688 return getDensityFromProperty("qemu.sf.lcd_density"); }
689 static int getBuildDensity() {
690 return getDensityFromProperty("ro.sf.lcd_density"); }
691 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700692
693 if (type == DisplayDevice::DISPLAY_PRIMARY) {
694 // The density of the device is provided by a build property
695 float density = Density::getBuildDensity() / 160.0f;
696 if (density == 0) {
697 // the build doesn't provide a density -- this is wrong!
698 // use xdpi instead
699 ALOGE("ro.sf.lcd_density must be defined as a build property");
700 density = xdpi / 160.0f;
701 }
702 if (Density::getEmuDensity()) {
703 // if "qemu.sf.lcd_density" is specified, it overrides everything
704 xdpi = ydpi = density = Density::getEmuDensity();
705 density /= 160.0f;
706 }
707 info->density = density;
708
709 // TODO: this needs to go away (currently needed only by webkit)
710 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
711 info->orientation = hw->getOrientation();
Mathias Agopian1604f772012-09-18 21:54:42 -0700712 } else {
713 // TODO: where should this value come from?
714 static const int TV_DENSITY = 213;
715 info->density = TV_DENSITY / 160.0f;
716 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700717 }
718
Mathias Agopian1604f772012-09-18 21:54:42 -0700719 info->w = hwc.getWidth(type);
720 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700721 info->xdpi = xdpi;
722 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700723 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700724
725 // All non-virtual displays are currently considered secure.
726 info->secure = true;
727
Mathias Agopian888c8222012-08-04 21:10:38 -0700728 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700729}
730
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800731// ----------------------------------------------------------------------------
732
733sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800734 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700735}
736
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800737// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800738
739void SurfaceFlinger::waitForEvent() {
740 mEventQueue.waitMessage();
741}
742
743void SurfaceFlinger::signalTransaction() {
744 mEventQueue.invalidate();
745}
746
747void SurfaceFlinger::signalLayerUpdate() {
748 mEventQueue.invalidate();
749}
750
751void SurfaceFlinger::signalRefresh() {
752 mEventQueue.refresh();
753}
754
755status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
756 nsecs_t reltime, uint32_t flags) {
757 return mEventQueue.postMessage(msg, reltime);
758}
759
760status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
761 nsecs_t reltime, uint32_t flags) {
762 status_t res = mEventQueue.postMessage(msg, reltime);
763 if (res == NO_ERROR) {
764 msg->wait();
765 }
766 return res;
767}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800768
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700769void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700770 do {
771 waitForEvent();
772 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800773}
774
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700775void SurfaceFlinger::enableHardwareVsync() {
776 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700777 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700778 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700779 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
780 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700781 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700782 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700783}
784
Jesse Hall948fe0c2013-10-14 12:56:09 -0700785void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700786 Mutex::Autolock _l(mHWVsyncLock);
787
Jesse Hall948fe0c2013-10-14 12:56:09 -0700788 if (makeAvailable) {
789 mHWVsyncAvailable = true;
790 } else if (!mHWVsyncAvailable) {
791 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
792 return;
793 }
794
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700795 const nsecs_t period =
796 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
797
798 mPrimaryDispSync.reset();
799 mPrimaryDispSync.setPeriod(period);
800
801 if (!mPrimaryHWVsyncEnabled) {
802 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700803 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
804 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700805 mPrimaryHWVsyncEnabled = true;
806 }
807}
808
Jesse Hall948fe0c2013-10-14 12:56:09 -0700809void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700810 Mutex::Autolock _l(mHWVsyncLock);
811 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700812 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
813 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700814 mPrimaryDispSync.endResync();
815 mPrimaryHWVsyncEnabled = false;
816 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700817 if (makeUnavailable) {
818 mHWVsyncAvailable = false;
819 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700820}
821
822void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700823 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700824
Jamie Gennisd1700752013-10-14 12:22:52 -0700825 { // Scope for the lock
826 Mutex::Autolock _l(mHWVsyncLock);
827 if (type == 0 && mPrimaryHWVsyncEnabled) {
828 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700829 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700830 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700831
832 if (needsHwVsync) {
833 enableHardwareVsync();
834 } else {
835 disableHardwareVsync(false);
836 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700837}
838
839void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
840 if (mEventThread == NULL) {
841 // This is a temporary workaround for b/7145521. A non-null pointer
842 // does not mean EventThread has finished initializing, so this
843 // is not a correct fix.
844 ALOGW("WARNING: EventThread not started, ignoring hotplug");
845 return;
846 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700847
Jesse Hall9e663de2013-08-16 14:28:37 -0700848 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700849 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800850 if (connected) {
851 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700852 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800853 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
854 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700855 }
856 setTransactionFlags(eDisplayTransactionNeeded);
857
Andy McFadden9e9689c2012-10-10 18:17:51 -0700858 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700859 }
Mathias Agopian86303202012-07-24 22:46:10 -0700860}
861
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700862void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700863 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700864 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700865}
866
Mathias Agopian4fec8732012-06-29 14:12:52 -0700867void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800868 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800869 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700870 case MessageQueue::TRANSACTION:
871 handleMessageTransaction();
872 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700873 case MessageQueue::INVALIDATE:
874 handleMessageTransaction();
875 handleMessageInvalidate();
876 signalRefresh();
877 break;
878 case MessageQueue::REFRESH:
879 handleMessageRefresh();
880 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800881 }
882}
883
Mathias Agopian4fec8732012-06-29 14:12:52 -0700884void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700885 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700886 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700887 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700888 }
889}
890
891void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700892 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700893 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700894}
895
896void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700897 ATRACE_CALL();
898 preComposition();
899 rebuildLayerStacks();
900 setUpHWComposer();
901 doDebugFlashRegions();
902 doComposition();
903 postComposition();
904}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700905
Mathias Agopiancd60f992012-08-16 16:28:27 -0700906void SurfaceFlinger::doDebugFlashRegions()
907{
908 // is debugging enabled
909 if (CC_LIKELY(!mDebugRegion))
910 return;
911
912 const bool repaintEverything = mRepaintEverything;
913 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
914 const sp<DisplayDevice>& hw(mDisplays[dpy]);
915 if (hw->canDraw()) {
916 // transform the dirty region into this screen's coordinate space
917 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
918 if (!dirtyRegion.isEmpty()) {
919 // redraw the whole screen
920 doComposeSurfaces(hw, Region(hw->bounds()));
921
922 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700923 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700924 RenderEngine& engine(getRenderEngine());
925 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
926
Mathias Agopiancd60f992012-08-16 16:28:27 -0700927 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700928 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700929 }
930 }
931 }
932
933 postFramebuffer();
934
935 if (mDebugRegion > 1) {
936 usleep(mDebugRegion * 1000);
937 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700938
939 HWComposer& hwc(getHwComposer());
940 if (hwc.initCheck() == NO_ERROR) {
941 status_t err = hwc.prepare();
942 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
943 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700944}
945
946void SurfaceFlinger::preComposition()
947{
948 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700949 const LayerVector& layers(mDrawingState.layersSortedByZ);
950 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700951 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700952 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700953 needExtraInvalidate = true;
954 }
955 }
956 if (needExtraInvalidate) {
957 signalLayerUpdate();
958 }
959}
960
961void SurfaceFlinger::postComposition()
962{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700963 const LayerVector& layers(mDrawingState.layersSortedByZ);
964 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700965 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700966 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700967 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800968
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700969 const HWComposer& hwc = getHwComposer();
970 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
971
972 if (presentFence->isValid()) {
973 if (mPrimaryDispSync.addPresentFence(presentFence)) {
974 enableHardwareVsync();
975 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700976 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700977 }
978 }
979
980 if (runningWithoutSyncFramework) {
981 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
982 if (hw->isScreenAcquired()) {
983 enableHardwareVsync();
984 }
985 }
986
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800987 if (mAnimCompositionPending) {
988 mAnimCompositionPending = false;
989
Jesse Halla9a1b002013-02-27 16:39:25 -0800990 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800991 mAnimFrameTracker.setActualPresentFence(presentFence);
992 } else {
993 // The HWC doesn't support present fences, so use the refresh
994 // timestamp instead.
995 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
996 mAnimFrameTracker.setActualPresentTime(presentTime);
997 }
998 mAnimFrameTracker.advanceFrame();
999 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001000}
1001
1002void SurfaceFlinger::rebuildLayerStacks() {
1003 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001004 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001005 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -07001006 mVisibleRegionsDirty = false;
1007 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001008
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001009 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001010 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001011 Region opaqueRegion;
1012 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -08001013 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -07001014 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001015 const Transform& tr(hw->getTransform());
1016 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001017 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001018 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001019 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001020
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001021 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001022 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001023 const sp<Layer>& layer(layers[i]);
1024 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001025 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -07001026 Region drawRegion(tr.transform(
1027 layer->visibleNonTransparentRegion));
1028 drawRegion.andSelf(bounds);
1029 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -07001030 layersSortedByZ.add(layer);
1031 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001032 }
1033 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001034 }
Mathias Agopian42977342012-08-05 00:40:46 -07001035 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -07001036 hw->undefinedRegion.set(bounds);
1037 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
1038 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001039 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001040 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001041}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001042
Mathias Agopiancd60f992012-08-16 16:28:27 -07001043void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -07001044 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1045 mDisplays[dpy]->beginFrame();
1046 }
1047
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001048 HWComposer& hwc(getHwComposer());
1049 if (hwc.initCheck() == NO_ERROR) {
1050 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -07001051 if (CC_UNLIKELY(mHwWorkListDirty)) {
1052 mHwWorkListDirty = false;
1053 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1054 sp<const DisplayDevice> hw(mDisplays[dpy]);
1055 const int32_t id = hw->getHwcDisplayId();
1056 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001057 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -07001058 hw->getVisibleLayersSortedByZ());
1059 const size_t count = currentLayers.size();
1060 if (hwc.createWorkList(id, count) == NO_ERROR) {
1061 HWComposer::LayerListIterator cur = hwc.begin(id);
1062 const HWComposer::LayerListIterator end = hwc.end(id);
1063 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001064 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001065 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -07001066 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -07001067 cur->setSkip(true);
1068 }
1069 }
1070 }
1071 }
1072 }
1073 }
1074
1075 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -07001076 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001077 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001078 const int32_t id = hw->getHwcDisplayId();
1079 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001080 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -07001081 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -07001082 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -07001083 HWComposer::LayerListIterator cur = hwc.begin(id);
1084 const HWComposer::LayerListIterator end = hwc.end(id);
1085 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
1086 /*
1087 * update the per-frame h/w composer data for each layer
1088 * and build the transparent region of the FB
1089 */
Mathias Agopian13127d82013-03-05 17:47:11 -08001090 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -07001091 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -07001092 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001093 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001094 }
Jamie Gennisa4310c82012-09-25 20:26:00 -07001095
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001096 status_t err = hwc.prepare();
1097 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -07001098
1099 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1100 sp<const DisplayDevice> hw(mDisplays[dpy]);
1101 hw->prepareFrame(hwc);
1102 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001103 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001104}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001105
Mathias Agopiancd60f992012-08-16 16:28:27 -07001106void SurfaceFlinger::doComposition() {
1107 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001108 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001109 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001110 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -07001111 if (hw->canDraw()) {
1112 // transform the dirty region into this screen's coordinate space
1113 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -08001114
1115 // repaint the framebuffer (if needed)
1116 doDisplayComposition(hw, dirtyRegion);
1117
Mathias Agopiancd60f992012-08-16 16:28:27 -07001118 hw->dirtyRegion.clear();
1119 hw->flip(hw->swapRegion);
1120 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001121 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001122 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001123 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001124 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001125 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001126}
1127
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001128void SurfaceFlinger::postFramebuffer()
1129{
Mathias Agopian841cde52012-03-01 15:44:37 -08001130 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001131
Mathias Agopiana44b0412011-10-16 18:46:35 -07001132 const nsecs_t now = systemTime();
1133 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001134
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001135 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001136 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001137 if (!hwc.supportsFramebufferTarget()) {
1138 // EGL spec says:
1139 // "surface must be bound to the calling thread's current context,
1140 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001141 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001142 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001143 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001144 }
1145
Mathias Agopian6da15f42013-09-25 20:40:07 -07001146 // make the default display current because the VirtualDisplayDevice code cannot
1147 // deal with dequeueBuffer() being called outside of the composition loop; however
1148 // the code below can call glFlush() which is allowed (and does in some case) call
1149 // dequeueBuffer().
1150 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1151
Mathias Agopian92a979a2012-08-02 18:32:23 -07001152 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001153 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001154 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001155 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001156 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001157 int32_t id = hw->getHwcDisplayId();
1158 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001159 HWComposer::LayerListIterator cur = hwc.begin(id);
1160 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001161 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001162 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001163 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001164 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001165 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001166 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001167 }
Jesse Hallef194142012-06-14 14:45:17 -07001168 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001169 }
1170
Mathias Agopiana44b0412011-10-16 18:46:35 -07001171 mLastSwapBufferTime = systemTime() - now;
1172 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001173
1174 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1175 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1176 logFrameStats();
1177 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001178}
1179
Mathias Agopian87baae12012-07-31 12:38:26 -07001180void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001181{
Mathias Agopian841cde52012-03-01 15:44:37 -08001182 ATRACE_CALL();
1183
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001184 // here we keep a copy of the drawing state (that is the state that's
1185 // going to be overwritten by handleTransactionLocked()) outside of
1186 // mStateLock so that the side-effects of the State assignment
1187 // don't happen with mStateLock held (which can cause deadlocks).
1188 State drawingState(mDrawingState);
1189
Mathias Agopianca4d3602011-05-19 15:38:14 -07001190 Mutex::Autolock _l(mStateLock);
1191 const nsecs_t now = systemTime();
1192 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001193
Mathias Agopianca4d3602011-05-19 15:38:14 -07001194 // Here we're guaranteed that some transaction flags are set
1195 // so we can call handleTransactionLocked() unconditionally.
1196 // We call getTransactionFlags(), which will also clear the flags,
1197 // with mStateLock held to guarantee that mCurrentState won't change
1198 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001199
Mathias Agopiane57f2922012-08-09 16:29:12 -07001200 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001201 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001202
Mathias Agopianca4d3602011-05-19 15:38:14 -07001203 mLastTransactionTime = systemTime() - now;
1204 mDebugInTransaction = 0;
1205 invalidateHwcGeometry();
1206 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001207}
1208
Mathias Agopian87baae12012-07-31 12:38:26 -07001209void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001210{
1211 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001212 const size_t count = currentLayers.size();
1213
1214 /*
1215 * Traversal of the children
1216 * (perform the transaction for each of them if needed)
1217 */
1218
Mathias Agopian3559b072012-08-15 13:46:03 -07001219 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001220 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001221 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001222 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1223 if (!trFlags) continue;
1224
1225 const uint32_t flags = layer->doTransaction(0);
1226 if (flags & Layer::eVisibleRegion)
1227 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001228 }
1229 }
1230
1231 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001232 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001233 */
1234
Mathias Agopiane57f2922012-08-09 16:29:12 -07001235 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001236 // here we take advantage of Vector's copy-on-write semantics to
1237 // improve performance by skipping the transaction entirely when
1238 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001239 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1240 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001241 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001242 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001243 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001244 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001245
1246 // find the displays that were removed
1247 // (ie: in drawing state but not in current state)
1248 // also handle displays that changed
1249 // (ie: displays that are in both lists)
1250 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001251 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1252 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001253 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001254 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001255 // Call makeCurrent() on the primary display so we can
1256 // be sure that nothing associated with this display
1257 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001258 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001259 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001260 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1261 if (hw != NULL)
1262 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001263 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001264 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001265 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001266 } else {
1267 ALOGW("trying to remove the main display");
1268 }
1269 } else {
1270 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001271 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001272 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001273 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001274 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001275 // recreating the DisplayDevice, so we just remove it
1276 // from the drawing state, so that it get re-added
1277 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001278 sp<DisplayDevice> hw(getDisplayDevice(display));
1279 if (hw != NULL)
1280 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001281 mDisplays.removeItem(display);
1282 mDrawingState.displays.removeItemsAt(i);
1283 dc--; i--;
1284 // at this point we must loop to the next item
1285 continue;
1286 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001287
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001288 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001289 if (disp != NULL) {
1290 if (state.layerStack != draw[i].layerStack) {
1291 disp->setLayerStack(state.layerStack);
1292 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001293 if ((state.orientation != draw[i].orientation)
1294 || (state.viewport != draw[i].viewport)
1295 || (state.frame != draw[i].frame))
1296 {
1297 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001298 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001299 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001300 }
1301 }
1302 }
1303
1304 // find displays that were added
1305 // (ie: in current state but not in drawing state)
1306 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001307 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001308 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001309
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001310 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001311 sp<IGraphicBufferProducer> producer;
1312 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
1313
Jesse Hall02d86562013-03-25 14:43:23 -07001314 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001315 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001316 // Virtual displays without a surface are dormant:
1317 // they have external state (layer stack, projection,
1318 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001319 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001320
Jesse Hall02d86562013-03-25 14:43:23 -07001321 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001322 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1323 *mHwc, hwcDisplayId, state.surface, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001324 state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001325
1326 dispSurface = vds;
1327 if (hwcDisplayId >= 0) {
1328 producer = vds;
1329 } else {
1330 // There won't be any interaction with HWC for this virtual display,
1331 // so the GLES driver can pass buffers directly to the sink.
1332 producer = state.surface;
1333 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001334 }
1335 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001336 ALOGE_IF(state.surface!=NULL,
1337 "adding a supported display, but rendering "
1338 "surface is provided (%p), ignoring it",
1339 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001340 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001341 // for supported (by hwc) displays we provide our
1342 // own rendering surface
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001343 dispSurface = new FramebufferSurface(*mHwc, state.type, bq);
1344 producer = bq;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001345 }
1346
1347 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001348 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001349 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hallffe1f192013-03-22 15:13:48 -07001350 state.type, hwcDisplayId, state.isSecure,
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001351 display, dispSurface, producer, mEGLConfig);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001352 hw->setLayerStack(state.layerStack);
1353 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001354 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001355 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001356 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001357 if (state.isVirtualDisplay()) {
1358 if (hwcDisplayId >= 0) {
1359 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1360 hw->getWidth(), hw->getHeight(),
1361 hw->getFormat());
1362 }
1363 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001364 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001365 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001366 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001367 }
1368 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001369 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001370 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371
Mathias Agopian84300952012-11-21 16:02:13 -08001372 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1373 // The transform hint might have changed for some layers
1374 // (either because a display has changed, or because a layer
1375 // as changed).
1376 //
1377 // Walk through all the layers in currentLayers,
1378 // and update their transform hint.
1379 //
1380 // If a layer is visible only on a single display, then that
1381 // display is used to calculate the hint, otherwise we use the
1382 // default display.
1383 //
1384 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1385 // the hint is set before we acquire a buffer from the surface texture.
1386 //
1387 // NOTE: layer transactions have taken place already, so we use their
1388 // drawing state. However, SurfaceFlinger's own transaction has not
1389 // happened yet, so we must use the current state layer list
1390 // (soon to become the drawing state list).
1391 //
1392 sp<const DisplayDevice> disp;
1393 uint32_t currentlayerStack = 0;
1394 for (size_t i=0; i<count; i++) {
1395 // NOTE: we rely on the fact that layers are sorted by
1396 // layerStack first (so we don't have to traverse the list
1397 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001398 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001399 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001400 if (i==0 || currentlayerStack != layerStack) {
1401 currentlayerStack = layerStack;
1402 // figure out if this layerstack is mirrored
1403 // (more than one display) if so, pick the default display,
1404 // if not, pick the only display it's on.
1405 disp.clear();
1406 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1407 sp<const DisplayDevice> hw(mDisplays[dpy]);
1408 if (hw->getLayerStack() == currentlayerStack) {
1409 if (disp == NULL) {
1410 disp = hw;
1411 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001412 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001413 break;
1414 }
1415 }
1416 }
1417 }
Chet Haase91d25932013-04-11 15:24:55 -07001418 if (disp == NULL) {
1419 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1420 // redraw after transform hint changes. See bug 8508397.
1421
1422 // could be null when this layer is using a layerStack
1423 // that is not visible on any display. Also can occur at
1424 // screen off/on times.
1425 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001426 }
Chet Haase91d25932013-04-11 15:24:55 -07001427 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001428 }
1429 }
1430
1431
Mathias Agopian3559b072012-08-15 13:46:03 -07001432 /*
1433 * Perform our own transaction if needed
1434 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001435
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001436 const LayerVector& layers(mDrawingState.layersSortedByZ);
1437 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001438 // layers have been added
1439 mVisibleRegionsDirty = true;
1440 }
1441
1442 // some layers might have been removed, so
1443 // we need to update the regions they're exposing.
1444 if (mLayersRemoved) {
1445 mLayersRemoved = false;
1446 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001447 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001448 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001449 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001450 if (currentLayers.indexOf(layer) < 0) {
1451 // this layer is not visible anymore
1452 // TODO: we could traverse the tree from front to back and
1453 // compute the actual visible region
1454 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001455 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001456 Region visibleReg = s.transform.transform(
1457 Region(Rect(s.active.w, s.active.h)));
1458 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001459 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001460 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001461 }
1462
1463 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001464}
1465
1466void SurfaceFlinger::commitTransaction()
1467{
1468 if (!mLayersPendingRemoval.isEmpty()) {
1469 // Notify removed layers now that they can't be drawn from
1470 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1471 mLayersPendingRemoval[i]->onRemoved();
1472 }
1473 mLayersPendingRemoval.clear();
1474 }
1475
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001476 // If this transaction is part of a window animation then the next frame
1477 // we composite should be considered an animation as well.
1478 mAnimCompositionPending = mAnimTransactionPending;
1479
Mathias Agopian4fec8732012-06-29 14:12:52 -07001480 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001481 mTransactionPending = false;
1482 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001483 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001484}
1485
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001487 const LayerVector& currentLayers, uint32_t layerStack,
1488 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489{
Mathias Agopian841cde52012-03-01 15:44:37 -08001490 ATRACE_CALL();
1491
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001492 Region aboveOpaqueLayers;
1493 Region aboveCoveredLayers;
1494 Region dirty;
1495
Mathias Agopian87baae12012-07-31 12:38:26 -07001496 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001497
1498 size_t i = currentLayers.size();
1499 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001500 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001501
1502 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001503 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001504
Jesse Hall01e29052013-02-19 16:13:35 -08001505 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001506 if (s.layerStack != layerStack)
1507 continue;
1508
Mathias Agopianab028732010-03-16 16:41:46 -07001509 /*
1510 * opaqueRegion: area of a surface that is fully opaque.
1511 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001512 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001513
1514 /*
1515 * visibleRegion: area of a surface that is visible on screen
1516 * and not fully transparent. This is essentially the layer's
1517 * footprint minus the opaque regions above it.
1518 * Areas covered by a translucent surface are considered visible.
1519 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001520 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001521
1522 /*
1523 * coveredRegion: area of a surface that is covered by all
1524 * visible regions above it (which includes the translucent areas).
1525 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001526 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001527
Jesse Halla8026d22012-09-25 13:25:04 -07001528 /*
1529 * transparentRegion: area of a surface that is hinted to be completely
1530 * transparent. This is only used to tell when the layer has no visible
1531 * non-transparent regions and can be removed from the layer list. It
1532 * does not affect the visibleRegion of this layer or any layers
1533 * beneath it. The hint may not be correct if apps don't respect the
1534 * SurfaceView restrictions (which, sadly, some don't).
1535 */
1536 Region transparentRegion;
1537
Mathias Agopianab028732010-03-16 16:41:46 -07001538
1539 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001540 if (CC_LIKELY(layer->isVisible())) {
Mathias Agopiana67932f2011-04-20 14:20:59 -07001541 const bool translucent = !layer->isOpaque();
Mathias Agopian5219a062013-02-26 16:37:53 -08001542 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001543 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001544 if (!visibleRegion.isEmpty()) {
1545 // Remove the transparent area from the visible region
1546 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001547 const Transform tr(s.transform);
1548 if (tr.transformed()) {
1549 if (tr.preserveRects()) {
1550 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001551 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001552 } else {
1553 // transformation too complex, can't do the
1554 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001555 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001556 }
1557 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001558 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001559 }
Mathias Agopianab028732010-03-16 16:41:46 -07001560 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001561
Mathias Agopianab028732010-03-16 16:41:46 -07001562 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001563 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001564 if (s.alpha==255 && !translucent &&
1565 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1566 // the opaque region is the layer's footprint
1567 opaqueRegion = visibleRegion;
1568 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001569 }
1570 }
1571
Mathias Agopianab028732010-03-16 16:41:46 -07001572 // Clip the covered region to the visible region
1573 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1574
1575 // Update aboveCoveredLayers for next (lower) layer
1576 aboveCoveredLayers.orSelf(visibleRegion);
1577
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001578 // subtract the opaque region covered by the layers above us
1579 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001580
1581 // compute this layer's dirty region
1582 if (layer->contentDirty) {
1583 // we need to invalidate the whole region
1584 dirty = visibleRegion;
1585 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001586 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001587 layer->contentDirty = false;
1588 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001589 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001590 * the exposed region consists of two components:
1591 * 1) what's VISIBLE now and was COVERED before
1592 * 2) what's EXPOSED now less what was EXPOSED before
1593 *
1594 * note that (1) is conservative, we start with the whole
1595 * visible region but only keep what used to be covered by
1596 * something -- which mean it may have been exposed.
1597 *
1598 * (2) handles areas that were not covered by anything but got
1599 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001600 */
Mathias Agopianab028732010-03-16 16:41:46 -07001601 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001602 const Region oldVisibleRegion = layer->visibleRegion;
1603 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001604 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1605 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001606 }
1607 dirty.subtractSelf(aboveOpaqueLayers);
1608
1609 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001610 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001611
Mathias Agopianab028732010-03-16 16:41:46 -07001612 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001613 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001614
Jesse Halla8026d22012-09-25 13:25:04 -07001615 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001616 layer->setVisibleRegion(visibleRegion);
1617 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001618 layer->setVisibleNonTransparentRegion(
1619 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001620 }
1621
Mathias Agopian87baae12012-07-31 12:38:26 -07001622 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001623}
1624
Mathias Agopian87baae12012-07-31 12:38:26 -07001625void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1626 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001627 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001628 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1629 if (hw->getLayerStack() == layerStack) {
1630 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001631 }
1632 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001633}
1634
1635void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001636{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001637 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001638
Mathias Agopian4fec8732012-06-29 14:12:52 -07001639 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001640 const LayerVector& layers(mDrawingState.layersSortedByZ);
1641 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001642 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001643 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001644 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001645 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001646 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001647 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001648
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001649 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001650}
1651
Mathias Agopianad456f92011-01-13 17:53:01 -08001652void SurfaceFlinger::invalidateHwcGeometry()
1653{
1654 mHwWorkListDirty = true;
1655}
1656
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001657
Mathias Agopiancd60f992012-08-16 16:28:27 -07001658void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001659 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001660{
Mathias Agopian87baae12012-07-31 12:38:26 -07001661 Region dirtyRegion(inDirtyRegion);
1662
Mathias Agopianb8a55602009-06-26 19:06:36 -07001663 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001664 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001665
Mathias Agopian42977342012-08-05 00:40:46 -07001666 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001667 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001668 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1669 // takes a rectangle, we must make sure to update that whole
1670 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001671 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001672 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001673 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001674 // We need to redraw the rectangle that will be updated
1675 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001676 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001677 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001678 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001679 } else {
1680 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001681 dirtyRegion.set(hw->bounds());
1682 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001683 }
1684 }
1685
Alan Viverette9c5a3332013-09-12 20:04:35 -07001686 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Mathias Agopianff2ed702013-09-01 21:36:12 -07001687 doComposeSurfaces(hw, dirtyRegion);
1688 } else {
1689 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001690 mat4 colorMatrix = mColorMatrix;
1691 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001692 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001693 }
1694 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001695 doComposeSurfaces(hw, dirtyRegion);
1696 engine.endGroup();
1697 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001698
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001699 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001700 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001701
1702 // swap buffers (presentation)
1703 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001704}
1705
Mathias Agopiancd60f992012-08-16 16:28:27 -07001706void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001707{
Mathias Agopian3f844832013-08-07 21:24:32 -07001708 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001709 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001710 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001711 HWComposer::LayerListIterator cur = hwc.begin(id);
1712 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001713
Jesse Halld05a17f2013-09-30 16:49:30 -07001714 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001715 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001716 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001717 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1718 hw->getDisplayName().string());
1719 return;
1720 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001721
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001722 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001723 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001724 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001725 // when using overlays, we assume a fully transparent framebuffer
1726 // NOTE: we could reduce how much we need to clear, for instance
1727 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001728 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001729 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001730 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001731 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001732 // we start with the whole screen area
1733 const Region bounds(hw->getBounds());
1734
1735 // we remove the scissor part
1736 // we're left with the letterbox region
1737 // (common case is that letterbox ends-up being empty)
1738 const Region letterbox(bounds.subtract(hw->getScissor()));
1739
1740 // compute the area to clear
1741 Region region(hw->undefinedRegion.merge(letterbox));
1742
1743 // but limit it to the dirty region
1744 region.andSelf(dirty);
1745
Mathias Agopianb9494d52012-04-18 02:28:45 -07001746 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001747 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001748 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001749 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001750 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001751 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001752
Mathias Agopian766dc492012-10-30 18:08:06 -07001753 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1754 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001755 // scissor on the main display. It should never be needed
1756 // anyways (though in theory it could since the API allows it).
1757 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001758 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001759 if (scissor != bounds) {
1760 // scissor doesn't match the screen's dimensions, so we
1761 // need to clear everything outside of it and enable
1762 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001763
Mathias Agopianf45c5102012-10-24 16:29:17 -07001764 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001765 const uint32_t height = hw->getHeight();
1766 engine.setScissor(scissor.left, height - scissor.bottom,
1767 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001768 }
1769 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001770 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001771
Mathias Agopian85d751c2012-08-29 16:59:24 -07001772 /*
1773 * and then, render the layers targeted at the framebuffer
1774 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001775
Mathias Agopian13127d82013-03-05 17:47:11 -08001776 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001777 const size_t count = layers.size();
1778 const Transform& tr = hw->getTransform();
1779 if (cur != end) {
1780 // we're using h/w composer
1781 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001782 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001783 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001784 if (!clip.isEmpty()) {
1785 switch (cur->getCompositionType()) {
1786 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001787 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001788 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1789 && i
Mathias Agopianac683022013-10-01 15:36:52 -07001790 && layer->isOpaque() && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001791 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001792 // never clear the very first layer since we're
1793 // guaranteed the FB is already cleared
1794 layer->clearWithOpenGL(hw, clip);
1795 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001796 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001797 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001798 case HWC_FRAMEBUFFER: {
1799 layer->draw(hw, clip);
1800 break;
1801 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001802 case HWC_FRAMEBUFFER_TARGET: {
1803 // this should not happen as the iterator shouldn't
1804 // let us get there.
1805 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%d)", i);
1806 break;
1807 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001808 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001809 }
1810 layer->setAcquireFence(hw, *cur);
1811 }
1812 } else {
1813 // we're not using h/w composer
1814 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001815 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001816 const Region clip(dirty.intersect(
1817 tr.transform(layer->visibleRegion)));
1818 if (!clip.isEmpty()) {
1819 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001820 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001821 }
1822 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001823
1824 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001825 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001826}
1827
Mathias Agopian3f844832013-08-07 21:24:32 -07001828void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001829 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001830 RenderEngine& engine(getRenderEngine());
1831 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001832}
1833
Mathias Agopianac9fa422013-02-11 16:40:36 -08001834void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1835 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001836 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001837 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001838{
Mathias Agopian96f08192010-06-02 23:28:45 -07001839 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001840 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001841
Mathias Agopian96f08192010-06-02 23:28:45 -07001842 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001843 Mutex::Autolock _l(mStateLock);
1844 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001845 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001846}
1847
Mathias Agopian3f844832013-08-07 21:24:32 -07001848status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001849 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001850 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001851 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001852 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001853 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001854 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001855 return NO_ERROR;
1856 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001857 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001858}
1859
Mathias Agopian3f844832013-08-07 21:24:32 -07001860uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t flags) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001861 return android_atomic_release_load(&mTransactionFlags);
1862}
1863
Mathias Agopian3f844832013-08-07 21:24:32 -07001864uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001865 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1866}
1867
Mathias Agopian3f844832013-08-07 21:24:32 -07001868uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001869 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1870 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001871 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001872 }
1873 return old;
1874}
1875
Mathias Agopian8b33f032012-07-24 20:43:54 -07001876void SurfaceFlinger::setTransactionState(
1877 const Vector<ComposerState>& state,
1878 const Vector<DisplayState>& displays,
1879 uint32_t flags)
1880{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001881 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001882 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001883 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001884
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001885 if (flags & eAnimation) {
1886 // For window updates that are part of an animation we must wait for
1887 // previous animation "frames" to be handled.
1888 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001889 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001890 if (CC_UNLIKELY(err != NO_ERROR)) {
1891 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001892 // caller after a few seconds.
1893 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1894 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001895 mAnimTransactionPending = false;
1896 break;
1897 }
1898 }
1899 }
1900
Mathias Agopiane57f2922012-08-09 16:29:12 -07001901 size_t count = displays.size();
1902 for (size_t i=0 ; i<count ; i++) {
1903 const DisplayState& s(displays[i]);
1904 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001905 }
1906
Mathias Agopiane57f2922012-08-09 16:29:12 -07001907 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001908 for (size_t i=0 ; i<count ; i++) {
1909 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001910 // Here we need to check that the interface we're given is indeed
1911 // one of our own. A malicious client could give us a NULL
1912 // IInterface, or one of its own or even one of our own but a
1913 // different type. All these situations would cause us to crash.
1914 //
1915 // NOTE: it would be better to use RTTI as we could directly check
1916 // that we have a Client*. however, RTTI is disabled in Android.
1917 if (s.client != NULL) {
1918 sp<IBinder> binder = s.client->asBinder();
1919 if (binder != NULL) {
1920 String16 desc(binder->getInterfaceDescriptor());
1921 if (desc == ISurfaceComposerClient::descriptor) {
1922 sp<Client> client( static_cast<Client *>(s.client.get()) );
1923 transactionFlags |= setClientStateLocked(client, s.state);
1924 }
1925 }
1926 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001927 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001928
Mathias Agopian386aa982011-11-07 21:58:03 -08001929 if (transactionFlags) {
1930 // this triggers the transaction
1931 setTransactionFlags(transactionFlags);
1932
1933 // if this is a synchronous transaction, wait for it to take effect
1934 // before returning.
1935 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001936 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001937 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001938 if (flags & eAnimation) {
1939 mAnimTransactionPending = true;
1940 }
1941 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001942 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1943 if (CC_UNLIKELY(err != NO_ERROR)) {
1944 // just in case something goes wrong in SF, return to the
1945 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001946 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1947 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001948 break;
1949 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001950 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001951 }
1952}
1953
Mathias Agopiane57f2922012-08-09 16:29:12 -07001954uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1955{
Jesse Hall9a143922012-10-04 16:29:19 -07001956 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1957 if (dpyIdx < 0)
1958 return 0;
1959
Mathias Agopiane57f2922012-08-09 16:29:12 -07001960 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001961 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001962 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001963 const uint32_t what = s.what;
1964 if (what & DisplayState::eSurfaceChanged) {
1965 if (disp.surface->asBinder() != s.surface->asBinder()) {
1966 disp.surface = s.surface;
1967 flags |= eDisplayTransactionNeeded;
1968 }
1969 }
1970 if (what & DisplayState::eLayerStackChanged) {
1971 if (disp.layerStack != s.layerStack) {
1972 disp.layerStack = s.layerStack;
1973 flags |= eDisplayTransactionNeeded;
1974 }
1975 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001976 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001977 if (disp.orientation != s.orientation) {
1978 disp.orientation = s.orientation;
1979 flags |= eDisplayTransactionNeeded;
1980 }
1981 if (disp.frame != s.frame) {
1982 disp.frame = s.frame;
1983 flags |= eDisplayTransactionNeeded;
1984 }
1985 if (disp.viewport != s.viewport) {
1986 disp.viewport = s.viewport;
1987 flags |= eDisplayTransactionNeeded;
1988 }
1989 }
1990 }
1991 return flags;
1992}
1993
1994uint32_t SurfaceFlinger::setClientStateLocked(
1995 const sp<Client>& client,
1996 const layer_state_t& s)
1997{
1998 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001999 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07002000 if (layer != 0) {
2001 const uint32_t what = s.what;
2002 if (what & layer_state_t::ePositionChanged) {
2003 if (layer->setPosition(s.x, s.y))
2004 flags |= eTraversalNeeded;
2005 }
2006 if (what & layer_state_t::eLayerChanged) {
2007 // NOTE: index needs to be calculated before we update the state
2008 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2009 if (layer->setLayer(s.z)) {
2010 mCurrentState.layersSortedByZ.removeAt(idx);
2011 mCurrentState.layersSortedByZ.add(layer);
2012 // we need traversal (state changed)
2013 // AND transaction (list changed)
2014 flags |= eTransactionNeeded|eTraversalNeeded;
2015 }
2016 }
2017 if (what & layer_state_t::eSizeChanged) {
2018 if (layer->setSize(s.w, s.h)) {
2019 flags |= eTraversalNeeded;
2020 }
2021 }
2022 if (what & layer_state_t::eAlphaChanged) {
2023 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
2024 flags |= eTraversalNeeded;
2025 }
2026 if (what & layer_state_t::eMatrixChanged) {
2027 if (layer->setMatrix(s.matrix))
2028 flags |= eTraversalNeeded;
2029 }
2030 if (what & layer_state_t::eTransparentRegionChanged) {
2031 if (layer->setTransparentRegionHint(s.transparentRegion))
2032 flags |= eTraversalNeeded;
2033 }
2034 if (what & layer_state_t::eVisibilityChanged) {
2035 if (layer->setFlags(s.flags, s.mask))
2036 flags |= eTraversalNeeded;
2037 }
2038 if (what & layer_state_t::eCropChanged) {
2039 if (layer->setCrop(s.crop))
2040 flags |= eTraversalNeeded;
2041 }
2042 if (what & layer_state_t::eLayerStackChanged) {
2043 // NOTE: index needs to be calculated before we update the state
2044 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
2045 if (layer->setLayerStack(s.layerStack)) {
2046 mCurrentState.layersSortedByZ.removeAt(idx);
2047 mCurrentState.layersSortedByZ.add(layer);
2048 // we need traversal (state changed)
2049 // AND transaction (list changed)
2050 flags |= eTransactionNeeded|eTraversalNeeded;
2051 }
2052 }
2053 }
2054 return flags;
2055}
2056
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002057status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07002058 const String8& name,
2059 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002060 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
2061 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002062{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002063 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002064 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002065 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002066 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002067 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07002068 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07002069
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002070 status_t result = NO_ERROR;
2071
2072 sp<Layer> layer;
2073
Mathias Agopian3165cc22012-08-08 19:42:09 -07002074 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
2075 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002076 result = createNormalLayer(client,
2077 name, w, h, flags, format,
2078 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002079 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07002080 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002081 result = createDimLayer(client,
2082 name, w, h, flags,
2083 handle, gbp, &layer);
2084 break;
2085 default:
2086 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002087 break;
2088 }
2089
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002090 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07002091 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07002092 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002093 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002094 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002095}
2096
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002097status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
2098 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
2099 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002100{
2101 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002102 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002103 case PIXEL_FORMAT_TRANSPARENT:
2104 case PIXEL_FORMAT_TRANSLUCENT:
2105 format = PIXEL_FORMAT_RGBA_8888;
2106 break;
2107 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07002108#ifdef NO_RGBX_8888
2109 format = PIXEL_FORMAT_RGB_565;
2110#else
Mathias Agopian8f105402010-04-05 18:01:24 -07002111 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07002112#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002113 break;
2114 }
2115
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07002116#ifdef NO_RGBX_8888
2117 if (format == PIXEL_FORMAT_RGBX_8888)
2118 format = PIXEL_FORMAT_RGBA_8888;
2119#endif
2120
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002121 *outLayer = new Layer(this, client, name, w, h, flags);
2122 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2123 if (err == NO_ERROR) {
2124 *handle = (*outLayer)->getHandle();
2125 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002126 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002127
2128 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2129 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002130}
2131
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002132status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2133 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2134 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002135{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002136 *outLayer = new LayerDim(this, client, name, w, h, flags);
2137 *handle = (*outLayer)->getHandle();
2138 *gbp = (*outLayer)->getBufferQueue();
2139 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002140}
2141
Mathias Agopianac9fa422013-02-11 16:40:36 -08002142status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002143{
Mathias Agopian67106042013-03-14 19:18:13 -07002144 // called by the window manager when it wants to remove a Layer
2145 status_t err = NO_ERROR;
2146 sp<Layer> l(client->getLayerUser(handle));
2147 if (l != NULL) {
2148 err = removeLayer(l);
2149 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2150 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002151 }
2152 return err;
2153}
2154
Mathias Agopian13127d82013-03-05 17:47:11 -08002155status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002156{
Mathias Agopian67106042013-03-14 19:18:13 -07002157 // called by ~LayerCleaner() when all references to the IBinder (handle)
2158 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002159 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002160 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002161 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002162 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002163 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002164 "error removing layer=%p (%s)", l.get(), strerror(-err));
2165 }
2166 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002167}
2168
Mathias Agopianb60314a2012-04-10 22:09:54 -07002169// ---------------------------------------------------------------------------
2170
Andy McFadden13a082e2012-08-24 10:16:42 -07002171void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002172 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002173 Vector<ComposerState> state;
2174 Vector<DisplayState> displays;
2175 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002176 d.what = DisplayState::eDisplayProjectionChanged |
2177 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002178 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002179 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002180 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002181 d.frame.makeInvalid();
2182 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002183 displays.add(d);
2184 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002185 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07002186
2187 const nsecs_t period =
2188 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2189 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002190}
2191
2192void SurfaceFlinger::initializeDisplays() {
2193 class MessageScreenInitialized : public MessageBase {
2194 SurfaceFlinger* flinger;
2195 public:
2196 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2197 virtual bool handler() {
2198 flinger->onInitializeDisplays();
2199 return true;
2200 }
2201 };
2202 sp<MessageBase> msg = new MessageScreenInitialized(this);
2203 postMessageAsync(msg); // we may be called from main thread, use async message
2204}
2205
2206
Mathias Agopiancde87a32012-09-13 14:09:01 -07002207void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002208 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2209 if (hw->isScreenAcquired()) {
2210 // this is expected, e.g. when power manager wakes up during boot
2211 ALOGD(" screen was previously acquired");
2212 return;
2213 }
2214
Mathias Agopian42977342012-08-05 00:40:46 -07002215 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002216 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002217 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002218 // built-in display, tell the HWC
2219 getHwComposer().acquire(type);
2220
2221 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2222 // FIXME: eventthread only knows about the main display right now
2223 mEventThread->onScreenAcquired();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07002224
Jesse Hall948fe0c2013-10-14 12:56:09 -07002225 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002226 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002227 }
Mathias Agopian20128302012-08-13 18:32:13 -07002228 mVisibleRegionsDirty = true;
2229 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002230}
2231
Mathias Agopiancde87a32012-09-13 14:09:01 -07002232void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002233 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2234 if (!hw->isScreenAcquired()) {
2235 ALOGD(" screen was previously released");
2236 return;
2237 }
2238
2239 hw->releaseScreen();
2240 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002241 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002242 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002243 disableHardwareVsync(true); // also cancels any in-progress resync
2244
Mathias Agopiancde87a32012-09-13 14:09:01 -07002245 // FIXME: eventthread only knows about the main display right now
2246 mEventThread->onScreenReleased();
2247 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002248
2249 // built-in display, tell the HWC
2250 getHwComposer().release(type);
2251 }
2252 mVisibleRegionsDirty = true;
2253 // from this point on, SF will stop drawing on this display
2254}
2255
2256void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2257 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002258 SurfaceFlinger& mFlinger;
2259 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002260 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002261 MessageScreenAcquired(SurfaceFlinger& flinger,
2262 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002263 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002264 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2265 if (hw == NULL) {
2266 ALOGE("Attempt to unblank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002267 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002268 ALOGW("Attempt to unblank virtual display");
2269 } else {
2270 mFlinger.onScreenAcquired(hw);
2271 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002272 return true;
2273 }
2274 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002275 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2276 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002277}
2278
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002279void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002280 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002281 SurfaceFlinger& mFlinger;
2282 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002283 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002284 MessageScreenReleased(SurfaceFlinger& flinger,
2285 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002286 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002287 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2288 if (hw == NULL) {
2289 ALOGE("Attempt to blank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002290 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002291 ALOGW("Attempt to blank virtual display");
2292 } else {
2293 mFlinger.onScreenReleased(hw);
2294 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002295 return true;
2296 }
2297 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002298 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2299 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002300}
2301
2302// ---------------------------------------------------------------------------
2303
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002304status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2305{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002306 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002307
Mathias Agopianbd115332013-04-18 16:41:04 -07002308 IPCThreadState* ipc = IPCThreadState::self();
2309 const int pid = ipc->getCallingPid();
2310 const int uid = ipc->getCallingUid();
2311 if ((uid != AID_SHELL) &&
2312 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002313 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002314 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002315 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002316 // Try to get the main lock, but don't insist if we can't
2317 // (this would indicate SF is stuck, but we want to be able to
2318 // print something in dumpsys).
2319 int retry = 3;
2320 while (mStateLock.tryLock()<0 && --retry>=0) {
2321 usleep(1000000);
2322 }
2323 const bool locked(retry >= 0);
2324 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002325 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002326 "SurfaceFlinger appears to be unresponsive, "
2327 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002328 }
2329
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002330 bool dumpAll = true;
2331 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002332 size_t numArgs = args.size();
2333 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002334 if ((index < numArgs) &&
2335 (args[index] == String16("--list"))) {
2336 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002337 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002338 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002339 }
2340
2341 if ((index < numArgs) &&
2342 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002343 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002344 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002345 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002346 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002347
2348 if ((index < numArgs) &&
2349 (args[index] == String16("--latency-clear"))) {
2350 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002351 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002352 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002353 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002354 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002355
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002356 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002357 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002358 }
2359
Mathias Agopian9795c422009-08-26 16:36:26 -07002360 if (locked) {
2361 mStateLock.unlock();
2362 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002363 }
2364 write(fd, result.string(), result.size());
2365 return NO_ERROR;
2366}
2367
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002368void SurfaceFlinger::listLayersLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002369 String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002370{
2371 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2372 const size_t count = currentLayers.size();
2373 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002374 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002375 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002376 }
2377}
2378
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002379void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002380 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002381{
2382 String8 name;
2383 if (index < args.size()) {
2384 name = String8(args[index]);
2385 index++;
2386 }
2387
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002388 const nsecs_t period =
2389 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2390 result.appendFormat("%lld\n", period);
2391
2392 if (name.isEmpty()) {
2393 mAnimFrameTracker.dump(result);
2394 } else {
2395 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2396 const size_t count = currentLayers.size();
2397 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002398 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002399 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002400 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002401 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002402 }
2403 }
2404}
2405
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002406void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002407 String8& result)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002408{
2409 String8 name;
2410 if (index < args.size()) {
2411 name = String8(args[index]);
2412 index++;
2413 }
2414
2415 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2416 const size_t count = currentLayers.size();
2417 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002418 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002419 if (name.isEmpty() || (name == layer->getName())) {
2420 layer->clearStats();
2421 }
2422 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002423
2424 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002425}
2426
Jamie Gennis6547ff42013-07-16 20:12:42 -07002427// This should only be called from the main thread. Otherwise it would need
2428// the lock and should use mCurrentState rather than mDrawingState.
2429void SurfaceFlinger::logFrameStats() {
2430 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2431 const size_t count = drawingLayers.size();
2432 for (size_t i=0 ; i<count ; i++) {
2433 const sp<Layer>& layer(drawingLayers[i]);
2434 layer->logFrameStats();
2435 }
2436
2437 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2438}
2439
Andy McFadden4803b742012-09-24 19:07:20 -07002440/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2441{
2442 static const char* config =
2443 " [sf"
2444#ifdef NO_RGBX_8888
2445 " NO_RGBX_8888"
2446#endif
2447#ifdef HAS_CONTEXT_PRIORITY
2448 " HAS_CONTEXT_PRIORITY"
2449#endif
2450#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2451 " NEVER_DEFAULT_TO_ASYNC_MODE"
2452#endif
2453#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2454 " TARGET_DISABLE_TRIPLE_BUFFERING"
2455#endif
2456 "]";
2457 result.append(config);
2458}
2459
Mathias Agopian74d211a2013-04-22 16:55:35 +02002460void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2461 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002462{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002463 bool colorize = false;
2464 if (index < args.size()
2465 && (args[index] == String16("--color"))) {
2466 colorize = true;
2467 index++;
2468 }
2469
2470 Colorizer colorizer(colorize);
2471
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002472 // figure out if we're stuck somewhere
2473 const nsecs_t now = systemTime();
2474 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2475 const nsecs_t inTransaction(mDebugInTransaction);
2476 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2477 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2478
2479 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002480 * Dump library configuration.
2481 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002482
2483 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002484 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002485 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002486 appendSfConfigString(result);
2487 appendUiConfigString(result);
2488 appendGuiConfigString(result);
2489 result.append("\n");
2490
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002491 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002492 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002493 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002494 result.append(SyncFeatures::getInstance().toString());
2495 result.append("\n");
2496
Andy McFadden4803b742012-09-24 19:07:20 -07002497 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002498 * Dump the visible layer list
2499 */
2500 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2501 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002502 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002503 result.appendFormat("Visible layers (count = %d)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002504 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002505 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002506 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002507 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002508 }
2509
2510 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002511 * Dump Display state
2512 */
2513
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002514 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002515 result.appendFormat("Displays (%d entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002516 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002517 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2518 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002519 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002520 }
2521
2522 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002523 * Dump SurfaceFlinger global state
2524 */
2525
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002526 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002527 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002528 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002529
Mathias Agopian888c8222012-08-04 21:10:38 -07002530 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002531 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002532
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002533 colorizer.bold(result);
2534 result.appendFormat("EGL implementation : %s\n",
2535 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2536 colorizer.reset(result);
2537 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002538 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002539
Mathias Agopian875d8e12013-06-07 15:35:48 -07002540 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002541
Mathias Agopian42977342012-08-05 00:40:46 -07002542 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002543 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002544 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002545 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002546 " last eglSwapBuffers() time: %f us\n"
2547 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002548 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002549 " refresh-rate : %f fps\n"
2550 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002551 " y-dpi : %f\n"
2552 " EGL_NATIVE_VISUAL_ID : %d\n"
2553 " gpu_to_cpu_unsupported : %d\n"
2554 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002555 mLastSwapBufferTime/1000.0,
2556 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002557 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002558 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2559 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002560 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
2561 mEGLNativeVisualId,
2562 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002563
Mathias Agopian74d211a2013-04-22 16:55:35 +02002564 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002565 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002566
Mathias Agopian74d211a2013-04-22 16:55:35 +02002567 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002568 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002569
2570 /*
2571 * VSYNC state
2572 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002573 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002574
2575 /*
2576 * Dump HWComposer state
2577 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002578 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002579 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002580 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002581 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002582 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002583 (mDebugDisableHWC || mDebugRegion || mDaltonize
2584 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002585 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002586
2587 /*
2588 * Dump gralloc state
2589 */
2590 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2591 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002592}
2593
Mathias Agopian13127d82013-03-05 17:47:11 -08002594const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002595SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002596 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002597 wp<IBinder> dpy;
2598 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2599 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2600 dpy = mDisplays.keyAt(i);
2601 break;
2602 }
2603 }
2604 if (dpy == NULL) {
2605 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2606 // Just use the primary display so we have something to return
2607 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2608 }
2609 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002610}
2611
Keun young Park63f165f2012-08-31 10:53:36 -07002612bool SurfaceFlinger::startDdmConnection()
2613{
2614 void* libddmconnection_dso =
2615 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2616 if (!libddmconnection_dso) {
2617 return false;
2618 }
2619 void (*DdmConnection_start)(const char* name);
2620 DdmConnection_start =
2621 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2622 if (!DdmConnection_start) {
2623 dlclose(libddmconnection_dso);
2624 return false;
2625 }
2626 (*DdmConnection_start)(getServiceName());
2627 return true;
2628}
2629
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002630status_t SurfaceFlinger::onTransact(
2631 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2632{
2633 switch (code) {
2634 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002635 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002636 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002637 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002638 case BLANK:
2639 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002640 {
2641 // codes that require permission check
2642 IPCThreadState* ipc = IPCThreadState::self();
2643 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002644 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002645 if ((uid != AID_GRAPHICS) &&
2646 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002647 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002648 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2649 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002650 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002651 break;
2652 }
2653 case CAPTURE_SCREEN:
2654 {
2655 // codes that require permission check
2656 IPCThreadState* ipc = IPCThreadState::self();
2657 const int pid = ipc->getCallingPid();
2658 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002659 if ((uid != AID_GRAPHICS) &&
2660 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002661 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002662 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2663 return PERMISSION_DENIED;
2664 }
2665 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002666 }
2667 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002668
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002669 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2670 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002671 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002672 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002673 IPCThreadState* ipc = IPCThreadState::self();
2674 const int pid = ipc->getCallingPid();
2675 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002676 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002677 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002678 return PERMISSION_DENIED;
2679 }
2680 int n;
2681 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002682 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002683 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002684 return NO_ERROR;
2685 case 1002: // SHOW_UPDATES
2686 n = data.readInt32();
2687 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002688 invalidateHwcGeometry();
2689 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002690 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002691 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002692 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002693 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002694 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002695 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002696 setTransactionFlags(
2697 eTransactionNeeded|
2698 eDisplayTransactionNeeded|
2699 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002700 return NO_ERROR;
2701 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002702 case 1006:{ // send empty update
2703 signalRefresh();
2704 return NO_ERROR;
2705 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002706 case 1008: // toggle use of hw composer
2707 n = data.readInt32();
2708 mDebugDisableHWC = n ? 1 : 0;
2709 invalidateHwcGeometry();
2710 repaintEverything();
2711 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002712 case 1009: // toggle use of transform hint
2713 n = data.readInt32();
2714 mDebugDisableTransformHint = n ? 1 : 0;
2715 invalidateHwcGeometry();
2716 repaintEverything();
2717 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002718 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002719 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002720 reply->writeInt32(0);
2721 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002722 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002723 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002724 return NO_ERROR;
2725 case 1013: {
2726 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002727 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2728 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002729 return NO_ERROR;
2730 }
2731 case 1014: {
2732 // daltonize
2733 n = data.readInt32();
2734 switch (n % 10) {
2735 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2736 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2737 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2738 }
2739 if (n >= 10) {
2740 mDaltonizer.setMode(Daltonizer::correction);
2741 } else {
2742 mDaltonizer.setMode(Daltonizer::simulation);
2743 }
2744 mDaltonize = n > 0;
2745 invalidateHwcGeometry();
2746 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002747 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002748 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002749 case 1015: {
2750 // apply a color matrix
2751 n = data.readInt32();
2752 mHasColorMatrix = n ? 1 : 0;
2753 if (n) {
2754 // color matrix is sent as mat3 matrix followed by vec3
2755 // offset, then packed into a mat4 where the last row is
2756 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002757 for (size_t i = 0 ; i < 4; i++) {
2758 for (size_t j = 0; j < 4; j++) {
2759 mColorMatrix[i][j] = data.readFloat();
2760 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002761 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002762 } else {
2763 mColorMatrix = mat4();
2764 }
2765 invalidateHwcGeometry();
2766 repaintEverything();
2767 return NO_ERROR;
2768 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002769 }
2770 }
2771 return err;
2772}
2773
Mathias Agopian53331da2011-08-22 21:44:41 -07002774void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002775 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002776 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002777}
2778
Mathias Agopian59119e62010-10-11 12:37:43 -07002779// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002780// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002781// ---------------------------------------------------------------------------
2782
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002783/* The code below is here to handle b/8734824
2784 *
2785 * We create a IGraphicBufferProducer wrapper that forwards all calls
2786 * to the calling binder thread, where they are executed. This allows
2787 * the calling thread to be reused (on the other side) and not
2788 * depend on having "enough" binder threads to handle the requests.
2789 *
2790 */
2791
2792class GraphicProducerWrapper : public BBinder, public MessageHandler {
2793 sp<IGraphicBufferProducer> impl;
2794 sp<Looper> looper;
2795 status_t result;
2796 bool exitPending;
2797 bool exitRequested;
2798 mutable Barrier barrier;
2799 volatile int32_t memoryBarrier;
2800 uint32_t code;
2801 Parcel const* data;
2802 Parcel* reply;
2803
2804 enum {
2805 MSG_API_CALL,
2806 MSG_EXIT
2807 };
2808
2809 /*
2810 * this is called by our "fake" BpGraphicBufferProducer. We package the
2811 * data and reply Parcel and forward them to the calling thread.
2812 */
2813 virtual status_t transact(uint32_t code,
2814 const Parcel& data, Parcel* reply, uint32_t flags) {
2815 this->code = code;
2816 this->data = &data;
2817 this->reply = reply;
2818 android_atomic_acquire_store(0, &memoryBarrier);
2819 if (exitPending) {
2820 // if we've exited, we run the message synchronously right here
2821 handleMessage(Message(MSG_API_CALL));
2822 } else {
2823 barrier.close();
2824 looper->sendMessage(this, Message(MSG_API_CALL));
2825 barrier.wait();
2826 }
2827 return NO_ERROR;
2828 }
2829
2830 /*
2831 * here we run on the binder calling thread. All we've got to do is
2832 * call the real BpGraphicBufferProducer.
2833 */
2834 virtual void handleMessage(const Message& message) {
2835 android_atomic_release_load(&memoryBarrier);
2836 if (message.what == MSG_API_CALL) {
2837 impl->asBinder()->transact(code, data[0], reply);
2838 barrier.open();
2839 } else if (message.what == MSG_EXIT) {
2840 exitRequested = true;
2841 }
2842 }
2843
2844public:
2845 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2846 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2847 exitPending(false), exitRequested(false) {
2848 }
2849
2850 status_t waitForResponse() {
2851 do {
2852 looper->pollOnce(-1);
2853 } while (!exitRequested);
2854 return result;
2855 }
2856
2857 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002858 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002859 exitPending = true;
2860 looper->sendMessage(this, Message(MSG_EXIT));
2861 }
2862};
2863
2864
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002865status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2866 const sp<IGraphicBufferProducer>& producer,
2867 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002868 uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002869
2870 if (CC_UNLIKELY(display == 0))
2871 return BAD_VALUE;
2872
2873 if (CC_UNLIKELY(producer == 0))
2874 return BAD_VALUE;
2875
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002876 // if we have secure windows on this display, never allow the screen capture
2877 // unless the producer interface is local (i.e.: we can take a screenshot for
2878 // ourselves).
2879 if (!producer->asBinder()->localBinder()) {
2880 Mutex::Autolock _l(mStateLock);
2881 sp<const DisplayDevice> hw(getDisplayDevice(display));
2882 if (hw->getSecureLayerVisible()) {
2883 ALOGW("FB is protected: PERMISSION_DENIED");
2884 return PERMISSION_DENIED;
2885 }
2886 }
2887
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002888 class MessageCaptureScreen : public MessageBase {
2889 SurfaceFlinger* flinger;
2890 sp<IBinder> display;
2891 sp<IGraphicBufferProducer> producer;
2892 uint32_t reqWidth, reqHeight;
2893 uint32_t minLayerZ,maxLayerZ;
2894 status_t result;
2895 public:
2896 MessageCaptureScreen(SurfaceFlinger* flinger,
2897 const sp<IBinder>& display,
2898 const sp<IGraphicBufferProducer>& producer,
2899 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002900 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002901 : flinger(flinger), display(display), producer(producer),
2902 reqWidth(reqWidth), reqHeight(reqHeight),
2903 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
2904 result(PERMISSION_DENIED)
2905 {
2906 }
2907 status_t getResult() const {
2908 return result;
2909 }
2910 virtual bool handler() {
2911 Mutex::Autolock _l(flinger->mStateLock);
2912 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002913 result = flinger->captureScreenImplLocked(hw,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002914 producer, reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002915 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002916 return true;
2917 }
2918 };
2919
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002920 // make sure to process transactions before screenshots -- a transaction
2921 // might already be pending but scheduled for VSYNC; this guarantees we
2922 // will handle it before the screenshot. When VSYNC finally arrives
2923 // the scheduled transaction will be a no-op. If no transactions are
2924 // scheduled at this time, this will end-up being a no-op as well.
2925 mEventQueue.invalidateTransactionNow();
2926
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002927 // this creates a "fake" BBinder which will serve as a "fake" remote
2928 // binder to receive the marshaled calls and forward them to the
2929 // real remote (a BpGraphicBufferProducer)
2930 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2931
2932 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2933 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002934 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002935 display, IGraphicBufferProducer::asInterface( wrapper ),
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002936 reqWidth, reqHeight, minLayerZ, maxLayerZ);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002937
2938 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002939 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002940 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002941 }
2942 return res;
2943}
2944
Mathias Agopian180f10d2013-04-10 22:55:41 -07002945
2946void SurfaceFlinger::renderScreenImplLocked(
2947 const sp<const DisplayDevice>& hw,
2948 uint32_t reqWidth, uint32_t reqHeight,
2949 uint32_t minLayerZ, uint32_t maxLayerZ,
2950 bool yswap)
2951{
2952 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002953 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002954
2955 // get screen geometry
2956 const uint32_t hw_w = hw->getWidth();
2957 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002958 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2959
2960 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002961 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002962
2963 // set-up our viewport
Mathias Agopian3f844832013-08-07 21:24:32 -07002964 engine.setViewportAndProjection(reqWidth, reqHeight, hw_w, hw_h, yswap);
2965 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002966
2967 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002968 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002969
2970 const LayerVector& layers( mDrawingState.layersSortedByZ );
2971 const size_t count = layers.size();
2972 for (size_t i=0 ; i<count ; ++i) {
2973 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002974 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002975 if (state.layerStack == hw->getLayerStack()) {
2976 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2977 if (layer->isVisible()) {
2978 if (filtering) layer->setFiltering(true);
2979 layer->draw(hw);
2980 if (filtering) layer->setFiltering(false);
2981 }
2982 }
2983 }
2984 }
2985
2986 // compositionComplete is needed for older driver
2987 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002988 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002989}
2990
2991
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002992status_t SurfaceFlinger::captureScreenImplLocked(
2993 const sp<const DisplayDevice>& hw,
2994 const sp<IGraphicBufferProducer>& producer,
2995 uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002996 uint32_t minLayerZ, uint32_t maxLayerZ)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002997{
2998 ATRACE_CALL();
2999
Mathias Agopian180f10d2013-04-10 22:55:41 -07003000 // get screen geometry
3001 const uint32_t hw_w = hw->getWidth();
3002 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08003003
Mathias Agopian180f10d2013-04-10 22:55:41 -07003004 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
3005 ALOGE("size mismatch (%d, %d) > (%d, %d)",
3006 reqWidth, reqHeight, hw_w, hw_h);
3007 return BAD_VALUE;
3008 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08003009
Mathias Agopian180f10d2013-04-10 22:55:41 -07003010 reqWidth = (!reqWidth) ? hw_w : reqWidth;
3011 reqHeight = (!reqHeight) ? hw_h : reqHeight;
3012
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003013 // create a surface (because we're a producer, and we need to
3014 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07003015 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003016 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07003017
3018 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003019 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07003020 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
3021 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07003022
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003023 int err = 0;
3024 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07003025 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003026 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
3027 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07003028
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003029 if (err == NO_ERROR) {
3030 ANativeWindowBuffer* buffer;
3031 /* TODO: Once we have the sync framework everywhere this can use
3032 * server-side waits on the fence that dequeueBuffer returns.
3033 */
3034 result = native_window_dequeue_buffer_and_wait(window, &buffer);
3035 if (result == NO_ERROR) {
3036 // create an EGLImage from the buffer so we can later
3037 // turn it into a texture
3038 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
3039 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
3040 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07003041 // this binds the given EGLImage as a framebuffer for the
3042 // duration of this scope.
3043 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
3044 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003045 // this will in fact render into our dequeued buffer
3046 // via an FBO, which means we didn't have to create
3047 // an EGLSurface and therefore we're not
3048 // dependent on the context's EGLConfig.
3049 renderScreenImplLocked(hw, reqWidth, reqHeight,
3050 minLayerZ, maxLayerZ, true);
Mathias Agopiand5556842013-09-19 17:08:37 -07003051
Andy McFadden2d8d1202013-10-09 16:38:02 -07003052 // Create a sync point and wait on it, so we know the buffer is
3053 // ready before we pass it along. We can't trivially call glFlush(),
3054 // so we use a wait flag instead.
3055 // TODO: pass a sync fd to queueBuffer() and let the consumer wait.
3056 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
3057 if (sync != EGL_NO_SYNC_KHR) {
3058 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
3059 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
3060 EGLint eglErr = eglGetError();
3061 eglDestroySyncKHR(mEGLDisplay, sync);
3062 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
3063 ALOGW("captureScreen: fence wait timed out");
3064 } else {
3065 ALOGW_IF(eglErr != EGL_SUCCESS,
3066 "captureScreen: error waiting on EGL fence: %#x", eglErr);
3067 }
3068 } else {
3069 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
3070 // not fatal
3071 }
3072
Mathias Agopiand5556842013-09-19 17:08:37 -07003073 if (DEBUG_SCREENSHOTS) {
3074 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3075 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3076 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3077 hw, minLayerZ, maxLayerZ);
3078 delete [] pixels;
3079 }
3080
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003081 } else {
3082 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3083 result = INVALID_OPERATION;
3084 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003085 // destroy our image
3086 eglDestroyImageKHR(mEGLDisplay, image);
3087 } else {
3088 result = BAD_VALUE;
3089 }
3090 window->queueBuffer(window, buffer, -1);
3091 }
3092 } else {
3093 result = BAD_VALUE;
3094 }
3095 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3096 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003097
Mathias Agopian74c40c02010-09-29 13:02:36 -07003098 return result;
3099}
3100
Mathias Agopiand5556842013-09-19 17:08:37 -07003101void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3102 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003103 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003104 for (size_t y=0 ; y<h ; y++) {
3105 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3106 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003107 if (p[x] != 0xFF000000) return;
3108 }
3109 }
3110 ALOGE("*** we just took a black screenshot ***\n"
3111 "requested minz=%d, maxz=%d, layerStack=%d",
3112 minLayerZ, maxLayerZ, hw->getLayerStack());
3113 const LayerVector& layers( mDrawingState.layersSortedByZ );
3114 const size_t count = layers.size();
3115 for (size_t i=0 ; i<count ; ++i) {
3116 const sp<Layer>& layer(layers[i]);
3117 const Layer::State& state(layer->getDrawingState());
3118 const bool visible = (state.layerStack == hw->getLayerStack())
3119 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3120 && (layer->isVisible());
3121 ALOGE("%c index=%d, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
3122 visible ? '+' : '-',
3123 i, layer->getName().string(), state.layerStack, state.z,
3124 layer->isVisible(), state.flags, state.alpha);
3125 }
3126 }
3127}
3128
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003129// ---------------------------------------------------------------------------
3130
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003131SurfaceFlinger::LayerVector::LayerVector() {
3132}
3133
3134SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003135 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003136}
3137
3138int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3139 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003140{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003141 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003142 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3143 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003144
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003145 uint32_t ls = l->getCurrentState().layerStack;
3146 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003147 if (ls != rs)
3148 return ls - rs;
3149
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003150 uint32_t lz = l->getCurrentState().z;
3151 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003152 if (lz != rz)
3153 return lz - rz;
3154
3155 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003156}
3157
3158// ---------------------------------------------------------------------------
3159
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003160SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
3161 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003162}
3163
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003164SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08003165 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003166 viewport.makeInvalid();
3167 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003168}
3169
3170// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003171
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003172}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003173
3174
3175#if defined(__gl_h_)
3176#error "don't include gl/gl.h in this file"
3177#endif
3178
3179#if defined(__gl2_h_)
3180#error "don't include gl2/gl2.h in this file"
3181#endif