blob: 06435abd7d7182c03347113b6aa3d12ada49dc01 [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);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700440 hw->setPowerMode(HWC_POWER_MODE_NORMAL);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700441 }
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
Dan Stoza7f7da322014-05-02 15:26:25 -0700505status_t SurfaceFlinger::getDisplayConfigs(const sp<IBinder>& display,
506 Vector<DisplayInfo>* configs) {
507 if (configs == NULL) {
508 return BAD_VALUE;
509 }
510
Jesse Hall692c7232012-11-08 15:41:56 -0800511 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700512 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800513 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700514 type = i;
515 break;
516 }
517 }
518
519 if (type < 0) {
520 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700521 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700522
Mathias Agopian8b736f12012-08-13 17:54:26 -0700523 // TODO: Not sure if display density should handled by SF any longer
524 class Density {
525 static int getDensityFromProperty(char const* propName) {
526 char property[PROPERTY_VALUE_MAX];
527 int density = 0;
528 if (property_get(propName, property, NULL) > 0) {
529 density = atoi(property);
530 }
531 return density;
532 }
533 public:
534 static int getEmuDensity() {
535 return getDensityFromProperty("qemu.sf.lcd_density"); }
536 static int getBuildDensity() {
537 return getDensityFromProperty("ro.sf.lcd_density"); }
538 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700539
Dan Stoza7f7da322014-05-02 15:26:25 -0700540 configs->clear();
Mathias Agopian1604f772012-09-18 21:54:42 -0700541
Dan Stoza7f7da322014-05-02 15:26:25 -0700542 const Vector<HWComposer::DisplayConfig>& hwConfigs =
543 getHwComposer().getConfigs(type);
544 for (size_t c = 0; c < hwConfigs.size(); ++c) {
545 const HWComposer::DisplayConfig& hwConfig = hwConfigs[c];
546 DisplayInfo info = DisplayInfo();
547
548 float xdpi = hwConfig.xdpi;
549 float ydpi = hwConfig.ydpi;
550
551 if (type == DisplayDevice::DISPLAY_PRIMARY) {
552 // The density of the device is provided by a build property
553 float density = Density::getBuildDensity() / 160.0f;
554 if (density == 0) {
555 // the build doesn't provide a density -- this is wrong!
556 // use xdpi instead
557 ALOGE("ro.sf.lcd_density must be defined as a build property");
558 density = xdpi / 160.0f;
559 }
560 if (Density::getEmuDensity()) {
561 // if "qemu.sf.lcd_density" is specified, it overrides everything
562 xdpi = ydpi = density = Density::getEmuDensity();
563 density /= 160.0f;
564 }
565 info.density = density;
566
567 // TODO: this needs to go away (currently needed only by webkit)
568 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
569 info.orientation = hw->getOrientation();
570 } else {
571 // TODO: where should this value come from?
572 static const int TV_DENSITY = 213;
573 info.density = TV_DENSITY / 160.0f;
574 info.orientation = 0;
575 }
576
577 info.w = hwConfig.width;
578 info.h = hwConfig.height;
579 info.xdpi = xdpi;
580 info.ydpi = ydpi;
581 info.fps = float(1e9 / hwConfig.refresh);
582
583 // All non-virtual displays are currently considered secure.
584 info.secure = true;
585
586 configs->push_back(info);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700587 }
588
Dan Stoza7f7da322014-05-02 15:26:25 -0700589 return NO_ERROR;
590}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700591
Dan Stoza7f7da322014-05-02 15:26:25 -0700592int SurfaceFlinger::getActiveConfig(const sp<IBinder>& display) {
593 return 0;
594}
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700595
Dan Stoza7f7da322014-05-02 15:26:25 -0700596status_t SurfaceFlinger::setActiveConfig(const sp<IBinder>& display, int id) {
Mathias Agopian888c8222012-08-04 21:10:38 -0700597 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700598}
599
Svetoslavd85084b2014-03-20 10:28:31 -0700600status_t SurfaceFlinger::clearAnimationFrameStats() {
601 Mutex::Autolock _l(mStateLock);
602 mAnimFrameTracker.clearStats();
603 return NO_ERROR;
604}
605
606status_t SurfaceFlinger::getAnimationFrameStats(FrameStats* outStats) const {
607 Mutex::Autolock _l(mStateLock);
608 mAnimFrameTracker.getStats(outStats);
609 return NO_ERROR;
610}
611
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800612// ----------------------------------------------------------------------------
613
614sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800615 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700616}
617
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800618// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800619
620void SurfaceFlinger::waitForEvent() {
621 mEventQueue.waitMessage();
622}
623
624void SurfaceFlinger::signalTransaction() {
625 mEventQueue.invalidate();
626}
627
628void SurfaceFlinger::signalLayerUpdate() {
629 mEventQueue.invalidate();
630}
631
632void SurfaceFlinger::signalRefresh() {
633 mEventQueue.refresh();
634}
635
636status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800637 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800638 return mEventQueue.postMessage(msg, reltime);
639}
640
641status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800642 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800643 status_t res = mEventQueue.postMessage(msg, reltime);
644 if (res == NO_ERROR) {
645 msg->wait();
646 }
647 return res;
648}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800649
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700650void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700651 do {
652 waitForEvent();
653 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800654}
655
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700656void SurfaceFlinger::enableHardwareVsync() {
657 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700658 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700659 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700660 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
661 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700662 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700663 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700664}
665
Jesse Hall948fe0c2013-10-14 12:56:09 -0700666void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700667 Mutex::Autolock _l(mHWVsyncLock);
668
Jesse Hall948fe0c2013-10-14 12:56:09 -0700669 if (makeAvailable) {
670 mHWVsyncAvailable = true;
671 } else if (!mHWVsyncAvailable) {
672 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
673 return;
674 }
675
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700676 const nsecs_t period =
677 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
678
679 mPrimaryDispSync.reset();
680 mPrimaryDispSync.setPeriod(period);
681
682 if (!mPrimaryHWVsyncEnabled) {
683 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700684 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
685 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700686 mPrimaryHWVsyncEnabled = true;
687 }
688}
689
Jesse Hall948fe0c2013-10-14 12:56:09 -0700690void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700691 Mutex::Autolock _l(mHWVsyncLock);
692 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700693 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
694 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700695 mPrimaryDispSync.endResync();
696 mPrimaryHWVsyncEnabled = false;
697 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700698 if (makeUnavailable) {
699 mHWVsyncAvailable = false;
700 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700701}
702
703void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700704 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700705
Jamie Gennisd1700752013-10-14 12:22:52 -0700706 { // Scope for the lock
707 Mutex::Autolock _l(mHWVsyncLock);
708 if (type == 0 && mPrimaryHWVsyncEnabled) {
709 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700710 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700711 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700712
713 if (needsHwVsync) {
714 enableHardwareVsync();
715 } else {
716 disableHardwareVsync(false);
717 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700718}
719
720void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
721 if (mEventThread == NULL) {
722 // This is a temporary workaround for b/7145521. A non-null pointer
723 // does not mean EventThread has finished initializing, so this
724 // is not a correct fix.
725 ALOGW("WARNING: EventThread not started, ignoring hotplug");
726 return;
727 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700728
Jesse Hall9e663de2013-08-16 14:28:37 -0700729 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700730 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800731 if (connected) {
732 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700733 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800734 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
735 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700736 }
737 setTransactionFlags(eDisplayTransactionNeeded);
738
Andy McFadden9e9689c2012-10-10 18:17:51 -0700739 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700740 }
Mathias Agopian86303202012-07-24 22:46:10 -0700741}
742
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700743void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700744 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700745 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700746}
747
Mathias Agopian4fec8732012-06-29 14:12:52 -0700748void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800749 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800750 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700751 case MessageQueue::TRANSACTION:
752 handleMessageTransaction();
753 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700754 case MessageQueue::INVALIDATE:
755 handleMessageTransaction();
756 handleMessageInvalidate();
757 signalRefresh();
758 break;
759 case MessageQueue::REFRESH:
760 handleMessageRefresh();
761 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800762 }
763}
764
Mathias Agopian4fec8732012-06-29 14:12:52 -0700765void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700766 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700767 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700768 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700769 }
770}
771
772void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700773 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700774 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700775}
776
777void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700778 ATRACE_CALL();
779 preComposition();
780 rebuildLayerStacks();
781 setUpHWComposer();
782 doDebugFlashRegions();
783 doComposition();
784 postComposition();
785}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700786
Mathias Agopiancd60f992012-08-16 16:28:27 -0700787void SurfaceFlinger::doDebugFlashRegions()
788{
789 // is debugging enabled
790 if (CC_LIKELY(!mDebugRegion))
791 return;
792
793 const bool repaintEverything = mRepaintEverything;
794 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
795 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700796 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700797 // transform the dirty region into this screen's coordinate space
798 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
799 if (!dirtyRegion.isEmpty()) {
800 // redraw the whole screen
801 doComposeSurfaces(hw, Region(hw->bounds()));
802
803 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700804 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700805 RenderEngine& engine(getRenderEngine());
806 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
807
Mathias Agopiancd60f992012-08-16 16:28:27 -0700808 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700809 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700810 }
811 }
812 }
813
814 postFramebuffer();
815
816 if (mDebugRegion > 1) {
817 usleep(mDebugRegion * 1000);
818 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700819
820 HWComposer& hwc(getHwComposer());
821 if (hwc.initCheck() == NO_ERROR) {
822 status_t err = hwc.prepare();
823 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
824 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700825}
826
827void SurfaceFlinger::preComposition()
828{
829 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700830 const LayerVector& layers(mDrawingState.layersSortedByZ);
831 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700832 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700833 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700834 needExtraInvalidate = true;
835 }
836 }
837 if (needExtraInvalidate) {
838 signalLayerUpdate();
839 }
840}
841
842void SurfaceFlinger::postComposition()
843{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700844 const LayerVector& layers(mDrawingState.layersSortedByZ);
845 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700846 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700847 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700848 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800849
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700850 const HWComposer& hwc = getHwComposer();
851 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
852
853 if (presentFence->isValid()) {
854 if (mPrimaryDispSync.addPresentFence(presentFence)) {
855 enableHardwareVsync();
856 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700857 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700858 }
859 }
860
861 if (runningWithoutSyncFramework) {
862 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700863 if (hw->isDisplayOn()) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700864 enableHardwareVsync();
865 }
866 }
867
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800868 if (mAnimCompositionPending) {
869 mAnimCompositionPending = false;
870
Jesse Halla9a1b002013-02-27 16:39:25 -0800871 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800872 mAnimFrameTracker.setActualPresentFence(presentFence);
873 } else {
874 // The HWC doesn't support present fences, so use the refresh
875 // timestamp instead.
876 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
877 mAnimFrameTracker.setActualPresentTime(presentTime);
878 }
879 mAnimFrameTracker.advanceFrame();
880 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700881}
882
883void SurfaceFlinger::rebuildLayerStacks() {
884 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700885 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700886 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700887 mVisibleRegionsDirty = false;
888 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700889
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700890 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700891 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700892 Region opaqueRegion;
893 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800894 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700895 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700896 const Transform& tr(hw->getTransform());
897 const Rect bounds(hw->getBounds());
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700898 if (hw->isDisplayOn()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700899 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700900 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700901
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700902 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700903 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700904 const sp<Layer>& layer(layers[i]);
905 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700906 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700907 Region drawRegion(tr.transform(
908 layer->visibleNonTransparentRegion));
909 drawRegion.andSelf(bounds);
910 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700911 layersSortedByZ.add(layer);
912 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700913 }
914 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700915 }
Mathias Agopian42977342012-08-05 00:40:46 -0700916 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700917 hw->undefinedRegion.set(bounds);
918 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
919 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700920 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700921 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700922}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700923
Mathias Agopiancd60f992012-08-16 16:28:27 -0700924void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700925 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Dan Stoza71433162014-02-04 16:22:36 -0800926 bool mustRecompose =
927 !(mDisplays[dpy]->getDirtyRegion(false).isEmpty());
928 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700929 }
930
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700931 HWComposer& hwc(getHwComposer());
932 if (hwc.initCheck() == NO_ERROR) {
933 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700934 if (CC_UNLIKELY(mHwWorkListDirty)) {
935 mHwWorkListDirty = false;
936 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
937 sp<const DisplayDevice> hw(mDisplays[dpy]);
938 const int32_t id = hw->getHwcDisplayId();
939 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800940 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700941 hw->getVisibleLayersSortedByZ());
942 const size_t count = currentLayers.size();
943 if (hwc.createWorkList(id, count) == NO_ERROR) {
944 HWComposer::LayerListIterator cur = hwc.begin(id);
945 const HWComposer::LayerListIterator end = hwc.end(id);
946 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800947 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700948 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -0700949 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -0700950 cur->setSkip(true);
951 }
952 }
953 }
954 }
955 }
956 }
957
958 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700959 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700960 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700961 const int32_t id = hw->getHwcDisplayId();
962 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800963 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700964 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700965 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700966 HWComposer::LayerListIterator cur = hwc.begin(id);
967 const HWComposer::LayerListIterator end = hwc.end(id);
968 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
969 /*
970 * update the per-frame h/w composer data for each layer
971 * and build the transparent region of the FB
972 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800973 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700974 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700975 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700976 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700977 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700978
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700979 status_t err = hwc.prepare();
980 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700981
982 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
983 sp<const DisplayDevice> hw(mDisplays[dpy]);
984 hw->prepareFrame(hwc);
985 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700986 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700987}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700988
Mathias Agopiancd60f992012-08-16 16:28:27 -0700989void SurfaceFlinger::doComposition() {
990 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700991 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700992 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700993 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Prashant Malani2c9b11f2014-05-25 01:36:31 -0700994 if (hw->isDisplayOn()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700995 // transform the dirty region into this screen's coordinate space
996 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800997
998 // repaint the framebuffer (if needed)
999 doDisplayComposition(hw, dirtyRegion);
1000
Mathias Agopiancd60f992012-08-16 16:28:27 -07001001 hw->dirtyRegion.clear();
1002 hw->flip(hw->swapRegion);
1003 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -07001004 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001005 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -07001006 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001007 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001008 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001009}
1010
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001011void SurfaceFlinger::postFramebuffer()
1012{
Mathias Agopian841cde52012-03-01 15:44:37 -08001013 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -08001014
Mathias Agopiana44b0412011-10-16 18:46:35 -07001015 const nsecs_t now = systemTime();
1016 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -07001017
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001018 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -07001019 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -07001020 if (!hwc.supportsFramebufferTarget()) {
1021 // EGL spec says:
1022 // "surface must be bound to the calling thread's current context,
1023 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -07001024 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -07001025 }
Mathias Agopiane60b0682012-08-21 23:34:09 -07001026 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001027 }
1028
Mathias Agopian6da15f42013-09-25 20:40:07 -07001029 // make the default display current because the VirtualDisplayDevice code cannot
1030 // deal with dequeueBuffer() being called outside of the composition loop; however
1031 // the code below can call glFlush() which is allowed (and does in some case) call
1032 // dequeueBuffer().
1033 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
1034
Mathias Agopian92a979a2012-08-02 18:32:23 -07001035 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001036 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -08001037 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -07001038 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001039 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001040 int32_t id = hw->getHwcDisplayId();
1041 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001042 HWComposer::LayerListIterator cur = hwc.begin(id);
1043 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001044 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001045 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001046 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001047 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001048 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001049 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001050 }
Jesse Hallef194142012-06-14 14:45:17 -07001051 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001052 }
1053
Mathias Agopiana44b0412011-10-16 18:46:35 -07001054 mLastSwapBufferTime = systemTime() - now;
1055 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001056
1057 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1058 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1059 logFrameStats();
1060 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001061}
1062
Mathias Agopian87baae12012-07-31 12:38:26 -07001063void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001064{
Mathias Agopian841cde52012-03-01 15:44:37 -08001065 ATRACE_CALL();
1066
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001067 // here we keep a copy of the drawing state (that is the state that's
1068 // going to be overwritten by handleTransactionLocked()) outside of
1069 // mStateLock so that the side-effects of the State assignment
1070 // don't happen with mStateLock held (which can cause deadlocks).
1071 State drawingState(mDrawingState);
1072
Mathias Agopianca4d3602011-05-19 15:38:14 -07001073 Mutex::Autolock _l(mStateLock);
1074 const nsecs_t now = systemTime();
1075 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001076
Mathias Agopianca4d3602011-05-19 15:38:14 -07001077 // Here we're guaranteed that some transaction flags are set
1078 // so we can call handleTransactionLocked() unconditionally.
1079 // We call getTransactionFlags(), which will also clear the flags,
1080 // with mStateLock held to guarantee that mCurrentState won't change
1081 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001082
Mathias Agopiane57f2922012-08-09 16:29:12 -07001083 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001084 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001085
Mathias Agopianca4d3602011-05-19 15:38:14 -07001086 mLastTransactionTime = systemTime() - now;
1087 mDebugInTransaction = 0;
1088 invalidateHwcGeometry();
1089 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001090}
1091
Mathias Agopian87baae12012-07-31 12:38:26 -07001092void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001093{
1094 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001095 const size_t count = currentLayers.size();
1096
1097 /*
1098 * Traversal of the children
1099 * (perform the transaction for each of them if needed)
1100 */
1101
Mathias Agopian3559b072012-08-15 13:46:03 -07001102 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001103 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001104 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001105 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1106 if (!trFlags) continue;
1107
1108 const uint32_t flags = layer->doTransaction(0);
1109 if (flags & Layer::eVisibleRegion)
1110 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001111 }
1112 }
1113
1114 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001115 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001116 */
1117
Mathias Agopiane57f2922012-08-09 16:29:12 -07001118 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001119 // here we take advantage of Vector's copy-on-write semantics to
1120 // improve performance by skipping the transaction entirely when
1121 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001122 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1123 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001124 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001125 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001126 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001127 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001128
1129 // find the displays that were removed
1130 // (ie: in drawing state but not in current state)
1131 // also handle displays that changed
1132 // (ie: displays that are in both lists)
1133 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001134 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1135 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001136 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001137 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001138 // Call makeCurrent() on the primary display so we can
1139 // be sure that nothing associated with this display
1140 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001141 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001142 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001143 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1144 if (hw != NULL)
1145 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001146 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001147 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001148 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001149 } else {
1150 ALOGW("trying to remove the main display");
1151 }
1152 } else {
1153 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001154 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001155 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001156 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001157 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001158 // recreating the DisplayDevice, so we just remove it
1159 // from the drawing state, so that it get re-added
1160 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001161 sp<DisplayDevice> hw(getDisplayDevice(display));
1162 if (hw != NULL)
1163 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001164 mDisplays.removeItem(display);
1165 mDrawingState.displays.removeItemsAt(i);
1166 dc--; i--;
1167 // at this point we must loop to the next item
1168 continue;
1169 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001170
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001171 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001172 if (disp != NULL) {
1173 if (state.layerStack != draw[i].layerStack) {
1174 disp->setLayerStack(state.layerStack);
1175 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001176 if ((state.orientation != draw[i].orientation)
1177 || (state.viewport != draw[i].viewport)
1178 || (state.frame != draw[i].frame))
1179 {
1180 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001181 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001182 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001183 }
1184 }
1185 }
1186
1187 // find displays that were added
1188 // (ie: in current state but not in drawing state)
1189 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001190 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001191 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001192
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001193 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001194 sp<IGraphicBufferProducer> producer;
Dan Stozab9b08832014-03-13 11:55:57 -07001195 sp<IGraphicBufferProducer> bqProducer;
1196 sp<IGraphicBufferConsumer> bqConsumer;
1197 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer,
1198 new GraphicBufferAlloc());
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001199
Jesse Hall02d86562013-03-25 14:43:23 -07001200 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001201 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001202 // Virtual displays without a surface are dormant:
1203 // they have external state (layer stack, projection,
1204 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001205 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001206
Jesse Hall02d86562013-03-25 14:43:23 -07001207 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001208 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
Dan Stozab9b08832014-03-13 11:55:57 -07001209 *mHwc, hwcDisplayId, state.surface,
1210 bqProducer, bqConsumer, state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001211
1212 dispSurface = vds;
1213 if (hwcDisplayId >= 0) {
1214 producer = vds;
1215 } else {
1216 // There won't be any interaction with HWC for this virtual display,
1217 // so the GLES driver can pass buffers directly to the sink.
1218 producer = state.surface;
1219 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001220 }
1221 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001222 ALOGE_IF(state.surface!=NULL,
1223 "adding a supported display, but rendering "
1224 "surface is provided (%p), ignoring it",
1225 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001226 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001227 // for supported (by hwc) displays we provide our
1228 // own rendering surface
Dan Stozab9b08832014-03-13 11:55:57 -07001229 dispSurface = new FramebufferSurface(*mHwc, state.type,
1230 bqConsumer);
1231 producer = bqProducer;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001232 }
1233
1234 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001235 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001236 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001237 state.type, hwcDisplayId,
1238 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001239 display, dispSurface, producer,
1240 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001241 hw->setLayerStack(state.layerStack);
1242 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001243 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001244 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001245 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001246 if (state.isVirtualDisplay()) {
1247 if (hwcDisplayId >= 0) {
1248 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1249 hw->getWidth(), hw->getHeight(),
1250 hw->getFormat());
1251 }
1252 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001253 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001254 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001255 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001256 }
1257 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001258 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001259 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001260
Mathias Agopian84300952012-11-21 16:02:13 -08001261 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1262 // The transform hint might have changed for some layers
1263 // (either because a display has changed, or because a layer
1264 // as changed).
1265 //
1266 // Walk through all the layers in currentLayers,
1267 // and update their transform hint.
1268 //
1269 // If a layer is visible only on a single display, then that
1270 // display is used to calculate the hint, otherwise we use the
1271 // default display.
1272 //
1273 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1274 // the hint is set before we acquire a buffer from the surface texture.
1275 //
1276 // NOTE: layer transactions have taken place already, so we use their
1277 // drawing state. However, SurfaceFlinger's own transaction has not
1278 // happened yet, so we must use the current state layer list
1279 // (soon to become the drawing state list).
1280 //
1281 sp<const DisplayDevice> disp;
1282 uint32_t currentlayerStack = 0;
1283 for (size_t i=0; i<count; i++) {
1284 // NOTE: we rely on the fact that layers are sorted by
1285 // layerStack first (so we don't have to traverse the list
1286 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001287 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001288 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001289 if (i==0 || currentlayerStack != layerStack) {
1290 currentlayerStack = layerStack;
1291 // figure out if this layerstack is mirrored
1292 // (more than one display) if so, pick the default display,
1293 // if not, pick the only display it's on.
1294 disp.clear();
1295 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1296 sp<const DisplayDevice> hw(mDisplays[dpy]);
1297 if (hw->getLayerStack() == currentlayerStack) {
1298 if (disp == NULL) {
1299 disp = hw;
1300 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001301 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001302 break;
1303 }
1304 }
1305 }
1306 }
Chet Haase91d25932013-04-11 15:24:55 -07001307 if (disp == NULL) {
1308 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1309 // redraw after transform hint changes. See bug 8508397.
1310
1311 // could be null when this layer is using a layerStack
1312 // that is not visible on any display. Also can occur at
1313 // screen off/on times.
1314 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001315 }
Chet Haase91d25932013-04-11 15:24:55 -07001316 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001317 }
1318 }
1319
1320
Mathias Agopian3559b072012-08-15 13:46:03 -07001321 /*
1322 * Perform our own transaction if needed
1323 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001324
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001325 const LayerVector& layers(mDrawingState.layersSortedByZ);
1326 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001327 // layers have been added
1328 mVisibleRegionsDirty = true;
1329 }
1330
1331 // some layers might have been removed, so
1332 // we need to update the regions they're exposing.
1333 if (mLayersRemoved) {
1334 mLayersRemoved = false;
1335 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001336 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001337 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001338 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001339 if (currentLayers.indexOf(layer) < 0) {
1340 // this layer is not visible anymore
1341 // TODO: we could traverse the tree from front to back and
1342 // compute the actual visible region
1343 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001344 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001345 Region visibleReg = s.transform.transform(
1346 Region(Rect(s.active.w, s.active.h)));
1347 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001348 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001349 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001350 }
1351
1352 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001353}
1354
1355void SurfaceFlinger::commitTransaction()
1356{
1357 if (!mLayersPendingRemoval.isEmpty()) {
1358 // Notify removed layers now that they can't be drawn from
1359 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1360 mLayersPendingRemoval[i]->onRemoved();
1361 }
1362 mLayersPendingRemoval.clear();
1363 }
1364
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001365 // If this transaction is part of a window animation then the next frame
1366 // we composite should be considered an animation as well.
1367 mAnimCompositionPending = mAnimTransactionPending;
1368
Mathias Agopian4fec8732012-06-29 14:12:52 -07001369 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001370 mTransactionPending = false;
1371 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001372 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001373}
1374
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001375void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001376 const LayerVector& currentLayers, uint32_t layerStack,
1377 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001378{
Mathias Agopian841cde52012-03-01 15:44:37 -08001379 ATRACE_CALL();
1380
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001381 Region aboveOpaqueLayers;
1382 Region aboveCoveredLayers;
1383 Region dirty;
1384
Mathias Agopian87baae12012-07-31 12:38:26 -07001385 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001386
1387 size_t i = currentLayers.size();
1388 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001389 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001390
1391 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001392 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001393
Jesse Hall01e29052013-02-19 16:13:35 -08001394 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001395 if (s.layerStack != layerStack)
1396 continue;
1397
Mathias Agopianab028732010-03-16 16:41:46 -07001398 /*
1399 * opaqueRegion: area of a surface that is fully opaque.
1400 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001401 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001402
1403 /*
1404 * visibleRegion: area of a surface that is visible on screen
1405 * and not fully transparent. This is essentially the layer's
1406 * footprint minus the opaque regions above it.
1407 * Areas covered by a translucent surface are considered visible.
1408 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001409 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001410
1411 /*
1412 * coveredRegion: area of a surface that is covered by all
1413 * visible regions above it (which includes the translucent areas).
1414 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001415 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001416
Jesse Halla8026d22012-09-25 13:25:04 -07001417 /*
1418 * transparentRegion: area of a surface that is hinted to be completely
1419 * transparent. This is only used to tell when the layer has no visible
1420 * non-transparent regions and can be removed from the layer list. It
1421 * does not affect the visibleRegion of this layer or any layers
1422 * beneath it. The hint may not be correct if apps don't respect the
1423 * SurfaceView restrictions (which, sadly, some don't).
1424 */
1425 Region transparentRegion;
1426
Mathias Agopianab028732010-03-16 16:41:46 -07001427
1428 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001429 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001430 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001431 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001432 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001433 if (!visibleRegion.isEmpty()) {
1434 // Remove the transparent area from the visible region
1435 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001436 const Transform tr(s.transform);
1437 if (tr.transformed()) {
1438 if (tr.preserveRects()) {
1439 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001440 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001441 } else {
1442 // transformation too complex, can't do the
1443 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001444 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001445 }
1446 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001447 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001448 }
Mathias Agopianab028732010-03-16 16:41:46 -07001449 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001450
Mathias Agopianab028732010-03-16 16:41:46 -07001451 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001452 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001453 if (s.alpha==255 && !translucent &&
1454 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1455 // the opaque region is the layer's footprint
1456 opaqueRegion = visibleRegion;
1457 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001458 }
1459 }
1460
Mathias Agopianab028732010-03-16 16:41:46 -07001461 // Clip the covered region to the visible region
1462 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1463
1464 // Update aboveCoveredLayers for next (lower) layer
1465 aboveCoveredLayers.orSelf(visibleRegion);
1466
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001467 // subtract the opaque region covered by the layers above us
1468 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001469
1470 // compute this layer's dirty region
1471 if (layer->contentDirty) {
1472 // we need to invalidate the whole region
1473 dirty = visibleRegion;
1474 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001475 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001476 layer->contentDirty = false;
1477 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001478 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001479 * the exposed region consists of two components:
1480 * 1) what's VISIBLE now and was COVERED before
1481 * 2) what's EXPOSED now less what was EXPOSED before
1482 *
1483 * note that (1) is conservative, we start with the whole
1484 * visible region but only keep what used to be covered by
1485 * something -- which mean it may have been exposed.
1486 *
1487 * (2) handles areas that were not covered by anything but got
1488 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001489 */
Mathias Agopianab028732010-03-16 16:41:46 -07001490 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001491 const Region oldVisibleRegion = layer->visibleRegion;
1492 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001493 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1494 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495 }
1496 dirty.subtractSelf(aboveOpaqueLayers);
1497
1498 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001499 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001500
Mathias Agopianab028732010-03-16 16:41:46 -07001501 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001502 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001503
Jesse Halla8026d22012-09-25 13:25:04 -07001504 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001505 layer->setVisibleRegion(visibleRegion);
1506 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001507 layer->setVisibleNonTransparentRegion(
1508 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001509 }
1510
Mathias Agopian87baae12012-07-31 12:38:26 -07001511 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001512}
1513
Mathias Agopian87baae12012-07-31 12:38:26 -07001514void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1515 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001516 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001517 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1518 if (hw->getLayerStack() == layerStack) {
1519 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001520 }
1521 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001522}
1523
1524void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001525{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001526 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001527
Mathias Agopian4fec8732012-06-29 14:12:52 -07001528 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001529 const LayerVector& layers(mDrawingState.layersSortedByZ);
1530 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001531 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001532 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001533 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001534 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001535 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001536 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001537
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001538 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001539}
1540
Mathias Agopianad456f92011-01-13 17:53:01 -08001541void SurfaceFlinger::invalidateHwcGeometry()
1542{
1543 mHwWorkListDirty = true;
1544}
1545
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001546
Mathias Agopiancd60f992012-08-16 16:28:27 -07001547void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001548 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001549{
Dan Stoza71433162014-02-04 16:22:36 -08001550 // We only need to actually compose the display if:
1551 // 1) It is being handled by hardware composer, which may need this to
1552 // keep its virtual display state machine in sync, or
1553 // 2) There is work to be done (the dirty region isn't empty)
1554 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1555 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1556 return;
1557 }
1558
Mathias Agopian87baae12012-07-31 12:38:26 -07001559 Region dirtyRegion(inDirtyRegion);
1560
Mathias Agopianb8a55602009-06-26 19:06:36 -07001561 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001562 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001563
Mathias Agopian42977342012-08-05 00:40:46 -07001564 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001565 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001566 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1567 // takes a rectangle, we must make sure to update that whole
1568 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001569 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001570 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001571 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001572 // We need to redraw the rectangle that will be updated
1573 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001574 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001575 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001576 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001577 } else {
1578 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001579 dirtyRegion.set(hw->bounds());
1580 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001581 }
1582 }
1583
Alan Viverette9c5a3332013-09-12 20:04:35 -07001584 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Mathias Agopianff2ed702013-09-01 21:36:12 -07001585 doComposeSurfaces(hw, dirtyRegion);
1586 } else {
1587 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001588 mat4 colorMatrix = mColorMatrix;
1589 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001590 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001591 }
1592 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001593 doComposeSurfaces(hw, dirtyRegion);
1594 engine.endGroup();
1595 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001596
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001597 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001598 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001599
1600 // swap buffers (presentation)
1601 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001602}
1603
Mathias Agopiancd60f992012-08-16 16:28:27 -07001604void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001605{
Mathias Agopian3f844832013-08-07 21:24:32 -07001606 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001607 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001608 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001609 HWComposer::LayerListIterator cur = hwc.begin(id);
1610 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001611
Jesse Halld05a17f2013-09-30 16:49:30 -07001612 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001613 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001614 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001615 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1616 hw->getDisplayName().string());
1617 return;
1618 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001619
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001620 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001621 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001622 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001623 // when using overlays, we assume a fully transparent framebuffer
1624 // NOTE: we could reduce how much we need to clear, for instance
1625 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001626 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001627 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001628 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001629 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001630 // we start with the whole screen area
1631 const Region bounds(hw->getBounds());
1632
1633 // we remove the scissor part
1634 // we're left with the letterbox region
1635 // (common case is that letterbox ends-up being empty)
1636 const Region letterbox(bounds.subtract(hw->getScissor()));
1637
1638 // compute the area to clear
1639 Region region(hw->undefinedRegion.merge(letterbox));
1640
1641 // but limit it to the dirty region
1642 region.andSelf(dirty);
1643
Mathias Agopianb9494d52012-04-18 02:28:45 -07001644 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001645 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001646 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001647 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001648 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001649 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001650
Mathias Agopian766dc492012-10-30 18:08:06 -07001651 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1652 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001653 // scissor on the main display. It should never be needed
1654 // anyways (though in theory it could since the API allows it).
1655 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001656 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001657 if (scissor != bounds) {
1658 // scissor doesn't match the screen's dimensions, so we
1659 // need to clear everything outside of it and enable
1660 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001661
Mathias Agopianf45c5102012-10-24 16:29:17 -07001662 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001663 const uint32_t height = hw->getHeight();
1664 engine.setScissor(scissor.left, height - scissor.bottom,
1665 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001666 }
1667 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001668 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001669
Mathias Agopian85d751c2012-08-29 16:59:24 -07001670 /*
1671 * and then, render the layers targeted at the framebuffer
1672 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001673
Mathias Agopian13127d82013-03-05 17:47:11 -08001674 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001675 const size_t count = layers.size();
1676 const Transform& tr = hw->getTransform();
1677 if (cur != end) {
1678 // we're using h/w composer
1679 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001680 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001681 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001682 if (!clip.isEmpty()) {
1683 switch (cur->getCompositionType()) {
1684 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001685 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001686 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1687 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001688 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001689 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001690 // never clear the very first layer since we're
1691 // guaranteed the FB is already cleared
1692 layer->clearWithOpenGL(hw, clip);
1693 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001694 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001695 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001696 case HWC_FRAMEBUFFER: {
1697 layer->draw(hw, clip);
1698 break;
1699 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001700 case HWC_FRAMEBUFFER_TARGET: {
1701 // this should not happen as the iterator shouldn't
1702 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001703 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001704 break;
1705 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001706 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001707 }
1708 layer->setAcquireFence(hw, *cur);
1709 }
1710 } else {
1711 // we're not using h/w composer
1712 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001713 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001714 const Region clip(dirty.intersect(
1715 tr.transform(layer->visibleRegion)));
1716 if (!clip.isEmpty()) {
1717 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001718 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001719 }
1720 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001721
1722 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001723 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001724}
1725
Mathias Agopian3f844832013-08-07 21:24:32 -07001726void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001727 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001728 RenderEngine& engine(getRenderEngine());
1729 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001730}
1731
Mathias Agopianac9fa422013-02-11 16:40:36 -08001732void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1733 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001734 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001735 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001736{
Mathias Agopian96f08192010-06-02 23:28:45 -07001737 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001738 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001739
Mathias Agopian96f08192010-06-02 23:28:45 -07001740 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001741 Mutex::Autolock _l(mStateLock);
1742 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001743 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001744}
1745
Mathias Agopian3f844832013-08-07 21:24:32 -07001746status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001747 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001748 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001749 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001750 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001751 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001752 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001753 return NO_ERROR;
1754 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001755 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001756}
1757
Dan Stozac7014012014-02-14 15:03:43 -08001758uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001759 return android_atomic_release_load(&mTransactionFlags);
1760}
1761
Mathias Agopian3f844832013-08-07 21:24:32 -07001762uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001763 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1764}
1765
Mathias Agopian3f844832013-08-07 21:24:32 -07001766uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001767 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1768 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001769 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001770 }
1771 return old;
1772}
1773
Mathias Agopian8b33f032012-07-24 20:43:54 -07001774void SurfaceFlinger::setTransactionState(
1775 const Vector<ComposerState>& state,
1776 const Vector<DisplayState>& displays,
1777 uint32_t flags)
1778{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001779 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001780 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001781 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001782
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001783 if (flags & eAnimation) {
1784 // For window updates that are part of an animation we must wait for
1785 // previous animation "frames" to be handled.
1786 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001787 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001788 if (CC_UNLIKELY(err != NO_ERROR)) {
1789 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001790 // caller after a few seconds.
1791 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1792 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001793 mAnimTransactionPending = false;
1794 break;
1795 }
1796 }
1797 }
1798
Mathias Agopiane57f2922012-08-09 16:29:12 -07001799 size_t count = displays.size();
1800 for (size_t i=0 ; i<count ; i++) {
1801 const DisplayState& s(displays[i]);
1802 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001803 }
1804
Mathias Agopiane57f2922012-08-09 16:29:12 -07001805 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001806 for (size_t i=0 ; i<count ; i++) {
1807 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001808 // Here we need to check that the interface we're given is indeed
1809 // one of our own. A malicious client could give us a NULL
1810 // IInterface, or one of its own or even one of our own but a
1811 // different type. All these situations would cause us to crash.
1812 //
1813 // NOTE: it would be better to use RTTI as we could directly check
1814 // that we have a Client*. however, RTTI is disabled in Android.
1815 if (s.client != NULL) {
1816 sp<IBinder> binder = s.client->asBinder();
1817 if (binder != NULL) {
1818 String16 desc(binder->getInterfaceDescriptor());
1819 if (desc == ISurfaceComposerClient::descriptor) {
1820 sp<Client> client( static_cast<Client *>(s.client.get()) );
1821 transactionFlags |= setClientStateLocked(client, s.state);
1822 }
1823 }
1824 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001825 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001826
Mathias Agopian386aa982011-11-07 21:58:03 -08001827 if (transactionFlags) {
1828 // this triggers the transaction
1829 setTransactionFlags(transactionFlags);
1830
1831 // if this is a synchronous transaction, wait for it to take effect
1832 // before returning.
1833 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001834 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001835 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001836 if (flags & eAnimation) {
1837 mAnimTransactionPending = true;
1838 }
1839 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001840 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1841 if (CC_UNLIKELY(err != NO_ERROR)) {
1842 // just in case something goes wrong in SF, return to the
1843 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001844 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1845 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001846 break;
1847 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001848 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001849 }
1850}
1851
Mathias Agopiane57f2922012-08-09 16:29:12 -07001852uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1853{
Jesse Hall9a143922012-10-04 16:29:19 -07001854 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1855 if (dpyIdx < 0)
1856 return 0;
1857
Mathias Agopiane57f2922012-08-09 16:29:12 -07001858 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001859 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001860 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001861 const uint32_t what = s.what;
1862 if (what & DisplayState::eSurfaceChanged) {
1863 if (disp.surface->asBinder() != s.surface->asBinder()) {
1864 disp.surface = s.surface;
1865 flags |= eDisplayTransactionNeeded;
1866 }
1867 }
1868 if (what & DisplayState::eLayerStackChanged) {
1869 if (disp.layerStack != s.layerStack) {
1870 disp.layerStack = s.layerStack;
1871 flags |= eDisplayTransactionNeeded;
1872 }
1873 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001874 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001875 if (disp.orientation != s.orientation) {
1876 disp.orientation = s.orientation;
1877 flags |= eDisplayTransactionNeeded;
1878 }
1879 if (disp.frame != s.frame) {
1880 disp.frame = s.frame;
1881 flags |= eDisplayTransactionNeeded;
1882 }
1883 if (disp.viewport != s.viewport) {
1884 disp.viewport = s.viewport;
1885 flags |= eDisplayTransactionNeeded;
1886 }
1887 }
1888 }
1889 return flags;
1890}
1891
1892uint32_t SurfaceFlinger::setClientStateLocked(
1893 const sp<Client>& client,
1894 const layer_state_t& s)
1895{
1896 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001897 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001898 if (layer != 0) {
1899 const uint32_t what = s.what;
1900 if (what & layer_state_t::ePositionChanged) {
1901 if (layer->setPosition(s.x, s.y))
1902 flags |= eTraversalNeeded;
1903 }
1904 if (what & layer_state_t::eLayerChanged) {
1905 // NOTE: index needs to be calculated before we update the state
1906 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1907 if (layer->setLayer(s.z)) {
1908 mCurrentState.layersSortedByZ.removeAt(idx);
1909 mCurrentState.layersSortedByZ.add(layer);
1910 // we need traversal (state changed)
1911 // AND transaction (list changed)
1912 flags |= eTransactionNeeded|eTraversalNeeded;
1913 }
1914 }
1915 if (what & layer_state_t::eSizeChanged) {
1916 if (layer->setSize(s.w, s.h)) {
1917 flags |= eTraversalNeeded;
1918 }
1919 }
1920 if (what & layer_state_t::eAlphaChanged) {
1921 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1922 flags |= eTraversalNeeded;
1923 }
1924 if (what & layer_state_t::eMatrixChanged) {
1925 if (layer->setMatrix(s.matrix))
1926 flags |= eTraversalNeeded;
1927 }
1928 if (what & layer_state_t::eTransparentRegionChanged) {
1929 if (layer->setTransparentRegionHint(s.transparentRegion))
1930 flags |= eTraversalNeeded;
1931 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08001932 if ((what & layer_state_t::eVisibilityChanged) ||
1933 (what & layer_state_t::eOpacityChanged)) {
1934 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07001935 if (layer->setFlags(s.flags, s.mask))
1936 flags |= eTraversalNeeded;
1937 }
1938 if (what & layer_state_t::eCropChanged) {
1939 if (layer->setCrop(s.crop))
1940 flags |= eTraversalNeeded;
1941 }
1942 if (what & layer_state_t::eLayerStackChanged) {
1943 // NOTE: index needs to be calculated before we update the state
1944 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1945 if (layer->setLayerStack(s.layerStack)) {
1946 mCurrentState.layersSortedByZ.removeAt(idx);
1947 mCurrentState.layersSortedByZ.add(layer);
1948 // we need traversal (state changed)
1949 // AND transaction (list changed)
1950 flags |= eTransactionNeeded|eTraversalNeeded;
1951 }
1952 }
1953 }
1954 return flags;
1955}
1956
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001957status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001958 const String8& name,
1959 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001960 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1961 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001962{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001963 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001964 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001965 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001966 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001967 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001968 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001969
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001970 status_t result = NO_ERROR;
1971
1972 sp<Layer> layer;
1973
Mathias Agopian3165cc22012-08-08 19:42:09 -07001974 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1975 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001976 result = createNormalLayer(client,
1977 name, w, h, flags, format,
1978 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001979 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001980 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001981 result = createDimLayer(client,
1982 name, w, h, flags,
1983 handle, gbp, &layer);
1984 break;
1985 default:
1986 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001987 break;
1988 }
1989
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001990 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001991 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001992 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001993 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001994 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001995}
1996
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001997status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1998 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1999 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002000{
2001 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07002002 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002003 case PIXEL_FORMAT_TRANSPARENT:
2004 case PIXEL_FORMAT_TRANSLUCENT:
2005 format = PIXEL_FORMAT_RGBA_8888;
2006 break;
2007 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07002008#ifdef NO_RGBX_8888
2009 format = PIXEL_FORMAT_RGB_565;
2010#else
Mathias Agopian8f105402010-04-05 18:01:24 -07002011 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07002012#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002013 break;
2014 }
2015
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07002016#ifdef NO_RGBX_8888
2017 if (format == PIXEL_FORMAT_RGBX_8888)
2018 format = PIXEL_FORMAT_RGBA_8888;
2019#endif
2020
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002021 *outLayer = new Layer(this, client, name, w, h, flags);
2022 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
2023 if (err == NO_ERROR) {
2024 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002025 *gbp = (*outLayer)->getProducer();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002026 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002027
2028 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
2029 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002030}
2031
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002032status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
2033 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
2034 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002035{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002036 *outLayer = new LayerDim(this, client, name, w, h, flags);
2037 *handle = (*outLayer)->getHandle();
Dan Stozab9b08832014-03-13 11:55:57 -07002038 *gbp = (*outLayer)->getProducer();
Mathias Agopian4d9b8222013-03-12 17:11:48 -07002039 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07002040}
2041
Mathias Agopianac9fa422013-02-11 16:40:36 -08002042status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002043{
Mathias Agopian67106042013-03-14 19:18:13 -07002044 // called by the window manager when it wants to remove a Layer
2045 status_t err = NO_ERROR;
2046 sp<Layer> l(client->getLayerUser(handle));
2047 if (l != NULL) {
2048 err = removeLayer(l);
2049 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2050 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002051 }
2052 return err;
2053}
2054
Mathias Agopian13127d82013-03-05 17:47:11 -08002055status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002056{
Mathias Agopian67106042013-03-14 19:18:13 -07002057 // called by ~LayerCleaner() when all references to the IBinder (handle)
2058 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002059 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002060 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002061 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002062 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002063 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002064 "error removing layer=%p (%s)", l.get(), strerror(-err));
2065 }
2066 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002067}
2068
Mathias Agopianb60314a2012-04-10 22:09:54 -07002069// ---------------------------------------------------------------------------
2070
Andy McFadden13a082e2012-08-24 10:16:42 -07002071void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002072 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002073 Vector<ComposerState> state;
2074 Vector<DisplayState> displays;
2075 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002076 d.what = DisplayState::eDisplayProjectionChanged |
2077 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002078 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002079 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002080 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002081 d.frame.makeInvalid();
2082 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002083 displays.add(d);
2084 setTransactionState(state, displays, 0);
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002085 setPowerModeInternal(getDisplayDevice(d.token), HWC_POWER_MODE_NORMAL);
Jamie Gennis6547ff42013-07-16 20:12:42 -07002086
2087 const nsecs_t period =
2088 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2089 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002090}
2091
2092void SurfaceFlinger::initializeDisplays() {
2093 class MessageScreenInitialized : public MessageBase {
2094 SurfaceFlinger* flinger;
2095 public:
2096 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2097 virtual bool handler() {
2098 flinger->onInitializeDisplays();
2099 return true;
2100 }
2101 };
2102 sp<MessageBase> msg = new MessageScreenInitialized(this);
2103 postMessageAsync(msg); // we may be called from main thread, use async message
2104}
2105
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002106void SurfaceFlinger::setPowerModeInternal(const sp<DisplayDevice>& hw,
2107 int mode) {
2108 ALOGD("Set power mode=%d, type=%d flinger=%p", mode, hw->getDisplayType(),
2109 this);
2110 int32_t type = hw->getDisplayType();
2111 int currentMode = hw->getPowerMode();
Andy McFadden13a082e2012-08-24 10:16:42 -07002112
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002113 if (mode == currentMode) {
2114 ALOGD("Screen type=%d is already mode=%d", hw->getDisplayType(), mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002115 return;
2116 }
2117
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002118 hw->setPowerMode(mode);
2119 if (type >= DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
2120 ALOGW("Trying to set power mode for virtual display");
2121 return;
2122 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002123
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002124 if (currentMode == HWC_POWER_MODE_OFF) {
2125 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002126 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2127 // FIXME: eventthread only knows about the main display right now
2128 mEventThread->onScreenAcquired();
Jesse Hall948fe0c2013-10-14 12:56:09 -07002129 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002130 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002131
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002132 mVisibleRegionsDirty = true;
2133 repaintEverything();
2134 } else if (mode == HWC_POWER_MODE_OFF) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002135 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002136 disableHardwareVsync(true); // also cancels any in-progress resync
2137
Mathias Agopiancde87a32012-09-13 14:09:01 -07002138 // FIXME: eventthread only knows about the main display right now
2139 mEventThread->onScreenReleased();
2140 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002141
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002142 getHwComposer().setPowerMode(type, mode);
2143 mVisibleRegionsDirty = true;
2144 // from this point on, SF will stop drawing on this display
2145 } else {
2146 getHwComposer().setPowerMode(type, mode);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002147 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002148}
2149
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002150void SurfaceFlinger::setPowerMode(const sp<IBinder>& display, int mode) {
2151 class MessageSetPowerMode: public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002152 SurfaceFlinger& mFlinger;
2153 sp<IBinder> mDisplay;
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002154 int mMode;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002155 public:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002156 MessageSetPowerMode(SurfaceFlinger& flinger,
2157 const sp<IBinder>& disp, int mode) : mFlinger(flinger),
2158 mDisplay(disp) { mMode = mode; }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002159 virtual bool handler() {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002160 sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002161 if (hw == NULL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002162 ALOGE("Attempt to set power mode = %d for null display %p",
2163 mDisplay.get(), mMode);
Jesse Hall9e663de2013-08-16 14:28:37 -07002164 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002165 ALOGW("Attempt to set power mode = %d for virtual display",
2166 mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002167 } else {
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002168 mFlinger.setPowerModeInternal(hw, mMode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002169 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002170 return true;
2171 }
2172 };
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002173 sp<MessageBase> msg = new MessageSetPowerMode(*this, display, mode);
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002174 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002175}
2176
2177// ---------------------------------------------------------------------------
2178
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002179status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2180{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002181 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002182
Mathias Agopianbd115332013-04-18 16:41:04 -07002183 IPCThreadState* ipc = IPCThreadState::self();
2184 const int pid = ipc->getCallingPid();
2185 const int uid = ipc->getCallingUid();
2186 if ((uid != AID_SHELL) &&
2187 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002188 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002189 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002190 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002191 // Try to get the main lock, but don't insist if we can't
2192 // (this would indicate SF is stuck, but we want to be able to
2193 // print something in dumpsys).
2194 int retry = 3;
2195 while (mStateLock.tryLock()<0 && --retry>=0) {
2196 usleep(1000000);
2197 }
2198 const bool locked(retry >= 0);
2199 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002200 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002201 "SurfaceFlinger appears to be unresponsive, "
2202 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002203 }
2204
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002205 bool dumpAll = true;
2206 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002207 size_t numArgs = args.size();
2208 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002209 if ((index < numArgs) &&
2210 (args[index] == String16("--list"))) {
2211 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002212 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002213 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002214 }
2215
2216 if ((index < numArgs) &&
2217 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002218 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002219 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002220 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002221 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002222
2223 if ((index < numArgs) &&
2224 (args[index] == String16("--latency-clear"))) {
2225 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002226 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002227 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002228 }
Andy McFaddenc751e922014-05-08 14:53:26 -07002229
2230 if ((index < numArgs) &&
2231 (args[index] == String16("--dispsync"))) {
2232 index++;
2233 mPrimaryDispSync.dump(result);
2234 dumpAll = false;
2235 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002236 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002237
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002238 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002239 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002240 }
2241
Mathias Agopian9795c422009-08-26 16:36:26 -07002242 if (locked) {
2243 mStateLock.unlock();
2244 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002245 }
2246 write(fd, result.string(), result.size());
2247 return NO_ERROR;
2248}
2249
Dan Stozac7014012014-02-14 15:03:43 -08002250void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2251 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002252{
2253 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2254 const size_t count = currentLayers.size();
2255 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002256 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002257 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002258 }
2259}
2260
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002261void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002262 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002263{
2264 String8 name;
2265 if (index < args.size()) {
2266 name = String8(args[index]);
2267 index++;
2268 }
2269
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002270 const nsecs_t period =
2271 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002272 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002273
2274 if (name.isEmpty()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002275 mAnimFrameTracker.dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002276 } else {
2277 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2278 const size_t count = currentLayers.size();
2279 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002280 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002281 if (name == layer->getName()) {
Svetoslavd85084b2014-03-20 10:28:31 -07002282 layer->dumpFrameStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002283 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002284 }
2285 }
2286}
2287
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002288void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002289 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002290{
2291 String8 name;
2292 if (index < args.size()) {
2293 name = String8(args[index]);
2294 index++;
2295 }
2296
2297 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2298 const size_t count = currentLayers.size();
2299 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002300 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002301 if (name.isEmpty() || (name == layer->getName())) {
Svetoslavd85084b2014-03-20 10:28:31 -07002302 layer->clearFrameStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002303 }
2304 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002305
Svetoslavd85084b2014-03-20 10:28:31 -07002306 mAnimFrameTracker.clearStats();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002307}
2308
Jamie Gennis6547ff42013-07-16 20:12:42 -07002309// This should only be called from the main thread. Otherwise it would need
2310// the lock and should use mCurrentState rather than mDrawingState.
2311void SurfaceFlinger::logFrameStats() {
2312 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2313 const size_t count = drawingLayers.size();
2314 for (size_t i=0 ; i<count ; i++) {
2315 const sp<Layer>& layer(drawingLayers[i]);
2316 layer->logFrameStats();
2317 }
2318
2319 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2320}
2321
Andy McFadden4803b742012-09-24 19:07:20 -07002322/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2323{
2324 static const char* config =
2325 " [sf"
2326#ifdef NO_RGBX_8888
2327 " NO_RGBX_8888"
2328#endif
2329#ifdef HAS_CONTEXT_PRIORITY
2330 " HAS_CONTEXT_PRIORITY"
2331#endif
2332#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2333 " NEVER_DEFAULT_TO_ASYNC_MODE"
2334#endif
2335#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2336 " TARGET_DISABLE_TRIPLE_BUFFERING"
2337#endif
2338 "]";
2339 result.append(config);
2340}
2341
Mathias Agopian74d211a2013-04-22 16:55:35 +02002342void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2343 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002344{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002345 bool colorize = false;
2346 if (index < args.size()
2347 && (args[index] == String16("--color"))) {
2348 colorize = true;
2349 index++;
2350 }
2351
2352 Colorizer colorizer(colorize);
2353
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002354 // figure out if we're stuck somewhere
2355 const nsecs_t now = systemTime();
2356 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2357 const nsecs_t inTransaction(mDebugInTransaction);
2358 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2359 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2360
2361 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002362 * Dump library configuration.
2363 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002364
2365 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002366 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002367 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002368 appendSfConfigString(result);
2369 appendUiConfigString(result);
2370 appendGuiConfigString(result);
2371 result.append("\n");
2372
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002373 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002374 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002375 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002376 result.append(SyncFeatures::getInstance().toString());
2377 result.append("\n");
2378
Andy McFadden41d67d72014-04-25 16:58:34 -07002379 colorizer.bold(result);
2380 result.append("DispSync configuration: ");
2381 colorizer.reset(result);
2382 result.appendFormat("app phase %"PRId64" ns, sf phase %"PRId64" ns, "
2383 "present offset %d ns (refresh %"PRId64" ns)",
2384 vsyncPhaseOffsetNs, sfVsyncPhaseOffsetNs, PRESENT_TIME_OFFSET_FROM_VSYNC_NS,
2385 mHwc->getRefreshPeriod(HWC_DISPLAY_PRIMARY));
2386 result.append("\n");
2387
Andy McFadden4803b742012-09-24 19:07:20 -07002388 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002389 * Dump the visible layer list
2390 */
2391 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2392 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002393 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002394 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002395 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002396 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002397 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002398 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002399 }
2400
2401 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002402 * Dump Display state
2403 */
2404
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002405 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002406 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002407 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002408 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2409 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002410 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002411 }
2412
2413 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002414 * Dump SurfaceFlinger global state
2415 */
2416
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002417 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002418 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002419 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002420
Mathias Agopian888c8222012-08-04 21:10:38 -07002421 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002422 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002423
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002424 colorizer.bold(result);
2425 result.appendFormat("EGL implementation : %s\n",
2426 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2427 colorizer.reset(result);
2428 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002429 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002430
Mathias Agopian875d8e12013-06-07 15:35:48 -07002431 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002432
Mathias Agopian42977342012-08-05 00:40:46 -07002433 hw->undefinedRegion.dump(result, "undefinedRegion");
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002434 result.appendFormat(" orientation=%d, isDisplayOn=%d\n",
2435 hw->getOrientation(), hw->isDisplayOn());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002436 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002437 " last eglSwapBuffers() time: %f us\n"
2438 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002439 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002440 " refresh-rate : %f fps\n"
2441 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002442 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002443 " gpu_to_cpu_unsupported : %d\n"
2444 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002445 mLastSwapBufferTime/1000.0,
2446 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002447 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002448 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2449 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002450 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002451 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002452
Mathias Agopian74d211a2013-04-22 16:55:35 +02002453 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002454 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002455
Mathias Agopian74d211a2013-04-22 16:55:35 +02002456 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002457 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002458
2459 /*
2460 * VSYNC state
2461 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002462 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002463
2464 /*
2465 * Dump HWComposer state
2466 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002467 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002468 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002469 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002470 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002471 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002472 (mDebugDisableHWC || mDebugRegion || mDaltonize
2473 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002474 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002475
2476 /*
2477 * Dump gralloc state
2478 */
2479 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2480 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002481}
2482
Mathias Agopian13127d82013-03-05 17:47:11 -08002483const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002484SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002485 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002486 wp<IBinder> dpy;
2487 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2488 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2489 dpy = mDisplays.keyAt(i);
2490 break;
2491 }
2492 }
2493 if (dpy == NULL) {
2494 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2495 // Just use the primary display so we have something to return
2496 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2497 }
2498 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002499}
2500
Keun young Park63f165f2012-08-31 10:53:36 -07002501bool SurfaceFlinger::startDdmConnection()
2502{
2503 void* libddmconnection_dso =
2504 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2505 if (!libddmconnection_dso) {
2506 return false;
2507 }
2508 void (*DdmConnection_start)(const char* name);
2509 DdmConnection_start =
2510 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2511 if (!DdmConnection_start) {
2512 dlclose(libddmconnection_dso);
2513 return false;
2514 }
2515 (*DdmConnection_start)(getServiceName());
2516 return true;
2517}
2518
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002519status_t SurfaceFlinger::onTransact(
2520 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2521{
2522 switch (code) {
2523 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002524 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002525 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002526 case BOOT_FINISHED:
Svetoslavd85084b2014-03-20 10:28:31 -07002527 case CLEAR_ANIMATION_FRAME_STATS:
2528 case GET_ANIMATION_FRAME_STATS:
Prashant Malani2c9b11f2014-05-25 01:36:31 -07002529 case SET_POWER_MODE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002530 {
2531 // codes that require permission check
2532 IPCThreadState* ipc = IPCThreadState::self();
2533 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002534 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002535 if ((uid != AID_GRAPHICS) &&
2536 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002537 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002538 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2539 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002540 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002541 break;
2542 }
2543 case CAPTURE_SCREEN:
2544 {
2545 // codes that require permission check
2546 IPCThreadState* ipc = IPCThreadState::self();
2547 const int pid = ipc->getCallingPid();
2548 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002549 if ((uid != AID_GRAPHICS) &&
2550 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002551 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002552 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2553 return PERMISSION_DENIED;
2554 }
2555 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002556 }
2557 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002558
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002559 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2560 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002561 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002562 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002563 IPCThreadState* ipc = IPCThreadState::self();
2564 const int pid = ipc->getCallingPid();
2565 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002566 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002567 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002568 return PERMISSION_DENIED;
2569 }
2570 int n;
2571 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002572 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002573 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002574 return NO_ERROR;
2575 case 1002: // SHOW_UPDATES
2576 n = data.readInt32();
2577 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002578 invalidateHwcGeometry();
2579 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002580 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002581 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002582 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002583 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002584 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002585 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002586 setTransactionFlags(
2587 eTransactionNeeded|
2588 eDisplayTransactionNeeded|
2589 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002590 return NO_ERROR;
2591 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002592 case 1006:{ // send empty update
2593 signalRefresh();
2594 return NO_ERROR;
2595 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002596 case 1008: // toggle use of hw composer
2597 n = data.readInt32();
2598 mDebugDisableHWC = n ? 1 : 0;
2599 invalidateHwcGeometry();
2600 repaintEverything();
2601 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002602 case 1009: // toggle use of transform hint
2603 n = data.readInt32();
2604 mDebugDisableTransformHint = n ? 1 : 0;
2605 invalidateHwcGeometry();
2606 repaintEverything();
2607 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002608 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002609 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002610 reply->writeInt32(0);
2611 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002612 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002613 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002614 return NO_ERROR;
2615 case 1013: {
2616 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002617 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2618 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002619 return NO_ERROR;
2620 }
2621 case 1014: {
2622 // daltonize
2623 n = data.readInt32();
2624 switch (n % 10) {
2625 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2626 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2627 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2628 }
2629 if (n >= 10) {
2630 mDaltonizer.setMode(Daltonizer::correction);
2631 } else {
2632 mDaltonizer.setMode(Daltonizer::simulation);
2633 }
2634 mDaltonize = n > 0;
2635 invalidateHwcGeometry();
2636 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002637 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002638 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002639 case 1015: {
2640 // apply a color matrix
2641 n = data.readInt32();
2642 mHasColorMatrix = n ? 1 : 0;
2643 if (n) {
2644 // color matrix is sent as mat3 matrix followed by vec3
2645 // offset, then packed into a mat4 where the last row is
2646 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002647 for (size_t i = 0 ; i < 4; i++) {
2648 for (size_t j = 0; j < 4; j++) {
2649 mColorMatrix[i][j] = data.readFloat();
2650 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002651 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002652 } else {
2653 mColorMatrix = mat4();
2654 }
2655 invalidateHwcGeometry();
2656 repaintEverything();
2657 return NO_ERROR;
2658 }
Ruchi Kandoif52b3c82014-04-24 16:42:35 -07002659 // This is an experimental interface
2660 // Needs to be shifted to proper binder interface when we productize
2661 case 1016: {
2662 mPrimaryDispSync.setLowPowerMode(true);
2663 return NO_ERROR;
2664 }
2665 case 1017: {
2666 mPrimaryDispSync.setLowPowerMode(false);
2667 return NO_ERROR;
2668 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002669 }
2670 }
2671 return err;
2672}
2673
Mathias Agopian53331da2011-08-22 21:44:41 -07002674void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002675 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002676 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002677}
2678
Mathias Agopian59119e62010-10-11 12:37:43 -07002679// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002680// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002681// ---------------------------------------------------------------------------
2682
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002683/* The code below is here to handle b/8734824
2684 *
2685 * We create a IGraphicBufferProducer wrapper that forwards all calls
2686 * to the calling binder thread, where they are executed. This allows
2687 * the calling thread to be reused (on the other side) and not
2688 * depend on having "enough" binder threads to handle the requests.
2689 *
2690 */
2691
2692class GraphicProducerWrapper : public BBinder, public MessageHandler {
2693 sp<IGraphicBufferProducer> impl;
2694 sp<Looper> looper;
2695 status_t result;
2696 bool exitPending;
2697 bool exitRequested;
2698 mutable Barrier barrier;
2699 volatile int32_t memoryBarrier;
2700 uint32_t code;
2701 Parcel const* data;
2702 Parcel* reply;
2703
2704 enum {
2705 MSG_API_CALL,
2706 MSG_EXIT
2707 };
2708
2709 /*
2710 * this is called by our "fake" BpGraphicBufferProducer. We package the
2711 * data and reply Parcel and forward them to the calling thread.
2712 */
2713 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08002714 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002715 this->code = code;
2716 this->data = &data;
2717 this->reply = reply;
2718 android_atomic_acquire_store(0, &memoryBarrier);
2719 if (exitPending) {
2720 // if we've exited, we run the message synchronously right here
2721 handleMessage(Message(MSG_API_CALL));
2722 } else {
2723 barrier.close();
2724 looper->sendMessage(this, Message(MSG_API_CALL));
2725 barrier.wait();
2726 }
bdeng3Xc2633ce2014-03-20 09:15:34 +08002727 return result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002728 }
2729
2730 /*
2731 * here we run on the binder calling thread. All we've got to do is
2732 * call the real BpGraphicBufferProducer.
2733 */
2734 virtual void handleMessage(const Message& message) {
2735 android_atomic_release_load(&memoryBarrier);
2736 if (message.what == MSG_API_CALL) {
bdeng3Xc2633ce2014-03-20 09:15:34 +08002737 result = impl->asBinder()->transact(code, data[0], reply);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002738 barrier.open();
2739 } else if (message.what == MSG_EXIT) {
2740 exitRequested = true;
2741 }
2742 }
2743
2744public:
2745 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2746 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2747 exitPending(false), exitRequested(false) {
2748 }
2749
2750 status_t waitForResponse() {
2751 do {
2752 looper->pollOnce(-1);
2753 } while (!exitRequested);
2754 return result;
2755 }
2756
2757 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002758 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002759 exitPending = true;
2760 looper->sendMessage(this, Message(MSG_EXIT));
2761 }
2762};
2763
2764
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002765status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2766 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002767 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002768 uint32_t minLayerZ, uint32_t maxLayerZ,
2769 bool useIdentityTransform) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002770
2771 if (CC_UNLIKELY(display == 0))
2772 return BAD_VALUE;
2773
2774 if (CC_UNLIKELY(producer == 0))
2775 return BAD_VALUE;
2776
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002777 // if we have secure windows on this display, never allow the screen capture
2778 // unless the producer interface is local (i.e.: we can take a screenshot for
2779 // ourselves).
2780 if (!producer->asBinder()->localBinder()) {
2781 Mutex::Autolock _l(mStateLock);
2782 sp<const DisplayDevice> hw(getDisplayDevice(display));
2783 if (hw->getSecureLayerVisible()) {
2784 ALOGW("FB is protected: PERMISSION_DENIED");
2785 return PERMISSION_DENIED;
2786 }
2787 }
2788
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002789 class MessageCaptureScreen : public MessageBase {
2790 SurfaceFlinger* flinger;
2791 sp<IBinder> display;
2792 sp<IGraphicBufferProducer> producer;
Dan Stozac1879002014-05-22 15:59:05 -07002793 Rect sourceCrop;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002794 uint32_t reqWidth, reqHeight;
2795 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08002796 bool useIdentityTransform;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002797 status_t result;
2798 public:
2799 MessageCaptureScreen(SurfaceFlinger* flinger,
2800 const sp<IBinder>& display,
2801 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002802 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002803 uint32_t minLayerZ, uint32_t maxLayerZ,
2804 bool useIdentityTransform)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002805 : flinger(flinger), display(display), producer(producer),
Dan Stozac1879002014-05-22 15:59:05 -07002806 sourceCrop(sourceCrop), reqWidth(reqWidth), reqHeight(reqHeight),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002807 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08002808 useIdentityTransform(useIdentityTransform),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002809 result(PERMISSION_DENIED)
2810 {
2811 }
2812 status_t getResult() const {
2813 return result;
2814 }
2815 virtual bool handler() {
2816 Mutex::Autolock _l(flinger->mStateLock);
2817 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08002818 result = flinger->captureScreenImplLocked(hw, producer,
Dan Stozac1879002014-05-22 15:59:05 -07002819 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
Dan Stozac7014012014-02-14 15:03:43 -08002820 useIdentityTransform);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002821 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002822 return true;
2823 }
2824 };
2825
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002826 // make sure to process transactions before screenshots -- a transaction
2827 // might already be pending but scheduled for VSYNC; this guarantees we
2828 // will handle it before the screenshot. When VSYNC finally arrives
2829 // the scheduled transaction will be a no-op. If no transactions are
2830 // scheduled at this time, this will end-up being a no-op as well.
2831 mEventQueue.invalidateTransactionNow();
2832
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002833 // this creates a "fake" BBinder which will serve as a "fake" remote
2834 // binder to receive the marshaled calls and forward them to the
2835 // real remote (a BpGraphicBufferProducer)
2836 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2837
2838 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2839 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002840 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002841 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac1879002014-05-22 15:59:05 -07002842 sourceCrop, reqWidth, reqHeight, minLayerZ, maxLayerZ,
2843 useIdentityTransform);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002844
2845 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002846 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002847 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002848 }
2849 return res;
2850}
2851
Mathias Agopian180f10d2013-04-10 22:55:41 -07002852
2853void SurfaceFlinger::renderScreenImplLocked(
2854 const sp<const DisplayDevice>& hw,
Dan Stozac1879002014-05-22 15:59:05 -07002855 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Mathias Agopian180f10d2013-04-10 22:55:41 -07002856 uint32_t minLayerZ, uint32_t maxLayerZ,
Dan Stozac7014012014-02-14 15:03:43 -08002857 bool yswap, bool useIdentityTransform)
Mathias Agopian180f10d2013-04-10 22:55:41 -07002858{
2859 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002860 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002861
2862 // get screen geometry
2863 const uint32_t hw_w = hw->getWidth();
2864 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002865 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2866
Dan Stozac1879002014-05-22 15:59:05 -07002867 // if a default or invalid sourceCrop is passed in, set reasonable values
2868 if (sourceCrop.width() == 0 || sourceCrop.height() == 0 ||
2869 !sourceCrop.isValid()) {
2870 sourceCrop.setLeftTop(Point(0, 0));
2871 sourceCrop.setRightBottom(Point(hw_w, hw_h));
2872 }
2873
2874 // ensure that sourceCrop is inside screen
2875 if (sourceCrop.left < 0) {
2876 ALOGE("Invalid crop rect: l = %d (< 0)", sourceCrop.left);
2877 }
2878 if (sourceCrop.right >= hw_w) {
2879 ALOGE("Invalid crop rect: r = %d (>= %d)", sourceCrop.right, hw_w);
2880 }
2881 if (sourceCrop.top < 0) {
2882 ALOGE("Invalid crop rect: t = %d (< 0)", sourceCrop.top);
2883 }
2884 if (sourceCrop.bottom >= hw_h) {
2885 ALOGE("Invalid crop rect: b = %d (>= %d)", sourceCrop.bottom, hw_h);
2886 }
2887
Mathias Agopian180f10d2013-04-10 22:55:41 -07002888 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002889 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002890
2891 // set-up our viewport
Dan Stozac1879002014-05-22 15:59:05 -07002892 engine.setViewportAndProjection(reqWidth, reqHeight, sourceCrop, hw_h, yswap);
Mathias Agopian3f844832013-08-07 21:24:32 -07002893 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002894
2895 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002896 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002897
2898 const LayerVector& layers( mDrawingState.layersSortedByZ );
2899 const size_t count = layers.size();
2900 for (size_t i=0 ; i<count ; ++i) {
2901 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002902 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002903 if (state.layerStack == hw->getLayerStack()) {
2904 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2905 if (layer->isVisible()) {
2906 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08002907 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002908 if (filtering) layer->setFiltering(false);
2909 }
2910 }
2911 }
2912 }
2913
2914 // compositionComplete is needed for older driver
2915 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002916 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002917}
2918
2919
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002920status_t SurfaceFlinger::captureScreenImplLocked(
2921 const sp<const DisplayDevice>& hw,
2922 const sp<IGraphicBufferProducer>& producer,
Dan Stozac1879002014-05-22 15:59:05 -07002923 Rect sourceCrop, uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002924 uint32_t minLayerZ, uint32_t maxLayerZ,
2925 bool useIdentityTransform)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002926{
2927 ATRACE_CALL();
2928
Mathias Agopian180f10d2013-04-10 22:55:41 -07002929 // get screen geometry
2930 const uint32_t hw_w = hw->getWidth();
2931 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002932
Mathias Agopian180f10d2013-04-10 22:55:41 -07002933 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2934 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2935 reqWidth, reqHeight, hw_w, hw_h);
2936 return BAD_VALUE;
2937 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002938
Mathias Agopian180f10d2013-04-10 22:55:41 -07002939 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2940 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2941
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002942 // create a surface (because we're a producer, and we need to
2943 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07002944 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002945 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002946
2947 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002948 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002949 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2950 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002951
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002952 int err = 0;
2953 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07002954 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002955 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2956 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002957
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002958 if (err == NO_ERROR) {
2959 ANativeWindowBuffer* buffer;
2960 /* TODO: Once we have the sync framework everywhere this can use
2961 * server-side waits on the fence that dequeueBuffer returns.
2962 */
2963 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2964 if (result == NO_ERROR) {
2965 // create an EGLImage from the buffer so we can later
2966 // turn it into a texture
2967 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2968 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2969 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002970 // this binds the given EGLImage as a framebuffer for the
2971 // duration of this scope.
2972 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2973 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002974 // this will in fact render into our dequeued buffer
2975 // via an FBO, which means we didn't have to create
2976 // an EGLSurface and therefore we're not
2977 // dependent on the context's EGLConfig.
Dan Stozac1879002014-05-22 15:59:05 -07002978 renderScreenImplLocked(hw, sourceCrop, reqWidth, reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002979 minLayerZ, maxLayerZ, true, useIdentityTransform);
Mathias Agopiand5556842013-09-19 17:08:37 -07002980
Andy McFadden2d8d1202013-10-09 16:38:02 -07002981 // Create a sync point and wait on it, so we know the buffer is
2982 // ready before we pass it along. We can't trivially call glFlush(),
2983 // so we use a wait flag instead.
2984 // TODO: pass a sync fd to queueBuffer() and let the consumer wait.
2985 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
2986 if (sync != EGL_NO_SYNC_KHR) {
2987 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
2988 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
2989 EGLint eglErr = eglGetError();
2990 eglDestroySyncKHR(mEGLDisplay, sync);
2991 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
2992 ALOGW("captureScreen: fence wait timed out");
2993 } else {
2994 ALOGW_IF(eglErr != EGL_SUCCESS,
2995 "captureScreen: error waiting on EGL fence: %#x", eglErr);
2996 }
2997 } else {
2998 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
2999 // not fatal
3000 }
3001
Mathias Agopiand5556842013-09-19 17:08:37 -07003002 if (DEBUG_SCREENSHOTS) {
3003 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
3004 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
3005 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
3006 hw, minLayerZ, maxLayerZ);
3007 delete [] pixels;
3008 }
3009
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003010 } else {
3011 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
3012 result = INVALID_OPERATION;
3013 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02003014 // destroy our image
3015 eglDestroyImageKHR(mEGLDisplay, image);
3016 } else {
3017 result = BAD_VALUE;
3018 }
3019 window->queueBuffer(window, buffer, -1);
3020 }
3021 } else {
3022 result = BAD_VALUE;
3023 }
3024 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
3025 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07003026
Mathias Agopian74c40c02010-09-29 13:02:36 -07003027 return result;
3028}
3029
Mathias Agopiand5556842013-09-19 17:08:37 -07003030void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
3031 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003032 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07003033 for (size_t y=0 ; y<h ; y++) {
3034 uint32_t const * p = (uint32_t const *)vaddr + y*s;
3035 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07003036 if (p[x] != 0xFF000000) return;
3037 }
3038 }
3039 ALOGE("*** we just took a black screenshot ***\n"
3040 "requested minz=%d, maxz=%d, layerStack=%d",
3041 minLayerZ, maxLayerZ, hw->getLayerStack());
3042 const LayerVector& layers( mDrawingState.layersSortedByZ );
3043 const size_t count = layers.size();
3044 for (size_t i=0 ; i<count ; ++i) {
3045 const sp<Layer>& layer(layers[i]);
3046 const Layer::State& state(layer->getDrawingState());
3047 const bool visible = (state.layerStack == hw->getLayerStack())
3048 && (state.z >= minLayerZ && state.z <= maxLayerZ)
3049 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08003050 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07003051 visible ? '+' : '-',
3052 i, layer->getName().string(), state.layerStack, state.z,
3053 layer->isVisible(), state.flags, state.alpha);
3054 }
3055 }
3056}
3057
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07003058// ---------------------------------------------------------------------------
3059
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003060SurfaceFlinger::LayerVector::LayerVector() {
3061}
3062
3063SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08003064 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003065}
3066
3067int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
3068 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003069{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003070 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003071 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3072 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003073
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003074 uint32_t ls = l->getCurrentState().layerStack;
3075 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003076 if (ls != rs)
3077 return ls - rs;
3078
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003079 uint32_t lz = l->getCurrentState().z;
3080 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003081 if (lz != rz)
3082 return lz - rz;
3083
3084 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003085}
3086
3087// ---------------------------------------------------------------------------
3088
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003089SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
3090 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003091}
3092
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003093SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08003094 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003095 viewport.makeInvalid();
3096 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003097}
3098
3099// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003100
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003101}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003102
3103
3104#if defined(__gl_h_)
3105#error "don't include gl/gl.h in this file"
3106#endif
3107
3108#if defined(__gl2_h_)
3109#error "don't include gl2/gl2.h in this file"
3110#endif