blob: 0142992a86d1bef9db32ce26be72b426dff6df4d [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>
Greg Hackmann86efcc02014-03-07 12:44:02 -080024#include <inttypes.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070025
26#include <EGL/egl.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027
28#include <cutils/log.h>
29#include <cutils/properties.h>
30
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070031#include <binder/IPCThreadState.h>
32#include <binder/IServiceManager.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070033#include <binder/MemoryHeapBase.h>
Mathias Agopian99b49842011-06-27 16:05:52 -070034#include <binder/PermissionCache.h>
Mathias Agopian7303c6b2009-07-02 18:11:53 -070035
Mathias Agopianc666cae2012-07-25 18:56:13 -070036#include <ui/DisplayInfo.h>
37
Mathias Agopian921e6ac2012-07-23 23:11:29 -070038#include <gui/BitTube.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070039#include <gui/BufferQueue.h>
Andy McFadden4803b742012-09-24 19:07:20 -070040#include <gui/GuiConfig.h>
Jamie Gennis1a4d8832012-08-02 20:11:05 -070041#include <gui/IDisplayEventConnection.h>
Mathias Agopiane3c697f2013-02-14 17:11:02 -080042#include <gui/Surface.h>
Jamie Gennis392edd82012-11-29 23:26:29 -080043#include <gui/GraphicBufferAlloc.h>
Mathias Agopian921e6ac2012-07-23 23:11:29 -070044
45#include <ui/GraphicBufferAllocator.h>
46#include <ui/PixelFormat.h>
Andy McFadden4803b742012-09-24 19:07:20 -070047#include <ui/UiConfig.h>
Mathias Agopiand0566bc2011-11-17 17:49:17 -080048
Mathias Agopiancde87a32012-09-13 14:09:01 -070049#include <utils/misc.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050#include <utils/String8.h>
51#include <utils/String16.h>
52#include <utils/StopWatch.h>
Jamie Gennis1c8e95c2012-02-23 19:27:23 -080053#include <utils/Trace.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054
Mathias Agopian921e6ac2012-07-23 23:11:29 -070055#include <private/android_filesystem_config.h>
Mathias Agopianca088332013-03-28 17:44:13 -070056#include <private/gui/SyncFeatures.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057
Mathias Agopian3e25fd82013-04-22 17:52:16 +020058#include "Client.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059#include "clz.h"
Mathias Agopian3e25fd82013-04-22 17:52:16 +020060#include "Colorizer.h"
Mathias Agopian90ac7992012-02-25 18:48:35 -080061#include "DdmConnection.h"
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -070062#include "DisplayDevice.h"
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070063#include "DispSync.h"
Jamie Gennisd1700752013-10-14 12:22:52 -070064#include "EventControlThread.h"
Mathias Agopiand0566bc2011-11-17 17:49:17 -080065#include "EventThread.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066#include "Layer.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067#include "LayerDim.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068#include "SurfaceFlinger.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069
Mathias Agopiana4912602012-07-12 14:25:33 -070070#include "DisplayHardware/FramebufferSurface.h"
Mathias Agopiana350ff92010-08-10 17:14:02 -070071#include "DisplayHardware/HWComposer.h"
Jesse Hall99c7dbb2013-03-14 14:29:29 -070072#include "DisplayHardware/VirtualDisplaySurface.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073
Mathias Agopianff2ed702013-09-01 21:36:12 -070074#include "Effects/Daltonizer.h"
75
Mathias Agopian875d8e12013-06-07 15:35:48 -070076#include "RenderEngine/RenderEngine.h"
Mathias Agopianff2ed702013-09-01 21:36:12 -070077#include <cutils/compiler.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070078
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079#define DISPLAY_COUNT 1
80
Mathias Agopianfee2b462013-07-03 12:34:01 -070081/*
82 * DEBUG_SCREENSHOTS: set to true to check that screenshots are not all
83 * black pixels.
84 */
85#define DEBUG_SCREENSHOTS false
86
Mathias Agopianca088332013-03-28 17:44:13 -070087EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
88
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089namespace android {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -070090
91// This works around the lack of support for the sync framework on some
92// devices.
93#ifdef RUNNING_WITHOUT_SYNC_FRAMEWORK
94static const bool runningWithoutSyncFramework = true;
95#else
96static const bool runningWithoutSyncFramework = false;
97#endif
98
99// This is the phase offset in nanoseconds of the software vsync event
100// relative to the vsync event reported by HWComposer. The software vsync
101// event is when SurfaceFlinger and Choreographer-based applications run each
102// frame.
103//
104// This phase offset allows adjustment of the minimum latency from application
105// wake-up (by Choregographer) time to the time at which the resulting window
106// image is displayed. This value may be either positive (after the HW vsync)
107// or negative (before the HW vsync). Setting it to 0 will result in a
108// minimum latency of two vsync periods because the app and SurfaceFlinger
109// will run just after the HW vsync. Setting it to a positive number will
110// result in the minimum latency being:
111//
112// (2 * VSYNC_PERIOD - (vsyncPhaseOffsetNs % VSYNC_PERIOD))
113//
114// Note that reducing this latency makes it more likely for the applications
115// to not have their window content image ready in time. When this happens
116// the latency will end up being an additional vsync period, and animations
117// will hiccup. Therefore, this latency should be tuned somewhat
118// conservatively (or at least with awareness of the trade-off being made).
119static const int64_t vsyncPhaseOffsetNs = VSYNC_EVENT_PHASE_OFFSET_NS;
120
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700121// This is the phase offset at which SurfaceFlinger's composition runs.
122static const int64_t sfVsyncPhaseOffsetNs = SF_VSYNC_EVENT_PHASE_OFFSET_NS;
123
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124// ---------------------------------------------------------------------------
125
Mathias Agopian99b49842011-06-27 16:05:52 -0700126const String16 sHardwareTest("android.permission.HARDWARE_TEST");
127const String16 sAccessSurfaceFlinger("android.permission.ACCESS_SURFACE_FLINGER");
128const String16 sReadFramebuffer("android.permission.READ_FRAME_BUFFER");
129const String16 sDump("android.permission.DUMP");
130
131// ---------------------------------------------------------------------------
132
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133SurfaceFlinger::SurfaceFlinger()
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700134 : BnSurfaceComposer(),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 mTransactionFlags(0),
Jamie Gennis2d5e2302012-10-15 18:24:43 -0700136 mTransactionPending(false),
137 mAnimTransactionPending(false),
Mathias Agopian076b1cc2009-04-10 14:24:30 -0700138 mLayersRemoved(false),
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700139 mRepaintEverything(0),
Mathias Agopian875d8e12013-06-07 15:35:48 -0700140 mRenderEngine(NULL),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 mBootTime(systemTime()),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142 mVisibleRegionsDirty(false),
Mathias Agopiana350ff92010-08-10 17:14:02 -0700143 mHwWorkListDirty(false),
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800144 mAnimCompositionPending(false),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145 mDebugRegion(0),
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700146 mDebugDDMS(0),
Mathias Agopian73d3ba92010-09-22 18:58:01 -0700147 mDebugDisableHWC(0),
Mathias Agopiana4583642011-08-23 18:03:18 -0700148 mDebugDisableTransformHint(0),
Mathias Agopian9795c422009-08-26 16:36:26 -0700149 mDebugInSwapBuffers(0),
150 mLastSwapBufferTime(0),
151 mDebugInTransaction(0),
152 mLastTransactionTime(0),
Mathias Agopianff2ed702013-09-01 21:36:12 -0700153 mBootFinished(false),
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700154 mPrimaryHWVsyncEnabled(false),
Jesse Hall948fe0c2013-10-14 12:56:09 -0700155 mHWVsyncAvailable(false),
Alan Viverette9c5a3332013-09-12 20:04:35 -0700156 mDaltonize(false),
157 mHasColorMatrix(false)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158{
Steve Blocka19954a2012-01-04 20:05:49 +0000159 ALOGI("SurfaceFlinger is starting");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160
161 // debugging stuff...
162 char value[PROPERTY_VALUE_MAX];
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700163
Mathias Agopianb4b17302013-03-20 18:36:41 -0700164 property_get("ro.bq.gpu_to_cpu_unsupported", value, "0");
Mathias Agopian50210b92013-03-21 21:13:21 -0700165 mGpuToCpuSupported = !atoi(value);
Mathias Agopianb4b17302013-03-20 18:36:41 -0700166
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 property_get("debug.sf.showupdates", value, "0");
168 mDebugRegion = atoi(value);
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700169
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700170 property_get("debug.sf.ddms", value, "0");
171 mDebugDDMS = atoi(value);
172 if (mDebugDDMS) {
Keun young Park63f165f2012-08-31 10:53:36 -0700173 if (!startDdmConnection()) {
174 // start failed, and DDMS debugging not enabled
175 mDebugDDMS = 0;
176 }
Mathias Agopian8afb7e32011-08-15 20:44:40 -0700177 }
Mathias Agopianc1d359d2012-08-04 20:09:03 -0700178 ALOGI_IF(mDebugRegion, "showupdates enabled");
179 ALOGI_IF(mDebugDDMS, "DDMS debugging enabled");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180}
181
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800182void SurfaceFlinger::onFirstRef()
183{
184 mEventQueue.init(this);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800185}
186
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800187SurfaceFlinger::~SurfaceFlinger()
188{
Mathias Agopiana4912602012-07-12 14:25:33 -0700189 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
190 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
191 eglTerminate(display);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192}
193
Dan Stozac7014012014-02-14 15:03:43 -0800194void SurfaceFlinger::binderDied(const wp<IBinder>& /* who */)
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800195{
196 // the window manager died on us. prepare its eulogy.
197
Andy McFadden13a082e2012-08-24 10:16:42 -0700198 // restore initial conditions (default device unblank, etc)
199 initializeDisplays();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800200
201 // restart the boot-animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700202 startBootAnim();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800203}
204
Mathias Agopian7e27f052010-05-28 14:22:23 -0700205sp<ISurfaceComposerClient> SurfaceFlinger::createConnection()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206{
Mathias Agopian96f08192010-06-02 23:28:45 -0700207 sp<ISurfaceComposerClient> bclient;
208 sp<Client> client(new Client(this));
209 status_t err = client->initCheck();
210 if (err == NO_ERROR) {
211 bclient = client;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 return bclient;
214}
215
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700216sp<IBinder> SurfaceFlinger::createDisplay(const String8& displayName,
217 bool secure)
Mathias Agopiane57f2922012-08-09 16:29:12 -0700218{
219 class DisplayToken : public BBinder {
220 sp<SurfaceFlinger> flinger;
221 virtual ~DisplayToken() {
222 // no more references, this display must be terminated
223 Mutex::Autolock _l(flinger->mStateLock);
224 flinger->mCurrentState.displays.removeItem(this);
225 flinger->setTransactionFlags(eDisplayTransactionNeeded);
226 }
227 public:
228 DisplayToken(const sp<SurfaceFlinger>& flinger)
229 : flinger(flinger) {
230 }
231 };
232
233 sp<BBinder> token = new DisplayToken(this);
234
235 Mutex::Autolock _l(mStateLock);
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700236 DisplayDeviceState info(DisplayDevice::DISPLAY_VIRTUAL);
Andy McFadden8dfa92f2012-09-17 18:27:17 -0700237 info.displayName = displayName;
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700238 info.isSecure = secure;
Mathias Agopiane57f2922012-08-09 16:29:12 -0700239 mCurrentState.displays.add(token, info);
240
241 return token;
242}
243
Jesse Hall6c913be2013-08-08 12:15:49 -0700244void SurfaceFlinger::destroyDisplay(const sp<IBinder>& display) {
245 Mutex::Autolock _l(mStateLock);
246
247 ssize_t idx = mCurrentState.displays.indexOfKey(display);
248 if (idx < 0) {
249 ALOGW("destroyDisplay: invalid display token");
250 return;
251 }
252
253 const DisplayDeviceState& info(mCurrentState.displays.valueAt(idx));
254 if (!info.isVirtualDisplay()) {
255 ALOGE("destroyDisplay called for non-virtual display");
256 return;
257 }
258
259 mCurrentState.displays.removeItemsAt(idx);
260 setTransactionFlags(eDisplayTransactionNeeded);
261}
262
Jesse Hall692c7232012-11-08 15:41:56 -0800263void SurfaceFlinger::createBuiltinDisplayLocked(DisplayDevice::DisplayType type) {
264 ALOGW_IF(mBuiltinDisplays[type],
265 "Overwriting display token for display type %d", type);
266 mBuiltinDisplays[type] = new BBinder();
267 DisplayDeviceState info(type);
268 // All non-virtual displays are currently considered secure.
269 info.isSecure = true;
270 mCurrentState.displays.add(mBuiltinDisplays[type], info);
271}
272
Mathias Agopiane57f2922012-08-09 16:29:12 -0700273sp<IBinder> SurfaceFlinger::getBuiltInDisplay(int32_t id) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700274 if (uint32_t(id) >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700275 ALOGE("getDefaultDisplay: id=%d is not a valid default display id", id);
276 return NULL;
277 }
Jesse Hall692c7232012-11-08 15:41:56 -0800278 return mBuiltinDisplays[id];
Mathias Agopiane57f2922012-08-09 16:29:12 -0700279}
280
Jamie Gennis9a78c902011-01-12 18:30:40 -0800281sp<IGraphicBufferAlloc> SurfaceFlinger::createGraphicBufferAlloc()
282{
283 sp<GraphicBufferAlloc> gba(new GraphicBufferAlloc());
284 return gba;
285}
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700286
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800287void SurfaceFlinger::bootFinished()
288{
289 const nsecs_t now = systemTime();
290 const nsecs_t duration = now - mBootTime;
Steve Blocka19954a2012-01-04 20:05:49 +0000291 ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
Mathias Agopian3330b202009-10-05 17:07:12 -0700292 mBootFinished = true;
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700293
294 // wait patiently for the window manager death
295 const String16 name("window");
296 sp<IBinder> window(defaultServiceManager()->getService(name));
297 if (window != 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700298 window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
Mathias Agopian1f339ff2011-07-01 17:08:43 -0700299 }
300
301 // stop boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700302 // formerly we would just kill the process, but we now ask it to exit so it
303 // can choose where to stop the animation.
304 property_set("service.bootanim.exit", "1");
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800305}
306
Mathias Agopian3f844832013-08-07 21:24:32 -0700307void SurfaceFlinger::deleteTextureAsync(uint32_t texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700308 class MessageDestroyGLTexture : public MessageBase {
Mathias Agopian3f844832013-08-07 21:24:32 -0700309 RenderEngine& engine;
310 uint32_t texture;
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700311 public:
Mathias Agopian3f844832013-08-07 21:24:32 -0700312 MessageDestroyGLTexture(RenderEngine& engine, uint32_t texture)
313 : engine(engine), texture(texture) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700314 }
315 virtual bool handler() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700316 engine.deleteTextures(1, &texture);
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700317 return true;
318 }
319 };
Mathias Agopian3f844832013-08-07 21:24:32 -0700320 postMessageAsync(new MessageDestroyGLTexture(getRenderEngine(), texture));
Mathias Agopian921e6ac2012-07-23 23:11:29 -0700321}
322
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700323class DispSyncSource : public VSyncSource, private DispSync::Callback {
324public:
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700325 DispSyncSource(DispSync* dispSync, nsecs_t phaseOffset, bool traceVsync) :
326 mValue(0),
327 mPhaseOffset(phaseOffset),
328 mTraceVsync(traceVsync),
329 mDispSync(dispSync) {}
Mathias Agopiana4912602012-07-12 14:25:33 -0700330
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700331 virtual ~DispSyncSource() {}
332
333 virtual void setVSyncEnabled(bool enable) {
334 // Do NOT lock the mutex here so as to avoid any mutex ordering issues
335 // with locking it in the onDispSyncEvent callback.
336 if (enable) {
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700337 status_t err = mDispSync->addEventListener(mPhaseOffset,
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700338 static_cast<DispSync::Callback*>(this));
339 if (err != NO_ERROR) {
340 ALOGE("error registering vsync callback: %s (%d)",
341 strerror(-err), err);
342 }
343 ATRACE_INT("VsyncOn", 1);
344 } else {
345 status_t err = mDispSync->removeEventListener(
346 static_cast<DispSync::Callback*>(this));
347 if (err != NO_ERROR) {
348 ALOGE("error unregistering vsync callback: %s (%d)",
349 strerror(-err), err);
350 }
351 ATRACE_INT("VsyncOn", 0);
352 }
353 }
354
355 virtual void setCallback(const sp<VSyncSource::Callback>& callback) {
356 Mutex::Autolock lock(mMutex);
357 mCallback = callback;
358 }
359
360private:
361 virtual void onDispSyncEvent(nsecs_t when) {
362 sp<VSyncSource::Callback> callback;
363 {
364 Mutex::Autolock lock(mMutex);
365 callback = mCallback;
366
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700367 if (mTraceVsync) {
368 mValue = (mValue + 1) % 2;
369 ATRACE_INT("VSYNC", mValue);
370 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700371 }
372
373 if (callback != NULL) {
374 callback->onVSyncEvent(when);
375 }
376 }
377
378 int mValue;
379
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700380 const nsecs_t mPhaseOffset;
381 const bool mTraceVsync;
382
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700383 DispSync* mDispSync;
384 sp<VSyncSource::Callback> mCallback;
385 Mutex mMutex;
386};
387
388void SurfaceFlinger::init() {
Mathias Agopiana4912602012-07-12 14:25:33 -0700389 ALOGI( "SurfaceFlinger's main thread ready to run. "
390 "Initializing graphics H/W...");
391
Jesse Hallb65f32e2013-09-27 22:10:47 -0700392 status_t err;
Jesse Hall692c7232012-11-08 15:41:56 -0800393 Mutex::Autolock _l(mStateLock);
394
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700395 // initialize EGL for the default display
Jesse Hall34a09ba2012-07-29 22:35:34 -0700396 mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
397 eglInitialize(mEGLDisplay, NULL, NULL);
Mathias Agopiana4912602012-07-12 14:25:33 -0700398
Andy McFaddenb0d1dd32012-09-10 14:08:09 -0700399 // Initialize the H/W composer object. There may or may not be an
400 // actual hardware composer underneath.
401 mHwc = new HWComposer(this,
402 *static_cast<HWComposer::EventHandler *>(this));
403
Mathias Agopian875d8e12013-06-07 15:35:48 -0700404 // get a RenderEngine for the given display / config (can't fail)
Jesse Hall05f8c702013-12-23 20:44:38 -0800405 mRenderEngine = RenderEngine::create(mEGLDisplay, mHwc->getVisualID());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700406
407 // retrieve the EGL context that was selected/created
408 mEGLContext = mRenderEngine->getEGLContext();
Mathias Agopiana4912602012-07-12 14:25:33 -0700409
Mathias Agopianda27af92012-09-13 18:17:13 -0700410 LOG_ALWAYS_FATAL_IF(mEGLContext == EGL_NO_CONTEXT,
411 "couldn't create EGLContext");
412
Mathias Agopiancde87a32012-09-13 14:09:01 -0700413 // initialize our non-virtual displays
Jesse Hall9e663de2013-08-16 14:28:37 -0700414 for (size_t i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Mathias Agopianf5a33922012-09-19 18:16:22 -0700415 DisplayDevice::DisplayType type((DisplayDevice::DisplayType)i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700416 // set-up the displays that are already connected
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700417 if (mHwc->isConnected(i) || type==DisplayDevice::DISPLAY_PRIMARY) {
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700418 // All non-virtual displays are currently considered secure.
419 bool isSecure = true;
Jesse Hall692c7232012-11-08 15:41:56 -0800420 createBuiltinDisplayLocked(type);
421 wp<IBinder> token = mBuiltinDisplays[i];
422
Dan Stozab9b08832014-03-13 11:55:57 -0700423 sp<IGraphicBufferProducer> producer;
424 sp<IGraphicBufferConsumer> consumer;
425 BufferQueue::createBufferQueue(&producer, &consumer,
426 new GraphicBufferAlloc());
427
428 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i,
429 consumer);
Jesse Hall19e87292013-12-23 21:02:15 -0800430 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700431 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800432 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Dan Stozab9b08832014-03-13 11:55:57 -0700433 fbs, producer,
Jesse Hall05f8c702013-12-23 20:44:38 -0800434 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700435 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700436 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700437 // for displays other than the main display, so we always
438 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800439 ALOGD("marking display %zu as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700440 hw->acquireScreen();
441 }
442 mDisplays.add(token, hw);
443 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700444 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700445
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700446 // make the GLContext current so that we can create textures when creating Layers
447 // (which may happens before we render something)
448 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
449
Mathias Agopian028508c2012-07-25 21:12:12 -0700450 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700451 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
452 vsyncPhaseOffsetNs, true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700453 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700454 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
455 sfVsyncPhaseOffsetNs, false);
456 mSFEventThread = new EventThread(sfVsyncSrc);
457 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700458
Jamie Gennisd1700752013-10-14 12:22:52 -0700459 mEventControlThread = new EventControlThread(this);
460 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
461
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700462 // set a fake vsync period if there is no HWComposer
463 if (mHwc->initCheck() != NO_ERROR) {
464 mPrimaryDispSync.setPeriod(16666667);
465 }
466
Mathias Agopian92a979a2012-08-02 18:32:23 -0700467 // initialize our drawing state
468 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700469
Andy McFadden13a082e2012-08-24 10:16:42 -0700470 // set initial conditions (e.g. unblank default device)
471 initializeDisplays();
472
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700473 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700474 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800475}
476
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700477int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700478 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700479 type : mHwc->allocateDisplayId();
480}
481
Mathias Agopiana67e4182012-06-19 17:26:12 -0700482void SurfaceFlinger::startBootAnim() {
483 // start boot animation
484 property_set("service.bootanim.exit", "0");
485 property_set("ctl.start", "bootanim");
486}
487
Mathias Agopian875d8e12013-06-07 15:35:48 -0700488size_t SurfaceFlinger::getMaxTextureSize() const {
489 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700490}
491
Mathias Agopian875d8e12013-06-07 15:35:48 -0700492size_t SurfaceFlinger::getMaxViewportDims() const {
493 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700494}
495
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800496// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800497
Jamie Gennis582270d2011-08-17 18:19:00 -0700498bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800499 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800500 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800501 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700502 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800503}
504
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700505status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800506 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700507 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800508 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700509 type = i;
510 break;
511 }
512 }
513
514 if (type < 0) {
515 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700516 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700517
518 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700519 float xdpi = hwc.getDpiX(type);
520 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700521
522 // TODO: Not sure if display density should handled by SF any longer
523 class Density {
524 static int getDensityFromProperty(char const* propName) {
525 char property[PROPERTY_VALUE_MAX];
526 int density = 0;
527 if (property_get(propName, property, NULL) > 0) {
528 density = atoi(property);
529 }
530 return density;
531 }
532 public:
533 static int getEmuDensity() {
534 return getDensityFromProperty("qemu.sf.lcd_density"); }
535 static int getBuildDensity() {
536 return getDensityFromProperty("ro.sf.lcd_density"); }
537 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700538
539 if (type == DisplayDevice::DISPLAY_PRIMARY) {
540 // The density of the device is provided by a build property
541 float density = Density::getBuildDensity() / 160.0f;
542 if (density == 0) {
543 // the build doesn't provide a density -- this is wrong!
544 // use xdpi instead
545 ALOGE("ro.sf.lcd_density must be defined as a build property");
546 density = xdpi / 160.0f;
547 }
548 if (Density::getEmuDensity()) {
549 // if "qemu.sf.lcd_density" is specified, it overrides everything
550 xdpi = ydpi = density = Density::getEmuDensity();
551 density /= 160.0f;
552 }
553 info->density = density;
554
555 // TODO: this needs to go away (currently needed only by webkit)
556 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
557 info->orientation = hw->getOrientation();
Mathias Agopian1604f772012-09-18 21:54:42 -0700558 } else {
559 // TODO: where should this value come from?
560 static const int TV_DENSITY = 213;
561 info->density = TV_DENSITY / 160.0f;
562 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700563 }
564
Mathias Agopian1604f772012-09-18 21:54:42 -0700565 info->w = hwc.getWidth(type);
566 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700567 info->xdpi = xdpi;
568 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700569 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700570
571 // All non-virtual displays are currently considered secure.
572 info->secure = true;
573
Mathias Agopian888c8222012-08-04 21:10:38 -0700574 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700575}
576
Svetoslavd85084b2014-03-20 10:28:31 -0700577status_t SurfaceFlinger::clearAnimationFrameStats() {
578 Mutex::Autolock _l(mStateLock);
579 mAnimFrameTracker.clearStats();
580 return NO_ERROR;
581}
582
583status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
584 Mutex::Autolock _l(mStateLock);
585 mAnimFrameTracker.getStats(outStats);
586 return NO_ERROR;
587}
588
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800589// ----------------------------------------------------------------------------
590
591sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800592 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700593}
594
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800595// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800596
597void SurfaceFlinger::waitForEvent() {
598 mEventQueue.waitMessage();
599}
600
601void SurfaceFlinger::signalTransaction() {
602 mEventQueue.invalidate();
603}
604
605void SurfaceFlinger::signalLayerUpdate() {
606 mEventQueue.invalidate();
607}
608
609void SurfaceFlinger::signalRefresh() {
610 mEventQueue.refresh();
611}
612
613status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800614 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800615 return mEventQueue.postMessage(msg, reltime);
616}
617
618status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800619 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800620 status_t res = mEventQueue.postMessage(msg, reltime);
621 if (res == NO_ERROR) {
622 msg->wait();
623 }
624 return res;
625}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800626
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700627void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700628 do {
629 waitForEvent();
630 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800631}
632
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700633void SurfaceFlinger::enableHardwareVsync() {
634 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700635 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700636 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700637 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
638 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700639 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700640 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700641}
642
Jesse Hall948fe0c2013-10-14 12:56:09 -0700643void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700644 Mutex::Autolock _l(mHWVsyncLock);
645
Jesse Hall948fe0c2013-10-14 12:56:09 -0700646 if (makeAvailable) {
647 mHWVsyncAvailable = true;
648 } else if (!mHWVsyncAvailable) {
649 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
650 return;
651 }
652
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700653 const nsecs_t period =
654 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
655
656 mPrimaryDispSync.reset();
657 mPrimaryDispSync.setPeriod(period);
658
659 if (!mPrimaryHWVsyncEnabled) {
660 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700661 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
662 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700663 mPrimaryHWVsyncEnabled = true;
664 }
665}
666
Jesse Hall948fe0c2013-10-14 12:56:09 -0700667void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700668 Mutex::Autolock _l(mHWVsyncLock);
669 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700670 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
671 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700672 mPrimaryDispSync.endResync();
673 mPrimaryHWVsyncEnabled = false;
674 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700675 if (makeUnavailable) {
676 mHWVsyncAvailable = false;
677 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700678}
679
680void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700681 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700682
Jamie Gennisd1700752013-10-14 12:22:52 -0700683 { // Scope for the lock
684 Mutex::Autolock _l(mHWVsyncLock);
685 if (type == 0 && mPrimaryHWVsyncEnabled) {
686 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700687 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700688 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700689
690 if (needsHwVsync) {
691 enableHardwareVsync();
692 } else {
693 disableHardwareVsync(false);
694 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700695}
696
697void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
698 if (mEventThread == NULL) {
699 // This is a temporary workaround for b/7145521. A non-null pointer
700 // does not mean EventThread has finished initializing, so this
701 // is not a correct fix.
702 ALOGW("WARNING: EventThread not started, ignoring hotplug");
703 return;
704 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700705
Jesse Hall9e663de2013-08-16 14:28:37 -0700706 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700707 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800708 if (connected) {
709 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700710 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800711 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
712 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700713 }
714 setTransactionFlags(eDisplayTransactionNeeded);
715
Andy McFadden9e9689c2012-10-10 18:17:51 -0700716 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700717 }
Mathias Agopian86303202012-07-24 22:46:10 -0700718}
719
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700720void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700721 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700722 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700723}
724
Mathias Agopian4fec8732012-06-29 14:12:52 -0700725void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800726 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800727 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700728 case MessageQueue::TRANSACTION:
729 handleMessageTransaction();
730 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700731 case MessageQueue::INVALIDATE:
732 handleMessageTransaction();
733 handleMessageInvalidate();
734 signalRefresh();
735 break;
736 case MessageQueue::REFRESH:
737 handleMessageRefresh();
738 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800739 }
740}
741
Mathias Agopian4fec8732012-06-29 14:12:52 -0700742void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700743 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700744 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700745 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700746 }
747}
748
749void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700750 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700751 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700752}
753
754void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700755 ATRACE_CALL();
756 preComposition();
757 rebuildLayerStacks();
758 setUpHWComposer();
759 doDebugFlashRegions();
760 doComposition();
761 postComposition();
762}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700763
Mathias Agopiancd60f992012-08-16 16:28:27 -0700764void SurfaceFlinger::doDebugFlashRegions()
765{
766 // is debugging enabled
767 if (CC_LIKELY(!mDebugRegion))
768 return;
769
770 const bool repaintEverything = mRepaintEverything;
771 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
772 const sp<DisplayDevice>& hw(mDisplays[dpy]);
773 if (hw->canDraw()) {
774 // transform the dirty region into this screen's coordinate space
775 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
776 if (!dirtyRegion.isEmpty()) {
777 // redraw the whole screen
778 doComposeSurfaces(hw, Region(hw->bounds()));
779
780 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700781 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700782 RenderEngine& engine(getRenderEngine());
783 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
784
Mathias Agopiancd60f992012-08-16 16:28:27 -0700785 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700786 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700787 }
788 }
789 }
790
791 postFramebuffer();
792
793 if (mDebugRegion > 1) {
794 usleep(mDebugRegion * 1000);
795 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700796
797 HWComposer& hwc(getHwComposer());
798 if (hwc.initCheck() == NO_ERROR) {
799 status_t err = hwc.prepare();
800 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
801 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700802}
803
804void SurfaceFlinger::preComposition()
805{
806 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700807 const LayerVector& layers(mDrawingState.layersSortedByZ);
808 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700809 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700810 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700811 needExtraInvalidate = true;
812 }
813 }
814 if (needExtraInvalidate) {
815 signalLayerUpdate();
816 }
817}
818
819void SurfaceFlinger::postComposition()
820{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700821 const LayerVector& layers(mDrawingState.layersSortedByZ);
822 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700823 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700824 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700825 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800826
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700827 const HWComposer& hwc = getHwComposer();
828 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
829
830 if (presentFence->isValid()) {
831 if (mPrimaryDispSync.addPresentFence(presentFence)) {
832 enableHardwareVsync();
833 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700834 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700835 }
836 }
837
838 if (runningWithoutSyncFramework) {
839 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
840 if (hw->isScreenAcquired()) {
841 enableHardwareVsync();
842 }
843 }
844
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800845 if (mAnimCompositionPending) {
846 mAnimCompositionPending = false;
847
Jesse Halla9a1b002013-02-27 16:39:25 -0800848 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800849 mAnimFrameTracker.setActualPresentFence(presentFence);
850 } else {
851 // The HWC doesn't support present fences, so use the refresh
852 // timestamp instead.
853 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
854 mAnimFrameTracker.setActualPresentTime(presentTime);
855 }
856 mAnimFrameTracker.advanceFrame();
857 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700858}
859
860void SurfaceFlinger::rebuildLayerStacks() {
861 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700862 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700863 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700864 mVisibleRegionsDirty = false;
865 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700866
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700867 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700868 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700869 Region opaqueRegion;
870 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800871 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700872 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700873 const Transform& tr(hw->getTransform());
874 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700875 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700876 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700877 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700878
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700879 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700880 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700881 const sp<Layer>& layer(layers[i]);
882 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700883 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700884 Region drawRegion(tr.transform(
885 layer->visibleNonTransparentRegion));
886 drawRegion.andSelf(bounds);
887 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700888 layersSortedByZ.add(layer);
889 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700890 }
891 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700892 }
Mathias Agopian42977342012-08-05 00:40:46 -0700893 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700894 hw->undefinedRegion.set(bounds);
895 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
896 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700897 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700898 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700899}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700900
Mathias Agopiancd60f992012-08-16 16:28:27 -0700901void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700902 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Dan Stoza71433162014-02-04 16:22:36 -0800903 bool mustRecompose =
904 !(mDisplays[dpy]->getDirtyRegion(false).isEmpty());
905 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700906 }
907
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700908 HWComposer& hwc(getHwComposer());
909 if (hwc.initCheck() == NO_ERROR) {
910 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700911 if (CC_UNLIKELY(mHwWorkListDirty)) {
912 mHwWorkListDirty = false;
913 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
914 sp<const DisplayDevice> hw(mDisplays[dpy]);
915 const int32_t id = hw->getHwcDisplayId();
916 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800917 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700918 hw->getVisibleLayersSortedByZ());
919 const size_t count = currentLayers.size();
920 if (hwc.createWorkList(id, count) == NO_ERROR) {
921 HWComposer::LayerListIterator cur = hwc.begin(id);
922 const HWComposer::LayerListIterator end = hwc.end(id);
923 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800924 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700925 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -0700926 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -0700927 cur->setSkip(true);
928 }
929 }
930 }
931 }
932 }
933 }
934
935 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700936 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700937 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700938 const int32_t id = hw->getHwcDisplayId();
939 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800940 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700941 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700942 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700943 HWComposer::LayerListIterator cur = hwc.begin(id);
944 const HWComposer::LayerListIterator end = hwc.end(id);
945 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
946 /*
947 * update the per-frame h/w composer data for each layer
948 * and build the transparent region of the FB
949 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800950 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700951 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700952 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700953 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700954 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700955
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700956 status_t err = hwc.prepare();
957 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700958
959 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
960 sp<const DisplayDevice> hw(mDisplays[dpy]);
961 hw->prepareFrame(hwc);
962 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700963 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700964}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700965
Mathias Agopiancd60f992012-08-16 16:28:27 -0700966void SurfaceFlinger::doComposition() {
967 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700968 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700969 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700970 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700971 if (hw->canDraw()) {
972 // transform the dirty region into this screen's coordinate space
973 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800974
975 // repaint the framebuffer (if needed)
976 doDisplayComposition(hw, dirtyRegion);
977
Mathias Agopiancd60f992012-08-16 16:28:27 -0700978 hw->dirtyRegion.clear();
979 hw->flip(hw->swapRegion);
980 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700981 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700982 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700983 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700984 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700985 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700986}
987
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800988void SurfaceFlinger::postFramebuffer()
989{
Mathias Agopian841cde52012-03-01 15:44:37 -0800990 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800991
Mathias Agopiana44b0412011-10-16 18:46:35 -0700992 const nsecs_t now = systemTime();
993 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700994
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700995 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700996 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700997 if (!hwc.supportsFramebufferTarget()) {
998 // EGL spec says:
999 // "surface must be bound to the calling thread's current context,
1000 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001001 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001002 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001003 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001004 }
1005
Mathias Agopian6da15f42013-09-25 20:40:07 -07001006 // make the default display current because the VirtualDisplayDevice code cannot
1007 // deal with dequeueBuffer() being called outside of the composition loop; however
1008 // the code below can call glFlush() which is allowed (and does in some case) call
1009 // dequeueBuffer().
1010 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1011
Mathias Agopian92a979a2012-08-02 18:32:23 -07001012 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001013 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001014 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001015 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001016 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001017 int32_t id = hw->getHwcDisplayId();
1018 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001019 HWComposer::LayerListIterator cur = hwc.begin(id);
1020 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001021 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001022 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001023 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001024 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001025 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001026 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001027 }
Jesse Hallef194142012-06-14 14:45:17 -07001028 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001029 }
1030
Mathias Agopiana44b0412011-10-16 18:46:35 -07001031 mLastSwapBufferTime = systemTime() - now;
1032 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001033
1034 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1035 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1036 logFrameStats();
1037 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001038}
1039
Mathias Agopian87baae12012-07-31 12:38:26 -07001040void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001041{
Mathias Agopian841cde52012-03-01 15:44:37 -08001042 ATRACE_CALL();
1043
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001044 // here we keep a copy of the drawing state (that is the state that's
1045 // going to be overwritten by handleTransactionLocked()) outside of
1046 // mStateLock so that the side-effects of the State assignment
1047 // don't happen with mStateLock held (which can cause deadlocks).
1048 State drawingState(mDrawingState);
1049
Mathias Agopianca4d3602011-05-19 15:38:14 -07001050 Mutex::Autolock _l(mStateLock);
1051 const nsecs_t now = systemTime();
1052 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001053
Mathias Agopianca4d3602011-05-19 15:38:14 -07001054 // Here we're guaranteed that some transaction flags are set
1055 // so we can call handleTransactionLocked() unconditionally.
1056 // We call getTransactionFlags(), which will also clear the flags,
1057 // with mStateLock held to guarantee that mCurrentState won't change
1058 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001059
Mathias Agopiane57f2922012-08-09 16:29:12 -07001060 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001061 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001062
Mathias Agopianca4d3602011-05-19 15:38:14 -07001063 mLastTransactionTime = systemTime() - now;
1064 mDebugInTransaction = 0;
1065 invalidateHwcGeometry();
1066 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001067}
1068
Mathias Agopian87baae12012-07-31 12:38:26 -07001069void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001070{
1071 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001072 const size_t count = currentLayers.size();
1073
1074 /*
1075 * Traversal of the children
1076 * (perform the transaction for each of them if needed)
1077 */
1078
Mathias Agopian3559b072012-08-15 13:46:03 -07001079 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001080 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001081 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001082 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1083 if (!trFlags) continue;
1084
1085 const uint32_t flags = layer->doTransaction(0);
1086 if (flags & Layer::eVisibleRegion)
1087 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001088 }
1089 }
1090
1091 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001092 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001093 */
1094
Mathias Agopiane57f2922012-08-09 16:29:12 -07001095 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001096 // here we take advantage of Vector's copy-on-write semantics to
1097 // improve performance by skipping the transaction entirely when
1098 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001099 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1100 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001101 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001102 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001103 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001104 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001105
1106 // find the displays that were removed
1107 // (ie: in drawing state but not in current state)
1108 // also handle displays that changed
1109 // (ie: displays that are in both lists)
1110 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001111 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1112 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001113 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001114 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001115 // Call makeCurrent() on the primary display so we can
1116 // be sure that nothing associated with this display
1117 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001118 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001119 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001120 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1121 if (hw != NULL)
1122 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001123 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001124 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001125 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001126 } else {
1127 ALOGW("trying to remove the main display");
1128 }
1129 } else {
1130 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001131 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001132 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001133 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001134 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001135 // recreating the DisplayDevice, so we just remove it
1136 // from the drawing state, so that it get re-added
1137 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001138 sp<DisplayDevice> hw(getDisplayDevice(display));
1139 if (hw != NULL)
1140 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001141 mDisplays.removeItem(display);
1142 mDrawingState.displays.removeItemsAt(i);
1143 dc--; i--;
1144 // at this point we must loop to the next item
1145 continue;
1146 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001147
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001148 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001149 if (disp != NULL) {
1150 if (state.layerStack != draw[i].layerStack) {
1151 disp->setLayerStack(state.layerStack);
1152 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001153 if ((state.orientation != draw[i].orientation)
1154 || (state.viewport != draw[i].viewport)
1155 || (state.frame != draw[i].frame))
1156 {
1157 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001158 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001159 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001160 }
1161 }
1162 }
1163
1164 // find displays that were added
1165 // (ie: in current state but not in drawing state)
1166 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001167 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001168 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001169
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001170 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001171 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001172 sp<IGraphicBufferProducer> bqProducer;
1173 sp<IGraphicBufferConsumer> bqConsumer;
1174 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1175 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001176
Jesse Hall02d86562013-03-25 14:43:23 -07001177 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001178 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001179 // Virtual displays without a surface are dormant:
1180 // they have external state (layer stack, projection,
1181 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001182 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001183
Jesse Hall02d86562013-03-25 14:43:23 -07001184 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001185 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
Dan Stozab9b08832014-03-13 11:55:57 -07001186 *mHwc, hwcDisplayId, state.surface,
1187 bqProducer, bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001188
1189 dispSurface = vds;
1190 if (hwcDisplayId >= 0) {
1191 producer = vds;
1192 } else {
1193 // There won't be any interaction with HWC for this virtual display,
1194 // so the GLES driver can pass buffers directly to the sink.
1195 producer = state.surface;
1196 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001197 }
1198 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001199 ALOGE_IF(state.surface!=NULL,
1200 "adding a supported display, but rendering "
1201 "surface is provided (%p), ignoring it",
1202 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001203 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001204 // for supported (by hwc) displays we provide our
1205 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001206 dispSurface = new FramebufferSurface(*mHwc, state.type,
1207 bqConsumer);
1208 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001209 }
1210
1211 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001212 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001213 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001214 state.type, hwcDisplayId,
1215 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001216 display, dispSurface, producer,
1217 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001218 hw->setLayerStack(state.layerStack);
1219 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001220 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001221 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001222 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001223 if (state.isVirtualDisplay()) {
1224 if (hwcDisplayId >= 0) {
1225 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1226 hw->getWidth(), hw->getHeight(),
1227 hw->getFormat());
1228 }
1229 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001230 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001231 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001232 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001233 }
1234 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001235 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001236 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001237
Mathias Agopian84300952012-11-21 16:02:13 -08001238 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1239 // The transform hint might have changed for some layers
1240 // (either because a display has changed, or because a layer
1241 // as changed).
1242 //
1243 // Walk through all the layers in currentLayers,
1244 // and update their transform hint.
1245 //
1246 // If a layer is visible only on a single display, then that
1247 // display is used to calculate the hint, otherwise we use the
1248 // default display.
1249 //
1250 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1251 // the hint is set before we acquire a buffer from the surface texture.
1252 //
1253 // NOTE: layer transactions have taken place already, so we use their
1254 // drawing state. However, SurfaceFlinger's own transaction has not
1255 // happened yet, so we must use the current state layer list
1256 // (soon to become the drawing state list).
1257 //
1258 sp<const DisplayDevice> disp;
1259 uint32_t currentlayerStack = 0;
1260 for (size_t i=0; i<count; i++) {
1261 // NOTE: we rely on the fact that layers are sorted by
1262 // layerStack first (so we don't have to traverse the list
1263 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001264 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001265 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001266 if (i==0 || currentlayerStack != layerStack) {
1267 currentlayerStack = layerStack;
1268 // figure out if this layerstack is mirrored
1269 // (more than one display) if so, pick the default display,
1270 // if not, pick the only display it's on.
1271 disp.clear();
1272 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1273 sp<const DisplayDevice> hw(mDisplays[dpy]);
1274 if (hw->getLayerStack() == currentlayerStack) {
1275 if (disp == NULL) {
1276 disp = hw;
1277 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001278 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001279 break;
1280 }
1281 }
1282 }
1283 }
Chet Haase91d25932013-04-11 15:24:55 -07001284 if (disp == NULL) {
1285 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1286 // redraw after transform hint changes. See bug 8508397.
1287
1288 // could be null when this layer is using a layerStack
1289 // that is not visible on any display. Also can occur at
1290 // screen off/on times.
1291 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001292 }
Chet Haase91d25932013-04-11 15:24:55 -07001293 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001294 }
1295 }
1296
1297
Mathias Agopian3559b072012-08-15 13:46:03 -07001298 /*
1299 * Perform our own transaction if needed
1300 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001301
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001302 const LayerVector& layers(mDrawingState.layersSortedByZ);
1303 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001304 // layers have been added
1305 mVisibleRegionsDirty = true;
1306 }
1307
1308 // some layers might have been removed, so
1309 // we need to update the regions they're exposing.
1310 if (mLayersRemoved) {
1311 mLayersRemoved = false;
1312 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001313 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001314 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001315 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001316 if (currentLayers.indexOf(layer) < 0) {
1317 // this layer is not visible anymore
1318 // TODO: we could traverse the tree from front to back and
1319 // compute the actual visible region
1320 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001321 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001322 Region visibleReg = s.transform.transform(
1323 Region(Rect(s.active.w, s.active.h)));
1324 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001325 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001326 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001327 }
1328
1329 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001330}
1331
1332void SurfaceFlinger::commitTransaction()
1333{
1334 if (!mLayersPendingRemoval.isEmpty()) {
1335 // Notify removed layers now that they can't be drawn from
1336 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1337 mLayersPendingRemoval[i]->onRemoved();
1338 }
1339 mLayersPendingRemoval.clear();
1340 }
1341
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001342 // If this transaction is part of a window animation then the next frame
1343 // we composite should be considered an animation as well.
1344 mAnimCompositionPending = mAnimTransactionPending;
1345
Mathias Agopian4fec8732012-06-29 14:12:52 -07001346 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001347 mTransactionPending = false;
1348 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001349 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350}
1351
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001352void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001353 const LayerVector& currentLayers, uint32_t layerStack,
1354 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001355{
Mathias Agopian841cde52012-03-01 15:44:37 -08001356 ATRACE_CALL();
1357
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001358 Region aboveOpaqueLayers;
1359 Region aboveCoveredLayers;
1360 Region dirty;
1361
Mathias Agopian87baae12012-07-31 12:38:26 -07001362 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001363
1364 size_t i = currentLayers.size();
1365 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001366 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001367
1368 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001369 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001370
Jesse Hall01e29052013-02-19 16:13:35 -08001371 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001372 if (s.layerStack != layerStack)
1373 continue;
1374
Mathias Agopianab028732010-03-16 16:41:46 -07001375 /*
1376 * opaqueRegion: area of a surface that is fully opaque.
1377 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001379
1380 /*
1381 * visibleRegion: area of a surface that is visible on screen
1382 * and not fully transparent. This is essentially the layer's
1383 * footprint minus the opaque regions above it.
1384 * Areas covered by a translucent surface are considered visible.
1385 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001387
1388 /*
1389 * coveredRegion: area of a surface that is covered by all
1390 * visible regions above it (which includes the translucent areas).
1391 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001392 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001393
Jesse Halla8026d22012-09-25 13:25:04 -07001394 /*
1395 * transparentRegion: area of a surface that is hinted to be completely
1396 * transparent. This is only used to tell when the layer has no visible
1397 * non-transparent regions and can be removed from the layer list. It
1398 * does not affect the visibleRegion of this layer or any layers
1399 * beneath it. The hint may not be correct if apps don't respect the
1400 * SurfaceView restrictions (which, sadly, some don't).
1401 */
1402 Region transparentRegion;
1403
Mathias Agopianab028732010-03-16 16:41:46 -07001404
1405 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001406 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001407 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001408 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001409 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001410 if (!visibleRegion.isEmpty()) {
1411 // Remove the transparent area from the visible region
1412 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001413 const Transform tr(s.transform);
1414 if (tr.transformed()) {
1415 if (tr.preserveRects()) {
1416 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001417 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001418 } else {
1419 // transformation too complex, can't do the
1420 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001421 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001422 }
1423 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001424 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001425 }
Mathias Agopianab028732010-03-16 16:41:46 -07001426 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001427
Mathias Agopianab028732010-03-16 16:41:46 -07001428 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001429 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001430 if (s.alpha==255 && !translucent &&
1431 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1432 // the opaque region is the layer's footprint
1433 opaqueRegion = visibleRegion;
1434 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001435 }
1436 }
1437
Mathias Agopianab028732010-03-16 16:41:46 -07001438 // Clip the covered region to the visible region
1439 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1440
1441 // Update aboveCoveredLayers for next (lower) layer
1442 aboveCoveredLayers.orSelf(visibleRegion);
1443
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001444 // subtract the opaque region covered by the layers above us
1445 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001446
1447 // compute this layer's dirty region
1448 if (layer->contentDirty) {
1449 // we need to invalidate the whole region
1450 dirty = visibleRegion;
1451 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001452 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001453 layer->contentDirty = false;
1454 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001455 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001456 * the exposed region consists of two components:
1457 * 1) what's VISIBLE now and was COVERED before
1458 * 2) what's EXPOSED now less what was EXPOSED before
1459 *
1460 * note that (1) is conservative, we start with the whole
1461 * visible region but only keep what used to be covered by
1462 * something -- which mean it may have been exposed.
1463 *
1464 * (2) handles areas that were not covered by anything but got
1465 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001466 */
Mathias Agopianab028732010-03-16 16:41:46 -07001467 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001468 const Region oldVisibleRegion = layer->visibleRegion;
1469 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001470 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1471 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001472 }
1473 dirty.subtractSelf(aboveOpaqueLayers);
1474
1475 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001476 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001477
Mathias Agopianab028732010-03-16 16:41:46 -07001478 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001479 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001480
Jesse Halla8026d22012-09-25 13:25:04 -07001481 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001482 layer->setVisibleRegion(visibleRegion);
1483 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001484 layer->setVisibleNonTransparentRegion(
1485 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001486 }
1487
Mathias Agopian87baae12012-07-31 12:38:26 -07001488 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001489}
1490
Mathias Agopian87baae12012-07-31 12:38:26 -07001491void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1492 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001493 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001494 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1495 if (hw->getLayerStack() == layerStack) {
1496 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001497 }
1498 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001499}
1500
1501void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001502{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001503 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001504
Mathias Agopian4fec8732012-06-29 14:12:52 -07001505 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001506 const LayerVector& layers(mDrawingState.layersSortedByZ);
1507 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001508 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001509 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001510 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001511 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001512 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001513 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001514
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001515 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001516}
1517
Mathias Agopianad456f92011-01-13 17:53:01 -08001518void SurfaceFlinger::invalidateHwcGeometry()
1519{
1520 mHwWorkListDirty = true;
1521}
1522
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001523
Mathias Agopiancd60f992012-08-16 16:28:27 -07001524void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001525 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001526{
Dan Stoza71433162014-02-04 16:22:36 -08001527 // We only need to actually compose the display if:
1528 // 1) It is being handled by hardware composer, which may need this to
1529 // keep its virtual display state machine in sync, or
1530 // 2) There is work to be done (the dirty region isn't empty)
1531 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1532 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1533 return;
1534 }
1535
Mathias Agopian87baae12012-07-31 12:38:26 -07001536 Region dirtyRegion(inDirtyRegion);
1537
Mathias Agopianb8a55602009-06-26 19:06:36 -07001538 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001539 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001540
Mathias Agopian42977342012-08-05 00:40:46 -07001541 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001542 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001543 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1544 // takes a rectangle, we must make sure to update that whole
1545 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001546 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001547 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001548 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001549 // We need to redraw the rectangle that will be updated
1550 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001551 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001552 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001553 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001554 } else {
1555 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001556 dirtyRegion.set(hw->bounds());
1557 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001558 }
1559 }
1560
Alan Viverette9c5a3332013-09-12 20:04:35 -07001561 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Mathias Agopianff2ed702013-09-01 21:36:12 -07001562 doComposeSurfaces(hw, dirtyRegion);
1563 } else {
1564 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001565 mat4 colorMatrix = mColorMatrix;
1566 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001567 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001568 }
1569 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001570 doComposeSurfaces(hw, dirtyRegion);
1571 engine.endGroup();
1572 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001573
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001574 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001575 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001576
1577 // swap buffers (presentation)
1578 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001579}
1580
Mathias Agopiancd60f992012-08-16 16:28:27 -07001581void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001582{
Mathias Agopian3f844832013-08-07 21:24:32 -07001583 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001584 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001585 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001586 HWComposer::LayerListIterator cur = hwc.begin(id);
1587 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001588
Jesse Halld05a17f2013-09-30 16:49:30 -07001589 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001590 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001591 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001592 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1593 hw->getDisplayName().string());
1594 return;
1595 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001596
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001597 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001598 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001599 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001600 // when using overlays, we assume a fully transparent framebuffer
1601 // NOTE: we could reduce how much we need to clear, for instance
1602 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001603 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001604 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001605 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001606 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001607 // we start with the whole screen area
1608 const Region bounds(hw->getBounds());
1609
1610 // we remove the scissor part
1611 // we're left with the letterbox region
1612 // (common case is that letterbox ends-up being empty)
1613 const Region letterbox(bounds.subtract(hw->getScissor()));
1614
1615 // compute the area to clear
1616 Region region(hw->undefinedRegion.merge(letterbox));
1617
1618 // but limit it to the dirty region
1619 region.andSelf(dirty);
1620
Mathias Agopianb9494d52012-04-18 02:28:45 -07001621 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001622 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001623 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001624 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001625 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001626 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001627
Mathias Agopian766dc492012-10-30 18:08:06 -07001628 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1629 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001630 // scissor on the main display. It should never be needed
1631 // anyways (though in theory it could since the API allows it).
1632 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001633 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001634 if (scissor != bounds) {
1635 // scissor doesn't match the screen's dimensions, so we
1636 // need to clear everything outside of it and enable
1637 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001638
Mathias Agopianf45c5102012-10-24 16:29:17 -07001639 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001640 const uint32_t height = hw->getHeight();
1641 engine.setScissor(scissor.left, height - scissor.bottom,
1642 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001643 }
1644 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001645 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001646
Mathias Agopian85d751c2012-08-29 16:59:24 -07001647 /*
1648 * and then, render the layers targeted at the framebuffer
1649 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001650
Mathias Agopian13127d82013-03-05 17:47:11 -08001651 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001652 const size_t count = layers.size();
1653 const Transform& tr = hw->getTransform();
1654 if (cur != end) {
1655 // we're using h/w composer
1656 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001657 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001658 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001659 if (!clip.isEmpty()) {
1660 switch (cur->getCompositionType()) {
1661 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001662 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001663 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1664 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001665 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001666 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001667 // never clear the very first layer since we're
1668 // guaranteed the FB is already cleared
1669 layer->clearWithOpenGL(hw, clip);
1670 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001671 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001672 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001673 case HWC_FRAMEBUFFER: {
1674 layer->draw(hw, clip);
1675 break;
1676 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001677 case HWC_FRAMEBUFFER_TARGET: {
1678 // this should not happen as the iterator shouldn't
1679 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001680 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001681 break;
1682 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001683 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001684 }
1685 layer->setAcquireFence(hw, *cur);
1686 }
1687 } else {
1688 // we're not using h/w composer
1689 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001690 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001691 const Region clip(dirty.intersect(
1692 tr.transform(layer->visibleRegion)));
1693 if (!clip.isEmpty()) {
1694 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001695 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001696 }
1697 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001698
1699 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001700 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001701}
1702
Mathias Agopian3f844832013-08-07 21:24:32 -07001703void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001704 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001705 RenderEngine& engine(getRenderEngine());
1706 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001707}
1708
Mathias Agopianac9fa422013-02-11 16:40:36 -08001709void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1710 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001711 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001712 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001713{
Mathias Agopian96f08192010-06-02 23:28:45 -07001714 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001715 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001716
Mathias Agopian96f08192010-06-02 23:28:45 -07001717 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001718 Mutex::Autolock _l(mStateLock);
1719 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001720 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001721}
1722
Mathias Agopian3f844832013-08-07 21:24:32 -07001723status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001724 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001725 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001726 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001727 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001728 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001729 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001730 return NO_ERROR;
1731 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001732 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001733}
1734
Dan Stozac7014012014-02-14 15:03:43 -08001735uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001736 return android_atomic_release_load(&mTransactionFlags);
1737}
1738
Mathias Agopian3f844832013-08-07 21:24:32 -07001739uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001740 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1741}
1742
Mathias Agopian3f844832013-08-07 21:24:32 -07001743uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001744 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1745 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001746 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001747 }
1748 return old;
1749}
1750
Mathias Agopian8b33f032012-07-24 20:43:54 -07001751void SurfaceFlinger::setTransactionState(
1752 const Vector<ComposerState>& state,
1753 const Vector<DisplayState>& displays,
1754 uint32_t flags)
1755{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001756 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001757 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001758 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001759
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001760 if (flags & eAnimation) {
1761 // For window updates that are part of an animation we must wait for
1762 // previous animation "frames" to be handled.
1763 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001764 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001765 if (CC_UNLIKELY(err != NO_ERROR)) {
1766 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001767 // caller after a few seconds.
1768 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1769 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001770 mAnimTransactionPending = false;
1771 break;
1772 }
1773 }
1774 }
1775
Mathias Agopiane57f2922012-08-09 16:29:12 -07001776 size_t count = displays.size();
1777 for (size_t i=0 ; i<count ; i++) {
1778 const DisplayState& s(displays[i]);
1779 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001780 }
1781
Mathias Agopiane57f2922012-08-09 16:29:12 -07001782 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001783 for (size_t i=0 ; i<count ; i++) {
1784 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001785 // Here we need to check that the interface we're given is indeed
1786 // one of our own. A malicious client could give us a NULL
1787 // IInterface, or one of its own or even one of our own but a
1788 // different type. All these situations would cause us to crash.
1789 //
1790 // NOTE: it would be better to use RTTI as we could directly check
1791 // that we have a Client*. however, RTTI is disabled in Android.
1792 if (s.client != NULL) {
1793 sp<IBinder> binder = s.client->asBinder();
1794 if (binder != NULL) {
1795 String16 desc(binder->getInterfaceDescriptor());
1796 if (desc == ISurfaceComposerClient::descriptor) {
1797 sp<Client> client( static_cast<Client *>(s.client.get()) );
1798 transactionFlags |= setClientStateLocked(client, s.state);
1799 }
1800 }
1801 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001802 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001803
Mathias Agopian386aa982011-11-07 21:58:03 -08001804 if (transactionFlags) {
1805 // this triggers the transaction
1806 setTransactionFlags(transactionFlags);
1807
1808 // if this is a synchronous transaction, wait for it to take effect
1809 // before returning.
1810 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001811 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001812 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001813 if (flags & eAnimation) {
1814 mAnimTransactionPending = true;
1815 }
1816 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001817 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1818 if (CC_UNLIKELY(err != NO_ERROR)) {
1819 // just in case something goes wrong in SF, return to the
1820 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001821 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1822 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001823 break;
1824 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001825 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001826 }
1827}
1828
Mathias Agopiane57f2922012-08-09 16:29:12 -07001829uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1830{
Jesse Hall9a143922012-10-04 16:29:19 -07001831 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1832 if (dpyIdx < 0)
1833 return 0;
1834
Mathias Agopiane57f2922012-08-09 16:29:12 -07001835 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001836 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001837 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001838 const uint32_t what = s.what;
1839 if (what & DisplayState::eSurfaceChanged) {
1840 if (disp.surface->asBinder() != s.surface->asBinder()) {
1841 disp.surface = s.surface;
1842 flags |= eDisplayTransactionNeeded;
1843 }
1844 }
1845 if (what & DisplayState::eLayerStackChanged) {
1846 if (disp.layerStack != s.layerStack) {
1847 disp.layerStack = s.layerStack;
1848 flags |= eDisplayTransactionNeeded;
1849 }
1850 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001851 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001852 if (disp.orientation != s.orientation) {
1853 disp.orientation = s.orientation;
1854 flags |= eDisplayTransactionNeeded;
1855 }
1856 if (disp.frame != s.frame) {
1857 disp.frame = s.frame;
1858 flags |= eDisplayTransactionNeeded;
1859 }
1860 if (disp.viewport != s.viewport) {
1861 disp.viewport = s.viewport;
1862 flags |= eDisplayTransactionNeeded;
1863 }
1864 }
1865 }
1866 return flags;
1867}
1868
1869uint32_t SurfaceFlinger::setClientStateLocked(
1870 const sp<Client>& client,
1871 const layer_state_t& s)
1872{
1873 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001874 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001875 if (layer != 0) {
1876 const uint32_t what = s.what;
1877 if (what & layer_state_t::ePositionChanged) {
1878 if (layer->setPosition(s.x, s.y))
1879 flags |= eTraversalNeeded;
1880 }
1881 if (what & layer_state_t::eLayerChanged) {
1882 // NOTE: index needs to be calculated before we update the state
1883 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1884 if (layer->setLayer(s.z)) {
1885 mCurrentState.layersSortedByZ.removeAt(idx);
1886 mCurrentState.layersSortedByZ.add(layer);
1887 // we need traversal (state changed)
1888 // AND transaction (list changed)
1889 flags |= eTransactionNeeded|eTraversalNeeded;
1890 }
1891 }
1892 if (what & layer_state_t::eSizeChanged) {
1893 if (layer->setSize(s.w, s.h)) {
1894 flags |= eTraversalNeeded;
1895 }
1896 }
1897 if (what & layer_state_t::eAlphaChanged) {
1898 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1899 flags |= eTraversalNeeded;
1900 }
1901 if (what & layer_state_t::eMatrixChanged) {
1902 if (layer->setMatrix(s.matrix))
1903 flags |= eTraversalNeeded;
1904 }
1905 if (what & layer_state_t::eTransparentRegionChanged) {
1906 if (layer->setTransparentRegionHint(s.transparentRegion))
1907 flags |= eTraversalNeeded;
1908 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08001909 if ((what & layer_state_t::eVisibilityChanged) ||
1910 (what & layer_state_t::eOpacityChanged)) {
1911 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07001912 if (layer->setFlags(s.flags, s.mask))
1913 flags |= eTraversalNeeded;
1914 }
1915 if (what & layer_state_t::eCropChanged) {
1916 if (layer->setCrop(s.crop))
1917 flags |= eTraversalNeeded;
1918 }
1919 if (what & layer_state_t::eLayerStackChanged) {
1920 // NOTE: index needs to be calculated before we update the state
1921 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1922 if (layer->setLayerStack(s.layerStack)) {
1923 mCurrentState.layersSortedByZ.removeAt(idx);
1924 mCurrentState.layersSortedByZ.add(layer);
1925 // we need traversal (state changed)
1926 // AND transaction (list changed)
1927 flags |= eTransactionNeeded|eTraversalNeeded;
1928 }
1929 }
1930 }
1931 return flags;
1932}
1933
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001934status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001935 const String8& name,
1936 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001937 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1938 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001939{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001940 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001941 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001942 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001943 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001944 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001945 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001946
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001947 status_t result = NO_ERROR;
1948
1949 sp<Layer> layer;
1950
Mathias Agopian3165cc22012-08-08 19:42:09 -07001951 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1952 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001953 result = createNormalLayer(client,
1954 name, w, h, flags, format,
1955 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001956 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001957 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001958 result = createDimLayer(client,
1959 name, w, h, flags,
1960 handle, gbp, &layer);
1961 break;
1962 default:
1963 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001964 break;
1965 }
1966
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001967 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001968 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001969 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001970 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001971 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001972}
1973
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001974status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1975 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1976 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001977{
1978 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001979 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001980 case PIXEL_FORMAT_TRANSPARENT:
1981 case PIXEL_FORMAT_TRANSLUCENT:
1982 format = PIXEL_FORMAT_RGBA_8888;
1983 break;
1984 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001985#ifdef NO_RGBX_8888
1986 format = PIXEL_FORMAT_RGB_565;
1987#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001988 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001989#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001990 break;
1991 }
1992
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001993#ifdef NO_RGBX_8888
1994 if (format == PIXEL_FORMAT_RGBX_8888)
1995 format = PIXEL_FORMAT_RGBA_8888;
1996#endif
1997
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001998 *outLayer = new Layer(this, client, name, w, h, flags);
1999 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2000 if (err == NO_ERROR) {
2001 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002002 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002003 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002004
2005 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2006 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002007}
2008
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002009status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2010 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2011 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002012{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002013 *outLayer = new LayerDim(this, client, name, w, h, flags);
2014 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002015 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002016 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002017}
2018
Mathias Agopianac9fa422013-02-11 16:40:36 -08002019status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002020{
Mathias Agopian67106042013-03-14 19:18:13 -07002021 // called by the window manager when it wants to remove a Layer
2022 status_t err = NO_ERROR;
2023 sp<Layer> l(client->getLayerUser(handle));
2024 if (l != NULL) {
2025 err = removeLayer(l);
2026 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2027 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002028 }
2029 return err;
2030}
2031
Mathias Agopian13127d82013-03-05 17:47:11 -08002032status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002033{
Mathias Agopian67106042013-03-14 19:18:13 -07002034 // called by ~LayerCleaner() when all references to the IBinder (handle)
2035 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002036 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002037 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002038 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002039 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002040 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002041 "error removing layer=%p (%s)", l.get(), strerror(-err));
2042 }
2043 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002044}
2045
Mathias Agopianb60314a2012-04-10 22:09:54 -07002046// ---------------------------------------------------------------------------
2047
Andy McFadden13a082e2012-08-24 10:16:42 -07002048void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002049 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002050 Vector<ComposerState> state;
2051 Vector<DisplayState> displays;
2052 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002053 d.what = DisplayState::eDisplayProjectionChanged |
2054 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002055 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002056 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002057 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002058 d.frame.makeInvalid();
2059 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002060 displays.add(d);
2061 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002062 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07002063
2064 const nsecs_t period =
2065 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2066 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002067}
2068
2069void SurfaceFlinger::initializeDisplays() {
2070 class MessageScreenInitialized : public MessageBase {
2071 SurfaceFlinger* flinger;
2072 public:
2073 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2074 virtual bool handler() {
2075 flinger->onInitializeDisplays();
2076 return true;
2077 }
2078 };
2079 sp<MessageBase> msg = new MessageScreenInitialized(this);
2080 postMessageAsync(msg); // we may be called from main thread, use async message
2081}
2082
2083
Mathias Agopiancde87a32012-09-13 14:09:01 -07002084void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002085 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2086 if (hw->isScreenAcquired()) {
2087 // this is expected, e.g. when power manager wakes up during boot
2088 ALOGD(" screen was previously acquired");
2089 return;
2090 }
2091
Mathias Agopian42977342012-08-05 00:40:46 -07002092 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002093 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002094 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002095 // built-in display, tell the HWC
2096 getHwComposer().acquire(type);
2097
2098 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2099 // FIXME: eventthread only knows about the main display right now
2100 mEventThread->onScreenAcquired();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07002101
Jesse Hall948fe0c2013-10-14 12:56:09 -07002102 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002103 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002104 }
Mathias Agopian20128302012-08-13 18:32:13 -07002105 mVisibleRegionsDirty = true;
2106 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002107}
2108
Mathias Agopiancde87a32012-09-13 14:09:01 -07002109void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002110 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2111 if (!hw->isScreenAcquired()) {
2112 ALOGD(" screen was previously released");
2113 return;
2114 }
2115
2116 hw->releaseScreen();
2117 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002118 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002119 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002120 disableHardwareVsync(true); // also cancels any in-progress resync
2121
Mathias Agopiancde87a32012-09-13 14:09:01 -07002122 // FIXME: eventthread only knows about the main display right now
2123 mEventThread->onScreenReleased();
2124 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002125
2126 // built-in display, tell the HWC
2127 getHwComposer().release(type);
2128 }
2129 mVisibleRegionsDirty = true;
2130 // from this point on, SF will stop drawing on this display
2131}
2132
2133void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2134 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002135 SurfaceFlinger& mFlinger;
2136 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002137 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002138 MessageScreenAcquired(SurfaceFlinger& flinger,
2139 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002140 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002141 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2142 if (hw == NULL) {
2143 ALOGE("Attempt to unblank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002144 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002145 ALOGW("Attempt to unblank virtual display");
2146 } else {
2147 mFlinger.onScreenAcquired(hw);
2148 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002149 return true;
2150 }
2151 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002152 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2153 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002154}
2155
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002156void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002157 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002158 SurfaceFlinger& mFlinger;
2159 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002160 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002161 MessageScreenReleased(SurfaceFlinger& flinger,
2162 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002163 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002164 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2165 if (hw == NULL) {
2166 ALOGE("Attempt to blank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002167 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002168 ALOGW("Attempt to blank virtual display");
2169 } else {
2170 mFlinger.onScreenReleased(hw);
2171 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002172 return true;
2173 }
2174 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002175 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2176 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002177}
2178
2179// ---------------------------------------------------------------------------
2180
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002181status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2182{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002183 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002184
Mathias Agopianbd115332013-04-18 16:41:04 -07002185 IPCThreadState* ipc = IPCThreadState::self();
2186 const int pid = ipc->getCallingPid();
2187 const int uid = ipc->getCallingUid();
2188 if ((uid != AID_SHELL) &&
2189 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002190 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002191 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002192 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002193 // Try to get the main lock, but don't insist if we can't
2194 // (this would indicate SF is stuck, but we want to be able to
2195 // print something in dumpsys).
2196 int retry = 3;
2197 while (mStateLock.tryLock()<0 && --retry>=0) {
2198 usleep(1000000);
2199 }
2200 const bool locked(retry >= 0);
2201 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002202 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002203 "SurfaceFlinger appears to be unresponsive, "
2204 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002205 }
2206
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002207 bool dumpAll = true;
2208 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002209 size_t numArgs = args.size();
2210 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002211 if ((index < numArgs) &&
2212 (args[index] == String16("--list"))) {
2213 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002214 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002215 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002216 }
2217
2218 if ((index < numArgs) &&
2219 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002220 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002221 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002222 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002223 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002224
2225 if ((index < numArgs) &&
2226 (args[index] == String16("--latency-clear"))) {
2227 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002228 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002229 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002230 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002231 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002232
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002233 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002234 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002235 }
2236
Mathias Agopian9795c422009-08-26 16:36:26 -07002237 if (locked) {
2238 mStateLock.unlock();
2239 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002240 }
2241 write(fd, result.string(), result.size());
2242 return NO_ERROR;
2243}
2244
Dan Stozac7014012014-02-14 15:03:43 -08002245void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2246 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002247{
2248 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2249 const size_t count = currentLayers.size();
2250 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002251 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002252 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002253 }
2254}
2255
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002256void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002257 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002258{
2259 String8 name;
2260 if (index < args.size()) {
2261 name = String8(args[index]);
2262 index++;
2263 }
2264
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002265 const nsecs_t period =
2266 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002267 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002268
2269 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002270 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002271 } else {
2272 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2273 const size_t count = currentLayers.size();
2274 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002275 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002276 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002277 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002278 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002279 }
2280 }
2281}
2282
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002283void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002284 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002285{
2286 String8 name;
2287 if (index < args.size()) {
2288 name = String8(args[index]);
2289 index++;
2290 }
2291
2292 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2293 const size_t count = currentLayers.size();
2294 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002295 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002296 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002297 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002298 }
2299 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002300
Svetoslavd85084b2014-03-20 10:28:31 -07002301 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002302}
2303
Jamie Gennis6547ff42013-07-16 20:12:42 -07002304// This should only be called from the main thread. Otherwise it would need
2305// the lock and should use mCurrentState rather than mDrawingState.
2306void SurfaceFlinger::logFrameStats() {
2307 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2308 const size_t count = drawingLayers.size();
2309 for (size_t i=0 ; i<count ; i++) {
2310 const sp<Layer>& layer(drawingLayers[i]);
2311 layer->logFrameStats();
2312 }
2313
2314 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2315}
2316
Andy McFadden4803b742012-09-24 19:07:20 -07002317/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2318{
2319 static const char* config =
2320 " [sf"
2321#ifdef NO_RGBX_8888
2322 " NO_RGBX_8888"
2323#endif
2324#ifdef HAS_CONTEXT_PRIORITY
2325 " HAS_CONTEXT_PRIORITY"
2326#endif
2327#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2328 " NEVER_DEFAULT_TO_ASYNC_MODE"
2329#endif
2330#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2331 " TARGET_DISABLE_TRIPLE_BUFFERING"
2332#endif
2333 "]";
2334 result.append(config);
2335}
2336
Mathias Agopian74d211a2013-04-22 16:55:35 +02002337void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2338 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002339{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002340 bool colorize = false;
2341 if (index < args.size()
2342 && (args[index] == String16("--color"))) {
2343 colorize = true;
2344 index++;
2345 }
2346
2347 Colorizer colorizer(colorize);
2348
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002349 // figure out if we're stuck somewhere
2350 const nsecs_t now = systemTime();
2351 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2352 const nsecs_t inTransaction(mDebugInTransaction);
2353 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2354 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2355
2356 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002357 * Dump library configuration.
2358 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002359
2360 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002361 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002362 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002363 appendSfConfigString(result);
2364 appendUiConfigString(result);
2365 appendGuiConfigString(result);
2366 result.append("\n");
2367
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002368 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002369 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002370 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002371 result.append(SyncFeatures::getInstance().toString());
2372 result.append("\n");
2373
Andy McFadden4803b742012-09-24 19:07:20 -07002374 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002375 * Dump the visible layer list
2376 */
2377 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2378 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002379 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002380 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002381 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002382 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002383 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002384 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002385 }
2386
2387 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002388 * Dump Display state
2389 */
2390
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002391 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002392 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002393 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002394 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2395 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002396 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002397 }
2398
2399 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002400 * Dump SurfaceFlinger global state
2401 */
2402
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002403 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002404 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002405 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002406
Mathias Agopian888c8222012-08-04 21:10:38 -07002407 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002408 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002409
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002410 colorizer.bold(result);
2411 result.appendFormat("EGL implementation : %s\n",
2412 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2413 colorizer.reset(result);
2414 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002415 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002416
Mathias Agopian875d8e12013-06-07 15:35:48 -07002417 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002418
Mathias Agopian42977342012-08-05 00:40:46 -07002419 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002420 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002421 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002422 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002423 " last eglSwapBuffers() time: %f us\n"
2424 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002425 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002426 " refresh-rate : %f fps\n"
2427 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002428 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002429 " gpu_to_cpu_unsupported : %d\n"
2430 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002431 mLastSwapBufferTime/1000.0,
2432 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002433 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002434 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2435 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002436 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002437 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002438
Mathias Agopian74d211a2013-04-22 16:55:35 +02002439 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002440 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002441
Mathias Agopian74d211a2013-04-22 16:55:35 +02002442 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002443 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002444
2445 /*
2446 * VSYNC state
2447 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002448 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002449
2450 /*
2451 * Dump HWComposer state
2452 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002453 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002454 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002455 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002456 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002457 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002458 (mDebugDisableHWC || mDebugRegion || mDaltonize
2459 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002460 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002461
2462 /*
2463 * Dump gralloc state
2464 */
2465 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2466 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002467}
2468
Mathias Agopian13127d82013-03-05 17:47:11 -08002469const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002470SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002471 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002472 wp<IBinder> dpy;
2473 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2474 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2475 dpy = mDisplays.keyAt(i);
2476 break;
2477 }
2478 }
2479 if (dpy == NULL) {
2480 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2481 // Just use the primary display so we have something to return
2482 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2483 }
2484 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002485}
2486
Keun young Park63f165f2012-08-31 10:53:36 -07002487bool SurfaceFlinger::startDdmConnection()
2488{
2489 void* libddmconnection_dso =
2490 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2491 if (!libddmconnection_dso) {
2492 return false;
2493 }
2494 void (*DdmConnection_start)(const char* name);
2495 DdmConnection_start =
2496 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2497 if (!DdmConnection_start) {
2498 dlclose(libddmconnection_dso);
2499 return false;
2500 }
2501 (*DdmConnection_start)(getServiceName());
2502 return true;
2503}
2504
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002505status_t SurfaceFlinger::onTransact(
2506 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2507{
2508 switch (code) {
2509 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002510 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002511 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002512 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002513 case BLANK:
2514 case UNBLANK:
Svetoslavd85084b2014-03-20 10:28:31 -07002515 case CLEAR_ANIMATION_FRAME_STATS:
2516 case GET_ANIMATION_FRAME_STATS:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002517 {
2518 // codes that require permission check
2519 IPCThreadState* ipc = IPCThreadState::self();
2520 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002521 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002522 if ((uid != AID_GRAPHICS) &&
2523 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002524 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002525 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2526 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002527 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002528 break;
2529 }
2530 case CAPTURE_SCREEN:
2531 {
2532 // codes that require permission check
2533 IPCThreadState* ipc = IPCThreadState::self();
2534 const int pid = ipc->getCallingPid();
2535 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002536 if ((uid != AID_GRAPHICS) &&
2537 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002538 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002539 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2540 return PERMISSION_DENIED;
2541 }
2542 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002543 }
2544 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002545
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002546 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2547 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002548 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002549 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002550 IPCThreadState* ipc = IPCThreadState::self();
2551 const int pid = ipc->getCallingPid();
2552 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002553 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002554 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002555 return PERMISSION_DENIED;
2556 }
2557 int n;
2558 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002559 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002560 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002561 return NO_ERROR;
2562 case 1002: // SHOW_UPDATES
2563 n = data.readInt32();
2564 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002565 invalidateHwcGeometry();
2566 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002567 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002568 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002569 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002570 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002571 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002572 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002573 setTransactionFlags(
2574 eTransactionNeeded|
2575 eDisplayTransactionNeeded|
2576 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002577 return NO_ERROR;
2578 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002579 case 1006:{ // send empty update
2580 signalRefresh();
2581 return NO_ERROR;
2582 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002583 case 1008: // toggle use of hw composer
2584 n = data.readInt32();
2585 mDebugDisableHWC = n ? 1 : 0;
2586 invalidateHwcGeometry();
2587 repaintEverything();
2588 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002589 case 1009: // toggle use of transform hint
2590 n = data.readInt32();
2591 mDebugDisableTransformHint = n ? 1 : 0;
2592 invalidateHwcGeometry();
2593 repaintEverything();
2594 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002595 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002596 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002597 reply->writeInt32(0);
2598 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002599 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002600 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002601 return NO_ERROR;
2602 case 1013: {
2603 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002604 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2605 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002606 return NO_ERROR;
2607 }
2608 case 1014: {
2609 // daltonize
2610 n = data.readInt32();
2611 switch (n % 10) {
2612 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2613 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2614 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2615 }
2616 if (n >= 10) {
2617 mDaltonizer.setMode(Daltonizer::correction);
2618 } else {
2619 mDaltonizer.setMode(Daltonizer::simulation);
2620 }
2621 mDaltonize = n > 0;
2622 invalidateHwcGeometry();
2623 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002624 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002625 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002626 case 1015: {
2627 // apply a color matrix
2628 n = data.readInt32();
2629 mHasColorMatrix = n ? 1 : 0;
2630 if (n) {
2631 // color matrix is sent as mat3 matrix followed by vec3
2632 // offset, then packed into a mat4 where the last row is
2633 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002634 for (size_t i = 0 ; i < 4; i++) {
2635 for (size_t j = 0; j < 4; j++) {
2636 mColorMatrix[i][j] = data.readFloat();
2637 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002638 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002639 } else {
2640 mColorMatrix = mat4();
2641 }
2642 invalidateHwcGeometry();
2643 repaintEverything();
2644 return NO_ERROR;
2645 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002646 // This is an experimental interface
2647 // Needs to be shifted to proper binder interface when we productize
2648 case 1016: {
2649 mPrimaryDispSync.setLowPowerMode(true);
2650 return NO_ERROR;
2651 }
2652 case 1017: {
2653 mPrimaryDispSync.setLowPowerMode(false);
2654 return NO_ERROR;
2655 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002656 }
2657 }
2658 return err;
2659}
2660
Mathias Agopian53331da2011-08-22 21:44:41 -07002661void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002662 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002663 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002664}
2665
Mathias Agopian59119e62010-10-11 12:37:43 -07002666// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002667// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002668// ---------------------------------------------------------------------------
2669
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002670/* The code below is here to handle b/8734824
2671 *
2672 * We create a IGraphicBufferProducer wrapper that forwards all calls
2673 * to the calling binder thread, where they are executed. This allows
2674 * the calling thread to be reused (on the other side) and not
2675 * depend on having "enough" binder threads to handle the requests.
2676 *
2677 */
2678
2679class GraphicProducerWrapper : public BBinder, public MessageHandler {
2680 sp<IGraphicBufferProducer> impl;
2681 sp<Looper> looper;
2682 status_t result;
2683 bool exitPending;
2684 bool exitRequested;
2685 mutable Barrier barrier;
2686 volatile int32_t memoryBarrier;
2687 uint32_t code;
2688 Parcel const* data;
2689 Parcel* reply;
2690
2691 enum {
2692 MSG_API_CALL,
2693 MSG_EXIT
2694 };
2695
2696 /*
2697 * this is called by our "fake" BpGraphicBufferProducer. We package the
2698 * data and reply Parcel and forward them to the calling thread.
2699 */
2700 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08002701 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002702 this->code = code;
2703 this->data = &data;
2704 this->reply = reply;
2705 android_atomic_acquire_store(0, &memoryBarrier);
2706 if (exitPending) {
2707 // if we've exited, we run the message synchronously right here
2708 handleMessage(Message(MSG_API_CALL));
2709 } else {
2710 barrier.close();
2711 looper->sendMessage(this, Message(MSG_API_CALL));
2712 barrier.wait();
2713 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08002714 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002715 }
2716
2717 /*
2718 * here we run on the binder calling thread. All we've got to do is
2719 * call the real BpGraphicBufferProducer.
2720 */
2721 virtual void handleMessage(const Message& message) {
2722 android_atomic_release_load(&memoryBarrier);
2723 if (message.what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08002724 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002725 barrier.open();
2726 } else if (message.what == MSG_EXIT) {
2727 exitRequested = true;
2728 }
2729 }
2730
2731public:
2732 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2733 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2734 exitPending(false), exitRequested(false) {
2735 }
2736
2737 status_t waitForResponse() {
2738 do {
2739 looper->pollOnce(-1);
2740 } while (!exitRequested);
2741 return result;
2742 }
2743
2744 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002745 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002746 exitPending = true;
2747 looper->sendMessage(this, Message(MSG_EXIT));
2748 }
2749};
2750
2751
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002752status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2753 const sp<IGraphicBufferProducer>& producer,
2754 uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002755 uint32_t minLayerZ, uint32_t maxLayerZ,
2756 bool useIdentityTransform) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002757
2758 if (CC_UNLIKELY(display == 0))
2759 return BAD_VALUE;
2760
2761 if (CC_UNLIKELY(producer == 0))
2762 return BAD_VALUE;
2763
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002764 // if we have secure windows on this display, never allow the screen capture
2765 // unless the producer interface is local (i.e.: we can take a screenshot for
2766 // ourselves).
2767 if (!producer->asBinder()->localBinder()) {
2768 Mutex::Autolock _l(mStateLock);
2769 sp<const DisplayDevice> hw(getDisplayDevice(display));
2770 if (hw->getSecureLayerVisible()) {
2771 ALOGW("FB is protected: PERMISSION_DENIED");
2772 return PERMISSION_DENIED;
2773 }
2774 }
2775
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002776 class MessageCaptureScreen : public MessageBase {
2777 SurfaceFlinger* flinger;
2778 sp<IBinder> display;
2779 sp<IGraphicBufferProducer> producer;
2780 uint32_t reqWidth, reqHeight;
2781 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08002782 bool useIdentityTransform;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002783 status_t result;
2784 public:
2785 MessageCaptureScreen(SurfaceFlinger* flinger,
2786 const sp<IBinder>& display,
2787 const sp<IGraphicBufferProducer>& producer,
2788 uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002789 uint32_t minLayerZ, uint32_t maxLayerZ,
2790 bool useIdentityTransform)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002791 : flinger(flinger), display(display), producer(producer),
2792 reqWidth(reqWidth), reqHeight(reqHeight),
2793 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08002794 useIdentityTransform(useIdentityTransform),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002795 result(PERMISSION_DENIED)
2796 {
2797 }
2798 status_t getResult() const {
2799 return result;
2800 }
2801 virtual bool handler() {
2802 Mutex::Autolock _l(flinger->mStateLock);
2803 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08002804 result = flinger->captureScreenImplLocked(hw, producer,
2805 reqWidth, reqHeight, minLayerZ, maxLayerZ,
2806 useIdentityTransform);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002807 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002808 return true;
2809 }
2810 };
2811
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002812 // make sure to process transactions before screenshots -- a transaction
2813 // might already be pending but scheduled for VSYNC; this guarantees we
2814 // will handle it before the screenshot. When VSYNC finally arrives
2815 // the scheduled transaction will be a no-op. If no transactions are
2816 // scheduled at this time, this will end-up being a no-op as well.
2817 mEventQueue.invalidateTransactionNow();
2818
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002819 // this creates a "fake" BBinder which will serve as a "fake" remote
2820 // binder to receive the marshaled calls and forward them to the
2821 // real remote (a BpGraphicBufferProducer)
2822 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2823
2824 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2825 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002826 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002827 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac7014012014-02-14 15:03:43 -08002828 reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002829
2830 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002831 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002832 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002833 }
2834 return res;
2835}
2836
Mathias Agopian180f10d2013-04-10 22:55:41 -07002837
2838void SurfaceFlinger::renderScreenImplLocked(
2839 const sp<const DisplayDevice>& hw,
2840 uint32_t reqWidth, uint32_t reqHeight,
2841 uint32_t minLayerZ, uint32_t maxLayerZ,
Dan Stozac7014012014-02-14 15:03:43 -08002842 bool yswap, bool useIdentityTransform)
Mathias Agopian180f10d2013-04-10 22:55:41 -07002843{
2844 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002845 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002846
2847 // get screen geometry
2848 const uint32_t hw_w = hw->getWidth();
2849 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002850 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2851
2852 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002853 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002854
2855 // set-up our viewport
Mathias Agopian3f844832013-08-07 21:24:32 -07002856 engine.setViewportAndProjection(reqWidth, reqHeight, hw_w, hw_h, yswap);
2857 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002858
2859 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002860 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002861
2862 const LayerVector& layers( mDrawingState.layersSortedByZ );
2863 const size_t count = layers.size();
2864 for (size_t i=0 ; i<count ; ++i) {
2865 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002866 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002867 if (state.layerStack == hw->getLayerStack()) {
2868 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2869 if (layer->isVisible()) {
2870 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08002871 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002872 if (filtering) layer->setFiltering(false);
2873 }
2874 }
2875 }
2876 }
2877
2878 // compositionComplete is needed for older driver
2879 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002880 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002881}
2882
2883
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002884status_t SurfaceFlinger::captureScreenImplLocked(
2885 const sp<const DisplayDevice>& hw,
2886 const sp<IGraphicBufferProducer>& producer,
2887 uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002888 uint32_t minLayerZ, uint32_t maxLayerZ,
2889 bool useIdentityTransform)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002890{
2891 ATRACE_CALL();
2892
Mathias Agopian180f10d2013-04-10 22:55:41 -07002893 // get screen geometry
2894 const uint32_t hw_w = hw->getWidth();
2895 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002896
Mathias Agopian180f10d2013-04-10 22:55:41 -07002897 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2898 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2899 reqWidth, reqHeight, hw_w, hw_h);
2900 return BAD_VALUE;
2901 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002902
Mathias Agopian180f10d2013-04-10 22:55:41 -07002903 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2904 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2905
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002906 // create a surface (because we're a producer, and we need to
2907 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07002908 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002909 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002910
2911 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002912 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002913 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2914 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002915
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002916 int err = 0;
2917 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07002918 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002919 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2920 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002921
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002922 if (err == NO_ERROR) {
2923 ANativeWindowBuffer* buffer;
2924 /* TODO: Once we have the sync framework everywhere this can use
2925 * server-side waits on the fence that dequeueBuffer returns.
2926 */
2927 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2928 if (result == NO_ERROR) {
2929 // create an EGLImage from the buffer so we can later
2930 // turn it into a texture
2931 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2932 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2933 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002934 // this binds the given EGLImage as a framebuffer for the
2935 // duration of this scope.
2936 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2937 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002938 // this will in fact render into our dequeued buffer
2939 // via an FBO, which means we didn't have to create
2940 // an EGLSurface and therefore we're not
2941 // dependent on the context's EGLConfig.
2942 renderScreenImplLocked(hw, reqWidth, reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002943 minLayerZ, maxLayerZ, true, useIdentityTransform);
Mathias Agopiand5556842013-09-19 17:08:37 -07002944
Andy McFadden2d8d1202013-10-09 16:38:02 -07002945 // Create a sync point and wait on it, so we know the buffer is
2946 // ready before we pass it along. We can't trivially call glFlush(),
2947 // so we use a wait flag instead.
2948 // TODO: pass a sync fd to queueBuffer() and let the consumer wait.
2949 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
2950 if (sync != EGL_NO_SYNC_KHR) {
2951 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
2952 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
2953 EGLint eglErr = eglGetError();
2954 eglDestroySyncKHR(mEGLDisplay, sync);
2955 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
2956 ALOGW("captureScreen: fence wait timed out");
2957 } else {
2958 ALOGW_IF(eglErr != EGL_SUCCESS,
2959 "captureScreen: error waiting on EGL fence: %#x", eglErr);
2960 }
2961 } else {
2962 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
2963 // not fatal
2964 }
2965
Mathias Agopiand5556842013-09-19 17:08:37 -07002966 if (DEBUG_SCREENSHOTS) {
2967 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
2968 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
2969 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
2970 hw, minLayerZ, maxLayerZ);
2971 delete [] pixels;
2972 }
2973
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002974 } else {
2975 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2976 result = INVALID_OPERATION;
2977 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002978 // destroy our image
2979 eglDestroyImageKHR(mEGLDisplay, image);
2980 } else {
2981 result = BAD_VALUE;
2982 }
2983 window->queueBuffer(window, buffer, -1);
2984 }
2985 } else {
2986 result = BAD_VALUE;
2987 }
2988 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2989 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002990
Mathias Agopian74c40c02010-09-29 13:02:36 -07002991 return result;
2992}
2993
Mathias Agopiand5556842013-09-19 17:08:37 -07002994void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
2995 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07002996 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07002997 for (size_t y=0 ; y<h ; y++) {
2998 uint32_t const * p = (uint32_t const *)vaddr + y*s;
2999 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003000 if (p[x] != 0xFF000000) return;
3001 }
3002 }
3003 ALOGE("*** we just took a black screenshot ***\n"
3004 "requested minz=%d, maxz=%d, layerStack=%d",
3005 minLayerZ, maxLayerZ, hw->getLayerStack());
3006 const LayerVector& layers( mDrawingState.layersSortedByZ );
3007 const size_t count = layers.size();
3008 for (size_t i=0 ; i<count ; ++i) {
3009 const sp<Layer>& layer(layers[i]);
3010 const Layer::State& state(layer->getDrawingState());
3011 const bool visible = (state.layerStack == hw->getLayerStack())
3012 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3013 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003014 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003015 visible ? '+' : '-',
3016 i, layer->getName().string(), state.layerStack, state.z,
3017 layer->isVisible(), state.flags, state.alpha);
3018 }
3019 }
3020}
3021
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003022// ---------------------------------------------------------------------------
3023
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003024SurfaceFlinger::LayerVector::LayerVector() {
3025}
3026
3027SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003028 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003029}
3030
3031int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3032 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003033{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003034 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003035 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3036 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003037
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003038 uint32_t ls = l->getCurrentState().layerStack;
3039 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003040 if (ls != rs)
3041 return ls - rs;
3042
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003043 uint32_t lz = l->getCurrentState().z;
3044 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003045 if (lz != rz)
3046 return lz - rz;
3047
3048 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003049}
3050
3051// ---------------------------------------------------------------------------
3052
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003053SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
3054 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003055}
3056
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003057SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08003058 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003059 viewport.makeInvalid();
3060 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003061}
3062
3063// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003064
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003065}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003066
3067
3068#if defined(__gl_h_)
3069#error "don't include gl/gl.h in this file"
3070#endif
3071
3072#if defined(__gl2_h_)
3073#error "don't include gl2/gl2.h in this file"
3074#endif