blob: e05417ffee4a01bdef5c233813605836d492f38c [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
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700423 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
424 sp<FramebufferSurface> fbs = new FramebufferSurface(*mHwc, i, bq);
Jesse Hall19e87292013-12-23 21:02:15 -0800425 int32_t hwcId = allocateHwcDisplayId(type);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700426 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -0800427 type, hwcId, mHwc->getFormat(hwcId), isSecure, token,
Mathias Agopiandb89edc2013-08-02 01:40:18 -0700428 fbs, bq,
Jesse Hall05f8c702013-12-23 20:44:38 -0800429 mRenderEngine->getEGLConfig());
Mathias Agopianf5a33922012-09-19 18:16:22 -0700430 if (i > DisplayDevice::DISPLAY_PRIMARY) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -0700431 // FIXME: currently we don't get blank/unblank requests
Mathias Agopianf5a33922012-09-19 18:16:22 -0700432 // for displays other than the main display, so we always
433 // assume a connected display is unblanked.
Greg Hackmann86efcc02014-03-07 12:44:02 -0800434 ALOGD("marking display %zu as acquired/unblanked", i);
Mathias Agopianf5a33922012-09-19 18:16:22 -0700435 hw->acquireScreen();
436 }
437 mDisplays.add(token, hw);
438 }
Mathias Agopiane57f2922012-08-09 16:29:12 -0700439 }
Mathias Agopiancde87a32012-09-13 14:09:01 -0700440
Mathias Agopiana6bb1072013-08-07 20:10:20 -0700441 // make the GLContext current so that we can create textures when creating Layers
442 // (which may happens before we render something)
443 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
444
Mathias Agopian028508c2012-07-25 21:12:12 -0700445 // start the EventThread
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700446 sp<VSyncSource> vsyncSrc = new DispSyncSource(&mPrimaryDispSync,
447 vsyncPhaseOffsetNs, true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700448 mEventThread = new EventThread(vsyncSrc);
Jamie Gennis0a645cc2013-10-14 20:52:46 -0700449 sp<VSyncSource> sfVsyncSrc = new DispSyncSource(&mPrimaryDispSync,
450 sfVsyncPhaseOffsetNs, false);
451 mSFEventThread = new EventThread(sfVsyncSrc);
452 mEventQueue.setEventThread(mSFEventThread);
Mathias Agopian028508c2012-07-25 21:12:12 -0700453
Jamie Gennisd1700752013-10-14 12:22:52 -0700454 mEventControlThread = new EventControlThread(this);
455 mEventControlThread->run("EventControl", PRIORITY_URGENT_DISPLAY);
456
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700457 // set a fake vsync period if there is no HWComposer
458 if (mHwc->initCheck() != NO_ERROR) {
459 mPrimaryDispSync.setPeriod(16666667);
460 }
461
Mathias Agopian92a979a2012-08-02 18:32:23 -0700462 // initialize our drawing state
463 mDrawingState = mCurrentState;
Mathias Agopian86303202012-07-24 22:46:10 -0700464
Andy McFadden13a082e2012-08-24 10:16:42 -0700465 // set initial conditions (e.g. unblank default device)
466 initializeDisplays();
467
Mathias Agopiana1ecca92009-05-21 19:21:59 -0700468 // start boot animation
Mathias Agopiana67e4182012-06-19 17:26:12 -0700469 startBootAnim();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800470}
471
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700472int32_t SurfaceFlinger::allocateHwcDisplayId(DisplayDevice::DisplayType type) {
Jesse Hall9e663de2013-08-16 14:28:37 -0700473 return (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) ?
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700474 type : mHwc->allocateDisplayId();
475}
476
Mathias Agopiana67e4182012-06-19 17:26:12 -0700477void SurfaceFlinger::startBootAnim() {
478 // start boot animation
479 property_set("service.bootanim.exit", "0");
480 property_set("ctl.start", "bootanim");
481}
482
Mathias Agopian875d8e12013-06-07 15:35:48 -0700483size_t SurfaceFlinger::getMaxTextureSize() const {
484 return mRenderEngine->getMaxTextureSize();
Mathias Agopiana4912602012-07-12 14:25:33 -0700485}
486
Mathias Agopian875d8e12013-06-07 15:35:48 -0700487size_t SurfaceFlinger::getMaxViewportDims() const {
488 return mRenderEngine->getMaxViewportDims();
Mathias Agopiana4912602012-07-12 14:25:33 -0700489}
490
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800491// ----------------------------------------------------------------------------
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800492
Jamie Gennis582270d2011-08-17 18:19:00 -0700493bool SurfaceFlinger::authenticateSurfaceTexture(
Andy McFadden2adaf042012-12-18 09:49:45 -0800494 const sp<IGraphicBufferProducer>& bufferProducer) const {
Jamie Gennis134f0422011-03-08 12:18:54 -0800495 Mutex::Autolock _l(mStateLock);
Andy McFadden2adaf042012-12-18 09:49:45 -0800496 sp<IBinder> surfaceTextureBinder(bufferProducer->asBinder());
Mathias Agopian67106042013-03-14 19:18:13 -0700497 return mGraphicBufferProducerList.indexOf(surfaceTextureBinder) >= 0;
Jamie Gennis134f0422011-03-08 12:18:54 -0800498}
499
Jeff Brown9d4e3d22012-08-24 20:00:51 -0700500status_t SurfaceFlinger::getDisplayInfo(const sp<IBinder>& display, DisplayInfo* info) {
Jesse Hall692c7232012-11-08 15:41:56 -0800501 int32_t type = NAME_NOT_FOUND;
Jesse Hall9e663de2013-08-16 14:28:37 -0700502 for (int i=0 ; i<DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES ; i++) {
Jesse Hall692c7232012-11-08 15:41:56 -0800503 if (display == mBuiltinDisplays[i]) {
Mathias Agopian1604f772012-09-18 21:54:42 -0700504 type = i;
505 break;
506 }
507 }
508
509 if (type < 0) {
510 return type;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700511 }
Mathias Agopian8b736f12012-08-13 17:54:26 -0700512
513 const HWComposer& hwc(getHwComposer());
Mathias Agopian1604f772012-09-18 21:54:42 -0700514 float xdpi = hwc.getDpiX(type);
515 float ydpi = hwc.getDpiY(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700516
517 // TODO: Not sure if display density should handled by SF any longer
518 class Density {
519 static int getDensityFromProperty(char const* propName) {
520 char property[PROPERTY_VALUE_MAX];
521 int density = 0;
522 if (property_get(propName, property, NULL) > 0) {
523 density = atoi(property);
524 }
525 return density;
526 }
527 public:
528 static int getEmuDensity() {
529 return getDensityFromProperty("qemu.sf.lcd_density"); }
530 static int getBuildDensity() {
531 return getDensityFromProperty("ro.sf.lcd_density"); }
532 };
Mathias Agopian1604f772012-09-18 21:54:42 -0700533
534 if (type == DisplayDevice::DISPLAY_PRIMARY) {
535 // The density of the device is provided by a build property
536 float density = Density::getBuildDensity() / 160.0f;
537 if (density == 0) {
538 // the build doesn't provide a density -- this is wrong!
539 // use xdpi instead
540 ALOGE("ro.sf.lcd_density must be defined as a build property");
541 density = xdpi / 160.0f;
542 }
543 if (Density::getEmuDensity()) {
544 // if "qemu.sf.lcd_density" is specified, it overrides everything
545 xdpi = ydpi = density = Density::getEmuDensity();
546 density /= 160.0f;
547 }
548 info->density = density;
549
550 // TODO: this needs to go away (currently needed only by webkit)
551 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
552 info->orientation = hw->getOrientation();
Mathias Agopian1604f772012-09-18 21:54:42 -0700553 } else {
554 // TODO: where should this value come from?
555 static const int TV_DENSITY = 213;
556 info->density = TV_DENSITY / 160.0f;
557 info->orientation = 0;
Mathias Agopian8b736f12012-08-13 17:54:26 -0700558 }
559
Mathias Agopian1604f772012-09-18 21:54:42 -0700560 info->w = hwc.getWidth(type);
561 info->h = hwc.getHeight(type);
Mathias Agopian8b736f12012-08-13 17:54:26 -0700562 info->xdpi = xdpi;
563 info->ydpi = ydpi;
Mathias Agopian1604f772012-09-18 21:54:42 -0700564 info->fps = float(1e9 / hwc.getRefreshPeriod(type));
Jamie Gennisdd3cb842012-10-19 18:19:11 -0700565
566 // All non-virtual displays are currently considered secure.
567 info->secure = true;
568
Mathias Agopian888c8222012-08-04 21:10:38 -0700569 return NO_ERROR;
Mathias Agopianc666cae2012-07-25 18:56:13 -0700570}
571
Mathias Agopiand0566bc2011-11-17 17:49:17 -0800572// ----------------------------------------------------------------------------
573
574sp<IDisplayEventConnection> SurfaceFlinger::createDisplayEventConnection() {
Mathias Agopian8aedd472012-01-24 16:39:14 -0800575 return mEventThread->createEventConnection();
Mathias Agopianbb641242010-05-18 17:06:55 -0700576}
577
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800578// ----------------------------------------------------------------------------
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800579
580void SurfaceFlinger::waitForEvent() {
581 mEventQueue.waitMessage();
582}
583
584void SurfaceFlinger::signalTransaction() {
585 mEventQueue.invalidate();
586}
587
588void SurfaceFlinger::signalLayerUpdate() {
589 mEventQueue.invalidate();
590}
591
592void SurfaceFlinger::signalRefresh() {
593 mEventQueue.refresh();
594}
595
596status_t SurfaceFlinger::postMessageAsync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800597 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800598 return mEventQueue.postMessage(msg, reltime);
599}
600
601status_t SurfaceFlinger::postMessageSync(const sp<MessageBase>& msg,
Dan Stozac7014012014-02-14 15:03:43 -0800602 nsecs_t reltime, uint32_t /* flags */) {
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800603 status_t res = mEventQueue.postMessage(msg, reltime);
604 if (res == NO_ERROR) {
605 msg->wait();
606 }
607 return res;
608}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800609
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700610void SurfaceFlinger::run() {
Mathias Agopian4f4f0942013-08-19 17:26:18 -0700611 do {
612 waitForEvent();
613 } while (true);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800614}
615
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700616void SurfaceFlinger::enableHardwareVsync() {
617 Mutex::Autolock _l(mHWVsyncLock);
Jesse Hall948fe0c2013-10-14 12:56:09 -0700618 if (!mPrimaryHWVsyncEnabled && mHWVsyncAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700619 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700620 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
621 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700622 mPrimaryHWVsyncEnabled = true;
Andy McFadden43601a22012-09-11 15:15:13 -0700623 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700624}
625
Jesse Hall948fe0c2013-10-14 12:56:09 -0700626void SurfaceFlinger::resyncToHardwareVsync(bool makeAvailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700627 Mutex::Autolock _l(mHWVsyncLock);
628
Jesse Hall948fe0c2013-10-14 12:56:09 -0700629 if (makeAvailable) {
630 mHWVsyncAvailable = true;
631 } else if (!mHWVsyncAvailable) {
632 ALOGE("resyncToHardwareVsync called when HW vsync unavailable");
633 return;
634 }
635
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700636 const nsecs_t period =
637 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
638
639 mPrimaryDispSync.reset();
640 mPrimaryDispSync.setPeriod(period);
641
642 if (!mPrimaryHWVsyncEnabled) {
643 mPrimaryDispSync.beginResync();
Jamie Gennisd1700752013-10-14 12:22:52 -0700644 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, true);
645 mEventControlThread->setVsyncEnabled(true);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700646 mPrimaryHWVsyncEnabled = true;
647 }
648}
649
Jesse Hall948fe0c2013-10-14 12:56:09 -0700650void SurfaceFlinger::disableHardwareVsync(bool makeUnavailable) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700651 Mutex::Autolock _l(mHWVsyncLock);
652 if (mPrimaryHWVsyncEnabled) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700653 //eventControl(HWC_DISPLAY_PRIMARY, SurfaceFlinger::EVENT_VSYNC, false);
654 mEventControlThread->setVsyncEnabled(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700655 mPrimaryDispSync.endResync();
656 mPrimaryHWVsyncEnabled = false;
657 }
Jesse Hall948fe0c2013-10-14 12:56:09 -0700658 if (makeUnavailable) {
659 mHWVsyncAvailable = false;
660 }
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700661}
662
663void SurfaceFlinger::onVSyncReceived(int type, nsecs_t timestamp) {
Jamie Gennisd1700752013-10-14 12:22:52 -0700664 bool needsHwVsync = false;
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700665
Jamie Gennisd1700752013-10-14 12:22:52 -0700666 { // Scope for the lock
667 Mutex::Autolock _l(mHWVsyncLock);
668 if (type == 0 && mPrimaryHWVsyncEnabled) {
669 needsHwVsync = mPrimaryDispSync.addResyncSample(timestamp);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700670 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700671 }
Jamie Gennisd1700752013-10-14 12:22:52 -0700672
673 if (needsHwVsync) {
674 enableHardwareVsync();
675 } else {
676 disableHardwareVsync(false);
677 }
Mathias Agopian148994e2012-09-19 17:31:36 -0700678}
679
680void SurfaceFlinger::onHotplugReceived(int type, bool connected) {
681 if (mEventThread == NULL) {
682 // This is a temporary workaround for b/7145521. A non-null pointer
683 // does not mean EventThread has finished initializing, so this
684 // is not a correct fix.
685 ALOGW("WARNING: EventThread not started, ignoring hotplug");
686 return;
687 }
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700688
Jesse Hall9e663de2013-08-16 14:28:37 -0700689 if (uint32_t(type) < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700690 Mutex::Autolock _l(mStateLock);
Jesse Hall692c7232012-11-08 15:41:56 -0800691 if (connected) {
692 createBuiltinDisplayLocked((DisplayDevice::DisplayType)type);
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700693 } else {
Jesse Hall692c7232012-11-08 15:41:56 -0800694 mCurrentState.displays.removeItem(mBuiltinDisplays[type]);
695 mBuiltinDisplays[type].clear();
Mathias Agopian9e2463e2012-09-21 18:26:16 -0700696 }
697 setTransactionFlags(eDisplayTransactionNeeded);
698
Andy McFadden9e9689c2012-10-10 18:17:51 -0700699 // Defer EventThread notification until SF has updated mDisplays.
Mathias Agopian3ee454a2012-08-27 16:28:24 -0700700 }
Mathias Agopian86303202012-07-24 22:46:10 -0700701}
702
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700703void SurfaceFlinger::eventControl(int disp, int event, int enabled) {
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700704 ATRACE_CALL();
Mathias Agopian81cd5d32012-10-04 02:34:38 -0700705 getHwComposer().eventControl(disp, event, enabled);
Mathias Agopian86303202012-07-24 22:46:10 -0700706}
707
Mathias Agopian4fec8732012-06-29 14:12:52 -0700708void SurfaceFlinger::onMessageReceived(int32_t what) {
Jamie Gennis1c8e95c2012-02-23 19:27:23 -0800709 ATRACE_CALL();
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800710 switch (what) {
Mathias Agopian9eb1f052013-04-10 16:27:17 -0700711 case MessageQueue::TRANSACTION:
712 handleMessageTransaction();
713 break;
Mathias Agopian4fec8732012-06-29 14:12:52 -0700714 case MessageQueue::INVALIDATE:
715 handleMessageTransaction();
716 handleMessageInvalidate();
717 signalRefresh();
718 break;
719 case MessageQueue::REFRESH:
720 handleMessageRefresh();
721 break;
Mathias Agopian99ce5cd2012-01-31 18:24:27 -0800722 }
723}
724
Mathias Agopian4fec8732012-06-29 14:12:52 -0700725void SurfaceFlinger::handleMessageTransaction() {
Mathias Agopiane57f2922012-08-09 16:29:12 -0700726 uint32_t transactionFlags = peekTransactionFlags(eTransactionMask);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700727 if (transactionFlags) {
Mathias Agopian87baae12012-07-31 12:38:26 -0700728 handleTransaction(transactionFlags);
Mathias Agopian4fec8732012-06-29 14:12:52 -0700729 }
730}
731
732void SurfaceFlinger::handleMessageInvalidate() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700733 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700734 handlePageFlip();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700735}
736
737void SurfaceFlinger::handleMessageRefresh() {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700738 ATRACE_CALL();
739 preComposition();
740 rebuildLayerStacks();
741 setUpHWComposer();
742 doDebugFlashRegions();
743 doComposition();
744 postComposition();
745}
Mathias Agopian4fec8732012-06-29 14:12:52 -0700746
Mathias Agopiancd60f992012-08-16 16:28:27 -0700747void SurfaceFlinger::doDebugFlashRegions()
748{
749 // is debugging enabled
750 if (CC_LIKELY(!mDebugRegion))
751 return;
752
753 const bool repaintEverything = mRepaintEverything;
754 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
755 const sp<DisplayDevice>& hw(mDisplays[dpy]);
756 if (hw->canDraw()) {
757 // transform the dirty region into this screen's coordinate space
758 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
759 if (!dirtyRegion.isEmpty()) {
760 // redraw the whole screen
761 doComposeSurfaces(hw, Region(hw->bounds()));
762
763 // and draw the dirty region
Mathias Agopiancd60f992012-08-16 16:28:27 -0700764 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -0700765 RenderEngine& engine(getRenderEngine());
766 engine.fillRegionWithColor(dirtyRegion, height, 1, 0, 1, 1);
767
Mathias Agopiancd60f992012-08-16 16:28:27 -0700768 hw->compositionComplete();
Mathias Agopianda27af92012-09-13 18:17:13 -0700769 hw->swapBuffers(getHwComposer());
Mathias Agopiancd60f992012-08-16 16:28:27 -0700770 }
771 }
772 }
773
774 postFramebuffer();
775
776 if (mDebugRegion > 1) {
777 usleep(mDebugRegion * 1000);
778 }
Mathias Agopianbb53b0e2012-09-24 21:27:29 -0700779
780 HWComposer& hwc(getHwComposer());
781 if (hwc.initCheck() == NO_ERROR) {
782 status_t err = hwc.prepare();
783 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
784 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700785}
786
787void SurfaceFlinger::preComposition()
788{
789 bool needExtraInvalidate = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700790 const LayerVector& layers(mDrawingState.layersSortedByZ);
791 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700792 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700793 if (layers[i]->onPreComposition()) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700794 needExtraInvalidate = true;
795 }
796 }
797 if (needExtraInvalidate) {
798 signalLayerUpdate();
799 }
800}
801
802void SurfaceFlinger::postComposition()
803{
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700804 const LayerVector& layers(mDrawingState.layersSortedByZ);
805 const size_t count = layers.size();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700806 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700807 layers[i]->onPostComposition();
Mathias Agopiancd60f992012-08-16 16:28:27 -0700808 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800809
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700810 const HWComposer& hwc = getHwComposer();
811 sp<Fence> presentFence = hwc.getDisplayFence(HWC_DISPLAY_PRIMARY);
812
813 if (presentFence->isValid()) {
814 if (mPrimaryDispSync.addPresentFence(presentFence)) {
815 enableHardwareVsync();
816 } else {
Jesse Hall948fe0c2013-10-14 12:56:09 -0700817 disableHardwareVsync(false);
Jamie Gennisfaf77cc2013-07-30 15:10:32 -0700818 }
819 }
820
821 if (runningWithoutSyncFramework) {
822 const sp<const DisplayDevice> hw(getDefaultDisplayDevice());
823 if (hw->isScreenAcquired()) {
824 enableHardwareVsync();
825 }
826 }
827
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800828 if (mAnimCompositionPending) {
829 mAnimCompositionPending = false;
830
Jesse Halla9a1b002013-02-27 16:39:25 -0800831 if (presentFence->isValid()) {
Jamie Gennis4b0eba92013-02-05 13:30:24 -0800832 mAnimFrameTracker.setActualPresentFence(presentFence);
833 } else {
834 // The HWC doesn't support present fences, so use the refresh
835 // timestamp instead.
836 nsecs_t presentTime = hwc.getRefreshTimestamp(HWC_DISPLAY_PRIMARY);
837 mAnimFrameTracker.setActualPresentTime(presentTime);
838 }
839 mAnimFrameTracker.advanceFrame();
840 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700841}
842
843void SurfaceFlinger::rebuildLayerStacks() {
844 // rebuild the visible layer list per screen
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700845 if (CC_UNLIKELY(mVisibleRegionsDirty)) {
Mathias Agopiancd60f992012-08-16 16:28:27 -0700846 ATRACE_CALL();
Mathias Agopian87baae12012-07-31 12:38:26 -0700847 mVisibleRegionsDirty = false;
848 invalidateHwcGeometry();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700849
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700850 const LayerVector& layers(mDrawingState.layersSortedByZ);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700851 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700852 Region opaqueRegion;
853 Region dirtyRegion;
Mathias Agopian13127d82013-03-05 17:47:11 -0800854 Vector< sp<Layer> > layersSortedByZ;
Mathias Agopian42977342012-08-05 00:40:46 -0700855 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700856 const Transform& tr(hw->getTransform());
857 const Rect bounds(hw->getBounds());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700858 if (hw->canDraw()) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700859 SurfaceFlinger::computeVisibleRegions(layers,
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700860 hw->getLayerStack(), dirtyRegion, opaqueRegion);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700861
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700862 const size_t count = layers.size();
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700863 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -0700864 const sp<Layer>& layer(layers[i]);
865 const Layer::State& s(layer->getDrawingState());
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700866 if (s.layerStack == hw->getLayerStack()) {
Jesse Halla8026d22012-09-25 13:25:04 -0700867 Region drawRegion(tr.transform(
868 layer->visibleNonTransparentRegion));
869 drawRegion.andSelf(bounds);
870 if (!drawRegion.isEmpty()) {
Mathias Agopiance3a0a52012-09-12 15:34:57 -0700871 layersSortedByZ.add(layer);
872 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700873 }
874 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700875 }
Mathias Agopian42977342012-08-05 00:40:46 -0700876 hw->setVisibleLayersSortedByZ(layersSortedByZ);
Mathias Agopian7e7ed7f2012-08-28 14:20:00 -0700877 hw->undefinedRegion.set(bounds);
878 hw->undefinedRegion.subtractSelf(tr.transform(opaqueRegion));
879 hw->dirtyRegion.orSelf(dirtyRegion);
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700880 }
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700881 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700882}
Mathias Agopian3b1d2b62012-07-11 13:48:17 -0700883
Mathias Agopiancd60f992012-08-16 16:28:27 -0700884void SurfaceFlinger::setUpHWComposer() {
Jesse Hall028dc8f2013-08-20 16:35:32 -0700885 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Dan Stoza71433162014-02-04 16:22:36 -0800886 bool mustRecompose =
887 !(mDisplays[dpy]->getDirtyRegion(false).isEmpty());
888 mDisplays[dpy]->beginFrame(mustRecompose);
Jesse Hall028dc8f2013-08-20 16:35:32 -0700889 }
890
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700891 HWComposer& hwc(getHwComposer());
892 if (hwc.initCheck() == NO_ERROR) {
893 // build the h/w work list
Jamie Gennisa4310c82012-09-25 20:26:00 -0700894 if (CC_UNLIKELY(mHwWorkListDirty)) {
895 mHwWorkListDirty = false;
896 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
897 sp<const DisplayDevice> hw(mDisplays[dpy]);
898 const int32_t id = hw->getHwcDisplayId();
899 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800900 const Vector< sp<Layer> >& currentLayers(
Jamie Gennisa4310c82012-09-25 20:26:00 -0700901 hw->getVisibleLayersSortedByZ());
902 const size_t count = currentLayers.size();
903 if (hwc.createWorkList(id, count) == NO_ERROR) {
904 HWComposer::LayerListIterator cur = hwc.begin(id);
905 const HWComposer::LayerListIterator end = hwc.end(id);
906 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800907 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700908 layer->setGeometry(hw, *cur);
Alan Viverette9c5a3332013-09-12 20:04:35 -0700909 if (mDebugDisableHWC || mDebugRegion || mDaltonize || mHasColorMatrix) {
Jamie Gennisa4310c82012-09-25 20:26:00 -0700910 cur->setSkip(true);
911 }
912 }
913 }
914 }
915 }
916 }
917
918 // set the per-frame data
Mathias Agopian92a979a2012-08-02 18:32:23 -0700919 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700920 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopiane60b0682012-08-21 23:34:09 -0700921 const int32_t id = hw->getHwcDisplayId();
922 if (id >= 0) {
Mathias Agopian13127d82013-03-05 17:47:11 -0800923 const Vector< sp<Layer> >& currentLayers(
Mathias Agopiancd60f992012-08-16 16:28:27 -0700924 hw->getVisibleLayersSortedByZ());
Mathias Agopiane60b0682012-08-21 23:34:09 -0700925 const size_t count = currentLayers.size();
Jamie Gennisa4310c82012-09-25 20:26:00 -0700926 HWComposer::LayerListIterator cur = hwc.begin(id);
927 const HWComposer::LayerListIterator end = hwc.end(id);
928 for (size_t i=0 ; cur!=end && i<count ; ++i, ++cur) {
929 /*
930 * update the per-frame h/w composer data for each layer
931 * and build the transparent region of the FB
932 */
Mathias Agopian13127d82013-03-05 17:47:11 -0800933 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennisa4310c82012-09-25 20:26:00 -0700934 layer->setPerFrameData(hw, *cur);
Mathias Agopian1e260872012-08-08 18:35:12 -0700935 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700936 }
Mathias Agopian87baae12012-07-31 12:38:26 -0700937 }
Jamie Gennisa4310c82012-09-25 20:26:00 -0700938
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700939 status_t err = hwc.prepare();
940 ALOGE_IF(err, "HWComposer::prepare failed (%s)", strerror(-err));
Jesse Hall38efe862013-04-06 23:12:29 -0700941
942 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
943 sp<const DisplayDevice> hw(mDisplays[dpy]);
944 hw->prepareFrame(hwc);
945 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700946 }
Mathias Agopiancd60f992012-08-16 16:28:27 -0700947}
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700948
Mathias Agopiancd60f992012-08-16 16:28:27 -0700949void SurfaceFlinger::doComposition() {
950 ATRACE_CALL();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700951 const bool repaintEverything = android_atomic_and(0, &mRepaintEverything);
Mathias Agopian92a979a2012-08-02 18:32:23 -0700952 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700953 const sp<DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopiancd60f992012-08-16 16:28:27 -0700954 if (hw->canDraw()) {
955 // transform the dirty region into this screen's coordinate space
956 const Region dirtyRegion(hw->getDirtyRegion(repaintEverything));
Mathias Agopian02b95102012-11-05 17:50:57 -0800957
958 // repaint the framebuffer (if needed)
959 doDisplayComposition(hw, dirtyRegion);
960
Mathias Agopiancd60f992012-08-16 16:28:27 -0700961 hw->dirtyRegion.clear();
962 hw->flip(hw->swapRegion);
963 hw->swapRegion.clear();
Mathias Agopian87baae12012-07-31 12:38:26 -0700964 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700965 // inform the h/w that we're done compositing
Mathias Agopian42977342012-08-05 00:40:46 -0700966 hw->compositionComplete();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700967 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700968 postFramebuffer();
Mathias Agopian4fec8732012-06-29 14:12:52 -0700969}
970
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800971void SurfaceFlinger::postFramebuffer()
972{
Mathias Agopian841cde52012-03-01 15:44:37 -0800973 ATRACE_CALL();
Mathias Agopianb048cef2012-02-04 15:44:04 -0800974
Mathias Agopiana44b0412011-10-16 18:46:35 -0700975 const nsecs_t now = systemTime();
976 mDebugInSwapBuffers = now;
Jesse Hallc5c5a142012-07-02 16:49:28 -0700977
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700978 HWComposer& hwc(getHwComposer());
Jesse Hallef194142012-06-14 14:45:17 -0700979 if (hwc.initCheck() == NO_ERROR) {
Mathias Agopian2a231842012-09-24 18:12:35 -0700980 if (!hwc.supportsFramebufferTarget()) {
981 // EGL spec says:
982 // "surface must be bound to the calling thread's current context,
983 // for the current rendering API."
Mathias Agopian875d8e12013-06-07 15:35:48 -0700984 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
Mathias Agopian2a231842012-09-24 18:12:35 -0700985 }
Mathias Agopiane60b0682012-08-21 23:34:09 -0700986 hwc.commit();
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700987 }
988
Mathias Agopian6da15f42013-09-25 20:40:07 -0700989 // make the default display current because the VirtualDisplayDevice code cannot
990 // deal with dequeueBuffer() being called outside of the composition loop; however
991 // the code below can call glFlush() which is allowed (and does in some case) call
992 // dequeueBuffer().
993 getDefaultDisplayDevice()->makeCurrent(mEGLDisplay, mEGLContext);
994
Mathias Agopian92a979a2012-08-02 18:32:23 -0700995 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -0700996 sp<const DisplayDevice> hw(mDisplays[dpy]);
Mathias Agopian13127d82013-03-05 17:47:11 -0800997 const Vector< sp<Layer> >& currentLayers(hw->getVisibleLayersSortedByZ());
Mathias Agopianda27af92012-09-13 18:17:13 -0700998 hw->onSwapBuffersCompleted(hwc);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -0700999 const size_t count = currentLayers.size();
Mathias Agopiane60b0682012-08-21 23:34:09 -07001000 int32_t id = hw->getHwcDisplayId();
1001 if (id >=0 && hwc.initCheck() == NO_ERROR) {
Mathias Agopian1e260872012-08-08 18:35:12 -07001002 HWComposer::LayerListIterator cur = hwc.begin(id);
1003 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001004 for (size_t i = 0; cur != end && i < count; ++i, ++cur) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001005 currentLayers[i]->onLayerDisplayed(hw, &*cur);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001006 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001007 } else {
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001008 for (size_t i = 0; i < count; i++) {
Mathias Agopiand3ee2312012-08-02 14:01:42 -07001009 currentLayers[i]->onLayerDisplayed(hw, NULL);
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001010 }
Jesse Hallef194142012-06-14 14:45:17 -07001011 }
Jamie Gennise8696a42012-01-15 18:54:57 -08001012 }
1013
Mathias Agopiana44b0412011-10-16 18:46:35 -07001014 mLastSwapBufferTime = systemTime() - now;
1015 mDebugInSwapBuffers = 0;
Jamie Gennis6547ff42013-07-16 20:12:42 -07001016
1017 uint32_t flipCount = getDefaultDisplayDevice()->getPageFlipCount();
1018 if (flipCount % LOG_FRAME_STATS_PERIOD == 0) {
1019 logFrameStats();
1020 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001021}
1022
Mathias Agopian87baae12012-07-31 12:38:26 -07001023void SurfaceFlinger::handleTransaction(uint32_t transactionFlags)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001024{
Mathias Agopian841cde52012-03-01 15:44:37 -08001025 ATRACE_CALL();
1026
Mathias Agopian7cc6df52013-06-05 14:30:54 -07001027 // here we keep a copy of the drawing state (that is the state that's
1028 // going to be overwritten by handleTransactionLocked()) outside of
1029 // mStateLock so that the side-effects of the State assignment
1030 // don't happen with mStateLock held (which can cause deadlocks).
1031 State drawingState(mDrawingState);
1032
Mathias Agopianca4d3602011-05-19 15:38:14 -07001033 Mutex::Autolock _l(mStateLock);
1034 const nsecs_t now = systemTime();
1035 mDebugInTransaction = now;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001036
Mathias Agopianca4d3602011-05-19 15:38:14 -07001037 // Here we're guaranteed that some transaction flags are set
1038 // so we can call handleTransactionLocked() unconditionally.
1039 // We call getTransactionFlags(), which will also clear the flags,
1040 // with mStateLock held to guarantee that mCurrentState won't change
1041 // until the transaction is committed.
Mathias Agopian4da75192010-08-10 17:19:56 -07001042
Mathias Agopiane57f2922012-08-09 16:29:12 -07001043 transactionFlags = getTransactionFlags(eTransactionMask);
Mathias Agopian87baae12012-07-31 12:38:26 -07001044 handleTransactionLocked(transactionFlags);
Mathias Agopiandea20b12011-05-03 17:04:02 -07001045
Mathias Agopianca4d3602011-05-19 15:38:14 -07001046 mLastTransactionTime = systemTime() - now;
1047 mDebugInTransaction = 0;
1048 invalidateHwcGeometry();
1049 // here the transaction has been committed
Mathias Agopian3d579642009-06-04 18:46:21 -07001050}
1051
Mathias Agopian87baae12012-07-31 12:38:26 -07001052void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
Mathias Agopian3d579642009-06-04 18:46:21 -07001053{
1054 const LayerVector& currentLayers(mCurrentState.layersSortedByZ);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001055 const size_t count = currentLayers.size();
1056
1057 /*
1058 * Traversal of the children
1059 * (perform the transaction for each of them if needed)
1060 */
1061
Mathias Agopian3559b072012-08-15 13:46:03 -07001062 if (transactionFlags & eTraversalNeeded) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001063 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001064 const sp<Layer>& layer(currentLayers[i]);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001065 uint32_t trFlags = layer->getTransactionFlags(eTransactionNeeded);
1066 if (!trFlags) continue;
1067
1068 const uint32_t flags = layer->doTransaction(0);
1069 if (flags & Layer::eVisibleRegion)
1070 mVisibleRegionsDirty = true;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001071 }
1072 }
1073
1074 /*
Mathias Agopian3559b072012-08-15 13:46:03 -07001075 * Perform display own transactions if needed
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001076 */
1077
Mathias Agopiane57f2922012-08-09 16:29:12 -07001078 if (transactionFlags & eDisplayTransactionNeeded) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001079 // here we take advantage of Vector's copy-on-write semantics to
1080 // improve performance by skipping the transaction entirely when
1081 // know that the lists are identical
Mathias Agopiane57f2922012-08-09 16:29:12 -07001082 const KeyedVector< wp<IBinder>, DisplayDeviceState>& curr(mCurrentState.displays);
1083 const KeyedVector< wp<IBinder>, DisplayDeviceState>& draw(mDrawingState.displays);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001084 if (!curr.isIdenticalTo(draw)) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001085 mVisibleRegionsDirty = true;
Mathias Agopian92a979a2012-08-02 18:32:23 -07001086 const size_t cc = curr.size();
Mathias Agopian93997a82012-08-29 17:30:36 -07001087 size_t dc = draw.size();
Mathias Agopian92a979a2012-08-02 18:32:23 -07001088
1089 // find the displays that were removed
1090 // (ie: in drawing state but not in current state)
1091 // also handle displays that changed
1092 // (ie: displays that are in both lists)
1093 for (size_t i=0 ; i<dc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001094 const ssize_t j = curr.indexOfKey(draw.keyAt(i));
1095 if (j < 0) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001096 // in drawing state but not in current state
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001097 if (!draw[i].isMainDisplay()) {
Andy McFadden27ec5732012-10-02 19:04:45 -07001098 // Call makeCurrent() on the primary display so we can
1099 // be sure that nothing associated with this display
1100 // is current.
Jesse Hall02d86562013-03-25 14:43:23 -07001101 const sp<const DisplayDevice> defaultDisplay(getDefaultDisplayDevice());
Mathias Agopian875d8e12013-06-07 15:35:48 -07001102 defaultDisplay->makeCurrent(mEGLDisplay, mEGLContext);
Jesse Hall02d86562013-03-25 14:43:23 -07001103 sp<DisplayDevice> hw(getDisplayDevice(draw.keyAt(i)));
1104 if (hw != NULL)
1105 hw->disconnect(getHwComposer());
Jesse Hall9e663de2013-08-16 14:28:37 -07001106 if (draw[i].type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES)
Jesse Hall7adb0f82013-03-06 16:13:49 -08001107 mEventThread->onHotplugReceived(draw[i].type, false);
Jesse Hall02d86562013-03-25 14:43:23 -07001108 mDisplays.removeItem(draw.keyAt(i));
Mathias Agopian92a979a2012-08-02 18:32:23 -07001109 } else {
1110 ALOGW("trying to remove the main display");
1111 }
1112 } else {
1113 // this display is in both lists. see if something changed.
Mathias Agopiane57f2922012-08-09 16:29:12 -07001114 const DisplayDeviceState& state(curr[j]);
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001115 const wp<IBinder>& display(curr.keyAt(j));
Mathias Agopian111b2d82012-08-16 20:52:17 -07001116 if (state.surface->asBinder() != draw[i].surface->asBinder()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001117 // changing the surface is like destroying and
Mathias Agopian93997a82012-08-29 17:30:36 -07001118 // recreating the DisplayDevice, so we just remove it
1119 // from the drawing state, so that it get re-added
1120 // below.
Jesse Hall02d86562013-03-25 14:43:23 -07001121 sp<DisplayDevice> hw(getDisplayDevice(display));
1122 if (hw != NULL)
1123 hw->disconnect(getHwComposer());
Mathias Agopian93997a82012-08-29 17:30:36 -07001124 mDisplays.removeItem(display);
1125 mDrawingState.displays.removeItemsAt(i);
1126 dc--; i--;
1127 // at this point we must loop to the next item
1128 continue;
1129 }
Mathias Agopiane57f2922012-08-09 16:29:12 -07001130
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07001131 const sp<DisplayDevice> disp(getDisplayDevice(display));
Mathias Agopian93997a82012-08-29 17:30:36 -07001132 if (disp != NULL) {
1133 if (state.layerStack != draw[i].layerStack) {
1134 disp->setLayerStack(state.layerStack);
1135 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001136 if ((state.orientation != draw[i].orientation)
1137 || (state.viewport != draw[i].viewport)
1138 || (state.frame != draw[i].frame))
1139 {
1140 disp->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001141 state.viewport, state.frame);
Mathias Agopian93997a82012-08-29 17:30:36 -07001142 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001143 }
1144 }
1145 }
1146
1147 // find displays that were added
1148 // (ie: in current state but not in drawing state)
1149 for (size_t i=0 ; i<cc ; i++) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001150 if (draw.indexOfKey(curr.keyAt(i)) < 0) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001151 const DisplayDeviceState& state(curr[i]);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001152
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001153 sp<DisplaySurface> dispSurface;
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001154 sp<IGraphicBufferProducer> producer;
1155 sp<BufferQueue> bq = new BufferQueue(new GraphicBufferAlloc());
1156
Jesse Hall02d86562013-03-25 14:43:23 -07001157 int32_t hwcDisplayId = -1;
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001158 if (state.isVirtualDisplay()) {
Jesse Hall02d86562013-03-25 14:43:23 -07001159 // Virtual displays without a surface are dormant:
1160 // they have external state (layer stack, projection,
1161 // etc.) but no internal state (i.e. a DisplayDevice).
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001162 if (state.surface != NULL) {
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001163
Jesse Hall02d86562013-03-25 14:43:23 -07001164 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001165 sp<VirtualDisplaySurface> vds = new VirtualDisplaySurface(
1166 *mHwc, hwcDisplayId, state.surface, bq,
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001167 state.displayName);
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001168
1169 dispSurface = vds;
1170 if (hwcDisplayId >= 0) {
1171 producer = vds;
1172 } else {
1173 // There won't be any interaction with HWC for this virtual display,
1174 // so the GLES driver can pass buffers directly to the sink.
1175 producer = state.surface;
1176 }
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001177 }
1178 } else {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001179 ALOGE_IF(state.surface!=NULL,
1180 "adding a supported display, but rendering "
1181 "surface is provided (%p), ignoring it",
1182 state.surface.get());
Jesse Hall02d86562013-03-25 14:43:23 -07001183 hwcDisplayId = allocateHwcDisplayId(state.type);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001184 // for supported (by hwc) displays we provide our
1185 // own rendering surface
Mathias Agopiandb89edc2013-08-02 01:40:18 -07001186 dispSurface = new FramebufferSurface(*mHwc, state.type, bq);
1187 producer = bq;
Mathias Agopiancde87a32012-09-13 14:09:01 -07001188 }
1189
1190 const wp<IBinder>& display(curr.keyAt(i));
Jesse Hall99c7dbb2013-03-14 14:29:29 -07001191 if (dispSurface != NULL) {
Mathias Agopiancde87a32012-09-13 14:09:01 -07001192 sp<DisplayDevice> hw = new DisplayDevice(this,
Jesse Hall19e87292013-12-23 21:02:15 -08001193 state.type, hwcDisplayId,
1194 mHwc->getFormat(hwcDisplayId), state.isSecure,
Jesse Hall05f8c702013-12-23 20:44:38 -08001195 display, dispSurface, producer,
1196 mRenderEngine->getEGLConfig());
Mathias Agopiancde87a32012-09-13 14:09:01 -07001197 hw->setLayerStack(state.layerStack);
1198 hw->setProjection(state.orientation,
Jeff Brown4fb39992012-09-07 12:55:10 -07001199 state.viewport, state.frame);
Andy McFadden8dfa92f2012-09-17 18:27:17 -07001200 hw->setDisplayName(state.displayName);
Mathias Agopiancde87a32012-09-13 14:09:01 -07001201 mDisplays.add(display, hw);
Jesse Hall1c569c42013-04-05 13:44:52 -07001202 if (state.isVirtualDisplay()) {
1203 if (hwcDisplayId >= 0) {
1204 mHwc->setVirtualDisplayProperties(hwcDisplayId,
1205 hw->getWidth(), hw->getHeight(),
1206 hw->getFormat());
1207 }
1208 } else {
Jesse Hall7adb0f82013-03-06 16:13:49 -08001209 mEventThread->onHotplugReceived(state.type, true);
Jesse Hall1c569c42013-04-05 13:44:52 -07001210 }
Mathias Agopian93997a82012-08-29 17:30:36 -07001211 }
Mathias Agopian92a979a2012-08-02 18:32:23 -07001212 }
1213 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001214 }
Mathias Agopian3559b072012-08-15 13:46:03 -07001215 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001216
Mathias Agopian84300952012-11-21 16:02:13 -08001217 if (transactionFlags & (eTraversalNeeded|eDisplayTransactionNeeded)) {
1218 // The transform hint might have changed for some layers
1219 // (either because a display has changed, or because a layer
1220 // as changed).
1221 //
1222 // Walk through all the layers in currentLayers,
1223 // and update their transform hint.
1224 //
1225 // If a layer is visible only on a single display, then that
1226 // display is used to calculate the hint, otherwise we use the
1227 // default display.
1228 //
1229 // NOTE: we do this here, rather than in rebuildLayerStacks() so that
1230 // the hint is set before we acquire a buffer from the surface texture.
1231 //
1232 // NOTE: layer transactions have taken place already, so we use their
1233 // drawing state. However, SurfaceFlinger's own transaction has not
1234 // happened yet, so we must use the current state layer list
1235 // (soon to become the drawing state list).
1236 //
1237 sp<const DisplayDevice> disp;
1238 uint32_t currentlayerStack = 0;
1239 for (size_t i=0; i<count; i++) {
1240 // NOTE: we rely on the fact that layers are sorted by
1241 // layerStack first (so we don't have to traverse the list
1242 // of displays for every layer).
Mathias Agopian13127d82013-03-05 17:47:11 -08001243 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001244 uint32_t layerStack = layer->getDrawingState().layerStack;
Mathias Agopian84300952012-11-21 16:02:13 -08001245 if (i==0 || currentlayerStack != layerStack) {
1246 currentlayerStack = layerStack;
1247 // figure out if this layerstack is mirrored
1248 // (more than one display) if so, pick the default display,
1249 // if not, pick the only display it's on.
1250 disp.clear();
1251 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
1252 sp<const DisplayDevice> hw(mDisplays[dpy]);
1253 if (hw->getLayerStack() == currentlayerStack) {
1254 if (disp == NULL) {
1255 disp = hw;
1256 } else {
Chet Haase91d25932013-04-11 15:24:55 -07001257 disp = NULL;
Mathias Agopian84300952012-11-21 16:02:13 -08001258 break;
1259 }
1260 }
1261 }
1262 }
Chet Haase91d25932013-04-11 15:24:55 -07001263 if (disp == NULL) {
1264 // NOTE: TEMPORARY FIX ONLY. Real fix should cause layers to
1265 // redraw after transform hint changes. See bug 8508397.
1266
1267 // could be null when this layer is using a layerStack
1268 // that is not visible on any display. Also can occur at
1269 // screen off/on times.
1270 disp = getDefaultDisplayDevice();
Mathias Agopian84300952012-11-21 16:02:13 -08001271 }
Chet Haase91d25932013-04-11 15:24:55 -07001272 layer->updateTransformHint(disp);
Mathias Agopian84300952012-11-21 16:02:13 -08001273 }
1274 }
1275
1276
Mathias Agopian3559b072012-08-15 13:46:03 -07001277 /*
1278 * Perform our own transaction if needed
1279 */
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001280
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001281 const LayerVector& layers(mDrawingState.layersSortedByZ);
1282 if (currentLayers.size() > layers.size()) {
Mathias Agopian3559b072012-08-15 13:46:03 -07001283 // layers have been added
1284 mVisibleRegionsDirty = true;
1285 }
1286
1287 // some layers might have been removed, so
1288 // we need to update the regions they're exposing.
1289 if (mLayersRemoved) {
1290 mLayersRemoved = false;
1291 mVisibleRegionsDirty = true;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001292 const size_t count = layers.size();
Mathias Agopian3559b072012-08-15 13:46:03 -07001293 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001294 const sp<Layer>& layer(layers[i]);
Mathias Agopian3559b072012-08-15 13:46:03 -07001295 if (currentLayers.indexOf(layer) < 0) {
1296 // this layer is not visible anymore
1297 // TODO: we could traverse the tree from front to back and
1298 // compute the actual visible region
1299 // TODO: we could cache the transformed region
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001300 const Layer::State& s(layer->getDrawingState());
Mathias Agopian1501d542012-09-04 21:04:09 -07001301 Region visibleReg = s.transform.transform(
1302 Region(Rect(s.active.w, s.active.h)));
1303 invalidateLayerStack(s.layerStack, visibleReg);
Mathias Agopian0aa758d2009-04-22 15:23:34 -07001304 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001305 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001306 }
1307
1308 commitTransaction();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001309}
1310
1311void SurfaceFlinger::commitTransaction()
1312{
1313 if (!mLayersPendingRemoval.isEmpty()) {
1314 // Notify removed layers now that they can't be drawn from
1315 for (size_t i = 0; i < mLayersPendingRemoval.size(); i++) {
1316 mLayersPendingRemoval[i]->onRemoved();
1317 }
1318 mLayersPendingRemoval.clear();
1319 }
1320
Jamie Gennis4b0eba92013-02-05 13:30:24 -08001321 // If this transaction is part of a window animation then the next frame
1322 // we composite should be considered an animation as well.
1323 mAnimCompositionPending = mAnimTransactionPending;
1324
Mathias Agopian4fec8732012-06-29 14:12:52 -07001325 mDrawingState = mCurrentState;
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001326 mTransactionPending = false;
1327 mAnimTransactionPending = false;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001328 mTransactionCV.broadcast();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001329}
1330
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001331void SurfaceFlinger::computeVisibleRegions(
Mathias Agopian87baae12012-07-31 12:38:26 -07001332 const LayerVector& currentLayers, uint32_t layerStack,
1333 Region& outDirtyRegion, Region& outOpaqueRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001334{
Mathias Agopian841cde52012-03-01 15:44:37 -08001335 ATRACE_CALL();
1336
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001337 Region aboveOpaqueLayers;
1338 Region aboveCoveredLayers;
1339 Region dirty;
1340
Mathias Agopian87baae12012-07-31 12:38:26 -07001341 outDirtyRegion.clear();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001342
1343 size_t i = currentLayers.size();
1344 while (i--) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001345 const sp<Layer>& layer = currentLayers[i];
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001346
1347 // start with the whole surface at its current location
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001348 const Layer::State& s(layer->getDrawingState());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001349
Jesse Hall01e29052013-02-19 16:13:35 -08001350 // only consider the layers on the given layer stack
Mathias Agopian87baae12012-07-31 12:38:26 -07001351 if (s.layerStack != layerStack)
1352 continue;
1353
Mathias Agopianab028732010-03-16 16:41:46 -07001354 /*
1355 * opaqueRegion: area of a surface that is fully opaque.
1356 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001357 Region opaqueRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001358
1359 /*
1360 * visibleRegion: area of a surface that is visible on screen
1361 * and not fully transparent. This is essentially the layer's
1362 * footprint minus the opaque regions above it.
1363 * Areas covered by a translucent surface are considered visible.
1364 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001365 Region visibleRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001366
1367 /*
1368 * coveredRegion: area of a surface that is covered by all
1369 * visible regions above it (which includes the translucent areas).
1370 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001371 Region coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001372
Jesse Halla8026d22012-09-25 13:25:04 -07001373 /*
1374 * transparentRegion: area of a surface that is hinted to be completely
1375 * transparent. This is only used to tell when the layer has no visible
1376 * non-transparent regions and can be removed from the layer list. It
1377 * does not affect the visibleRegion of this layer or any layers
1378 * beneath it. The hint may not be correct if apps don't respect the
1379 * SurfaceView restrictions (which, sadly, some don't).
1380 */
1381 Region transparentRegion;
1382
Mathias Agopianab028732010-03-16 16:41:46 -07001383
1384 // handle hidden surfaces by setting the visible region to empty
Mathias Agopianda27af92012-09-13 18:17:13 -07001385 if (CC_LIKELY(layer->isVisible())) {
Andy McFadden4125a4f2014-01-29 17:17:11 -08001386 const bool translucent = !layer->isOpaque(s);
Mathias Agopian5219a062013-02-26 16:37:53 -08001387 Rect bounds(s.transform.transform(layer->computeBounds()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001388 visibleRegion.set(bounds);
Mathias Agopianab028732010-03-16 16:41:46 -07001389 if (!visibleRegion.isEmpty()) {
1390 // Remove the transparent area from the visible region
1391 if (translucent) {
Mathias Agopian4fec8732012-06-29 14:12:52 -07001392 const Transform tr(s.transform);
1393 if (tr.transformed()) {
1394 if (tr.preserveRects()) {
1395 // transform the transparent region
Mathias Agopian2ca79392013-04-02 18:30:32 -07001396 transparentRegion = tr.transform(s.activeTransparentRegion);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001397 } else {
1398 // transformation too complex, can't do the
1399 // transparent region optimization.
Jesse Halla8026d22012-09-25 13:25:04 -07001400 transparentRegion.clear();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001401 }
1402 } else {
Mathias Agopian2ca79392013-04-02 18:30:32 -07001403 transparentRegion = s.activeTransparentRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001404 }
Mathias Agopianab028732010-03-16 16:41:46 -07001405 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001406
Mathias Agopianab028732010-03-16 16:41:46 -07001407 // compute the opaque region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001408 const int32_t layerOrientation = s.transform.getOrientation();
Mathias Agopianab028732010-03-16 16:41:46 -07001409 if (s.alpha==255 && !translucent &&
1410 ((layerOrientation & Transform::ROT_INVALID) == false)) {
1411 // the opaque region is the layer's footprint
1412 opaqueRegion = visibleRegion;
1413 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001414 }
1415 }
1416
Mathias Agopianab028732010-03-16 16:41:46 -07001417 // Clip the covered region to the visible region
1418 coveredRegion = aboveCoveredLayers.intersect(visibleRegion);
1419
1420 // Update aboveCoveredLayers for next (lower) layer
1421 aboveCoveredLayers.orSelf(visibleRegion);
1422
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001423 // subtract the opaque region covered by the layers above us
1424 visibleRegion.subtractSelf(aboveOpaqueLayers);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001425
1426 // compute this layer's dirty region
1427 if (layer->contentDirty) {
1428 // we need to invalidate the whole region
1429 dirty = visibleRegion;
1430 // as well, as the old visible region
Mathias Agopian4fec8732012-06-29 14:12:52 -07001431 dirty.orSelf(layer->visibleRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001432 layer->contentDirty = false;
1433 } else {
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001434 /* compute the exposed region:
Mathias Agopianab028732010-03-16 16:41:46 -07001435 * the exposed region consists of two components:
1436 * 1) what's VISIBLE now and was COVERED before
1437 * 2) what's EXPOSED now less what was EXPOSED before
1438 *
1439 * note that (1) is conservative, we start with the whole
1440 * visible region but only keep what used to be covered by
1441 * something -- which mean it may have been exposed.
1442 *
1443 * (2) handles areas that were not covered by anything but got
1444 * exposed because of a resize.
Mathias Agopiana8d44f72009-06-28 02:54:16 -07001445 */
Mathias Agopianab028732010-03-16 16:41:46 -07001446 const Region newExposed = visibleRegion - coveredRegion;
Mathias Agopian4fec8732012-06-29 14:12:52 -07001447 const Region oldVisibleRegion = layer->visibleRegion;
1448 const Region oldCoveredRegion = layer->coveredRegion;
Mathias Agopianab028732010-03-16 16:41:46 -07001449 const Region oldExposed = oldVisibleRegion - oldCoveredRegion;
1450 dirty = (visibleRegion&oldCoveredRegion) | (newExposed-oldExposed);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001451 }
1452 dirty.subtractSelf(aboveOpaqueLayers);
1453
1454 // accumulate to the screen dirty region
Mathias Agopian87baae12012-07-31 12:38:26 -07001455 outDirtyRegion.orSelf(dirty);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001456
Mathias Agopianab028732010-03-16 16:41:46 -07001457 // Update aboveOpaqueLayers for next (lower) layer
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001458 aboveOpaqueLayers.orSelf(opaqueRegion);
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001459
Jesse Halla8026d22012-09-25 13:25:04 -07001460 // Store the visible region in screen space
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001461 layer->setVisibleRegion(visibleRegion);
1462 layer->setCoveredRegion(coveredRegion);
Jesse Halla8026d22012-09-25 13:25:04 -07001463 layer->setVisibleNonTransparentRegion(
1464 visibleRegion.subtract(transparentRegion));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001465 }
1466
Mathias Agopian87baae12012-07-31 12:38:26 -07001467 outOpaqueRegion = aboveOpaqueLayers;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001468}
1469
Mathias Agopian87baae12012-07-31 12:38:26 -07001470void SurfaceFlinger::invalidateLayerStack(uint32_t layerStack,
1471 const Region& dirty) {
Mathias Agopian92a979a2012-08-02 18:32:23 -07001472 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
Mathias Agopian42977342012-08-05 00:40:46 -07001473 const sp<DisplayDevice>& hw(mDisplays[dpy]);
1474 if (hw->getLayerStack() == layerStack) {
1475 hw->dirtyRegion.orSelf(dirty);
Mathias Agopian92a979a2012-08-02 18:32:23 -07001476 }
1477 }
Mathias Agopian87baae12012-07-31 12:38:26 -07001478}
1479
1480void SurfaceFlinger::handlePageFlip()
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001481{
Mathias Agopian4fec8732012-06-29 14:12:52 -07001482 Region dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001483
Mathias Agopian4fec8732012-06-29 14:12:52 -07001484 bool visibleRegions = false;
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001485 const LayerVector& layers(mDrawingState.layersSortedByZ);
1486 const size_t count = layers.size();
Mathias Agopian4fec8732012-06-29 14:12:52 -07001487 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001488 const sp<Layer>& layer(layers[i]);
Mathias Agopian87baae12012-07-31 12:38:26 -07001489 const Region dirty(layer->latchBuffer(visibleRegions));
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07001490 const Layer::State& s(layer->getDrawingState());
Mathias Agopian87baae12012-07-31 12:38:26 -07001491 invalidateLayerStack(s.layerStack, dirty);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001492 }
Mathias Agopian4da75192010-08-10 17:19:56 -07001493
Mathias Agopian3b1d2b62012-07-11 13:48:17 -07001494 mVisibleRegionsDirty |= visibleRegions;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001495}
1496
Mathias Agopianad456f92011-01-13 17:53:01 -08001497void SurfaceFlinger::invalidateHwcGeometry()
1498{
1499 mHwWorkListDirty = true;
1500}
1501
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001502
Mathias Agopiancd60f992012-08-16 16:28:27 -07001503void SurfaceFlinger::doDisplayComposition(const sp<const DisplayDevice>& hw,
Mathias Agopian87baae12012-07-31 12:38:26 -07001504 const Region& inDirtyRegion)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001505{
Dan Stoza71433162014-02-04 16:22:36 -08001506 // We only need to actually compose the display if:
1507 // 1) It is being handled by hardware composer, which may need this to
1508 // keep its virtual display state machine in sync, or
1509 // 2) There is work to be done (the dirty region isn't empty)
1510 bool isHwcDisplay = hw->getHwcDisplayId() >= 0;
1511 if (!isHwcDisplay && inDirtyRegion.isEmpty()) {
1512 return;
1513 }
1514
Mathias Agopian87baae12012-07-31 12:38:26 -07001515 Region dirtyRegion(inDirtyRegion);
1516
Mathias Agopianb8a55602009-06-26 19:06:36 -07001517 // compute the invalid region
Mathias Agopian42977342012-08-05 00:40:46 -07001518 hw->swapRegion.orSelf(dirtyRegion);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001519
Mathias Agopian42977342012-08-05 00:40:46 -07001520 uint32_t flags = hw->getFlags();
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001521 if (flags & DisplayDevice::SWAP_RECTANGLE) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001522 // we can redraw only what's dirty, but since SWAP_RECTANGLE only
1523 // takes a rectangle, we must make sure to update that whole
1524 // rectangle in that case
Mathias Agopian42977342012-08-05 00:40:46 -07001525 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001526 } else {
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001527 if (flags & DisplayDevice::PARTIAL_UPDATES) {
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001528 // We need to redraw the rectangle that will be updated
1529 // (pushed to the framebuffer).
Mathias Agopian95a666b2009-09-24 14:57:26 -07001530 // This is needed because PARTIAL_UPDATES only takes one
Mathias Agopian0f2f5ff2012-07-31 23:09:07 -07001531 // rectangle instead of a region (see DisplayDevice::flip())
Mathias Agopian42977342012-08-05 00:40:46 -07001532 dirtyRegion.set(hw->swapRegion.bounds());
Mathias Agopian29d06ac2009-06-29 18:49:56 -07001533 } else {
1534 // we need to redraw everything (the whole screen)
Mathias Agopian42977342012-08-05 00:40:46 -07001535 dirtyRegion.set(hw->bounds());
1536 hw->swapRegion = dirtyRegion;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001537 }
1538 }
1539
Alan Viverette9c5a3332013-09-12 20:04:35 -07001540 if (CC_LIKELY(!mDaltonize && !mHasColorMatrix)) {
Mathias Agopianff2ed702013-09-01 21:36:12 -07001541 doComposeSurfaces(hw, dirtyRegion);
1542 } else {
1543 RenderEngine& engine(getRenderEngine());
Alan Viverette9c5a3332013-09-12 20:04:35 -07001544 mat4 colorMatrix = mColorMatrix;
1545 if (mDaltonize) {
Alan Viverette9c5a3332013-09-12 20:04:35 -07001546 colorMatrix = colorMatrix * mDaltonizer();
Alan Viverette9c5a3332013-09-12 20:04:35 -07001547 }
1548 engine.beginGroup(colorMatrix);
Mathias Agopianff2ed702013-09-01 21:36:12 -07001549 doComposeSurfaces(hw, dirtyRegion);
1550 engine.endGroup();
1551 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001552
Mathias Agopian9c6e2972011-09-20 17:21:56 -07001553 // update the swap region and clear the dirty region
Mathias Agopian42977342012-08-05 00:40:46 -07001554 hw->swapRegion.orSelf(dirtyRegion);
Mathias Agopianda27af92012-09-13 18:17:13 -07001555
1556 // swap buffers (presentation)
1557 hw->swapBuffers(getHwComposer());
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001558}
1559
Mathias Agopiancd60f992012-08-16 16:28:27 -07001560void SurfaceFlinger::doComposeSurfaces(const sp<const DisplayDevice>& hw, const Region& dirty)
Mathias Agopianf384cc32011-09-08 18:31:55 -07001561{
Mathias Agopian3f844832013-08-07 21:24:32 -07001562 RenderEngine& engine(getRenderEngine());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001563 const int32_t id = hw->getHwcDisplayId();
Mathias Agopian86303202012-07-24 22:46:10 -07001564 HWComposer& hwc(getHwComposer());
Mathias Agopian1e260872012-08-08 18:35:12 -07001565 HWComposer::LayerListIterator cur = hwc.begin(id);
1566 const HWComposer::LayerListIterator end = hwc.end(id);
Mathias Agopiancd20eb02011-09-22 20:57:04 -07001567
Jesse Halld05a17f2013-09-30 16:49:30 -07001568 bool hasGlesComposition = hwc.hasGlesComposition(id);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001569 if (hasGlesComposition) {
Mathias Agopian875d8e12013-06-07 15:35:48 -07001570 if (!hw->makeCurrent(mEGLDisplay, mEGLContext)) {
Michael Chockc8c71092013-03-04 15:15:46 -08001571 ALOGW("DisplayDevice::makeCurrent failed. Aborting surface composition for display %s",
1572 hw->getDisplayName().string());
1573 return;
1574 }
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001575
Mathias Agopian52bbb1a2012-07-31 19:01:53 -07001576 // Never touch the framebuffer if we don't have any framebuffer layers
Mathias Agopian85d751c2012-08-29 16:59:24 -07001577 const bool hasHwcComposition = hwc.hasHwcComposition(id);
Mathias Agopiane60b0682012-08-21 23:34:09 -07001578 if (hasHwcComposition) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001579 // when using overlays, we assume a fully transparent framebuffer
1580 // NOTE: we could reduce how much we need to clear, for instance
1581 // remove where there are opaque FB layers. however, on some
Mathias Agopian3f844832013-08-07 21:24:32 -07001582 // GPUs doing a "clean slate" clear might be more efficient.
Mathias Agopianb9494d52012-04-18 02:28:45 -07001583 // We'll revisit later if needed.
Mathias Agopian3f844832013-08-07 21:24:32 -07001584 engine.clearWithColor(0, 0, 0, 0);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001585 } else {
Mathias Agopian766dc492012-10-30 18:08:06 -07001586 // we start with the whole screen area
1587 const Region bounds(hw->getBounds());
1588
1589 // we remove the scissor part
1590 // we're left with the letterbox region
1591 // (common case is that letterbox ends-up being empty)
1592 const Region letterbox(bounds.subtract(hw->getScissor()));
1593
1594 // compute the area to clear
1595 Region region(hw->undefinedRegion.merge(letterbox));
1596
1597 // but limit it to the dirty region
1598 region.andSelf(dirty);
1599
Mathias Agopianb9494d52012-04-18 02:28:45 -07001600 // screen is already cleared here
Mathias Agopian87baae12012-07-31 12:38:26 -07001601 if (!region.isEmpty()) {
Mathias Agopianb9494d52012-04-18 02:28:45 -07001602 // can happen with SurfaceView
Mathias Agopian55801e42012-08-27 18:54:24 -07001603 drawWormhole(hw, region);
Mathias Agopianb9494d52012-04-18 02:28:45 -07001604 }
Mathias Agopiana2f4e562012-04-15 23:34:59 -07001605 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001606
Mathias Agopian766dc492012-10-30 18:08:06 -07001607 if (hw->getDisplayType() != DisplayDevice::DISPLAY_PRIMARY) {
1608 // just to be on the safe side, we don't set the
Mathias Agopianf45c5102012-10-24 16:29:17 -07001609 // scissor on the main display. It should never be needed
1610 // anyways (though in theory it could since the API allows it).
1611 const Rect& bounds(hw->getBounds());
Mathias Agopian766dc492012-10-30 18:08:06 -07001612 const Rect& scissor(hw->getScissor());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001613 if (scissor != bounds) {
1614 // scissor doesn't match the screen's dimensions, so we
1615 // need to clear everything outside of it and enable
1616 // the GL scissor so we don't draw anything where we shouldn't
Mathias Agopian3f844832013-08-07 21:24:32 -07001617
Mathias Agopianf45c5102012-10-24 16:29:17 -07001618 // enable scissor for this frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001619 const uint32_t height = hw->getHeight();
1620 engine.setScissor(scissor.left, height - scissor.bottom,
1621 scissor.getWidth(), scissor.getHeight());
Mathias Agopianf45c5102012-10-24 16:29:17 -07001622 }
1623 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001624 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001625
Mathias Agopian85d751c2012-08-29 16:59:24 -07001626 /*
1627 * and then, render the layers targeted at the framebuffer
1628 */
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001629
Mathias Agopian13127d82013-03-05 17:47:11 -08001630 const Vector< sp<Layer> >& layers(hw->getVisibleLayersSortedByZ());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001631 const size_t count = layers.size();
1632 const Transform& tr = hw->getTransform();
1633 if (cur != end) {
1634 // we're using h/w composer
1635 for (size_t i=0 ; i<count && cur!=end ; ++i, ++cur) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001636 const sp<Layer>& layer(layers[i]);
Mathias Agopian4fec8732012-06-29 14:12:52 -07001637 const Region clip(dirty.intersect(tr.transform(layer->visibleRegion)));
Mathias Agopian85d751c2012-08-29 16:59:24 -07001638 if (!clip.isEmpty()) {
1639 switch (cur->getCompositionType()) {
1640 case HWC_OVERLAY: {
Mathias Agopianac683022013-10-01 15:36:52 -07001641 const Layer::State& state(layer->getDrawingState());
Mathias Agopian85d751c2012-08-29 16:59:24 -07001642 if ((cur->getHints() & HWC_HINT_CLEAR_FB)
1643 && i
Andy McFadden4125a4f2014-01-29 17:17:11 -08001644 && layer->isOpaque(state) && (state.alpha == 0xFF)
Mathias Agopian85d751c2012-08-29 16:59:24 -07001645 && hasGlesComposition) {
Mathias Agopiancd60f992012-08-16 16:28:27 -07001646 // never clear the very first layer since we're
1647 // guaranteed the FB is already cleared
1648 layer->clearWithOpenGL(hw, clip);
1649 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001650 break;
Mathias Agopiancd60f992012-08-16 16:28:27 -07001651 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001652 case HWC_FRAMEBUFFER: {
1653 layer->draw(hw, clip);
1654 break;
1655 }
Mathias Agopianda27af92012-09-13 18:17:13 -07001656 case HWC_FRAMEBUFFER_TARGET: {
1657 // this should not happen as the iterator shouldn't
1658 // let us get there.
Greg Hackmann86efcc02014-03-07 12:44:02 -08001659 ALOGW("HWC_FRAMEBUFFER_TARGET found in hwc list (index=%zu)", i);
Mathias Agopianda27af92012-09-13 18:17:13 -07001660 break;
1661 }
Mathias Agopiancd60f992012-08-16 16:28:27 -07001662 }
Mathias Agopian85d751c2012-08-29 16:59:24 -07001663 }
1664 layer->setAcquireFence(hw, *cur);
1665 }
1666 } else {
1667 // we're not using h/w composer
1668 for (size_t i=0 ; i<count ; ++i) {
Mathias Agopian13127d82013-03-05 17:47:11 -08001669 const sp<Layer>& layer(layers[i]);
Mathias Agopian85d751c2012-08-29 16:59:24 -07001670 const Region clip(dirty.intersect(
1671 tr.transform(layer->visibleRegion)));
1672 if (!clip.isEmpty()) {
1673 layer->draw(hw, clip);
Jesse Halla6b32db2012-07-19 16:44:38 -07001674 }
Mathias Agopian4b2ba532012-03-29 12:23:51 -07001675 }
1676 }
Mathias Agopianf45c5102012-10-24 16:29:17 -07001677
1678 // disable scissor at the end of the frame
Mathias Agopian3f844832013-08-07 21:24:32 -07001679 engine.disableScissor();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001680}
1681
Mathias Agopian3f844832013-08-07 21:24:32 -07001682void SurfaceFlinger::drawWormhole(const sp<const DisplayDevice>& hw, const Region& region) const {
Mathias Agopian55801e42012-08-27 18:54:24 -07001683 const int32_t height = hw->getHeight();
Mathias Agopian3f844832013-08-07 21:24:32 -07001684 RenderEngine& engine(getRenderEngine());
1685 engine.fillRegionWithColor(region, height, 0, 0, 0, 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001686}
1687
Mathias Agopianac9fa422013-02-11 16:40:36 -08001688void SurfaceFlinger::addClientLayer(const sp<Client>& client,
1689 const sp<IBinder>& handle,
Mathias Agopian67106042013-03-14 19:18:13 -07001690 const sp<IGraphicBufferProducer>& gbc,
Mathias Agopian13127d82013-03-05 17:47:11 -08001691 const sp<Layer>& lbc)
Mathias Agopian96f08192010-06-02 23:28:45 -07001692{
Mathias Agopian96f08192010-06-02 23:28:45 -07001693 // attach this layer to the client
Mathias Agopianac9fa422013-02-11 16:40:36 -08001694 client->attachLayer(handle, lbc);
Mathias Agopian4f113742011-05-03 16:21:41 -07001695
Mathias Agopian96f08192010-06-02 23:28:45 -07001696 // add this layer to the current state list
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001697 Mutex::Autolock _l(mStateLock);
1698 mCurrentState.layersSortedByZ.add(lbc);
Mathias Agopian67106042013-03-14 19:18:13 -07001699 mGraphicBufferProducerList.add(gbc->asBinder());
Mathias Agopian96f08192010-06-02 23:28:45 -07001700}
1701
Mathias Agopian3f844832013-08-07 21:24:32 -07001702status_t SurfaceFlinger::removeLayer(const sp<Layer>& layer) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001703 Mutex::Autolock _l(mStateLock);
Mathias Agopian13127d82013-03-05 17:47:11 -08001704 ssize_t index = mCurrentState.layersSortedByZ.remove(layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001705 if (index >= 0) {
Mathias Agopian67106042013-03-14 19:18:13 -07001706 mLayersPendingRemoval.push(layer);
Mathias Agopian076b1cc2009-04-10 14:24:30 -07001707 mLayersRemoved = true;
Mathias Agopian67106042013-03-14 19:18:13 -07001708 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001709 return NO_ERROR;
1710 }
Mathias Agopian3d579642009-06-04 18:46:21 -07001711 return status_t(index);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001712}
1713
Dan Stozac7014012014-02-14 15:03:43 -08001714uint32_t SurfaceFlinger::peekTransactionFlags(uint32_t /* flags */) {
Mathias Agopiandea20b12011-05-03 17:04:02 -07001715 return android_atomic_release_load(&mTransactionFlags);
1716}
1717
Mathias Agopian3f844832013-08-07 21:24:32 -07001718uint32_t SurfaceFlinger::getTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001719 return android_atomic_and(~flags, &mTransactionFlags) & flags;
1720}
1721
Mathias Agopian3f844832013-08-07 21:24:32 -07001722uint32_t SurfaceFlinger::setTransactionFlags(uint32_t flags) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001723 uint32_t old = android_atomic_or(flags, &mTransactionFlags);
1724 if ((old & flags)==0) { // wake the server up
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08001725 signalTransaction();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001726 }
1727 return old;
1728}
1729
Mathias Agopian8b33f032012-07-24 20:43:54 -07001730void SurfaceFlinger::setTransactionState(
1731 const Vector<ComposerState>& state,
1732 const Vector<DisplayState>& displays,
1733 uint32_t flags)
1734{
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001735 ATRACE_CALL();
Mathias Agopian698c0872011-06-28 19:09:31 -07001736 Mutex::Autolock _l(mStateLock);
Jamie Gennis28378392011-10-12 17:39:00 -07001737 uint32_t transactionFlags = 0;
Mathias Agopiane57f2922012-08-09 16:29:12 -07001738
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001739 if (flags & eAnimation) {
1740 // For window updates that are part of an animation we must wait for
1741 // previous animation "frames" to be handled.
1742 while (mAnimTransactionPending) {
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001743 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001744 if (CC_UNLIKELY(err != NO_ERROR)) {
1745 // just in case something goes wrong in SF, return to the
Jamie Gennis7c41bf72012-10-17 09:29:47 -07001746 // caller after a few seconds.
1747 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out "
1748 "waiting for previous animation frame");
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001749 mAnimTransactionPending = false;
1750 break;
1751 }
1752 }
1753 }
1754
Mathias Agopiane57f2922012-08-09 16:29:12 -07001755 size_t count = displays.size();
1756 for (size_t i=0 ; i<count ; i++) {
1757 const DisplayState& s(displays[i]);
1758 transactionFlags |= setDisplayStateLocked(s);
Jamie Gennisb8d69a52011-10-10 15:48:06 -07001759 }
1760
Mathias Agopiane57f2922012-08-09 16:29:12 -07001761 count = state.size();
Mathias Agopian698c0872011-06-28 19:09:31 -07001762 for (size_t i=0 ; i<count ; i++) {
1763 const ComposerState& s(state[i]);
Mathias Agopiand17e3b52012-10-22 14:27:45 -07001764 // Here we need to check that the interface we're given is indeed
1765 // one of our own. A malicious client could give us a NULL
1766 // IInterface, or one of its own or even one of our own but a
1767 // different type. All these situations would cause us to crash.
1768 //
1769 // NOTE: it would be better to use RTTI as we could directly check
1770 // that we have a Client*. however, RTTI is disabled in Android.
1771 if (s.client != NULL) {
1772 sp<IBinder> binder = s.client->asBinder();
1773 if (binder != NULL) {
1774 String16 desc(binder->getInterfaceDescriptor());
1775 if (desc == ISurfaceComposerClient::descriptor) {
1776 sp<Client> client( static_cast<Client *>(s.client.get()) );
1777 transactionFlags |= setClientStateLocked(client, s.state);
1778 }
1779 }
1780 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001781 }
Mathias Agopian698c0872011-06-28 19:09:31 -07001782
Mathias Agopian386aa982011-11-07 21:58:03 -08001783 if (transactionFlags) {
1784 // this triggers the transaction
1785 setTransactionFlags(transactionFlags);
1786
1787 // if this is a synchronous transaction, wait for it to take effect
1788 // before returning.
1789 if (flags & eSynchronous) {
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001790 mTransactionPending = true;
Mathias Agopian386aa982011-11-07 21:58:03 -08001791 }
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001792 if (flags & eAnimation) {
1793 mAnimTransactionPending = true;
1794 }
1795 while (mTransactionPending) {
Mathias Agopian386aa982011-11-07 21:58:03 -08001796 status_t err = mTransactionCV.waitRelative(mStateLock, s2ns(5));
1797 if (CC_UNLIKELY(err != NO_ERROR)) {
1798 // just in case something goes wrong in SF, return to the
1799 // called after a few seconds.
Jamie Gennis2d5e2302012-10-15 18:24:43 -07001800 ALOGW_IF(err == TIMED_OUT, "setTransactionState timed out!");
1801 mTransactionPending = false;
Mathias Agopian386aa982011-11-07 21:58:03 -08001802 break;
1803 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07001804 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001805 }
1806}
1807
Mathias Agopiane57f2922012-08-09 16:29:12 -07001808uint32_t SurfaceFlinger::setDisplayStateLocked(const DisplayState& s)
1809{
Jesse Hall9a143922012-10-04 16:29:19 -07001810 ssize_t dpyIdx = mCurrentState.displays.indexOfKey(s.token);
1811 if (dpyIdx < 0)
1812 return 0;
1813
Mathias Agopiane57f2922012-08-09 16:29:12 -07001814 uint32_t flags = 0;
Jesse Hall9a143922012-10-04 16:29:19 -07001815 DisplayDeviceState& disp(mCurrentState.displays.editValueAt(dpyIdx));
Mathias Agopian3ee454a2012-08-27 16:28:24 -07001816 if (disp.isValid()) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001817 const uint32_t what = s.what;
1818 if (what & DisplayState::eSurfaceChanged) {
1819 if (disp.surface->asBinder() != s.surface->asBinder()) {
1820 disp.surface = s.surface;
1821 flags |= eDisplayTransactionNeeded;
1822 }
1823 }
1824 if (what & DisplayState::eLayerStackChanged) {
1825 if (disp.layerStack != s.layerStack) {
1826 disp.layerStack = s.layerStack;
1827 flags |= eDisplayTransactionNeeded;
1828 }
1829 }
Mathias Agopian00e8c7a2012-09-04 19:30:46 -07001830 if (what & DisplayState::eDisplayProjectionChanged) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07001831 if (disp.orientation != s.orientation) {
1832 disp.orientation = s.orientation;
1833 flags |= eDisplayTransactionNeeded;
1834 }
1835 if (disp.frame != s.frame) {
1836 disp.frame = s.frame;
1837 flags |= eDisplayTransactionNeeded;
1838 }
1839 if (disp.viewport != s.viewport) {
1840 disp.viewport = s.viewport;
1841 flags |= eDisplayTransactionNeeded;
1842 }
1843 }
1844 }
1845 return flags;
1846}
1847
1848uint32_t SurfaceFlinger::setClientStateLocked(
1849 const sp<Client>& client,
1850 const layer_state_t& s)
1851{
1852 uint32_t flags = 0;
Mathias Agopian13127d82013-03-05 17:47:11 -08001853 sp<Layer> layer(client->getLayerUser(s.surface));
Mathias Agopiane57f2922012-08-09 16:29:12 -07001854 if (layer != 0) {
1855 const uint32_t what = s.what;
1856 if (what & layer_state_t::ePositionChanged) {
1857 if (layer->setPosition(s.x, s.y))
1858 flags |= eTraversalNeeded;
1859 }
1860 if (what & layer_state_t::eLayerChanged) {
1861 // NOTE: index needs to be calculated before we update the state
1862 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1863 if (layer->setLayer(s.z)) {
1864 mCurrentState.layersSortedByZ.removeAt(idx);
1865 mCurrentState.layersSortedByZ.add(layer);
1866 // we need traversal (state changed)
1867 // AND transaction (list changed)
1868 flags |= eTransactionNeeded|eTraversalNeeded;
1869 }
1870 }
1871 if (what & layer_state_t::eSizeChanged) {
1872 if (layer->setSize(s.w, s.h)) {
1873 flags |= eTraversalNeeded;
1874 }
1875 }
1876 if (what & layer_state_t::eAlphaChanged) {
1877 if (layer->setAlpha(uint8_t(255.0f*s.alpha+0.5f)))
1878 flags |= eTraversalNeeded;
1879 }
1880 if (what & layer_state_t::eMatrixChanged) {
1881 if (layer->setMatrix(s.matrix))
1882 flags |= eTraversalNeeded;
1883 }
1884 if (what & layer_state_t::eTransparentRegionChanged) {
1885 if (layer->setTransparentRegionHint(s.transparentRegion))
1886 flags |= eTraversalNeeded;
1887 }
Andy McFadden4125a4f2014-01-29 17:17:11 -08001888 if ((what & layer_state_t::eVisibilityChanged) ||
1889 (what & layer_state_t::eOpacityChanged)) {
1890 // TODO: should we just use an eFlagsChanged for this?
Mathias Agopiane57f2922012-08-09 16:29:12 -07001891 if (layer->setFlags(s.flags, s.mask))
1892 flags |= eTraversalNeeded;
1893 }
1894 if (what & layer_state_t::eCropChanged) {
1895 if (layer->setCrop(s.crop))
1896 flags |= eTraversalNeeded;
1897 }
1898 if (what & layer_state_t::eLayerStackChanged) {
1899 // NOTE: index needs to be calculated before we update the state
1900 ssize_t idx = mCurrentState.layersSortedByZ.indexOf(layer);
1901 if (layer->setLayerStack(s.layerStack)) {
1902 mCurrentState.layersSortedByZ.removeAt(idx);
1903 mCurrentState.layersSortedByZ.add(layer);
1904 // we need traversal (state changed)
1905 // AND transaction (list changed)
1906 flags |= eTransactionNeeded|eTraversalNeeded;
1907 }
1908 }
1909 }
1910 return flags;
1911}
1912
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001913status_t SurfaceFlinger::createLayer(
Mathias Agopian0ef4e152011-04-20 14:19:32 -07001914 const String8& name,
1915 const sp<Client>& client,
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001916 uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
1917 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001918{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001919 //ALOGD("createLayer for (%d x %d), name=%s", w, h, name.string());
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001920 if (int32_t(w|h) < 0) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07001921 ALOGE("createLayer() failed, w or h is negative (w=%d, h=%d)",
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001922 int(w), int(h));
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001923 return BAD_VALUE;
Mathias Agopian6e2d6482009-07-09 18:16:43 -07001924 }
Andreas Huber8b42e8a2010-08-16 08:49:37 -07001925
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001926 status_t result = NO_ERROR;
1927
1928 sp<Layer> layer;
1929
Mathias Agopian3165cc22012-08-08 19:42:09 -07001930 switch (flags & ISurfaceComposerClient::eFXSurfaceMask) {
1931 case ISurfaceComposerClient::eFXSurfaceNormal:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001932 result = createNormalLayer(client,
1933 name, w, h, flags, format,
1934 handle, gbp, &layer);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001935 break;
Mathias Agopian3165cc22012-08-08 19:42:09 -07001936 case ISurfaceComposerClient::eFXSurfaceDim:
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001937 result = createDimLayer(client,
1938 name, w, h, flags,
1939 handle, gbp, &layer);
1940 break;
1941 default:
1942 result = BAD_VALUE;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001943 break;
1944 }
1945
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001946 if (result == NO_ERROR) {
Mathias Agopian67106042013-03-14 19:18:13 -07001947 addClientLayer(client, *handle, *gbp, layer);
Mathias Agopian96f08192010-06-02 23:28:45 -07001948 setTransactionFlags(eTransactionNeeded);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001949 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001950 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001951}
1952
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001953status_t SurfaceFlinger::createNormalLayer(const sp<Client>& client,
1954 const String8& name, uint32_t w, uint32_t h, uint32_t flags, PixelFormat& format,
1955 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001956{
1957 // initialize the surfaces
Mathias Agopian92a979a2012-08-02 18:32:23 -07001958 switch (format) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001959 case PIXEL_FORMAT_TRANSPARENT:
1960 case PIXEL_FORMAT_TRANSLUCENT:
1961 format = PIXEL_FORMAT_RGBA_8888;
1962 break;
1963 case PIXEL_FORMAT_OPAQUE:
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001964#ifdef NO_RGBX_8888
1965 format = PIXEL_FORMAT_RGB_565;
1966#else
Mathias Agopian8f105402010-04-05 18:01:24 -07001967 format = PIXEL_FORMAT_RGBX_8888;
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001968#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001969 break;
1970 }
1971
Mathias Agopiana8f3e4e2010-06-30 15:43:47 -07001972#ifdef NO_RGBX_8888
1973 if (format == PIXEL_FORMAT_RGBX_8888)
1974 format = PIXEL_FORMAT_RGBA_8888;
1975#endif
1976
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001977 *outLayer = new Layer(this, client, name, w, h, flags);
1978 status_t err = (*outLayer)->setBuffers(w, h, format, flags);
1979 if (err == NO_ERROR) {
1980 *handle = (*outLayer)->getHandle();
1981 *gbp = (*outLayer)->getBufferQueue();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001982 }
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001983
1984 ALOGE_IF(err, "createNormalLayer() failed (%s)", strerror(-err));
1985 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001986}
1987
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001988status_t SurfaceFlinger::createDimLayer(const sp<Client>& client,
1989 const String8& name, uint32_t w, uint32_t h, uint32_t flags,
1990 sp<IBinder>* handle, sp<IGraphicBufferProducer>* gbp, sp<Layer>* outLayer)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001991{
Mathias Agopian4d9b8222013-03-12 17:11:48 -07001992 *outLayer = new LayerDim(this, client, name, w, h, flags);
1993 *handle = (*outLayer)->getHandle();
1994 *gbp = (*outLayer)->getBufferQueue();
1995 return NO_ERROR;
Mathias Agopian118d0242011-10-13 16:02:48 -07001996}
1997
Mathias Agopianac9fa422013-02-11 16:40:36 -08001998status_t SurfaceFlinger::onLayerRemoved(const sp<Client>& client, const sp<IBinder>& handle)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001999{
Mathias Agopian67106042013-03-14 19:18:13 -07002000 // called by the window manager when it wants to remove a Layer
2001 status_t err = NO_ERROR;
2002 sp<Layer> l(client->getLayerUser(handle));
2003 if (l != NULL) {
2004 err = removeLayer(l);
2005 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
2006 "error removing layer=%p (%s)", l.get(), strerror(-err));
Mathias Agopian9a112062009-04-17 19:36:26 -07002007 }
2008 return err;
2009}
2010
Mathias Agopian13127d82013-03-05 17:47:11 -08002011status_t SurfaceFlinger::onLayerDestroyed(const wp<Layer>& layer)
Mathias Agopian9a112062009-04-17 19:36:26 -07002012{
Mathias Agopian67106042013-03-14 19:18:13 -07002013 // called by ~LayerCleaner() when all references to the IBinder (handle)
2014 // are gone
Mathias Agopianca4d3602011-05-19 15:38:14 -07002015 status_t err = NO_ERROR;
Mathias Agopian13127d82013-03-05 17:47:11 -08002016 sp<Layer> l(layer.promote());
Mathias Agopianca4d3602011-05-19 15:38:14 -07002017 if (l != NULL) {
Mathias Agopian67106042013-03-14 19:18:13 -07002018 err = removeLayer(l);
Steve Blocke6f43dd2012-01-06 19:20:56 +00002019 ALOGE_IF(err<0 && err != NAME_NOT_FOUND,
Mathias Agopianca4d3602011-05-19 15:38:14 -07002020 "error removing layer=%p (%s)", l.get(), strerror(-err));
2021 }
2022 return err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002023}
2024
Mathias Agopianb60314a2012-04-10 22:09:54 -07002025// ---------------------------------------------------------------------------
2026
Andy McFadden13a082e2012-08-24 10:16:42 -07002027void SurfaceFlinger::onInitializeDisplays() {
Jesse Hall01e29052013-02-19 16:13:35 -08002028 // reset screen orientation and use primary layer stack
Andy McFadden13a082e2012-08-24 10:16:42 -07002029 Vector<ComposerState> state;
2030 Vector<DisplayState> displays;
2031 DisplayState d;
Jesse Hall01e29052013-02-19 16:13:35 -08002032 d.what = DisplayState::eDisplayProjectionChanged |
2033 DisplayState::eLayerStackChanged;
Jesse Hall692c7232012-11-08 15:41:56 -08002034 d.token = mBuiltinDisplays[DisplayDevice::DISPLAY_PRIMARY];
Jesse Hall01e29052013-02-19 16:13:35 -08002035 d.layerStack = 0;
Andy McFadden13a082e2012-08-24 10:16:42 -07002036 d.orientation = DisplayState::eOrientationDefault;
Jeff Brown4c05dd12012-09-09 00:07:17 -07002037 d.frame.makeInvalid();
2038 d.viewport.makeInvalid();
Andy McFadden13a082e2012-08-24 10:16:42 -07002039 displays.add(d);
2040 setTransactionState(state, displays, 0);
Mathias Agopiancde87a32012-09-13 14:09:01 -07002041 onScreenAcquired(getDefaultDisplayDevice());
Jamie Gennis6547ff42013-07-16 20:12:42 -07002042
2043 const nsecs_t period =
2044 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
2045 mAnimFrameTracker.setDisplayRefreshPeriod(period);
Andy McFadden13a082e2012-08-24 10:16:42 -07002046}
2047
2048void SurfaceFlinger::initializeDisplays() {
2049 class MessageScreenInitialized : public MessageBase {
2050 SurfaceFlinger* flinger;
2051 public:
2052 MessageScreenInitialized(SurfaceFlinger* flinger) : flinger(flinger) { }
2053 virtual bool handler() {
2054 flinger->onInitializeDisplays();
2055 return true;
2056 }
2057 };
2058 sp<MessageBase> msg = new MessageScreenInitialized(this);
2059 postMessageAsync(msg); // we may be called from main thread, use async message
2060}
2061
2062
Mathias Agopiancde87a32012-09-13 14:09:01 -07002063void SurfaceFlinger::onScreenAcquired(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002064 ALOGD("Screen acquired, type=%d flinger=%p", hw->getDisplayType(), this);
2065 if (hw->isScreenAcquired()) {
2066 // this is expected, e.g. when power manager wakes up during boot
2067 ALOGD(" screen was previously acquired");
2068 return;
2069 }
2070
Mathias Agopian42977342012-08-05 00:40:46 -07002071 hw->acquireScreen();
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002072 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002073 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002074 // built-in display, tell the HWC
2075 getHwComposer().acquire(type);
2076
2077 if (type == DisplayDevice::DISPLAY_PRIMARY) {
2078 // FIXME: eventthread only knows about the main display right now
2079 mEventThread->onScreenAcquired();
Jamie Gennisfaf77cc2013-07-30 15:10:32 -07002080
Jesse Hall948fe0c2013-10-14 12:56:09 -07002081 resyncToHardwareVsync(true);
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002082 }
Mathias Agopiancde87a32012-09-13 14:09:01 -07002083 }
Mathias Agopian20128302012-08-13 18:32:13 -07002084 mVisibleRegionsDirty = true;
2085 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002086}
2087
Mathias Agopiancde87a32012-09-13 14:09:01 -07002088void SurfaceFlinger::onScreenReleased(const sp<const DisplayDevice>& hw) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002089 ALOGD("Screen released, type=%d flinger=%p", hw->getDisplayType(), this);
2090 if (!hw->isScreenAcquired()) {
2091 ALOGD(" screen was previously released");
2092 return;
2093 }
2094
2095 hw->releaseScreen();
2096 int32_t type = hw->getDisplayType();
Jesse Hall9e663de2013-08-16 14:28:37 -07002097 if (type < DisplayDevice::NUM_BUILTIN_DISPLAY_TYPES) {
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002098 if (type == DisplayDevice::DISPLAY_PRIMARY) {
Jesse Hall948fe0c2013-10-14 12:56:09 -07002099 disableHardwareVsync(true); // also cancels any in-progress resync
2100
Mathias Agopiancde87a32012-09-13 14:09:01 -07002101 // FIXME: eventthread only knows about the main display right now
2102 mEventThread->onScreenReleased();
2103 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002104
2105 // built-in display, tell the HWC
2106 getHwComposer().release(type);
2107 }
2108 mVisibleRegionsDirty = true;
2109 // from this point on, SF will stop drawing on this display
2110}
2111
2112void SurfaceFlinger::unblank(const sp<IBinder>& display) {
2113 class MessageScreenAcquired : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002114 SurfaceFlinger& mFlinger;
2115 sp<IBinder> mDisplay;
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002116 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002117 MessageScreenAcquired(SurfaceFlinger& flinger,
2118 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002119 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002120 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2121 if (hw == NULL) {
2122 ALOGE("Attempt to unblank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002123 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002124 ALOGW("Attempt to unblank virtual display");
2125 } else {
2126 mFlinger.onScreenAcquired(hw);
2127 }
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002128 return true;
2129 }
2130 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002131 sp<MessageBase> msg = new MessageScreenAcquired(*this, display);
2132 postMessageSync(msg);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002133}
2134
Andy McFaddenc01a79d2012-09-27 16:02:06 -07002135void SurfaceFlinger::blank(const sp<IBinder>& display) {
Mathias Agopianb60314a2012-04-10 22:09:54 -07002136 class MessageScreenReleased : public MessageBase {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002137 SurfaceFlinger& mFlinger;
2138 sp<IBinder> mDisplay;
Mathias Agopianb60314a2012-04-10 22:09:54 -07002139 public:
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002140 MessageScreenReleased(SurfaceFlinger& flinger,
2141 const sp<IBinder>& disp) : mFlinger(flinger), mDisplay(disp) { }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002142 virtual bool handler() {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002143 const sp<DisplayDevice> hw(mFlinger.getDisplayDevice(mDisplay));
2144 if (hw == NULL) {
2145 ALOGE("Attempt to blank null display %p", mDisplay.get());
Jesse Hall9e663de2013-08-16 14:28:37 -07002146 } else if (hw->getDisplayType() >= DisplayDevice::DISPLAY_VIRTUAL) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002147 ALOGW("Attempt to blank virtual display");
2148 } else {
2149 mFlinger.onScreenReleased(hw);
2150 }
Mathias Agopianb60314a2012-04-10 22:09:54 -07002151 return true;
2152 }
2153 };
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002154 sp<MessageBase> msg = new MessageScreenReleased(*this, display);
2155 postMessageSync(msg);
Mathias Agopianb60314a2012-04-10 22:09:54 -07002156}
2157
2158// ---------------------------------------------------------------------------
2159
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002160status_t SurfaceFlinger::dump(int fd, const Vector<String16>& args)
2161{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002162 String8 result;
Mathias Agopian99b49842011-06-27 16:05:52 -07002163
Mathias Agopianbd115332013-04-18 16:41:04 -07002164 IPCThreadState* ipc = IPCThreadState::self();
2165 const int pid = ipc->getCallingPid();
2166 const int uid = ipc->getCallingUid();
2167 if ((uid != AID_SHELL) &&
2168 !PermissionCache::checkPermission(sDump, pid, uid)) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002169 result.appendFormat("Permission Denial: "
Mathias Agopianbd115332013-04-18 16:41:04 -07002170 "can't dump SurfaceFlinger from pid=%d, uid=%d\n", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002171 } else {
Mathias Agopian9795c422009-08-26 16:36:26 -07002172 // Try to get the main lock, but don't insist if we can't
2173 // (this would indicate SF is stuck, but we want to be able to
2174 // print something in dumpsys).
2175 int retry = 3;
2176 while (mStateLock.tryLock()<0 && --retry>=0) {
2177 usleep(1000000);
2178 }
2179 const bool locked(retry >= 0);
2180 if (!locked) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002181 result.append(
Mathias Agopian9795c422009-08-26 16:36:26 -07002182 "SurfaceFlinger appears to be unresponsive, "
2183 "dumping anyways (no locks held)\n");
Mathias Agopian9795c422009-08-26 16:36:26 -07002184 }
2185
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002186 bool dumpAll = true;
2187 size_t index = 0;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002188 size_t numArgs = args.size();
2189 if (numArgs) {
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002190 if ((index < numArgs) &&
2191 (args[index] == String16("--list"))) {
2192 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002193 listLayersLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002194 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002195 }
2196
2197 if ((index < numArgs) &&
2198 (args[index] == String16("--latency"))) {
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002199 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002200 dumpStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002201 dumpAll = false;
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002202 }
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002203
2204 if ((index < numArgs) &&
2205 (args[index] == String16("--latency-clear"))) {
2206 index++;
Mathias Agopian74d211a2013-04-22 16:55:35 +02002207 clearStatsLocked(args, index, result);
Mathias Agopian35aadd62012-03-08 22:01:51 -08002208 dumpAll = false;
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002209 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002210 }
Mathias Agopian1b5e1022010-04-20 17:55:49 -07002211
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002212 if (dumpAll) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002213 dumpAllLocked(args, index, result);
Mathias Agopian48b888a2011-01-19 16:15:53 -08002214 }
2215
Mathias Agopian9795c422009-08-26 16:36:26 -07002216 if (locked) {
2217 mStateLock.unlock();
2218 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002219 }
2220 write(fd, result.string(), result.size());
2221 return NO_ERROR;
2222}
2223
Dan Stozac7014012014-02-14 15:03:43 -08002224void SurfaceFlinger::listLayersLocked(const Vector<String16>& /* args */,
2225 size_t& /* index */, String8& result) const
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002226{
2227 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2228 const size_t count = currentLayers.size();
2229 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002230 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002231 result.appendFormat("%s\n", layer->getName().string());
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002232 }
2233}
2234
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002235void SurfaceFlinger::dumpStatsLocked(const Vector<String16>& args, size_t& index,
Mathias Agopian74d211a2013-04-22 16:55:35 +02002236 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002237{
2238 String8 name;
2239 if (index < args.size()) {
2240 name = String8(args[index]);
2241 index++;
2242 }
2243
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002244 const nsecs_t period =
2245 getHwComposer().getRefreshPeriod(HWC_DISPLAY_PRIMARY);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002246 result.appendFormat("%" PRId64 "\n", period);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002247
2248 if (name.isEmpty()) {
2249 mAnimFrameTracker.dump(result);
2250 } else {
2251 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2252 const size_t count = currentLayers.size();
2253 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002254 const sp<Layer>& layer(currentLayers[i]);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002255 if (name == layer->getName()) {
Mathias Agopian74d211a2013-04-22 16:55:35 +02002256 layer->dumpStats(result);
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002257 }
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002258 }
2259 }
2260}
2261
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002262void SurfaceFlinger::clearStatsLocked(const Vector<String16>& args, size_t& index,
Dan Stozac7014012014-02-14 15:03:43 -08002263 String8& /* result */)
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002264{
2265 String8 name;
2266 if (index < args.size()) {
2267 name = String8(args[index]);
2268 index++;
2269 }
2270
2271 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2272 const size_t count = currentLayers.size();
2273 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002274 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002275 if (name.isEmpty() || (name == layer->getName())) {
2276 layer->clearStats();
2277 }
2278 }
Jamie Gennis4b0eba92013-02-05 13:30:24 -08002279
2280 mAnimFrameTracker.clear();
Mathias Agopian25e66fc2012-01-28 22:31:55 -08002281}
2282
Jamie Gennis6547ff42013-07-16 20:12:42 -07002283// This should only be called from the main thread. Otherwise it would need
2284// the lock and should use mCurrentState rather than mDrawingState.
2285void SurfaceFlinger::logFrameStats() {
2286 const LayerVector& drawingLayers = mDrawingState.layersSortedByZ;
2287 const size_t count = drawingLayers.size();
2288 for (size_t i=0 ; i<count ; i++) {
2289 const sp<Layer>& layer(drawingLayers[i]);
2290 layer->logFrameStats();
2291 }
2292
2293 mAnimFrameTracker.logAndResetStats(String8("<win-anim>"));
2294}
2295
Andy McFadden4803b742012-09-24 19:07:20 -07002296/*static*/ void SurfaceFlinger::appendSfConfigString(String8& result)
2297{
2298 static const char* config =
2299 " [sf"
2300#ifdef NO_RGBX_8888
2301 " NO_RGBX_8888"
2302#endif
2303#ifdef HAS_CONTEXT_PRIORITY
2304 " HAS_CONTEXT_PRIORITY"
2305#endif
2306#ifdef NEVER_DEFAULT_TO_ASYNC_MODE
2307 " NEVER_DEFAULT_TO_ASYNC_MODE"
2308#endif
2309#ifdef TARGET_DISABLE_TRIPLE_BUFFERING
2310 " TARGET_DISABLE_TRIPLE_BUFFERING"
2311#endif
2312 "]";
2313 result.append(config);
2314}
2315
Mathias Agopian74d211a2013-04-22 16:55:35 +02002316void SurfaceFlinger::dumpAllLocked(const Vector<String16>& args, size_t& index,
2317 String8& result) const
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002318{
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002319 bool colorize = false;
2320 if (index < args.size()
2321 && (args[index] == String16("--color"))) {
2322 colorize = true;
2323 index++;
2324 }
2325
2326 Colorizer colorizer(colorize);
2327
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002328 // figure out if we're stuck somewhere
2329 const nsecs_t now = systemTime();
2330 const nsecs_t inSwapBuffers(mDebugInSwapBuffers);
2331 const nsecs_t inTransaction(mDebugInTransaction);
2332 nsecs_t inSwapBuffersDuration = (inSwapBuffers) ? now-inSwapBuffers : 0;
2333 nsecs_t inTransactionDuration = (inTransaction) ? now-inTransaction : 0;
2334
2335 /*
Andy McFadden4803b742012-09-24 19:07:20 -07002336 * Dump library configuration.
2337 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002338
2339 colorizer.bold(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002340 result.append("Build configuration:");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002341 colorizer.reset(result);
Andy McFadden4803b742012-09-24 19:07:20 -07002342 appendSfConfigString(result);
2343 appendUiConfigString(result);
2344 appendGuiConfigString(result);
2345 result.append("\n");
2346
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002347 colorizer.bold(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002348 result.append("Sync configuration: ");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002349 colorizer.reset(result);
Mathias Agopianca088332013-03-28 17:44:13 -07002350 result.append(SyncFeatures::getInstance().toString());
2351 result.append("\n");
2352
Andy McFadden4803b742012-09-24 19:07:20 -07002353 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002354 * Dump the visible layer list
2355 */
2356 const LayerVector& currentLayers = mCurrentState.layersSortedByZ;
2357 const size_t count = currentLayers.size();
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002358 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002359 result.appendFormat("Visible layers (count = %zu)\n", count);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002360 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002361 for (size_t i=0 ; i<count ; i++) {
Mathias Agopian13127d82013-03-05 17:47:11 -08002362 const sp<Layer>& layer(currentLayers[i]);
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002363 layer->dump(result, colorizer);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002364 }
2365
2366 /*
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002367 * Dump Display state
2368 */
2369
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002370 colorizer.bold(result);
Greg Hackmann86efcc02014-03-07 12:44:02 -08002371 result.appendFormat("Displays (%zu entries)\n", mDisplays.size());
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002372 colorizer.reset(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002373 for (size_t dpy=0 ; dpy<mDisplays.size() ; dpy++) {
2374 const sp<const DisplayDevice>& hw(mDisplays[dpy]);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002375 hw->dump(result);
Mathias Agopian5f20e2d2012-08-10 18:50:38 -07002376 }
2377
2378 /*
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002379 * Dump SurfaceFlinger global state
2380 */
2381
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002382 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002383 result.append("SurfaceFlinger global state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002384 colorizer.reset(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002385
Mathias Agopian888c8222012-08-04 21:10:38 -07002386 HWComposer& hwc(getHwComposer());
Mathias Agopian42977342012-08-05 00:40:46 -07002387 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
Mathias Agopianca088332013-03-28 17:44:13 -07002388
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002389 colorizer.bold(result);
2390 result.appendFormat("EGL implementation : %s\n",
2391 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_VERSION));
2392 colorizer.reset(result);
2393 result.appendFormat("%s\n",
Mathias Agopianca088332013-03-28 17:44:13 -07002394 eglQueryStringImplementationANDROID(mEGLDisplay, EGL_EXTENSIONS));
Mathias Agopianca088332013-03-28 17:44:13 -07002395
Mathias Agopian875d8e12013-06-07 15:35:48 -07002396 mRenderEngine->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002397
Mathias Agopian42977342012-08-05 00:40:46 -07002398 hw->undefinedRegion.dump(result, "undefinedRegion");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002399 result.appendFormat(" orientation=%d, canDraw=%d\n",
Mathias Agopian42977342012-08-05 00:40:46 -07002400 hw->getOrientation(), hw->canDraw());
Mathias Agopian74d211a2013-04-22 16:55:35 +02002401 result.appendFormat(
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002402 " last eglSwapBuffers() time: %f us\n"
2403 " last transaction time : %f us\n"
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002404 " transaction-flags : %08x\n"
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002405 " refresh-rate : %f fps\n"
2406 " x-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002407 " y-dpi : %f\n"
Mathias Agopianed985572013-03-22 00:24:39 -07002408 " gpu_to_cpu_unsupported : %d\n"
2409 ,
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002410 mLastSwapBufferTime/1000.0,
2411 mLastTransactionTime/1000.0,
Mathias Agopianc95dbdc2012-02-05 00:19:27 -08002412 mTransactionFlags,
Andy McFaddenb0d1dd32012-09-10 14:08:09 -07002413 1e9 / hwc.getRefreshPeriod(HWC_DISPLAY_PRIMARY),
2414 hwc.getDpiX(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002415 hwc.getDpiY(HWC_DISPLAY_PRIMARY),
Mathias Agopianed985572013-03-22 00:24:39 -07002416 !mGpuToCpuSupported);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002417
Mathias Agopian74d211a2013-04-22 16:55:35 +02002418 result.appendFormat(" eglSwapBuffers time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002419 inSwapBuffersDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002420
Mathias Agopian74d211a2013-04-22 16:55:35 +02002421 result.appendFormat(" transaction time: %f us\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002422 inTransactionDuration/1000.0);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002423
2424 /*
2425 * VSYNC state
2426 */
Mathias Agopian74d211a2013-04-22 16:55:35 +02002427 mEventThread->dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002428
2429 /*
2430 * Dump HWComposer state
2431 */
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002432 colorizer.bold(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002433 result.append("h/w composer state:\n");
Mathias Agopian3e25fd82013-04-22 17:52:16 +02002434 colorizer.reset(result);
Mathias Agopian74d211a2013-04-22 16:55:35 +02002435 result.appendFormat(" h/w composer %s and %s\n",
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002436 hwc.initCheck()==NO_ERROR ? "present" : "not present",
Alan Viverette9c5a3332013-09-12 20:04:35 -07002437 (mDebugDisableHWC || mDebugRegion || mDaltonize
2438 || mHasColorMatrix) ? "disabled" : "enabled");
Mathias Agopian74d211a2013-04-22 16:55:35 +02002439 hwc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002440
2441 /*
2442 * Dump gralloc state
2443 */
2444 const GraphicBufferAllocator& alloc(GraphicBufferAllocator::get());
2445 alloc.dump(result);
Mathias Agopian82d7ab62012-01-19 18:34:40 -08002446}
2447
Mathias Agopian13127d82013-03-05 17:47:11 -08002448const Vector< sp<Layer> >&
Jesse Hall48bc05b2013-03-21 14:06:52 -07002449SurfaceFlinger::getLayerSortedByZForHwcDisplay(int id) {
Mathias Agopiandb9b41f2012-10-15 16:51:41 -07002450 // Note: mStateLock is held here
Jesse Hall48bc05b2013-03-21 14:06:52 -07002451 wp<IBinder> dpy;
2452 for (size_t i=0 ; i<mDisplays.size() ; i++) {
2453 if (mDisplays.valueAt(i)->getHwcDisplayId() == id) {
2454 dpy = mDisplays.keyAt(i);
2455 break;
2456 }
2457 }
2458 if (dpy == NULL) {
2459 ALOGE("getLayerSortedByZForHwcDisplay: invalid hwc display id %d", id);
2460 // Just use the primary display so we have something to return
2461 dpy = getBuiltInDisplay(DisplayDevice::DISPLAY_PRIMARY);
2462 }
2463 return getDisplayDevice(dpy)->getVisibleLayersSortedByZ();
Mathias Agopiancb558572012-10-04 15:58:54 -07002464}
2465
Keun young Park63f165f2012-08-31 10:53:36 -07002466bool SurfaceFlinger::startDdmConnection()
2467{
2468 void* libddmconnection_dso =
2469 dlopen("libsurfaceflinger_ddmconnection.so", RTLD_NOW);
2470 if (!libddmconnection_dso) {
2471 return false;
2472 }
2473 void (*DdmConnection_start)(const char* name);
2474 DdmConnection_start =
2475 (typeof DdmConnection_start)dlsym(libddmconnection_dso, "DdmConnection_start");
2476 if (!DdmConnection_start) {
2477 dlclose(libddmconnection_dso);
2478 return false;
2479 }
2480 (*DdmConnection_start)(getServiceName());
2481 return true;
2482}
2483
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002484status_t SurfaceFlinger::onTransact(
2485 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
2486{
2487 switch (code) {
2488 case CREATE_CONNECTION:
Mathias Agopian041a0752013-03-15 18:31:56 -07002489 case CREATE_DISPLAY:
Mathias Agopian698c0872011-06-28 19:09:31 -07002490 case SET_TRANSACTION_STATE:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002491 case BOOT_FINISHED:
Colin Cross8e533062012-06-07 13:17:52 -07002492 case BLANK:
2493 case UNBLANK:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002494 {
2495 // codes that require permission check
2496 IPCThreadState* ipc = IPCThreadState::self();
2497 const int pid = ipc->getCallingPid();
Mathias Agopiana1ecca92009-05-21 19:21:59 -07002498 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002499 if ((uid != AID_GRAPHICS) &&
2500 !PermissionCache::checkPermission(sAccessSurfaceFlinger, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002501 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002502 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
2503 return PERMISSION_DENIED;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002504 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002505 break;
2506 }
2507 case CAPTURE_SCREEN:
2508 {
2509 // codes that require permission check
2510 IPCThreadState* ipc = IPCThreadState::self();
2511 const int pid = ipc->getCallingPid();
2512 const int uid = ipc->getCallingUid();
Mathias Agopian99b49842011-06-27 16:05:52 -07002513 if ((uid != AID_GRAPHICS) &&
2514 !PermissionCache::checkPermission(sReadFramebuffer, pid, uid)) {
Steve Blocke6f43dd2012-01-06 19:20:56 +00002515 ALOGE("Permission Denial: "
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002516 "can't read framebuffer pid=%d, uid=%d", pid, uid);
2517 return PERMISSION_DENIED;
2518 }
2519 break;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002520 }
2521 }
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002522
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002523 status_t err = BnSurfaceComposer::onTransact(code, data, reply, flags);
2524 if (err == UNKNOWN_TRANSACTION || err == PERMISSION_DENIED) {
Mathias Agopianb8a55602009-06-26 19:06:36 -07002525 CHECK_INTERFACE(ISurfaceComposer, data, reply);
Glenn Kasten99ed2242011-12-15 09:51:17 -08002526 if (CC_UNLIKELY(!PermissionCache::checkCallingPermission(sHardwareTest))) {
Mathias Agopian375f5632009-06-15 18:24:59 -07002527 IPCThreadState* ipc = IPCThreadState::self();
2528 const int pid = ipc->getCallingPid();
2529 const int uid = ipc->getCallingUid();
Steve Blocke6f43dd2012-01-06 19:20:56 +00002530 ALOGE("Permission Denial: "
Mathias Agopian375f5632009-06-15 18:24:59 -07002531 "can't access SurfaceFlinger pid=%d, uid=%d", pid, uid);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002532 return PERMISSION_DENIED;
2533 }
2534 int n;
2535 switch (code) {
Mathias Agopian01b76682009-04-16 20:04:08 -07002536 case 1000: // SHOW_CPU, NOT SUPPORTED ANYMORE
Mathias Agopian35b48d12010-09-13 22:57:58 -07002537 case 1001: // SHOW_FPS, NOT SUPPORTED ANYMORE
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002538 return NO_ERROR;
2539 case 1002: // SHOW_UPDATES
2540 n = data.readInt32();
2541 mDebugRegion = n ? n : (mDebugRegion ? 0 : 1);
Mathias Agopian53331da2011-08-22 21:44:41 -07002542 invalidateHwcGeometry();
2543 repaintEverything();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002544 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002545 case 1004:{ // repaint everything
Mathias Agopian53331da2011-08-22 21:44:41 -07002546 repaintEverything();
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002547 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002548 }
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002549 case 1005:{ // force transaction
Mathias Agopiane57f2922012-08-09 16:29:12 -07002550 setTransactionFlags(
2551 eTransactionNeeded|
2552 eDisplayTransactionNeeded|
2553 eTraversalNeeded);
Mathias Agopiancbb288b2009-09-07 16:32:45 -07002554 return NO_ERROR;
2555 }
Mathias Agopian4d143ee2012-02-23 20:05:39 -08002556 case 1006:{ // send empty update
2557 signalRefresh();
2558 return NO_ERROR;
2559 }
Mathias Agopian53331da2011-08-22 21:44:41 -07002560 case 1008: // toggle use of hw composer
2561 n = data.readInt32();
2562 mDebugDisableHWC = n ? 1 : 0;
2563 invalidateHwcGeometry();
2564 repaintEverything();
2565 return NO_ERROR;
Mathias Agopiana4583642011-08-23 18:03:18 -07002566 case 1009: // toggle use of transform hint
2567 n = data.readInt32();
2568 mDebugDisableTransformHint = n ? 1 : 0;
2569 invalidateHwcGeometry();
2570 repaintEverything();
2571 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002572 case 1010: // interrogate.
Mathias Agopian01b76682009-04-16 20:04:08 -07002573 reply->writeInt32(0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002574 reply->writeInt32(0);
2575 reply->writeInt32(mDebugRegion);
Mathias Agopianb9494d52012-04-18 02:28:45 -07002576 reply->writeInt32(0);
Dianne Hackborn12839be2012-02-06 21:21:05 -08002577 reply->writeInt32(mDebugDisableHWC);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002578 return NO_ERROR;
2579 case 1013: {
2580 Mutex::Autolock _l(mStateLock);
Mathias Agopian42977342012-08-05 00:40:46 -07002581 sp<const DisplayDevice> hw(getDefaultDisplayDevice());
2582 reply->writeInt32(hw->getPageFlipCount());
Mathias Agopianff2ed702013-09-01 21:36:12 -07002583 return NO_ERROR;
2584 }
2585 case 1014: {
2586 // daltonize
2587 n = data.readInt32();
2588 switch (n % 10) {
2589 case 1: mDaltonizer.setType(Daltonizer::protanomaly); break;
2590 case 2: mDaltonizer.setType(Daltonizer::deuteranomaly); break;
2591 case 3: mDaltonizer.setType(Daltonizer::tritanomaly); break;
2592 }
2593 if (n >= 10) {
2594 mDaltonizer.setMode(Daltonizer::correction);
2595 } else {
2596 mDaltonizer.setMode(Daltonizer::simulation);
2597 }
2598 mDaltonize = n > 0;
2599 invalidateHwcGeometry();
2600 repaintEverything();
Alan Viverette9c5a3332013-09-12 20:04:35 -07002601 return NO_ERROR;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002602 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002603 case 1015: {
2604 // apply a color matrix
2605 n = data.readInt32();
2606 mHasColorMatrix = n ? 1 : 0;
2607 if (n) {
2608 // color matrix is sent as mat3 matrix followed by vec3
2609 // offset, then packed into a mat4 where the last row is
2610 // the offset and extra values are 0
Alan Viverette794c5ba2013-10-03 16:40:52 -07002611 for (size_t i = 0 ; i < 4; i++) {
2612 for (size_t j = 0; j < 4; j++) {
2613 mColorMatrix[i][j] = data.readFloat();
2614 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002615 }
Alan Viverette9c5a3332013-09-12 20:04:35 -07002616 } else {
2617 mColorMatrix = mat4();
2618 }
2619 invalidateHwcGeometry();
2620 repaintEverything();
2621 return NO_ERROR;
2622 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08002623 }
2624 }
2625 return err;
2626}
2627
Mathias Agopian53331da2011-08-22 21:44:41 -07002628void SurfaceFlinger::repaintEverything() {
Mathias Agopian87baae12012-07-31 12:38:26 -07002629 android_atomic_or(1, &mRepaintEverything);
Mathias Agopian99ce5cd2012-01-31 18:24:27 -08002630 signalTransaction();
Mathias Agopian53331da2011-08-22 21:44:41 -07002631}
2632
Mathias Agopian59119e62010-10-11 12:37:43 -07002633// ---------------------------------------------------------------------------
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002634// Capture screen into an IGraphiBufferProducer
Mathias Agopian9daa5c92010-10-12 16:05:48 -07002635// ---------------------------------------------------------------------------
2636
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002637/* The code below is here to handle b/8734824
2638 *
2639 * We create a IGraphicBufferProducer wrapper that forwards all calls
2640 * to the calling binder thread, where they are executed. This allows
2641 * the calling thread to be reused (on the other side) and not
2642 * depend on having "enough" binder threads to handle the requests.
2643 *
2644 */
2645
2646class GraphicProducerWrapper : public BBinder, public MessageHandler {
2647 sp<IGraphicBufferProducer> impl;
2648 sp<Looper> looper;
2649 status_t result;
2650 bool exitPending;
2651 bool exitRequested;
2652 mutable Barrier barrier;
2653 volatile int32_t memoryBarrier;
2654 uint32_t code;
2655 Parcel const* data;
2656 Parcel* reply;
2657
2658 enum {
2659 MSG_API_CALL,
2660 MSG_EXIT
2661 };
2662
2663 /*
2664 * this is called by our "fake" BpGraphicBufferProducer. We package the
2665 * data and reply Parcel and forward them to the calling thread.
2666 */
2667 virtual status_t transact(uint32_t code,
Dan Stozac7014012014-02-14 15:03:43 -08002668 const Parcel& data, Parcel* reply, uint32_t /* flags */) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002669 this->code = code;
2670 this->data = &data;
2671 this->reply = reply;
2672 android_atomic_acquire_store(0, &memoryBarrier);
2673 if (exitPending) {
2674 // if we've exited, we run the message synchronously right here
2675 handleMessage(Message(MSG_API_CALL));
2676 } else {
2677 barrier.close();
2678 looper->sendMessage(this, Message(MSG_API_CALL));
2679 barrier.wait();
2680 }
2681 return NO_ERROR;
2682 }
2683
2684 /*
2685 * here we run on the binder calling thread. All we've got to do is
2686 * call the real BpGraphicBufferProducer.
2687 */
2688 virtual void handleMessage(const Message& message) {
2689 android_atomic_release_load(&memoryBarrier);
2690 if (message.what == MSG_API_CALL) {
2691 impl->asBinder()->transact(code, data[0], reply);
2692 barrier.open();
2693 } else if (message.what == MSG_EXIT) {
2694 exitRequested = true;
2695 }
2696 }
2697
2698public:
2699 GraphicProducerWrapper(const sp<IGraphicBufferProducer>& impl) :
2700 impl(impl), looper(new Looper(true)), result(NO_ERROR),
2701 exitPending(false), exitRequested(false) {
2702 }
2703
2704 status_t waitForResponse() {
2705 do {
2706 looper->pollOnce(-1);
2707 } while (!exitRequested);
2708 return result;
2709 }
2710
2711 void exit(status_t result) {
Mike J. Chenaaff4ef2013-07-30 10:19:24 -07002712 this->result = result;
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002713 exitPending = true;
2714 looper->sendMessage(this, Message(MSG_EXIT));
2715 }
2716};
2717
2718
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002719status_t SurfaceFlinger::captureScreen(const sp<IBinder>& display,
2720 const sp<IGraphicBufferProducer>& producer,
2721 uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002722 uint32_t minLayerZ, uint32_t maxLayerZ,
2723 bool useIdentityTransform) {
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002724
2725 if (CC_UNLIKELY(display == 0))
2726 return BAD_VALUE;
2727
2728 if (CC_UNLIKELY(producer == 0))
2729 return BAD_VALUE;
2730
Mathias Agopian5ff5a842013-08-13 15:55:43 -07002731 // if we have secure windows on this display, never allow the screen capture
2732 // unless the producer interface is local (i.e.: we can take a screenshot for
2733 // ourselves).
2734 if (!producer->asBinder()->localBinder()) {
2735 Mutex::Autolock _l(mStateLock);
2736 sp<const DisplayDevice> hw(getDisplayDevice(display));
2737 if (hw->getSecureLayerVisible()) {
2738 ALOGW("FB is protected: PERMISSION_DENIED");
2739 return PERMISSION_DENIED;
2740 }
2741 }
2742
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002743 class MessageCaptureScreen : public MessageBase {
2744 SurfaceFlinger* flinger;
2745 sp<IBinder> display;
2746 sp<IGraphicBufferProducer> producer;
2747 uint32_t reqWidth, reqHeight;
2748 uint32_t minLayerZ,maxLayerZ;
Dan Stozac7014012014-02-14 15:03:43 -08002749 bool useIdentityTransform;
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002750 status_t result;
2751 public:
2752 MessageCaptureScreen(SurfaceFlinger* flinger,
2753 const sp<IBinder>& display,
2754 const sp<IGraphicBufferProducer>& producer,
2755 uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002756 uint32_t minLayerZ, uint32_t maxLayerZ,
2757 bool useIdentityTransform)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002758 : flinger(flinger), display(display), producer(producer),
2759 reqWidth(reqWidth), reqHeight(reqHeight),
2760 minLayerZ(minLayerZ), maxLayerZ(maxLayerZ),
Dan Stozac7014012014-02-14 15:03:43 -08002761 useIdentityTransform(useIdentityTransform),
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002762 result(PERMISSION_DENIED)
2763 {
2764 }
2765 status_t getResult() const {
2766 return result;
2767 }
2768 virtual bool handler() {
2769 Mutex::Autolock _l(flinger->mStateLock);
2770 sp<const DisplayDevice> hw(flinger->getDisplayDevice(display));
Dan Stozac7014012014-02-14 15:03:43 -08002771 result = flinger->captureScreenImplLocked(hw, producer,
2772 reqWidth, reqHeight, minLayerZ, maxLayerZ,
2773 useIdentityTransform);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002774 static_cast<GraphicProducerWrapper*>(producer->asBinder().get())->exit(result);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002775 return true;
2776 }
2777 };
2778
Mathias Agopian9eb1f052013-04-10 16:27:17 -07002779 // make sure to process transactions before screenshots -- a transaction
2780 // might already be pending but scheduled for VSYNC; this guarantees we
2781 // will handle it before the screenshot. When VSYNC finally arrives
2782 // the scheduled transaction will be a no-op. If no transactions are
2783 // scheduled at this time, this will end-up being a no-op as well.
2784 mEventQueue.invalidateTransactionNow();
2785
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002786 // this creates a "fake" BBinder which will serve as a "fake" remote
2787 // binder to receive the marshaled calls and forward them to the
2788 // real remote (a BpGraphicBufferProducer)
2789 sp<GraphicProducerWrapper> wrapper = new GraphicProducerWrapper(producer);
2790
2791 // the asInterface() call below creates our "fake" BpGraphicBufferProducer
2792 // which does the marshaling work forwards to our "fake remote" above.
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002793 sp<MessageBase> msg = new MessageCaptureScreen(this,
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002794 display, IGraphicBufferProducer::asInterface( wrapper ),
Dan Stozac7014012014-02-14 15:03:43 -08002795 reqWidth, reqHeight, minLayerZ, maxLayerZ, useIdentityTransform);
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002796
2797 status_t res = postMessageAsync(msg);
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002798 if (res == NO_ERROR) {
Mathias Agopian2ed0fe52013-07-08 17:09:41 -07002799 res = wrapper->waitForResponse();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002800 }
2801 return res;
2802}
2803
Mathias Agopian180f10d2013-04-10 22:55:41 -07002804
2805void SurfaceFlinger::renderScreenImplLocked(
2806 const sp<const DisplayDevice>& hw,
2807 uint32_t reqWidth, uint32_t reqHeight,
2808 uint32_t minLayerZ, uint32_t maxLayerZ,
Dan Stozac7014012014-02-14 15:03:43 -08002809 bool yswap, bool useIdentityTransform)
Mathias Agopian180f10d2013-04-10 22:55:41 -07002810{
2811 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -07002812 RenderEngine& engine(getRenderEngine());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002813
2814 // get screen geometry
2815 const uint32_t hw_w = hw->getWidth();
2816 const uint32_t hw_h = hw->getHeight();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002817 const bool filtering = reqWidth != hw_w || reqWidth != hw_h;
2818
2819 // make sure to clear all GL error flags
Mathias Agopian3f844832013-08-07 21:24:32 -07002820 engine.checkErrors();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002821
2822 // set-up our viewport
Mathias Agopian3f844832013-08-07 21:24:32 -07002823 engine.setViewportAndProjection(reqWidth, reqHeight, hw_w, hw_h, yswap);
2824 engine.disableTexturing();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002825
2826 // redraw the screen entirely...
Mathias Agopian3f844832013-08-07 21:24:32 -07002827 engine.clearWithColor(0, 0, 0, 1);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002828
2829 const LayerVector& layers( mDrawingState.layersSortedByZ );
2830 const size_t count = layers.size();
2831 for (size_t i=0 ; i<count ; ++i) {
2832 const sp<Layer>& layer(layers[i]);
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07002833 const Layer::State& state(layer->getDrawingState());
Mathias Agopian180f10d2013-04-10 22:55:41 -07002834 if (state.layerStack == hw->getLayerStack()) {
2835 if (state.z >= minLayerZ && state.z <= maxLayerZ) {
2836 if (layer->isVisible()) {
2837 if (filtering) layer->setFiltering(true);
Dan Stozac7014012014-02-14 15:03:43 -08002838 layer->draw(hw, useIdentityTransform);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002839 if (filtering) layer->setFiltering(false);
2840 }
2841 }
2842 }
2843 }
2844
2845 // compositionComplete is needed for older driver
2846 hw->compositionComplete();
Mathias Agopian931bda12013-08-28 18:11:46 -07002847 hw->setViewportAndProjection();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002848}
2849
2850
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002851status_t SurfaceFlinger::captureScreenImplLocked(
2852 const sp<const DisplayDevice>& hw,
2853 const sp<IGraphicBufferProducer>& producer,
2854 uint32_t reqWidth, uint32_t reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002855 uint32_t minLayerZ, uint32_t maxLayerZ,
2856 bool useIdentityTransform)
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002857{
2858 ATRACE_CALL();
2859
Mathias Agopian180f10d2013-04-10 22:55:41 -07002860 // get screen geometry
2861 const uint32_t hw_w = hw->getWidth();
2862 const uint32_t hw_h = hw->getHeight();
Mathias Agopian2a9fc492013-03-01 13:42:57 -08002863
Mathias Agopian180f10d2013-04-10 22:55:41 -07002864 if ((reqWidth > hw_w) || (reqHeight > hw_h)) {
2865 ALOGE("size mismatch (%d, %d) > (%d, %d)",
2866 reqWidth, reqHeight, hw_w, hw_h);
2867 return BAD_VALUE;
2868 }
Mathias Agopianc1d1b0d2011-01-16 14:05:02 -08002869
Mathias Agopian180f10d2013-04-10 22:55:41 -07002870 reqWidth = (!reqWidth) ? hw_w : reqWidth;
2871 reqHeight = (!reqHeight) ? hw_h : reqHeight;
2872
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002873 // create a surface (because we're a producer, and we need to
2874 // dequeue/queue a buffer)
Jesse Hall83cafff2013-09-16 15:24:53 -07002875 sp<Surface> sur = new Surface(producer, false);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002876 ANativeWindow* window = sur.get();
Mathias Agopian180f10d2013-04-10 22:55:41 -07002877
2878 status_t result = NO_ERROR;
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002879 if (native_window_api_connect(window, NATIVE_WINDOW_API_EGL) == NO_ERROR) {
Mathias Agopian3ca76f42013-08-06 16:07:33 -07002880 uint32_t usage = GRALLOC_USAGE_SW_READ_OFTEN | GRALLOC_USAGE_SW_WRITE_OFTEN |
2881 GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Mathias Agopian180f10d2013-04-10 22:55:41 -07002882
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002883 int err = 0;
2884 err = native_window_set_buffers_dimensions(window, reqWidth, reqHeight);
Mathias Agopian4ceff3d2013-08-21 15:23:15 -07002885 err |= native_window_set_scaling_mode(window, NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002886 err |= native_window_set_buffers_format(window, HAL_PIXEL_FORMAT_RGBA_8888);
2887 err |= native_window_set_usage(window, usage);
Mathias Agopian180f10d2013-04-10 22:55:41 -07002888
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002889 if (err == NO_ERROR) {
2890 ANativeWindowBuffer* buffer;
2891 /* TODO: Once we have the sync framework everywhere this can use
2892 * server-side waits on the fence that dequeueBuffer returns.
2893 */
2894 result = native_window_dequeue_buffer_and_wait(window, &buffer);
2895 if (result == NO_ERROR) {
2896 // create an EGLImage from the buffer so we can later
2897 // turn it into a texture
2898 EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT,
2899 EGL_NATIVE_BUFFER_ANDROID, buffer, NULL);
2900 if (image != EGL_NO_IMAGE_KHR) {
Mathias Agopian3f844832013-08-07 21:24:32 -07002901 // this binds the given EGLImage as a framebuffer for the
2902 // duration of this scope.
2903 RenderEngine::BindImageAsFramebuffer imageBond(getRenderEngine(), image);
2904 if (imageBond.getStatus() == NO_ERROR) {
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002905 // this will in fact render into our dequeued buffer
2906 // via an FBO, which means we didn't have to create
2907 // an EGLSurface and therefore we're not
2908 // dependent on the context's EGLConfig.
2909 renderScreenImplLocked(hw, reqWidth, reqHeight,
Dan Stozac7014012014-02-14 15:03:43 -08002910 minLayerZ, maxLayerZ, true, useIdentityTransform);
Mathias Agopiand5556842013-09-19 17:08:37 -07002911
Andy McFadden2d8d1202013-10-09 16:38:02 -07002912 // Create a sync point and wait on it, so we know the buffer is
2913 // ready before we pass it along. We can't trivially call glFlush(),
2914 // so we use a wait flag instead.
2915 // TODO: pass a sync fd to queueBuffer() and let the consumer wait.
2916 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
2917 if (sync != EGL_NO_SYNC_KHR) {
2918 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync,
2919 EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, 2000000000 /*2 sec*/);
2920 EGLint eglErr = eglGetError();
2921 eglDestroySyncKHR(mEGLDisplay, sync);
2922 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
2923 ALOGW("captureScreen: fence wait timed out");
2924 } else {
2925 ALOGW_IF(eglErr != EGL_SUCCESS,
2926 "captureScreen: error waiting on EGL fence: %#x", eglErr);
2927 }
2928 } else {
2929 ALOGW("captureScreen: error creating EGL fence: %#x", eglGetError());
2930 // not fatal
2931 }
2932
Mathias Agopiand5556842013-09-19 17:08:37 -07002933 if (DEBUG_SCREENSHOTS) {
2934 uint32_t* pixels = new uint32_t[reqWidth*reqHeight];
2935 getRenderEngine().readPixels(0, 0, reqWidth, reqHeight, pixels);
2936 checkScreenshot(reqWidth, reqHeight, reqWidth, pixels,
2937 hw, minLayerZ, maxLayerZ);
2938 delete [] pixels;
2939 }
2940
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002941 } else {
2942 ALOGE("got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot");
2943 result = INVALID_OPERATION;
2944 }
Mathias Agopian0aea53f2013-04-24 19:03:08 +02002945 // destroy our image
2946 eglDestroyImageKHR(mEGLDisplay, image);
2947 } else {
2948 result = BAD_VALUE;
2949 }
2950 window->queueBuffer(window, buffer, -1);
2951 }
2952 } else {
2953 result = BAD_VALUE;
2954 }
2955 native_window_api_disconnect(window, NATIVE_WINDOW_API_EGL);
2956 }
Mathias Agopian180f10d2013-04-10 22:55:41 -07002957
Mathias Agopian74c40c02010-09-29 13:02:36 -07002958 return result;
2959}
2960
Mathias Agopiand5556842013-09-19 17:08:37 -07002961void SurfaceFlinger::checkScreenshot(size_t w, size_t s, size_t h, void const* vaddr,
2962 const sp<const DisplayDevice>& hw, uint32_t minLayerZ, uint32_t maxLayerZ) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07002963 if (DEBUG_SCREENSHOTS) {
Mathias Agopiand5556842013-09-19 17:08:37 -07002964 for (size_t y=0 ; y<h ; y++) {
2965 uint32_t const * p = (uint32_t const *)vaddr + y*s;
2966 for (size_t x=0 ; x<w ; x++) {
Mathias Agopianfee2b462013-07-03 12:34:01 -07002967 if (p[x] != 0xFF000000) return;
2968 }
2969 }
2970 ALOGE("*** we just took a black screenshot ***\n"
2971 "requested minz=%d, maxz=%d, layerStack=%d",
2972 minLayerZ, maxLayerZ, hw->getLayerStack());
2973 const LayerVector& layers( mDrawingState.layersSortedByZ );
2974 const size_t count = layers.size();
2975 for (size_t i=0 ; i<count ; ++i) {
2976 const sp<Layer>& layer(layers[i]);
2977 const Layer::State& state(layer->getDrawingState());
2978 const bool visible = (state.layerStack == hw->getLayerStack())
2979 && (state.z >= minLayerZ && state.z <= maxLayerZ)
2980 && (layer->isVisible());
Greg Hackmann86efcc02014-03-07 12:44:02 -08002981 ALOGE("%c index=%zu, name=%s, layerStack=%d, z=%d, visible=%d, flags=%x, alpha=%x",
Mathias Agopianfee2b462013-07-03 12:34:01 -07002982 visible ? '+' : '-',
2983 i, layer->getName().string(), state.layerStack, state.z,
2984 layer->isVisible(), state.flags, state.alpha);
2985 }
2986 }
2987}
2988
Mathias Agopian1b0b30d2010-09-24 11:26:58 -07002989// ---------------------------------------------------------------------------
2990
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002991SurfaceFlinger::LayerVector::LayerVector() {
2992}
2993
2994SurfaceFlinger::LayerVector::LayerVector(const LayerVector& rhs)
Mathias Agopian13127d82013-03-05 17:47:11 -08002995 : SortedVector<sp<Layer> >(rhs) {
Mathias Agopian921e6ac2012-07-23 23:11:29 -07002996}
2997
2998int SurfaceFlinger::LayerVector::do_compare(const void* lhs,
2999 const void* rhs) const
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003000{
Mathias Agopianbe246f82012-07-31 22:59:38 -07003001 // sort layers per layer-stack, then by z-order and finally by sequence
Mathias Agopian13127d82013-03-05 17:47:11 -08003002 const sp<Layer>& l(*reinterpret_cast<const sp<Layer>*>(lhs));
3003 const sp<Layer>& r(*reinterpret_cast<const sp<Layer>*>(rhs));
Mathias Agopianbe246f82012-07-31 22:59:38 -07003004
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003005 uint32_t ls = l->getCurrentState().layerStack;
3006 uint32_t rs = r->getCurrentState().layerStack;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003007 if (ls != rs)
3008 return ls - rs;
3009
Mathias Agopian1eae0ee2013-06-05 16:59:15 -07003010 uint32_t lz = l->getCurrentState().z;
3011 uint32_t rz = r->getCurrentState().z;
Mathias Agopianbe246f82012-07-31 22:59:38 -07003012 if (lz != rz)
3013 return lz - rz;
3014
3015 return l->sequence - r->sequence;
Mathias Agopian921e6ac2012-07-23 23:11:29 -07003016}
3017
3018// ---------------------------------------------------------------------------
3019
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003020SurfaceFlinger::DisplayDeviceState::DisplayDeviceState()
3021 : type(DisplayDevice::DISPLAY_ID_INVALID) {
Mathias Agopiane57f2922012-08-09 16:29:12 -07003022}
3023
Mathias Agopian3ee454a2012-08-27 16:28:24 -07003024SurfaceFlinger::DisplayDeviceState::DisplayDeviceState(DisplayDevice::DisplayType type)
Jesse Hall01e29052013-02-19 16:13:35 -08003025 : type(type), layerStack(DisplayDevice::NO_LAYER_STACK), orientation(0) {
Mathias Agopianda8d0a52012-09-04 15:05:38 -07003026 viewport.makeInvalid();
3027 frame.makeInvalid();
Mathias Agopianb7e930d2010-06-01 15:12:58 -07003028}
3029
3030// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003031
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003032}; // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -07003033
3034
3035#if defined(__gl_h_)
3036#error "don't include gl/gl.h in this file"
3037#endif
3038
3039#if defined(__gl2_h_)
3040#error "don't include gl2/gl2.h in this file"
3041#endif